All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
2.9 KiB
C++
112 lines
2.9 KiB
C++
#include "sysctl_overlay.hpp"
|
|
#include <utility>
|
|
|
|
namespace sysctloverlay {
|
|
namespace {
|
|
|
|
bool component_byte(unsigned char byte, bool pattern)
|
|
{
|
|
const bool letter = (byte >= 'A' && byte <= 'Z') ||
|
|
(byte >= 'a' && byte <= 'z');
|
|
const bool digit = byte >= '0' && byte <= '9';
|
|
return letter || digit || byte == '_' || byte == '.' || byte == '-' ||
|
|
(pattern && (byte == '*' || byte == '?'));
|
|
}
|
|
|
|
bool path_value(const std::string &text, bool pattern)
|
|
{
|
|
if (text.empty() || text.size() > 256) {
|
|
return false;
|
|
}
|
|
std::size_t begin = 0;
|
|
for (;;) {
|
|
const auto slash = text.find('/', begin);
|
|
const auto end = slash == std::string::npos ? text.size() : slash;
|
|
if (end == begin) {
|
|
return false;
|
|
}
|
|
const auto component = text.substr(begin, end - begin);
|
|
if (component == "." || component == "..") {
|
|
return false;
|
|
}
|
|
for (unsigned char byte : component) {
|
|
if (!component_byte(byte, pattern)) {
|
|
return false;
|
|
}
|
|
}
|
|
if (slash == std::string::npos) {
|
|
return true;
|
|
}
|
|
begin = slash + 1;
|
|
}
|
|
}
|
|
|
|
bool full_match(const std::string &pattern, const std::string &key)
|
|
{
|
|
std::size_t token = 0;
|
|
std::size_t byte = 0;
|
|
std::size_t star = std::string::npos;
|
|
std::size_t restart = 0;
|
|
while (byte < key.size()) {
|
|
if (token < pattern.size() &&
|
|
(pattern[token] == '?' || pattern[token] == key[byte])) {
|
|
++token;
|
|
++byte;
|
|
} else if (token < pattern.size() && pattern[token] == '*') {
|
|
star = token++;
|
|
restart = byte;
|
|
} else if (star != std::string::npos) {
|
|
byte = ++restart;
|
|
token = star + 1;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
while (token < pattern.size() && pattern[token] == '*') {
|
|
++token;
|
|
}
|
|
return token == pattern.size();
|
|
}
|
|
|
|
}
|
|
|
|
bool valid_kernel_key(const std::string &key)
|
|
{
|
|
return path_value(key, false);
|
|
}
|
|
|
|
Error canonical_key(const std::string &raw, bool allow_pattern,
|
|
std::string &output)
|
|
{
|
|
output.clear();
|
|
std::string result = raw;
|
|
const auto separator = raw.find_first_of("./");
|
|
if (separator != std::string::npos && raw[separator] == '.') {
|
|
for (char &byte : result) {
|
|
if (byte == '.') {
|
|
byte = '/';
|
|
} else if (byte == '/') {
|
|
byte = '.';
|
|
}
|
|
}
|
|
}
|
|
if (!path_value(result, allow_pattern)) {
|
|
return Error::invalid;
|
|
}
|
|
output = std::move(result);
|
|
return Error::none;
|
|
}
|
|
|
|
Error match_pattern(const std::string &pattern, const std::string &key,
|
|
bool &output)
|
|
{
|
|
output = false;
|
|
if (!path_value(pattern, true) || !valid_kernel_key(key)) {
|
|
return Error::invalid;
|
|
}
|
|
output = full_match(pattern, key);
|
|
return Error::none;
|
|
}
|
|
|
|
}
|