#include "pc_closure.hpp" #include #include namespace pc_closure { namespace { bool alpha(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; } bool identifier(const std::string &name, bool package) { if (name.empty() || name.size() > 64 || !alpha(name[0])) return false; for (char ch : name) { if (alpha(ch) || (ch >= '0' && ch <= '9')) continue; if (package && (ch == '.' || ch == '-')) continue; return false; } return true; } std::string trim(const std::string &text) { const auto first = text.find_first_not_of(" \t"); if (first == std::string::npos) return {}; return text.substr(first, text.find_last_not_of(" \t") - first + 1); } bool expression_syntax(const std::string &text) { for (std::size_t i = 0; i < text.size();) { if (text[i] != '$') { ++i; continue; } if (i + 1 == text.size()) return false; if (text[i + 1] == '$') { i += 2; continue; } if (text[i + 1] != '{') return false; const auto end = text.find('}', i + 2); if (end == std::string::npos || !identifier(text.substr(i + 2, end - i - 2), false)) return false; i = end + 1; } return true; } bool source_bytes(const std::string &text) { for (std::size_t i = 0; i < text.size(); ++i) { const auto ch = static_cast(text[i]); if ((ch >= 32 && ch <= 126) || ch == '\t' || ch == '\n') continue; if (ch == '\r' && i + 1 < text.size() && text[i + 1] == '\n') continue; return false; } return true; } int field_number(const std::string &name) { static const std::array names{ "Version", "Requires", "Requires.private", "Libs", "Libs.private", "Cflags" }; for (std::size_t i = 0; i < names.size(); ++i) if (names[i] == name) return static_cast(i); return -1; } } // namespace Status parse_sources(const std::vector &sources, Repository &output) { output = {}; if (sources.size() > 64) return Status::invalid; std::size_t source_size = 0; std::size_t variable_count = 0; std::set names; Repository draft; for (const Source &source : sources) { if (!identifier(source.name, true) || !names.insert(source.name).second || source.text.size() > 262144 - source_size || !source_bytes(source.text)) return Status::invalid; source_size += source.text.size(); Package package; package.name = source.name; std::set variables; std::array present{}; std::size_t position = 0; while (position < source.text.size()) { const auto found = source.text.find('\n', position); const auto end = found == std::string::npos ? source.text.size() : found; std::string line = source.text.substr(position, end - position); position = found == std::string::npos ? source.text.size() : found + 1; if (!line.empty() && line.back() == '\r') line.pop_back(); line = trim(line); if (line.empty() || line[0] == '#') continue; const auto separator = line.find_first_of("=:"); if (separator == std::string::npos) return Status::invalid; const std::string key = trim(line.substr(0, separator)); const std::string value = trim(line.substr(separator + 1)); if (!expression_syntax(value)) return Status::invalid; if (line[separator] == '=') { if (!identifier(key, false) || !variables.insert(key).second || ++variable_count > 256) return Status::invalid; package.variables.push_back(Variable{key, value}); } else { const int field = field_number(key); if (field < 0 || present[static_cast(field)]) return Status::invalid; present[static_cast(field)] = true; package.fields[static_cast(field)] = value; } } if (!present[0]) return Status::invalid; std::sort(package.variables.begin(), package.variables.end(), [](const Variable &a, const Variable &b) { return a.name < b.name; }); draft.packages.push_back(std::move(package)); } std::sort(draft.packages.begin(), draft.packages.end(), [](const Package &a, const Package &b) { return a.name < b.name; }); output = std::move(draft); return Status::ok; } } // namespace pc_closure