summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FormatSupport/WAVE/INFOMetadata.cpp
blob: 13c8178cf942dd5cbe52c6e62e6363dc8c4d1c36 (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
// =================================================================================================
// 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/INFOMetadata.h"
#include "source/Endian.h"

using namespace IFF_RIFF;

typedef std::map<XMP_Uns32, std::string>::const_iterator MapIterator;

static const XMP_Uns32 kSizeChunkID		= 4;
static const XMP_Uns32 kSizeChunkSize	= 4;
static const XMP_Uns32 kSizeChunkType	= 4;
static const XMP_Uns32 kChunkHeaderSize	= kSizeChunkID + kSizeChunkSize;

static const XMP_Uns32 kType_INFO		= 0x494E464F;

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

INFOMetadata::INFOMetadata()
{
}

INFOMetadata::~INFOMetadata()
{
}

//-----------------------------------------------------------------------------
// 
// INFOMetadata::parse(...)
// 
// Purpose: @see IMetadata::parse
// 
//-----------------------------------------------------------------------------

void INFOMetadata::parse( const XMP_Uns8* input, XMP_Uns64 size )
{
	if( input != NULL && size >= kSizeChunkType )
	{
		const BigEndian& BE		= BigEndian::getInstance();
		const LittleEndian& LE	= LittleEndian::getInstance();

		XMP_Uns32 type = BE.getUns32( &input[0] );

		XMP_Validate( type == kType_INFO, "Invalid LIST:INFO data", kXMPErr_InternalFailure );

		XMP_Uns64 offset = kSizeChunkType;	// pointer into the buffer

		while( offset < size )
		{
			//
			// continue parsing only if the remaing buffer is greater than
			// the chunk header size
			//
			if( size - offset >= kChunkHeaderSize )
			{
				//
				// read: chunk id
				//		 chunk size
				//		 chunk data
				XMP_Uns32 id		= BE.getUns32( &input[offset] );
				XMP_Uns32 datasize	= LE.getUns32( &input[offset+kSizeChunkID] );

				if( offset + kChunkHeaderSize + datasize <= size )
				{
					if( datasize > 0 )
					{
						//
						// don't store empty values
						//
						std::string value( reinterpret_cast<const char*>( &input[offset+kChunkHeaderSize] ), datasize );

						//
						// set new value
						//
						this->setValue<std::string>( id, value );
					}

					//
					// update pointer
					//
					offset = offset + datasize + kChunkHeaderSize;

					if( datasize & 1 )
					{
						// pad byte
						offset++;
					}
				}
				else
				{
					//
					// invalid chunk, clean up and throw exception
					//
					this->deleteAll();
					XMP_Throw ( "Not a valid LIST:INFO chunk", kXMPErr_BadFileFormat );
				}
			}
			else
			{
				//
				// invalid chunk, clean up and throw exception
				//
				this->deleteAll();
				XMP_Throw ( "Not a valid LIST:INFO chunk", kXMPErr_BadFileFormat );
			}
		}

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

//-----------------------------------------------------------------------------
// 
// INFOMetadata::serialize(...)
// 
// Purpose: @see IMetadata::serialize
// 
//-----------------------------------------------------------------------------

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

	if( outBuffer != NULL )
	{
		//
		// calculate required buffer size
		//
		for( ValueMap::iterator iter=mValues.begin(); iter!=mValues.end(); iter++ )
		{
			TValueObject<std::string>* strObj = dynamic_cast<TValueObject<std::string>*>(iter->second);

			XMP_Uns32 chunkSize = kChunkHeaderSize + (XMP_Uns32)strObj->getValue().length() + 1;	// 1 byte is added for NULL termination string

			if( chunkSize & 1 )
			{
				// take account of pad byte
				chunkSize++;
			}

			size += chunkSize;
		}
		
		size += kSizeChunkType; // add size of type "INFO"

		if( size > 0 )
		{
			XMP_Uns8* buffer = new XMP_Uns8[static_cast<size_t>(size)];		// output buffer

			memset( buffer, 0, static_cast<size_t>(size) );

			const BigEndian& BE		= BigEndian::getInstance();
			const LittleEndian& LE	= LittleEndian::getInstance();

			// Put type "INFO" in front of the buffer
			XMP_Uns32 typeInfo = BE.getUns32(&kType_INFO);
			memcpy( buffer, &typeInfo, kSizeChunkType );
			XMP_Uns64 offset = kSizeChunkType;						// pointer into the buffer

			//
			// for each stored value
			//
			for( ValueMap::iterator iter=mValues.begin(); iter!=mValues.end(); iter++ )
			{
				//
				// get: chunk data
				//		chunk id
				//		chunk size
				//
				XMP_Validate( iter->second != NULL, "ERROR inserting serialize. iter->second is NULL.", kXMPErr_InternalFailure );
				XMP_Assert(dynamic_cast<TValueObject<std::string>*>(iter->second) == static_cast<TValueObject<std::string>*>(iter->second));
				TValueObject<std::string>* strObj = dynamic_cast<TValueObject<std::string>*>(iter->second);
				std::string value	= strObj->getValue();
				XMP_Uns32 id		= iter->first;
				XMP_Uns32 lSize		= (XMP_Uns32)value.length() + 1;		// Null terminated string

				if( lSize & 1 && strObj->hasChanged() )
				{
					//
					// if we modified the value of this entry
					// then fill chunk data with zero bytes
					// rather than use a pad byte, i.e.
					// size of each LIST:INFO entry has
					// an odd size
					//
					lSize++;
				}

				//
				// chunk id and chunk size are stored in little endian format
				//
				id		= BE.getUns32( &id );
				lSize	= LE.getUns32( &lSize );

				//
				// copy values into output buffer
				//
				memcpy( buffer+offset, &id, kSizeChunkID );
				memcpy( buffer+offset+kSizeChunkID, &lSize, kSizeChunkSize );
				//size has been changed in little endian format. Change it back to bigendina
				lSize	= LE.getUns32( &lSize );
				memcpy( buffer+offset+kChunkHeaderSize, value.c_str(), value.length() );

				//
				// update pointer
				//
				offset += kChunkHeaderSize;
				offset += lSize;

				if( lSize & 1 )
				{
					//
					// take account of pad byte
					offset++;
				}
			}

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

	return size;
}

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

bool INFOMetadata::isEmptyValue( XMP_Uns32 id, ValueObject& valueObj )
{
	TValueObject<std::string>* strObj = dynamic_cast<TValueObject<std::string>*>(&valueObj);

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