1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/Compression.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* Various simple compression/decompression functions. */ 1.10 + 1.11 +#ifndef mozilla_Compression_h_ 1.12 +#define mozilla_Compression_h_ 1.13 + 1.14 +#include "mozilla/Types.h" 1.15 +#include "mozilla/Assertions.h" 1.16 + 1.17 +namespace mozilla { 1.18 +namespace Compression { 1.19 + 1.20 +/** 1.21 + * LZ4 is a very fast byte-wise compression algorithm. 1.22 + * 1.23 + * Compared to Google's Snappy it is faster to compress and decompress and 1.24 + * generally produces output of about the same size. 1.25 + * 1.26 + * Compared to zlib it compresses at about 10x the speed, decompresses at about 1.27 + * 4x the speed and produces output of about 1.5x the size. 1.28 + * 1.29 + */ 1.30 + 1.31 +class LZ4 1.32 +{ 1.33 + 1.34 +public: 1.35 + 1.36 + /** 1.37 + * Compresses 'inputSize' bytes from 'source' into 'dest'. 1.38 + * Destination buffer must be already allocated, 1.39 + * and must be sized to handle worst cases situations (input data not compressible) 1.40 + * Worst case size evaluation is provided by function maxCompressedSize() 1.41 + * 1.42 + * @param inputSize is the input size. Max supported value is ~1.9GB 1.43 + * @param return the number of bytes written in buffer dest 1.44 + */ 1.45 + static MFBT_API size_t compress(const char* source, size_t inputSize, char* dest); 1.46 + 1.47 + /** 1.48 + * Compress 'inputSize' bytes from 'source' into an output buffer 1.49 + * 'dest' of maximum size 'maxOutputSize'. If it cannot achieve it, 1.50 + * compression will stop, and result of the function will be zero, 1.51 + * 'dest' will still be written to, but since the number of input 1.52 + * bytes consumed is not returned the result is not usable. 1.53 + * 1.54 + * This function never writes outside of provided output buffer. 1.55 + * 1.56 + * @param inputSize is the input size. Max supported value is ~1.9GB 1.57 + * @param maxOutputSize is the size of the destination buffer (which must be already allocated) 1.58 + * @return the number of bytes written in buffer 'dest' 1.59 + or 0 if the compression fails 1.60 + */ 1.61 + static MFBT_API size_t compressLimitedOutput(const char* source, size_t inputSize, char* dest, 1.62 + size_t maxOutputSize); 1.63 + 1.64 + /** 1.65 + * If the source stream is malformed, the function will stop decoding 1.66 + * and return a negative result, indicating the byte position of the 1.67 + * faulty instruction 1.68 + * 1.69 + * This function never writes outside of provided buffers, and never 1.70 + * modifies input buffer. 1.71 + * 1.72 + * note : destination buffer must be already allocated. 1.73 + * its size must be a minimum of 'outputSize' bytes. 1.74 + * @param outputSize is the output size, therefore the original size 1.75 + * @return the number of bytes read in the source buffer 1.76 + */ 1.77 + static MFBT_API bool decompress(const char* source, char* dest, size_t outputSize); 1.78 + 1.79 + /** 1.80 + * If the source stream is malformed, the function will stop decoding 1.81 + * and return false. 1.82 + * 1.83 + * This function never writes beyond dest + maxOutputSize, and is 1.84 + * therefore protected against malicious data packets. 1.85 + * 1.86 + * note : Destination buffer must be already allocated. 1.87 + * This version is slightly slower than the decompress 1.88 + * without the maxOutputSize 1.89 + * 1.90 + * @param inputSize is the length of the input compressed data 1.91 + * @param maxOutputSize is the size of the destination buffer (which must be already allocated) 1.92 + * @param outputSize the actual number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 1.93 + 1.94 + */ 1.95 + static MFBT_API bool decompress(const char* source, size_t inputSize, char* dest, 1.96 + size_t maxOutputSize, size_t *outputSize); 1.97 + 1.98 + /* 1.99 + Provides the maximum size that LZ4 may output in a "worst case" 1.100 + scenario (input data not compressible) primarily useful for memory 1.101 + allocation of output buffer. 1.102 + note : this function is limited by "int" range (2^31-1) 1.103 + 1.104 + @param inputSize is the input size. Max supported value is ~1.9GB 1.105 + @return maximum output size in a "worst case" scenario 1.106 + */ 1.107 + static inline size_t maxCompressedSize(size_t inputSize) 1.108 + { 1.109 + size_t max = ((inputSize) + ((inputSize)/255) + 16); 1.110 + MOZ_ASSERT(max > inputSize); 1.111 + return max; 1.112 + } 1.113 + 1.114 +}; 1.115 + 1.116 +} /* namespace Compression */ 1.117 +} /* namespace mozilla */ 1.118 + 1.119 +#endif /* mozilla_Compression_h_ */