-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary-reader.js
215 lines (157 loc) · 4.58 KB
/
binary-reader.js
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
let stringDecoder = new TextDecoder('utf-8')
let wstringDecoder = new TextDecoder('utf-16le')
class UnexpectedContentError extends Error {
constructor(message, actual, expected) {
super(message)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, UnexpectedContentError);
}
this.name = "UnexpectedContentError"
this.actual = actual
this.expected = expected
}
}
function expect(actual, expected, message) {
if (actual != expected) {
throw new UnexpectedContentError(`${message}`, actual, expected)
}
}
export default class BinaryReader {
constructor(view, context) {
this.view = view
this.context = context
this.debugContext = null
this.fileStart = null
this.offset = 0
this.references = null
this.strings = null
}
copy() {
let reader = new BinaryReader(this.view, this.context)
reader.debugContext = this.debugContext
reader.fileStart = this.fileStart
reader.offset = this.offset
reader.references = this.references
reader.strings = this.strings
return reader
}
get length() {
return this.view.byteLength - this.offset
}
atEnd() {
return this.length == 0
}
readBinaryReader(n) {
let reader = new BinaryReader(this.readDataView(n), this.context)
reader.debugContext = this.debugContext
reader.fileStart = this.fileStart
reader.references = this.references
reader.strings = this.strings
return reader
}
readCString() {
let view = this.view
let startOffset = this.offset
while (this.readU8() != 0) { }
let arrayOffset = view.byteOffset + startOffset
let arrayLength = this.offset - startOffset - 1
let array = new Uint8Array(view.buffer, arrayOffset, arrayLength)
return stringDecoder.decode(array)
}
readCWString() {
let view = this.view
let startOffset = this.offset
while (this.readU16() != 0) { }
let arrayOffset = view.byteOffset + startOffset
let arrayLength = this.offset - startOffset - 2
let array = new Uint8Array(view.buffer, arrayOffset, arrayLength)
return wstringDecoder.decode(array)
}
readDataView(n) {
if (n < 0) { throw `n should be positive: got ${n}` }
let view = this.view
if (this.length < n) {
throw `n is too big: remaining ${this.length}, got ${n}`
}
let value = new DataView(view.buffer, view.byteOffset + this.offset, n)
this.offset += n
return value
}
readF32() {
let value = this.view.getFloat32(this.offset, true)
this.offset += 4
this.debugContext.log("reading f32 (%d)", value)
return value
}
readF64() {
let value = this.view.getFloat64(this.offset, true)
this.offset += 8
this.debugContext.log("reading f64 (%d)", value)
return value
}
readStringU16() {
let key = this.readU16()
if (key > this.strings.length) {
throw new RangeError(`reading string ${key}, but only ${this.strings.length} exist`)
}
let value = this.strings[key]
this.debugContext.log("reading string (key: %d, value: %s)", key, value)
return value
}
readU8() {
let value = this.view.getUint8(this.offset, true)
this.offset += 1
this.debugContext.log("reading u8 (%d)", value)
return value
}
readU8Array(n) {
if (n < 0) { throw new RangeError(`n should be positive: got ${n}`) }
let value = new Uint8Array(n)
for (let i = 0; i < n; i++) {
value[i] = this.readU8()
}
return value
}
readU16() {
let value = this.view.getUint16(this.offset, true)
this.offset += 2
this.debugContext.log("reading u16 (%d)", value)
return value
}
readU16Array(n) {
if (n < 0) { throw new RangeError(`n should be positive: got ${n}`) }
let value = new Uint16Array(n)
for (let i = 0; i < n; i++) {
value[i] = this.readU16()
}
return value
}
readU32() {
let value = this.view.getUint32(this.offset, true)
this.offset += 4
this.debugContext.log("reading u32 (%d)", value)
return value
}
readU32Array(n) {
if (n < 0) { throw new RangeError(`n should be positive: got ${n}`) }
let value = new Uint32Array(n)
for (let i = 0; i < n; i++) {
value[i] = this.readU32()
}
return value
}
expectEnd(message) {
if (this.length != 0) {
throw new UnexpectedContentError(message, `${this.length} bytes`, "0 bytes")
}
}
expectU8(expected, message) {
expect(this.readU8(), expected, message)
}
expectU16(expected, message) {
expect(this.readU16(), expected, message)
}
expectU32(expected, message) {
expect(this.readU32(), expected, message)
}
}