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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Compression.h" michael@0: michael@0: /** michael@0: * LZ4 is a very fast byte-wise compression algorithm. michael@0: * michael@0: * Compared to Google's Snappy it is faster to compress and decompress and michael@0: * generally produces output of about the same size. michael@0: * michael@0: * Compared to zlib it compresses at about 10x the speed, decompresses at about michael@0: * 4x the speed and produces output of about 1.5x the size. michael@0: * michael@0: */ michael@0: michael@0: using namespace mozilla::Compression; michael@0: michael@0: /** michael@0: * Compresses 'inputSize' bytes from 'source' into 'dest'. michael@0: * Destination buffer must be already allocated, michael@0: * and must be sized to handle worst cases situations (input data not compressible) michael@0: * Worst case size evaluation is provided by function LZ4_compressBound() michael@0: * michael@0: * @param inputSize is the input size. Max supported value is ~1.9GB michael@0: * @param return the number of bytes written in buffer dest michael@0: */ michael@0: extern "C" MOZ_EXPORT size_t michael@0: workerlz4_compress(const char* source, size_t inputSize, char* dest) { michael@0: return LZ4::compress(source, inputSize, dest); michael@0: } michael@0: michael@0: /** michael@0: * If the source stream is malformed, the function will stop decoding michael@0: * and return a negative result, indicating the byte position of the michael@0: * faulty instruction michael@0: * michael@0: * This function never writes outside of provided buffers, and never michael@0: * modifies input buffer. michael@0: * michael@0: * note : destination buffer must be already allocated. michael@0: * its size must be a minimum of 'outputSize' bytes. michael@0: * @param outputSize is the output size, therefore the original size michael@0: * @return true/false michael@0: */ michael@0: extern "C" MOZ_EXPORT int michael@0: workerlz4_decompress(const char* source, size_t inputSize, michael@0: char* dest, size_t maxOutputSize, michael@0: size_t *bytesOutput) { michael@0: return LZ4::decompress(source, inputSize, michael@0: dest, maxOutputSize, michael@0: bytesOutput); michael@0: } michael@0: michael@0: michael@0: /* michael@0: Provides the maximum size that LZ4 may output in a "worst case" michael@0: scenario (input data not compressible) primarily useful for memory michael@0: allocation of output buffer. michael@0: note : this function is limited by "int" range (2^31-1) michael@0: michael@0: @param inputSize is the input size. Max supported value is ~1.9GB michael@0: @return maximum output size in a "worst case" scenario michael@0: */ michael@0: extern "C" MOZ_EXPORT size_t michael@0: workerlz4_maxCompressedSize(size_t inputSize) michael@0: { michael@0: return LZ4::maxCompressedSize(inputSize); michael@0: } michael@0: michael@0: michael@0: