diff --git a/README.md b/README.md index d5e24b4..59776d4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/lib/binary_parser.ts b/lib/binary_parser.ts index 31c3e9f..ab38a53 100644 --- a/lib/binary_parser.ts +++ b/lib/binary_parser.ts @@ -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(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); } @@ -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) && @@ -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]; @@ -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) && diff --git a/test/composite_parser.ts b/test/composite_parser.ts index bac7cec..5e0ebbe 100644 --- a/test/composite_parser.ts +++ b/test/composite_parser.ts @@ -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" }) @@ -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") @@ -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",