1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
// =================================================================================================
// Copyright 2005 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================
#include "public/include/XMP_Environment.h" // ! Must be the first #include!
#include "XMPCore/source/XMPCore_Impl.hpp"
#include "source/ExpatAdapter.hpp"
#include "XMPCore/source/XMPMeta.hpp"
#include "third-party/expat/lib/expat.h"
#include <string.h>
using namespace std;
#if XMP_WinBuild
#pragma warning ( disable : 4996 ) // '...' was declared deprecated
#endif
// *** Set memory handlers.
#ifndef DumpXMLParseEvents
#define DumpXMLParseEvents 0
#endif
#define FullNameSeparator '@'
// =================================================================================================
static void StartNamespaceDeclHandler ( void * userData, XMP_StringPtr prefix, XMP_StringPtr uri );
static void EndNamespaceDeclHandler ( void * userData, XMP_StringPtr prefix );
static void StartElementHandler ( void * userData, XMP_StringPtr name, XMP_StringPtr* attrs );
static void EndElementHandler ( void * userData, XMP_StringPtr name );
static void CharacterDataHandler ( void * userData, XMP_StringPtr cData, int len );
static void StartCdataSectionHandler ( void * userData );
static void EndCdataSectionHandler ( void * userData );
static void ProcessingInstructionHandler ( void * userData, XMP_StringPtr target, XMP_StringPtr data );
static void CommentHandler ( void * userData, XMP_StringPtr comment );
#if BanAllEntityUsage
// For now we do this by banning DOCTYPE entirely. This is easy and consistent with what is
// available in recent Java XML parsers. Another, somewhat less drastic, approach would be to
// ban all entity declarations. We can't allow declarations and ban references, Expat does not
// call the SkippedEntityHandler for references in attribute values.
// ! Standard entities (&, <, >, ", ', and numeric character references) are
// ! not banned. Expat handles them transparently no matter what.
static void StartDoctypeDeclHandler ( void * userData, XMP_StringPtr doctypeName,
XMP_StringPtr sysid, XMP_StringPtr pubid, int has_internal_subset );
#endif
// =================================================================================================
extern "C" ExpatAdapter * XMP_NewExpatAdapter ( bool useGlobalNamespaces )
{
return new ExpatAdapter ( useGlobalNamespaces );
} // XMP_NewExpatAdapter
// =================================================================================================
ExpatAdapter::ExpatAdapter ( bool useGlobalNamespaces ) : parser(0), registeredNamespaces(0)
{
#if XMP_DebugBuild
this->elemNesting = 0;
#if DumpXMLParseEvents
if ( this->parseLog == 0 ) this->parseLog = stdout;
#endif
#endif
this->parser = XML_ParserCreateNS ( 0, FullNameSeparator );
if ( this->parser == 0 ) {
XMP_Error error(kXMPErr_NoMemory, "Failure creating Expat parser" );
this->NotifyClient ( kXMPErrSev_ProcessFatal, error );
}else{
if ( useGlobalNamespaces ) {
this->registeredNamespaces = sRegisteredNamespaces;
} else {
this->registeredNamespaces = new XMP_NamespaceTable ( *sRegisteredNamespaces );
}
XML_SetUserData ( this->parser, this );
XML_SetNamespaceDeclHandler ( this->parser, StartNamespaceDeclHandler, EndNamespaceDeclHandler );
XML_SetElementHandler ( this->parser, StartElementHandler, EndElementHandler );
XML_SetCharacterDataHandler ( this->parser, CharacterDataHandler );
XML_SetCdataSectionHandler ( this->parser, StartCdataSectionHandler, EndCdataSectionHandler );
XML_SetProcessingInstructionHandler ( this->parser, ProcessingInstructionHandler );
XML_SetCommentHandler ( this->parser, CommentHandler );
#if BanAllEntityUsage
XML_SetStartDoctypeDeclHandler ( this->parser, StartDoctypeDeclHandler );
isAborted = false;
#endif
this->parseStack.push_back ( &this->tree ); // Push the XML root node.
}
} // ExpatAdapter::ExpatAdapter
// =================================================================================================
ExpatAdapter::~ExpatAdapter()
{
if ( this->parser != 0 ) XML_ParserFree ( this->parser );
this->parser = 0;
if ( this->registeredNamespaces != sRegisteredNamespaces ) delete ( this->registeredNamespaces );
this->registeredNamespaces = 0;
} // ExpatAdapter::~ExpatAdapter
// =================================================================================================
#if XMP_DebugBuild
static XMP_VarString sExpatMessage;
#endif
static const char * kOneSpace = " ";
void ExpatAdapter::ParseBuffer ( const void * buffer, size_t length, bool last /* = true */ )
{
enum XML_Status status;
if ( length == 0 ) { // Expat does not like empty buffers.
if ( ! last ) return;
buffer = kOneSpace;
length = 1;
}
status = XML_Parse ( this->parser, (const char *)buffer, length, last );
#if BanAllEntityUsage
if ( this->isAborted ) {
XMP_Error error(kXMPErr_BadXML, "DOCTYPE is not allowed" );
this->NotifyClient ( kXMPErrSev_Recoverable, error );
}
#endif
if ( status != XML_STATUS_OK ) {
XMP_StringPtr errMsg = "XML parsing failure";
#if 0 // XMP_DebugBuild // Disable for now to make test output uniform. Restore later with thread safety.
// *** This is a good candidate for a callback error notification mechanism.
// *** This code is not thread safe, the sExpatMessage isn't locked. But that's OK for debug usage.
enum XML_Error expatErr = XML_GetErrorCode ( this->parser );
const char * expatMsg = XML_ErrorString ( expatErr );
int errLine = XML_GetCurrentLineNumber ( this->parser );
char msgBuffer[1000];
// AUDIT: Use of sizeof(msgBuffer) for snprintf length is safe.
snprintf ( msgBuffer, sizeof(msgBuffer), "# Expat error %d at line %d, \"%s\"", expatErr, errLine, expatMsg );
sExpatMessage = msgBuffer;
errMsg = sExpatMessage.c_str();
#if DumpXMLParseEvents
if ( this->parseLog != 0 ) fprintf ( this->parseLog, "%s\n", errMsg, expatErr, errLine, expatMsg );
#endif
#endif
XMP_Error error(kXMPErr_BadXML, errMsg);
this->NotifyClient ( kXMPErrSev_Recoverable, error );
}
} // ExpatAdapter::ParseBuffer
// =================================================================================================
// =================================================================================================
#if XMP_DebugBuild & DumpXMLParseEvents
static inline void PrintIndent ( FILE * file, size_t count )
{
for ( ; count > 0; --count ) fprintf ( file, " " );
}
#endif
// =================================================================================================
static void SetQualName ( ExpatAdapter * thiz, XMP_StringPtr fullName, XML_Node * node )
{
// Expat delivers the full name as a catenation of namespace URI, separator, and local name.
// As a compatibility hack, an "about" or "ID" attribute of an rdf:Description element is
// changed to "rdf:about" or rdf:ID. Easier done here than in the RDF recognizer.
// As a bug fix hack, change a URI of "http://purl.org/dc/1.1/" to ""http://purl.org/dc/elements/1.1/.
// Early versions of Flash that put XMP in SWF used a bad URI for the dc: namespace.
// ! This code presumes the RDF namespace prefix is "rdf".
size_t sepPos = strlen(fullName);
for ( --sepPos; sepPos > 0; --sepPos ) {
if ( fullName[sepPos] == FullNameSeparator ) break;
}
if ( fullName[sepPos] == FullNameSeparator ) {
XMP_StringPtr prefix;
XMP_StringLen prefixLen;
XMP_StringPtr localPart = fullName + sepPos + 1;
node->ns.assign ( fullName, sepPos );
if ( node->ns == "http://purl.org/dc/1.1/" ) node->ns = "http://purl.org/dc/elements/1.1/";
bool found = thiz->registeredNamespaces->GetPrefix ( node->ns.c_str(), &prefix, &prefixLen );
if ( ! found ) {
XMP_Error error(kXMPErr_ExternalFailure, "Unknown URI in Expat full name" );
thiz->NotifyClient ( kXMPErrSev_OperationFatal, error );
}
node->nsPrefixLen = prefixLen; // ! Includes the ':'.
node->name = prefix;
node->name += localPart;
} else {
node->name = fullName; // The name is not in a namespace.
if ( node->parent->name == "rdf:Description" ) {
if ( node->name == "about" ) {
node->ns = kXMP_NS_RDF;
node->name = "rdf:about";
node->nsPrefixLen = 4; // ! Include the ':'.
} else if ( node->name == "ID" ) {
node->ns = kXMP_NS_RDF;
node->name = "rdf:ID";
node->nsPrefixLen = 4; // ! Include the ':'.
}
}
}
} // SetQualName
// =================================================================================================
static void StartNamespaceDeclHandler ( void * userData, XMP_StringPtr prefix, XMP_StringPtr uri )
{
IgnoreParam(userData);
// As a bug fix hack, change a URI of "http://purl.org/dc/1.1/" to ""http://purl.org/dc/elements/1.1/.
// Early versions of Flash that put XMP in SWF used a bad URI for the dc: namespace.
ExpatAdapter * thiz = (ExpatAdapter*)userData;
if ( prefix == 0 ) prefix = "_dflt_"; // Have default namespace.
if ( uri == 0 ) return; // Ignore, have xmlns:pre="", no URI to register.
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "StartNamespace: %s - \"%s\"\n", prefix, uri );
}
#endif
if ( XMP_LitMatch ( uri, "http://purl.org/dc/1.1/" ) ) uri = "http://purl.org/dc/elements/1.1/";
(void) thiz->registeredNamespaces->Define ( uri, prefix, 0, 0 );
} // StartNamespaceDeclHandler
// =================================================================================================
static void EndNamespaceDeclHandler ( void * userData, XMP_StringPtr prefix )
{
IgnoreParam(userData);
#if XMP_DebugBuild & DumpXMLParseEvents // Avoid unused variable warning.
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#endif
if ( prefix == 0 ) prefix = "_dflt_"; // Have default namespace.
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "EndNamespace: %s\n", prefix );
}
#endif
// ! Nothing to do, Expat has done all of the XML processing.
} // EndNamespaceDeclHandler
// =================================================================================================
static void StartElementHandler ( void * userData, XMP_StringPtr name, XMP_StringPtr* attrs )
{
XMP_Assert ( attrs != 0 );
ExpatAdapter * thiz = (ExpatAdapter*)userData;
size_t attrCount = 0;
for ( XMP_StringPtr* a = attrs; *a != 0; ++a ) ++attrCount;
if ( (attrCount & 1) != 0 ) {
XMP_Error error(kXMPErr_ExternalFailure, "Expat attribute info has odd length");
thiz->NotifyClient ( kXMPErrSev_OperationFatal, error );
}
attrCount = attrCount/2; // They are name/value pairs.
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "StartElement: %s, %d attrs", name, attrCount );
for ( XMP_StringPtr* attr = attrs; *attr != 0; attr += 2 ) {
XMP_StringPtr attrName = *attr;
XMP_StringPtr attrValue = *(attr+1);
fprintf ( thiz->parseLog, ", %s = \"%s\"", attrName, attrValue );
}
fprintf ( thiz->parseLog, "\n" );
}
#endif
XML_Node * parentNode = thiz->parseStack.back();
XML_Node * elemNode = new XML_Node ( parentNode, "", kElemNode );
SetQualName ( thiz, name, elemNode );
for ( XMP_StringPtr* attr = attrs; *attr != 0; attr += 2 ) {
XMP_StringPtr attrName = *attr;
XMP_StringPtr attrValue = *(attr+1);
XML_Node * attrNode = new XML_Node ( elemNode, "", kAttrNode );
SetQualName ( thiz, attrName, attrNode );
attrNode->value = attrValue;
if ( attrNode->name == "xml:lang" ) NormalizeLangValue ( &attrNode->value );
elemNode->attrs.push_back ( attrNode );
}
parentNode->content.push_back ( elemNode );
thiz->parseStack.push_back ( elemNode );
if ( elemNode->name == "rdf:RDF" ) {
thiz->rootNode = elemNode;
++thiz->rootCount;
}
#if XMP_DebugBuild
++thiz->elemNesting;
#endif
} // StartElementHandler
// =================================================================================================
static void EndElementHandler ( void * userData, XMP_StringPtr name )
{
IgnoreParam(name);
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#if XMP_DebugBuild
--thiz->elemNesting;
#endif
(void) thiz->parseStack.pop_back();
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "EndElement: %s\n", name );
}
#endif
} // EndElementHandler
// =================================================================================================
static void CharacterDataHandler ( void * userData, XMP_StringPtr cData, int len )
{
ExpatAdapter * thiz = (ExpatAdapter*)userData;
if ( (cData == 0) || (len == 0) ) { cData = ""; len = 0; }
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "CharContent: \"" );
for ( int i = 0; i < len; ++i ) fprintf ( thiz->parseLog, "%c", cData[i] );
fprintf ( thiz->parseLog, "\"\n" );
}
#endif
XML_Node * parentNode = thiz->parseStack.back();
XML_Node * cDataNode = new XML_Node ( parentNode, "", kCDataNode );
cDataNode->value.assign ( cData, len );
parentNode->content.push_back ( cDataNode );
} // CharacterDataHandler
// =================================================================================================
static void StartCdataSectionHandler ( void * userData )
{
IgnoreParam(userData);
#if XMP_DebugBuild & DumpXMLParseEvents // Avoid unused variable warning.
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#endif
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "StartCDATA\n" );
}
#endif
// *** Since markup isn't recognized inside CDATA, this affects XMP's double escaping.
} // StartCdataSectionHandler
// =================================================================================================
static void EndCdataSectionHandler ( void * userData )
{
IgnoreParam(userData);
#if XMP_DebugBuild & DumpXMLParseEvents // Avoid unused variable warning.
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#endif
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "EndCDATA\n" );
}
#endif
} // EndCdataSectionHandler
// =================================================================================================
static void ProcessingInstructionHandler ( void * userData, XMP_StringPtr target, XMP_StringPtr data )
{
XMP_Assert ( target != 0 );
ExpatAdapter * thiz = (ExpatAdapter*)userData;
if ( ! XMP_LitMatch ( target, "xpacket" ) ) return; // Ignore all PIs except the XMP packet wrapper.
if ( data == 0 ) data = "";
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "PI: %s - \"%s\"\n", target, data );
}
#endif
XML_Node * parentNode = thiz->parseStack.back();
XML_Node * piNode = new XML_Node ( parentNode, target, kPINode );
piNode->value.assign ( data );
parentNode->content.push_back ( piNode );
} // ProcessingInstructionHandler
// =================================================================================================
static void CommentHandler ( void * userData, XMP_StringPtr comment )
{
IgnoreParam(userData);
#if XMP_DebugBuild & DumpXMLParseEvents // Avoid unused variable warning.
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#endif
if ( comment == 0 ) comment = "";
#if XMP_DebugBuild & DumpXMLParseEvents
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "Comment: \"%s\"\n", comment );
}
#endif
// ! Comments are ignored.
} // CommentHandler
// =================================================================================================
#if BanAllEntityUsage
static void StartDoctypeDeclHandler ( void * userData, XMP_StringPtr doctypeName,
XMP_StringPtr sysid, XMP_StringPtr pubid, int has_internal_subset )
{
IgnoreParam(userData);
ExpatAdapter * thiz = (ExpatAdapter*)userData;
#if XMP_DebugBuild & DumpXMLParseEvents // Avoid unused variable warning.
if ( thiz->parseLog != 0 ) {
PrintIndent ( thiz->parseLog, thiz->elemNesting );
fprintf ( thiz->parseLog, "DocType: \"%s\"\n", doctypeName );
}
#endif
thiz->isAborted = true; // ! Can't throw an exception across the plain C Expat frames.
(void) XML_StopParser ( thiz->parser, XML_FALSE /* not resumable */ );
} // StartDoctypeDeclHandler
#endif
// =================================================================================================
|