mfbt/lz4.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 LZ4 - Fast LZ compression algorithm
michael@0 3 Header File
michael@0 4 Copyright (C) 2011-2014, Yann Collet.
michael@0 5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
michael@0 6
michael@0 7 Redistribution and use in source and binary forms, with or without
michael@0 8 modification, are permitted provided that the following conditions are
michael@0 9 met:
michael@0 10
michael@0 11 * Redistributions of source code must retain the above copyright
michael@0 12 notice, this list of conditions and the following disclaimer.
michael@0 13 * Redistributions in binary form must reproduce the above
michael@0 14 copyright notice, this list of conditions and the following disclaimer
michael@0 15 in the documentation and/or other materials provided with the
michael@0 16 distribution.
michael@0 17
michael@0 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 You can contact the author at :
michael@0 31 - LZ4 source repository : http://code.google.com/p/lz4/
michael@0 32 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
michael@0 33 */
michael@0 34 #pragma once
michael@0 35
michael@0 36 #if defined (__cplusplus)
michael@0 37 extern "C" {
michael@0 38 #endif
michael@0 39
michael@0 40
michael@0 41 /**************************************
michael@0 42 Version
michael@0 43 **************************************/
michael@0 44 #define LZ4_VERSION_MAJOR 1 /* for major interface/format changes */
michael@0 45 #define LZ4_VERSION_MINOR 2 /* for minor interface/format changes */
michael@0 46 #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
michael@0 47
michael@0 48
michael@0 49 /**************************************
michael@0 50 Tuning parameter
michael@0 51 **************************************/
michael@0 52 /*
michael@0 53 * LZ4_MEMORY_USAGE :
michael@0 54 * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
michael@0 55 * Increasing memory usage improves compression ratio
michael@0 56 * Reduced memory usage can improve speed, due to cache effect
michael@0 57 * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
michael@0 58 */
michael@0 59 #define LZ4_MEMORY_USAGE 14
michael@0 60
michael@0 61
michael@0 62 /**************************************
michael@0 63 Simple Functions
michael@0 64 **************************************/
michael@0 65
michael@0 66 int LZ4_compress (const char* source, char* dest, int inputSize);
michael@0 67 int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxOutputSize);
michael@0 68
michael@0 69 /*
michael@0 70 LZ4_compress() :
michael@0 71 Compresses 'inputSize' bytes from 'source' into 'dest'.
michael@0 72 Destination buffer must be already allocated,
michael@0 73 and must be sized to handle worst cases situations (input data not compressible)
michael@0 74 Worst case size evaluation is provided by function LZ4_compressBound()
michael@0 75 inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
michael@0 76 return : the number of bytes written in buffer dest
michael@0 77 or 0 if the compression fails
michael@0 78
michael@0 79 LZ4_decompress_safe() :
michael@0 80 compressedSize : is obviously the source size
michael@0 81 maxOutputSize : is the size of the destination buffer, which must be already allocated.
michael@0 82 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
michael@0 83 If the destination buffer is not large enough, decoding will stop and output an error code (<0).
michael@0 84 If the source stream is detected malformed, the function will stop decoding and return a negative result.
michael@0 85 This function is protected against buffer overflow exploits :
michael@0 86 it never writes outside of output buffer, and never reads outside of input buffer.
michael@0 87 Therefore, it is protected against malicious data packets.
michael@0 88 */
michael@0 89
michael@0 90
michael@0 91 /*
michael@0 92 Note :
michael@0 93 Should you prefer to explicitly allocate compression-table memory using your own allocation method,
michael@0 94 use the streaming functions provided below, simply reset the memory area between each call to LZ4_compress_continue()
michael@0 95 */
michael@0 96
michael@0 97
michael@0 98 /**************************************
michael@0 99 Advanced Functions
michael@0 100 **************************************/
michael@0 101 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
michael@0 102 #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
michael@0 103
michael@0 104 /*
michael@0 105 LZ4_compressBound() :
michael@0 106 Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
michael@0 107 primarily useful for memory allocation of output buffer.
michael@0 108 macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation).
michael@0 109
michael@0 110 isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
michael@0 111 return : maximum output size in a "worst case" scenario
michael@0 112 or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
michael@0 113 */
michael@0 114 int LZ4_compressBound(int isize);
michael@0 115
michael@0 116
michael@0 117 /*
michael@0 118 LZ4_compress_limitedOutput() :
michael@0 119 Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
michael@0 120 If it cannot achieve it, compression will stop, and result of the function will be zero.
michael@0 121 This function never writes outside of provided output buffer.
michael@0 122
michael@0 123 inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
michael@0 124 maxOutputSize : is the size of the destination buffer (which must be already allocated)
michael@0 125 return : the number of bytes written in buffer 'dest'
michael@0 126 or 0 if the compression fails
michael@0 127 */
michael@0 128 int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
michael@0 129
michael@0 130
michael@0 131 /*
michael@0 132 LZ4_decompress_fast() :
michael@0 133 originalSize : is the original and therefore uncompressed size
michael@0 134 return : the number of bytes read from the source buffer (in other words, the compressed size)
michael@0 135 If the source stream is malformed, the function will stop decoding and return a negative result.
michael@0 136 Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
michael@0 137 note : This function is a bit faster than LZ4_decompress_safe()
michael@0 138 It provides fast decompression and fully respect memory boundaries for properly formed compressed data.
michael@0 139 It does not provide full protection against intentionnally modified data stream.
michael@0 140 Use this function in a trusted environment (data to decode comes from a trusted source).
michael@0 141 */
michael@0 142 int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
michael@0 143
michael@0 144
michael@0 145 /*
michael@0 146 LZ4_decompress_safe_partial() :
michael@0 147 This function decompress a compressed block of size 'compressedSize' at position 'source'
michael@0 148 into output buffer 'dest' of size 'maxOutputSize'.
michael@0 149 The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
michael@0 150 reducing decompression time.
michael@0 151 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
michael@0 152 Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
michael@0 153 Always control how many bytes were decoded.
michael@0 154 If the source stream is detected malformed, the function will stop decoding and return a negative result.
michael@0 155 This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
michael@0 156 */
michael@0 157 int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxOutputSize);
michael@0 158
michael@0 159
michael@0 160 /***********************************************
michael@0 161 Experimental Streaming Compression Functions
michael@0 162 ***********************************************/
michael@0 163
michael@0 164 #define LZ4_STREAMSIZE_U32 ((1 << (LZ4_MEMORY_USAGE-2)) + 8)
michael@0 165 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U32 * sizeof(unsigned int))
michael@0 166 /*
michael@0 167 * LZ4_stream_t
michael@0 168 * information structure to track an LZ4 stream.
michael@0 169 * important : set this structure content to zero before first use !
michael@0 170 */
michael@0 171 typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t;
michael@0 172
michael@0 173 /*
michael@0 174 * If you prefer dynamic allocation methods,
michael@0 175 * LZ4_createStream
michael@0 176 * provides a pointer (void*) towards an initialized LZ4_stream_t structure.
michael@0 177 * LZ4_free just frees it.
michael@0 178 */
michael@0 179 void* LZ4_createStream();
michael@0 180 int LZ4_free (void* LZ4_stream);
michael@0 181
michael@0 182
michael@0 183 /*
michael@0 184 * LZ4_loadDict
michael@0 185 * Use this function to load a static dictionary into LZ4_stream.
michael@0 186 * Any previous data will be forgotten, only 'dictionary' will remain in memory.
michael@0 187 * Loading a size of 0 is allowed (same effect as init).
michael@0 188 * Return : 1 if OK, 0 if error
michael@0 189 */
michael@0 190 int LZ4_loadDict (void* LZ4_stream, const char* dictionary, int dictSize);
michael@0 191
michael@0 192 /*
michael@0 193 * LZ4_compress_continue
michael@0 194 * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio
michael@0 195 * Previous data blocks are assumed to still be present at their previous location.
michael@0 196 */
michael@0 197 int LZ4_compress_continue (void* LZ4_stream, const char* source, char* dest, int inputSize);
michael@0 198
michael@0 199 /*
michael@0 200 * LZ4_compress_limitedOutput_continue
michael@0 201 * Same as before, but also specify a maximum target compressed size (maxOutputSize)
michael@0 202 * If objective cannot be met, compression exits, and returns a zero.
michael@0 203 */
michael@0 204 int LZ4_compress_limitedOutput_continue (void* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize);
michael@0 205
michael@0 206 /*
michael@0 207 * LZ4_saveDict
michael@0 208 * If previously compressed data block is not guaranteed to remain at its previous memory location
michael@0 209 * save it into a safe place (char* safeBuffer)
michael@0 210 * Note : you don't need to call LZ4_loadDict() afterwards,
michael@0 211 * dictionary is immediately usable, you can therefore call again LZ4_compress_continue()
michael@0 212 * Return : 1 if OK, 0 if error
michael@0 213 * Note : any dictSize > 64 KB will be interpreted as 64KB.
michael@0 214 */
michael@0 215 int LZ4_saveDict (void* LZ4_stream, char* safeBuffer, int dictSize);
michael@0 216
michael@0 217
michael@0 218 /************************************************
michael@0 219 Experimental Streaming Decompression Functions
michael@0 220 ************************************************/
michael@0 221
michael@0 222 #define LZ4_STREAMDECODESIZE_U32 4
michael@0 223 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U32 * sizeof(unsigned int))
michael@0 224 /*
michael@0 225 * LZ4_streamDecode_t
michael@0 226 * information structure to track an LZ4 stream.
michael@0 227 * important : set this structure content to zero before first use !
michael@0 228 */
michael@0 229 typedef struct { unsigned int table[LZ4_STREAMDECODESIZE_U32]; } LZ4_streamDecode_t;
michael@0 230
michael@0 231 /*
michael@0 232 * If you prefer dynamic allocation methods,
michael@0 233 * LZ4_createStreamDecode()
michael@0 234 * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure.
michael@0 235 * LZ4_free just frees it.
michael@0 236 */
michael@0 237 void* LZ4_createStreamDecode();
michael@0 238 int LZ4_free (void* LZ4_stream); /* yes, it's the same one as for compression */
michael@0 239
michael@0 240 /*
michael@0 241 *_continue() :
michael@0 242 These decoding functions allow decompression of multiple blocks in "streaming" mode.
michael@0 243 Previously decoded blocks must still be available at the memory position where they were decoded.
michael@0 244 If it's not possible, save the relevant part of decoded data into a safe buffer,
michael@0 245 and indicate where it stands using LZ4_setDictDecode()
michael@0 246 */
michael@0 247 int LZ4_decompress_safe_continue (void* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize);
michael@0 248 int LZ4_decompress_fast_continue (void* LZ4_streamDecode, const char* source, char* dest, int originalSize);
michael@0 249
michael@0 250 /*
michael@0 251 * LZ4_setDictDecode
michael@0 252 * Use this function to instruct where to find the dictionary.
michael@0 253 * This function can be used to specify a static dictionary,
michael@0 254 * or to instruct where to find some previously decoded data saved into a different memory space.
michael@0 255 * Setting a size of 0 is allowed (same effect as no dictionary).
michael@0 256 * Return : 1 if OK, 0 if error
michael@0 257 */
michael@0 258 int LZ4_setDictDecode (void* LZ4_streamDecode, const char* dictionary, int dictSize);
michael@0 259
michael@0 260
michael@0 261 /*
michael@0 262 Advanced decoding functions :
michael@0 263 *_usingDict() :
michael@0 264 These decoding functions work the same as
michael@0 265 a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue()
michael@0 266 all together into a single function call.
michael@0 267 It doesn't use nor update an LZ4_streamDecode_t structure.
michael@0 268 */
michael@0 269 int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize);
michael@0 270 int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize);
michael@0 271
michael@0 272
michael@0 273
michael@0 274 #if defined (__cplusplus)
michael@0 275 }
michael@0 276 #endif

mercurial