michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: if (typeof Components != "undefined") { michael@0: throw new Error("This file is meant to be loaded in a worker"); michael@0: } michael@0: if (!module || !exports) { michael@0: throw new Error("Please load this module with require()"); michael@0: } michael@0: michael@0: const SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm"); michael@0: const Internals = require("resource://gre/modules/workers/lz4_internal.js"); michael@0: michael@0: const MAGIC_NUMBER = new Uint8Array([109, 111, 122, 76, 122, 52, 48, 0]); // "mozLz4a\0" michael@0: michael@0: const BYTES_IN_SIZE_HEADER = ctypes.uint32_t.size; michael@0: michael@0: const HEADER_SIZE = MAGIC_NUMBER.byteLength + BYTES_IN_SIZE_HEADER; michael@0: michael@0: const EXPECTED_HEADER_TYPE = new ctypes.ArrayType(ctypes.uint8_t, HEADER_SIZE); michael@0: const EXPECTED_SIZE_BUFFER_TYPE = new ctypes.ArrayType(ctypes.uint8_t, BYTES_IN_SIZE_HEADER); michael@0: michael@0: /** michael@0: * An error during (de)compression michael@0: * michael@0: * @param {string} operation The name of the operation ("compress", "decompress") michael@0: * @param {string} reason A reason to be used when matching errors. Must start michael@0: * with "because", e.g. "becauseInvalidContent". michael@0: * @param {string} message A human-readable message. michael@0: */ michael@0: function LZError(operation, reason, message) { michael@0: SharedAll.OSError.call(this); michael@0: this.operation = operation; michael@0: this[reason] = true; michael@0: this.message = message; michael@0: } michael@0: LZError.prototype = Object.create(SharedAll.OSError); michael@0: LZError.prototype.toString = function toString() { michael@0: return this.message; michael@0: }; michael@0: exports.Error = LZError; michael@0: michael@0: /** michael@0: * Compress a block to a form suitable for writing to disk. michael@0: * michael@0: * Compatibility note: For the moment, we are basing our code on lz4 michael@0: * 1.3, which does not specify a *file* format. Therefore, we define michael@0: * our own format. Once lz4 defines a complete file format, we will michael@0: * migrate both |compressFileContent| and |decompressFileContent| to this file michael@0: * format. For backwards-compatibility, |decompressFileContent| will however michael@0: * keep the ability to decompress files provided with older versions of michael@0: * |compressFileContent|. michael@0: * michael@0: * Compressed files have the following layout: michael@0: * michael@0: * | MAGIC_NUMBER (8 bytes) | content size (uint32_t, little endian) | content, as obtained from lz4_compress | michael@0: * michael@0: * @param {TypedArray|void*} buffer The buffer to write to the disk. michael@0: * @param {object=} options An object that may contain the following fields: michael@0: * - {number} bytes The number of bytes to read from |buffer|. If |buffer| michael@0: * is an |ArrayBuffer|, |bytes| defaults to |buffer.byteLength|. If michael@0: * |buffer| is a |void*|, |bytes| MUST be provided. michael@0: * @return {Uint8Array} An array of bytes suitable for being written to the michael@0: * disk. michael@0: */ michael@0: function compressFileContent(array, options = {}) { michael@0: // Prepare the output array michael@0: let inputBytes; michael@0: if (SharedAll.isTypedArray(array) && !(options && "bytes" in options)) { michael@0: inputBytes = array.byteLength; michael@0: } else if (options && options.bytes) { michael@0: inputBytes = options.bytes; michael@0: } else { michael@0: throw new TypeError("compressFileContent requires a size"); michael@0: } michael@0: let maxCompressedSize = Internals.maxCompressedSize(inputBytes); michael@0: let outputArray = new Uint8Array(HEADER_SIZE + maxCompressedSize); michael@0: michael@0: // Compress to output array michael@0: let payload = new Uint8Array(outputArray.buffer, outputArray.byteOffset + HEADER_SIZE); michael@0: let compressedSize = Internals.compress(array, inputBytes, payload); michael@0: michael@0: // Add headers michael@0: outputArray.set(MAGIC_NUMBER); michael@0: let view = new DataView(outputArray.buffer); michael@0: view.setUint32(MAGIC_NUMBER.byteLength, inputBytes, true); michael@0: michael@0: return new Uint8Array(outputArray.buffer, 0, HEADER_SIZE + compressedSize); michael@0: } michael@0: exports.compressFileContent = compressFileContent; michael@0: michael@0: function decompressFileContent(array, options = {}) { michael@0: let {ptr, bytes} = SharedAll.normalizeToPointer(array, options.bytes || null); michael@0: if (bytes < HEADER_SIZE) { michael@0: throw new LZError("decompress", "becauseLZNoHeader", "Buffer is too short (no header)"); michael@0: } michael@0: michael@0: // Read headers michael@0: let expectMagicNumber = ctypes.cast(ptr, EXPECTED_HEADER_TYPE.ptr).contents; michael@0: for (let i = 0; i < MAGIC_NUMBER.byteLength; ++i) { michael@0: if (expectMagicNumber[i] != MAGIC_NUMBER[i]) { michael@0: throw new LZError("decompress", "becauseLZWrongMagicNumber", "Invalid header (no magic number"); michael@0: } michael@0: } michael@0: michael@0: let sizeBuf = michael@0: ctypes.cast( michael@0: SharedAll.offsetBy(ptr, MAGIC_NUMBER.byteLength), michael@0: EXPECTED_SIZE_BUFFER_TYPE.ptr).contents; michael@0: let expectDecompressedSize = michael@0: sizeBuf[0] + (sizeBuf[1] << 8) + (sizeBuf[2] << 16) + (sizeBuf[3] << 24); michael@0: if (expectDecompressedSize == 0) { michael@0: // The underlying algorithm cannot handle a size of 0 michael@0: return new Uint8Array(0); michael@0: } michael@0: michael@0: // Prepare the input buffer michael@0: let inputPtr = SharedAll.offsetBy(ptr, HEADER_SIZE); michael@0: michael@0: // Prepare the output buffer michael@0: let outputBuffer = new Uint8Array(expectDecompressedSize); michael@0: let decompressedBytes = (new SharedAll.Type.size_t.implementation(0)); michael@0: michael@0: // Decompress michael@0: let success = Internals.decompress(inputPtr, bytes - HEADER_SIZE, michael@0: outputBuffer, outputBuffer.byteLength, michael@0: decompressedBytes.address()); michael@0: if (!success) { michael@0: throw new LZError("decompress", "becauseLZInvalidContent", "Invalid content:Decompression stopped at " + decompressedBytes.value); michael@0: } michael@0: return new Uint8Array(outputBuffer.buffer, outputBuffer.byteOffset, decompressedBytes.value); michael@0: } michael@0: exports.decompressFileContent = decompressFileContent;