Skip to content

Commit

Permalink
Replace binary with latin1 for ISO/IEC 8859-1 text decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Jun 29, 2024
1 parent a49df48 commit 2a3744d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/asf/AsfUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Buffer } from 'node:buffer';
import * as util from '../common/Util.js';
import { DataType } from './AsfObject.js';

export type AttributeParser = (buf: Buffer) => boolean | string | number | bigint | Buffer;
export type AttributeParser = (buf: Uint8Array) => boolean | string | number | bigint | Uint8Array;

export class AsfUtil {

Expand Down
2 changes: 1 addition & 1 deletion lib/id3v1/ID3v1Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function hasID3v1Header(reader: IRandomReader): Promise<boolean> {
if (reader.fileSize >= 128) {
const tag = Buffer.alloc(3);
await reader.randomRead(tag, 0, tag.length, reader.fileSize - 128);
return tag.toString('binary') === 'TAG';
return tag.toString('latin1') === 'TAG';
}
return false;
}
2 changes: 1 addition & 1 deletion lib/lyrics3/Lyrics3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function getLyricsHeaderLength(reader: IRandomReader): Promise<numb
if (reader.fileSize >= 143) {
const buf = Buffer.alloc(15);
await reader.randomRead(buf, 0, buf.length, reader.fileSize - 143);
const txt = buf.toString('binary');
const txt = buf.toString('latin1');
const tag = txt.substr(6);
if (tag === endTag2) {
return parseInt(txt.substr(0, 6), 10) + 15;
Expand Down
2 changes: 1 addition & 1 deletion lib/riff/RiffChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const Header: IGetToken<IChunkHeader> = {
get: (buf: Buffer, off): IChunkHeader => {
return {
// Group-ID
chunkID: buf.toString('binary', off, off + 4),
chunkID: buf.toString('latin1', off, off + 4),
// Size
chunkSize: Token.UINT32_LE.get(buf, 4)
};
Expand Down
16 changes: 8 additions & 8 deletions test/test-file-asf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,41 @@ describe('Parse ASF', () => {
*/
it('should be able to roughly decode a 64-bit QWord', () => {

const tests: { raw: string, expected: number, description: string }[] = [
const tests: { raw: number[], expected: number, description: string }[] = [
{
raw: '\xFF\x00\x00\x00\x00\x00\x00\x00',
raw: [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
expected: 0xFF,
description: '8-bit'
},
{
raw: '\xFF\xFF\x00\x00\x00\x00\x00\x00',
raw: [0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
expected: 0xFFFF,
description: '16-bit'
},
{
raw: '\xFF\xFF\xFF\xFF\x00\x00\x00\x00',
raw: [0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00],
expected: 0xFFFFFFFF,
description: '32-bit'
},
{
raw: '\xFF\xFF\xFF\xFF\xFF\x00\x00\x00',
raw: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00],
expected: 0xFFFFFFFFFF,
description: '40-bit'
},
{
raw: '\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00',
raw: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00],
expected: 0xFFFFFFFFFFFF,
description: '48-bit'
},
{
raw: '\xFF\xFF\xFF\xFF\xFF\xFF\x0F\x00',
raw: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00],
expected: 0xFFFFFFFFFFFFF,
description: '52-bit'
}
];

tests.forEach(test => {
const buf = Buffer.from(test.raw, 'binary');
const buf = Uint8Array.from(test.raw);
assert.strictEqual(Number(AsfUtil.getParserForAttr(DataType.QWord)(buf)), test.expected, test.description);
});

Expand Down

0 comments on commit 2a3744d

Please sign in to comment.