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" #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 */ /* Tokenize an entire XML input */
static sxi32 XML_Tokenize(SyStream *pStream, SyToken *pToken, void *pUserData, void *pUnused2) { static sxi32 XML_Tokenize(SyStream *pStream, SyToken *pToken, void *pUserData, void *pUnused2) {
SyXMLParser *pParse = (SyXMLParser *)pUserData; SyXMLParser *pParse = (SyXMLParser *)pUserData;
@@ -351,8 +355,8 @@ static sxi32 XMLProcessNamesSpace(SyXMLParser *pParse, SyXMLRawStrNS *pTag, SyTo
*pDup = *pUri; /* Structure assignment */ *pDup = *pUri; /* Structure assignment */
/* Save the namespace */ /* Save the namespace */
if(pPrefix->nByte == 0) { if(pPrefix->nByte == 0) {
pPrefix->zString = "Default"; pPrefix->zString = sDefaultNsKey;
pPrefix->nByte = sizeof("Default") - 1; pPrefix->nByte = SXML_DEFAULT_NS_KEY_LEN;
} }
SyHashInsert(&pParse->hns, (const void *)pPrefix->zString, pPrefix->nByte, pDup); SyHashInsert(&pParse->hns, (const void *)pPrefix->zString, pPrefix->nByte, pDup);
/* Peek the last inserted entry */ /* 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); rc = SyByteFind(pTag->zString, pTag->nByte, ':', &nOfft);
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
/* Check if there is a default namespace */ /* 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) { if(pEntry) {
/* Extract the ns URI */ /* Extract the ns URI */
pUri = (SyXMLRawStr *)pEntry->pUserData; pUri = (SyXMLRawStr *)pEntry->pUserData;
+10 -1
View File
@@ -63,9 +63,18 @@ enum xml_err_code {
SXML_ERROR_UNKNOWN_ENCODING, SXML_ERROR_UNKNOWN_ENCODING,
SXML_ERROR_INCORRECT_ENCODING, SXML_ERROR_INCORRECT_ENCODING,
SXML_ERROR_UNCLOSED_CDATA_SECTION, 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 * An XML raw text,CDATA,tag name and son is parsed out and stored
* in an instance of the following structure. * in an instance of the following structure.
+378 -234
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_DESC "XML Module"
#define MODULE_VER 1.0 #define MODULE_VER 1.0
#define XML_TOTAL_HANDLER (PH7_XML_DOCTYPE + 1)
#define XML_ENGINE_MAGIC 0x851EFC52 #define XML_ENGINE_MAGIC 0x851EFC52
#define IS_INVALID_XML_ENGINE(XML) (XML == 0 || (XML)->nMagic != XML_ENGINE_MAGIC) #define IS_INVALID_XML_ENGINE(XML) (XML == 0 || (XML)->nMagic != XML_ENGINE_MAGIC)
enum ph7_xml_handler_id { /* One snapshot of the QName fields of an open element. The stack of these
PH7_XML_START_TAG = 0, /* Start element */ * lets the end handler restore the parent (enclosing) element after its own
PH7_XML_END_TAG, /* End element */ * callback, and gives text/PI/DOCTYPE callbacks the enclosing element. */
PH7_XML_CDATA, /* Character data */ typedef struct ph7_xml_elem_snapshot ph7_xml_elem_snapshot;
PH7_XML_PI, /* Processing instruction */ struct ph7_xml_elem_snapshot {
PH7_XML_DEF, /* Default (unused) */ ph7_value sLocalName;
PH7_XML_UNPED, /* Unparsed entity (unused) */ ph7_value sNsUri;
PH7_XML_ND, /* Notation declaration (unused) */ ph7_value sPrefix;
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 */
}; };
/* /*
@@ -45,17 +40,13 @@ struct ph7_xml_engine {
ph7_vm *pVm; /* VM that owns this instance */ ph7_vm *pVm; /* VM that owns this instance */
ph7_context *pCtx; /* Current call context */ ph7_context *pCtx; /* Current call context */
SyXMLParser sParser; /* Underlying SAX parser */ 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 sReaderValue; /* XmlReader object, owned only across Parse() */
ph7_value sHandler; /* IXmlHandler implementation, when installed */ ph7_value sHandler; /* IXmlHandler implementation, when installed */
ph7_value sAttrs; /* Snapshot of the current element attributes */ 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 sNsUri; /* Current element namespace URI */
ph7_value sPrefix; /* Current element prefix */ ph7_value sPrefix; /* Current element prefix */
/* Reserved: the public API forwards QName components separately and never SySet sElemStack; /* Stack of open-element QName snapshots */
* concatenates URI + separator + local name, so this value is unused. */
int ns_sep;
SyBlob sErr; /* Last SAX error message */ SyBlob sErr; /* Last SAX error message */
sxi32 iErrCode; /* Last SAX error code */ sxi32 iErrCode; /* Last SAX error code */
sxu32 nErrLine; /* Line of the last SAX error */ sxu32 nErrLine; /* Line of the last SAX error */