diff --git a/README.md b/README.md index a98d0ee..42a7144 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/lib/binary_parser.ts b/lib/binary_parser.ts index 91e527d..fb12a34 100644 --- a/lib/binary_parser.ts +++ b/lib/binary_parser.ts @@ -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" @@ -181,10 +189,14 @@ type PrimitiveTypes = type PrimitiveTypesWithoutEndian = | "uint8" | "uint16" + | "uint24" | "uint32" | "int8" | "int16" + | "int24" | "int32" + | "int48" + | "uint48" | "int64" | "uint64"; @@ -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, @@ -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", @@ -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, @@ -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"]; @@ -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]};`); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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": @@ -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(); @@ -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(); @@ -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]};`); } diff --git a/test/primitive_parser.ts b/test/primitive_parser.ts index 1e28774..b2ebeaf 100644 --- a/test/primitive_parser.ts +++ b/test/primitive_parser.ts @@ -38,6 +38,99 @@ function primitiveParserTests( c: 12345678, }); }); + describe("24-bit integer parsers", () => { + // https://github.com/keichi/binary-parser/issues/93 + const buf = hexToBuf("f959a61a57c80fd3004801a9"); + + it("should parse big endian 24-bit integers", () => { + const parser = Parser.start() + .int24be("a") + .uint24be("b") + .int24be("c") + .uint24be("d"); + expect(parser.parse(buf)).toStrictEqual({ + a: -435802, + b: 1726408, + c: 1037056, + d: 4719017, + }); + }); + + it("should parse little endian 24-bit integers", () => { + const parser = Parser.start() + .int24le("a") + .uint24le("b") + .int24le("c") + .uint24le("d"); + expect(parser.parse(buf)).toStrictEqual({ + a: -5875207, + b: 13129498, + c: 54031, + d: 11075912, + }); + }); + + it("should follow endianness()", () => { + const be = Parser.start().int24("a").uint24("b"); + const le = Parser.start().endianness("little").int24("a").uint24("b"); + expect(be.parse(buf)).toStrictEqual({ a: -435802, b: 1726408 }); + expect(le.parse(buf)).toStrictEqual({ a: -5875207, b: 13129498 }); + }); + + it("should parse arrays of 24-bit integers", () => { + const parser = Parser.start() + .array("a", { type: "int24be", length: 2 }) + .array("b", { type: "uint24le", length: 2 }); + expect(parser.parse(buf)).toStrictEqual({ + a: [-435802, 1726408], + b: [54031, 11075912], + }); + expect(parser.sizeOf()).toBe(12); + }); + }); + describe("48-bit integer parsers", () => { + const buf = hexToBuf("f959a61a57c80fd3004801a9"); + + it("should parse big endian 48-bit integers", () => { + const parser = Parser.start().int48be("a").uint48be("b"); + expect(parser.parse(buf)).toStrictEqual({ + a: -7311542560824, + b: 17398917235113, + }); + }); + + it("should parse little endian 48-bit integers", () => { + const parser = Parser.start().int48le("a").uint48le("b"); + expect(parser.parse(buf)).toStrictEqual({ + a: -61198541891079, + b: 185822968075023, + }); + }); + + it("should follow endianness()", () => { + const be = Parser.start().int48("a").uint48("b"); + const le = Parser.start().endianness("little").int48("a").uint48("b"); + expect(be.parse(buf)).toStrictEqual({ + a: -7311542560824, + b: 17398917235113, + }); + expect(le.parse(buf)).toStrictEqual({ + a: -61198541891079, + b: 185822968075023, + }); + }); + + it("should parse arrays of 48-bit integers", () => { + const parser = Parser.start() + .array("a", { type: "uint48be", length: 1 }) + .array("b", { type: "int48le", length: 1 }); + expect(parser.parse(buf)).toStrictEqual({ + a: [274163434149832], + b: [-95652008635633], + }); + expect(parser.sizeOf()).toBe(12); + }); + }); describe("BigInt64 parsers", () => { it("should parse uint64", () => { const parser = Parser.start().uint64be("a").uint64le("b");