summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FileHandlers/UCF_Handler.hpp
blob: f20a9b25d9805233003c51d9a66fdbac51e98d8e (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
#ifndef __UCF_Handler_hpp__
#define __UCF_Handler_hpp__	1

// =================================================================================================
// Copyright Adobe
// Copyright 2007 Adobe
// 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"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"
#include "public/include/XMP_IO.hpp"

#include "XMPFiles/source/XMPFiles_Impl.hpp"
#include "source/XMPFiles_IO.hpp"

// =================================================================================================
/// \file UCF_Handler.hpp
//
//  underlying math:
//    __   0   ______    0    ______  __
//            |  A   |       |  A   |
//            |      |       |      |
//         al |      |  (a2l)|      |
//         x  |------|   b2  |------|
//         xl |  X   |       |  B   |_
//         b  |------|  (b2l)|      | |
//            |  B   |   x2  |------| | B2 could also be
//         bl |      |   x2l |  X2  | | _after_ X2
//         cd |------|   cd2 |------|<'
//            |//CD//|       |//CD2/|
//        cdx |------|  cdx2 |------|
//        cdxl|------|  cdx2l|------|
//         cdl|//////|   cd2l|//////|
//         z  |------|     z2|------|
//            |      |       |      |
//        [zl]|  Z   |   z2l |  Z2  |
//         h  |------|   h2  |------|
//    fl      |  H   |       |  H2  |  f2l
//    __   hl |______|  (h2l)|______|  __
//
//    fl  file length pre (2 = post)
//    numCf  number of content files prior to injection
//    numCf2            "            post
//
//
//    l	length variable, all else offset
//    [ ] variable is not needed
//    ( ) variable is identical to left
//    a   content files prior to xmp (possibly: all)
//    b   content files behind xmp (possibly: 0)
//    x   xmp packet (possibly: 0)
//    cd  central directory
//    h   end of central directory
//
//    z   zip64 record and locator (if existing)
//
//    general rules:
//    the bigger A, the less rewrite effort.
//    (also within the CD)
//    putting XMP at the end maximizes A.
//
//    bool previousXMP == x!=0
//
//    (x==0) == (cdx==0) == (xl==0) == (cdxl==0)
//
//    std::vector<XMP_Uns32> cdOffsetsPre;
//
//    -----------------
//    asserts:
//( 1)  a == a2 == 0,	making these variables obsolete
//( 2)  a2l == al,		this block is not touched
//( 3)  b2 <= b,		b is only moved closer to the beginning of file
//( 4)  b2l == bl,		b does not change in size
//( 5)  x2 >= x,		b is only moved further down in the file
//
//( 6) x != 0, x2l != 0, cd != 0, cdl != 0
//    				none of these blocks is at the beginning ('mimetype' by spec),
//    				nor is any of them zero byte long
//( 7) h!=0, hl >= 22	header is not at the beginning, minimum size 22
//
//    file size computation:
//( 8) al + bl + xl +cdl +hl = fl
//( 9) al + bl + x2l+cd2l+hl = fl2
//
//(10) ( x==0 ) <=> ( cdx == 0 )
//    				if there's a packet in the pre-file, or there isn't
//(11) (x==0)	=> xl=0
//(12) (cdx==0)=> cdx=0
//
//(13) x==0 ==> b,bl,b2,b2l==0
//    				if there is no pre-xmp, B does not exist
//(14) x!=0 ==> al:=x, b:=x+xl, bl:=cd-b
//
//    zip 64:
//(15)  zl and z2l are basically equal, except _one_ of them is 0 :
//
//(16)  b2l is indeed never different t
//
//    FIXED_SIZE means the fixed (minimal) portion of a struct
//    TOTAL_SIZE indicates, that this struct indeed has a fixed, known total length
//
// =================================================================================================

extern XMPFileHandler* UCF_MetaHandlerCTor ( XMPFiles * parent );

extern bool UCF_CheckFormat ( XMP_FileFormat format,
							  XMP_StringPtr  filePath,
                              XMP_IO*    fileRef,
                              XMPFiles *     parent );

static const XMP_OptionBits kUCF_HandlerFlags = (
						kXMPFiles_CanInjectXMP     |
						kXMPFiles_CanExpand        |
						kXMPFiles_CanRewrite       |
						/* kXMPFiles_PrefersInPlace | removed, only reasonable for formats where difference is significant */
						kXMPFiles_AllowsOnlyXMP    |
						kXMPFiles_ReturnsRawPacket |
						// *** kXMPFiles_AllowsSafeUpdate |
						kXMPFiles_NeedsReadOnlyPacket //UCF/zip has checksums...
						);

enum {	// data descriptor
	// may or may not have a signature: 0x08074b50
	kUCF_DD_crc32				=   0,
	kUCF_DD_sizeCompressed		=   4,
	kUCF_DD_sizeUncompressed	=   8,
};

class UCF_MetaHandler : public XMPFileHandler
{
public:
	UCF_MetaHandler ( XMPFiles * _parent );
	~UCF_MetaHandler();

	void CacheFileData();
	void ProcessXMP();

	void UpdateFile ( bool doSafeUpdate );
    void WriteTempFile ( XMP_IO* tempRef );

protected:
	const static XMP_Uns16 xmpFilenameLen = 21;
	const static char* xmpFilename;

private:
	class Zip64EndOfDirectory {
	private:

	public:
		const static XMP_Uns16 o_sig				=	0;	// 0x06064b50
		const static XMP_Uns16 o_size				=	4;	// of this, excluding leading 12 bytes
				// == FIXED_SIZE -12, since we're never creating the extensible data sector...
		const static XMP_Uns16 o_VersionMade		=	12;
		const static XMP_Uns16 o_VersionNeededExtr	=	14;
		const static XMP_Uns16 o_numDisk			=	16;	// force 0
		const static XMP_Uns16 o_numCDDisk			=	20; // force 0
		const static XMP_Uns16 o_numCFsThisDisk		=	24;
		const static XMP_Uns16 o_numCFsTotal		=	32; // force equal
		const static XMP_Uns16 o_sizeOfCD			=	40; // (regular one, not Z64)
		const static XMP_Uns16 o_offsetCD			=	48; //           "

		const static XMP_Int32 FIXED_SIZE = 56;
		char  fields[FIXED_SIZE];

		const static XMP_Uns32 ID = 0x06064b50;

		Zip64EndOfDirectory( XMP_Int64 offsetCD, XMP_Int64 sizeOfCD, XMP_Uns64 numCFs )
		{
			memset(fields,'\0',FIXED_SIZE);

			PutUns32LE(ID				,&fields[o_sig] );
			PutUns64LE(FIXED_SIZE - 12,	&fields[o_size] );	//see above
			PutUns16LE(	45	,&fields[o_VersionMade] );
			PutUns16LE(	45	,&fields[o_VersionNeededExtr] );
			// fine at 0:	o_numDisk
			// fine at 0:	o_numCDDisk
			PutUns64LE( numCFs, &fields[o_numCFsThisDisk] );
			PutUns64LE( numCFs, &fields[o_numCFsTotal] );
			PutUns64LE( sizeOfCD,	&fields[o_sizeOfCD] );
			PutUns64LE( offsetCD,	&fields[o_offsetCD] );
		}

		void write(XMP_IO* file)
		{
			XMP_Validate( ID == GetUns32LE( &this->fields[o_sig] ), "invalid header on write", kXMPErr_BadFileFormat );
			file ->Write ( fields , FIXED_SIZE  );
		}

	};

	class Zip64Locator {
	public:
		const static XMP_Uns16 o_sig				=	0;	// 0x07064b50
		const static XMP_Uns16 o_numDiskZ64CD		=	4;	// force 0
		const static XMP_Uns16 o_offsZ64EOD			=   8;
		const static XMP_Uns16 o_numDisks			=   16; // set 1, tolerate 0

		const static XMP_Int32 TOTAL_SIZE			=	20;
		char  fields[TOTAL_SIZE];

		const static XMP_Uns32 ID = 0x07064b50;

		Zip64Locator( XMP_Int64 offsetZ64EOD )
		{
			memset(fields,'\0',TOTAL_SIZE);
			PutUns32LE(ID,				&fields[Zip64Locator::o_sig] );
			PutUns32LE(0,				&fields[Zip64Locator::o_numDiskZ64CD] );
			PutUns64LE(offsetZ64EOD,	&fields[Zip64Locator::o_offsZ64EOD] );
			PutUns32LE(1,				&fields[Zip64Locator::o_numDisks] );
		}

		// writes structure to file (starting at current position)
		void write(XMP_IO* file)
		{
			XMP_Validate( ID == GetUns32LE( &this->fields[o_sig] ), "invalid header on write", kXMPErr_BadFileFormat );
			file ->Write ( fields , TOTAL_SIZE  );
		}
	};

	struct EndOfDirectory {
	public:
		const static XMP_Int32 FIXED_SIZE = 22;	//32 bit type is important to not overrun on maxcomment
		const static XMP_Uns32 ID = 0x06054b50;
		const static XMP_Int32 COMMENT_MAX = 0xFFFF;
		//offsets
		const static XMP_Int32 o_CentralDirectorySize = 12;
		const static XMP_Int32 o_CentralDirectoryOffset = 16;
	};

	class FileHeader {
		private:
			//TODO intergrate in clear()
			void release() // avoid terminus free() since subject to a #define (mem-leak-check)
			{
				if (filename)	delete filename;
				if (extraField)	delete extraField;
				filename=0;
				extraField=0;
			}

		public:
			const static XMP_Uns32 SIG = 0x04034b50;
			const static XMP_Uns16 kdataDescriptorFlag = 0x8;

			const static XMP_Uns16 o_sig = 0;
			const static XMP_Uns16 o_extractVersion		=	4;
			const static XMP_Uns16 o_flags				=   6;
			const static XMP_Uns16 o_compression		=   8;
			const static XMP_Uns16 o_lastmodTime		=   10;
			const static XMP_Uns16 o_lastmodDate		=   12;
			const static XMP_Uns16 o_crc32				=   14;
			const static XMP_Uns16 o_sizeCompressed		=   18;
			const static XMP_Uns16 o_sizeUncompressed	=   22;
			const static XMP_Uns16 o_fileNameLength		=   26;
			const static XMP_Uns16 o_extraFieldLength	=   28;
			// total									30

			const static int FIXED_SIZE = 30;
			char  fields[FIXED_SIZE];

			char* filename;
			char* extraField;
			XMP_Uns16 filenameLen;
			XMP_Uns16 extraFieldLen;

			void clear()
			{
				this->release();
				memset(fields,'\0',FIXED_SIZE);
				//arm with minimal default values:
				PutUns32LE(0x04034b50,	&fields[FileHeader::o_sig] );
				PutUns16LE(0x14,		&fields[FileHeader::o_extractVersion] );
			}

			FileHeader() : filename(0),filenameLen(0),extraField(0),extraFieldLen(0)
			{
				clear();
			};

			// reads entire *FileHeader* structure from file (starting at current position)
			void read(XMP_IO* file)
			{
				this->release();

				file ->ReadAll ( fields , FIXED_SIZE  );

				XMP_Uns32 tmp32 = GetUns32LE( &this->fields[FileHeader::o_sig] );
				XMP_Validate( SIG == tmp32, "invalid header", kXMPErr_BadFileFormat );
				filenameLen   = GetUns16LE( &this->fields[FileHeader::o_fileNameLength] );
				extraFieldLen = GetUns16LE( &this->fields[FileHeader::o_extraFieldLength] );

				// nb unlike the CDFileHeader the FileHeader will in practice never have
				// extra fields. Reasoning: File headers never carry (their own) offsets,
				// (un)compressed size of XMP will hardly ever reach 4 GB

				if (filenameLen) {
					filename = new char[filenameLen];
					file->ReadAll ( filename, filenameLen );
				}
				if (extraFieldLen) {
					extraField = new char[extraFieldLen];
					file->ReadAll ( extraField, extraFieldLen );
					// *** NB: this WOULD need parsing for content files that are
					//   compressed or uncompressed >4GB (VERY unlikely for XMP)
				}
			}

			// writes structure to file (starting at current position)
			void write(XMP_IO* file)
			{
				XMP_Validate( SIG == GetUns32LE( &this->fields[FileHeader::o_sig] ), "invalid header on write", kXMPErr_BadFileFormat );

				filenameLen   = GetUns16LE( &this->fields[FileHeader::o_fileNameLength] );
				extraFieldLen = GetUns16LE( &this->fields[FileHeader::o_extraFieldLength] );

				file ->Write ( fields , FIXED_SIZE  );
				if (filenameLen)	file->Write ( filename, filenameLen    );
				if (extraFieldLen)	file->Write ( extraField, extraFieldLen  );
			}

			void transfer(const FileHeader &orig)
			{
				memcpy(fields,orig.fields,FIXED_SIZE);
				if (orig.extraField)
				{
					extraFieldLen=orig.extraFieldLen;
					extraField = new char[extraFieldLen];
					memcpy(extraField,orig.extraField,extraFieldLen);
				}
				if (orig.filename)
				{
					filenameLen=orig.filenameLen;
					filename = new char[filenameLen];
					memcpy(filename,orig.filename,filenameLen);
				}
			};

			void setXMPFilename()
			{
				// only needed for fresh structs, thus enforcing rather than catering to memory issues
				XMP_Enforce( (filenameLen==0) && (extraFieldLen == 0) );
				filenameLen = xmpFilenameLen;
				PutUns16LE(filenameLen,	&fields[FileHeader::o_fileNameLength] );
				filename = new char[xmpFilenameLen];
				memcpy(filename,"META-INF/metadata.xml",xmpFilenameLen);
			}

			XMP_Uns32 sizeHeader()
			{
				return this->FIXED_SIZE + this->filenameLen + this->extraFieldLen;
			}

			XMP_Uns32 sizeTotalCF()
			{
				//*** not zip64 bit safe yet, use only for non-large xmp packet
				return this->sizeHeader() + GetUns32LE( &fields[FileHeader::o_sizeCompressed] );
			}

			~FileHeader()
			{
				this->release();
			};

	}; //class FileHeader

	////// yes, this needs an own class
	////// offsets must be extracted, added, modified,
	////// come&go depending on being >0xffffff
	////class extraField {
	////	private:


	class CDFileHeader {
		private:
			void release()  //*** needed or can go?
			{
				if (filename)	delete filename;
				if (extraField)	delete extraField;
				if (comment)	delete comment;
				filename=0; filenameLen=0;
				extraField=0; extraFieldLen=0;
				comment=0; commentLen=0;
			}

			const static XMP_Uns32 SIG = 0x02014b50;

		public:
			const static XMP_Uns16	o_sig	=   0;	//0x02014b50
			const static XMP_Uns16	o_versionMadeBy		=   4;
			const static XMP_Uns16	o_extractVersion	=	6;
			const static XMP_Uns16	o_flags				=   8;
			const static XMP_Uns16	o_compression		=   10;
			const static XMP_Uns16	o_lastmodTime		=   12;
			const static XMP_Uns16	o_lastmodDate		=   14;
			const static XMP_Uns16	o_crc32				=   16;
			const static XMP_Uns16	o_sizeCompressed	=   20; // 16bit stub
			const static XMP_Uns16	o_sizeUncompressed	=   24; // 16bit stub
			const static XMP_Uns16	o_fileNameLength	=   28;
			const static XMP_Uns16	o_extraFieldLength	=   30;
			const static XMP_Uns16	o_commentLength		=   32;
			const static XMP_Uns16	o_diskNo			=   34;
			const static XMP_Uns16	o_internalAttribs	=   36;
			const static XMP_Uns16	o_externalAttribs	=   38;
			const static XMP_Uns16	o_offsetLocalHeader	=   42;	// 16bit stub
			//					total size is 4+12+12+10+8=46

			const static int FIXED_SIZE = 46;
			char fields[FIXED_SIZE];

			// do not bet on any zero-freeness,
			// certainly no zero termination (pascal strings),
			// treat as data blocks
			char* filename;
			char* extraField;
			char* comment;
			XMP_Uns16 filenameLen;
			XMP_Uns16 extraFieldLen;
			XMP_Uns16 commentLen;

			// full, real, parsed 64 bit values
			XMP_Int64 sizeUncompressed;
			XMP_Int64 sizeCompressed;
			XMP_Int64 offsetLocalHeader;

			CDFileHeader() : filename(0),extraField(0),comment(0),filenameLen(0),
				extraFieldLen(0),commentLen(0),sizeUncompressed(0),sizeCompressed(0),offsetLocalHeader(0)
			{
				memset(fields,'\0',FIXED_SIZE);
				//already arm with appropriate values where applicable:
				PutUns32LE(0x02014b50,	&fields[CDFileHeader::o_sig] );
				PutUns16LE(0x14,		&fields[CDFileHeader::o_extractVersion] );
			};

			// copy constructor
			CDFileHeader(const CDFileHeader& orig) : filename(0),extraField(0),comment(0),filenameLen(0),
				extraFieldLen(0),commentLen(0),sizeUncompressed(0),sizeCompressed(0),offsetLocalHeader(0)
			{
				memcpy(fields,orig.fields,FIXED_SIZE);
				if (orig.extraField)
				{
					extraFieldLen=orig.extraFieldLen;
					extraField = new char[extraFieldLen];
					memcpy(extraField , orig.extraField , extraFieldLen);
				}
				if (orig.filename)
				{
					filenameLen=orig.filenameLen;
					filename = new char[filenameLen];
					memcpy(filename , orig.filename , filenameLen);
				}
				if (orig.comment)
				{
					commentLen=orig.commentLen;
					comment = new char[commentLen];
					memcpy(comment , orig.comment , commentLen);
				}

				filenameLen   = orig.filenameLen;
				extraFieldLen = orig.extraFieldLen;
				commentLen    = orig.commentLen;

				sizeUncompressed = orig.sizeUncompressed;
				sizeCompressed = orig.sizeCompressed;
				offsetLocalHeader = orig.offsetLocalHeader;
			}

			// Assignment operator
			CDFileHeader& operator=(const CDFileHeader& obj)
			{
				XMP_Throw("not supported",kXMPErr_Unimplemented);
			}

			// reads entire structure from file (starting at current position)
			void read(XMP_IO* file)
			{
				this->release();

				file->ReadAll ( fields, FIXED_SIZE );
				XMP_Validate( SIG == GetUns32LE( &this->fields[CDFileHeader::o_sig] ), "invalid header", kXMPErr_BadFileFormat );

				filenameLen   = GetUns16LE( &this->fields[CDFileHeader::o_fileNameLength] );
				extraFieldLen = GetUns16LE( &this->fields[CDFileHeader::o_extraFieldLength] );
				commentLen    = GetUns16LE( &this->fields[CDFileHeader::o_commentLength] );

				if (filenameLen) {
					filename = new char[filenameLen];
					file->ReadAll ( filename, filenameLen );
				}
				if (extraFieldLen) {
					extraField = new char[extraFieldLen];
					file->ReadAll ( extraField, extraFieldLen );
				}
				if (commentLen) {
					comment = new char[commentLen];
					file->ReadAll ( comment, commentLen );
				}

				////// GET ACTUAL 64 BIT VALUES //////////////////////////////////////////////
				// get 32bit goodies first, correct later
				sizeUncompressed		= GetUns32LE( &fields[o_sizeUncompressed] );
				sizeCompressed			= GetUns32LE( &fields[o_sizeCompressed] );
				offsetLocalHeader		= GetUns32LE( &fields[o_offsetLocalHeader] );

				XMP_Int32 offset = 0;
				while ( offset < extraFieldLen )
				{
					XMP_Validate( (extraFieldLen - offset) >= 4, "need 4 bytes for next header ID+len", kXMPErr_BadFileFormat);
					XMP_Uns16 headerID = GetUns16LE( &extraField[offset] );
					XMP_Uns16 dataSize = GetUns16LE( &extraField[offset+2] );
					offset += 4;

					XMP_Validate( (extraFieldLen - offset) <= dataSize,
									"actual field lenght not given", kXMPErr_BadFileFormat);
					if ( headerID == 0x1 ) //we only care about "Zip64 extended information extra field"
					{
						XMP_Validate( offset < extraFieldLen, "extra field too short", kXMPErr_BadFileFormat);
						if (sizeUncompressed == 0xffffffff)
						{
							sizeUncompressed = GetUns64LE( &extraField[offset] );
							offset += 8;
						}
						if (sizeCompressed == 0xffffffff)
						{
							sizeCompressed = GetUns64LE( &extraField[offset] );
							offset += 8;
						}
						if (offsetLocalHeader == 0xffffffff)
						{
							offsetLocalHeader = GetUns64LE( &extraField[offset] );
							offset += 8;
						}
					}
					else
					{
						offset += dataSize;
					} // if
				} // while
			} // read()

			// writes structure to file (starting at current position)
			void write(XMP_IO* file)
			{
				//// WRITE BACK REAL 64 BIT VALUES, CREATE EXTRA FIELD ///////////////
				//may only wipe extra field after obtaining all Info from it
				if (extraField)	delete extraField;
				extraFieldLen=0;

				if ( ( sizeUncompressed  > 0xffffffff ) ||
					 ( sizeCompressed    > 0xffffffff ) ||
					 ( offsetLocalHeader > 0xffffffff )  )
				{
					extraField = new char[64]; // actual maxlen is 32
					extraFieldLen = 4; //first fields are for ID, size
					if ( sizeUncompressed > 0xffffffff )
					{
						PutUns64LE( sizeUncompressed, &extraField[extraFieldLen] );
						extraFieldLen += 8;
						sizeUncompressed = 0xffffffff;
					}
					if ( sizeCompressed > 0xffffffff )
					{
						PutUns64LE( sizeCompressed, &extraField[extraFieldLen] );
						extraFieldLen += 8;
						sizeCompressed = 0xffffffff;
					}
					if ( offsetLocalHeader > 0xffffffff )
					{
						PutUns64LE( offsetLocalHeader, &extraField[extraFieldLen] );
						extraFieldLen += 8;
						offsetLocalHeader = 0xffffffff;
					}

					//write ID, dataSize
					PutUns16LE( 0x0001, &extraField[0] );
					PutUns16LE( extraFieldLen-4, &extraField[2] );
					//extraFieldSize
					PutUns16LE( extraFieldLen, &this->fields[CDFileHeader::o_extraFieldLength] );
				}

				// write out 32-bit ('ff-stubs' or not)
				PutUns32LE( (XMP_Uns32)sizeUncompressed, &fields[o_sizeUncompressed] );
				PutUns32LE( (XMP_Uns32)sizeCompressed, &fields[o_sizeCompressed] );
				PutUns32LE( (XMP_Uns32)offsetLocalHeader, &fields[o_offsetLocalHeader] );

				/// WRITE /////////////////////////////////////////////////////////////////
				XMP_Enforce( SIG == GetUns32LE( &this->fields[CDFileHeader::o_sig] ) );

				file ->Write ( fields , FIXED_SIZE  );
				if (filenameLen)	file->Write ( filename   , filenameLen    );
				if (extraFieldLen)	file->Write ( extraField , extraFieldLen  );
				if (commentLen)		file->Write ( comment , commentLen  );
			}

			void setXMPFilename()
			{
				if (filename) delete filename;
				filenameLen = xmpFilenameLen;
				filename = new char[xmpFilenameLen];
				PutUns16LE(filenameLen,	&fields[CDFileHeader::o_fileNameLength] );
				memcpy(filename,"META-INF/metadata.xml",xmpFilenameLen);
			}

			XMP_Int64 size()
			{
				XMP_Int64 r = this->FIXED_SIZE + this->filenameLen + this->commentLen;
				// predict serialization size
				if (  (sizeUncompressed > 0xffffffff)||(sizeCompressed > 0xffffffff)||(offsetLocalHeader>0xffffffff) )
				{
					r += 4;		//extra fields necessary
					if (sizeUncompressed > 0xffffffff) r += 8;
					if (sizeCompressed > 0xffffffff) r += 8;
					if (offsetLocalHeader > 0xffffffff) r += 8;
				}
				return r;
			}

			~CDFileHeader()
			{
				this->release();
			};
	}; // class CDFileHeader

	class EndOfCD {
	private:
		const static XMP_Uns32 SIG = 0x06054b50;
		void UCFECD_Free()
		{
			if(commentLen) delete comment;
			commentLen = 0;
		}
	public:
		const static XMP_Int32 o_Sig = 0;
		const static XMP_Int32 o_CdNumEntriesDisk = 8;  // same-same for UCF, since single-volume
		const static XMP_Int32 o_CdNumEntriesTotal = 10;// must update both
		const static XMP_Int32 o_CdSize = 12;
		const static XMP_Int32 o_CdOffset = 16;
		const static XMP_Int32 o_CommentLen = 20;

		const static int FIXED_SIZE = 22;
		char fields[FIXED_SIZE];

		char* comment;
		XMP_Uns16 commentLen;

		EndOfCD() : comment(0), commentLen(0)
		{
			//nothing
		};

		void read (XMP_IO* file)
		{
			UCFECD_Free();

			file->ReadAll ( fields, FIXED_SIZE );
			XMP_Validate( this->SIG == GetUns32LE( &this->fields[o_Sig] ), "invalid header", kXMPErr_BadFileFormat );

			commentLen = GetUns16LE( &this->fields[o_CommentLen] );
			if(commentLen)
			{
				comment = new char[commentLen];
				file->ReadAll ( comment, commentLen );
			}
		};

		void write(XMP_IO* file)
		{
			XMP_Enforce( this->SIG == GetUns32LE( &this->fields[o_Sig] ) );
			commentLen   = GetUns16LE( &this->fields[o_CommentLen] );
			file ->Write ( fields , FIXED_SIZE  );
			if (commentLen)
				file->Write ( comment, commentLen );
		}

		~EndOfCD()
		{
			if (comment)	delete comment;
		};
	}; //class EndOfCD

	////////////////////////////////////////////////////////////////////////////////////
	// EMBEDDING MATH
	//
	// a = content files before xmp (always 0 thus ommited)
	// b/b2 = content files behind xmp (before/after injection)
	// x/x2 = offset xmp content header + content file (before/after injection)
	// cd/cd = central directory
	// h/h2 = end of central directory record
	XMP_Int64	b,b2,x,x2,cd,cd2,cdx,cdx2,z,z2,h,h2,
	// length thereof ('2' only where possibly different)
	// using XMP_Int64 here also for length (not XMP_Int32),
	// to be prepared for zip64, our LFA functions might need things in multiple chunks...
				al,bl,xl,x2l,cdl,cd2l,cdxl,cdx2l,z2l,hl,fl,f2l;
	XMP_Uns16	numCF,numCF2;

	bool wasCompressed;	// ..before, false if no prior xmp
	bool compressXMP;   // compress this time?
	bool inPlacePossible;
	/* bool isZip64; <=> z2 != 0 */

	FileHeader		xmpFileHeader;
	CDFileHeader	xmpCDHeader;

	XMP_StringPtr uncomprPacketStr;
	XMP_StringLen uncomprPacketLen;
	XMP_StringPtr finalPacketStr;
	XMP_StringLen finalPacketLen;
	std::string compressedPacket;
	std::vector<CDFileHeader> cdEntries;
	EndOfCD endOfCD;
	void writeOut( XMP_IO* sourceFile, XMP_IO* targetFile, bool isRewrite, bool isInPlace);

};	// UCF_MetaHandler

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

#endif /* __UCF_Handler_hpp__ */