~ubuntu-branches/ubuntu/vivid/bmagic/vivid

« back to all changes in this revision

Viewing changes to src/encoding.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Salomon
  • Date: 2008-01-05 23:58:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080105235856-2kmxhxkz14qjy9ia
Tags: 3.5.0-1
* New upstream release.
* Add tcpp.dpatch.  This stops tests/stress/t.cpp from including
  ncbi_pch.hpp.  As far as I can tell, NCBI is not used at all, I have
  no idea where that came from..
* Silence some lintian warnings; binary-arch-rules-but-pkg-is-arch-indep
  and ancient-standards-version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
 
 
4
 
 
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:
12
 
 
13
 
The above copyright notice and this permission notice shall be included 
14
 
in all copies or substantial portions of the Software.
15
 
 
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.
23
 
 
24
 
 
25
 
For more information please visit:   http://bmagic.sourceforge.net
26
 
 
27
 
*/
28
 
 
29
 
#ifndef ENCODING_H__INCLUDED__
30
 
#define ENCODING_H__INCLUDED__
31
 
 
32
 
#include <memory.h>
33
 
 
34
 
namespace bm
35
 
{
36
 
 
37
 
// ----------------------------------------------------------------
38
 
/*!
39
 
   \brief Memory encoding.
40
 
   
41
 
   Class for encoding data into memory. 
42
 
   Properly handles aligment issues with integer data types.
43
 
*/
44
 
class encoder
45
 
{
46
 
public:
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;
54
 
private:
55
 
    unsigned char*  buf_;
56
 
    unsigned char*  start_;
57
 
    unsigned int    size_;
58
 
};
59
 
 
60
 
// ----------------------------------------------------------------
61
 
/**
62
 
    Base class for all decoding functionality
63
 
*/
64
 
class decoder_base
65
 
{
66
 
public:
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.
71
 
    BMFORCEINLINE 
72
 
    unsigned size() const { return (unsigned)(buf_ - start_); }
73
 
protected:
74
 
   const unsigned char*   buf_;
75
 
   const unsigned char*   start_;
76
 
};
77
 
 
78
 
 
79
 
// ----------------------------------------------------------------
80
 
/**
81
 
   Class for decoding data from memory buffer.
82
 
   Properly handles aligment issues with integer data types.
83
 
*/
84
 
class decoder : public decoder_base
85
 
{
86
 
public:
87
 
    decoder(const unsigned char* buf);
88
 
    bm::short_t get_16();
89
 
    bm::word_t get_32();
90
 
    void get_32(bm::word_t* w, unsigned count);
91
 
    void get_16(bm::short_t* s, unsigned count);
92
 
};
93
 
 
94
 
// ----------------------------------------------------------------
95
 
/**
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)
100
 
*/
101
 
typedef decoder decoder_big_endian;
102
 
 
103
 
 
104
 
// ----------------------------------------------------------------
105
 
/**
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)
110
 
*/
111
 
class decoder_little_endian : public decoder_base
112
 
{
113
 
public:
114
 
    decoder_little_endian(const unsigned char* buf);
115
 
    bm::short_t get_16();
116
 
    bm::word_t get_32();
117
 
    void get_32(bm::word_t* w, unsigned count);
118
 
    void get_16(bm::short_t* s, unsigned count);
119
 
};
120
 
 
121
 
 
122
 
 
123
 
// ----------------------------------------------------------------
124
 
// Implementation details. 
125
 
// ----------------------------------------------------------------
126
 
 
127
 
/*! 
128
 
    \fn encoder::encoder(unsigned char* buf, unsigned size) 
129
 
    \brief Construction.
130
 
    \param buf - memory buffer pointer.
131
 
    \param size - size of the buffer
132
 
*/
133
 
inline encoder::encoder(unsigned char* buf, unsigned size)
134
 
: buf_(buf), start_(buf), size_(size)
135
 
{
136
 
}
137
 
 
138
 
/*!
139
 
   \fn void encoder::put_8(unsigned char c) 
140
 
   \brief Puts one character into the encoding buffer.
141
 
   \param c - character to encode
142
 
*/
143
 
BMFORCEINLINE void encoder::put_8(unsigned char c)
144
 
{
145
 
    *buf_++ = c;
146
 
}
147
 
 
148
 
/*!
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
152
 
*/
153
 
BMFORCEINLINE void encoder::put_16(bm::short_t s)
154
 
{
155
 
    *buf_++ = (unsigned char) s;
156
 
    s >>= 8;
157
 
    *buf_++ = (unsigned char) s;
158
 
}
159
 
 
160
 
/*!
161
 
   \brief Method puts array of short words (16 bits) into the encoding buffer.
162
 
*/
163
 
inline void encoder::put_16(const bm::short_t* s, unsigned count)
164
 
{
165
 
    unsigned char* buf = buf_;
166
 
    const bm::short_t* s_end = s + count;
167
 
    do 
168
 
    {
169
 
        bm::short_t w16 = *s++;
170
 
        unsigned char a = (unsigned char)  w16;
171
 
        unsigned char b = (unsigned char) (w16 >> 8);
172
 
        
173
 
        *buf++ = a;
174
 
        *buf++ = b;
175
 
                
176
 
    } while (s < s_end);
177
 
    
178
 
    buf_ = (unsigned char*)buf;
179
 
}
180
 
 
181
 
 
182
 
/*!
183
 
   \fn unsigned encoder::size() const
184
 
   \brief Returns size of the current encoding stream.
185
 
*/
186
 
inline unsigned encoder::size() const
187
 
{
188
 
    return (unsigned)(buf_ - start_);
189
 
}
190
 
 
191
 
/*!
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.
195
 
*/
196
 
BMFORCEINLINE void encoder::put_32(bm::word_t w)
197
 
{
198
 
    *buf_++ = (unsigned char) w;
199
 
    *buf_++ = (unsigned char) (w >> 8);
200
 
    *buf_++ = (unsigned char) (w >> 16);
201
 
    *buf_++ = (unsigned char) (w >> 24);
202
 
}
203
 
 
204
 
/*!
205
 
    \brief Encodes array of 32-bit words
206
 
*/
207
 
inline void encoder::put_32(const bm::word_t* w, unsigned count)
208
 
{
209
 
    unsigned char* buf = buf_;
210
 
    const bm::word_t* w_end = w + count;
211
 
    do 
212
 
    {
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);
218
 
 
219
 
        *buf++ = a;
220
 
        *buf++ = b;
221
 
        *buf++ = c;
222
 
        *buf++ = d;
223
 
    } while (w < w_end);
224
 
    
225
 
    buf_ = (unsigned char*)buf;
226
 
}
227
 
 
228
 
 
229
 
// ---------------------------------------------------------------------
230
 
 
231
 
/*!
232
 
   \fn decoder::decoder(const unsigned char* buf) 
233
 
   \brief Construction
234
 
   \param buf - pointer to the decoding memory. 
235
 
*/
236
 
inline decoder::decoder(const unsigned char* buf) 
237
 
: decoder_base(buf)
238
 
{
239
 
}
240
 
 
241
 
/*!
242
 
   \fn bm::short_t decoder::get_16()
243
 
   \brief Reads 16bit word from the decoding buffer.
244
 
*/
245
 
BMFORCEINLINE bm::short_t decoder::get_16() 
246
 
{
247
 
    bm::short_t a = (bm::short_t)(buf_[0] + ((bm::short_t)buf_[1] << 8));
248
 
    buf_ += sizeof(a);
249
 
    return a;
250
 
}
251
 
 
252
 
/*!
253
 
   \fn bm::word_t decoder::get_32()
254
 
   \brief Reads 32 bit word from the decoding buffer.
255
 
*/
256
 
BMFORCEINLINE bm::word_t decoder::get_32() 
257
 
{
258
 
    bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
259
 
                   ((unsigned)buf_[2] << 16) + ((unsigned)buf_[3] << 24);
260
 
    buf_+=sizeof(a);
261
 
    return a;
262
 
}
263
 
 
264
 
 
265
 
/*!
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.
270
 
*/
271
 
inline void decoder::get_32(bm::word_t* w, unsigned count)
272
 
{
273
 
 
274
 
    const unsigned char* buf = buf_;
275
 
    const bm::word_t* w_end = w + count;
276
 
    do 
277
 
    {
278
 
        bm::word_t a = buf[0]+ ((unsigned)buf[1] << 8) +
279
 
                   ((unsigned)buf[2] << 16) + ((unsigned)buf[3] << 24);
280
 
        *w++ = a;
281
 
        buf += sizeof(a);
282
 
    } while (w < w_end);
283
 
    buf_ = (unsigned char*)buf;
284
 
}
285
 
 
286
 
/*!
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.
291
 
*/
292
 
inline void decoder::get_16(bm::short_t* s, unsigned count)
293
 
{
294
 
    const unsigned char* buf = buf_;
295
 
    const bm::short_t* s_end = s + count;
296
 
    do 
297
 
    {
298
 
        bm::short_t a = (bm::short_t)(buf[0] + ((bm::short_t)buf[1] << 8));
299
 
        *s++ = a;
300
 
        buf += sizeof(a);
301
 
    } while (s < s_end);
302
 
    buf_ = (unsigned char*)buf;
303
 
}
304
 
 
305
 
 
306
 
 
307
 
// ---------------------------------------------------------------------
308
 
 
309
 
inline decoder_little_endian::decoder_little_endian(const unsigned char* buf)
310
 
: decoder_base(buf)
311
 
{
312
 
}
313
 
 
314
 
BMFORCEINLINE bm::short_t decoder_little_endian::get_16()
315
 
{
316
 
    bm::short_t a = ((bm::short_t)buf_[0] << 8) + ((bm::short_t)buf_[1]);
317
 
    buf_ += sizeof(a);
318
 
    return a;
319
 
}
320
 
 
321
 
BMFORCEINLINE bm::word_t decoder_little_endian::get_32() 
322
 
{
323
 
    bm::word_t a = ((unsigned)buf_[0] << 24)+ ((unsigned)buf_[1] << 16) +
324
 
                   ((unsigned)buf_[2] << 8) + ((unsigned)buf_[3]);
325
 
    buf_+=sizeof(a);
326
 
    return a;
327
 
}
328
 
 
329
 
inline void decoder_little_endian::get_32(bm::word_t* w, unsigned count)
330
 
{
331
 
    const unsigned char* buf = buf_;
332
 
    const bm::word_t* w_end = w + count;
333
 
    do 
334
 
    {
335
 
        bm::word_t a = ((unsigned)buf[0] << 24)+ ((unsigned)buf[1] << 16) +
336
 
                       ((unsigned)buf[2] << 8) + ((unsigned)buf[3]);
337
 
        *w++ = a;
338
 
        buf += sizeof(a);
339
 
    } while (w < w_end);
340
 
    buf_ = (unsigned char*)buf;
341
 
}
342
 
 
343
 
inline void decoder_little_endian::get_16(bm::short_t* s, unsigned count)
344
 
{
345
 
    const unsigned char* buf = buf_;
346
 
    const bm::short_t* s_end = s + count;
347
 
    do 
348
 
    {
349
 
        bm::short_t a = ((bm::short_t)buf[0] << 8) + ((bm::short_t)buf[1]);
350
 
        *s++ = a;
351
 
        buf += sizeof(a);
352
 
    } while (s < s_end);
353
 
    buf_ = (unsigned char*)buf;
354
 
}
355
 
 
356
 
 
357
 
} // namespace bm
358
 
 
359
 
#endif
360
 
 
361
 
 
 
1
/*
 
2
Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
 
3
 
 
4
 
 
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:
 
12
 
 
13
The above copyright notice and this permission notice shall be included 
 
14
in all copies or substantial portions of the Software.
 
15
 
 
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.
 
23
 
 
24
 
 
25
For more information please visit:   http://bmagic.sourceforge.net
 
26
 
 
27
*/
 
28
 
 
29
#ifndef ENCODING_H__INCLUDED__
 
30
#define ENCODING_H__INCLUDED__
 
31
 
 
32
#include <memory.h>
 
33
 
 
34
namespace bm
 
35
{
 
36
 
 
37
// ----------------------------------------------------------------
 
38
/*!
 
39
   \brief Memory encoding.
 
40
   
 
41
   Class for encoding data into memory. 
 
42
   Properly handles aligment issues with integer data types.
 
43
*/
 
44
class encoder
 
45
{
 
46
public:
 
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;
 
54
private:
 
55
    unsigned char*  buf_;
 
56
    unsigned char*  start_;
 
57
    unsigned int    size_;
 
58
};
 
59
 
 
60
// ----------------------------------------------------------------
 
61
/**
 
62
    Base class for all decoding functionality
 
63
*/
 
64
class decoder_base
 
65
{
 
66
public:
 
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.
 
71
    BMFORCEINLINE 
 
72
    unsigned size() const { return (unsigned)(buf_ - start_); }
 
73
    /// change current position
 
74
    BMFORCEINLINE
 
75
    void seek(int delta) { buf_ += delta; }
 
76
protected:
 
77
   const unsigned char*   buf_;
 
78
   const unsigned char*   start_;
 
79
};
 
80
 
 
81
 
 
82
// ----------------------------------------------------------------
 
83
/**
 
84
   Class for decoding data from memory buffer.
 
85
   Properly handles aligment issues with integer data types.
 
86
*/
 
87
class decoder : public decoder_base
 
88
{
 
89
public:
 
90
    decoder(const unsigned char* buf);
 
91
    bm::short_t get_16();
 
92
    bm::word_t get_32();
 
93
    void get_32(bm::word_t* w, unsigned count);
 
94
    void get_16(bm::short_t* s, unsigned count);
 
95
};
 
96
 
 
97
// ----------------------------------------------------------------
 
98
/**
 
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)
 
103
*/
 
104
typedef decoder decoder_big_endian;
 
105
 
 
106
 
 
107
// ----------------------------------------------------------------
 
108
/**
 
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)
 
113
*/
 
114
class decoder_little_endian : public decoder_base
 
115
{
 
116
public:
 
117
    decoder_little_endian(const unsigned char* buf);
 
118
    bm::short_t get_16();
 
119
    bm::word_t get_32();
 
120
    void get_32(bm::word_t* w, unsigned count);
 
121
    void get_16(bm::short_t* s, unsigned count);
 
122
};
 
123
 
 
124
 
 
125
 
 
126
// ----------------------------------------------------------------
 
127
// Implementation details. 
 
128
// ----------------------------------------------------------------
 
129
 
 
130
/*! 
 
131
    \fn encoder::encoder(unsigned char* buf, unsigned size) 
 
132
    \brief Construction.
 
133
    \param buf - memory buffer pointer.
 
134
    \param size - size of the buffer
 
135
*/
 
136
inline encoder::encoder(unsigned char* buf, unsigned size)
 
137
: buf_(buf), start_(buf), size_(size)
 
138
{
 
139
}
 
140
 
 
141
/*!
 
142
   \fn void encoder::put_8(unsigned char c) 
 
143
   \brief Puts one character into the encoding buffer.
 
144
   \param c - character to encode
 
145
*/
 
146
BMFORCEINLINE void encoder::put_8(unsigned char c)
 
147
{
 
148
    *buf_++ = c;
 
149
}
 
150
 
 
151
/*!
 
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
 
155
*/
 
156
BMFORCEINLINE void encoder::put_16(bm::short_t s)
 
157
{
 
158
    *buf_++ = (unsigned char) s;
 
159
    s >>= 8;
 
160
    *buf_++ = (unsigned char) s;
 
161
}
 
162
 
 
163
/*!
 
164
   \brief Method puts array of short words (16 bits) into the encoding buffer.
 
165
*/
 
166
inline void encoder::put_16(const bm::short_t* s, unsigned count)
 
167
{
 
168
    unsigned char* buf = buf_;
 
169
    const bm::short_t* s_end = s + count;
 
170
    do 
 
171
    {
 
172
        bm::short_t w16 = *s++;
 
173
        unsigned char a = (unsigned char)  w16;
 
174
        unsigned char b = (unsigned char) (w16 >> 8);
 
175
        
 
176
        *buf++ = a;
 
177
        *buf++ = b;
 
178
                
 
179
    } while (s < s_end);
 
180
    
 
181
    buf_ = (unsigned char*)buf;
 
182
}
 
183
 
 
184
 
 
185
/*!
 
186
   \fn unsigned encoder::size() const
 
187
   \brief Returns size of the current encoding stream.
 
188
*/
 
189
inline unsigned encoder::size() const
 
190
{
 
191
    return (unsigned)(buf_ - start_);
 
192
}
 
193
 
 
194
/*!
 
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.
 
198
*/
 
199
BMFORCEINLINE void encoder::put_32(bm::word_t w)
 
200
{
 
201
    *buf_++ = (unsigned char) w;
 
202
    *buf_++ = (unsigned char) (w >> 8);
 
203
    *buf_++ = (unsigned char) (w >> 16);
 
204
    *buf_++ = (unsigned char) (w >> 24);
 
205
}
 
206
 
 
207
/*!
 
208
    \brief Encodes array of 32-bit words
 
209
*/
 
210
inline 
 
211
void encoder::put_32(const bm::word_t* w, unsigned count)
 
212
{
 
213
    unsigned char* buf = buf_;
 
214
    const bm::word_t* w_end = w + count;
 
215
    do 
 
216
    {
 
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);
 
222
 
 
223
        *buf++ = a;
 
224
        *buf++ = b;
 
225
        *buf++ = c;
 
226
        *buf++ = d;
 
227
    } while (w < w_end);
 
228
    
 
229
    buf_ = (unsigned char*)buf;
 
230
}
 
231
 
 
232
 
 
233
// ---------------------------------------------------------------------
 
234
 
 
235
/*!
 
236
   \fn decoder::decoder(const unsigned char* buf) 
 
237
   \brief Construction
 
238
   \param buf - pointer to the decoding memory. 
 
239
*/
 
240
inline decoder::decoder(const unsigned char* buf) 
 
241
: decoder_base(buf)
 
242
{
 
243
}
 
244
 
 
245
/*!
 
246
   \fn bm::short_t decoder::get_16()
 
247
   \brief Reads 16bit word from the decoding buffer.
 
248
*/
 
249
BMFORCEINLINE bm::short_t decoder::get_16() 
 
250
{
 
251
    bm::short_t a = (bm::short_t)(buf_[0] + ((bm::short_t)buf_[1] << 8));
 
252
    buf_ += sizeof(a);
 
253
    return a;
 
254
}
 
255
 
 
256
/*!
 
257
   \fn bm::word_t decoder::get_32()
 
258
   \brief Reads 32 bit word from the decoding buffer.
 
259
*/
 
260
BMFORCEINLINE bm::word_t decoder::get_32() 
 
261
{
 
262
    bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
 
263
                   ((unsigned)buf_[2] << 16) + ((unsigned)buf_[3] << 24);
 
264
    buf_+=sizeof(a);
 
265
    return a;
 
266
}
 
267
 
 
268
 
 
269
/*!
 
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.
 
274
*/
 
275
inline void decoder::get_32(bm::word_t* w, unsigned count)
 
276
{
 
277
    if (!w) 
 
278
    {
 
279
        seek(count * 4);
 
280
        return;
 
281
    }
 
282
    const unsigned char* buf = buf_;
 
283
    const bm::word_t* w_end = w + count;
 
284
    do 
 
285
    {
 
286
        bm::word_t a = buf[0]+ ((unsigned)buf[1] << 8) +
 
287
                   ((unsigned)buf[2] << 16) + ((unsigned)buf[3] << 24);
 
288
        *w++ = a;
 
289
        buf += sizeof(a);
 
290
    } while (w < w_end);
 
291
    buf_ = (unsigned char*)buf;
 
292
}
 
293
 
 
294
/*!
 
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.
 
299
*/
 
300
inline void decoder::get_16(bm::short_t* s, unsigned count)
 
301
{
 
302
    if (!s) 
 
303
    {
 
304
        seek(count * 2);
 
305
        return;
 
306
    }
 
307
 
 
308
    const unsigned char* buf = buf_;
 
309
    const bm::short_t* s_end = s + count;
 
310
    do 
 
311
    {
 
312
        bm::short_t a = (bm::short_t)(buf[0] + ((bm::short_t)buf[1] << 8));
 
313
        *s++ = a;
 
314
        buf += sizeof(a);
 
315
    } while (s < s_end);
 
316
    buf_ = (unsigned char*)buf;
 
317
}
 
318
 
 
319
 
 
320
 
 
321
// ---------------------------------------------------------------------
 
322
 
 
323
inline decoder_little_endian::decoder_little_endian(const unsigned char* buf)
 
324
: decoder_base(buf)
 
325
{
 
326
}
 
327
 
 
328
BMFORCEINLINE bm::short_t decoder_little_endian::get_16()
 
329
{
 
330
    bm::short_t a = ((bm::short_t)buf_[0] << 8) + ((bm::short_t)buf_[1]);
 
331
    buf_ += sizeof(a);
 
332
    return a;
 
333
}
 
334
 
 
335
BMFORCEINLINE bm::word_t decoder_little_endian::get_32() 
 
336
{
 
337
    bm::word_t a = ((unsigned)buf_[0] << 24)+ ((unsigned)buf_[1] << 16) +
 
338
                   ((unsigned)buf_[2] << 8) + ((unsigned)buf_[3]);
 
339
    buf_+=sizeof(a);
 
340
    return a;
 
341
}
 
342
 
 
343
inline void decoder_little_endian::get_32(bm::word_t* w, unsigned count)
 
344
{
 
345
    if (!w) 
 
346
    {
 
347
        seek(count * 4);
 
348
        return;
 
349
    }
 
350
 
 
351
    const unsigned char* buf = buf_;
 
352
    const bm::word_t* w_end = w + count;
 
353
    do 
 
354
    {
 
355
        bm::word_t a = ((unsigned)buf[0] << 24)+ ((unsigned)buf[1] << 16) +
 
356
                       ((unsigned)buf[2] << 8) + ((unsigned)buf[3]);
 
357
        *w++ = a;
 
358
        buf += sizeof(a);
 
359
    } while (w < w_end);
 
360
    buf_ = (unsigned char*)buf;
 
361
}
 
362
 
 
363
inline void decoder_little_endian::get_16(bm::short_t* s, unsigned count)
 
364
{
 
365
    if (!s) 
 
366
    {
 
367
        seek(count * 2);
 
368
        return;
 
369
    }
 
370
 
 
371
    const unsigned char* buf = buf_;
 
372
    const bm::short_t* s_end = s + count;
 
373
    do 
 
374
    {
 
375
        bm::short_t a = ((bm::short_t)buf[0] << 8) + ((bm::short_t)buf[1]);
 
376
        *s++ = a;
 
377
        buf += sizeof(a);
 
378
    } while (s < s_end);
 
379
    buf_ = (unsigned char*)buf;
 
380
}
 
381
 
 
382
 
 
383
} // namespace bm
 
384
 
 
385
#endif
 
386
 
 
387