Skip to content

Commit

Permalink
Merge branch 'hotfix/ogg-bigint-comparison'
Browse files Browse the repository at this point in the history
  • Loading branch information
benrr101 committed Nov 10, 2022
2 parents a9313f7 + d27e711 commit 26997fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-taglib-sharp",
"description": "Read and write audio/video/picture tags using a similar interface to TagLib#",
"version": "5.0.0",
"version": "5.0.1",
"license": "LGPL-2.1-or-later",
"author": "Ben Russell <benrr101@outlook.com> (https://github.com/benrr101)",
"repository": "github:benrr101/node-taglib-sharp",
Expand Down
37 changes: 29 additions & 8 deletions src/ogg/oggPageHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ export enum OggPageFlags {
* This structure provides a representation of an Ogg page header.
*/
export class OggPageHeader {
public static readonly MINIMUM_SIZE = 27;
public static readonly HEADER_IDENTIFIER = ByteVector.fromString("OggS", StringType.Latin1).makeReadOnly();
public static readonly MINIMUM_SIZE = 27;
public static readonly NO_PACKETS_END_IN_PAGE = ByteVector.fromByteArray([
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF
]);

private _absoluteGranularPosition: number;
private _dataSize: number;
Expand Down Expand Up @@ -64,13 +74,20 @@ export class OggPageHeader {
header._version = data.get(4);
header._flags = data.get(5);

const absoluteGranularPosition = data.subarray(6, 8).toUlong(false);
if (absoluteGranularPosition > Number.MAX_SAFE_INTEGER) {
throw new UnsupportedFormatError(
"Granular position is too large to be handled with this version of node-taglib-sharp"
);
const absoluteGranularPositionBytes = data.subarray(6, 8);
if (absoluteGranularPositionBytes.equals(this.NO_PACKETS_END_IN_PAGE)) {
// Handle special condition where no packets end in the current page
header._absoluteGranularPosition = -1;
} else {
const absoluteGranularPosition = absoluteGranularPositionBytes.toUlong(false);
if (absoluteGranularPosition > Number.MAX_SAFE_INTEGER ) {
throw new UnsupportedFormatError(
"Granular position is too large to be handled with this version of node-taglib-sharp"
);
}
header._absoluteGranularPosition = Number(absoluteGranularPosition);
}
header._absoluteGranularPosition = Number(absoluteGranularPosition);

header._streamSerialNumber = data.subarray(14, 4).toUint(false);
header._pageSequenceNumber = data.subarray(18, 4).toUint(false);

Expand Down Expand Up @@ -245,11 +262,15 @@ export class OggPageHeader {
}, <Array<ByteVector|number>> []);
const lacingBytes = ByteVector.concatenate(...lacingValues);

const absoluteGranularPositionBytes = this._absoluteGranularPosition === -1
? OggPageHeader.NO_PACKETS_END_IN_PAGE
: ByteVector.fromUlong(this._absoluteGranularPosition, false);

return ByteVector.concatenate(
OggPageHeader.HEADER_IDENTIFIER,
this._version,
this._flags,
ByteVector.fromUlong(this._absoluteGranularPosition, false),
absoluteGranularPositionBytes,
ByteVector.fromUint(this._streamSerialNumber, false),
ByteVector.fromUint(this._pageSequenceNumber, false),
ByteVector.fromSize(4), // Checksum to be filled later
Expand Down
32 changes: 32 additions & 0 deletions test-unit/ogg/oggPageHeaderTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ import {Testers} from "../utilities/testers";
assert.strictEqual(header.streamSerialNumber, 0x1234);
}

@test
public fromFile_noPacketsEndInFile() {
// Arrange
const data = ByteVector.concatenate(
ByteVector.fromSize(10), // Buffer
OggPageHeader.HEADER_IDENTIFIER,
0x05, // Version
0x07, // Flags
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Absolute granular position
ByteVector.fromUint(0x1234, false), // Stream serial number
ByteVector.fromUint(0x2345, false), // Page sequence number
ByteVector.fromSize(4), // Checksum
0x01, // Page segment count
0xFF // Page segment 1 (partial)
);
const file = TestFile.getFile(data);

// Act
const header = OggPageHeader.fromFile(file, 10);

// Assert
assert.ok(header);
assert.strictEqual(header.absoluteGranularPosition, -1);
assert.strictEqual(header.dataSize, 255);
assert.strictEqual(header.flags, 0x07);
assert.isFalse(header.lastPacketComplete);
assert.deepStrictEqual(header.packetSizes, [0xFF]);
assert.strictEqual(header.pageSequenceNumber, 0x2345);
assert.strictEqual(header.size, 28);
assert.strictEqual(header.streamSerialNumber, 0x1234);
}

@test
public fromInfo_invalidParameters() {
// Arrange
Expand Down

0 comments on commit 26997fe

Please sign in to comment.