Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data. Binary-parser dynamically generates and compiles the parser code
on-the-fly, which runs as fast as a hand-written parser (which takes much more
time and effort to write). Supported data types are:

- [Integers](#uint8-16-32-64le-bename-options) (8, 16, 32 and 64 bit signed
- [Integers](#uint8-16-24-32-48-64le-bename-options) (8, 16, 24, 32, 48 and 64 bit signed
and unsigned integers)
- [Floating point numbers](#float-doublele-bename-options) (32 and 64 bit
floating point values)
Expand Down Expand Up @@ -102,13 +102,13 @@ associated parser code is compiled on-the-fly and internally cached.
Set the constructor function that should be called to create the object
returned from the `parse` method.

### [u]int{8, 16, 32, 64}{le, be}(name[, options])
### [u]int{8, 16, 24, 32, 48, 64}{le, be}(name[, options])
Parse bytes as an integer and store it in a variable named `name`. `name`
should consist only of alphanumeric characters and start with an alphabet.
Number of bits can be chosen from 8, 16, 32 and 64. Byte-ordering can be either
`le` for little endian or `be` for big endian. With no prefix, it parses as a
signed number, with `u` prefix as an unsigned number. The runtime type
returned by the 8, 16, 32 bit methods is `number` while the type
Number of bits can be chosen from 8, 16, 24, 32, 48 and 64. Byte-ordering can
be either `le` for little endian or `be` for big endian. With no prefix, it
parses as a signed number, with `u` prefix as an unsigned number. The runtime
type returned by the 8, 16, 24, 32, 48 bit methods is `number` while the type
returned by the 64 bit is `bigint`.

```javascript
Expand Down
142 changes: 122 additions & 20 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,23 @@ type PrimitiveTypes =
| "uint8"
| "uint16le"
| "uint16be"
| "uint24le"
| "uint24be"
| "uint32le"
| "uint32be"
| "uint48le"
| "uint48be"
| "uint64le"
| "uint64be"
| "int8"
| "int16le"
| "int16be"
| "int24le"
| "int24be"
| "int32le"
| "int32be"
| "int48le"
| "int48be"
| "int64le"
| "int64be"
| "floatle"
Expand All @@ -181,10 +189,14 @@ type PrimitiveTypes =
type PrimitiveTypesWithoutEndian =
| "uint8"
| "uint16"
| "uint24"
| "uint32"
| "int8"
| "int16"
| "int24"
| "int32"
| "int48"
| "uint48"
| "int64"
| "uint64";

Expand Down Expand Up @@ -226,13 +238,21 @@ const PRIMITIVE_SIZES: { [key in PrimitiveTypes]: number } = {
uint8: 1,
uint16le: 2,
uint16be: 2,
uint24le: 3,
uint24be: 3,
uint32le: 4,
uint32be: 4,
int8: 1,
int16le: 2,
int16be: 2,
int24le: 3,
int24be: 3,
int32le: 4,
int32be: 4,
int48be: 6,
int48le: 6,
uint48be: 6,
uint48le: 6,
int64be: 8,
int64le: 8,
uint64be: 8,
Expand All @@ -247,13 +267,21 @@ const PRIMITIVE_NAMES: { [key in PrimitiveTypes]: string } = {
uint8: "Uint8",
uint16le: "Uint16",
uint16be: "Uint16",
uint24le: "Uint24",
uint24be: "Uint24",
uint32le: "Uint32",
uint32be: "Uint32",
int8: "Int8",
int16le: "Int16",
int16be: "Int16",
int24le: "Int24",
int24be: "Int24",
int32le: "Int32",
int32be: "Int32",
int48be: "Int48",
int48le: "Int48",
uint48be: "Uint48",
uint48le: "Uint48",
int64be: "BigInt64",
int64le: "BigInt64",
uint64be: "BigUint64",
Expand All @@ -268,13 +296,21 @@ const PRIMITIVE_LITTLE_ENDIANS: { [key in PrimitiveTypes]: boolean } = {
uint8: false,
uint16le: true,
uint16be: false,
uint24le: true,
uint24be: false,
uint32le: true,
uint32be: false,
int8: false,
int16le: true,
int16be: false,
int24le: true,
int24be: false,
int32le: true,
int32be: false,
int48be: false,
int48le: true,
uint48be: false,
uint48le: true,
int64be: false,
int64le: true,
uint64be: false,
Expand All @@ -285,6 +321,32 @@ const PRIMITIVE_LITTLE_ENDIANS: { [key in PrimitiveTypes]: boolean } = {
doublebe: false,
};

// Returns an expression that reads a value of the given type at offset.
// DataView has no 24/48-bit getters, so compose them from narrower reads.
// 48-bit values fit in a safe integer, so they are returned as number.
function readPrimitive(type: PrimitiveTypes): string {
switch (type) {
case "uint48be":
return "(dataView.getUint16(offset) * 0x100000000 + dataView.getUint32(offset + 2))";
case "int48be":
return "(dataView.getInt16(offset) * 0x100000000 + dataView.getUint32(offset + 2))";
case "uint48le":
return "(dataView.getUint32(offset, true) + dataView.getUint16(offset + 4, true) * 0x100000000)";
case "int48le":
return "(dataView.getUint32(offset, true) + dataView.getInt16(offset + 4, true) * 0x100000000)";
case "uint24be":
return "(dataView.getUint16(offset) << 8 | dataView.getUint8(offset + 2))";
case "int24be":
return "(dataView.getInt16(offset) << 8 | dataView.getUint8(offset + 2))";
case "uint24le":
return "(dataView.getUint16(offset, true) | dataView.getUint8(offset + 2) << 16)";
case "int24le":
return "(dataView.getUint16(offset, true) | dataView.getInt8(offset + 2) << 16)";
default:
return `dataView.get${PRIMITIVE_NAMES[type]}(offset, ${PRIMITIVE_LITTLE_ENDIANS[type]})`;
}
}

// Encodings that TextDecoder doesn't support, decoded like Node.js Buffer
const BYTE_ENCODINGS = ["hex", "binary", "base64", "base64url"];

Expand Down Expand Up @@ -321,12 +383,8 @@ export class Parser {
}

private primitiveGenerateN(type: PrimitiveTypes, ctx: Context) {
const typeName = PRIMITIVE_NAMES[type];
const littleEndian = PRIMITIVE_LITTLE_ENDIANS[type];
ctx.pushCode(
`${ctx.generateVariable(
this.varName,
)} = dataView.get${typeName}(offset, ${littleEndian});`,
`${ctx.generateVariable(this.varName)} = ${readPrimitive(type)};`,
);
ctx.pushCode(`offset += ${PRIMITIVE_SIZES[type]};`);
}
Expand Down Expand Up @@ -359,6 +417,18 @@ export class Parser {
return this.primitiveN("uint16be", varName, options);
}

uint24(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("uint24"), varName, options);
}

uint24le(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("uint24le", varName, options);
}

uint24be(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("uint24be", varName, options);
}

uint32(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("uint32"), varName, options);
}
Expand All @@ -371,6 +441,18 @@ export class Parser {
return this.primitiveN("uint32be", varName, options);
}

uint48(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("uint48"), varName, options);
}

uint48le(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("uint48le", varName, options);
}

uint48be(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("uint48be", varName, options);
}

int8(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("int8", varName, options);
}
Expand All @@ -387,6 +469,18 @@ export class Parser {
return this.primitiveN("int16be", varName, options);
}

int24(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("int24"), varName, options);
}

int24le(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("int24le", varName, options);
}

int24be(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("int24be", varName, options);
}

int32(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("int32"), varName, options);
}
Expand All @@ -399,6 +493,18 @@ export class Parser {
return this.primitiveN("int32be", varName, options);
}

int48(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("int48"), varName, options);
}

int48le(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("int48le", varName, options);
}

int48be(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("int48be", varName, options);
}

int64(varName: string, options: ParserOptions = {}): this {
return this.primitiveN(this.useThisEndian("int64"), varName, options);
}
Expand Down Expand Up @@ -1015,11 +1121,19 @@ export class Parser {
case "uint8":
case "uint16le":
case "uint16be":
case "uint24le":
case "uint24be":
case "uint32le":
case "uint32be":
case "int8":
case "int16le":
case "int16be":
case "int24le":
case "int24be":
case "uint48le":
case "uint48be":
case "int48le":
case "int48be":
case "int32le":
case "int32be":
case "int64be":
Expand Down Expand Up @@ -1374,11 +1488,7 @@ export class Parser {

if (typeof type === "string") {
if (!aliasRegistry.get(type)) {
const typeName = PRIMITIVE_NAMES[type as PrimitiveTypes];
const littleEndian = PRIMITIVE_LITTLE_ENDIANS[type as PrimitiveTypes];
ctx.pushCode(
`var ${item} = dataView.get${typeName}(offset, ${littleEndian});`,
);
ctx.pushCode(`var ${item} = ${readPrimitive(type as PrimitiveTypes)};`);
ctx.pushCode(`offset += ${PRIMITIVE_SIZES[type as PrimitiveTypes]};`);
} else {
const tempVar = ctx.generateTmpVariable();
Expand Down Expand Up @@ -1457,11 +1567,7 @@ export class Parser {
if (typeof type === "string") {
const fieldVar = ctx.generateVariable(this.varName);
if (!aliasRegistry.has(type)) {
const typeName = PRIMITIVE_NAMES[type as PrimitiveTypes];
const littleEndian = PRIMITIVE_LITTLE_ENDIANS[type as PrimitiveTypes];
ctx.pushCode(
`${fieldVar} = dataView.get${typeName}(offset, ${littleEndian});`,
);
ctx.pushCode(`${fieldVar} = ${readPrimitive(type as PrimitiveTypes)};`);
ctx.pushCode(`offset += ${PRIMITIVE_SIZES[type as PrimitiveTypes]}`);
} else {
const tempVar = ctx.generateTmpVariable();
Expand Down Expand Up @@ -1688,11 +1794,7 @@ export class Parser {
ctx.addReference(this.options.type!);
}
} else if (Object.keys(PRIMITIVE_SIZES).indexOf(this.options.type!) >= 0) {
const typeName = PRIMITIVE_NAMES[type as PrimitiveTypes];
const littleEndian = PRIMITIVE_LITTLE_ENDIANS[type as PrimitiveTypes];
ctx.pushCode(
`${nestVar} = dataView.get${typeName}(offset, ${littleEndian});`,
);
ctx.pushCode(`${nestVar} = ${readPrimitive(type as PrimitiveTypes)};`);
ctx.pushCode(`offset += ${PRIMITIVE_SIZES[type as PrimitiveTypes]};`);
}

Expand Down
Loading