diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/lz4 | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | toolkit/components/lz4/lz4.cpp | 66 | ||||
-rw-r--r-- | toolkit/components/lz4/lz4.js | 188 | ||||
-rw-r--r-- | toolkit/components/lz4/lz4_internal.js | 78 | ||||
-rw-r--r-- | toolkit/components/lz4/moz.build | 21 | ||||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/data/chrome.manifest | 1 | ||||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/data/compression.lz | bin | 0 -> 23 bytes | |||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/data/worker_lz4.js | 164 | ||||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/test_lz4.js | 35 | ||||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/test_lz4_sync.js | 41 | ||||
-rw-r--r-- | toolkit/components/lz4/tests/xpcshell/xpcshell.ini | 10 |
10 files changed, 604 insertions, 0 deletions
diff --git a/toolkit/components/lz4/lz4.cpp b/toolkit/components/lz4/lz4.cpp new file mode 100644 index 0000000000..7e0ea13288 --- /dev/null +++ b/toolkit/components/lz4/lz4.cpp @@ -0,0 +1,66 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/Compression.h" + +/** + * LZ4 is a very fast byte-wise compression algorithm. + * + * Compared to Google's Snappy it is faster to compress and decompress and + * generally produces output of about the same size. + * + * Compared to zlib it compresses at about 10x the speed, decompresses at about + * 4x the speed and produces output of about 1.5x the size. + * + */ + +using namespace mozilla::Compression; + +/** + * Compresses 'inputSize' bytes from 'source' into 'dest'. + * Destination buffer must be already allocated, + * and must be sized to handle worst cases situations (input data not + * compressible) Worst case size evaluation is provided by function + * LZ4_compressBound() + * + * @param inputSize is the input size. Max supported value is ~1.9GB + * @param return the number of bytes written in buffer dest + */ +extern "C" MOZ_EXPORT size_t workerlz4_compress(const char* source, + size_t inputSize, char* dest) { + return LZ4::compress(source, inputSize, dest); +} + +/** + * If the source stream is malformed, the function will stop decoding + * and return a negative result, indicating the byte position of the + * faulty instruction + * + * This function never writes outside of provided buffers, and never + * modifies input buffer. + * + * note : destination buffer must be already allocated. + * its size must be a minimum of 'outputSize' bytes. + * @param outputSize is the output size, therefore the original size + * @return true/false + */ +extern "C" MOZ_EXPORT int workerlz4_decompress(const char* source, + size_t inputSize, char* dest, + size_t maxOutputSize, + size_t* bytesOutput) { + return LZ4::decompress(source, inputSize, dest, maxOutputSize, bytesOutput); +} + +/* + Provides the maximum size that LZ4 may output in a "worst case" + scenario (input data not compressible) primarily useful for memory + allocation of output buffer. + note : this function is limited by "int" range (2^31-1) + + @param inputSize is the input size. Max supported value is ~1.9GB + @return maximum output size in a "worst case" scenario +*/ +extern "C" MOZ_EXPORT size_t workerlz4_maxCompressedSize(size_t inputSize) { + return LZ4::maxCompressedSize(inputSize); +} diff --git a/toolkit/components/lz4/lz4.js b/toolkit/components/lz4/lz4.js new file mode 100644 index 0000000000..b929d97c78 --- /dev/null +++ b/toolkit/components/lz4/lz4.js @@ -0,0 +1,188 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +var SharedAll; +if (typeof Components != "undefined") { + SharedAll = {}; + ChromeUtils.import( + "resource://gre/modules/osfile/osfile_shared_allthreads.jsm", + SharedAll + ); + var { Primitives } = ChromeUtils.import( + "resource://gre/modules/lz4_internal.js" + ); + var { ctypes } = ChromeUtils.import("resource://gre/modules/ctypes.jsm"); + + this.EXPORTED_SYMBOLS = ["Lz4"]; + this.exports = {}; +} else if (typeof module != "undefined" && typeof require != "undefined") { + /* eslint-env commonjs */ + SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm"); + Primitives = require("resource://gre/modules/lz4_internal.js"); + ctypes = self.ctypes; +} else { + throw new Error( + "Please load this module with Component.utils.import or with require()" + ); +} + +const MAGIC_NUMBER = new Uint8Array([109, 111, 122, 76, 122, 52, 48, 0]); // "mozLz40\0" + +const BYTES_IN_SIZE_HEADER = ctypes.uint32_t.size; + +const HEADER_SIZE = MAGIC_NUMBER.byteLength + BYTES_IN_SIZE_HEADER; + +/** + * An error during (de)compression + * + * @param {string} operation The name of the operation ("compress", "decompress") + * @param {string} reason A reason to be used when matching errors. Must start + * with "because", e.g. "becauseInvalidContent". + * @param {string} message A human-readable message. + */ +function LZError(operation, reason, message) { + SharedAll.OSError.call(this); + this.operation = operation; + this[reason] = true; + this.message = message; +} +LZError.prototype = Object.create(SharedAll.OSError); +LZError.prototype.toString = function toString() { + return this.message; +}; +exports.Error = LZError; + +/** + * Compress a block to a form suitable for writing to disk. + * + * Compatibility note: For the moment, we are basing our code on lz4 + * 1.3, which does not specify a *file* format. Therefore, we define + * our own format. Once lz4 defines a complete file format, we will + * migrate both |compressFileContent| and |decompressFileContent| to this file + * format. For backwards-compatibility, |decompressFileContent| will however + * keep the ability to decompress files provided with older versions of + * |compressFileContent|. + * + * Compressed files have the following layout: + * + * | MAGIC_NUMBER (8 bytes) | content size (uint32_t, little endian) | content, as obtained from lz4_compress | + * + * @param {TypedArray|void*} buffer The buffer to write to the disk. + * @param {object=} options An object that may contain the following fields: + * - {number} bytes The number of bytes to read from |buffer|. If |buffer| + * is an |ArrayBuffer|, |bytes| defaults to |buffer.byteLength|. If + * |buffer| is a |void*|, |bytes| MUST be provided. + * @return {Uint8Array} An array of bytes suitable for being written to the + * disk. + */ +function compressFileContent(array, options = {}) { + // Prepare the output array + let inputBytes; + if (SharedAll.isTypedArray(array) && !(options && "bytes" in options)) { + inputBytes = array.byteLength; + } else if (options && options.bytes) { + inputBytes = options.bytes; + } else { + throw new TypeError("compressFileContent requires a size"); + } + let maxCompressedSize = Primitives.maxCompressedSize(inputBytes); + let outputArray = new Uint8Array(HEADER_SIZE + maxCompressedSize); + + // Compress to output array + let payload = new Uint8Array( + outputArray.buffer, + outputArray.byteOffset + HEADER_SIZE + ); + let compressedSize = Primitives.compress(array, inputBytes, payload); + + // Add headers + outputArray.set(MAGIC_NUMBER); + let view = new DataView(outputArray.buffer); + view.setUint32(MAGIC_NUMBER.byteLength, inputBytes, true); + + return new Uint8Array(outputArray.buffer, 0, HEADER_SIZE + compressedSize); +} +exports.compressFileContent = compressFileContent; + +function decompressFileContent(array, options = {}) { + let bytes = SharedAll.normalizeBufferArgs(array, options.bytes || null); + if (bytes < HEADER_SIZE) { + throw new LZError( + "decompress", + "becauseLZNoHeader", + `Buffer is too short (no header) - Data: ${options.path || array}` + ); + } + + // Read headers + let expectMagicNumber = new DataView( + array.buffer, + 0, + MAGIC_NUMBER.byteLength + ); + for (let i = 0; i < MAGIC_NUMBER.byteLength; ++i) { + if (expectMagicNumber.getUint8(i) != MAGIC_NUMBER[i]) { + throw new LZError( + "decompress", + "becauseLZWrongMagicNumber", + `Invalid header (no magic number) - Data: ${options.path || array}` + ); + } + } + + let sizeBuf = new DataView( + array.buffer, + MAGIC_NUMBER.byteLength, + BYTES_IN_SIZE_HEADER + ); + let expectDecompressedSize = + sizeBuf.getUint8(0) + + (sizeBuf.getUint8(1) << 8) + + (sizeBuf.getUint8(2) << 16) + + (sizeBuf.getUint8(3) << 24); + if (expectDecompressedSize == 0) { + // The underlying algorithm cannot handle a size of 0 + return new Uint8Array(0); + } + + // Prepare the input buffer + let inputData = new DataView(array.buffer, HEADER_SIZE); + + // Prepare the output buffer + let outputBuffer = new Uint8Array(expectDecompressedSize); + let decompressedBytes = new SharedAll.Type.size_t.implementation(0); + + // Decompress + let success = Primitives.decompress( + inputData, + bytes - HEADER_SIZE, + outputBuffer, + outputBuffer.byteLength, + decompressedBytes.address() + ); + if (!success) { + throw new LZError( + "decompress", + "becauseLZInvalidContent", + `Invalid content: Decompression stopped at ${ + decompressedBytes.value + } - Data: ${options.path || array}` + ); + } + return new Uint8Array( + outputBuffer.buffer, + outputBuffer.byteOffset, + decompressedBytes.value + ); +} +exports.decompressFileContent = decompressFileContent; + +if (typeof Components != "undefined") { + this.Lz4 = { + compressFileContent, + decompressFileContent, + }; +} diff --git a/toolkit/components/lz4/lz4_internal.js b/toolkit/components/lz4/lz4_internal.js new file mode 100644 index 0000000000..9fd06cd69a --- /dev/null +++ b/toolkit/components/lz4/lz4_internal.js @@ -0,0 +1,78 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* eslint-env commonjs */ + +"use strict"; + +var Primitives = {}; + +var SharedAll; +if (typeof Components != "undefined") { + SharedAll = {}; + ChromeUtils.import( + "resource://gre/modules/osfile/osfile_shared_allthreads.jsm", + SharedAll + ); + + this.EXPORTED_SYMBOLS = ["Primitives"]; + this.Primitives = Primitives; + this.exports = {}; +} else if (typeof module != "undefined" && typeof require != "undefined") { + SharedAll = require("resource://gre/modules/osfile/osfile_shared_allthreads.jsm"); +} else { + throw new Error( + "Please load this module with Component.utils.import or with require()" + ); +} + +var libxul = new SharedAll.Library("libxul", SharedAll.Constants.Path.libxul); +var Type = SharedAll.Type; + +libxul.declareLazyFFI( + Primitives, + "compress", + "workerlz4_compress", + null, + /* return*/ Type.size_t, + /* const source*/ Type.void_t.in_ptr, + /* inputSize*/ Type.size_t, + /* dest*/ Type.void_t.out_ptr +); + +libxul.declareLazyFFI( + Primitives, + "decompress", + "workerlz4_decompress", + null, + /* return*/ Type.int, + /* const source*/ Type.void_t.in_ptr, + /* inputSize*/ Type.size_t, + /* dest*/ Type.void_t.out_ptr, + /* maxOutputSize*/ Type.size_t, + /* actualOutputSize*/ Type.size_t.out_ptr +); + +libxul.declareLazyFFI( + Primitives, + "maxCompressedSize", + "workerlz4_maxCompressedSize", + null, + /* return*/ Type.size_t, + /* inputSize*/ Type.size_t +); + +if (typeof module != "undefined") { + module.exports = { + get compress() { + return Primitives.compress; + }, + get decompress() { + return Primitives.decompress; + }, + get maxCompressedSize() { + return Primitives.maxCompressedSize; + }, + }; +} diff --git a/toolkit/components/lz4/moz.build b/toolkit/components/lz4/moz.build new file mode 100644 index 0000000000..b6259ddacc --- /dev/null +++ b/toolkit/components/lz4/moz.build @@ -0,0 +1,21 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Toolkit", "OS.File") + +XPCSHELL_TESTS_MANIFESTS += ["tests/xpcshell/xpcshell.ini"] + +EXTRA_JS_MODULES += [ + "lz4.js", + "lz4_internal.js", +] + +SOURCES += [ + "lz4.cpp", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/components/lz4/tests/xpcshell/data/chrome.manifest b/toolkit/components/lz4/tests/xpcshell/data/chrome.manifest new file mode 100644 index 0000000000..e2f9a9d8ef --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/data/chrome.manifest @@ -0,0 +1 @@ +content test_lz4 ./ diff --git a/toolkit/components/lz4/tests/xpcshell/data/compression.lz b/toolkit/components/lz4/tests/xpcshell/data/compression.lz Binary files differnew file mode 100644 index 0000000000..a354edc036 --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/data/compression.lz diff --git a/toolkit/components/lz4/tests/xpcshell/data/worker_lz4.js b/toolkit/components/lz4/tests/xpcshell/data/worker_lz4.js new file mode 100644 index 0000000000..d079e78b93 --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/data/worker_lz4.js @@ -0,0 +1,164 @@ +/* eslint-env mozilla/chrome-worker */ + +importScripts("resource://gre/modules/workers/require.js"); +importScripts("resource://gre/modules/osfile.jsm"); + +function info(x) { + // self.postMessage({kind: "do_print", args: [x]}); + dump("TEST-INFO: " + x + "\n"); +} + +const Assert = { + ok(x) { + self.postMessage({ kind: "assert_ok", args: [!!x] }); + if (x) { + dump("TEST-PASS: " + x + "\n"); + } else { + throw new Error("Assert.ok failed"); + } + }, + + equal(a, b) { + let result = a == b; + self.postMessage({ kind: "assert_ok", args: [result] }); + if (!result) { + throw new Error("Assert.equal failed " + a + " != " + b); + } + }, +}; + +function do_test_complete() { + self.postMessage({ kind: "do_test_complete", args: [] }); +} + +self.onmessage = function() { + try { + run_test(); + } catch (ex) { + let { message, moduleStack, moduleName, lineNumber } = ex; + let error = new Error(message, moduleName, lineNumber); + error.stack = moduleStack; + dump("Uncaught error: " + error + "\n"); + dump("Full stack: " + moduleStack + "\n"); + throw error; + } +}; + +var Lz4; +var Internals; +function test_import() { + Lz4 = require("resource://gre/modules/lz4.js"); + Internals = require("resource://gre/modules/lz4_internal.js"); +} + +function test_bound() { + for (let k of ["compress", "decompress", "maxCompressedSize"]) { + try { + info("Checking the existence of " + k + "\n"); + Assert.ok(!!Internals[k]); + info(k + " exists"); + } catch (ex) { + // Ignore errors + info(k + " doesn't exist!"); + } + } +} + +function test_reference_file() { + info("Decompress reference file"); + let path = OS.Path.join("data", "compression.lz"); + let data = OS.File.read(path); + let decompressed = Lz4.decompressFileContent(data); + let text = new TextDecoder().decode(decompressed); + Assert.equal(text, "Hello, lz4"); +} + +function compare_arrays(a, b) { + return Array.prototype.join.call(a) == Array.prototype.join.call(b); +} + +function run_rawcompression(name, array) { + info("Raw compression test " + name); + let length = array.byteLength; + let compressedArray = new Uint8Array(Internals.maxCompressedSize(length)); + let compressedBytes = Internals.compress(array, length, compressedArray); + compressedArray = new Uint8Array(compressedArray.buffer, 0, compressedBytes); + info("Raw compressed: " + length + " into " + compressedBytes); + + let decompressedArray = new Uint8Array(length); + let decompressedBytes = new ctypes.size_t(); + let success = Internals.decompress( + compressedArray, + compressedBytes, + decompressedArray, + length, + decompressedBytes.address() + ); + info("Raw decompression success? " + success); + info("Raw decompression size: " + decompressedBytes.value); + Assert.ok(compare_arrays(array, decompressedArray)); +} + +function run_filecompression(name, array) { + info("File compression test " + name); + let compressed = Lz4.compressFileContent(array); + info( + "Compressed " + array.byteLength + " bytes into " + compressed.byteLength + ); + + let decompressed = Lz4.decompressFileContent(compressed); + info( + "Decompressed " + + compressed.byteLength + + " bytes into " + + decompressed.byteLength + ); + Assert.ok(compare_arrays(array, decompressed)); +} + +function run_faileddecompression(name, array) { + info("invalid decompression test " + name); + + // Ensure that raw decompression doesn't segfault + let length = 1 << 14; + let decompressedArray = new Uint8Array(length); + let decompressedBytes = new ctypes.size_t(); + Internals.decompress( + array, + array.byteLength, + decompressedArray, + length, + decompressedBytes.address() + ); + + // File decompression should fail with an acceptable exception + let exn = null; + try { + Lz4.decompressFileContent(array); + } catch (ex) { + exn = ex; + } + Assert.ok(exn); + if (array.byteLength < 10) { + Assert.ok(exn.becauseLZNoHeader); + } else { + Assert.ok(exn.becauseLZWrongMagicNumber); + } +} + +function run_test() { + test_import(); + test_bound(); + test_reference_file(); + for (let length of [0, 1, 1024]) { + let array = new Uint8Array(length); + for (let i = 0; i < length; ++i) { + array[i] = i % 256; + } + let name = length + " bytes"; + run_rawcompression(name, array); + run_filecompression(name, array); + run_faileddecompression(name, array); + } + do_test_complete(); +} diff --git a/toolkit/components/lz4/tests/xpcshell/test_lz4.js b/toolkit/components/lz4/tests/xpcshell/test_lz4.js new file mode 100644 index 0000000000..d8da4457dc --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/test_lz4.js @@ -0,0 +1,35 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +var WORKER_SOURCE_URI = "chrome://test_lz4/content/worker_lz4.js"; +do_load_manifest("data/chrome.manifest"); + +add_task(function() { + let worker = new ChromeWorker(WORKER_SOURCE_URI); + return new Promise((resolve, reject) => { + worker.onmessage = function(event) { + let data = event.data; + switch (data.kind) { + case "assert_ok": + try { + Assert.ok(data.args[0]); + } catch (ex) { + // Ignore errors + } + return; + case "do_test_complete": + resolve(); + worker.terminate(); + break; + case "do_print": + info(data.args[0]); + } + }; + worker.onerror = function(event) { + let error = new Error(event.message, event.filename, event.lineno); + worker.terminate(); + reject(error); + }; + worker.postMessage("START"); + }); +}); diff --git a/toolkit/components/lz4/tests/xpcshell/test_lz4_sync.js b/toolkit/components/lz4/tests/xpcshell/test_lz4_sync.js new file mode 100644 index 0000000000..531da8a966 --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/test_lz4_sync.js @@ -0,0 +1,41 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const { Lz4 } = ChromeUtils.import("resource://gre/modules/lz4.js"); +const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm"); + +function compare_arrays(a, b) { + return Array.prototype.join.call(a) == Array.prototype.join.call(b); +} + +add_task(async function() { + let path = OS.Path.join("data", "compression.lz"); + let data = await OS.File.read(path); + let decompressed = Lz4.decompressFileContent(data); + let text = new TextDecoder().decode(decompressed); + Assert.equal(text, "Hello, lz4"); +}); + +add_task(async function() { + for (let length of [0, 1, 1024]) { + let array = new Uint8Array(length); + for (let i = 0; i < length; ++i) { + array[i] = i % 256; + } + + let compressed = Lz4.compressFileContent(array); + info( + "Compressed " + array.byteLength + " bytes into " + compressed.byteLength + ); + + let decompressed = Lz4.decompressFileContent(compressed); + info( + "Decompressed " + + compressed.byteLength + + " bytes into " + + decompressed.byteLength + ); + + Assert.ok(compare_arrays(array, decompressed)); + } +}); diff --git a/toolkit/components/lz4/tests/xpcshell/xpcshell.ini b/toolkit/components/lz4/tests/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..92a6a99207 --- /dev/null +++ b/toolkit/components/lz4/tests/xpcshell/xpcshell.ini @@ -0,0 +1,10 @@ +[DEFAULT] +head = +skip-if = toolkit == 'android' +support-files = + data/worker_lz4.js + data/chrome.manifest + data/compression.lz + +[test_lz4.js] +[test_lz4_sync.js] |