2 Copyright (c) 2006-2008, 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 abstraction for byte-oriented conversion of integers and objects
35 #include <KM_platform.h>
45 KM_NO_COPY_CONSTRUCT(MemIOWriter);
54 MemIOWriter(byte_t* p, ui32_t c) : m_p(p), m_capacity(c), m_size(0) {
55 assert(m_p); assert(m_capacity);
58 MemIOWriter(ByteString* Buf);
61 inline void Reset() { m_size = 0; }
62 inline byte_t* Data() { return m_p; }
63 inline byte_t* CurrentData() { return m_p + m_size; }
64 inline ui32_t Length() { return m_size; }
65 inline ui32_t Remainder() { return m_capacity - m_size; }
67 inline bool AddOffset(ui32_t offset) {
68 if ( ( m_size + offset ) > m_capacity )
76 inline bool WriteRaw(const byte_t* p, ui32_t buf_len) {
77 if ( ( m_size + buf_len ) > m_capacity )
80 memcpy(m_p + m_size, p, buf_len);
85 bool WriteBER(ui64_t i, ui32_t ber_len);
87 inline bool WriteUi8(ui8_t i) {
88 if ( ( m_size + 1 ) > m_capacity )
96 inline bool WriteUi16BE(ui16_t i) {
97 if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
100 i2p<ui16_t>(KM_i16_BE(i), m_p + m_size);
101 m_size += sizeof(ui16_t);
105 inline bool WriteUi32BE(ui32_t i) {
106 if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
109 i2p<ui32_t>(KM_i32_BE(i), m_p + m_size);
110 m_size += sizeof(ui32_t);
114 inline bool WriteUi64BE(ui64_t i) {
115 if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
118 i2p<ui64_t>(KM_i64_BE(i), m_p + m_size);
119 m_size += sizeof(ui64_t);
127 KM_NO_COPY_CONSTRUCT(MemIOReader);
133 ui32_t m_size; // this is sort of a misnomer, when we are reading it measures offset
136 MemIOReader(const byte_t* p, ui32_t c) :
137 m_p(p), m_capacity(c), m_size(0) {
138 assert(m_p); assert(m_capacity);
141 MemIOReader(const ByteString* Buf);
144 inline void Reset() { m_size = 0; }
145 inline const byte_t* Data() { return m_p; }
146 inline const byte_t* CurrentData() { return m_p + m_size; }
147 inline ui32_t Offset() { return m_size; }
148 inline ui32_t Remainder() { return m_capacity - m_size; }
150 inline bool SkipOffset(ui32_t offset) {
151 if ( ( m_size + offset ) > m_capacity )
158 inline bool ReadRaw(byte_t* p, ui32_t buf_len) {
159 if ( ( m_size + buf_len ) > m_capacity )
162 memcpy(p, m_p + m_size, buf_len);
167 bool ReadBER(ui64_t* i, ui32_t* ber_len);
169 inline bool ReadUi8(ui8_t* i) {
171 if ( ( m_size + 1 ) > m_capacity )
174 *i = *(m_p + m_size);
179 inline bool ReadUi16BE(ui16_t* i) {
181 if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
184 *i = KM_i16_BE(cp2i<ui16_t>(m_p + m_size));
185 m_size += sizeof(ui16_t);
189 inline bool ReadUi32BE(ui32_t* i) {
191 if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
194 *i = KM_i32_BE(cp2i<ui32_t>(m_p + m_size));
195 m_size += sizeof(ui32_t);
199 inline bool ReadUi64BE(ui64_t* i) {
201 if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
204 *i = KM_i64_BE(cp2i<ui64_t>(m_p + m_size));
205 m_size += sizeof(ui64_t);
212 UnarchiveString(MemIOReader& Reader, std::string& str)
215 if ( ! Reader.ReadUi32BE(&str_length) ) return false;
216 str.assign((const char*)Reader.CurrentData(), str_length);
217 if ( ! Reader.SkipOffset(str_length) ) return false;
223 ArchiveString(MemIOWriter& Writer, const std::string& str)
225 if ( ! Writer.WriteUi32BE(str.length()) ) return false;
226 if ( ! Writer.WriteRaw((const byte_t*)str.c_str(), str.length()) ) return false;
232 #endif // _KM_MEMIO_H_