File: SWF_Handler.cpp

package info (click to toggle)
exempi 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,312 kB
  • ctags: 5,732
  • sloc: cpp: 49,942; sh: 11,002; makefile: 275; ansic: 93
file content (386 lines) | stat: -rw-r--r-- 10,609 bytes parent folder | download | duplicates (2)
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
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2006 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 "SWF_Handler.hpp"

#include "SWF_Support.hpp"


using namespace std;

// =================================================================================================
/// \file SWF_Handler.hpp
/// \brief File format handler for SWF.
///
/// This handler ...
///
// =================================================================================================

// =================================================================================================
// SWF_MetaHandlerCTor
// ====================

XMPFileHandler * SWF_MetaHandlerCTor ( XMPFiles * parent )
{
	return new SWF_MetaHandler ( parent );

}	// SWF_MetaHandlerCTor

// =================================================================================================
// SWF_CheckFormat
// ===============

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

	IOBuffer ioBuf;
	
	LFA_Seek ( fileRef, 0, SEEK_SET );
	if ( ! CheckFileSpace ( fileRef, &ioBuf, SWF_SIGNATURE_LEN ) ) return false;

	if ( !(CheckBytes ( ioBuf.ptr, SWF_F_SIGNATURE_DATA, SWF_SIGNATURE_LEN ) || 
		  CheckBytes(ioBuf.ptr, SWF_C_SIGNATURE_DATA, SWF_SIGNATURE_LEN)) ) 
			return false;

	return true;

}	// SWF_CheckFormat

// =================================================================================================
// SWF_MetaHandler::SWF_MetaHandler
// ==================================

SWF_MetaHandler::SWF_MetaHandler ( XMPFiles * _parent )
{
	this->parent = _parent;
	this->handlerFlags = kSWF_HandlerFlags;
	this->stdCharForm  = kXMP_Char8Bit;

}

// =================================================================================================
// SWF_MetaHandler::~SWF_MetaHandler
// ===================================

SWF_MetaHandler::~SWF_MetaHandler()
{
}

// =================================================================================================
// SWF_MetaHandler::CacheFileData
// ===============================

void SWF_MetaHandler::CacheFileData()
{
	
	this->containsXMP = false;

	LFA_FileRef fileRef ( this->parent->fileRef );
	if ( fileRef == 0) return;
	
	SWF_Support::FileInfo fileInfo(fileRef, this->parent->filePath);
	IO::InputStream * is = NULL;
	if(fileInfo.IsCompressed())
	{
		XMP_Uns32 fileSize = fileInfo.GetSize();
		is = new IO::ZIP::DeflateInputStream(fileRef, fileSize);
		
		IO::ZIP::DeflateInputStream * in = dynamic_cast<IO::ZIP::DeflateInputStream*>(is);
		in->Skip(SWF_COMPRESSION_BEGIN, IO::ZIP::DEFLATE_NO);
	}
	else
	{
		is = new IO::FileInputStream(fileRef);
		is->Skip(SWF_COMPRESSION_BEGIN);
	}

	SWF_Support::TagState tagState;
	//important flag to stop iteration over all tags when xmp flag within FileAttributeTag isn't set
	tagState.cachingFile = true;

	long numTags = SWF_Support::OpenSWF ( is, tagState );
	
	is->Close();
	delete is;

	if ( numTags == 0 ) return;

	if (tagState.hasXMP && tagState.xmpLen != 0)
	{
		this->xmpPacket.assign(tagState.xmpPacket);
		this->containsXMP = true;
	}
	else
	{
		// no XMP
	}
	

}	// SWF_MetaHandler::CacheFileData

// =================================================================================================
// SWF_MetaHandler::ProcessXMP
// ============================
//
// Process the raw XMP and legacy metadata that was previously cached.

void SWF_MetaHandler::ProcessXMP()
{

	this->processedXMP = true;	// Make sure we only come through here once.

	// Process the XMP packet.

	if ( ! this->xmpPacket.empty() ) {
	
		XMP_Assert ( this->containsXMP );
		XMP_StringPtr packetStr = this->xmpPacket.c_str();
		XMP_StringLen packetLen = (XMP_StringLen)this->xmpPacket.size();

		this->xmpObj.ParseFromBuffer ( packetStr, packetLen );

		this->containsXMP = true;

	}

}	// SWF_MetaHandler::ProcessXMP


// =================================================================================================
// XMPFileHandler::GetSerializeOptions
// ===================================
//
// Override default implementation to ensure omitting xmp wrapper
// 
XMP_OptionBits SWF_MetaHandler::GetSerializeOptions()
{
	return (kXMP_OmitPacketWrapper | kXMP_OmitAllFormatting | kXMP_OmitXMPMetaElement);
} // XMPFileHandler::GetSerializeOptions


// =================================================================================================
// SWF_MetaHandler::UpdateFile
// ============================

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

	LFA_FileRef sourceRef = this->parent->fileRef;
	std::string sourcePath = this->parent->filePath;

	SWF_Support::FileInfo fileInfo(sourceRef, sourcePath);
	
	if(fileInfo.IsCompressed())
		sourceRef = fileInfo.Decompress();

	IO::InputStream * is = new IO::FileInputStream(sourceRef);
	//processing SWF starts after byte SWF_COMPRESSION_BEGIN - currently 8 bytes
	is->Skip(SWF_COMPRESSION_BEGIN);
	
	SWF_Support::TagState tagState;
	
	long numTags = SWF_Support::OpenSWF ( is, tagState );
	
	//clean objects
	is->Close();
	delete is;

	bool foundTag = false;
	SWF_Support::TailBufferDef tailBuffer;
	

	//find end position to measure tail buffer
	tailBuffer.tailEndPosition = LFA_Seek(sourceRef, 0, SEEK_END);

	SWF_Support::TagIterator curPos = tagState.tags.begin();
	SWF_Support::TagIterator endPos = tagState.tags.end();

	for( ;(curPos != endPos) && ! foundTag; ++curPos)
	{
		SWF_Support::TagData tag = *curPos;
		
		//write XMP Tag at the beginning of the file
		if(! tagState.hasXMP && ! tagState.hasFileAttrTag)
		{
			tailBuffer.tailStartPosition = tag.pos;
			tailBuffer.writePosition = tag.pos;
			foundTag = true;
		}
		//update existing XMP Tag
		if(tagState.hasXMP && (tagState.xmpTag.pos == tag.pos))
		{
			++curPos;
			SWF_Support::TagData nextTag = *curPos;
			tailBuffer.tailStartPosition = nextTag.pos;
			tailBuffer.writePosition = tagState.xmpTag.pos;
			foundTag = true;
		}
		//write XMP Tag after FileAttribute Tag
		else if(! tagState.hasXMP && (tag.id == SWF_TAG_ID_FILEATTRIBUTES))
		{
			++curPos;
			SWF_Support::TagData nextTag = *curPos;
			tailBuffer.tailStartPosition = nextTag.pos;
			tailBuffer.writePosition = nextTag.pos;
			foundTag = true;
		}
	}

	//cache tail of file
	XMP_Assert(tailBuffer.tailEndPosition > tailBuffer.tailStartPosition);
	XMP_Uns32 tailSize = tailBuffer.GetTailSize();
	XMP_Uns8 * buffer = new XMP_Uns8[tailSize];
	SWF_Support::ReadBuffer(sourceRef, tailBuffer.tailStartPosition, tailSize, buffer);

	//write new XMP packet
	XMP_StringPtr packetStr = xmpPacket.c_str();
	XMP_StringLen packetLen = (XMP_StringLen)xmpPacket.size();

	LFA_Seek(sourceRef, tailBuffer.writePosition, SEEK_SET);
	SWF_Support::WriteXMPTag(sourceRef, packetLen, packetStr);

	// truncate to minimal size
	LFA_Truncate(sourceRef, LFA_Tell(sourceRef));
	
	//move tail of the file
	LFA_Write(sourceRef, buffer, tailSize);
	
	//cleaning buffer
	delete [] buffer;

	//update FileAttribute Tag if exists
	if(tagState.hasFileAttrTag)
		SWF_Support::UpdateFileAttrTag(sourceRef, tagState.fileAttrTag, tagState);

	
	//update File Size
	SWF_Support::UpdateHeader(sourceRef);

	//compress file after writing XMP
	if(fileInfo.IsCompressed())
	{
		fileInfo.Compress(sourceRef, this->parent->fileRef);
		fileInfo.Clean();
	}
	
	
	if ( ! updated )return;	// If there's an error writing the chunk, bail.

	this->needsUpdate = false;

}	// SWF_MetaHandler::UpdateFile

// =================================================================================================
// SWF_MetaHandler::WriteFile
// ===========================

void SWF_MetaHandler::WriteFile ( LFA_FileRef sourceRef, const std::string & sourcePath )
{

	LFA_FileRef destRef = this->parent->fileRef;

	SWF_Support::TagState tagState;

	LFA_FileRef origRef(destRef);
	std::string updatePath;
	

	SWF_Support::FileInfo fileInfo(sourceRef, sourcePath);
	
	if(fileInfo.IsCompressed())
	{
		sourceRef = fileInfo.Decompress();
		CreateTempFile ( sourcePath, &updatePath, kCopyMacRsrc );
		destRef = LFA_Open ( updatePath.c_str(), 'w' );
	}

	IO::InputStream * is = NULL;
	
	is = new IO::FileInputStream(sourceRef);
	is->Skip(SWF_COMPRESSION_BEGIN);
	
	long numTags = SWF_Support::OpenSWF( is, tagState );

	is->Close();
	delete is;

	if ( numTags == 0 ) return;

	LFA_Truncate(destRef, 0);
	SWF_Support::CopyHeader(sourceRef, destRef, tagState);

	SWF_Support::TagIterator curPos = tagState.tags.begin();
	SWF_Support::TagIterator endPos = tagState.tags.end();

	XMP_StringPtr packetStr = xmpPacket.c_str();
	XMP_StringLen packetLen = (XMP_StringLen)xmpPacket.size();

	bool copying = true;
	bool isXMPTagWritten = false;

	for (; (curPos != endPos); ++curPos)
	{
		SWF_Support::TagData tag = *curPos;

		// write XMP tag right after FileAttribute Tag if no XMP tag exists
		if (! tagState.hasXMP &&  (tag.id == SWF_TAG_ID_FILEATTRIBUTES))
			SWF_Support::WriteXMPTag(destRef, packetLen, packetStr );

		//no FileAttribute Tag and no XMP tag write XMP Tag at the beginning of the file
		if(!tagState.hasXMP && !tagState.hasFileAttrTag && ! isXMPTagWritten)
		{
			isXMPTagWritten = true;
			SWF_Support::WriteXMPTag(destRef, packetLen, packetStr );
		}
		
		// write XMP tag where old XMP exists
		if(tagState.hasXMP && (tag.pos == tagState.xmpTag.pos))
		{
			copying = false;
			SWF_Support::WriteXMPTag(destRef, packetLen, packetStr );
		}

		// copy any other chunk
		if(copying)
			SWF_Support::CopyTag(sourceRef, destRef, tag);

		copying = true;
	}

	// update FileAttribute Tag in new file
	if(tagState.hasFileAttrTag)
		SWF_Support::UpdateFileAttrTag(destRef, tagState.fileAttrTag, tagState);

	
	// update file header by measuring new file size
	SWF_Support::UpdateHeader(origRef);

	//compress re-written file
	if(fileInfo.IsCompressed())
	{
		fileInfo.Compress(destRef, origRef);
		fileInfo.Clean();
		LFA_Close(destRef);
		LFA_Delete(updatePath.c_str());
	}

	


}	// SWF_MetaHandler::WriteFile