2
Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
Permission is hereby granted, free of charge, to any person
6
obtaining a copy of this software and associated documentation
7
files (the "Software"), to deal in the Software without restriction,
8
including without limitation the rights to use, copy, modify, merge,
9
publish, distribute, sublicense, and/or sell copies of the Software,
10
and to permit persons to whom the Software is furnished to do so,
11
subject to the following conditions:
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
OTHER DEALINGS IN THE SOFTWARE.
25
For more information please visit: http://bmagic.sourceforge.net
29
#ifndef ENCODING_H__INCLUDED__
30
#define ENCODING_H__INCLUDED__
37
// ----------------------------------------------------------------
39
\brief Memory encoding.
41
Class for encoding data into memory.
42
Properly handles aligment issues with integer data types.
47
encoder(unsigned char* buf, unsigned size);
48
void put_8(unsigned char c);
49
void put_16(bm::short_t s);
50
void put_16(const bm::short_t* s, unsigned count);
51
void put_32(bm::word_t w);
52
void put_32(const bm::word_t* w, unsigned count);
53
unsigned size() const;
56
unsigned char* start_;
60
// ----------------------------------------------------------------
62
Base class for all decoding functionality
67
decoder_base(const unsigned char* buf) { buf_ = start_ = buf; }
68
/// Reads character from the decoding buffer.
69
BMFORCEINLINE unsigned char get_8() { return *buf_++; }
70
/// Returns size of the current decoding stream.
72
unsigned size() const { return (unsigned)(buf_ - start_); }
74
const unsigned char* buf_;
75
const unsigned char* start_;
79
// ----------------------------------------------------------------
81
Class for decoding data from memory buffer.
82
Properly handles aligment issues with integer data types.
84
class decoder : public decoder_base
87
decoder(const unsigned char* buf);
90
void get_32(bm::word_t* w, unsigned count);
91
void get_16(bm::short_t* s, unsigned count);
94
// ----------------------------------------------------------------
96
Class for decoding data from memory buffer.
97
Properly handles aligment issues with integer data types.
98
Converts data to big endian architecture
99
(presumed it was encoded as little endian)
101
typedef decoder decoder_big_endian;
104
// ----------------------------------------------------------------
106
Class for decoding data from memory buffer.
107
Properly handles aligment issues with integer data types.
108
Converts data to little endian architecture
109
(presumed it was encoded as big endian)
111
class decoder_little_endian : public decoder_base
114
decoder_little_endian(const unsigned char* buf);
115
bm::short_t get_16();
117
void get_32(bm::word_t* w, unsigned count);
118
void get_16(bm::short_t* s, unsigned count);
123
// ----------------------------------------------------------------
124
// Implementation details.
125
// ----------------------------------------------------------------
128
\fn encoder::encoder(unsigned char* buf, unsigned size)
130
\param buf - memory buffer pointer.
131
\param size - size of the buffer
133
inline encoder::encoder(unsigned char* buf, unsigned size)
134
: buf_(buf), start_(buf), size_(size)
139
\fn void encoder::put_8(unsigned char c)
140
\brief Puts one character into the encoding buffer.
141
\param c - character to encode
143
BMFORCEINLINE void encoder::put_8(unsigned char c)
149
\fn encoder::put_16(bm::short_t s)
150
\brief Puts short word (16 bits) into the encoding buffer.
151
\param s - short word to encode
153
BMFORCEINLINE void encoder::put_16(bm::short_t s)
155
*buf_++ = (unsigned char) s;
157
*buf_++ = (unsigned char) s;
161
\brief Method puts array of short words (16 bits) into the encoding buffer.
163
inline void encoder::put_16(const bm::short_t* s, unsigned count)
165
unsigned char* buf = buf_;
166
const bm::short_t* s_end = s + count;
169
bm::short_t w16 = *s++;
170
unsigned char a = (unsigned char) w16;
171
unsigned char b = (unsigned char) (w16 >> 8);
178
buf_ = (unsigned char*)buf;
183
\fn unsigned encoder::size() const
184
\brief Returns size of the current encoding stream.
186
inline unsigned encoder::size() const
188
return (unsigned)(buf_ - start_);
192
\fn void encoder::put_32(bm::word_t w)
193
\brief Puts 32 bits word into encoding buffer.
194
\param w - word to encode.
196
BMFORCEINLINE void encoder::put_32(bm::word_t w)
198
*buf_++ = (unsigned char) w;
199
*buf_++ = (unsigned char) (w >> 8);
200
*buf_++ = (unsigned char) (w >> 16);
201
*buf_++ = (unsigned char) (w >> 24);
205
\brief Encodes array of 32-bit words
207
inline void encoder::put_32(const bm::word_t* w, unsigned count)
209
unsigned char* buf = buf_;
210
const bm::word_t* w_end = w + count;
213
bm::word_t w32 = *w++;
214
unsigned char a = (unsigned char) w32;
215
unsigned char b = (unsigned char) (w32 >> 8);
216
unsigned char c = (unsigned char) (w32 >> 16);
217
unsigned char d = (unsigned char) (w32 >> 24);
225
buf_ = (unsigned char*)buf;
229
// ---------------------------------------------------------------------
232
\fn decoder::decoder(const unsigned char* buf)
234
\param buf - pointer to the decoding memory.
236
inline decoder::decoder(const unsigned char* buf)
242
\fn bm::short_t decoder::get_16()
243
\brief Reads 16bit word from the decoding buffer.
245
BMFORCEINLINE bm::short_t decoder::get_16()
247
bm::short_t a = (bm::short_t)(buf_[0] + ((bm::short_t)buf_[1] << 8));
253
\fn bm::word_t decoder::get_32()
254
\brief Reads 32 bit word from the decoding buffer.
256
BMFORCEINLINE bm::word_t decoder::get_32()
258
bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
259
((unsigned)buf_[2] << 16) + ((unsigned)buf_[3] << 24);
266
\fn void decoder::get_32(bm::word_t* w, unsigned count)
267
\brief Reads block of 32-bit words from the decoding buffer.
268
\param w - pointer on memory block to read into.
269
\param count - size of memory block in words.
271
inline void decoder::get_32(bm::word_t* w, unsigned count)
274
const unsigned char* buf = buf_;
275
const bm::word_t* w_end = w + count;
278
bm::word_t a = buf[0]+ ((unsigned)buf[1] << 8) +
279
((unsigned)buf[2] << 16) + ((unsigned)buf[3] << 24);
283
buf_ = (unsigned char*)buf;
287
\fn void decoder::get_16(bm::short_t* s, unsigned count)
288
\brief Reads block of 32-bit words from the decoding buffer.
289
\param s - pointer on memory block to read into.
290
\param count - size of memory block in words.
292
inline void decoder::get_16(bm::short_t* s, unsigned count)
294
const unsigned char* buf = buf_;
295
const bm::short_t* s_end = s + count;
298
bm::short_t a = (bm::short_t)(buf[0] + ((bm::short_t)buf[1] << 8));
302
buf_ = (unsigned char*)buf;
307
// ---------------------------------------------------------------------
309
inline decoder_little_endian::decoder_little_endian(const unsigned char* buf)
314
BMFORCEINLINE bm::short_t decoder_little_endian::get_16()
316
bm::short_t a = ((bm::short_t)buf_[0] << 8) + ((bm::short_t)buf_[1]);
321
BMFORCEINLINE bm::word_t decoder_little_endian::get_32()
323
bm::word_t a = ((unsigned)buf_[0] << 24)+ ((unsigned)buf_[1] << 16) +
324
((unsigned)buf_[2] << 8) + ((unsigned)buf_[3]);
329
inline void decoder_little_endian::get_32(bm::word_t* w, unsigned count)
331
const unsigned char* buf = buf_;
332
const bm::word_t* w_end = w + count;
335
bm::word_t a = ((unsigned)buf[0] << 24)+ ((unsigned)buf[1] << 16) +
336
((unsigned)buf[2] << 8) + ((unsigned)buf[3]);
340
buf_ = (unsigned char*)buf;
343
inline void decoder_little_endian::get_16(bm::short_t* s, unsigned count)
345
const unsigned char* buf = buf_;
346
const bm::short_t* s_end = s + count;
349
bm::short_t a = ((bm::short_t)buf[0] << 8) + ((bm::short_t)buf[1]);
353
buf_ = (unsigned char*)buf;
2
Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
Permission is hereby granted, free of charge, to any person
6
obtaining a copy of this software and associated documentation
7
files (the "Software"), to deal in the Software without restriction,
8
including without limitation the rights to use, copy, modify, merge,
9
publish, distribute, sublicense, and/or sell copies of the Software,
10
and to permit persons to whom the Software is furnished to do so,
11
subject to the following conditions:
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
OTHER DEALINGS IN THE SOFTWARE.
25
For more information please visit: http://bmagic.sourceforge.net
29
#ifndef ENCODING_H__INCLUDED__
30
#define ENCODING_H__INCLUDED__
37
// ----------------------------------------------------------------
39
\brief Memory encoding.
41
Class for encoding data into memory.
42
Properly handles aligment issues with integer data types.
47
encoder(unsigned char* buf, unsigned size);
48
void put_8(unsigned char c);
49
void put_16(bm::short_t s);
50
void put_16(const bm::short_t* s, unsigned count);
51
void put_32(bm::word_t w);
52
void put_32(const bm::word_t* w, unsigned count);
53
unsigned size() const;
56
unsigned char* start_;
60
// ----------------------------------------------------------------
62
Base class for all decoding functionality
67
decoder_base(const unsigned char* buf) { buf_ = start_ = buf; }
68
/// Reads character from the decoding buffer.
69
BMFORCEINLINE unsigned char get_8() { return *buf_++; }
70
/// Returns size of the current decoding stream.
72
unsigned size() const { return (unsigned)(buf_ - start_); }
73
/// change current position
75
void seek(int delta) { buf_ += delta; }
77
const unsigned char* buf_;
78
const unsigned char* start_;
82
// ----------------------------------------------------------------
84
Class for decoding data from memory buffer.
85
Properly handles aligment issues with integer data types.
87
class decoder : public decoder_base
90
decoder(const unsigned char* buf);
93
void get_32(bm::word_t* w, unsigned count);
94
void get_16(bm::short_t* s, unsigned count);
97
// ----------------------------------------------------------------
99
Class for decoding data from memory buffer.
100
Properly handles aligment issues with integer data types.
101
Converts data to big endian architecture
102
(presumed it was encoded as little endian)
104
typedef decoder decoder_big_endian;
107
// ----------------------------------------------------------------
109
Class for decoding data from memory buffer.
110
Properly handles aligment issues with integer data types.
111
Converts data to little endian architecture
112
(presumed it was encoded as big endian)
114
class decoder_little_endian : public decoder_base
117
decoder_little_endian(const unsigned char* buf);
118
bm::short_t get_16();
120
void get_32(bm::word_t* w, unsigned count);
121
void get_16(bm::short_t* s, unsigned count);
126
// ----------------------------------------------------------------
127
// Implementation details.
128
// ----------------------------------------------------------------
131
\fn encoder::encoder(unsigned char* buf, unsigned size)
133
\param buf - memory buffer pointer.
134
\param size - size of the buffer
136
inline encoder::encoder(unsigned char* buf, unsigned size)
137
: buf_(buf), start_(buf), size_(size)
142
\fn void encoder::put_8(unsigned char c)
143
\brief Puts one character into the encoding buffer.
144
\param c - character to encode
146
BMFORCEINLINE void encoder::put_8(unsigned char c)
152
\fn encoder::put_16(bm::short_t s)
153
\brief Puts short word (16 bits) into the encoding buffer.
154
\param s - short word to encode
156
BMFORCEINLINE void encoder::put_16(bm::short_t s)
158
*buf_++ = (unsigned char) s;
160
*buf_++ = (unsigned char) s;
164
\brief Method puts array of short words (16 bits) into the encoding buffer.
166
inline void encoder::put_16(const bm::short_t* s, unsigned count)
168
unsigned char* buf = buf_;
169
const bm::short_t* s_end = s + count;
172
bm::short_t w16 = *s++;
173
unsigned char a = (unsigned char) w16;
174
unsigned char b = (unsigned char) (w16 >> 8);
181
buf_ = (unsigned char*)buf;
186
\fn unsigned encoder::size() const
187
\brief Returns size of the current encoding stream.
189
inline unsigned encoder::size() const
191
return (unsigned)(buf_ - start_);
195
\fn void encoder::put_32(bm::word_t w)
196
\brief Puts 32 bits word into encoding buffer.
197
\param w - word to encode.
199
BMFORCEINLINE void encoder::put_32(bm::word_t w)
201
*buf_++ = (unsigned char) w;
202
*buf_++ = (unsigned char) (w >> 8);
203
*buf_++ = (unsigned char) (w >> 16);
204
*buf_++ = (unsigned char) (w >> 24);
208
\brief Encodes array of 32-bit words
211
void encoder::put_32(const bm::word_t* w, unsigned count)
213
unsigned char* buf = buf_;
214
const bm::word_t* w_end = w + count;
217
bm::word_t w32 = *w++;
218
unsigned char a = (unsigned char) w32;
219
unsigned char b = (unsigned char) (w32 >> 8);
220
unsigned char c = (unsigned char) (w32 >> 16);
221
unsigned char d = (unsigned char) (w32 >> 24);
229
buf_ = (unsigned char*)buf;
233
// ---------------------------------------------------------------------
236
\fn decoder::decoder(const unsigned char* buf)
238
\param buf - pointer to the decoding memory.
240
inline decoder::decoder(const unsigned char* buf)
246
\fn bm::short_t decoder::get_16()
247
\brief Reads 16bit word from the decoding buffer.
249
BMFORCEINLINE bm::short_t decoder::get_16()
251
bm::short_t a = (bm::short_t)(buf_[0] + ((bm::short_t)buf_[1] << 8));
257
\fn bm::word_t decoder::get_32()
258
\brief Reads 32 bit word from the decoding buffer.
260
BMFORCEINLINE bm::word_t decoder::get_32()
262
bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
263
((unsigned)buf_[2] << 16) + ((unsigned)buf_[3] << 24);
270
\fn void decoder::get_32(bm::word_t* w, unsigned count)
271
\brief Reads block of 32-bit words from the decoding buffer.
272
\param w - pointer on memory block to read into.
273
\param count - size of memory block in words.
275
inline void decoder::get_32(bm::word_t* w, unsigned count)
282
const unsigned char* buf = buf_;
283
const bm::word_t* w_end = w + count;
286
bm::word_t a = buf[0]+ ((unsigned)buf[1] << 8) +
287
((unsigned)buf[2] << 16) + ((unsigned)buf[3] << 24);
291
buf_ = (unsigned char*)buf;
295
\fn void decoder::get_16(bm::short_t* s, unsigned count)
296
\brief Reads block of 32-bit words from the decoding buffer.
297
\param s - pointer on memory block to read into.
298
\param count - size of memory block in words.
300
inline void decoder::get_16(bm::short_t* s, unsigned count)
308
const unsigned char* buf = buf_;
309
const bm::short_t* s_end = s + count;
312
bm::short_t a = (bm::short_t)(buf[0] + ((bm::short_t)buf[1] << 8));
316
buf_ = (unsigned char*)buf;
321
// ---------------------------------------------------------------------
323
inline decoder_little_endian::decoder_little_endian(const unsigned char* buf)
328
BMFORCEINLINE bm::short_t decoder_little_endian::get_16()
330
bm::short_t a = ((bm::short_t)buf_[0] << 8) + ((bm::short_t)buf_[1]);
335
BMFORCEINLINE bm::word_t decoder_little_endian::get_32()
337
bm::word_t a = ((unsigned)buf_[0] << 24)+ ((unsigned)buf_[1] << 16) +
338
((unsigned)buf_[2] << 8) + ((unsigned)buf_[3]);
343
inline void decoder_little_endian::get_32(bm::word_t* w, unsigned count)
351
const unsigned char* buf = buf_;
352
const bm::word_t* w_end = w + count;
355
bm::word_t a = ((unsigned)buf[0] << 24)+ ((unsigned)buf[1] << 16) +
356
((unsigned)buf[2] << 8) + ((unsigned)buf[3]);
360
buf_ = (unsigned char*)buf;
363
inline void decoder_little_endian::get_16(bm::short_t* s, unsigned count)
371
const unsigned char* buf = buf_;
372
const bm::short_t* s_end = s + count;
375
bm::short_t a = ((bm::short_t)buf[0] << 8) + ((bm::short_t)buf[1]);
379
buf_ = (unsigned char*)buf;