#include "affinity.hpp" #include #include namespace affinity { namespace detail { bool normalize_set(const CoreSet &input, CoreSet &output) { output.ids.clear(); std::array seen{}; for (std::uint16_t id : input.ids) { if (id > 255 || seen[id]) return false; seen[id] = true; } for (std::uint16_t id = 0; id < 256; ++id) { if (seen[id]) output.ids.push_back(id); } return true; } } namespace { bool decimal(const std::string &text, std::size_t &cursor, unsigned maximum, unsigned &result) { const std::size_t first = cursor; unsigned value = 0; while (cursor < text.size() && text[cursor] >= '0' && text[cursor] <= '9') { const unsigned digit = static_cast(text[cursor] - '0'); if (value > (maximum - digit) / 10) return false; value = value * 10 + digit; ++cursor; } if (cursor == first) return false; result = value; return true; } bool separator(const std::string &text, std::size_t &cursor, char wanted) { if (cursor == text.size() || text[cursor] != wanted) return false; ++cursor; return true; } bool list_item(const std::string &item, std::array &present) { std::size_t at = 0; unsigned first = 0; if (!decimal(item, at, 255, first)) return false; if (at == item.size()) { present[first] = true; return true; } if (!separator(item, at, '-')) return false; unsigned last = 0; if (!decimal(item, at, 255, last) || first > last) return false; unsigned used = 1; unsigned group = 1; if (at != item.size()) { if (!separator(item, at, ':') || !decimal(item, at, 256, used) || !separator(item, at, '/') || !decimal(item, at, 256, group) || used == 0 || group == 0 || used > group) return false; } if (at != item.size()) return false; for (unsigned id = first; id <= last; ++id) { if ((id - first) % group < used) present[id] = true; } return true; } int hex_digit(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; return -1; } bool mask_word(const std::string &text, std::uint32_t &value) { if (text.empty() || text.size() > 8) return false; value = 0; for (unsigned char byte : text) { const int digit = hex_digit(byte); if (digit < 0) return false; value = value * 16 + static_cast(digit); } return true; } std::string word_text(std::uint32_t value, bool full_width) { constexpr char digits[] = "0123456789abcdef"; std::string text; do { text.push_back(digits[value & 15]); value >>= 4; } while (value != 0); while (full_width && text.size() < 8) text.push_back('0'); std::reverse(text.begin(), text.end()); return text; } } Error parse_list(const std::string &text, CoreSet &output) { output.ids.clear(); if (text.size() > 4096) return Error::invalid; if (text.empty()) return Error::none; std::array present{}; std::size_t begin = 0; for (;;) { const auto comma = text.find(',', begin); const auto end = comma == std::string::npos ? text.size() : comma; if (!list_item(text.substr(begin, end - begin), present)) return Error::invalid; if (comma == std::string::npos) break; begin = comma + 1; } for (std::uint16_t id = 0; id < 256; ++id) { if (present[id]) output.ids.push_back(id); } return Error::none; } Error format_list(const CoreSet &input, std::string &output) { output.clear(); CoreSet normalized; if (!detail::normalize_set(input, normalized)) return Error::invalid; std::size_t first = 0; while (first < normalized.ids.size()) { std::size_t last = first; while (last + 1 < normalized.ids.size() && normalized.ids[last + 1] == normalized.ids[last] + 1) ++last; if (!output.empty()) output.push_back(','); output += std::to_string(normalized.ids[first]); if (last != first) output += "-" + std::to_string(normalized.ids[last]); first = last + 1; } return Error::none; } Error parse_mask(const std::string &text, CoreSet &output) { output.ids.clear(); if (text.empty() || text.size() > 4096) return Error::invalid; std::vector words; std::size_t begin = 0; for (;;) { const auto comma = text.find(',', begin); const auto end = comma == std::string::npos ? text.size() : comma; std::uint32_t word = 0; if (words.size() == 8 || !mask_word(text.substr(begin, end - begin), word)) return Error::invalid; words.push_back(word); if (comma == std::string::npos) break; begin = comma + 1; } std::array present{}; for (std::size_t index = 0; index < words.size(); ++index) { const auto offset = (words.size() - 1 - index) * 32; for (unsigned bit = 0; bit < 32; ++bit) { if ((words[index] >> bit) & 1u) present[offset + bit] = true; } } for (std::uint16_t id = 0; id < 256; ++id) { if (present[id]) output.ids.push_back(id); } return Error::none; } Error format_mask(const CoreSet &input, std::string &output) { output.clear(); CoreSet normalized; if (!detail::normalize_set(input, normalized)) return Error::invalid; std::array words{}; for (std::uint16_t id : normalized.ids) words[id / 32] |= std::uint32_t(1) << (id % 32); std::size_t count = words.size(); while (count > 1 && words[count - 1] == 0) --count; output = word_text(words[count - 1], false); while (--count != 0) output += "," + word_text(words[count - 1], true); return Error::none; } Error combine(const CoreSet &left, const CoreSet &right, SetOperation operation, CoreSet &output) { output.ids.clear(); CoreSet a; CoreSet b; if (!detail::normalize_set(left, a) || !detail::normalize_set(right, b)) return Error::invalid; if (operation != SetOperation::unite && operation != SetOperation::intersect && operation != SetOperation::subtract) return Error::invalid; for (std::uint16_t id = 0; id < 256; ++id) { const bool in_a = std::binary_search(a.ids.begin(), a.ids.end(), id); const bool in_b = std::binary_search(b.ids.begin(), b.ids.end(), id); const bool keep = operation == SetOperation::unite ? in_a || in_b : operation == SetOperation::intersect ? in_a && in_b : in_a && !in_b; if (keep) output.ids.push_back(id); } return Error::none; } }