]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lz4.h
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / lz4.h
1 /*
2    LZ4 - Fast LZ compression algorithm
3    Header File
4    Copyright (C) 2011-2013, Yann Collet.
5    BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions are
9    met:
10
11        * Redistributions of source code must retain the above copyright
12    notice, this list of conditions and the following disclaimer.
13        * Redistributions in binary form must reproduce the above
14    copyright notice, this list of conditions and the following disclaimer
15    in the documentation and/or other materials provided with the
16    distribution.
17
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30    You can contact the author at :
31    - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
32    - LZ4 source repository : http://code.google.com/p/lz4/
33 */
34 #pragma once
35
36 #if defined (__cplusplus)
37 extern "C" {
38 #endif
39
40
41 //**************************************
42 // Compiler Options
43 //**************************************
44 #if defined(_MSC_VER) && !defined(__cplusplus)   // Visual Studio
45 #  define inline __forceinline           // Visual C is not C99, but supports some kind of inline. Note : we *do* want to force inline
46 #endif
47
48
49 //****************************
50 // Simple Functions
51 //****************************
52
53 int LZ4_compress        (const char* source, char* dest, int inputSize);
54 int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize);
55
56 /*
57 LZ4_compress() :
58     Compresses 'inputSize' bytes from 'source' into 'dest'.
59     Destination buffer must be already allocated,
60     and must be sized to handle worst cases situations (input data not compressible)
61     Worst case size evaluation is provided by function LZ4_compressBound()
62     inputSize : Max supported value is ~1.9GB
63     return : the number of bytes written in buffer dest
64              or 0 if the compression fails
65
66 LZ4_decompress_safe() :
67     maxOutputSize : is the size of the destination buffer (which must be already allocated)
68     return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
69              If the source stream is malformed or too large, the function will stop decoding and return a negative result.
70              This function is protected against any kind of buffer overflow attemps (never writes outside of output buffer, and never reads outside of input buffer). It is therefore protected against malicious data packets
71 */
72
73
74 //****************************
75 // Advanced Functions
76 //****************************
77
78 static inline int LZ4_compressBound(int isize)   { return ((isize) + ((isize)/255) + 16); }
79 #define           LZ4_COMPRESSBOUND(    isize)            ((isize) + ((isize)/255) + 16)
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 compile time (such as table size 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 (const 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 (const 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, and never read before input buffer, but may read beyond input buffer (since it doesn't know its size) 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 (const 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 might be < 'targetOutputSize' if the number of bytes to decode into the compressed block is not enough.
132              Always control how many bytes were  decoded.
133              If the source stream is 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 int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int inputSize, int maxOutputSize);
139 int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int outputSize);
140
141 /*
142 *_withPrefix64k() :
143     These decoding functions work the same as their "normal name" versions,
144     but will potentially use up to 64KB of data in front of 'char* dest'.
145     These functions are used for decoding inter-dependant blocks.
146 */
147
148
149 //****************************
150 // Obsolete Functions
151 //****************************
152
153 static inline int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); }
154 static inline int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); }
155
156 /*
157 These functions are deprecated and should no longer be used.
158 They are provided here for compatibility with existing user programs.
159 */
160
161
162
163 #if defined (__cplusplus)
164 }
165 #endif