-
Notifications
You must be signed in to change notification settings - Fork 246
/
reader.go
263 lines (227 loc) · 5.38 KB
/
reader.go
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package reader
import (
"encoding/binary"
"errors"
"io"
"github.com/unidoc/unipdf/v3/common"
)
// Reader is the bit reader implementation.
// Implements io.Reader, io.ByteReader, io.Seeker
type Reader struct {
in []byte
cache byte // unread bits are stored here
bits byte // number of unread bits in cache
r, w int64 // buf read and write positions
err error
lastByte int
lastRuneSize int
mark int64
markBits byte
}
// compile time checks for the interface implementation of the Reader
var (
_ io.Reader = &Reader{}
_ io.ByteReader = &Reader{}
_ io.Seeker = &Reader{}
_ StreamReader = &Reader{}
)
// New creates a new reader.Reader using the byte slice data as input.
func New(data []byte) *Reader {
return &Reader{in: data}
}
// Align implements StreamReader interface.
func (r *Reader) Align() (skipped byte) {
skipped = r.bits
r.bits = 0 // no need to clear cache, will be overwritten on next read
return
}
// ConsumeRemainingBits consumes the remaining bits from the given reader.
func (r *Reader) ConsumeRemainingBits() {
if r.bits != 0 {
_, err := r.ReadBits(r.bits)
if err != nil {
common.Log.Debug("ConsumeRemainigBits failed: %v", err)
}
}
}
// BitPosition implements StreamReader inteface.
func (r *Reader) BitPosition() int {
return int(r.bits)
}
// Length implements StreamReader interface.
func (r *Reader) Length() uint64 {
return uint64(len(r.in))
}
// Mark implements StreamReader interface.
func (r *Reader) Mark() {
r.mark = r.r
r.markBits = r.bits
}
// Read implements io.Reader interface.
func (r *Reader) Read(p []byte) (n int, err error) {
if r.bits == 0 {
return r.read(p)
}
for ; n < len(p); n++ {
if p[n], err = r.readUnalignedByte(); err != nil {
return
}
}
return
}
// ReadBit implements StreamReader interface.
func (r *Reader) ReadBit() (b int, err error) {
var bit bool
bit, err = r.readBool()
if err != nil {
return
}
if bit {
b = 1
}
return
}
// ReadBits implements StreamReader interface.
func (r *Reader) ReadBits(n byte) (u uint64, err error) {
// Frequent optimization.
if n < r.bits {
// cache has all needed bits, and there are some extra which will be left in cache
shift := r.bits - n
u = uint64(r.cache >> shift)
r.cache &= 1<<shift - 1
r.bits = shift
return
}
if n > r.bits {
if r.bits > 0 {
u = uint64(r.cache)
n -= r.bits
}
// Read whole bytes.
for n >= 8 {
b, err2 := r.readBufferByte()
if err2 != nil {
return 0, err2
}
u = u<<8 + uint64(b)
n -= 8
}
// Read last fraction if exists.
if n > 0 {
if r.cache, err = r.readBufferByte(); err != nil {
return 0, err
}
shift := 8 - n
u = u<<n + uint64(r.cache>>shift)
r.cache &= 1<<shift - 1
r.bits = shift
} else {
r.bits = 0
}
return u, nil
}
r.bits = 0 // no need to clear cache, will be overridden on next read
return uint64(r.cache), nil
}
// ReadBool implements StreamReader interface.
func (r *Reader) ReadBool() (b bool, err error) {
return r.readBool()
}
// ReadByte implements io.ByteReader.
func (r *Reader) ReadByte() (b byte, err error) {
// r.bits will be the same after reading 8 bits, so we don't need to update that.
if r.bits == 0 {
return r.readBufferByte()
}
return r.readUnalignedByte()
}
// ReadUnsignedInt reads the unsigned uint32 from the reader.
func (r *Reader) ReadUnsignedInt() (uint32, error) {
ub := make([]byte, 4)
_, err := r.Read(ub)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(ub), nil
}
// Reset implements StreamReader interface.
func (r *Reader) Reset() {
r.r = r.mark
r.bits = r.markBits
}
// Seek implements the io.Seeker interface.
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
r.lastRuneSize = -1
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = r.r + offset
case io.SeekEnd:
abs = int64(len(r.in)) + offset
default:
return 0, errors.New("reader.Reader.Seek: invalid whence")
}
if abs < 0 {
return 0, errors.New("reader.Reader.Seek: negative position")
}
r.r = abs
r.bits = 0
return abs, nil
}
// StreamPosition implements StreamReader interface.
func (r *Reader) StreamPosition() int64 {
return r.r
}
func (r *Reader) read(p []byte) (n int, err error) {
if r.r >= int64(len(r.in)) {
return 0, io.EOF
}
r.lastRuneSize = -1
n = copy(p, r.in[r.r:])
r.r += int64(n)
return
}
func (r *Reader) readBufferByte() (b byte, err error) {
if r.r >= int64(len(r.in)) {
return 0, io.EOF
}
r.lastRuneSize = -1
c := r.in[r.r]
r.r++
r.lastByte = int(c)
return c, nil
}
// readUnalignedByte reads the next 8 bits which are (may be) unaligned and returns them as a byte.
func (r *Reader) readUnalignedByte() (b byte, err error) {
// r.bits will be the same after reading 8 bits, so we don't need to update that.
bits := r.bits
b = r.cache << (8 - bits)
r.cache, err = r.readBufferByte()
if err != nil {
return 0, err
}
b |= r.cache >> bits
r.cache &= 1<<bits - 1
return
}
func (r *Reader) readBool() (b bool, err error) {
if r.bits == 0 {
r.cache, err = r.readBufferByte()
if err != nil {
return
}
b = (r.cache & 0x80) != 0
r.cache, r.bits = r.cache&0x7f, 7
return
}
r.bits--
b = (r.cache & (1 << r.bits)) != 0
r.cache &= 1<<r.bits - 1
return
}