summaryrefslogtreecommitdiff
path: root/src/KM_memio.h
blob: caf4fc0aca0f48a1517a8a6bbd1c93822baa0c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Copyright (c) 2006-2011, John Hurst
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
  /*! \file  KM_memio.h
    \version $Id$
    \brief   abstraction for byte-oriented conversion of integers and objects
  */

#ifndef _KM_MEMIO_H_
#define _KM_MEMIO_H_

#include <KM_platform.h>
#include <string>
#include <cstring>

namespace Kumu
{
  class ByteString;

  //
  class MemIOWriter
    {
      KM_NO_COPY_CONSTRUCT(MemIOWriter);
      MemIOWriter();
      
    protected:
      byte_t* m_p;
      ui32_t  m_capacity;
      ui32_t  m_size;

    public:
      MemIOWriter(byte_t* p, ui32_t c) : m_p(p), m_capacity(c), m_size(0) {
	assert(m_p); assert(m_capacity);
      }

      MemIOWriter(ByteString* Buf);
      ~MemIOWriter() {}

      inline void    Reset() { m_size = 0; }
      inline byte_t* Data() { return m_p; }
      inline const byte_t* RoData() const { return m_p; }
      inline byte_t* CurrentData() { return m_p + m_size; }
      inline ui32_t  Length() const { return m_size; }
      inline ui32_t  Remainder() const { return m_capacity - m_size; }

      inline bool AddOffset(ui32_t offset) {
	if ( ( m_size + offset ) > m_capacity )
	  return false;

	m_size += offset;
	return true;

      }

      inline bool WriteRaw(const byte_t* p, ui32_t buf_len) {
	if ( ( m_size + buf_len ) > m_capacity )
	  return false;

	memcpy(m_p + m_size, p, buf_len);
	m_size += buf_len;
	return true;
      }

      bool WriteBER(ui64_t i, ui32_t ber_len);

      inline bool WriteUi8(ui8_t i) {
	if ( ( m_size + 1 ) > m_capacity )
	  return false;

	*(m_p + m_size) = i;
	m_size++;
	return true;
      }

      inline bool WriteUi16BE(ui16_t i) {
	if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
	  return false;
	
	i2p<ui16_t>(KM_i16_BE(i), m_p + m_size);
	m_size += sizeof(ui16_t);
	return true;
      }

      inline bool WriteUi32BE(ui32_t i) {
	if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
	  return false;
	
	i2p<ui32_t>(KM_i32_BE(i), m_p + m_size);
	m_size += sizeof(ui32_t);
	return true;
      }

      inline bool WriteUi64BE(ui64_t i) {
	if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
	  return false;
	
	i2p<ui64_t>(KM_i64_BE(i), m_p + m_size);
	m_size += sizeof(ui64_t);
	return true;
      }

      inline bool WriteString(const std::string& str) {
	ui32_t len = static_cast<ui32_t>(str.length());
	if ( ! WriteUi32BE(len) ) return false;
	if ( ! WriteRaw((const byte_t*)str.c_str(), len) ) return false;
	return true;
      }
   };

  //
  class MemIOReader
    {
      KM_NO_COPY_CONSTRUCT(MemIOReader);
      MemIOReader();
      
    protected:
      const byte_t* m_p;
      ui32_t  m_capacity;
      ui32_t  m_size; // this is sort of a misnomer, when we are reading it measures offset

    public:
      MemIOReader(const byte_t* p, ui32_t c) :
	m_p(p), m_capacity(c), m_size(0) {
	assert(m_p); assert(m_capacity);
      }

      MemIOReader(const ByteString* Buf);
      ~MemIOReader() {}

      inline void          Reset() { m_size = 0; }
      inline const byte_t* Data() const { return m_p; }
      inline const byte_t* CurrentData() const { return m_p + m_size; }
      inline ui32_t        Offset() const { return m_size; }
      inline ui32_t        Remainder() const { return m_capacity - m_size; }

      inline bool SkipOffset(ui32_t offset) {
	if ( ( m_size + offset ) > m_capacity )
	  return false;

	m_size += offset;
	return true;
      }

      inline bool ReadRaw(byte_t* p, ui32_t buf_len) {
	if ( ( m_size + buf_len ) > m_capacity )
	  return false;

	memcpy(p, m_p + m_size, buf_len);
	m_size += buf_len;
	return true;
      }

      bool ReadBER(ui64_t* i, ui32_t* ber_len);

      inline bool ReadUi8(ui8_t* i) {
	assert(i);
	if ( ( m_size + 1 ) > m_capacity )
	  return false;

	*i = *(m_p + m_size);
	m_size++;
	return true;
      }

      inline bool ReadUi16BE(ui16_t* i) {
	assert(i);
	if ( ( m_size + sizeof(ui16_t) ) > m_capacity )
	  return false;

	*i = KM_i16_BE(cp2i<ui16_t>(m_p + m_size));
	m_size += sizeof(ui16_t);
	return true;
      }

      inline bool ReadUi32BE(ui32_t* i) {
	assert(i);
	if ( ( m_size + sizeof(ui32_t) ) > m_capacity )
	  return false;

	*i = KM_i32_BE(cp2i<ui32_t>(m_p + m_size));
	m_size += sizeof(ui32_t);
	return true;
      }

      inline bool ReadUi64BE(ui64_t* i) {
	assert(i);
	if ( ( m_size + sizeof(ui64_t) ) > m_capacity )
	  return false;

	*i = KM_i64_BE(cp2i<ui64_t>(m_p + m_size));
	m_size += sizeof(ui64_t);
	return true;
      }

      inline bool ReadString(std::string& str)
      {
	ui32_t str_length = 0;
	if ( ! ReadUi32BE(&str_length) ) return false;

	if ( str_length > 0 )
	  {
	    if ( ( m_size + str_length ) > m_capacity ) return false;
	    str.assign((const char*)CurrentData(), str_length);
	    if ( ! SkipOffset(str_length) ) return false;
	  }

	return true;
      }
    };

  //
  inline bool
  UnarchiveString(MemIOReader& Reader, std::string& str) {
    return Reader.ReadString(str);
  }

  //
  inline bool
  ArchiveString(MemIOWriter& Writer, const std::string& str)
  {
    return Writer.WriteString(str);
  }


} // namespace Kumu

#endif // _KM_MEMIO_H_

//
// end KM_memio.h
//