-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstreambuf.ts
More file actions
337 lines (317 loc) · 10.4 KB
/
streambuf.ts
File metadata and controls
337 lines (317 loc) · 10.4 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
export class StreamBuffer {
readonly #buf: Buffer
#pos = 0
get buffer() {
return this.#buf
}
get length() {
return this.#buf.length
}
constructor(buffer: Buffer | StreamBuffer) {
if (!(buffer instanceof Buffer)) {
if (buffer instanceof StreamBuffer) {
buffer = buffer.buffer
} else {
throw new TypeError('Not a valid Buffer or StreamBuffer')
}
}
this.#buf = buffer
}
static from(buffer: Buffer | StreamBuffer) {
return new StreamBuffer(buffer)
}
// Numeric methods
#readNumber(val: number, skip: number) {
this.skip(skip)
return val
}
#readBigInt(val: bigint, skip: number) {
this.skip(skip)
return val
}
#writeNumber(val: number, pos: number) {
this.#pos = pos
return val
}
#writeBigInt(val: bigint, pos: number) {
this.#pos = pos
return val
}
// Int
readInt8() {
return this.#readNumber(this.#buf.readInt8(this.#pos), 1)
}
readInt16LE() {
return this.#readNumber(this.#buf.readInt16LE(this.#pos), 2)
}
readInt16BE() {
return this.#readNumber(this.#buf.readInt16BE(this.#pos), 2)
}
readInt32LE() {
return this.#readNumber(this.#buf.readInt32LE(this.#pos), 4)
}
readInt32BE() {
return this.#readNumber(this.#buf.readInt32BE(this.#pos), 4)
}
readIntLE(byteLength: number) {
return this.#readNumber(this.#buf.readIntLE(this.#pos, byteLength), byteLength)
}
readIntBE(byteLength: number) {
return this.#readNumber(this.#buf.readIntBE(this.#pos, byteLength), byteLength)
}
// UInt
readUInt8() {
return this.#readNumber(this.#buf.readUInt8(this.#pos), 1)
}
readUInt16LE() {
return this.#readNumber(this.#buf.readUInt16LE(this.#pos), 2)
}
readUInt16BE() {
return this.#readNumber(this.#buf.readUInt16BE(this.#pos), 2)
}
readUInt32LE() {
return this.#readNumber(this.#buf.readUInt32LE(this.#pos), 4)
}
readUInt32BE() {
return this.#readNumber(this.#buf.readUInt32BE(this.#pos), 4)
}
readUIntLE(byteLength: number) {
return this.#readNumber(this.#buf.readUIntLE(this.#pos, byteLength), byteLength)
}
readUIntBE(byteLength: number) {
return this.#readNumber(this.#buf.readUIntBE(this.#pos, byteLength), byteLength)
}
// Floating point
readFloatLE() {
return this.#readNumber(this.#buf.readFloatLE(this.#pos), 4)
}
readFloatBE() {
return this.#readNumber(this.#buf.readFloatBE(this.#pos), 4)
}
readDoubleLE() {
return this.#readNumber(this.#buf.readDoubleLE(this.#pos), 8)
}
readDoubleBE() {
return this.#readNumber(this.#buf.readDoubleBE(this.#pos), 8)
}
// BigInt
readBigInt64LE() {
return this.#readBigInt(this.#buf.readBigInt64LE(this.#pos), 8)
}
readBigInt64BE() {
return this.#readBigInt(this.#buf.readBigInt64BE(this.#pos), 8)
}
readBigUInt64LE() {
return this.#readBigInt(this.#buf.readBigUInt64LE(this.#pos), 8)
}
readBigUInt64BE() {
return this.#readBigInt(this.#buf.readBigUInt64BE(this.#pos), 8)
}
// Byte
readByte() {
return this.readUInt8()
}
readSByte() {
return this.readInt8()
}
// Int
writeInt8(value: number) {
return this.#writeNumber(value, this.#buf.writeInt8(value, this.#pos))
}
writeInt16LE(value: number) {
return this.#writeNumber(value, this.#buf.writeInt16LE(value, this.#pos))
}
writeInt16BE(value: number) {
return this.#writeNumber(value, this.#buf.writeInt16BE(value, this.#pos))
}
writeInt32LE(value: number) {
return this.#writeNumber(value, this.#buf.writeInt32LE(value, this.#pos))
}
writeInt32BE(value: number) {
return this.#writeNumber(value, this.#buf.writeInt32BE(value, this.#pos))
}
writeIntLE(value: number, byteLength: number) {
return this.#writeNumber(value, this.#buf.writeIntLE(value, this.#pos, byteLength))
}
writeIntBE(value: number, byteLength: number) {
return this.#writeNumber(value, this.#buf.writeIntBE(value, this.#pos, byteLength))
}
// UInt
writeUInt8(value: number) {
return this.#writeNumber(value, this.#buf.writeUInt8(value, this.#pos))
}
writeUInt16LE(value: number) {
return this.#writeNumber(value, this.#buf.writeUInt16LE(value, this.#pos))
}
writeUInt16BE(value: number) {
return this.#writeNumber(value, this.#buf.writeUInt16BE(value, this.#pos))
}
writeUInt32LE(value: number) {
return this.#writeNumber(value, this.#buf.writeUInt32LE(value, this.#pos))
}
writeUInt32BE(value: number) {
return this.#writeNumber(value, this.#buf.writeUInt32BE(value, this.#pos))
}
writeUIntLE(value: number, byteLength: number) {
return this.#writeNumber(value, this.#buf.writeUIntLE(value, this.#pos, byteLength))
}
writeUIntBE(value: number, byteLength: number) {
return this.#writeNumber(value, this.#buf.writeUIntBE(value, this.#pos, byteLength))
}
// Floating point
writeFloatLE(value: number) {
return this.#writeNumber(value, this.#buf.writeFloatLE(value, this.#pos))
}
writeFloatBE(value: number) {
return this.#writeNumber(value, this.#buf.writeFloatBE(value, this.#pos))
}
writeDoubleLE(value: number) {
return this.#writeNumber(value, this.#buf.writeDoubleLE(value, this.#pos))
}
writeDoubleBE(value: number) {
return this.#writeNumber(value, this.#buf.writeDoubleBE(value, this.#pos))
}
// BigInt
writeBigInt64LE(value: bigint) {
return this.#writeBigInt(value, this.#buf.writeBigInt64LE(value, this.#pos))
}
writeBigInt64BE(value: bigint) {
return this.#writeBigInt(value, this.#buf.writeBigInt64BE(value, this.#pos))
}
writeBigUInt64LE(value: bigint) {
return this.#writeBigInt(value, this.#buf.writeBigUInt64LE(value, this.#pos))
}
writeBigUInt64BE(value: bigint) {
return this.#writeBigInt(value, this.#buf.writeBigUInt64BE(value, this.#pos))
}
// Byte
writeByte(val: number) {
return this.writeUInt8(val)
}
writeSByte(val: number) {
return this.writeInt8(val)
}
// Read 7bit encoded integer (like those used by .NET's BinaryWriter)
read7BitInt() {
let byte = 0,
shift = 0,
num = 0
do {
byte = this.readByte()
num |= (byte & 0x7f) << shift
shift += 7
} while ((byte & 0x80) != 0)
return num
}
// Write 7bit encoded integer (like those used by .NET's BinaryWriter)
write7BitInt(val: number) {
while (val >= 0x80) {
this.writeByte((val | 0x80) & 0xff) // set 8th to 1, keep only the first 8 bits
val >>= 7
}
this.writeByte(val & 0x7f)
return val
}
// Read (sub) buffer
read(numBytes: number) {
const res = this.#buf.subarray(this.#pos, this.#pos + numBytes)
this.#pos = this.#pos + numBytes
return new StreamBuffer(res)
}
write(src: Buffer) {
if (!(src instanceof Buffer)) {
throw new TypeError('Not a valid Buffer')
}
this.#pos = this.#pos + src.copy(this.#buf, this.#pos, 0)
return src
}
// String methods
#readString(length: number, encoding: BufferEncoding) {
if (length == undefined) {
for (length = 0; ; length++) {
if (this.#pos + length >= this.#buf.length || this.#buf[this.#pos + length] === 0) break
}
}
return this.#buf.toString(encoding || 'utf8', this.#pos, this.#pos + length)
}
readString(length?: number, encoding?: BufferEncoding) {
const res = this.#readString(length, encoding)
this.#pos = this.#pos + (length ?? Buffer.byteLength(res, encoding) + 1)
return res
}
readChar(encoding?: BufferEncoding) {
return this.readString(1, encoding)
}
readChars(length: number, encoding?: BufferEncoding) {
const str = this.readString(length, encoding)
const nullIndex = str.indexOf('\0')
if (nullIndex !== -1) {
return str.substring(0, nullIndex)
}
return str
}
readString7(encoding?: BufferEncoding) {
const len = this.read7BitInt()
return this.readString(len, encoding)
}
readString0(encoding?: BufferEncoding) {
return this.readString(undefined, encoding)
}
writeString(val: string, encoding?: BufferEncoding) {
this.#pos = this.#pos + this.#buf.write(val, this.#pos, encoding)
return val
}
writeChar(val: string, encoding?: BufferEncoding) {
this.#pos = this.#pos + this.#buf.write(val, this.#pos, 1, encoding)
return val
}
writeChars(val: string, length: number, encoding?: BufferEncoding) {
const bytesWritten = this.#buf.write(val, this.#pos, length, encoding)
this.#pos = this.#pos + bytesWritten
for (let i = bytesWritten; i < length; i++) {
this.writeByte(0)
}
return val
}
writeString7(val: string, encoding?: BufferEncoding) {
const len = Buffer.byteLength(val, encoding)
this.write7BitInt(len)
return this.writeString(val, encoding)
}
writeString0(val: string, encoding?: BufferEncoding) {
this.writeString(val, encoding)
this.writeByte(0)
return val
}
peekString(length?: number, encoding?: BufferEncoding) {
return this.#readString(length, encoding)
}
// Cursor methods
skip(numBytes?: number) {
numBytes ??= 1
this.#pos = this.#pos + numBytes
if (this.#pos < 0) this.#pos = 0
if (this.#pos >= this.#buf.length) this.#pos = this.#buf.length
}
setPos(position: number) {
if (position == undefined) return
this.#pos = position
if (this.#pos < 0) this.#pos = 0
if (this.#pos >= this.#buf.length) this.#pos = this.#buf.length
}
seek(position: number) {
this.setPos(position)
}
rewind() {
this.#pos = 0
}
getPos() {
return this.#pos
}
tell() {
return this.getPos()
}
isEOF() {
return this.#pos >= this.#buf.length
}
}