toolkit/components/workerlz4/lz4.js

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:f6fd8daf35a1
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 if (typeof Components != "undefined") {
8 throw new Error("This file is meant to be loaded in a worker");
9 }
10 if (!module || !exports) {
11 throw new Error("Please load this module with require()");
12 }
13
14 const SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm");
15 const Internals = require("resource://gre/modules/workers/lz4_internal.js");
16
17 const MAGIC_NUMBER = new Uint8Array([109, 111, 122, 76, 122, 52, 48, 0]); // "mozLz4a\0"
18
19 const BYTES_IN_SIZE_HEADER = ctypes.uint32_t.size;
20
21 const HEADER_SIZE = MAGIC_NUMBER.byteLength + BYTES_IN_SIZE_HEADER;
22
23 const EXPECTED_HEADER_TYPE = new ctypes.ArrayType(ctypes.uint8_t, HEADER_SIZE);
24 const EXPECTED_SIZE_BUFFER_TYPE = new ctypes.ArrayType(ctypes.uint8_t, BYTES_IN_SIZE_HEADER);
25
26 /**
27 * An error during (de)compression
28 *
29 * @param {string} operation The name of the operation ("compress", "decompress")
30 * @param {string} reason A reason to be used when matching errors. Must start
31 * with "because", e.g. "becauseInvalidContent".
32 * @param {string} message A human-readable message.
33 */
34 function LZError(operation, reason, message) {
35 SharedAll.OSError.call(this);
36 this.operation = operation;
37 this[reason] = true;
38 this.message = message;
39 }
40 LZError.prototype = Object.create(SharedAll.OSError);
41 LZError.prototype.toString = function toString() {
42 return this.message;
43 };
44 exports.Error = LZError;
45
46 /**
47 * Compress a block to a form suitable for writing to disk.
48 *
49 * Compatibility note: For the moment, we are basing our code on lz4
50 * 1.3, which does not specify a *file* format. Therefore, we define
51 * our own format. Once lz4 defines a complete file format, we will
52 * migrate both |compressFileContent| and |decompressFileContent| to this file
53 * format. For backwards-compatibility, |decompressFileContent| will however
54 * keep the ability to decompress files provided with older versions of
55 * |compressFileContent|.
56 *
57 * Compressed files have the following layout:
58 *
59 * | MAGIC_NUMBER (8 bytes) | content size (uint32_t, little endian) | content, as obtained from lz4_compress |
60 *
61 * @param {TypedArray|void*} buffer The buffer to write to the disk.
62 * @param {object=} options An object that may contain the following fields:
63 * - {number} bytes The number of bytes to read from |buffer|. If |buffer|
64 * is an |ArrayBuffer|, |bytes| defaults to |buffer.byteLength|. If
65 * |buffer| is a |void*|, |bytes| MUST be provided.
66 * @return {Uint8Array} An array of bytes suitable for being written to the
67 * disk.
68 */
69 function compressFileContent(array, options = {}) {
70 // Prepare the output array
71 let inputBytes;
72 if (SharedAll.isTypedArray(array) && !(options && "bytes" in options)) {
73 inputBytes = array.byteLength;
74 } else if (options && options.bytes) {
75 inputBytes = options.bytes;
76 } else {
77 throw new TypeError("compressFileContent requires a size");
78 }
79 let maxCompressedSize = Internals.maxCompressedSize(inputBytes);
80 let outputArray = new Uint8Array(HEADER_SIZE + maxCompressedSize);
81
82 // Compress to output array
83 let payload = new Uint8Array(outputArray.buffer, outputArray.byteOffset + HEADER_SIZE);
84 let compressedSize = Internals.compress(array, inputBytes, payload);
85
86 // Add headers
87 outputArray.set(MAGIC_NUMBER);
88 let view = new DataView(outputArray.buffer);
89 view.setUint32(MAGIC_NUMBER.byteLength, inputBytes, true);
90
91 return new Uint8Array(outputArray.buffer, 0, HEADER_SIZE + compressedSize);
92 }
93 exports.compressFileContent = compressFileContent;
94
95 function decompressFileContent(array, options = {}) {
96 let {ptr, bytes} = SharedAll.normalizeToPointer(array, options.bytes || null);
97 if (bytes < HEADER_SIZE) {
98 throw new LZError("decompress", "becauseLZNoHeader", "Buffer is too short (no header)");
99 }
100
101 // Read headers
102 let expectMagicNumber = ctypes.cast(ptr, EXPECTED_HEADER_TYPE.ptr).contents;
103 for (let i = 0; i < MAGIC_NUMBER.byteLength; ++i) {
104 if (expectMagicNumber[i] != MAGIC_NUMBER[i]) {
105 throw new LZError("decompress", "becauseLZWrongMagicNumber", "Invalid header (no magic number");
106 }
107 }
108
109 let sizeBuf =
110 ctypes.cast(
111 SharedAll.offsetBy(ptr, MAGIC_NUMBER.byteLength),
112 EXPECTED_SIZE_BUFFER_TYPE.ptr).contents;
113 let expectDecompressedSize =
114 sizeBuf[0] + (sizeBuf[1] << 8) + (sizeBuf[2] << 16) + (sizeBuf[3] << 24);
115 if (expectDecompressedSize == 0) {
116 // The underlying algorithm cannot handle a size of 0
117 return new Uint8Array(0);
118 }
119
120 // Prepare the input buffer
121 let inputPtr = SharedAll.offsetBy(ptr, HEADER_SIZE);
122
123 // Prepare the output buffer
124 let outputBuffer = new Uint8Array(expectDecompressedSize);
125 let decompressedBytes = (new SharedAll.Type.size_t.implementation(0));
126
127 // Decompress
128 let success = Internals.decompress(inputPtr, bytes - HEADER_SIZE,
129 outputBuffer, outputBuffer.byteLength,
130 decompressedBytes.address());
131 if (!success) {
132 throw new LZError("decompress", "becauseLZInvalidContent", "Invalid content:Decompression stopped at " + decompressedBytes.value);
133 }
134 return new Uint8Array(outputBuffer.buffer, outputBuffer.byteOffset, decompressedBytes.value);
135 }
136 exports.decompressFileContent = decompressFileContent;

mercurial