1 | {"version":3,"file":"definitions.js","names":["SECTOR_SIZE","exports","compressionDeflate","compressionNone","MARKER_EOS","MARKER_GT","MARKER_GD","MARKER_FOOTER","compressionMap","parseFlags","flagBuffer","number","readUInt32LE","newLineTest","useSecondaryGrain","useZeroedGrainTable","compressedGrains","hasMarkers","parseS64b","buffer","offset","valueName","extraBits","readIntLE","value","hadValueInHighBytes","readWrongSign","Math","sign","Error","parseU64b","readUIntLE","unpackHeader","magicString","slice","toString","version","flags","capacitySectors","grainSizeSectors","descriptorOffsetSectors","descriptorSizeSectors","numGTEsPerGT","rGrainDirectoryOffsetSectors","grainDirectoryOffsetSectors","overheadSectors","compressionMethod","readUInt16LE","l1EntrySectors","createStreamOptimizedHeader","headerBuffer","Buffer","alloc","from","copy","writeUInt32LE","writeBigUInt64LE","BigInt","writeBigInt64LE","grainDirectoryEntries","ceil","grainTableEntries","grainDirectorySizeSectors","grainTableSizeSectors","write","writeUInt16LE"],"sources":["../src/definitions.js"],"sourcesContent":["export const SECTOR_SIZE = 512\nexport const compressionDeflate = 'COMPRESSION_DEFLATE'\nexport const compressionNone = 'COMPRESSION_NONE'\n\nexport const MARKER_EOS = 0\nexport const MARKER_GT = 1\nexport const MARKER_GD = 2\nexport const MARKER_FOOTER = 3\n\nconst compressionMap = [compressionNone, compressionDeflate]\n\nexport function parseFlags(flagBuffer) {\n const number = flagBuffer.readUInt32LE(0)\n return {\n newLineTest: !!(number & (1 << 0)),\n useSecondaryGrain: !!(number & (1 << 1)),\n useZeroedGrainTable: !!(number & (1 << 2)),\n compressedGrains: !!(number & (1 << 16)),\n hasMarkers: !!(number & (1 << 17)),\n }\n}\n\n// actually reads 47 bits\nfunction parseS64b(buffer, offset, valueName) {\n const extraBits = buffer.readIntLE(offset + 6, 2)\n const value = buffer.readIntLE(offset, 6)\n const hadValueInHighBytes = !(extraBits === 0 || extraBits === -1)\n const readWrongSign = Math.sign(value) * Math.sign(extraBits) < 0\n if (hadValueInHighBytes || readWrongSign) {\n throw new Error('Unsupported VMDK, ' + valueName + ' is too big')\n }\n return value\n}\n\n// reads 48bits\nexport function parseU64b(buffer, offset, valueName) {\n const extraBits = buffer.readUIntLE(offset + 6, 2)\n const value = buffer.readUIntLE(offset, 6)\n if (extraBits > 0) {\n throw new Error('Unsupported VMDK, ' + valueName + ' is too big')\n }\n return value\n}\n\nexport function unpackHeader(buffer) {\n const magicString = buffer.slice(0, 4).toString('ascii')\n if (magicString !== 'KDMV') {\n throw new Error('not a VMDK file')\n }\n const version = buffer.readUInt32LE(4)\n if (version !== 1 && version !== 3) {\n throw new Error('unsupported VMDK version ' + version + ', only version 1 and 3 are supported')\n }\n const flags = parseFlags(buffer.slice(8, 12))\n const capacitySectors = parseU64b(buffer, 12, 'capacitySectors')\n const grainSizeSectors = parseU64b(buffer, 20, 'grainSizeSectors')\n const descriptorOffsetSectors = parseU64b(buffer, 28, 'descriptorOffsetSectors')\n const descriptorSizeSectors = parseU64b(buffer, 36, 'descriptorSizeSectors')\n const numGTEsPerGT = buffer.readUInt32LE(44)\n const rGrainDirectoryOffsetSectors = parseS64b(buffer, 48, 'rGrainDirectoryOffsetSectors')\n const grainDirectoryOffsetSectors = parseS64b(buffer, 56, 'grainDirectoryOffsetSectors')\n const overheadSectors = parseS64b(buffer, 64, 'overheadSectors')\n const compressionMethod = compressionMap[buffer.readUInt16LE(77)]\n const l1EntrySectors = numGTEsPerGT * grainSizeSectors\n return {\n magicString,\n version,\n flags,\n compressionMethod,\n grainSizeSectors,\n overheadSectors,\n capacitySectors,\n descriptorOffsetSectors,\n descriptorSizeSectors,\n grainDirectoryOffsetSectors,\n rGrainDirectoryOffsetSectors,\n l1EntrySectors,\n numGTEsPerGT,\n }\n}\n\nexport function createStreamOptimizedHeader(\n capacitySectors,\n descriptorSizeSectors,\n grainDirectoryOffsetSectors = -1,\n rGrainDirectoryOffsetSectors = 0\n) {\n const headerBuffer = Buffer.alloc(SECTOR_SIZE)\n Buffer.from('KDMV', 'ascii').copy(headerBuffer, 0)\n // version\n headerBuffer.writeUInt32LE(3, 4)\n // newline, secondary grain directory , compression, markers\n const flags = 1 | (1 << 1) | (1 << 16) | (1 << 17)\n headerBuffer.writeUInt32LE(flags, 8)\n\n // number of sectors of the full disk\n headerBuffer.writeBigUInt64LE(BigInt(capacitySectors), 12)\n\n // number of sectors in the data of a grain (uncompressed)\n const grainSizeSectors = 128\n headerBuffer.writeBigUInt64LE(BigInt(grainSizeSectors), 20)\n\n // offset of the descriptor(should be directly after header which is 1 sector long)\n const descriptorOffsetSectors = 1\n headerBuffer.writeBigUInt64LE(BigInt(descriptorOffsetSectors), 28)\n // size of the descriptor in sectors\n headerBuffer.writeBigUInt64LE(BigInt(descriptorSizeSectors), 36)\n const numGTEsPerGT = 512\n // number of entries in a grain table\n headerBuffer.writeUInt32LE(numGTEsPerGT, 44)\n\n // redundant grain directory offset\n headerBuffer.writeBigInt64LE(BigInt(rGrainDirectoryOffsetSectors), 48)\n\n // grain directory offset in sector\n headerBuffer.writeBigInt64LE(BigInt(grainDirectoryOffsetSectors), 56)\n const grainDirectoryEntries = Math.ceil(Math.ceil(capacitySectors / grainSizeSectors) / numGTEsPerGT)\n const grainTableEntries = grainDirectoryEntries * numGTEsPerGT\n const grainDirectorySizeSectors = Math.ceil((grainDirectoryEntries * 4) / SECTOR_SIZE)\n const grainTableSizeSectors = Math.ceil((grainTableEntries * 4) / SECTOR_SIZE)\n let overheadSectors = 1 + descriptorSizeSectors\n // only grow the header if there really is a grain directory\n if (grainDirectoryOffsetSectors > 0) {\n overheadSectors += grainDirectorySizeSectors + grainTableSizeSectors\n }\n\n // first offset available for data\n headerBuffer.writeBigInt64LE(BigInt(overheadSectors), 64)\n\n // newline mangling detector\n headerBuffer.write('\\n \\r\\n', 73, 4, 'ascii')\n\n // use DEFLATE compression algorithm\n headerBuffer.writeUInt16LE(1, 77)\n return {\n buffer: headerBuffer,\n grainDirectorySizeSectors,\n grainTableSizeSectors,\n grainDirectoryEntries,\n grainTableEntries,\n }\n}\n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