1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* Copyright (C) Teemu Suutari */
#ifndef INPUTSTREAM_HPP
#define INPUTSTREAM_HPP
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include "common/Buffer.hpp"
namespace ancient::internal
{
class BackwardInputStream;
class ForwardInputStream
{
friend class BackwardInputStream;
public:
ForwardInputStream(const Buffer &buffer,size_t startOffset,size_t endOffset,bool allowOverrun=false);
~ForwardInputStream();
uint8_t readByte();
const uint8_t *consume(size_t bytes,uint8_t *buffer=nullptr);
bool eof() const { return _currentOffset==_endOffset; }
size_t getOffset() const { return _currentOffset; }
size_t getEndOffset() const { return _endOffset; }
void link(BackwardInputStream &stream) { _linkedInputStream=&stream; }
private:
void setOffset(size_t offset) { _endOffset=offset; }
const uint8_t *_bufPtr;
size_t _currentOffset;
size_t _endOffset;
bool _allowOverrun;
BackwardInputStream *_linkedInputStream=nullptr;
};
class BackwardInputStream
{
friend class ForwardInputStream;
public:
BackwardInputStream(const Buffer &buffer,size_t startOffset,size_t endOffset,bool allowOverrun=false);
~BackwardInputStream();
uint8_t readByte();
const uint8_t *consume(size_t bytes,uint8_t *buffer=nullptr);
bool eof() const { return _currentOffset==_endOffset; }
size_t getOffset() const { return _currentOffset; }
void link(ForwardInputStream &stream) { _linkedInputStream=&stream; }
private:
void setOffset(size_t offset) { _endOffset=offset; }
const uint8_t *_bufPtr;
size_t _currentOffset;
size_t _endOffset;
bool _allowOverrun;
ForwardInputStream *_linkedInputStream=nullptr;
};
template<typename T>
class LSBBitReader
{
public:
LSBBitReader(T &inputStream) :
_inputStream(inputStream)
{
// nothing needed
}
~LSBBitReader()
{
// nothing needed
}
uint32_t readBits8(uint32_t count)
{
return readBitsInternal(count,[&](){
_bufContent=_inputStream.readByte();
_bufLength=8;
});
}
uint32_t readBitsBE16(uint32_t count)
{
return readBitsInternal(count,[&](){
uint8_t tmp[2];
const uint8_t *buf=_inputStream.consume(2,tmp);
_bufContent=(uint32_t(buf[0])<<8)|uint32_t(buf[1]);
_bufLength=16;
});
}
uint32_t readBitsBE32(uint32_t count)
{
return readBitsInternal(count,[&](){
uint8_t tmp[4];
const uint8_t *buf=_inputStream.consume(4,tmp);
_bufContent=(uint32_t(buf[0])<<24)|(uint32_t(buf[1])<<16)|
(uint32_t(buf[2])<<8)|uint32_t(buf[3]);
_bufLength=32;
});
}
// RNC
uint32_t readBits16Limit(uint32_t count)
{
return readBitsInternal(count,[&](){
_bufContent=_inputStream.readByte();
if (_inputStream.eof())
{
_bufLength=8;
} else {
_bufContent=_bufContent|(uint32_t(_inputStream.readByte())<<8);
_bufLength=16;
}
});
}
void reset(uint32_t bufContent=0,uint8_t bufLength=0)
{
_bufContent=bufContent;
_bufLength=bufLength;
}
private:
template<typename F>
uint32_t readBitsInternal(uint32_t count,F readWord)
{
uint32_t ret=0,pos=0;
while (count)
{
if (!_bufLength)
readWord();
uint8_t maxCount=std::min(uint8_t(count),_bufLength);
ret|=(_bufContent&((1<<maxCount)-1))<<pos;
_bufContent>>=maxCount;
_bufLength-=maxCount;
count-=maxCount;
pos+=maxCount;
}
return ret;
}
T &_inputStream;
uint32_t _bufContent=0;
uint8_t _bufLength=0;
};
template<typename T>
class MSBBitReader
{
public:
MSBBitReader(T &inputStream) :
_inputStream(inputStream)
{
// nothing needed
}
~MSBBitReader()
{
// nothing needed
}
uint32_t readBits8(uint32_t count)
{
return readBitsInternal(count,[&](){
_bufContent=_inputStream.readByte();
_bufLength=8;
});
}
uint32_t readBitsBE16(uint32_t count)
{
return readBitsInternal(count,[&](){
uint8_t tmp[2];
const uint8_t *buf=_inputStream.consume(2,tmp);
_bufContent=(uint32_t(buf[0])<<8)|uint32_t(buf[1]);
_bufLength=16;
});
}
uint32_t readBitsBE32(uint32_t count)
{
return readBitsInternal(count,[&](){
uint8_t tmp[4];
const uint8_t *buf=_inputStream.consume(4,tmp);
_bufContent=(uint32_t(buf[0])<<24)|(uint32_t(buf[1])<<16)|
(uint32_t(buf[2])<<8)|uint32_t(buf[3]);
_bufLength=32;
});
}
void reset(uint32_t bufContent=0,uint8_t bufLength=0)
{
_bufContent=bufContent;
_bufLength=bufLength;
}
private:
template<typename F>
uint32_t readBitsInternal(uint32_t count,F readWord)
{
uint32_t ret=0;
while (count)
{
if (!_bufLength)
readWord();
uint8_t maxCount=std::min(uint8_t(count),_bufLength);
_bufLength-=maxCount;
ret=(ret<<maxCount)|((_bufContent>>_bufLength)&((1<<maxCount)-1));
count-=maxCount;
}
return ret;
}
T &_inputStream;
uint32_t _bufContent=0;
uint8_t _bufLength=0;
};
}
#endif
|