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 
23 namespace ZenLib
24 {
25 
26 class BitStream_LE : public BitStream
27 {
28 public:
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  long ret;
55  static const int32u Mask[33]={
56  0x00000000,
57  0x00000001, 0x00000003, 0x00000007, 0x0000000f,
58  0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
59  0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
60  0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
61  0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
62  0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
63  0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
64  0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff,
65  };
66 
67  if (HowMany==0 || HowMany>32)
68  return 0;
69 
70  ptr_BeforeLastCall=ptr;
71 
72  unsigned long m=Mask[HowMany];
73 
74  HowMany+=endbit;
75 
76  if(endbyte+4>=storage){
77  ret=-1L;
78  if(endbyte*8+(long)HowMany>storage*8){
79  Attach(NULL, 0);
80  goto overflow;
81  }
82  }
83 
84  ret=ptr[0]>>endbit;
85  if(HowMany>8){
86  ret|=ptr[1]<<(8-endbit);
87  if(HowMany>16){
88  ret|=ptr[2]<<(16-endbit);
89  if(HowMany>24){
90  ret|=ptr[3]<<(24-endbit);
91  if(HowMany>32 && endbit){
92  ret|=ptr[4]<<(32-endbit);
93  }
94  }
95  }
96  }
97  ret&=m;
98 
99  ptr+=HowMany/8;
100  endbyte+=(long)HowMany/8;
101  endbit=(long)HowMany&7;
102 
103  overflow:
104 
105  return(ret);
106  };
107 
108  void Skip(size_t bits)
109  {
110  if (bits==0)
111  return;
112  if (bits>32) //Get is only for <=32 bits
113  {
114  do {
115  Get(32);
116  if (!buffer)
117  return;
118  bits-=32;
119  } while (bits>32);
120  if (bits)
121  Get(bits);
122  return;
123  }
124  Get(bits);
125  }
126 
127  int32u Remain () //How many bits remain?
128  {
129  return storage*8-(endbyte*8+endbit);
130  };
131 
132  void Byte_Align()
133  {
134  if (endbit)
135  Get(8-endbit);
136  };
137 
138  size_t Offset_Get()
139  {
140  return ptr-buffer;
141  };
142 
143  size_t BitOffset_Get()
144  {
145  return endbit;
146  };
147 
149  {
150  return ptr_BeforeLastCall-buffer;
151  };
152 
153 private :
154  long endbyte;
155  int endbit;
156 
157  const unsigned char *buffer;
158  const unsigned char *ptr;
159  const unsigned char *ptr_BeforeLastCall;
160  long storage;
161 };
162 
163 } //namespace ZenLib
164 #endif
#define NULL
Definition: HTTPClientWrapper.h:98
Definition: BitStream_LE.h:27
int32u Remain()
Definition: BitStream_LE.h:127
void Skip(size_t bits)
Definition: BitStream_LE.h:108
void Byte_Align()
Definition: BitStream_LE.h:132
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:143
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:148
size_t Offset_Get()
Definition: BitStream_LE.h:138
Definition: BitStream.h:31
Definition: BitStream.h:24