summaryrefslogtreecommitdiff
path: root/source/XMPFiles/FileHandlers/WAV_Handler.cpp
blob: 6820d01725423f5255acba6e74eb8e41e130a418 (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
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// 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.
// =================================================================================================

#include "XMP_Environment.h"	// ! This must be the first include.
#if ! XMP_UNIXBuild				//	Closes at very bottom. Disabled on UNIX until legacy-as-local is fixed.

#if XMP_WinBuild
	#pragma warning ( disable : 4996 )	// '...' was declared deprecated
#endif

#include "WAV_Handler.hpp"
#include "RIFF_Support.hpp"
#include "Reconcile_Impl.hpp"
#include "XMP_Const.h"

using namespace std;

#define kXMPUserDataType MakeFourCC ( '_', 'P', 'M', 'X' )	/* Yes, backwards! */

#define	formtypeWAVE	MakeFourCC('W', 'A', 'V', 'E')


// -------------------------------------------------------------------------------------------------
// Premiere Pro specific info for reconciliation

// ! MakeFourCC warning: The MakeFourCC macro creates a 32 bit int with the letters "reversed". This
// ! is a leftover of the original Win-only code. It happens to work OK on little endian machines,
// ! when stored in memory the letters are in the expected order. To be safe, always use the XMP
// ! endian control macros when storing to memory or loading from memory.

// FourCC codes for the RIFF chunks
#define	wavWaveTitleChunk		MakeFourCC('D','I','S','P')
#define	wavInfoCreateDateChunk	MakeFourCC('I','C','R','D')
#define	wavInfoArtistChunk		MakeFourCC('I','A','R','T')
#define	wavInfoAlbumChunk		MakeFourCC('I','N','A','M')
#define	wavInfoGenreChunk		MakeFourCC('I','G','N','R')
#define	wavInfoCommentChunk		MakeFourCC('I','C','M','T')
#define wavInfoEngineerChunk	MakeFourCC('I','E','N','G')
#define wavInfoCopyrightChunk	MakeFourCC('I','C','O','P')
#define wavInfoSoftwareChunk	MakeFourCC('I','S','F','T')

#define	wavInfoTag		MakeFourCC('I','N','F','O')
#define	wavWaveTag		MakeFourCC('W','A','V','E')

// DC
#define kTitle		"title"
#define kCopyright	"rights"

// XMP
#define kCreateDate	"CreateDate"

// DM
#define kArtist		"artist"
#define kAlbum		"album"
#define kGenre		"genre"
#define kLogComment "logComment"
#define kEngineer	"engineer"
#define kSoftware	"CreatorTool"

// -------------------------------------------------------------------------------------------------
// Legacy digest info
// ------------------
//
// The original WAV handler code didn't keep a legacy digest, it imported the legacy on every open.
// Because local encoding is used for the legacy, this can cause loss in the XMP. (The use of local
// encoding itself is an issue, the AVI handler is using UTF-8.)
//
// The legacy digest for WAV is a list of chunk IDs and a 128-bit MD5 digest, formatted like:
//   DISP,IART,ICMT,ICOP,ICRD,IENG,IGNR,INAM,ISFT;012345678ABCDEF012345678ABCDEF
//
// The list of IDs are the recognized legacy chunks, in alphabetical order. This the full list that
// could be recognized, not restricted to those actually present in the specific file. When opening
// a file the new software's list is used to create the comparison digest, not the list from the
// file. So that changes to the recognized list will trigger an import.
//
// The MD5 digest is computed from the full legacy chunk, including the ID and length portions, not
// including any pad byte for odd data length. The length must be in file (little endian) order,
// not native machine order. The legacy chunks are added to the digest in list (alphabetical by ID)
// order.
//
// Legacy can be imported in 3 circumstances:
//
// 1. If the file does not yet have XMP, all of the legacy is imported.
//
// 2. If the file has XMP and a digest, and the recomputed digest differs from the saved digest, the
//    legacy is imported. The digest comparison is the full string, including the list of IDs. A
//    check is made for each legacy item:
//    2a. If the legacy item is missing or has an empty value, the corresponding XMP is deleted.
//    2b. If the corresponding XMP is missing, the legacy value is imported.
//    2c. If the new legacy value differs from a local encoding of the XMP value, the legacy value
//        is imported.
//
// 3. If the file has XMP but no digest, legacy is imported for items that have no corresponding XMP.
//    Any existing XMP is left alone. This is protection for tools that might be XMP aware but do
//    not provide a digest or any legacy reconciliation.

#define TAG_MAX_SIZE 5024

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

static inline int GetStringRiffSize ( const std::string & str )
{
	int l = (int)strlen ( const_cast<char *> (str.data()) );
	if ( l & 1 ) ++l;
	return l;
}

// =================================================================================================
/// \file WAV_Handler.cpp
/// \brief File format handler for WAV.
///
/// This header ...
///
// =================================================================================================

// =================================================================================================
// WAV_MetaHandlerCTor
// ===================

XMPFileHandler * WAV_MetaHandlerCTor ( XMPFiles * parent )
{
	return new WAV_MetaHandler ( parent );

}	// WAV_MetaHandlerCTor

// =================================================================================================
// WAV_CheckFormat
// ===============
//
// A WAVE file must begin with "RIFF", a 4 byte little endian length, then "WAVE". The length should
// be fileSize-8, but we don't bother checking this here.

bool WAV_CheckFormat ( XMP_FileFormat format,
					   XMP_StringPtr  filePath,
			           LFA_FileRef    fileRef,
			           XMPFiles *     parent )
{
	IgnoreParam(format); IgnoreParam(parent);
	XMP_Assert ( format == kXMP_WAVFile );

	if ( fileRef == 0 ) return false;
	
	enum { kBufferSize = 12 };
	XMP_Uns8 buffer [kBufferSize];
	
	LFA_Seek ( fileRef, 0, SEEK_SET );
	LFA_Read ( fileRef, buffer, kBufferSize );
	
	// "RIFF" is 52 49 46 46, "WAVE" is 57 41 56 45
	if ( (! CheckBytes ( &buffer[0], "\x52\x49\x46\x46", 4 )) ||
		 (! CheckBytes ( &buffer[8], "\x57\x41\x56\x45", 4 )) ) return false;

	return true;
	
}	// WAV_CheckFormat

// =================================================================================================
// WAV_MetaHandler::WAV_MetaHandler
// ================================

WAV_MetaHandler::WAV_MetaHandler ( XMPFiles * _parent )
{
	this->parent = _parent;
	this->handlerFlags = kWAV_HandlerFlags;
	this->stdCharForm  = kXMP_Char8Bit;
	
}	// WAV_MetaHandler::WAV_MetaHandler

// =================================================================================================
// WAV_MetaHandler::~WAV_MetaHandler
// =================================

WAV_MetaHandler::~WAV_MetaHandler()
{
	// Nothing to do.
}	// WAV_MetaHandler::~WAV_MetaHandler



// =================================================================================================
// WAV_MetaHandler::UpdateFile
// ===========================

void WAV_MetaHandler::UpdateFile ( bool doSafeUpdate )
{
	if ( ! this->needsUpdate ) return;
	if ( doSafeUpdate ) XMP_Throw ( "WAV_MetaHandler::UpdateFile: Safe update not supported", kXMPErr_Unavailable );

	bool fReconciliate = ! (this->parent->openFlags & kXMPFiles_OpenOnlyXMP);
	bool ok;

	std::string strTitle, strArtist, strComment, strCopyright, strCreateDate,
				strEngineer, strGenre, strAlbum, strSoftware;

	if ( fReconciliate ) {
	
		// Get the legacy item values, create the new digest, and add the digest to the XMP. The
		// legacy chunks in alphabetical ID order are:
		//   DISP - title
		//   IART - artist
		//   ICMT - log comment
		//   ICOP - copyright
		//   ICRD - create date
		//   IENG - engineer
		//   IGNR - genre
		//   INAM - album
		//   ISFT - software

		MD5_CTX md5Ctx;
		std::string digestStr;
		XMP_Uns8 md5Val[16];
		static char* hexDigits = "0123456789ABCDEF";
		
		MD5Init ( &md5Ctx );

		// Prepare the legacy values and compute the new digest.
		
		PrepareLegacyExport ( kXMP_NS_DC, kTitle, wavWaveTitleChunk, &strTitle, &digestStr, &md5Ctx, true /* LangAlt */ );
		PrepareLegacyExport ( kXMP_NS_DM, kArtist, wavInfoArtistChunk, &strArtist, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_DM, kLogComment, wavInfoCommentChunk, &strComment, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_DC, kCopyright, wavInfoCopyrightChunk, &strCopyright, &digestStr, &md5Ctx, true /* LangAlt */ );
		PrepareLegacyExport ( kXMP_NS_XMP, kCreateDate, wavInfoCreateDateChunk, &strCreateDate, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_DM, kEngineer, wavInfoEngineerChunk, &strEngineer, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_DM, kGenre, wavInfoGenreChunk, &strGenre, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_DM, kAlbum, wavInfoAlbumChunk, &strAlbum, &digestStr, &md5Ctx );
		PrepareLegacyExport ( kXMP_NS_XMP, kSoftware, wavInfoSoftwareChunk, &strSoftware, &digestStr, &md5Ctx );

		// Finish the digest and add it to the XMP.
		
		MD5Final ( md5Val, &md5Ctx );
		
		digestStr[digestStr.size()-1] = ';';
		for ( size_t i = 0; i < 16; ++i ) {
			XMP_Uns8 byte = md5Val[i];
			digestStr += hexDigits [byte >> 4];
			digestStr += hexDigits [byte & 0xF];
		}

		XMP_StringLen oldLen = (XMP_StringLen)this->xmpPacket.size();
		this->xmpObj.SetProperty ( kXMP_NS_WAV, "NativeDigest", digestStr.c_str() );
		try {
			this->xmpObj.SerializeToBuffer ( &this->xmpPacket, (kXMP_UseCompactFormat | kXMP_ExactPacketLength),
							oldLen );
		} catch ( ... ) {
			this->xmpObj.SerializeToBuffer ( &this->xmpPacket, kXMP_UseCompactFormat );
		}
	
	}
	
	XMP_StringPtr packetStr = this->xmpPacket.c_str();
	XMP_StringLen packetLen = (XMP_StringLen)this->xmpPacket.size();
	if ( packetLen == 0 ) return;

	// Make sure we're writing an even number of bytes as required by the RIFF specification
	if ( (this->xmpPacket.size() & 1) == 1 )
		this->xmpPacket.push_back (' ');
	XMP_Assert ( (this->xmpPacket.size() & 1) == 0 );
	packetStr = this->xmpPacket.c_str();	// ! Make sure they are current.
	packetLen = (XMP_StringLen)this->xmpPacket.size();
	
	LFA_FileRef fileRef ( this->parent->fileRef );
	if ( fileRef == 0 ) return;

	RIFF_Support::RiffState riffState;
	long numTags = RIFF_Support::OpenRIFF ( fileRef, riffState );
	if ( numTags == 0 ) return;

	ok = RIFF_Support::PutChunk ( fileRef, riffState, formtypeWAVE, kXMPUserDataType, (char*)packetStr, packetLen );
	if ( ! ok ) return;

	ok = CreatorAtom::Update ( this->xmpObj, fileRef, formtypeWAVE, riffState );
	if ( ! ok ) return;

	// If needed, reconciliate the XMP data back into the native metadata.
	if ( fReconciliate ) {

		PutChunk ( fileRef, riffState, wavWaveTag, wavWaveTitleChunk, strTitle.c_str(), (XMP_Int32)strTitle.size() );

		// Pad the old tags
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoCreateDateChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoArtistChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoAlbumChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoGenreChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoCommentChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoEngineerChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoCopyrightChunk, 0 );
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, 0, wavInfoSoftwareChunk, 0 );

		// Get the old INFO list
		std::string strOldInfo;
		unsigned long lOldSize = 0;
		bool ok = RIFF_Support::GetRIFFChunk ( fileRef, riffState, FOURCC_LIST, wavWaveTag, wavInfoTag, 0, &lOldSize );
		if ( ok ) {
			// We have to get rid of the "INFO" first.
			strOldInfo.reserve ( lOldSize );
			strOldInfo.assign ( lOldSize, ' ' );
			RIFF_Support::GetRIFFChunk ( fileRef, riffState, FOURCC_LIST, wavWaveTag, wavInfoTag, (char*)strOldInfo.c_str(), &lOldSize );
			// lOldSize -= 4;
		}

		// TODO: Cleaning up the OldInfo from the padding

		// Pad the old INFO list
		RIFF_Support::MarkChunkAsPadding ( fileRef, riffState, wavWaveTag, FOURCC_LIST, wavInfoTag );
		
		// Calculating the new INFO list size
		XMP_Int32 dwListSize = 4 + 8 * 8 +
						   GetStringRiffSize ( strCreateDate )  +
						   GetStringRiffSize ( strArtist ) +
						   GetStringRiffSize ( strAlbum ) +
						   GetStringRiffSize ( strGenre ) +
						   GetStringRiffSize ( strComment ) +
						   GetStringRiffSize ( strEngineer ) +
						   GetStringRiffSize ( strCopyright ) +
						   GetStringRiffSize ( strSoftware ) +
						   lOldSize;  // list id (4 bytes) + 8 tags hdrs (8 each)

		ok = MakeChunk ( fileRef, riffState, formtypeWAVE, dwListSize + 8 );
		if ( ! ok ) return; // If there's an error making a chunk, bail

		// Building the INFO list header
		RIFF_Support::ltag listtag;
		listtag.id = MakeUns32LE ( FOURCC_LIST );
		listtag.len = MakeUns32LE ( dwListSize );
		listtag.subid = MakeUns32LE ( wavInfoTag );
		LFA_Write ( fileRef, &listtag, 12 );

		// Writing all the chunks
		RIFF_Support::WriteChunk ( fileRef, wavInfoCreateDateChunk, strCreateDate.c_str(), GetStringRiffSize ( strCreateDate ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoArtistChunk, strArtist.c_str(), GetStringRiffSize ( strArtist ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoAlbumChunk, strAlbum.c_str(), GetStringRiffSize ( strAlbum ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoGenreChunk, strGenre.c_str(), GetStringRiffSize ( strGenre ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoCommentChunk, strComment.c_str(), GetStringRiffSize ( strComment ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoEngineerChunk, strEngineer.c_str(), GetStringRiffSize ( strEngineer ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoCopyrightChunk, strCopyright.c_str(), GetStringRiffSize ( strCopyright ) );
		RIFF_Support::WriteChunk ( fileRef, wavInfoSoftwareChunk, strSoftware.c_str(), GetStringRiffSize ( strSoftware ) );

		LFA_Write ( fileRef, strOldInfo.c_str(), lOldSize );

	}

	this->needsUpdate = false;

}	// WAV_MetaHandler::UpdateFile

// =================================================================================================
// WAV_MetaHandler::WriteFile
// ==========================

void WAV_MetaHandler::WriteFile ( LFA_FileRef         sourceRef,
								  const std::string & sourcePath )
{
	IgnoreParam(sourceRef); IgnoreParam(sourcePath);
	
	XMP_Throw ( "WAV_MetaHandler::WriteFile: Not supported", kXMPErr_Unavailable );

}	// WAV_MetaHandler::WriteFile

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

static void AddDigestItem ( XMP_Uns32 legacyID, std::string & legacyStr, std::string * digestStr, MD5_CTX * md5 )
{
	
	XMP_Uns32 leID  = MakeUns32LE ( legacyID );
	XMP_Uns32 leLen = MakeUns32LE ( (XMP_Uns32)legacyStr.size());
	
	digestStr->append ( (char*)(&leID), 4 );
	digestStr->append ( "," );
	
	MD5Update ( md5, (XMP_Uns8*)&leID, 4 );
	MD5Update ( md5, (XMP_Uns8*)&leLen, 4 );
	MD5Update ( md5, (XMP_Uns8*)legacyStr.c_str(), (XMP_Int32)legacyStr.size() );

}	// AddDigestItem

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

static void AddCurrentDigestItem ( LFA_FileRef fileRef, RIFF_Support::RiffState riffState, 
								   XMP_Uns32 tagID, XMP_Uns32 parentID, std::string * digestStr, MD5_CTX * md5 )
{
	unsigned long legacySize;
	std::string legacyStr;

	bool found = RIFF_Support::GetRIFFChunk ( fileRef, riffState, tagID, parentID, 0, 0, &legacySize );
	if ( found ) {
		legacyStr.reserve ( legacySize );
		legacyStr.assign ( legacySize, ' ' );
		(void) RIFF_Support::GetRIFFChunk ( fileRef, riffState, tagID, parentID, 0, (char*)legacyStr.c_str(), &legacySize );
	}

	AddDigestItem ( tagID, legacyStr, digestStr, md5 );

}	// AddCurrentDigestItem

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

static void CreateCurrentDigest ( LFA_FileRef fileRef, RIFF_Support::RiffState riffState, std::string * digestStr )
{
	MD5_CTX md5Ctx;
	XMP_Uns8 md5Val[16];
	static char* hexDigits = "0123456789ABCDEF";
	
	MD5Init ( &md5Ctx );
	
	AddCurrentDigestItem ( fileRef, riffState, wavWaveTitleChunk, wavWaveTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoArtistChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoCommentChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoCopyrightChunk, wavInfoTag, digestStr, &md5Ctx);
	AddCurrentDigestItem ( fileRef, riffState, wavInfoCreateDateChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoEngineerChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoGenreChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoAlbumChunk, wavInfoTag, digestStr, &md5Ctx );
	AddCurrentDigestItem ( fileRef, riffState, wavInfoSoftwareChunk, wavInfoTag, digestStr, &md5Ctx );

	MD5Final ( md5Val, &md5Ctx );
	
	(*digestStr)[digestStr->size()-1] = ';';
	for ( size_t i = 0; i < 16; ++i ) {
		XMP_Uns8 byte = md5Val[i];
		(*digestStr) += hexDigits [byte >> 4];
		(*digestStr) += hexDigits [byte & 0xF];
	}

}	// CreateCurrentDigest


// =================================================================================================
// WAV_MetaHandler::CacheFileData
// ==============================

void WAV_MetaHandler::CacheFileData()
{

	this->containsXMP = false;
	bool fReconciliate = ! (this->parent->openFlags & kXMPFiles_OpenOnlyXMP);
	bool keepExistingXMP = false;	// By default an import will replace existing XMP.
	bool haveLegacyItem, haveXMPItem;

	LFA_FileRef fileRef ( this->parent->fileRef );	//*** simplify to assignment

	bool updateFile = XMP_OptionIsSet ( this->parent->openFlags, kXMPFiles_OpenForUpdate );
	if ( updateFile ) {
		
		// Workaround for bad files in the field that have a bad size in the outermost RIFF chunk.
		// Repair the cases where the length is too long (beyond EOF). Don't repair a length that is
		// less than EOF, we don't know if there actually are multiple top level chunks. There is
		// also a check and "runtime repair" inside ReadTag, needed for read-only file access.
	
		XMP_Int64 fileLen = LFA_Measure ( fileRef );
		XMP_Uns32 riffLen;
		
		LFA_Seek ( fileRef, 4, SEEK_SET );
		LFA_Read ( fileRef, &riffLen, 4 );
		riffLen = GetUns32LE ( &riffLen );
		
		if ( (fileLen >= 8) && ((XMP_Int64)riffLen > (fileLen - 8)) ) {	// Is the initial chunk too long?

			bool repairFile = XMP_OptionIsSet ( this->parent->openFlags, kXMPFiles_OpenRepairFile );
			if ( ! repairFile ) {
				XMP_Throw ( "Initial RIFF tag exceeds file length", kXMPErr_BadValue );
			} else {
				riffLen = MakeUns32LE ( (XMP_Uns32)fileLen - 8 );
				LFA_Seek ( fileRef, 4, SEEK_SET );
				LFA_Write ( fileRef, &riffLen, 4 );
			}

		}
	
	}

	// Contnue with normal processing.

	RIFF_Support::RiffState riffState;
	long numTags = RIFF_Support::OpenRIFF ( fileRef, riffState );
	if ( numTags == 0 ) return; //*** shouldn't we throw ? XMP_Throw("invalid file format") or such?

	// Determine the size of the metadata
	unsigned long bufferSize(0);
	haveLegacyItem = RIFF_Support::GetRIFFChunk ( fileRef, riffState, kXMPUserDataType /* _PMX, the xmp packet */, 0, 0, 0, &bufferSize );

	if ( ! haveLegacyItem ) {

		packetInfo.writeable = true;	// If no packet found, created packets will be writeable

	} else if ( bufferSize > 0 ) {

		// Size and clear the buffer
		this->xmpPacket.reserve(bufferSize);
		this->xmpPacket.assign(bufferSize, ' ');

		// Get the metadata
		XMP_Uns64 xmpPacketPosition;
		haveLegacyItem = RIFF_Support::GetRIFFChunk ( fileRef, riffState, kXMPUserDataType /* _PMX, the xmp packet */, 0, 0,
													  const_cast<char *>(this->xmpPacket.data()), &bufferSize, &xmpPacketPosition );
		if ( haveLegacyItem ) {
			this->packetInfo.offset = xmpPacketPosition;
			this->packetInfo.length = bufferSize;
			this->xmpObj.ParseFromBuffer ( this->xmpPacket.c_str(), (XMP_StringLen)this->xmpPacket.size() );
			this->containsXMP = true;
		}

	}

	// Figure out what to do overall. If there is no XMP, everything gets imported. If there is XMP
	// but no digest, only import if there is no corresponding XMP. If there is XMP and a digest
	// that matches, nothing is imported. If there is XMP and a digest that does not match, import
	// everything that provides "new info".
	
	if ( fReconciliate && this->containsXMP ) {
	
		std::string savedDigest;
		haveXMPItem = this->xmpObj.GetProperty ( kXMP_NS_WAV, "NativeDigest", &savedDigest, 0 );
		
		if ( ! haveXMPItem ) {
			keepExistingXMP = true;
		} else {
			std::string currDigest;
			CreateCurrentDigest ( fileRef, riffState, &currDigest );
			if ( currDigest == savedDigest ) fReconciliate = false;
		}
	
	}
	
	// Now import the individual legacy items.
	
	if ( fReconciliate ) {

		ImportLegacyItem ( riffState, wavWaveTitleChunk, wavWaveTag, kXMP_NS_DC, kTitle, keepExistingXMP, true /* LangAlt */ );
		ImportLegacyItem ( riffState, wavInfoCreateDateChunk, wavInfoTag, kXMP_NS_XMP, kCreateDate, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoArtistChunk, wavInfoTag, kXMP_NS_DM, kArtist, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoAlbumChunk, wavInfoTag, kXMP_NS_DM, kAlbum, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoGenreChunk, wavInfoTag, kXMP_NS_DM, kGenre, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoCommentChunk, wavInfoTag, kXMP_NS_DM, kLogComment, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoEngineerChunk, wavInfoTag, kXMP_NS_DM, kEngineer, keepExistingXMP );
		ImportLegacyItem ( riffState, wavInfoCopyrightChunk, wavInfoTag, kXMP_NS_DC, kCopyright, keepExistingXMP, true /* LangAlt */ );
		ImportLegacyItem ( riffState, wavInfoSoftwareChunk, wavInfoTag, kXMP_NS_XMP, kSoftware, keepExistingXMP );

	}

	CreatorAtom::Import ( this->xmpObj, fileRef, riffState );

	// Update the xmpPacket, as the xmpObj might have been updated with legacy info
	if ( this->packetInfo.length == kXMPFiles_UnknownLength )
	{
		// kXMPFiles_UnknownLength <=> no prior packet, thus do not attempt
		// to put in place (creates several seconds of delay
		this->xmpObj.SerializeToBuffer ( &this->xmpPacket, (kXMP_UseCompactFormat ) );
	}
	else
	{
		try {
			this->xmpObj.SerializeToBuffer ( &this->xmpPacket,
				(kXMP_UseCompactFormat | kXMP_ExactPacketLength) , this->packetInfo.length );
		} catch ( XMP_Error ) {
			this->xmpObj.SerializeToBuffer ( &this->xmpPacket, (kXMP_UseCompactFormat ) );
		}
	}

	this->processedXMP = this->containsXMP;
	
}	// WAV_MetaHandler::CacheFileData

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

void WAV_MetaHandler::UTF8ToMBCS ( std::string * inoutStr )
{
	std::string localStr;

	try {
		ReconcileUtils::UTF8ToLocal ( inoutStr->c_str(), inoutStr->size(), &localStr );
		inoutStr->swap ( localStr );
	} catch ( ... ) {
		inoutStr->erase();
	}

}

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

void WAV_MetaHandler::MBCSToUTF8 ( std::string * inoutStr )
{
	std::string utf8Str;

	try {
		ReconcileUtils::LocalToUTF8 ( inoutStr->c_str(), inoutStr->size(), &utf8Str );
		inoutStr->swap ( utf8Str );
	} catch ( ... ) {
		inoutStr->erase();
	}

}

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

void WAV_MetaHandler::PrepareLegacyExport ( XMP_StringPtr xmpNS, XMP_StringPtr xmpProp, XMP_Uns32 legacyID, std::string * legacyStr,
											std::string * digestStr, MD5_CTX * md5, bool langAlt /* = false */ )
{
	if ( ! langAlt ) {
		this->xmpObj.GetProperty ( xmpNS, xmpProp, legacyStr, 0 );
	} else {
		this->xmpObj.GetLocalizedText ( xmpNS, xmpProp, "", "x-default", 0, legacyStr, 0 );
	}
	UTF8ToMBCS ( legacyStr );	// Convert the XMP value to local encoding.
	
	// ! Add a 0 pad byte if the value has an odd length. This would be better done inside RIFF_Support,
	// ! but that means changing too much at this moment.
	
	if ( (legacyStr->size() & 1) == 1 ) {
		(*legacyStr) += " ";
		(*legacyStr)[legacyStr->size()-1] = 0;
	}
	
	if ( legacyID == wavWaveTitleChunk ) {	// ! The title gets 32 bit LE type code of 1 inserted.
		legacyStr->insert ( 0, "1234" );
		PutUns32LE ( 1, (void*)legacyStr->c_str() );
	}
	
	AddDigestItem ( legacyID, *legacyStr, digestStr, md5 );

}	// WAV_MetaHandler::PrepareLegacyExport

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

void WAV_MetaHandler::ImportLegacyItem ( RIFF_Support::RiffState & inOutRiffState,
										 XMP_Uns32 tagID,
										 XMP_Uns32 parentID,
										 XMP_StringPtr xmpNS,
										 XMP_StringPtr xmpProp,
										 bool keepExistingXMP,
										 bool langAlt /* = false */ )
{
	LFA_FileRef fileRef ( this->parent->fileRef );

	bool haveLegacyItem, haveXMPItem;
	std::string legacyStr, xmpStr;
	unsigned long legacySize;

	if ( ! langAlt ) {
		haveXMPItem = this->xmpObj.GetProperty ( xmpNS, xmpProp, &xmpStr, 0 );
	} else {
		haveXMPItem = this->xmpObj.GetLocalizedText ( xmpNS, xmpProp, "", "x-default", 0, &xmpStr, 0 );
	}

	haveLegacyItem = RIFF_Support::GetRIFFChunk ( fileRef, inOutRiffState, tagID, parentID, 0, 0, &legacySize );
	if ( (legacySize == 0) || ((tagID == wavWaveTitleChunk) && (legacySize <= 4)) ) haveLegacyItem = false;
	if ( haveXMPItem && keepExistingXMP ) haveLegacyItem = false;	// Simplify following checks.

	if ( ! haveLegacyItem ) {
	
		// No legacy item, delete the corresponding XMP if we're not keeping existing XMP.
	
		if ( haveXMPItem && (! keepExistingXMP) ) {
			if ( ! langAlt ) {
				this->xmpObj.DeleteProperty ( xmpNS, xmpProp );
			} else {
				std::string xdPath;
				SXMPUtils::ComposeLangSelector ( xmpNS, xmpProp, "x-default", &xdPath );
				this->xmpObj.DeleteProperty ( xmpNS, xdPath.c_str() );
				if ( this->xmpObj.CountArrayItems ( xmpNS, xmpProp ) == 0 ) this->xmpObj.DeleteProperty ( xmpNS, xmpProp );
			}
		}
	
	} else {
	
		// Have a legacy Item, update the XMP as appropriate.
	
		XMP_Assert ( (! haveXMPItem) || (! keepExistingXMP) );

		legacyStr.reserve ( legacySize );
		legacyStr.assign ( legacySize, ' ' );
		haveLegacyItem = RIFF_Support::GetRIFFChunk ( fileRef, inOutRiffState, tagID, parentID, 0, (char*)legacyStr.c_str(), &legacySize );
		XMP_Assert ( haveLegacyItem );
		
		if ( tagID == wavWaveTitleChunk ) {	// Check and strip the type code from the title.
			XMP_Assert ( legacySize > 4 );
			XMP_Uns32 typeCode = GetUns32LE ( legacyStr.data() );
			if( typeCode != 1 ) return;	// Bail if the type code isn't 1.
			legacyStr.erase ( 0, 4 );	// Reduce the value to just the text part.
		}
		
		if ( haveXMPItem ) {
			// Don't update the XMP if the legacy value matches the localized XMP value.
			UTF8ToMBCS ( &xmpStr );
			if ( xmpStr == legacyStr ) return;
		}
		
		MBCSToUTF8 ( &legacyStr );
		if ( ! langAlt ) {
			this->xmpObj.SetProperty ( xmpNS, xmpProp, legacyStr.c_str(), 0 );
		} else {
			this->xmpObj.SetLocalizedText ( xmpNS, xmpProp, "", "x-default", legacyStr.c_str(), 0 );
		}
		this->containsXMP = true;

	}

} // WAV_MetaHandler::LoadPropertyFromRIFF

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

#endif	// XMP_UNIXBuild