Coverage Report

Created: 2025-08-29 06:48

/src/zstd/lib/legacy/zstd_v02.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
12
#include <stddef.h>    /* size_t, ptrdiff_t */
13
#include "zstd_v02.h"
14
#include "../common/compiler.h"
15
#include "../common/error_private.h"
16
17
18
/******************************************
19
*  Compiler-specific
20
******************************************/
21
#if defined(_MSC_VER)   /* Visual Studio */
22
#   include <stdlib.h>  /* _byteswap_ulong */
23
#   include <intrin.h>  /* _byteswap_* */
24
#endif
25
26
27
/* ******************************************************************
28
   mem.h
29
   low-level memory access routines
30
   Copyright (C) 2013-2015, Yann Collet.
31
32
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
33
34
   Redistribution and use in source and binary forms, with or without
35
   modification, are permitted provided that the following conditions are
36
   met:
37
38
       * Redistributions of source code must retain the above copyright
39
   notice, this list of conditions and the following disclaimer.
40
       * Redistributions in binary form must reproduce the above
41
   copyright notice, this list of conditions and the following disclaimer
42
   in the documentation and/or other materials provided with the
43
   distribution.
44
45
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56
57
    You can contact the author at :
58
    - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
59
    - Public forum : https://groups.google.com/forum/#!forum/lz4c
60
****************************************************************** */
61
#ifndef MEM_H_MODULE
62
#define MEM_H_MODULE
63
64
#if defined (__cplusplus)
65
extern "C" {
66
#endif
67
68
/******************************************
69
*  Includes
70
******************************************/
71
#include <stddef.h>    /* size_t, ptrdiff_t */
72
#include <string.h>    /* memcpy */
73
74
75
/****************************************************************
76
*  Basic Types
77
*****************************************************************/
78
#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
79
# if defined(_AIX)
80
#  include <inttypes.h>
81
# else
82
#  include <stdint.h> /* intptr_t */
83
# endif
84
  typedef  uint8_t BYTE;
85
  typedef uint16_t U16;
86
  typedef  int16_t S16;
87
  typedef uint32_t U32;
88
  typedef  int32_t S32;
89
  typedef uint64_t U64;
90
  typedef  int64_t S64;
91
#else
92
  typedef unsigned char       BYTE;
93
  typedef unsigned short      U16;
94
  typedef   signed short      S16;
95
  typedef unsigned int        U32;
96
  typedef   signed int        S32;
97
  typedef unsigned long long  U64;
98
  typedef   signed long long  S64;
99
#endif
100
101
102
/****************************************************************
103
*  Memory I/O
104
*****************************************************************/
105
106
1.03M
MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
107
1.08M
MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
108
109
MEM_STATIC unsigned MEM_isLittleEndian(void)
110
605k
{
111
605k
    const union { U32 u; BYTE c[4]; } one = { 1 };   /* don't use static : performance detrimental  */
112
605k
    return one.c[0];
113
605k
}
114
115
MEM_STATIC U16 MEM_read16(const void* memPtr)
116
9.08k
{
117
9.08k
    U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
118
9.08k
}
119
120
MEM_STATIC U32 MEM_read32(const void* memPtr)
121
39.2k
{
122
39.2k
    U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
123
39.2k
}
124
125
MEM_STATIC U64 MEM_read64(const void* memPtr)
126
523k
{
127
523k
    U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
128
523k
}
129
130
MEM_STATIC void MEM_write16(void* memPtr, U16 value)
131
33.9k
{
132
33.9k
    memcpy(memPtr, &value, sizeof(value));
133
33.9k
}
134
135
MEM_STATIC U16 MEM_readLE16(const void* memPtr)
136
9.08k
{
137
9.08k
    if (MEM_isLittleEndian())
138
9.08k
        return MEM_read16(memPtr);
139
0
    else
140
0
    {
141
0
        const BYTE* p = (const BYTE*)memPtr;
142
0
        return (U16)(p[0] + (p[1]<<8));
143
0
    }
144
9.08k
}
145
146
MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
147
33.9k
{
148
33.9k
    if (MEM_isLittleEndian())
149
33.9k
    {
150
33.9k
        MEM_write16(memPtr, val);
151
33.9k
    }
152
0
    else
153
0
    {
154
0
        BYTE* p = (BYTE*)memPtr;
155
0
        p[0] = (BYTE)val;
156
0
        p[1] = (BYTE)(val>>8);
157
0
    }
158
33.9k
}
159
160
MEM_STATIC U32 MEM_readLE24(const void* memPtr)
161
137
{
162
137
    return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
163
137
}
164
165
MEM_STATIC U32 MEM_readLE32(const void* memPtr)
166
39.2k
{
167
39.2k
    if (MEM_isLittleEndian())
168
39.2k
        return MEM_read32(memPtr);
169
0
    else
170
0
    {
171
0
        const BYTE* p = (const BYTE*)memPtr;
172
0
        return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
173
0
    }
174
39.2k
}
175
176
177
MEM_STATIC U64 MEM_readLE64(const void* memPtr)
178
523k
{
179
523k
    if (MEM_isLittleEndian())
180
523k
        return MEM_read64(memPtr);
181
0
    else
182
0
    {
183
0
        const BYTE* p = (const BYTE*)memPtr;
184
0
        return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
185
0
                     + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
186
0
    }
187
523k
}
188
189
190
MEM_STATIC size_t MEM_readLEST(const void* memPtr)
191
523k
{
192
523k
    if (MEM_32bits())
193
0
        return (size_t)MEM_readLE32(memPtr);
194
523k
    else
195
523k
        return (size_t)MEM_readLE64(memPtr);
196
523k
}
197
198
#if defined (__cplusplus)
199
}
200
#endif
201
202
#endif /* MEM_H_MODULE */
203
204
205
/* ******************************************************************
206
   bitstream
207
   Part of NewGen Entropy library
208
   header file (to include)
209
   Copyright (C) 2013-2015, Yann Collet.
210
211
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
212
213
   Redistribution and use in source and binary forms, with or without
214
   modification, are permitted provided that the following conditions are
215
   met:
216
217
       * Redistributions of source code must retain the above copyright
218
   notice, this list of conditions and the following disclaimer.
219
       * Redistributions in binary form must reproduce the above
220
   copyright notice, this list of conditions and the following disclaimer
221
   in the documentation and/or other materials provided with the
222
   distribution.
223
224
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
227
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
229
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
230
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
234
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
235
236
   You can contact the author at :
237
   - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
238
   - Public forum : https://groups.google.com/forum/#!forum/lz4c
239
****************************************************************** */
240
#ifndef BITSTREAM_H_MODULE
241
#define BITSTREAM_H_MODULE
242
243
#if defined (__cplusplus)
244
extern "C" {
245
#endif
246
247
248
/*
249
*  This API consists of small unitary functions, which highly benefit from being inlined.
250
*  Since link-time-optimization is not available for all compilers,
251
*  these functions are defined into a .h to be included.
252
*/
253
254
255
/**********************************************
256
*  bitStream decompression API (read backward)
257
**********************************************/
258
typedef struct
259
{
260
    size_t   bitContainer;
261
    unsigned bitsConsumed;
262
    const char* ptr;
263
    const char* start;
264
} BIT_DStream_t;
265
266
typedef enum { BIT_DStream_unfinished = 0,
267
               BIT_DStream_endOfBuffer = 1,
268
               BIT_DStream_completed = 2,
269
               BIT_DStream_overflow = 3 } BIT_DStream_status;  /* result of BIT_reloadDStream() */
270
               /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
271
272
MEM_STATIC size_t   BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
273
MEM_STATIC size_t   BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
274
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
275
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
276
277
278
/******************************************
279
*  unsafe API
280
******************************************/
281
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
282
/* faster, but works only if nbBits >= 1 */
283
284
285
286
/****************************************************************
287
*  Helper functions
288
****************************************************************/
289
MEM_STATIC unsigned BIT_highbit32 (U32 val)
290
911k
{
291
#   if defined(_MSC_VER)   /* Visual */
292
    unsigned long r;
293
    return _BitScanReverse(&r, val) ? (unsigned)r : 0;
294
#   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* Use GCC Intrinsic */
295
    return __builtin_clz (val) ^ 31;
296
#   else   /* Software version */
297
    static const unsigned 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 };
298
    U32 v = val;
299
    unsigned r;
300
    v |= v >> 1;
301
    v |= v >> 2;
302
    v |= v >> 4;
303
    v |= v >> 8;
304
    v |= v >> 16;
305
    r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
306
    return r;
307
#   endif
308
911k
}
309
310
311
312
/**********************************************************
313
* bitStream decoding
314
**********************************************************/
315
316
/*!BIT_initDStream
317
*  Initialize a BIT_DStream_t.
318
*  @bitD : a pointer to an already allocated BIT_DStream_t structure
319
*  @srcBuffer must point at the beginning of a bitStream
320
*  @srcSize must be the exact size of the bitStream
321
*  @result : size of stream (== srcSize) or an errorCode if a problem is detected
322
*/
323
MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
324
9.90k
{
325
9.90k
    if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
326
327
9.87k
    if (srcSize >=  sizeof(size_t))   /* normal case */
328
1.83k
    {
329
1.83k
        U32 contain32;
330
1.83k
        bitD->start = (const char*)srcBuffer;
331
1.83k
        bitD->ptr   = (const char*)srcBuffer + srcSize - sizeof(size_t);
332
1.83k
        bitD->bitContainer = MEM_readLEST(bitD->ptr);
333
1.83k
        contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
334
1.83k
        if (contain32 == 0) return ERROR(GENERIC);   /* endMark not present */
335
1.80k
        bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
336
1.80k
    }
337
8.03k
    else
338
8.03k
    {
339
8.03k
        U32 contain32;
340
8.03k
        bitD->start = (const char*)srcBuffer;
341
8.03k
        bitD->ptr   = bitD->start;
342
8.03k
        bitD->bitContainer = *(const BYTE*)(bitD->start);
343
8.03k
        switch(srcSize)
344
8.03k
        {
345
24
            case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
346
                    /* fallthrough */
347
122
            case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
348
                    /* fallthrough */
349
184
            case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
350
                    /* fallthrough */
351
762
            case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
352
                    /* fallthrough */
353
3.04k
            case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
354
                    /* fallthrough */
355
3.72k
            case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) <<  8;
356
                    /* fallthrough */
357
8.03k
            default:;
358
8.03k
        }
359
8.03k
        contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
360
8.03k
        if (contain32 == 0) return ERROR(GENERIC);   /* endMark not present */
361
7.99k
        bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
362
7.99k
        bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
363
7.99k
    }
364
365
9.79k
    return srcSize;
366
9.87k
}
367
368
MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
369
1.04M
{
370
1.04M
    const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
371
1.04M
    return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
372
1.04M
}
373
374
/*! BIT_lookBitsFast :
375
*   unsafe version; only works if nbBits >= 1 */
376
MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
377
5.26M
{
378
5.26M
    const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
379
5.26M
    return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
380
5.26M
}
381
382
MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
383
6.31M
{
384
6.31M
    bitD->bitsConsumed += nbBits;
385
6.31M
}
386
387
MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
388
1.04M
{
389
1.04M
    size_t value = BIT_lookBits(bitD, nbBits);
390
1.04M
    BIT_skipBits(bitD, nbBits);
391
1.04M
    return value;
392
1.04M
}
393
394
/*!BIT_readBitsFast :
395
*  unsafe version; only works if nbBits >= 1 */
396
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
397
4.59k
{
398
4.59k
    size_t value = BIT_lookBitsFast(bitD, nbBits);
399
4.59k
    BIT_skipBits(bitD, nbBits);
400
4.59k
    return value;
401
4.59k
}
402
403
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
404
661k
{
405
661k
    if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8))  /* should never happen */
406
253
        return BIT_DStream_overflow;
407
408
660k
    if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
409
495k
    {
410
495k
        bitD->ptr -= bitD->bitsConsumed >> 3;
411
495k
        bitD->bitsConsumed &= 7;
412
495k
        bitD->bitContainer = MEM_readLEST(bitD->ptr);
413
495k
        return BIT_DStream_unfinished;
414
495k
    }
415
165k
    if (bitD->ptr == bitD->start)
416
139k
    {
417
139k
        if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
418
32.9k
        return BIT_DStream_completed;
419
139k
    }
420
25.7k
    {
421
25.7k
        U32 nbBytes = bitD->bitsConsumed >> 3;
422
25.7k
        BIT_DStream_status result = BIT_DStream_unfinished;
423
25.7k
        if (bitD->ptr - nbBytes < bitD->start)
424
881
        {
425
881
            nbBytes = (U32)(bitD->ptr - bitD->start);  /* ptr > start */
426
881
            result = BIT_DStream_endOfBuffer;
427
881
        }
428
25.7k
        bitD->ptr -= nbBytes;
429
25.7k
        bitD->bitsConsumed -= nbBytes*8;
430
25.7k
        bitD->bitContainer = MEM_readLEST(bitD->ptr);   /* reminder : srcSize > sizeof(bitD) */
431
25.7k
        return result;
432
165k
    }
433
165k
}
434
435
/*! BIT_endOfDStream
436
*   @return Tells if DStream has reached its exact end
437
*/
438
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
439
18.2k
{
440
18.2k
    return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
441
18.2k
}
442
443
#if defined (__cplusplus)
444
}
445
#endif
446
447
#endif /* BITSTREAM_H_MODULE */
448
/* ******************************************************************
449
   Error codes and messages
450
   Copyright (C) 2013-2015, Yann Collet
451
452
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
453
454
   Redistribution and use in source and binary forms, with or without
455
   modification, are permitted provided that the following conditions are
456
   met:
457
458
       * Redistributions of source code must retain the above copyright
459
   notice, this list of conditions and the following disclaimer.
460
       * Redistributions in binary form must reproduce the above
461
   copyright notice, this list of conditions and the following disclaimer
462
   in the documentation and/or other materials provided with the
463
   distribution.
464
465
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
466
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
467
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
468
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
469
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
470
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
471
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
472
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
473
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
474
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
475
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
476
477
   You can contact the author at :
478
   - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
479
   - Public forum : https://groups.google.com/forum/#!forum/lz4c
480
****************************************************************** */
481
#ifndef ERROR_H_MODULE
482
#define ERROR_H_MODULE
483
484
#if defined (__cplusplus)
485
extern "C" {
486
#endif
487
488
489
/******************************************
490
*  Compiler-specific
491
******************************************/
492
#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
493
#  define ERR_STATIC static inline
494
#elif defined(_MSC_VER)
495
#  define ERR_STATIC static __inline
496
#elif defined(__GNUC__)
497
#  define ERR_STATIC static __attribute__((unused))
498
#else
499
#  define ERR_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
500
#endif
501
502
503
/******************************************
504
*  Error Management
505
******************************************/
506
#define PREFIX(name) ZSTD_error_##name
507
508
#define ERROR(name) (size_t)-PREFIX(name)
509
510
#define ERROR_LIST(ITEM) \
511
        ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
512
        ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
513
        ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
514
        ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
515
        ITEM(PREFIX(maxCode))
516
517
#define ERROR_GENERATE_ENUM(ENUM) ENUM,
518
typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes;  /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */
519
520
#define ERROR_CONVERTTOSTRING(STRING) #STRING,
521
#define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR)
522
static const char* ERR_strings[] = { ERROR_LIST(ERROR_GENERATE_STRING) };
523
524
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
525
526
ERR_STATIC const char* ERR_getErrorName(size_t code)
527
{
528
    static const char* codeError = "Unspecified error code";
529
    if (ERR_isError(code)) return ERR_strings[-(int)(code)];
530
    return codeError;
531
}
532
533
534
#if defined (__cplusplus)
535
}
536
#endif
537
538
#endif /* ERROR_H_MODULE */
539
/*
540
Constructor and Destructor of type FSE_CTable
541
    Note that its size depends on 'tableLog' and 'maxSymbolValue' */
542
typedef unsigned FSE_CTable;   /* don't allocate that. It's just a way to be more restrictive than void* */
543
typedef unsigned FSE_DTable;   /* don't allocate that. It's just a way to be more restrictive than void* */
544
545
546
/* ******************************************************************
547
   FSE : Finite State Entropy coder
548
   header file for static linking (only)
549
   Copyright (C) 2013-2015, Yann Collet
550
551
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
552
553
   Redistribution and use in source and binary forms, with or without
554
   modification, are permitted provided that the following conditions are
555
   met:
556
557
       * Redistributions of source code must retain the above copyright
558
   notice, this list of conditions and the following disclaimer.
559
       * Redistributions in binary form must reproduce the above
560
   copyright notice, this list of conditions and the following disclaimer
561
   in the documentation and/or other materials provided with the
562
   distribution.
563
564
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
565
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
566
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
567
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
568
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
569
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
570
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
571
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
572
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
573
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
574
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
575
576
   You can contact the author at :
577
   - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
578
   - Public forum : https://groups.google.com/forum/#!forum/lz4c
579
****************************************************************** */
580
#if defined (__cplusplus)
581
extern "C" {
582
#endif
583
584
585
/******************************************
586
*  Static allocation
587
******************************************/
588
/* FSE buffer bounds */
589
#define FSE_NCOUNTBOUND 512
590
#define FSE_BLOCKBOUND(size) (size + (size>>7))
591
#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size))   /* Macro version, useful for static allocation */
592
593
/* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */
594
#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue)   (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
595
#define FSE_DTABLE_SIZE_U32(maxTableLog)                   (1 + (1<<maxTableLog))
596
597
598
/******************************************
599
*  FSE advanced API
600
******************************************/
601
static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
602
/* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
603
604
static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
605
/* build a fake FSE_DTable, designed to always generate the same symbolValue */
606
607
608
/******************************************
609
*  FSE symbol decompression API
610
******************************************/
611
typedef struct
612
{
613
    size_t      state;
614
    const void* table;   /* precise table may vary, depending on U16 */
615
} FSE_DState_t;
616
617
618
static void     FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
619
620
static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
621
622
static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
623
624
625
/******************************************
626
*  FSE unsafe API
627
******************************************/
628
static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
629
/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
630
631
632
/******************************************
633
*  Implementation of inline functions
634
******************************************/
635
636
/* decompression */
637
638
typedef struct {
639
    U16 tableLog;
640
    U16 fastMode;
641
} FSE_DTableHeader;   /* sizeof U32 */
642
643
typedef struct
644
{
645
    unsigned short newState;
646
    unsigned char  symbol;
647
    unsigned char  nbBits;
648
} FSE_decode_t;   /* size == U32 */
649
650
MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
651
16.3k
{
652
16.3k
    FSE_DTableHeader DTableH;
653
16.3k
    memcpy(&DTableH, dt, sizeof(DTableH));
654
16.3k
    DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog);
655
16.3k
    BIT_reloadDStream(bitD);
656
16.3k
    DStatePtr->table = dt + 1;
657
16.3k
}
658
659
MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
660
777k
{
661
777k
    const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
662
777k
    const U32  nbBits = DInfo.nbBits;
663
777k
    BYTE symbol = DInfo.symbol;
664
777k
    size_t lowBits = BIT_readBits(bitD, nbBits);
665
666
777k
    DStatePtr->state = DInfo.newState + lowBits;
667
777k
    return symbol;
668
777k
}
669
670
MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
671
4.59k
{
672
4.59k
    const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
673
4.59k
    const U32 nbBits = DInfo.nbBits;
674
4.59k
    BYTE symbol = DInfo.symbol;
675
4.59k
    size_t lowBits = BIT_readBitsFast(bitD, nbBits);
676
677
4.59k
    DStatePtr->state = DInfo.newState + lowBits;
678
4.59k
    return symbol;
679
4.59k
}
680
681
MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
682
1.28k
{
683
1.28k
    return DStatePtr->state == 0;
684
1.28k
}
685
686
687
#if defined (__cplusplus)
688
}
689
#endif
690
/* ******************************************************************
691
   Huff0 : Huffman coder, part of New Generation Entropy library
692
   header file for static linking (only)
693
   Copyright (C) 2013-2015, Yann Collet
694
695
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
696
697
   Redistribution and use in source and binary forms, with or without
698
   modification, are permitted provided that the following conditions are
699
   met:
700
701
       * Redistributions of source code must retain the above copyright
702
   notice, this list of conditions and the following disclaimer.
703
       * Redistributions in binary form must reproduce the above
704
   copyright notice, this list of conditions and the following disclaimer
705
   in the documentation and/or other materials provided with the
706
   distribution.
707
708
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
709
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
710
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
711
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
712
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
713
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
714
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
715
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
716
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
717
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
718
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
719
720
   You can contact the author at :
721
   - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
722
   - Public forum : https://groups.google.com/forum/#!forum/lz4c
723
****************************************************************** */
724
725
#if defined (__cplusplus)
726
extern "C" {
727
#endif
728
729
/******************************************
730
*  Static allocation macros
731
******************************************/
732
/* Huff0 buffer bounds */
733
#define HUF_CTABLEBOUND 129
734
#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8)   /* only true if incompressible pre-filtered with fast heuristic */
735
#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size))   /* Macro version, useful for static allocation */
736
737
/* static allocation of Huff0's DTable */
738
#define HUF_DTABLE_SIZE(maxTableLog)   (1 + (1<<maxTableLog))  /* nb Cells; use unsigned short for X2, unsigned int for X4 */
739
#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
740
1.17k
        unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
741
#define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
742
117
        unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
743
#define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
744
88
        unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
745
746
747
/******************************************
748
*  Advanced functions
749
******************************************/
750
static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* single-symbol decoder */
751
static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* double-symbols decoder */
752
static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* quad-symbols decoder */
753
754
755
#if defined (__cplusplus)
756
}
757
#endif
758
759
/*
760
    zstd - standard compression library
761
    Header File
762
    Copyright (C) 2014-2015, Yann Collet.
763
764
    BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
765
766
    Redistribution and use in source and binary forms, with or without
767
    modification, are permitted provided that the following conditions are
768
    met:
769
    * Redistributions of source code must retain the above copyright
770
    notice, this list of conditions and the following disclaimer.
771
    * Redistributions in binary form must reproduce the above
772
    copyright notice, this list of conditions and the following disclaimer
773
    in the documentation and/or other materials provided with the
774
    distribution.
775
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
776
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
777
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
778
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
779
    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
780
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
781
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
782
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
783
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
784
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
785
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
786
787
    You can contact the author at :
788
    - zstd source repository : https://github.com/Cyan4973/zstd
789
    - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
790
*/
791
792
#if defined (__cplusplus)
793
extern "C" {
794
#endif
795
796
/* *************************************
797
*  Includes
798
***************************************/
799
#include <stddef.h>   /* size_t */
800
801
802
/* *************************************
803
*  Version
804
***************************************/
805
#define ZSTD_VERSION_MAJOR    0    /* for breaking interface changes  */
806
#define ZSTD_VERSION_MINOR    2    /* for new (non-breaking) interface capabilities */
807
#define ZSTD_VERSION_RELEASE  2    /* for tweaks, bug-fixes, or development */
808
#define ZSTD_VERSION_NUMBER  (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
809
810
811
/* *************************************
812
*  Advanced functions
813
***************************************/
814
typedef struct ZSTD_CCtx_s ZSTD_CCtx;   /* incomplete type */
815
816
#if defined (__cplusplus)
817
}
818
#endif
819
/*
820
    zstd - standard compression library
821
    Header File for static linking only
822
    Copyright (C) 2014-2015, Yann Collet.
823
824
    BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
825
826
    Redistribution and use in source and binary forms, with or without
827
    modification, are permitted provided that the following conditions are
828
    met:
829
    * Redistributions of source code must retain the above copyright
830
    notice, this list of conditions and the following disclaimer.
831
    * Redistributions in binary form must reproduce the above
832
    copyright notice, this list of conditions and the following disclaimer
833
    in the documentation and/or other materials provided with the
834
    distribution.
835
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
836
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
837
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
838
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
839
    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
840
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
841
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
842
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
843
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
844
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
845
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
846
847
    You can contact the author at :
848
    - zstd source repository : https://github.com/Cyan4973/zstd
849
    - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
850
*/
851
852
/* The objects defined into this file should be considered experimental.
853
 * They are not labelled stable, as their prototype may change in the future.
854
 * You can use them for tests, provide feedback, or if you can endure risk of future changes.
855
 */
856
857
#if defined (__cplusplus)
858
extern "C" {
859
#endif
860
861
/* *************************************
862
*  Streaming functions
863
***************************************/
864
865
typedef struct ZSTDv02_Dctx_s ZSTD_DCtx;
866
867
/*
868
  Use above functions alternatively.
869
  ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
870
  ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
871
  Result is the number of bytes regenerated within 'dst'.
872
  It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
873
*/
874
875
/* *************************************
876
*  Prefix - version detection
877
***************************************/
878
3.65k
#define ZSTD_magicNumber 0xFD2FB522   /* v0.2 (current)*/
879
880
881
#if defined (__cplusplus)
882
}
883
#endif
884
/* ******************************************************************
885
   FSE : Finite State Entropy coder
886
   Copyright (C) 2013-2015, Yann Collet.
887
888
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
889
890
   Redistribution and use in source and binary forms, with or without
891
   modification, are permitted provided that the following conditions are
892
   met:
893
894
       * Redistributions of source code must retain the above copyright
895
   notice, this list of conditions and the following disclaimer.
896
       * Redistributions in binary form must reproduce the above
897
   copyright notice, this list of conditions and the following disclaimer
898
   in the documentation and/or other materials provided with the
899
   distribution.
900
901
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
902
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
903
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
904
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
905
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
906
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
907
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
908
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
909
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
910
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
911
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
912
913
    You can contact the author at :
914
    - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
915
    - Public forum : https://groups.google.com/forum/#!forum/lz4c
916
****************************************************************** */
917
918
#ifndef FSE_COMMONDEFS_ONLY
919
920
/****************************************************************
921
*  Tuning parameters
922
****************************************************************/
923
/* MEMORY_USAGE :
924
*  Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
925
*  Increasing memory usage improves compression ratio
926
*  Reduced memory usage can improve speed, due to cache effect
927
*  Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
928
12.3k
#define FSE_MAX_MEMORY_USAGE 14
929
#define FSE_DEFAULT_MEMORY_USAGE 13
930
931
/* FSE_MAX_SYMBOL_VALUE :
932
*  Maximum symbol value authorized.
933
*  Required for proper stack allocation */
934
3.31k
#define FSE_MAX_SYMBOL_VALUE 255
935
936
937
/****************************************************************
938
*  template functions type & suffix
939
****************************************************************/
940
898k
#define FSE_FUNCTION_TYPE BYTE
941
#define FSE_FUNCTION_EXTENSION
942
943
944
/****************************************************************
945
*  Byte symbol type
946
****************************************************************/
947
#endif   /* !FSE_COMMONDEFS_ONLY */
948
949
950
/****************************************************************
951
*  Compiler specifics
952
****************************************************************/
953
#ifdef _MSC_VER    /* Visual Studio */
954
#  define FORCE_INLINE static __forceinline
955
#  include <intrin.h>                    /* For Visual 2005 */
956
#  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
957
#  pragma warning(disable : 4214)        /* disable: C4214: non-int bitfields */
958
#else
959
#  if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
960
#    ifdef __GNUC__
961
#      define FORCE_INLINE static inline __attribute__((always_inline))
962
#    else
963
#      define FORCE_INLINE static inline
964
#    endif
965
#  else
966
#    define FORCE_INLINE static
967
#  endif /* __STDC_VERSION__ */
968
#endif
969
970
971
/****************************************************************
972
*  Includes
973
****************************************************************/
974
#include <stdlib.h>     /* malloc, free, qsort */
975
#include <string.h>     /* memcpy, memset */
976
#include <stdio.h>      /* printf (debug) */
977
978
/****************************************************************
979
*  Constants
980
*****************************************************************/
981
12.3k
#define FSE_MAX_TABLELOG  (FSE_MAX_MEMORY_USAGE-2)
982
#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
983
#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
984
#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
985
3.16k
#define FSE_MIN_TABLELOG 5
986
987
3.16k
#define FSE_TABLELOG_ABSOLUTE_MAX 15
988
#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
989
#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
990
#endif
991
992
993
/****************************************************************
994
*  Error Management
995
****************************************************************/
996
#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; }   /* use only *after* variable declarations */
997
998
999
/****************************************************************
1000
*  Complex types
1001
****************************************************************/
1002
typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
1003
1004
1005
/****************************************************************
1006
*  Templates
1007
****************************************************************/
1008
/*
1009
  designed to be included
1010
  for type-specific functions (template emulation in C)
1011
  Objective is to write these functions only once, for improved maintenance
1012
*/
1013
1014
/* safety checks */
1015
#ifndef FSE_FUNCTION_EXTENSION
1016
#  error "FSE_FUNCTION_EXTENSION must be defined"
1017
#endif
1018
#ifndef FSE_FUNCTION_TYPE
1019
#  error "FSE_FUNCTION_TYPE must be defined"
1020
#endif
1021
1022
/* Function names */
1023
#define FSE_CAT(X,Y) X##Y
1024
#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1025
#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1026
1027
1028
/* Function templates */
1029
1030
3.09k
#define FSE_DECODE_TYPE FSE_decode_t
1031
1032
3.09k
static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1033
1034
static size_t FSE_buildDTable
1035
(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1036
3.09k
{
1037
3.09k
    void* ptr = dt+1;
1038
3.09k
    FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)ptr;
1039
3.09k
    FSE_DTableHeader DTableH;
1040
3.09k
    const U32 tableSize = 1 << tableLog;
1041
3.09k
    const U32 tableMask = tableSize-1;
1042
3.09k
    const U32 step = FSE_tableStep(tableSize);
1043
3.09k
    U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1044
3.09k
    U32 position = 0;
1045
3.09k
    U32 highThreshold = tableSize-1;
1046
3.09k
    const S16 largeLimit= (S16)(1 << (tableLog-1));
1047
3.09k
    U32 noLarge = 1;
1048
3.09k
    U32 s;
1049
1050
    /* Sanity Checks */
1051
3.09k
    if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1052
3.09k
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1053
1054
    /* Init, lay down lowprob symbols */
1055
3.08k
    DTableH.tableLog = (U16)tableLog;
1056
28.4k
    for (s=0; s<=maxSymbolValue; s++)
1057
25.3k
    {
1058
25.3k
        if (normalizedCounter[s]==-1)
1059
10.8k
        {
1060
10.8k
            tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1061
10.8k
            symbolNext[s] = 1;
1062
10.8k
        }
1063
14.4k
        else
1064
14.4k
        {
1065
14.4k
            if (normalizedCounter[s] >= largeLimit) noLarge=0;
1066
14.4k
            symbolNext[s] = normalizedCounter[s];
1067
14.4k
        }
1068
25.3k
    }
1069
1070
    /* Spread symbols */
1071
28.4k
    for (s=0; s<=maxSymbolValue; s++)
1072
25.3k
    {
1073
25.3k
        int i;
1074
912k
        for (i=0; i<normalizedCounter[s]; i++)
1075
887k
        {
1076
887k
            tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1077
887k
            position = (position + step) & tableMask;
1078
897k
            while (position > highThreshold) position = (position + step) & tableMask;   /* lowprob area */
1079
887k
        }
1080
25.3k
    }
1081
1082
3.08k
    if (position!=0) return ERROR(GENERIC);   /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1083
1084
    /* Build Decoding table */
1085
3.08k
    {
1086
3.08k
        U32 i;
1087
901k
        for (i=0; i<tableSize; i++)
1088
898k
        {
1089
898k
            FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);
1090
898k
            U16 nextState = symbolNext[symbol]++;
1091
898k
            tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
1092
898k
            tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1093
898k
        }
1094
3.08k
    }
1095
1096
3.08k
    DTableH.fastMode = (U16)noLarge;
1097
3.08k
    memcpy(dt, &DTableH, sizeof(DTableH));   /* memcpy(), to avoid strict aliasing warnings */
1098
3.08k
    return 0;
1099
3.08k
}
1100
1101
1102
#ifndef FSE_COMMONDEFS_ONLY
1103
/******************************************
1104
*  FSE helper functions
1105
******************************************/
1106
3.78k
static unsigned FSE_isError(size_t code) { return ERR_isError(code); }
1107
1108
1109
/****************************************************************
1110
*  FSE NCount encoding-decoding
1111
****************************************************************/
1112
static short FSE_abs(short a)
1113
23.4k
{
1114
23.4k
    return (short)(a<0 ? -a : a);
1115
23.4k
}
1116
1117
static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1118
                 const void* headerBuffer, size_t hbSize)
1119
3.18k
{
1120
3.18k
    const BYTE* const istart = (const BYTE*) headerBuffer;
1121
3.18k
    const BYTE* const iend = istart + hbSize;
1122
3.18k
    const BYTE* ip = istart;
1123
3.18k
    int nbBits;
1124
3.18k
    int remaining;
1125
3.18k
    int threshold;
1126
3.18k
    U32 bitStream;
1127
3.18k
    int bitCount;
1128
3.18k
    unsigned charnum = 0;
1129
3.18k
    int previous0 = 0;
1130
1131
3.18k
    if (hbSize < 4) return ERROR(srcSize_wrong);
1132
3.16k
    bitStream = MEM_readLE32(ip);
1133
3.16k
    nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG;   /* extract tableLog */
1134
3.16k
    if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1135
3.15k
    bitStream >>= 4;
1136
3.15k
    bitCount = 4;
1137
3.15k
    *tableLogPtr = nbBits;
1138
3.15k
    remaining = (1<<nbBits)+1;
1139
3.15k
    threshold = 1<<nbBits;
1140
3.15k
    nbBits++;
1141
1142
26.6k
    while ((remaining>1) && (charnum<=*maxSVPtr))
1143
23.4k
    {
1144
23.4k
        if (previous0)
1145
1.86k
        {
1146
1.86k
            unsigned n0 = charnum;
1147
2.33k
            while ((bitStream & 0xFFFF) == 0xFFFF)
1148
467
            {
1149
467
                n0+=24;
1150
467
                if (ip < iend-5)
1151
429
                {
1152
429
                    ip+=2;
1153
429
                    bitStream = MEM_readLE32(ip) >> bitCount;
1154
429
                }
1155
38
                else
1156
38
                {
1157
38
                    bitStream >>= 16;
1158
38
                    bitCount+=16;
1159
38
                }
1160
467
            }
1161
2.12k
            while ((bitStream & 3) == 3)
1162
255
            {
1163
255
                n0+=3;
1164
255
                bitStream>>=2;
1165
255
                bitCount+=2;
1166
255
            }
1167
1.86k
            n0 += bitStream & 3;
1168
1.86k
            bitCount += 2;
1169
1.86k
            if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1170
7.55k
            while (charnum < n0) normalizedCounter[charnum++] = 0;
1171
1.85k
            if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1172
1.00k
            {
1173
1.00k
                ip += bitCount>>3;
1174
1.00k
                bitCount &= 7;
1175
1.00k
                bitStream = MEM_readLE32(ip) >> bitCount;
1176
1.00k
            }
1177
855
            else
1178
855
                bitStream >>= 2;
1179
1.85k
        }
1180
23.4k
        {
1181
23.4k
            const short max = (short)((2*threshold-1)-remaining);
1182
23.4k
            short count;
1183
1184
23.4k
            if ((bitStream & (threshold-1)) < (U32)max)
1185
15.9k
            {
1186
15.9k
                count = (short)(bitStream & (threshold-1));
1187
15.9k
                bitCount   += nbBits-1;
1188
15.9k
            }
1189
7.45k
            else
1190
7.45k
            {
1191
7.45k
                count = (short)(bitStream & (2*threshold-1));
1192
7.45k
                if (count >= threshold) count -= max;
1193
7.45k
                bitCount   += nbBits;
1194
7.45k
            }
1195
1196
23.4k
            count--;   /* extra accuracy */
1197
23.4k
            remaining -= FSE_abs(count);
1198
23.4k
            normalizedCounter[charnum++] = count;
1199
23.4k
            previous0 = !count;
1200
44.1k
            while (remaining < threshold)
1201
20.7k
            {
1202
20.7k
                nbBits--;
1203
20.7k
                threshold >>= 1;
1204
20.7k
            }
1205
1206
23.4k
            {
1207
23.4k
                if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1208
20.3k
                {
1209
20.3k
                    ip += bitCount>>3;
1210
20.3k
                    bitCount &= 7;
1211
20.3k
                }
1212
3.05k
                else
1213
3.05k
                {
1214
3.05k
                    bitCount -= (int)(8 * (iend - 4 - ip));
1215
3.05k
                    ip = iend - 4;
1216
3.05k
                }
1217
23.4k
                bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1218
23.4k
            }
1219
23.4k
        }
1220
23.4k
    }
1221
3.14k
    if (remaining != 1) return ERROR(GENERIC);
1222
3.13k
    *maxSVPtr = charnum-1;
1223
1224
3.13k
    ip += (bitCount+7)>>3;
1225
3.13k
    if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1226
3.10k
    return ip-istart;
1227
3.13k
}
1228
1229
1230
/*********************************************************
1231
*  Decompression (Byte symbols)
1232
*********************************************************/
1233
static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
1234
3.79k
{
1235
3.79k
    void* ptr = dt;
1236
3.79k
    FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1237
3.79k
    FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1;   /* because dt is unsigned */
1238
1239
3.79k
    DTableH->tableLog = 0;
1240
3.79k
    DTableH->fastMode = 0;
1241
1242
3.79k
    cell->newState = 0;
1243
3.79k
    cell->symbol = symbolValue;
1244
3.79k
    cell->nbBits = 0;
1245
1246
3.79k
    return 0;
1247
3.79k
}
1248
1249
1250
static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
1251
9.48k
{
1252
9.48k
    void* ptr = dt;
1253
9.48k
    FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1254
9.48k
    FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1;   /* because dt is unsigned */
1255
9.48k
    const unsigned tableSize = 1 << nbBits;
1256
9.48k
    const unsigned tableMask = tableSize - 1;
1257
9.48k
    const unsigned maxSymbolValue = tableMask;
1258
9.48k
    unsigned s;
1259
1260
    /* Sanity checks */
1261
9.48k
    if (nbBits < 1) return ERROR(GENERIC);         /* min size */
1262
1263
    /* Build Decoding Table */
1264
9.48k
    DTableH->tableLog = (U16)nbBits;
1265
9.48k
    DTableH->fastMode = 1;
1266
752k
    for (s=0; s<=maxSymbolValue; s++)
1267
743k
    {
1268
743k
        dinfo[s].newState = 0;
1269
743k
        dinfo[s].symbol = (BYTE)s;
1270
743k
        dinfo[s].nbBits = (BYTE)nbBits;
1271
743k
    }
1272
1273
9.48k
    return 0;
1274
9.48k
}
1275
1276
FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1277
          void* dst, size_t maxDstSize,
1278
    const void* cSrc, size_t cSrcSize,
1279
    const FSE_DTable* dt, const unsigned fast)
1280
188
{
1281
188
    BYTE* const ostart = (BYTE*) dst;
1282
188
    BYTE* op = ostart;
1283
188
    BYTE* const omax = op + maxDstSize;
1284
188
    BYTE* const olimit = omax-3;
1285
1286
188
    BIT_DStream_t bitD;
1287
188
    FSE_DState_t state1;
1288
188
    FSE_DState_t state2;
1289
188
    size_t errorCode;
1290
1291
    /* Init */
1292
188
    errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);   /* replaced last arg by maxCompressed Size */
1293
188
    if (FSE_isError(errorCode)) return errorCode;
1294
1295
180
    FSE_initDState(&state1, &bitD, dt);
1296
180
    FSE_initDState(&state2, &bitD, dt);
1297
1298
20.9k
#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
1299
1300
    /* 4 symbols per loop */
1301
3.25k
    for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4)
1302
3.07k
    {
1303
3.07k
        op[0] = FSE_GETSYMBOL(&state1);
1304
1305
3.07k
        if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */
1306
0
            BIT_reloadDStream(&bitD);
1307
1308
3.07k
        op[1] = FSE_GETSYMBOL(&state2);
1309
1310
3.07k
        if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */
1311
0
            { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
1312
1313
3.07k
        op[2] = FSE_GETSYMBOL(&state1);
1314
1315
3.07k
        if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */
1316
0
            BIT_reloadDStream(&bitD);
1317
1318
3.07k
        op[3] = FSE_GETSYMBOL(&state2);
1319
3.07k
    }
1320
1321
    /* tail */
1322
    /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
1323
4.47k
    while (1)
1324
4.47k
    {
1325
4.47k
        if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )
1326
87
            break;
1327
1328
4.38k
        *op++ = FSE_GETSYMBOL(&state1);
1329
1330
4.38k
        if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )
1331
93
            break;
1332
1333
4.29k
        *op++ = FSE_GETSYMBOL(&state2);
1334
4.29k
    }
1335
1336
    /* end ? */
1337
180
    if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2))
1338
70
        return op-ostart;
1339
1340
110
    if (op==omax) return ERROR(dstSize_tooSmall);   /* dst buffer is full, but cSrc unfinished */
1341
1342
82
    return ERROR(corruption_detected);
1343
110
}
1344
1345
1346
static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1347
                            const void* cSrc, size_t cSrcSize,
1348
                            const FSE_DTable* dt)
1349
188
{
1350
188
    FSE_DTableHeader DTableH;
1351
188
    memcpy(&DTableH, dt, sizeof(DTableH));
1352
1353
    /* select fast mode (static) */
1354
188
    if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
1355
124
    return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1356
188
}
1357
1358
1359
static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1360
224
{
1361
224
    const BYTE* const istart = (const BYTE*)cSrc;
1362
224
    const BYTE* ip = istart;
1363
224
    short counting[FSE_MAX_SYMBOL_VALUE+1];
1364
224
    DTable_max_t dt;   /* Static analyzer seems unable to understand this table will be properly initialized later */
1365
224
    unsigned tableLog;
1366
224
    unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1367
224
    size_t errorCode;
1368
1369
224
    if (cSrcSize<2) return ERROR(srcSize_wrong);   /* too small input size */
1370
1371
    /* normal FSE decoding mode */
1372
221
    errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1373
221
    if (FSE_isError(errorCode)) return errorCode;
1374
192
    if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);   /* too small input size */
1375
189
    ip += errorCode;
1376
189
    cSrcSize -= errorCode;
1377
1378
189
    errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
1379
189
    if (FSE_isError(errorCode)) return errorCode;
1380
1381
    /* always return, even if it is an error code */
1382
188
    return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
1383
189
}
1384
1385
1386
1387
#endif   /* FSE_COMMONDEFS_ONLY */
1388
/* ******************************************************************
1389
   Huff0 : Huffman coder, part of New Generation Entropy library
1390
   Copyright (C) 2013-2015, Yann Collet.
1391
1392
   BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1393
1394
   Redistribution and use in source and binary forms, with or without
1395
   modification, are permitted provided that the following conditions are
1396
   met:
1397
1398
       * Redistributions of source code must retain the above copyright
1399
   notice, this list of conditions and the following disclaimer.
1400
       * Redistributions in binary form must reproduce the above
1401
   copyright notice, this list of conditions and the following disclaimer
1402
   in the documentation and/or other materials provided with the
1403
   distribution.
1404
1405
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1406
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1407
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1408
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1409
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1410
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1411
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1412
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1413
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1414
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1415
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1416
1417
    You can contact the author at :
1418
    - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1419
    - Public forum : https://groups.google.com/forum/#!forum/lz4c
1420
****************************************************************** */
1421
1422
/****************************************************************
1423
*  Compiler specifics
1424
****************************************************************/
1425
#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1426
/* inline is defined */
1427
#elif defined(_MSC_VER)
1428
#  define inline __inline
1429
#else
1430
#  define inline /* disable inline */
1431
#endif
1432
1433
1434
#ifdef _MSC_VER    /* Visual Studio */
1435
#  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
1436
#endif
1437
1438
1439
/****************************************************************
1440
*  Includes
1441
****************************************************************/
1442
#include <stdlib.h>     /* malloc, free, qsort */
1443
#include <string.h>     /* memcpy, memset */
1444
#include <stdio.h>      /* printf (debug) */
1445
1446
/****************************************************************
1447
*  Error Management
1448
****************************************************************/
1449
1.28k
#define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; }   /* use only *after* variable declarations */
1450
1451
1452
/******************************************
1453
*  Helper functions
1454
******************************************/
1455
8.52k
static unsigned HUF_isError(size_t code) { return ERR_isError(code); }
1456
1457
132k
#define HUF_ABSOLUTEMAX_TABLELOG  16   /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
1458
0
#define HUF_MAX_TABLELOG  12           /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
1459
#define HUF_DEFAULT_TABLELOG  HUF_MAX_TABLELOG   /* tableLog by default, when not specified */
1460
1.37k
#define HUF_MAX_SYMBOL_VALUE 255
1461
#if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)
1462
#  error "HUF_MAX_TABLELOG is too large !"
1463
#endif
1464
1465
1466
1467
/*********************************************************
1468
*  Huff0 : Huffman block decompression
1469
*********************************************************/
1470
typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2;   /* single-symbol decoding */
1471
1472
typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4;  /* double-symbols decoding */
1473
1474
typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1475
1476
/*! HUF_readStats
1477
    Read compact Huffman tree, saved by HUF_writeCTable
1478
    @huffWeight : destination buffer
1479
    @return : size read from `src`
1480
*/
1481
static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1482
                            U32* nbSymbolsPtr, U32* tableLogPtr,
1483
                            const void* src, size_t srcSize)
1484
1.37k
{
1485
1.37k
    U32 weightTotal;
1486
1.37k
    U32 tableLog;
1487
1.37k
    const BYTE* ip = (const BYTE*) src;
1488
1.37k
    size_t iSize;
1489
1.37k
    size_t oSize;
1490
1.37k
    U32 n;
1491
1492
1.37k
    if (!srcSize) return ERROR(srcSize_wrong);
1493
1.37k
    iSize = ip[0];
1494
    //memset(huffWeight, 0, hwSize);   /* is not necessary, even though some analyzer complain ... */
1495
1496
1.37k
    if (iSize >= 128)  /* special header */
1497
1.14k
    {
1498
1.14k
        if (iSize >= (242))   /* RLE */
1499
1.03k
        {
1500
1.03k
            static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1501
1.03k
            oSize = l[iSize-242];
1502
1.03k
            memset(huffWeight, 1, hwSize);
1503
1.03k
            iSize = 0;
1504
1.03k
        }
1505
112
        else   /* Incompressible */
1506
112
        {
1507
112
            oSize = iSize - 127;
1508
112
            iSize = ((oSize+1)/2);
1509
112
            if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1510
104
            if (oSize >= hwSize) return ERROR(corruption_detected);
1511
104
            ip += 1;
1512
2.68k
            for (n=0; n<oSize; n+=2)
1513
2.58k
            {
1514
2.58k
                huffWeight[n]   = ip[n/2] >> 4;
1515
2.58k
                huffWeight[n+1] = ip[n/2] & 15;
1516
2.58k
            }
1517
104
        }
1518
1.14k
    }
1519
229
    else  /* header compressed with FSE (normal case) */
1520
229
    {
1521
229
        if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1522
224
        oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize);   /* max (hwSize-1) values decoded, as last one is implied */
1523
224
        if (FSE_isError(oSize)) return oSize;
1524
224
    }
1525
1526
    /* collect weight stats */
1527
1.20k
    memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
1528
1.20k
    weightTotal = 0;
1529
131k
    for (n=0; n<oSize; n++)
1530
130k
    {
1531
130k
        if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1532
130k
        rankStats[huffWeight[n]]++;
1533
130k
        weightTotal += (1 << huffWeight[n]) >> 1;
1534
130k
    }
1535
1.20k
    if (weightTotal == 0) return ERROR(corruption_detected);
1536
1537
    /* get last non-null symbol weight (implied, total must be 2^n) */
1538
1.20k
    tableLog = BIT_highbit32(weightTotal) + 1;
1539
1.20k
    if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1540
1.19k
    {
1541
1.19k
        U32 total = 1 << tableLog;
1542
1.19k
        U32 rest = total - weightTotal;
1543
1.19k
        U32 verif = 1 << BIT_highbit32(rest);
1544
1.19k
        U32 lastWeight = BIT_highbit32(rest) + 1;
1545
1.19k
        if (verif != rest) return ERROR(corruption_detected);    /* last value must be a clean power of 2 */
1546
1.19k
        huffWeight[oSize] = (BYTE)lastWeight;
1547
1.19k
        rankStats[lastWeight]++;
1548
1.19k
    }
1549
1550
    /* check tree construction validity */
1551
1.19k
    if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected);   /* by construction : at least 2 elts of rank 1, must be even */
1552
1553
    /* results */
1554
1.19k
    *nbSymbolsPtr = (U32)(oSize+1);
1555
1.19k
    *tableLogPtr = tableLog;
1556
1.19k
    return iSize+1;
1557
1.19k
}
1558
1559
1560
/**************************/
1561
/* single-symbol decoding */
1562
/**************************/
1563
1564
static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1565
1.17k
{
1566
1.17k
    BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];
1567
1.17k
    U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];   /* large enough for values from 0 to 16 */
1568
1.17k
    U32 tableLog = 0;
1569
1.17k
    const BYTE* ip = (const BYTE*) src;
1570
1.17k
    size_t iSize = ip[0];
1571
1.17k
    U32 nbSymbols = 0;
1572
1.17k
    U32 n;
1573
1.17k
    U32 nextRankStart;
1574
1.17k
    void* ptr = DTable+1;
1575
1.17k
    HUF_DEltX2* const dt = (HUF_DEltX2*)ptr;
1576
1577
1.17k
    HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16));   /* if compilation fails here, assertion is false */
1578
    //memset(huffWeight, 0, sizeof(huffWeight));   /* is not necessary, even though some analyzer complain ... */
1579
1580
1.17k
    iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1581
1.17k
    if (HUF_isError(iSize)) return iSize;
1582
1583
    /* check result */
1584
988
    if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge);   /* DTable is too small */
1585
987
    DTable[0] = (U16)tableLog;   /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */
1586
1587
    /* Prepare ranks */
1588
987
    nextRankStart = 0;
1589
8.37k
    for (n=1; n<=tableLog; n++)
1590
7.38k
    {
1591
7.38k
        U32 current = nextRankStart;
1592
7.38k
        nextRankStart += (rankVal[n] << (n-1));
1593
7.38k
        rankVal[n] = current;
1594
7.38k
    }
1595
1596
    /* fill DTable */
1597
115k
    for (n=0; n<nbSymbols; n++)
1598
114k
    {
1599
114k
        const U32 w = huffWeight[n];
1600
114k
        const U32 length = (1 << w) >> 1;
1601
114k
        U32 i;
1602
114k
        HUF_DEltX2 D;
1603
114k
        D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1604
362k
        for (i = rankVal[w]; i < rankVal[w] + length; i++)
1605
248k
            dt[i] = D;
1606
114k
        rankVal[w] += length;
1607
114k
    }
1608
1609
987
    return iSize;
1610
988
}
1611
1612
static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
1613
3.93M
{
1614
3.93M
        const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1615
3.93M
        const BYTE c = dt[val].byte;
1616
3.93M
        BIT_skipBits(Dstream, dt[val].nbBits);
1617
3.93M
        return c;
1618
3.93M
}
1619
1620
#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1621
3.93M
    *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
1622
1623
#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1624
170k
    if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
1625
170k
        HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1626
1627
#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1628
341k
    if (MEM_64bits()) \
1629
341k
        HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1630
1631
static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
1632
3.66k
{
1633
3.66k
    BYTE* const pStart = p;
1634
1635
    /* up to 4 symbols at a time */
1636
135k
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
1637
131k
    {
1638
131k
        HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1639
131k
        HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
1640
131k
        HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1641
131k
        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1642
131k
    }
1643
1644
    /* closer to the end */
1645
3.82k
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
1646
168
        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1647
1648
    /* no more data to retrieve from bitstream, hence no need to reload */
1649
3.25M
    while (p < pEnd)
1650
3.25M
        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1651
1652
3.66k
    return pEnd-pStart;
1653
3.66k
}
1654
1655
1656
static size_t HUF_decompress4X2_usingDTable(
1657
          void* dst,  size_t dstSize,
1658
    const void* cSrc, size_t cSrcSize,
1659
    const U16* DTable)
1660
975
{
1661
975
    if (cSrcSize < 10) return ERROR(corruption_detected);   /* strict minimum : jump table + 1 byte per stream */
1662
1663
962
    {
1664
962
        const BYTE* const istart = (const BYTE*) cSrc;
1665
962
        BYTE* const ostart = (BYTE*) dst;
1666
962
        BYTE* const oend = ostart + dstSize;
1667
1668
962
        const void* ptr = DTable;
1669
962
        const HUF_DEltX2* const dt = ((const HUF_DEltX2*)ptr) +1;
1670
962
        const U32 dtLog = DTable[0];
1671
962
        size_t errorCode;
1672
1673
        /* Init */
1674
962
        BIT_DStream_t bitD1;
1675
962
        BIT_DStream_t bitD2;
1676
962
        BIT_DStream_t bitD3;
1677
962
        BIT_DStream_t bitD4;
1678
962
        const size_t length1 = MEM_readLE16(istart);
1679
962
        const size_t length2 = MEM_readLE16(istart+2);
1680
962
        const size_t length3 = MEM_readLE16(istart+4);
1681
962
        size_t length4;
1682
962
        const BYTE* const istart1 = istart + 6;  /* jumpTable */
1683
962
        const BYTE* const istart2 = istart1 + length1;
1684
962
        const BYTE* const istart3 = istart2 + length2;
1685
962
        const BYTE* const istart4 = istart3 + length3;
1686
962
        const size_t segmentSize = (dstSize+3) / 4;
1687
962
        BYTE* const opStart2 = ostart + segmentSize;
1688
962
        BYTE* const opStart3 = opStart2 + segmentSize;
1689
962
        BYTE* const opStart4 = opStart3 + segmentSize;
1690
962
        BYTE* op1 = ostart;
1691
962
        BYTE* op2 = opStart2;
1692
962
        BYTE* op3 = opStart3;
1693
962
        BYTE* op4 = opStart4;
1694
962
        U32 endSignal;
1695
1696
962
        length4 = cSrcSize - (length1 + length2 + length3 + 6);
1697
962
        if (length4 > cSrcSize) return ERROR(corruption_detected);   /* overflow */
1698
944
        errorCode = BIT_initDStream(&bitD1, istart1, length1);
1699
944
        if (HUF_isError(errorCode)) return errorCode;
1700
938
        errorCode = BIT_initDStream(&bitD2, istart2, length2);
1701
938
        if (HUF_isError(errorCode)) return errorCode;
1702
929
        errorCode = BIT_initDStream(&bitD3, istart3, length3);
1703
929
        if (HUF_isError(errorCode)) return errorCode;
1704
921
        errorCode = BIT_initDStream(&bitD4, istart4, length4);
1705
921
        if (HUF_isError(errorCode)) return errorCode;
1706
1707
        /* 16-32 symbols per loop (4-8 symbols per stream) */
1708
915
        endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1709
10.7k
        for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
1710
9.82k
        {
1711
9.82k
            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1712
9.82k
            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1713
9.82k
            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1714
9.82k
            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1715
9.82k
            HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1716
9.82k
            HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1717
9.82k
            HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1718
9.82k
            HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1719
9.82k
            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1720
9.82k
            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1721
9.82k
            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1722
9.82k
            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1723
9.82k
            HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1724
9.82k
            HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1725
9.82k
            HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1726
9.82k
            HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1727
1728
9.82k
            endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1729
9.82k
        }
1730
1731
        /* check corruption */
1732
915
        if (op1 > opStart2) return ERROR(corruption_detected);
1733
915
        if (op2 > opStart3) return ERROR(corruption_detected);
1734
915
        if (op3 > opStart4) return ERROR(corruption_detected);
1735
        /* note : op4 supposed already verified within main loop */
1736
1737
        /* finish bitStreams one by one */
1738
915
        HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1739
915
        HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1740
915
        HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1741
915
        HUF_decodeStreamX2(op4, &bitD4, oend,     dt, dtLog);
1742
1743
        /* check */
1744
915
        endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
1745
915
        if (!endSignal) return ERROR(corruption_detected);
1746
1747
        /* decoded size */
1748
783
        return dstSize;
1749
915
    }
1750
915
}
1751
1752
1753
static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1754
1.17k
{
1755
1.17k
    HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
1756
1.17k
    const BYTE* ip = (const BYTE*) cSrc;
1757
1.17k
    size_t errorCode;
1758
1759
1.17k
    errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
1760
1.17k
    if (HUF_isError(errorCode)) return errorCode;
1761
987
    if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
1762
975
    ip += errorCode;
1763
975
    cSrcSize -= errorCode;
1764
1765
975
    return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
1766
987
}
1767
1768
1769
/***************************/
1770
/* double-symbols decoding */
1771
/***************************/
1772
1773
static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
1774
                           const U32* rankValOrigin, const int minWeight,
1775
                           const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
1776
                           U32 nbBitsBaseline, U16 baseSeq)
1777
5.27k
{
1778
5.27k
    HUF_DEltX4 DElt;
1779
5.27k
    U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1780
5.27k
    U32 s;
1781
1782
    /* get pre-calculated rankVal */
1783
5.27k
    memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1784
1785
    /* fill skipped values */
1786
5.27k
    if (minWeight>1)
1787
4.88k
    {
1788
4.88k
        U32 i, skipSize = rankVal[minWeight];
1789
4.88k
        MEM_writeLE16(&(DElt.sequence), baseSeq);
1790
4.88k
        DElt.nbBits   = (BYTE)(consumed);
1791
4.88k
        DElt.length   = 1;
1792
36.3k
        for (i = 0; i < skipSize; i++)
1793
31.4k
            DTable[i] = DElt;
1794
4.88k
    }
1795
1796
    /* fill DTable */
1797
33.0k
    for (s=0; s<sortedListSize; s++)   /* note : sortedSymbols already skipped */
1798
27.7k
    {
1799
27.7k
        const U32 symbol = sortedSymbols[s].symbol;
1800
27.7k
        const U32 weight = sortedSymbols[s].weight;
1801
27.7k
        const U32 nbBits = nbBitsBaseline - weight;
1802
27.7k
        const U32 length = 1 << (sizeLog-nbBits);
1803
27.7k
        const U32 start = rankVal[weight];
1804
27.7k
        U32 i = start;
1805
27.7k
        const U32 end = start + length;
1806
1807
27.7k
        MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
1808
27.7k
        DElt.nbBits = (BYTE)(nbBits + consumed);
1809
27.7k
        DElt.length = 2;
1810
415k
        do { DTable[i++] = DElt; } while (i<end);   /* since length >= 1 */
1811
1812
27.7k
        rankVal[weight] += length;
1813
27.7k
    }
1814
5.27k
}
1815
1816
typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1];
1817
1818
static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
1819
                           const sortedSymbol_t* sortedList, const U32 sortedListSize,
1820
                           const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
1821
                           const U32 nbBitsBaseline)
1822
115
{
1823
115
    U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1824
115
    const int scaleLog = nbBitsBaseline - targetLog;   /* note : targetLog >= srcLog, hence scaleLog <= 1 */
1825
115
    const U32 minBits  = nbBitsBaseline - maxWeight;
1826
115
    U32 s;
1827
1828
115
    memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1829
1830
    /* fill DTable */
1831
6.73k
    for (s=0; s<sortedListSize; s++)
1832
6.62k
    {
1833
6.62k
        const U16 symbol = sortedList[s].symbol;
1834
6.62k
        const U32 weight = sortedList[s].weight;
1835
6.62k
        const U32 nbBits = nbBitsBaseline - weight;
1836
6.62k
        const U32 start = rankVal[weight];
1837
6.62k
        const U32 length = 1 << (targetLog-nbBits);
1838
1839
6.62k
        if (targetLog-nbBits >= minBits)   /* enough room for a second symbol */
1840
5.27k
        {
1841
5.27k
            U32 sortedRank;
1842
5.27k
            int minWeight = nbBits + scaleLog;
1843
5.27k
            if (minWeight < 1) minWeight = 1;
1844
5.27k
            sortedRank = rankStart[minWeight];
1845
5.27k
            HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
1846
5.27k
                           rankValOrigin[nbBits], minWeight,
1847
5.27k
                           sortedList+sortedRank, sortedListSize-sortedRank,
1848
5.27k
                           nbBitsBaseline, symbol);
1849
5.27k
        }
1850
1.34k
        else
1851
1.34k
        {
1852
1.34k
            U32 i;
1853
1.34k
            const U32 end = start + length;
1854
1.34k
            HUF_DEltX4 DElt;
1855
1856
1.34k
            MEM_writeLE16(&(DElt.sequence), symbol);
1857
1.34k
            DElt.nbBits   = (BYTE)(nbBits);
1858
1.34k
            DElt.length   = 1;
1859
25.3k
            for (i = start; i < end; i++)
1860
23.9k
                DTable[i] = DElt;
1861
1.34k
        }
1862
6.62k
        rankVal[weight] += length;
1863
6.62k
    }
1864
115
}
1865
1866
static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
1867
117
{
1868
117
    BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
1869
117
    sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
1870
117
    U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
1871
117
    U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
1872
117
    U32* const rankStart = rankStart0+1;
1873
117
    rankVal_t rankVal;
1874
117
    U32 tableLog, maxW, sizeOfSort, nbSymbols;
1875
117
    const U32 memLog = DTable[0];
1876
117
    const BYTE* ip = (const BYTE*) src;
1877
117
    size_t iSize = ip[0];
1878
117
    void* ptr = DTable;
1879
117
    HUF_DEltX4* const dt = ((HUF_DEltX4*)ptr) + 1;
1880
1881
117
    HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32));   /* if compilation fails here, assertion is false */
1882
117
    if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
1883
    //memset(weightList, 0, sizeof(weightList));   /* is not necessary, even though some analyzer complain ... */
1884
1885
117
    iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
1886
117
    if (HUF_isError(iSize)) return iSize;
1887
1888
    /* check result */
1889
116
    if (tableLog > memLog) return ERROR(tableLog_tooLarge);   /* DTable can't fit code depth */
1890
1891
    /* find maxWeight */
1892
197
    for (maxW = tableLog; rankStats[maxW]==0; maxW--)
1893
82
        {if (!maxW) return ERROR(GENERIC); }  /* necessarily finds a solution before maxW==0 */
1894
1895
    /* Get start index of each weight */
1896
115
    {
1897
115
        U32 w, nextRankStart = 0;
1898
1.01k
        for (w=1; w<=maxW; w++)
1899
896
        {
1900
896
            U32 current = nextRankStart;
1901
896
            nextRankStart += rankStats[w];
1902
896
            rankStart[w] = current;
1903
896
        }
1904
115
        rankStart[0] = nextRankStart;   /* put all 0w symbols at the end of sorted list*/
1905
115
        sizeOfSort = nextRankStart;
1906
115
    }
1907
1908
    /* sort symbols by weight */
1909
115
    {
1910
115
        U32 s;
1911
8.69k
        for (s=0; s<nbSymbols; s++)
1912
8.57k
        {
1913
8.57k
            U32 w = weightList[s];
1914
8.57k
            U32 r = rankStart[w]++;
1915
8.57k
            sortedSymbol[r].symbol = (BYTE)s;
1916
8.57k
            sortedSymbol[r].weight = (BYTE)w;
1917
8.57k
        }
1918
115
        rankStart[0] = 0;   /* forget 0w symbols; this is beginning of weight(1) */
1919
115
    }
1920
1921
    /* Build rankVal */
1922
115
    {
1923
115
        const U32 minBits = tableLog+1 - maxW;
1924
115
        U32 nextRankVal = 0;
1925
115
        U32 w, consumed;
1926
115
        const int rescale = (memLog-tableLog) - 1;   /* tableLog <= memLog */
1927
115
        U32* rankVal0 = rankVal[0];
1928
1.01k
        for (w=1; w<=maxW; w++)
1929
896
        {
1930
896
            U32 current = nextRankVal;
1931
896
            nextRankVal += rankStats[w] << (w+rescale);
1932
896
            rankVal0[w] = current;
1933
896
        }
1934
1.21k
        for (consumed = minBits; consumed <= memLog - minBits; consumed++)
1935
1.10k
        {
1936
1.10k
            U32* rankValPtr = rankVal[consumed];
1937
10.2k
            for (w = 1; w <= maxW; w++)
1938
9.11k
            {
1939
9.11k
                rankValPtr[w] = rankVal0[w] >> consumed;
1940
9.11k
            }
1941
1.10k
        }
1942
115
    }
1943
1944
115
    HUF_fillDTableX4(dt, memLog,
1945
115
                   sortedSymbol, sizeOfSort,
1946
115
                   rankStart0, rankVal, maxW,
1947
115
                   tableLog+1);
1948
1949
115
    return iSize;
1950
115
}
1951
1952
1953
static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
1954
1.15M
{
1955
1.15M
    const size_t val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */
1956
1.15M
    memcpy(op, dt+val, 2);
1957
1.15M
    BIT_skipBits(DStream, dt[val].nbBits);
1958
1.15M
    return dt[val].length;
1959
1.15M
}
1960
1961
static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
1962
172
{
1963
172
    const size_t val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */
1964
172
    memcpy(op, dt+val, 1);
1965
172
    if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
1966
101
    else
1967
101
    {
1968
101
        if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
1969
38
        {
1970
38
            BIT_skipBits(DStream, dt[val].nbBits);
1971
38
            if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
1972
10
                DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);   /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
1973
38
        }
1974
101
    }
1975
172
    return 1;
1976
172
}
1977
1978
1979
#define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
1980
636k
    ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
1981
1982
#define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
1983
172k
    if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
1984
172k
        ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
1985
1986
#define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
1987
345k
    if (MEM_64bits()) \
1988
345k
        ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
1989
1990
static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog)
1991
288
{
1992
288
    BYTE* const pStart = p;
1993
1994
    /* up to 8 symbols at a time */
1995
129k
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7))
1996
129k
    {
1997
129k
        HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
1998
129k
        HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
1999
129k
        HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2000
129k
        HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2001
129k
    }
2002
2003
    /* closer to the end */
2004
444
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2))
2005
156
        HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2006
2007
463k
    while (p <= pEnd-2)
2008
463k
        HUF_DECODE_SYMBOLX4_0(p, bitDPtr);   /* no need to reload : reached the end of DStream */
2009
2010
288
    if (p < pEnd)
2011
172
        p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2012
2013
288
    return p-pStart;
2014
288
}
2015
2016
2017
2018
static size_t HUF_decompress4X4_usingDTable(
2019
          void* dst,  size_t dstSize,
2020
    const void* cSrc, size_t cSrcSize,
2021
    const U32* DTable)
2022
115
{
2023
115
    if (cSrcSize < 10) return ERROR(corruption_detected);   /* strict minimum : jump table + 1 byte per stream */
2024
2025
115
    {
2026
115
        const BYTE* const istart = (const BYTE*) cSrc;
2027
115
        BYTE* const ostart = (BYTE*) dst;
2028
115
        BYTE* const oend = ostart + dstSize;
2029
2030
115
        const void* ptr = DTable;
2031
115
        const HUF_DEltX4* const dt = ((const HUF_DEltX4*)ptr) +1;
2032
115
        const U32 dtLog = DTable[0];
2033
115
        size_t errorCode;
2034
2035
        /* Init */
2036
115
        BIT_DStream_t bitD1;
2037
115
        BIT_DStream_t bitD2;
2038
115
        BIT_DStream_t bitD3;
2039
115
        BIT_DStream_t bitD4;
2040
115
        const size_t length1 = MEM_readLE16(istart);
2041
115
        const size_t length2 = MEM_readLE16(istart+2);
2042
115
        const size_t length3 = MEM_readLE16(istart+4);
2043
115
        size_t length4;
2044
115
        const BYTE* const istart1 = istart + 6;  /* jumpTable */
2045
115
        const BYTE* const istart2 = istart1 + length1;
2046
115
        const BYTE* const istart3 = istart2 + length2;
2047
115
        const BYTE* const istart4 = istart3 + length3;
2048
115
        const size_t segmentSize = (dstSize+3) / 4;
2049
115
        BYTE* const opStart2 = ostart + segmentSize;
2050
115
        BYTE* const opStart3 = opStart2 + segmentSize;
2051
115
        BYTE* const opStart4 = opStart3 + segmentSize;
2052
115
        BYTE* op1 = ostart;
2053
115
        BYTE* op2 = opStart2;
2054
115
        BYTE* op3 = opStart3;
2055
115
        BYTE* op4 = opStart4;
2056
115
        U32 endSignal;
2057
2058
115
        length4 = cSrcSize - (length1 + length2 + length3 + 6);
2059
115
        if (length4 > cSrcSize) return ERROR(corruption_detected);   /* overflow */
2060
97
        errorCode = BIT_initDStream(&bitD1, istart1, length1);
2061
97
        if (HUF_isError(errorCode)) return errorCode;
2062
90
        errorCode = BIT_initDStream(&bitD2, istart2, length2);
2063
90
        if (HUF_isError(errorCode)) return errorCode;
2064
84
        errorCode = BIT_initDStream(&bitD3, istart3, length3);
2065
84
        if (HUF_isError(errorCode)) return errorCode;
2066
78
        errorCode = BIT_initDStream(&bitD4, istart4, length4);
2067
78
        if (HUF_isError(errorCode)) return errorCode;
2068
2069
        /* 16-32 symbols per loop (4-8 symbols per stream) */
2070
75
        endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2071
10.9k
        for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
2072
10.8k
        {
2073
10.8k
            HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2074
10.8k
            HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2075
10.8k
            HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2076
10.8k
            HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2077
10.8k
            HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
2078
10.8k
            HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
2079
10.8k
            HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
2080
10.8k
            HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
2081
10.8k
            HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2082
10.8k
            HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2083
10.8k
            HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2084
10.8k
            HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2085
10.8k
            HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
2086
10.8k
            HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
2087
10.8k
            HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
2088
10.8k
            HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
2089
2090
10.8k
            endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2091
10.8k
        }
2092
2093
        /* check corruption */
2094
75
        if (op1 > opStart2) return ERROR(corruption_detected);
2095
74
        if (op2 > opStart3) return ERROR(corruption_detected);
2096
73
        if (op3 > opStart4) return ERROR(corruption_detected);
2097
        /* note : op4 supposed already verified within main loop */
2098
2099
        /* finish bitStreams one by one */
2100
72
        HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2101
72
        HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2102
72
        HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2103
72
        HUF_decodeStreamX4(op4, &bitD4, oend,     dt, dtLog);
2104
2105
        /* check */
2106
72
        endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2107
72
        if (!endSignal) return ERROR(corruption_detected);
2108
2109
        /* decoded size */
2110
1
        return dstSize;
2111
72
    }
2112
72
}
2113
2114
2115
static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2116
117
{
2117
117
    HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG);
2118
117
    const BYTE* ip = (const BYTE*) cSrc;
2119
2120
117
    size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize);
2121
117
    if (HUF_isError(hSize)) return hSize;
2122
115
    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2123
115
    ip += hSize;
2124
115
    cSrcSize -= hSize;
2125
2126
115
    return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2127
115
}
2128
2129
2130
/**********************************/
2131
/* quad-symbol decoding           */
2132
/**********************************/
2133
typedef struct { BYTE nbBits; BYTE nbBytes; } HUF_DDescX6;
2134
typedef union { BYTE byte[4]; U32 sequence; } HUF_DSeqX6;
2135
2136
/* recursive, up to level 3; may benefit from <template>-like strategy to nest each level inline */
2137
static void HUF_fillDTableX6LevelN(HUF_DDescX6* DDescription, HUF_DSeqX6* DSequence, int sizeLog,
2138
                           const rankVal_t rankValOrigin, const U32 consumed, const int minWeight, const U32 maxWeight,
2139
                           const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, const U32* rankStart,
2140
                           const U32 nbBitsBaseline, HUF_DSeqX6 baseSeq, HUF_DDescX6 DDesc)
2141
8.75k
{
2142
8.75k
    const int scaleLog = nbBitsBaseline - sizeLog;   /* note : targetLog >= (nbBitsBaseline-1), hence scaleLog <= 1 */
2143
8.75k
    const int minBits  = nbBitsBaseline - maxWeight;
2144
8.75k
    const U32 level = DDesc.nbBytes;
2145
8.75k
    U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
2146
8.75k
    U32 symbolStartPos, s;
2147
2148
    /* local rankVal, will be modified */
2149
8.75k
    memcpy(rankVal, rankValOrigin[consumed], sizeof(rankVal));
2150
2151
    /* fill skipped values */
2152
8.75k
    if (minWeight>1)
2153
7.10k
    {
2154
7.10k
        U32 i;
2155
7.10k
        const U32 skipSize = rankVal[minWeight];
2156
88.4k
        for (i = 0; i < skipSize; i++)
2157
81.3k
        {
2158
81.3k
            DSequence[i] = baseSeq;
2159
81.3k
            DDescription[i] = DDesc;
2160
81.3k
        }
2161
7.10k
    }
2162
2163
    /* fill DTable */
2164
8.75k
    DDesc.nbBytes++;
2165
8.75k
    symbolStartPos = rankStart[minWeight];
2166
55.2k
    for (s=symbolStartPos; s<sortedListSize; s++)
2167
46.5k
    {
2168
46.5k
        const BYTE symbol = sortedSymbols[s].symbol;
2169
46.5k
        const U32  weight = sortedSymbols[s].weight;   /* >= 1 (sorted) */
2170
46.5k
        const int  nbBits = nbBitsBaseline - weight;   /* >= 1 (by construction) */
2171
46.5k
        const int  totalBits = consumed+nbBits;
2172
46.5k
        const U32  start  = rankVal[weight];
2173
46.5k
        const U32  length = 1 << (sizeLog-nbBits);
2174
46.5k
        baseSeq.byte[level] = symbol;
2175
46.5k
        DDesc.nbBits = (BYTE)totalBits;
2176
2177
46.5k
        if ((level<3) && (sizeLog-totalBits >= minBits))   /* enough room for another symbol */
2178
8.66k
        {
2179
8.66k
            int nextMinWeight = totalBits + scaleLog;
2180
8.66k
            if (nextMinWeight < 1) nextMinWeight = 1;
2181
8.66k
            HUF_fillDTableX6LevelN(DDescription+start, DSequence+start, sizeLog-nbBits,
2182
8.66k
                           rankValOrigin, totalBits, nextMinWeight, maxWeight,
2183
8.66k
                           sortedSymbols, sortedListSize, rankStart,
2184
8.66k
                           nbBitsBaseline, baseSeq, DDesc);   /* recursive (max : level 3) */
2185
8.66k
        }
2186
37.8k
        else
2187
37.8k
        {
2188
37.8k
            U32 i;
2189
37.8k
            const U32 end = start + length;
2190
308k
            for (i = start; i < end; i++)
2191
270k
            {
2192
270k
                DDescription[i] = DDesc;
2193
270k
                DSequence[i] = baseSeq;
2194
270k
            }
2195
37.8k
        }
2196
46.5k
        rankVal[weight] += length;
2197
46.5k
    }
2198
8.75k
}
2199
2200
2201
/* note : same preparation as X4 */
2202
static size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
2203
88
{
2204
88
    BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
2205
88
    sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
2206
88
    U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
2207
88
    U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
2208
88
    U32* const rankStart = rankStart0+1;
2209
88
    U32 tableLog, maxW, sizeOfSort, nbSymbols;
2210
88
    rankVal_t rankVal;
2211
88
    const U32 memLog = DTable[0];
2212
88
    const BYTE* ip = (const BYTE*) src;
2213
88
    size_t iSize = ip[0];
2214
2215
88
    if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2216
    //memset(weightList, 0, sizeof(weightList));   /* is not necessary, even though some analyzer complain ... */
2217
2218
88
    iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2219
88
    if (HUF_isError(iSize)) return iSize;
2220
2221
    /* check result */
2222
87
    if (tableLog > memLog) return ERROR(tableLog_tooLarge);   /* DTable is too small */
2223
2224
    /* find maxWeight */
2225
169
    for (maxW = tableLog; rankStats[maxW]==0; maxW--)
2226
83
        { if (!maxW) return ERROR(GENERIC); }  /* necessarily finds a solution before maxW==0 */
2227
2228
2229
    /* Get start index of each weight */
2230
86
    {
2231
86
        U32 w, nextRankStart = 0;
2232
539
        for (w=1; w<=maxW; w++)
2233
453
        {
2234
453
            U32 current = nextRankStart;
2235
453
            nextRankStart += rankStats[w];
2236
453
            rankStart[w] = current;
2237
453
        }
2238
86
        rankStart[0] = nextRankStart;   /* put all 0w symbols at the end of sorted list*/
2239
86
        sizeOfSort = nextRankStart;
2240
86
    }
2241
2242
    /* sort symbols by weight */
2243
86
    {
2244
86
        U32 s;
2245
7.83k
        for (s=0; s<nbSymbols; s++)
2246
7.74k
        {
2247
7.74k
            U32 w = weightList[s];
2248
7.74k
            U32 r = rankStart[w]++;
2249
7.74k
            sortedSymbol[r].symbol = (BYTE)s;
2250
7.74k
            sortedSymbol[r].weight = (BYTE)w;
2251
7.74k
        }
2252
86
        rankStart[0] = 0;   /* forget 0w symbols; this is beginning of weight(1) */
2253
86
    }
2254
2255
    /* Build rankVal */
2256
86
    {
2257
86
        const U32 minBits = tableLog+1 - maxW;
2258
86
        U32 nextRankVal = 0;
2259
86
        U32 w, consumed;
2260
86
        const int rescale = (memLog-tableLog) - 1;   /* tableLog <= memLog */
2261
86
        U32* rankVal0 = rankVal[0];
2262
539
        for (w=1; w<=maxW; w++)
2263
453
        {
2264
453
            U32 current = nextRankVal;
2265
453
            nextRankVal += rankStats[w] << (w+rescale);
2266
453
            rankVal0[w] = current;
2267
453
        }
2268
869
        for (consumed = minBits; consumed <= memLog - minBits; consumed++)
2269
783
        {
2270
783
            U32* rankValPtr = rankVal[consumed];
2271
5.38k
            for (w = 1; w <= maxW; w++)
2272
4.60k
            {
2273
4.60k
                rankValPtr[w] = rankVal0[w] >> consumed;
2274
4.60k
            }
2275
783
        }
2276
86
    }
2277
2278
2279
    /* fill tables */
2280
86
    {
2281
86
        void* ptr = DTable+1;
2282
86
        HUF_DDescX6* DDescription = (HUF_DDescX6*)(ptr);
2283
86
        void* dSeqStart = DTable + 1 + ((size_t)1<<(memLog-1));
2284
86
        HUF_DSeqX6* DSequence = (HUF_DSeqX6*)(dSeqStart);
2285
86
        HUF_DSeqX6 DSeq;
2286
86
        HUF_DDescX6 DDesc;
2287
86
        DSeq.sequence = 0;
2288
86
        DDesc.nbBits = 0;
2289
86
        DDesc.nbBytes = 0;
2290
86
        HUF_fillDTableX6LevelN(DDescription, DSequence, memLog,
2291
86
                       (const U32 (*)[HUF_ABSOLUTEMAX_TABLELOG + 1])rankVal, 0, 1, maxW,
2292
86
                       sortedSymbol, sizeOfSort, rankStart0,
2293
86
                       tableLog+1, DSeq, DDesc);
2294
86
    }
2295
2296
86
    return iSize;
2297
86
}
2298
2299
2300
static U32 HUF_decodeSymbolX6(void* op, BIT_DStream_t* DStream, const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2301
171k
{
2302
171k
    const size_t val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */
2303
171k
    memcpy(op, ds+val, sizeof(HUF_DSeqX6));
2304
171k
    BIT_skipBits(DStream, dd[val].nbBits);
2305
171k
    return dd[val].nbBytes;
2306
171k
}
2307
2308
static U32 HUF_decodeLastSymbolsX6(void* op, const U32 maxL, BIT_DStream_t* DStream,
2309
                                  const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2310
274
{
2311
274
    const size_t val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */
2312
274
    U32 length = dd[val].nbBytes;
2313
274
    if (length <= maxL)
2314
140
    {
2315
140
        memcpy(op, ds+val, length);
2316
140
        BIT_skipBits(DStream, dd[val].nbBits);
2317
140
        return length;
2318
140
    }
2319
134
    memcpy(op, ds+val, maxL);
2320
134
    if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2321
46
    {
2322
46
        BIT_skipBits(DStream, dd[val].nbBits);
2323
46
        if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2324
10
            DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);   /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
2325
46
    }
2326
134
    return maxL;
2327
274
}
2328
2329
2330
#define HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr) \
2331
223k
    ptr += HUF_decodeSymbolX6(ptr, DStreamPtr, dd, ds, dtLog)
2332
2333
#define HUF_DECODE_SYMBOLX6_1(ptr, DStreamPtr) \
2334
17.2k
    if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2335
17.2k
        HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2336
2337
#define HUF_DECODE_SYMBOLX6_2(ptr, DStreamPtr) \
2338
34.5k
    if (MEM_64bits()) \
2339
34.5k
        HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2340
2341
static inline size_t HUF_decodeStreamX6(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const U32* DTable, const U32 dtLog)
2342
220
{
2343
220
    const void* ddPtr = DTable+1;
2344
220
    const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2345
220
    const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2346
220
    const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
2347
220
    BYTE* const pStart = p;
2348
2349
    /* up to 16 symbols at a time */
2350
11.5k
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-16))
2351
11.3k
    {
2352
11.3k
        HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2353
11.3k
        HUF_DECODE_SYMBOLX6_1(p, bitDPtr);
2354
11.3k
        HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2355
11.3k
        HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2356
11.3k
    }
2357
2358
    /* closer to the end, up to 4 symbols at a time */
2359
325
    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
2360
105
        HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2361
2362
102k
    while (p <= pEnd-4)
2363
102k
        HUF_DECODE_SYMBOLX6_0(p, bitDPtr);   /* no need to reload : reached the end of DStream */
2364
2365
494
    while (p < pEnd)
2366
274
        p += HUF_decodeLastSymbolsX6(p, (U32)(pEnd-p), bitDPtr, dd, ds, dtLog);
2367
2368
220
    return p-pStart;
2369
220
}
2370
2371
2372
2373
static size_t HUF_decompress4X6_usingDTable(
2374
          void* dst,  size_t dstSize,
2375
    const void* cSrc, size_t cSrcSize,
2376
    const U32* DTable)
2377
86
{
2378
86
    if (cSrcSize < 10) return ERROR(corruption_detected);   /* strict minimum : jump table + 1 byte per stream */
2379
2380
86
    {
2381
86
        const BYTE* const istart = (const BYTE*) cSrc;
2382
86
        BYTE* const ostart = (BYTE*) dst;
2383
86
        BYTE* const oend = ostart + dstSize;
2384
2385
86
        const U32 dtLog = DTable[0];
2386
86
        const void* ddPtr = DTable+1;
2387
86
        const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2388
86
        const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2389
86
        const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
2390
86
        size_t errorCode;
2391
2392
        /* Init */
2393
86
        BIT_DStream_t bitD1;
2394
86
        BIT_DStream_t bitD2;
2395
86
        BIT_DStream_t bitD3;
2396
86
        BIT_DStream_t bitD4;
2397
86
        const size_t length1 = MEM_readLE16(istart);
2398
86
        const size_t length2 = MEM_readLE16(istart+2);
2399
86
        const size_t length3 = MEM_readLE16(istart+4);
2400
86
        size_t length4;
2401
86
        const BYTE* const istart1 = istart + 6;  /* jumpTable */
2402
86
        const BYTE* const istart2 = istart1 + length1;
2403
86
        const BYTE* const istart3 = istart2 + length2;
2404
86
        const BYTE* const istart4 = istart3 + length3;
2405
86
        const size_t segmentSize = (dstSize+3) / 4;
2406
86
        BYTE* const opStart2 = ostart + segmentSize;
2407
86
        BYTE* const opStart3 = opStart2 + segmentSize;
2408
86
        BYTE* const opStart4 = opStart3 + segmentSize;
2409
86
        BYTE* op1 = ostart;
2410
86
        BYTE* op2 = opStart2;
2411
86
        BYTE* op3 = opStart3;
2412
86
        BYTE* op4 = opStart4;
2413
86
        U32 endSignal;
2414
2415
86
        length4 = cSrcSize - (length1 + length2 + length3 + 6);
2416
86
        if (length4 > cSrcSize) return ERROR(corruption_detected);   /* overflow */
2417
71
        errorCode = BIT_initDStream(&bitD1, istart1, length1);
2418
71
        if (HUF_isError(errorCode)) return errorCode;
2419
68
        errorCode = BIT_initDStream(&bitD2, istart2, length2);
2420
68
        if (HUF_isError(errorCode)) return errorCode;
2421
64
        errorCode = BIT_initDStream(&bitD3, istart3, length3);
2422
64
        if (HUF_isError(errorCode)) return errorCode;
2423
59
        errorCode = BIT_initDStream(&bitD4, istart4, length4);
2424
59
        if (HUF_isError(errorCode)) return errorCode;
2425
2426
        /* 16-64 symbols per loop (4-16 symbols per stream) */
2427
58
        endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2428
1.54k
        for ( ; (op3 <= opStart4) && (endSignal==BIT_DStream_unfinished) && (op4<=(oend-16)) ; )
2429
1.48k
        {
2430
1.48k
            HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2431
1.48k
            HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2432
1.48k
            HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2433
1.48k
            HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2434
1.48k
            HUF_DECODE_SYMBOLX6_1(op1, &bitD1);
2435
1.48k
            HUF_DECODE_SYMBOLX6_1(op2, &bitD2);
2436
1.48k
            HUF_DECODE_SYMBOLX6_1(op3, &bitD3);
2437
1.48k
            HUF_DECODE_SYMBOLX6_1(op4, &bitD4);
2438
1.48k
            HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2439
1.48k
            HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2440
1.48k
            HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2441
1.48k
            HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2442
1.48k
            HUF_DECODE_SYMBOLX6_0(op1, &bitD1);
2443
1.48k
            HUF_DECODE_SYMBOLX6_0(op2, &bitD2);
2444
1.48k
            HUF_DECODE_SYMBOLX6_0(op3, &bitD3);
2445
1.48k
            HUF_DECODE_SYMBOLX6_0(op4, &bitD4);
2446
2447
1.48k
            endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2448
1.48k
        }
2449
2450
        /* check corruption */
2451
58
        if (op1 > opStart2) return ERROR(corruption_detected);
2452
57
        if (op2 > opStart3) return ERROR(corruption_detected);
2453
56
        if (op3 > opStart4) return ERROR(corruption_detected);
2454
        /* note : op4 supposed already verified within main loop */
2455
2456
        /* finish bitStreams one by one */
2457
55
        HUF_decodeStreamX6(op1, &bitD1, opStart2, DTable, dtLog);
2458
55
        HUF_decodeStreamX6(op2, &bitD2, opStart3, DTable, dtLog);
2459
55
        HUF_decodeStreamX6(op3, &bitD3, opStart4, DTable, dtLog);
2460
55
        HUF_decodeStreamX6(op4, &bitD4, oend,     DTable, dtLog);
2461
2462
        /* check */
2463
55
        endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2464
55
        if (!endSignal) return ERROR(corruption_detected);
2465
2466
        /* decoded size */
2467
1
        return dstSize;
2468
55
    }
2469
55
}
2470
2471
2472
static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2473
88
{
2474
88
    HUF_CREATE_STATIC_DTABLEX6(DTable, HUF_MAX_TABLELOG);
2475
88
    const BYTE* ip = (const BYTE*) cSrc;
2476
2477
88
    size_t hSize = HUF_readDTableX6 (DTable, cSrc, cSrcSize);
2478
88
    if (HUF_isError(hSize)) return hSize;
2479
86
    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2480
86
    ip += hSize;
2481
86
    cSrcSize -= hSize;
2482
2483
86
    return HUF_decompress4X6_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2484
86
}
2485
2486
2487
/**********************************/
2488
/* Generic decompression selector */
2489
/**********************************/
2490
2491
typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2492
static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2493
{
2494
    /* single, double, quad */
2495
    {{0,0}, {1,1}, {2,2}},  /* Q==0 : impossible */
2496
    {{0,0}, {1,1}, {2,2}},  /* Q==1 : impossible */
2497
    {{  38,130}, {1313, 74}, {2151, 38}},   /* Q == 2 : 12-18% */
2498
    {{ 448,128}, {1353, 74}, {2238, 41}},   /* Q == 3 : 18-25% */
2499
    {{ 556,128}, {1353, 74}, {2238, 47}},   /* Q == 4 : 25-32% */
2500
    {{ 714,128}, {1418, 74}, {2436, 53}},   /* Q == 5 : 32-38% */
2501
    {{ 883,128}, {1437, 74}, {2464, 61}},   /* Q == 6 : 38-44% */
2502
    {{ 897,128}, {1515, 75}, {2622, 68}},   /* Q == 7 : 44-50% */
2503
    {{ 926,128}, {1613, 75}, {2730, 75}},   /* Q == 8 : 50-56% */
2504
    {{ 947,128}, {1729, 77}, {3359, 77}},   /* Q == 9 : 56-62% */
2505
    {{1107,128}, {2083, 81}, {4006, 84}},   /* Q ==10 : 62-69% */
2506
    {{1177,128}, {2379, 87}, {4785, 88}},   /* Q ==11 : 69-75% */
2507
    {{1242,128}, {2415, 93}, {5155, 84}},   /* Q ==12 : 75-81% */
2508
    {{1349,128}, {2644,106}, {5260,106}},   /* Q ==13 : 81-87% */
2509
    {{1455,128}, {2422,124}, {4174,124}},   /* Q ==14 : 87-93% */
2510
    {{ 722,128}, {1891,145}, {1936,146}},   /* Q ==15 : 93-99% */
2511
};
2512
2513
typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
2514
2515
static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2516
1.42k
{
2517
1.42k
    static const decompressionAlgo decompress[3] = { HUF_decompress4X2, HUF_decompress4X4, HUF_decompress4X6 };
2518
    /* estimate decompression time */
2519
1.42k
    U32 Q;
2520
1.42k
    const U32 D256 = (U32)(dstSize >> 8);
2521
1.42k
    U32 Dtime[3];
2522
1.42k
    U32 algoNb = 0;
2523
1.42k
    int n;
2524
2525
    /* validation checks */
2526
1.42k
    if (dstSize == 0) return ERROR(dstSize_tooSmall);
2527
1.42k
    if (cSrcSize > dstSize) return ERROR(corruption_detected);   /* invalid */
2528
1.42k
    if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; }   /* not compressed */
2529
1.40k
    if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; }   /* RLE */
2530
2531
    /* decoder timing evaluation */
2532
1.37k
    Q = (U32)(cSrcSize * 16 / dstSize);   /* Q < 16 since dstSize > cSrcSize */
2533
5.50k
    for (n=0; n<3; n++)
2534
4.13k
        Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256);
2535
2536
1.37k
    Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */
2537
2538
1.37k
    if (Dtime[1] < Dtime[0]) algoNb = 1;
2539
1.37k
    if (Dtime[2] < Dtime[algoNb]) algoNb = 2;
2540
2541
1.37k
    return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2542
2543
    //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize);   /* multi-streams single-symbol decoding */
2544
    //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize);   /* multi-streams double-symbols decoding */
2545
    //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize);   /* multi-streams quad-symbols decoding */
2546
1.40k
}
2547
/*
2548
    zstd - standard compression library
2549
    Copyright (C) 2014-2015, Yann Collet.
2550
2551
    BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2552
2553
    Redistribution and use in source and binary forms, with or without
2554
    modification, are permitted provided that the following conditions are
2555
    met:
2556
    * Redistributions of source code must retain the above copyright
2557
    notice, this list of conditions and the following disclaimer.
2558
    * Redistributions in binary form must reproduce the above
2559
    copyright notice, this list of conditions and the following disclaimer
2560
    in the documentation and/or other materials provided with the
2561
    distribution.
2562
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2563
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2564
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2565
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2566
    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2567
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2568
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2569
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2570
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2571
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2572
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2573
2574
    You can contact the author at :
2575
    - zstd source repository : https://github.com/Cyan4973/zstd
2576
    - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
2577
*/
2578
2579
/* ***************************************************************
2580
*  Tuning parameters
2581
*****************************************************************/
2582
/*!
2583
*  MEMORY_USAGE :
2584
*  Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
2585
*  Increasing memory usage improves compression ratio
2586
*  Reduced memory usage can improve speed, due to cache effect
2587
*/
2588
#define ZSTD_MEMORY_USAGE 17
2589
2590
/*!
2591
 * HEAPMODE :
2592
 * Select how default compression functions will allocate memory for their hash table,
2593
 * in memory stack (0, fastest), or in memory heap (1, requires malloc())
2594
 * Note that compression context is fairly large, as a consequence heap memory is recommended.
2595
 */
2596
#ifndef ZSTD_HEAPMODE
2597
#  define ZSTD_HEAPMODE 1
2598
#endif /* ZSTD_HEAPMODE */
2599
2600
/*!
2601
*  LEGACY_SUPPORT :
2602
*  decompressor can decode older formats (starting from Zstd 0.1+)
2603
*/
2604
#ifndef ZSTD_LEGACY_SUPPORT
2605
#  define ZSTD_LEGACY_SUPPORT 1
2606
#endif
2607
2608
2609
/* *******************************************************
2610
*  Includes
2611
*********************************************************/
2612
#include <stdlib.h>      /* calloc */
2613
#include <string.h>      /* memcpy, memmove */
2614
#include <stdio.h>       /* debug : printf */
2615
2616
2617
/* *******************************************************
2618
*  Compiler specifics
2619
*********************************************************/
2620
#ifdef __AVX2__
2621
#  include <immintrin.h>   /* AVX2 intrinsics */
2622
#endif
2623
2624
#ifdef _MSC_VER    /* Visual Studio */
2625
#  include <intrin.h>                    /* For Visual 2005 */
2626
#  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
2627
#  pragma warning(disable : 4324)        /* disable: C4324: padded structure */
2628
#endif
2629
2630
2631
/* *******************************************************
2632
*  Constants
2633
*********************************************************/
2634
#define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
2635
#define HASH_TABLESIZE (1 << HASH_LOG)
2636
#define HASH_MASK (HASH_TABLESIZE - 1)
2637
2638
#define KNUTH 2654435761
2639
2640
#define BIT7 128
2641
#define BIT6  64
2642
#define BIT5  32
2643
#define BIT4  16
2644
2.19k
#define BIT1   2
2645
2.47k
#define BIT0   1
2646
2647
6.10k
#define KB *(1 <<10)
2648
#define MB *(1 <<20)
2649
#define GB *(1U<<30)
2650
2651
6.10k
#define BLOCKSIZE (128 KB)                 /* define, for static allocation */
2652
6.16k
#define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
2653
6.16k
#define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
2654
2.47k
#define IS_RAW BIT0
2655
2.19k
#define IS_RLE BIT1
2656
2657
#define WORKPLACESIZE (BLOCKSIZE*3)
2658
507k
#define MINMATCH 4
2659
261k
#define MLbits   7
2660
261k
#define LLbits   6
2661
4.83k
#define Offbits  5
2662
254k
#define MaxML  ((1<<MLbits )-1)
2663
254k
#define MaxLL  ((1<<LLbits )-1)
2664
3.00k
#define MaxOff   31
2665
#define LitFSELog  11
2666
1.10k
#define MLFSELog   10
2667
714
#define LLFSELog   10
2668
1.09k
#define OffFSELog   9
2669
#define MAX(a,b) ((a)<(b)?(b):(a))
2670
#define MaxSeq MAX(MaxLL, MaxML)
2671
2672
#define LITERAL_NOENTROPY 63
2673
#define COMMAND_NOENTROPY 7   /* to remove */
2674
2675
52
#define ZSTD_CONTENTSIZE_ERROR   (0ULL - 2)
2676
2677
static const size_t ZSTD_blockHeaderSize = 3;
2678
static const size_t ZSTD_frameHeaderSize = 4;
2679
2680
2681
/* *******************************************************
2682
*  Memory operations
2683
**********************************************************/
2684
225k
static void   ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2685
2686
2.86M
static void   ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2687
2688
2.83M
#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
2689
2690
/*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
2691
static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
2692
507k
{
2693
507k
    const BYTE* ip = (const BYTE*)src;
2694
507k
    BYTE* op = (BYTE*)dst;
2695
507k
    BYTE* const oend = op + length;
2696
2.83M
    do COPY8(op, ip) while (op < oend);
2697
507k
}
2698
2699
2700
/* **************************************
2701
*  Local structures
2702
****************************************/
2703
typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
2704
2705
typedef struct
2706
{
2707
    blockType_t blockType;
2708
    U32 origSize;
2709
} blockProperties_t;
2710
2711
typedef struct {
2712
    void* buffer;
2713
    U32*  offsetStart;
2714
    U32*  offset;
2715
    BYTE* offCodeStart;
2716
    BYTE* offCode;
2717
    BYTE* litStart;
2718
    BYTE* lit;
2719
    BYTE* litLengthStart;
2720
    BYTE* litLength;
2721
    BYTE* matchLengthStart;
2722
    BYTE* matchLength;
2723
    BYTE* dumpsStart;
2724
    BYTE* dumps;
2725
} SeqStore_t;
2726
2727
2728
/* *************************************
2729
*  Error Management
2730
***************************************/
2731
/*! ZSTD_isError
2732
*   tells if a return value is an error code */
2733
295k
static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2734
2735
2736
2737
/* *************************************************************
2738
*   Decompression section
2739
***************************************************************/
2740
struct ZSTDv02_Dctx_s
2741
{
2742
    U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2743
    U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
2744
    U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
2745
    void* previousDstEnd;
2746
    void* base;
2747
    size_t expected;
2748
    blockType_t bType;
2749
    U32 phase;
2750
    const BYTE* litPtr;
2751
    size_t litSize;
2752
    BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2753
};   /* typedef'd to ZSTD_Dctx within "zstd_static.h" */
2754
2755
2756
static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2757
23.4k
{
2758
23.4k
    const BYTE* const in = (const BYTE* const)src;
2759
23.4k
    BYTE headerFlags;
2760
23.4k
    U32 cSize;
2761
2762
23.4k
    if (srcSize < 3) return ERROR(srcSize_wrong);
2763
2764
23.4k
    headerFlags = *in;
2765
23.4k
    cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
2766
2767
23.4k
    bpPtr->blockType = (blockType_t)(headerFlags >> 6);
2768
23.4k
    bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
2769
2770
23.4k
    if (bpPtr->blockType == bt_end) return 0;
2771
21.4k
    if (bpPtr->blockType == bt_rle) return 1;
2772
20.5k
    return cSize;
2773
21.4k
}
2774
2775
static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2776
372
{
2777
372
    if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2778
359
    if (srcSize > 0) {
2779
347
        memcpy(dst, src, srcSize);
2780
347
    }
2781
359
    return srcSize;
2782
372
}
2783
2784
2785
/** ZSTD_decompressLiterals
2786
    @return : nb of bytes read from src, or an error code*/
2787
static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr,
2788
                                const void* src, size_t srcSize)
2789
1.44k
{
2790
1.44k
    const BYTE* ip = (const BYTE*)src;
2791
2792
1.44k
    const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2;   /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2793
1.44k
    const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5;   /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2794
2795
1.44k
    if (litSize > *maxDstSizePtr) return ERROR(corruption_detected);
2796
1.43k
    if (litCSize + 5 > srcSize) return ERROR(corruption_detected);
2797
2798
1.42k
    if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected);
2799
2800
832
    *maxDstSizePtr = litSize;
2801
832
    return litCSize + 5;
2802
1.42k
}
2803
2804
2805
/** ZSTD_decodeLiteralsBlock
2806
    @return : nb of bytes read from src (< srcSize )*/
2807
static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2808
                          const void* src, size_t srcSize)
2809
6.16k
{
2810
6.16k
    ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
2811
6.16k
    const BYTE* const istart = (const BYTE* const)src;
2812
2813
    /* any compressed block with literals segment must be at least this size */
2814
6.16k
    if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
2815
2816
6.10k
    switch(*istart & 3)
2817
6.10k
    {
2818
227
    default:
2819
1.44k
    case 0:
2820
1.44k
        {
2821
1.44k
            size_t litSize = BLOCKSIZE;
2822
1.44k
            const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
2823
1.44k
            dctx->litPtr = dctx->litBuffer;
2824
1.44k
            dctx->litSize = litSize;
2825
1.44k
            memset(dctx->litBuffer + dctx->litSize, 0, 8);
2826
1.44k
            return readSize;   /* works if it's an error too */
2827
227
        }
2828
2.47k
    case IS_RAW:
2829
2.47k
        {
2830
2.47k
            const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2;   /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2831
2.47k
            if (litSize > srcSize-11)   /* risk of reading too far with wildcopy */
2832
57
            {
2833
57
                if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2834
51
                if (litSize > srcSize-3) return ERROR(corruption_detected);
2835
38
                memcpy(dctx->litBuffer, istart, litSize);
2836
38
                dctx->litPtr = dctx->litBuffer;
2837
38
                dctx->litSize = litSize;
2838
38
                memset(dctx->litBuffer + dctx->litSize, 0, 8);
2839
38
                return litSize+3;
2840
51
            }
2841
            /* direct reference into compressed stream */
2842
2.41k
            dctx->litPtr = istart+3;
2843
2.41k
            dctx->litSize = litSize;
2844
2.41k
            return litSize+3;
2845
2.47k
        }
2846
2.19k
    case IS_RLE:
2847
2.19k
        {
2848
2.19k
            const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2;   /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2849
2.19k
            if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2850
2.18k
            memset(dctx->litBuffer, istart[3], litSize + 8);
2851
2.18k
            dctx->litPtr = dctx->litBuffer;
2852
2.18k
            dctx->litSize = litSize;
2853
2.18k
            return 4;
2854
2.19k
        }
2855
6.10k
    }
2856
6.10k
}
2857
2858
2859
static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
2860
                         FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
2861
                         const void* src, size_t srcSize)
2862
5.46k
{
2863
5.46k
    const BYTE* const istart = (const BYTE* const)src;
2864
5.46k
    const BYTE* ip = istart;
2865
5.46k
    const BYTE* const iend = istart + srcSize;
2866
5.46k
    U32 LLtype, Offtype, MLtype;
2867
5.46k
    U32 LLlog, Offlog, MLlog;
2868
5.46k
    size_t dumpsLength;
2869
2870
    /* check */
2871
5.46k
    if (srcSize < 5) return ERROR(srcSize_wrong);
2872
2873
    /* SeqHead */
2874
5.46k
    *nbSeq = MEM_readLE16(ip); ip+=2;
2875
5.46k
    LLtype  = *ip >> 6;
2876
5.46k
    Offtype = (*ip >> 4) & 3;
2877
5.46k
    MLtype  = (*ip >> 2) & 3;
2878
5.46k
    if (*ip & 2)
2879
2.96k
    {
2880
2.96k
        dumpsLength  = ip[2];
2881
2.96k
        dumpsLength += ip[1] << 8;
2882
2.96k
        ip += 3;
2883
2.96k
    }
2884
2.50k
    else
2885
2.50k
    {
2886
2.50k
        dumpsLength  = ip[1];
2887
2.50k
        dumpsLength += (ip[0] & 1) << 8;
2888
2.50k
        ip += 2;
2889
2.50k
    }
2890
5.46k
    *dumpsPtr = ip;
2891
5.46k
    ip += dumpsLength;
2892
5.46k
    *dumpsLengthPtr = dumpsLength;
2893
2894
    /* check */
2895
5.46k
    if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
2896
2897
    /* sequences */
2898
5.44k
    {
2899
5.44k
        S16 norm[MaxML+1];    /* assumption : MaxML >= MaxLL and MaxOff */
2900
5.44k
        size_t headerSize;
2901
2902
        /* Build DTables */
2903
5.44k
        switch(LLtype)
2904
5.44k
        {
2905
978
        case bt_rle :
2906
978
            LLlog = 0;
2907
978
            FSE_buildDTable_rle(DTableLL, *ip++); break;
2908
3.73k
        case bt_raw :
2909
3.73k
            LLlog = LLbits;
2910
3.73k
            FSE_buildDTable_raw(DTableLL, LLbits); break;
2911
729
        default :
2912
729
            {   U32 max = MaxLL;
2913
729
                headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
2914
729
                if (FSE_isError(headerSize)) return ERROR(GENERIC);
2915
714
                if (LLlog > LLFSELog) return ERROR(corruption_detected);
2916
710
                ip += headerSize;
2917
710
                FSE_buildDTable(DTableLL, norm, max, LLlog);
2918
710
        }   }
2919
2920
5.42k
        switch(Offtype)
2921
5.42k
        {
2922
1.88k
        case bt_rle :
2923
1.88k
            Offlog = 0;
2924
1.88k
            if (ip > iend-2) return ERROR(srcSize_wrong);   /* min : "raw", hence no header, but at least xxLog bits */
2925
1.88k
            FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
2926
1.88k
            break;
2927
2.41k
        case bt_raw :
2928
2.41k
            Offlog = Offbits;
2929
2.41k
            FSE_buildDTable_raw(DTableOffb, Offbits); break;
2930
1.11k
        default :
2931
1.11k
            {   U32 max = MaxOff;
2932
1.11k
                headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
2933
1.11k
                if (FSE_isError(headerSize)) return ERROR(GENERIC);
2934
1.09k
                if (Offlog > OffFSELog) return ERROR(corruption_detected);
2935
1.08k
                ip += headerSize;
2936
1.08k
                FSE_buildDTable(DTableOffb, norm, max, Offlog);
2937
1.08k
        }   }
2938
2939
5.38k
        switch(MLtype)
2940
5.38k
        {
2941
934
        case bt_rle :
2942
934
            MLlog = 0;
2943
934
            if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
2944
933
            FSE_buildDTable_rle(DTableML, *ip++); break;
2945
3.33k
        case bt_raw :
2946
3.33k
            MLlog = MLbits;
2947
3.33k
            FSE_buildDTable_raw(DTableML, MLbits); break;
2948
1.11k
        default :
2949
1.11k
            {   U32 max = MaxML;
2950
1.11k
                headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
2951
1.11k
                if (FSE_isError(headerSize)) return ERROR(GENERIC);
2952
1.10k
                if (MLlog > MLFSELog) return ERROR(corruption_detected);
2953
1.10k
                ip += headerSize;
2954
1.10k
                FSE_buildDTable(DTableML, norm, max, MLlog);
2955
1.10k
    }   }   }
2956
2957
5.37k
    return ip-istart;
2958
5.38k
}
2959
2960
2961
typedef struct {
2962
    size_t litLength;
2963
    size_t offset;
2964
    size_t matchLength;
2965
} seq_t;
2966
2967
typedef struct {
2968
    BIT_DStream_t DStream;
2969
    FSE_DState_t stateLL;
2970
    FSE_DState_t stateOffb;
2971
    FSE_DState_t stateML;
2972
    size_t prevOffset;
2973
    const BYTE* dumps;
2974
    const BYTE* dumpsEnd;
2975
} seqState_t;
2976
2977
2978
static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2979
253k
{
2980
253k
    size_t litLength;
2981
253k
    size_t prevOffset;
2982
253k
    size_t offset;
2983
253k
    size_t matchLength;
2984
253k
    const BYTE* dumps = seqState->dumps;
2985
253k
    const BYTE* const de = seqState->dumpsEnd;
2986
2987
    /* Literal length */
2988
253k
    litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
2989
253k
    prevOffset = litLength ? seq->offset : seqState->prevOffset;
2990
253k
    seqState->prevOffset = seq->offset;
2991
253k
    if (litLength == MaxLL)
2992
8.70k
    {
2993
8.70k
        const U32 add = dumps<de ? *dumps++ : 0;
2994
8.70k
        if (add < 255) litLength += add;
2995
1.70k
        else if (dumps + 3 <= de)
2996
57
        {
2997
57
            litLength = MEM_readLE24(dumps);
2998
57
            dumps += 3;
2999
57
        }
3000
8.70k
        if (dumps >= de) dumps = de-1;   /* late correction, to avoid read overflow (data is now corrupted anyway) */
3001
8.70k
    }
3002
3003
    /* Offset */
3004
253k
    {
3005
253k
        static const size_t offsetPrefix[MaxOff+1] = {  /* note : size_t faster than U32 */
3006
253k
                1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3007
253k
                512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3008
253k
                524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
3009
253k
        U32 offsetCode, nbBits;
3010
253k
        offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream));   /* <= maxOff, by table construction */
3011
253k
        if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3012
253k
        nbBits = offsetCode - 1;
3013
253k
        if (offsetCode==0) nbBits = 0;   /* cmove */
3014
253k
        offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits);
3015
253k
        if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3016
253k
        if (offsetCode==0) offset = prevOffset;   /* cmove */
3017
253k
    }
3018
3019
    /* MatchLength */
3020
253k
    matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3021
253k
    if (matchLength == MaxML)
3022
7.01k
    {
3023
7.01k
        const U32 add = dumps<de ? *dumps++ : 0;
3024
7.01k
        if (add < 255) matchLength += add;
3025
325
        else if (dumps + 3 <= de)
3026
80
        {
3027
80
            matchLength = MEM_readLE24(dumps);
3028
80
            dumps += 3;
3029
80
        }
3030
7.01k
        if (dumps >= de) dumps = de-1;   /* late correction, to avoid read overflow (data is now corrupted anyway) */
3031
7.01k
    }
3032
253k
    matchLength += MINMATCH;
3033
3034
    /* save result */
3035
253k
    seq->litLength = litLength;
3036
253k
    seq->offset = offset;
3037
253k
    seq->matchLength = matchLength;
3038
253k
    seqState->dumps = dumps;
3039
253k
}
3040
3041
3042
static size_t ZSTD_execSequence(BYTE* op,
3043
                                seq_t sequence,
3044
                                const BYTE** litPtr, const BYTE* const litLimit,
3045
                                BYTE* const base, BYTE* const oend)
3046
253k
{
3047
253k
    static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4};   /* added */
3048
253k
    static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11};   /* subtracted */
3049
253k
    const BYTE* const ostart = op;
3050
253k
    BYTE* const oLitEnd = op + sequence.litLength;
3051
253k
    BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength;   /* risk : address space overflow (32-bits) */
3052
253k
    BYTE* const oend_8 = oend-8;
3053
253k
    const BYTE* const litEnd = *litPtr + sequence.litLength;
3054
3055
    /* checks */
3056
253k
    size_t const seqLength = sequence.litLength + sequence.matchLength;
3057
3058
253k
    if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
3059
253k
    if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
3060
    /* Now we know there are no overflow in literal nor match lengths, can use the pointer check */
3061
253k
    if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall);
3062
253k
    if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected);
3063
3064
253k
    if (oMatchEnd > oend) return ERROR(dstSize_tooSmall);   /* overwrite beyond dst buffer */
3065
253k
    if (litEnd > litLimit) return ERROR(corruption_detected);   /* overRead beyond lit buffer */
3066
3067
    /* copy Literals */
3068
253k
    ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength);   /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3069
253k
    op = oLitEnd;
3070
253k
    *litPtr = litEnd;   /* update for next sequence */
3071
3072
    /* copy Match */
3073
253k
    {
3074
253k
        const BYTE* match = op - sequence.offset;
3075
3076
        /* check */
3077
253k
        if (sequence.offset > (size_t)op) return ERROR(corruption_detected);   /* address space overflow test (this test seems kept by clang optimizer) */
3078
        //if (match > op) return ERROR(corruption_detected);   /* address space overflow test (is clang optimizer removing this test ?) */
3079
253k
        if (match < base) return ERROR(corruption_detected);
3080
3081
        /* close range match, overlap */
3082
253k
        if (sequence.offset < 8)
3083
225k
        {
3084
225k
            const int dec64 = dec64table[sequence.offset];
3085
225k
            op[0] = match[0];
3086
225k
            op[1] = match[1];
3087
225k
            op[2] = match[2];
3088
225k
            op[3] = match[3];
3089
225k
            match += dec32table[sequence.offset];
3090
225k
            ZSTD_copy4(op+4, match);
3091
225k
            match -= dec64;
3092
225k
        }
3093
27.9k
        else
3094
27.9k
        {
3095
27.9k
            ZSTD_copy8(op, match);
3096
27.9k
        }
3097
253k
        op += 8; match += 8;
3098
3099
253k
        if (oMatchEnd > oend-(16-MINMATCH))
3100
18
        {
3101
18
            if (op < oend_8)
3102
14
            {
3103
14
                ZSTD_wildcopy(op, match, oend_8 - op);
3104
14
                match += oend_8 - op;
3105
14
                op = oend_8;
3106
14
            }
3107
75
            while (op < oMatchEnd) *op++ = *match++;
3108
18
        }
3109
253k
        else
3110
253k
        {
3111
253k
            ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8);   /* works even if matchLength < 8 */
3112
253k
        }
3113
253k
    }
3114
3115
0
    return oMatchEnd - ostart;
3116
253k
}
3117
3118
static size_t ZSTD_decompressSequences(
3119
                               void* ctx,
3120
                               void* dst, size_t maxDstSize,
3121
                         const void* seqStart, size_t seqSize)
3122
5.46k
{
3123
5.46k
    ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
3124
5.46k
    const BYTE* ip = (const BYTE*)seqStart;
3125
5.46k
    const BYTE* const iend = ip + seqSize;
3126
5.46k
    BYTE* const ostart = (BYTE* const)dst;
3127
5.46k
    BYTE* op = ostart;
3128
5.46k
    BYTE* const oend = ostart + maxDstSize;
3129
5.46k
    size_t errorCode, dumpsLength;
3130
5.46k
    const BYTE* litPtr = dctx->litPtr;
3131
5.46k
    const BYTE* const litEnd = litPtr + dctx->litSize;
3132
5.46k
    int nbSeq;
3133
5.46k
    const BYTE* dumps;
3134
5.46k
    U32* DTableLL = dctx->LLTable;
3135
5.46k
    U32* DTableML = dctx->MLTable;
3136
5.46k
    U32* DTableOffb = dctx->OffTable;
3137
5.46k
    BYTE* const base = (BYTE*) (dctx->base);
3138
3139
    /* Build Decoding Tables */
3140
5.46k
    errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3141
5.46k
                                      DTableLL, DTableML, DTableOffb,
3142
5.46k
                                      ip, iend-ip);
3143
5.46k
    if (ZSTD_isError(errorCode)) return errorCode;
3144
5.37k
    ip += errorCode;
3145
3146
    /* Regen sequences */
3147
5.37k
    {
3148
5.37k
        seq_t sequence;
3149
5.37k
        seqState_t seqState;
3150
3151
5.37k
        memset(&sequence, 0, sizeof(sequence));
3152
5.37k
        seqState.dumps = dumps;
3153
5.37k
        seqState.dumpsEnd = dumps + dumpsLength;
3154
5.37k
        seqState.prevOffset = 1;
3155
5.37k
        errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
3156
5.37k
        if (ERR_isError(errorCode)) return ERROR(corruption_detected);
3157
5.33k
        FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
3158
5.33k
        FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
3159
5.33k
        FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
3160
3161
258k
        for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; )
3162
253k
        {
3163
253k
            size_t oneSeqSize;
3164
253k
            nbSeq--;
3165
253k
            ZSTD_decodeSequence(&sequence, &seqState);
3166
253k
            oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend);
3167
253k
            if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
3168
253k
            op += oneSeqSize;
3169
253k
        }
3170
3171
        /* check if reached exact end */
3172
5.16k
        if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected);   /* requested too much : data is corrupted */
3173
5.03k
        if (nbSeq<0) return ERROR(corruption_detected);   /* requested too many sequences : data is corrupted */
3174
3175
        /* last literal segment */
3176
5.03k
        {
3177
5.03k
            size_t lastLLSize = litEnd - litPtr;
3178
5.03k
            if (litPtr > litEnd) return ERROR(corruption_detected);
3179
5.03k
            if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3180
5.03k
            if (lastLLSize > 0) {
3181
2.73k
                if (op != litPtr) memmove(op, litPtr, lastLLSize);
3182
2.73k
                op += lastLLSize;
3183
2.73k
            }
3184
5.03k
        }
3185
5.03k
    }
3186
3187
0
    return op-ostart;
3188
5.03k
}
3189
3190
3191
static size_t ZSTD_decompressBlock(
3192
                            void* ctx,
3193
                            void* dst, size_t maxDstSize,
3194
                      const void* src, size_t srcSize)
3195
6.16k
{
3196
    /* blockType == blockCompressed */
3197
6.16k
    const BYTE* ip = (const BYTE*)src;
3198
3199
    /* Decode literals sub-block */
3200
6.16k
    size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize);
3201
6.16k
    if (ZSTD_isError(litCSize)) return litCSize;
3202
5.46k
    ip += litCSize;
3203
5.46k
    srcSize -= litCSize;
3204
3205
5.46k
    return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize);
3206
6.16k
}
3207
3208
3209
static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3210
1.20k
{
3211
1.20k
    const BYTE* ip = (const BYTE*)src;
3212
1.20k
    const BYTE* iend = ip + srcSize;
3213
1.20k
    BYTE* const ostart = (BYTE* const)dst;
3214
1.20k
    BYTE* op = ostart;
3215
1.20k
    BYTE* const oend = ostart + maxDstSize;
3216
1.20k
    size_t remainingSize = srcSize;
3217
1.20k
    U32 magicNumber;
3218
1.20k
    blockProperties_t blockProperties;
3219
3220
    /* Frame Header */
3221
1.20k
    if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
3222
1.20k
    magicNumber = MEM_readLE32(src);
3223
1.20k
    if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3224
1.20k
    ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3225
3226
    /* Loop on each block */
3227
6.58k
    while (1)
3228
6.58k
    {
3229
6.58k
        size_t decodedSize=0;
3230
6.58k
        size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
3231
6.58k
        if (ZSTD_isError(cBlockSize)) return cBlockSize;
3232
3233
6.58k
        ip += ZSTD_blockHeaderSize;
3234
6.58k
        remainingSize -= ZSTD_blockHeaderSize;
3235
6.58k
        if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3236
3237
6.58k
        switch(blockProperties.blockType)
3238
6.58k
        {
3239
6.16k
        case bt_compressed:
3240
6.16k
            decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize);
3241
6.16k
            break;
3242
372
        case bt_raw :
3243
372
            decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize);
3244
372
            break;
3245
7
        case bt_rle :
3246
7
            return ERROR(GENERIC);   /* not yet supported */
3247
0
            break;
3248
34
        case bt_end :
3249
            /* end of frame */
3250
34
            if (remainingSize) return ERROR(srcSize_wrong);
3251
34
            break;
3252
34
        default:
3253
0
            return ERROR(GENERIC);   /* impossible */
3254
6.58k
        }
3255
6.57k
        if (cBlockSize == 0) break;   /* bt_end */
3256
3257
6.47k
        if (ZSTD_isError(decodedSize)) return decodedSize;
3258
5.37k
        op += decodedSize;
3259
5.37k
        ip += cBlockSize;
3260
5.37k
        remainingSize -= cBlockSize;
3261
5.37k
    }
3262
3263
99
    return op-ostart;
3264
1.20k
}
3265
3266
static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3267
1.20k
{
3268
1.20k
    ZSTD_DCtx ctx;
3269
1.20k
    ctx.base = dst;
3270
1.20k
    return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
3271
1.20k
}
3272
3273
/* ZSTD_errorFrameSizeInfoLegacy() :
3274
   assumes `cSize` and `dBound` are _not_ NULL */
3275
static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3276
52
{
3277
52
    *cSize = ret;
3278
52
    *dBound = ZSTD_CONTENTSIZE_ERROR;
3279
52
}
3280
3281
void ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3282
2.45k
{
3283
2.45k
    const BYTE* ip = (const BYTE*)src;
3284
2.45k
    size_t remainingSize = srcSize;
3285
2.45k
    size_t nbBlocks = 0;
3286
2.45k
    U32 magicNumber;
3287
2.45k
    blockProperties_t blockProperties;
3288
3289
    /* Frame Header */
3290
2.45k
    if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {
3291
5
        ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3292
5
        return;
3293
5
    }
3294
2.45k
    magicNumber = MEM_readLE32(src);
3295
2.45k
    if (magicNumber != ZSTD_magicNumber) {
3296
0
        ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3297
0
        return;
3298
0
    }
3299
2.45k
    ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3300
3301
    /* Loop on each block */
3302
16.8k
    while (1)
3303
16.8k
    {
3304
16.8k
        size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
3305
16.8k
        if (ZSTD_isError(cBlockSize)) {
3306
2
            ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3307
2
            return;
3308
2
        }
3309
3310
16.8k
        ip += ZSTD_blockHeaderSize;
3311
16.8k
        remainingSize -= ZSTD_blockHeaderSize;
3312
16.8k
        if (cBlockSize > remainingSize) {
3313
45
            ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3314
45
            return;
3315
45
        }
3316
3317
16.7k
        if (cBlockSize == 0) break;   /* bt_end */
3318
3319
14.3k
        ip += cBlockSize;
3320
14.3k
        remainingSize -= cBlockSize;
3321
14.3k
        nbBlocks++;
3322
14.3k
    }
3323
3324
2.40k
    *cSize = ip - (const BYTE*)src;
3325
2.40k
    *dBound = nbBlocks * BLOCKSIZE;
3326
2.40k
}
3327
3328
/*******************************
3329
*  Streaming Decompression API
3330
*******************************/
3331
3332
static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx)
3333
0
{
3334
0
    dctx->expected = ZSTD_frameHeaderSize;
3335
0
    dctx->phase = 0;
3336
0
    dctx->previousDstEnd = NULL;
3337
0
    dctx->base = NULL;
3338
0
    return 0;
3339
0
}
3340
3341
static ZSTD_DCtx* ZSTD_createDCtx(void)
3342
0
{
3343
0
    ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
3344
0
    if (dctx==NULL) return NULL;
3345
0
    ZSTD_resetDCtx(dctx);
3346
0
    return dctx;
3347
0
}
3348
3349
static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
3350
0
{
3351
0
    free(dctx);
3352
0
    return 0;
3353
0
}
3354
3355
static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
3356
0
{
3357
0
    return dctx->expected;
3358
0
}
3359
3360
static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3361
0
{
3362
    /* Sanity check */
3363
0
    if (srcSize != ctx->expected) return ERROR(srcSize_wrong);
3364
0
    if (dst != ctx->previousDstEnd)  /* not contiguous */
3365
0
        ctx->base = dst;
3366
3367
    /* Decompress : frame header */
3368
0
    if (ctx->phase == 0)
3369
0
    {
3370
        /* Check frame magic header */
3371
0
        U32 magicNumber = MEM_readLE32(src);
3372
0
        if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3373
0
        ctx->phase = 1;
3374
0
        ctx->expected = ZSTD_blockHeaderSize;
3375
0
        return 0;
3376
0
    }
3377
3378
    /* Decompress : block header */
3379
0
    if (ctx->phase == 1)
3380
0
    {
3381
0
        blockProperties_t bp;
3382
0
        size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
3383
0
        if (ZSTD_isError(blockSize)) return blockSize;
3384
0
        if (bp.blockType == bt_end)
3385
0
        {
3386
0
            ctx->expected = 0;
3387
0
            ctx->phase = 0;
3388
0
        }
3389
0
        else
3390
0
        {
3391
0
            ctx->expected = blockSize;
3392
0
            ctx->bType = bp.blockType;
3393
0
            ctx->phase = 2;
3394
0
        }
3395
3396
0
        return 0;
3397
0
    }
3398
3399
    /* Decompress : block content */
3400
0
    {
3401
0
        size_t rSize;
3402
0
        switch(ctx->bType)
3403
0
        {
3404
0
        case bt_compressed:
3405
0
            rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
3406
0
            break;
3407
0
        case bt_raw :
3408
0
            rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
3409
0
            break;
3410
0
        case bt_rle :
3411
0
            return ERROR(GENERIC);   /* not yet handled */
3412
0
            break;
3413
0
        case bt_end :   /* should never happen (filtered at phase 1) */
3414
0
            rSize = 0;
3415
0
            break;
3416
0
        default:
3417
0
            return ERROR(GENERIC);
3418
0
        }
3419
0
        ctx->phase = 1;
3420
0
        ctx->expected = ZSTD_blockHeaderSize;
3421
0
        if (ZSTD_isError(rSize)) return rSize;
3422
0
        ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3423
0
        return rSize;
3424
0
    }
3425
3426
0
}
3427
3428
3429
/* wrapper layer */
3430
3431
unsigned ZSTDv02_isError(size_t code)
3432
0
{
3433
0
    return ZSTD_isError(code);
3434
0
}
3435
3436
size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
3437
                     const void* src, size_t compressedSize)
3438
1.20k
{
3439
1.20k
    return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3440
1.20k
}
3441
3442
ZSTDv02_Dctx* ZSTDv02_createDCtx(void)
3443
0
{
3444
0
    return (ZSTDv02_Dctx*)ZSTD_createDCtx();
3445
0
}
3446
3447
size_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx)
3448
0
{
3449
0
    return ZSTD_freeDCtx((ZSTD_DCtx*)dctx);
3450
0
}
3451
3452
size_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx)
3453
0
{
3454
0
    return ZSTD_resetDCtx((ZSTD_DCtx*)dctx);
3455
0
}
3456
3457
size_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx)
3458
0
{
3459
0
    return ZSTD_nextSrcSizeToDecompress((ZSTD_DCtx*)dctx);
3460
0
}
3461
3462
size_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3463
0
{
3464
0
    return ZSTD_decompressContinue((ZSTD_DCtx*)dctx, dst, maxDstSize, src, srcSize);
3465
0
}