summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FormatSupport/WAVE/BEXTMetadata.cpp
blob: f45f2449f29e4c2f57b09e16c7baf02dbc650e66 (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
// =================================================================================================
// Copyright Adobe
// Copyright 2010 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. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#include <string.h>

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.
#include "public/include/XMP_Const.h"

#include "XMPFiles/source/FormatSupport/WAVE/BEXTMetadata.h"
#include "source/Endian.h"

using namespace IFF_RIFF;

static const XMP_Uns32 kBEXTSizeMin					= 602;							// at minimum 602 bytes

static const XMP_Uns32 kSizeDescription				= 256;
static const XMP_Uns32 kSizeOriginator				= 32;
static const XMP_Uns32 kSizeOriginatorReference		= 32;
static const XMP_Uns32 kSizeOriginationDate			= 10;
static const XMP_Uns32 kSizeOriginationTime			= 8;

// Needed to be able to memcpy directly to this struct.
#if SUNOS_SPARC || SUNOS_X86
#pragma pack ( 1 )
#else
#pragma pack ( push, 1 )
#endif //#if SUNOS_SPARC || SUNOS_X86
	struct BEXT
	{
		char			mDescription[256];
		char			mOriginator[32];
		char			mOriginatorReference[32];
		char			mOriginationDate[10];
		char			mOriginationTime[8];
		XMP_Uns32		mTimeReferenceLow;
		XMP_Uns32		mTimeReferenceHigh;
		XMP_Uns16		mVersion;
		XMP_Uns8		mUMID[64];
		XMP_Uns8		mReserved[190];
	};
#if SUNOS_SPARC || SUNOS_X86
#pragma pack ( )
#else
#pragma pack ( pop )
#endif //#if SUNOS_SPARC || SUNOS_X86

//-----------------------------------------------------------------------------
// 
// [static] convertLF(...)
// 
// Purpose: Convert Mac/Unix line feeds to CR/LF
// 
//-----------------------------------------------------------------------------

void BEXTMetadata::NormalizeLF( std::string& str )
{
	XMP_Uns32 i = 0;
	while( i < str.length() )
	{
		char ch = str[i];

		if( ch == 0x0d )
		{
			//
			// possible Mac lf
			//
			if( i+1 < str.length() )
			{
				if( str[i+1] != 0x0a )
				{
					//
					// insert missing LF character
					//
					str.insert( i+1, 1, 0x0a );
				}

				i += 2;
			}
			else
			{
				str.push_back( 0x0a );
			}
		}
		else if( ch == 0x0a )
		{
			//
			// possible Unix LF
			//
			if( i == 0 || str[i-1] != 0x0d )
			{
				//
				// insert missing CR character
				//
				str.insert( i, 1, 0x0d );
				i += 2;
			}
			else
			{
				i++;
			}
		}
		else
		{
			i++;
		}
	}
}

//-----------------------------------------------------------------------------
// 
// BEXTMetadata::BEXTMetadata(...)
// 
// Purpose: ctor/dtor
// 
//-----------------------------------------------------------------------------

BEXTMetadata::BEXTMetadata()
{
}

BEXTMetadata::~BEXTMetadata()
{
}

//-----------------------------------------------------------------------------
// 
// BEXTMetadata::parse(...)
// 
// Purpose: Parses the given memory block and creates a data model representation
//			The implementation expects that the memory block is the data area of
//			the BEXT chunk and its size is at least as big as the minimum size
//			of a BEXT data block.
//			Throws exceptions if parsing is not possible
// 
//-----------------------------------------------------------------------------

void BEXTMetadata::parse( const XMP_Uns8* chunkData, XMP_Uns64 size )
{
	if( size >= kBEXTSizeMin )
	{
		const LittleEndian& LE = LittleEndian::getInstance();

		BEXT bext;
		memset( &bext, 0, kBEXTSizeMin );

		//
		// copy input data into BEXT block (except CodingHistory field)
		// Safe as fixed size matches size of struct that is #pragma packed(1)
		//
		memcpy( &bext, chunkData, kBEXTSizeMin );

		//
		// copy CodingHistory
		//
		if( size > kBEXTSizeMin )
		{
			this->setValue<std::string>( kCodingHistory, std::string( reinterpret_cast<const char*>(&chunkData[kBEXTSizeMin]), static_cast<std::string::size_type>(size - kBEXTSizeMin) ) );
		}

		//
		// copy values to map
		//
		this->setValue<std::string>( kDescription,			std::string( bext.mDescription, kSizeDescription ) );
		this->setValue<std::string>( kOriginator,			std::string( bext.mOriginator, kSizeOriginator ) );
		this->setValue<std::string>( kOriginatorReference,	std::string( bext.mOriginatorReference, kSizeOriginatorReference ) );
		this->setValue<std::string>( kOriginationDate,		std::string( bext.mOriginationDate, kSizeOriginationDate ) );
		this->setValue<std::string>( kOriginationTime,		std::string( bext.mOriginationTime, kSizeOriginationTime ) );

		this->setValue<XMP_Uns64>( kTimeReference,			LE.getUns64( &bext.mTimeReferenceLow ) );
		this->setValue<XMP_Uns16>( kVersion,				LE.getUns16( &bext.mVersion ) );

		this->setArray<XMP_Uns8>( kUMID,					bext.mUMID, 64 );

		this->resetChanges();
	}
	else
	{
		XMP_Throw ( "Not a valid BEXT chunk", kXMPErr_BadFileFormat );
	}
}

//-----------------------------------------------------------------------------
// 
// BEXTMetadata::serialize(...)
// 
// Purpose: Serializes the data model to a memory block. 
//			The memory block will be the data area of a BEXT chunk.
//			Throws exceptions if serializing is not possible
// 
//-----------------------------------------------------------------------------

XMP_Uns64 BEXTMetadata::serialize( XMP_Uns8** outBuffer )
{
	XMP_Uns64 size = 0;

	if( outBuffer != NULL )
	{
		const LittleEndian& LE = LittleEndian::getInstance();

		size = kBEXTSizeMin;

		std::string codingHistory;

		if( this->valueExists( kCodingHistory ) )
		{
			codingHistory = this->getValue<std::string>( kCodingHistory );
			NormalizeLF( codingHistory );

			size += codingHistory.length();
		}

		//
		// setup buffer
		//
		XMP_Uns8* buffer = new XMP_Uns8[static_cast<size_t>(size)];

		//
		// copy values and strings back to BEXT block
		//
		// ! Safe use of strncpy as the fixed size is consistent with the size of the destination buffer
		// But it is intentional here that the string might not be null terminated if
		// the size of the source is equal to the fixed size of the destination
		//
		BEXT bext;
		memset( &bext, 0, kBEXTSizeMin );

		if( this->valueExists( kDescription ) )
		{
			strncpy( bext.mDescription, this->getValue<std::string>( kDescription ).c_str(), kSizeDescription );
		}
		if( this->valueExists( kOriginator ) )
		{
			strncpy( bext.mOriginator, this->getValue<std::string>( kOriginator ).c_str(), kSizeOriginator );
		}
		if( this->valueExists( kOriginatorReference ) )
		{
			strncpy( bext.mOriginatorReference, this->getValue<std::string>( kOriginatorReference ).c_str(), kSizeOriginatorReference );
		}
		if( this->valueExists( kOriginationDate ) )
		{
			strncpy( bext.mOriginationDate, this->getValue<std::string>( kOriginationDate ).c_str(), kSizeOriginationDate );
		}
		if( this->valueExists( kOriginationTime ) )
		{
			strncpy( bext.mOriginationTime, this->getValue<std::string>( kOriginationTime ).c_str(), kSizeOriginationTime );
		}

		if( this->valueExists( kTimeReference ) )
		{
			LE.putUns64( this->getValue<XMP_Uns64>( kTimeReference ), &bext.mTimeReferenceLow );
		}

		if( this->valueExists( kVersion ) )
		{
			LE.putUns16( this->getValue<XMP_Uns16>( kVersion ), &bext.mVersion );
		}
		else // Special case: If no value is given, a value of "1" is the default!
		{
			LE.putUns16( 1, &bext.mVersion );
		}

		if( this->valueExists( kUMID ) )
		{
			XMP_Uns32 muidSize = 0;
			const XMP_Uns8* const muid = this->getArray<XMP_Uns8>( kUMID, muidSize );
			
			// Make sure to copy 64 bytes max.
			muidSize = muidSize > 64 ? 64 : muidSize;
			memcpy( bext.mUMID, muid, muidSize );
		}
		//
		// set input buffer to zero
		//
		memset( buffer, 0, static_cast<size_t>(size) );

		//
		// copy BEXT block into buffer (except CodingHistory field)
		//
		memcpy( buffer, &bext, kBEXTSizeMin );

		//
		// copy CodingHistory field into buffer
		//
		if( ! codingHistory.empty() )
		{
			memcpy( buffer + kBEXTSizeMin, codingHistory.c_str(), static_cast<size_t>(size - kBEXTSizeMin) );
		}

		*outBuffer = buffer;
	}
	else
	{
		XMP_Throw ( "Invalid buffer", kXMPErr_InternalFailure );
	}

	return size;
}

//-----------------------------------------------------------------------------
// 
// BEXTMetadata::isEmptyValue(...)
// 
// Purpose: Is the value of the passed ValueObject and its id "empty"?
// 
//-----------------------------------------------------------------------------

bool BEXTMetadata::isEmptyValue( XMP_Uns32 id, ValueObject& valueObj )
{
	bool ret = true;

	switch( id )
	{
		case kDescription:
		case kOriginator:
		case kOriginatorReference:
		case kOriginationDate:
		case kOriginationTime:
		case kCodingHistory:
		{
			TValueObject<std::string>* strObj = dynamic_cast<TValueObject<std::string>*>(&valueObj);

			ret = ( strObj == NULL || ( strObj != NULL && strObj->getValue().empty() ) );
		}
		break;

		case kTimeReference:
		case kVersion:
			ret = false;
			break;
		case kUMID:
		{
			TArrayObject<XMP_Uns8>* obj = dynamic_cast<TArrayObject<XMP_Uns8>*>(&valueObj);

			if( obj != NULL )
			{
				XMP_Uns32 size	 = 0;
				const XMP_Uns8* const buffer = obj->getArray( size );

				ret = ( size == 0 );
			}
		}
		break;

		default:
			ret = true;
	}

	return ret;
}