-
Notifications
You must be signed in to change notification settings - Fork 282
/
XMPUtils.cpp
2152 lines (1621 loc) · 68.9 KB
/
XMPUtils.cpp
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
// =================================================================================================
// Copyright 2002-2007 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 "XMP_Environment.h" // ! This must be the first include!
#include "XMPCore_Impl.hpp"
#include "XMPUtils.hpp"
#include "MD5.h"
#include <map>
#include <limits>
#include <time.h>
#include <string.h>
#include <cstdlib>
#include <locale.h>
#include <errno.h>
#include <stdio.h> // For snprintf.
#if XMP_WinBuild
#ifdef _MSC_VER
#pragma warning ( disable : 4800 ) // forcing value to bool 'true' or 'false' (performance warning)
#pragma warning ( disable : 4996 ) // '...' was declared deprecated
#endif
#endif
// =================================================================================================
// Local Types and Constants
// =========================
static const char * sBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// =================================================================================================
// Static Variables
// ================
XMP_VarString * sComposedPath = 0; // *** Only really need 1 string. Shrink periodically?
XMP_VarString * sConvertedValue = 0;
XMP_VarString * sBase64Str = 0;
XMP_VarString * sCatenatedItems = 0;
XMP_VarString * sStandardXMP = 0;
XMP_VarString * sExtendedXMP = 0;
XMP_VarString * sExtendedDigest = 0;
// =================================================================================================
// Local Utilities
// ===============
// -------------------------------------------------------------------------------------------------
// ANSI Time Functions
// -------------------
//
// A bit of hackery to use the best available time functions. Mac and UNIX have thread safe versions
// of gmtime and localtime. On Mac the CodeWarrior functions are buggy, use Apple's.
#if XMP_UNIXBuild
typedef time_t ansi_tt;
typedef struct tm ansi_tm;
#define ansi_time time
#define ansi_mktime mktime
#define ansi_difftime difftime
#define ansi_gmtime gmtime_r
#define ansi_localtime localtime_r
#elif XMP_WinBuild
// ! VS.Net 2003 (VC7) does not provide thread safe versions of gmtime and localtime.
// ! VS.Net 2005 (VC8) inverts the parameters for the safe versions of gmtime and localtime.
typedef time_t ansi_tt;
typedef struct tm ansi_tm;
#define ansi_time time
#define ansi_mktime mktime
#define ansi_difftime difftime
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#define ansi_gmtime(tt,tm) gmtime_s ( tm, tt )
#define ansi_localtime(tt,tm) localtime_s ( tm, tt )
#else
static inline void ansi_gmtime ( const ansi_tt * ttTime, ansi_tm * tmTime )
{
ansi_tm * tmx = gmtime ( ttTime ); // ! Hope that there is no race!
if ( tmx == 0 ) XMP_Throw ( "Failure from ANSI C gmtime function", kXMPErr_ExternalFailure );
*tmTime = *tmx;
}
static inline void ansi_localtime ( const ansi_tt * ttTime, ansi_tm * tmTime )
{
ansi_tm * tmx = localtime ( ttTime ); // ! Hope that there is no race!
if ( tmx == 0 ) XMP_Throw ( "Failure from ANSI C localtime function", kXMPErr_ExternalFailure );
*tmTime = *tmx;
}
#endif
#elif XMP_MacBuild
#if ! __MWERKS__
typedef time_t ansi_tt;
typedef struct tm ansi_tm;
#define ansi_time time
#define ansi_mktime mktime
#define ansi_difftime difftime
#define ansi_gmtime gmtime_r
#define ansi_localtime localtime_r
#else
// ! The CW versions are buggy. Use Apple's code, time_t, and "struct tm".
#include <mach-o/dyld.h>
typedef _BSD_TIME_T_ ansi_tt;
typedef struct apple_tm {
int tm_sec; /* seconds after the minute [0-60] */
int tm_min; /* minutes after the hour [0-59] */
int tm_hour; /* hours since midnight [0-23] */
int tm_mday; /* day of the month [1-31] */
int tm_mon; /* months since January [0-11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0-6] */
int tm_yday; /* days since January 1 [0-365] */
int tm_isdst; /* Daylight Savings Time flag */
long tm_gmtoff; /* offset from CUT in seconds */
char *tm_zone; /* timezone abbreviation */
} ansi_tm;
typedef ansi_tt (* GetTimeProc) ( ansi_tt * ttTime );
typedef ansi_tt (* MakeTimeProc) ( ansi_tm * tmTime );
typedef double (* DiffTimeProc) ( ansi_tt t1, ansi_tt t0 );
typedef void (* ConvertTimeProc) ( const ansi_tt * ttTime, ansi_tm * tmTime );
static GetTimeProc ansi_time = 0;
static MakeTimeProc ansi_mktime = 0;
static DiffTimeProc ansi_difftime = 0;
static ConvertTimeProc ansi_gmtime = 0;
static ConvertTimeProc ansi_localtime = 0;
static void LookupTimeProcs()
{
_dyld_lookup_and_bind_with_hint ( "_time", "libSystem", (XMP_Uns32*)&ansi_time, 0 );
_dyld_lookup_and_bind_with_hint ( "_mktime", "libSystem", (XMP_Uns32*)&ansi_mktime, 0 );
_dyld_lookup_and_bind_with_hint ( "_difftime", "libSystem", (XMP_Uns32*)&ansi_difftime, 0 );
_dyld_lookup_and_bind_with_hint ( "_gmtime_r", "libSystem", (XMP_Uns32*)&ansi_gmtime, 0 );
_dyld_lookup_and_bind_with_hint ( "_localtime_r", "libSystem", (XMP_Uns32*)&ansi_localtime, 0 );
}
#endif
#endif
// -------------------------------------------------------------------------------------------------
// IsLeapYear
// ----------
static bool
IsLeapYear ( long year )
{
if ( year < 0 ) year = -year + 1; // Fold the negative years, assuming there is a year 0.
if ( (year % 4) != 0 ) return false; // Not a multiple of 4.
if ( (year % 100) != 0 ) return true; // A multiple of 4 but not a multiple of 100.
if ( (year % 400) == 0 ) return true; // A multiple of 400.
return false; // A multiple of 100 but not a multiple of 400.
} // IsLeapYear
// -------------------------------------------------------------------------------------------------
// DaysInMonth
// -----------
static int
DaysInMonth ( XMP_Int32 year, XMP_Int32 month )
{
static short daysInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
int days = daysInMonth [ month ];
if ( (month == 2) && IsLeapYear ( year ) ) days += 1;
return days;
} // DaysInMonth
// -------------------------------------------------------------------------------------------------
// AdjustTimeOverflow
// ------------------
static void
AdjustTimeOverflow ( XMP_DateTime * time )
{
enum { kBillion = 1000*1000*1000L };
// ----------------------------------------------------------------------------------------------
// To be safe against pathalogical overflow we first adjust from month to second, then from
// nanosecond back up to month. This leaves each value closer to zero before propagating into it.
// For example if the hour and minute are both near max, adjusting minutes first can cause the
// hour to overflow.
// ! Photoshop 8 creates "time only" values with zeros for year, month, and day.
if ( (time->year != 0) || (time->month != 0) || (time->day != 0) ) {
while ( time->month < 1 ) {
time->year -= 1;
time->month += 12;
}
while ( time->month > 12 ) {
time->year += 1;
time->month -= 12;
}
while ( time->day < 1 ) {
time->month -= 1;
if ( time->month < 1 ) { // ! Keep the months in range for indexing daysInMonth!
time->year -= 1;
time->month += 12;
}
time->day += DaysInMonth ( time->year, time->month ); // ! Decrement month before so index here is right!
}
while ( time->day > DaysInMonth ( time->year, time->month ) ) {
time->day -= DaysInMonth ( time->year, time->month ); // ! Increment month after so index here is right!
time->month += 1;
if ( time->month > 12 ) {
time->year += 1;
time->month -= 12;
}
}
}
while ( time->hour < 0 ) {
time->day -= 1;
time->hour += 24;
}
while ( time->hour >= 24 ) {
time->day += 1;
time->hour -= 24;
}
while ( time->minute < 0 ) {
time->hour -= 1;
time->minute += 60;
}
while ( time->minute >= 60 ) {
time->hour += 1;
time->minute -= 60;
}
while ( time->second < 0 ) {
time->minute -= 1;
time->second += 60;
}
while ( time->second >= 60 ) {
time->minute += 1;
time->second -= 60;
}
while ( time->nanoSecond < 0 ) {
time->second -= 1;
time->nanoSecond += kBillion;
}
while ( time->nanoSecond >= kBillion ) {
time->second += 1;
time->nanoSecond -= kBillion;
}
while ( time->second < 0 ) {
time->minute -= 1;
time->second += 60;
}
while ( time->second >= 60 ) {
time->minute += 1;
time->second -= 60;
}
while ( time->minute < 0 ) {
time->hour -= 1;
time->minute += 60;
}
while ( time->minute >= 60 ) {
time->hour += 1;
time->minute -= 60;
}
while ( time->hour < 0 ) {
time->day -= 1;
time->hour += 24;
}
while ( time->hour >= 24 ) {
time->day += 1;
time->hour -= 24;
}
if ( (time->year != 0) || (time->month != 0) || (time->day != 0) ) {
while ( time->month < 1 ) { // Make sure the months are OK first, for DaysInMonth.
time->year -= 1;
time->month += 12;
}
while ( time->month > 12 ) {
time->year += 1;
time->month -= 12;
}
while ( time->day < 1 ) {
time->month -= 1;
if ( time->month < 1 ) {
time->year -= 1;
time->month += 12;
}
time->day += DaysInMonth ( time->year, time->month );
}
while ( time->day > DaysInMonth ( time->year, time->month ) ) {
time->day -= DaysInMonth ( time->year, time->month );
time->month += 1;
if ( time->month > 12 ) {
time->year += 1;
time->month -= 12;
}
}
}
} // AdjustTimeOverflow
// -------------------------------------------------------------------------------------------------
// GatherInt
// ---------
static XMP_Int32
GatherInt ( XMP_StringPtr strValue, size_t * _pos, const char * errMsg )
{
size_t pos = *_pos;
XMP_Int32 value = 0;
// Limits for overflow checking. Assuming that the maximum value of XMP_Int32
// is 2147483647, then tens_upperbound == 214748364 and ones_upperbound == 7.
// Most of the time, we can just check that value < tens_upperbound to confirm
// that the calculation won't overflow, which makes the bounds checking more
// efficient for the common case.
const XMP_Int32 tens_upperbound = std::numeric_limits<XMP_Int32>::max() / 10;
const XMP_Int32 ones_upperbound = std::numeric_limits<XMP_Int32>::max() % 10;
for ( char ch = strValue[pos]; ('0' <= ch) && (ch <= '9'); ++pos, ch = strValue[pos] ) {
const XMP_Int32 digit = ch - '0';
if (value >= tens_upperbound) {
if (value > tens_upperbound || digit > ones_upperbound) {
XMP_Throw ( errMsg, kXMPErr_BadParam );
}
}
value = (value * 10) + digit;
}
if ( pos == *_pos ) XMP_Throw ( errMsg, kXMPErr_BadParam );
*_pos = pos;
return value;
} // GatherInt
// -------------------------------------------------------------------------------------------------
static void FormatFullDateTime ( XMP_DateTime & tempDate, char * buffer, size_t bufferLen )
{
AdjustTimeOverflow ( &tempDate ); // Make sure all time parts are in range.
if ( (tempDate.second == 0) && (tempDate.nanoSecond == 0) ) {
// Output YYYY-MM-DDThh:mmTZD.
snprintf ( buffer, bufferLen, "%.4d-%02d-%02dT%02d:%02d", // AUDIT: Callers pass sizeof(buffer).
static_cast<int>(tempDate.year), static_cast<int>(tempDate.month), static_cast<int>(tempDate.day), static_cast<int>(tempDate.hour), static_cast<int>(tempDate.minute) );
} else if ( tempDate.nanoSecond == 0 ) {
// Output YYYY-MM-DDThh:mm:ssTZD.
snprintf ( buffer, bufferLen, "%.4d-%02d-%02dT%02d:%02d:%02d", // AUDIT: Callers pass sizeof(buffer).
static_cast<int>(tempDate.year), static_cast<int>(tempDate.month), static_cast<int>(tempDate.day),
static_cast<int>(tempDate.hour), static_cast<int>(tempDate.minute), static_cast<int>(tempDate.second) );
} else {
// Output YYYY-MM-DDThh:mm:ss.sTZD.
snprintf ( buffer, bufferLen, "%.4d-%02d-%02dT%02d:%02d:%02d.%09d", // AUDIT: Callers pass sizeof(buffer).
static_cast<int>(tempDate.year), static_cast<int>(tempDate.month), static_cast<int>(tempDate.day),
static_cast<int>(tempDate.hour), static_cast<int>(tempDate.minute), static_cast<int>(tempDate.second), static_cast<int>(tempDate.nanoSecond) );
for ( size_t i = strlen(buffer)-1; buffer[i] == '0'; --i ) buffer[i] = 0; // Trim excess digits.
}
} // FormatFullDateTime
// -------------------------------------------------------------------------------------------------
// DecodeBase64Char
// ----------------
// The decode mapping:
//
// encoded encoded raw
// char value value
// ------- ------- -----
// A .. Z 0x41 .. 0x5A 0 .. 25
// a .. z 0x61 .. 0x7A 26 .. 51
// 0 .. 9 0x30 .. 0x39 52 .. 61
// + 0x2B 62
// / 0x2F 63
static unsigned char
DecodeBase64Char ( XMP_Uns8 ch )
{
if ( ('A' <= ch) && (ch <= 'Z') ) {
ch = ch - 'A';
} else if ( ('a' <= ch) && (ch <= 'z') ) {
ch = ch - 'a' + 26;
} else if ( ('0' <= ch) && (ch <= '9') ) {
ch = ch - '0' + 52;
} else if ( ch == '+' ) {
ch = 62;
} else if ( ch == '/' ) {
ch = 63;
} else if ( (ch == ' ') || (ch == kTab) || (ch == kLF) || (ch == kCR) ) {
ch = 0xFF; // Will be ignored by the caller.
} else {
XMP_Throw ( "Invalid base-64 encoded character", kXMPErr_BadParam );
}
return ch;
} // DecodeBase64Char ();
// -------------------------------------------------------------------------------------------------
// EstimateSizeForJPEG
// -------------------
//
// Estimate the serialized size for the subtree of an XMP_Node. Support for PackageForJPEG.
static size_t
EstimateSizeForJPEG ( const XMP_Node * xmpNode )
{
size_t estSize = 0;
size_t nameSize = xmpNode->name.size();
bool includeName = (! XMP_PropIsArray ( xmpNode->parent->options ));
if ( XMP_PropIsSimple ( xmpNode->options ) ) {
if ( includeName ) estSize += (nameSize + 3); // Assume attribute form.
estSize += xmpNode->value.size();
} else if ( XMP_PropIsArray ( xmpNode->options ) ) {
// The form of the value portion is: <rdf:Xyz><rdf:li>...</rdf:li>...</rdf:Xyx>
if ( includeName ) estSize += (2*nameSize + 5);
size_t arraySize = xmpNode->children.size();
estSize += 9 + 10; // The rdf:Xyz tags.
estSize += arraySize * (8 + 9); // The rdf:li tags.
for ( size_t i = 0; i < arraySize; ++i ) {
estSize += EstimateSizeForJPEG ( xmpNode->children[i] );
}
} else {
// The form is: <headTag rdf:parseType="Resource">...fields...</tailTag>
if ( includeName ) estSize += (2*nameSize + 5);
estSize += 25; // The rdf:parseType="Resource" attribute.
size_t fieldCount = xmpNode->children.size();
for ( size_t i = 0; i < fieldCount; ++i ) {
estSize += EstimateSizeForJPEG ( xmpNode->children[i] );
}
}
return estSize;
} // EstimateSizeForJPEG
// -------------------------------------------------------------------------------------------------
// MoveOneProperty
// ---------------
static bool MoveOneProperty ( XMPMeta & stdXMP, XMPMeta * extXMP,
XMP_StringPtr schemaURI, XMP_StringPtr propName )
{
XMP_Node * propNode = 0;
XMP_NodePtrPos stdPropPos;
XMP_Node * stdSchema = FindSchemaNode ( &stdXMP.tree, schemaURI, kXMP_ExistingOnly, 0 );
if ( stdSchema != 0 ) {
propNode = FindChildNode ( stdSchema, propName, kXMP_ExistingOnly, &stdPropPos );
}
if ( propNode == 0 ) return false;
XMP_Node * extSchema = FindSchemaNode ( &extXMP->tree, schemaURI, kXMP_CreateNodes );
propNode->parent = extSchema;
extSchema->options &= ~kXMP_NewImplicitNode;
extSchema->children.push_back ( propNode );
stdSchema->children.erase ( stdPropPos );
DeleteEmptySchema ( stdSchema );
return true;
} // MoveOneProperty
// -------------------------------------------------------------------------------------------------
// CreateEstimatedSizeMap
// ----------------------
#ifndef Trace_PackageForJPEG
#define Trace_PackageForJPEG 0
#endif
typedef std::pair < XMP_VarString*, XMP_VarString* > StringPtrPair;
typedef std::multimap < size_t, StringPtrPair > PropSizeMap;
static void CreateEstimatedSizeMap ( XMPMeta & stdXMP, PropSizeMap * propSizes )
{
#if Trace_PackageForJPEG
printf ( " Creating top level property map:\n" );
#endif
for ( size_t s = stdXMP.tree.children.size(); s > 0; --s ) {
XMP_Node * stdSchema = stdXMP.tree.children[s-1];
for ( size_t p = stdSchema->children.size(); p > 0; --p ) {
XMP_Node * stdProp = stdSchema->children[p-1];
if ( (stdSchema->name == kXMP_NS_XMP_Note) &&
(stdProp->name == "xmpNote:HasExtendedXMP") ) continue; // ! Don't move xmpNote:HasExtendedXMP.
size_t propSize = EstimateSizeForJPEG ( stdProp );
StringPtrPair namePair ( &stdSchema->name, &stdProp->name );
PropSizeMap::value_type mapValue ( propSize, namePair );
(void) propSizes->insert ( propSizes->upper_bound ( propSize ), mapValue );
#if Trace_PackageForJPEG
printf ( " %d bytes, %s in %s\n", propSize, stdProp->name.c_str(), stdSchema->name.c_str() );
#endif
}
}
} // CreateEstimatedSizeMap
// -------------------------------------------------------------------------------------------------
// MoveLargestProperty
// -------------------
static size_t MoveLargestProperty ( XMPMeta & stdXMP, XMPMeta * extXMP, PropSizeMap & propSizes )
{
XMP_Assert ( ! propSizes.empty() );
#if 0
// *** Xcode 2.3 on Mac OS X 10.4.7 seems to have a bug where this does not pick the last
// *** item in the map. We'll just avoid it on all platforms until thoroughly tested.
PropSizeMap::iterator lastPos = propSizes.end();
--lastPos; // Move to the actual last item.
#else
PropSizeMap::iterator lastPos = propSizes.begin();
PropSizeMap::iterator nextPos = lastPos;
for ( ++nextPos; nextPos != propSizes.end(); ++nextPos ) lastPos = nextPos;
#endif
size_t propSize = lastPos->first;
const char * schemaURI = lastPos->second.first->c_str();
const char * propName = lastPos->second.second->c_str();
#if Trace_PackageForJPEG
printf ( " Move %s, %d bytes\n", propName, propSize );
#endif
bool moved = MoveOneProperty ( stdXMP, extXMP, schemaURI, propName );
XMP_Assert ( moved );
UNUSED(moved);
propSizes.erase ( lastPos );
return propSize;
} // MoveLargestProperty
// =================================================================================================
// Class Static Functions
// ======================
// -------------------------------------------------------------------------------------------------
// Initialize
// ----------
/* class static */ bool
XMPUtils::Initialize()
{
sComposedPath = new XMP_VarString();
sConvertedValue = new XMP_VarString();
sBase64Str = new XMP_VarString();
sCatenatedItems = new XMP_VarString();
sStandardXMP = new XMP_VarString();
sExtendedXMP = new XMP_VarString();
sExtendedDigest = new XMP_VarString();
#if XMP_MacBuild && __MWERKS__
LookupTimeProcs();
#endif
return true;
} // Initialize
// -------------------------------------------------------------------------------------------------
// Terminate
// ---------
#define EliminateGlobal(g) delete ( g ); g = 0
/* class static */ void
XMPUtils::Terminate() RELEASE_NO_THROW
{
EliminateGlobal ( sComposedPath );
EliminateGlobal ( sConvertedValue );
EliminateGlobal ( sBase64Str );
EliminateGlobal ( sCatenatedItems );
EliminateGlobal ( sStandardXMP );
EliminateGlobal ( sExtendedXMP );
EliminateGlobal ( sExtendedDigest );
return;
} // Terminate
// -------------------------------------------------------------------------------------------------
// Unlock
// ------
/* class static */ void
XMPUtils::Unlock ( XMP_OptionBits options )
{
UNUSED(options);
XMPMeta::Unlock ( 0 );
} // Unlock
// -------------------------------------------------------------------------------------------------
// ComposeArrayItemPath
// --------------------
//
// Return "arrayName[index]".
/* class static */ void
XMPUtils::ComposeArrayItemPath ( XMP_StringPtr schemaNS,
XMP_StringPtr arrayName,
XMP_Index itemIndex,
XMP_StringPtr * fullPath,
XMP_StringLen * pathSize )
{
XMP_Assert ( schemaNS != 0 ); // Enforced by wrapper.
XMP_Assert ( *arrayName != 0 ); // Enforced by wrapper.
XMP_Assert ( (fullPath != 0) && (pathSize != 0) ); // Enforced by wrapper.
XMP_ExpandedXPath expPath; // Just for side effects to check namespace and basic path.
ExpandXPath ( schemaNS, arrayName, &expPath );
if ( (itemIndex < 0) && (itemIndex != kXMP_ArrayLastItem) ) XMP_Throw ( "Array index out of bounds", kXMPErr_BadParam );
XMP_StringLen reserveLen = strlen(arrayName) + 2 + 32; // Room plus padding.
sComposedPath->erase();
sComposedPath->reserve ( reserveLen );
sComposedPath->append ( reserveLen, ' ' );
if ( itemIndex != kXMP_ArrayLastItem ) {
// AUDIT: Using string->size() for the snprintf length is safe.
snprintf ( const_cast<char*>(sComposedPath->c_str()), sComposedPath->size(), "%s[%d]", arrayName, static_cast<int>(itemIndex) );
} else {
*sComposedPath = arrayName;
*sComposedPath += "[last()] ";
(*sComposedPath)[sComposedPath->size()-1] = 0; // ! Final null is for the strlen at exit.
}
*fullPath = sComposedPath->c_str();
*pathSize = strlen ( *fullPath ); // ! Don't use sComposedPath->size()!
XMP_Enforce ( *pathSize < sComposedPath->size() ); // Rather late, but complain about buffer overflow.
} // ComposeArrayItemPath
// -------------------------------------------------------------------------------------------------
// ComposeStructFieldPath
// ----------------------
//
// Return "structName/ns:fieldName".
/* class static */ void
XMPUtils::ComposeStructFieldPath ( XMP_StringPtr schemaNS,
XMP_StringPtr structName,
XMP_StringPtr fieldNS,
XMP_StringPtr fieldName,
XMP_StringPtr * fullPath,
XMP_StringLen * pathSize )
{
XMP_Assert ( (schemaNS != 0) && (fieldNS != 0) ); // Enforced by wrapper.
XMP_Assert ( (*structName != 0) && (*fieldName != 0) ); // Enforced by wrapper.
XMP_Assert ( (fullPath != 0) && (pathSize != 0) ); // Enforced by wrapper.
XMP_ExpandedXPath expPath; // Just for side effects to check namespace and basic path.
ExpandXPath ( schemaNS, structName, &expPath );
XMP_ExpandedXPath fieldPath;
ExpandXPath ( fieldNS, fieldName, &fieldPath );
if ( fieldPath.size() != 2 ) XMP_Throw ( "The fieldName must be simple", kXMPErr_BadXPath );
XMP_StringLen reserveLen = strlen(structName) + fieldPath[kRootPropStep].step.size() + 1;
sComposedPath->erase();
sComposedPath->reserve ( reserveLen );
*sComposedPath = structName;
*sComposedPath += '/';
*sComposedPath += fieldPath[kRootPropStep].step;
*fullPath = sComposedPath->c_str();
*pathSize = sComposedPath->size();
} // ComposeStructFieldPath
// -------------------------------------------------------------------------------------------------
// ComposeQualifierPath
// --------------------
//
// Return "propName/?ns:qualName".
/* class static */ void
XMPUtils::ComposeQualifierPath ( XMP_StringPtr schemaNS,
XMP_StringPtr propName,
XMP_StringPtr qualNS,
XMP_StringPtr qualName,
XMP_StringPtr * fullPath,
XMP_StringLen * pathSize )
{
XMP_Assert ( (schemaNS != 0) && (qualNS != 0) ); // Enforced by wrapper.
XMP_Assert ( (*propName != 0) && (*qualName != 0) ); // Enforced by wrapper.
XMP_Assert ( (fullPath != 0) && (pathSize != 0) ); // Enforced by wrapper.
XMP_ExpandedXPath expPath; // Just for side effects to check namespace and basic path.
ExpandXPath ( schemaNS, propName, &expPath );
XMP_ExpandedXPath qualPath;
ExpandXPath ( qualNS, qualName, &qualPath );
if ( qualPath.size() != 2 ) XMP_Throw ( "The qualifier name must be simple", kXMPErr_BadXPath );
XMP_StringLen reserveLen = strlen(propName) + qualPath[kRootPropStep].step.size() + 2;
sComposedPath->erase();
sComposedPath->reserve ( reserveLen );
*sComposedPath = propName;
*sComposedPath += "/?";
*sComposedPath += qualPath[kRootPropStep].step;
*fullPath = sComposedPath->c_str();
*pathSize = sComposedPath->size();
} // ComposeQualifierPath
// -------------------------------------------------------------------------------------------------
// ComposeLangSelector
// -------------------
//
// Return "arrayName[?xml:lang="lang"]".
// *** #error "handle quotes in the lang - or verify format"
/* class static */ void
XMPUtils::ComposeLangSelector ( XMP_StringPtr schemaNS,
XMP_StringPtr arrayName,
XMP_StringPtr _langName,
XMP_StringPtr * fullPath,
XMP_StringLen * pathSize )
{
XMP_Assert ( schemaNS != 0 ); // Enforced by wrapper.
XMP_Assert ( (*arrayName != 0) && (*_langName != 0) ); // Enforced by wrapper.
XMP_Assert ( (fullPath != 0) && (pathSize != 0) ); // Enforced by wrapper.
XMP_ExpandedXPath expPath; // Just for side effects to check namespace and basic path.
ExpandXPath ( schemaNS, arrayName, &expPath );
XMP_VarString langName ( _langName );
NormalizeLangValue ( &langName );
XMP_StringLen reserveLen = strlen(arrayName) + langName.size() + 14;
sComposedPath->erase();
sComposedPath->reserve ( reserveLen );
*sComposedPath = arrayName;
*sComposedPath += "[?xml:lang=\"";
*sComposedPath += langName;
*sComposedPath += "\"]";
*fullPath = sComposedPath->c_str();
*pathSize = sComposedPath->size();
} // ComposeLangSelector
// -------------------------------------------------------------------------------------------------
// ComposeFieldSelector
// --------------------
//
// Return "arrayName[ns:fieldName="fieldValue"]".
// *** #error "handle quotes in the value"
/* class static */ void
XMPUtils::ComposeFieldSelector ( XMP_StringPtr schemaNS,
XMP_StringPtr arrayName,
XMP_StringPtr fieldNS,
XMP_StringPtr fieldName,
XMP_StringPtr fieldValue,
XMP_StringPtr * fullPath,
XMP_StringLen * pathSize )
{
XMP_Assert ( (schemaNS != 0) && (fieldNS != 0) && (fieldValue != 0) ); // Enforced by wrapper.
XMP_Assert ( (*arrayName != 0) && (*fieldName != 0) ); // Enforced by wrapper.
XMP_Assert ( (fullPath != 0) && (pathSize != 0) ); // Enforced by wrapper.
XMP_ExpandedXPath expPath; // Just for side effects to check namespace and basic path.
ExpandXPath ( schemaNS, arrayName, &expPath );
XMP_ExpandedXPath fieldPath;
ExpandXPath ( fieldNS, fieldName, &fieldPath );
if ( fieldPath.size() != 2 ) XMP_Throw ( "The fieldName must be simple", kXMPErr_BadXPath );
XMP_StringLen reserveLen = strlen(arrayName) + fieldPath[kRootPropStep].step.size() + strlen(fieldValue) + 5;
sComposedPath->erase();
sComposedPath->reserve ( reserveLen );
*sComposedPath = arrayName;
*sComposedPath += '[';
*sComposedPath += fieldPath[kRootPropStep].step;
*sComposedPath += "=\"";
*sComposedPath += fieldValue;
*sComposedPath += "\"]";
*fullPath = sComposedPath->c_str();
*pathSize = sComposedPath->size();
} // ComposeFieldSelector
// -------------------------------------------------------------------------------------------------
// ConvertFromBool
// ---------------
/* class static */ void
XMPUtils::ConvertFromBool ( bool binValue,
XMP_StringPtr * strValue,
XMP_StringLen * strSize )
{
XMP_Assert ( (strValue != 0) && (strSize != 0) ); // Enforced by wrapper.
if ( binValue ) {
*strValue = kXMP_TrueStr;
*strSize = strlen ( kXMP_TrueStr );
} else {
*strValue = kXMP_FalseStr;
*strSize = strlen ( kXMP_FalseStr );
}
} // ConvertFromBool
// -------------------------------------------------------------------------------------------------
// ConvertFromInt
// --------------
/* class static */ void
XMPUtils::ConvertFromInt ( XMP_Int32 binValue,
XMP_StringPtr format,
XMP_StringPtr * strValue,
XMP_StringLen * strSize )
{
XMP_Assert ( (format != 0) && (strValue != 0) && (strSize != 0) ); // Enforced by wrapper.
if ( *format == 0 ) format = "%d";
sConvertedValue->erase();
sConvertedValue->reserve ( 100 ); // More than enough for any reasonable format and value.
sConvertedValue->append ( 100, ' ' );
// AUDIT: Using string->size() for the snprintf length is safe.
snprintf ( const_cast<char*>(sConvertedValue->c_str()), sConvertedValue->size(), format, binValue );
*strValue = sConvertedValue->c_str();
*strSize = strlen ( *strValue ); // ! Don't use sConvertedValue->size()!
XMP_Enforce ( *strSize < sConvertedValue->size() ); // Rather late, but complain about buffer overflow.
} // ConvertFromInt
// -------------------------------------------------------------------------------------------------
// ConvertFromInt64
// ----------------
/* class static */ void
XMPUtils::ConvertFromInt64 ( XMP_Int64 binValue,
XMP_StringPtr format,
XMP_StringPtr * strValue,
XMP_StringLen * strSize )
{
XMP_Assert ( (format != 0) && (strValue != 0) && (strSize != 0) ); // Enforced by wrapper.
if ( *format == 0 ) format = "%lld";
sConvertedValue->erase();
sConvertedValue->reserve ( 100 ); // More than enough for any reasonable format and value.
sConvertedValue->append ( 100, ' ' );
// AUDIT: Using string->size() for the snprintf length is safe.
snprintf ( const_cast<char*>(sConvertedValue->c_str()), sConvertedValue->size(), format, binValue );
*strValue = sConvertedValue->c_str();
*strSize = strlen ( *strValue ); // ! Don't use sConvertedValue->size()!
XMP_Enforce ( *strSize < sConvertedValue->size() ); // Rather late, but complain about buffer overflow.
} // ConvertFromInt64
// -------------------------------------------------------------------------------------------------
// ConvertFromFloat
// ----------------
/* class static */ void
XMPUtils::ConvertFromFloat ( double binValue,
XMP_StringPtr format,
XMP_StringPtr * strValue,
XMP_StringLen * strSize )
{
XMP_Assert ( (format != 0) && (strValue != 0) && (strSize != 0) ); // Enforced by wrapper.
if ( *format == 0 ) format = "%f";
sConvertedValue->erase();
sConvertedValue->reserve ( 1000 ); // More than enough for any reasonable format and value.
sConvertedValue->append ( 1000, ' ' );
// AUDIT: Using string->size() for the snprintf length is safe.
snprintf ( const_cast<char*>(sConvertedValue->c_str()), sConvertedValue->size(), format, binValue );