1 #ifndef __ASF_Support_hpp__
2 #define __ASF_Support_hpp__ 1
3 
4 // =================================================================================================
5 // ADOBE SYSTEMS INCORPORATED
6 // Copyright 2006 Adobe Systems Incorporated
7 // All Rights Reserved
8 //
9 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
10 // of the Adobe license agreement accompanying it.
11 // =================================================================================================
12 
13 #include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.
14 #include "public/include/XMP_Const.h"
15 
16 #include "XMPFiles/source/XMPFiles_Impl.hpp"
17 #include "XMPFiles/source/FormatSupport/Reconcile_Impl.hpp"
18 #include "source/XIO.hpp"
19 #include "source/XMP_ProgressTracker.hpp"
20 
21 // currently exclude LicenseURL from reconciliation
22 #define Exclude_LicenseURL_Recon 1
23 #define EXCLUDE_LICENSEURL_RECON 1
24 
25 // Defines for platforms other than Win
26 #if ! XMP_WinBuild
27 
28 	typedef struct _GUID
29 	{
30 		XMP_Uns32	Data1;
31 		XMP_Uns16	Data2;
32 		XMP_Uns16	Data3;
33 		XMP_Uns8	Data4[8];
34 	} GUID;
35 
36 	int IsEqualGUID ( const GUID& guid1, const GUID& guid2 );
37 
38 	static const GUID GUID_NULL = { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } };
39 
40 #endif
41 
42 // header object
43 static const GUID ASF_Header_Object = { MakeUns32LE(0x75b22630), MakeUns16LE(0x668e), MakeUns16LE(0x11cf), { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c } };
44 // contains ...
45 static const GUID ASF_File_Properties_Object = { MakeUns32LE(0x8cabdca1), MakeUns16LE(0xa947), MakeUns16LE(0x11cf), { 0x8e, 0xe4, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 } };
46 static const GUID ASF_Content_Description_Object = { MakeUns32LE(0x75b22633), MakeUns16LE(0x668e), MakeUns16LE(0x11cf), { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c } };
47 static const GUID ASF_Content_Branding_Object = { MakeUns32LE(0x2211b3fa), MakeUns16LE(0xbd23), MakeUns16LE(0x11d2), { 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e } };
48 static const GUID ASF_Content_Encryption_Object = { MakeUns32LE(0x2211b3fb), MakeUns16LE(0xbd23), MakeUns16LE(0x11d2), { 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e } };
49 // padding
50 // Remark: regarding to Microsofts spec only the ASF_Header_Object contains a ASF_Padding_Object
51 // Real world files show, that the ASF_Header_Extension_Object contains a ASF_Padding_Object
52 static const GUID ASF_Header_Extension_Object = { MakeUns32LE(0x5fbf03b5), MakeUns16LE(0xa92e), MakeUns16LE(0x11cf), { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 } };
53 static const GUID ASF_Padding_Object = { MakeUns32LE(0x1806d474), MakeUns16LE(0xcadf), MakeUns16LE(0x4509), { 0xa4, 0xba, 0x9a, 0xab, 0xcb, 0x96, 0xaa, 0xe8 } };
54 
55 // data object
56 static const GUID ASF_Data_Object = { MakeUns32LE(0x75b22636), MakeUns16LE(0x668e), MakeUns16LE(0x11cf), { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c } };
57 
58 // XMP object
59 static const GUID ASF_XMP_Metadata = { MakeUns32LE(0xbe7acfcb), MakeUns16LE(0x97a9), MakeUns16LE(0x42e8), { 0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac } };
60 
61 static const int guidLen = sizeof(GUID);
62 
63 typedef struct _ASF_ObjectBase
64 {
65 	GUID		guid;
66 	XMP_Uns64	size;
67 
68 } ASF_ObjectBase;
69 
70 static const XMP_Uns32 kASF_ObjectBaseLen = (XMP_Uns32) sizeof(ASF_ObjectBase);
71 
72 // =================================================================================================
73 
74 class ASF_LegacyManager {
75 public:
76 
77 	enum objectType {
78 		objectFileProperties		= 1 << 0,
79 		objectContentDescription	= 1 << 1,
80 		objectContentBranding		= 1 << 2,
81 		objectContentEncryption		= 1 << 3
82 	};
83 
84 	enum minObjectSize {
85 		sizeContentDescription	= 34,
86 		sizeContentBranding		= 40,
87 		sizeContentEncryption	= 40
88 	};
89 
90 	enum fieldType {
91 		// File_Properties_Object
92 		fieldCreationDate = 0,
93 		// Content_Description_Object
94 		fieldTitle,
95 		fieldAuthor,
96 		fieldCopyright,
97 		fieldDescription,
98 		// Content_Branding_Object
99 		fieldCopyrightURL,
100 		#if ! Exclude_LicenseURL_Recon
101 			// Content_Encryption_Object
102 			fieldLicenseURL,
103 		#endif
104 		// last
105 		fieldLast
106 	};
107 
108 	ASF_LegacyManager();
109 	virtual ~ASF_LegacyManager();
110 
111 	bool SetField ( fieldType field, const std::string& value );
112 	std::string GetField ( fieldType field );
113 	unsigned int GetFieldMaxSize ( fieldType field );
114 
115 	void SetObjectExists ( objectType object );
116 
117 	void SetBroadcast ( const bool broadcast );
118 	bool GetBroadcast();
119 
120 	void ComputeDigest();
121 	bool CheckDigest ( const SXMPMeta& xmp );
122 	void SetDigest ( SXMPMeta* xmp );
123 
124 	void ImportLegacy ( SXMPMeta* xmp );
125 	int ExportLegacy ( const SXMPMeta& xmp );
126 	bool hasLegacyChanged();
127 	XMP_Int64 getLegacyDiff();
128 	int changedObjects();
129 
130 	void SetPadding ( XMP_Int64 padding );
131 	XMP_Int64 GetPadding();
132 
133 private:
134 
135 	typedef std::vector<std::string> TFields;
136 	TFields fields;
137 	bool broadcastSet;
138 
139 	std::string digestStr;
140 	bool digestComputed;
141 
142 	bool imported;
143 	int objectsExisting;
144 	int objectsToExport;
145 	XMP_Int64 legacyDiff;
146 	XMP_Int64 padding;
147 
148 	static std::string NormalizeStringDisplayASCII ( std::string& operand );
149 	static std::string NormalizeStringTrailingNull ( std::string& operand );
150 
151 	static void ConvertMSDateToISODate ( std::string& source, std::string* dest );
152 	static void ConvertISODateToMSDate ( std::string& source, std::string* dest );
153 
154 	static int DaysInMonth ( XMP_Int32 year, XMP_Int32 month );
155 	static bool IsLeapYear ( long year );
156 
157 }; // class ASF_LegacyManager
158 
159 // =================================================================================================
160 
161 class ASF_Support {
162 public:
163 
164 	class ObjectData {
165 		public:
ObjectData()166 			ObjectData() : pos(0), len(0), guid(GUID_NULL), xmp(false) {}
~ObjectData()167 			virtual ~ObjectData() {}
168 			XMP_Uns64	pos;		// file offset of object
169 			XMP_Uns64	len;		// length of object data
170 			GUID		guid;		// object GUID
171 			bool		xmp;		// object with XMP ?
172 	};
173 
174 	typedef std::vector<ObjectData> ObjectVector;
175 	typedef ObjectVector::iterator ObjectIterator;
176 
177 	class ObjectState {
178 
179 		public:
ObjectState()180 			ObjectState() : xmpPos(0), xmpLen(0), xmpIsLastObject(false), broadcast(false) {}
~ObjectState()181 			virtual ~ObjectState() {}
182 			XMP_Uns64		xmpPos;
183 			XMP_Uns64		xmpLen;
184 			bool			xmpIsLastObject;
185 			bool			broadcast;
186 			ObjectData		xmpObject;
187 			ObjectVector	objects;
188 	};
189 
190 	ASF_Support();
191 	ASF_Support ( ASF_LegacyManager* legacyManager, XMP_ProgressTracker* _progressTracker);
192 	virtual ~ASF_Support();
193 
194 	long OpenASF ( XMP_IO* fileRef, ObjectState & inOutObjectState );
195 
196 	bool ReadObject ( XMP_IO* fileRef, ObjectState & inOutObjectState, XMP_Uns64 * objectLength, XMP_Uns64 & inOutPosition );
197 
198 	bool ReadHeaderObject ( XMP_IO* fileRef, ObjectState& inOutObjectState, const ObjectData& newObject );
199 	bool WriteHeaderObject ( XMP_IO* sourceRef, XMP_IO* destRef, const ObjectData& object, ASF_LegacyManager& legacyManager, bool usePadding );
200 	bool UpdateHeaderObject ( XMP_IO* fileRef, const ObjectData& object, ASF_LegacyManager& legacyManager );
201 
202 	bool UpdateFileSize ( XMP_IO* fileRef );
203 
204 	bool ReadHeaderExtensionObject ( XMP_IO* fileRef, ObjectState& inOutObjectState, const XMP_Uns64& pos, const ASF_ObjectBase& objectBase );
205 	static bool WriteHeaderExtensionObject ( const std::string& buffer, std::string* header, const ASF_ObjectBase& objectBase, const int reservePadding );
206 
207 	static bool CreatePaddingObject ( std::string* header, const XMP_Uns64 size );
208 
209 	static bool WriteXMPObject ( XMP_IO* fileRef, XMP_Uns32 len, const char* inBuffer );
210 	static bool UpdateXMPObject ( XMP_IO* fileRef, const ObjectData& object, XMP_Uns32 len, const char * inBuffer );
211 	static bool CopyObject ( XMP_IO* sourceRef, XMP_IO* destRef, const ObjectData& object );
212 
213 	static bool ReadBuffer ( XMP_IO* fileRef, XMP_Uns64 & pos, XMP_Uns64 len, char * outBuffer );
214 	static bool WriteBuffer ( XMP_IO* fileRef, XMP_Uns64 & pos, XMP_Uns32 len, const char * inBuffer );
215 
216 private:
217 
218 	ASF_LegacyManager* legacyManager;
219 	XMP_ProgressTracker* progressTracker;//not owned by ASF_Support
220 	XMP_Uns64 posFileSizeInfo;
221 
222 	static std::string ReplaceString ( std::string& operand, std::string& str, int offset, int count );
223 
224 }; // class ASF_Support
225 
226 // =================================================================================================
227 
228 #endif	// __ASF_Support_hpp__
229