nlib
lz4.h
1 /*
2  * LZ4 - Fast LZ compression algorithm
3  * Header File
4  * Copyright (C) 2011-2017, Yann Collet.
5 
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://www.lz4.org
33  - LZ4 source repository : https://github.com/lz4/lz4
34 */
35 #if defined (__cplusplus)
36 extern "C" {
37 #endif
38 
39 #ifndef LZ4_H_2983827168210
40 #define LZ4_H_2983827168210
41 
42 /* --- Dependency --- */
43 #include <stddef.h> /* size_t */
44 
45 
69 /*^***************************************************************
70 * Export parameters
71 *****************************************************************/
72 /*
73 * LZ4_DLL_EXPORT :
74 * Enable exporting of functions when building a Windows DLL
75 * LZ4LIB_VISIBILITY :
76 * Control library symbols visibility.
77 */
78 #ifndef LZ4LIB_VISIBILITY
79 # if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(CAFE)
80 # define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default")))
81 # else
82 # define LZ4LIB_VISIBILITY
83 # endif
84 #endif
85 #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
86 # define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
87 #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
88 # define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
89 #else
90 # define LZ4LIB_API LZ4LIB_VISIBILITY
91 #endif
92 
93 /*------ Version ------*/
94 #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
95 #define LZ4_VERSION_MINOR 8 /* for new (non-breaking) interface capabilities */
96 #define LZ4_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
97 
98 #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
99 
100 #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
101 #define LZ4_QUOTE(str) #str
102 #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
103 #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
104 
105 LZ4LIB_API int LZ4_versionNumber (void);
106 LZ4LIB_API const char* LZ4_versionString (void);
109 /*-************************************
110 * Tuning parameter
111 **************************************/
119 #ifndef LZ4_MEMORY_USAGE
120 # define LZ4_MEMORY_USAGE 14
121 #endif
122 
123 /*-************************************
124 * Simple Functions
125 **************************************/
139 LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
140 
149 LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
150 
151 
152 /*-************************************
153 * Advanced Functions
154 **************************************/
155 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
156 #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
157 
168 LZ4LIB_API int LZ4_compressBound(int inputSize);
169 
178 LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
179 
180 
188 LZ4LIB_API int LZ4_sizeofState(void);
189 LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
190 
191 
203 LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
204 
205 
221 LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
222 
235 LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
236 
237 
238 /*-*********************************************
239 * Streaming Compression Functions
240 ***********************************************/
241 typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
242 
247 LZ4LIB_API LZ4_stream_t* LZ4_createStream(void);
248 LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
249 
254 LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
255 
262 LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
263 
280 LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
281 
289 LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize);
290 
291 
292 /*-**********************************************
293 * Streaming Decompression Functions
294 * Bufferless synchronous API
295 ************************************************/
296 typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
297 
302 LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void);
303 LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
304 
312 LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
313 
325 LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
326 #define LZ4_DECODER_RING_BUFFER_SIZE(mbs) (65536 + 14 + (mbs)) /* for static allocation; mbs presumed valid */
327 
353 LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
354 LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
355 
356 
363 LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
364 LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
365 
366 
367 /*^**********************************************
368  * !!!!!! STATIC LINKING ONLY !!!!!!
369  ***********************************************/
370 
371 /*-************************************
372  * Unstable declarations
373  **************************************
374  * Declarations in this section should be considered unstable.
375  * Use at your own peril, etc., etc.
376  * They may be removed in the future.
377  * Their signatures may change.
378  **************************************/
379 
380 #ifdef LZ4_STATIC_LINKING_ONLY
381 
413 LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
414 
426 LZ4LIB_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
427 
454 LZ4LIB_API void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream);
455 
456 #endif
457 
458 /*-************************************
459  * Private definitions
460  **************************************
461  * Do not use these definitions.
462  * They are exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
463  * Using these definitions will expose code to API and/or ABI break in future versions of the library.
464  **************************************/
465 #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
466 #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
467 #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
468 
469 #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
470 #include <stdint.h>
471 
472 typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
473 struct LZ4_stream_t_internal {
474  uint32_t hashTable[LZ4_HASH_SIZE_U32];
475  uint32_t currentOffset;
476  uint16_t initCheck;
477  uint16_t tableType;
478  const uint8_t* dictionary;
479  const LZ4_stream_t_internal* dictCtx;
480  uint32_t dictSize;
481 };
482 
483 typedef struct {
484  const uint8_t* externalDict;
485  size_t extDictSize;
486  const uint8_t* prefixEnd;
487  size_t prefixSize;
488 } LZ4_streamDecode_t_internal;
489 
490 #else
491 
492 typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
493 struct LZ4_stream_t_internal {
494  unsigned int hashTable[LZ4_HASH_SIZE_U32];
495  unsigned int currentOffset;
496  unsigned short initCheck;
497  unsigned short tableType;
498  const unsigned char* dictionary;
499  const LZ4_stream_t_internal* dictCtx;
500  unsigned int dictSize;
501 };
502 
503 typedef struct {
504  const unsigned char* externalDict;
505  size_t extDictSize;
506  const unsigned char* prefixEnd;
507  size_t prefixSize;
508 } LZ4_streamDecode_t_internal;
509 
510 #endif
511 
520 #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
521 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
522 union LZ4_stream_u {
523  unsigned long long table[LZ4_STREAMSIZE_U64];
524  LZ4_stream_t_internal internal_donotuse;
525 } ; /* previously typedef'd to LZ4_stream_t */
526 
527 
536 #define LZ4_STREAMDECODESIZE_U64 4
537 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
538 union LZ4_streamDecode_u {
539  unsigned long long table[LZ4_STREAMDECODESIZE_U64];
540  LZ4_streamDecode_t_internal internal_donotuse;
541 } ; /* previously typedef'd to LZ4_streamDecode_t */
542 
543 
544 /*-************************************
545 * Obsolete Functions
546 **************************************/
547 
554 #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
555 # define LZ4_DEPRECATED(message) /* disable deprecation warnings */
556 #else
557 # define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
558 # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
559 # define LZ4_DEPRECATED(message) [[deprecated(message)]]
560 # elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
561 # define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
562 # elif (LZ4_GCC_VERSION >= 301)
563 # define LZ4_DEPRECATED(message) __attribute__((deprecated))
564 # elif defined(_MSC_VER)
565 # define LZ4_DEPRECATED(message) __declspec(deprecated(message))
566 # else
567 // # pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
568 # define LZ4_DEPRECATED(message) NLIB_DEPRECATED_MSG(message)
569 # endif
570 #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
571 
572 /* Obsolete compression functions */
573 LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize);
574 LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
575 LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
576 LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
577 LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
578 LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
579 
580 /* Obsolete decompression functions */
581 LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
582 LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
583 
584 /* Obsolete streaming functions; degraded functionality; do not use!
585  *
586  * In order to perform streaming compression, these functions depended on data
587  * that is no longer tracked in the state. They have been preserved as well as
588  * possible: using them will still produce a correct output. However, they don't
589  * actually retain any history between compression calls. The compression ratio
590  * achieved will therefore be no better than compressing each chunk
591  * independently.
592  */
593 LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer);
594 LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void);
595 LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer);
596 LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state);
597 
598 /* Obsolete streaming decoding functions */
599 LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
600 LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
601 
602 #endif /* LZ4_H_2983827168210 */
603 
604 
605 #if defined (__cplusplus)
606 }
607 #endif