-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbitstring.co
More file actions
270 lines (204 loc) · 6.6 KB
/
bitstring.co
File metadata and controls
270 lines (204 loc) · 6.6 KB
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
264
265
266
267
268
269
### BitString.js -- v1.0.0
###
### Copyright (c) 2012-2014 David Schoonover <dsc@less.ly> (http://less.ly)
### BitString.js is freely distributable under the MIT license.
###
### For all details, documentation, and source:
### https://github.com/dsc/bitstring.js
# Constants
SEEK_ABSOLUTE = 0
SEEK_RELATIVE = 1
SEEK_FROM_EOF = 2
# Binary representation of the number
bin = (n) ->
do
s = (if n % 2 then '1' else '0') + (s or '')
n >>= 1
while n
s
hex = (n) ->
Number(n).toString(16)
# Number of bits needed to represent the absolute value of n.
binlen = (n) ->
bin Math.abs n .length
# Returns a run of 1s of size n.
mask = (n) ->
(1 << n) - 1
chr = -> String.fromCharCode it
ord = -> String(it).charCodeAt 0
/**
* File-like object for reading/writing bits.
* @class
*/
class BitString
# Array<UInt8>
buf : null
# Byte position of read/write cursor (-1 for end).
_pos : -1
# Spill cache for bits smaller than a byte waiting to write.
_spill : 0
# Number of bits in the spill cache.
_spillen : 0
# Peek cache for read requests smaller than a byte.
_peek : 0
# Number of bits in the peek cache.
_peeklen : 0
(source='', buf=[]) ->
@buf = buf.slice()
for i til source.length
@_bufwrite source.charCodeAt i
size: ->
@buf.length + if @_spillen then 1 else 0
bitsize: ->
@buf.length*8 + @_spillen
_bufwrite: (b) ->
if @_pos is -1
@buf.push b
else
@buf[@_pos] = b
@_pos = -1 if ++@_pos >= @buf.length
this
# Writes bits to the stream; bits must be supplied as a number. Supplying n=0 will write one bit.
# Supplying the optional parameter length treats the bits as a field with the given length.
writebits: (n, size) ->
size = size or binlen n
bits = (@_spill << size) | n
size += @_spillen # handles _spill=0 but _spillen > 0
while size >= 8
size -= 8
b = bits >> size
bits &= mask size
@_bufwrite b
@_spill = bits
@_spillen = size
this
# Flushes any pending bits to the stream.
flush: ->
b = @_spill
if @_spillen
b <<= 8 - @_spillen
@_bufwrite b
@_spill = 0
@_spillen = 0
this
# Truncates the stream to zero bits.
truncate: ->
@buf = []
@_pos = -1
@_spill = 0
@_spillen = 0
@_peek = 0
@_peeklen = 0
this
# Move buffer cursor to given byte-offset. mode: 0 = absolute, 1 = relative, 2 = relative EOF
_bufseek: (n, mode=SEEK_ABSOLUTE) ->
switch mode
case 1 # relative
pos = @_pos + n
case 2
pos = @buf.length + n
default # absolute
pos = n
@_pos = if pos >= @buf.length then -1 else Math.max 0, pos
this
# Flushes the bit-buffer and moves to the given byte-offset. mode: 0 = absolute, 1 = relative, 2 = relative EOF
seek: (n, mode=SEEK_ABSOLUTE) ->
@flush()
@_peek = 0
@_peeklen = 0
@_bufseek n, mode
this
# Returns the current position of the cursor as a *byte* offset from the start of the stream.
tell: ->
if @_pos is -1 then @buf.length else @_pos
_nextbyte: ->
return null if @_pos is -1
byte = @buf[ @_pos++ ]
@_pos = -1 if @_pos >= @buf.length
byte
# Reads the next `n` bits from the stream.
readbits: (n) ->
return 0 if n == 0
size = @_peeklen
bits = @_peek
while size < n
byte = @_nextbyte()
break unless byte?
size += 8
bits = (bits << 8) | byte
if size > n
@_peeklen = size - n
@_peek = bits & mask(@_peeklen)
bits >>= @_peeklen
else
@_peeklen = 0
@_peek = 0
return if size then bits else null
# Reads the next `n` bits without moving the cursor.
peek: (n) ->
offset = 0
size = @_peeklen
bits = @_peek
while size < n
byte = @_nextbyte()
break unless byte?
offset += 1
size += 8
bits = (bits << 8) | byte
if size == 0
return null
if size > n
bits >>= size - n
if offset
@_bufseek -offset, SEEK_RELATIVE
bits
# True if there is more data to read.
hasMore: ->
@peek(1)?
### XXX: Should .each(), .map(), .reduce() flush?
# forEach of bytes
each: (fn, cxt=this) ->
@buf.forEach fn, cxt
this
# map over bytes
map: (fn, cxt=this) ->
@buf.map fn, cxt
# reduce over bytes
reduce: (fn, acc, cxt=this) ->
fn .= bind this
@buf.reduce fn, acc
# Returns the stream as a bytearray.
bytearray: ->
@flush().buf.slice()
# Dumps the stream as a binary string. Unlike __index__(), bin() will not cause int overflow.
bin: (byte_sep='') ->
@flush().buf.map(bin).join(byte_sep)
# Returns the stream as a hex string.
hex: ->
@flush().buf.map(hex).join('')
# Returns the buffer as a number. Use this with obvious caution. Called by builtins bin(), int(), long(), etc.
number: ->
@flush()
@reduce (n, byte) -> (n << 8) | byte
# Dumps the stream as a string; does not flush or change cursor position.
dump: ->
@buf.map(chr).join('') + if @_spillen then chr @_spill << (8 - @_spillen) else ''
repr: (dump_buf=true) ->
s = if dump_buf then "buf=#{@dump()}" else "len(buf)=#{@buf.length}"
return "BitString(#s,
spill[#{@_spillen}]=#{bin @_spill},
tell=#{@tell()},
peek[#{@_peeklen}]=#{bin @_peek})
"
# Dumps the stream as a string; flushes the bit-buffer but leaves cursor position unchanged.
toString: ->
@flush().dump()
BitString import {SEEK_ABSOLUTE, SEEK_RELATIVE, SEEK_FROM_EOF}
#### Module systems magic dance ####
if typeof define is 'function' and define.amd
define 'bitstring', [], (require, exports, module) -> module.exports = BitString
else if exports?
exports = module.exports = BitString if module?.exports
exports.BitString = BitString
else
window?.BitString = BitString