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

Simplify utility (util) functions #846

Merged
merged 1 commit into from
Jul 13, 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
10 changes: 5 additions & 5 deletions lib/asf/AsfObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ export class FilePropertiesObject extends State<IFilePropertiesObject> {
sendDuration: Token.UINT64_LE.get(buf, off + 48),
preroll: Token.UINT64_LE.get(buf, off + 56),
flags: {
broadcast: util.strtokBITSET.get(buf, off + 64, 24),
seekable: util.strtokBITSET.get(buf, off + 64, 25)
broadcast: util.getBit(buf, off + 64, 24),
seekable: util.getBit(buf, off + 64, 25)
},
// flagsNumeric: Token.UINT32_LE.get(buf, off + 64),
minimumDataPacketSize: Token.UINT32_LE.get(buf, off + 68),
Expand Down Expand Up @@ -528,9 +528,9 @@ export class ExtendedStreamPropertiesObjectState extends State<IExtendedStreamPr
alternateInitialBufferFullness: buf.readInt32LE(off + 32),
maximumObjectSize: buf.readInt32LE(off + 36),
flags: { // ToDo, check flag positions
reliableFlag: util.strtokBITSET.get(buf, off + 40, 0),
seekableFlag: util.strtokBITSET.get(buf, off + 40, 1),
resendLiveCleanpointsFlag: util.strtokBITSET.get(buf, off + 40, 2)
reliableFlag: util.getBit(buf, off + 40, 0),
seekableFlag: util.getBit(buf, off + 40, 1),
resendLiveCleanpointsFlag: util.getBit(buf, off + 40, 2)
},
// flagsNumeric: Token.UINT32_LE.get(buf, off + 64),
streamNumber: buf.readInt16LE(off + 42),
Expand Down
31 changes: 14 additions & 17 deletions lib/common/Util.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { Windows1292Decoder } from './Windows1292Decoder';
import { IRatio } from "../type";
import { IRatio } from '../type';

export type StringEncoding = 'iso-8859-1' | 'utf16' | 'utf8' | 'utf16le';

export const strtokBITSET = {
get: (buf: Buffer, off: number, bit: number): boolean => {
return (buf[off] & (1 << bit)) !== 0;
},
len: 1
};
export function getBit(buf: Uint8Array, off: number, bit: number): boolean {
return (buf[off] & (1 << bit)) !== 0;
}

/**
*
* @param buffer
* @param uint8Array
* @param start
* @param end
* @param encoding // ToDo: ts.enum
* @return {number}
*/
export function findZero(buffer: Buffer, start: number, end: number, encoding?: string): number {
export function findZero(uint8Array: Uint8Array, start: number, end: number, encoding?: string): number {
let i = start;
if (encoding === 'utf16') {
while (buffer[i] !== 0 || buffer[i + 1] !== 0) {
while (uint8Array[i] !== 0 || uint8Array[i + 1] !== 0) {
if (i >= end) return end;
i += 2;
}
return i;
} else {
while (buffer[i] !== 0) {
while (uint8Array[i] !== 0) {
if (i >= end) return end;
i++;
}
Expand All @@ -40,15 +37,15 @@ export function trimRightNull(x: string): string {
return pos0 === -1 ? x : x.substr(0, pos0);
}

export function swapBytes(buffer: Buffer): Buffer {
const l = buffer.length;
export function swapBytes<T extends Uint8Array>(uint8Array: T): T {
const l = uint8Array.length;
if ((l & 1) !== 0) throw new Error('Buffer length must be even');
for (let i = 0; i < l; i += 2) {
const a = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = a;
const a = uint8Array[i];
uint8Array[i] = uint8Array[i + 1];
uint8Array[i + 1] = a;
}
return buffer;
return uint8Array;
}

export function readUTF16String(buffer: Buffer): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/flac/FlacParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Metadata {

get: (buf: Buffer, off: number): IBlockHeader => {
return {
lastBlock: util.strtokBITSET.get(buf, off, 7),
lastBlock: util.getBit(buf, off, 7),
type: util.getBitAllignedNumber(buf, off, 1, 7),
length: Token.UINT24_BE.get(buf, off + 1)
};
Expand Down
16 changes: 8 additions & 8 deletions lib/id3v2/ID3v2Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ export class ID3v2Parser {
private static readFrameFlags(b: Buffer): IFrameFlags {
return {
status: {
tag_alter_preservation: util.strtokBITSET.get(b, 0, 6),
file_alter_preservation: util.strtokBITSET.get(b, 0, 5),
read_only: util.strtokBITSET.get(b, 0, 4)
tag_alter_preservation: util.getBit(b, 0, 6),
file_alter_preservation: util.getBit(b, 0, 5),
read_only: util.getBit(b, 0, 4)
},
format: {
grouping_identity: util.strtokBITSET.get(b, 1, 7),
compression: util.strtokBITSET.get(b, 1, 3),
encryption: util.strtokBITSET.get(b, 1, 2),
unsynchronisation: util.strtokBITSET.get(b, 1, 1),
data_length_indicator: util.strtokBITSET.get(b, 1, 0)
grouping_identity: util.getBit(b, 1, 7),
compression: util.getBit(b, 1, 3),
encryption: util.getBit(b, 1, 2),
unsynchronisation: util.getBit(b, 1, 1),
data_length_indicator: util.getBit(b, 1, 0)
}
};
}
Expand Down
10 changes: 5 additions & 5 deletions lib/id3v2/ID3v2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ export const ID3v2Header: IGetToken<IID3v2header> = {
// ID3v2 flags
flags: {
// Unsynchronisation
unsynchronisation: util.strtokBITSET.get(buf, off + 5, 7),
unsynchronisation: util.getBit(buf, off + 5, 7),
// Extended header
isExtendedHeader: util.strtokBITSET.get(buf, off + 5, 6),
isExtendedHeader: util.getBit(buf, off + 5, 6),
// Experimental indicator
expIndicator: util.strtokBITSET.get(buf, off + 5, 5),
footer: util.strtokBITSET.get(buf, off + 5, 4)
expIndicator: util.getBit(buf, off + 5, 5),
footer: util.getBit(buf, off + 5, 4)
},
size: UINT32SYNCSAFE.get(buf, off + 6)
};
Expand All @@ -122,7 +122,7 @@ export const ExtendedHeader: IGetToken<IExtendedHeader> = {
// Size of padding
sizeOfPadding: Token.UINT32_BE.get(buf, off + 6),
// CRC data present
crcDataPresent: util.strtokBITSET.get(buf, off + 4, 31)
crcDataPresent: util.getBit(buf, off + 4, 31)
};
}
};
Expand Down
6 changes: 3 additions & 3 deletions lib/ogg/OggParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class OggParser extends BasicParser {
version: buf.readUInt8(off + 4),

headerType: {
continued: util.strtokBITSET.get(buf, off + 5, 0),
firstPage: util.strtokBITSET.get(buf, off + 5, 1),
lastPage: util.strtokBITSET.get(buf, off + 5, 2)
continued: util.getBit(buf, off + 5, 0),
firstPage: util.getBit(buf, off + 5, 1),
lastPage: util.getBit(buf, off + 5, 2)
},
// packet_flag: buf.readUInt8(off + 5),
absoluteGranulePosition: buf.readIntLE(off + 6, 6), // cannot read 2 of 8 most significant bytes
Expand Down