Skip to content

Commit

Permalink
Renamed things
Browse files Browse the repository at this point in the history
Updated doc comments
  • Loading branch information
james-pre committed May 3, 2024
1 parent cf62037 commit 97f3864
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/ZipFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class ZipFS extends SyncIndexFS<FileEntry> {
while (cdPtr < cdEnd) {
const cd: FileEntry = new FileEntry(data, data.slice(cdPtr));
ZipFS._addToIndex(cd, index);
cdPtr += cd.totalSize;
cdPtr += cd.size;
entries.push(cd);
}

Expand Down
29 changes: 14 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@ import { extendedASCIIChars } from './constants.js';
import { decode } from '@zenfs/core/utils.js';

/**
* Converts the input time and date in MS-DOS format into a JavaScript Date
* object.
* Converts the input `time` and `date` in MS-DOS format into a `Date`.
*
* MS-DOS format:
* second 5 bits
* minute 6 bits
* hour 5 bits
* day (1-31) 5 bits
* month (1-23) 4 bits
* year (from 1980) 7 bits
* @hidden
*/
export function msdos2date(time: number, date: number): Date {
// MS-DOS Date
// |0 0 0 0 0|0 0 0 0|0 0 0 0 0 0 0
// D (1-31) M (1-23) Y (from 1980)
const day = date & 31;
// JS date is 0-indexed, DOS is 1-indexed.
const month = ((date >> 5) & 15) - 1;
const year = (date >> 9) + 1980;
// MS DOS Time
// |0 0 0 0 0|0 0 0 0 0 0|0 0 0 0 0
// Second Minute Hour
const second = time & 31;
const minute = (time >> 5) & 63;
const hour = time >> 11;
return new Date(year, month, day, hour, minute, second);
}

/**
* Safely returns the string from the buffer, even if it is 0 bytes long.
* (Normally, calling toString() on a buffer with start === end causes an
* exception).
* Safely decodes the string from a buffer.
* @hidden
*/
export function safeToString(buff: ArrayBufferLike | ArrayBufferView, useUTF8: boolean, start: number, length: number): string {
export function safeDecode(buffer: ArrayBufferLike | ArrayBufferView, utf8: boolean, start: number, length: number): string {
if (length === 0) {
return '';
}

const uintArray = new Uint8Array('buffer' in buff ? buff.buffer : buff).slice(start, start + length);
if (useUTF8) {
const uintArray = new Uint8Array('buffer' in buffer ? buffer.buffer : buffer).slice(start, start + length);
if (utf8) {
return decode(uintArray);
} else {
return [...uintArray].map(char => (char > 127 ? extendedASCIIChars[char - 128] : String.fromCharCode(char))).join('');
Expand Down
30 changes: 20 additions & 10 deletions src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiError, ErrorCode } from '@zenfs/core/ApiError.js';
import { FileType, Stats } from '@zenfs/core/stats.js';
import { deserialize, sizeof, struct, types as t } from 'utilium';
import { CompressionMethod, decompressionMethods } from './compression.js';
import { msdos2date, safeToString } from './utils.js';
import { msdos2date, safeDecode } from './utils.js';

/**
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.4.2.2
Expand Down Expand Up @@ -121,7 +121,7 @@ class LocalFileHeader {
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.4.17
*/
public get name(): string {
return safeToString(this.data, this.useUTF8, 30, this.nameLength);
return safeDecode(this.data, this.useUTF8, 30, this.nameLength);
}

/**
Expand Down Expand Up @@ -187,8 +187,8 @@ class FileEntry {
}

const size = sizeof(FileEntry);
this.name = safeToString(this._data, this.useUTF8, size, this.nameLength).replace(/\\/g, '/');
this.comment = safeToString(this._data, this.useUTF8, size + this.nameLength + this.extraFieldLength, this.commentLength);
this.name = safeDecode(this._data, this.useUTF8, size, this.nameLength).replace(/\\/g, '/');
this.comment = safeDecode(this._data, this.useUTF8, size + this.nameLength + this.extraLength, this.commentLength);
}

@t.uint32 public signature: number;
Expand Down Expand Up @@ -278,7 +278,7 @@ class FileEntry {
* The length of the extra field
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.4.11
*/
@t.uint16 public extraFieldLength: number;
@t.uint16 public extraLength: number;

/**
* The length of the comment
Expand Down Expand Up @@ -334,9 +334,9 @@ class FileEntry {
* This should be used for storage expansion.
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.4.28
*/
public get extraField(): ArrayBuffer {
public get extra(): ArrayBuffer {
const offset = 44 + this.nameLength;
return this._data.slice(offset, offset + this.extraFieldLength);
return this._data.slice(offset, offset + this.extraLength);
}

/**
Expand All @@ -345,10 +345,16 @@ class FileEntry {
*/
public readonly comment: string;

public get totalSize(): number {
return sizeof(FileEntry) + this.nameLength + this.extraFieldLength + this.commentLength;
/**
* The total size of the this entry
*/
public get size(): number {
return sizeof(FileEntry) + this.nameLength + this.extraLength + this.commentLength;
}

/**
* Whether this entry is a directory
*/
public get isDirectory(): boolean {
/*
NOTE: This assumes that the zip file implementation uses the lower byte
Expand All @@ -360,11 +366,15 @@ class FileEntry {
return !!(this.externalAttributes & 16) || this.name.endsWith('/');
}

/**
* Whether this entry is a file
*/
public get isFile(): boolean {
return !this.isDirectory;
}

/**
* Gets the file data, and decompresses it if needed.
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.3.8
*/
public get data(): Uint8Array {
Expand Down Expand Up @@ -477,6 +487,6 @@ class Header {
* @see http://pkware.com/documents/casestudies/APPNOTE.TXT#:~:text=4.4.26
*/
public get comment(): string {
return safeToString(this.data, true, 22, this.commentLength);
return safeDecode(this.data, true, 22, this.commentLength);
}
}

0 comments on commit 97f3864

Please sign in to comment.