#include "clock.hpp" #include #include namespace shared_clock { namespace { std::string trim(const std::string& field) { std::size_t first = 0; while (first < field.size() && (field[first] == ' ' || field[first] == '\t')) { ++first; } std::size_t last = field.size(); while (last > first && (field[last - 1] == ' ' || field[last - 1] == '\t')) { --last; } return field.substr(first, last - first); } bool digits_only(const std::string& value) { if (value.empty()) { return false; } for (char ch : value) { if (ch < '0' || ch > '9') { return false; } } return true; } bool unsigned_decimal(const std::string& text, std::uint64_t& value) { std::uint64_t current = 0; for (char ch : text) { const unsigned digit = static_cast(ch - '0'); if (current > (std::numeric_limits::max() - digit) / 10) { return false; } current = current * 10 + digit; } value = current; return true; } int hex_digit(char ch) { if (ch >= '0' && ch <= '9') { return ch - '0'; } if (ch >= 'a' && ch <= 'f') { return ch - 'a' + 10; } if (ch >= 'A' && ch <= 'F') { return ch - 'A' + 10; } return -1; } } Status parse_record(const std::string& text, Sample& out) { if (text.size() > 512) { return Status::Invalid; } std::vector fields; std::size_t start = 0; for (std::size_t pos = 0; pos <= text.size(); ++pos) { if (pos == text.size() || text[pos] == ',') { fields.push_back(trim(text.substr(start, pos - start))); start = pos + 1; } } if (fields.size() != 8) { return Status::Invalid; } for (const auto& field : fields) { if (!digits_only(field)) { return Status::Invalid; } } std::array values{}; for (std::size_t index = 0; index < values.size(); ++index) { if (!unsigned_decimal(fields[index], values[index])) { return Status::Range; } } const auto word_max = std::numeric_limits::max(); if (!values[0] || values[0] > word_max || values[1] > LIMIT || values[2] > LIMIT || values[3] > word_max || values[4] > 1023 || values[5] > word_max || values[6] > 1023 || values[7] > LIMIT) { return Status::Range; } Sample parsed; parsed.id = static_cast(values[0]); parsed.local_send = values[1]; parsed.local_receive = values[2]; parsed.remote_receive.seconds = static_cast(values[3]); parsed.remote_receive.fraction = static_cast(values[4]); parsed.remote_send.seconds = static_cast(values[5]); parsed.remote_send.fraction = static_cast(values[6]); parsed.hint = values[7]; out = parsed; return Status::Ok; } std::string format_record(const Sample& sample) { std::ostringstream out; out << sample.id << ',' << sample.local_send << ',' << sample.local_receive; out << ',' << sample.remote_receive.seconds << ',' << sample.remote_receive.fraction; out << ',' << sample.remote_send.seconds << ',' << sample.remote_send.fraction; out << ',' << sample.hint; return out.str(); } Status decode_hex(const std::string& text, std::vector& out) { if (text.size() % 2 != 0) { return Status::Invalid; } std::vector decoded; decoded.reserve(text.size() / 2); for (std::size_t pos = 0; pos < text.size(); pos += 2) { const int high = hex_digit(text[pos]); const int low = hex_digit(text[pos + 1]); if (high < 0 || low < 0) { return Status::Invalid; } decoded.push_back(static_cast(high * 16 + low)); } out.swap(decoded); return Status::Ok; } std::string encode_hex(const std::vector& bytes) { static constexpr char digits[] = "0123456789abcdef"; std::string text; text.reserve(bytes.size() * 2); for (std::uint8_t byte : bytes) { text.push_back(digits[byte >> 4]); text.push_back(digits[byte & 15]); } return text; } }