forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.ts
266 lines (249 loc) · 7.68 KB
/
binary.ts
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
type RawBaseType = "int8" | "int16" | "int32" | "uint8" | "uint16" | "uint32";
type RawNumberType = RawBaseType | "float32" | "float64";
type RawBigType = RawBaseType | "int64" | "uint64";
export type DataType = RawNumberType | RawBigType;
/** How encoded binary data is ordered. */
export type Endianness = "little" | "big";
/** Options for working with the `number` type. */
export interface VarnumOptions {
/** The binary format used. */
dataType?: RawNumberType;
/** The binary encoding order used. */
endian?: Endianness;
}
/** Options for working with the `bigint` type. */
export interface VarbigOptions {
/** The binary format used. */
dataType?: RawBigType;
/** The binary encoding order used. */
endian?: Endianness;
}
const rawTypeSizes: Record<DataType, number> = {
int8: 1,
uint8: 1,
int16: 2,
uint16: 2,
int32: 4,
uint32: 4,
int64: 8,
uint64: 8,
float32: 4,
float64: 8,
} as const;
/** Number of bytes required to store `dataType`. */
export function sizeof(dataType: DataType): number {
return rawTypeSizes[dataType];
}
/** Reads `n` bytes from `r`.
*
* Resolves it in a `Uint8Array`, or throws `Deno.errors.UnexpectedEof` if `n` bytes cannot be read. */
export async function getNBytes(
r: Deno.Reader,
n: number,
): Promise<Uint8Array> {
const scratch = new Uint8Array(n);
const nRead = await r.read(scratch);
if (nRead === null || nRead < n) throw new Deno.errors.UnexpectedEof();
return scratch;
}
/** Decodes a number from `b`. If `o.bytes` is shorter than `sizeof(o.dataType)`, returns `null`.
*
* `o.dataType` defaults to `"int32"`. */
export function varnum(b: Uint8Array, o: VarnumOptions = {}): number | null {
o.dataType = o.dataType ?? "int32";
const littleEndian = (o.endian ?? "big") === "little" ? true : false;
if (b.length < sizeof(o.dataType)) return null;
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
return view.getInt8(0);
case "uint8":
return view.getUint8(0);
case "int16":
return view.getInt16(0, littleEndian);
case "uint16":
return view.getUint16(0, littleEndian);
case "int32":
return view.getInt32(0, littleEndian);
case "uint32":
return view.getUint32(0, littleEndian);
case "float32":
return view.getFloat32(0, littleEndian);
case "float64":
return view.getFloat64(0, littleEndian);
}
}
/** Decodes a bigint from `b`. If `o.bytes` is shorter than `sizeof(o.dataType)`, returns `null`.
*
* `o.dataType` defaults to `"int64"`. */
export function varbig(b: Uint8Array, o: VarbigOptions = {}): bigint | null {
o.dataType = o.dataType ?? "int64";
const littleEndian = (o.endian ?? "big") === "little" ? true : false;
if (b.length < sizeof(o.dataType)) return null;
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
return BigInt(view.getInt8(0));
case "uint8":
return BigInt(view.getUint8(0));
case "int16":
return BigInt(view.getInt16(0, littleEndian));
case "uint16":
return BigInt(view.getUint16(0, littleEndian));
case "int32":
return BigInt(view.getInt32(0, littleEndian));
case "uint32":
return BigInt(view.getUint32(0, littleEndian));
case "int64":
return view.getBigInt64(0, littleEndian);
case "uint64":
return view.getBigUint64(0, littleEndian);
}
}
/** Encodes number `x` into `b`. Returns the number of bytes used, or `0` if `b` is shorter than `sizeof(o.dataType)`.
*
* `o.dataType` defaults to `"int32"`. */
export function putVarnum(
b: Uint8Array,
x: number,
o: VarnumOptions = {},
): number {
o.dataType = o.dataType ?? "int32";
const littleEndian = (o.endian ?? "big") === "little" ? true : false;
if (b.length < sizeof(o.dataType)) return 0;
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
view.setInt8(0, x);
break;
case "uint8":
view.setUint8(0, x);
break;
case "int16":
view.setInt16(0, x, littleEndian);
break;
case "uint16":
view.setUint16(0, x, littleEndian);
break;
case "int32":
view.setInt32(0, x, littleEndian);
break;
case "uint32":
view.setUint32(0, x, littleEndian);
break;
case "float32":
view.setFloat32(0, x, littleEndian);
break;
case "float64":
view.setFloat64(0, x, littleEndian);
break;
}
return sizeof(o.dataType);
}
/** Encodes bigint `x` into `b`. Returns the number of bytes used, or `0` if `b` is shorter than `sizeof(o.dataType)`.
*
* `o.dataType` defaults to `"int64"`. */
export function putVarbig(
b: Uint8Array,
x: bigint,
o: VarbigOptions = {},
): number {
o.dataType = o.dataType ?? "int64";
const littleEndian = (o.endian ?? "big") === "little" ? true : false;
if (b.length < sizeof(o.dataType)) return 0;
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
view.setInt8(0, Number(x));
break;
case "uint8":
view.setUint8(0, Number(x));
break;
case "int16":
view.setInt16(0, Number(x), littleEndian);
break;
case "uint16":
view.setUint16(0, Number(x), littleEndian);
break;
case "int32":
view.setInt32(0, Number(x), littleEndian);
break;
case "uint32":
view.setUint32(0, Number(x), littleEndian);
break;
case "int64":
view.setBigInt64(0, x, littleEndian);
break;
case "uint64":
view.setBigUint64(0, x, littleEndian);
break;
}
return sizeof(o.dataType);
}
/** Decodes a number from `r`, consuming `sizeof(o.dataType)` bytes. If less than `sizeof(o.dataType)` bytes were read, throws `Deno.errors.unexpectedEof`.
*
* `o.dataType` defaults to `"int32"`. */
export async function readVarnum(
r: Deno.Reader,
o: VarnumOptions = {},
): Promise<number> {
o.dataType = o.dataType ?? "int32";
const scratch = await getNBytes(r, sizeof(o.dataType));
return varnum(scratch, o) as number;
}
/** Decodes a bigint from `r`, consuming `sizeof(o.dataType)` bytes. If less than `sizeof(o.dataType)` bytes were read, throws `Deno.errors.unexpectedEof`.
*
* `o.dataType` defaults to `"int64"`. */
export async function readVarbig(
r: Deno.Reader,
o: VarbigOptions = {},
): Promise<bigint> {
o.dataType = o.dataType ?? "int64";
const scratch = await getNBytes(r, sizeof(o.dataType));
return varbig(scratch, o) as bigint;
}
/** Encodes and writes `x` to `w`. Resolves to the number of bytes written.
*
* `o.dataType` defaults to `"int32"`. */
export function writeVarnum(
w: Deno.Writer,
x: number,
o: VarnumOptions = {},
): Promise<number> {
o.dataType = o.dataType ?? "int32";
const scratch = new Uint8Array(sizeof(o.dataType));
putVarnum(scratch, x, o);
return w.write(scratch);
}
/** Encodes and writes `x` to `w`. Resolves to the number of bytes written.
*
* `o.dataType` defaults to `"int64"`. */
export function writeVarbig(
w: Deno.Writer,
x: bigint,
o: VarbigOptions = {},
): Promise<number> {
o.dataType = o.dataType ?? "int64";
const scratch = new Uint8Array(sizeof(o.dataType));
putVarbig(scratch, x, o);
return w.write(scratch);
}
/** Encodes `x` into a new `Uint8Array`.
*
* `o.dataType` defaults to `"int32"` */
export function varnumBytes(x: number, o: VarnumOptions = {}): Uint8Array {
o.dataType = o.dataType ?? "int32";
const b = new Uint8Array(sizeof(o.dataType));
putVarnum(b, x, o);
return b;
}
/** Encodes `x` into a new `Uint8Array`.
*
* `o.dataType` defaults to `"int64"` */
export function varbigBytes(x: bigint, o: VarbigOptions = {}): Uint8Array {
o.dataType = o.dataType ?? "int64";
const b = new Uint8Array(sizeof(o.dataType));
putVarbig(b, x, o);
return b;
}