michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: /* Various simple compression/decompression functions. */ michael@0: michael@0: #ifndef mozilla_Compression_h_ michael@0: #define mozilla_Compression_h_ michael@0: michael@0: #include "mozilla/Types.h" michael@0: #include "mozilla/Assertions.h" michael@0: michael@0: namespace mozilla { michael@0: namespace Compression { 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: class LZ4 michael@0: { michael@0: michael@0: public: 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 maxCompressedSize() 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: static MFBT_API size_t compress(const char* source, size_t inputSize, char* dest); michael@0: michael@0: /** michael@0: * Compress 'inputSize' bytes from 'source' into an output buffer michael@0: * 'dest' of maximum size 'maxOutputSize'. If it cannot achieve it, michael@0: * compression will stop, and result of the function will be zero, michael@0: * 'dest' will still be written to, but since the number of input michael@0: * bytes consumed is not returned the result is not usable. michael@0: * michael@0: * This function never writes outside of provided output buffer. michael@0: * michael@0: * @param inputSize is the input size. Max supported value is ~1.9GB michael@0: * @param maxOutputSize is the size of the destination buffer (which must be already allocated) michael@0: * @return the number of bytes written in buffer 'dest' michael@0: or 0 if the compression fails michael@0: */ michael@0: static MFBT_API size_t compressLimitedOutput(const char* source, size_t inputSize, char* dest, michael@0: size_t maxOutputSize); 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 the number of bytes read in the source buffer michael@0: */ michael@0: static MFBT_API bool decompress(const char* source, char* dest, size_t outputSize); michael@0: michael@0: /** michael@0: * If the source stream is malformed, the function will stop decoding michael@0: * and return false. michael@0: * michael@0: * This function never writes beyond dest + maxOutputSize, and is michael@0: * therefore protected against malicious data packets. michael@0: * michael@0: * note : Destination buffer must be already allocated. michael@0: * This version is slightly slower than the decompress michael@0: * without the maxOutputSize michael@0: * michael@0: * @param inputSize is the length of the input compressed data michael@0: * @param maxOutputSize is the size of the destination buffer (which must be already allocated) michael@0: * @param outputSize the actual number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) michael@0: michael@0: */ michael@0: static MFBT_API bool decompress(const char* source, size_t inputSize, char* dest, michael@0: size_t maxOutputSize, size_t *outputSize); 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: static inline size_t maxCompressedSize(size_t inputSize) michael@0: { michael@0: size_t max = ((inputSize) + ((inputSize)/255) + 16); michael@0: MOZ_ASSERT(max > inputSize); michael@0: return max; michael@0: } michael@0: michael@0: }; michael@0: michael@0: } /* namespace Compression */ michael@0: } /* namespace mozilla */ michael@0: michael@0: #endif /* mozilla_Compression_h_ */