Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix / Inconsistency: Respect Uint8Array offsets #826

Merged
merged 1 commit into from
May 31, 2021
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
64 changes: 32 additions & 32 deletions encoding/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ export function varnum(b: Uint8Array, o: VarnumOptions = {}): number | null {
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
return view.getInt8(0);
return view.getInt8(b.byteOffset);
case "uint8":
return view.getUint8(0);
return view.getUint8(b.byteOffset);
case "int16":
return view.getInt16(0, littleEndian);
return view.getInt16(b.byteOffset, littleEndian);
case "uint16":
return view.getUint16(0, littleEndian);
return view.getUint16(b.byteOffset, littleEndian);
case "int32":
return view.getInt32(0, littleEndian);
return view.getInt32(b.byteOffset, littleEndian);
case "uint32":
return view.getUint32(0, littleEndian);
return view.getUint32(b.byteOffset, littleEndian);
case "float32":
return view.getFloat32(0, littleEndian);
return view.getFloat32(b.byteOffset, littleEndian);
case "float64":
return view.getFloat64(0, littleEndian);
return view.getFloat64(b.byteOffset, littleEndian);
}
}

Expand All @@ -93,21 +93,21 @@ export function varbig(b: Uint8Array, o: VarbigOptions = {}): bigint | null {
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
return BigInt(view.getInt8(0));
return BigInt(view.getInt8(b.byteOffset));
case "uint8":
return BigInt(view.getUint8(0));
return BigInt(view.getUint8(b.byteOffset));
case "int16":
return BigInt(view.getInt16(0, littleEndian));
return BigInt(view.getInt16(b.byteOffset, littleEndian));
case "uint16":
return BigInt(view.getUint16(0, littleEndian));
return BigInt(view.getUint16(b.byteOffset, littleEndian));
case "int32":
return BigInt(view.getInt32(0, littleEndian));
return BigInt(view.getInt32(b.byteOffset, littleEndian));
case "uint32":
return BigInt(view.getUint32(0, littleEndian));
return BigInt(view.getUint32(b.byteOffset, littleEndian));
case "int64":
return view.getBigInt64(0, littleEndian);
return view.getBigInt64(b.byteOffset, littleEndian);
case "uint64":
return view.getBigUint64(0, littleEndian);
return view.getBigUint64(b.byteOffset, littleEndian);
}
}

Expand All @@ -125,28 +125,28 @@ export function putVarnum(
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
view.setInt8(0, x);
view.setInt8(b.byteOffset, x);
break;
case "uint8":
view.setUint8(0, x);
view.setUint8(b.byteOffset, x);
break;
case "int16":
view.setInt16(0, x, littleEndian);
view.setInt16(b.byteOffset, x, littleEndian);
break;
case "uint16":
view.setUint16(0, x, littleEndian);
view.setUint16(b.byteOffset, x, littleEndian);
break;
case "int32":
view.setInt32(0, x, littleEndian);
view.setInt32(b.byteOffset, x, littleEndian);
break;
case "uint32":
view.setUint32(0, x, littleEndian);
view.setUint32(b.byteOffset, x, littleEndian);
break;
case "float32":
view.setFloat32(0, x, littleEndian);
view.setFloat32(b.byteOffset, x, littleEndian);
break;
case "float64":
view.setFloat64(0, x, littleEndian);
view.setFloat64(b.byteOffset, x, littleEndian);
break;
}
return sizeof(o.dataType);
Expand All @@ -166,28 +166,28 @@ export function putVarbig(
const view = new DataView(b.buffer);
switch (o.dataType) {
case "int8":
view.setInt8(0, Number(x));
view.setInt8(b.byteOffset, Number(x));
break;
case "uint8":
view.setUint8(0, Number(x));
view.setUint8(b.byteOffset, Number(x));
break;
case "int16":
view.setInt16(0, Number(x), littleEndian);
view.setInt16(b.byteOffset, Number(x), littleEndian);
break;
case "uint16":
view.setUint16(0, Number(x), littleEndian);
view.setUint16(b.byteOffset, Number(x), littleEndian);
break;
case "int32":
view.setInt32(0, Number(x), littleEndian);
view.setInt32(b.byteOffset, Number(x), littleEndian);
break;
case "uint32":
view.setUint32(0, Number(x), littleEndian);
view.setUint32(b.byteOffset, Number(x), littleEndian);
break;
case "int64":
view.setBigInt64(0, x, littleEndian);
view.setBigInt64(b.byteOffset, x, littleEndian);
break;
case "uint64":
view.setBigUint64(0, x, littleEndian);
view.setBigUint64(b.byteOffset, x, littleEndian);
break;
}
return sizeof(o.dataType);
Expand Down
7 changes: 7 additions & 0 deletions encoding/binary_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,10 @@ Deno.test("testVarnumBytesLittleEndian", function (): void {
const rslt = varnumBytes(0x04030201, { endian: "little" });
assertEquals(rslt, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
});

Deno.test("testVarnumSubarray", function (): void {
const data = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
const sub = data.subarray(2, 4);
const rslt = varnum(sub, { dataType: "uint8" });
assertEquals(rslt, 3);
});