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