#include "sysctl_overlay.hpp" #include #include #include #include using namespace sysctloverlay; namespace { std::size_t wire_bytes = 0; std::string quote(const std::string &value) { constexpr char digits[] = "0123456789abcdef"; std::string result = "\""; for (unsigned char byte : value) { if (byte == '"' || byte == '\\') { result.push_back('\\'); result.push_back(static_cast(byte)); } else if (byte < 32 || byte > 126) { result += "\\u00"; result.push_back(digits[byte >> 4]); result.push_back(digits[byte & 15]); } else { result.push_back(static_cast(byte)); } } return result + '"'; } std::string boolean(bool value) { return value ? "true" : "false"; } template std::string sequence(const Range &items, Encode encode) { std::string result = "["; bool first = true; for (const auto &item : items) { if (!first) { result.push_back(','); } first = false; result += encode(item); } return result + ']'; } std::string file_json(const File &file) { return "[" + quote(file.basename) + ',' + (file.text.has_value() ? quote(*file.text) : "null") + ']'; } std::string layers_json(const std::vector &layers) { return sequence(layers, [](const Layer &layer) { return sequence(layer, file_json); }); } std::string selected_json(const SelectedFile &selected) { const File &file = selected.file; return "[" + std::to_string(selected.layer) + ',' + quote(file.basename) + ',' + (file.text.has_value() ? quote(*file.text) : "null") + ']'; } std::string rule_json(const Rule &rule) { return "[" + std::to_string(rule.row) + ',' + std::to_string(static_cast(rule.kind)) + ',' + quote(rule.key) + ',' + quote(rule.value) + ',' + boolean(rule.ignore_error) + ',' + boolean(rule.pattern) + ']'; } std::string located_json(const LocatedRule &located) { return "[" + std::to_string(located.layer) + ',' + quote(located.basename) + ',' + rule_json(located.rule) + ']'; } std::string configuration_json(const Configuration &configuration) { return "[" + sequence(configuration.selected, selected_json) + ',' + sequence(configuration.rules, located_json) + ',' + sequence(configuration.excluded, quote) + ']'; } std::string entry_json(const Entry &entry) { return "[" + quote(entry.key) + ',' + quote(entry.value) + ',' + boolean(entry.ignore_error) + ',' + std::to_string(entry.layer) + ',' + quote(entry.basename) + ',' + std::to_string(entry.row) + ']'; } std::string plan_json(const Plan &plan) { return sequence(plan.entries, entry_json); } std::string token() { std::string text; if (!(std::cin >> text)) { throw std::runtime_error("missing token"); } return text; } std::size_t count(std::size_t maximum) { const std::string text = token(); std::size_t value = 0; if (text.empty()) { throw std::runtime_error("empty count"); } for (unsigned char byte : text) { if (byte < '0' || byte > '9' || value > maximum / 10) { throw std::runtime_error("count syntax"); } value = value * 10 + (byte - '0'); if (value > maximum) { throw std::runtime_error("count range"); } } return value; } unsigned nibble(unsigned char byte) { if (byte >= '0' && byte <= '9') return byte - '0'; if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10; if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10; throw std::runtime_error("hex digit"); } std::string read_text() { const std::string text = token(); if (text == "-") { return {}; } if (text.size() % 2 != 0 || text.size() / 2 > 1048576 - wire_bytes) { throw std::runtime_error("hex size"); } wire_bytes += text.size() / 2; std::string result; result.reserve(text.size() / 2); for (std::size_t at = 0; at < text.size(); at += 2) { const auto byte = nibble(text[at]) * 16 + nibble(text[at + 1]); result.push_back(static_cast(byte)); } return result; } void require(const char *wanted) { if (token() != wanted) { throw std::runtime_error("unexpected data row"); } } std::vector read_layers() { std::vector layers; const auto layer_count = count(4096); std::size_t total = 0; for (std::size_t index = 0; index < layer_count; ++index) { require("Y"); const auto file_count = count(4096); total += file_count; if (total > 4096) { throw std::runtime_error("wire file count"); } Layer layer; for (std::size_t file_index = 0; file_index < file_count; ++file_index) { require("F"); File file; file.basename = read_text(); const bool present = count(1) != 0; std::string text = read_text(); if (present) { file.text = std::move(text); } else if (!text.empty()) { throw std::runtime_error("null text payload"); } layer.push_back(std::move(file)); } layers.push_back(std::move(layer)); } return layers; } std::vector read_keys() { const auto size = count(4096); std::vector keys; for (std::size_t index = 0; index < size; ++index) { keys.push_back(read_text()); } return keys; } Rule dirty_rule() { Rule rule; rule.row = 77; rule.key = "old/key"; rule.value = "old_value"; rule.ignore_error = true; return rule; } Configuration dirty_configuration() { Configuration result; result.selected.push_back(SelectedFile{7, File{"old.conf", "old/key=old_value"}}); result.rules.push_back(LocatedRule{7, "old.conf", dirty_rule()}); result.excluded.push_back("old/key"); return result; } Plan dirty_plan() { Plan result; result.entries.push_back(Entry{"old/key", "old_value", true, 7, "old.conf", 77}); return result; } std::string result_row(const std::string &command, Error error, const std::string &value, bool unchanged) { return "[" + quote(command) + ',' + std::to_string(static_cast(error)) + ',' + value + ',' + boolean(unchanged) + ']'; } } int main() { try { std::vector layers; Configuration last_configuration; Configuration held_configuration; Plan last_plan; Plan held_plan; bool compiled = false; bool held = false; std::string command; while (std::cin >> command) { wire_bytes = 0; std::string row; if (command == "K" || command == "N" || command == "L") { const bool pattern = command == "N" ? count(1) != 0 : false; const std::string text = read_text(); const std::string original = text; if (command == "K") { const bool value = valid_kernel_key(text); row = "[\"K\"," + boolean(value) + ',' + boolean(text == original) + ']'; } else if (command == "N") { std::string output = "old_output"; const Error error = canonical_key(text, pattern, output); row = result_row(command, error, quote(output), text == original); } else { std::vector output{dirty_rule()}; const Error error = parse_text(text, output); row = result_row(command, error, sequence(output, rule_json), text == original); } } else if (command == "M") { const std::string pattern = read_text(); const std::string key = read_text(); const auto original_pattern = pattern; const auto original_key = key; bool output = true; const Error error = match_pattern(pattern, key, output); row = result_row(command, error, boolean(output), pattern == original_pattern && key == original_key); } else if (command == "C") { layers = read_layers(); const std::string original = layers_json(layers); last_configuration = dirty_configuration(); const Error error = compile_configuration(layers, last_configuration); compiled = error == Error::none; row = result_row(command, error, configuration_json(last_configuration), original == layers_json(layers)); } else if (command == "S") { const std::string original = layers_json(layers); auto output = dirty_configuration().selected; const Error error = select_files(layers, output); row = result_row(command, error, sequence(output, selected_json), original == layers_json(layers)); } else if (command == "P" || command == "R" || command == "J") { const auto keys = read_keys(); const std::string original_keys = sequence(keys, quote); last_plan = dirty_plan(); Error error; bool unchanged; if (command == "P") { const std::string original = layers_json(layers); error = make_plan(layers, keys, last_plan); unchanged = original == layers_json(layers); } else { if ((command == "R" && !compiled) || (command == "J" && !held)) { throw std::runtime_error("compiled input unavailable"); } const auto &configuration = command == "R" ? last_configuration : held_configuration; const std::string original = configuration_json(configuration); error = plan_configuration(configuration, keys, last_plan); unchanged = original == configuration_json(configuration); } row = result_row(command, error, plan_json(last_plan), unchanged && original_keys == sequence(keys, quote)); } else if (command == "H" || command == "Q") { if (command == "H") { if (!compiled) { throw std::runtime_error("compiled input unavailable"); } held_configuration = last_configuration; held_plan = last_plan; held = true; } else if (!held) { throw std::runtime_error("held input unavailable"); } row = "[" + quote(command) + ',' + configuration_json(held_configuration) + ',' + plan_json(held_plan) + ']'; } else { throw std::runtime_error("unknown command"); } std::cout << row << '\n'; } return std::cin.bad() ? 2 : 0; } catch (const std::exception &error) { std::cerr << error.what() << '\n'; return 2; } }