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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ keys:

- `type` - (Required) Type of the array element. Can be a string or a user
defined `Parser` object. If it's a string, you have to choose from
[u]int{8, 16, 32, 64}{le, be}, {float, double}{le, be}, or an alias defined
with `namely()`.
[u]int{8, 16, 24, 32, 48, 64}{le, be}, {float, double}{le, be}, or an alias
defined with `namely()`. Without the `le`/`be` suffix, the byte-ordering set
by [`endianness()`](#endiannessendianness) is used.
- `length` - (either `length`, `lengthInBytes`, or `readUntil` is required)
Length of the array. Can be a number, string or a function. Use number for
statically sized arrays.
Expand Down Expand Up @@ -327,7 +328,8 @@ Useful for parsing binary formats such as ELF where the offset of a field is
pointed by another field.

- `type` - (Required) Can be a string `[u]int{8, 16, 32, 64}{le, be}`
or a user defined `Parser` object.
or a user defined `Parser` object. Without the `le`/`be` suffix, the
byte-ordering set by [`endianness()`](#endiannessendianness) is used.
- `offset` - (Required) Indicates absolute offset from the beginning of the
input buffer. Can be a number, string or a function.

Expand Down
29 changes: 29 additions & 0 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ export class Parser {
return (type + this.endian.toLowerCase()) as PrimitiveTypes;
}

// Appends this parser's endianness to type names without an endian suffix
// (e.g. "int32" -> "int32be"). Aliases and other values are left as is.
private resolveEndian<T>(type: T): T {
if (
typeof type === "string" &&
!aliasRegistry.has(type) &&
!(type in PRIMITIVE_SIZES) &&
type + this.endian in PRIMITIVE_SIZES
) {
return (type + this.endian) as T;
}
return type;
}

uint8(varName: string, options: ParserOptions = {}): this {
return this.primitiveN("uint8", varName, options);
}
Expand Down Expand Up @@ -805,6 +819,8 @@ export class Parser {
throw new Error("type is required for array.");
}

options = { ...options, type: this.resolveEndian(options.type) };

if (
typeof options.type === "string" &&
!aliasRegistry.has(options.type) &&
Expand Down Expand Up @@ -834,6 +850,17 @@ export class Parser {
throw new Error("choices is required for choice.");
}

options = {
...options,
choices: Object.fromEntries(
Object.entries(options.choices).map(([k, v]) => [
k,
this.resolveEndian(v),
]),
),
defaultChoice: this.resolveEndian(options.defaultChoice),
};

for (const keyString in options.choices) {
const key = parseInt(keyString, 10);
const value = options.choices[key];
Expand Down Expand Up @@ -898,6 +925,8 @@ export class Parser {
throw new Error("type is required for pointer.");
}

options = { ...options, type: this.resolveEndian(options.type) };

if (
typeof options.type === "string" &&
!(options.type in PRIMITIVE_SIZES) &&
Expand Down
42 changes: 42 additions & 0 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ function compositeParserTests(

describe(`Composite parser (${name})`, () => {
describe("Array parser", () => {
it("should resolve element type without endian suffix by endianness()", () => {
const buffer = factory([0x01, 0x00, 0x00, 0x02]);
expect(
Parser.start()
.array("a", { type: "uint16", length: 2 })
.parse(buffer),
).toStrictEqual({ a: [0x0100, 0x0002] });
expect(
Parser.start()
.endianness("little")
.array("a", { type: "uint16", length: 2 })
.parse(buffer),
).toStrictEqual({ a: [0x0001, 0x0200] });
});
it("should parse array with length 0", () => {
const parser = Parser.start()
.array("a", { length: 0, type: "uint8" })
Expand Down Expand Up @@ -531,6 +545,24 @@ function compositeParserTests(
});

describe("Choice parser", () => {
it("should resolve choice types without endian suffix by endianness()", () => {
const parser = Parser.start()
.endianness("little")
.uint8("tag")
.choice("data", {
tag: "tag",
choices: { 0: "uint16" },
defaultChoice: "int32",
});

expect(parser.parse(factory([0x0, 0x01, 0x00]))).toStrictEqual({
tag: 0,
data: 1,
});
expect(
parser.parse(factory([0x1, 0xff, 0xff, 0xff, 0xff])),
).toStrictEqual({ tag: 1, data: -1 });
});
it("should parse choices of primitive types", () => {
const parser = Parser.start()
.uint8("tag1")
Expand Down Expand Up @@ -1165,6 +1197,16 @@ function compositeParserTests(
});

describe("Pointer parser", () => {
it("should resolve type without endian suffix by endianness()", () => {
const parser = Parser.start()
.endianness("little")
.pointer("x", { type: "uint16", offset: 1 });

expect(parser.parse(factory([0x0, 0x01, 0x00]))).toStrictEqual({
x: 1,
});
});

it("should move pointer to specified offset", () => {
const parser = Parser.start().pointer("x", {
type: "uint8",
Expand Down