1 module deimos.lz4.lz4; 2 /* 3 LZ4 - Fast LZ compression algorithm 4 Header File 5 Copyright (C) 2011-2013, Yann Collet. 6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions are 10 met: 11 12 * Redistributions of source code must retain the above copyright 13 notice, this list of conditions and the following disclaimer. 14 * Redistributions in binary form must reproduce the above 15 copyright notice, this list of conditions and the following disclaimer 16 in the documentation and/or other materials provided with the 17 distribution. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 You can contact the author at : 32 - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html 33 - LZ4 source repository : http://code.google.com/p/lz4/ 34 */ 35 extern(C) nothrow: 36 37 38 //************************************** 39 // Compiler Options 40 //************************************** 41 //#if defined(_MSC_VER) && !defined(__cplusplus) // Visual Studio 42 //# define inline __inline // Visual C is not C99, but supports some kind of inline 43 //#endif 44 45 46 //**************************** 47 // Simple Functions 48 //**************************** 49 50 int LZ4_compress (const char* source, char* dest, int inputSize); 51 int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize); 52 53 /* 54 LZ4_compress() : 55 Compresses 'inputSize' bytes from 'source' into 'dest'. 56 Destination buffer must be already allocated, 57 and must be sized to handle worst cases situations (input data not compressible) 58 Worst case size evaluation is provided by function LZ4_compressBound() 59 inputSize : Max supported value is ~1.9GB 60 return : the number of bytes written in buffer dest 61 or 0 if the compression fails 62 63 LZ4_decompress_safe() : 64 maxOutputSize : is the size of the destination buffer (which must be already allocated) 65 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 66 If the source stream is detected malformed, the function will stop decoding and return a negative result. 67 This function is protected against buffer overflow exploits (never writes outside of output buffer, and never reads outside of input buffer). Therefore, it is protected against malicious data packets 68 */ 69 70 71 //**************************** 72 // Advanced Functions 73 //**************************** 74 75 static int LZ4_compressBound(int isize) { return ((isize) + ((isize)/255) + 16); } 76 template LZ4_COMPRESSBOUND(isize) 77 { 78 enum LZ4_COMPRESSBOUND = ((isize) + ((isize)/255) + 16); 79 } 80 81 /* 82 LZ4_compressBound() : 83 Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible) 84 primarily useful for memory allocation of output buffer. 85 inline function is recommended for the general case, 86 macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation). 87 88 isize : is the input size. Max supported value is ~1.9GB 89 return : maximum output size in a "worst case" scenario 90 note : this function is limited by "int" range (2^31-1) 91 */ 92 93 94 int LZ4_compress_limitedOutput (in char* source, char* dest, int inputSize, int maxOutputSize); 95 96 /* 97 LZ4_compress_limitedOutput() : 98 Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. 99 If it cannot achieve it, compression will stop, and result of the function will be zero. 100 This function never writes outside of provided output buffer. 101 102 inputSize : Max supported value is ~1.9GB 103 maxOutputSize : is the size of the destination buffer (which must be already allocated) 104 return : the number of bytes written in buffer 'dest' 105 or 0 if the compression fails 106 */ 107 108 109 int LZ4_decompress_fast (in char* source, char* dest, int outputSize); 110 111 /* 112 LZ4_decompress_fast() : 113 outputSize : is the original (uncompressed) size 114 return : the number of bytes read from the source buffer (in other words, the compressed size) 115 If the source stream is malformed, the function will stop decoding and return a negative result. 116 note : This function is a bit faster than LZ4_decompress_safe() 117 This function never writes outside of output buffers, but may read beyond input buffer in case of malicious data packet. 118 Use this function preferably into a trusted environment (data to decode comes from a trusted source). 119 Destination buffer must be already allocated. Its size must be a minimum of 'outputSize' bytes. 120 */ 121 122 int LZ4_decompress_safe_partial (in char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize); 123 124 /* 125 LZ4_decompress_safe_partial() : 126 This function decompress a compressed block of size 'inputSize' at position 'source' 127 into output buffer 'dest' of size 'maxOutputSize'. 128 The function stops decompressing operation as soon as 'targetOutputSize' has been reached, 129 reducing decompression time. 130 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 131 Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. 132 Always control how many bytes were decoded. 133 If the source stream is detected malformed, the function will stop decoding and return a negative result. 134 This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets 135 */ 136 137 138 //**************************** 139 // Stream Functions 140 //**************************** 141 142 void* LZ4_create (in char* inputBuffer); 143 int LZ4_compress_continue (void* LZ4_Data, in char* source, char* dest, int inputSize); 144 int LZ4_compress_limitedOutput_continue (void* LZ4_Data, in char* source, char* dest, int inputSize, int maxOutputSize); 145 char* LZ4_slideInputBuffer (void* LZ4_Data); 146 int LZ4_free (void* LZ4_Data); 147 148 /* 149 These functions allow the compression of dependent blocks, where each block benefits from prior 64 KB within preceding blocks. 150 In order to achieve this, it is necessary to start creating the LZ4 Data Structure, thanks to the function : 151 152 void* LZ4_create (const char* inputBuffer); 153 The result of the function is the (void*) pointer on the LZ4 Data Structure. 154 This pointer will be needed in all other functions. 155 If the pointer returned is NULL, then the allocation has failed, and compression must be aborted. 156 The only parameter 'const char* inputBuffer' must, obviously, point at the beginning of input buffer. 157 The input buffer must be already allocated, and size at least 192KB. 158 'inputBuffer' will also be the 'const char* source' of the first block. 159 160 All blocks are expected to lay next to each other within the input buffer, starting from 'inputBuffer'. 161 To compress each block, use either LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(). 162 Their behavior are identical to LZ4_compress() or LZ4_compress_limitedOutput(), 163 but require the LZ4 Data Structure as their first argument, and check that each block starts right after the previous one. 164 If next block does not begin immediately after the previous one, the compression will fail (return 0). 165 166 When it's no longer possible to lay the next block after the previous one (not enough space left into input buffer), a call to : 167 char* LZ4_slideInputBuffer(void* LZ4_Data); 168 must be performed. It will typically copy the latest 64KB of input at the beginning of input buffer. 169 Note that, for this function to work properly, minimum size of an input buffer must be 192KB. 170 ==> The memory position where the next input data block must start is provided as the result of the function. 171 172 Compression can then resume, using LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(), as usual. 173 174 When compression is completed, a call to LZ4_free() will release the memory used by the LZ4 Data Structure. 175 */ 176 177 178 int LZ4_decompress_safe_withPrefix64k (in char* source, char* dest, int inputSize, int maxOutputSize); 179 int LZ4_decompress_fast_withPrefix64k (in char* source, char* dest, int outputSize); 180 181 /* 182 *_withPrefix64k() : 183 These decoding functions work the same as their "normal name" versions, 184 but can use up to 64KB of data in front of 'char* dest'. 185 These functions are necessary to decode inter-dependant blocks. 186 */ 187 188 189 //**************************** 190 // Obsolete Functions 191 //**************************** 192 193 static int LZ4_uncompress (in char* source, char* dest, int outputSize) 194 { return LZ4_decompress_fast(source, dest, outputSize); } 195 static int LZ4_uncompress_unknownOutputSize (in char* source, char* dest, int isize, int maxOutputSize) 196 { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); } 197 198 /* 199 These functions are deprecated and should no longer be used. 200 They are provided here for compatibility with existing user programs. 201 */ 202