diff --git a/src/ssn/readSolidpkg.ts b/src/ssn/readSolidpkg.ts index f475008..2e1f525 100644 --- a/src/ssn/readSolidpkg.ts +++ b/src/ssn/readSolidpkg.ts @@ -1,8 +1,8 @@ import { TextDecoder } from 'util'; import modifyPassword from './modifyPassword'; -const MAGIC_END_OF_CENTRAL_DIR = 0x06054b50; -const MAGIC_CENTRAL_DIR = 0x02014b50; +const SIGNATURE_END_OF_CENTRAL_DIR = 0x06054b50; +const SIGNATURE_CENTRAL_DIR = 0x02014b50; const COMPRESSION_DEFLATE = 8; const Decoder = new TextDecoder('utf-8'); @@ -17,7 +17,7 @@ export default function readSolidpkg(buffer: Buffer) { let pos = buffer.length - 22; //end of central dir is at least 22 bytes long //Find end of central dir - while (pos >= 0 && dv.getUint32(pos, true) !== MAGIC_END_OF_CENTRAL_DIR) { + while (pos >= 0 && dv.getUint32(pos, true) !== SIGNATURE_END_OF_CENTRAL_DIR) { pos -= 1; } if (pos < 0) { @@ -25,7 +25,11 @@ export default function readSolidpkg(buffer: Buffer) { } pos += 4; - //skip 6 bytes + /** skip 6 bytes: + * number of this disk, + * number of the disk with the start of the central directory + * total number of entries in the central directory on this disk + */ pos += 6; /** Total number of entries in the central directory */ @@ -34,6 +38,7 @@ export default function readSolidpkg(buffer: Buffer) { const centralDirSize = dv.getUint32(pos, true); pos += 4; /** Offset of start of central directory with respect to the starting disk number */ const centralDirOffset = dv.getUint32(pos, true); pos += 4; + //ignore .ZIP file comment length if (numEntries !== 1) { throw new Error(`Expected numEntries == 1 in end of central dir but it was "${numEntries}"`); @@ -51,9 +56,9 @@ export default function readSolidpkg(buffer: Buffer) { pos -= 20 + centralDirSize; { - const magicNum = dv.getUint32(pos, true); pos += 4; - if (magicNum !== MAGIC_CENTRAL_DIR) { - throw new Error(`Expected central dir signature but found "0x${magicNum.toString(16)}"`); + const signature = dv.getUint32(pos, true); pos += 4; + if (signature !== SIGNATURE_CENTRAL_DIR) { + throw new Error(`Expected central dir signature but found "0x${signature.toString(16)}"`); } } @@ -82,6 +87,9 @@ export default function readSolidpkg(buffer: Buffer) { pos += 8; //skip disk num start and file attributes const relOffset = dv.getUint32(pos, true); pos += 4; const fileName = Decoder.decode(new DataView(arrayBuffer, pos, fileNameLength)); pos += fileNameLength; //skip file name + if (fileName !== 'metafile.solid') { + throw new Error(`Expected file name to be "metafile.solid" but it was "${fileName}".`); + } //read password from extra field let encodedPassword: Uint8Array | undefined;