Skip to content

Commit

Permalink
Added buffer search algorithm to find signature in hex format
Browse files Browse the repository at this point in the history
  • Loading branch information
JackRKelly committed Dec 22, 2020
1 parent 008f023 commit 16344b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,26 @@ export class Reader {
}

hasSignature(signature: LESignature): boolean {
return this.buffer.includes(signature, this.offset, "hex");
let cursor = this.offset;
while (this.buffer.readUInt32LE(cursor) !== signature) {
cursor += 1;
if (cursor + 4 > this.buffer.length) {
return false;
}
}
return true;
}

findSignature(signature: LESignature): number {
return this.buffer.indexOf(signature, this.offset, "hex");
let cursor = this.offset;
while (this.buffer.readUInt32LE(cursor) !== signature) {
cursor += 1;
if (cursor + 4 > this.buffer.length) {
cursor = -1;
break;
}
}
return cursor;
}

setOffset(offset: number): void {
Expand Down Expand Up @@ -359,10 +374,8 @@ export class Reader {
this.offset = 0;
this.buffer = zip;
//Determine endianess on reader initialization.
if (
zip.readUInt32LE() ===
Buffer.from(LESignature.LocalFile, "hex").readUInt32LE()
) {

if (zip.readUInt32LE() === LESignature.LocalFile) {
this.endian = Endian.Little;
} else {
this.endian = Endian.Big;
Expand Down
8 changes: 4 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export enum Endian {
}

export enum LESignature {
LocalFile = "504b0304",
DataDescriptor = "504b0708",
CentralDirectory = "504b0102",
EndCentralDirectory = "504b0506",
LocalFile = 0x04034b50,
DataDescriptor = 0x08074b50,
CentralDirectory = 0x02014b50,
EndCentralDirectory = 0x06054b50,
}

export enum OS {
Expand Down

0 comments on commit 16344b6

Please sign in to comment.