#include "cache.hpp" namespace tile { std::vector tokenize(const std::string& line) { std::vector result; std::string current; for (char byte : line) { if (byte == ' ' || byte == '\t' || byte == '\r') { if (!current.empty()) { result.push_back(std::move(current)); current.clear(); } } else { current.push_back(byte); } } if (!current.empty()) { result.push_back(std::move(current)); } return result; } bool decimal(const std::string& token, unsigned long long maximum, unsigned long long& value) { if (token.empty()) { return false; } std::size_t index = 0; bool negative = false; if (token[index] == '+' || token[index] == '-') { negative = token[index] == '-'; ++index; } if (index == token.size()) { return false; } unsigned long long parsed = 0; bool overflow = false; for (; index < token.size(); ++index) { const unsigned char byte = static_cast(token[index]); if (byte < '0' || byte > '9') { return false; } const unsigned digit = byte - '0'; if (parsed > maximum / 10 || (parsed == maximum / 10 && digit > maximum % 10)) { overflow = true; } else if (!overflow) { parsed = parsed * 10 + digit; } } if (overflow || (negative && parsed != 0)) { return false; } value = parsed; return true; } bool name_valid(const std::string& token) { if (token.empty() || token.size() > 32) { return false; } for (unsigned char byte : token) { const bool lower = byte >= 'a' && byte <= 'z'; const bool upper = byte >= 'A' && byte <= 'Z'; const bool digit = byte >= '0' && byte <= '9'; const bool punctuation = byte == '_' || byte == '.' || byte == '-'; if (!lower && !upper && !digit && !punctuation) { return false; } } return true; } Numbers Service::execute(const std::string& line) { const auto tokens = tokenize(line); if (tokens.empty()) { return invalid(); } const std::string& command = tokens[0]; auto arity = [&](std::size_t wanted) { return tokens.size() == wanted + 1; }; auto number = [&](std::size_t position, unsigned long long maximum, unsigned long long& result) { return position < tokens.size() && decimal(tokens[position], maximum, result); }; auto key = [&](std::size_t position, Key& result) { if (position + 1 >= tokens.size()) { return false; } if (!name_valid(tokens[position]) || !name_valid(tokens[position + 1])) { return false; } result = {tokens[position], tokens[position + 1]}; return true; }; unsigned long long slot = 0; unsigned long long source = 0; unsigned long long destination = 0; unsigned long long length = 0; unsigned long long seed = 0; unsigned long long offset = 0; unsigned long long count = 0; unsigned long long value = 0; Key resource; if (command == "ACQUIRE") { if (!arity(5) || !number(1, 31, slot) || !key(2, resource) || !number(4, 4096, length) || !number(5, 255, seed)) { return invalid(); } return acquire(slot, resource, length, static_cast(seed)); } if (command == "LOOKUP") { if (!arity(3) || !number(1, 31, slot) || !key(2, resource)) { return invalid(); } return lookup(slot, resource); } if (command == "COPY" || command == "MOVE") { if (!arity(2) || !number(1, 31, source) || !number(2, 31, destination)) { return invalid(); } if (command == "COPY") { return copy(source, destination); } return move(source, destination); } if (command == "DROP" || command == "VIEW" || command == "OWNERS") { if (!arity(1) || !number(1, 31, slot)) { return invalid(); } if (command == "DROP") { return drop(slot); } if (command == "VIEW") { return view(slot); } return owners(slot); } if (command == "WRITE") { if (!arity(3) || !number(1, 31, slot) || !number(2, 4096, offset) || !number(3, 255, value)) { return invalid(); } return write(slot, offset, static_cast(value)); } if (command == "FILL") { if (!arity(4) || !number(1, 31, slot) || !number(2, 4096, offset) || !number(3, 4096, count) || !number(4, 255, value)) { return invalid(); } return fill(slot, offset, count, static_cast(value)); } if (command == "RESIZE") { if (!arity(3) || !number(1, 31, slot) || !number(2, 4096, length) || !number(3, 255, seed)) { return invalid(); } return resize(slot, length, static_cast(seed)); } if (command == "READ") { if (!arity(3) || !number(1, 31, slot) || !number(2, 4096, offset) || !number(3, 4096, count)) { return invalid(); } return read(slot, offset, count); } if (command == "COPY_RANGE") { unsigned long long target_offset = 0; if (!arity(5) || !number(1, 31, source) || !number(2, 4096, offset) || !number(3, 31, destination) || !number(4, 4096, target_offset) || !number(5, 4096, count)) { return invalid(); } return copy_range(source, offset, destination, target_offset, count); } if (command == "SWAP") { if (!arity(2) || !number(1, 31, source) || !number(2, 31, destination)) { return invalid(); } return swap(source, destination); } if (command == "COMPARE") { if (!arity(2) || !number(1, 31, source) || !number(2, 31, destination)) { return invalid(); } return compare(source, destination); } if (command == "ERASE") { if (!arity(2) || !key(1, resource)) { return invalid(); } return erase(resource); } if (command == "RETIRE") { if (!arity(1) || !name_valid(tokens[1])) { return invalid(); } return retire(tokens[1]); } if (!arity(0)) { return invalid(); } if (command == "DROP_ALL") { return drop_all(); } if (command == "CLEAR") { return clear(); } if (command == "SWEEP") { return sweep(); } if (command == "CACHE") { return cache(); } if (command == "STATS") { return stats(); } if (command == "DEATHS") { return deaths(); } return invalid(); } }