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 Kumu::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&, Kumu::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 Kumu::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&, Kumu::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 Kumu::IArchive
105 virtual bool Unarchive(Kumu::MemIOReader* Reader) {
106 ui32_t ItemCount, ItemSize;
107 if ( ! Reader->ReadUi32BE(&ItemCount) ) return false;
108 if ( ! Reader->ReadUi32BE(&ItemSize) ) return false;
110 if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
114 for ( ui32_t i = 0; i < ItemCount && result; i++ )
117 result = Tmp.Unarchive(Reader);
126 inline virtual bool HasValue() const { return ! this->empty(); }
128 virtual ui32_t ArchiveLength() const {
129 ui32_t arch_size = sizeof(ui32_t)*2;
131 typename std::vector<T>::const_iterator l_i = this->begin();
132 assert(l_i != this->end());
134 for ( ; l_i != this->end(); l_i++ )
135 arch_size += l_i->ArchiveLength();
141 virtual bool Archive(Kumu::MemIOWriter* Writer) const {
142 if ( ! Writer->WriteUi32BE(this->size()) ) return false;
143 byte_t* p = Writer->CurrentData();
145 if ( ! Writer->WriteUi32BE(0) ) return false;
146 if ( this->empty() ) return true;
148 typename std::vector<T>::const_iterator l_i = this->begin();
149 assert(l_i != this->end());
151 ui32_t ItemSize = Writer->Remainder();
152 if ( ! (*l_i).Archive(Writer) ) return false;
153 ItemSize -= Writer->Remainder();
154 Kumu::i2p<ui32_t>(KM_i32_BE(ItemSize), p);
158 for ( ; l_i != this->end() && result; l_i++ )
159 result = (*l_i).Archive(Writer);
165 void Dump(FILE* stream = 0, ui32_t depth = 0)
167 char identbuf[IdentBufferLen];
172 typename std::vector<T>::iterator i = this->begin();
173 for ( ; i != this->end(); i++ )
174 fprintf(stream, " %s\n", (*i).EncodeString(identbuf, IdentBufferLen));
180 class Array : public std::list<T>, public Kumu::IArchive
187 virtual bool Unarchive(Kumu::MemIOReader* Reader)
191 while ( Reader->Remainder() > 0 && result )
194 result = Tmp.Unarchive(Reader);
201 inline virtual bool HasValue() const { return ! this->empty(); }
203 virtual ui32_t ArchiveLength() const {
204 ui32_t arch_size = 0;
206 typename std::list<T>::const_iterator l_i = this->begin();
208 for ( ; l_i != this->end(); l_i++ )
209 arch_size += l_i->ArchiveLength();
215 virtual bool Archive(Kumu::MemIOWriter* Writer) const {
217 typename std::list<T>::const_iterator l_i = this->begin();
219 for ( ; l_i != this->end() && result; l_i++ )
220 result = (*l_i).Archive(Writer);
226 void Dump(FILE* stream = 0, ui32_t depth = 0)
228 char identbuf[IdentBufferLen];
233 typename std::list<T>::iterator i = this->begin();
234 for ( ; i != this->end(); i++ )
235 fprintf(stream, " %s\n", (*i).EncodeString(identbuf, IdentBufferLen));
240 class Timestamp : public Kumu::IArchive
252 Timestamp(const Timestamp& rhs);
253 Timestamp(const char* datestr);
254 virtual ~Timestamp();
256 const Timestamp& operator=(const Timestamp& rhs);
257 bool operator<(const Timestamp& rhs) const;
258 bool operator==(const Timestamp& rhs) const;
259 bool operator!=(const Timestamp& rhs) const;
261 // decode and set value from string formatted by EncodeAsString
262 Result_t SetFromString(const char* datestr);
264 // add the given number of days or hours to the timestamp value. Values less than zero
265 // will cause the value to decrease
267 void AddHours(i32_t);
269 // Write the timestamp value to the given buffer in the form 2004-05-01 13:20:00.000
270 // returns 0 if the buffer is smaller than DateTimeLen
271 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
274 inline virtual bool Unarchive(Kumu::MemIOReader* Reader) {
275 if ( ! Reader->ReadUi16BE(&Year) ) return false;
276 if ( ! Reader->ReadRaw(&Month, 6) ) return false;
280 inline virtual bool HasValue() const { return true; }
281 inline virtual ui32_t ArchiveLength() const { return 8L; }
284 inline virtual bool Archive(Kumu::MemIOWriter* Writer) const {
285 if ( ! Writer->WriteUi16BE(Year) ) return false;
286 if ( ! Writer->WriteRaw(&Month, 6) ) return false;
292 class UTF16String : public std::string, public Kumu::IArchive
298 const UTF16String& operator=(const char*);
299 const UTF16String& operator=(const std::string&);
301 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
302 inline virtual bool HasValue() const { return ! empty(); }
303 inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t) + size(); }
304 virtual bool Unarchive(Kumu::MemIOReader* Reader);
305 virtual bool Archive(Kumu::MemIOWriter* Writer) const;
309 class Rational : public ASDCP::Rational, public Kumu::IArchive
315 Rational(const Rational& rhs) {
316 Numerator = rhs.Numerator;
317 Denominator = rhs.Denominator;
320 const Rational& operator=(const Rational& rhs) {
321 Numerator = rhs.Numerator;
322 Denominator = rhs.Denominator;
326 Rational(const ASDCP::Rational& rhs) {
327 Numerator = rhs.Numerator;
328 Denominator = rhs.Denominator;
331 const Rational& operator=(const ASDCP::Rational& rhs) {
332 Numerator = rhs.Numerator;
333 Denominator = rhs.Denominator;
338 inline const char* EncodeString(char* str_buf, ui32_t buf_len) const {
339 snprintf(str_buf, buf_len, "%d/%d", Numerator, Denominator);
343 inline virtual bool Unarchive(Kumu::MemIOReader* Reader) {
344 if ( ! Reader->ReadUi32BE((ui32_t*)&Numerator) ) return false;
345 if ( ! Reader->ReadUi32BE((ui32_t*)&Denominator) ) return false;
349 inline virtual bool HasValue() const { return true; }
350 inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; }
352 inline virtual bool Archive(Kumu::MemIOWriter* Writer) const {
353 if ( ! Writer->WriteUi32BE((ui32_t)Numerator) ) return false;
354 if ( ! Writer->WriteUi32BE((ui32_t)Denominator) ) return false;
360 class VersionType : public Kumu::IArchive
362 ASDCP_NO_COPY_CONSTRUCT(VersionType);
365 enum Release_t { RL_UNKNOWN, RL_RELEASE, RL_DEVELOPMENT, RL_PATCHED, RL_BETA, RL_PRIVATE };
372 VersionType() : Major(0), Minor(0), Patch(0), Build(0), Release(RL_UNKNOWN) {}
374 void Dump(FILE* = 0);
376 const char* EncodeString(char* str_buf, ui32_t buf_len) const {
377 snprintf(str_buf, buf_len, "%hu.%hu.%hu.%hur%hu", Major, Minor, Patch, Build, Release);
381 virtual bool Unarchive(Kumu::MemIOReader* Reader) {
382 if ( ! Reader->ReadUi16BE(&Major) ) return false;
383 if ( ! Reader->ReadUi16BE(&Minor) ) return false;
384 if ( ! Reader->ReadUi16BE(&Patch) ) return false;
385 if ( ! Reader->ReadUi16BE(&Build) ) return false;
387 if ( ! Reader->ReadUi16BE(&tmp_release) ) return false;
388 Release = (Release_t)tmp_release;
392 inline virtual bool HasValue() const { return true; }
393 inline virtual ui32_t ArchiveLength() const { return sizeof(ui16_t)*5; }
395 virtual bool Archive(Kumu::MemIOWriter* Writer) const {
396 if ( ! Writer->WriteUi16BE(Major) ) return false;
397 if ( ! Writer->WriteUi16BE(Minor) ) return false;
398 if ( ! Writer->WriteUi16BE(Patch) ) return false;
399 if ( ! Writer->WriteUi16BE(Build) ) return false;
400 if ( ! Writer->WriteUi16BE((ui16_t)(Release & 0x0000ffffL)) ) return false;
406 class Raw : public Kumu::ByteString
408 ASDCP_NO_COPY_CONSTRUCT(Raw);
415 virtual bool Unarchive(Kumu::MemIOReader* Reader);
416 virtual bool Archive(Kumu::MemIOWriter* Writer) const;
417 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
424 #endif //_MXFTYPES_H_