Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * LZ4 auto-framing library  | 
3  |  |  * Copyright (c) Yann Collet. All rights reserved.  | 
4  |  |  *  | 
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://www.lz4.org  | 
32  |  |  * - LZ4 source repository : https://github.com/lz4/lz4  | 
33  |  |  */  | 
34  |  |  | 
35  |  | /* LZ4F is a stand-alone API to create LZ4-compressed Frames  | 
36  |  |  * in full conformance with specification v1.6.1 .  | 
37  |  |  * This library rely upon memory management capabilities (malloc, free)  | 
38  |  |  * provided either by <stdlib.h>,  | 
39  |  |  * or redirected towards another library of user's choice  | 
40  |  |  * (see Memory Routines below).  | 
41  |  |  */  | 
42  |  |  | 
43  |  |  | 
44  |  | /*-************************************  | 
45  |  | *  Compiler Options  | 
46  |  | **************************************/  | 
47  |  | #include <limits.h>  | 
48  |  | #ifdef _MSC_VER    /* Visual Studio */  | 
49  |  | #  pragma warning(disable : 4127)   /* disable: C4127: conditional expression is constant */  | 
50  |  | #endif  | 
51  |  |  | 
52  |  |  | 
53  |  | /*-************************************  | 
54  |  | *  Tuning parameters  | 
55  |  | **************************************/  | 
56  |  | /*  | 
57  |  |  * LZ4F_HEAPMODE :  | 
58  |  |  * Control how LZ4F_compressFrame allocates the Compression State,  | 
59  |  |  * either on stack (0:default, fastest), or in memory heap (1:requires malloc()).  | 
60  |  |  */  | 
61  |  | #ifndef LZ4F_HEAPMODE  | 
62  |  | #  define LZ4F_HEAPMODE 0  | 
63  |  | #endif  | 
64  |  |  | 
65  |  |  | 
66  |  | /*-************************************  | 
67  |  | *  Library declarations  | 
68  |  | **************************************/  | 
69  |  | #define LZ4F_STATIC_LINKING_ONLY  | 
70  |  | #include "lz4frame.h"  | 
71  |  | #define LZ4_STATIC_LINKING_ONLY  | 
72  |  | #include "lz4.h"  | 
73  |  | #define LZ4_HC_STATIC_LINKING_ONLY  | 
74  |  | #include "lz4hc.h"  | 
75  |  | #define XXH_STATIC_LINKING_ONLY  | 
76  |  | #include "xxhash.h"  | 
77  |  |  | 
78  |  |  | 
79  |  | /*-************************************  | 
80  |  | *  Memory routines  | 
81  |  | **************************************/  | 
82  |  | /*  | 
83  |  |  * User may redirect invocations of  | 
84  |  |  * malloc(), calloc() and free()  | 
85  |  |  * towards another library or solution of their choice  | 
86  |  |  * by modifying below section.  | 
87  |  | **/  | 
88  |  |  | 
89  |  | #include <string.h>   /* memset, memcpy, memmove */  | 
90  |  | #ifndef LZ4_SRC_INCLUDED  /* avoid redefinition when sources are coalesced */  | 
91  | 0  | #  define MEM_INIT(p,v,s)   memset((p),(v),(s))  | 
92  |  | #endif  | 
93  |  |  | 
94  |  | #ifndef LZ4_SRC_INCLUDED   /* avoid redefinition when sources are coalesced */  | 
95  |  | #  include <stdlib.h>   /* malloc, calloc, free */  | 
96  | 0  | #  define ALLOC(s)          malloc(s)  | 
97  | 0  | #  define ALLOC_AND_ZERO(s) calloc(1,(s))  | 
98  | 0  | #  define FREEMEM(p)        free(p)  | 
99  |  | #endif  | 
100  |  |  | 
101  |  | static void* LZ4F_calloc(size_t s, LZ4F_CustomMem cmem)  | 
102  | 0  | { | 
103  |  |     /* custom calloc defined : use it */  | 
104  | 0  |     if (cmem.customCalloc != NULL) { | 
105  | 0  |         return cmem.customCalloc(cmem.opaqueState, s);  | 
106  | 0  |     }  | 
107  |  |     /* nothing defined : use default <stdlib.h>'s calloc() */  | 
108  | 0  |     if (cmem.customAlloc == NULL) { | 
109  | 0  |         return ALLOC_AND_ZERO(s);  | 
110  | 0  |     }  | 
111  |  |     /* only custom alloc defined : use it, and combine it with memset() */  | 
112  | 0  |     {   void* const p = cmem.customAlloc(cmem.opaqueState, s); | 
113  | 0  |         if (p != NULL) MEM_INIT(p, 0, s);  | 
114  | 0  |         return p;  | 
115  | 0  | }   }  | 
116  |  |  | 
117  |  | static void* LZ4F_malloc(size_t s, LZ4F_CustomMem cmem)  | 
118  | 0  | { | 
119  |  |     /* custom malloc defined : use it */  | 
120  | 0  |     if (cmem.customAlloc != NULL) { | 
121  | 0  |         return cmem.customAlloc(cmem.opaqueState, s);  | 
122  | 0  |     }  | 
123  |  |     /* nothing defined : use default <stdlib.h>'s malloc() */  | 
124  | 0  |     return ALLOC(s);  | 
125  | 0  | }  | 
126  |  |  | 
127  |  | static void LZ4F_free(void* p, LZ4F_CustomMem cmem)  | 
128  | 0  | { | 
129  | 0  |     if (p == NULL) return;  | 
130  | 0  |     if (cmem.customFree != NULL) { | 
131  |  |         /* custom allocation defined : use it */  | 
132  | 0  |         cmem.customFree(cmem.opaqueState, p);  | 
133  | 0  |         return;  | 
134  | 0  |     }  | 
135  |  |     /* nothing defined : use default <stdlib.h>'s free() */  | 
136  | 0  |     FREEMEM(p);  | 
137  | 0  | }  | 
138  |  |  | 
139  |  |  | 
140  |  | /*-************************************  | 
141  |  | *  Debug  | 
142  |  | **************************************/  | 
143  |  | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1)  | 
144  |  | #  include <assert.h>  | 
145  |  | #else  | 
146  |  | #  ifndef assert  | 
147  |  | #    define assert(condition) ((void)0)  | 
148  |  | #  endif  | 
149  |  | #endif  | 
150  |  |  | 
151  | 0  | #define LZ4F_STATIC_ASSERT(c)    { enum { LZ4F_static_assert = 1/(int)(!!(c)) }; }   /* use only *after* variable declarations */ | 
152  |  |  | 
153  |  | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) && !defined(DEBUGLOG)  | 
154  |  | #  include <stdio.h>  | 
155  |  | static int g_debuglog_enable = 1;  | 
156  |  | #  define DEBUGLOG(l, ...) {                                  \ | 
157  |  |                 if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) {  \ | 
158  |  |                     fprintf(stderr, __FILE__ " %i: ", __LINE__ );  \  | 
159  |  |                     fprintf(stderr, __VA_ARGS__);             \  | 
160  |  |                     fprintf(stderr, " \n");                   \  | 
161  |  |             }   }  | 
162  |  | #else  | 
163  | 0  | #  define DEBUGLOG(l, ...)      {}    /* disabled */ | 
164  |  | #endif  | 
165  |  |  | 
166  |  |  | 
167  |  | /*-************************************  | 
168  |  | *  Basic Types  | 
169  |  | **************************************/  | 
170  |  | #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )  | 
171  |  | # include <stdint.h>  | 
172  |  |   typedef  uint8_t BYTE;  | 
173  |  |   typedef uint16_t U16;  | 
174  |  |   typedef uint32_t U32;  | 
175  |  |   typedef  int32_t S32;  | 
176  |  |   typedef uint64_t U64;  | 
177  |  | #else  | 
178  |  |   typedef unsigned char       BYTE;  | 
179  |  |   typedef unsigned short      U16;  | 
180  |  |   typedef unsigned int        U32;  | 
181  |  |   typedef   signed int        S32;  | 
182  |  |   typedef unsigned long long  U64;  | 
183  |  | #endif  | 
184  |  |  | 
185  |  |  | 
186  |  | /* unoptimized version; solves endianness & alignment issues */  | 
187  |  | static U32 LZ4F_readLE32 (const void* src)  | 
188  | 0  | { | 
189  | 0  |     const BYTE* const srcPtr = (const BYTE*)src;  | 
190  | 0  |     U32 value32 = srcPtr[0];  | 
191  | 0  |     value32 |= ((U32)srcPtr[1])<< 8;  | 
192  | 0  |     value32 |= ((U32)srcPtr[2])<<16;  | 
193  | 0  |     value32 |= ((U32)srcPtr[3])<<24;  | 
194  | 0  |     return value32;  | 
195  | 0  | }  | 
196  |  |  | 
197  |  | static void LZ4F_writeLE32 (void* dst, U32 value32)  | 
198  | 0  | { | 
199  | 0  |     BYTE* const dstPtr = (BYTE*)dst;  | 
200  | 0  |     dstPtr[0] = (BYTE)value32;  | 
201  | 0  |     dstPtr[1] = (BYTE)(value32 >> 8);  | 
202  | 0  |     dstPtr[2] = (BYTE)(value32 >> 16);  | 
203  | 0  |     dstPtr[3] = (BYTE)(value32 >> 24);  | 
204  | 0  | }  | 
205  |  |  | 
206  |  | static U64 LZ4F_readLE64 (const void* src)  | 
207  | 0  | { | 
208  | 0  |     const BYTE* const srcPtr = (const BYTE*)src;  | 
209  | 0  |     U64 value64 = srcPtr[0];  | 
210  | 0  |     value64 |= ((U64)srcPtr[1]<<8);  | 
211  | 0  |     value64 |= ((U64)srcPtr[2]<<16);  | 
212  | 0  |     value64 |= ((U64)srcPtr[3]<<24);  | 
213  | 0  |     value64 |= ((U64)srcPtr[4]<<32);  | 
214  | 0  |     value64 |= ((U64)srcPtr[5]<<40);  | 
215  | 0  |     value64 |= ((U64)srcPtr[6]<<48);  | 
216  | 0  |     value64 |= ((U64)srcPtr[7]<<56);  | 
217  | 0  |     return value64;  | 
218  | 0  | }  | 
219  |  |  | 
220  |  | static void LZ4F_writeLE64 (void* dst, U64 value64)  | 
221  | 0  | { | 
222  | 0  |     BYTE* const dstPtr = (BYTE*)dst;  | 
223  | 0  |     dstPtr[0] = (BYTE)value64;  | 
224  | 0  |     dstPtr[1] = (BYTE)(value64 >> 8);  | 
225  | 0  |     dstPtr[2] = (BYTE)(value64 >> 16);  | 
226  | 0  |     dstPtr[3] = (BYTE)(value64 >> 24);  | 
227  | 0  |     dstPtr[4] = (BYTE)(value64 >> 32);  | 
228  | 0  |     dstPtr[5] = (BYTE)(value64 >> 40);  | 
229  | 0  |     dstPtr[6] = (BYTE)(value64 >> 48);  | 
230  | 0  |     dstPtr[7] = (BYTE)(value64 >> 56);  | 
231  | 0  | }  | 
232  |  |  | 
233  |  |  | 
234  |  | /*-************************************  | 
235  |  | *  Constants  | 
236  |  | **************************************/  | 
237  |  | #ifndef LZ4_SRC_INCLUDED   /* avoid double definition */  | 
238  | 0  | #  define KB *(1<<10)  | 
239  | 0  | #  define MB *(1<<20)  | 
240  | 0  | #  define GB *(1<<30)  | 
241  |  | #endif  | 
242  |  |  | 
243  | 0  | #define _1BIT  0x01  | 
244  | 0  | #define _2BITS 0x03  | 
245  | 0  | #define _3BITS 0x07  | 
246  | 0  | #define _4BITS 0x0F  | 
247  |  | #define _8BITS 0xFF  | 
248  |  |  | 
249  | 0  | #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U  | 
250  | 0  | #define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB  | 
251  |  |  | 
252  |  | static const size_t minFHSize = LZ4F_HEADER_SIZE_MIN;   /*  7 */  | 
253  |  | static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX;   /* 19 */  | 
254  |  | static const size_t BHSize = LZ4F_BLOCK_HEADER_SIZE;  /* block header : size, and compress flag */  | 
255  |  | static const size_t BFSize = LZ4F_BLOCK_CHECKSUM_SIZE;  /* block footer : checksum (optional) */  | 
256  |  |  | 
257  |  |  | 
258  |  | /*-************************************  | 
259  |  | *  Structures and local types  | 
260  |  | **************************************/  | 
261  |  |  | 
262  |  | typedef enum { LZ4B_COMPRESSED, LZ4B_UNCOMPRESSED} LZ4F_BlockCompressMode_e; | 
263  |  | typedef enum { ctxNone, ctxFast, ctxHC } LZ4F_CtxType_e; | 
264  |  |  | 
265  |  | typedef struct LZ4F_cctx_s  | 
266  |  | { | 
267  |  |     LZ4F_CustomMem cmem;  | 
268  |  |     LZ4F_preferences_t prefs;  | 
269  |  |     U32    version;  | 
270  |  |     U32    cStage;     /* 0 : compression uninitialized ; 1 : initialized, can compress */  | 
271  |  |     const LZ4F_CDict* cdict;  | 
272  |  |     size_t maxBlockSize;  | 
273  |  |     size_t maxBufferSize;  | 
274  |  |     BYTE*  tmpBuff;    /* internal buffer, for streaming */  | 
275  |  |     BYTE*  tmpIn;      /* starting position of data compress within internal buffer (>= tmpBuff) */  | 
276  |  |     size_t tmpInSize;  /* amount of data to compress after tmpIn */  | 
277  |  |     U64    totalInSize;  | 
278  |  |     XXH32_state_t xxh;  | 
279  |  |     void*  lz4CtxPtr;  | 
280  |  |     U16    lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */  | 
281  |  |     U16    lz4CtxType;  /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */  | 
282  |  |     LZ4F_BlockCompressMode_e  blockCompressMode;  | 
283  |  | } LZ4F_cctx_t;  | 
284  |  |  | 
285  |  |  | 
286  |  | /*-************************************  | 
287  |  | *  Error management  | 
288  |  | **************************************/  | 
289  |  | #define LZ4F_GENERATE_STRING(STRING) #STRING,  | 
290  |  | static const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) }; | 
291  |  |  | 
292  |  |  | 
293  |  | unsigned LZ4F_isError(LZ4F_errorCode_t code)  | 
294  | 0  | { | 
295  | 0  |     return (code > (LZ4F_errorCode_t)(-LZ4F_ERROR_maxCode));  | 
296  | 0  | }  | 
297  |  |  | 
298  |  | const char* LZ4F_getErrorName(LZ4F_errorCode_t code)  | 
299  | 0  | { | 
300  | 0  |     static const char* codeError = "Unspecified error code";  | 
301  | 0  |     if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)];  | 
302  | 0  |     return codeError;  | 
303  | 0  | }  | 
304  |  |  | 
305  |  | LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult)  | 
306  | 0  | { | 
307  | 0  |     if (!LZ4F_isError(functionResult)) return LZ4F_OK_NoError;  | 
308  | 0  |     return (LZ4F_errorCodes)(-(ptrdiff_t)functionResult);  | 
309  | 0  | }  | 
310  |  |  | 
311  |  | static LZ4F_errorCode_t LZ4F_returnErrorCode(LZ4F_errorCodes code)  | 
312  | 0  | { | 
313  |  |     /* A compilation error here means sizeof(ptrdiff_t) is not large enough */  | 
314  | 0  |     LZ4F_STATIC_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));  | 
315  | 0  |     return (LZ4F_errorCode_t)-(ptrdiff_t)code;  | 
316  | 0  | }  | 
317  |  |  | 
318  | 0  | #define RETURN_ERROR(e) return LZ4F_returnErrorCode(LZ4F_ERROR_ ## e)  | 
319  |  |  | 
320  | 0  | #define RETURN_ERROR_IF(c,e) do {  \ | 
321  | 0  |         if (c) {                   \ | 
322  | 0  |             DEBUGLOG(3, "Error: " #c); \  | 
323  | 0  |             RETURN_ERROR(e);       \  | 
324  | 0  |         }                          \  | 
325  | 0  |     } while (0)  | 
326  |  |  | 
327  | 0  | #define FORWARD_IF_ERROR(r) do { if (LZ4F_isError(r)) return (r); } while (0) | 
328  |  |  | 
329  | 0  | unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; } | 
330  |  |  | 
331  | 0  | int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; } | 
332  |  |  | 
333  |  | size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID)  | 
334  | 0  | { | 
335  | 0  |     static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; | 
336  |  | 
  | 
337  | 0  |     if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;  | 
338  | 0  |     if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB)  | 
339  | 0  |         RETURN_ERROR(maxBlockSize_invalid);  | 
340  | 0  |     {   int const blockSizeIdx = (int)blockSizeID - (int)LZ4F_max64KB; | 
341  | 0  |         return blockSizes[blockSizeIdx];  | 
342  | 0  | }   }  | 
343  |  |  | 
344  |  | /*-************************************  | 
345  |  | *  Private functions  | 
346  |  | **************************************/  | 
347  | 0  | #define MIN(a,b)   ( (a) < (b) ? (a) : (b) )  | 
348  |  |  | 
349  |  | static BYTE LZ4F_headerChecksum (const void* header, size_t length)  | 
350  | 0  | { | 
351  | 0  |     U32 const xxh = XXH32(header, length, 0);  | 
352  | 0  |     return (BYTE)(xxh >> 8);  | 
353  | 0  | }  | 
354  |  |  | 
355  |  |  | 
356  |  | /*-************************************  | 
357  |  | *  Simple-pass compression functions  | 
358  |  | **************************************/  | 
359  |  | static LZ4F_blockSizeID_t LZ4F_optimalBSID(const LZ4F_blockSizeID_t requestedBSID,  | 
360  |  |                                            const size_t srcSize)  | 
361  | 0  | { | 
362  | 0  |     LZ4F_blockSizeID_t proposedBSID = LZ4F_max64KB;  | 
363  | 0  |     size_t maxBlockSize = 64 KB;  | 
364  | 0  |     while (requestedBSID > proposedBSID) { | 
365  | 0  |         if (srcSize <= maxBlockSize)  | 
366  | 0  |             return proposedBSID;  | 
367  | 0  |         proposedBSID = (LZ4F_blockSizeID_t)((int)proposedBSID + 1);  | 
368  | 0  |         maxBlockSize <<= 2;  | 
369  | 0  |     }  | 
370  | 0  |     return requestedBSID;  | 
371  | 0  | }  | 
372  |  |  | 
373  |  | /*! LZ4F_compressBound_internal() :  | 
374  |  |  *  Provides dstCapacity given a srcSize to guarantee operation success in worst case situations.  | 
375  |  |  *  prefsPtr is optional : if NULL is provided, preferences will be set to cover worst case scenario.  | 
376  |  |  * @return is always the same for a srcSize and prefsPtr, so it can be relied upon to size reusable buffers.  | 
377  |  |  *  When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations.  | 
378  |  |  */  | 
379  |  | static size_t LZ4F_compressBound_internal(size_t srcSize,  | 
380  |  |                                     const LZ4F_preferences_t* preferencesPtr,  | 
381  |  |                                           size_t alreadyBuffered)  | 
382  | 0  | { | 
383  | 0  |     LZ4F_preferences_t prefsNull = LZ4F_INIT_PREFERENCES;  | 
384  | 0  |     prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;   /* worst case */  | 
385  | 0  |     prefsNull.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled;   /* worst case */  | 
386  | 0  |     {   const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr; | 
387  | 0  |         U32 const flush = prefsPtr->autoFlush | (srcSize==0);  | 
388  | 0  |         LZ4F_blockSizeID_t const blockID = prefsPtr->frameInfo.blockSizeID;  | 
389  | 0  |         size_t const blockSize = LZ4F_getBlockSize(blockID);  | 
390  | 0  |         size_t const maxBuffered = blockSize - 1;  | 
391  | 0  |         size_t const bufferedSize = MIN(alreadyBuffered, maxBuffered);  | 
392  | 0  |         size_t const maxSrcSize = srcSize + bufferedSize;  | 
393  | 0  |         unsigned const nbFullBlocks = (unsigned)(maxSrcSize / blockSize);  | 
394  | 0  |         size_t const partialBlockSize = maxSrcSize & (blockSize-1);  | 
395  | 0  |         size_t const lastBlockSize = flush ? partialBlockSize : 0;  | 
396  | 0  |         unsigned const nbBlocks = nbFullBlocks + (lastBlockSize>0);  | 
397  |  | 
  | 
398  | 0  |         size_t const blockCRCSize = BFSize * prefsPtr->frameInfo.blockChecksumFlag;  | 
399  | 0  |         size_t const frameEnd = BHSize + (prefsPtr->frameInfo.contentChecksumFlag*BFSize);  | 
400  |  | 
  | 
401  | 0  |         return ((BHSize + blockCRCSize) * nbBlocks) +  | 
402  | 0  |                (blockSize * nbFullBlocks) + lastBlockSize + frameEnd;  | 
403  | 0  |     }  | 
404  | 0  | }  | 
405  |  |  | 
406  |  | size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)  | 
407  | 0  | { | 
408  | 0  |     LZ4F_preferences_t prefs;  | 
409  | 0  |     size_t const headerSize = maxFHSize;      /* max header size, including optional fields */  | 
410  |  | 
  | 
411  | 0  |     if (preferencesPtr!=NULL) prefs = *preferencesPtr;  | 
412  | 0  |     else MEM_INIT(&prefs, 0, sizeof(prefs));  | 
413  | 0  |     prefs.autoFlush = 1;  | 
414  |  | 
  | 
415  | 0  |     return headerSize + LZ4F_compressBound_internal(srcSize, &prefs, 0);;  | 
416  | 0  | }  | 
417  |  |  | 
418  |  |  | 
419  |  | /*! LZ4F_compressFrame_usingCDict() :  | 
420  |  |  *  Compress srcBuffer using a dictionary, in a single step.  | 
421  |  |  *  cdict can be NULL, in which case, no dictionary is used.  | 
422  |  |  *  dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).  | 
423  |  |  *  The LZ4F_preferences_t structure is optional : you may provide NULL as argument,  | 
424  |  |  *  however, it's the only way to provide a dictID, so it's not recommended.  | 
425  |  |  * @return : number of bytes written into dstBuffer,  | 
426  |  |  *           or an error code if it fails (can be tested using LZ4F_isError())  | 
427  |  |  */  | 
428  |  | size_t LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx,  | 
429  |  |                                      void* dstBuffer, size_t dstCapacity,  | 
430  |  |                                const void* srcBuffer, size_t srcSize,  | 
431  |  |                                const LZ4F_CDict* cdict,  | 
432  |  |                                const LZ4F_preferences_t* preferencesPtr)  | 
433  | 0  | { | 
434  | 0  |     LZ4F_preferences_t prefs;  | 
435  | 0  |     LZ4F_compressOptions_t options;  | 
436  | 0  |     BYTE* const dstStart = (BYTE*) dstBuffer;  | 
437  | 0  |     BYTE* dstPtr = dstStart;  | 
438  | 0  |     BYTE* const dstEnd = dstStart + dstCapacity;  | 
439  |  | 
  | 
440  | 0  |     DEBUGLOG(4, "LZ4F_compressFrame_usingCDict (srcSize=%u)", (unsigned)srcSize);  | 
441  | 0  |     if (preferencesPtr!=NULL)  | 
442  | 0  |         prefs = *preferencesPtr;  | 
443  | 0  |     else  | 
444  | 0  |         MEM_INIT(&prefs, 0, sizeof(prefs));  | 
445  | 0  |     if (prefs.frameInfo.contentSize != 0)  | 
446  | 0  |         prefs.frameInfo.contentSize = (U64)srcSize;   /* auto-correct content size if selected (!=0) */  | 
447  |  | 
  | 
448  | 0  |     prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);  | 
449  | 0  |     prefs.autoFlush = 1;  | 
450  | 0  |     if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID))  | 
451  | 0  |         prefs.frameInfo.blockMode = LZ4F_blockIndependent;   /* only one block => no need for inter-block link */  | 
452  |  | 
  | 
453  | 0  |     MEM_INIT(&options, 0, sizeof(options));  | 
454  | 0  |     options.stableSrc = 1;  | 
455  |  | 
  | 
456  | 0  |     RETURN_ERROR_IF(dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs), dstMaxSize_tooSmall);  | 
457  |  |  | 
458  | 0  |     { size_t const headerSize = LZ4F_compressBegin_usingCDict(cctx, dstBuffer, dstCapacity, cdict, &prefs);  /* write header */ | 
459  | 0  |       FORWARD_IF_ERROR(headerSize);  | 
460  | 0  |       dstPtr += headerSize;   /* header size */ }  | 
461  |  |  | 
462  | 0  |     assert(dstEnd >= dstPtr);  | 
463  | 0  |     { size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, (size_t)(dstEnd-dstPtr), srcBuffer, srcSize, &options); | 
464  | 0  |       FORWARD_IF_ERROR(cSize);  | 
465  | 0  |       dstPtr += cSize; }  | 
466  |  |  | 
467  | 0  |     assert(dstEnd >= dstPtr);  | 
468  | 0  |     { size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, (size_t)(dstEnd-dstPtr), &options);   /* flush last block, and generate suffix */ | 
469  | 0  |       FORWARD_IF_ERROR(tailSize);  | 
470  | 0  |       dstPtr += tailSize; }  | 
471  |  |  | 
472  | 0  |     assert(dstEnd >= dstStart);  | 
473  | 0  |     return (size_t)(dstPtr - dstStart);  | 
474  | 0  | }  | 
475  |  |  | 
476  |  |  | 
477  |  | /*! LZ4F_compressFrame() :  | 
478  |  |  *  Compress an entire srcBuffer into a valid LZ4 frame, in a single step.  | 
479  |  |  *  dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).  | 
480  |  |  *  The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.  | 
481  |  |  * @return : number of bytes written into dstBuffer.  | 
482  |  |  *           or an error code if it fails (can be tested using LZ4F_isError())  | 
483  |  |  */  | 
484  |  | size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity,  | 
485  |  |                     const void* srcBuffer, size_t srcSize,  | 
486  |  |                     const LZ4F_preferences_t* preferencesPtr)  | 
487  | 0  | { | 
488  | 0  |     size_t result;  | 
489  |  | #if (LZ4F_HEAPMODE)  | 
490  |  |     LZ4F_cctx_t* cctxPtr;  | 
491  |  |     result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION);  | 
492  |  |     FORWARD_IF_ERROR(result);  | 
493  |  | #else  | 
494  | 0  |     LZ4F_cctx_t cctx;  | 
495  | 0  |     LZ4_stream_t lz4ctx;  | 
496  | 0  |     LZ4F_cctx_t* const cctxPtr = &cctx;  | 
497  |  | 
  | 
498  | 0  |     MEM_INIT(&cctx, 0, sizeof(cctx));  | 
499  | 0  |     cctx.version = LZ4F_VERSION;  | 
500  | 0  |     cctx.maxBufferSize = 5 MB;   /* mess with real buffer size to prevent dynamic allocation; works only because autoflush==1 & stableSrc==1 */  | 
501  | 0  |     if ( preferencesPtr == NULL  | 
502  | 0  |       || preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN ) { | 
503  | 0  |         LZ4_initStream(&lz4ctx, sizeof(lz4ctx));  | 
504  | 0  |         cctxPtr->lz4CtxPtr = &lz4ctx;  | 
505  | 0  |         cctxPtr->lz4CtxAlloc = 1;  | 
506  | 0  |         cctxPtr->lz4CtxType = ctxFast;  | 
507  | 0  |     }  | 
508  | 0  | #endif  | 
509  | 0  |     DEBUGLOG(4, "LZ4F_compressFrame");  | 
510  |  | 
  | 
511  | 0  |     result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity,  | 
512  | 0  |                                            srcBuffer, srcSize,  | 
513  | 0  |                                            NULL, preferencesPtr);  | 
514  |  | 
  | 
515  |  | #if (LZ4F_HEAPMODE)  | 
516  |  |     LZ4F_freeCompressionContext(cctxPtr);  | 
517  |  | #else  | 
518  | 0  |     if ( preferencesPtr != NULL  | 
519  | 0  |       && preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN ) { | 
520  | 0  |         LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem);  | 
521  | 0  |     }  | 
522  | 0  | #endif  | 
523  | 0  |     return result;  | 
524  | 0  | }  | 
525  |  |  | 
526  |  |  | 
527  |  | /*-***************************************************  | 
528  |  | *   Dictionary compression  | 
529  |  | *****************************************************/  | 
530  |  |  | 
531  |  | struct LZ4F_CDict_s { | 
532  |  |     LZ4F_CustomMem cmem;  | 
533  |  |     void* dictContent;  | 
534  |  |     LZ4_stream_t* fastCtx;  | 
535  |  |     LZ4_streamHC_t* HCCtx;  | 
536  |  | }; /* typedef'd to LZ4F_CDict within lz4frame_static.h */  | 
537  |  |  | 
538  |  | LZ4F_CDict*  | 
539  |  | LZ4F_createCDict_advanced(LZ4F_CustomMem cmem, const void* dictBuffer, size_t dictSize)  | 
540  | 0  | { | 
541  | 0  |     const char* dictStart = (const char*)dictBuffer;  | 
542  | 0  |     LZ4F_CDict* cdict = NULL;  | 
543  |  | 
  | 
544  | 0  |     DEBUGLOG(4, "LZ4F_createCDict_advanced");  | 
545  |  | 
  | 
546  | 0  |     if (!dictStart)  | 
547  | 0  |         return NULL;  | 
548  | 0  |     cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), cmem);  | 
549  | 0  |     if (!cdict)  | 
550  | 0  |         return NULL;  | 
551  |  |  | 
552  | 0  |     cdict->cmem = cmem;  | 
553  | 0  |     if (dictSize > 64 KB) { | 
554  | 0  |         dictStart += dictSize - 64 KB;  | 
555  | 0  |         dictSize = 64 KB;  | 
556  | 0  |     }  | 
557  | 0  |     cdict->dictContent = LZ4F_malloc(dictSize, cmem);  | 
558  |  |     /* note: using @cmem to allocate => can't use default create */  | 
559  | 0  |     cdict->fastCtx = (LZ4_stream_t*)LZ4F_malloc(sizeof(LZ4_stream_t), cmem);  | 
560  | 0  |     cdict->HCCtx = (LZ4_streamHC_t*)LZ4F_malloc(sizeof(LZ4_streamHC_t), cmem);  | 
561  | 0  |     if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) { | 
562  | 0  |         LZ4F_freeCDict(cdict);  | 
563  | 0  |         return NULL;  | 
564  | 0  |     }  | 
565  | 0  |     memcpy(cdict->dictContent, dictStart, dictSize);  | 
566  | 0  |     LZ4_initStream(cdict->fastCtx, sizeof(LZ4_stream_t));  | 
567  | 0  |     LZ4_loadDictSlow(cdict->fastCtx, (const char*)cdict->dictContent, (int)dictSize);  | 
568  | 0  |     LZ4_initStreamHC(cdict->HCCtx, sizeof(LZ4_streamHC_t));  | 
569  |  |     /* note: we don't know at this point which compression level is going to be used  | 
570  |  |      * as a consequence, HCCtx is created for the more common HC mode */  | 
571  | 0  |     LZ4_setCompressionLevel(cdict->HCCtx, LZ4HC_CLEVEL_DEFAULT);  | 
572  | 0  |     LZ4_loadDictHC(cdict->HCCtx, (const char*)cdict->dictContent, (int)dictSize);  | 
573  | 0  |     return cdict;  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | /*! LZ4F_createCDict() :  | 
577  |  |  *  When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.  | 
578  |  |  *  LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.  | 
579  |  |  *  LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.  | 
580  |  |  * @dictBuffer can be released after LZ4F_CDict creation, since its content is copied within CDict  | 
581  |  |  * @return : digested dictionary for compression, or NULL if failed */  | 
582  |  | LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize)  | 
583  | 0  | { | 
584  | 0  |     DEBUGLOG(4, "LZ4F_createCDict");  | 
585  | 0  |     return LZ4F_createCDict_advanced(LZ4F_defaultCMem, dictBuffer, dictSize);  | 
586  | 0  | }  | 
587  |  |  | 
588  |  | void LZ4F_freeCDict(LZ4F_CDict* cdict)  | 
589  | 0  | { | 
590  | 0  |     if (cdict==NULL) return;  /* support free on NULL */  | 
591  | 0  |     LZ4F_free(cdict->dictContent, cdict->cmem);  | 
592  | 0  |     LZ4F_free(cdict->fastCtx, cdict->cmem);  | 
593  | 0  |     LZ4F_free(cdict->HCCtx, cdict->cmem);  | 
594  | 0  |     LZ4F_free(cdict, cdict->cmem);  | 
595  | 0  | }  | 
596  |  |  | 
597  |  |  | 
598  |  | /*-*********************************  | 
599  |  | *  Advanced compression functions  | 
600  |  | ***********************************/  | 
601  |  |  | 
602  |  | LZ4F_cctx*  | 
603  |  | LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version)  | 
604  | 0  | { | 
605  | 0  |     LZ4F_cctx* const cctxPtr =  | 
606  | 0  |         (LZ4F_cctx*)LZ4F_calloc(sizeof(LZ4F_cctx), customMem);  | 
607  | 0  |     if (cctxPtr==NULL) return NULL;  | 
608  |  |  | 
609  | 0  |     cctxPtr->cmem = customMem;  | 
610  | 0  |     cctxPtr->version = version;  | 
611  | 0  |     cctxPtr->cStage = 0;   /* Uninitialized. Next stage : init cctx */  | 
612  |  | 
  | 
613  | 0  |     return cctxPtr;  | 
614  | 0  | }  | 
615  |  |  | 
616  |  | /*! LZ4F_createCompressionContext() :  | 
617  |  |  *  The first thing to do is to create a compressionContext object, which will be used in all compression operations.  | 
618  |  |  *  This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.  | 
619  |  |  *  The version provided MUST be LZ4F_VERSION. It is intended to track potential incompatible differences between different binaries.  | 
620  |  |  *  The function will provide a pointer to an allocated LZ4F_compressionContext_t object.  | 
621  |  |  *  If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation.  | 
622  |  |  *  Object can release its memory using LZ4F_freeCompressionContext();  | 
623  |  | **/  | 
624  |  | LZ4F_errorCode_t  | 
625  |  | LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version)  | 
626  | 0  | { | 
627  | 0  |     assert(LZ4F_compressionContextPtr != NULL); /* considered a violation of narrow contract */  | 
628  |  |     /* in case it nonetheless happen in production */  | 
629  | 0  |     RETURN_ERROR_IF(LZ4F_compressionContextPtr == NULL, parameter_null);  | 
630  |  |  | 
631  | 0  |     *LZ4F_compressionContextPtr = LZ4F_createCompressionContext_advanced(LZ4F_defaultCMem, version);  | 
632  | 0  |     RETURN_ERROR_IF(*LZ4F_compressionContextPtr==NULL, allocation_failed);  | 
633  | 0  |     return LZ4F_OK_NoError;  | 
634  | 0  | }  | 
635  |  |  | 
636  |  | LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctxPtr)  | 
637  | 0  | { | 
638  | 0  |     if (cctxPtr != NULL) {  /* support free on NULL */ | 
639  | 0  |        LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem);  /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */  | 
640  | 0  |        LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem);  | 
641  | 0  |        LZ4F_free(cctxPtr, cctxPtr->cmem);  | 
642  | 0  |     }  | 
643  | 0  |     return LZ4F_OK_NoError;  | 
644  | 0  | }  | 
645  |  |  | 
646  |  |  | 
647  |  | /**  | 
648  |  |  * This function prepares the internal LZ4(HC) stream for a new compression,  | 
649  |  |  * resetting the context and attaching the dictionary, if there is one.  | 
650  |  |  *  | 
651  |  |  * It needs to be called at the beginning of each independent compression  | 
652  |  |  * stream (i.e., at the beginning of a frame in blockLinked mode, or at the  | 
653  |  |  * beginning of each block in blockIndependent mode).  | 
654  |  |  */  | 
655  |  | static void LZ4F_initStream(void* ctx,  | 
656  |  |                             const LZ4F_CDict* cdict,  | 
657  |  |                             int level,  | 
658  | 0  |                             LZ4F_blockMode_t blockMode) { | 
659  | 0  |     if (level < LZ4HC_CLEVEL_MIN) { | 
660  | 0  |         if (cdict || blockMode == LZ4F_blockLinked) { | 
661  |  |             /* In these cases, we will call LZ4_compress_fast_continue(),  | 
662  |  |              * which needs an already reset context. Otherwise, we'll call a  | 
663  |  |              * one-shot API. The non-continued APIs internally perform their own  | 
664  |  |              * resets at the beginning of their calls, where they know what  | 
665  |  |              * tableType they need the context to be in. So in that case this  | 
666  |  |              * would be misguided / wasted work. */  | 
667  | 0  |             LZ4_resetStream_fast((LZ4_stream_t*)ctx);  | 
668  | 0  |             if (cdict)  | 
669  | 0  |                 LZ4_attach_dictionary((LZ4_stream_t*)ctx, cdict->fastCtx);  | 
670  | 0  |         }  | 
671  |  |         /* In these cases, we'll call a one-shot API.  | 
672  |  |          * The non-continued APIs internally perform their own resets  | 
673  |  |          * at the beginning of their calls, where they know  | 
674  |  |          * which tableType they need the context to be in.  | 
675  |  |          * Therefore, a reset here would be wasted work. */  | 
676  | 0  |     } else { | 
677  | 0  |         LZ4_resetStreamHC_fast((LZ4_streamHC_t*)ctx, level);  | 
678  | 0  |         if (cdict)  | 
679  | 0  |             LZ4_attach_HC_dictionary((LZ4_streamHC_t*)ctx, cdict->HCCtx);  | 
680  | 0  |     }  | 
681  | 0  | }  | 
682  |  |  | 
683  | 0  | static int ctxTypeID_to_size(int ctxTypeID) { | 
684  | 0  |     switch(ctxTypeID) { | 
685  | 0  |     case 1:  | 
686  | 0  |         return LZ4_sizeofState();  | 
687  | 0  |     case 2:  | 
688  | 0  |         return LZ4_sizeofStateHC();  | 
689  | 0  |     default:  | 
690  | 0  |         return 0;  | 
691  | 0  |     }  | 
692  | 0  | }  | 
693  |  |  | 
694  | 0  | size_t LZ4F_cctx_size(const LZ4F_cctx* cctx) { | 
695  | 0  |     if (cctx == NULL) { | 
696  | 0  |         return 0;  | 
697  | 0  |     }  | 
698  | 0  |     return sizeof(*cctx) + cctx->maxBufferSize + ctxTypeID_to_size(cctx->lz4CtxAlloc);  | 
699  | 0  | }  | 
700  |  |  | 
701  |  | /* LZ4F_compressBegin_internal()  | 
702  |  |  * Note: only accepts @cdict _or_ @dictBuffer as non NULL.  | 
703  |  |  */  | 
704  |  | size_t LZ4F_compressBegin_internal(LZ4F_cctx* cctx,  | 
705  |  |                           void* dstBuffer, size_t dstCapacity,  | 
706  |  |                           const void* dictBuffer, size_t dictSize,  | 
707  |  |                           const LZ4F_CDict* cdict,  | 
708  |  |                           const LZ4F_preferences_t* preferencesPtr)  | 
709  | 0  | { | 
710  | 0  |     LZ4F_preferences_t const prefNull = LZ4F_INIT_PREFERENCES;  | 
711  | 0  |     BYTE* const dstStart = (BYTE*)dstBuffer;  | 
712  | 0  |     BYTE* dstPtr = dstStart;  | 
713  |  | 
  | 
714  | 0  |     RETURN_ERROR_IF(dstCapacity < maxFHSize, dstMaxSize_tooSmall);  | 
715  | 0  |     if (preferencesPtr == NULL) preferencesPtr = &prefNull;  | 
716  | 0  |     cctx->prefs = *preferencesPtr;  | 
717  | 0  |     DEBUGLOG(5, "LZ4F_compressBegin_internal: Independent_blocks=%u", cctx->prefs.frameInfo.blockMode);  | 
718  |  |  | 
719  |  |     /* cctx Management */  | 
720  | 0  |     {   U16 const ctxTypeID = (cctx->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2; | 
721  | 0  |         int requiredSize = ctxTypeID_to_size(ctxTypeID);  | 
722  | 0  |         int allocatedSize = ctxTypeID_to_size(cctx->lz4CtxAlloc);  | 
723  | 0  |         if (allocatedSize < requiredSize) { | 
724  |  |             /* not enough space allocated */  | 
725  | 0  |             LZ4F_free(cctx->lz4CtxPtr, cctx->cmem);  | 
726  | 0  |             if (cctx->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { | 
727  |  |                 /* must take ownership of memory allocation,  | 
728  |  |                  * in order to respect custom allocator contract */  | 
729  | 0  |                 cctx->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_stream_t), cctx->cmem);  | 
730  | 0  |                 if (cctx->lz4CtxPtr)  | 
731  | 0  |                     LZ4_initStream(cctx->lz4CtxPtr, sizeof(LZ4_stream_t));  | 
732  | 0  |             } else { | 
733  | 0  |                 cctx->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_streamHC_t), cctx->cmem);  | 
734  | 0  |                 if (cctx->lz4CtxPtr)  | 
735  | 0  |                     LZ4_initStreamHC(cctx->lz4CtxPtr, sizeof(LZ4_streamHC_t));  | 
736  | 0  |             }  | 
737  | 0  |             RETURN_ERROR_IF(cctx->lz4CtxPtr == NULL, allocation_failed);  | 
738  | 0  |             cctx->lz4CtxAlloc = ctxTypeID;  | 
739  | 0  |             cctx->lz4CtxType = ctxTypeID;  | 
740  | 0  |         } else if (cctx->lz4CtxType != ctxTypeID) { | 
741  |  |             /* otherwise, a sufficient buffer is already allocated,  | 
742  |  |              * but we need to reset it to the correct context type */  | 
743  | 0  |             if (cctx->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { | 
744  | 0  |                 LZ4_initStream((LZ4_stream_t*)cctx->lz4CtxPtr, sizeof(LZ4_stream_t));  | 
745  | 0  |             } else { | 
746  | 0  |                 LZ4_initStreamHC((LZ4_streamHC_t*)cctx->lz4CtxPtr, sizeof(LZ4_streamHC_t));  | 
747  | 0  |                 LZ4_setCompressionLevel((LZ4_streamHC_t*)cctx->lz4CtxPtr, cctx->prefs.compressionLevel);  | 
748  | 0  |             }  | 
749  | 0  |             cctx->lz4CtxType = ctxTypeID;  | 
750  | 0  |     }   }  | 
751  |  |  | 
752  |  |     /* Buffer Management */  | 
753  | 0  |     if (cctx->prefs.frameInfo.blockSizeID == 0)  | 
754  | 0  |         cctx->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;  | 
755  | 0  |     cctx->maxBlockSize = LZ4F_getBlockSize(cctx->prefs.frameInfo.blockSizeID);  | 
756  |  | 
  | 
757  | 0  |     {   size_t const requiredBuffSize = preferencesPtr->autoFlush ? | 
758  | 0  |                 ((cctx->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 64 KB : 0) :  /* only needs past data up to window size */  | 
759  | 0  |                 cctx->maxBlockSize + ((cctx->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 128 KB : 0);  | 
760  |  | 
  | 
761  | 0  |         if (cctx->maxBufferSize < requiredBuffSize) { | 
762  | 0  |             cctx->maxBufferSize = 0;  | 
763  | 0  |             LZ4F_free(cctx->tmpBuff, cctx->cmem);  | 
764  | 0  |             cctx->tmpBuff = (BYTE*)LZ4F_malloc(requiredBuffSize, cctx->cmem);  | 
765  | 0  |             RETURN_ERROR_IF(cctx->tmpBuff == NULL, allocation_failed);  | 
766  | 0  |             cctx->maxBufferSize = requiredBuffSize;  | 
767  | 0  |     }   }  | 
768  | 0  |     cctx->tmpIn = cctx->tmpBuff;  | 
769  | 0  |     cctx->tmpInSize = 0;  | 
770  | 0  |     (void)XXH32_reset(&(cctx->xxh), 0);  | 
771  |  |  | 
772  |  |     /* context init */  | 
773  | 0  |     cctx->cdict = cdict;  | 
774  | 0  |     if (cctx->prefs.frameInfo.blockMode == LZ4F_blockLinked) { | 
775  |  |         /* frame init only for blockLinked : blockIndependent will be init at each block */  | 
776  | 0  |         LZ4F_initStream(cctx->lz4CtxPtr, cdict, cctx->prefs.compressionLevel, LZ4F_blockLinked);  | 
777  | 0  |     }  | 
778  | 0  |     if (preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN) { | 
779  | 0  |         LZ4_favorDecompressionSpeed((LZ4_streamHC_t*)cctx->lz4CtxPtr, (int)preferencesPtr->favorDecSpeed);  | 
780  | 0  |     }  | 
781  | 0  |     if (dictBuffer) { | 
782  | 0  |         assert(cdict == NULL);  | 
783  | 0  |         RETURN_ERROR_IF(dictSize > INT_MAX, parameter_invalid);  | 
784  | 0  |         if (cctx->lz4CtxType == ctxFast) { | 
785  |  |             /* lz4 fast*/  | 
786  | 0  |             LZ4_loadDict((LZ4_stream_t*)cctx->lz4CtxPtr, (const char*)dictBuffer, (int)dictSize);  | 
787  | 0  |         } else { | 
788  |  |             /* lz4hc */  | 
789  | 0  |             assert(cctx->lz4CtxType == ctxHC);  | 
790  | 0  |             LZ4_loadDictHC((LZ4_streamHC_t*)cctx->lz4CtxPtr, (const char*)dictBuffer, (int)dictSize);  | 
791  | 0  |         }  | 
792  | 0  |     }  | 
793  |  |  | 
794  |  |     /* Stage 2 : Write Frame Header */  | 
795  |  |  | 
796  |  |     /* Magic Number */  | 
797  | 0  |     LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER);  | 
798  | 0  |     dstPtr += 4;  | 
799  | 0  |     {   BYTE* const headerStart = dstPtr; | 
800  |  |  | 
801  |  |         /* FLG Byte */  | 
802  | 0  |         *dstPtr++ = (BYTE)(((1 & _2BITS) << 6)    /* Version('01') */ | 
803  | 0  |             + ((cctx->prefs.frameInfo.blockMode & _1BIT ) << 5)  | 
804  | 0  |             + ((cctx->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)  | 
805  | 0  |             + ((unsigned)(cctx->prefs.frameInfo.contentSize > 0) << 3)  | 
806  | 0  |             + ((cctx->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)  | 
807  | 0  |             +  (cctx->prefs.frameInfo.dictID > 0) );  | 
808  |  |         /* BD Byte */  | 
809  | 0  |         *dstPtr++ = (BYTE)((cctx->prefs.frameInfo.blockSizeID & _3BITS) << 4);  | 
810  |  |         /* Optional Frame content size field */  | 
811  | 0  |         if (cctx->prefs.frameInfo.contentSize) { | 
812  | 0  |             LZ4F_writeLE64(dstPtr, cctx->prefs.frameInfo.contentSize);  | 
813  | 0  |             dstPtr += 8;  | 
814  | 0  |             cctx->totalInSize = 0;  | 
815  | 0  |         }  | 
816  |  |         /* Optional dictionary ID field */  | 
817  | 0  |         if (cctx->prefs.frameInfo.dictID) { | 
818  | 0  |             LZ4F_writeLE32(dstPtr, cctx->prefs.frameInfo.dictID);  | 
819  | 0  |             dstPtr += 4;  | 
820  | 0  |         }  | 
821  |  |         /* Header CRC Byte */  | 
822  | 0  |         *dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart));  | 
823  | 0  |         dstPtr++;  | 
824  | 0  |     }  | 
825  |  | 
  | 
826  | 0  |     cctx->cStage = 1;   /* header written, now request input data block */  | 
827  | 0  |     return (size_t)(dstPtr - dstStart);  | 
828  | 0  | }  | 
829  |  |  | 
830  |  | size_t LZ4F_compressBegin(LZ4F_cctx* cctx,  | 
831  |  |                           void* dstBuffer, size_t dstCapacity,  | 
832  |  |                           const LZ4F_preferences_t* preferencesPtr)  | 
833  | 0  | { | 
834  | 0  |     return LZ4F_compressBegin_internal(cctx, dstBuffer, dstCapacity,  | 
835  | 0  |                                         NULL, 0,  | 
836  | 0  |                                         NULL, preferencesPtr);  | 
837  | 0  | }  | 
838  |  |  | 
839  |  | /* LZ4F_compressBegin_usingDictOnce:  | 
840  |  |  * Hidden implementation,  | 
841  |  |  * employed for multi-threaded compression  | 
842  |  |  * when frame defines linked blocks */  | 
843  |  | size_t LZ4F_compressBegin_usingDictOnce(LZ4F_cctx* cctx,  | 
844  |  |                           void* dstBuffer, size_t dstCapacity,  | 
845  |  |                           const void* dict, size_t dictSize,  | 
846  |  |                           const LZ4F_preferences_t* preferencesPtr)  | 
847  | 0  | { | 
848  | 0  |     return LZ4F_compressBegin_internal(cctx, dstBuffer, dstCapacity,  | 
849  | 0  |                                         dict, dictSize,  | 
850  | 0  |                                         NULL, preferencesPtr);  | 
851  | 0  | }  | 
852  |  |  | 
853  |  | size_t LZ4F_compressBegin_usingDict(LZ4F_cctx* cctx,  | 
854  |  |                           void* dstBuffer, size_t dstCapacity,  | 
855  |  |                           const void* dict, size_t dictSize,  | 
856  |  |                           const LZ4F_preferences_t* preferencesPtr)  | 
857  | 0  | { | 
858  |  |     /* note : incorrect implementation :  | 
859  |  |      * this will only use the dictionary once,  | 
860  |  |      * instead of once *per* block when frames defines independent blocks */  | 
861  | 0  |     return LZ4F_compressBegin_usingDictOnce(cctx, dstBuffer, dstCapacity,  | 
862  | 0  |                                         dict, dictSize,  | 
863  | 0  |                                         preferencesPtr);  | 
864  | 0  | }  | 
865  |  |  | 
866  |  | size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctx,  | 
867  |  |                           void* dstBuffer, size_t dstCapacity,  | 
868  |  |                           const LZ4F_CDict* cdict,  | 
869  |  |                           const LZ4F_preferences_t* preferencesPtr)  | 
870  | 0  | { | 
871  | 0  |     return LZ4F_compressBegin_internal(cctx, dstBuffer, dstCapacity,  | 
872  | 0  |                                         NULL, 0,  | 
873  | 0  |                                        cdict, preferencesPtr);  | 
874  | 0  | }  | 
875  |  |  | 
876  |  |  | 
877  |  | /*  LZ4F_compressBound() :  | 
878  |  |  * @return minimum capacity of dstBuffer for a given srcSize to handle worst case scenario.  | 
879  |  |  *  LZ4F_preferences_t structure is optional : if NULL, preferences will be set to cover worst case scenario.  | 
880  |  |  *  This function cannot fail.  | 
881  |  |  */  | 
882  |  | size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)  | 
883  | 0  | { | 
884  | 0  |     if (preferencesPtr && preferencesPtr->autoFlush) { | 
885  | 0  |         return LZ4F_compressBound_internal(srcSize, preferencesPtr, 0);  | 
886  | 0  |     }  | 
887  | 0  |     return LZ4F_compressBound_internal(srcSize, preferencesPtr, (size_t)-1);  | 
888  | 0  | }  | 
889  |  |  | 
890  |  |  | 
891  |  | typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level, const LZ4F_CDict* cdict);  | 
892  |  |  | 
893  |  |  | 
894  |  | /*! LZ4F_makeBlock():  | 
895  |  |  *  compress a single block, add header and optional checksum.  | 
896  |  |  *  assumption : dst buffer capacity is >= BHSize + srcSize + crcSize  | 
897  |  |  */  | 
898  |  | static size_t LZ4F_makeBlock(void* dst,  | 
899  |  |                        const void* src, size_t srcSize,  | 
900  |  |                              compressFunc_t compress, void* lz4ctx, int level,  | 
901  |  |                        const LZ4F_CDict* cdict,  | 
902  |  |                              LZ4F_blockChecksum_t crcFlag)  | 
903  | 0  | { | 
904  | 0  |     BYTE* const cSizePtr = (BYTE*)dst;  | 
905  | 0  |     int dstCapacity = (srcSize > 1) ? (int)srcSize - 1 : 1;  | 
906  | 0  |     U32 cSize;  | 
907  | 0  |     assert(compress != NULL);  | 
908  | 0  |     cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize),  | 
909  | 0  |                           (int)srcSize, dstCapacity,  | 
910  | 0  |                           level, cdict);  | 
911  |  | 
  | 
912  | 0  |     if (cSize == 0 || cSize >= srcSize) { | 
913  | 0  |         cSize = (U32)srcSize;  | 
914  | 0  |         LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG);  | 
915  | 0  |         memcpy(cSizePtr+BHSize, src, srcSize);  | 
916  | 0  |     } else { | 
917  | 0  |         LZ4F_writeLE32(cSizePtr, cSize);  | 
918  | 0  |     }  | 
919  | 0  |     if (crcFlag) { | 
920  | 0  |         U32 const crc32 = XXH32(cSizePtr+BHSize, cSize, 0);  /* checksum of compressed data */  | 
921  | 0  |         LZ4F_writeLE32(cSizePtr+BHSize+cSize, crc32);  | 
922  | 0  |     }  | 
923  | 0  |     return BHSize + cSize + ((U32)crcFlag)*BFSize;  | 
924  | 0  | }  | 
925  |  |  | 
926  |  |  | 
927  |  | static int LZ4F_compressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)  | 
928  | 0  | { | 
929  | 0  |     int const acceleration = (level < 0) ? -level + 1 : 1;  | 
930  | 0  |     DEBUGLOG(5, "LZ4F_compressBlock (srcSize=%i)", srcSize);  | 
931  | 0  |     LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent);  | 
932  | 0  |     if (cdict) { | 
933  | 0  |         return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);  | 
934  | 0  |     } else { | 
935  | 0  |         return LZ4_compress_fast_extState_fastReset(ctx, src, dst, srcSize, dstCapacity, acceleration);  | 
936  | 0  |     }  | 
937  | 0  | }  | 
938  |  |  | 
939  |  | static int LZ4F_compressBlock_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)  | 
940  | 0  | { | 
941  | 0  |     int const acceleration = (level < 0) ? -level + 1 : 1;  | 
942  | 0  |     (void)cdict; /* init once at beginning of frame */  | 
943  | 0  |     DEBUGLOG(5, "LZ4F_compressBlock_continue (srcSize=%i)", srcSize);  | 
944  | 0  |     return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);  | 
945  | 0  | }  | 
946  |  |  | 
947  |  | static int LZ4F_compressBlockHC(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)  | 
948  | 0  | { | 
949  | 0  |     LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent);  | 
950  | 0  |     if (cdict) { | 
951  | 0  |         return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);  | 
952  | 0  |     }  | 
953  | 0  |     return LZ4_compress_HC_extStateHC_fastReset(ctx, src, dst, srcSize, dstCapacity, level);  | 
954  | 0  | }  | 
955  |  |  | 
956  |  | static int LZ4F_compressBlockHC_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)  | 
957  | 0  | { | 
958  | 0  |     (void)level; (void)cdict; /* init once at beginning of frame */  | 
959  | 0  |     return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);  | 
960  | 0  | }  | 
961  |  |  | 
962  |  | static int LZ4F_doNotCompressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)  | 
963  | 0  | { | 
964  | 0  |     (void)ctx; (void)src; (void)dst; (void)srcSize; (void)dstCapacity; (void)level; (void)cdict;  | 
965  | 0  |     return 0;  | 
966  | 0  | }  | 
967  |  |  | 
968  |  | static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level, LZ4F_BlockCompressMode_e  compressMode)  | 
969  | 0  | { | 
970  | 0  |     if (compressMode == LZ4B_UNCOMPRESSED)  | 
971  | 0  |         return LZ4F_doNotCompressBlock;  | 
972  | 0  |     if (level < LZ4HC_CLEVEL_MIN) { | 
973  | 0  |         if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock;  | 
974  | 0  |         return LZ4F_compressBlock_continue;  | 
975  | 0  |     }  | 
976  | 0  |     if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlockHC;  | 
977  | 0  |     return LZ4F_compressBlockHC_continue;  | 
978  | 0  | }  | 
979  |  |  | 
980  |  | /* Save or shorten history (up to 64KB) into @tmpBuff */  | 
981  |  | static void LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr)  | 
982  | 0  | { | 
983  | 0  |     int const dictSize = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ?  | 
984  | 0  |                     LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB) :  | 
985  | 0  |                     LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB);  | 
986  | 0  |     cctxPtr->tmpIn = cctxPtr->tmpBuff + dictSize;  | 
987  | 0  | }  | 
988  |  |  | 
989  |  | typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; | 
990  |  |  | 
991  |  | static const LZ4F_compressOptions_t k_cOptionsNull = { 0, { 0, 0, 0 } }; | 
992  |  |  | 
993  |  |  | 
994  |  | /*! LZ4F_compressUpdateImpl() :  | 
995  |  |  *  LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.  | 
996  |  |  *  When successful, the function always entirely consumes @srcBuffer.  | 
997  |  |  *  src data is either buffered or compressed into @dstBuffer.  | 
998  |  |  *  If the block compression does not match the compression of the previous block, the old data is flushed  | 
999  |  |  *  and operations continue with the new compression mode.  | 
1000  |  |  * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr) when block compression is turned on.  | 
1001  |  |  * @compressOptionsPtr is optional : provide NULL to mean "default".  | 
1002  |  |  * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.  | 
1003  |  |  *           or an error code if it fails (which can be tested using LZ4F_isError())  | 
1004  |  |  *  After an error, the state is left in a UB state, and must be re-initialized.  | 
1005  |  |  */  | 
1006  |  | static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr,  | 
1007  |  |                      void* dstBuffer, size_t dstCapacity,  | 
1008  |  |                      const void* srcBuffer, size_t srcSize,  | 
1009  |  |                      const LZ4F_compressOptions_t* compressOptionsPtr,  | 
1010  |  |                      LZ4F_BlockCompressMode_e blockCompression)  | 
1011  | 0  |   { | 
1012  | 0  |     size_t const blockSize = cctxPtr->maxBlockSize;  | 
1013  | 0  |     const BYTE* srcPtr = (const BYTE*)srcBuffer;  | 
1014  | 0  |     const BYTE* const srcEnd = srcSize ? (assert(srcPtr!=NULL), srcPtr + srcSize) : srcPtr;  | 
1015  | 0  |     BYTE* const dstStart = (BYTE*)dstBuffer;  | 
1016  | 0  |     BYTE* dstPtr = dstStart;  | 
1017  | 0  |     LZ4F_lastBlockStatus lastBlockCompressed = notDone;  | 
1018  | 0  |     compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, blockCompression);  | 
1019  | 0  |     size_t bytesWritten;  | 
1020  | 0  |     DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize);  | 
1021  |  | 
  | 
1022  | 0  |     RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized);   /* state must be initialized and waiting for next block */  | 
1023  | 0  |     if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize))  | 
1024  | 0  |         RETURN_ERROR(dstMaxSize_tooSmall);  | 
1025  |  |  | 
1026  | 0  |     if (blockCompression == LZ4B_UNCOMPRESSED && dstCapacity < srcSize)  | 
1027  | 0  |         RETURN_ERROR(dstMaxSize_tooSmall);  | 
1028  |  |  | 
1029  |  |     /* flush currently written block, to continue with new block compression */  | 
1030  | 0  |     if (cctxPtr->blockCompressMode != blockCompression) { | 
1031  | 0  |         bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr);  | 
1032  | 0  |         dstPtr += bytesWritten;  | 
1033  | 0  |         cctxPtr->blockCompressMode = blockCompression;  | 
1034  | 0  |     }  | 
1035  |  | 
  | 
1036  | 0  |     if (compressOptionsPtr == NULL) compressOptionsPtr = &k_cOptionsNull;  | 
1037  |  |  | 
1038  |  |     /* complete tmp buffer */  | 
1039  | 0  |     if (cctxPtr->tmpInSize > 0) {   /* some data already within tmp buffer */ | 
1040  | 0  |         size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize;  | 
1041  | 0  |         assert(blockSize > cctxPtr->tmpInSize);  | 
1042  | 0  |         if (sizeToCopy > srcSize) { | 
1043  |  |             /* add src to tmpIn buffer */  | 
1044  | 0  |             memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize);  | 
1045  | 0  |             srcPtr = srcEnd;  | 
1046  | 0  |             cctxPtr->tmpInSize += srcSize;  | 
1047  |  |             /* still needs some CRC */  | 
1048  | 0  |         } else { | 
1049  |  |             /* complete tmpIn block and then compress it */  | 
1050  | 0  |             lastBlockCompressed = fromTmpBuffer;  | 
1051  | 0  |             memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy);  | 
1052  | 0  |             srcPtr += sizeToCopy;  | 
1053  |  | 
  | 
1054  | 0  |             dstPtr += LZ4F_makeBlock(dstPtr,  | 
1055  | 0  |                                      cctxPtr->tmpIn, blockSize,  | 
1056  | 0  |                                      compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,  | 
1057  | 0  |                                      cctxPtr->cdict,  | 
1058  | 0  |                                      cctxPtr->prefs.frameInfo.blockChecksumFlag);  | 
1059  | 0  |             if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize;  | 
1060  | 0  |             cctxPtr->tmpInSize = 0;  | 
1061  | 0  |     }   }  | 
1062  |  | 
  | 
1063  | 0  |     while ((size_t)(srcEnd - srcPtr) >= blockSize) { | 
1064  |  |         /* compress full blocks */  | 
1065  | 0  |         lastBlockCompressed = fromSrcBuffer;  | 
1066  | 0  |         dstPtr += LZ4F_makeBlock(dstPtr,  | 
1067  | 0  |                                  srcPtr, blockSize,  | 
1068  | 0  |                                  compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,  | 
1069  | 0  |                                  cctxPtr->cdict,  | 
1070  | 0  |                                  cctxPtr->prefs.frameInfo.blockChecksumFlag);  | 
1071  | 0  |         srcPtr += blockSize;  | 
1072  | 0  |     }  | 
1073  |  | 
  | 
1074  | 0  |     if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { | 
1075  |  |         /* autoFlush : remaining input (< blockSize) is compressed */  | 
1076  | 0  |         lastBlockCompressed = fromSrcBuffer;  | 
1077  | 0  |         dstPtr += LZ4F_makeBlock(dstPtr,  | 
1078  | 0  |                                  srcPtr, (size_t)(srcEnd - srcPtr),  | 
1079  | 0  |                                  compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,  | 
1080  | 0  |                                  cctxPtr->cdict,  | 
1081  | 0  |                                  cctxPtr->prefs.frameInfo.blockChecksumFlag);  | 
1082  | 0  |         srcPtr = srcEnd;  | 
1083  | 0  |     }  | 
1084  |  |  | 
1085  |  |     /* preserve dictionary within @tmpBuff whenever necessary */  | 
1086  | 0  |     if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) { | 
1087  |  |         /* linked blocks are only supported in compressed mode, see LZ4F_uncompressedUpdate */  | 
1088  | 0  |         assert(blockCompression == LZ4B_COMPRESSED);  | 
1089  | 0  |         if (compressOptionsPtr->stableSrc) { | 
1090  | 0  |             cctxPtr->tmpIn = cctxPtr->tmpBuff;  /* src is stable : dictionary remains in src across invocations */  | 
1091  | 0  |         } else { | 
1092  | 0  |             LZ4F_localSaveDict(cctxPtr);  | 
1093  | 0  |         }  | 
1094  | 0  |     }  | 
1095  |  |  | 
1096  |  |     /* keep tmpIn within limits */  | 
1097  | 0  |     if (!(cctxPtr->prefs.autoFlush)  /* no autoflush : there may be some data left within internal buffer */  | 
1098  | 0  |       && (cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) )  /* not enough room to store next block */  | 
1099  | 0  |     { | 
1100  |  |         /* only preserve 64KB within internal buffer. Ensures there is enough room for next block.  | 
1101  |  |          * note: this situation necessarily implies lastBlockCompressed==fromTmpBuffer */  | 
1102  | 0  |         LZ4F_localSaveDict(cctxPtr);  | 
1103  | 0  |         assert((cctxPtr->tmpIn + blockSize) <= (cctxPtr->tmpBuff + cctxPtr->maxBufferSize));  | 
1104  | 0  |     }  | 
1105  |  |  | 
1106  |  |     /* some input data left, necessarily < blockSize */  | 
1107  | 0  |     if (srcPtr < srcEnd) { | 
1108  |  |         /* fill tmp buffer */  | 
1109  | 0  |         size_t const sizeToCopy = (size_t)(srcEnd - srcPtr);  | 
1110  | 0  |         memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy);  | 
1111  | 0  |         cctxPtr->tmpInSize = sizeToCopy;  | 
1112  | 0  |     }  | 
1113  |  | 
  | 
1114  | 0  |     if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled)  | 
1115  | 0  |         (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize);  | 
1116  |  | 
  | 
1117  | 0  |     cctxPtr->totalInSize += srcSize;  | 
1118  | 0  |     return (size_t)(dstPtr - dstStart);  | 
1119  | 0  | }  | 
1120  |  |  | 
1121  |  | /*! LZ4F_compressUpdate() :  | 
1122  |  |  *  LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.  | 
1123  |  |  *  When successful, the function always entirely consumes @srcBuffer.  | 
1124  |  |  *  src data is either buffered or compressed into @dstBuffer.  | 
1125  |  |  *  If previously an uncompressed block was written, buffered data is flushed  | 
1126  |  |  *  before appending compressed data is continued.  | 
1127  |  |  * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).  | 
1128  |  |  * @compressOptionsPtr is optional : provide NULL to mean "default".  | 
1129  |  |  * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.  | 
1130  |  |  *           or an error code if it fails (which can be tested using LZ4F_isError())  | 
1131  |  |  *  After an error, the state is left in a UB state, and must be re-initialized.  | 
1132  |  |  */  | 
1133  |  | size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,  | 
1134  |  |                            void* dstBuffer, size_t dstCapacity,  | 
1135  |  |                      const void* srcBuffer, size_t srcSize,  | 
1136  |  |                      const LZ4F_compressOptions_t* compressOptionsPtr)  | 
1137  | 0  | { | 
1138  | 0  |      return LZ4F_compressUpdateImpl(cctxPtr,  | 
1139  | 0  |                                    dstBuffer, dstCapacity,  | 
1140  | 0  |                                    srcBuffer, srcSize,  | 
1141  | 0  |                                    compressOptionsPtr, LZ4B_COMPRESSED);  | 
1142  | 0  | }  | 
1143  |  |  | 
1144  |  | /*! LZ4F_uncompressedUpdate() :  | 
1145  |  |  *  Same as LZ4F_compressUpdate(), but requests blocks to be sent uncompressed.  | 
1146  |  |  *  This symbol is only supported when LZ4F_blockIndependent is used  | 
1147  |  |  * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).  | 
1148  |  |  * @compressOptionsPtr is optional : provide NULL to mean "default".  | 
1149  |  |  * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.  | 
1150  |  |  *           or an error code if it fails (which can be tested using LZ4F_isError())  | 
1151  |  |  *  After an error, the state is left in a UB state, and must be re-initialized.  | 
1152  |  |  */  | 
1153  |  | size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr,  | 
1154  |  |                                void* dstBuffer, size_t dstCapacity,  | 
1155  |  |                          const void* srcBuffer, size_t srcSize,  | 
1156  |  |                          const LZ4F_compressOptions_t* compressOptionsPtr)  | 
1157  | 0  | { | 
1158  | 0  |     return LZ4F_compressUpdateImpl(cctxPtr,  | 
1159  | 0  |                                    dstBuffer, dstCapacity,  | 
1160  | 0  |                                    srcBuffer, srcSize,  | 
1161  | 0  |                                    compressOptionsPtr, LZ4B_UNCOMPRESSED);  | 
1162  | 0  | }  | 
1163  |  |  | 
1164  |  |  | 
1165  |  | /*! LZ4F_flush() :  | 
1166  |  |  *  When compressed data must be sent immediately, without waiting for a block to be filled,  | 
1167  |  |  *  invoke LZ4F_flush(), which will immediately compress any remaining data stored within LZ4F_cctx.  | 
1168  |  |  *  The result of the function is the number of bytes written into dstBuffer.  | 
1169  |  |  *  It can be zero, this means there was no data left within LZ4F_cctx.  | 
1170  |  |  *  The function outputs an error code if it fails (can be tested using LZ4F_isError())  | 
1171  |  |  *  LZ4F_compressOptions_t* is optional. NULL is a valid argument.  | 
1172  |  |  */  | 
1173  |  | size_t LZ4F_flush(LZ4F_cctx* cctxPtr,  | 
1174  |  |                   void* dstBuffer, size_t dstCapacity,  | 
1175  |  |             const LZ4F_compressOptions_t* compressOptionsPtr)  | 
1176  | 0  | { | 
1177  | 0  |     BYTE* const dstStart = (BYTE*)dstBuffer;  | 
1178  | 0  |     BYTE* dstPtr = dstStart;  | 
1179  | 0  |     compressFunc_t compress;  | 
1180  |  | 
  | 
1181  | 0  |     DEBUGLOG(5, "LZ4F_flush: %zu buffered bytes (saved dict size = %i) (dstCapacity=%u)",  | 
1182  | 0  |             cctxPtr->tmpInSize, (int)(cctxPtr->tmpIn - cctxPtr->tmpBuff), (unsigned)dstCapacity);  | 
1183  | 0  |     if (cctxPtr->tmpInSize == 0) return 0;   /* nothing to flush */  | 
1184  | 0  |     RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized);  | 
1185  | 0  |     RETURN_ERROR_IF(dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize), dstMaxSize_tooSmall);  | 
1186  | 0  |     (void)compressOptionsPtr;   /* not useful (yet) */  | 
1187  |  |  | 
1188  |  |     /* select compression function */  | 
1189  | 0  |     compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, cctxPtr->blockCompressMode);  | 
1190  |  |  | 
1191  |  |     /* compress tmp buffer */  | 
1192  | 0  |     dstPtr += LZ4F_makeBlock(dstPtr,  | 
1193  | 0  |                              cctxPtr->tmpIn, cctxPtr->tmpInSize,  | 
1194  | 0  |                              compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,  | 
1195  | 0  |                              cctxPtr->cdict,  | 
1196  | 0  |                              cctxPtr->prefs.frameInfo.blockChecksumFlag);  | 
1197  | 0  |     assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity));  | 
1198  |  | 
  | 
1199  | 0  |     if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked)  | 
1200  | 0  |         cctxPtr->tmpIn += cctxPtr->tmpInSize;  | 
1201  | 0  |     cctxPtr->tmpInSize = 0;  | 
1202  |  |  | 
1203  |  |     /* keep tmpIn within limits */  | 
1204  | 0  |     if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) { | 
1205  | 0  |         assert(cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked);  | 
1206  | 0  |         LZ4F_localSaveDict(cctxPtr);  | 
1207  | 0  |     }  | 
1208  |  | 
  | 
1209  | 0  |     return (size_t)(dstPtr - dstStart);  | 
1210  | 0  | }  | 
1211  |  |  | 
1212  |  |  | 
1213  |  | /*! LZ4F_compressEnd() :  | 
1214  |  |  *  When you want to properly finish the compressed frame, just call LZ4F_compressEnd().  | 
1215  |  |  *  It will flush whatever data remained within compressionContext (like LZ4_flush())  | 
1216  |  |  *  but also properly finalize the frame, with an endMark and an (optional) checksum.  | 
1217  |  |  *  LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.  | 
1218  |  |  * @return: the number of bytes written into dstBuffer (necessarily >= 4 (endMark size))  | 
1219  |  |  *       or an error code if it fails (can be tested using LZ4F_isError())  | 
1220  |  |  *  The context can then be used again to compress a new frame, starting with LZ4F_compressBegin().  | 
1221  |  |  */  | 
1222  |  | size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr,  | 
1223  |  |                         void* dstBuffer, size_t dstCapacity,  | 
1224  |  |                   const LZ4F_compressOptions_t* compressOptionsPtr)  | 
1225  | 0  | { | 
1226  | 0  |     BYTE* const dstStart = (BYTE*)dstBuffer;  | 
1227  | 0  |     BYTE* dstPtr = dstStart;  | 
1228  |  | 
  | 
1229  | 0  |     size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr);  | 
1230  | 0  |     DEBUGLOG(5,"LZ4F_compressEnd: dstCapacity=%u", (unsigned)dstCapacity);  | 
1231  | 0  |     FORWARD_IF_ERROR(flushSize);  | 
1232  | 0  |     dstPtr += flushSize;  | 
1233  |  | 
  | 
1234  | 0  |     assert(flushSize <= dstCapacity);  | 
1235  | 0  |     dstCapacity -= flushSize;  | 
1236  |  | 
  | 
1237  | 0  |     RETURN_ERROR_IF(dstCapacity < 4, dstMaxSize_tooSmall);  | 
1238  | 0  |     LZ4F_writeLE32(dstPtr, 0);  | 
1239  | 0  |     dstPtr += 4;   /* endMark */  | 
1240  |  | 
  | 
1241  | 0  |     if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) { | 
1242  | 0  |         U32 const xxh = XXH32_digest(&(cctxPtr->xxh));  | 
1243  | 0  |         RETURN_ERROR_IF(dstCapacity < 8, dstMaxSize_tooSmall);  | 
1244  | 0  |         DEBUGLOG(5,"Writing 32-bit content checksum (0x%0X)", xxh);  | 
1245  | 0  |         LZ4F_writeLE32(dstPtr, xxh);  | 
1246  | 0  |         dstPtr+=4;   /* content Checksum */  | 
1247  | 0  |     }  | 
1248  |  |  | 
1249  | 0  |     cctxPtr->cStage = 0;   /* state is now re-usable (with identical preferences) */  | 
1250  |  | 
  | 
1251  | 0  |     if (cctxPtr->prefs.frameInfo.contentSize) { | 
1252  | 0  |         if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize)  | 
1253  | 0  |             RETURN_ERROR(frameSize_wrong);  | 
1254  | 0  |     }  | 
1255  |  |  | 
1256  | 0  |     return (size_t)(dstPtr - dstStart);  | 
1257  | 0  | }  | 
1258  |  |  | 
1259  |  |  | 
1260  |  | /*-***************************************************  | 
1261  |  | *   Frame Decompression  | 
1262  |  | *****************************************************/  | 
1263  |  |  | 
1264  |  | typedef enum { | 
1265  |  |     dstage_getFrameHeader=0, dstage_storeFrameHeader,  | 
1266  |  |     dstage_init,  | 
1267  |  |     dstage_getBlockHeader, dstage_storeBlockHeader,  | 
1268  |  |     dstage_copyDirect, dstage_getBlockChecksum,  | 
1269  |  |     dstage_getCBlock, dstage_storeCBlock,  | 
1270  |  |     dstage_flushOut,  | 
1271  |  |     dstage_getSuffix, dstage_storeSuffix,  | 
1272  |  |     dstage_getSFrameSize, dstage_storeSFrameSize,  | 
1273  |  |     dstage_skipSkippable  | 
1274  |  | } dStage_t;  | 
1275  |  |  | 
1276  |  | struct LZ4F_dctx_s { | 
1277  |  |     LZ4F_CustomMem cmem;  | 
1278  |  |     LZ4F_frameInfo_t frameInfo;  | 
1279  |  |     U32    version;  | 
1280  |  |     dStage_t dStage;  | 
1281  |  |     U64    frameRemainingSize;  | 
1282  |  |     size_t maxBlockSize;  | 
1283  |  |     size_t maxBufferSize;  | 
1284  |  |     BYTE*  tmpIn;  | 
1285  |  |     size_t tmpInSize;  | 
1286  |  |     size_t tmpInTarget;  | 
1287  |  |     BYTE*  tmpOutBuffer;  | 
1288  |  |     const BYTE* dict;  | 
1289  |  |     size_t dictSize;  | 
1290  |  |     BYTE*  tmpOut;  | 
1291  |  |     size_t tmpOutSize;  | 
1292  |  |     size_t tmpOutStart;  | 
1293  |  |     XXH32_state_t xxh;  | 
1294  |  |     XXH32_state_t blockChecksum;  | 
1295  |  |     int    skipChecksum;  | 
1296  |  |     BYTE   header[LZ4F_HEADER_SIZE_MAX];  | 
1297  |  | };  /* typedef'd to LZ4F_dctx in lz4frame.h */  | 
1298  |  |  | 
1299  |  |  | 
1300  |  | LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version)  | 
1301  | 0  | { | 
1302  | 0  |     LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), customMem);  | 
1303  | 0  |     if (dctx == NULL) return NULL;  | 
1304  |  |  | 
1305  | 0  |     dctx->cmem = customMem;  | 
1306  | 0  |     dctx->version = version;  | 
1307  | 0  |     return dctx;  | 
1308  | 0  | }  | 
1309  |  |  | 
1310  |  | /*! LZ4F_createDecompressionContext() :  | 
1311  |  |  *  Create a decompressionContext object, which will track all decompression operations.  | 
1312  |  |  *  Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object.  | 
1313  |  |  *  Object can later be released using LZ4F_freeDecompressionContext().  | 
1314  |  |  * @return : if != 0, there was an error during context creation.  | 
1315  |  |  */  | 
1316  |  | LZ4F_errorCode_t  | 
1317  |  | LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber)  | 
1318  | 0  | { | 
1319  | 0  |     assert(LZ4F_decompressionContextPtr != NULL);  /* violation of narrow contract */  | 
1320  | 0  |     RETURN_ERROR_IF(LZ4F_decompressionContextPtr == NULL, parameter_null);  /* in case it nonetheless happen in production */  | 
1321  |  |  | 
1322  | 0  |     *LZ4F_decompressionContextPtr = LZ4F_createDecompressionContext_advanced(LZ4F_defaultCMem, versionNumber);  | 
1323  | 0  |     if (*LZ4F_decompressionContextPtr == NULL) {  /* failed allocation */ | 
1324  | 0  |         RETURN_ERROR(allocation_failed);  | 
1325  | 0  |     }  | 
1326  | 0  |     return LZ4F_OK_NoError;  | 
1327  | 0  | }  | 
1328  |  |  | 
1329  |  | LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)  | 
1330  | 0  | { | 
1331  | 0  |     LZ4F_errorCode_t result = LZ4F_OK_NoError;  | 
1332  | 0  |     if (dctx != NULL) {   /* can accept NULL input, like free() */ | 
1333  | 0  |       result = (LZ4F_errorCode_t)dctx->dStage;  | 
1334  | 0  |       LZ4F_free(dctx->tmpIn, dctx->cmem);  | 
1335  | 0  |       LZ4F_free(dctx->tmpOutBuffer, dctx->cmem);  | 
1336  | 0  |       LZ4F_free(dctx, dctx->cmem);  | 
1337  | 0  |     }  | 
1338  | 0  |     return result;  | 
1339  | 0  | }  | 
1340  |  |  | 
1341  | 0  | size_t LZ4F_dctx_size(const LZ4F_dctx* dctx) { | 
1342  | 0  |     if (dctx == NULL) { | 
1343  | 0  |         return 0;  | 
1344  | 0  |     }  | 
1345  | 0  |     return sizeof(*dctx)  | 
1346  | 0  |          + (dctx->tmpIn != NULL ? dctx->maxBlockSize + BFSize : 0)  | 
1347  | 0  |          + (dctx->tmpOutBuffer != NULL ? dctx->maxBufferSize : 0);  | 
1348  | 0  | }  | 
1349  |  |  | 
1350  |  |  | 
1351  |  | /*==---   Streaming Decompression operations   ---==*/  | 
1352  |  | void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)  | 
1353  | 0  | { | 
1354  | 0  |     DEBUGLOG(5, "LZ4F_resetDecompressionContext");  | 
1355  | 0  |     dctx->dStage = dstage_getFrameHeader;  | 
1356  | 0  |     dctx->dict = NULL;  | 
1357  | 0  |     dctx->dictSize = 0;  | 
1358  | 0  |     dctx->skipChecksum = 0;  | 
1359  | 0  |     dctx->frameRemainingSize = 0;  | 
1360  | 0  | }  | 
1361  |  |  | 
1362  |  |  | 
1363  |  | /*! LZ4F_decodeHeader() :  | 
1364  |  |  *  input   : `src` points at the **beginning of the frame**  | 
1365  |  |  *  output  : set internal values of dctx, such as  | 
1366  |  |  *            dctx->frameInfo and dctx->dStage.  | 
1367  |  |  *            Also allocates internal buffers.  | 
1368  |  |  *  @return : nb Bytes read from src (necessarily <= srcSize)  | 
1369  |  |  *            or an error code (testable with LZ4F_isError())  | 
1370  |  |  */  | 
1371  |  | static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize)  | 
1372  | 0  | { | 
1373  | 0  |     unsigned blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictIDFlag, blockSizeID;  | 
1374  | 0  |     size_t frameHeaderSize;  | 
1375  | 0  |     const BYTE* srcPtr = (const BYTE*)src;  | 
1376  |  | 
  | 
1377  | 0  |     DEBUGLOG(5, "LZ4F_decodeHeader");  | 
1378  |  |     /* need to decode header to get frameInfo */  | 
1379  | 0  |     RETURN_ERROR_IF(srcSize < minFHSize, frameHeader_incomplete);   /* minimal frame header size */  | 
1380  | 0  |     MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo));  | 
1381  |  |  | 
1382  |  |     /* special case : skippable frames */  | 
1383  | 0  |     if ((LZ4F_readLE32(srcPtr) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) { | 
1384  | 0  |         dctx->frameInfo.frameType = LZ4F_skippableFrame;  | 
1385  | 0  |         if (src == (void*)(dctx->header)) { | 
1386  | 0  |             dctx->tmpInSize = srcSize;  | 
1387  | 0  |             dctx->tmpInTarget = 8;  | 
1388  | 0  |             dctx->dStage = dstage_storeSFrameSize;  | 
1389  | 0  |             return srcSize;  | 
1390  | 0  |         } else { | 
1391  | 0  |             dctx->dStage = dstage_getSFrameSize;  | 
1392  | 0  |             return 4;  | 
1393  | 0  |     }   }  | 
1394  |  |  | 
1395  |  |     /* control magic number */  | 
1396  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
1397  |  |     if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) { | 
1398  |  |         DEBUGLOG(4, "frame header error : unknown magic number");  | 
1399  |  |         RETURN_ERROR(frameType_unknown);  | 
1400  |  |     }  | 
1401  |  | #endif  | 
1402  | 0  |     dctx->frameInfo.frameType = LZ4F_frame;  | 
1403  |  |  | 
1404  |  |     /* Flags */  | 
1405  | 0  |     {   U32 const FLG = srcPtr[4]; | 
1406  | 0  |         U32 const version = (FLG>>6) & _2BITS;  | 
1407  | 0  |         blockChecksumFlag = (FLG>>4) & _1BIT;  | 
1408  | 0  |         blockMode = (FLG>>5) & _1BIT;  | 
1409  | 0  |         contentSizeFlag = (FLG>>3) & _1BIT;  | 
1410  | 0  |         contentChecksumFlag = (FLG>>2) & _1BIT;  | 
1411  | 0  |         dictIDFlag = FLG & _1BIT;  | 
1412  |  |         /* validate */  | 
1413  | 0  |         if (((FLG>>1)&_1BIT) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bit */  | 
1414  | 0  |         if (version != 1) RETURN_ERROR(headerVersion_wrong);       /* Version Number, only supported value */  | 
1415  | 0  |     }  | 
1416  | 0  |     DEBUGLOG(6, "contentSizeFlag: %u", contentSizeFlag);  | 
1417  |  |  | 
1418  |  |     /* Frame Header Size */  | 
1419  | 0  |     frameHeaderSize = minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0);  | 
1420  |  | 
  | 
1421  | 0  |     if (srcSize < frameHeaderSize) { | 
1422  |  |         /* not enough input to fully decode frame header */  | 
1423  | 0  |         if (srcPtr != dctx->header)  | 
1424  | 0  |             memcpy(dctx->header, srcPtr, srcSize);  | 
1425  | 0  |         dctx->tmpInSize = srcSize;  | 
1426  | 0  |         dctx->tmpInTarget = frameHeaderSize;  | 
1427  | 0  |         dctx->dStage = dstage_storeFrameHeader;  | 
1428  | 0  |         return srcSize;  | 
1429  | 0  |     }  | 
1430  |  |  | 
1431  | 0  |     {   U32 const BD = srcPtr[5]; | 
1432  | 0  |         blockSizeID = (BD>>4) & _3BITS;  | 
1433  |  |         /* validate */  | 
1434  | 0  |         if (((BD>>7)&_1BIT) != 0) RETURN_ERROR(reservedFlag_set);   /* Reserved bit */  | 
1435  | 0  |         if (blockSizeID < 4) RETURN_ERROR(maxBlockSize_invalid);    /* 4-7 only supported values for the time being */  | 
1436  | 0  |         if (((BD>>0)&_4BITS) != 0) RETURN_ERROR(reservedFlag_set);  /* Reserved bits */  | 
1437  | 0  |     }  | 
1438  |  |  | 
1439  |  |     /* check header */  | 
1440  | 0  |     assert(frameHeaderSize > 5);  | 
1441  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
1442  |  |     {   BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); | 
1443  |  |         RETURN_ERROR_IF(HC != srcPtr[frameHeaderSize-1], headerChecksum_invalid);  | 
1444  |  |     }  | 
1445  |  | #endif  | 
1446  |  |  | 
1447  |  |     /* save */  | 
1448  | 0  |     dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;  | 
1449  | 0  |     dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag;  | 
1450  | 0  |     dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;  | 
1451  | 0  |     dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;  | 
1452  | 0  |     dctx->maxBlockSize = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID);  | 
1453  | 0  |     if (contentSizeFlag) { | 
1454  | 0  |         dctx->frameRemainingSize = dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);  | 
1455  | 0  |     }  | 
1456  | 0  |     if (dictIDFlag)  | 
1457  | 0  |         dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5);  | 
1458  |  | 
  | 
1459  | 0  |     dctx->dStage = dstage_init;  | 
1460  |  | 
  | 
1461  | 0  |     return frameHeaderSize;  | 
1462  | 0  | }  | 
1463  |  |  | 
1464  |  |  | 
1465  |  | /*! LZ4F_headerSize() :  | 
1466  |  |  * @return : size of frame header  | 
1467  |  |  *           or an error code, which can be tested using LZ4F_isError()  | 
1468  |  |  */  | 
1469  |  | size_t LZ4F_headerSize(const void* src, size_t srcSize)  | 
1470  | 0  | { | 
1471  | 0  |     RETURN_ERROR_IF(src == NULL, srcPtr_wrong);  | 
1472  |  |  | 
1473  |  |     /* minimal srcSize to determine header size */  | 
1474  | 0  |     if (srcSize < LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH)  | 
1475  | 0  |         RETURN_ERROR(frameHeader_incomplete);  | 
1476  |  |  | 
1477  |  |     /* special case : skippable frames */  | 
1478  | 0  |     if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START)  | 
1479  | 0  |         return 8;  | 
1480  |  |  | 
1481  |  |     /* control magic number */  | 
1482  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
1483  |  |     if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER)  | 
1484  |  |         RETURN_ERROR(frameType_unknown);  | 
1485  |  | #endif  | 
1486  |  |  | 
1487  |  |     /* Frame Header Size */  | 
1488  | 0  |     {   BYTE const FLG = ((const BYTE*)src)[4]; | 
1489  | 0  |         U32 const contentSizeFlag = (FLG>>3) & _1BIT;  | 
1490  | 0  |         U32 const dictIDFlag = FLG & _1BIT;  | 
1491  | 0  |         return minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0);  | 
1492  | 0  |     }  | 
1493  | 0  | }  | 
1494  |  |  | 
1495  |  | /*! LZ4F_getFrameInfo() :  | 
1496  |  |  *  This function extracts frame parameters (max blockSize, frame checksum, etc.).  | 
1497  |  |  *  Usage is optional. Objective is to provide relevant information for allocation purposes.  | 
1498  |  |  *  This function works in 2 situations :  | 
1499  |  |  *   - At the beginning of a new frame, in which case it will decode this information from `srcBuffer`, and start the decoding process.  | 
1500  |  |  *     Amount of input data provided must be large enough to successfully decode the frame header.  | 
1501  |  |  *     A header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. It's possible to provide more input data than this minimum.  | 
1502  |  |  *   - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.  | 
1503  |  |  *  The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value).  | 
1504  |  |  *  Decompression must resume from (srcBuffer + *srcSizePtr).  | 
1505  |  |  * @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call,  | 
1506  |  |  *           or an error code which can be tested using LZ4F_isError()  | 
1507  |  |  *  note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.  | 
1508  |  |  *  note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure.  | 
1509  |  |  */  | 
1510  |  | LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx,  | 
1511  |  |                                    LZ4F_frameInfo_t* frameInfoPtr,  | 
1512  |  |                              const void* srcBuffer, size_t* srcSizePtr)  | 
1513  | 0  | { | 
1514  | 0  |     assert(dctx != NULL);  | 
1515  | 0  |     RETURN_ERROR_IF(frameInfoPtr == NULL, parameter_null);  | 
1516  | 0  |     RETURN_ERROR_IF(srcSizePtr == NULL, parameter_null);  | 
1517  |  |  | 
1518  | 0  |     LZ4F_STATIC_ASSERT(dstage_getFrameHeader < dstage_storeFrameHeader);  | 
1519  | 0  |     if (dctx->dStage > dstage_storeFrameHeader) { | 
1520  |  |         /* frameInfo already decoded */  | 
1521  | 0  |         size_t o=0, i=0;  | 
1522  | 0  |         *srcSizePtr = 0;  | 
1523  | 0  |         *frameInfoPtr = dctx->frameInfo;  | 
1524  |  |         /* returns : recommended nb of bytes for LZ4F_decompress() */  | 
1525  | 0  |         return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL);  | 
1526  | 0  |     } else { | 
1527  | 0  |         if (dctx->dStage == dstage_storeFrameHeader) { | 
1528  |  |             /* frame decoding already started, in the middle of header => automatic fail */  | 
1529  | 0  |             *srcSizePtr = 0;  | 
1530  | 0  |             RETURN_ERROR(frameDecoding_alreadyStarted);  | 
1531  | 0  |         } else { | 
1532  | 0  |             size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr);  | 
1533  | 0  |             if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; } | 
1534  | 0  |             if (*srcSizePtr < hSize) { | 
1535  | 0  |                 *srcSizePtr=0;  | 
1536  | 0  |                 RETURN_ERROR(frameHeader_incomplete);  | 
1537  | 0  |             }  | 
1538  |  |  | 
1539  | 0  |             {   size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize); | 
1540  | 0  |                 if (LZ4F_isError(decodeResult)) { | 
1541  | 0  |                     *srcSizePtr = 0;  | 
1542  | 0  |                 } else { | 
1543  | 0  |                     *srcSizePtr = decodeResult;  | 
1544  | 0  |                     decodeResult = BHSize;   /* block header size */  | 
1545  | 0  |                 }  | 
1546  | 0  |                 *frameInfoPtr = dctx->frameInfo;  | 
1547  | 0  |                 return decodeResult;  | 
1548  | 0  |     }   }   }  | 
1549  | 0  | }  | 
1550  |  |  | 
1551  |  |  | 
1552  |  | /* LZ4F_updateDict() :  | 
1553  |  |  * only used for LZ4F_blockLinked mode  | 
1554  |  |  * Condition : @dstPtr != NULL  | 
1555  |  |  */  | 
1556  |  | static void LZ4F_updateDict(LZ4F_dctx* dctx,  | 
1557  |  |                       const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart,  | 
1558  |  |                       unsigned withinTmp)  | 
1559  | 0  | { | 
1560  | 0  |     assert(dstPtr != NULL);  | 
1561  | 0  |     if (dctx->dictSize==0) dctx->dict = (const BYTE*)dstPtr;  /* will lead to prefix mode */  | 
1562  | 0  |     assert(dctx->dict != NULL);  | 
1563  |  | 
  | 
1564  | 0  |     if (dctx->dict + dctx->dictSize == dstPtr) {  /* prefix mode, everything within dstBuffer */ | 
1565  | 0  |         dctx->dictSize += dstSize;  | 
1566  | 0  |         return;  | 
1567  | 0  |     }  | 
1568  |  |  | 
1569  | 0  |     assert(dstPtr >= dstBufferStart);  | 
1570  | 0  |     if ((size_t)(dstPtr - dstBufferStart) + dstSize >= 64 KB) {  /* history in dstBuffer becomes large enough to become dictionary */ | 
1571  | 0  |         dctx->dict = (const BYTE*)dstBufferStart;  | 
1572  | 0  |         dctx->dictSize = (size_t)(dstPtr - dstBufferStart) + dstSize;  | 
1573  | 0  |         return;  | 
1574  | 0  |     }  | 
1575  |  |  | 
1576  | 0  |     assert(dstSize < 64 KB);   /* if dstSize >= 64 KB, dictionary would be set into dstBuffer directly */  | 
1577  |  |  | 
1578  |  |     /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOutBuffer */  | 
1579  | 0  |     assert(dctx->tmpOutBuffer != NULL);  | 
1580  |  | 
  | 
1581  | 0  |     if (withinTmp && (dctx->dict == dctx->tmpOutBuffer)) {   /* continue history within tmpOutBuffer */ | 
1582  |  |         /* withinTmp expectation : content of [dstPtr,dstSize] is same as [dict+dictSize,dstSize], so we just extend it */  | 
1583  | 0  |         assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart);  | 
1584  | 0  |         dctx->dictSize += dstSize;  | 
1585  | 0  |         return;  | 
1586  | 0  |     }  | 
1587  |  |  | 
1588  | 0  |     if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ | 
1589  | 0  |         size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);  | 
1590  | 0  |         size_t copySize = 64 KB - dctx->tmpOutSize;  | 
1591  | 0  |         const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;  | 
1592  | 0  |         if (dctx->tmpOutSize > 64 KB) copySize = 0;  | 
1593  | 0  |         if (copySize > preserveSize) copySize = preserveSize;  | 
1594  |  | 
  | 
1595  | 0  |         memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);  | 
1596  |  | 
  | 
1597  | 0  |         dctx->dict = dctx->tmpOutBuffer;  | 
1598  | 0  |         dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize;  | 
1599  | 0  |         return;  | 
1600  | 0  |     }  | 
1601  |  |  | 
1602  | 0  |     if (dctx->dict == dctx->tmpOutBuffer) {    /* copy dst into tmp to complete dict */ | 
1603  | 0  |         if (dctx->dictSize + dstSize > dctx->maxBufferSize) {  /* tmp buffer not large enough */ | 
1604  | 0  |             size_t const preserveSize = 64 KB - dstSize;  | 
1605  | 0  |             memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);  | 
1606  | 0  |             dctx->dictSize = preserveSize;  | 
1607  | 0  |         }  | 
1608  | 0  |         memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize);  | 
1609  | 0  |         dctx->dictSize += dstSize;  | 
1610  | 0  |         return;  | 
1611  | 0  |     }  | 
1612  |  |  | 
1613  |  |     /* join dict & dest into tmp */  | 
1614  | 0  |     {   size_t preserveSize = 64 KB - dstSize; | 
1615  | 0  |         if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize;  | 
1616  | 0  |         memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);  | 
1617  | 0  |         memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize);  | 
1618  | 0  |         dctx->dict = dctx->tmpOutBuffer;  | 
1619  | 0  |         dctx->dictSize = preserveSize + dstSize;  | 
1620  | 0  |     }  | 
1621  | 0  | }  | 
1622  |  |  | 
1623  |  |  | 
1624  |  | /*! LZ4F_decompress() :  | 
1625  |  |  *  Call this function repetitively to regenerate compressed data in srcBuffer.  | 
1626  |  |  *  The function will attempt to decode up to *srcSizePtr bytes from srcBuffer  | 
1627  |  |  *  into dstBuffer of capacity *dstSizePtr.  | 
1628  |  |  *  | 
1629  |  |  *  The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).  | 
1630  |  |  *  | 
1631  |  |  *  The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).  | 
1632  |  |  *  If number of bytes read is < number of bytes provided, then decompression operation is not complete.  | 
1633  |  |  *  Remaining data will have to be presented again in a subsequent invocation.  | 
1634  |  |  *  | 
1635  |  |  *  The function result is an hint of the better srcSize to use for next call to LZ4F_decompress.  | 
1636  |  |  *  Schematically, it's the size of the current (or remaining) compressed block + header of next block.  | 
1637  |  |  *  Respecting the hint provides a small boost to performance, since it allows less buffer shuffling.  | 
1638  |  |  *  Note that this is just a hint, and it's always possible to any srcSize value.  | 
1639  |  |  *  When a frame is fully decoded, @return will be 0.  | 
1640  |  |  *  If decompression failed, @return is an error code which can be tested using LZ4F_isError().  | 
1641  |  |  */  | 
1642  |  | size_t LZ4F_decompress(LZ4F_dctx* dctx,  | 
1643  |  |                        void* dstBuffer, size_t* dstSizePtr,  | 
1644  |  |                        const void* srcBuffer, size_t* srcSizePtr,  | 
1645  |  |                        const LZ4F_decompressOptions_t* decompressOptionsPtr)  | 
1646  | 0  | { | 
1647  | 0  |     LZ4F_decompressOptions_t optionsNull;  | 
1648  | 0  |     const BYTE* const srcStart = (const BYTE*)srcBuffer;  | 
1649  | 0  |     const BYTE* const srcEnd = srcStart + *srcSizePtr;  | 
1650  | 0  |     const BYTE* srcPtr = srcStart;  | 
1651  | 0  |     BYTE* const dstStart = (BYTE*)dstBuffer;  | 
1652  | 0  |     BYTE* const dstEnd = dstStart ? dstStart + *dstSizePtr : NULL;  | 
1653  | 0  |     BYTE* dstPtr = dstStart;  | 
1654  | 0  |     const BYTE* selectedIn = NULL;  | 
1655  | 0  |     unsigned doAnotherStage = 1;  | 
1656  | 0  |     size_t nextSrcSizeHint = 1;  | 
1657  |  |  | 
1658  |  | 
  | 
1659  | 0  |     DEBUGLOG(5, "LZ4F_decompress: src[%p](%u) => dst[%p](%u)",  | 
1660  | 0  |             srcBuffer, (unsigned)*srcSizePtr, dstBuffer, (unsigned)*dstSizePtr);  | 
1661  | 0  |     if (dstBuffer == NULL) assert(*dstSizePtr == 0);  | 
1662  | 0  |     MEM_INIT(&optionsNull, 0, sizeof(optionsNull));  | 
1663  | 0  |     if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull;  | 
1664  | 0  |     *srcSizePtr = 0;  | 
1665  | 0  |     *dstSizePtr = 0;  | 
1666  | 0  |     assert(dctx != NULL);  | 
1667  | 0  |     dctx->skipChecksum |= (decompressOptionsPtr->skipChecksums != 0); /* once set, disable for the remainder of the frame */  | 
1668  |  |  | 
1669  |  |     /* behaves as a state machine */  | 
1670  |  | 
  | 
1671  | 0  |     while (doAnotherStage) { | 
1672  |  | 
  | 
1673  | 0  |         switch(dctx->dStage)  | 
1674  | 0  |         { | 
1675  |  |  | 
1676  | 0  |         case dstage_getFrameHeader:  | 
1677  | 0  |             DEBUGLOG(6, "dstage_getFrameHeader");  | 
1678  | 0  |             if ((size_t)(srcEnd-srcPtr) >= maxFHSize) {  /* enough to decode - shortcut */ | 
1679  | 0  |                 size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr));  /* will update dStage appropriately */  | 
1680  | 0  |                 FORWARD_IF_ERROR(hSize);  | 
1681  | 0  |                 srcPtr += hSize;  | 
1682  | 0  |                 break;  | 
1683  | 0  |             }  | 
1684  | 0  |             dctx->tmpInSize = 0;  | 
1685  | 0  |             if (srcEnd-srcPtr == 0) return minFHSize;   /* 0-size input */  | 
1686  | 0  |             dctx->tmpInTarget = minFHSize;   /* minimum size to decode header */  | 
1687  | 0  |             dctx->dStage = dstage_storeFrameHeader;  | 
1688  |  |             /* fall-through */  | 
1689  |  | 
  | 
1690  | 0  |         case dstage_storeFrameHeader:  | 
1691  | 0  |             DEBUGLOG(6, "dstage_storeFrameHeader");  | 
1692  | 0  |             {   size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr)); | 
1693  | 0  |                 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
1694  | 0  |                 dctx->tmpInSize += sizeToCopy;  | 
1695  | 0  |                 srcPtr += sizeToCopy;  | 
1696  | 0  |             }  | 
1697  | 0  |             if (dctx->tmpInSize < dctx->tmpInTarget) { | 
1698  | 0  |                 nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize;   /* rest of header + nextBlockHeader */  | 
1699  | 0  |                 doAnotherStage = 0;   /* not enough src data, ask for some more */  | 
1700  | 0  |                 break;  | 
1701  | 0  |             }  | 
1702  | 0  |             FORWARD_IF_ERROR( LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget) ); /* will update dStage appropriately */  | 
1703  | 0  |             break;  | 
1704  |  |  | 
1705  | 0  |         case dstage_init:  | 
1706  | 0  |             DEBUGLOG(6, "dstage_init");  | 
1707  | 0  |             if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0);  | 
1708  |  |             /* internal buffers allocation */  | 
1709  | 0  |             {   size_t const bufferNeeded = dctx->maxBlockSize | 
1710  | 0  |                     + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0);  | 
1711  | 0  |                 if (bufferNeeded > dctx->maxBufferSize) {   /* tmp buffers too small */ | 
1712  | 0  |                     dctx->maxBufferSize = 0;   /* ensure allocation will be re-attempted on next entry*/  | 
1713  | 0  |                     LZ4F_free(dctx->tmpIn, dctx->cmem);  | 
1714  | 0  |                     dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, dctx->cmem);  | 
1715  | 0  |                     RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed);  | 
1716  | 0  |                     LZ4F_free(dctx->tmpOutBuffer, dctx->cmem);  | 
1717  | 0  |                     dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, dctx->cmem);  | 
1718  | 0  |                     RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed);  | 
1719  | 0  |                     dctx->maxBufferSize = bufferNeeded;  | 
1720  | 0  |             }   }  | 
1721  | 0  |             dctx->tmpInSize = 0;  | 
1722  | 0  |             dctx->tmpInTarget = 0;  | 
1723  | 0  |             dctx->tmpOut = dctx->tmpOutBuffer;  | 
1724  | 0  |             dctx->tmpOutStart = 0;  | 
1725  | 0  |             dctx->tmpOutSize = 0;  | 
1726  |  | 
  | 
1727  | 0  |             dctx->dStage = dstage_getBlockHeader;  | 
1728  |  |             /* fall-through */  | 
1729  |  | 
  | 
1730  | 0  |         case dstage_getBlockHeader:  | 
1731  | 0  |             if ((size_t)(srcEnd - srcPtr) >= BHSize) { | 
1732  | 0  |                 selectedIn = srcPtr;  | 
1733  | 0  |                 srcPtr += BHSize;  | 
1734  | 0  |             } else { | 
1735  |  |                 /* not enough input to read cBlockSize field */  | 
1736  | 0  |                 dctx->tmpInSize = 0;  | 
1737  | 0  |                 dctx->dStage = dstage_storeBlockHeader;  | 
1738  | 0  |             }  | 
1739  |  | 
  | 
1740  | 0  |             if (dctx->dStage == dstage_storeBlockHeader)   /* can be skipped */  | 
1741  | 0  |         case dstage_storeBlockHeader:  | 
1742  | 0  |             {   size_t const remainingInput = (size_t)(srcEnd - srcPtr); | 
1743  | 0  |                 size_t const wantedData = BHSize - dctx->tmpInSize;  | 
1744  | 0  |                 size_t const sizeToCopy = MIN(wantedData, remainingInput);  | 
1745  | 0  |                 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
1746  | 0  |                 srcPtr += sizeToCopy;  | 
1747  | 0  |                 dctx->tmpInSize += sizeToCopy;  | 
1748  |  | 
  | 
1749  | 0  |                 if (dctx->tmpInSize < BHSize) {   /* not enough input for cBlockSize */ | 
1750  | 0  |                     nextSrcSizeHint = BHSize - dctx->tmpInSize;  | 
1751  | 0  |                     doAnotherStage  = 0;  | 
1752  | 0  |                     break;  | 
1753  | 0  |                 }  | 
1754  | 0  |                 selectedIn = dctx->tmpIn;  | 
1755  | 0  |             }   /* if (dctx->dStage == dstage_storeBlockHeader) */  | 
1756  |  |  | 
1757  |  |         /* decode block header */  | 
1758  | 0  |             {   U32 const blockHeader = LZ4F_readLE32(selectedIn); | 
1759  | 0  |                 size_t const nextCBlockSize = blockHeader & 0x7FFFFFFFU;  | 
1760  | 0  |                 size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize;  | 
1761  | 0  |                 if (blockHeader==0) {  /* frameEnd signal, no more block */ | 
1762  | 0  |                     DEBUGLOG(5, "end of frame");  | 
1763  | 0  |                     dctx->dStage = dstage_getSuffix;  | 
1764  | 0  |                     break;  | 
1765  | 0  |                 }  | 
1766  | 0  |                 if (nextCBlockSize > dctx->maxBlockSize) { | 
1767  | 0  |                     RETURN_ERROR(maxBlockSize_invalid);  | 
1768  | 0  |                 }  | 
1769  | 0  |                 if (blockHeader & LZ4F_BLOCKUNCOMPRESSED_FLAG) { | 
1770  |  |                     /* next block is uncompressed */  | 
1771  | 0  |                     dctx->tmpInTarget = nextCBlockSize;  | 
1772  | 0  |                     DEBUGLOG(5, "next block is uncompressed (size %u)", (U32)nextCBlockSize);  | 
1773  | 0  |                     if (dctx->frameInfo.blockChecksumFlag) { | 
1774  | 0  |                         (void)XXH32_reset(&dctx->blockChecksum, 0);  | 
1775  | 0  |                     }  | 
1776  | 0  |                     dctx->dStage = dstage_copyDirect;  | 
1777  | 0  |                     break;  | 
1778  | 0  |                 }  | 
1779  |  |                 /* next block is a compressed block */  | 
1780  | 0  |                 dctx->tmpInTarget = nextCBlockSize + crcSize;  | 
1781  | 0  |                 dctx->dStage = dstage_getCBlock;  | 
1782  | 0  |                 if (dstPtr==dstEnd || srcPtr==srcEnd) { | 
1783  | 0  |                     nextSrcSizeHint = BHSize + nextCBlockSize + crcSize;  | 
1784  | 0  |                     doAnotherStage = 0;  | 
1785  | 0  |                 }  | 
1786  | 0  |                 break;  | 
1787  | 0  |             }  | 
1788  |  |  | 
1789  | 0  |         case dstage_copyDirect:   /* uncompressed block */  | 
1790  | 0  |             DEBUGLOG(6, "dstage_copyDirect");  | 
1791  | 0  |             {   size_t sizeToCopy; | 
1792  | 0  |                 if (dstPtr == NULL) { | 
1793  | 0  |                     sizeToCopy = 0;  | 
1794  | 0  |                 } else { | 
1795  | 0  |                     size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr));  | 
1796  | 0  |                     sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);  | 
1797  | 0  |                     memcpy(dstPtr, srcPtr, sizeToCopy);  | 
1798  | 0  |                     if (!dctx->skipChecksum) { | 
1799  | 0  |                         if (dctx->frameInfo.blockChecksumFlag) { | 
1800  | 0  |                             (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);  | 
1801  | 0  |                         }  | 
1802  | 0  |                         if (dctx->frameInfo.contentChecksumFlag)  | 
1803  | 0  |                             (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);  | 
1804  | 0  |                     }  | 
1805  | 0  |                     if (dctx->frameInfo.contentSize)  | 
1806  | 0  |                         dctx->frameRemainingSize -= sizeToCopy;  | 
1807  |  |  | 
1808  |  |                     /* history management (linked blocks only)*/  | 
1809  | 0  |                     if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { | 
1810  | 0  |                         LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0);  | 
1811  | 0  |                     }  | 
1812  | 0  |                     srcPtr += sizeToCopy;  | 
1813  | 0  |                     dstPtr += sizeToCopy;  | 
1814  | 0  |                 }  | 
1815  | 0  |                 if (sizeToCopy == dctx->tmpInTarget) {   /* all done */ | 
1816  | 0  |                     if (dctx->frameInfo.blockChecksumFlag) { | 
1817  | 0  |                         dctx->tmpInSize = 0;  | 
1818  | 0  |                         dctx->dStage = dstage_getBlockChecksum;  | 
1819  | 0  |                     } else  | 
1820  | 0  |                         dctx->dStage = dstage_getBlockHeader;  /* new block */  | 
1821  | 0  |                     break;  | 
1822  | 0  |                 }  | 
1823  | 0  |                 dctx->tmpInTarget -= sizeToCopy;  /* need to copy more */  | 
1824  | 0  |             }  | 
1825  | 0  |             nextSrcSizeHint = dctx->tmpInTarget +  | 
1826  | 0  |                             +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0)  | 
1827  | 0  |                             + BHSize /* next header size */;  | 
1828  | 0  |             doAnotherStage = 0;  | 
1829  | 0  |             break;  | 
1830  |  |  | 
1831  |  |         /* check block checksum for recently transferred uncompressed block */  | 
1832  | 0  |         case dstage_getBlockChecksum:  | 
1833  | 0  |             DEBUGLOG(6, "dstage_getBlockChecksum");  | 
1834  | 0  |             {   const void* crcSrc; | 
1835  | 0  |                 if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) { | 
1836  | 0  |                     crcSrc = srcPtr;  | 
1837  | 0  |                     srcPtr += 4;  | 
1838  | 0  |                 } else { | 
1839  | 0  |                     size_t const stillToCopy = 4 - dctx->tmpInSize;  | 
1840  | 0  |                     size_t const sizeToCopy = MIN(stillToCopy, (size_t)(srcEnd-srcPtr));  | 
1841  | 0  |                     memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
1842  | 0  |                     dctx->tmpInSize += sizeToCopy;  | 
1843  | 0  |                     srcPtr += sizeToCopy;  | 
1844  | 0  |                     if (dctx->tmpInSize < 4) {  /* all input consumed */ | 
1845  | 0  |                         doAnotherStage = 0;  | 
1846  | 0  |                         break;  | 
1847  | 0  |                     }  | 
1848  | 0  |                     crcSrc = dctx->header;  | 
1849  | 0  |                 }  | 
1850  | 0  |                 if (!dctx->skipChecksum) { | 
1851  | 0  |                     U32 const readCRC = LZ4F_readLE32(crcSrc);  | 
1852  | 0  |                     U32 const calcCRC = XXH32_digest(&dctx->blockChecksum);  | 
1853  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
1854  |  |                     DEBUGLOG(6, "compare block checksum");  | 
1855  |  |                     if (readCRC != calcCRC) { | 
1856  |  |                         DEBUGLOG(4, "incorrect block checksum: %08X != %08X",  | 
1857  |  |                                 readCRC, calcCRC);  | 
1858  |  |                         RETURN_ERROR(blockChecksum_invalid);  | 
1859  |  |                     }  | 
1860  |  | #else  | 
1861  | 0  |                     (void)readCRC;  | 
1862  | 0  |                     (void)calcCRC;  | 
1863  | 0  | #endif  | 
1864  | 0  |             }   }  | 
1865  | 0  |             dctx->dStage = dstage_getBlockHeader;  /* new block */  | 
1866  | 0  |             break;  | 
1867  |  |  | 
1868  | 0  |         case dstage_getCBlock:  | 
1869  | 0  |             DEBUGLOG(6, "dstage_getCBlock");  | 
1870  | 0  |             if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) { | 
1871  | 0  |                 dctx->tmpInSize = 0;  | 
1872  | 0  |                 dctx->dStage = dstage_storeCBlock;  | 
1873  | 0  |                 break;  | 
1874  | 0  |             }  | 
1875  |  |             /* input large enough to read full block directly */  | 
1876  | 0  |             selectedIn = srcPtr;  | 
1877  | 0  |             srcPtr += dctx->tmpInTarget;  | 
1878  |  | 
  | 
1879  | 0  |             if (0)  /* always jump over next block */  | 
1880  | 0  |         case dstage_storeCBlock:  | 
1881  | 0  |             {   size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize; | 
1882  | 0  |                 size_t const inputLeft = (size_t)(srcEnd-srcPtr);  | 
1883  | 0  |                 size_t const sizeToCopy = MIN(wantedData, inputLeft);  | 
1884  | 0  |                 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
1885  | 0  |                 dctx->tmpInSize += sizeToCopy;  | 
1886  | 0  |                 srcPtr += sizeToCopy;  | 
1887  | 0  |                 if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */ | 
1888  | 0  |                     nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize)  | 
1889  | 0  |                                     + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0)  | 
1890  | 0  |                                     + BHSize /* next header size */;  | 
1891  | 0  |                     doAnotherStage = 0;  | 
1892  | 0  |                     break;  | 
1893  | 0  |                 }  | 
1894  | 0  |                 selectedIn = dctx->tmpIn;  | 
1895  | 0  |             }  | 
1896  |  |  | 
1897  |  |             /* At this stage, input is large enough to decode a block */  | 
1898  |  |  | 
1899  |  |             /* First, decode and control block checksum if it exists */  | 
1900  | 0  |             if (dctx->frameInfo.blockChecksumFlag) { | 
1901  | 0  |                 assert(dctx->tmpInTarget >= 4);  | 
1902  | 0  |                 dctx->tmpInTarget -= 4;  | 
1903  | 0  |                 assert(selectedIn != NULL);  /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */  | 
1904  | 0  |                 {   U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget); | 
1905  | 0  |                     U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);  | 
1906  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
1907  |  |                     RETURN_ERROR_IF(readBlockCrc != calcBlockCrc, blockChecksum_invalid);  | 
1908  |  | #else  | 
1909  | 0  |                     (void)readBlockCrc;  | 
1910  | 0  |                     (void)calcBlockCrc;  | 
1911  | 0  | #endif  | 
1912  | 0  |             }   }  | 
1913  |  |  | 
1914  |  |             /* decode directly into destination buffer if there is enough room */  | 
1915  | 0  |             if ( ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize)  | 
1916  |  |                  /* unless the dictionary is stored in tmpOut:  | 
1917  |  |                   * in which case it's faster to decode within tmpOut  | 
1918  |  |                   * to benefit from prefix speedup */  | 
1919  | 0  |               && !(dctx->dict!= NULL && (const BYTE*)dctx->dict + dctx->dictSize == dctx->tmpOut) )  | 
1920  | 0  |             { | 
1921  | 0  |                 const char* dict = (const char*)dctx->dict;  | 
1922  | 0  |                 size_t dictSize = dctx->dictSize;  | 
1923  | 0  |                 int decodedSize;  | 
1924  | 0  |                 assert(dstPtr != NULL);  | 
1925  | 0  |                 if (dict && dictSize > 1 GB) { | 
1926  |  |                     /* overflow control : dctx->dictSize is an int, avoid truncation / sign issues */  | 
1927  | 0  |                     dict += dictSize - 64 KB;  | 
1928  | 0  |                     dictSize = 64 KB;  | 
1929  | 0  |                 }  | 
1930  | 0  |                 decodedSize = LZ4_decompress_safe_usingDict(  | 
1931  | 0  |                         (const char*)selectedIn, (char*)dstPtr,  | 
1932  | 0  |                         (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,  | 
1933  | 0  |                         dict, (int)dictSize);  | 
1934  | 0  |                 RETURN_ERROR_IF(decodedSize < 0, decompressionFailed);  | 
1935  | 0  |                 if ((dctx->frameInfo.contentChecksumFlag) && (!dctx->skipChecksum))  | 
1936  | 0  |                     XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize);  | 
1937  | 0  |                 if (dctx->frameInfo.contentSize)  | 
1938  | 0  |                     dctx->frameRemainingSize -= (size_t)decodedSize;  | 
1939  |  |  | 
1940  |  |                 /* dictionary management */  | 
1941  | 0  |                 if (dctx->frameInfo.blockMode==LZ4F_blockLinked) { | 
1942  | 0  |                     LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0);  | 
1943  | 0  |                 }  | 
1944  |  | 
  | 
1945  | 0  |                 dstPtr += decodedSize;  | 
1946  | 0  |                 dctx->dStage = dstage_getBlockHeader;  /* end of block, let's get another one */  | 
1947  | 0  |                 break;  | 
1948  | 0  |             }  | 
1949  |  |  | 
1950  |  |             /* not enough place into dst : decode into tmpOut */  | 
1951  |  |  | 
1952  |  |             /* manage dictionary */  | 
1953  | 0  |             if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { | 
1954  | 0  |                 if (dctx->dict == dctx->tmpOutBuffer) { | 
1955  |  |                     /* truncate dictionary to 64 KB if too big */  | 
1956  | 0  |                     if (dctx->dictSize > 128 KB) { | 
1957  | 0  |                         memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);  | 
1958  | 0  |                         dctx->dictSize = 64 KB;  | 
1959  | 0  |                     }  | 
1960  | 0  |                     dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;  | 
1961  | 0  |                 } else {  /* dict not within tmpOut */ | 
1962  | 0  |                     size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);  | 
1963  | 0  |                     dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;  | 
1964  | 0  |             }   }  | 
1965  |  |  | 
1966  |  |             /* Decode block into tmpOut */  | 
1967  | 0  |             {   const char* dict = (const char*)dctx->dict; | 
1968  | 0  |                 size_t dictSize = dctx->dictSize;  | 
1969  | 0  |                 int decodedSize;  | 
1970  | 0  |                 if (dict && dictSize > 1 GB) { | 
1971  |  |                     /* the dictSize param is an int, avoid truncation / sign issues */  | 
1972  | 0  |                     dict += dictSize - 64 KB;  | 
1973  | 0  |                     dictSize = 64 KB;  | 
1974  | 0  |                 }  | 
1975  | 0  |                 decodedSize = LZ4_decompress_safe_usingDict(  | 
1976  | 0  |                         (const char*)selectedIn, (char*)dctx->tmpOut,  | 
1977  | 0  |                         (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,  | 
1978  | 0  |                         dict, (int)dictSize);  | 
1979  | 0  |                 RETURN_ERROR_IF(decodedSize < 0, decompressionFailed);  | 
1980  | 0  |                 if (dctx->frameInfo.contentChecksumFlag && !dctx->skipChecksum)  | 
1981  | 0  |                     XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize);  | 
1982  | 0  |                 if (dctx->frameInfo.contentSize)  | 
1983  | 0  |                     dctx->frameRemainingSize -= (size_t)decodedSize;  | 
1984  | 0  |                 dctx->tmpOutSize = (size_t)decodedSize;  | 
1985  | 0  |                 dctx->tmpOutStart = 0;  | 
1986  | 0  |                 dctx->dStage = dstage_flushOut;  | 
1987  | 0  |             }  | 
1988  |  |             /* fall-through */  | 
1989  |  |  | 
1990  | 0  |         case dstage_flushOut:  /* flush decoded data from tmpOut to dstBuffer */  | 
1991  | 0  |             DEBUGLOG(6, "dstage_flushOut");  | 
1992  | 0  |             if (dstPtr != NULL) { | 
1993  | 0  |                 size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr));  | 
1994  | 0  |                 memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy);  | 
1995  |  |  | 
1996  |  |                 /* dictionary management */  | 
1997  | 0  |                 if (dctx->frameInfo.blockMode == LZ4F_blockLinked)  | 
1998  | 0  |                     LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1 /*withinTmp*/);  | 
1999  |  | 
  | 
2000  | 0  |                 dctx->tmpOutStart += sizeToCopy;  | 
2001  | 0  |                 dstPtr += sizeToCopy;  | 
2002  | 0  |             }  | 
2003  | 0  |             if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */ | 
2004  | 0  |                 dctx->dStage = dstage_getBlockHeader;  /* get next block */  | 
2005  | 0  |                 break;  | 
2006  | 0  |             }  | 
2007  |  |             /* could not flush everything : stop there, just request a block header */  | 
2008  | 0  |             doAnotherStage = 0;  | 
2009  | 0  |             nextSrcSizeHint = BHSize;  | 
2010  | 0  |             break;  | 
2011  |  |  | 
2012  | 0  |         case dstage_getSuffix:  | 
2013  | 0  |             RETURN_ERROR_IF(dctx->frameRemainingSize, frameSize_wrong);   /* incorrect frame size decoded */  | 
2014  | 0  |             if (!dctx->frameInfo.contentChecksumFlag) {  /* no checksum, frame is completed */ | 
2015  | 0  |                 nextSrcSizeHint = 0;  | 
2016  | 0  |                 LZ4F_resetDecompressionContext(dctx);  | 
2017  | 0  |                 doAnotherStage = 0;  | 
2018  | 0  |                 break;  | 
2019  | 0  |             }  | 
2020  | 0  |             if ((srcEnd - srcPtr) < 4) {  /* not enough size for entire CRC */ | 
2021  | 0  |                 dctx->tmpInSize = 0;  | 
2022  | 0  |                 dctx->dStage = dstage_storeSuffix;  | 
2023  | 0  |             } else { | 
2024  | 0  |                 selectedIn = srcPtr;  | 
2025  | 0  |                 srcPtr += 4;  | 
2026  | 0  |             }  | 
2027  |  | 
  | 
2028  | 0  |             if (dctx->dStage == dstage_storeSuffix)   /* can be skipped */  | 
2029  | 0  |         case dstage_storeSuffix:  | 
2030  | 0  |             {   size_t const remainingInput = (size_t)(srcEnd - srcPtr); | 
2031  | 0  |                 size_t const wantedData = 4 - dctx->tmpInSize;  | 
2032  | 0  |                 size_t const sizeToCopy = MIN(wantedData, remainingInput);  | 
2033  | 0  |                 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
2034  | 0  |                 srcPtr += sizeToCopy;  | 
2035  | 0  |                 dctx->tmpInSize += sizeToCopy;  | 
2036  | 0  |                 if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */ | 
2037  | 0  |                     nextSrcSizeHint = 4 - dctx->tmpInSize;  | 
2038  | 0  |                     doAnotherStage=0;  | 
2039  | 0  |                     break;  | 
2040  | 0  |                 }  | 
2041  | 0  |                 selectedIn = dctx->tmpIn;  | 
2042  | 0  |             }   /* if (dctx->dStage == dstage_storeSuffix) */  | 
2043  |  |  | 
2044  |  |         /* case dstage_checkSuffix: */   /* no direct entry, avoid initialization risks */  | 
2045  | 0  |             if (!dctx->skipChecksum) { | 
2046  | 0  |                 U32 const readCRC = LZ4F_readLE32(selectedIn);  | 
2047  | 0  |                 U32 const resultCRC = XXH32_digest(&(dctx->xxh));  | 
2048  | 0  |                 DEBUGLOG(4, "frame checksum: stored 0x%0X vs 0x%0X processed", readCRC, resultCRC);  | 
2049  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
2050  |  |                 RETURN_ERROR_IF(readCRC != resultCRC, contentChecksum_invalid);  | 
2051  |  | #else  | 
2052  | 0  |                 (void)readCRC;  | 
2053  | 0  |                 (void)resultCRC;  | 
2054  | 0  | #endif  | 
2055  | 0  |             }  | 
2056  | 0  |             nextSrcSizeHint = 0;  | 
2057  | 0  |             LZ4F_resetDecompressionContext(dctx);  | 
2058  | 0  |             doAnotherStage = 0;  | 
2059  | 0  |             break;  | 
2060  |  |  | 
2061  | 0  |         case dstage_getSFrameSize:  | 
2062  | 0  |             if ((srcEnd - srcPtr) >= 4) { | 
2063  | 0  |                 selectedIn = srcPtr;  | 
2064  | 0  |                 srcPtr += 4;  | 
2065  | 0  |             } else { | 
2066  |  |                 /* not enough input to read cBlockSize field */  | 
2067  | 0  |                 dctx->tmpInSize = 4;  | 
2068  | 0  |                 dctx->tmpInTarget = 8;  | 
2069  | 0  |                 dctx->dStage = dstage_storeSFrameSize;  | 
2070  | 0  |             }  | 
2071  |  | 
  | 
2072  | 0  |             if (dctx->dStage == dstage_storeSFrameSize)  | 
2073  | 0  |         case dstage_storeSFrameSize:  | 
2074  | 0  |             {   size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, | 
2075  | 0  |                                              (size_t)(srcEnd - srcPtr) );  | 
2076  | 0  |                 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);  | 
2077  | 0  |                 srcPtr += sizeToCopy;  | 
2078  | 0  |                 dctx->tmpInSize += sizeToCopy;  | 
2079  | 0  |                 if (dctx->tmpInSize < dctx->tmpInTarget) { | 
2080  |  |                     /* not enough input to get full sBlockSize; wait for more */  | 
2081  | 0  |                     nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize;  | 
2082  | 0  |                     doAnotherStage = 0;  | 
2083  | 0  |                     break;  | 
2084  | 0  |                 }  | 
2085  | 0  |                 selectedIn = dctx->header + 4;  | 
2086  | 0  |             }   /* if (dctx->dStage == dstage_storeSFrameSize) */  | 
2087  |  |  | 
2088  |  |         /* case dstage_decodeSFrameSize: */   /* no direct entry */  | 
2089  | 0  |             {   size_t const SFrameSize = LZ4F_readLE32(selectedIn); | 
2090  | 0  |                 dctx->frameInfo.contentSize = SFrameSize;  | 
2091  | 0  |                 dctx->tmpInTarget = SFrameSize;  | 
2092  | 0  |                 dctx->dStage = dstage_skipSkippable;  | 
2093  | 0  |                 break;  | 
2094  | 0  |             }  | 
2095  |  |  | 
2096  | 0  |         case dstage_skipSkippable:  | 
2097  | 0  |             {   size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr)); | 
2098  | 0  |                 srcPtr += skipSize;  | 
2099  | 0  |                 dctx->tmpInTarget -= skipSize;  | 
2100  | 0  |                 doAnotherStage = 0;  | 
2101  | 0  |                 nextSrcSizeHint = dctx->tmpInTarget;  | 
2102  | 0  |                 if (nextSrcSizeHint) break;  /* still more to skip */  | 
2103  |  |                 /* frame fully skipped : prepare context for a new frame */  | 
2104  | 0  |                 LZ4F_resetDecompressionContext(dctx);  | 
2105  | 0  |                 break;  | 
2106  | 0  |             }  | 
2107  | 0  |         }   /* switch (dctx->dStage) */  | 
2108  | 0  |     }   /* while (doAnotherStage) */  | 
2109  |  |  | 
2110  |  |     /* preserve history within tmpOut whenever necessary */  | 
2111  | 0  |     LZ4F_STATIC_ASSERT((unsigned)dstage_init == 2);  | 
2112  | 0  |     if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked)  /* next block will use up to 64KB from previous ones */  | 
2113  | 0  |       && (dctx->dict != dctx->tmpOutBuffer)             /* dictionary is not already within tmp */  | 
2114  | 0  |       && (dctx->dict != NULL)                           /* dictionary exists */  | 
2115  | 0  |       && (!decompressOptionsPtr->stableDst)             /* cannot rely on dst data to remain there for next call */  | 
2116  | 0  |       && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) )  /* valid stages : [init ... getSuffix[ */  | 
2117  | 0  |     { | 
2118  | 0  |         if (dctx->dStage == dstage_flushOut) { | 
2119  | 0  |             size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);  | 
2120  | 0  |             size_t copySize = 64 KB - dctx->tmpOutSize;  | 
2121  | 0  |             const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;  | 
2122  | 0  |             if (dctx->tmpOutSize > 64 KB) copySize = 0;  | 
2123  | 0  |             if (copySize > preserveSize) copySize = preserveSize;  | 
2124  | 0  |             assert(dctx->tmpOutBuffer != NULL);  | 
2125  |  | 
  | 
2126  | 0  |             memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);  | 
2127  |  | 
  | 
2128  | 0  |             dctx->dict = dctx->tmpOutBuffer;  | 
2129  | 0  |             dctx->dictSize = preserveSize + dctx->tmpOutStart;  | 
2130  | 0  |         } else { | 
2131  | 0  |             const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize;  | 
2132  | 0  |             size_t const newDictSize = MIN(dctx->dictSize, 64 KB);  | 
2133  |  | 
  | 
2134  | 0  |             memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);  | 
2135  |  | 
  | 
2136  | 0  |             dctx->dict = dctx->tmpOutBuffer;  | 
2137  | 0  |             dctx->dictSize = newDictSize;  | 
2138  | 0  |             dctx->tmpOut = dctx->tmpOutBuffer + newDictSize;  | 
2139  | 0  |         }  | 
2140  | 0  |     }  | 
2141  |  | 
  | 
2142  | 0  |     *srcSizePtr = (size_t)(srcPtr - srcStart);  | 
2143  | 0  |     *dstSizePtr = (size_t)(dstPtr - dstStart);  | 
2144  | 0  |     return nextSrcSizeHint;  | 
2145  | 0  | }  | 
2146  |  |  | 
2147  |  | /*! LZ4F_decompress_usingDict() :  | 
2148  |  |  *  Same as LZ4F_decompress(), using a predefined dictionary.  | 
2149  |  |  *  Dictionary is used "in place", without any preprocessing.  | 
2150  |  |  *  It must remain accessible throughout the entire frame decoding.  | 
2151  |  |  */  | 
2152  |  | size_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx,  | 
2153  |  |                        void* dstBuffer, size_t* dstSizePtr,  | 
2154  |  |                        const void* srcBuffer, size_t* srcSizePtr,  | 
2155  |  |                        const void* dict, size_t dictSize,  | 
2156  |  |                        const LZ4F_decompressOptions_t* decompressOptionsPtr)  | 
2157  | 0  | { | 
2158  | 0  |     if (dctx->dStage <= dstage_init) { | 
2159  | 0  |         dctx->dict = (const BYTE*)dict;  | 
2160  | 0  |         dctx->dictSize = dictSize;  | 
2161  | 0  |     }  | 
2162  | 0  |     return LZ4F_decompress(dctx, dstBuffer, dstSizePtr,  | 
2163  | 0  |                            srcBuffer, srcSizePtr,  | 
2164  | 0  |                            decompressOptionsPtr);  | 
2165  | 0  | }  |