extzstd 0.0.2.CONCEPT-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ # ZSTD - standard compression algorithm
2
+ # Copyright (C) 2014-2015, Yann Collet.
3
+ # BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
4
+
5
+ prefix=@PREFIX@
6
+ libdir=@LIBDIR@
7
+ includedir=@INCLUDEDIR@
8
+
9
+ Name: zstd
10
+ Description: lossless compression algorithm library
11
+ URL: https://github.com/Cyan4973/zstd
12
+ Version: @VERSION@
13
+ Libs: -L@LIBDIR@ -lzstd
14
+ Cflags: -I@INCLUDEDIR@
@@ -0,0 +1,1768 @@
1
+ /*
2
+ zstd - standard compression library
3
+ Copyright (C) 2014-2015, 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
+ * Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+ * Redistributions in binary form must reproduce the above
13
+ copyright notice, this list of conditions and the following disclaimer
14
+ in the documentation and/or other materials provided with the
15
+ distribution.
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
28
+ You can contact the author at :
29
+ - zstd source repository : https://github.com/Cyan4973/zstd
30
+ - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
31
+ */
32
+
33
+ /****************************************************************
34
+ * Tuning parameters
35
+ *****************************************************************/
36
+ /* MEMORY_USAGE :
37
+ * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
38
+ * Increasing memory usage improves compression ratio
39
+ * Reduced memory usage can improve speed, due to cache effect */
40
+ #define ZSTD_MEMORY_USAGE 17
41
+
42
+
43
+ /**************************************
44
+ CPU Feature Detection
45
+ **************************************/
46
+ /*
47
+ * Automated efficient unaligned memory access detection
48
+ * Based on known hardware architectures
49
+ * This list will be updated thanks to feedbacks
50
+ */
51
+ #if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \
52
+ || defined(__ARM_FEATURE_UNALIGNED) \
53
+ || defined(__i386__) || defined(__x86_64__) \
54
+ || defined(_M_IX86) || defined(_M_X64) \
55
+ || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \
56
+ || (defined(_M_ARM) && (_M_ARM >= 7))
57
+ # define ZSTD_UNALIGNED_ACCESS 1
58
+ #else
59
+ # define ZSTD_UNALIGNED_ACCESS 0
60
+ #endif
61
+
62
+
63
+ /********************************************************
64
+ * Includes
65
+ *********************************************************/
66
+ #include <stdlib.h> /* calloc */
67
+ #include <string.h> /* memcpy, memmove */
68
+ #include <stdio.h> /* debug : printf */
69
+ #include "zstd_static.h"
70
+ #if defined(__clang__) || defined(__GNUC__)
71
+ # include "fse.c" /* due to GCC/Clang inlining limitations, including *.c runs noticeably faster */
72
+ #else
73
+ # include "fse_static.h"
74
+ #endif
75
+
76
+
77
+ /********************************************************
78
+ * Compiler specifics
79
+ *********************************************************/
80
+ #ifdef __AVX2__
81
+ # include <immintrin.h> /* AVX2 intrinsics */
82
+ #endif
83
+
84
+ #ifdef _MSC_VER /* Visual Studio */
85
+ # define FORCE_INLINE static __forceinline
86
+ # include <intrin.h> /* For Visual 2005 */
87
+ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
88
+ # pragma warning(disable : 4324) /* disable: C4324: padded structure */
89
+ #else
90
+ # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
91
+ # ifdef __GNUC__
92
+ # define FORCE_INLINE static inline __attribute__((always_inline))
93
+ # else
94
+ # define FORCE_INLINE static inline
95
+ # endif
96
+ #endif
97
+
98
+
99
+ #ifndef MEM_ACCESS_MODULE
100
+ #define MEM_ACCESS_MODULE
101
+ /********************************************************
102
+ * Basic Types
103
+ *********************************************************/
104
+ #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
105
+ # include <stdint.h>
106
+ typedef uint8_t BYTE;
107
+ typedef uint16_t U16;
108
+ typedef int16_t S16;
109
+ typedef uint32_t U32;
110
+ typedef int32_t S32;
111
+ typedef uint64_t U64;
112
+ #else
113
+ typedef unsigned char BYTE;
114
+ typedef unsigned short U16;
115
+ typedef signed short S16;
116
+ typedef unsigned int U32;
117
+ typedef signed int S32;
118
+ typedef unsigned long long U64;
119
+ #endif
120
+
121
+ #endif /* MEM_ACCESS_MODULE */
122
+
123
+
124
+ /********************************************************
125
+ * Constants
126
+ *********************************************************/
127
+ static const U32 ZSTD_magicNumber = 0xFD2FB51E; /* 3rd version : seqNb header */
128
+
129
+ #define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
130
+ #define HASH_TABLESIZE (1 << HASH_LOG)
131
+ #define HASH_MASK (HASH_TABLESIZE - 1)
132
+
133
+ #define KNUTH 2654435761
134
+
135
+ #define BIT7 128
136
+ #define BIT6 64
137
+ #define BIT5 32
138
+ #define BIT4 16
139
+
140
+ #define KB *(1 <<10)
141
+ #define MB *(1 <<20)
142
+ #define GB *(1U<<30)
143
+
144
+ #define BLOCKSIZE (128 KB) /* define, for static allocation */
145
+ static const U32 g_maxDistance = 4 * BLOCKSIZE;
146
+ static const U32 g_maxLimit = 1 GB;
147
+ static const U32 g_searchStrength = 8;
148
+
149
+ #define WORKPLACESIZE (BLOCKSIZE*3)
150
+ #define MINMATCH 4
151
+ #define MLbits 7
152
+ #define LLbits 6
153
+ #define Offbits 5
154
+ #define MaxML ((1<<MLbits )-1)
155
+ #define MaxLL ((1<<LLbits )-1)
156
+ #define MaxOff ((1<<Offbits)-1)
157
+ #define LitFSELog 11
158
+ #define MLFSELog 10
159
+ #define LLFSELog 10
160
+ #define OffFSELog 9
161
+ #define MAX(a,b) ((a)<(b)?(b):(a))
162
+ #define MaxSeq MAX(MaxLL, MaxML)
163
+
164
+ #define LITERAL_NOENTROPY 63
165
+ #define COMMAND_NOENTROPY 7 /* to remove */
166
+
167
+ static const size_t ZSTD_blockHeaderSize = 3;
168
+ static const size_t ZSTD_frameHeaderSize = 4;
169
+
170
+
171
+ /********************************************************
172
+ * Memory operations
173
+ *********************************************************/
174
+ static unsigned ZSTD_32bits(void) { return sizeof(void*)==4; }
175
+ static unsigned ZSTD_64bits(void) { return sizeof(void*)==8; }
176
+
177
+ static unsigned ZSTD_isLittleEndian(void)
178
+ {
179
+ const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
180
+ return one.c[0];
181
+ }
182
+
183
+ static U16 ZSTD_read16(const void* p) { U16 r; memcpy(&r, p, sizeof(r)); return r; }
184
+
185
+ static U32 ZSTD_read32(const void* p) { U32 r; memcpy(&r, p, sizeof(r)); return r; }
186
+
187
+ static U64 ZSTD_read64(const void* p) { U64 r; memcpy(&r, p, sizeof(r)); return r; }
188
+
189
+ static size_t ZSTD_read_ARCH(const void* p) { size_t r; memcpy(&r, p, sizeof(r)); return r; }
190
+
191
+ static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
192
+
193
+ static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
194
+
195
+ #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
196
+
197
+ static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
198
+ {
199
+ const BYTE* ip = (const BYTE*)src;
200
+ BYTE* op = (BYTE*)dst;
201
+ BYTE* const oend = op + length;
202
+ while (op < oend) COPY8(op, ip);
203
+ }
204
+
205
+ static U16 ZSTD_readLE16(const void* memPtr)
206
+ {
207
+ if (ZSTD_isLittleEndian()) return ZSTD_read16(memPtr);
208
+ else
209
+ {
210
+ const BYTE* p = (const BYTE*)memPtr;
211
+ return (U16)((U16)p[0] + ((U16)p[1]<<8));
212
+ }
213
+ }
214
+
215
+ static void ZSTD_writeLE16(void* memPtr, U16 val)
216
+ {
217
+ if (ZSTD_isLittleEndian()) memcpy(memPtr, &val, sizeof(val));
218
+ else
219
+ {
220
+ BYTE* p = (BYTE*)memPtr;
221
+ p[0] = (BYTE)val;
222
+ p[1] = (BYTE)(val>>8);
223
+ }
224
+ }
225
+
226
+ static U32 ZSTD_readLE32(const void* memPtr)
227
+ {
228
+ if (ZSTD_isLittleEndian())
229
+ return ZSTD_read32(memPtr);
230
+ else
231
+ {
232
+ const BYTE* p = (const BYTE*)memPtr;
233
+ return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
234
+ }
235
+ }
236
+
237
+ static void ZSTD_writeLE32(void* memPtr, U32 val32)
238
+ {
239
+ if (ZSTD_isLittleEndian())
240
+ {
241
+ memcpy(memPtr, &val32, 4);
242
+ }
243
+ else
244
+ {
245
+ BYTE* p = (BYTE*)memPtr;
246
+ p[0] = (BYTE)val32;
247
+ p[1] = (BYTE)(val32>>8);
248
+ p[2] = (BYTE)(val32>>16);
249
+ p[3] = (BYTE)(val32>>24);
250
+ }
251
+ }
252
+
253
+ static U32 ZSTD_readBE32(const void* memPtr)
254
+ {
255
+ const BYTE* p = (const BYTE*)memPtr;
256
+ return (U32)(((U32)p[0]<<24) + ((U32)p[1]<<16) + ((U32)p[2]<<8) + ((U32)p[3]<<0));
257
+ }
258
+
259
+ static void ZSTD_writeBE32(void* memPtr, U32 value)
260
+ {
261
+ BYTE* const p = (BYTE* const) memPtr;
262
+ p[0] = (BYTE)(value>>24);
263
+ p[1] = (BYTE)(value>>16);
264
+ p[2] = (BYTE)(value>>8);
265
+ p[3] = (BYTE)(value>>0);
266
+ }
267
+
268
+
269
+ /**************************************
270
+ * Local structures
271
+ ***************************************/
272
+ typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
273
+
274
+ typedef struct
275
+ {
276
+ blockType_t blockType;
277
+ U32 origSize;
278
+ } blockProperties_t;
279
+
280
+ typedef struct {
281
+ void* buffer;
282
+ U32* offsetStart;
283
+ U32* offset;
284
+ BYTE* offCodeStart;
285
+ BYTE* offCode;
286
+ BYTE* litStart;
287
+ BYTE* lit;
288
+ BYTE* litLengthStart;
289
+ BYTE* litLength;
290
+ BYTE* matchLengthStart;
291
+ BYTE* matchLength;
292
+ BYTE* dumpsStart;
293
+ BYTE* dumps;
294
+ } seqStore_t;
295
+
296
+ void ZSTD_resetSeqStore(seqStore_t* ssPtr)
297
+ {
298
+ ssPtr->offset = ssPtr->offsetStart;
299
+ ssPtr->lit = ssPtr->litStart;
300
+ ssPtr->litLength = ssPtr->litLengthStart;
301
+ ssPtr->matchLength = ssPtr->matchLengthStart;
302
+ ssPtr->dumps = ssPtr->dumpsStart;
303
+ }
304
+
305
+
306
+ typedef struct ZSTD_Cctx_s
307
+ {
308
+ const BYTE* base;
309
+ U32 current;
310
+ U32 nextUpdate;
311
+ seqStore_t seqStore;
312
+ #ifdef __AVX2__
313
+ __m256i hashTable[HASH_TABLESIZE>>3];
314
+ #else
315
+ U32 hashTable[HASH_TABLESIZE];
316
+ #endif
317
+ } cctxi_t;
318
+
319
+
320
+ ZSTD_Cctx* ZSTD_createCCtx(void)
321
+ {
322
+ ZSTD_Cctx* ctx = (ZSTD_Cctx*) malloc( sizeof(ZSTD_Cctx) );
323
+ if (ctx==NULL) return NULL;
324
+ ctx->seqStore.buffer = malloc(WORKPLACESIZE);
325
+ if (ctx->seqStore.buffer==NULL)
326
+ {
327
+ free(ctx);
328
+ return NULL;
329
+ }
330
+ ctx->seqStore.offsetStart = (U32*) (ctx->seqStore.buffer);
331
+ ctx->seqStore.offCodeStart = (BYTE*) (ctx->seqStore.offsetStart + (BLOCKSIZE>>2));
332
+ ctx->seqStore.litStart = ctx->seqStore.offCodeStart + (BLOCKSIZE>>2);
333
+ ctx->seqStore.litLengthStart = ctx->seqStore.litStart + BLOCKSIZE;
334
+ ctx->seqStore.matchLengthStart = ctx->seqStore.litLengthStart + (BLOCKSIZE>>2);
335
+ ctx->seqStore.dumpsStart = ctx->seqStore.matchLengthStart + (BLOCKSIZE>>2);
336
+ return ctx;
337
+ }
338
+
339
+ void ZSTD_resetCCtx(ZSTD_Cctx* ctx)
340
+ {
341
+ ctx->base = NULL;
342
+ memset(ctx->hashTable, 0, HASH_TABLESIZE*4);
343
+ }
344
+
345
+ size_t ZSTD_freeCCtx(ZSTD_Cctx* ctx)
346
+ {
347
+ free(ctx->seqStore.buffer);
348
+ free(ctx);
349
+ return 0;
350
+ }
351
+
352
+
353
+ /**************************************
354
+ * Error Management
355
+ **************************************/
356
+ /* tells if a return value is an error code */
357
+ unsigned ZSTD_isError(size_t code)
358
+ {
359
+ return (code > (size_t)(-ZSTD_ERROR_maxCode));
360
+ }
361
+
362
+ #define ZSTD_GENERATE_STRING(STRING) #STRING,
363
+ static const char* ZSTD_errorStrings[] = { ZSTD_LIST_ERRORS(ZSTD_GENERATE_STRING) };
364
+
365
+ /* provides error code string (useful for debugging) */
366
+ const char* ZSTD_getErrorName(size_t code)
367
+ {
368
+ static const char* codeError = "Unspecified error code";
369
+ if (ZSTD_isError(code)) return ZSTD_errorStrings[-(int)(code)];
370
+ return codeError;
371
+ }
372
+
373
+
374
+ /**************************************
375
+ * Tool functions
376
+ **************************************/
377
+ unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; }
378
+
379
+ static unsigned ZSTD_highbit(U32 val)
380
+ {
381
+ # if defined(_MSC_VER) /* Visual */
382
+ unsigned long r;
383
+ _BitScanReverse(&r, val);
384
+ return (unsigned)r;
385
+ # elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */
386
+ return 31 - __builtin_clz(val);
387
+ # else /* Software version */
388
+ static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
389
+ U32 v = val;
390
+ int r;
391
+ v |= v >> 1;
392
+ v |= v >> 2;
393
+ v |= v >> 4;
394
+ v |= v >> 8;
395
+ v |= v >> 16;
396
+ r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
397
+ return r;
398
+ # endif
399
+ }
400
+
401
+ static unsigned ZSTD_NbCommonBytes (register size_t val)
402
+ {
403
+ if (ZSTD_isLittleEndian())
404
+ {
405
+ if (ZSTD_64bits())
406
+ {
407
+ # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
408
+ unsigned long r = 0;
409
+ _BitScanForward64( &r, (U64)val );
410
+ return (int)(r>>3);
411
+ # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
412
+ return (__builtin_ctzll((U64)val) >> 3);
413
+ # else
414
+ static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
415
+ return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
416
+ # endif
417
+ }
418
+ else /* 32 bits */
419
+ {
420
+ # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
421
+ unsigned long r;
422
+ _BitScanForward( &r, (U32)val );
423
+ return (int)(r>>3);
424
+ # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
425
+ return (__builtin_ctz((U32)val) >> 3);
426
+ # else
427
+ static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
428
+ return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
429
+ # endif
430
+ }
431
+ }
432
+ else /* Big Endian CPU */
433
+ {
434
+ if (ZSTD_64bits())
435
+ {
436
+ # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
437
+ unsigned long r = 0;
438
+ _BitScanReverse64( &r, val );
439
+ return (unsigned)(r>>3);
440
+ # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
441
+ return (__builtin_clzll(val) >> 3);
442
+ # else
443
+ unsigned r;
444
+ const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */
445
+ if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; }
446
+ if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
447
+ r += (!val);
448
+ return r;
449
+ # endif
450
+ }
451
+ else /* 32 bits */
452
+ {
453
+ # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
454
+ unsigned long r = 0;
455
+ _BitScanReverse( &r, (unsigned long)val );
456
+ return (unsigned)(r>>3);
457
+ # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
458
+ return (__builtin_clz((U32)val) >> 3);
459
+ # else
460
+ unsigned r;
461
+ if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
462
+ r += (!val);
463
+ return r;
464
+ # endif
465
+ }
466
+ }
467
+ }
468
+
469
+ static unsigned ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
470
+ {
471
+ const BYTE* const pStart = pIn;
472
+
473
+ while ((pIn<pInLimit-(sizeof(size_t)-1)))
474
+ {
475
+ size_t diff = ZSTD_read_ARCH(pMatch) ^ ZSTD_read_ARCH(pIn);
476
+ if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
477
+ pIn += ZSTD_NbCommonBytes(diff);
478
+ return (unsigned)(pIn - pStart);
479
+ }
480
+
481
+ if (ZSTD_64bits()) if ((pIn<(pInLimit-3)) && (ZSTD_read32(pMatch) == ZSTD_read32(pIn))) { pIn+=4; pMatch+=4; }
482
+ if ((pIn<(pInLimit-1)) && (ZSTD_read16(pMatch) == ZSTD_read16(pIn))) { pIn+=2; pMatch+=2; }
483
+ if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
484
+ return (unsigned)(pIn - pStart);
485
+ }
486
+
487
+
488
+ /********************************************************
489
+ * Compression
490
+ *********************************************************/
491
+ size_t ZSTD_compressBound(size_t srcSize) /* maximum compressed size */
492
+ {
493
+ return FSE_compressBound(srcSize) + 12;
494
+ }
495
+
496
+
497
+ static size_t ZSTD_compressRle (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
498
+ {
499
+ BYTE* const ostart = (BYTE* const)dst;
500
+
501
+ /* at this stage : dstSize >= FSE_compressBound(srcSize) > (ZSTD_blockHeaderSize+1) (checked by ZSTD_compressLiterals()) */
502
+ (void)maxDstSize;
503
+
504
+ ostart[ZSTD_blockHeaderSize] = *(const BYTE*)src;
505
+
506
+ /* Build header */
507
+ ostart[0] = (BYTE)(srcSize>>16);
508
+ ostart[1] = (BYTE)(srcSize>>8);
509
+ ostart[2] = (BYTE) srcSize;
510
+ ostart[0] += (BYTE)(bt_rle<<6);
511
+
512
+ return ZSTD_blockHeaderSize+1;
513
+ }
514
+
515
+
516
+ static size_t ZSTD_noCompressBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
517
+ {
518
+ BYTE* const ostart = (BYTE* const)dst;
519
+
520
+ if (srcSize + ZSTD_blockHeaderSize > maxDstSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
521
+ memcpy(ostart + ZSTD_blockHeaderSize, src, srcSize);
522
+
523
+ /* Build header */
524
+ ostart[0] = (BYTE)(srcSize>>16);
525
+ ostart[1] = (BYTE)(srcSize>>8);
526
+ ostart[2] = (BYTE) srcSize;
527
+ ostart[0] += (BYTE)(bt_raw<<6); /* is a raw (uncompressed) block */
528
+
529
+ return ZSTD_blockHeaderSize+srcSize;
530
+ }
531
+
532
+
533
+ size_t ZSTD_minGain(size_t srcSize)
534
+ {
535
+ return (srcSize >> 6) + 1;
536
+ }
537
+
538
+
539
+ static size_t ZSTD_compressLiterals (void* dst, size_t dstSize,
540
+ const void* src, size_t srcSize)
541
+ {
542
+ const size_t minGain = ZSTD_minGain(srcSize);
543
+ BYTE* const ostart = (BYTE*)dst;
544
+ size_t hsize;
545
+ static const size_t LHSIZE = 5;
546
+
547
+ if (dstSize < LHSIZE+1) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall; /* not enough space for compression */
548
+
549
+ hsize = HUF_compress(ostart+LHSIZE, dstSize-LHSIZE, src, srcSize);
550
+ if (hsize<2) return hsize; /* special cases */
551
+ if (hsize >= srcSize - minGain) return 0;
552
+
553
+ hsize += 2; /* work around vs fixed 3-bytes header */
554
+
555
+ /* Build header */
556
+ {
557
+ ostart[0] = (BYTE)(bt_compressed<<6); /* is a block, is compressed */
558
+ ostart[0] += (BYTE)(hsize>>16);
559
+ ostart[1] = (BYTE)(hsize>>8);
560
+ ostart[2] = (BYTE)(hsize>>0);
561
+ ostart[0] += (BYTE)((srcSize>>16)<<3);
562
+ ostart[3] = (BYTE)(srcSize>>8);
563
+ ostart[4] = (BYTE)(srcSize>>0);
564
+ }
565
+
566
+ hsize -= 2;
567
+ return hsize+LHSIZE;
568
+ }
569
+
570
+
571
+ static size_t ZSTD_compressSequences(BYTE* dst, size_t maxDstSize,
572
+ const seqStore_t* seqStorePtr,
573
+ size_t srcSize)
574
+ {
575
+ U32 count[MaxSeq+1];
576
+ S16 norm[MaxSeq+1];
577
+ size_t mostFrequent;
578
+ U32 max = 255;
579
+ U32 tableLog = 11;
580
+ U32 CTable_LitLength [FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL )];
581
+ U32 CTable_OffsetBits [FSE_CTABLE_SIZE_U32(OffFSELog,MaxOff)];
582
+ U32 CTable_MatchLength[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML )];
583
+ U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */
584
+ const BYTE* const op_lit_start = seqStorePtr->litStart;
585
+ const BYTE* op_lit = seqStorePtr->lit;
586
+ const BYTE* const llTable = seqStorePtr->litLengthStart;
587
+ const BYTE* op_litLength = seqStorePtr->litLength;
588
+ const BYTE* const mlTable = seqStorePtr->matchLengthStart;
589
+ const U32* const offsetTable = seqStorePtr->offsetStart;
590
+ BYTE* const offCodeTable = seqStorePtr->offCodeStart;
591
+ BYTE* op = dst;
592
+ BYTE* const oend = dst + maxDstSize;
593
+ const size_t nbSeq = op_litLength - llTable;
594
+ const size_t minGain = ZSTD_minGain(srcSize);
595
+ const size_t maxCSize = srcSize - minGain;
596
+ const size_t minSeqSize = 1 /*lastL*/ + 2 /*dHead*/ + 2 /*dumpsIn*/ + 5 /*SeqHead*/ + 3 /*SeqIn*/ + 1 /*margin*/ + ZSTD_blockHeaderSize;
597
+ const size_t maxLSize = maxCSize > minSeqSize ? maxCSize - minSeqSize : 0;
598
+ BYTE* seqHead;
599
+
600
+
601
+ /* Compress literals */
602
+ {
603
+ size_t cSize;
604
+ size_t litSize = op_lit - op_lit_start;
605
+
606
+ if (litSize <= LITERAL_NOENTROPY) cSize = ZSTD_noCompressBlock (op, maxDstSize, op_lit_start, litSize);
607
+ else
608
+ {
609
+ cSize = ZSTD_compressLiterals(op, maxDstSize, op_lit_start, litSize);
610
+ if (cSize == 1) cSize = ZSTD_compressRle (op, maxDstSize, op_lit_start, litSize);
611
+ else if (cSize == 0)
612
+ {
613
+ if (litSize >= maxLSize) return 0; /* block not compressible enough */
614
+ cSize = ZSTD_noCompressBlock (op, maxDstSize, op_lit_start, litSize);
615
+ }
616
+ }
617
+ if (ZSTD_isError(cSize)) return cSize;
618
+ op += cSize;
619
+ }
620
+
621
+ /* Sequences Header */
622
+ if ((oend-op) < 2+3+6) /* nbSeq + dumpsLength + 3*rleCTable*/
623
+ return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
624
+ ZSTD_writeLE16(op, (U16)nbSeq); op+=2;
625
+ seqHead = op;
626
+
627
+ /* dumps : contains too large lengths */
628
+ {
629
+ size_t dumpsLength = seqStorePtr->dumps - seqStorePtr->dumpsStart;
630
+ if (dumpsLength < 512)
631
+ {
632
+ op[0] = (BYTE)(dumpsLength >> 8);
633
+ op[1] = (BYTE)(dumpsLength);
634
+ op += 2;
635
+ }
636
+ else
637
+ {
638
+ op[0] = 2;
639
+ op[1] = (BYTE)(dumpsLength>>8);
640
+ op[2] = (BYTE)(dumpsLength);
641
+ op += 3;
642
+ }
643
+ if ((size_t)(oend-op) < dumpsLength+6) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
644
+ memcpy(op, seqStorePtr->dumpsStart, dumpsLength);
645
+ op += dumpsLength;
646
+ }
647
+
648
+ /* CTable for Literal Lengths */
649
+ max = MaxLL;
650
+ mostFrequent = FSE_countFast(count, &max, seqStorePtr->litLengthStart, nbSeq);
651
+ if ((mostFrequent == nbSeq) && (nbSeq > 2))
652
+ {
653
+ *op++ = *(seqStorePtr->litLengthStart);
654
+ FSE_buildCTable_rle(CTable_LitLength, (BYTE)max);
655
+ LLtype = bt_rle;
656
+ }
657
+ else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (LLbits-1))))
658
+ {
659
+ FSE_buildCTable_raw(CTable_LitLength, LLbits);
660
+ LLtype = bt_raw;
661
+ }
662
+ else
663
+ {
664
+ size_t NCountSize;
665
+ tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max);
666
+ FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
667
+ NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
668
+ if (FSE_isError(NCountSize)) return (size_t)-ZSTD_ERROR_GENERIC;
669
+ op += NCountSize;
670
+ FSE_buildCTable(CTable_LitLength, norm, max, tableLog);
671
+ LLtype = bt_compressed;
672
+ }
673
+
674
+ /* CTable for Offsets codes */
675
+ {
676
+ /* create Offset codes */
677
+ size_t i;
678
+ max = MaxOff;
679
+ for (i=0; i<nbSeq; i++)
680
+ {
681
+ offCodeTable[i] = (BYTE)ZSTD_highbit(offsetTable[i]) + 1;
682
+ if (offsetTable[i]==0) offCodeTable[i]=0;
683
+ }
684
+ mostFrequent = FSE_countFast(count, &max, offCodeTable, nbSeq);
685
+ }
686
+ if ((mostFrequent == nbSeq) && (nbSeq > 2))
687
+ {
688
+ *op++ = *offCodeTable;
689
+ FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
690
+ Offtype = bt_rle;
691
+ }
692
+ else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (Offbits-1))))
693
+ {
694
+ FSE_buildCTable_raw(CTable_OffsetBits, Offbits);
695
+ Offtype = bt_raw;
696
+ }
697
+ else
698
+ {
699
+ size_t NCountSize;
700
+ tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max);
701
+ FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
702
+ NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
703
+ if (FSE_isError(NCountSize)) return (size_t)-ZSTD_ERROR_GENERIC;
704
+ op += NCountSize;
705
+ FSE_buildCTable(CTable_OffsetBits, norm, max, tableLog);
706
+ Offtype = bt_compressed;
707
+ }
708
+
709
+ /* CTable for MatchLengths */
710
+ max = MaxML;
711
+ mostFrequent = FSE_countFast(count, &max, seqStorePtr->matchLengthStart, nbSeq);
712
+ if ((mostFrequent == nbSeq) && (nbSeq > 2))
713
+ {
714
+ *op++ = *seqStorePtr->matchLengthStart;
715
+ FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
716
+ MLtype = bt_rle;
717
+ }
718
+ else if ((nbSeq < 64) || (mostFrequent < (nbSeq >> (MLbits-1))))
719
+ {
720
+ FSE_buildCTable_raw(CTable_MatchLength, MLbits);
721
+ MLtype = bt_raw;
722
+ }
723
+ else
724
+ {
725
+ size_t NCountSize;
726
+ tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max);
727
+ FSE_normalizeCount(norm, tableLog, count, nbSeq, max);
728
+ NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
729
+ if (FSE_isError(NCountSize)) return (size_t)-ZSTD_ERROR_GENERIC;
730
+ op += NCountSize;
731
+ FSE_buildCTable(CTable_MatchLength, norm, max, tableLog);
732
+ MLtype = bt_compressed;
733
+ }
734
+
735
+ seqHead[0] += (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
736
+
737
+ /* Encoding Sequences */
738
+ {
739
+ size_t streamSize, errorCode;
740
+ FSE_CStream_t blockStream;
741
+ FSE_CState_t stateMatchLength;
742
+ FSE_CState_t stateOffsetBits;
743
+ FSE_CState_t stateLitLength;
744
+ int i;
745
+
746
+ errorCode = FSE_initCStream(&blockStream, op, oend-op);
747
+ if (FSE_isError(errorCode)) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall; /* not enough space remaining */
748
+ FSE_initCState(&stateMatchLength, CTable_MatchLength);
749
+ FSE_initCState(&stateOffsetBits, CTable_OffsetBits);
750
+ FSE_initCState(&stateLitLength, CTable_LitLength);
751
+
752
+ for (i=(int)nbSeq-1; i>=0; i--)
753
+ {
754
+ BYTE matchLength = mlTable[i];
755
+ U32 offset = offsetTable[i];
756
+ BYTE offCode = offCodeTable[i]; /* 32b*/ /* 64b*/
757
+ U32 nbBits = (offCode-1) * (!!offCode);
758
+ BYTE litLength = llTable[i]; /* (7)*/ /* (7)*/
759
+ FSE_encodeSymbol(&blockStream, &stateMatchLength, matchLength); /* 17 */ /* 17 */
760
+ if (ZSTD_32bits()) FSE_flushBits(&blockStream); /* 7 */
761
+ FSE_addBits(&blockStream, offset, nbBits); /* 32 */ /* 42 */
762
+ if (ZSTD_32bits()) FSE_flushBits(&blockStream); /* 7 */
763
+ FSE_encodeSymbol(&blockStream, &stateOffsetBits, offCode); /* 16 */ /* 51 */
764
+ FSE_encodeSymbol(&blockStream, &stateLitLength, litLength); /* 26 */ /* 61 */
765
+ FSE_flushBits(&blockStream); /* 7 */ /* 7 */
766
+ }
767
+
768
+ FSE_flushCState(&blockStream, &stateMatchLength);
769
+ FSE_flushCState(&blockStream, &stateOffsetBits);
770
+ FSE_flushCState(&blockStream, &stateLitLength);
771
+
772
+ streamSize = FSE_closeCStream(&blockStream);
773
+ if (streamSize==0) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall; /* not enough space */
774
+ op += streamSize;
775
+ }
776
+
777
+ /* check compressibility */
778
+ if ((size_t)(op-dst) >= maxCSize) return 0;
779
+
780
+ return op - dst;
781
+ }
782
+
783
+
784
+ static void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength)
785
+ {
786
+ BYTE* op_lit = seqStorePtr->lit;
787
+ BYTE* const l_end = op_lit + litLength;
788
+
789
+ /* copy Literals */
790
+ while (op_lit<l_end) COPY8(op_lit, literals);
791
+ seqStorePtr->lit += litLength;
792
+
793
+ /* literal Length */
794
+ if (litLength >= MaxLL)
795
+ {
796
+ *(seqStorePtr->litLength++) = MaxLL;
797
+ if (litLength<255 + MaxLL)
798
+ *(seqStorePtr->dumps++) = (BYTE)(litLength - MaxLL);
799
+ else
800
+ {
801
+ *(seqStorePtr->dumps++) = 255;
802
+ ZSTD_writeLE32(seqStorePtr->dumps, (U32)litLength); seqStorePtr->dumps += 3;
803
+ }
804
+ }
805
+ else *(seqStorePtr->litLength++) = (BYTE)litLength;
806
+
807
+ /* match offset */
808
+ *(seqStorePtr->offset++) = (U32)offset;
809
+
810
+ /* match Length */
811
+ if (matchLength >= MaxML)
812
+ {
813
+ *(seqStorePtr->matchLength++) = MaxML;
814
+ if (matchLength < 255+MaxML)
815
+ *(seqStorePtr->dumps++) = (BYTE)(matchLength - MaxML);
816
+ else
817
+ {
818
+ *(seqStorePtr->dumps++) = 255;
819
+ ZSTD_writeLE32(seqStorePtr->dumps, (U32)matchLength); seqStorePtr->dumps+=3;
820
+ }
821
+ }
822
+ else *(seqStorePtr->matchLength++) = (BYTE)matchLength;
823
+ }
824
+
825
+
826
+ //static const U32 hashMask = (1<<HASH_LOG)-1;
827
+ //static const U64 prime5bytes = 889523592379ULL;
828
+ //static const U64 prime6bytes = 227718039650203ULL;
829
+ static const U64 prime7bytes = 58295818150454627ULL;
830
+ //static const U64 prime8bytes = 14923729446516375013ULL;
831
+
832
+ //static U32 ZSTD_hashPtr(const void* p) { return (U32) _bextr_u64(*(U64*)p * prime7bytes, (56-HASH_LOG), HASH_LOG); }
833
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime7bytes) << 8 >> (64-HASH_LOG)); }
834
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime7bytes) >> (56-HASH_LOG)) & ((1<<HASH_LOG)-1); }
835
+ //static U32 ZSTD_hashPtr(const void* p) { return ( ((*(U64*)p & 0xFFFFFFFFFFFFFF) * prime7bytes) >> (64-HASH_LOG)); }
836
+
837
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime8bytes) >> (64-HASH_LOG)); }
838
+ static U32 ZSTD_hashPtr(const void* p) { return ( (ZSTD_read64(p) * prime7bytes) >> (56-HASH_LOG)) & HASH_MASK; }
839
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime6bytes) >> (48-HASH_LOG)) & HASH_MASK; }
840
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U64*)p * prime5bytes) >> (40-HASH_LOG)) & HASH_MASK; }
841
+ //static U32 ZSTD_hashPtr(const void* p) { return ( (*(U32*)p * KNUTH) >> (32-HASH_LOG)); }
842
+
843
+ static void ZSTD_addPtr(U32* table, const BYTE* p, const BYTE* start) { table[ZSTD_hashPtr(p)] = (U32)(p-start); }
844
+
845
+ static const BYTE* ZSTD_updateMatch(U32* table, const BYTE* p, const BYTE* start)
846
+ {
847
+ U32 h = ZSTD_hashPtr(p);
848
+ const BYTE* r;
849
+ r = table[h] + start;
850
+ ZSTD_addPtr(table, p, start);
851
+ return r;
852
+ }
853
+
854
+ static int ZSTD_checkMatch(const BYTE* match, const BYTE* ip)
855
+ {
856
+ return ZSTD_read32(match) == ZSTD_read32(ip);
857
+ }
858
+
859
+
860
+ static size_t ZSTD_compressBlock(void* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
861
+ {
862
+ cctxi_t* ctx = (cctxi_t*) cctx;
863
+ U32* HashTable = (U32*)(ctx->hashTable);
864
+ seqStore_t* seqStorePtr = &(ctx->seqStore);
865
+ const BYTE* const base = ctx->base;
866
+
867
+ const BYTE* const istart = (const BYTE*)src;
868
+ const BYTE* ip = istart + 1;
869
+ const BYTE* anchor = istart;
870
+ const BYTE* const iend = istart + srcSize;
871
+ const BYTE* const ilimit = iend - 16;
872
+
873
+ size_t prevOffset=0, offset=0;
874
+
875
+
876
+ /* init */
877
+ ZSTD_resetSeqStore(seqStorePtr);
878
+
879
+ /* Main Search Loop */
880
+ while (ip < ilimit)
881
+ {
882
+ const BYTE* match = (const BYTE*) ZSTD_updateMatch(HashTable, ip, base);
883
+
884
+ if (!ZSTD_checkMatch(match,ip)) { ip += ((ip-anchor) >> g_searchStrength) + 1; continue; }
885
+
886
+ /* catch up */
887
+ while ((ip>anchor) && (match>base) && (ip[-1] == match[-1])) { ip--; match--; }
888
+
889
+ {
890
+ size_t litLength = ip-anchor;
891
+ size_t matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend);
892
+ size_t offsetCode;
893
+ if (litLength) prevOffset = offset;
894
+ offsetCode = ip-match;
895
+ if (offsetCode == prevOffset) offsetCode = 0;
896
+ prevOffset = offset;
897
+ offset = ip-match;
898
+ ZSTD_storeSeq(seqStorePtr, litLength, anchor, offsetCode, matchLength);
899
+
900
+ /* Fill Table */
901
+ ZSTD_addPtr(HashTable, ip+1, base);
902
+ ip += matchLength + MINMATCH;
903
+ if (ip<=iend-8) ZSTD_addPtr(HashTable, ip-2, base);
904
+ anchor = ip;
905
+ }
906
+ }
907
+
908
+ /* Last Literals */
909
+ {
910
+ size_t lastLLSize = iend - anchor;
911
+ memcpy(seqStorePtr->lit, anchor, lastLLSize);
912
+ seqStorePtr->lit += lastLLSize;
913
+ }
914
+
915
+ /* Finale compression stage */
916
+ return ZSTD_compressSequences((BYTE*)dst, maxDstSize,
917
+ seqStorePtr, srcSize);
918
+ }
919
+
920
+
921
+ size_t ZSTD_compressBegin(ZSTD_Cctx* ctx, void* dst, size_t maxDstSize)
922
+ {
923
+ /* Sanity check */
924
+ if (maxDstSize < ZSTD_frameHeaderSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
925
+
926
+ /* Init */
927
+ ZSTD_resetCCtx(ctx);
928
+
929
+ /* Write Header */
930
+ ZSTD_writeBE32(dst, ZSTD_magicNumber);
931
+
932
+ return ZSTD_frameHeaderSize;
933
+ }
934
+
935
+
936
+ static void ZSTD_scaleDownCtx(void* cctx, const U32 limit)
937
+ {
938
+ cctxi_t* ctx = (cctxi_t*) cctx;
939
+ int i;
940
+
941
+ #if defined(__AVX2__)
942
+ /* AVX2 version */
943
+ __m256i* h = ctx->hashTable;
944
+ const __m256i limit8 = _mm256_set1_epi32(limit);
945
+ for (i=0; i<(HASH_TABLESIZE>>3); i++)
946
+ {
947
+ __m256i src =_mm256_loadu_si256((const __m256i*)(h+i));
948
+ const __m256i dec = _mm256_min_epu32(src, limit8);
949
+ src = _mm256_sub_epi32(src, dec);
950
+ _mm256_storeu_si256((__m256i*)(h+i), src);
951
+ }
952
+ #else
953
+ /* this should be auto-vectorized by compiler */
954
+ U32* h = ctx->hashTable;
955
+ for (i=0; i<HASH_TABLESIZE; ++i)
956
+ {
957
+ U32 dec;
958
+ if (h[i] > limit) dec = limit; else dec = h[i];
959
+ h[i] -= dec;
960
+ }
961
+ #endif
962
+ }
963
+
964
+
965
+ static void ZSTD_limitCtx(void* cctx, const U32 limit)
966
+ {
967
+ cctxi_t* ctx = (cctxi_t*) cctx;
968
+ int i;
969
+
970
+ if (limit > g_maxLimit)
971
+ {
972
+ ZSTD_scaleDownCtx(cctx, limit);
973
+ ctx->base += limit;
974
+ ctx->current -= limit;
975
+ ctx->nextUpdate -= limit;
976
+ return;
977
+ }
978
+
979
+ #if defined(__AVX2__)
980
+ /* AVX2 version */
981
+ {
982
+ __m256i* h = ctx->hashTable;
983
+ const __m256i limit8 = _mm256_set1_epi32(limit);
984
+ //printf("Address h : %0X\n", (U32)h); // address test
985
+ for (i=0; i<(HASH_TABLESIZE>>3); i++)
986
+ {
987
+ __m256i src =_mm256_loadu_si256((const __m256i*)(h+i)); // Unfortunately, clang doesn't guarantee 32-bytes alignment
988
+ src = _mm256_max_epu32(src, limit8);
989
+ _mm256_storeu_si256((__m256i*)(h+i), src);
990
+ }
991
+ }
992
+ #else
993
+ /* this should be auto-vectorized by compiler */
994
+ {
995
+ U32* h = (U32*)(ctx->hashTable);
996
+ for (i=0; i<HASH_TABLESIZE; ++i)
997
+ {
998
+ if (h[i] < limit) h[i] = limit;
999
+ }
1000
+ }
1001
+ #endif
1002
+ }
1003
+
1004
+
1005
+ size_t ZSTD_compressContinue(ZSTD_Cctx* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1006
+ {
1007
+ cctxi_t* ctx = (cctxi_t*) cctx;
1008
+ const BYTE* const istart = (const BYTE* const)src;
1009
+ const BYTE* ip = istart;
1010
+ BYTE* const ostart = (BYTE* const)dst;
1011
+ BYTE* op = ostart;
1012
+ const U32 updateRate = 2 * BLOCKSIZE;
1013
+
1014
+ /* Init */
1015
+ if (ctx->base==NULL)
1016
+ ctx->base = (const BYTE*)src, ctx->current=0, ctx->nextUpdate = g_maxDistance;
1017
+ if (src != ctx->base + ctx->current) /* not contiguous */
1018
+ {
1019
+ ZSTD_resetCCtx(ctx);
1020
+ ctx->base = (const BYTE*)src;
1021
+ ctx->current = 0;
1022
+ }
1023
+ ctx->current += (U32)srcSize;
1024
+
1025
+ while (srcSize)
1026
+ {
1027
+ size_t cSize;
1028
+ size_t blockSize = BLOCKSIZE;
1029
+ if (blockSize > srcSize) blockSize = srcSize;
1030
+
1031
+ if (maxDstSize < 2*ZSTD_blockHeaderSize+1) /* one RLE block + endMark */
1032
+ return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1033
+
1034
+ /* update hash table */
1035
+ if (g_maxDistance <= BLOCKSIZE) /* static test ; yes == blocks are independent */
1036
+ {
1037
+ ZSTD_resetCCtx(ctx);
1038
+ ctx->base = ip;
1039
+ ctx->current=0;
1040
+ }
1041
+ else if (ip >= ctx->base + ctx->nextUpdate)
1042
+ {
1043
+ ctx->nextUpdate += updateRate;
1044
+ ZSTD_limitCtx(ctx, ctx->nextUpdate - g_maxDistance);
1045
+ }
1046
+
1047
+ /* compress */
1048
+ cSize = ZSTD_compressBlock(ctx, op+ZSTD_blockHeaderSize, maxDstSize-ZSTD_blockHeaderSize, ip, blockSize);
1049
+ if (cSize == 0)
1050
+ {
1051
+ cSize = ZSTD_noCompressBlock(op, maxDstSize, ip, blockSize); /* block is not compressible */
1052
+ if (ZSTD_isError(cSize)) return cSize;
1053
+ }
1054
+ else
1055
+ {
1056
+ if (ZSTD_isError(cSize)) return cSize;
1057
+ op[0] = (BYTE)(cSize>>16);
1058
+ op[1] = (BYTE)(cSize>>8);
1059
+ op[2] = (BYTE)cSize;
1060
+ op[0] += (BYTE)(bt_compressed << 6); /* is a compressed block */
1061
+ cSize += 3;
1062
+ }
1063
+ op += cSize;
1064
+ maxDstSize -= cSize;
1065
+ ip += blockSize;
1066
+ srcSize -= blockSize;
1067
+ }
1068
+
1069
+ return op-ostart;
1070
+ }
1071
+
1072
+
1073
+ size_t ZSTD_compressEnd(ZSTD_Cctx* ctx, void* dst, size_t maxDstSize)
1074
+ {
1075
+ BYTE* op = (BYTE*)dst;
1076
+
1077
+ /* Sanity check */
1078
+ (void)ctx;
1079
+ if (maxDstSize < ZSTD_blockHeaderSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1080
+
1081
+ /* End of frame */
1082
+ op[0] = (BYTE)(bt_end << 6);
1083
+ op[1] = 0;
1084
+ op[2] = 0;
1085
+
1086
+ return 3;
1087
+ }
1088
+
1089
+
1090
+ static size_t ZSTD_compressCCtx(ZSTD_Cctx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1091
+ {
1092
+ BYTE* const ostart = (BYTE* const)dst;
1093
+ BYTE* op = ostart;
1094
+
1095
+ /* Header */
1096
+ {
1097
+ size_t headerSize = ZSTD_compressBegin(ctx, dst, maxDstSize);
1098
+ if(ZSTD_isError(headerSize)) return headerSize;
1099
+ op += headerSize;
1100
+ maxDstSize -= headerSize;
1101
+ }
1102
+
1103
+ /* Compression */
1104
+ {
1105
+ size_t cSize = ZSTD_compressContinue(ctx, op, maxDstSize, src, srcSize);
1106
+ if (ZSTD_isError(cSize)) return cSize;
1107
+ op += cSize;
1108
+ maxDstSize -= cSize;
1109
+ }
1110
+
1111
+ /* Close frame */
1112
+ {
1113
+ size_t endSize = ZSTD_compressEnd(ctx, op, maxDstSize);
1114
+ if(ZSTD_isError(endSize)) return endSize;
1115
+ op += endSize;
1116
+ }
1117
+
1118
+ return (op - ostart);
1119
+ }
1120
+
1121
+
1122
+ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1123
+ {
1124
+ ZSTD_Cctx* ctx;
1125
+ size_t r;
1126
+
1127
+ ctx = ZSTD_createCCtx();
1128
+ if (ctx==NULL) return (size_t)-ZSTD_ERROR_GENERIC;
1129
+ r = ZSTD_compressCCtx(ctx, dst, maxDstSize, src, srcSize);
1130
+ ZSTD_freeCCtx(ctx);
1131
+ return r;
1132
+ }
1133
+
1134
+
1135
+ /**************************************************************
1136
+ * Decompression code
1137
+ **************************************************************/
1138
+
1139
+ size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
1140
+ {
1141
+ const BYTE* const in = (const BYTE* const)src;
1142
+ BYTE headerFlags;
1143
+ U32 cSize;
1144
+
1145
+ if (srcSize < 3) return (size_t)-ZSTD_ERROR_SrcSize;
1146
+
1147
+ headerFlags = *in;
1148
+ cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
1149
+
1150
+ bpPtr->blockType = (blockType_t)(headerFlags >> 6);
1151
+ bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
1152
+
1153
+ if (bpPtr->blockType == bt_end) return 0;
1154
+ if (bpPtr->blockType == bt_rle) return 1;
1155
+ return cSize;
1156
+ }
1157
+
1158
+
1159
+ static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1160
+ {
1161
+ if (srcSize > maxDstSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1162
+ memcpy(dst, src, srcSize);
1163
+ return srcSize;
1164
+ }
1165
+
1166
+
1167
+ static size_t ZSTD_decompressLiterals(void* ctx,
1168
+ void* dst, size_t maxDstSize,
1169
+ const void* src, size_t srcSize)
1170
+ {
1171
+ BYTE* op = (BYTE*)dst;
1172
+ BYTE* const oend = op + maxDstSize;
1173
+ const BYTE* ip = (const BYTE*)src;
1174
+ size_t errorCode;
1175
+ size_t litSize;
1176
+
1177
+ /* check : minimum 2, for litSize, +1, for content */
1178
+ if (srcSize <= 3) return (size_t)-ZSTD_ERROR_corruption;
1179
+
1180
+ litSize = ip[1] + (ip[0]<<8);
1181
+ litSize += ((ip[-3] >> 3) & 7) << 16; // mmmmh....
1182
+ op = oend - litSize;
1183
+
1184
+ (void)ctx;
1185
+ if (litSize > maxDstSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1186
+ errorCode = HUF_decompress(op, litSize, ip+2, srcSize-2);
1187
+ if (FSE_isError(errorCode)) return (size_t)-ZSTD_ERROR_GENERIC;
1188
+ return litSize;
1189
+ }
1190
+
1191
+
1192
+ size_t ZSTD_decodeLiteralsBlock(void* ctx,
1193
+ void* dst, size_t maxDstSize,
1194
+ const BYTE** litStart, size_t* litSize,
1195
+ const void* src, size_t srcSize)
1196
+ {
1197
+ const BYTE* const istart = (const BYTE* const)src;
1198
+ const BYTE* ip = istart;
1199
+ BYTE* const ostart = (BYTE* const)dst;
1200
+ BYTE* const oend = ostart + maxDstSize;
1201
+ blockProperties_t litbp;
1202
+
1203
+ size_t litcSize = ZSTD_getcBlockSize(src, srcSize, &litbp);
1204
+ if (ZSTD_isError(litcSize)) return litcSize;
1205
+ if (litcSize > srcSize - ZSTD_blockHeaderSize) return (size_t)-ZSTD_ERROR_SrcSize;
1206
+ ip += ZSTD_blockHeaderSize;
1207
+
1208
+ switch(litbp.blockType)
1209
+ {
1210
+ case bt_raw:
1211
+ *litStart = ip;
1212
+ ip += litcSize;
1213
+ *litSize = litcSize;
1214
+ break;
1215
+ case bt_rle:
1216
+ {
1217
+ size_t rleSize = litbp.origSize;
1218
+ if (rleSize>maxDstSize) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1219
+ memset(oend - rleSize, *ip, rleSize);
1220
+ *litStart = oend - rleSize;
1221
+ *litSize = rleSize;
1222
+ ip++;
1223
+ break;
1224
+ }
1225
+ case bt_compressed:
1226
+ {
1227
+ size_t decodedLitSize = ZSTD_decompressLiterals(ctx, dst, maxDstSize, ip, litcSize);
1228
+ if (ZSTD_isError(decodedLitSize)) return decodedLitSize;
1229
+ *litStart = oend - decodedLitSize;
1230
+ *litSize = decodedLitSize;
1231
+ ip += litcSize;
1232
+ break;
1233
+ }
1234
+ default:
1235
+ return (size_t)-ZSTD_ERROR_GENERIC;
1236
+ }
1237
+
1238
+ return ip-istart;
1239
+ }
1240
+
1241
+
1242
+ size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr,
1243
+ FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
1244
+ const void* src, size_t srcSize)
1245
+ {
1246
+ const BYTE* const istart = (const BYTE* const)src;
1247
+ const BYTE* ip = istart;
1248
+ const BYTE* const iend = istart + srcSize;
1249
+ U32 LLtype, Offtype, MLtype;
1250
+ U32 LLlog, Offlog, MLlog;
1251
+ size_t dumpsLength;
1252
+
1253
+ /* check */
1254
+ if (srcSize < 5) return (size_t)-ZSTD_ERROR_SrcSize;
1255
+
1256
+ /* SeqHead */
1257
+ *nbSeq = ZSTD_readLE16(ip); ip+=2;
1258
+ LLtype = *ip >> 6;
1259
+ Offtype = (*ip >> 4) & 3;
1260
+ MLtype = (*ip >> 2) & 3;
1261
+ if (*ip & 2)
1262
+ {
1263
+ dumpsLength = ip[2];
1264
+ dumpsLength += ip[1] << 8;
1265
+ ip += 3;
1266
+ }
1267
+ else
1268
+ {
1269
+ dumpsLength = ip[1];
1270
+ dumpsLength += (ip[0] & 1) << 8;
1271
+ ip += 2;
1272
+ }
1273
+ *dumpsPtr = ip;
1274
+ ip += dumpsLength;
1275
+
1276
+ /* check */
1277
+ if (ip > iend-3) return (size_t)-ZSTD_ERROR_SrcSize; /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
1278
+
1279
+ /* sequences */
1280
+ {
1281
+ S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */
1282
+ size_t headerSize;
1283
+
1284
+ /* Build DTables */
1285
+ switch(LLtype)
1286
+ {
1287
+ U32 max;
1288
+ case bt_rle :
1289
+ LLlog = 0;
1290
+ FSE_buildDTable_rle(DTableLL, *ip++); break;
1291
+ case bt_raw :
1292
+ LLlog = LLbits;
1293
+ FSE_buildDTable_raw(DTableLL, LLbits); break;
1294
+ default :
1295
+ max = MaxLL;
1296
+ headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
1297
+ if (FSE_isError(headerSize)) return (size_t)-ZSTD_ERROR_GENERIC;
1298
+ if (LLlog > LLFSELog) return (size_t)-ZSTD_ERROR_corruption;
1299
+ ip += headerSize;
1300
+ FSE_buildDTable(DTableLL, norm, max, LLlog);
1301
+ }
1302
+
1303
+ switch(Offtype)
1304
+ {
1305
+ U32 max;
1306
+ case bt_rle :
1307
+ Offlog = 0;
1308
+ if (ip > iend-2) return (size_t)-ZSTD_ERROR_SrcSize; /* min : "raw", hence no header, but at least xxLog bits */
1309
+ FSE_buildDTable_rle(DTableOffb, *ip++); break;
1310
+ case bt_raw :
1311
+ Offlog = Offbits;
1312
+ FSE_buildDTable_raw(DTableOffb, Offbits); break;
1313
+ default :
1314
+ max = MaxOff;
1315
+ headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
1316
+ if (FSE_isError(headerSize)) return (size_t)-ZSTD_ERROR_GENERIC;
1317
+ if (Offlog > OffFSELog) return (size_t)-ZSTD_ERROR_corruption;
1318
+ ip += headerSize;
1319
+ FSE_buildDTable(DTableOffb, norm, max, Offlog);
1320
+ }
1321
+
1322
+ switch(MLtype)
1323
+ {
1324
+ U32 max;
1325
+ case bt_rle :
1326
+ MLlog = 0;
1327
+ if (ip > iend-2) return (size_t)-ZSTD_ERROR_SrcSize; /* min : "raw", hence no header, but at least xxLog bits */
1328
+ FSE_buildDTable_rle(DTableML, *ip++); break;
1329
+ case bt_raw :
1330
+ MLlog = MLbits;
1331
+ FSE_buildDTable_raw(DTableML, MLbits); break;
1332
+ default :
1333
+ max = MaxML;
1334
+ headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
1335
+ if (FSE_isError(headerSize)) return (size_t)-ZSTD_ERROR_GENERIC;
1336
+ if (MLlog > MLFSELog) return (size_t)-ZSTD_ERROR_corruption;
1337
+ ip += headerSize;
1338
+ FSE_buildDTable(DTableML, norm, max, MLlog);
1339
+ }
1340
+ }
1341
+
1342
+ return ip-istart;
1343
+ }
1344
+
1345
+
1346
+ typedef struct {
1347
+ size_t litLength;
1348
+ size_t offset;
1349
+ size_t matchLength;
1350
+ } seq_t;
1351
+
1352
+ typedef struct {
1353
+ FSE_DStream_t DStream;
1354
+ FSE_DState_t stateLL;
1355
+ FSE_DState_t stateOffb;
1356
+ FSE_DState_t stateML;
1357
+ size_t prevOffset;
1358
+ const BYTE* dumps;
1359
+ } seqState_t;
1360
+
1361
+
1362
+ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
1363
+ {
1364
+ size_t litLength;
1365
+ size_t prevOffset;
1366
+ size_t offset;
1367
+ size_t matchLength;
1368
+ const BYTE* dumps = seqState->dumps;
1369
+
1370
+ /* Literal length */
1371
+ litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
1372
+ prevOffset = litLength ? seq->offset : seqState->prevOffset;
1373
+ seqState->prevOffset = seq->offset;
1374
+ if (litLength == MaxLL)
1375
+ {
1376
+ U32 add = *dumps++;
1377
+ if (add < 255) litLength += add;
1378
+ else
1379
+ {
1380
+ litLength = ZSTD_readLE32(dumps) & 0xFFFFFF;
1381
+ dumps += 3;
1382
+ }
1383
+ }
1384
+
1385
+ /* Offset */
1386
+ {
1387
+ U32 offsetCode, nbBits;
1388
+ offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream));
1389
+ if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream));
1390
+ nbBits = offsetCode - 1;
1391
+ if (offsetCode==0) nbBits = 0; /* cmove */
1392
+ offset = ((size_t)1 << (nbBits & ((sizeof(offset)*8)-1))) + FSE_readBits(&(seqState->DStream), nbBits);
1393
+ if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream));
1394
+ if (offsetCode==0) offset = prevOffset;
1395
+ }
1396
+
1397
+ /* MatchLength */
1398
+ matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
1399
+ if (matchLength == MaxML)
1400
+ {
1401
+ U32 add = *dumps++;
1402
+ if (add < 255) matchLength += add;
1403
+ else
1404
+ {
1405
+ matchLength = ZSTD_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
1406
+ dumps += 3;
1407
+ }
1408
+ }
1409
+ matchLength += MINMATCH;
1410
+
1411
+ /* save result */
1412
+ seq->litLength = litLength;
1413
+ seq->offset = offset;
1414
+ seq->matchLength = matchLength;
1415
+ seqState->dumps = dumps;
1416
+ }
1417
+
1418
+
1419
+ static size_t ZSTD_execSequence(BYTE* op,
1420
+ seq_t sequence,
1421
+ const BYTE** litPtr, const BYTE* const litLimit,
1422
+ BYTE* const base, BYTE* const oend)
1423
+ {
1424
+ static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
1425
+ static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
1426
+ const BYTE* const ostart = op;
1427
+ const size_t litLength = sequence.litLength;
1428
+ BYTE* const endMatch = op + litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
1429
+ const BYTE* const litEnd = *litPtr + litLength;
1430
+
1431
+ /* check */
1432
+ if (endMatch > oend) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall; /* overwrite beyond dst buffer */
1433
+ if (litEnd > litLimit) return (size_t)-ZSTD_ERROR_corruption;
1434
+ if (sequence.matchLength > (size_t)(*litPtr-op)) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall; /* overwrite literal segment */
1435
+
1436
+ /* copy Literals */
1437
+ if (((size_t)(*litPtr - op) < 8) || ((size_t)(oend-litEnd) < 8) || (op+litLength > oend-8))
1438
+ memmove(op, *litPtr, litLength); /* overwrite risk */
1439
+ else
1440
+ ZSTD_wildcopy(op, *litPtr, litLength);
1441
+ op += litLength;
1442
+ *litPtr = litEnd; /* update for next sequence */
1443
+
1444
+ /* check : last match must be at a minimum distance of 8 from end of dest buffer */
1445
+ if (oend-op < 8) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1446
+
1447
+ /* copy Match */
1448
+ {
1449
+ const U32 overlapRisk = (((size_t)(litEnd - endMatch)) < 12);
1450
+ const BYTE* match = op - sequence.offset; /* possible underflow at op - offset ? */
1451
+ size_t qutt = 12;
1452
+ U64 saved[2];
1453
+
1454
+ /* check */
1455
+ if (match < base) return (size_t)-ZSTD_ERROR_corruption;
1456
+ if (sequence.offset > (size_t)base) return (size_t)-ZSTD_ERROR_corruption;
1457
+
1458
+ /* save beginning of literal sequence, in case of write overlap */
1459
+ if (overlapRisk)
1460
+ {
1461
+ if ((endMatch + qutt) > oend) qutt = oend-endMatch;
1462
+ memcpy(saved, endMatch, qutt);
1463
+ }
1464
+
1465
+ if (sequence.offset < 8)
1466
+ {
1467
+ const int dec64 = dec64table[sequence.offset];
1468
+ op[0] = match[0];
1469
+ op[1] = match[1];
1470
+ op[2] = match[2];
1471
+ op[3] = match[3];
1472
+ match += dec32table[sequence.offset];
1473
+ ZSTD_copy4(op+4, match);
1474
+ match -= dec64;
1475
+ } else { ZSTD_copy8(op, match); }
1476
+ op += 8; match += 8;
1477
+
1478
+ if (endMatch > oend-12)
1479
+ {
1480
+ if (op < oend-8)
1481
+ {
1482
+ ZSTD_wildcopy(op, match, (oend-8) - op);
1483
+ match += (oend-8) - op;
1484
+ op = oend-8;
1485
+ }
1486
+ while (op<endMatch) *op++ = *match++;
1487
+ }
1488
+ else
1489
+ ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
1490
+
1491
+ /* restore, in case of overlap */
1492
+ if (overlapRisk) memcpy(endMatch, saved, qutt);
1493
+ }
1494
+
1495
+ return endMatch-ostart;
1496
+ }
1497
+
1498
+ typedef struct ZSTD_Dctx_s
1499
+ {
1500
+ U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
1501
+ U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
1502
+ U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
1503
+ void* previousDstEnd;
1504
+ void* base;
1505
+ size_t expected;
1506
+ blockType_t bType;
1507
+ U32 phase;
1508
+ } dctx_t;
1509
+
1510
+
1511
+ static size_t ZSTD_decompressSequences(
1512
+ void* ctx,
1513
+ void* dst, size_t maxDstSize,
1514
+ const void* seqStart, size_t seqSize,
1515
+ const BYTE* litStart, size_t litSize)
1516
+ {
1517
+ dctx_t* dctx = (dctx_t*)ctx;
1518
+ const BYTE* ip = (const BYTE*)seqStart;
1519
+ const BYTE* const iend = ip + seqSize;
1520
+ BYTE* const ostart = (BYTE* const)dst;
1521
+ BYTE* op = ostart;
1522
+ BYTE* const oend = ostart + maxDstSize;
1523
+ size_t errorCode;
1524
+ const BYTE* litPtr = litStart;
1525
+ const BYTE* const litEnd = litStart + litSize;
1526
+ int nbSeq;
1527
+ const BYTE* dumps;
1528
+ U32* DTableLL = dctx->LLTable;
1529
+ U32* DTableML = dctx->MLTable;
1530
+ U32* DTableOffb = dctx->OffTable;
1531
+ BYTE* const base = (BYTE*) (dctx->base);
1532
+
1533
+ /* Build Decoding Tables */
1534
+ errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps,
1535
+ DTableLL, DTableML, DTableOffb,
1536
+ ip, iend-ip);
1537
+ if (ZSTD_isError(errorCode)) return errorCode;
1538
+ ip += errorCode;
1539
+
1540
+ /* Regen sequences */
1541
+ {
1542
+ seq_t sequence;
1543
+ seqState_t seqState;
1544
+
1545
+ memset(&sequence, 0, sizeof(sequence));
1546
+ seqState.dumps = dumps;
1547
+ seqState.prevOffset = 1;
1548
+ errorCode = FSE_initDStream(&(seqState.DStream), ip, iend-ip);
1549
+ if (FSE_isError(errorCode)) return (size_t)-ZSTD_ERROR_corruption;
1550
+ FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
1551
+ FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
1552
+ FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
1553
+
1554
+ for ( ; (FSE_reloadDStream(&(seqState.DStream)) < FSE_DStream_completed) || (nbSeq>0) ; )
1555
+ {
1556
+ size_t oneSeqSize;
1557
+ nbSeq--;
1558
+ ZSTD_decodeSequence(&sequence, &seqState);
1559
+ oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend);
1560
+ if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
1561
+ op += oneSeqSize;
1562
+ }
1563
+
1564
+ /* check if reached exact end */
1565
+ if (FSE_reloadDStream(&(seqState.DStream)) > FSE_DStream_completed) return (size_t)-ZSTD_ERROR_corruption; /* requested too much : data is corrupted */
1566
+ if (nbSeq<0) return (size_t)-ZSTD_ERROR_corruption; /* requested too many sequences : data is corrupted */
1567
+
1568
+ /* last literal segment */
1569
+ {
1570
+ size_t lastLLSize = litEnd - litPtr;
1571
+ if (op+lastLLSize > oend) return (size_t)-ZSTD_ERROR_maxDstSize_tooSmall;
1572
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
1573
+ op += lastLLSize;
1574
+ }
1575
+ }
1576
+
1577
+ return op-ostart;
1578
+ }
1579
+
1580
+
1581
+ static size_t ZSTD_decompressBlock(
1582
+ void* ctx,
1583
+ void* dst, size_t maxDstSize,
1584
+ const void* src, size_t srcSize)
1585
+ {
1586
+ /* blockType == blockCompressed, srcSize is trusted */
1587
+ const BYTE* ip = (const BYTE*)src;
1588
+ const BYTE* litPtr;
1589
+ size_t litSize;
1590
+ size_t errorCode;
1591
+
1592
+ /* Decode literals sub-block */
1593
+ errorCode = ZSTD_decodeLiteralsBlock(ctx, dst, maxDstSize, &litPtr, &litSize, src, srcSize);
1594
+ if (ZSTD_isError(errorCode)) return errorCode;
1595
+ ip += errorCode;
1596
+ srcSize -= errorCode;
1597
+
1598
+ return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize, litPtr, litSize);
1599
+ }
1600
+
1601
+
1602
+ static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1603
+ {
1604
+ const BYTE* ip = (const BYTE*)src;
1605
+ const BYTE* iend = ip + srcSize;
1606
+ BYTE* const ostart = (BYTE* const)dst;
1607
+ BYTE* op = ostart;
1608
+ BYTE* const oend = ostart + maxDstSize;
1609
+ size_t remainingSize = srcSize;
1610
+ U32 magicNumber;
1611
+ size_t errorCode=0;
1612
+ blockProperties_t blockProperties;
1613
+
1614
+ /* Frame Header */
1615
+ if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return (size_t)-ZSTD_ERROR_SrcSize;
1616
+ magicNumber = ZSTD_readBE32(src);
1617
+ if (magicNumber != ZSTD_magicNumber) return (size_t)-ZSTD_ERROR_MagicNumber;
1618
+ ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
1619
+
1620
+ /* Loop on each block */
1621
+ while (1)
1622
+ {
1623
+ size_t blockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
1624
+ if (ZSTD_isError(blockSize)) return blockSize;
1625
+
1626
+ ip += ZSTD_blockHeaderSize;
1627
+ remainingSize -= ZSTD_blockHeaderSize;
1628
+ if (blockSize > remainingSize) return (size_t)-ZSTD_ERROR_SrcSize;
1629
+
1630
+ switch(blockProperties.blockType)
1631
+ {
1632
+ case bt_compressed:
1633
+ errorCode = ZSTD_decompressBlock(ctx, op, oend-op, ip, blockSize);
1634
+ break;
1635
+ case bt_raw :
1636
+ errorCode = ZSTD_copyUncompressedBlock(op, oend-op, ip, blockSize);
1637
+ break;
1638
+ case bt_rle :
1639
+ return (size_t)-ZSTD_ERROR_GENERIC; /* not yet supported */
1640
+ break;
1641
+ case bt_end :
1642
+ /* end of frame */
1643
+ if (remainingSize) return (size_t)-ZSTD_ERROR_SrcSize;
1644
+ break;
1645
+ default:
1646
+ return (size_t)-ZSTD_ERROR_GENERIC;
1647
+ }
1648
+ if (blockSize == 0) break; /* bt_end */
1649
+
1650
+ if (ZSTD_isError(errorCode)) return errorCode;
1651
+ op += errorCode;
1652
+ ip += blockSize;
1653
+ remainingSize -= blockSize;
1654
+ }
1655
+
1656
+ return op-ostart;
1657
+ }
1658
+
1659
+ size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1660
+ {
1661
+ dctx_t ctx;
1662
+ ctx.base = dst;
1663
+ return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
1664
+ }
1665
+
1666
+
1667
+ /*******************************
1668
+ * Streaming Decompression API
1669
+ *******************************/
1670
+
1671
+ size_t ZSTD_resetDCtx(ZSTD_Dctx* dctx)
1672
+ {
1673
+ dctx->expected = ZSTD_frameHeaderSize;
1674
+ dctx->phase = 0;
1675
+ dctx->previousDstEnd = NULL;
1676
+ dctx->base = NULL;
1677
+ return 0;
1678
+ }
1679
+
1680
+ ZSTD_Dctx* ZSTD_createDCtx(void)
1681
+ {
1682
+ ZSTD_Dctx* dctx = (ZSTD_Dctx*)malloc(sizeof(ZSTD_Dctx));
1683
+ if (dctx==NULL) return NULL;
1684
+ ZSTD_resetDCtx(dctx);
1685
+ return dctx;
1686
+ }
1687
+
1688
+ size_t ZSTD_freeDCtx(ZSTD_Dctx* dctx)
1689
+ {
1690
+ free(dctx);
1691
+ return 0;
1692
+ }
1693
+
1694
+ size_t ZSTD_nextSrcSizeToDecompress(ZSTD_Dctx* dctx)
1695
+ {
1696
+ return ((dctx_t*)dctx)->expected;
1697
+ }
1698
+
1699
+ size_t ZSTD_decompressContinue(ZSTD_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1700
+ {
1701
+ dctx_t* ctx = (dctx_t*)dctx;
1702
+
1703
+ /* Sanity check */
1704
+ if (srcSize != ctx->expected) return (size_t)-ZSTD_ERROR_SrcSize;
1705
+ if (dst != ctx->previousDstEnd) /* not contiguous */
1706
+ ctx->base = dst;
1707
+
1708
+ /* Decompress : frame header */
1709
+ if (ctx->phase == 0)
1710
+ {
1711
+ /* Check frame magic header */
1712
+ U32 magicNumber = ZSTD_readBE32(src);
1713
+ if (magicNumber != ZSTD_magicNumber) return (size_t)-ZSTD_ERROR_MagicNumber;
1714
+ ctx->phase = 1;
1715
+ ctx->expected = ZSTD_blockHeaderSize;
1716
+ return 0;
1717
+ }
1718
+
1719
+ /* Decompress : block header */
1720
+ if (ctx->phase == 1)
1721
+ {
1722
+ blockProperties_t bp;
1723
+ size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
1724
+ if (ZSTD_isError(blockSize)) return blockSize;
1725
+ if (bp.blockType == bt_end)
1726
+ {
1727
+ ctx->expected = 0;
1728
+ ctx->phase = 0;
1729
+ }
1730
+ else
1731
+ {
1732
+ ctx->expected = blockSize;
1733
+ ctx->bType = bp.blockType;
1734
+ ctx->phase = 2;
1735
+ }
1736
+
1737
+ return 0;
1738
+ }
1739
+
1740
+ /* Decompress : block content */
1741
+ {
1742
+ size_t rSize;
1743
+ switch(ctx->bType)
1744
+ {
1745
+ case bt_compressed:
1746
+ rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
1747
+ break;
1748
+ case bt_raw :
1749
+ rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
1750
+ break;
1751
+ case bt_rle :
1752
+ return (size_t)-ZSTD_ERROR_GENERIC; /* not yet handled */
1753
+ break;
1754
+ case bt_end : /* should never happen (filtered at phase 1) */
1755
+ rSize = 0;
1756
+ break;
1757
+ default:
1758
+ return (size_t)-ZSTD_ERROR_GENERIC;
1759
+ }
1760
+ ctx->phase = 1;
1761
+ ctx->expected = ZSTD_blockHeaderSize;
1762
+ ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
1763
+ return rSize;
1764
+ }
1765
+
1766
+ }
1767
+
1768
+