ZenLib
BitStream_LE.h
Go to the documentation of this file.
1/* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2 *
3 * Use of this source code is governed by a zlib-style license that can
4 * be found in the License.txt file in the root of the source tree.
5 */
6
7//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8//
9// Read a stream bit per bit, Little Endian version (rarely used!!!)
10// Can read up to 32 bits at once
11//
12//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
14//---------------------------------------------------------------------------
15#ifndef ZenBitStream_LEH
16#define ZenBitStream_LEH
17//---------------------------------------------------------------------------
18
19//---------------------------------------------------------------------------
20#include "ZenLib/BitStream.h"
21//---------------------------------------------------------------------------
22
23namespace ZenLib
24{
25
26class BitStream_LE : public BitStream
27{
28public:
30 endbyte(0),
31 endbit(0),
32 buffer(NULL),
33 ptr(NULL),
34 ptr_BeforeLastCall(NULL),
35 storage(0) {
36 };
37
38 BitStream_LE (const int8u* Buffer_, size_t Size_) : BitStream(Buffer_, Size_),
39 ptr_BeforeLastCall(NULL) {
40 Attach(Buffer_, Size_);
41 };
42
43 void Attach(const int8u* Buffer_, size_t Size_)
44 {
45 endbyte=0;
46 endbit=0;
47 buffer=Buffer_;
48 ptr=Buffer_;
49 storage=(long)Size_;
50 };
51
52 int32u Get (size_t HowMany)
53 {
54 ptr_BeforeLastCall=ptr;
55
56 long ret;
57 static const int32u Mask[33]={
58 0x00000000,
59 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
60 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
61 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
62 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
63 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
64 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
65 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
66 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff,
67 };
68 unsigned long m=Mask[HowMany];
69
70 HowMany+=endbit;
71
72 if(endbyte+4>=storage){
73 ret=-1L;
74 if(endbyte*8+(long)HowMany>storage*8){
75 Attach(NULL, 0);
76 goto overflow;
77 }
78 }
79
80 ret=ptr[0]>>endbit;
81 if(HowMany>8){
82 ret|=ptr[1]<<(8-endbit);
83 if(HowMany>16){
84 ret|=ptr[2]<<(16-endbit);
85 if(HowMany>24){
86 ret|=ptr[3]<<(24-endbit);
87 if(HowMany>32 && endbit){
88 ret|=ptr[4]<<(32-endbit);
89 }
90 }
91 }
92 }
93 ret&=m;
94
95 ptr+=HowMany/8;
96 endbyte+=(long)HowMany/8;
97 endbit=(long)HowMany&7;
98
99 overflow:
100
101 return(ret);
102 };
103
104 void Skip(size_t bits)
105 {
106 Get(bits);
107 }
108
109 int32u Remain () //How many bits remain?
110 {
111 return storage*8-(endbyte*8+endbit);
112 };
113
115 {
116 if (endbit)
117 Get(endbit);
118 };
119
120 size_t Offset_Get()
121 {
122 return ptr-buffer;
123 };
124
126 {
127 return endbit;
128 };
129
131 {
132 return ptr_BeforeLastCall-buffer;
133 };
134
135private :
136 long endbyte;
137 int endbit;
138
139 const unsigned char *buffer;
140 const unsigned char *ptr;
141 const unsigned char *ptr_BeforeLastCall;
142 long storage;
143};
144
145} //namespace ZenLib
146#endif
#define NULL
Definition: HTTPClientWrapper.h:98
Definition: BitStream_LE.h:27
int32u Remain()
Definition: BitStream_LE.h:109
void Skip(size_t bits)
Definition: BitStream_LE.h:104
void Byte_Align()
Definition: BitStream_LE.h:114
BitStream_LE()
Definition: BitStream_LE.h:29
BitStream_LE(const int8u *Buffer_, size_t Size_)
Definition: BitStream_LE.h:38
size_t BitOffset_Get()
Definition: BitStream_LE.h:125
void Attach(const int8u *Buffer_, size_t Size_)
Definition: BitStream_LE.h:43
int32u Get(size_t HowMany)
Definition: BitStream_LE.h:52
size_t OffsetBeforeLastCall_Get()
Definition: BitStream_LE.h:130
size_t Offset_Get()
Definition: BitStream_LE.h:120
Definition: BitStream.h:31
Definition: BitStream.h:24