summaryrefslogtreecommitdiff
path: root/source/XMPCore/XMPMeta-Parse.cpp
blob: 5ff64f41ea8f439ba32afc0c152f88283ae32063 (plain)
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
// =================================================================================================
// Copyright 2002-2008 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.
//
// Adobe patent application tracking #P435, entitled 'Unique markers to simplify embedding data of
// one format in a file with a different format', inventors: Sean Parent, Greg Gilley.
// =================================================================================================

#include "XMP_Environment.h"	// ! This must be the first include!
#include "XMPCore_Impl.hpp"

#include "XMPMeta.hpp"
#include "XMPUtils.hpp"

#include "UnicodeInlines.incl_cpp"
#include "UnicodeConversions.hpp"
#include "ExpatAdapter.hpp"

#if XMP_DebugBuild
	#include <iostream>
#endif

using namespace std;

#if XMP_WinBuild
	#pragma warning ( disable : 4533 )	// initialization of '...' is skipped by 'goto ...'
	#pragma warning ( disable : 4702 )	// unreachable code
	#pragma warning ( disable : 4800 )	// forcing value to bool 'true' or 'false' (performance warning)
	#pragma warning ( disable : 4996 )	// '...' was declared deprecated
#endif


// *** Use the XMP_PropIsXyz (Schema, Simple, Struct, Array, ...) macros
// *** Add debug codegen checks, e.g. that typical masking operations really work
// *** Change all uses of strcmp and strncmp to XMP_LitMatch and XMP_LitNMatch


// =================================================================================================
// Local Types and Constants
// =========================


// =================================================================================================
// Static Variables
// ================

#ifndef Trace_ParsingHackery
	#define Trace_ParsingHackery 0
#endif

static const char * kReplaceLatin1[128] =
	{

		// The 0x80..0x9F range is undefined in Latin-1, but is defined in Windows code page 1252.
		// The bytes 0x81, 0x8D, 0x8F, 0x90, and 0x9D are formally undefined by Windows 1252, but
		// their conversion API maps them to U+0081, etc. These are in XML's RestrictedChar set, so
		// we map them to a space.
		
		"\xE2\x82\xAC", " ",            "\xE2\x80\x9A", "\xC6\x92",		// 0x80 .. 0x83
		"\xE2\x80\x9E", "\xE2\x80\xA6", "\xE2\x80\xA0", "\xE2\x80\xA1",	// 0x84 .. 0x87
		"\xCB\x86",     "\xE2\x80\xB0", "\xC5\xA0",     "\xE2\x80\xB9",	// 0x88 .. 0x8B
		"\xC5\x92",     " ",            "\xC5\xBD",     " ",			// 0x8C .. 0x8F

		" ",            "\xE2\x80\x98", "\xE2\x80\x99", "\xE2\x80\x9C",	// 0x90 .. 0x93
		"\xE2\x80\x9D", "\xE2\x80\xA2", "\xE2\x80\x93", "\xE2\x80\x94",	// 0x94 .. 0x97
		"\xCB\x9C",     "\xE2\x84\xA2", "\xC5\xA1",     "\xE2\x80\xBA",	// 0x98 .. 0x9B
		"\xC5\x93",     " ",            "\xC5\xBE",     "\xC5\xB8",		// 0x9C .. 0x9F

		// These are the UTF-8 forms of the official Latin-1 characters in the range 0xA0..0xFF. Not
		// too surprisingly these map to U+00A0, etc. Which is the Unicode Latin Supplement range.
		
		"\xC2\xA0", "\xC2\xA1", "\xC2\xA2", "\xC2\xA3", "\xC2\xA4", "\xC2\xA5", "\xC2\xA6", "\xC2\xA7",	// 0xA0 .. 0xA7
		"\xC2\xA8", "\xC2\xA9", "\xC2\xAA", "\xC2\xAB", "\xC2\xAC", "\xC2\xAD", "\xC2\xAE", "\xC2\xAF",	// 0xA8 .. 0xAF

		"\xC2\xB0", "\xC2\xB1", "\xC2\xB2", "\xC2\xB3", "\xC2\xB4", "\xC2\xB5", "\xC2\xB6", "\xC2\xB7",	// 0xB0 .. 0xB7
		"\xC2\xB8", "\xC2\xB9", "\xC2\xBA", "\xC2\xBB", "\xC2\xBC", "\xC2\xBD", "\xC2\xBE", "\xC2\xBF",	// 0xB8 .. 0xBF

		"\xC3\x80", "\xC3\x81", "\xC3\x82", "\xC3\x83", "\xC3\x84", "\xC3\x85", "\xC3\x86", "\xC3\x87",	// 0xC0 .. 0xC7
		"\xC3\x88", "\xC3\x89", "\xC3\x8A", "\xC3\x8B", "\xC3\x8C", "\xC3\x8D", "\xC3\x8E", "\xC3\x8F",	// 0xC8 .. 0xCF

		"\xC3\x90", "\xC3\x91", "\xC3\x92", "\xC3\x93", "\xC3\x94", "\xC3\x95", "\xC3\x96", "\xC3\x97",	// 0xD0 .. 0xD7
		"\xC3\x98", "\xC3\x99", "\xC3\x9A", "\xC3\x9B", "\xC3\x9C", "\xC3\x9D", "\xC3\x9E", "\xC3\x9F",	// 0xD8 .. 0xDF

		"\xC3\xA0", "\xC3\xA1", "\xC3\xA2", "\xC3\xA3", "\xC3\xA4", "\xC3\xA5", "\xC3\xA6", "\xC3\xA7",	// 0xE0 .. 0xE7
		"\xC3\xA8", "\xC3\xA9", "\xC3\xAA", "\xC3\xAB", "\xC3\xAC", "\xC3\xAD", "\xC3\xAE", "\xC3\xAF",	// 0xE8 .. 0xEF

		"\xC3\xB0", "\xC3\xB1", "\xC3\xB2", "\xC3\xB3", "\xC3\xB4", "\xC3\xB5", "\xC3\xB6", "\xC3\xB7",	// 0xF0 .. 0xF7
		"\xC3\xB8", "\xC3\xB9", "\xC3\xBA", "\xC3\xBB", "\xC3\xBC", "\xC3\xBD", "\xC3\xBE", "\xC3\xBF",	// 0xF8 .. 0xFF

	};


// =================================================================================================
// Local Utilities
// ===============


#define IsHexDigit(ch)		( (('0' <= (ch)) && ((ch) <= '9')) || (('A' <= (ch)) && ((ch) <= 'F')) )
#define HexDigitValue(ch)	( (((ch) - '0') < 10) ? ((ch) - '0') : ((ch) - 'A' + 10) )


// -------------------------------------------------------------------------------------------------
// PickBestRoot
// ------------
static const XML_Node * PickBestRoot ( const XML_Node & xmlParent, XMP_OptionBits options )
{

	// Look among this parent's content for x:xmpmeta. The recursion for x:xmpmeta is broader than
	// the strictly defined choice, but gives us smaller code.
	for ( size_t childNum = 0, childLim = xmlParent.content.size(); childNum < childLim; ++childNum ) {
		const XML_Node * childNode = xmlParent.content[childNum];
		if ( childNode->kind != kElemNode ) continue;
		if ( (childNode->name == "x:xmpmeta") || (childNode->name == "x:xapmeta") ) return PickBestRoot ( *childNode, 0 );
	}
	// Look among this parent's content for a bare rdf:RDF if that is allowed.
	if ( ! (options & kXMP_RequireXMPMeta) ) {
		for ( size_t childNum = 0, childLim = xmlParent.content.size(); childNum < childLim; ++childNum ) {
			const XML_Node * childNode = xmlParent.content[childNum];
			if ( childNode->kind != kElemNode ) continue;
			if ( childNode->name == "rdf:RDF" ) return childNode;
		}
	}
	
	// Recurse into the content.
	for ( size_t childNum = 0, childLim = xmlParent.content.size(); childNum < childLim; ++childNum ) {
		const XML_Node * foundRoot = PickBestRoot ( *xmlParent.content[childNum], options );
		if ( foundRoot != 0 ) return foundRoot;
	}
	
	return 0;

}	// PickBestRoot

// -------------------------------------------------------------------------------------------------
// FindRootNode
// ------------
//
// Find the XML node that is the root of the XMP data tree. Generally this will be an outer node,
// but it could be anywhere if a general XML document is parsed (e.g. SVG). The XML parser counted
// all possible root nodes, and kept a pointer to the last one. If there is more than one possible
// root use PickBestRoot to choose among them.
//
// If there is a root node, try to extract the version of the previous XMP toolkit.

static const XML_Node * FindRootNode ( XMPMeta * thiz, const XMLParserAdapter & xmlParser, XMP_OptionBits options )
{
	const XML_Node * rootNode = xmlParser.rootNode;
	
	if ( xmlParser.rootCount > 1 ) rootNode = PickBestRoot ( xmlParser.tree, options );
	if ( rootNode == 0 ) return 0;
	
	// We have a root node. Try to extract previous toolkit version number.
	
	XMP_StringPtr verStr = "";
	
		XMP_Assert ( rootNode->name == "rdf:RDF" );
	
		if ( (options & kXMP_RequireXMPMeta) &&
		     ((rootNode->parent == 0) ||
		      ((rootNode->parent->name != "x:xmpmeta") && (rootNode->parent->name != "x:xapmeta"))) ) return 0;

		for ( size_t attrNum = 0, attrLim = rootNode->parent->attrs.size(); attrNum < attrLim; ++attrNum ) {
			const XML_Node * currAttr =rootNode->parent->attrs[attrNum];
			if ( (currAttr->name == "x:xmptk") || (currAttr->name == "x:xaptk") ) {
				verStr = currAttr->value.c_str();
				break;
			}
		}
		
	// Decode the version number into MMmmuubbb digits. If any part is too big, peg it at 99 or 999.
	
	unsigned long part;
	while ( (*verStr != 0) && ((*verStr < '0') || (*verStr > '9')) ) ++verStr;
	
	part = 0;
	while ( (*verStr != 0) && ('0' <= *verStr) && (*verStr <= '9') ) {
		part = (part * 10) + (*verStr - '0');
		++verStr;
	}
	if ( part > 99 ) part = 99;
	thiz->prevTkVer = part * 100*100*1000;
	
	part = 0;
	if ( *verStr == '.' ) ++verStr;
	while ( (*verStr != 0) && ('0' <= *verStr) && (*verStr <= '9') ) {
		part = (part * 10) + (*verStr - '0');
		++verStr;
	}
	if ( part > 99 ) part = 99;
	thiz->prevTkVer += part * 100*1000;
	
	part = 0;
	if ( *verStr == '.' ) ++verStr;
	while ( (*verStr != 0) && ('0' <= *verStr) && (*verStr <= '9') ) {
		part = (part * 10) + (*verStr - '0');
		++verStr;
	}
	if ( part > 99 ) part = 99;
	thiz->prevTkVer += part * 1000;
	
	part = 0;
	if ( *verStr == '-' ) ++verStr;
	while ( (*verStr != 0) && ('0' <= *verStr) && (*verStr <= '9') ) {
		part = (part * 10) + (*verStr - '0');
		++verStr;
	}
	if ( part > 999 ) part = 999;
	thiz->prevTkVer += part;
	
	return rootNode;
	
}	// FindRootNode

// -------------------------------------------------------------------------------------------------
// NormalizeDCArrays
// -----------------
//
// Undo the denormalization performed by the XMP used in Acrobat 5. If a Dublin Core array had only
// one item, it was serialized as a simple property. The xml:lang attribute was dropped from an
// alt-text item if the language was x-default.

// *** This depends on the dc: namespace prefix.

static void
NormalizeDCArrays ( XMP_Node * xmpTree )
{
	XMP_Node * dcSchema = FindSchemaNode ( xmpTree, kXMP_NS_DC, kXMP_ExistingOnly );
	if ( dcSchema == 0 ) return;
	
	for ( size_t propNum = 0, propLimit = dcSchema->children.size(); propNum < propLimit; ++propNum ) {
		XMP_Node *     currProp  = dcSchema->children[propNum];
		XMP_OptionBits arrayForm = 0;
		
		if ( ! XMP_PropIsSimple ( currProp->options ) ) continue;	// Nothing to do if not simple.
		
		if ( (currProp->name == "dc:creator" )     ||	// See if it is supposed to be an array.
		     (currProp->name == "dc:date" ) ) {			// *** Think about an array of char* and a loop.
			arrayForm = kXMP_PropArrayIsOrdered;
		} else if (
		     (currProp->name == "dc:description" ) ||
		     (currProp->name == "dc:rights" )      ||
		     (currProp->name == "dc:title" ) ) {
			arrayForm = kXMP_PropArrayIsAltText;
		} else if (
		     (currProp->name == "dc:contributor" ) ||
		     (currProp->name == "dc:language" )    ||
		     (currProp->name == "dc:publisher" )   ||
		     (currProp->name == "dc:relation" )    ||
		     (currProp->name == "dc:subject" )     ||
		     (currProp->name == "dc:type" ) ) {
			arrayForm = kXMP_PropValueIsArray;
		}
		if ( arrayForm == 0 ) continue;	// Nothing to do if it isn't supposed to be an array.
		
		arrayForm = VerifySetOptions ( arrayForm, 0 );	// Set the implicit array bits.
		XMP_Node * newArray = new XMP_Node ( dcSchema, currProp->name.c_str(), arrayForm );
		dcSchema->children[propNum] = newArray;
		newArray->children.push_back ( currProp );
		currProp->parent = newArray;
		currProp->name = kXMP_ArrayItemName;
		
		if ( XMP_ArrayIsAltText ( arrayForm ) && (! (currProp->options & kXMP_PropHasLang)) ) {
			XMP_Node * newLang = new XMP_Node ( currProp, "xml:lang", "x-default", kXMP_PropIsQualifier );
			currProp->options |= (kXMP_PropHasQualifiers | kXMP_PropHasLang);
			if ( currProp->qualifiers.empty() ) {	// *** Need a util?
				currProp->qualifiers.push_back ( newLang );
			} else {
				currProp->qualifiers.insert ( currProp->qualifiers.begin(), newLang );
			}
		}

	}
	
}	// NormalizeDCArrays


// -------------------------------------------------------------------------------------------------
// CompareAliasedSubtrees
// ----------------------

// *** Change to do some alias-specific setup, then use CompareSubtrees. One special case for
// *** aliases is a simple to x-default alias, the options and qualifiers obviously differ.

static void
CompareAliasedSubtrees ( XMP_Node * aliasNode, XMP_Node * baseNode, bool outerCall = true )
{
	// ! The outermost call is special. The names almost certainly differ. The qualifiers (and
	// ! hence options) will differ for an alias to the x-default item of a langAlt array.
	if ( (aliasNode->value != baseNode->value) ||
	     (aliasNode->children.size() != baseNode->children.size()) ) {
		XMP_Throw ( "Mismatch between alias and base nodes", kXMPErr_BadXMP );
	}
	if ( ! outerCall ) {
		if ( (aliasNode->name != baseNode->name) ||
		     (aliasNode->options != baseNode->options) ||
		     (aliasNode->qualifiers.size() != baseNode->qualifiers.size()) ) {
			XMP_Throw ( "Mismatch between alias and base nodes", kXMPErr_BadXMP );
		}
	}
	
	for ( size_t childNum = 0, childLim = aliasNode->children.size(); childNum < childLim; ++childNum ) {
		XMP_Node * aliasChild = aliasNode->children[childNum];
		XMP_Node * baseChild  = baseNode->children[childNum];
		CompareAliasedSubtrees ( aliasChild, baseChild, false );
	}
	
	for ( size_t qualNum = 0, qualLim = aliasNode->qualifiers.size(); qualNum < qualLim; ++qualNum ) {
		XMP_Node * aliasQual = aliasNode->qualifiers[qualNum];
		XMP_Node * baseQual  = baseNode->qualifiers[qualNum];
		CompareAliasedSubtrees ( aliasQual, baseQual, false );
	}
	
}	// CompareAliasedSubtrees


// -------------------------------------------------------------------------------------------------
// TransplantArrayItemAlias
// ------------------------

static void
TransplantArrayItemAlias ( XMP_Node * oldParent, size_t oldNum, XMP_Node * newParent )
{
	XMP_Node * childNode = oldParent->children[oldNum];

	if ( newParent->options & kXMP_PropArrayIsAltText ) {
		if ( childNode->options & kXMP_PropHasLang ) {
			XMP_Throw ( "Alias to x-default already has a language qualifier", kXMPErr_BadXMP );	// *** Allow x-default.
		}
		childNode->options |= (kXMP_PropHasQualifiers | kXMP_PropHasLang);
		XMP_Node * langQual = new XMP_Node ( childNode, "xml:lang", "x-default", kXMP_PropIsQualifier );	// *** AddLangQual util?
		if ( childNode->qualifiers.empty() ) {
			childNode->qualifiers.push_back ( langQual );
		} else {
			childNode->qualifiers.insert ( childNode->qualifiers.begin(), langQual );
		}
	}

	oldParent->children.erase ( oldParent->children.begin() + oldNum );
	childNode->name = kXMP_ArrayItemName;
	childNode->parent = newParent;
	if ( newParent->children.empty() ) {
		newParent->children.push_back ( childNode );
	} else {
		newParent->children.insert ( newParent->children.begin(), childNode );
	}

}	// TransplantArrayItemAlias


// -------------------------------------------------------------------------------------------------
// TransplantNamedAlias
// --------------------

static void
TransplantNamedAlias ( XMP_Node * oldParent, size_t oldNum, XMP_Node * newParent, XMP_VarString & newName )
{
	XMP_Node * childNode = oldParent->children[oldNum];

	oldParent->children.erase ( oldParent->children.begin() + oldNum );
	childNode->name = newName;
	childNode->parent = newParent;
	newParent->children.push_back ( childNode );

}	// TransplantNamedAlias


// -------------------------------------------------------------------------------------------------
// MoveExplicitAliases
// -------------------

static void
MoveExplicitAliases ( XMP_Node * tree, XMP_OptionBits parseOptions )
{
	tree->options ^= kXMP_PropHasAliases;
	const bool strictAliasing = ((parseOptions & kXMP_StrictAliasing) != 0);
	
	// Visit all of the top level nodes looking for aliases. If there is no base, transplant the
	// alias subtree. If there is a base and strict aliasing is on, make sure the alias and base 
	// subtrees match.
	
	// ! Use "while" loops not "for" loops since both the schema and property loops can remove the
	// ! current item from the vector being traversed. And don't increment the counter for a delete.
	
	size_t schemaNum = 0;
	while ( schemaNum < tree->children.size() ) {
		XMP_Node * currSchema = tree->children[schemaNum];
		
		size_t propNum = 0;
		while ( propNum < currSchema->children.size() ) {
			XMP_Node * currProp = currSchema->children[propNum];
			if ( ! (currProp->options & kXMP_PropIsAlias) ) {
				++propNum;
				continue;
			}
			currProp->options ^= kXMP_PropIsAlias;

			// Find the base path, look for the base schema and root node.

			XMP_AliasMapPos aliasPos = sRegisteredAliasMap->find ( currProp->name );
			XMP_Assert ( aliasPos != sRegisteredAliasMap->end() );
			XMP_ExpandedXPath & basePath = aliasPos->second;
			XMP_OptionBits arrayOptions = (basePath[kRootPropStep].options & kXMP_PropArrayFormMask);

			XMP_Node * baseSchema = FindSchemaNode ( tree, basePath[kSchemaStep].step.c_str(), kXMP_CreateNodes );
			if ( baseSchema->options & kXMP_NewImplicitNode ) baseSchema->options ^= kXMP_NewImplicitNode;
			XMP_Node * baseNode = FindChildNode ( baseSchema, basePath[kRootPropStep].step.c_str(), kXMP_ExistingOnly );

			if ( baseNode == 0 ) {
			
				if ( basePath.size() == 2 ) {
					// A top-to-top alias, transplant the property.
					TransplantNamedAlias ( currSchema, propNum, baseSchema, basePath[kRootPropStep].step );
				} else {
					// An alias to an array item, create the array and transplant the property.
					baseNode = new XMP_Node ( baseSchema, basePath[kRootPropStep].step.c_str(), arrayOptions );
					baseSchema->children.push_back ( baseNode );
					TransplantArrayItemAlias ( currSchema, propNum, baseNode );
				}
			
			} else if ( basePath.size() == 2 ) {
			
				// The base node does exist and this is a top-to-top alias. Check for conflicts if
				// strict aliasing is on. Remove and delete the alias subtree.
				if ( strictAliasing ) CompareAliasedSubtrees ( currProp, baseNode );
				currSchema->children.erase ( currSchema->children.begin() + propNum );
				delete currProp;
			
			} else {
			
				// This is an alias to an array item and the array exists. Look for the aliased item.
				// Then transplant or check & delete as appropriate.
				
				XMP_Node * itemNode = 0;
				if ( arrayOptions & kXMP_PropArrayIsAltText ) {
					XMP_Index xdIndex = LookupLangItem ( baseNode, *xdefaultName );
					if ( xdIndex != -1 ) itemNode = baseNode->children[xdIndex];
				} else if ( ! baseNode->children.empty() ) {
					itemNode = baseNode->children[0];
				}
				
				if ( itemNode == 0 ) {
					TransplantArrayItemAlias ( currSchema, propNum, baseNode );
				} else {
					if ( strictAliasing ) CompareAliasedSubtrees ( currProp, itemNode );
					currSchema->children.erase ( currSchema->children.begin() + propNum );
					delete currProp;
				}

			}

		}	// Property loop
		
		// Increment the counter or remove an empty schema node.
		if ( currSchema->children.size() > 0 ) {
			++schemaNum;
		} else {
			delete tree->children[schemaNum];	// ! Delete the schema node itself.
			tree->children.erase ( tree->children.begin() + schemaNum );
		}
		
	}	// Schema loop
	
}	// MoveExplicitAliases


// -------------------------------------------------------------------------------------------------
// FixGPSTimeStamp
// ---------------

static void
FixGPSTimeStamp ( XMP_Node * exifSchema, XMP_Node * gpsDateTime )
{
	XMP_DateTime binGPSStamp;
	try {
		XMPUtils::ConvertToDate ( gpsDateTime->value.c_str(), &binGPSStamp );
	} catch ( ... ) {
		return;	// Don't let a bad date stop other things.
	}
	if ( (binGPSStamp.year != 0) || (binGPSStamp.month != 0) || (binGPSStamp.day != 0) ) return;
	
	XMP_Node * otherDate = FindChildNode ( exifSchema, "exif:DateTimeOriginal", kXMP_ExistingOnly );
	if ( otherDate == 0 ) otherDate = FindChildNode ( exifSchema, "exif:DateTimeDigitized", kXMP_ExistingOnly );
	if ( otherDate == 0 ) return;

	XMP_DateTime binOtherDate;
	try {
		XMPUtils::ConvertToDate ( otherDate->value.c_str(), &binOtherDate );
	} catch ( ... ) {
		return;	// Don't let a bad date stop other things.
	}
	
	binGPSStamp.year  = binOtherDate.year;
	binGPSStamp.month = binOtherDate.month;
	binGPSStamp.day   = binOtherDate.day;

	XMP_StringPtr goodStr;
	XMP_StringLen goodLen;
	XMPUtils::ConvertFromDate ( binGPSStamp, &goodStr, &goodLen );
	
	gpsDateTime->value.assign ( goodStr, goodLen );

}	// FixGPSTimeStamp


// -------------------------------------------------------------------------------------------------
// MigrateAudioCopyright
// ---------------------
//
// The initial support for WAV files mapped a legacy ID3 audio copyright into a new xmpDM:copyright
// property. This is special case code to migrate that into dc:rights['x-default']. The rules:
//
//   1. If there is no dc:rights array, or an empty array -
//      Create one with dc:rights['x-default'] set from double linefeed and xmpDM:copyright.
//
//   2. If there is a dc:rights array but it has no x-default item -
//      Create an x-default item as a copy of the first item then apply rule #3.
//
//   3. If there is a dc:rights array with an x-default item, look for a double linefeed in the value.
//      A. If no double linefeed, compare the x-default value to the xmpDM:copyright value.
//         A1. If they match then leave the x-default value alone.
//         A2. Otherwise, append a double linefeed and the xmpDM:copyright value to the x-default value.
//      B. If there is a double linefeed, compare the trailing text to the xmpDM:copyright value.
//         B1. If they match then leave the x-default value alone.
//         B2. Otherwise, replace the trailing x-default text with the xmpDM:copyright value.
//
//   4. In all cases, delete the xmpDM:copyright property.

static void
MigrateAudioCopyright ( XMPMeta * xmp, XMP_Node * dmCopyright )
{

	try {
	
		std::string & dmValue = dmCopyright->value;
		static const char * kDoubleLF = "\xA\xA";
		
		XMP_Node * dcSchema = FindSchemaNode ( &xmp->tree, kXMP_NS_DC, kXMP_CreateNodes );
		XMP_Node * dcRightsArray = FindChildNode ( dcSchema, "dc:rights", kXMP_ExistingOnly );
		
		if ( (dcRightsArray == 0) || dcRightsArray->children.empty() ) {
		
			// 1. No dc:rights array, create from double linefeed and xmpDM:copyright.
			dmValue.insert ( 0, kDoubleLF );
			xmp->SetLocalizedText ( kXMP_NS_DC, "rights", "", "x-default",  dmValue.c_str(), 0 );
		
		} else {

			std::string xdefaultStr ( "x-default" );
			
			XMP_Index xdIndex = LookupLangItem ( dcRightsArray, xdefaultStr );
			
			if ( xdIndex < 0 ) {
				// 2. No x-default item, create from the first item.
				XMP_StringPtr firstValue = dcRightsArray->children[0]->value.c_str();
				xmp->SetLocalizedText ( kXMP_NS_DC, "rights", "", "x-default",  firstValue, 0 );
				xdIndex = LookupLangItem ( dcRightsArray, xdefaultStr );
			}
						
			// 3. Look for a double linefeed in the x-default value.
			XMP_Assert ( xdIndex == 0 );
			std::string & defaultValue = dcRightsArray->children[xdIndex]->value;
			XMP_Index lfPos = defaultValue.find ( kDoubleLF );
			
			if ( lfPos < 0 ) {
			
				// 3A. No double LF, compare whole values.
				if ( dmValue != defaultValue ) {
					// 3A2. Append the xmpDM:copyright to the x-default item.
					defaultValue += kDoubleLF;
					defaultValue += dmValue;
				}
			
			} else {
			
				// 3B. Has double LF, compare the tail.
				if ( defaultValue.compare ( lfPos+2, std::string::npos, dmValue ) != 0 ) {
					// 3B2. Replace the x-default tail.
					defaultValue.replace ( lfPos+2, std::string::npos, dmValue );
				}
			
			}

		}
		
		// 4. Get rid of the xmpDM:copyright.
		xmp->DeleteProperty ( kXMP_NS_DM, "copyright" );
	
	} catch ( ... ) {
		// Don't let failures (like a bad dc:rights form) stop other cleanup.
	}

}	// MigrateAudioCopyright


// -------------------------------------------------------------------------------------------------
// RepairAltText
// -------------
//
// Make sure that the array is well-formed AltText. Each item must be simple and have an xml:lang
// qualifier. If repairs are needed, keep simple non-empty items by adding the xml:lang.

static void
RepairAltText ( XMP_Node & tree, XMP_StringPtr schemaNS, XMP_StringPtr arrayName )
{
	XMP_Node * schemaNode = FindSchemaNode ( &tree, schemaNS, kXMP_ExistingOnly );
	if ( schemaNode == 0 ) return;
	
	XMP_Node * arrayNode = FindChildNode ( schemaNode, arrayName, kXMP_ExistingOnly );
	if ( (arrayNode == 0) || XMP_ArrayIsAltText ( arrayNode->options ) ) return;	// Already OK.
	
	if ( ! XMP_PropIsArray ( arrayNode->options ) ) return;	// ! Not even an array, leave it alone.
	// *** Should probably change simple values to LangAlt with 'x-default' item.
	
	arrayNode->options |= (kXMP_PropArrayIsOrdered | kXMP_PropArrayIsAlternate | kXMP_PropArrayIsAltText);
	
	for ( int i = arrayNode->children.size()-1; i >= 0; --i ) {	// ! Need a signed index type.

		XMP_Node * currChild = arrayNode->children[i];

		if ( ! XMP_PropIsSimple ( currChild->options ) ) {

			// Delete non-simple children.
			delete ( currChild );
			arrayNode->children.erase ( arrayNode->children.begin() + i );

		} else if ( ! XMP_PropHasLang ( currChild->options ) ) {
		
			if ( currChild->value.empty() ) {

				// Delete empty valued children that have no xml:lang.
				delete ( currChild );
				arrayNode->children.erase ( arrayNode->children.begin() + i );

			} else {

				// Add an xml:lang qualifier with the value "x-repair".
				XMP_Node * repairLang = new XMP_Node ( currChild, "xml:lang", "x-repair", kXMP_PropIsQualifier );
				if ( currChild->qualifiers.empty() ) {
					currChild->qualifiers.push_back ( repairLang );
				} else {
					currChild->qualifiers.insert ( currChild->qualifiers.begin(), repairLang );
				}
				currChild->options |= (kXMP_PropHasQualifiers | kXMP_PropHasLang);

			}

		}

	}

}	// RepairAltText


// -------------------------------------------------------------------------------------------------
// TouchUpDataModel
// ----------------

static void
TouchUpDataModel ( XMPMeta * xmp )
{
	XMP_Node & tree = xmp->tree;
	
	// Do special case touch ups for certain schema.

	XMP_Node * currSchema = 0;

	currSchema = FindSchemaNode ( &tree, kXMP_NS_EXIF, kXMP_ExistingOnly );
	if ( currSchema != 0 ) {

		// Do a special case fix for exif:GPSTimeStamp.
		XMP_Node * gpsDateTime = FindChildNode ( currSchema, "exif:GPSTimeStamp", kXMP_ExistingOnly );
		if ( gpsDateTime != 0 ) FixGPSTimeStamp ( currSchema, gpsDateTime );
	
		// *** Should probably have RepairAltText change simple values to LangAlt with 'x-default' item.
		// *** For now just do this for exif:UserComment, the one case we know about, late in cycle fix.
		XMP_Node * userComment = FindChildNode ( currSchema, "exif:UserComment", kXMP_ExistingOnly );
		if ( (userComment != 0) && XMP_PropIsSimple ( userComment->options ) ) {
			XMP_Node * newChild = new XMP_Node ( userComment, kXMP_ArrayItemName,
												 userComment->value.c_str(), userComment->options );
			newChild->qualifiers.swap ( userComment->qualifiers );
			if ( ! XMP_PropHasLang ( newChild->options ) ) {
				XMP_Node * langQual = new XMP_Node ( newChild, "xml:lang", "x-default", kXMP_PropIsQualifier );
				newChild->qualifiers.insert ( newChild->qualifiers.begin(), langQual );
				newChild->options |= (kXMP_PropHasQualifiers | kXMP_PropHasLang);
			}
			userComment->value.erase();
			userComment->options = kXMP_PropArrayFormMask;	// ! Happens to have all the right bits.
			userComment->children.push_back ( newChild );
		}

	}

	currSchema = FindSchemaNode ( &tree, kXMP_NS_DM, kXMP_ExistingOnly );
	if ( currSchema != 0 ) {
		// Do a special case migration of xmpDM:copyright to dc:rights['x-default']. Do this before
		// the dc: touch up since it can affect the dc: schema.
		XMP_Node * dmCopyright = FindChildNode ( currSchema, "xmpDM:copyright", kXMP_ExistingOnly );
		if ( dmCopyright != 0 ) MigrateAudioCopyright ( xmp, dmCopyright );
	}

	currSchema = FindSchemaNode ( &tree, kXMP_NS_DC, kXMP_ExistingOnly );
	if ( currSchema != 0 ) {
		// Do a special case fix for dc:subject, make sure it is an unordered array.
		XMP_Node * dcSubject = FindChildNode ( currSchema, "dc:subject", kXMP_ExistingOnly );
		if ( dcSubject != 0 ) {
			XMP_OptionBits keepMask = ~(kXMP_PropArrayIsOrdered | kXMP_PropArrayIsAlternate | kXMP_PropArrayIsAltText);
			dcSubject->options &= keepMask;	// Make sure any ordered array bits are clear.
		}
	}
	
	// Fix any broken AltText arrays that we know about.
	
	RepairAltText ( tree, kXMP_NS_DC, "dc:description" );	// ! Note inclusion of prefixes for direct node lookup!
	RepairAltText ( tree, kXMP_NS_DC, "dc:rights" );
	RepairAltText ( tree, kXMP_NS_DC, "dc:title" );
	RepairAltText ( tree, kXMP_NS_XMP_Rights, "xmpRights:UsageTerms" );
	RepairAltText ( tree, kXMP_NS_EXIF, "exif:UserComment" );
	
	// Tweak old XMP: Move an instance ID from rdf:about to the xmpMM:InstanceID property. An old
	// instance ID usually looks like "uuid:bac965c4-9d87-11d9-9a30-000d936b79c4", plus InDesign
	// 3.0 wrote them like "bac965c4-9d87-11d9-9a30-000d936b79c4". If the name looks like a UUID
	// simply move it to xmpMM:InstanceID, don't worry about any existing xmpMM:InstanceID. Both
	// will only be present when a newer file with the xmpMM:InstanceID property is updated by an
	// old app that uses rdf:about.
	
	if ( ! tree.name.empty() ) {

		bool nameIsUUID = false;
		XMP_StringPtr nameStr = tree.name.c_str();

		if ( XMP_LitNMatch ( nameStr, "uuid:", 5 ) ) {

			nameIsUUID = true;

		} else if ( tree.name.size() == 36 ) {

			nameIsUUID = true;	// ! Assume true, we'll set it to false below if not.
			for ( int i = 0;  i < 36; ++i ) {
				char ch = nameStr[i];
				if ( ch == '-' ) {
					if ( (i == 8) || (i == 13) || (i == 18) || (i == 23) ) continue;
					nameIsUUID = false;
					break;
				} else {
					if ( (('0' <= ch) && (ch <= '9')) || (('a' <= ch) && (ch <= 'z')) ) continue;
					nameIsUUID = false;
					break;
				}
			}

		}
		
		if ( nameIsUUID ) {

			XMP_ExpandedXPath expPath;
			ExpandXPath ( kXMP_NS_XMP_MM, "InstanceID", &expPath );
			XMP_Node * idNode = FindNode ( &tree, expPath, kXMP_CreateNodes, 0 );
			if ( idNode == 0 ) XMP_Throw ( "Failure creating xmpMM:InstanceID", kXMPErr_InternalFailure );

			idNode->options = 0;	// Clobber any existing xmpMM:InstanceID.
			idNode->value = tree.name;
			idNode->RemoveChildren();
			idNode->RemoveQualifiers();

			tree.name.erase();

		}

	}

}	// TouchUpDataModel


// -------------------------------------------------------------------------------------------------
// DetermineInputEncoding
// ----------------------
//
// Try to determine the character encoding, making a guess if the input is too short. We make some
// simplifying assumtions: the first character must be U+FEFF or ASCII, U+0000 is not allowed. The
// XML 1.1 spec is even more strict, UTF-16 XML documents must begin with U+FEFF, and the first
// "real" character must be '<'. Ignoring the XML declaration, the first XML character could be '<',
// space, tab, CR, or LF.
//
// The possible input sequences are:
//
//   Cases with U+FEFF
//      EF BB BF -- - UTF-8
//      FE FF -- -- - Big endian UTF-16
//      00 00 FE FF - Big endian UTF 32
//      FF FE 00 00 - Little endian UTF-32
//      FF FE -- -- - Little endian UTF-16
//
//   Cases with ASCII
//      nn mm -- -- - UTF-8 -
//      00 00 00 nn - Big endian UTF-32
//      00 nn -- -- - Big endian UTF-16
//      nn 00 00 00 - Little endian UTF-32
//      nn 00 -- -- - Little endian UTF-16
//
// ! We don't check for full patterns, or for errors. We just check enough to determine what the
// ! only possible (or reasonable) case would be.

static XMP_OptionBits
DetermineInputEncoding ( const XMP_Uns8 * buffer, size_t length )
{
	if ( length < 2 ) return kXMP_EncodeUTF8;
	
	XMP_Uns8 * uniChar = (XMP_Uns8*)buffer;	// ! Make sure comparisons are unsigned.
	
	if ( uniChar[0] == 0 ) {
	
		// These cases are:
		//   00 nn -- -- - Big endian UTF-16
		//   00 00 00 nn - Big endian UTF-32
		//   00 00 FE FF - Big endian UTF 32

		if ( (length < 4) || (uniChar[1] != 0) ) return kXMP_EncodeUTF16Big;
		return kXMP_EncodeUTF32Big;
		
	} else if ( uniChar[0] < 0x80 ) {
	
		// These cases are:
		//   nn mm -- -- - UTF-8, includes EF BB BF case
		//   nn 00 00 00 - Little endian UTF-32
		//   nn 00 -- -- - Little endian UTF-16

		if ( uniChar[1] != 0 )  return kXMP_EncodeUTF8;
		if ( (length < 4) || (uniChar[2] != 0) ) return kXMP_EncodeUTF16Little;
		return kXMP_EncodeUTF32Little;

	} else {
	
		// These cases are:
		//   EF BB BF -- - UTF-8
		//   FE FF -- -- - Big endian UTF-16
		//   FF FE 00 00 - Little endian UTF-32
		//   FF FE -- -- - Little endian UTF-16

		if ( uniChar[0] == 0xEF ) return kXMP_EncodeUTF8;
		if ( uniChar[0] == 0xFE ) return kXMP_EncodeUTF16Big;
		if ( (length < 4) || (uniChar[2] != 0) ) return kXMP_EncodeUTF16Little;
		return kXMP_EncodeUTF32Little;

	}
		
}	// DetermineInputEncoding


// -------------------------------------------------------------------------------------------------
// CountUTF8
// ---------
//
// Look for a valid multi-byte UTF-8 sequence and return its length. Returns 0 for an invalid UTF-8
// sequence. Returns a negative value for a partial valid sequence at the end of the buffer.
//
// The checking is not strict. We simply count the number of high order 1 bits in the first byte,
// then look for n-1 following bytes whose high order 2 bits are 1 and 0. We do not check for a
// minimal length representation of the codepoint, or that the codepoint is defined by Unicode.

static int
CountUTF8 ( const XMP_Uns8 * charStart, const XMP_Uns8 * bufEnd )
{
	XMP_Assert ( charStart < bufEnd );		// Catch this in debug builds.
	if ( charStart >= bufEnd ) return 0;	// Don't run-on in release builds.
	if ( (*charStart & 0xC0) != 0xC0 ) return 0;	// Must have at least 2 high bits set.
	
	int byteCount = 2;
	XMP_Uns8 firstByte = *charStart;
	for ( firstByte = firstByte << 2; (firstByte & 0x80) != 0; firstByte = firstByte << 1 ) ++byteCount;
	
	if ( (charStart + byteCount) > bufEnd ) return -byteCount;

	for ( int i = 1; i < byteCount; ++i ) {
		if ( (charStart[i] & 0xC0) != 0x80 ) return 0;
	}
	
	return byteCount;
	
}	// CountUTF8


// -------------------------------------------------------------------------------------------------
// CountControlEscape
// ------------------
//
// Look for a numeric escape sequence for a "prohibited" ASCII control character. These are 0x7F,
// and the range 0x00..0x1F except for tab/LF/CR. Return 0 if this is definitely not a numeric
// escape, the length of the escape if found, or a negative value for a partial escape.

static int
CountControlEscape ( const XMP_Uns8 * escStart, const XMP_Uns8 * bufEnd )
{
	XMP_Assert ( escStart < bufEnd );	// Catch this in debug builds.
	if ( escStart >= bufEnd ) return 0;	// Don't run-on in release builds.
	XMP_Assert ( *escStart == '&' );
	
	size_t tailLen = bufEnd - escStart;
	if ( tailLen < 5 ) return -1;	// Don't need a more thorough check, we'll catch it on the next pass.
	
	if ( strncmp ( (char*)escStart, "&#x", 3 ) != 0 ) return 0;
	
	XMP_Uns8 escValue = 0;
	const XMP_Uns8 * escPos = escStart + 3;
	
	if ( ('0' <= *escPos) && (*escPos <= '9') ) {
		escValue = *escPos - '0';
		++escPos;
	} else if ( ('A' <= *escPos) && (*escPos <= 'F') ) {
		escValue = *escPos - 'A' + 10;
		++escPos;
	} else if ( ('a' <= *escPos) && (*escPos <= 'f') ) {
		escValue = *escPos - 'a' + 10;
		++escPos;
	}
	
	if ( ('0' <= *escPos) && (*escPos <= '9') ) {
		escValue = (escValue << 4) + (*escPos - '0');
		++escPos;
	} else if ( ('A' <= *escPos) && (*escPos <= 'F') ) {
		escValue = (escValue << 4) + (*escPos - 'A' + 10);
		++escPos;
	} else if ( ('a' <= *escPos) && (*escPos <= 'f') ) {
		escValue = (escValue << 4) + (*escPos - 'a' + 10);
		++escPos;
	}
	
	if ( escPos == bufEnd ) return -1;	// Partial escape.
	if ( *escPos != ';' ) return 0;
	
	size_t escLen = escPos - escStart + 1;
	if ( escLen < 5 ) return 0;	// ! Catch "&#x;".
	
	if ( (escValue == kTab) || (escValue == kLF) || (escValue == kCR) ) return 0;	// An allowed escape.
	
	return escLen;	// Found a full "prohibited" numeric escape.
	
}	// CountControlEscape


// -------------------------------------------------------------------------------------------------
// ProcessUTF8Portion
// ------------------
//
// Early versions of the XMP spec mentioned allowing ISO Latin-1 input. There are also problems with
// some clients placing ASCII control characters within XMP values. This is an XML problem, the XML
// spec only allows tab (0x09), LF (0x0A), and CR (0x0D) from the 0x00..0x1F range. As a concession
// to this we scan 8-bit input for byte sequences that are not valid UTF-8 or in the 0x00..0x1F
// range and replace each byte as follows:
//   0x00..0x1F - Replace with a space, except for tab, CR, and LF.
//   0x7F       - Replace with a space. This is ASCII Delete, not allowed by ISO Latin-1.
//   0x80..0x9F - Replace with the UTF-8 for a corresponding Unicode character.
//   0xA0..0XFF - Replace with the UTF-8 for a corresponding Unicode character.
//
// The 0x80..0x9F range is not defined by Latin-1. But the Windows 1252 code page defines these and
// is otherwise the same as Latin-1.
//
// For at least historical compatibility reasons we also find and replace singly escaped ASCII
// control characters. The Expat parser we're using does not allow numeric escapes like "&#x10;".
// The XML spec is clear that raw controls are not allowed (in the RestrictedChar set), but it isn't
// as clear about numeric escapes for them. At any rate, Expat complains, so we treat the numeric
// escapes like raw characters and replace them with a space.
//
// We check for 1 or 2 hex digits ("&#x9;" or "&#x09;") and upper or lower case ("&#xA;" or "&#xa;").
// The full escape sequence is 5 or 6 bytes.

static size_t
ProcessUTF8Portion ( XMLParserAdapter * xmlParser,
					 const XMP_Uns8 *   buffer,
					 size_t				length,
					 bool				last )
{
	const XMP_Uns8 * bufEnd = buffer + length;
	
	const XMP_Uns8 * spanStart = buffer;
	const XMP_Uns8 * spanEnd;
		
	for ( spanEnd = spanStart; spanEnd < bufEnd; ++spanEnd ) {

		if ( (0x20 <= *spanEnd) && (*spanEnd <= 0x7E) && (*spanEnd != '&') ) continue;	// A regular ASCII character.

		if ( *spanEnd >= 0x80 ) {
		
			// See if this is a multi-byte UTF-8 sequence, or a Latin-1 character to replace.

			int uniLen = CountUTF8 ( spanEnd, bufEnd );

			if ( uniLen > 0 ) {

				// A valid UTF-8 character, keep it as-is.
				spanEnd += uniLen - 1;	// ! The loop increment will put back the +1.

			} else if ( (uniLen < 0) && (! last) ) {

				// Have a partial UTF-8 character at the end of the buffer and more input coming.
				xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
				return (spanEnd - buffer);

			} else {

				// Not a valid UTF-8 sequence. Replace the first byte with the Latin-1 equivalent.
				xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
				const char * replacement = kReplaceLatin1 [ *spanEnd - 0x80 ];
				xmlParser->ParseBuffer ( replacement, strlen ( replacement ), false );
				spanStart = spanEnd + 1;	// ! The loop increment will do "spanEnd = spanStart".

			}
		
		} else if ( (*spanEnd < 0x20) || (*spanEnd == 0x7F) ) {

			// Replace ASCII controls other than tab, LF, and CR with a space.

			if ( (*spanEnd == kTab) || (*spanEnd == kLF) || (*spanEnd == kCR) ) continue;

			xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
			xmlParser->ParseBuffer ( " ", 1, false );
			spanStart = spanEnd + 1;	// ! The loop increment will do "spanEnd = spanStart".
		
		} else {
		
			// See if this is a numeric escape sequence for a prohibited ASCII control.
			
			XMP_Assert ( *spanEnd == '&' );
			int escLen = CountControlEscape ( spanEnd, bufEnd );
			
			if ( escLen < 0 ) {

				// Have a partial numeric escape in this buffer, wait for more input.
				if ( last ) continue;	// No more buffers, not an escape, absorb as normal input.
				xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
				return (spanEnd - buffer);

			} else if ( escLen > 0 ) {

				// Have a complete numeric escape to replace.
				xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
				xmlParser->ParseBuffer ( " ", 1, false );
				spanStart = spanEnd + escLen;
				spanEnd = spanStart - 1;	// ! The loop continuation will increment spanEnd!

			}

		}
		
	}
	
	XMP_Assert ( spanEnd == bufEnd );

	if ( spanStart < bufEnd ) xmlParser->ParseBuffer ( spanStart, (spanEnd - spanStart), false );
	if ( last ) xmlParser->ParseBuffer ( " ", 1, true );
	
	return length;

}	// ProcessUTF8Portion


// -------------------------------------------------------------------------------------------------
// ParseFromBuffer
// ---------------
//
// Although most clients will probably parse everything in one call, we have a buffered API model
// and need to support even the extreme case of 1 byte at a time parsing. This is considerably
// complicated by some special cases for 8-bit input. Because of this, the first thing we do is
// determine whether the input is 8-bit, UTF-16, or UTF-32.
//
// Both the 8-bit special cases and the encoding determination are easier to do with 8 bytes or more
// of input. The XMLParserAdapter class has a pending-input buffer for this. At the start of parsing
// we (moght) try to fill this buffer before determining the input character encoding. After that,
// we (might) use this buffer with the current input to simplify the logic in Process8BitInput. The
// "(might)" part means that we don't actually use the pending-input buffer unless we have to. In
// particular, the common case of single-buffer parsing won't use it.

void
XMPMeta::ParseFromBuffer ( XMP_StringPtr  buffer,
						   XMP_StringLen  xmpSize,
						   XMP_OptionBits options )
{
	if ( (buffer == 0) && (xmpSize != 0) ) XMP_Throw ( "Null parse buffer", kXMPErr_BadParam );
	if ( xmpSize == kXMP_UseNullTermination ) xmpSize = strlen ( buffer );
	
	const bool lastClientCall = ((options & kXMP_ParseMoreBuffers) == 0);	// *** Could use FlagIsSet & FlagIsClear macros.
	
	this->tree.ClearNode();	// Make sure the target XMP object is totally empty.

	if ( this->xmlParser == 0 ) {
		if ( (xmpSize == 0) && lastClientCall ) return;	// Tolerate empty parse. Expat complains if there are no XML elements.
		this->xmlParser = XMP_NewExpatAdapter();
	}
	
	XMLParserAdapter& parser = *this->xmlParser;
	
	#if 0	// XMP_DebugBuild
		if ( parser.parseLog != 0 ) {
			char message [200];	// AUDIT: Using sizeof(message) below for snprintf length is safe.
			snprintf ( message, sizeof(message), "<!-- ParseFromBuffer, length = %d, options = %X%s -->",	// AUDIT: See above.
					   xmpSize, options, (lastClientCall ? " (last)" : "") );
			fwrite ( message, 1, strlen(message), parser.parseLog );
			fflush ( parser.parseLog );
		}
	#endif
		
	try {	// Cleanup the tree and xmlParser if anything fails.
	
		// Determine the character encoding before doing any real parsing. This is needed to do the
		// 8-bit special processing.
		
		if ( parser.charEncoding == XMP_OptionBits(-1) ) {

			if ( (parser.pendingCount == 0) && (xmpSize >= kXMLPendingInputMax) ) {

				// This ought to be the common case, the first buffer is big enough.
				parser.charEncoding = DetermineInputEncoding ( (XMP_Uns8*)buffer, xmpSize );

			} else {
			
				// Try to fill the pendingInput buffer before calling DetermineInputEncoding.

				size_t pendingOverlap = kXMLPendingInputMax - parser.pendingCount;
				if ( pendingOverlap > xmpSize ) pendingOverlap = xmpSize;

				memcpy ( &parser.pendingInput[parser.pendingCount], buffer, pendingOverlap );	// AUDIT: Count is safe.
				buffer += pendingOverlap;
				xmpSize -= pendingOverlap;
				parser.pendingCount += pendingOverlap;

				if ( (! lastClientCall) && (parser.pendingCount < kXMLPendingInputMax) ) return;
				parser.charEncoding = DetermineInputEncoding ( parser.pendingInput, parser.pendingCount );
				
				#if Trace_ParsingHackery
					fprintf ( stderr, "XMP Character encoding is %d\n", parser.charEncoding );
				#endif
			
			}

		}
		
		// We have the character encoding. Process UTF-16 and UTF-32 as is. UTF-8 needs special
		// handling to take care of things like ISO Latin-1 or unescaped ASCII controls.

		XMP_Assert ( parser.charEncoding != XMP_OptionBits(-1) );

		if ( parser.charEncoding != kXMP_EncodeUTF8 ) {
		
			if ( parser.pendingCount > 0 ) {
				// Might have pendingInput from the above portion to determine the character encoding.
				parser.ParseBuffer ( parser.pendingInput, parser.pendingCount, false );
			}
			parser.ParseBuffer ( buffer, xmpSize, lastClientCall );
			
		} else {

			#if Trace_ParsingHackery
				fprintf ( stderr, "Parsing %d bytes @ %.8X, %s, %d pending, context: %.8s\n",
						  xmpSize, buffer, (lastClientCall ? "last" : "not last"), parser.pendingCount, buffer );
			#endif

			// The UTF-8 processing is a bit complex due to the need to tolerate ISO Latin-1 input.
			// This is done by scanning the input for byte sequences that are not valid UTF-8,
			// assuming they are Latin-1 characters in the range 0x80..0xFF. This requires saving a
			// pending input buffer to handle partial UTF-8 sequences at the end of a buffer.
			
			while ( parser.pendingCount > 0 ) {
			
				// We've got some leftover input, process it first then continue with the current
				// buffer. Try to fill the pendingInput buffer before parsing further. We use a loop
				// for wierd edge cases like a 2 byte input buffer, using 1 byte for pendingInput,
				// then having a partial UTF-8 end and need to absorb more.
				
				size_t pendingOverlap = kXMLPendingInputMax - parser.pendingCount;
				if ( pendingOverlap > xmpSize ) pendingOverlap = xmpSize;
				
				memcpy ( &parser.pendingInput[parser.pendingCount], buffer, pendingOverlap );	// AUDIT: Count is safe.
				parser.pendingCount += pendingOverlap;
				buffer += pendingOverlap;
				xmpSize -= pendingOverlap;

				if ( (! lastClientCall) && (parser.pendingCount < kXMLPendingInputMax) ) return;
				size_t bytesDone = ProcessUTF8Portion ( &parser, parser.pendingInput, parser.pendingCount, lastClientCall );
				size_t bytesLeft = parser.pendingCount - bytesDone;

				#if Trace_ParsingHackery
					fprintf ( stderr, "   ProcessUTF8Portion handled %d pending bytes\n", bytesDone );
				#endif
				
				if ( bytesDone == parser.pendingCount ) {

					// Done with all of the pending input, move on to the current buffer.
					parser.pendingCount = 0;

				} else if ( bytesLeft <= pendingOverlap ) {

					// The leftover pending input all came from the current buffer. Exit this loop.
					buffer -= bytesLeft;
					xmpSize += bytesLeft;
					parser.pendingCount = 0;

				} else if ( xmpSize > 0 ) {

					// Pull more of the current buffer into the pending input and try again.
					// Backup by this pass's overlap so the loop entry code runs OK.
					parser.pendingCount -= pendingOverlap;
					buffer -= pendingOverlap;
					xmpSize += pendingOverlap;

				} else {

					// There is no more of the current buffer. Wait for more. Partial sequences at
					// the end of the last buffer should be treated as Latin-1 by ProcessUTF8Portion.
					XMP_Assert ( ! lastClientCall );
					parser.pendingCount = bytesLeft;
					memcpy ( &parser.pendingInput[0], &parser.pendingInput[bytesDone], bytesLeft );	// AUDIT: Count is safe.
					return;

				}
			
			}
			
			// Done with the pending input, process the current buffer.

			size_t bytesDone = ProcessUTF8Portion ( &parser, (XMP_Uns8*)buffer, xmpSize, lastClientCall );

			#if Trace_ParsingHackery
				fprintf ( stderr, "   ProcessUTF8Portion handled %d additional bytes\n", bytesDone );
			#endif
			
			if ( bytesDone < xmpSize ) {

				XMP_Assert ( ! lastClientCall );
				size_t bytesLeft = xmpSize - bytesDone;
				if ( bytesLeft > kXMLPendingInputMax ) XMP_Throw ( "Parser bytesLeft too large", kXMPErr_InternalFailure );

				memcpy ( parser.pendingInput, &buffer[bytesDone], bytesLeft );	// AUDIT: Count is safe.
				parser.pendingCount = bytesLeft;
				return;	// Wait for the next buffer.

			}

		}
		
		if ( lastClientCall ) {
		
			#if XMP_DebugBuild && DumpXMLParseTree
				if ( parser.parseLog == 0 ) parser.parseLog = stdout;
				DumpXMLTree ( parser.parseLog, parser.tree, 0 );
			#endif

			const XML_Node * xmlRoot = FindRootNode ( this, *this->xmlParser, options );

			if ( xmlRoot != 0 ) {

				ProcessRDF ( &this->tree, *xmlRoot, options );
				NormalizeDCArrays ( &this->tree );
				if ( this->tree.options & kXMP_PropHasAliases ) MoveExplicitAliases ( &this->tree, options );
				TouchUpDataModel ( this );
				
				// Delete empty schema nodes. Do this last, other cleanup can make empty schema.
				size_t schemaNum = 0;
				while ( schemaNum < this->tree.children.size() ) {
					XMP_Node * currSchema = this->tree.children[schemaNum];
					if ( currSchema->children.size() > 0 ) {
						++schemaNum;
					} else {
						delete this->tree.children[schemaNum];	// ! Delete the schema node itself.
						this->tree.children.erase ( this->tree.children.begin() + schemaNum );
					}
				}
				
			}

			delete this->xmlParser;
			this->xmlParser = 0;

		}
		
	} catch ( ... ) {

		delete this->xmlParser;
		this->xmlParser = 0;
		prevTkVer = 0;
		this->tree.ClearNode();
		throw;

	}
	
}	// ParseFromBuffer

// =================================================================================================