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