Skip to content

Commit

Permalink
Parse and slice string values using Buffer's toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
korya committed Oct 24, 2023
1 parent b51eaf4 commit 769b89a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tar-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class TarExtractor {
parseHeader(buffer: Buffer, paxHeaderData: Record<string, any>): TarFileHeader | null {
const h = new TarFileHeader();

h.name = buffer.subarray(0, 100).toString('utf8').replace(/\0+$/, '');
h.name = this.__parseStr(buffer, 0, 100, true);
this.__log('trace', `- file name '${h.name}' from `, buffer.subarray(0, 100));
if (!h.name) {
return null;
Expand All @@ -67,16 +67,16 @@ export class TarExtractor {
// 8 bytes for mode
// 8 bytes for uid
// 8 bytes for gid
h.size = parseInt(buffer.subarray(124, 136).toString('utf8'), 8);
h.size = parseInt(this.__parseStr(buffer, 124, 136), 8);
this.__log('trace', `- file size '${h.size}' from `, buffer.subarray(124, 136));
// 12 bytes for mtime
// 8 bytes for checksum
h.typeFlag = buffer.subarray(156, 157).toString('utf8');
h.typeFlag = this.__parseStr(buffer, 156, 157, true);
this.__log('trace', `- file type '${h.typeFlag}' from `, buffer.subarray(156, 157));
// 100 bytes for linkname
h.ustarIndicator = buffer.subarray(257, 263).toString('utf8');
h.ustarIndicator = this.__parseStr(buffer, 257, 263);
this.__log('trace', `- ustar ind. '${h.ustarIndicator}' from `, buffer.subarray(257, 263));
h.prefix = buffer.subarray(345, 500).toString('utf8').replace(/\0+$/, '');
h.prefix = this.__parseStr(buffer, 345, 500, true);
this.__log('trace', `- prefix '${h.prefix}' from `, buffer.subarray(345, 500));
if (h.prefix) {
h.name = `${h.prefix}/${h.name}`;
Expand Down Expand Up @@ -134,6 +134,11 @@ export class TarExtractor {
this.logLevel = level;
}

__parseStr(buffer: Buffer, start: number, end: number, trim?: boolean): string {
const s = buffer.toString('utf8', start, end);
return !trim ? s : s.replace(/\0+$/, '');
}

__log(level: LogLevel, ...args: any[]): void {
if (this.logLevel === level || this.logLevel === 'trace') {
args[0] = `rnfs-tar: ${args[0]}`;
Expand Down

0 comments on commit 769b89a

Please sign in to comment.