Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ export class Parser {
ctx.pushCode(`${varName} = buffer.subarray(${start}, offset);`);
} else if (this.options.readUntil === "eof") {
ctx.pushCode(`${varName} = buffer.subarray(offset);`);
ctx.pushCode("offset = buffer.length;");
} else {
const len = ctx.generateOption(this.options.length!);

Expand Down Expand Up @@ -1738,6 +1739,7 @@ export class Parser {
ctx.pushCode(`${wrappedBuf} = buffer.subarray(${start}, offset);`);
} else if (this.options.readUntil === "eof") {
ctx.pushCode(`${wrappedBuf} = buffer.subarray(offset);`);
ctx.pushCode(`offset = buffer.length;`);
} else {
const len = ctx.generateOption(this.options.length!);
ctx.pushCode(`${wrappedBuf} = buffer.subarray(offset, offset + ${len});`);
Expand Down
37 changes: 37 additions & 0 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,43 @@ function compositeParserTests(
});

describe("Wrapper", () => {
it("should advance to the original EOF after transforming the remainder", () => {
const makeParser = () =>
new Parser()
.uint8("prefix")
.wrapped("body", {
readUntil: "eof",
wrapper: (buffer: Uint8Array) => factory([buffer[0]]),
type: new Parser().uint8("value").saveOffset("innerOffset"),
})
.saveOffset("outerOffset");
const buffer = factory([99, 7, 8, 9, 100]).subarray(1, 4);
expect(makeParser().parse(buffer)).toStrictEqual({
prefix: 7,
body: { value: 8, innerOffset: 1 },
outerOffset: 3,
});
expect(() => makeParser().uint8("pastEnd").parse(buffer)).toThrow(
RangeError,
);
});

it("should consume an EOF wrapper inside an EOF array", () => {
const parser = new Parser().array("items", {
readUntil: "eof",
type: new Parser().wrapped("body", {
readUntil: "eof",
clone: true,
wrapper: (buffer: Uint8Array) => buffer,
type: new Parser().uint8("value"),
}),
});
expect(parser.parse(factory([7, 8, 9]))).toStrictEqual({
items: [{ body: { value: 7 } }],
});
expect(parser.parse(factory([]))).toStrictEqual({ items: [] });
});

it.skipIf(!isNode)(
"should parse deflated then inflated data",
async () => {
Expand Down
39 changes: 39 additions & 0 deletions test/primitive_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,45 @@ function primitiveParserTests(
const buffer = factory(new TextEncoder().encode("John\0Doe\0"));
expect(bufferParser.parse(buffer)).toStrictEqual({ buf: buffer });
});

it("should advance the offset after reading a buffer to eof", () => {
const parser = new Parser()
.uint8("header")
.buffer("data", { readUntil: "eof" })
.saveOffset("end");
const input = factory([0xff, 1, 2, 3, 0xff]).subarray(1, 4);

expect(parser.parse(input)).toStrictEqual({
header: 1,
data: factory([2, 3]),
end: 3,
});
expect(parser.parse(factory([1]))).toStrictEqual({
header: 1,
data: factory([]),
end: 1,
});
});

it("should consume an eof buffer inside an array element", () => {
const parser = new Parser().array("items", {
type: new Parser().buffer("data", { readUntil: "eof", clone: true }),
readUntil: "eof",
});

expect(parser.parse(factory([1, 2]))).toStrictEqual({
items: [{ data: factory([1, 2]) }],
});
expect(parser.parse(factory([]))).toStrictEqual({ items: [] });
});

it("should not reread consumed bytes after an eof buffer", () => {
const parser = new Parser()
.buffer("data", { readUntil: "eof" })
.uint8("after");

expect(() => parser.parse(factory([1, 2]))).toThrow(RangeError);
});
});

describe("Security", () => {
Expand Down