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:
29 BitStream_LE () :BitStream()
30 {
31 endbyte=0;
32 endbit=0;
33 buffer=NULL;
34 ptr=NULL;
35 storage=0;
36 };
37
38 BitStream_LE (const int8u* Buffer_, size_t Size_) :BitStream(Buffer_, Size_) {
39 Attach(Buffer_, Size_);
40 };
41
42 void Attach(const int8u* Buffer_, size_t Size_)
43 {
44 endbyte=0;
45 endbit=0;
46 buffer=Buffer_;
47 ptr=Buffer_;
48 storage=(long)Size_;
49 };
50
51 int32u Get (size_t HowMany)
52 {
53 ptr_BeforeLastCall=ptr;
54
55 long ret;
56 static const int32u Mask[33]={
57 0x00000000,
58 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
59 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
60 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
61 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
62 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
63 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
64 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
65 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff,
66 };
67 unsigned long m=Mask[HowMany];
68
69 HowMany+=endbit;
70
71 if(endbyte+4>=storage){
72 ret=-1L;
73 if(endbyte*8+(long)HowMany>storage*8){
74 Attach(NULL, 0);
75 goto overflow;
76 }
77 }
78
79 ret=ptr[0]>>endbit;
80 if(HowMany>8){
81 ret|=ptr[1]<<(8-endbit);
82 if(HowMany>16){
83 ret|=ptr[2]<<(16-endbit);
84 if(HowMany>24){
85 ret|=ptr[3]<<(24-endbit);
86 if(HowMany>32 && endbit){
87 ret|=ptr[4]<<(32-endbit);
88 }
89 }
90 }
91 }
92 ret&=m;
93
94 ptr+=HowMany/8;
95 endbyte+=(long)HowMany/8;
96 endbit=(long)HowMany&7;
97
98 overflow:
99
100 return(ret);
101 };
102
103 void Skip(size_t bits)
104 {
105 Get(bits);
106 }
107
108 int32u Remain () //How many bits remain?
109 {
110 return storage*8-(endbyte*8+endbit);
111 };
112
113 void Byte_Align()
114 {
115 if (endbit)
116 Get(endbit);
117 };
118
119 size_t Offset_Get()
120 {
121 return ptr-buffer;
122 };
123
124 size_t BitOffset_Get()
125 {
126 return endbit;
127 };
128
129 size_t OffsetBeforeLastCall_Get()
130 {
131 return ptr_BeforeLastCall-buffer;
132 };
133
134private :
135 long endbyte;
136 int endbit;
137
138 const unsigned char *buffer;
139 const unsigned char *ptr;
140 const unsigned char *ptr_BeforeLastCall;
141 long storage;
142};
143
144} //namespace ZenLib
145#endif
BitStream.h
NULL
#define NULL
Definition: HTTPClientWrapper.h:97
ZenLib::BitStream_LE
Definition: BitStream_LE.h:27
ZenLib::BitStream_LE::Remain
int32u Remain()
Definition: BitStream_LE.h:108
ZenLib::BitStream_LE::Skip
void Skip(size_t bits)
Definition: BitStream_LE.h:103
ZenLib::BitStream_LE::Byte_Align
void Byte_Align()
Definition: BitStream_LE.h:113
ZenLib::BitStream_LE::BitStream_LE
BitStream_LE()
Definition: BitStream_LE.h:29
ZenLib::BitStream_LE::BitStream_LE
BitStream_LE(const int8u *Buffer_, size_t Size_)
Definition: BitStream_LE.h:38
ZenLib::BitStream_LE::BitOffset_Get
size_t BitOffset_Get()
Definition: BitStream_LE.h:124
ZenLib::BitStream_LE::Attach
void Attach(const int8u *Buffer_, size_t Size_)
Definition: BitStream_LE.h:42
ZenLib::BitStream_LE::Get
int32u Get(size_t HowMany)
Definition: BitStream_LE.h:51
ZenLib::BitStream_LE::OffsetBeforeLastCall_Get
size_t OffsetBeforeLastCall_Get()
Definition: BitStream_LE.h:129
ZenLib::BitStream_LE::Offset_Get
size_t Offset_Get()
Definition: BitStream_LE.h:119
ZenLib::BitStream
Definition: BitStream.h:31
ZenLib
Definition: BitStream.h:24