OpenVPN
compat-lz4.h
Go to the documentation of this file.
1 /*
2  * LZ4 - Fast LZ compression algorithm
3  * Header File
4  * Copyright (C) 2011-present, 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 
76 /*^***************************************************************
77 * Export parameters
78 *****************************************************************/
79 /*
80 * LZ4_DLL_EXPORT :
81 * Enable exporting of functions when building a Windows DLL
82 * LZ4LIB_VISIBILITY :
83 * Control library symbols visibility.
84 */
85 #ifndef LZ4LIB_VISIBILITY
86 # if defined(__GNUC__) && (__GNUC__ >= 4)
87 # define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default")))
88 # else
89 # define LZ4LIB_VISIBILITY
90 # endif
91 #endif
92 #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
93 # define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
94 #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
95 # 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.*/
96 #else
97 # define LZ4LIB_API LZ4LIB_VISIBILITY
98 #endif
99 
100 /*------ Version ------*/
101 #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
102 #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
103 #define LZ4_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
104 
105 #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
106 
107 #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
108 #define LZ4_QUOTE(str) #str
109 #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
110 #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
111 
112 LZ4LIB_API int LZ4_versionNumber (void);
113 LZ4LIB_API const char* LZ4_versionString (void);
116 /*-************************************
117 * Tuning parameter
118 **************************************/
126 #ifndef LZ4_MEMORY_USAGE
127 # define LZ4_MEMORY_USAGE 14
128 #endif
129 
130 
131 /*-************************************
132 * Simple Functions
133 **************************************/
148 LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
149 
164 LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
165 
166 
167 /*-************************************
168 * Advanced Functions
169 **************************************/
170 #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
171 #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
172 
183 
191 LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
192 
193 
200 LZ4LIB_API int LZ4_sizeofState(void);
201 LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
202 
203 
216 LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
217 
218 
243 LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
244 
245 
246 /*-*********************************************
247 * Streaming Compression Functions
248 ***********************************************/
249 typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
250 
252 LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
253 
277 
289 LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
290 
314 LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
315 
323 LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize);
324 
325 
326 /*-**********************************************
327 * Streaming Decompression Functions
328 * Bufferless synchronous API
329 ************************************************/
330 typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
331 
338 
346 LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
347 
359 LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
360 #define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */
361 
387 LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
388 
389 
398 LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
399 
400 #endif /* LZ4_H_2983827168210 */
401 
402 
403 /*^*************************************
404  * !!!!!! STATIC LINKING ONLY !!!!!!
405  ***************************************/
406 
407 /*-****************************************************************************
408  * Experimental section
409  *
410  * Symbols declared in this section must be considered unstable. Their
411  * signatures or semantics may change, or they may be removed altogether in the
412  * future. They are therefore only safe to depend on when the caller is
413  * statically linked against the library.
414  *
415  * To protect against unsafe usage, not only are the declarations guarded,
416  * the definitions are hidden by default
417  * when building LZ4 as a shared/dynamic library.
418  *
419  * In order to access these declarations,
420  * define LZ4_STATIC_LINKING_ONLY in your application
421  * before including LZ4's headers.
422  *
423  * In order to make their implementations accessible dynamically, you must
424  * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
425  ******************************************************************************/
426 
427 #ifdef LZ4_STATIC_LINKING_ONLY
428 
429 #ifndef LZ4_STATIC_3504398509
430 #define LZ4_STATIC_3504398509
431 
432 #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
433 #define LZ4LIB_STATIC_API LZ4LIB_API
434 #else
435 #define LZ4LIB_STATIC_API
436 #endif
437 
438 
449 LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
450 
477 LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream);
478 
479 
531 #define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32)
532 #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))
534 #ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */
535 # define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
536 #endif
537 
538 #define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */
539 #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)
541 #endif /* LZ4_STATIC_3504398509 */
542 #endif /* LZ4_STATIC_LINKING_ONLY */
543 
544 
545 
546 #ifndef LZ4_H_98237428734687
547 #define LZ4_H_98237428734687
548 
549 /*-************************************************************
550  * PRIVATE DEFINITIONS
551  **************************************************************
552  * Do not use these definitions directly.
553  * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
554  * Accessing members will expose code to API and/or ABI break in future versions of the library.
555  **************************************************************/
556 #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
557 #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
558 #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
559 
560 #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
561 #include <stdint.h>
562 
564 struct LZ4_stream_t_internal {
567  uint16_t dirty;
569  const uint8_t* dictionary;
572 };
573 
574 typedef struct {
575  const uint8_t* externalDict;
576  size_t extDictSize;
577  const uint8_t* prefixEnd;
578  size_t prefixSize;
580 
581 #else
582 
586  unsigned int currentOffset;
587  unsigned short dirty;
588  unsigned short tableType;
589  const unsigned char* dictionary;
591  unsigned int dictSize;
592 };
593 
594 typedef struct {
595  const unsigned char* externalDict;
596  const unsigned char* prefixEnd;
597  size_t extDictSize;
598  size_t prefixSize;
600 
601 #endif
602 
612 #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ )
613 #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
615  unsigned long long table[LZ4_STREAMSIZE_U64];
617 } ; /* previously typedef'd to LZ4_stream_t */
618 
633 LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
634 
635 
643 #define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ )
644 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
646  unsigned long long table[LZ4_STREAMDECODESIZE_U64];
648 } ; /* previously typedef'd to LZ4_streamDecode_t */
649 
650 
651 
652 /*-************************************
653 * Obsolete Functions
654 **************************************/
655 
667 #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
668 # define LZ4_DEPRECATED(message) /* disable deprecation warnings */
669 #else
670 # define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
671 # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
672 # define LZ4_DEPRECATED(message) [[deprecated(message)]]
673 # elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
674 # define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
675 # elif (LZ4_GCC_VERSION >= 301)
676 # define LZ4_DEPRECATED(message) __attribute__((deprecated))
677 # elif defined(_MSC_VER)
678 # define LZ4_DEPRECATED(message) __declspec(deprecated(message))
679 # else
680 # pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
681 # define LZ4_DEPRECATED(message)
682 # endif
683 #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
684 
685 /* Obsolete compression functions */
686 LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize);
687 LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize);
688 LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
689 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);
690 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);
691 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);
692 
693 /* Obsolete decompression functions */
694 LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
695 LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
696 
697 /* Obsolete streaming functions; degraded functionality; do not use!
698  *
699  * In order to perform streaming compression, these functions depended on data
700  * that is no longer tracked in the state. They have been preserved as well as
701  * possible: using them will still produce a correct output. However, they don't
702  * actually retain any history between compression calls. The compression ratio
703  * achieved will therefore be no better than compressing each chunk
704  * independently.
705  */
706 LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer);
707 LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void);
708 LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer);
709 LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state);
710 
711 /* Obsolete streaming decoding functions */
712 LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
713 LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
714 
743 LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead")
744 LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
745 LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead")
746 LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
747 LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead")
748 LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
749 
756 LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
757 
758 
759 #endif /* LZ4_H_98237428734687 */
760 
761 
762 #if defined (__cplusplus)
763 }
764 #endif
#define LZ4_HASH_SIZE_U32
Definition: compat-lz4.h:558
LZ4LIB_API int LZ4_compress_fast(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration)
LZ4LIB_API int LZ4_decompress_fast_usingDict(const char *src, char *dst, int originalSize, const char *dictStart, int dictSize)
const unsigned char * prefixEnd
Definition: compat-lz4.h:596
LZ4LIB_API int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
LZ4LIB_API int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int srcSize, int dstCapacity)
#define LZ4_DEPRECATED(message)
Definition: compat-lz4.h:681
const unsigned char * dictionary
Definition: compat-lz4.h:589
LZ4LIB_API int LZ4_decompress_fast(const char *src, char *dst, int originalSize)
LZ4LIB_API int LZ4_decompress_safe_usingDict(const char *src, char *dst, int srcSize, int dstCapcity, const char *dictStart, int dictSize)
LZ4LIB_API int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration)
unsigned int hashTable[LZ4_HASH_SIZE_U32]
Definition: compat-lz4.h:585
char int int maxDstSize
Definition: compat-lz4.h:712
const char char int inputSize
Definition: compat-lz4.h:688
char int originalSize
Definition: compat-lz4.h:713
const LZ4_stream_t_internal * dictCtx
Definition: compat-lz4.h:590
LZ4LIB_API int LZ4_decompress_safe_partial(const char *src, char *dst, int srcSize, int targetOutputSize, int dstCapacity)
LZ4LIB_API const char * LZ4_versionString(void)
library version string; useful to check dll version
char int int maxOutputSize
Definition: compat-lz4.h:687
unsigned short tableType
Definition: compat-lz4.h:588
LZ4LIB_API int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize)
char * inputBuffer
Definition: compat-lz4.h:708
LZ4_stream_t_internal internal_donotuse
Definition: compat-lz4.h:616
LZ4LIB_API void LZ4_resetStream(LZ4_stream_t *streamPtr)
LZ4LIB_API int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream)
LZ4LIB_API int LZ4_compressBound(int inputSize)
LZ4LIB_API LZ4_streamDecode_t * LZ4_createStreamDecode(void)
LZ4LIB_API int LZ4_versionNumber(void)
library version number; useful to check dll version
LZ4LIB_API int LZ4_compress_fast_extState(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration)
char int outputSize
Definition: compat-lz4.h:694
LZ4LIB_API LZ4_stream_t * LZ4_createStream(void)
char int compressedSize
Definition: compat-lz4.h:712
unsigned __int32 uint32_t
Definition: config-msvc.h:157
unsigned short dirty
Definition: compat-lz4.h:587
LZ4LIB_API int LZ4_compress_default(const char *src, char *dst, int srcSize, int dstCapacity)
unsigned int currentOffset
Definition: compat-lz4.h:586
char int srcSize
Definition: compat-lz4.h:686
char * dest
Definition: compat-lz4.h:686
const char * source
Definition: compat-lz4.h:688
char int isize
Definition: compat-lz4.h:695
LZ4LIB_API int LZ4_freeStream(LZ4_stream_t *streamPtr)
LZ4LIB_API int LZ4_sizeofState(void)
#define LZ4LIB_API
Definition: compat-lz4.h:97
unsigned __int8 uint8_t
Definition: config-msvc.h:159
LZ4LIB_API LZ4_stream_t * LZ4_initStream(void *buffer, size_t size)
LZ4LIB_API int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary, int dictSize)
LZ4LIB_API int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int maxDictSize)
LZ4LIB_API int LZ4_decompress_safe(const char *src, char *dst, int compressedSize, int dstCapacity)
LZ4_streamDecode_t_internal internal_donotuse
Definition: compat-lz4.h:647
#define LZ4_STREAMDECODESIZE_U64
Definition: compat-lz4.h:643
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
LZ4LIB_API void LZ4_resetStream_fast(LZ4_stream_t *streamPtr)
LZ4LIB_API int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int originalSize)
unsigned __int16 uint16_t
Definition: config-msvc.h:158
unsigned int dictSize
Definition: compat-lz4.h:591
char * dst
Definition: compat-lz4.h:712
const unsigned char * externalDict
Definition: compat-lz4.h:595
#define LZ4_STREAMSIZE_U64
Definition: compat-lz4.h:612
LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize)