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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "mozilla/Compression.h"
6
7 /**
8 * LZ4 is a very fast byte-wise compression algorithm.
9 *
10 * Compared to Google's Snappy it is faster to compress and decompress and
11 * generally produces output of about the same size.
12 *
13 * Compared to zlib it compresses at about 10x the speed, decompresses at about
14 * 4x the speed and produces output of about 1.5x the size.
15 *
16 */
17
18 using namespace mozilla::Compression;
19
20 /**
21 * Compresses 'inputSize' bytes from 'source' into 'dest'.
22 * Destination buffer must be already allocated,
23 * and must be sized to handle worst cases situations (input data not compressible)
24 * Worst case size evaluation is provided by function LZ4_compressBound()
25 *
26 * @param inputSize is the input size. Max supported value is ~1.9GB
27 * @param return the number of bytes written in buffer dest
28 */
29 extern "C" MOZ_EXPORT size_t
workerlz4_compress(const char * source,size_t inputSize,char * dest)30 workerlz4_compress(const char* source, size_t inputSize, char* dest) {
31 return LZ4::compress(source, inputSize, dest);
32 }
33
34 /**
35 * If the source stream is malformed, the function will stop decoding
36 * and return a negative result, indicating the byte position of the
37 * faulty instruction
38 *
39 * This function never writes outside of provided buffers, and never
40 * modifies input buffer.
41 *
42 * note : destination buffer must be already allocated.
43 * its size must be a minimum of 'outputSize' bytes.
44 * @param outputSize is the output size, therefore the original size
45 * @return true/false
46 */
47 extern "C" MOZ_EXPORT int
workerlz4_decompress(const char * source,size_t inputSize,char * dest,size_t maxOutputSize,size_t * bytesOutput)48 workerlz4_decompress(const char* source, size_t inputSize,
49 char* dest, size_t maxOutputSize,
50 size_t *bytesOutput) {
51 return LZ4::decompress(source, inputSize,
52 dest, maxOutputSize,
53 bytesOutput);
54 }
55
56
57 /*
58 Provides the maximum size that LZ4 may output in a "worst case"
59 scenario (input data not compressible) primarily useful for memory
60 allocation of output buffer.
61 note : this function is limited by "int" range (2^31-1)
62
63 @param inputSize is the input size. Max supported value is ~1.9GB
64 @return maximum output size in a "worst case" scenario
65 */
66 extern "C" MOZ_EXPORT size_t
workerlz4_maxCompressedSize(size_t inputSize)67 workerlz4_maxCompressedSize(size_t inputSize)
68 {
69 return LZ4::maxCompressedSize(inputSize);
70 }
71
72
73
74