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) s_MDD_Table[MDDindex_##s##_##l], &l
45 #define OBJ_READ_ARGS_R(s,l,r) s_MDD_Table[MDDindex_##s##_##l], &r
47 #define OBJ_WRITE_ARGS(s,l) s_MDD_Table[MDDindex_##s##_##l], &l
49 #define OBJ_TYPE_ARGS(t) s_MDD_Table[MDDindex_##t].ul
56 typedef std::pair<ui32_t, ui32_t> ItemInfo;
57 typedef std::map<TagValue, ItemInfo> TagMap;
60 class TLVReader : public ASDCP::MemIOReader
64 IPrimerLookup* m_Lookup;
67 ASDCP_NO_COPY_CONSTRUCT(TLVReader);
68 bool FindTL(const MDDEntry&);
71 TLVReader(const byte_t* p, ui32_t c, IPrimerLookup* = 0);
72 Result_t ReadObject(const MDDEntry&, IArchive*);
73 Result_t ReadUi8(const MDDEntry&, ui8_t*);
74 Result_t ReadUi16(const MDDEntry&, ui16_t*);
75 Result_t ReadUi32(const MDDEntry&, ui32_t*);
76 Result_t ReadUi64(const MDDEntry&, ui64_t*);
80 class TLVWriter : public ASDCP::MemIOWriter
84 IPrimerLookup* m_Lookup;
87 ASDCP_NO_COPY_CONSTRUCT(TLVWriter);
88 Result_t WriteTag(const MDDEntry&);
91 TLVWriter(byte_t* p, ui32_t c, IPrimerLookup* = 0);
92 Result_t WriteObject(const MDDEntry&, IArchive*);
93 Result_t WriteUi8(const MDDEntry&, ui8_t*);
94 Result_t WriteUi16(const MDDEntry&, ui16_t*);
95 Result_t WriteUi32(const MDDEntry&, ui32_t*);
96 Result_t WriteUi64(const MDDEntry&, ui64_t*);
101 class Batch : public std::vector<T>, public IArchive
107 Batch() : ItemCount(0), ItemSize(0) { ItemSize = sizeof(T); }
111 Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
112 Result_t result = Reader.ReadUi32BE(&ItemCount);
114 if ( ASDCP_SUCCESS(result) )
115 result = Reader.ReadUi32BE(&ItemSize);
117 if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
120 for ( ui32_t i = 0; i < ItemCount && ASDCP_SUCCESS(result); i++ )
123 result = Tmp.ReadFrom(Reader);
125 if ( ASDCP_SUCCESS(result) )
133 Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
134 Result_t result = Writer.WriteUi32BE(size());
136 if ( ASDCP_SUCCESS(result) )
137 result = Writer.WriteUi32BE(ItemSize);
139 typename std::vector<T>::iterator l_i = begin();
140 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
141 result = (*l_i).WriteTo(Writer);
147 void Dump(FILE* stream = 0, ui32_t depth = 0)
149 char identbuf[IdentBufferLen];
154 typename std::vector<T>::iterator i = this->begin();
155 for ( ; i != this->end(); i++ )
156 fprintf(stream, " %s\n", (*i).ToString(identbuf));
162 class Array : public std::list<T>, public IArchive
169 Result_t ReadFrom(ASDCP::MemIOReader& Reader)
171 while ( Reader.Remainder() > 0 )
174 Tmp.ReadFrom(Reader);
182 Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
183 Result_t result = RESULT_OK;
184 typename std::list<T>::iterator l_i = begin();
186 for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
187 result = (*l_i).WriteTo(Writer);
193 void Dump(FILE* stream = 0, ui32_t depth = 0)
195 char identbuf[IdentBufferLen];
200 typename std::list<T>::iterator i = this->begin();
201 for ( ; i != this->end(); i++ )
202 fprintf(stream, " %s\n", (*i).ToString(identbuf));
207 class Timestamp : public IArchive
219 Year(0), Month(0), Day(0),
220 Hour(0), Minute(0), Second(0), mSec_4(0) {}
223 inline const char* ToString(char* str_buf) const {
224 snprintf(str_buf, IdentBufferLen,
225 "%04hu-%02hu-%02hu %02hu:%02hu:%02hu.%03hu",
226 Year, Month, Day, Hour, Minute, Second, mSec_4);
231 inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
232 Result_t result = Reader.ReadUi16BE(&Year);
234 if ( ASDCP_SUCCESS(result) )
235 result = Reader.ReadRaw(&Month, 6);
241 inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
242 Result_t result = Writer.WriteUi16BE(Year);
244 if ( ASDCP_SUCCESS(result) )
245 result = Writer.WriteRaw(&Month, 6);
252 class UTF16String : public IArchive
255 char m_buffer[IdentBufferLen];
258 UTF16String() : m_length(0) { *m_buffer = 0; }
262 const char* ToString(char* str_buf) const {
263 strncpy(str_buf, m_buffer, m_length+1);
267 Result_t ReadFrom(ASDCP::MemIOReader& Reader);
268 Result_t WriteTo(ASDCP::MemIOWriter& Writer);
272 class Rational : public ASDCP::Rational, public IArchive
279 const char* ToString(char* str_buf) const {
280 snprintf(str_buf, IdentBufferLen, "%lu/%lu", Numerator, Denominator);
284 Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
285 Result_t result = Reader.ReadUi32BE((ui32_t*)&Numerator);
287 if ( ASDCP_SUCCESS(result) )
288 result = Reader.ReadUi32BE((ui32_t*)&Denominator);
293 Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
294 Result_t result = Writer.WriteUi32BE((ui32_t)Numerator);
296 if ( ASDCP_SUCCESS(result) )
297 result = Writer.WriteUi32BE((ui32_t)Denominator);
307 #endif //_MXFTYPES_H_