forked from justmoon/node-binary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.coffee
138 lines (128 loc) · 3.18 KB
/
index.coffee
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
wordfuncs =
word8s: ['readInt8',1]
word8u: ['readUInt8',1]
word16bs: [ 'readInt16BE', 2]
word16ls: [ 'readInt16LE', 2]
word16bu: [ 'readUInt16BE', 2]
word16lu: [ 'readUInt16LE', 2]
word32bs: [ 'readInt32BE', 4]
word32ls: [ 'readInt32LE', 4]
word32bu: [ 'readUInt32BE', 4]
word32lu: [ 'readUInt32LE', 4]
aliaspairs=
word8: 'word8u'
word8be: 'word8u'
word8bs: 'word8s'
word16le: 'word16lu'
word16be: 'word16bu'
word32le: 'word32lu'
word32be: 'word32bu'
wordfuncs[fn] = wordfuncs[afn] for fn, afn of aliaspairs
class BParser
constructor: (inbuf) ->
@inbuf = inbuf
@vars = {}
@offt=0
tap: (cb)->
cb.call @, @vars
@
into: (key, cb)->
if not @vars[key]?
@vars[key]= {}
cb.call this, vars
@
loop: (cb)->
end = false
ender = (-> end = true)
while end is false
cb.call @, ender, vars
@
buffer: ( name, size)->
size = @inbuf.length if not size?
if typeof size is 'string'
size = @vars[size]
buf = @inbuf.slice @offt, Math.min(@inbuf.length, @offt + size)
@offt += size
@vars[name] = buf
@
string: ( name, size)->
[email protected] if size is null
@vars[name] = @inbuf.toString( 'utf8', @offt, Math.min(@inbuf.length, @offt + size))
@offt += size
@
cstring: (name, size) ->
@string name, size
s = @vars[name]
si = s.indexOf('\0')
if si >=0
@vars[name] = s.slice 0, s.indexOf('\0')
@
skip: (nbytes) ->
if typeof nbytes is 'string'
nbytes = @vars[nbytes]
@offt += nbytes
@
scan: (name, search) ->
if typeof search is 'string'
search = new Buffer(search)
else if not Buffer.isBuffer(search)
throw new Error('search must be a Buffer or a string')
@vars[name]= null
# simple but slow string search
i=0
while (i + @offs) <= (@inbuf.length - search.length + 1)
j=0
while j < search.length and @inbuf[@offs+i+j] is search[j]
break if j is search.length
j++
i++
@vars[name]= buffer.slice @offt, @offt + i
@offt+=i+search.length
@
peek: (cb)->
was = offset
cb.call @, @vars
offset = @offt offset = was
@
flush:->
@vars = {}
@
tell:->
@offt
eof:->
@offt >= @inbuf.length
attach_word_methods = (bpclass)->
methods = bpclass::
for wm, wfp of wordfuncs
methods[wm] = ((wfp)->
[bm, len]=wfp
(nm)->
@vars[nm] = @inbuf[bm]( @offt)
@offt+=len
@
)(wfp)
null
attach_word_methods BParser
#exports = module.exports
module.exports = (buf)->
module.exports.parse buf
module.exports.parse = (buf)->
new BParser(buf)
if require.main == module
#if process.argv.length <3
buf = new Buffer([ 97, 98, 99, 99, 99, 99, 1, 65,0,67 ])
bufpar = new BParser( buf)
obj = bufpar.word8('a').word16be('bc').skip(1).buffer('d', 3).cstring('e', 3).vars
bufs = [
new Buffer([ 97, 65, 66, 67 ]),
new Buffer([ 33, 0xf3, 0xf5 ])
]
for buf in bufs
bp = new BParser(buf)
bph = bp.word8('type').tap (hdr)->
switch hdr.type
when 97 then this
.string('name',3)
when 33 then this
.word16bs('code')
console.log 'obj=', bph.vars