2 Copyright (c) 2005, 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.
29 \brief Interface for reading and writing typed objects to a byte-oriented buffer
35 #include <AS_DCP_system.h>
36 #include <hex_utils.h>
43 ASDCP_NO_COPY_CONSTRUCT(MemIOWriter);
52 MemIOWriter(byte_t* p, ui32_t c) :
53 m_p(p), m_capacity(c), m_size(0) {
59 inline byte_t* Data() { return m_p; }
60 inline byte_t* CurrentData() { return m_p + m_size; }
61 inline ui32_t Size() { return m_size; }
62 inline ui32_t Remainder() { return m_capacity - m_size; }
64 inline Result_t AddOffset(ui32_t offset) {
65 if ( ( m_size + offset ) > m_capacity )
73 inline Result_t WriteRaw(const byte_t* p, ui32_t buf_len) {
74 if ( ( m_size + buf_len ) > m_capacity )
77 memcpy(m_p + m_size, p, buf_len);
82 inline Result_t WriteBER(ui64_t i, ui32_t ber_len) {
83 if ( ( m_size + ber_len ) > m_capacity )
86 if ( ! write_BER(m_p + m_size, i, ber_len) )
93 inline Result_t WriteUi8(ui8_t i) {
94 if ( ( m_size + 1 ) > m_capacity )
102 inline Result_t WriteUi16BE(ui16_t i) {
103 if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
106 i2p<ui16_t>(ASDCP_i16_BE(i), m_p + m_size);
107 m_size += sizeof(ui16_t);
111 inline Result_t WriteUi32BE(ui32_t i) {
112 if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
115 i2p<ui32_t>(ASDCP_i32_BE(i), m_p + m_size);
116 m_size += sizeof(ui32_t);
120 inline Result_t WriteUi64BE(ui64_t i) {
121 if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
124 i2p<ui64_t>(ASDCP_i64_BE(i), m_p + m_size);
125 m_size += sizeof(ui64_t);
133 ASDCP_NO_COPY_CONSTRUCT(MemIOReader);
139 ui32_t m_size; // this is sort of a misnomer, when we are reading it measures offset
142 MemIOReader(const byte_t* p, ui32_t c) :
143 m_p(p), m_capacity(c), m_size(0) {
149 inline const byte_t* Data() { return m_p; }
150 inline const byte_t* CurrentData() { return m_p + m_size; }
151 inline ui32_t Offset() { return m_size; }
152 inline ui32_t Remainder() { return m_capacity - m_size; }
154 inline Result_t SkipOffset(ui32_t offset) {
155 if ( ( m_size + offset ) > m_capacity )
162 inline Result_t ReadRaw(byte_t* p, ui32_t buf_len) {
163 if ( ( m_size + buf_len ) > m_capacity )
166 memcpy(p, m_p + m_size, buf_len);
171 Result_t ReadBER(ui64_t* i, ui32_t* ber_len) {
173 ASDCP_TEST_NULL(ber_len);
175 if ( ( *ber_len = BER_length(m_p + m_size) ) == 0 )
178 if ( ( m_size + *ber_len ) > m_capacity )
181 if ( ! read_BER(m_p + m_size, i) )
188 inline Result_t ReadUi8(ui8_t* i) {
190 if ( ( m_size + 1 ) > m_capacity )
193 *i = *(m_p + m_size);
198 inline Result_t ReadUi16BE(ui16_t* i) {
200 if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
203 *i = ASDCP_i16_BE(cp2i<ui16_t>(m_p + m_size));
204 m_size += sizeof(ui16_t);
208 inline Result_t ReadUi32BE(ui32_t* i) {
210 if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
213 *i = ASDCP_i32_BE(cp2i<ui32_t>(m_p + m_size));
214 m_size += sizeof(ui32_t);
218 inline Result_t ReadUi64BE(ui64_t* i) {
220 if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
223 *i = ASDCP_i64_BE(cp2i<ui64_t>(m_p + m_size));
224 m_size += sizeof(ui64_t);