1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/lz4.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,276 @@ 1.4 +/* 1.5 + LZ4 - Fast LZ compression algorithm 1.6 + Header File 1.7 + Copyright (C) 2011-2014, Yann Collet. 1.8 + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 1.9 + 1.10 + Redistribution and use in source and binary forms, with or without 1.11 + modification, are permitted provided that the following conditions are 1.12 + met: 1.13 + 1.14 + * Redistributions of source code must retain the above copyright 1.15 + notice, this list of conditions and the following disclaimer. 1.16 + * Redistributions in binary form must reproduce the above 1.17 + copyright notice, this list of conditions and the following disclaimer 1.18 + in the documentation and/or other materials provided with the 1.19 + distribution. 1.20 + 1.21 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 + You can contact the author at : 1.34 + - LZ4 source repository : http://code.google.com/p/lz4/ 1.35 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c 1.36 +*/ 1.37 +#pragma once 1.38 + 1.39 +#if defined (__cplusplus) 1.40 +extern "C" { 1.41 +#endif 1.42 + 1.43 + 1.44 +/************************************** 1.45 + Version 1.46 +**************************************/ 1.47 +#define LZ4_VERSION_MAJOR 1 /* for major interface/format changes */ 1.48 +#define LZ4_VERSION_MINOR 2 /* for minor interface/format changes */ 1.49 +#define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ 1.50 + 1.51 + 1.52 +/************************************** 1.53 + Tuning parameter 1.54 +**************************************/ 1.55 +/* 1.56 + * LZ4_MEMORY_USAGE : 1.57 + * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) 1.58 + * Increasing memory usage improves compression ratio 1.59 + * Reduced memory usage can improve speed, due to cache effect 1.60 + * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache 1.61 + */ 1.62 +#define LZ4_MEMORY_USAGE 14 1.63 + 1.64 + 1.65 +/************************************** 1.66 + Simple Functions 1.67 +**************************************/ 1.68 + 1.69 +int LZ4_compress (const char* source, char* dest, int inputSize); 1.70 +int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxOutputSize); 1.71 + 1.72 +/* 1.73 +LZ4_compress() : 1.74 + Compresses 'inputSize' bytes from 'source' into 'dest'. 1.75 + Destination buffer must be already allocated, 1.76 + and must be sized to handle worst cases situations (input data not compressible) 1.77 + Worst case size evaluation is provided by function LZ4_compressBound() 1.78 + inputSize : Max supported value is LZ4_MAX_INPUT_VALUE 1.79 + return : the number of bytes written in buffer dest 1.80 + or 0 if the compression fails 1.81 + 1.82 +LZ4_decompress_safe() : 1.83 + compressedSize : is obviously the source size 1.84 + maxOutputSize : is the size of the destination buffer, which must be already allocated. 1.85 + return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 1.86 + If the destination buffer is not large enough, decoding will stop and output an error code (<0). 1.87 + If the source stream is detected malformed, the function will stop decoding and return a negative result. 1.88 + This function is protected against buffer overflow exploits : 1.89 + it never writes outside of output buffer, and never reads outside of input buffer. 1.90 + Therefore, it is protected against malicious data packets. 1.91 +*/ 1.92 + 1.93 + 1.94 +/* 1.95 +Note : 1.96 + Should you prefer to explicitly allocate compression-table memory using your own allocation method, 1.97 + use the streaming functions provided below, simply reset the memory area between each call to LZ4_compress_continue() 1.98 +*/ 1.99 + 1.100 + 1.101 +/************************************** 1.102 + Advanced Functions 1.103 +**************************************/ 1.104 +#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ 1.105 +#define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) 1.106 + 1.107 +/* 1.108 +LZ4_compressBound() : 1.109 + Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible) 1.110 + primarily useful for memory allocation of output buffer. 1.111 + macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation). 1.112 + 1.113 + isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE 1.114 + return : maximum output size in a "worst case" scenario 1.115 + or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE) 1.116 +*/ 1.117 +int LZ4_compressBound(int isize); 1.118 + 1.119 + 1.120 +/* 1.121 +LZ4_compress_limitedOutput() : 1.122 + Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. 1.123 + If it cannot achieve it, compression will stop, and result of the function will be zero. 1.124 + This function never writes outside of provided output buffer. 1.125 + 1.126 + inputSize : Max supported value is LZ4_MAX_INPUT_VALUE 1.127 + maxOutputSize : is the size of the destination buffer (which must be already allocated) 1.128 + return : the number of bytes written in buffer 'dest' 1.129 + or 0 if the compression fails 1.130 +*/ 1.131 +int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); 1.132 + 1.133 + 1.134 +/* 1.135 +LZ4_decompress_fast() : 1.136 + originalSize : is the original and therefore uncompressed size 1.137 + return : the number of bytes read from the source buffer (in other words, the compressed size) 1.138 + If the source stream is malformed, the function will stop decoding and return a negative result. 1.139 + Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. 1.140 + note : This function is a bit faster than LZ4_decompress_safe() 1.141 + It provides fast decompression and fully respect memory boundaries for properly formed compressed data. 1.142 + It does not provide full protection against intentionnally modified data stream. 1.143 + Use this function in a trusted environment (data to decode comes from a trusted source). 1.144 +*/ 1.145 +int LZ4_decompress_fast (const char* source, char* dest, int originalSize); 1.146 + 1.147 + 1.148 +/* 1.149 +LZ4_decompress_safe_partial() : 1.150 + This function decompress a compressed block of size 'compressedSize' at position 'source' 1.151 + into output buffer 'dest' of size 'maxOutputSize'. 1.152 + The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, 1.153 + reducing decompression time. 1.154 + return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 1.155 + Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. 1.156 + Always control how many bytes were decoded. 1.157 + If the source stream is detected malformed, the function will stop decoding and return a negative result. 1.158 + This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets 1.159 +*/ 1.160 +int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxOutputSize); 1.161 + 1.162 + 1.163 +/*********************************************** 1.164 + Experimental Streaming Compression Functions 1.165 +***********************************************/ 1.166 + 1.167 +#define LZ4_STREAMSIZE_U32 ((1 << (LZ4_MEMORY_USAGE-2)) + 8) 1.168 +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U32 * sizeof(unsigned int)) 1.169 +/* 1.170 + * LZ4_stream_t 1.171 + * information structure to track an LZ4 stream. 1.172 + * important : set this structure content to zero before first use ! 1.173 + */ 1.174 +typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t; 1.175 + 1.176 +/* 1.177 + * If you prefer dynamic allocation methods, 1.178 + * LZ4_createStream 1.179 + * provides a pointer (void*) towards an initialized LZ4_stream_t structure. 1.180 + * LZ4_free just frees it. 1.181 + */ 1.182 +void* LZ4_createStream(); 1.183 +int LZ4_free (void* LZ4_stream); 1.184 + 1.185 + 1.186 +/* 1.187 + * LZ4_loadDict 1.188 + * Use this function to load a static dictionary into LZ4_stream. 1.189 + * Any previous data will be forgotten, only 'dictionary' will remain in memory. 1.190 + * Loading a size of 0 is allowed (same effect as init). 1.191 + * Return : 1 if OK, 0 if error 1.192 + */ 1.193 +int LZ4_loadDict (void* LZ4_stream, const char* dictionary, int dictSize); 1.194 + 1.195 +/* 1.196 + * LZ4_compress_continue 1.197 + * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio 1.198 + * Previous data blocks are assumed to still be present at their previous location. 1.199 + */ 1.200 +int LZ4_compress_continue (void* LZ4_stream, const char* source, char* dest, int inputSize); 1.201 + 1.202 +/* 1.203 + * LZ4_compress_limitedOutput_continue 1.204 + * Same as before, but also specify a maximum target compressed size (maxOutputSize) 1.205 + * If objective cannot be met, compression exits, and returns a zero. 1.206 + */ 1.207 +int LZ4_compress_limitedOutput_continue (void* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize); 1.208 + 1.209 +/* 1.210 + * LZ4_saveDict 1.211 + * If previously compressed data block is not guaranteed to remain at its previous memory location 1.212 + * save it into a safe place (char* safeBuffer) 1.213 + * Note : you don't need to call LZ4_loadDict() afterwards, 1.214 + * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() 1.215 + * Return : 1 if OK, 0 if error 1.216 + * Note : any dictSize > 64 KB will be interpreted as 64KB. 1.217 + */ 1.218 +int LZ4_saveDict (void* LZ4_stream, char* safeBuffer, int dictSize); 1.219 + 1.220 + 1.221 +/************************************************ 1.222 + Experimental Streaming Decompression Functions 1.223 +************************************************/ 1.224 + 1.225 +#define LZ4_STREAMDECODESIZE_U32 4 1.226 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U32 * sizeof(unsigned int)) 1.227 +/* 1.228 + * LZ4_streamDecode_t 1.229 + * information structure to track an LZ4 stream. 1.230 + * important : set this structure content to zero before first use ! 1.231 + */ 1.232 +typedef struct { unsigned int table[LZ4_STREAMDECODESIZE_U32]; } LZ4_streamDecode_t; 1.233 + 1.234 +/* 1.235 + * If you prefer dynamic allocation methods, 1.236 + * LZ4_createStreamDecode() 1.237 + * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure. 1.238 + * LZ4_free just frees it. 1.239 + */ 1.240 +void* LZ4_createStreamDecode(); 1.241 +int LZ4_free (void* LZ4_stream); /* yes, it's the same one as for compression */ 1.242 + 1.243 +/* 1.244 +*_continue() : 1.245 + These decoding functions allow decompression of multiple blocks in "streaming" mode. 1.246 + Previously decoded blocks must still be available at the memory position where they were decoded. 1.247 + If it's not possible, save the relevant part of decoded data into a safe buffer, 1.248 + and indicate where it stands using LZ4_setDictDecode() 1.249 +*/ 1.250 +int LZ4_decompress_safe_continue (void* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize); 1.251 +int LZ4_decompress_fast_continue (void* LZ4_streamDecode, const char* source, char* dest, int originalSize); 1.252 + 1.253 +/* 1.254 + * LZ4_setDictDecode 1.255 + * Use this function to instruct where to find the dictionary. 1.256 + * This function can be used to specify a static dictionary, 1.257 + * or to instruct where to find some previously decoded data saved into a different memory space. 1.258 + * Setting a size of 0 is allowed (same effect as no dictionary). 1.259 + * Return : 1 if OK, 0 if error 1.260 + */ 1.261 +int LZ4_setDictDecode (void* LZ4_streamDecode, const char* dictionary, int dictSize); 1.262 + 1.263 + 1.264 +/* 1.265 +Advanced decoding functions : 1.266 +*_usingDict() : 1.267 + These decoding functions work the same as 1.268 + a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() 1.269 + all together into a single function call. 1.270 + It doesn't use nor update an LZ4_streamDecode_t structure. 1.271 +*/ 1.272 +int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize); 1.273 +int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); 1.274 + 1.275 + 1.276 + 1.277 +#if defined (__cplusplus) 1.278 +} 1.279 +#endif