2 Copyright (c) 2005-2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 // used with TLVReader::Read*
43 // these are used below to manufacture arguments
44 #define OBJ_READ_ARGS(s,l) Dict::Type(MDD_##s##_##l), &l
45 #define OBJ_WRITE_ARGS(s,l) Dict::Type(MDD_##s##_##l), &l
46 #define OBJ_TYPE_ARGS(t) Dict::Type(MDD_##t).ul
53 typedef std::pair<ui32_t, ui32_t> ItemInfo;
54 typedef std::map<TagValue, ItemInfo> TagMap;
57 class TLVReader : public ASDCP::MemIOReader
61 IPrimerLookup* m_Lookup;
64 ASDCP_NO_COPY_CONSTRUCT(TLVReader);
65 bool FindTL(const MDDEntry&);
68 TLVReader(const byte_t* p, ui32_t c, IPrimerLookup* = 0);
69 Result_t ReadObject(const MDDEntry&, IArchive*);
70 Result_t ReadUi8(const MDDEntry&, ui8_t*);
71 Result_t ReadUi16(const MDDEntry&, ui16_t*);
72 Result_t ReadUi32(const MDDEntry&, ui32_t*);
73 Result_t ReadUi64(const MDDEntry&, ui64_t*);
77 class TLVWriter : public ASDCP::MemIOWriter
81 IPrimerLookup* m_Lookup;
84 ASDCP_NO_COPY_CONSTRUCT(TLVWriter);
85 Result_t WriteTag(const MDDEntry&);
88 TLVWriter(byte_t* p, ui32_t c, IPrimerLookup* = 0);
89 Result_t WriteObject(const MDDEntry&, IArchive*);
90 Result_t WriteUi8(const MDDEntry&, ui8_t*);
91 Result_t WriteUi16(const MDDEntry&, ui16_t*);
92 Result_t WriteUi32(const MDDEntry&, ui32_t*);
93 Result_t WriteUi64(const MDDEntry&, ui64_t*);
98 class Batch : public std::vector<T>, public IArchive
104 Batch() : ItemCount(0), ItemSize(0) { ItemSize = sizeof(T); }
108 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
109 Result_t result = Reader.ReadUi32BE(&ItemCount);
111 if ( ASDCP_SUCCESS(result) )
112 result = Reader.ReadUi32BE(&ItemSize);
114 if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
117 for ( ui32_t i = 0; i < ItemCount && ASDCP_SUCCESS(result); i++ )
120 result = Tmp.Unarchive(Reader);
122 if ( ASDCP_SUCCESS(result) )
130 Result_t Archive(ASDCP::MemIOWriter& Writer) {
131 Result_t result = Writer.WriteUi32BE(size());
133 if ( ASDCP_SUCCESS(result) )
134 result = Writer.WriteUi32BE(ItemSize);
136 typename std::vector<T>::iterator l_i = begin();
137 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
138 result = (*l_i).Archive(Writer);
144 void Dump(FILE* stream = 0, ui32_t depth = 0)
146 char identbuf[IdentBufferLen];
151 typename std::vector<T>::iterator i = this->begin();
152 for ( ; i != this->end(); i++ )
153 fprintf(stream, " %s\n", (*i).ToString(identbuf));
159 class Array : public std::list<T>, public IArchive
166 Result_t Unarchive(ASDCP::MemIOReader& Reader)
168 while ( Reader.Remainder() > 0 )
171 Tmp.Unarchive(Reader);
179 Result_t Archive(ASDCP::MemIOWriter& Writer) {
180 Result_t result = RESULT_OK;
181 typename std::list<T>::iterator l_i = begin();
183 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
184 result = (*l_i).Archive(Writer);
190 void Dump(FILE* stream = 0, ui32_t depth = 0)
192 char identbuf[IdentBufferLen];
197 typename std::list<T>::iterator i = this->begin();
198 for ( ; i != this->end(); i++ )
199 fprintf(stream, " %s\n", (*i).ToString(identbuf));
204 class Timestamp : public IArchive
216 Timestamp(const Timestamp& rhs);
217 Timestamp(const char* datestr);
218 virtual ~Timestamp();
220 const Timestamp& operator=(const Timestamp& rhs);
221 bool operator<(const Timestamp& rhs) const;
222 bool operator==(const Timestamp& rhs) const;
223 bool operator!=(const Timestamp& rhs) const;
225 // decode and set value from string formatted by EncodeAsString
226 Result_t SetFromString(const char* datestr);
228 // add the given number of days or hours to the timestamp value. Values less than zero
229 // will cause the value to decrease
231 void AddHours(i32_t);
233 // Write the timestamp value to the given buffer in the form 2004-05-01 13:20:00.000
234 // returns 0 if the buffer is smaller than DateTimeLen
235 const char* ToString(char* str_buf) const;
238 inline Result_t Unarchive(ASDCP::MemIOReader& Reader) {
239 Result_t result = Reader.ReadUi16BE(&Year);
241 if ( ASDCP_SUCCESS(result) )
242 result = Reader.ReadRaw(&Month, 6);
248 inline Result_t Archive(ASDCP::MemIOWriter& Writer) {
249 Result_t result = Writer.WriteUi16BE(Year);
251 if ( ASDCP_SUCCESS(result) )
252 result = Writer.WriteRaw(&Month, 6);
259 class UTF16String : public IArchive
262 char m_buffer[IdentBufferLen];
265 UTF16String() : m_length(0) { *m_buffer = 0; }
268 const UTF16String& operator=(const char*);
271 const char* ToString(char* str_buf) const {
272 strncpy(str_buf, m_buffer, m_length+1);
276 Result_t Unarchive(ASDCP::MemIOReader& Reader);
277 Result_t Archive(ASDCP::MemIOWriter& Writer);
281 class Rational : public ASDCP::Rational, public IArchive
287 Rational(const Rational& rhs) {
288 Numerator = rhs.Numerator;
289 Denominator = rhs.Denominator;
292 const Rational& operator=(const Rational& rhs) {
293 Numerator = rhs.Numerator;
294 Denominator = rhs.Denominator;
298 Rational(const ASDCP::Rational& rhs) {
299 Numerator = rhs.Numerator;
300 Denominator = rhs.Denominator;
303 const Rational& operator=(const ASDCP::Rational& rhs) {
304 Numerator = rhs.Numerator;
305 Denominator = rhs.Denominator;
310 const char* ToString(char* str_buf) const {
311 snprintf(str_buf, IdentBufferLen, "%lu/%lu", Numerator, Denominator);
315 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
316 Result_t result = Reader.ReadUi32BE((ui32_t*)&Numerator);
318 if ( ASDCP_SUCCESS(result) )
319 result = Reader.ReadUi32BE((ui32_t*)&Denominator);
324 Result_t Archive(ASDCP::MemIOWriter& Writer) {
325 Result_t result = Writer.WriteUi32BE((ui32_t)Numerator);
327 if ( ASDCP_SUCCESS(result) )
328 result = Writer.WriteUi32BE((ui32_t)Denominator);
335 class VersionType : public IArchive
337 ASDCP_NO_COPY_CONSTRUCT(VersionType);
340 enum Release_t { RL_UNKNOWN, RM_RELEASE, RL_DEVELOPMENT, RL_PATCHED, RL_BETA, RL_PRIVATE };
347 VersionType() : Major(0), Minor(0), Patch(0), Build(0), Release(RL_UNKNOWN) {}
349 void Dump(FILE* = 0);
351 const char* ToString(char* str_buf) const {
352 snprintf(str_buf, IdentBufferLen, "%hu.%hu.%hu.%hu.%hu", Major, Minor, Patch, Build, Release);
356 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
357 Result_t result = Reader.ReadUi16BE(&Major);
358 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Minor);
359 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Patch);
360 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Build);
361 if ( ASDCP_SUCCESS(result) )
364 result = Reader.ReadUi16BE(&tmp_release);
365 Release = (Release_t)tmp_release;
371 Result_t Archive(ASDCP::MemIOWriter& Writer) {
372 Result_t result = Writer.WriteUi32BE(Major);
373 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi32BE(Minor);
374 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi32BE(Patch);
375 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi32BE(Build);
376 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi32BE((ui16_t)(Release & 0x0000ffffL));
382 class RGBLayout : public IArchive
389 RGBLayout() { memset(PictureElement, 0, sizeof(PictureElement)); }
392 Result_t Unarchive(ASDCP::MemIOReader& Reader) { return RESULT_OK; }
393 Result_t Archive(ASDCP::MemIOWriter& Writer) { return RESULT_OK; }
394 inline const char* ToString(char* str_buf) const {
395 snprintf(str_buf, IdentBufferLen, "RGBLayout: <PictureElement[8]>\n");
401 class Raw : public IArchive
403 ASDCP_NO_COPY_CONSTRUCT(Raw);
411 Result_t Unarchive(ASDCP::MemIOReader& Reader) { return RESULT_OK; }
412 Result_t Archive(ASDCP::MemIOWriter& Writer) { return RESULT_OK; }
413 inline const char* ToString(char* str_buf) const {
414 snprintf(str_buf, IdentBufferLen, "RAW\n");
423 #endif //_MXFTYPES_H_