-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
409 lines (349 loc) · 15.3 KB
/
index.ts
File metadata and controls
409 lines (349 loc) · 15.3 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { BLOCK_MAXSIZE, BLOCK, BLOCK_OVERHEAD } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";
const BUFFER_INSPECT_HEADER_START: string = "<Buffer ";
export let INSPECT_MAX_BYTES: i32 = 50;
export class Buffer extends Uint8Array {
constructor(size: i32) {
super(size);
}
public static alloc(size: i32): Buffer {
return new Buffer(size);
}
@unsafe public static allocUnsafe(size: i32): Buffer {
// Node throws an error if size is less than 0
if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
let buffer = __alloc(size, idof<ArrayBuffer>());
// This retains the pointer to the result Buffer.
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = changetype<usize>(buffer);
result.dataLength = size;
return result;
}
public static isBuffer<T>(value: T): bool {
return value instanceof Buffer;
}
// Adapted from https://github.com/AssemblyScript/assemblyscript/blob/master/std/assembly/typedarray.ts
public subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer {
var len = <i32>this.dataLength;
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
end = max(end, begin);
var out = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>())); // retains
out.data = this.data; // retains
out.dataStart = this.dataStart + <usize>begin;
out.dataLength = end - begin;
return out;
}
readInt8(offset: i32 = 0): i8 {
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + <usize>offset);
}
readUInt8(offset: i32 = 0): u8 {
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u8>(this.dataStart + <usize>offset);
}
writeInt8(value: i8, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i8>(this.dataStart + offset, value);
return offset + 1;
}
writeUInt8(value: u8, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u8>(this.dataStart + offset, value);
return offset + 1;
}
readInt16LE(offset: i32 = 0): i16 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i16>(this.dataStart + <usize>offset);
}
readInt16BE(offset: i32 = 0): i16 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<i16>(load<i16>(this.dataStart + <usize>offset));
}
readUInt16LE(offset: i32 = 0): u16 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u16>(this.dataStart + <usize>offset);
}
readUInt16BE(offset: i32 = 0): u16 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<u16>(load<u16>(this.dataStart + <usize>offset));
}
writeInt16LE(value: i16, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i16>(this.dataStart + offset, value);
return offset + 2;
}
writeInt16BE(value: i16, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i16>(this.dataStart + offset, bswap<i16>(value));
return offset + 2;
}
writeUInt16LE(value: u16, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u16>(this.dataStart + offset, value);
return offset + 2;
}
writeUInt16BE(value: u16, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u16>(this.dataStart + offset, bswap<u16>(value));
return offset + 2;
}
readInt32LE(offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i32>(this.dataStart + <usize>offset);
}
readInt32BE(offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<i32>(load<i32>(this.dataStart + <usize>offset));
}
readUInt32LE(offset: i32 = 0): u32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u32>(this.dataStart + <usize>offset);
}
readUInt32BE(offset: i32 = 0): u32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<u32>(load<u32>(this.dataStart + <usize>offset));
}
writeInt32LE(value: i32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, value);
return offset + 4;
}
writeInt32BE(value: i32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, bswap<i32>(value));
return offset + 4;
}
writeUInt32LE(value: u32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u32>(this.dataStart + offset, value);
return offset + 4;
}
writeUInt32BE(value: u32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u32>(this.dataStart + offset, bswap<u32>(value));
return offset + 4;
}
readFloatLE(offset: i32 = 0): f32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<f32>(this.dataStart + <usize>offset);
}
readFloatBE(offset: i32 = 0): f32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return reinterpret<f32>(bswap<i32>(load<i32>(this.dataStart + <usize>offset)));
}
writeFloatLE(value: f32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<f32>(this.dataStart + offset, value);
return offset + 4;
}
writeFloatBE(value: f32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, bswap<i32>(reinterpret<i32>(value)));
return offset + 4;
}
readBigInt64LE(offset: i32 = 0): i64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i64>(this.dataStart + <usize>offset);
}
readBigInt64BE(offset: i32 = 0): i64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<i64>(load<i64>(this.dataStart + <usize>offset));
}
readBigUInt64LE(offset: i32 = 0): u64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u64>(this.dataStart + <usize>offset);
}
readBigUInt64BE(offset: i32 = 0): u64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<u64>(load<u64>(this.dataStart + <usize>offset));
}
writeBigInt64LE(value: i64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i64>(this.dataStart + offset, value);
return offset + 8;
}
writeBigInt64BE(value: i64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i64>(this.dataStart + offset, bswap<i64>(value));
return offset + 8;
}
writeBigUInt64LE(value: u64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u64>(this.dataStart + offset, value);
return offset + 8;
}
writeBigUInt64BE(value: u64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u64>(this.dataStart + offset, bswap<u64>(value));
return offset + 8;
}
readDoubleLE(offset: i32 = 0): f64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<f64>(this.dataStart + <usize>offset);
}
readDoubleBE(offset: i32 = 0): f64 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return reinterpret<f64>(bswap<i64>(load<i64>(this.dataStart + <usize>offset)));
}
writeDoubleLE(value: f64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<f64>(this.dataStart + offset, value);
return offset + 8;
}
writeDoubleBE(value: f64, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i64>(this.dataStart + offset, bswap<i64>(reinterpret<i64>(value)));
return offset + 8;
}
/**
* Calculate an inspectable string to print to the console.
*
* @example
* let result = Buffer.from([1, 2, 3, 4, 5]).inspect();
* // '<Buffer 01 02 03 04 05>'
*/
inspect(): string {
let byteLength = this.byteLength;
if (INSPECT_MAX_BYTES == 0 || byteLength == 0) return "<Buffer >";
// Calculate if an elipsis will be in the string
let elipsisEnd = byteLength > INSPECT_MAX_BYTES;
let maxBytes = elipsisEnd ? INSPECT_MAX_BYTES : byteLength;
// find the start of the buffer
let dataStart = this.dataStart;
/**
* Formula for stringLength is calculated by adding the constant character count
* to the number of visible bytes multiplied by 3, and adding 3 more for an ellipsis
* if the total number of visible bytes is less than the actual byteLength.
*
* @example
* let stringLength = (3 * maxBytes) + len("<Buffer ") + len(">") + (elipsisEnd ? 3 : 0);
*
* // This can be reduced to...
* let stringLength = (3 * maxBytes) + 8 + (elipsisEnd ? 3 : 0);
*
* // finally, `a * 3 + 3` is the same thing as `(a + 1) * 3`
* // we can cast `elipsisEnd` to an integer `1 | 0`
* let stringLength = 3 * (maxBytes + i32(elipsisEnd)) + 8;
*/
let stringLength = 3 * (maxBytes + i32(elipsisEnd)) + 8;
// create the result
let result = __alloc(stringLength << 1, idof<String>());
// copy the 16 "<Buffer " bytes
memory.copy(
result,
changetype<usize>(BUFFER_INSPECT_HEADER_START),
changetype<BLOCK>(changetype<usize>(BUFFER_INSPECT_HEADER_START) - BLOCK_OVERHEAD).rtSize,
);
// Start writing at index 8
let writeOffset: usize = result + 16;
for (let i = 0; i < maxBytes; i++, writeOffset += 6) {
let byte = <u32>load<u8>(dataStart + <usize>i);
store<u32>(writeOffset, Buffer.HEX.charsFromByte(byte));
if (i == (maxBytes - 1)) {
if (elipsisEnd) {
// make this a single 64 bit store
store<u64>(writeOffset, <u64>0x003e_002e_002e_002e, 4); // "...>"
} else {
store<u16>(writeOffset, <u16>62, 4); // ">"
}
} else {
store<u16>(writeOffset, <u16>32, 4); // " "
}
}
return changetype<string>(result);
}
}
export namespace Buffer {
export namespace HEX {
/** Calculates the two char combination from the byte. */
@inline export function charsFromByte(byte: u32): u32 {
let top = (byte >>> 4) & 0xF;
let bottom = (0xF & byte);
top += select(0x57, 0x30, top > 9);
bottom += select(0x57, 0x30, bottom > 9);
return (bottom << 16) | top;
}
/** Calculates the byte length of the specified string when encoded as HEX. */
export function byteLength(str: string): i32 {
let ptr = changetype<usize>(str);
let byteCount = changetype<BLOCK>(changetype<usize>(str) - BLOCK_OVERHEAD).rtSize;
let length = byteCount >> 2;
// The string length must be even because the bytes come in pairs of characters two wide
if (byteCount & 0x3) return 0; // encoding fails and returns an empty ArrayBuffer
byteCount += ptr;
while (ptr < byteCount) {
var char = load<u16>(ptr);
if ( ((char - 0x30) <= 0x9)
|| ((char - 0x61) <= 0x5)
|| ((char - 0x41) <= 0x5)) {
ptr += 2;
continue;
} else {
return 0;
}
}
return length;
}
/** Creates an ArrayBuffer from a given string that is encoded in the HEX format. */
export function encode(str: string): ArrayBuffer {
let bufferLength = byteLength(str);
// short path: string is not a valid hex string, return a new empty ArrayBuffer
if (bufferLength == 0) return changetype<ArrayBuffer>(__alloc(0, idof<ArrayBuffer>()));
// long path: loop over each enociding pair and perform the conversion
let ptr = changetype<usize>(str);
let byteEnd = changetype<BLOCK>(changetype<usize>(str) - BLOCK_OVERHEAD).rtSize + ptr;
let result = __alloc(bufferLength, idof<ArrayBuffer>());
let b: u32 = 0;
let outChar = 0;
for (let i: usize = 0; ptr < byteEnd; i++) {
let odd = i & 1;
if (odd) {
outChar <<= 4;
b >>>= 16;
if ((b - 0x30) <= 9) {
outChar |= b - 0x30;
} else if ((b - 0x61) <= 0x5) {
outChar |= b - 0x57;
} else if (b - 0x41 <= 0x5) {
outChar |= b - 0x37;
}
store<u8>(result + (i >> 1), <u8>(outChar & 0xFF));
ptr += 4;
} else {
b = load<u32>(ptr);
outChar <<= 4;
let c = b & 0xFF;
if ((c - 0x30) <= 9) {
outChar |= c - 0x30;
} else if ((c - 0x61) <= 0x5) {
outChar |= c - 0x57;
} else if (c - 0x41 <= 0x5) {
outChar |= c - 0x37;
}
}
}
return changetype<ArrayBuffer>(result);
}
/** Creates a string from a given ArrayBuffer that is decoded into hex format. */
export function decode(buff: ArrayBuffer): string {
return decodeUnsafe(changetype<usize>(buff), buff.byteLength);
}
/** Decodes a chunk of memory to a utf16le encoded string in hex format. */
@unsafe export function decodeUnsafe(ptr: usize, length: i32): string {
let stringByteLength = length << 2; // length * (2 bytes per char) * (2 chars per input byte)
let result = __alloc(stringByteLength, idof<String>());
let i = <usize>0;
let inputByteLength = <usize>length + ptr;
// loop over each byte and store a `u32` for each one
while (ptr < inputByteLength) {
store<u32>(result + i, charsFromByte(<u32>load<u8>(ptr)));
i += 4;
ptr++;
}
return changetype<string>(result);
}
}
}