Harden XmlReader
Build / AerScript (push) Successful in 14s

This commit is contained in:
2026-11-22 16:25:36 +01:00
parent 4d47dac2b1
commit e99462aea1
4 changed files with 408 additions and 260 deletions
+7 -3
View File
@@ -8,6 +8,10 @@
*/
#include "lib.h"
/* Shared key for the default namespace declaration. Declared at file scope so
* the stored byte sequence stays alive for the lifetime of the parser. */
static const char sDefaultNsKey[] = { SXML_DEFAULT_NS_KEY, 0 };
/* Tokenize an entire XML input */
static sxi32 XML_Tokenize(SyStream *pStream, SyToken *pToken, void *pUserData, void *pUnused2) {
SyXMLParser *pParse = (SyXMLParser *)pUserData;
@@ -351,8 +355,8 @@ static sxi32 XMLProcessNamesSpace(SyXMLParser *pParse, SyXMLRawStrNS *pTag, SyTo
*pDup = *pUri; /* Structure assignment */
/* Save the namespace */
if(pPrefix->nByte == 0) {
pPrefix->zString = "Default";
pPrefix->nByte = sizeof("Default") - 1;
pPrefix->zString = sDefaultNsKey;
pPrefix->nByte = SXML_DEFAULT_NS_KEY_LEN;
}
SyHashInsert(&pParse->hns, (const void *)pPrefix->zString, pPrefix->nByte, pDup);
/* Peek the last inserted entry */
@@ -622,7 +626,7 @@ static sxi32 XMLExtractNS(SyXMLParser *pParse, SyToken *pToken, SyXMLRawStrNS *p
rc = SyByteFind(pTag->zString, pTag->nByte, ':', &nOfft);
if(rc != SXRET_OK) {
/* Check if there is a default namespace */
pEntry = SyHashGet(&pParse->hns, "Default", sizeof("Default") - 1);
pEntry = SyHashGet(&pParse->hns, sDefaultNsKey, SXML_DEFAULT_NS_KEY_LEN);
if(pEntry) {
/* Extract the ns URI */
pUri = (SyXMLRawStr *)pEntry->pUserData;
+10 -1
View File
@@ -63,9 +63,18 @@ enum xml_err_code {
SXML_ERROR_UNKNOWN_ENCODING,
SXML_ERROR_INCORRECT_ENCODING,
SXML_ERROR_UNCLOSED_CDATA_SECTION,
SXML_ERROR_EXTERNAL_ENTITY_HANDLING
SXML_ERROR_EXTERNAL_ENTITY_HANDLING,
/* Module-level conditions (not produced by the SAX tokenizer). */
SXML_ERROR_INVALID_OPERATION,
SXML_ERROR_TYPE_MISMATCH
};
/* SyHashGet() ignores zero-length keys, so the default namespace cannot be
* stored under "". Use a non-printable one-byte sentinel instead. */
#define SXML_DEFAULT_NS_KEY '\x01'
#define SXML_DEFAULT_NS_KEY_STR "\x01"
#define SXML_DEFAULT_NS_KEY_LEN 1
/*
* An XML raw text,CDATA,tag name and son is parsed out and stored
* in an instance of the following structure.
+381 -237
View File
File diff suppressed because it is too large Load Diff
+10 -19
View File
@@ -16,22 +16,17 @@
#define MODULE_DESC "XML Module"
#define MODULE_VER 1.0
#define XML_TOTAL_HANDLER (PH7_XML_DOCTYPE + 1)
#define XML_ENGINE_MAGIC 0x851EFC52
#define IS_INVALID_XML_ENGINE(XML) (XML == 0 || (XML)->nMagic != XML_ENGINE_MAGIC)
enum ph7_xml_handler_id {
PH7_XML_START_TAG = 0, /* Start element */
PH7_XML_END_TAG, /* End element */
PH7_XML_CDATA, /* Character data */
PH7_XML_PI, /* Processing instruction */
PH7_XML_DEF, /* Default (unused) */
PH7_XML_UNPED, /* Unparsed entity (unused) */
PH7_XML_ND, /* Notation declaration (unused) */
PH7_XML_EER, /* External entity reference (unused) */
PH7_XML_NS_START, /* Start namespace declaration */
PH7_XML_NS_END, /* End namespace declaration */
PH7_XML_DOCTYPE /* DOCTYPE directive */
/* One snapshot of the QName fields of an open element. The stack of these
* lets the end handler restore the parent (enclosing) element after its own
* callback, and gives text/PI/DOCTYPE callbacks the enclosing element. */
typedef struct ph7_xml_elem_snapshot ph7_xml_elem_snapshot;
struct ph7_xml_elem_snapshot {
ph7_value sLocalName;
ph7_value sNsUri;
ph7_value sPrefix;
};
/*
@@ -45,17 +40,13 @@ struct ph7_xml_engine {
ph7_vm *pVm; /* VM that owns this instance */
ph7_context *pCtx; /* Current call context */
SyXMLParser sParser; /* Underlying SAX parser */
ph7_value aCB[XML_TOTAL_HANDLER]; /* Convenience callbacks by event id */
ph7_value sParserValue; /* Resource value holding this engine */
ph7_value sReaderValue; /* XmlReader object, owned only across Parse() */
ph7_value sHandler; /* IXmlHandler implementation, when installed */
ph7_value sAttrs; /* Snapshot of the current element attributes */
ph7_value sLocalName; /* Current element local name */
ph7_value sLocalName; /* Current (innermost open) element local name */
ph7_value sNsUri; /* Current element namespace URI */
ph7_value sPrefix; /* Current element prefix */
/* Reserved: the public API forwards QName components separately and never
* concatenates URI + separator + local name, so this value is unused. */
int ns_sep;
SySet sElemStack; /* Stack of open-element QName snapshots */
SyBlob sErr; /* Last SAX error message */
sxi32 iErrCode; /* Last SAX error code */
sxu32 nErrLine; /* Line of the last SAX error */