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
105 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
106 ui32_t ItemCount, ItemSize;
107 Result_t result = Reader.ReadUi32BE(&ItemCount);
109 if ( ASDCP_SUCCESS(result) )
110 result = Reader.ReadUi32BE(&ItemSize);
112 if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
115 for ( ui32_t i = 0; i < ItemCount && ASDCP_SUCCESS(result); i++ )
118 result = Tmp.Unarchive(Reader);
120 if ( ASDCP_SUCCESS(result) )
127 inline bool HasValue() const { return ! empty(); }
130 Result_t Archive(ASDCP::MemIOWriter& Writer) const {
131 Result_t result = Writer.WriteUi32BE(size());
132 byte_t* p = Writer.CurrentData();
134 if ( ASDCP_SUCCESS(result) )
135 result = Writer.WriteUi32BE(0);
137 if ( ASDCP_FAILURE(result) || empty() )
140 typename std::vector<T>::const_iterator l_i = begin();
141 assert(l_i != end());
143 ui32_t ItemSize = Writer.Remainder();
144 result = (*l_i).Archive(Writer);
145 ItemSize -= Writer.Remainder();
146 i2p<ui32_t>(ASDCP_i32_BE(ItemSize), p);
149 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
150 result = (*l_i).Archive(Writer);
156 void Dump(FILE* stream = 0, ui32_t depth = 0)
158 char identbuf[IdentBufferLen];
163 typename std::vector<T>::iterator i = this->begin();
164 for ( ; i != this->end(); i++ )
165 fprintf(stream, " %s\n", (*i).ToString(identbuf));
171 class Array : public std::list<T>, public IArchive
178 Result_t Unarchive(ASDCP::MemIOReader& Reader)
180 while ( Reader.Remainder() > 0 )
183 Tmp.Unarchive(Reader);
190 inline bool HasValue() const { return ! empty(); }
193 Result_t Archive(ASDCP::MemIOWriter& Writer) const {
194 Result_t result = RESULT_OK;
195 typename std::list<T>::const_iterator l_i = begin();
197 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
198 result = (*l_i).Archive(Writer);
204 void Dump(FILE* stream = 0, ui32_t depth = 0)
206 char identbuf[IdentBufferLen];
211 typename std::list<T>::iterator i = this->begin();
212 for ( ; i != this->end(); i++ )
213 fprintf(stream, " %s\n", (*i).ToString(identbuf));
218 class Timestamp : public IArchive
230 Timestamp(const Timestamp& rhs);
231 Timestamp(const char* datestr);
232 virtual ~Timestamp();
234 const Timestamp& operator=(const Timestamp& rhs);
235 bool operator<(const Timestamp& rhs) const;
236 bool operator==(const Timestamp& rhs) const;
237 bool operator!=(const Timestamp& rhs) const;
239 // decode and set value from string formatted by EncodeAsString
240 Result_t SetFromString(const char* datestr);
242 // add the given number of days or hours to the timestamp value. Values less than zero
243 // will cause the value to decrease
245 void AddHours(i32_t);
247 // Write the timestamp value to the given buffer in the form 2004-05-01 13:20:00.000
248 // returns 0 if the buffer is smaller than DateTimeLen
249 const char* ToString(char* str_buf) const;
252 inline Result_t Unarchive(ASDCP::MemIOReader& Reader) {
253 Result_t result = Reader.ReadUi16BE(&Year);
255 if ( ASDCP_SUCCESS(result) )
256 result = Reader.ReadRaw(&Month, 6);
261 inline bool HasValue() const { return true; }
264 inline Result_t Archive(ASDCP::MemIOWriter& Writer) const {
265 Result_t result = Writer.WriteUi16BE(Year);
267 if ( ASDCP_SUCCESS(result) )
268 result = Writer.WriteRaw(&Month, 6);
275 class UTF16String : public IArchive
278 char m_buffer[IdentBufferLen];
279 ASDCP_NO_COPY_CONSTRUCT(UTF16String);
282 UTF16String() : m_length(0) { *m_buffer = 0; }
285 const UTF16String& operator=(const char*);
288 const char* ToString(char* str_buf) const {
289 strncpy(str_buf, m_buffer, m_length+1);
293 Result_t Unarchive(ASDCP::MemIOReader& Reader);
294 inline bool HasValue() const { return m_length > 0; }
295 Result_t Archive(ASDCP::MemIOWriter& Writer) const;
299 class Rational : public ASDCP::Rational, public IArchive
305 Rational(const Rational& rhs) {
306 Numerator = rhs.Numerator;
307 Denominator = rhs.Denominator;
310 const Rational& operator=(const Rational& rhs) {
311 Numerator = rhs.Numerator;
312 Denominator = rhs.Denominator;
316 Rational(const ASDCP::Rational& rhs) {
317 Numerator = rhs.Numerator;
318 Denominator = rhs.Denominator;
321 const Rational& operator=(const ASDCP::Rational& rhs) {
322 Numerator = rhs.Numerator;
323 Denominator = rhs.Denominator;
328 const char* ToString(char* str_buf) const {
329 snprintf(str_buf, IdentBufferLen, "%lu/%lu", Numerator, Denominator);
333 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
334 Result_t result = Reader.ReadUi32BE((ui32_t*)&Numerator);
336 if ( ASDCP_SUCCESS(result) )
337 result = Reader.ReadUi32BE((ui32_t*)&Denominator);
342 inline bool HasValue() const { return true; }
344 Result_t Archive(ASDCP::MemIOWriter& Writer) const {
345 Result_t result = Writer.WriteUi32BE((ui32_t)Numerator);
347 if ( ASDCP_SUCCESS(result) )
348 result = Writer.WriteUi32BE((ui32_t)Denominator);
355 class VersionType : public IArchive
357 ASDCP_NO_COPY_CONSTRUCT(VersionType);
360 enum Release_t { RL_UNKNOWN, RM_RELEASE, RL_DEVELOPMENT, RL_PATCHED, RL_BETA, RL_PRIVATE };
367 VersionType() : Major(0), Minor(0), Patch(0), Build(0), Release(RL_UNKNOWN) {}
369 void Dump(FILE* = 0);
371 const char* ToString(char* str_buf) const {
372 snprintf(str_buf, IdentBufferLen, "%hu.%hu.%hu.%hur%hu", Major, Minor, Patch, Build, Release);
376 Result_t Unarchive(ASDCP::MemIOReader& Reader) {
377 Result_t result = Reader.ReadUi16BE(&Major);
378 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Minor);
379 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Patch);
380 if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi16BE(&Build);
381 if ( ASDCP_SUCCESS(result) )
384 result = Reader.ReadUi16BE(&tmp_release);
385 Release = (Release_t)tmp_release;
391 inline bool HasValue() const { return true; }
393 Result_t Archive(ASDCP::MemIOWriter& Writer) const {
394 Result_t result = Writer.WriteUi16BE(Major);
395 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi16BE(Minor);
396 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi16BE(Patch);
397 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi16BE(Build);
398 if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi16BE((ui16_t)(Release & 0x0000ffffL));
404 class Raw : public ASDCP::FrameBuffer, public IArchive
406 ASDCP_NO_COPY_CONSTRUCT(Raw);
413 Result_t Unarchive(ASDCP::MemIOReader& Reader);
414 inline bool HasValue() const { return Size() > 0; }
415 Result_t Archive(ASDCP::MemIOWriter& Writer) const;
416 const char* ToString(char* str_buf) const;
423 #endif //_MXFTYPES_H_