Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3755 lines
104 KiB

  1. /*
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in
  4. compliance with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6. Software distributed under the License is distributed on an "AS IS"
  7. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. License for the specific language governing rights and limitations
  9. under the License.
  10. The Original Code is expat.
  11. The Initial Developer of the Original Code is James Clark.
  12. Portions created by James Clark are Copyright (C) 1998, 1999
  13. James Clark. All Rights Reserved.
  14. Contributor(s):
  15. Alternatively, the contents of this file may be used under the terms
  16. of the GNU General Public License (the "GPL"), in which case the
  17. provisions of the GPL are applicable instead of those above. If you
  18. wish to allow use of your version of this file only under the terms of
  19. the GPL and not to allow others to use your version of this file under
  20. the MPL, indicate your decision by deleting the provisions above and
  21. replace them with the notice and other provisions required by the
  22. GPL. If you do not delete the provisions above, a recipient may use
  23. your version of this file under either the MPL or the GPL.
  24. */
  25. #include "xmldef.h"
  26. #include "xmlparse.h"
  27. #ifdef XML_UNICODE
  28. #define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
  29. #define XmlConvert XmlUtf16Convert
  30. #define XmlGetInternalEncoding XmlGetUtf16InternalEncoding
  31. #define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS
  32. #define XmlEncode XmlUtf16Encode
  33. #define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((unsigned long)s) & 1))
  34. typedef unsigned short ICHAR;
  35. #else
  36. #define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
  37. #define XmlConvert XmlUtf8Convert
  38. #define XmlGetInternalEncoding XmlGetUtf8InternalEncoding
  39. #define XmlGetInternalEncodingNS XmlGetUtf8InternalEncodingNS
  40. #define XmlEncode XmlUtf8Encode
  41. #define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
  42. typedef char ICHAR;
  43. #endif
  44. #ifndef XML_NS
  45. #define XmlInitEncodingNS XmlInitEncoding
  46. #define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
  47. #undef XmlGetInternalEncodingNS
  48. #define XmlGetInternalEncodingNS XmlGetInternalEncoding
  49. #define XmlParseXmlDeclNS XmlParseXmlDecl
  50. #endif
  51. #ifdef XML_UNICODE_WCHAR_T
  52. #define XML_T(x) L ## x
  53. #else
  54. #define XML_T(x) x
  55. #endif
  56. /* Round up n to be a multiple of sz, where sz is a power of 2. */
  57. #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
  58. #include "xmltok.h"
  59. #include "xmlrole.h"
  60. #include "hashtable.h"
  61. #define INIT_TAG_BUF_SIZE 32 /* must be a multiple of sizeof(XML_Char) */
  62. #define INIT_DATA_BUF_SIZE 1024
  63. #define INIT_ATTS_SIZE 16
  64. #define INIT_BLOCK_SIZE 1024
  65. #define INIT_BUFFER_SIZE 1024
  66. #define EXPAND_SPARE 24
  67. typedef struct binding {
  68. struct prefix *prefix;
  69. struct binding *nextTagBinding;
  70. struct binding *prevPrefixBinding;
  71. const struct attribute_id *attId;
  72. XML_Char *uri;
  73. int uriLen;
  74. int uriAlloc;
  75. } BINDING;
  76. typedef struct prefix {
  77. const XML_Char *name;
  78. BINDING *binding;
  79. } PREFIX;
  80. typedef struct {
  81. const XML_Char *str;
  82. const XML_Char *localPart;
  83. int uriLen;
  84. } TAG_NAME;
  85. typedef struct tag {
  86. struct tag *parent;
  87. const char *rawName;
  88. int rawNameLength;
  89. TAG_NAME name;
  90. char *buf;
  91. char *bufEnd;
  92. BINDING *bindings;
  93. } TAG;
  94. typedef struct {
  95. const XML_Char *name;
  96. const XML_Char *textPtr;
  97. int textLen;
  98. const XML_Char *systemId;
  99. const XML_Char *base;
  100. const XML_Char *publicId;
  101. const XML_Char *notation;
  102. char open;
  103. } ENTITY;
  104. typedef struct block {
  105. struct block *next;
  106. int size;
  107. XML_Char s[1];
  108. } BLOCK;
  109. typedef struct {
  110. BLOCK *blocks;
  111. BLOCK *freeBlocks;
  112. const XML_Char *end;
  113. XML_Char *ptr;
  114. XML_Char *start;
  115. } STRING_POOL;
  116. /* The XML_Char before the name is used to determine whether
  117. an attribute has been specified. */
  118. typedef struct attribute_id {
  119. XML_Char *name;
  120. PREFIX *prefix;
  121. char maybeTokenized;
  122. char xmlns;
  123. } ATTRIBUTE_ID;
  124. typedef struct {
  125. const ATTRIBUTE_ID *id;
  126. char isCdata;
  127. const XML_Char *value;
  128. } DEFAULT_ATTRIBUTE;
  129. typedef struct {
  130. const XML_Char *name;
  131. PREFIX *prefix;
  132. int nDefaultAtts;
  133. int allocDefaultAtts;
  134. DEFAULT_ATTRIBUTE *defaultAtts;
  135. } ELEMENT_TYPE;
  136. typedef struct {
  137. HASH_TABLE generalEntities;
  138. HASH_TABLE elementTypes;
  139. HASH_TABLE attributeIds;
  140. HASH_TABLE prefixes;
  141. STRING_POOL pool;
  142. int complete;
  143. int standalone;
  144. #ifdef XML_DTD
  145. HASH_TABLE paramEntities;
  146. #endif /* XML_DTD */
  147. PREFIX defaultPrefix;
  148. } DTD;
  149. typedef struct open_internal_entity {
  150. const char *internalEventPtr;
  151. const char *internalEventEndPtr;
  152. struct open_internal_entity *next;
  153. ENTITY *entity;
  154. } OPEN_INTERNAL_ENTITY;
  155. typedef enum XML_Error Processor(XML_Parser parser,
  156. const char *start,
  157. const char *end,
  158. const char **endPtr);
  159. static Processor prologProcessor;
  160. static Processor prologInitProcessor;
  161. static Processor contentProcessor;
  162. static Processor cdataSectionProcessor;
  163. #ifdef XML_DTD
  164. static Processor ignoreSectionProcessor;
  165. #endif /* XML_DTD */
  166. static Processor epilogProcessor;
  167. static Processor errorProcessor;
  168. static Processor externalEntityInitProcessor;
  169. static Processor externalEntityInitProcessor2;
  170. static Processor externalEntityInitProcessor3;
  171. static Processor externalEntityContentProcessor;
  172. static enum XML_Error
  173. handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName);
  174. static enum XML_Error
  175. processXmlDecl(XML_Parser parser, int isGeneralTextEntity, const char *, const char *);
  176. static enum XML_Error
  177. initializeEncoding(XML_Parser parser);
  178. static enum XML_Error
  179. doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
  180. const char *end, int tok, const char *next, const char **nextPtr);
  181. static enum XML_Error
  182. processInternalParamEntity(XML_Parser parser, ENTITY *entity);
  183. static enum XML_Error
  184. doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc,
  185. const char *start, const char *end, const char **endPtr);
  186. static enum XML_Error
  187. doCdataSection(XML_Parser parser, const ENCODING *, const char **startPtr, const char *end, const char **nextPtr);
  188. #ifdef XML_DTD
  189. static enum XML_Error
  190. doIgnoreSection(XML_Parser parser, const ENCODING *, const char **startPtr, const char *end, const char **nextPtr);
  191. #endif /* XML_DTD */
  192. static enum XML_Error storeAtts(XML_Parser parser, const ENCODING *, const char *s,
  193. TAG_NAME *tagNamePtr, BINDING **bindingsPtr);
  194. static
  195. int addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId, const XML_Char *uri, BINDING **bindingsPtr);
  196. static int
  197. defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *, int isCdata, const XML_Char *dfltValue);
  198. static enum XML_Error
  199. storeAttributeValue(XML_Parser parser, const ENCODING *, int isCdata, const char *, const char *,
  200. STRING_POOL *);
  201. static enum XML_Error
  202. appendAttributeValue(XML_Parser parser, const ENCODING *, int isCdata, const char *, const char *,
  203. STRING_POOL *);
  204. static ATTRIBUTE_ID *
  205. getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start, const char *end);
  206. static int setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *);
  207. static enum XML_Error
  208. storeEntityValue(XML_Parser parser, const ENCODING *enc, const char *start, const char *end);
  209. static int
  210. reportProcessingInstruction(XML_Parser parser, const ENCODING *enc, const char *start, const char *end);
  211. static int
  212. reportComment(XML_Parser parser, const ENCODING *enc, const char *start, const char *end);
  213. static void
  214. reportDefault(XML_Parser parser, const ENCODING *enc, const char *start, const char *end);
  215. static const XML_Char *getContext(XML_Parser parser);
  216. static int setContext(XML_Parser parser, const XML_Char *context);
  217. static void normalizePublicId(XML_Char *s);
  218. static int dtdInit(DTD *);
  219. static void dtdDestroy(DTD *);
  220. static int dtdCopy(DTD *newDtd, const DTD *oldDtd);
  221. static int copyEntityTable(HASH_TABLE *, STRING_POOL *, const HASH_TABLE *);
  222. #ifdef XML_DTD
  223. static void dtdSwap(DTD *, DTD *);
  224. #endif /* XML_DTD */
  225. static void poolInit(STRING_POOL *);
  226. static void poolClear(STRING_POOL *);
  227. static void poolDestroy(STRING_POOL *);
  228. static XML_Char *poolAppend(STRING_POOL *pool, const ENCODING *enc,
  229. const char *ptr, const char *end);
  230. static XML_Char *poolStoreString(STRING_POOL *pool, const ENCODING *enc,
  231. const char *ptr, const char *end);
  232. static int poolGrow(STRING_POOL *pool);
  233. static const XML_Char *poolCopyString(STRING_POOL *pool, const XML_Char *s);
  234. static const XML_Char *poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n);
  235. #define poolStart(pool) ((pool)->start)
  236. #define poolEnd(pool) ((pool)->ptr)
  237. #define poolLength(pool) ((pool)->ptr - (pool)->start)
  238. #define poolChop(pool) ((void)--(pool->ptr))
  239. #define poolLastChar(pool) (((pool)->ptr)[-1])
  240. #define poolDiscard(pool) ((pool)->ptr = (pool)->start)
  241. #define poolFinish(pool) ((pool)->start = (pool)->ptr)
  242. #define poolAppendChar(pool, c) \
  243. (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
  244. ? 0 \
  245. : ((*((pool)->ptr)++ = c), 1))
  246. typedef struct {
  247. /* The first member must be userData so that the XML_GetUserData macro works. */
  248. void *m_userData;
  249. void *m_handlerArg;
  250. char *m_buffer;
  251. /* first character to be parsed */
  252. const char *m_bufferPtr;
  253. /* past last character to be parsed */
  254. char *m_bufferEnd;
  255. /* allocated end of buffer */
  256. const char *m_bufferLim;
  257. long m_parseEndByteIndex;
  258. const char *m_parseEndPtr;
  259. XML_Char *m_dataBuf;
  260. XML_Char *m_dataBufEnd;
  261. XML_StartElementHandler m_startElementHandler;
  262. XML_EndElementHandler m_endElementHandler;
  263. XML_CharacterDataHandler m_characterDataHandler;
  264. XML_ProcessingInstructionHandler m_processingInstructionHandler;
  265. XML_CommentHandler m_commentHandler;
  266. XML_StartCdataSectionHandler m_startCdataSectionHandler;
  267. XML_EndCdataSectionHandler m_endCdataSectionHandler;
  268. XML_DefaultHandler m_defaultHandler;
  269. XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
  270. XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
  271. XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
  272. XML_NotationDeclHandler m_notationDeclHandler;
  273. XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
  274. XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
  275. XML_NotStandaloneHandler m_notStandaloneHandler;
  276. XML_ExternalEntityRefHandler m_externalEntityRefHandler;
  277. void *m_externalEntityRefHandlerArg;
  278. XML_UnknownEncodingHandler m_unknownEncodingHandler;
  279. const ENCODING *m_encoding;
  280. INIT_ENCODING m_initEncoding;
  281. const ENCODING *m_internalEncoding;
  282. const XML_Char *m_protocolEncodingName;
  283. int m_ns;
  284. void *m_unknownEncodingMem;
  285. void *m_unknownEncodingData;
  286. void *m_unknownEncodingHandlerData;
  287. void (*m_unknownEncodingRelease)(void *);
  288. PROLOG_STATE m_prologState;
  289. Processor *m_processor;
  290. enum XML_Error m_errorCode;
  291. const char *m_eventPtr;
  292. const char *m_eventEndPtr;
  293. const char *m_positionPtr;
  294. OPEN_INTERNAL_ENTITY *m_openInternalEntities;
  295. int m_defaultExpandInternalEntities;
  296. int m_tagLevel;
  297. ENTITY *m_declEntity;
  298. const XML_Char *m_declNotationName;
  299. const XML_Char *m_declNotationPublicId;
  300. ELEMENT_TYPE *m_declElementType;
  301. ATTRIBUTE_ID *m_declAttributeId;
  302. char m_declAttributeIsCdata;
  303. DTD m_dtd;
  304. const XML_Char *m_curBase;
  305. TAG *m_tagStack;
  306. TAG *m_freeTagList;
  307. BINDING *m_inheritedBindings;
  308. BINDING *m_freeBindingList;
  309. int m_attsSize;
  310. int m_nSpecifiedAtts;
  311. ATTRIBUTE *m_atts;
  312. POSITION m_position;
  313. STRING_POOL m_tempPool;
  314. STRING_POOL m_temp2Pool;
  315. char *m_groupConnector;
  316. unsigned m_groupSize;
  317. int m_hadExternalDoctype;
  318. XML_Char m_namespaceSeparator;
  319. #ifdef XML_DTD
  320. enum XML_ParamEntityParsing m_paramEntityParsing;
  321. XML_Parser m_parentParser;
  322. #endif
  323. } Parser;
  324. #define userData (((Parser *)parser)->m_userData)
  325. #define handlerArg (((Parser *)parser)->m_handlerArg)
  326. #define startElementHandler (((Parser *)parser)->m_startElementHandler)
  327. #define endElementHandler (((Parser *)parser)->m_endElementHandler)
  328. #define characterDataHandler (((Parser *)parser)->m_characterDataHandler)
  329. #define processingInstructionHandler (((Parser *)parser)->m_processingInstructionHandler)
  330. #define commentHandler (((Parser *)parser)->m_commentHandler)
  331. #define startCdataSectionHandler (((Parser *)parser)->m_startCdataSectionHandler)
  332. #define endCdataSectionHandler (((Parser *)parser)->m_endCdataSectionHandler)
  333. #define defaultHandler (((Parser *)parser)->m_defaultHandler)
  334. #define startDoctypeDeclHandler (((Parser *)parser)->m_startDoctypeDeclHandler)
  335. #define endDoctypeDeclHandler (((Parser *)parser)->m_endDoctypeDeclHandler)
  336. #define unparsedEntityDeclHandler (((Parser *)parser)->m_unparsedEntityDeclHandler)
  337. #define notationDeclHandler (((Parser *)parser)->m_notationDeclHandler)
  338. #define startNamespaceDeclHandler (((Parser *)parser)->m_startNamespaceDeclHandler)
  339. #define endNamespaceDeclHandler (((Parser *)parser)->m_endNamespaceDeclHandler)
  340. #define notStandaloneHandler (((Parser *)parser)->m_notStandaloneHandler)
  341. #define externalEntityRefHandler (((Parser *)parser)->m_externalEntityRefHandler)
  342. #define externalEntityRefHandlerArg (((Parser *)parser)->m_externalEntityRefHandlerArg)
  343. #define unknownEncodingHandler (((Parser *)parser)->m_unknownEncodingHandler)
  344. #define encoding (((Parser *)parser)->m_encoding)
  345. #define initEncoding (((Parser *)parser)->m_initEncoding)
  346. #define internalEncoding (((Parser *)parser)->m_internalEncoding)
  347. #define unknownEncodingMem (((Parser *)parser)->m_unknownEncodingMem)
  348. #define unknownEncodingData (((Parser *)parser)->m_unknownEncodingData)
  349. #define unknownEncodingHandlerData \
  350. (((Parser *)parser)->m_unknownEncodingHandlerData)
  351. #define unknownEncodingRelease (((Parser *)parser)->m_unknownEncodingRelease)
  352. #define protocolEncodingName (((Parser *)parser)->m_protocolEncodingName)
  353. #define ns (((Parser *)parser)->m_ns)
  354. #define prologState (((Parser *)parser)->m_prologState)
  355. #define processor (((Parser *)parser)->m_processor)
  356. #define errorCode (((Parser *)parser)->m_errorCode)
  357. #define eventPtr (((Parser *)parser)->m_eventPtr)
  358. #define eventEndPtr (((Parser *)parser)->m_eventEndPtr)
  359. #define positionPtr (((Parser *)parser)->m_positionPtr)
  360. #define position (((Parser *)parser)->m_position)
  361. #define openInternalEntities (((Parser *)parser)->m_openInternalEntities)
  362. #define defaultExpandInternalEntities (((Parser *)parser)->m_defaultExpandInternalEntities)
  363. #define tagLevel (((Parser *)parser)->m_tagLevel)
  364. #define buffer (((Parser *)parser)->m_buffer)
  365. #define bufferPtr (((Parser *)parser)->m_bufferPtr)
  366. #define bufferEnd (((Parser *)parser)->m_bufferEnd)
  367. #define parseEndByteIndex (((Parser *)parser)->m_parseEndByteIndex)
  368. #define parseEndPtr (((Parser *)parser)->m_parseEndPtr)
  369. #define bufferLim (((Parser *)parser)->m_bufferLim)
  370. #define dataBuf (((Parser *)parser)->m_dataBuf)
  371. #define dataBufEnd (((Parser *)parser)->m_dataBufEnd)
  372. #define dtd (((Parser *)parser)->m_dtd)
  373. #define curBase (((Parser *)parser)->m_curBase)
  374. #define declEntity (((Parser *)parser)->m_declEntity)
  375. #define declNotationName (((Parser *)parser)->m_declNotationName)
  376. #define declNotationPublicId (((Parser *)parser)->m_declNotationPublicId)
  377. #define declElementType (((Parser *)parser)->m_declElementType)
  378. #define declAttributeId (((Parser *)parser)->m_declAttributeId)
  379. #define declAttributeIsCdata (((Parser *)parser)->m_declAttributeIsCdata)
  380. #define freeTagList (((Parser *)parser)->m_freeTagList)
  381. #define freeBindingList (((Parser *)parser)->m_freeBindingList)
  382. #define inheritedBindings (((Parser *)parser)->m_inheritedBindings)
  383. #define tagStack (((Parser *)parser)->m_tagStack)
  384. #define atts (((Parser *)parser)->m_atts)
  385. #define attsSize (((Parser *)parser)->m_attsSize)
  386. #define nSpecifiedAtts (((Parser *)parser)->m_nSpecifiedAtts)
  387. #define tempPool (((Parser *)parser)->m_tempPool)
  388. #define temp2Pool (((Parser *)parser)->m_temp2Pool)
  389. #define groupConnector (((Parser *)parser)->m_groupConnector)
  390. #define groupSize (((Parser *)parser)->m_groupSize)
  391. #define hadExternalDoctype (((Parser *)parser)->m_hadExternalDoctype)
  392. #define namespaceSeparator (((Parser *)parser)->m_namespaceSeparator)
  393. #ifdef XML_DTD
  394. #define parentParser (((Parser *)parser)->m_parentParser)
  395. #define paramEntityParsing (((Parser *)parser)->m_paramEntityParsing)
  396. #endif /* XML_DTD */
  397. #ifdef _MSC_VER
  398. #ifdef _DEBUG
  399. Parser *asParser(XML_Parser parser)
  400. {
  401. return parser;
  402. }
  403. #endif
  404. #endif
  405. XML_Parser XML_ParserCreate(const XML_Char *encodingName)
  406. {
  407. XML_Parser parser = malloc(sizeof(Parser));
  408. if (!parser)
  409. return parser;
  410. processor = prologInitProcessor;
  411. XmlPrologStateInit(&prologState);
  412. userData = 0;
  413. handlerArg = 0;
  414. startElementHandler = 0;
  415. endElementHandler = 0;
  416. characterDataHandler = 0;
  417. processingInstructionHandler = 0;
  418. commentHandler = 0;
  419. startCdataSectionHandler = 0;
  420. endCdataSectionHandler = 0;
  421. defaultHandler = 0;
  422. startDoctypeDeclHandler = 0;
  423. endDoctypeDeclHandler = 0;
  424. unparsedEntityDeclHandler = 0;
  425. notationDeclHandler = 0;
  426. startNamespaceDeclHandler = 0;
  427. endNamespaceDeclHandler = 0;
  428. notStandaloneHandler = 0;
  429. externalEntityRefHandler = 0;
  430. externalEntityRefHandlerArg = parser;
  431. unknownEncodingHandler = 0;
  432. buffer = 0;
  433. bufferPtr = 0;
  434. bufferEnd = 0;
  435. parseEndByteIndex = 0;
  436. parseEndPtr = 0;
  437. bufferLim = 0;
  438. declElementType = 0;
  439. declAttributeId = 0;
  440. declEntity = 0;
  441. declNotationName = 0;
  442. declNotationPublicId = 0;
  443. memset(&position, 0, sizeof(POSITION));
  444. errorCode = XML_ERROR_NONE;
  445. eventPtr = 0;
  446. eventEndPtr = 0;
  447. positionPtr = 0;
  448. openInternalEntities = 0;
  449. tagLevel = 0;
  450. tagStack = 0;
  451. freeTagList = 0;
  452. freeBindingList = 0;
  453. inheritedBindings = 0;
  454. attsSize = INIT_ATTS_SIZE;
  455. atts = malloc(attsSize * sizeof(ATTRIBUTE));
  456. nSpecifiedAtts = 0;
  457. dataBuf = malloc(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
  458. groupSize = 0;
  459. groupConnector = 0;
  460. hadExternalDoctype = 0;
  461. unknownEncodingMem = 0;
  462. unknownEncodingRelease = 0;
  463. unknownEncodingData = 0;
  464. unknownEncodingHandlerData = 0;
  465. namespaceSeparator = '!';
  466. #ifdef XML_DTD
  467. parentParser = 0;
  468. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  469. #endif
  470. ns = 0;
  471. poolInit(&tempPool);
  472. poolInit(&temp2Pool);
  473. protocolEncodingName = encodingName ? poolCopyString(&tempPool, encodingName) : 0;
  474. curBase = 0;
  475. if (!dtdInit(&dtd) || !atts || !dataBuf
  476. || (encodingName && !protocolEncodingName)) {
  477. XML_ParserFree(parser);
  478. return 0;
  479. }
  480. dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
  481. XmlInitEncoding(&initEncoding, &encoding, 0);
  482. internalEncoding = XmlGetInternalEncoding();
  483. return parser;
  484. }
  485. XML_Parser XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
  486. {
  487. static
  488. const XML_Char implicitContext[] = {
  489. XML_T('x'), XML_T('m'), XML_T('l'), XML_T('='),
  490. XML_T('h'), XML_T('t'), XML_T('t'), XML_T('p'), XML_T(':'),
  491. XML_T('/'), XML_T('/'), XML_T('w'), XML_T('w'), XML_T('w'),
  492. XML_T('.'), XML_T('w'), XML_T('3'),
  493. XML_T('.'), XML_T('o'), XML_T('r'), XML_T('g'),
  494. XML_T('/'), XML_T('X'), XML_T('M'), XML_T('L'),
  495. XML_T('/'), XML_T('1'), XML_T('9'), XML_T('9'), XML_T('8'),
  496. XML_T('/'), XML_T('n'), XML_T('a'), XML_T('m'), XML_T('e'),
  497. XML_T('s'), XML_T('p'), XML_T('a'), XML_T('c'), XML_T('e'),
  498. XML_T('\0')
  499. };
  500. XML_Parser parser = XML_ParserCreate(encodingName);
  501. if (parser) {
  502. XmlInitEncodingNS(&initEncoding, &encoding, 0);
  503. ns = 1;
  504. internalEncoding = XmlGetInternalEncodingNS();
  505. namespaceSeparator = nsSep;
  506. }
  507. if (!setContext(parser, implicitContext)) {
  508. XML_ParserFree(parser);
  509. return 0;
  510. }
  511. return parser;
  512. }
  513. int XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
  514. {
  515. if (!encodingName)
  516. protocolEncodingName = 0;
  517. else {
  518. protocolEncodingName = poolCopyString(&tempPool, encodingName);
  519. if (!protocolEncodingName)
  520. return 0;
  521. }
  522. return 1;
  523. }
  524. XML_Parser XML_ExternalEntityParserCreate(XML_Parser oldParser,
  525. const XML_Char *context,
  526. const XML_Char *encodingName)
  527. {
  528. XML_Parser parser = oldParser;
  529. DTD *oldDtd = &dtd;
  530. XML_StartElementHandler oldStartElementHandler = startElementHandler;
  531. XML_EndElementHandler oldEndElementHandler = endElementHandler;
  532. XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler;
  533. XML_ProcessingInstructionHandler oldProcessingInstructionHandler = processingInstructionHandler;
  534. XML_CommentHandler oldCommentHandler = commentHandler;
  535. XML_StartCdataSectionHandler oldStartCdataSectionHandler = startCdataSectionHandler;
  536. XML_EndCdataSectionHandler oldEndCdataSectionHandler = endCdataSectionHandler;
  537. XML_DefaultHandler oldDefaultHandler = defaultHandler;
  538. XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler = startNamespaceDeclHandler;
  539. XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler = endNamespaceDeclHandler;
  540. XML_NotStandaloneHandler oldNotStandaloneHandler = notStandaloneHandler;
  541. XML_ExternalEntityRefHandler oldExternalEntityRefHandler = externalEntityRefHandler;
  542. XML_UnknownEncodingHandler oldUnknownEncodingHandler = unknownEncodingHandler;
  543. void *oldUserData = userData;
  544. void *oldHandlerArg = handlerArg;
  545. int oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
  546. void *oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
  547. #ifdef XML_DTD
  548. int oldParamEntityParsing = paramEntityParsing;
  549. #endif
  550. parser = (ns
  551. ? XML_ParserCreateNS(encodingName, namespaceSeparator)
  552. : XML_ParserCreate(encodingName));
  553. if (!parser)
  554. return 0;
  555. startElementHandler = oldStartElementHandler;
  556. endElementHandler = oldEndElementHandler;
  557. characterDataHandler = oldCharacterDataHandler;
  558. processingInstructionHandler = oldProcessingInstructionHandler;
  559. commentHandler = oldCommentHandler;
  560. startCdataSectionHandler = oldStartCdataSectionHandler;
  561. endCdataSectionHandler = oldEndCdataSectionHandler;
  562. defaultHandler = oldDefaultHandler;
  563. startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
  564. endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
  565. notStandaloneHandler = oldNotStandaloneHandler;
  566. externalEntityRefHandler = oldExternalEntityRefHandler;
  567. unknownEncodingHandler = oldUnknownEncodingHandler;
  568. userData = oldUserData;
  569. if (oldUserData == oldHandlerArg)
  570. handlerArg = userData;
  571. else
  572. handlerArg = parser;
  573. if (oldExternalEntityRefHandlerArg != oldParser)
  574. externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
  575. defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
  576. #ifdef XML_DTD
  577. paramEntityParsing = oldParamEntityParsing;
  578. if (context) {
  579. #endif /* XML_DTD */
  580. if (!dtdCopy(&dtd, oldDtd) || !setContext(parser, context)) {
  581. XML_ParserFree(parser);
  582. return 0;
  583. }
  584. processor = externalEntityInitProcessor;
  585. #ifdef XML_DTD
  586. }
  587. else {
  588. dtdSwap(&dtd, oldDtd);
  589. parentParser = oldParser;
  590. XmlPrologStateInitExternalEntity(&prologState);
  591. dtd.complete = 1;
  592. hadExternalDoctype = 1;
  593. }
  594. #endif /* XML_DTD */
  595. return parser;
  596. }
  597. static
  598. void destroyBindings(BINDING *bindings)
  599. {
  600. for (;;) {
  601. BINDING *b = bindings;
  602. if (!b)
  603. break;
  604. bindings = b->nextTagBinding;
  605. free(b->uri);
  606. free(b);
  607. }
  608. }
  609. void XML_ParserFree(XML_Parser parser)
  610. {
  611. for (;;) {
  612. TAG *p;
  613. if (tagStack == 0) {
  614. if (freeTagList == 0)
  615. break;
  616. tagStack = freeTagList;
  617. freeTagList = 0;
  618. }
  619. p = tagStack;
  620. tagStack = tagStack->parent;
  621. free(p->buf);
  622. destroyBindings(p->bindings);
  623. free(p);
  624. }
  625. destroyBindings(freeBindingList);
  626. destroyBindings(inheritedBindings);
  627. poolDestroy(&tempPool);
  628. poolDestroy(&temp2Pool);
  629. #ifdef XML_DTD
  630. if (parentParser) {
  631. if (hadExternalDoctype)
  632. dtd.complete = 0;
  633. dtdSwap(&dtd, &((Parser *)parentParser)->m_dtd);
  634. }
  635. #endif /* XML_DTD */
  636. dtdDestroy(&dtd);
  637. free((void *)atts);
  638. free(groupConnector);
  639. free(buffer);
  640. free(dataBuf);
  641. free(unknownEncodingMem);
  642. if (unknownEncodingRelease)
  643. unknownEncodingRelease(unknownEncodingData);
  644. free(parser);
  645. }
  646. void XML_UseParserAsHandlerArg(XML_Parser parser)
  647. {
  648. handlerArg = parser;
  649. }
  650. void XML_SetUserData(XML_Parser parser, void *p)
  651. {
  652. if (handlerArg == userData)
  653. handlerArg = userData = p;
  654. else
  655. userData = p;
  656. }
  657. int XML_SetBase(XML_Parser parser, const XML_Char *p)
  658. {
  659. if (p) {
  660. p = poolCopyString(&dtd.pool, p);
  661. if (!p)
  662. return 0;
  663. curBase = p;
  664. }
  665. else
  666. curBase = 0;
  667. return 1;
  668. }
  669. const XML_Char *XML_GetBase(XML_Parser parser)
  670. {
  671. return curBase;
  672. }
  673. int XML_GetSpecifiedAttributeCount(XML_Parser parser)
  674. {
  675. return nSpecifiedAtts;
  676. }
  677. void XML_SetElementHandler(XML_Parser parser,
  678. XML_StartElementHandler start,
  679. XML_EndElementHandler end)
  680. {
  681. startElementHandler = start;
  682. endElementHandler = end;
  683. }
  684. void XML_SetCharacterDataHandler(XML_Parser parser,
  685. XML_CharacterDataHandler handler)
  686. {
  687. characterDataHandler = handler;
  688. }
  689. void XML_SetProcessingInstructionHandler(XML_Parser parser,
  690. XML_ProcessingInstructionHandler handler)
  691. {
  692. processingInstructionHandler = handler;
  693. }
  694. void XML_SetCommentHandler(XML_Parser parser,
  695. XML_CommentHandler handler)
  696. {
  697. commentHandler = handler;
  698. }
  699. void XML_SetCdataSectionHandler(XML_Parser parser,
  700. XML_StartCdataSectionHandler start,
  701. XML_EndCdataSectionHandler end)
  702. {
  703. startCdataSectionHandler = start;
  704. endCdataSectionHandler = end;
  705. }
  706. void XML_SetDefaultHandler(XML_Parser parser,
  707. XML_DefaultHandler handler)
  708. {
  709. defaultHandler = handler;
  710. defaultExpandInternalEntities = 0;
  711. }
  712. void XML_SetDefaultHandlerExpand(XML_Parser parser,
  713. XML_DefaultHandler handler)
  714. {
  715. defaultHandler = handler;
  716. defaultExpandInternalEntities = 1;
  717. }
  718. void XML_SetDoctypeDeclHandler(XML_Parser parser,
  719. XML_StartDoctypeDeclHandler start,
  720. XML_EndDoctypeDeclHandler end)
  721. {
  722. startDoctypeDeclHandler = start;
  723. endDoctypeDeclHandler = end;
  724. }
  725. void XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
  726. XML_UnparsedEntityDeclHandler handler)
  727. {
  728. unparsedEntityDeclHandler = handler;
  729. }
  730. void XML_SetNotationDeclHandler(XML_Parser parser,
  731. XML_NotationDeclHandler handler)
  732. {
  733. notationDeclHandler = handler;
  734. }
  735. void XML_SetNamespaceDeclHandler(XML_Parser parser,
  736. XML_StartNamespaceDeclHandler start,
  737. XML_EndNamespaceDeclHandler end)
  738. {
  739. startNamespaceDeclHandler = start;
  740. endNamespaceDeclHandler = end;
  741. }
  742. void XML_SetNotStandaloneHandler(XML_Parser parser,
  743. XML_NotStandaloneHandler handler)
  744. {
  745. notStandaloneHandler = handler;
  746. }
  747. void XML_SetExternalEntityRefHandler(XML_Parser parser,
  748. XML_ExternalEntityRefHandler handler)
  749. {
  750. externalEntityRefHandler = handler;
  751. }
  752. void XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
  753. {
  754. if (arg)
  755. externalEntityRefHandlerArg = arg;
  756. else
  757. externalEntityRefHandlerArg = parser;
  758. }
  759. void XML_SetUnknownEncodingHandler(XML_Parser parser,
  760. XML_UnknownEncodingHandler handler,
  761. void *data)
  762. {
  763. unknownEncodingHandler = handler;
  764. unknownEncodingHandlerData = data;
  765. }
  766. int XML_SetParamEntityParsing(XML_Parser parser,
  767. enum XML_ParamEntityParsing parsing)
  768. {
  769. #ifdef XML_DTD
  770. paramEntityParsing = parsing;
  771. return 1;
  772. #else
  773. return parsing == XML_PARAM_ENTITY_PARSING_NEVER;
  774. #endif
  775. }
  776. int XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
  777. {
  778. if (len == 0) {
  779. if (!isFinal)
  780. return 1;
  781. positionPtr = bufferPtr;
  782. errorCode = processor(parser, bufferPtr, parseEndPtr = bufferEnd, 0);
  783. if (errorCode == XML_ERROR_NONE)
  784. return 1;
  785. eventEndPtr = eventPtr;
  786. processor = errorProcessor;
  787. return 0;
  788. }
  789. #ifndef XML_CONTEXT_BYTES
  790. else if (bufferPtr == bufferEnd) {
  791. const char *end;
  792. int nLeftOver;
  793. parseEndByteIndex += len;
  794. positionPtr = s;
  795. if (isFinal) {
  796. errorCode = processor(parser, s, parseEndPtr = s + len, 0);
  797. if (errorCode == XML_ERROR_NONE)
  798. return 1;
  799. eventEndPtr = eventPtr;
  800. processor = errorProcessor;
  801. return 0;
  802. }
  803. errorCode = processor(parser, s, parseEndPtr = s + len, &end);
  804. if (errorCode != XML_ERROR_NONE) {
  805. eventEndPtr = eventPtr;
  806. processor = errorProcessor;
  807. return 0;
  808. }
  809. XmlUpdatePosition(encoding, positionPtr, end, &position);
  810. nLeftOver = s + len - end;
  811. if (nLeftOver) {
  812. if (buffer == 0 || nLeftOver > bufferLim - buffer) {
  813. /* FIXME avoid integer overflow */
  814. buffer = buffer == 0 ? malloc(len * 2) : realloc(buffer, len * 2);
  815. /* FIXME storage leak if realloc fails */
  816. if (!buffer) {
  817. errorCode = XML_ERROR_NO_MEMORY;
  818. eventPtr = eventEndPtr = 0;
  819. processor = errorProcessor;
  820. return 0;
  821. }
  822. bufferLim = buffer + len * 2;
  823. }
  824. memcpy(buffer, end, nLeftOver);
  825. bufferPtr = buffer;
  826. bufferEnd = buffer + nLeftOver;
  827. }
  828. return 1;
  829. }
  830. #endif /* not defined XML_CONTEXT_BYTES */
  831. else {
  832. memcpy(XML_GetBuffer(parser, len), s, len);
  833. return XML_ParseBuffer(parser, len, isFinal);
  834. }
  835. }
  836. int XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
  837. {
  838. const char *start = bufferPtr;
  839. positionPtr = start;
  840. bufferEnd += len;
  841. parseEndByteIndex += len;
  842. errorCode = processor(parser, start, parseEndPtr = bufferEnd,
  843. isFinal ? (const char **)0 : &bufferPtr);
  844. if (errorCode == XML_ERROR_NONE) {
  845. if (!isFinal)
  846. XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
  847. return 1;
  848. }
  849. else {
  850. eventEndPtr = eventPtr;
  851. processor = errorProcessor;
  852. return 0;
  853. }
  854. }
  855. void *XML_GetBuffer(XML_Parser parser, int len)
  856. {
  857. if (len > bufferLim - bufferEnd) {
  858. /* FIXME avoid integer overflow */
  859. int neededSize = len + (bufferEnd - bufferPtr);
  860. #ifdef XML_CONTEXT_BYTES
  861. int keep = bufferPtr - buffer;
  862. if (keep > XML_CONTEXT_BYTES)
  863. keep = XML_CONTEXT_BYTES;
  864. neededSize += keep;
  865. #endif /* defined XML_CONTEXT_BYTES */
  866. if (neededSize <= bufferLim - buffer) {
  867. #ifdef XML_CONTEXT_BYTES
  868. if (keep < bufferPtr - buffer) {
  869. int offset = (bufferPtr - buffer) - keep;
  870. memmove(buffer, &buffer[offset], bufferEnd - bufferPtr + keep);
  871. bufferEnd -= offset;
  872. bufferPtr -= offset;
  873. }
  874. #else
  875. memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
  876. bufferEnd = buffer + (bufferEnd - bufferPtr);
  877. bufferPtr = buffer;
  878. #endif /* not defined XML_CONTEXT_BYTES */
  879. }
  880. else {
  881. char *newBuf;
  882. int bufferSize = bufferLim - bufferPtr;
  883. if (bufferSize == 0)
  884. bufferSize = INIT_BUFFER_SIZE;
  885. do {
  886. bufferSize *= 2;
  887. } while (bufferSize < neededSize);
  888. newBuf = malloc(bufferSize);
  889. if (newBuf == 0) {
  890. errorCode = XML_ERROR_NO_MEMORY;
  891. return 0;
  892. }
  893. bufferLim = newBuf + bufferSize;
  894. #ifdef XML_CONTEXT_BYTES
  895. if (bufferPtr) {
  896. int keep = bufferPtr - buffer;
  897. if (keep > XML_CONTEXT_BYTES)
  898. keep = XML_CONTEXT_BYTES;
  899. memcpy(newBuf, &bufferPtr[-keep], bufferEnd - bufferPtr + keep);
  900. free(buffer);
  901. buffer = newBuf;
  902. bufferEnd = buffer + (bufferEnd - bufferPtr) + keep;
  903. bufferPtr = buffer + keep;
  904. }
  905. else {
  906. bufferEnd = newBuf + (bufferEnd - bufferPtr);
  907. bufferPtr = buffer = newBuf;
  908. }
  909. #else
  910. if (bufferPtr) {
  911. memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
  912. free(buffer);
  913. }
  914. bufferEnd = newBuf + (bufferEnd - bufferPtr);
  915. bufferPtr = buffer = newBuf;
  916. #endif /* not defined XML_CONTEXT_BYTES */
  917. }
  918. }
  919. return bufferEnd;
  920. }
  921. enum XML_Error XML_GetErrorCode(XML_Parser parser)
  922. {
  923. return errorCode;
  924. }
  925. long XML_GetCurrentByteIndex(XML_Parser parser)
  926. {
  927. if (eventPtr)
  928. return parseEndByteIndex - (parseEndPtr - eventPtr);
  929. return -1;
  930. }
  931. int XML_GetCurrentByteCount(XML_Parser parser)
  932. {
  933. if (eventEndPtr && eventPtr)
  934. return eventEndPtr - eventPtr;
  935. return 0;
  936. }
  937. const char * XML_GetInputContext(XML_Parser parser, int *offset, int *size)
  938. {
  939. #ifdef XML_CONTEXT_BYTES
  940. if (eventPtr && buffer) {
  941. *offset = eventPtr - buffer;
  942. *size = bufferEnd - buffer;
  943. return buffer;
  944. }
  945. #endif /* defined XML_CONTEXT_BYTES */
  946. return (char *) 0;
  947. }
  948. int XML_GetCurrentLineNumber(XML_Parser parser)
  949. {
  950. if (eventPtr) {
  951. XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
  952. positionPtr = eventPtr;
  953. }
  954. return position.lineNumber + 1;
  955. }
  956. int XML_GetCurrentColumnNumber(XML_Parser parser)
  957. {
  958. if (eventPtr) {
  959. XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
  960. positionPtr = eventPtr;
  961. }
  962. return position.columnNumber;
  963. }
  964. void XML_DefaultCurrent(XML_Parser parser)
  965. {
  966. if (defaultHandler) {
  967. if (openInternalEntities)
  968. reportDefault(parser,
  969. internalEncoding,
  970. openInternalEntities->internalEventPtr,
  971. openInternalEntities->internalEventEndPtr);
  972. else
  973. reportDefault(parser, encoding, eventPtr, eventEndPtr);
  974. }
  975. }
  976. const XML_LChar *XML_ErrorString(int code)
  977. {
  978. static const XML_LChar *message[] = {
  979. 0,
  980. XML_T("out of memory"),
  981. XML_T("syntax error"),
  982. XML_T("no element found"),
  983. XML_T("not well-formed"),
  984. XML_T("unclosed token"),
  985. XML_T("unclosed token"),
  986. XML_T("mismatched tag"),
  987. XML_T("duplicate attribute"),
  988. XML_T("junk after document element"),
  989. XML_T("illegal parameter entity reference"),
  990. XML_T("undefined entity"),
  991. XML_T("recursive entity reference"),
  992. XML_T("asynchronous entity"),
  993. XML_T("reference to invalid character number"),
  994. XML_T("reference to binary entity"),
  995. XML_T("reference to external entity in attribute"),
  996. XML_T("xml processing instruction not at start of external entity"),
  997. XML_T("unknown encoding"),
  998. XML_T("encoding specified in XML declaration is incorrect"),
  999. XML_T("unclosed CDATA section"),
  1000. XML_T("error in processing external entity reference"),
  1001. XML_T("document is not standalone")
  1002. };
  1003. if (code > 0 && code < sizeof(message)/sizeof(message[0]))
  1004. return message[code];
  1005. return 0;
  1006. }
  1007. static
  1008. enum XML_Error contentProcessor(XML_Parser parser,
  1009. const char *start,
  1010. const char *end,
  1011. const char **endPtr)
  1012. {
  1013. return doContent(parser, 0, encoding, start, end, endPtr);
  1014. }
  1015. static
  1016. enum XML_Error externalEntityInitProcessor(XML_Parser parser,
  1017. const char *start,
  1018. const char *end,
  1019. const char **endPtr)
  1020. {
  1021. enum XML_Error result = initializeEncoding(parser);
  1022. if (result != XML_ERROR_NONE)
  1023. return result;
  1024. processor = externalEntityInitProcessor2;
  1025. return externalEntityInitProcessor2(parser, start, end, endPtr);
  1026. }
  1027. static
  1028. enum XML_Error externalEntityInitProcessor2(XML_Parser parser,
  1029. const char *start,
  1030. const char *end,
  1031. const char **endPtr)
  1032. {
  1033. const char *next;
  1034. int tok = XmlContentTok(encoding, start, end, &next);
  1035. switch (tok) {
  1036. case XML_TOK_BOM:
  1037. start = next;
  1038. break;
  1039. case XML_TOK_PARTIAL:
  1040. if (endPtr) {
  1041. *endPtr = start;
  1042. return XML_ERROR_NONE;
  1043. }
  1044. eventPtr = start;
  1045. return XML_ERROR_UNCLOSED_TOKEN;
  1046. case XML_TOK_PARTIAL_CHAR:
  1047. if (endPtr) {
  1048. *endPtr = start;
  1049. return XML_ERROR_NONE;
  1050. }
  1051. eventPtr = start;
  1052. return XML_ERROR_PARTIAL_CHAR;
  1053. }
  1054. processor = externalEntityInitProcessor3;
  1055. return externalEntityInitProcessor3(parser, start, end, endPtr);
  1056. }
  1057. static
  1058. enum XML_Error externalEntityInitProcessor3(XML_Parser parser,
  1059. const char *start,
  1060. const char *end,
  1061. const char **endPtr)
  1062. {
  1063. const char *next;
  1064. int tok = XmlContentTok(encoding, start, end, &next);
  1065. switch (tok) {
  1066. case XML_TOK_XML_DECL:
  1067. {
  1068. enum XML_Error result = processXmlDecl(parser, 1, start, next);
  1069. if (result != XML_ERROR_NONE)
  1070. return result;
  1071. start = next;
  1072. }
  1073. break;
  1074. case XML_TOK_PARTIAL:
  1075. if (endPtr) {
  1076. *endPtr = start;
  1077. return XML_ERROR_NONE;
  1078. }
  1079. eventPtr = start;
  1080. return XML_ERROR_UNCLOSED_TOKEN;
  1081. case XML_TOK_PARTIAL_CHAR:
  1082. if (endPtr) {
  1083. *endPtr = start;
  1084. return XML_ERROR_NONE;
  1085. }
  1086. eventPtr = start;
  1087. return XML_ERROR_PARTIAL_CHAR;
  1088. }
  1089. processor = externalEntityContentProcessor;
  1090. tagLevel = 1;
  1091. return doContent(parser, 1, encoding, start, end, endPtr);
  1092. }
  1093. static
  1094. enum XML_Error externalEntityContentProcessor(XML_Parser parser,
  1095. const char *start,
  1096. const char *end,
  1097. const char **endPtr)
  1098. {
  1099. return doContent(parser, 1, encoding, start, end, endPtr);
  1100. }
  1101. static enum XML_Error
  1102. doContent(XML_Parser parser,
  1103. int startTagLevel,
  1104. const ENCODING *enc,
  1105. const char *s,
  1106. const char *end,
  1107. const char **nextPtr)
  1108. {
  1109. const char **eventPP;
  1110. const char **eventEndPP;
  1111. if (enc == encoding) {
  1112. eventPP = &eventPtr;
  1113. eventEndPP = &eventEndPtr;
  1114. }
  1115. else {
  1116. eventPP = &(openInternalEntities->internalEventPtr);
  1117. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  1118. }
  1119. *eventPP = s;
  1120. for (;;) {
  1121. const char *next = s; /* XmlContentTok doesn't always set the last arg */
  1122. int tok = XmlContentTok(enc, s, end, &next);
  1123. *eventEndPP = next;
  1124. switch (tok) {
  1125. case XML_TOK_TRAILING_CR:
  1126. if (nextPtr) {
  1127. *nextPtr = s;
  1128. return XML_ERROR_NONE;
  1129. }
  1130. *eventEndPP = end;
  1131. if (characterDataHandler) {
  1132. XML_Char c = 0xA;
  1133. characterDataHandler(handlerArg, &c, 1);
  1134. }
  1135. else if (defaultHandler)
  1136. reportDefault(parser, enc, s, end);
  1137. if (startTagLevel == 0)
  1138. return XML_ERROR_NO_ELEMENTS;
  1139. if (tagLevel != startTagLevel)
  1140. return XML_ERROR_ASYNC_ENTITY;
  1141. return XML_ERROR_NONE;
  1142. case XML_TOK_NONE:
  1143. if (nextPtr) {
  1144. *nextPtr = s;
  1145. return XML_ERROR_NONE;
  1146. }
  1147. if (startTagLevel > 0) {
  1148. if (tagLevel != startTagLevel)
  1149. return XML_ERROR_ASYNC_ENTITY;
  1150. return XML_ERROR_NONE;
  1151. }
  1152. return XML_ERROR_NO_ELEMENTS;
  1153. case XML_TOK_INVALID:
  1154. *eventPP = next;
  1155. return XML_ERROR_INVALID_TOKEN;
  1156. case XML_TOK_PARTIAL:
  1157. if (nextPtr) {
  1158. *nextPtr = s;
  1159. return XML_ERROR_NONE;
  1160. }
  1161. return XML_ERROR_UNCLOSED_TOKEN;
  1162. case XML_TOK_PARTIAL_CHAR:
  1163. if (nextPtr) {
  1164. *nextPtr = s;
  1165. return XML_ERROR_NONE;
  1166. }
  1167. return XML_ERROR_PARTIAL_CHAR;
  1168. case XML_TOK_ENTITY_REF:
  1169. {
  1170. const XML_Char *name;
  1171. ENTITY *entity;
  1172. XML_Char ch = XmlPredefinedEntityName(enc,
  1173. s + enc->minBytesPerChar,
  1174. next - enc->minBytesPerChar);
  1175. if (ch) {
  1176. if (characterDataHandler)
  1177. characterDataHandler(handlerArg, &ch, 1);
  1178. else if (defaultHandler)
  1179. reportDefault(parser, enc, s, next);
  1180. break;
  1181. }
  1182. name = poolStoreString(&dtd.pool, enc,
  1183. s + enc->minBytesPerChar,
  1184. next - enc->minBytesPerChar);
  1185. if (!name)
  1186. return XML_ERROR_NO_MEMORY;
  1187. entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
  1188. poolDiscard(&dtd.pool);
  1189. if (!entity) {
  1190. if (dtd.complete || dtd.standalone)
  1191. return XML_ERROR_UNDEFINED_ENTITY;
  1192. if (defaultHandler)
  1193. reportDefault(parser, enc, s, next);
  1194. break;
  1195. }
  1196. if (entity->open)
  1197. return XML_ERROR_RECURSIVE_ENTITY_REF;
  1198. if (entity->notation)
  1199. return XML_ERROR_BINARY_ENTITY_REF;
  1200. if (entity) {
  1201. if (entity->textPtr) {
  1202. enum XML_Error result;
  1203. OPEN_INTERNAL_ENTITY openEntity;
  1204. if (defaultHandler && !defaultExpandInternalEntities) {
  1205. reportDefault(parser, enc, s, next);
  1206. break;
  1207. }
  1208. entity->open = 1;
  1209. openEntity.next = openInternalEntities;
  1210. openInternalEntities = &openEntity;
  1211. openEntity.entity = entity;
  1212. openEntity.internalEventPtr = 0;
  1213. openEntity.internalEventEndPtr = 0;
  1214. result = doContent(parser,
  1215. tagLevel,
  1216. internalEncoding,
  1217. (char *)entity->textPtr,
  1218. (char *)(entity->textPtr + entity->textLen),
  1219. 0);
  1220. entity->open = 0;
  1221. openInternalEntities = openEntity.next;
  1222. if (result)
  1223. return result;
  1224. }
  1225. else if (externalEntityRefHandler) {
  1226. const XML_Char *context;
  1227. entity->open = 1;
  1228. context = getContext(parser);
  1229. entity->open = 0;
  1230. if (!context)
  1231. return XML_ERROR_NO_MEMORY;
  1232. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  1233. context,
  1234. entity->base,
  1235. entity->systemId,
  1236. entity->publicId))
  1237. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  1238. poolDiscard(&tempPool);
  1239. }
  1240. else if (defaultHandler)
  1241. reportDefault(parser, enc, s, next);
  1242. }
  1243. break;
  1244. }
  1245. case XML_TOK_START_TAG_WITH_ATTS:
  1246. if (!startElementHandler) {
  1247. enum XML_Error result = storeAtts(parser, enc, s, 0, 0);
  1248. if (result)
  1249. return result;
  1250. }
  1251. /* fall through */
  1252. case XML_TOK_START_TAG_NO_ATTS:
  1253. {
  1254. TAG *tag;
  1255. if (freeTagList) {
  1256. tag = freeTagList;
  1257. freeTagList = freeTagList->parent;
  1258. }
  1259. else {
  1260. tag = malloc(sizeof(TAG));
  1261. if (!tag)
  1262. return XML_ERROR_NO_MEMORY;
  1263. tag->buf = malloc(INIT_TAG_BUF_SIZE);
  1264. if (!tag->buf)
  1265. return XML_ERROR_NO_MEMORY;
  1266. tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
  1267. }
  1268. tag->bindings = 0;
  1269. tag->parent = tagStack;
  1270. tagStack = tag;
  1271. tag->name.localPart = 0;
  1272. tag->rawName = s + enc->minBytesPerChar;
  1273. tag->rawNameLength = XmlNameLength(enc, tag->rawName);
  1274. if (nextPtr) {
  1275. /* Need to guarantee that:
  1276. tag->buf + ROUND_UP(tag->rawNameLength, sizeof(XML_Char)) <= tag->bufEnd - sizeof(XML_Char) */
  1277. if (tag->rawNameLength + (int)(sizeof(XML_Char) - 1) + (int)sizeof(XML_Char) > tag->bufEnd - tag->buf) {
  1278. int bufSize = tag->rawNameLength * 4;
  1279. bufSize = ROUND_UP(bufSize, sizeof(XML_Char));
  1280. tag->buf = realloc(tag->buf, bufSize);
  1281. if (!tag->buf)
  1282. return XML_ERROR_NO_MEMORY;
  1283. tag->bufEnd = tag->buf + bufSize;
  1284. }
  1285. memcpy(tag->buf, tag->rawName, tag->rawNameLength);
  1286. tag->rawName = tag->buf;
  1287. }
  1288. ++tagLevel;
  1289. if (startElementHandler) {
  1290. enum XML_Error result;
  1291. XML_Char *toPtr;
  1292. for (;;) {
  1293. const char *rawNameEnd = tag->rawName + tag->rawNameLength;
  1294. const char *fromPtr = tag->rawName;
  1295. int bufSize;
  1296. if (nextPtr)
  1297. toPtr = (XML_Char *)(tag->buf + ROUND_UP(tag->rawNameLength, sizeof(XML_Char)));
  1298. else
  1299. toPtr = (XML_Char *)tag->buf;
  1300. tag->name.str = toPtr;
  1301. XmlConvert(enc,
  1302. &fromPtr, rawNameEnd,
  1303. (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
  1304. if (fromPtr == rawNameEnd)
  1305. break;
  1306. bufSize = (tag->bufEnd - tag->buf) << 1;
  1307. tag->buf = realloc(tag->buf, bufSize);
  1308. if (!tag->buf)
  1309. return XML_ERROR_NO_MEMORY;
  1310. tag->bufEnd = tag->buf + bufSize;
  1311. if (nextPtr)
  1312. tag->rawName = tag->buf;
  1313. }
  1314. *toPtr = XML_T('\0');
  1315. result = storeAtts(parser, enc, s, &(tag->name), &(tag->bindings));
  1316. if (result)
  1317. return result;
  1318. startElementHandler(handlerArg, tag->name.str, (const XML_Char **)atts);
  1319. poolClear(&tempPool);
  1320. }
  1321. else {
  1322. tag->name.str = 0;
  1323. if (defaultHandler)
  1324. reportDefault(parser, enc, s, next);
  1325. }
  1326. break;
  1327. }
  1328. case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
  1329. if (!startElementHandler) {
  1330. enum XML_Error result = storeAtts(parser, enc, s, 0, 0);
  1331. if (result)
  1332. return result;
  1333. }
  1334. /* fall through */
  1335. case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
  1336. if (startElementHandler || endElementHandler) {
  1337. const char *rawName = s + enc->minBytesPerChar;
  1338. enum XML_Error result;
  1339. BINDING *bindings = 0;
  1340. TAG_NAME name;
  1341. name.str = poolStoreString(&tempPool, enc, rawName,
  1342. rawName + XmlNameLength(enc, rawName));
  1343. if (!name.str)
  1344. return XML_ERROR_NO_MEMORY;
  1345. poolFinish(&tempPool);
  1346. result = storeAtts(parser, enc, s, &name, &bindings);
  1347. if (result)
  1348. return result;
  1349. poolFinish(&tempPool);
  1350. if (startElementHandler)
  1351. startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
  1352. if (endElementHandler) {
  1353. if (startElementHandler)
  1354. *eventPP = *eventEndPP;
  1355. endElementHandler(handlerArg, name.str);
  1356. }
  1357. poolClear(&tempPool);
  1358. while (bindings) {
  1359. BINDING *b = bindings;
  1360. if (endNamespaceDeclHandler)
  1361. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  1362. bindings = bindings->nextTagBinding;
  1363. b->nextTagBinding = freeBindingList;
  1364. freeBindingList = b;
  1365. b->prefix->binding = b->prevPrefixBinding;
  1366. }
  1367. }
  1368. else if (defaultHandler)
  1369. reportDefault(parser, enc, s, next);
  1370. if (tagLevel == 0)
  1371. return epilogProcessor(parser, next, end, nextPtr);
  1372. break;
  1373. case XML_TOK_END_TAG:
  1374. if (tagLevel == startTagLevel)
  1375. return XML_ERROR_ASYNC_ENTITY;
  1376. else {
  1377. int len;
  1378. const char *rawName;
  1379. TAG *tag = tagStack;
  1380. tagStack = tag->parent;
  1381. tag->parent = freeTagList;
  1382. freeTagList = tag;
  1383. rawName = s + enc->minBytesPerChar*2;
  1384. len = XmlNameLength(enc, rawName);
  1385. if (len != tag->rawNameLength
  1386. || memcmp(tag->rawName, rawName, len) != 0) {
  1387. *eventPP = rawName;
  1388. return XML_ERROR_TAG_MISMATCH;
  1389. }
  1390. --tagLevel;
  1391. if (endElementHandler && tag->name.str) {
  1392. if (tag->name.localPart) {
  1393. XML_Char *to = (XML_Char *)tag->name.str + tag->name.uriLen;
  1394. const XML_Char *from = tag->name.localPart;
  1395. while ((*to++ = *from++) != 0)
  1396. ;
  1397. }
  1398. endElementHandler(handlerArg, tag->name.str);
  1399. }
  1400. else if (defaultHandler)
  1401. reportDefault(parser, enc, s, next);
  1402. while (tag->bindings) {
  1403. BINDING *b = tag->bindings;
  1404. if (endNamespaceDeclHandler)
  1405. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  1406. tag->bindings = tag->bindings->nextTagBinding;
  1407. b->nextTagBinding = freeBindingList;
  1408. freeBindingList = b;
  1409. b->prefix->binding = b->prevPrefixBinding;
  1410. }
  1411. if (tagLevel == 0)
  1412. return epilogProcessor(parser, next, end, nextPtr);
  1413. }
  1414. break;
  1415. case XML_TOK_CHAR_REF:
  1416. {
  1417. int n = XmlCharRefNumber(enc, s);
  1418. if (n < 0)
  1419. return XML_ERROR_BAD_CHAR_REF;
  1420. if (characterDataHandler) {
  1421. XML_Char buf[XML_ENCODE_MAX];
  1422. characterDataHandler(handlerArg, buf, XmlEncode(n, (ICHAR *)buf));
  1423. }
  1424. else if (defaultHandler)
  1425. reportDefault(parser, enc, s, next);
  1426. }
  1427. break;
  1428. case XML_TOK_XML_DECL:
  1429. return XML_ERROR_MISPLACED_XML_PI;
  1430. case XML_TOK_DATA_NEWLINE:
  1431. if (characterDataHandler) {
  1432. XML_Char c = 0xA;
  1433. characterDataHandler(handlerArg, &c, 1);
  1434. }
  1435. else if (defaultHandler)
  1436. reportDefault(parser, enc, s, next);
  1437. break;
  1438. case XML_TOK_CDATA_SECT_OPEN:
  1439. {
  1440. enum XML_Error result;
  1441. if (startCdataSectionHandler)
  1442. startCdataSectionHandler(handlerArg);
  1443. #if 0
  1444. /* Suppose you doing a transformation on a document that involves
  1445. changing only the character data. You set up a defaultHandler
  1446. and a characterDataHandler. The defaultHandler simply copies
  1447. characters through. The characterDataHandler does the transformation
  1448. and writes the characters out escaping them as necessary. This case
  1449. will fail to work if we leave out the following two lines (because &
  1450. and < inside CDATA sections will be incorrectly escaped).
  1451. However, now we have a start/endCdataSectionHandler, so it seems
  1452. easier to let the user deal with this. */
  1453. else if (characterDataHandler)
  1454. characterDataHandler(handlerArg, dataBuf, 0);
  1455. #endif
  1456. else if (defaultHandler)
  1457. reportDefault(parser, enc, s, next);
  1458. result = doCdataSection(parser, enc, &next, end, nextPtr);
  1459. if (!next) {
  1460. processor = cdataSectionProcessor;
  1461. return result;
  1462. }
  1463. }
  1464. break;
  1465. case XML_TOK_TRAILING_RSQB:
  1466. if (nextPtr) {
  1467. *nextPtr = s;
  1468. return XML_ERROR_NONE;
  1469. }
  1470. if (characterDataHandler) {
  1471. if (MUST_CONVERT(enc, s)) {
  1472. ICHAR *dataPtr = (ICHAR *)dataBuf;
  1473. XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
  1474. characterDataHandler(handlerArg, dataBuf, dataPtr - (ICHAR *)dataBuf);
  1475. }
  1476. else
  1477. characterDataHandler(handlerArg,
  1478. (XML_Char *)s,
  1479. (XML_Char *)end - (XML_Char *)s);
  1480. }
  1481. else if (defaultHandler)
  1482. reportDefault(parser, enc, s, end);
  1483. if (startTagLevel == 0) {
  1484. *eventPP = end;
  1485. return XML_ERROR_NO_ELEMENTS;
  1486. }
  1487. if (tagLevel != startTagLevel) {
  1488. *eventPP = end;
  1489. return XML_ERROR_ASYNC_ENTITY;
  1490. }
  1491. return XML_ERROR_NONE;
  1492. case XML_TOK_DATA_CHARS:
  1493. if (characterDataHandler) {
  1494. if (MUST_CONVERT(enc, s)) {
  1495. for (;;) {
  1496. ICHAR *dataPtr = (ICHAR *)dataBuf;
  1497. XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
  1498. *eventEndPP = s;
  1499. characterDataHandler(handlerArg, dataBuf, dataPtr - (ICHAR *)dataBuf);
  1500. if (s == next)
  1501. break;
  1502. *eventPP = s;
  1503. }
  1504. }
  1505. else
  1506. characterDataHandler(handlerArg,
  1507. (XML_Char *)s,
  1508. (XML_Char *)next - (XML_Char *)s);
  1509. }
  1510. else if (defaultHandler)
  1511. reportDefault(parser, enc, s, next);
  1512. break;
  1513. case XML_TOK_PI:
  1514. if (!reportProcessingInstruction(parser, enc, s, next))
  1515. return XML_ERROR_NO_MEMORY;
  1516. break;
  1517. case XML_TOK_COMMENT:
  1518. if (!reportComment(parser, enc, s, next))
  1519. return XML_ERROR_NO_MEMORY;
  1520. break;
  1521. default:
  1522. if (defaultHandler)
  1523. reportDefault(parser, enc, s, next);
  1524. break;
  1525. }
  1526. *eventPP = s = next;
  1527. }
  1528. /* not reached */
  1529. }
  1530. /* If tagNamePtr is non-null, build a real list of attributes,
  1531. otherwise just check the attributes for well-formedness. */
  1532. static enum XML_Error storeAtts(XML_Parser parser, const ENCODING *enc,
  1533. const char *attStr, TAG_NAME *tagNamePtr,
  1534. BINDING **bindingsPtr)
  1535. {
  1536. ELEMENT_TYPE *elementType = 0;
  1537. int nDefaultAtts = 0;
  1538. const XML_Char **appAtts;
  1539. int attIndex = 0;
  1540. int i;
  1541. int n;
  1542. int nPrefixes = 0;
  1543. BINDING *binding;
  1544. const XML_Char *localPart;
  1545. if (tagNamePtr) {
  1546. elementType = (ELEMENT_TYPE *)lookup(&dtd.elementTypes, tagNamePtr->str, 0);
  1547. if (!elementType) {
  1548. tagNamePtr->str = poolCopyString(&dtd.pool, tagNamePtr->str);
  1549. if (!tagNamePtr->str)
  1550. return XML_ERROR_NO_MEMORY;
  1551. elementType = (ELEMENT_TYPE *)lookup(&dtd.elementTypes, tagNamePtr->str, sizeof(ELEMENT_TYPE));
  1552. if (!elementType)
  1553. return XML_ERROR_NO_MEMORY;
  1554. if (ns && !setElementTypePrefix(parser, elementType))
  1555. return XML_ERROR_NO_MEMORY;
  1556. }
  1557. nDefaultAtts = elementType->nDefaultAtts;
  1558. }
  1559. n = XmlGetAttributes(enc, attStr, attsSize, atts);
  1560. if (n + nDefaultAtts > attsSize) {
  1561. int oldAttsSize = attsSize;
  1562. attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
  1563. atts = realloc((void *)atts, attsSize * sizeof(ATTRIBUTE));
  1564. if (!atts)
  1565. return XML_ERROR_NO_MEMORY;
  1566. if (n > oldAttsSize)
  1567. XmlGetAttributes(enc, attStr, n, atts);
  1568. }
  1569. appAtts = (const XML_Char **)atts;
  1570. for (i = 0; i < n; i++) {
  1571. ATTRIBUTE_ID *attId = getAttributeId(parser, enc, atts[i].name,
  1572. atts[i].name
  1573. + XmlNameLength(enc, atts[i].name));
  1574. if (!attId)
  1575. return XML_ERROR_NO_MEMORY;
  1576. if ((attId->name)[-1]) {
  1577. if (enc == encoding)
  1578. eventPtr = atts[i].name;
  1579. return XML_ERROR_DUPLICATE_ATTRIBUTE;
  1580. }
  1581. (attId->name)[-1] = 1;
  1582. appAtts[attIndex++] = attId->name;
  1583. if (!atts[i].normalized) {
  1584. enum XML_Error result;
  1585. int isCdata = 1;
  1586. if (attId->maybeTokenized) {
  1587. int j;
  1588. for (j = 0; j < nDefaultAtts; j++) {
  1589. if (attId == elementType->defaultAtts[j].id) {
  1590. isCdata = elementType->defaultAtts[j].isCdata;
  1591. break;
  1592. }
  1593. }
  1594. }
  1595. result = storeAttributeValue(parser, enc, isCdata,
  1596. atts[i].valuePtr, atts[i].valueEnd,
  1597. &tempPool);
  1598. if (result)
  1599. return result;
  1600. if (tagNamePtr) {
  1601. appAtts[attIndex] = poolStart(&tempPool);
  1602. poolFinish(&tempPool);
  1603. }
  1604. else
  1605. poolDiscard(&tempPool);
  1606. }
  1607. else if (tagNamePtr) {
  1608. appAtts[attIndex] = poolStoreString(&tempPool, enc, atts[i].valuePtr, atts[i].valueEnd);
  1609. if (appAtts[attIndex] == 0)
  1610. return XML_ERROR_NO_MEMORY;
  1611. poolFinish(&tempPool);
  1612. }
  1613. if (attId->prefix && tagNamePtr) {
  1614. if (attId->xmlns) {
  1615. if (!addBinding(parser, attId->prefix, attId, appAtts[attIndex], bindingsPtr))
  1616. return XML_ERROR_NO_MEMORY;
  1617. --attIndex;
  1618. }
  1619. else {
  1620. attIndex++;
  1621. nPrefixes++;
  1622. (attId->name)[-1] = 2;
  1623. }
  1624. }
  1625. else
  1626. attIndex++;
  1627. }
  1628. nSpecifiedAtts = attIndex;
  1629. if (tagNamePtr) {
  1630. int j;
  1631. for (j = 0; j < nDefaultAtts; j++) {
  1632. const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + j;
  1633. if (!(da->id->name)[-1] && da->value) {
  1634. if (da->id->prefix) {
  1635. if (da->id->xmlns) {
  1636. if (!addBinding(parser, da->id->prefix, da->id, da->value, bindingsPtr))
  1637. return XML_ERROR_NO_MEMORY;
  1638. }
  1639. else {
  1640. (da->id->name)[-1] = 2;
  1641. nPrefixes++;
  1642. appAtts[attIndex++] = da->id->name;
  1643. appAtts[attIndex++] = da->value;
  1644. }
  1645. }
  1646. else {
  1647. (da->id->name)[-1] = 1;
  1648. appAtts[attIndex++] = da->id->name;
  1649. appAtts[attIndex++] = da->value;
  1650. }
  1651. }
  1652. }
  1653. appAtts[attIndex] = 0;
  1654. }
  1655. i = 0;
  1656. if (nPrefixes) {
  1657. for (; i < attIndex; i += 2) {
  1658. if (appAtts[i][-1] == 2) {
  1659. ATTRIBUTE_ID *id;
  1660. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1661. id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, appAtts[i], 0);
  1662. if (id->prefix->binding) {
  1663. int j;
  1664. const BINDING *b = id->prefix->binding;
  1665. const XML_Char *s = appAtts[i];
  1666. for (j = 0; j < b->uriLen; j++) {
  1667. if (!poolAppendChar(&tempPool, b->uri[j]))
  1668. return XML_ERROR_NO_MEMORY;
  1669. }
  1670. while (*s++ != ':')
  1671. ;
  1672. do {
  1673. if (!poolAppendChar(&tempPool, *s))
  1674. return XML_ERROR_NO_MEMORY;
  1675. } while (*s++);
  1676. appAtts[i] = poolStart(&tempPool);
  1677. poolFinish(&tempPool);
  1678. }
  1679. if (!--nPrefixes)
  1680. break;
  1681. }
  1682. else
  1683. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1684. }
  1685. }
  1686. for (; i < attIndex; i += 2)
  1687. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1688. if (!tagNamePtr)
  1689. return XML_ERROR_NONE;
  1690. for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
  1691. binding->attId->name[-1] = 0;
  1692. if (elementType->prefix) {
  1693. binding = elementType->prefix->binding;
  1694. if (!binding)
  1695. return XML_ERROR_NONE;
  1696. localPart = tagNamePtr->str;
  1697. while (*localPart++ != XML_T(':'))
  1698. ;
  1699. }
  1700. else if (dtd.defaultPrefix.binding) {
  1701. binding = dtd.defaultPrefix.binding;
  1702. localPart = tagNamePtr->str;
  1703. }
  1704. else
  1705. return XML_ERROR_NONE;
  1706. tagNamePtr->localPart = localPart;
  1707. tagNamePtr->uriLen = binding->uriLen;
  1708. i = binding->uriLen;
  1709. do {
  1710. if (i == binding->uriAlloc) {
  1711. binding->uri = realloc(binding->uri, binding->uriAlloc *= 2);
  1712. if (!binding->uri)
  1713. return XML_ERROR_NO_MEMORY;
  1714. }
  1715. binding->uri[i++] = *localPart;
  1716. } while (*localPart++);
  1717. tagNamePtr->str = binding->uri;
  1718. return XML_ERROR_NONE;
  1719. }
  1720. static
  1721. int addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId, const XML_Char *uri, BINDING **bindingsPtr)
  1722. {
  1723. BINDING *b;
  1724. int len;
  1725. for (len = 0; uri[len]; len++)
  1726. ;
  1727. if (namespaceSeparator)
  1728. len++;
  1729. if (freeBindingList) {
  1730. b = freeBindingList;
  1731. if (len > b->uriAlloc) {
  1732. b->uri = realloc(b->uri, len + EXPAND_SPARE);
  1733. if (!b->uri)
  1734. return 0;
  1735. b->uriAlloc = len + EXPAND_SPARE;
  1736. }
  1737. freeBindingList = b->nextTagBinding;
  1738. }
  1739. else {
  1740. b = malloc(sizeof(BINDING));
  1741. if (!b)
  1742. return 0;
  1743. b->uri = malloc(sizeof(XML_Char) * len + EXPAND_SPARE);
  1744. if (!b->uri) {
  1745. free(b);
  1746. return 0;
  1747. }
  1748. b->uriAlloc = len;
  1749. }
  1750. b->uriLen = len;
  1751. memcpy(b->uri, uri, len * sizeof(XML_Char));
  1752. if (namespaceSeparator)
  1753. b->uri[len - 1] = namespaceSeparator;
  1754. b->prefix = prefix;
  1755. b->attId = attId;
  1756. b->prevPrefixBinding = prefix->binding;
  1757. if (*uri == XML_T('\0') && prefix == &dtd.defaultPrefix)
  1758. prefix->binding = 0;
  1759. else
  1760. prefix->binding = b;
  1761. b->nextTagBinding = *bindingsPtr;
  1762. *bindingsPtr = b;
  1763. if (startNamespaceDeclHandler)
  1764. startNamespaceDeclHandler(handlerArg, prefix->name,
  1765. prefix->binding ? uri : 0);
  1766. return 1;
  1767. }
  1768. /* The idea here is to avoid using stack for each CDATA section when
  1769. the whole file is parsed with one call. */
  1770. static
  1771. enum XML_Error cdataSectionProcessor(XML_Parser parser,
  1772. const char *start,
  1773. const char *end,
  1774. const char **endPtr)
  1775. {
  1776. enum XML_Error result = doCdataSection(parser, encoding, &start, end, endPtr);
  1777. if (start) {
  1778. processor = contentProcessor;
  1779. return contentProcessor(parser, start, end, endPtr);
  1780. }
  1781. return result;
  1782. }
  1783. /* startPtr gets set to non-null is the section is closed, and to null if
  1784. the section is not yet closed. */
  1785. static
  1786. enum XML_Error doCdataSection(XML_Parser parser,
  1787. const ENCODING *enc,
  1788. const char **startPtr,
  1789. const char *end,
  1790. const char **nextPtr)
  1791. {
  1792. const char *s = *startPtr;
  1793. const char **eventPP;
  1794. const char **eventEndPP;
  1795. if (enc == encoding) {
  1796. eventPP = &eventPtr;
  1797. *eventPP = s;
  1798. eventEndPP = &eventEndPtr;
  1799. }
  1800. else {
  1801. eventPP = &(openInternalEntities->internalEventPtr);
  1802. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  1803. }
  1804. *eventPP = s;
  1805. *startPtr = 0;
  1806. for (;;) {
  1807. const char *next;
  1808. int tok = XmlCdataSectionTok(enc, s, end, &next);
  1809. *eventEndPP = next;
  1810. switch (tok) {
  1811. case XML_TOK_CDATA_SECT_CLOSE:
  1812. if (endCdataSectionHandler)
  1813. endCdataSectionHandler(handlerArg);
  1814. #if 0
  1815. /* see comment under XML_TOK_CDATA_SECT_OPEN */
  1816. else if (characterDataHandler)
  1817. characterDataHandler(handlerArg, dataBuf, 0);
  1818. #endif
  1819. else if (defaultHandler)
  1820. reportDefault(parser, enc, s, next);
  1821. *startPtr = next;
  1822. return XML_ERROR_NONE;
  1823. case XML_TOK_DATA_NEWLINE:
  1824. if (characterDataHandler) {
  1825. XML_Char c = 0xA;
  1826. characterDataHandler(handlerArg, &c, 1);
  1827. }
  1828. else if (defaultHandler)
  1829. reportDefault(parser, enc, s, next);
  1830. break;
  1831. case XML_TOK_DATA_CHARS:
  1832. if (characterDataHandler) {
  1833. if (MUST_CONVERT(enc, s)) {
  1834. for (;;) {
  1835. ICHAR *dataPtr = (ICHAR *)dataBuf;
  1836. XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
  1837. *eventEndPP = next;
  1838. characterDataHandler(handlerArg, dataBuf, dataPtr - (ICHAR *)dataBuf);
  1839. if (s == next)
  1840. break;
  1841. *eventPP = s;
  1842. }
  1843. }
  1844. else
  1845. characterDataHandler(handlerArg,
  1846. (XML_Char *)s,
  1847. (XML_Char *)next - (XML_Char *)s);
  1848. }
  1849. else if (defaultHandler)
  1850. reportDefault(parser, enc, s, next);
  1851. break;
  1852. case XML_TOK_INVALID:
  1853. *eventPP = next;
  1854. return XML_ERROR_INVALID_TOKEN;
  1855. case XML_TOK_PARTIAL_CHAR:
  1856. if (nextPtr) {
  1857. *nextPtr = s;
  1858. return XML_ERROR_NONE;
  1859. }
  1860. return XML_ERROR_PARTIAL_CHAR;
  1861. case XML_TOK_PARTIAL:
  1862. case XML_TOK_NONE:
  1863. if (nextPtr) {
  1864. *nextPtr = s;
  1865. return XML_ERROR_NONE;
  1866. }
  1867. return XML_ERROR_UNCLOSED_CDATA_SECTION;
  1868. default:
  1869. abort();
  1870. }
  1871. *eventPP = s = next;
  1872. }
  1873. /* not reached */
  1874. }
  1875. #ifdef XML_DTD
  1876. /* The idea here is to avoid using stack for each IGNORE section when
  1877. the whole file is parsed with one call. */
  1878. static
  1879. enum XML_Error ignoreSectionProcessor(XML_Parser parser,
  1880. const char *start,
  1881. const char *end,
  1882. const char **endPtr)
  1883. {
  1884. enum XML_Error result = doIgnoreSection(parser, encoding, &start, end, endPtr);
  1885. if (start) {
  1886. processor = prologProcessor;
  1887. return prologProcessor(parser, start, end, endPtr);
  1888. }
  1889. return result;
  1890. }
  1891. /* startPtr gets set to non-null is the section is closed, and to null if
  1892. the section is not yet closed. */
  1893. static
  1894. enum XML_Error doIgnoreSection(XML_Parser parser,
  1895. const ENCODING *enc,
  1896. const char **startPtr,
  1897. const char *end,
  1898. const char **nextPtr)
  1899. {
  1900. const char *next;
  1901. int tok;
  1902. const char *s = *startPtr;
  1903. const char **eventPP;
  1904. const char **eventEndPP;
  1905. if (enc == encoding) {
  1906. eventPP = &eventPtr;
  1907. *eventPP = s;
  1908. eventEndPP = &eventEndPtr;
  1909. }
  1910. else {
  1911. eventPP = &(openInternalEntities->internalEventPtr);
  1912. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  1913. }
  1914. *eventPP = s;
  1915. *startPtr = 0;
  1916. tok = XmlIgnoreSectionTok(enc, s, end, &next);
  1917. *eventEndPP = next;
  1918. switch (tok) {
  1919. case XML_TOK_IGNORE_SECT:
  1920. if (defaultHandler)
  1921. reportDefault(parser, enc, s, next);
  1922. *startPtr = next;
  1923. return XML_ERROR_NONE;
  1924. case XML_TOK_INVALID:
  1925. *eventPP = next;
  1926. return XML_ERROR_INVALID_TOKEN;
  1927. case XML_TOK_PARTIAL_CHAR:
  1928. if (nextPtr) {
  1929. *nextPtr = s;
  1930. return XML_ERROR_NONE;
  1931. }
  1932. return XML_ERROR_PARTIAL_CHAR;
  1933. case XML_TOK_PARTIAL:
  1934. case XML_TOK_NONE:
  1935. if (nextPtr) {
  1936. *nextPtr = s;
  1937. return XML_ERROR_NONE;
  1938. }
  1939. return XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
  1940. default:
  1941. abort();
  1942. }
  1943. /* not reached */
  1944. }
  1945. #endif /* XML_DTD */
  1946. static enum XML_Error
  1947. initializeEncoding(XML_Parser parser)
  1948. {
  1949. const char *s;
  1950. #ifdef XML_UNICODE
  1951. char encodingBuf[128];
  1952. if (!protocolEncodingName)
  1953. s = 0;
  1954. else {
  1955. int i;
  1956. for (i = 0; protocolEncodingName[i]; i++) {
  1957. if (i == sizeof(encodingBuf) - 1
  1958. || protocolEncodingName[i] >= 0x80
  1959. || protocolEncodingName[i] < 0) {
  1960. encodingBuf[0] = '\0';
  1961. break;
  1962. }
  1963. encodingBuf[i] = (char)protocolEncodingName[i];
  1964. }
  1965. encodingBuf[i] = '\0';
  1966. s = encodingBuf;
  1967. }
  1968. #else
  1969. s = protocolEncodingName;
  1970. #endif
  1971. if ((ns ? XmlInitEncodingNS : XmlInitEncoding)(&initEncoding, &encoding, s))
  1972. return XML_ERROR_NONE;
  1973. return handleUnknownEncoding(parser, protocolEncodingName);
  1974. }
  1975. static enum XML_Error
  1976. processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
  1977. const char *s, const char *next)
  1978. {
  1979. const char *encodingName = 0;
  1980. const ENCODING *newEncoding = 0;
  1981. const char *version;
  1982. int standalone = -1;
  1983. if (!(ns
  1984. ? XmlParseXmlDeclNS
  1985. : XmlParseXmlDecl)(isGeneralTextEntity,
  1986. encoding,
  1987. s,
  1988. next,
  1989. &eventPtr,
  1990. &version,
  1991. &encodingName,
  1992. &newEncoding,
  1993. &standalone))
  1994. return XML_ERROR_SYNTAX;
  1995. if (!isGeneralTextEntity && standalone == 1) {
  1996. dtd.standalone = 1;
  1997. #ifdef XML_DTD
  1998. if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
  1999. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  2000. #endif /* XML_DTD */
  2001. }
  2002. if (defaultHandler)
  2003. reportDefault(parser, encoding, s, next);
  2004. if (!protocolEncodingName) {
  2005. if (newEncoding) {
  2006. if (newEncoding->minBytesPerChar != encoding->minBytesPerChar) {
  2007. eventPtr = encodingName;
  2008. return XML_ERROR_INCORRECT_ENCODING;
  2009. }
  2010. encoding = newEncoding;
  2011. }
  2012. else if (encodingName) {
  2013. enum XML_Error result;
  2014. const XML_Char *s = poolStoreString(&tempPool,
  2015. encoding,
  2016. encodingName,
  2017. encodingName
  2018. + XmlNameLength(encoding, encodingName));
  2019. if (!s)
  2020. return XML_ERROR_NO_MEMORY;
  2021. result = handleUnknownEncoding(parser, s);
  2022. poolDiscard(&tempPool);
  2023. if (result == XML_ERROR_UNKNOWN_ENCODING)
  2024. eventPtr = encodingName;
  2025. return result;
  2026. }
  2027. }
  2028. return XML_ERROR_NONE;
  2029. }
  2030. static enum XML_Error
  2031. handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName)
  2032. {
  2033. if (unknownEncodingHandler) {
  2034. XML_Encoding info;
  2035. int i;
  2036. for (i = 0; i < 256; i++)
  2037. info.map[i] = -1;
  2038. info.convert = 0;
  2039. info.data = 0;
  2040. info.release = 0;
  2041. if (unknownEncodingHandler(unknownEncodingHandlerData, encodingName, &info)) {
  2042. ENCODING *enc;
  2043. unknownEncodingMem = malloc(XmlSizeOfUnknownEncoding());
  2044. if (!unknownEncodingMem) {
  2045. if (info.release)
  2046. info.release(info.data);
  2047. return XML_ERROR_NO_MEMORY;
  2048. }
  2049. enc = (ns
  2050. ? XmlInitUnknownEncodingNS
  2051. : XmlInitUnknownEncoding)(unknownEncodingMem,
  2052. info.map,
  2053. info.convert,
  2054. info.data);
  2055. if (enc) {
  2056. unknownEncodingData = info.data;
  2057. unknownEncodingRelease = info.release;
  2058. encoding = enc;
  2059. return XML_ERROR_NONE;
  2060. }
  2061. }
  2062. if (info.release)
  2063. info.release(info.data);
  2064. }
  2065. return XML_ERROR_UNKNOWN_ENCODING;
  2066. }
  2067. static enum XML_Error
  2068. prologInitProcessor(XML_Parser parser,
  2069. const char *s,
  2070. const char *end,
  2071. const char **nextPtr)
  2072. {
  2073. enum XML_Error result = initializeEncoding(parser);
  2074. if (result != XML_ERROR_NONE)
  2075. return result;
  2076. processor = prologProcessor;
  2077. return prologProcessor(parser, s, end, nextPtr);
  2078. }
  2079. static enum XML_Error
  2080. prologProcessor(XML_Parser parser,
  2081. const char *s,
  2082. const char *end,
  2083. const char **nextPtr)
  2084. {
  2085. const char *next;
  2086. int tok = XmlPrologTok(encoding, s, end, &next);
  2087. return doProlog(parser, encoding, s, end, tok, next, nextPtr);
  2088. }
  2089. static enum XML_Error
  2090. doProlog(XML_Parser parser,
  2091. const ENCODING *enc,
  2092. const char *s,
  2093. const char *end,
  2094. int tok,
  2095. const char *next,
  2096. const char **nextPtr)
  2097. {
  2098. #ifdef XML_DTD
  2099. static const XML_Char externalSubsetName[] = { '#' , '\0' };
  2100. #endif /* XML_DTD */
  2101. const char **eventPP;
  2102. const char **eventEndPP;
  2103. if (enc == encoding) {
  2104. eventPP = &eventPtr;
  2105. eventEndPP = &eventEndPtr;
  2106. }
  2107. else {
  2108. eventPP = &(openInternalEntities->internalEventPtr);
  2109. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2110. }
  2111. for (;;) {
  2112. int role;
  2113. *eventPP = s;
  2114. *eventEndPP = next;
  2115. if (tok <= 0) {
  2116. if (nextPtr != 0 && tok != XML_TOK_INVALID) {
  2117. *nextPtr = s;
  2118. return XML_ERROR_NONE;
  2119. }
  2120. switch (tok) {
  2121. case XML_TOK_INVALID:
  2122. *eventPP = next;
  2123. return XML_ERROR_INVALID_TOKEN;
  2124. case XML_TOK_PARTIAL:
  2125. return XML_ERROR_UNCLOSED_TOKEN;
  2126. case XML_TOK_PARTIAL_CHAR:
  2127. return XML_ERROR_PARTIAL_CHAR;
  2128. case XML_TOK_NONE:
  2129. #ifdef XML_DTD
  2130. if (enc != encoding)
  2131. return XML_ERROR_NONE;
  2132. if (parentParser) {
  2133. if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
  2134. == XML_ROLE_ERROR)
  2135. return XML_ERROR_SYNTAX;
  2136. hadExternalDoctype = 0;
  2137. return XML_ERROR_NONE;
  2138. }
  2139. #endif /* XML_DTD */
  2140. return XML_ERROR_NO_ELEMENTS;
  2141. default:
  2142. tok = -tok;
  2143. next = end;
  2144. break;
  2145. }
  2146. }
  2147. role = XmlTokenRole(&prologState, tok, s, next, enc);
  2148. switch (role) {
  2149. case XML_ROLE_XML_DECL:
  2150. {
  2151. enum XML_Error result = processXmlDecl(parser, 0, s, next);
  2152. if (result != XML_ERROR_NONE)
  2153. return result;
  2154. enc = encoding;
  2155. }
  2156. break;
  2157. case XML_ROLE_DOCTYPE_NAME:
  2158. if (startDoctypeDeclHandler) {
  2159. const XML_Char *name = poolStoreString(&tempPool, enc, s, next);
  2160. if (!name)
  2161. return XML_ERROR_NO_MEMORY;
  2162. startDoctypeDeclHandler(handlerArg, name);
  2163. poolClear(&tempPool);
  2164. }
  2165. break;
  2166. #ifdef XML_DTD
  2167. case XML_ROLE_TEXT_DECL:
  2168. {
  2169. enum XML_Error result = processXmlDecl(parser, 1, s, next);
  2170. if (result != XML_ERROR_NONE)
  2171. return result;
  2172. enc = encoding;
  2173. }
  2174. break;
  2175. #endif /* XML_DTD */
  2176. case XML_ROLE_DOCTYPE_PUBLIC_ID:
  2177. #ifdef XML_DTD
  2178. declEntity = (ENTITY *)lookup(&dtd.paramEntities,
  2179. externalSubsetName,
  2180. sizeof(ENTITY));
  2181. if (!declEntity)
  2182. return XML_ERROR_NO_MEMORY;
  2183. #endif /* XML_DTD */
  2184. /* fall through */
  2185. case XML_ROLE_ENTITY_PUBLIC_ID:
  2186. if (!XmlIsPublicId(enc, s, next, eventPP))
  2187. return XML_ERROR_SYNTAX;
  2188. if (declEntity) {
  2189. XML_Char *tem = poolStoreString(&dtd.pool,
  2190. enc,
  2191. s + enc->minBytesPerChar,
  2192. next - enc->minBytesPerChar);
  2193. if (!tem)
  2194. return XML_ERROR_NO_MEMORY;
  2195. normalizePublicId(tem);
  2196. declEntity->publicId = tem;
  2197. poolFinish(&dtd.pool);
  2198. }
  2199. break;
  2200. case XML_ROLE_DOCTYPE_CLOSE:
  2201. if (dtd.complete && hadExternalDoctype) {
  2202. dtd.complete = 0;
  2203. #ifdef XML_DTD
  2204. if (paramEntityParsing && externalEntityRefHandler) {
  2205. ENTITY *entity = (ENTITY *)lookup(&dtd.paramEntities,
  2206. externalSubsetName,
  2207. 0);
  2208. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  2209. 0,
  2210. entity->base,
  2211. entity->systemId,
  2212. entity->publicId))
  2213. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  2214. }
  2215. #endif /* XML_DTD */
  2216. if (!dtd.complete
  2217. && !dtd.standalone
  2218. && notStandaloneHandler
  2219. && !notStandaloneHandler(handlerArg))
  2220. return XML_ERROR_NOT_STANDALONE;
  2221. }
  2222. if (endDoctypeDeclHandler)
  2223. endDoctypeDeclHandler(handlerArg);
  2224. break;
  2225. case XML_ROLE_INSTANCE_START:
  2226. processor = contentProcessor;
  2227. return contentProcessor(parser, s, end, nextPtr);
  2228. case XML_ROLE_ATTLIST_ELEMENT_NAME:
  2229. {
  2230. const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
  2231. if (!name)
  2232. return XML_ERROR_NO_MEMORY;
  2233. declElementType = (ELEMENT_TYPE *)lookup(&dtd.elementTypes, name, sizeof(ELEMENT_TYPE));
  2234. if (!declElementType)
  2235. return XML_ERROR_NO_MEMORY;
  2236. if (declElementType->name != name)
  2237. poolDiscard(&dtd.pool);
  2238. else {
  2239. poolFinish(&dtd.pool);
  2240. if (!setElementTypePrefix(parser, declElementType))
  2241. return XML_ERROR_NO_MEMORY;
  2242. }
  2243. break;
  2244. }
  2245. case XML_ROLE_ATTRIBUTE_NAME:
  2246. declAttributeId = getAttributeId(parser, enc, s, next);
  2247. if (!declAttributeId)
  2248. return XML_ERROR_NO_MEMORY;
  2249. declAttributeIsCdata = 0;
  2250. break;
  2251. case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
  2252. declAttributeIsCdata = 1;
  2253. break;
  2254. case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
  2255. case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
  2256. if (dtd.complete
  2257. && !defineAttribute(declElementType, declAttributeId, declAttributeIsCdata, 0))
  2258. return XML_ERROR_NO_MEMORY;
  2259. break;
  2260. case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
  2261. case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
  2262. {
  2263. const XML_Char *attVal;
  2264. enum XML_Error result
  2265. = storeAttributeValue(parser, enc, declAttributeIsCdata,
  2266. s + enc->minBytesPerChar,
  2267. next - enc->minBytesPerChar,
  2268. &dtd.pool);
  2269. if (result)
  2270. return result;
  2271. attVal = poolStart(&dtd.pool);
  2272. poolFinish(&dtd.pool);
  2273. if (dtd.complete
  2274. && !defineAttribute(declElementType, declAttributeId, declAttributeIsCdata, attVal))
  2275. return XML_ERROR_NO_MEMORY;
  2276. break;
  2277. }
  2278. case XML_ROLE_ENTITY_VALUE:
  2279. {
  2280. enum XML_Error result = storeEntityValue(parser, enc,
  2281. s + enc->minBytesPerChar,
  2282. next - enc->minBytesPerChar);
  2283. if (declEntity) {
  2284. declEntity->textPtr = poolStart(&dtd.pool);
  2285. declEntity->textLen = poolLength(&dtd.pool);
  2286. poolFinish(&dtd.pool);
  2287. }
  2288. else
  2289. poolDiscard(&dtd.pool);
  2290. if (result != XML_ERROR_NONE)
  2291. return result;
  2292. }
  2293. break;
  2294. case XML_ROLE_DOCTYPE_SYSTEM_ID:
  2295. if (!dtd.standalone
  2296. #ifdef XML_DTD
  2297. && !paramEntityParsing
  2298. #endif /* XML_DTD */
  2299. && notStandaloneHandler
  2300. && !notStandaloneHandler(handlerArg))
  2301. return XML_ERROR_NOT_STANDALONE;
  2302. hadExternalDoctype = 1;
  2303. #ifndef XML_DTD
  2304. break;
  2305. #else /* XML_DTD */
  2306. if (!declEntity) {
  2307. declEntity = (ENTITY *)lookup(&dtd.paramEntities,
  2308. externalSubsetName,
  2309. sizeof(ENTITY));
  2310. if (!declEntity)
  2311. return XML_ERROR_NO_MEMORY;
  2312. }
  2313. /* fall through */
  2314. #endif /* XML_DTD */
  2315. case XML_ROLE_ENTITY_SYSTEM_ID:
  2316. if (declEntity) {
  2317. declEntity->systemId = poolStoreString(&dtd.pool, enc,
  2318. s + enc->minBytesPerChar,
  2319. next - enc->minBytesPerChar);
  2320. if (!declEntity->systemId)
  2321. return XML_ERROR_NO_MEMORY;
  2322. declEntity->base = curBase;
  2323. poolFinish(&dtd.pool);
  2324. }
  2325. break;
  2326. case XML_ROLE_ENTITY_NOTATION_NAME:
  2327. if (declEntity) {
  2328. declEntity->notation = poolStoreString(&dtd.pool, enc, s, next);
  2329. if (!declEntity->notation)
  2330. return XML_ERROR_NO_MEMORY;
  2331. poolFinish(&dtd.pool);
  2332. if (unparsedEntityDeclHandler) {
  2333. *eventEndPP = s;
  2334. unparsedEntityDeclHandler(handlerArg,
  2335. declEntity->name,
  2336. declEntity->base,
  2337. declEntity->systemId,
  2338. declEntity->publicId,
  2339. declEntity->notation);
  2340. }
  2341. }
  2342. break;
  2343. case XML_ROLE_GENERAL_ENTITY_NAME:
  2344. {
  2345. const XML_Char *name;
  2346. if (XmlPredefinedEntityName(enc, s, next)) {
  2347. declEntity = 0;
  2348. break;
  2349. }
  2350. name = poolStoreString(&dtd.pool, enc, s, next);
  2351. if (!name)
  2352. return XML_ERROR_NO_MEMORY;
  2353. if (dtd.complete) {
  2354. declEntity = (ENTITY *)lookup(&dtd.generalEntities, name, sizeof(ENTITY));
  2355. if (!declEntity)
  2356. return XML_ERROR_NO_MEMORY;
  2357. if (declEntity->name != name) {
  2358. poolDiscard(&dtd.pool);
  2359. declEntity = 0;
  2360. }
  2361. else
  2362. poolFinish(&dtd.pool);
  2363. }
  2364. else {
  2365. poolDiscard(&dtd.pool);
  2366. declEntity = 0;
  2367. }
  2368. }
  2369. break;
  2370. case XML_ROLE_PARAM_ENTITY_NAME:
  2371. #ifdef XML_DTD
  2372. if (dtd.complete) {
  2373. const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
  2374. if (!name)
  2375. return XML_ERROR_NO_MEMORY;
  2376. declEntity = (ENTITY *)lookup(&dtd.paramEntities, name, sizeof(ENTITY));
  2377. if (!declEntity)
  2378. return XML_ERROR_NO_MEMORY;
  2379. if (declEntity->name != name) {
  2380. poolDiscard(&dtd.pool);
  2381. declEntity = 0;
  2382. }
  2383. else
  2384. poolFinish(&dtd.pool);
  2385. }
  2386. #else /* not XML_DTD */
  2387. declEntity = 0;
  2388. #endif /* not XML_DTD */
  2389. break;
  2390. case XML_ROLE_NOTATION_NAME:
  2391. declNotationPublicId = 0;
  2392. declNotationName = 0;
  2393. if (notationDeclHandler) {
  2394. declNotationName = poolStoreString(&tempPool, enc, s, next);
  2395. if (!declNotationName)
  2396. return XML_ERROR_NO_MEMORY;
  2397. poolFinish(&tempPool);
  2398. }
  2399. break;
  2400. case XML_ROLE_NOTATION_PUBLIC_ID:
  2401. if (!XmlIsPublicId(enc, s, next, eventPP))
  2402. return XML_ERROR_SYNTAX;
  2403. if (declNotationName) {
  2404. XML_Char *tem = poolStoreString(&tempPool,
  2405. enc,
  2406. s + enc->minBytesPerChar,
  2407. next - enc->minBytesPerChar);
  2408. if (!tem)
  2409. return XML_ERROR_NO_MEMORY;
  2410. normalizePublicId(tem);
  2411. declNotationPublicId = tem;
  2412. poolFinish(&tempPool);
  2413. }
  2414. break;
  2415. case XML_ROLE_NOTATION_SYSTEM_ID:
  2416. if (declNotationName && notationDeclHandler) {
  2417. const XML_Char *systemId
  2418. = poolStoreString(&tempPool, enc,
  2419. s + enc->minBytesPerChar,
  2420. next - enc->minBytesPerChar);
  2421. if (!systemId)
  2422. return XML_ERROR_NO_MEMORY;
  2423. *eventEndPP = s;
  2424. notationDeclHandler(handlerArg,
  2425. declNotationName,
  2426. curBase,
  2427. systemId,
  2428. declNotationPublicId);
  2429. }
  2430. poolClear(&tempPool);
  2431. break;
  2432. case XML_ROLE_NOTATION_NO_SYSTEM_ID:
  2433. if (declNotationPublicId && notationDeclHandler) {
  2434. *eventEndPP = s;
  2435. notationDeclHandler(handlerArg,
  2436. declNotationName,
  2437. curBase,
  2438. 0,
  2439. declNotationPublicId);
  2440. }
  2441. poolClear(&tempPool);
  2442. break;
  2443. case XML_ROLE_ERROR:
  2444. switch (tok) {
  2445. case XML_TOK_PARAM_ENTITY_REF:
  2446. return XML_ERROR_PARAM_ENTITY_REF;
  2447. case XML_TOK_XML_DECL:
  2448. return XML_ERROR_MISPLACED_XML_PI;
  2449. default:
  2450. return XML_ERROR_SYNTAX;
  2451. }
  2452. #ifdef XML_DTD
  2453. case XML_ROLE_IGNORE_SECT:
  2454. {
  2455. enum XML_Error result;
  2456. if (defaultHandler)
  2457. reportDefault(parser, enc, s, next);
  2458. result = doIgnoreSection(parser, enc, &next, end, nextPtr);
  2459. if (!next) {
  2460. processor = ignoreSectionProcessor;
  2461. return result;
  2462. }
  2463. }
  2464. break;
  2465. #endif /* XML_DTD */
  2466. case XML_ROLE_GROUP_OPEN:
  2467. if (prologState.level >= groupSize) {
  2468. if (groupSize)
  2469. groupConnector = realloc(groupConnector, groupSize *= 2);
  2470. else
  2471. groupConnector = malloc(groupSize = 32);
  2472. if (!groupConnector)
  2473. return XML_ERROR_NO_MEMORY;
  2474. }
  2475. groupConnector[prologState.level] = 0;
  2476. break;
  2477. case XML_ROLE_GROUP_SEQUENCE:
  2478. if (groupConnector[prologState.level] == '|')
  2479. return XML_ERROR_SYNTAX;
  2480. groupConnector[prologState.level] = ',';
  2481. break;
  2482. case XML_ROLE_GROUP_CHOICE:
  2483. if (groupConnector[prologState.level] == ',')
  2484. return XML_ERROR_SYNTAX;
  2485. groupConnector[prologState.level] = '|';
  2486. break;
  2487. case XML_ROLE_PARAM_ENTITY_REF:
  2488. #ifdef XML_DTD
  2489. case XML_ROLE_INNER_PARAM_ENTITY_REF:
  2490. if (paramEntityParsing
  2491. && (dtd.complete || role == XML_ROLE_INNER_PARAM_ENTITY_REF)) {
  2492. const XML_Char *name;
  2493. ENTITY *entity;
  2494. name = poolStoreString(&dtd.pool, enc,
  2495. s + enc->minBytesPerChar,
  2496. next - enc->minBytesPerChar);
  2497. if (!name)
  2498. return XML_ERROR_NO_MEMORY;
  2499. entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
  2500. poolDiscard(&dtd.pool);
  2501. if (!entity) {
  2502. /* FIXME what to do if !dtd.complete? */
  2503. return XML_ERROR_UNDEFINED_ENTITY;
  2504. }
  2505. if (entity->open)
  2506. return XML_ERROR_RECURSIVE_ENTITY_REF;
  2507. if (entity->textPtr) {
  2508. enum XML_Error result;
  2509. result = processInternalParamEntity(parser, entity);
  2510. if (result != XML_ERROR_NONE)
  2511. return result;
  2512. break;
  2513. }
  2514. if (role == XML_ROLE_INNER_PARAM_ENTITY_REF)
  2515. return XML_ERROR_PARAM_ENTITY_REF;
  2516. if (externalEntityRefHandler) {
  2517. dtd.complete = 0;
  2518. entity->open = 1;
  2519. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  2520. 0,
  2521. entity->base,
  2522. entity->systemId,
  2523. entity->publicId)) {
  2524. entity->open = 0;
  2525. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  2526. }
  2527. entity->open = 0;
  2528. if (dtd.complete)
  2529. break;
  2530. }
  2531. }
  2532. #endif /* XML_DTD */
  2533. if (!dtd.standalone
  2534. && notStandaloneHandler
  2535. && !notStandaloneHandler(handlerArg))
  2536. return XML_ERROR_NOT_STANDALONE;
  2537. dtd.complete = 0;
  2538. if (defaultHandler)
  2539. reportDefault(parser, enc, s, next);
  2540. break;
  2541. case XML_ROLE_NONE:
  2542. switch (tok) {
  2543. case XML_TOK_PI:
  2544. if (!reportProcessingInstruction(parser, enc, s, next))
  2545. return XML_ERROR_NO_MEMORY;
  2546. break;
  2547. case XML_TOK_COMMENT:
  2548. if (!reportComment(parser, enc, s, next))
  2549. return XML_ERROR_NO_MEMORY;
  2550. break;
  2551. }
  2552. break;
  2553. }
  2554. if (defaultHandler) {
  2555. switch (tok) {
  2556. case XML_TOK_PI:
  2557. case XML_TOK_COMMENT:
  2558. case XML_TOK_BOM:
  2559. case XML_TOK_XML_DECL:
  2560. #ifdef XML_DTD
  2561. case XML_TOK_IGNORE_SECT:
  2562. #endif /* XML_DTD */
  2563. case XML_TOK_PARAM_ENTITY_REF:
  2564. break;
  2565. default:
  2566. if (role != XML_ROLE_IGNORE_SECT)
  2567. reportDefault(parser, enc, s, next);
  2568. }
  2569. }
  2570. s = next;
  2571. tok = XmlPrologTok(enc, s, end, &next);
  2572. }
  2573. /* not reached */
  2574. }
  2575. static
  2576. enum XML_Error epilogProcessor(XML_Parser parser,
  2577. const char *s,
  2578. const char *end,
  2579. const char **nextPtr)
  2580. {
  2581. processor = epilogProcessor;
  2582. eventPtr = s;
  2583. for (;;) {
  2584. const char *next;
  2585. int tok = XmlPrologTok(encoding, s, end, &next);
  2586. eventEndPtr = next;
  2587. switch (tok) {
  2588. case -XML_TOK_PROLOG_S:
  2589. if (defaultHandler) {
  2590. eventEndPtr = end;
  2591. reportDefault(parser, encoding, s, end);
  2592. }
  2593. /* fall through */
  2594. case XML_TOK_NONE:
  2595. if (nextPtr)
  2596. *nextPtr = end;
  2597. return XML_ERROR_NONE;
  2598. case XML_TOK_PROLOG_S:
  2599. if (defaultHandler)
  2600. reportDefault(parser, encoding, s, next);
  2601. break;
  2602. case XML_TOK_PI:
  2603. if (!reportProcessingInstruction(parser, encoding, s, next))
  2604. return XML_ERROR_NO_MEMORY;
  2605. break;
  2606. case XML_TOK_COMMENT:
  2607. if (!reportComment(parser, encoding, s, next))
  2608. return XML_ERROR_NO_MEMORY;
  2609. break;
  2610. case XML_TOK_INVALID:
  2611. eventPtr = next;
  2612. return XML_ERROR_INVALID_TOKEN;
  2613. case XML_TOK_PARTIAL:
  2614. if (nextPtr) {
  2615. *nextPtr = s;
  2616. return XML_ERROR_NONE;
  2617. }
  2618. return XML_ERROR_UNCLOSED_TOKEN;
  2619. case XML_TOK_PARTIAL_CHAR:
  2620. if (nextPtr) {
  2621. *nextPtr = s;
  2622. return XML_ERROR_NONE;
  2623. }
  2624. return XML_ERROR_PARTIAL_CHAR;
  2625. default:
  2626. return XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
  2627. }
  2628. eventPtr = s = next;
  2629. }
  2630. }
  2631. #ifdef XML_DTD
  2632. static enum XML_Error
  2633. processInternalParamEntity(XML_Parser parser, ENTITY *entity)
  2634. {
  2635. const char *s, *end, *next;
  2636. int tok;
  2637. enum XML_Error result;
  2638. OPEN_INTERNAL_ENTITY openEntity;
  2639. entity->open = 1;
  2640. openEntity.next = openInternalEntities;
  2641. openInternalEntities = &openEntity;
  2642. openEntity.entity = entity;
  2643. openEntity.internalEventPtr = 0;
  2644. openEntity.internalEventEndPtr = 0;
  2645. s = (char *)entity->textPtr;
  2646. end = (char *)(entity->textPtr + entity->textLen);
  2647. tok = XmlPrologTok(internalEncoding, s, end, &next);
  2648. result = doProlog(parser, internalEncoding, s, end, tok, next, 0);
  2649. entity->open = 0;
  2650. openInternalEntities = openEntity.next;
  2651. return result;
  2652. }
  2653. #endif /* XML_DTD */
  2654. static
  2655. enum XML_Error errorProcessor(XML_Parser parser,
  2656. const char *s,
  2657. const char *end,
  2658. const char **nextPtr)
  2659. {
  2660. return errorCode;
  2661. }
  2662. static enum XML_Error
  2663. storeAttributeValue(XML_Parser parser, const ENCODING *enc, int isCdata,
  2664. const char *ptr, const char *end,
  2665. STRING_POOL *pool)
  2666. {
  2667. enum XML_Error result = appendAttributeValue(parser, enc, isCdata, ptr, end, pool);
  2668. if (result)
  2669. return result;
  2670. if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
  2671. poolChop(pool);
  2672. if (!poolAppendChar(pool, XML_T('\0')))
  2673. return XML_ERROR_NO_MEMORY;
  2674. return XML_ERROR_NONE;
  2675. }
  2676. static enum XML_Error
  2677. appendAttributeValue(XML_Parser parser, const ENCODING *enc, int isCdata,
  2678. const char *ptr, const char *end,
  2679. STRING_POOL *pool)
  2680. {
  2681. for (;;) {
  2682. const char *next;
  2683. int tok = XmlAttributeValueTok(enc, ptr, end, &next);
  2684. switch (tok) {
  2685. case XML_TOK_NONE:
  2686. return XML_ERROR_NONE;
  2687. case XML_TOK_INVALID:
  2688. if (enc == encoding)
  2689. eventPtr = next;
  2690. return XML_ERROR_INVALID_TOKEN;
  2691. case XML_TOK_PARTIAL:
  2692. if (enc == encoding)
  2693. eventPtr = ptr;
  2694. return XML_ERROR_INVALID_TOKEN;
  2695. case XML_TOK_CHAR_REF:
  2696. {
  2697. XML_Char buf[XML_ENCODE_MAX];
  2698. int i;
  2699. int n = XmlCharRefNumber(enc, ptr);
  2700. if (n < 0) {
  2701. if (enc == encoding)
  2702. eventPtr = ptr;
  2703. return XML_ERROR_BAD_CHAR_REF;
  2704. }
  2705. if (!isCdata
  2706. && n == 0x20 /* space */
  2707. && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  2708. break;
  2709. n = XmlEncode(n, (ICHAR *)buf);
  2710. if (!n) {
  2711. if (enc == encoding)
  2712. eventPtr = ptr;
  2713. return XML_ERROR_BAD_CHAR_REF;
  2714. }
  2715. for (i = 0; i < n; i++) {
  2716. if (!poolAppendChar(pool, buf[i]))
  2717. return XML_ERROR_NO_MEMORY;
  2718. }
  2719. }
  2720. break;
  2721. case XML_TOK_DATA_CHARS:
  2722. if (!poolAppend(pool, enc, ptr, next))
  2723. return XML_ERROR_NO_MEMORY;
  2724. break;
  2725. break;
  2726. case XML_TOK_TRAILING_CR:
  2727. next = ptr + enc->minBytesPerChar;
  2728. /* fall through */
  2729. case XML_TOK_ATTRIBUTE_VALUE_S:
  2730. case XML_TOK_DATA_NEWLINE:
  2731. if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  2732. break;
  2733. if (!poolAppendChar(pool, 0x20))
  2734. return XML_ERROR_NO_MEMORY;
  2735. break;
  2736. case XML_TOK_ENTITY_REF:
  2737. {
  2738. const XML_Char *name;
  2739. ENTITY *entity;
  2740. XML_Char ch = XmlPredefinedEntityName(enc,
  2741. ptr + enc->minBytesPerChar,
  2742. next - enc->minBytesPerChar);
  2743. if (ch) {
  2744. if (!poolAppendChar(pool, ch))
  2745. return XML_ERROR_NO_MEMORY;
  2746. break;
  2747. }
  2748. name = poolStoreString(&temp2Pool, enc,
  2749. ptr + enc->minBytesPerChar,
  2750. next - enc->minBytesPerChar);
  2751. if (!name)
  2752. return XML_ERROR_NO_MEMORY;
  2753. entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
  2754. poolDiscard(&temp2Pool);
  2755. if (!entity) {
  2756. if (dtd.complete) {
  2757. if (enc == encoding)
  2758. eventPtr = ptr;
  2759. return XML_ERROR_UNDEFINED_ENTITY;
  2760. }
  2761. }
  2762. else if (entity->open) {
  2763. if (enc == encoding)
  2764. eventPtr = ptr;
  2765. return XML_ERROR_RECURSIVE_ENTITY_REF;
  2766. }
  2767. else if (entity->notation) {
  2768. if (enc == encoding)
  2769. eventPtr = ptr;
  2770. return XML_ERROR_BINARY_ENTITY_REF;
  2771. }
  2772. else if (!entity->textPtr) {
  2773. if (enc == encoding)
  2774. eventPtr = ptr;
  2775. return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
  2776. }
  2777. else {
  2778. enum XML_Error result;
  2779. const XML_Char *textEnd = entity->textPtr + entity->textLen;
  2780. entity->open = 1;
  2781. result = appendAttributeValue(parser, internalEncoding, isCdata, (char *)entity->textPtr, (char *)textEnd, pool);
  2782. entity->open = 0;
  2783. if (result)
  2784. return result;
  2785. }
  2786. }
  2787. break;
  2788. default:
  2789. abort();
  2790. }
  2791. ptr = next;
  2792. }
  2793. /* not reached */
  2794. }
  2795. static
  2796. enum XML_Error storeEntityValue(XML_Parser parser,
  2797. const ENCODING *enc,
  2798. const char *entityTextPtr,
  2799. const char *entityTextEnd)
  2800. {
  2801. STRING_POOL *pool = &(dtd.pool);
  2802. for (;;) {
  2803. const char *next;
  2804. int tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
  2805. switch (tok) {
  2806. case XML_TOK_PARAM_ENTITY_REF:
  2807. #ifdef XML_DTD
  2808. if (parentParser || enc != encoding) {
  2809. enum XML_Error result;
  2810. const XML_Char *name;
  2811. ENTITY *entity;
  2812. name = poolStoreString(&tempPool, enc,
  2813. entityTextPtr + enc->minBytesPerChar,
  2814. next - enc->minBytesPerChar);
  2815. if (!name)
  2816. return XML_ERROR_NO_MEMORY;
  2817. entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
  2818. poolDiscard(&tempPool);
  2819. if (!entity) {
  2820. if (enc == encoding)
  2821. eventPtr = entityTextPtr;
  2822. return XML_ERROR_UNDEFINED_ENTITY;
  2823. }
  2824. if (entity->open) {
  2825. if (enc == encoding)
  2826. eventPtr = entityTextPtr;
  2827. return XML_ERROR_RECURSIVE_ENTITY_REF;
  2828. }
  2829. if (entity->systemId) {
  2830. if (enc == encoding)
  2831. eventPtr = entityTextPtr;
  2832. return XML_ERROR_PARAM_ENTITY_REF;
  2833. }
  2834. entity->open = 1;
  2835. result = storeEntityValue(parser,
  2836. internalEncoding,
  2837. (char *)entity->textPtr,
  2838. (char *)(entity->textPtr + entity->textLen));
  2839. entity->open = 0;
  2840. if (result)
  2841. return result;
  2842. break;
  2843. }
  2844. #endif /* XML_DTD */
  2845. eventPtr = entityTextPtr;
  2846. return XML_ERROR_SYNTAX;
  2847. case XML_TOK_NONE:
  2848. return XML_ERROR_NONE;
  2849. case XML_TOK_ENTITY_REF:
  2850. case XML_TOK_DATA_CHARS:
  2851. if (!poolAppend(pool, enc, entityTextPtr, next))
  2852. return XML_ERROR_NO_MEMORY;
  2853. break;
  2854. case XML_TOK_TRAILING_CR:
  2855. next = entityTextPtr + enc->minBytesPerChar;
  2856. /* fall through */
  2857. case XML_TOK_DATA_NEWLINE:
  2858. if (pool->end == pool->ptr && !poolGrow(pool))
  2859. return XML_ERROR_NO_MEMORY;
  2860. *(pool->ptr)++ = 0xA;
  2861. break;
  2862. case XML_TOK_CHAR_REF:
  2863. {
  2864. XML_Char buf[XML_ENCODE_MAX];
  2865. int i;
  2866. int n = XmlCharRefNumber(enc, entityTextPtr);
  2867. if (n < 0) {
  2868. if (enc == encoding)
  2869. eventPtr = entityTextPtr;
  2870. return XML_ERROR_BAD_CHAR_REF;
  2871. }
  2872. n = XmlEncode(n, (ICHAR *)buf);
  2873. if (!n) {
  2874. if (enc == encoding)
  2875. eventPtr = entityTextPtr;
  2876. return XML_ERROR_BAD_CHAR_REF;
  2877. }
  2878. for (i = 0; i < n; i++) {
  2879. if (pool->end == pool->ptr && !poolGrow(pool))
  2880. return XML_ERROR_NO_MEMORY;
  2881. *(pool->ptr)++ = buf[i];
  2882. }
  2883. }
  2884. break;
  2885. case XML_TOK_PARTIAL:
  2886. if (enc == encoding)
  2887. eventPtr = entityTextPtr;
  2888. return XML_ERROR_INVALID_TOKEN;
  2889. case XML_TOK_INVALID:
  2890. if (enc == encoding)
  2891. eventPtr = next;
  2892. return XML_ERROR_INVALID_TOKEN;
  2893. default:
  2894. abort();
  2895. }
  2896. entityTextPtr = next;
  2897. }
  2898. /* not reached */
  2899. }
  2900. static void
  2901. normalizeLines(XML_Char *s)
  2902. {
  2903. XML_Char *p;
  2904. for (;; s++) {
  2905. if (*s == XML_T('\0'))
  2906. return;
  2907. if (*s == 0xD)
  2908. break;
  2909. }
  2910. p = s;
  2911. do {
  2912. if (*s == 0xD) {
  2913. *p++ = 0xA;
  2914. if (*++s == 0xA)
  2915. s++;
  2916. }
  2917. else
  2918. *p++ = *s++;
  2919. } while (*s);
  2920. *p = XML_T('\0');
  2921. }
  2922. static int
  2923. reportProcessingInstruction(XML_Parser parser, const ENCODING *enc, const char *start, const char *end)
  2924. {
  2925. const XML_Char *target;
  2926. XML_Char *data;
  2927. const char *tem;
  2928. if (!processingInstructionHandler) {
  2929. if (defaultHandler)
  2930. reportDefault(parser, enc, start, end);
  2931. return 1;
  2932. }
  2933. start += enc->minBytesPerChar * 2;
  2934. tem = start + XmlNameLength(enc, start);
  2935. target = poolStoreString(&tempPool, enc, start, tem);
  2936. if (!target)
  2937. return 0;
  2938. poolFinish(&tempPool);
  2939. data = poolStoreString(&tempPool, enc,
  2940. XmlSkipS(enc, tem),
  2941. end - enc->minBytesPerChar*2);
  2942. if (!data)
  2943. return 0;
  2944. normalizeLines(data);
  2945. processingInstructionHandler(handlerArg, target, data);
  2946. poolClear(&tempPool);
  2947. return 1;
  2948. }
  2949. static int
  2950. reportComment(XML_Parser parser, const ENCODING *enc, const char *start, const char *end)
  2951. {
  2952. XML_Char *data;
  2953. if (!commentHandler) {
  2954. if (defaultHandler)
  2955. reportDefault(parser, enc, start, end);
  2956. return 1;
  2957. }
  2958. data = poolStoreString(&tempPool,
  2959. enc,
  2960. start + enc->minBytesPerChar * 4,
  2961. end - enc->minBytesPerChar * 3);
  2962. if (!data)
  2963. return 0;
  2964. normalizeLines(data);
  2965. commentHandler(handlerArg, data);
  2966. poolClear(&tempPool);
  2967. return 1;
  2968. }
  2969. static void
  2970. reportDefault(XML_Parser parser, const ENCODING *enc, const char *s, const char *end)
  2971. {
  2972. if (MUST_CONVERT(enc, s)) {
  2973. const char **eventPP;
  2974. const char **eventEndPP;
  2975. if (enc == encoding) {
  2976. eventPP = &eventPtr;
  2977. eventEndPP = &eventEndPtr;
  2978. }
  2979. else {
  2980. eventPP = &(openInternalEntities->internalEventPtr);
  2981. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2982. }
  2983. do {
  2984. ICHAR *dataPtr = (ICHAR *)dataBuf;
  2985. XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
  2986. *eventEndPP = s;
  2987. defaultHandler(handlerArg, dataBuf, dataPtr - (ICHAR *)dataBuf);
  2988. *eventPP = s;
  2989. } while (s != end);
  2990. }
  2991. else
  2992. defaultHandler(handlerArg, (XML_Char *)s, (XML_Char *)end - (XML_Char *)s);
  2993. }
  2994. static int
  2995. defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, int isCdata, const XML_Char *value)
  2996. {
  2997. DEFAULT_ATTRIBUTE *att;
  2998. if (value) {
  2999. /* The handling of default attributes gets messed up if we have
  3000. a default which duplicates a non-default. */
  3001. int i;
  3002. for (i = 0; i < type->nDefaultAtts; i++)
  3003. if (attId == type->defaultAtts[i].id)
  3004. return 1;
  3005. }
  3006. if (type->nDefaultAtts == type->allocDefaultAtts) {
  3007. if (type->allocDefaultAtts == 0) {
  3008. type->allocDefaultAtts = 8;
  3009. type->defaultAtts = malloc(type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
  3010. }
  3011. else {
  3012. type->allocDefaultAtts *= 2;
  3013. type->defaultAtts = realloc(type->defaultAtts,
  3014. type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
  3015. }
  3016. if (!type->defaultAtts)
  3017. return 0;
  3018. }
  3019. att = type->defaultAtts + type->nDefaultAtts;
  3020. att->id = attId;
  3021. att->value = value;
  3022. att->isCdata = isCdata;
  3023. if (!isCdata)
  3024. attId->maybeTokenized = 1;
  3025. type->nDefaultAtts += 1;
  3026. return 1;
  3027. }
  3028. static int setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
  3029. {
  3030. const XML_Char *name;
  3031. for (name = elementType->name; *name; name++) {
  3032. if (*name == XML_T(':')) {
  3033. PREFIX *prefix;
  3034. const XML_Char *s;
  3035. for (s = elementType->name; s != name; s++) {
  3036. if (!poolAppendChar(&dtd.pool, *s))
  3037. return 0;
  3038. }
  3039. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  3040. return 0;
  3041. prefix = (PREFIX *)lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
  3042. if (!prefix)
  3043. return 0;
  3044. if (prefix->name == poolStart(&dtd.pool))
  3045. poolFinish(&dtd.pool);
  3046. else
  3047. poolDiscard(&dtd.pool);
  3048. elementType->prefix = prefix;
  3049. }
  3050. }
  3051. return 1;
  3052. }
  3053. static ATTRIBUTE_ID *
  3054. getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start, const char *end)
  3055. {
  3056. ATTRIBUTE_ID *id;
  3057. const XML_Char *name;
  3058. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  3059. return 0;
  3060. name = poolStoreString(&dtd.pool, enc, start, end);
  3061. if (!name)
  3062. return 0;
  3063. ++name;
  3064. id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, name, sizeof(ATTRIBUTE_ID));
  3065. if (!id)
  3066. return 0;
  3067. if (id->name != name)
  3068. poolDiscard(&dtd.pool);
  3069. else {
  3070. poolFinish(&dtd.pool);
  3071. if (!ns)
  3072. ;
  3073. else if (name[0] == 'x'
  3074. && name[1] == 'm'
  3075. && name[2] == 'l'
  3076. && name[3] == 'n'
  3077. && name[4] == 's'
  3078. && (name[5] == XML_T('\0') || name[5] == XML_T(':'))) {
  3079. if (name[5] == '\0')
  3080. id->prefix = &dtd.defaultPrefix;
  3081. else
  3082. id->prefix = (PREFIX *)lookup(&dtd.prefixes, name + 6, sizeof(PREFIX));
  3083. id->xmlns = 1;
  3084. }
  3085. else {
  3086. int i;
  3087. for (i = 0; name[i]; i++) {
  3088. if (name[i] == XML_T(':')) {
  3089. int j;
  3090. for (j = 0; j < i; j++) {
  3091. if (!poolAppendChar(&dtd.pool, name[j]))
  3092. return 0;
  3093. }
  3094. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  3095. return 0;
  3096. id->prefix = (PREFIX *)lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
  3097. if (id->prefix->name == poolStart(&dtd.pool))
  3098. poolFinish(&dtd.pool);
  3099. else
  3100. poolDiscard(&dtd.pool);
  3101. break;
  3102. }
  3103. }
  3104. }
  3105. }
  3106. return id;
  3107. }
  3108. #define CONTEXT_SEP XML_T('\f')
  3109. static
  3110. const XML_Char *getContext(XML_Parser parser)
  3111. {
  3112. HASH_TABLE_ITER iter;
  3113. int needSep = 0;
  3114. if (dtd.defaultPrefix.binding) {
  3115. int i;
  3116. int len;
  3117. if (!poolAppendChar(&tempPool, XML_T('=')))
  3118. return 0;
  3119. len = dtd.defaultPrefix.binding->uriLen;
  3120. if (namespaceSeparator != XML_T('\0'))
  3121. len--;
  3122. for (i = 0; i < len; i++)
  3123. if (!poolAppendChar(&tempPool, dtd.defaultPrefix.binding->uri[i]))
  3124. return 0;
  3125. needSep = 1;
  3126. }
  3127. hashTableIterInit(&iter, &(dtd.prefixes));
  3128. for (;;) {
  3129. int i;
  3130. int len;
  3131. const XML_Char *s;
  3132. PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
  3133. if (!prefix)
  3134. break;
  3135. if (!prefix->binding)
  3136. continue;
  3137. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  3138. return 0;
  3139. for (s = prefix->name; *s; s++)
  3140. if (!poolAppendChar(&tempPool, *s))
  3141. return 0;
  3142. if (!poolAppendChar(&tempPool, XML_T('=')))
  3143. return 0;
  3144. len = prefix->binding->uriLen;
  3145. if (namespaceSeparator != XML_T('\0'))
  3146. len--;
  3147. for (i = 0; i < len; i++)
  3148. if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
  3149. return 0;
  3150. needSep = 1;
  3151. }
  3152. hashTableIterInit(&iter, &(dtd.generalEntities));
  3153. for (;;) {
  3154. const XML_Char *s;
  3155. ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
  3156. if (!e)
  3157. break;
  3158. if (!e->open)
  3159. continue;
  3160. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  3161. return 0;
  3162. for (s = e->name; *s; s++)
  3163. if (!poolAppendChar(&tempPool, *s))
  3164. return 0;
  3165. needSep = 1;
  3166. }
  3167. if (!poolAppendChar(&tempPool, XML_T('\0')))
  3168. return 0;
  3169. return tempPool.start;
  3170. }
  3171. static
  3172. int setContext(XML_Parser parser, const XML_Char *context)
  3173. {
  3174. const XML_Char *s = context;
  3175. while (*context != XML_T('\0')) {
  3176. if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
  3177. ENTITY *e;
  3178. if (!poolAppendChar(&tempPool, XML_T('\0')))
  3179. return 0;
  3180. e = (ENTITY *)lookup(&dtd.generalEntities, poolStart(&tempPool), 0);
  3181. if (e)
  3182. e->open = 1;
  3183. if (*s != XML_T('\0'))
  3184. s++;
  3185. context = s;
  3186. poolDiscard(&tempPool);
  3187. }
  3188. else if (*s == '=') {
  3189. PREFIX *prefix;
  3190. if (poolLength(&tempPool) == 0)
  3191. prefix = &dtd.defaultPrefix;
  3192. else {
  3193. if (!poolAppendChar(&tempPool, XML_T('\0')))
  3194. return 0;
  3195. prefix = (PREFIX *)lookup(&dtd.prefixes, poolStart(&tempPool), sizeof(PREFIX));
  3196. if (!prefix)
  3197. return 0;
  3198. if (prefix->name == poolStart(&tempPool))
  3199. poolFinish(&tempPool);
  3200. else
  3201. poolDiscard(&tempPool);
  3202. }
  3203. for (context = s + 1; *context != CONTEXT_SEP && *context != XML_T('\0'); context++)
  3204. if (!poolAppendChar(&tempPool, *context))
  3205. return 0;
  3206. if (!poolAppendChar(&tempPool, XML_T('\0')))
  3207. return 0;
  3208. if (!addBinding(parser, prefix, 0, poolStart(&tempPool), &inheritedBindings))
  3209. return 0;
  3210. poolDiscard(&tempPool);
  3211. if (*context != XML_T('\0'))
  3212. ++context;
  3213. s = context;
  3214. }
  3215. else {
  3216. if (!poolAppendChar(&tempPool, *s))
  3217. return 0;
  3218. s++;
  3219. }
  3220. }
  3221. return 1;
  3222. }
  3223. static
  3224. void normalizePublicId(XML_Char *publicId)
  3225. {
  3226. XML_Char *p = publicId;
  3227. XML_Char *s;
  3228. for (s = publicId; *s; s++) {
  3229. switch (*s) {
  3230. case 0x20:
  3231. case 0xD:
  3232. case 0xA:
  3233. if (p != publicId && p[-1] != 0x20)
  3234. *p++ = 0x20;
  3235. break;
  3236. default:
  3237. *p++ = *s;
  3238. }
  3239. }
  3240. if (p != publicId && p[-1] == 0x20)
  3241. --p;
  3242. *p = XML_T('\0');
  3243. }
  3244. static int dtdInit(DTD *p)
  3245. {
  3246. poolInit(&(p->pool));
  3247. hashTableInit(&(p->generalEntities));
  3248. hashTableInit(&(p->elementTypes));
  3249. hashTableInit(&(p->attributeIds));
  3250. hashTableInit(&(p->prefixes));
  3251. p->complete = 1;
  3252. p->standalone = 0;
  3253. #ifdef XML_DTD
  3254. hashTableInit(&(p->paramEntities));
  3255. #endif /* XML_DTD */
  3256. p->defaultPrefix.name = 0;
  3257. p->defaultPrefix.binding = 0;
  3258. return 1;
  3259. }
  3260. #ifdef XML_DTD
  3261. static void dtdSwap(DTD *p1, DTD *p2)
  3262. {
  3263. DTD tem;
  3264. memcpy(&tem, p1, sizeof(DTD));
  3265. memcpy(p1, p2, sizeof(DTD));
  3266. memcpy(p2, &tem, sizeof(DTD));
  3267. }
  3268. #endif /* XML_DTD */
  3269. static void dtdDestroy(DTD *p)
  3270. {
  3271. HASH_TABLE_ITER iter;
  3272. hashTableIterInit(&iter, &(p->elementTypes));
  3273. for (;;) {
  3274. ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  3275. if (!e)
  3276. break;
  3277. if (e->allocDefaultAtts != 0)
  3278. free(e->defaultAtts);
  3279. }
  3280. hashTableDestroy(&(p->generalEntities));
  3281. #ifdef XML_DTD
  3282. hashTableDestroy(&(p->paramEntities));
  3283. #endif /* XML_DTD */
  3284. hashTableDestroy(&(p->elementTypes));
  3285. hashTableDestroy(&(p->attributeIds));
  3286. hashTableDestroy(&(p->prefixes));
  3287. poolDestroy(&(p->pool));
  3288. }
  3289. /* Do a deep copy of the DTD. Return 0 for out of memory; non-zero otherwise.
  3290. The new DTD has already been initialized. */
  3291. static int dtdCopy(DTD *newDtd, const DTD *oldDtd)
  3292. {
  3293. HASH_TABLE_ITER iter;
  3294. /* Copy the prefix table. */
  3295. hashTableIterInit(&iter, &(oldDtd->prefixes));
  3296. for (;;) {
  3297. const XML_Char *name;
  3298. const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
  3299. if (!oldP)
  3300. break;
  3301. name = poolCopyString(&(newDtd->pool), oldP->name);
  3302. if (!name)
  3303. return 0;
  3304. if (!lookup(&(newDtd->prefixes), name, sizeof(PREFIX)))
  3305. return 0;
  3306. }
  3307. hashTableIterInit(&iter, &(oldDtd->attributeIds));
  3308. /* Copy the attribute id table. */
  3309. for (;;) {
  3310. ATTRIBUTE_ID *newA;
  3311. const XML_Char *name;
  3312. const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
  3313. if (!oldA)
  3314. break;
  3315. /* Remember to allocate the scratch byte before the name. */
  3316. if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
  3317. return 0;
  3318. name = poolCopyString(&(newDtd->pool), oldA->name);
  3319. if (!name)
  3320. return 0;
  3321. ++name;
  3322. newA = (ATTRIBUTE_ID *)lookup(&(newDtd->attributeIds), name, sizeof(ATTRIBUTE_ID));
  3323. if (!newA)
  3324. return 0;
  3325. newA->maybeTokenized = oldA->maybeTokenized;
  3326. if (oldA->prefix) {
  3327. newA->xmlns = oldA->xmlns;
  3328. if (oldA->prefix == &oldDtd->defaultPrefix)
  3329. newA->prefix = &newDtd->defaultPrefix;
  3330. else
  3331. newA->prefix = (PREFIX *)lookup(&(newDtd->prefixes), oldA->prefix->name, 0);
  3332. }
  3333. }
  3334. /* Copy the element type table. */
  3335. hashTableIterInit(&iter, &(oldDtd->elementTypes));
  3336. for (;;) {
  3337. int i;
  3338. ELEMENT_TYPE *newE;
  3339. const XML_Char *name;
  3340. const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  3341. if (!oldE)
  3342. break;
  3343. name = poolCopyString(&(newDtd->pool), oldE->name);
  3344. if (!name)
  3345. return 0;
  3346. newE = (ELEMENT_TYPE *)lookup(&(newDtd->elementTypes), name, sizeof(ELEMENT_TYPE));
  3347. if (!newE)
  3348. return 0;
  3349. if (oldE->nDefaultAtts) {
  3350. newE->defaultAtts = (DEFAULT_ATTRIBUTE *)malloc(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
  3351. if (!newE->defaultAtts)
  3352. return 0;
  3353. }
  3354. newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
  3355. if (oldE->prefix)
  3356. newE->prefix = (PREFIX *)lookup(&(newDtd->prefixes), oldE->prefix->name, 0);
  3357. for (i = 0; i < newE->nDefaultAtts; i++) {
  3358. newE->defaultAtts[i].id = (ATTRIBUTE_ID *)lookup(&(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
  3359. newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
  3360. if (oldE->defaultAtts[i].value) {
  3361. newE->defaultAtts[i].value = poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
  3362. if (!newE->defaultAtts[i].value)
  3363. return 0;
  3364. }
  3365. else
  3366. newE->defaultAtts[i].value = 0;
  3367. }
  3368. }
  3369. /* Copy the entity tables. */
  3370. if (!copyEntityTable(&(newDtd->generalEntities),
  3371. &(newDtd->pool),
  3372. &(oldDtd->generalEntities)))
  3373. return 0;
  3374. #ifdef XML_DTD
  3375. if (!copyEntityTable(&(newDtd->paramEntities),
  3376. &(newDtd->pool),
  3377. &(oldDtd->paramEntities)))
  3378. return 0;
  3379. #endif /* XML_DTD */
  3380. newDtd->complete = oldDtd->complete;
  3381. newDtd->standalone = oldDtd->standalone;
  3382. return 1;
  3383. }
  3384. static int copyEntityTable(HASH_TABLE *newTable,
  3385. STRING_POOL *newPool,
  3386. const HASH_TABLE *oldTable)
  3387. {
  3388. HASH_TABLE_ITER iter;
  3389. const XML_Char *cachedOldBase = 0;
  3390. const XML_Char *cachedNewBase = 0;
  3391. hashTableIterInit(&iter, oldTable);
  3392. for (;;) {
  3393. ENTITY *newE;
  3394. const XML_Char *name;
  3395. const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
  3396. if (!oldE)
  3397. break;
  3398. name = poolCopyString(newPool, oldE->name);
  3399. if (!name)
  3400. return 0;
  3401. newE = (ENTITY *)lookup(newTable, name, sizeof(ENTITY));
  3402. if (!newE)
  3403. return 0;
  3404. if (oldE->systemId) {
  3405. const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
  3406. if (!tem)
  3407. return 0;
  3408. newE->systemId = tem;
  3409. if (oldE->base) {
  3410. if (oldE->base == cachedOldBase)
  3411. newE->base = cachedNewBase;
  3412. else {
  3413. cachedOldBase = oldE->base;
  3414. tem = poolCopyString(newPool, cachedOldBase);
  3415. if (!tem)
  3416. return 0;
  3417. cachedNewBase = newE->base = tem;
  3418. }
  3419. }
  3420. }
  3421. else {
  3422. const XML_Char *tem = poolCopyStringN(newPool, oldE->textPtr, oldE->textLen);
  3423. if (!tem)
  3424. return 0;
  3425. newE->textPtr = tem;
  3426. newE->textLen = oldE->textLen;
  3427. }
  3428. if (oldE->notation) {
  3429. const XML_Char *tem = poolCopyString(newPool, oldE->notation);
  3430. if (!tem)
  3431. return 0;
  3432. newE->notation = tem;
  3433. }
  3434. }
  3435. return 1;
  3436. }
  3437. static
  3438. void poolInit(STRING_POOL *pool)
  3439. {
  3440. pool->blocks = 0;
  3441. pool->freeBlocks = 0;
  3442. pool->start = 0;
  3443. pool->ptr = 0;
  3444. pool->end = 0;
  3445. }
  3446. static
  3447. void poolClear(STRING_POOL *pool)
  3448. {
  3449. if (!pool->freeBlocks)
  3450. pool->freeBlocks = pool->blocks;
  3451. else {
  3452. BLOCK *p = pool->blocks;
  3453. while (p) {
  3454. BLOCK *tem = p->next;
  3455. p->next = pool->freeBlocks;
  3456. pool->freeBlocks = p;
  3457. p = tem;
  3458. }
  3459. }
  3460. pool->blocks = 0;
  3461. pool->start = 0;
  3462. pool->ptr = 0;
  3463. pool->end = 0;
  3464. }
  3465. static
  3466. void poolDestroy(STRING_POOL *pool)
  3467. {
  3468. BLOCK *p = pool->blocks;
  3469. while (p) {
  3470. BLOCK *tem = p->next;
  3471. free(p);
  3472. p = tem;
  3473. }
  3474. pool->blocks = 0;
  3475. p = pool->freeBlocks;
  3476. while (p) {
  3477. BLOCK *tem = p->next;
  3478. free(p);
  3479. p = tem;
  3480. }
  3481. pool->freeBlocks = 0;
  3482. pool->ptr = 0;
  3483. pool->start = 0;
  3484. pool->end = 0;
  3485. }
  3486. static
  3487. XML_Char *poolAppend(STRING_POOL *pool, const ENCODING *enc,
  3488. const char *ptr, const char *end)
  3489. {
  3490. if (!pool->ptr && !poolGrow(pool))
  3491. return 0;
  3492. for (;;) {
  3493. XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
  3494. if (ptr == end)
  3495. break;
  3496. if (!poolGrow(pool))
  3497. return 0;
  3498. }
  3499. return pool->start;
  3500. }
  3501. static const XML_Char *poolCopyString(STRING_POOL *pool, const XML_Char *s)
  3502. {
  3503. do {
  3504. if (!poolAppendChar(pool, *s))
  3505. return 0;
  3506. } while (*s++);
  3507. s = pool->start;
  3508. poolFinish(pool);
  3509. return s;
  3510. }
  3511. static const XML_Char *poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n)
  3512. {
  3513. if (!pool->ptr && !poolGrow(pool))
  3514. return 0;
  3515. for (; n > 0; --n, s++) {
  3516. if (!poolAppendChar(pool, *s))
  3517. return 0;
  3518. }
  3519. s = pool->start;
  3520. poolFinish(pool);
  3521. return s;
  3522. }
  3523. static
  3524. XML_Char *poolStoreString(STRING_POOL *pool, const ENCODING *enc,
  3525. const char *ptr, const char *end)
  3526. {
  3527. if (!poolAppend(pool, enc, ptr, end))
  3528. return 0;
  3529. if (pool->ptr == pool->end && !poolGrow(pool))
  3530. return 0;
  3531. *(pool->ptr)++ = 0;
  3532. return pool->start;
  3533. }
  3534. static
  3535. int poolGrow(STRING_POOL *pool)
  3536. {
  3537. if (pool->freeBlocks) {
  3538. if (pool->start == 0) {
  3539. pool->blocks = pool->freeBlocks;
  3540. pool->freeBlocks = pool->freeBlocks->next;
  3541. pool->blocks->next = 0;
  3542. pool->start = pool->blocks->s;
  3543. pool->end = pool->start + pool->blocks->size;
  3544. pool->ptr = pool->start;
  3545. return 1;
  3546. }
  3547. if (pool->end - pool->start < pool->freeBlocks->size) {
  3548. BLOCK *tem = pool->freeBlocks->next;
  3549. pool->freeBlocks->next = pool->blocks;
  3550. pool->blocks = pool->freeBlocks;
  3551. pool->freeBlocks = tem;
  3552. memcpy(pool->blocks->s, pool->start, (pool->end - pool->start) * sizeof(XML_Char));
  3553. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  3554. pool->start = pool->blocks->s;
  3555. pool->end = pool->start + pool->blocks->size;
  3556. return 1;
  3557. }
  3558. }
  3559. if (pool->blocks && pool->start == pool->blocks->s) {
  3560. int blockSize = (pool->end - pool->start)*2;
  3561. pool->blocks = realloc(pool->blocks, offsetof(BLOCK, s) + blockSize * sizeof(XML_Char));
  3562. if (!pool->blocks)
  3563. return 0;
  3564. pool->blocks->size = blockSize;
  3565. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  3566. pool->start = pool->blocks->s;
  3567. pool->end = pool->start + blockSize;
  3568. }
  3569. else {
  3570. BLOCK *tem;
  3571. int blockSize = pool->end - pool->start;
  3572. if (blockSize < INIT_BLOCK_SIZE)
  3573. blockSize = INIT_BLOCK_SIZE;
  3574. else
  3575. blockSize *= 2;
  3576. tem = malloc(offsetof(BLOCK, s) + blockSize * sizeof(XML_Char));
  3577. if (!tem)
  3578. return 0;
  3579. tem->size = blockSize;
  3580. tem->next = pool->blocks;
  3581. pool->blocks = tem;
  3582. memcpy(tem->s, pool->start, (pool->ptr - pool->start) * sizeof(XML_Char));
  3583. pool->ptr = tem->s + (pool->ptr - pool->start);
  3584. pool->start = tem->s;
  3585. pool->end = tem->s + blockSize;
  3586. }
  3587. return 1;
  3588. }