1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/workerlz4/lz4.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/Compression.h" 1.9 + 1.10 +/** 1.11 + * LZ4 is a very fast byte-wise compression algorithm. 1.12 + * 1.13 + * Compared to Google's Snappy it is faster to compress and decompress and 1.14 + * generally produces output of about the same size. 1.15 + * 1.16 + * Compared to zlib it compresses at about 10x the speed, decompresses at about 1.17 + * 4x the speed and produces output of about 1.5x the size. 1.18 + * 1.19 + */ 1.20 + 1.21 +using namespace mozilla::Compression; 1.22 + 1.23 +/** 1.24 + * Compresses 'inputSize' bytes from 'source' into 'dest'. 1.25 + * Destination buffer must be already allocated, 1.26 + * and must be sized to handle worst cases situations (input data not compressible) 1.27 + * Worst case size evaluation is provided by function LZ4_compressBound() 1.28 + * 1.29 + * @param inputSize is the input size. Max supported value is ~1.9GB 1.30 + * @param return the number of bytes written in buffer dest 1.31 + */ 1.32 +extern "C" MOZ_EXPORT size_t 1.33 +workerlz4_compress(const char* source, size_t inputSize, char* dest) { 1.34 + return LZ4::compress(source, inputSize, dest); 1.35 +} 1.36 + 1.37 +/** 1.38 + * If the source stream is malformed, the function will stop decoding 1.39 + * and return a negative result, indicating the byte position of the 1.40 + * faulty instruction 1.41 + * 1.42 + * This function never writes outside of provided buffers, and never 1.43 + * modifies input buffer. 1.44 + * 1.45 + * note : destination buffer must be already allocated. 1.46 + * its size must be a minimum of 'outputSize' bytes. 1.47 + * @param outputSize is the output size, therefore the original size 1.48 + * @return true/false 1.49 + */ 1.50 +extern "C" MOZ_EXPORT int 1.51 +workerlz4_decompress(const char* source, size_t inputSize, 1.52 + char* dest, size_t maxOutputSize, 1.53 + size_t *bytesOutput) { 1.54 + return LZ4::decompress(source, inputSize, 1.55 + dest, maxOutputSize, 1.56 + bytesOutput); 1.57 +} 1.58 + 1.59 + 1.60 +/* 1.61 + Provides the maximum size that LZ4 may output in a "worst case" 1.62 + scenario (input data not compressible) primarily useful for memory 1.63 + allocation of output buffer. 1.64 + note : this function is limited by "int" range (2^31-1) 1.65 + 1.66 + @param inputSize is the input size. Max supported value is ~1.9GB 1.67 + @return maximum output size in a "worst case" scenario 1.68 +*/ 1.69 +extern "C" MOZ_EXPORT size_t 1.70 +workerlz4_maxCompressedSize(size_t inputSize) 1.71 +{ 1.72 + return LZ4::maxCompressedSize(inputSize); 1.73 +} 1.74 + 1.75 + 1.76 +