Skip to content

Commit

Permalink
fix(encoding/ascii85): fix encode() returns a wrong result with a s…
Browse files Browse the repository at this point in the history
…ubarray (#3310)
  • Loading branch information
babiabeo authored Apr 11, 2023
1 parent c05ce92 commit 7d63d59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion encoding/ascii85.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function encode(uint8: Uint8Array, options?: Ascii85Options): string {
uint8 = new Uint8Array(tmp.length + difference);
uint8.set(tmp);
}
const view = new DataView(uint8.buffer);
const view = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
for (let i = 0, len = uint8.length; i < len; i += 4) {
v = view.getUint32(i);
// Adobe and btoa standards compress 4 zeroes to single "z" character
Expand Down
16 changes: 16 additions & 0 deletions encoding/ascii85_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,19 @@ for (const [standard, tests] of Object.entries(testCasesDelimiter)) {
},
});
}

Deno.test({
name: `[encoding/ascii85] encode subarray of an Uint8Array`,
fn() {
const data1 = new Uint8Array([0x73, 0x70, 0x61, 0x6d]);
const data2 = new Uint8Array(
[0x01, 0x02, 0x03, 0x04, 0x73, 0x70, 0x61, 0x6d],
);

const encoded1 = encode(data1);
const encoded2 = encode(data2.subarray(4));

assertEquals(encoded1, "F)YQ)");
assertEquals(encoded2, "F)YQ)");
},
});

0 comments on commit 7d63d59

Please sign in to comment.