// =========================================================================== // xclass_loader.cpp -- .xclass (SecureJDK 28) ingestion into a sleela::Program. // =========================================================================== #include "xclass_loader.h" #include #include #include #include #include namespace sleela { namespace xclass { // --------------------------------------------------------------------------- // A tiny, dependency-free XML scanner sufficient for the well-formed .xclass // schema (elements, attributes, text, self-closing tags; comments + the XML // declaration are skipped). It is NOT a general XML parser: it rejects DOCTYPE // so entity-expansion tricks cannot appear (matching the SecureJDK config's // reject-dtd posture). // --------------------------------------------------------------------------- namespace { struct Node { std::string tag; std::map attrs; std::string text; // concatenated direct text std::vector children; const Node* child(const std::string& t) const { for (auto& c : children) if (c.tag == t) return &c; return nullptr; } std::string childText(const std::string& t, const std::string& def = "") const { const Node* c = child(t); return c ? c->text : def; } std::string attr(const std::string& a, const std::string& def = "") const { auto it = attrs.find(a); return it == attrs.end() ? def : it->second; } }; struct XmlParser { const std::string& s; size_t p = 0; explicit XmlParser(const std::string& src) : s(src) {} [[noreturn]] void fail(const std::string& why) { throw std::runtime_error("xclass: malformed XML: " + why); } void skipWs() { while (p < s.size() && std::isspace((unsigned char) s[p])) p++; } // Skip , , and reject = s.size() || s[p] != '<') break; if (s.compare(p, 4, "", p + 4); if (e == std::string::npos) fail("unterminated comment"); p = e + 3; any = true; continue; } if (s.compare(p, 2, "", p + 2); if (e == std::string::npos) fail("unterminated "); p = e + 2; any = true; continue; } if (s.compare(p, 9, "= s.size()) fail("unterminated attribute value"); std::string raw = s.substr(start, p - start); p++; // closing quote return unescape(raw); } static std::string unescape(const std::string& in) { std::string out; out.reserve(in.size()); for (size_t i = 0; i < in.size(); i++) { if (in[i] == '&') { if (in.compare(i, 4, "<") == 0) { out += '<'; i += 3; } else if (in.compare(i, 4, ">") == 0) { out += '>'; i += 3; } else if (in.compare(i, 5, "&") == 0) { out += '&'; i += 4; } else if (in.compare(i, 6, """) == 0) { out += '"'; i += 5; } else if (in.compare(i, 6, "'") == 0) { out += '\''; i += 5; } else out += in[i]; } else out += in[i]; } return out; } // Parse one element (assumes current char is '<' of a start tag). Node parseElement() { skipMisc(); if (p >= s.size() || s[p] != '<') fail("expected '<'"); p++; // consume '<' Node n; n.tag = parseName(); // attributes for (;;) { skipWs(); if (p >= s.size()) fail("unterminated start tag"); if (s[p] == '/' && p + 1 < s.size() && s[p + 1] == '>') { p += 2; return n; } // self-closing if (s[p] == '>') { p++; break; } std::string an = parseName(); skipWs(); if (p >= s.size() || s[p] != '=') fail("expected '=' in attribute"); p++; skipWs(); n.attrs[an] = parseQuoted(); } // content: children + text until the matching end tag for (;;) { // gather text up to next '<' size_t textStart = p; while (p < s.size() && s[p] != '<') p++; if (p > textStart) { std::string t = s.substr(textStart, p - textStart); // trim + append if non-empty size_t a = t.find_first_not_of(" \t\r\n"); size_t b = t.find_last_not_of(" \t\r\n"); if (a != std::string::npos) n.text += unescape(t.substr(a, b - a + 1)); } if (p >= s.size()) fail("unterminated element <" + n.tag + ">"); if (s.compare(p, 4, "