zstdlib 0.9.0-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +6 -0
  3. data/CHANGES.md +78 -0
  4. data/Gemfile +3 -0
  5. data/README.md +107 -0
  6. data/Rakefile +59 -0
  7. data/ext/zstdlib_c/extconf.rb +54 -0
  8. data/ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c +4675 -0
  9. data/ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c +4702 -0
  10. data/ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c +4859 -0
  11. data/ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c +4864 -0
  12. data/ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c +4906 -0
  13. data/ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c +4895 -0
  14. data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
  15. data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
  16. data/ext/zstdlib_c/zlib-1.2.11/adler32.c +186 -0
  17. data/ext/zstdlib_c/zlib-1.2.11/compress.c +86 -0
  18. data/ext/zstdlib_c/zlib-1.2.11/crc32.c +442 -0
  19. data/ext/zstdlib_c/zlib-1.2.11/crc32.h +441 -0
  20. data/ext/zstdlib_c/zlib-1.2.11/deflate.c +2163 -0
  21. data/ext/zstdlib_c/zlib-1.2.11/deflate.h +349 -0
  22. data/ext/zstdlib_c/zlib-1.2.11/gzclose.c +25 -0
  23. data/ext/zstdlib_c/zlib-1.2.11/gzguts.h +218 -0
  24. data/ext/zstdlib_c/zlib-1.2.11/gzlib.c +637 -0
  25. data/ext/zstdlib_c/zlib-1.2.11/gzread.c +654 -0
  26. data/ext/zstdlib_c/zlib-1.2.11/gzwrite.c +665 -0
  27. data/ext/zstdlib_c/zlib-1.2.11/infback.c +640 -0
  28. data/ext/zstdlib_c/zlib-1.2.11/inffast.c +323 -0
  29. data/ext/zstdlib_c/zlib-1.2.11/inffast.h +11 -0
  30. data/ext/zstdlib_c/zlib-1.2.11/inffixed.h +94 -0
  31. data/ext/zstdlib_c/zlib-1.2.11/inflate.c +1561 -0
  32. data/ext/zstdlib_c/zlib-1.2.11/inflate.h +125 -0
  33. data/ext/zstdlib_c/zlib-1.2.11/inftrees.c +304 -0
  34. data/ext/zstdlib_c/zlib-1.2.11/inftrees.h +62 -0
  35. data/ext/zstdlib_c/zlib-1.2.11/trees.c +1203 -0
  36. data/ext/zstdlib_c/zlib-1.2.11/trees.h +128 -0
  37. data/ext/zstdlib_c/zlib-1.2.11/uncompr.c +93 -0
  38. data/ext/zstdlib_c/zlib-1.2.11/zconf.h +534 -0
  39. data/ext/zstdlib_c/zlib-1.2.11/zlib.h +1912 -0
  40. data/ext/zstdlib_c/zlib-1.2.11/zutil.c +325 -0
  41. data/ext/zstdlib_c/zlib-1.2.11/zutil.h +271 -0
  42. data/ext/zstdlib_c/zlib.mk +14 -0
  43. data/ext/zstdlib_c/zlibwrapper/zlibwrapper.c +10 -0
  44. data/ext/zstdlib_c/zlibwrapper.mk +14 -0
  45. data/ext/zstdlib_c/zstd-1.5.2/lib/common/bitstream.h +478 -0
  46. data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
  47. data/ext/zstdlib_c/zstd-1.5.2/lib/common/cpu.h +213 -0
  48. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.c +24 -0
  49. data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.h +107 -0
  50. data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
  51. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.c +56 -0
  52. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
  53. data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse.h +717 -0
  54. data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse_decompress.c +403 -0
  55. data/ext/zstdlib_c/zstd-1.5.2/lib/common/huf.h +364 -0
  56. data/ext/zstdlib_c/zstd-1.5.2/lib/common/mem.h +442 -0
  57. data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.c +355 -0
  58. data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.h +84 -0
  59. data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
  60. data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.c +122 -0
  61. data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.h +155 -0
  62. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
  63. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
  64. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_common.c +83 -0
  65. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
  66. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_internal.h +493 -0
  67. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
  68. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
  69. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/fse_compress.c +741 -0
  70. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.c +181 -0
  71. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.h +75 -0
  72. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
  73. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress.c +6327 -0
  74. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_internal.h +1458 -0
  75. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.c +159 -0
  76. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.h +31 -0
  77. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.c +442 -0
  78. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.h +54 -0
  79. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.c +573 -0
  80. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.h +32 -0
  81. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_cwksp.h +676 -0
  82. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.c +696 -0
  83. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.h +38 -0
  84. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.c +675 -0
  85. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.h +37 -0
  86. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
  87. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
  88. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.c +724 -0
  89. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.h +117 -0
  90. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
  91. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.c +1446 -0
  92. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.h +56 -0
  93. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.c +1859 -0
  94. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
  95. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress.c +1889 -0
  96. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
  97. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.c +244 -0
  98. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.h +44 -0
  99. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress.c +2230 -0
  100. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.c +2072 -0
  101. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.h +68 -0
  102. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_internal.h +236 -0
  103. data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
  104. data/ext/zstdlib_c/zstd-1.5.2/lib/zstd.h +2575 -0
  105. data/ext/zstdlib_c/zstd-1.5.2/lib/zstd_errors.h +95 -0
  106. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzclose.c +28 -0
  107. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzcompatibility.h +68 -0
  108. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzguts.h +229 -0
  109. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzlib.c +640 -0
  110. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzread.c +678 -0
  111. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzwrite.c +671 -0
  112. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.c +1198 -0
  113. data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.h +88 -0
  114. data/ext/zstdlib_c/zstd.mk +15 -0
  115. data/lib/3.1/zstdlib_c.so +0 -0
  116. data/lib/zstdlib.rb +6 -0
  117. data/test/zstdlib_test.rb +21 -0
  118. metadata +232 -0
@@ -0,0 +1,478 @@
1
+ /* ******************************************************************
2
+ * bitstream
3
+ * Part of FSE library
4
+ * Copyright (c) Yann Collet, Facebook, Inc.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
13
+ ****************************************************************** */
14
+ #ifndef BITSTREAM_H_MODULE
15
+ #define BITSTREAM_H_MODULE
16
+
17
+ #if defined (__cplusplus)
18
+ extern "C" {
19
+ #endif
20
+ /*
21
+ * This API consists of small unitary functions, which must be inlined for best performance.
22
+ * Since link-time-optimization is not available for all compilers,
23
+ * these functions are defined into a .h to be included.
24
+ */
25
+
26
+ /*-****************************************
27
+ * Dependencies
28
+ ******************************************/
29
+ #include "mem.h" /* unaligned access routines */
30
+ #include "compiler.h" /* UNLIKELY() */
31
+ #include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */
32
+ #include "error_private.h" /* error codes and messages */
33
+
34
+
35
+ /*=========================================
36
+ * Target specific
37
+ =========================================*/
38
+ #ifndef ZSTD_NO_INTRINSICS
39
+ # if defined(__BMI__) && defined(__GNUC__)
40
+ # include <immintrin.h> /* support for bextr (experimental) */
41
+ # elif defined(__ICCARM__)
42
+ # include <intrinsics.h>
43
+ # endif
44
+ #endif
45
+
46
+ #define STREAM_ACCUMULATOR_MIN_32 25
47
+ #define STREAM_ACCUMULATOR_MIN_64 57
48
+ #define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
49
+
50
+
51
+ /*-******************************************
52
+ * bitStream encoding API (write forward)
53
+ ********************************************/
54
+ /* bitStream can mix input from multiple sources.
55
+ * A critical property of these streams is that they encode and decode in **reverse** direction.
56
+ * So the first bit sequence you add will be the last to be read, like a LIFO stack.
57
+ */
58
+ typedef struct {
59
+ size_t bitContainer;
60
+ unsigned bitPos;
61
+ char* startPtr;
62
+ char* ptr;
63
+ char* endPtr;
64
+ } BIT_CStream_t;
65
+
66
+ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
67
+ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
68
+ MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
69
+ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
70
+
71
+ /* Start with initCStream, providing the size of buffer to write into.
72
+ * bitStream will never write outside of this buffer.
73
+ * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
74
+ *
75
+ * bits are first added to a local register.
76
+ * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
77
+ * Writing data into memory is an explicit operation, performed by the flushBits function.
78
+ * Hence keep track how many bits are potentially stored into local register to avoid register overflow.
79
+ * After a flushBits, a maximum of 7 bits might still be stored into local register.
80
+ *
81
+ * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
82
+ *
83
+ * Last operation is to close the bitStream.
84
+ * The function returns the final size of CStream in bytes.
85
+ * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
86
+ */
87
+
88
+
89
+ /*-********************************************
90
+ * bitStream decoding API (read backward)
91
+ **********************************************/
92
+ typedef struct {
93
+ size_t bitContainer;
94
+ unsigned bitsConsumed;
95
+ const char* ptr;
96
+ const char* start;
97
+ const char* limitPtr;
98
+ } BIT_DStream_t;
99
+
100
+ typedef enum { BIT_DStream_unfinished = 0,
101
+ BIT_DStream_endOfBuffer = 1,
102
+ BIT_DStream_completed = 2,
103
+ BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
104
+ /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
105
+
106
+ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
107
+ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
108
+ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
109
+ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
110
+
111
+
112
+ /* Start by invoking BIT_initDStream().
113
+ * A chunk of the bitStream is then stored into a local register.
114
+ * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
115
+ * You can then retrieve bitFields stored into the local register, **in reverse order**.
116
+ * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
117
+ * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
118
+ * Otherwise, it can be less than that, so proceed accordingly.
119
+ * Checking if DStream has reached its end can be performed with BIT_endOfDStream().
120
+ */
121
+
122
+
123
+ /*-****************************************
124
+ * unsafe API
125
+ ******************************************/
126
+ MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
127
+ /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
128
+
129
+ MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
130
+ /* unsafe version; does not check buffer overflow */
131
+
132
+ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
133
+ /* faster, but works only if nbBits >= 1 */
134
+
135
+
136
+
137
+ /*-**************************************************************
138
+ * Internal functions
139
+ ****************************************************************/
140
+ MEM_STATIC unsigned BIT_highbit32 (U32 val)
141
+ {
142
+ assert(val != 0);
143
+ {
144
+ # if defined(_MSC_VER) /* Visual */
145
+ # if STATIC_BMI2 == 1
146
+ return _lzcnt_u32(val) ^ 31;
147
+ # else
148
+ if (val != 0) {
149
+ unsigned long r;
150
+ _BitScanReverse(&r, val);
151
+ return (unsigned)r;
152
+ } else {
153
+ /* Should not reach this code path */
154
+ __assume(0);
155
+ }
156
+ # endif
157
+ # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
158
+ return __builtin_clz (val) ^ 31;
159
+ # elif defined(__ICCARM__) /* IAR Intrinsic */
160
+ return 31 - __CLZ(val);
161
+ # else /* Software version */
162
+ static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
163
+ 11, 14, 16, 18, 22, 25, 3, 30,
164
+ 8, 12, 20, 28, 15, 17, 24, 7,
165
+ 19, 27, 23, 6, 26, 5, 4, 31 };
166
+ U32 v = val;
167
+ v |= v >> 1;
168
+ v |= v >> 2;
169
+ v |= v >> 4;
170
+ v |= v >> 8;
171
+ v |= v >> 16;
172
+ return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
173
+ # endif
174
+ }
175
+ }
176
+
177
+ /*===== Local Constants =====*/
178
+ static const unsigned BIT_mask[] = {
179
+ 0, 1, 3, 7, 0xF, 0x1F,
180
+ 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
181
+ 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF,
182
+ 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
183
+ 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,
184
+ 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */
185
+ #define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))
186
+
187
+ /*-**************************************************************
188
+ * bitStream encoding
189
+ ****************************************************************/
190
+ /*! BIT_initCStream() :
191
+ * `dstCapacity` must be > sizeof(size_t)
192
+ * @return : 0 if success,
193
+ * otherwise an error code (can be tested using ERR_isError()) */
194
+ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
195
+ void* startPtr, size_t dstCapacity)
196
+ {
197
+ bitC->bitContainer = 0;
198
+ bitC->bitPos = 0;
199
+ bitC->startPtr = (char*)startPtr;
200
+ bitC->ptr = bitC->startPtr;
201
+ bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer);
202
+ if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall);
203
+ return 0;
204
+ }
205
+
206
+ /*! BIT_addBits() :
207
+ * can add up to 31 bits into `bitC`.
208
+ * Note : does not check for register overflow ! */
209
+ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
210
+ size_t value, unsigned nbBits)
211
+ {
212
+ DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32);
213
+ assert(nbBits < BIT_MASK_SIZE);
214
+ assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
215
+ bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
216
+ bitC->bitPos += nbBits;
217
+ }
218
+
219
+ /*! BIT_addBitsFast() :
220
+ * works only if `value` is _clean_,
221
+ * meaning all high bits above nbBits are 0 */
222
+ MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
223
+ size_t value, unsigned nbBits)
224
+ {
225
+ assert((value>>nbBits) == 0);
226
+ assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
227
+ bitC->bitContainer |= value << bitC->bitPos;
228
+ bitC->bitPos += nbBits;
229
+ }
230
+
231
+ /*! BIT_flushBitsFast() :
232
+ * assumption : bitContainer has not overflowed
233
+ * unsafe version; does not check buffer overflow */
234
+ MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
235
+ {
236
+ size_t const nbBytes = bitC->bitPos >> 3;
237
+ assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
238
+ assert(bitC->ptr <= bitC->endPtr);
239
+ MEM_writeLEST(bitC->ptr, bitC->bitContainer);
240
+ bitC->ptr += nbBytes;
241
+ bitC->bitPos &= 7;
242
+ bitC->bitContainer >>= nbBytes*8;
243
+ }
244
+
245
+ /*! BIT_flushBits() :
246
+ * assumption : bitContainer has not overflowed
247
+ * safe version; check for buffer overflow, and prevents it.
248
+ * note : does not signal buffer overflow.
249
+ * overflow will be revealed later on using BIT_closeCStream() */
250
+ MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
251
+ {
252
+ size_t const nbBytes = bitC->bitPos >> 3;
253
+ assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
254
+ assert(bitC->ptr <= bitC->endPtr);
255
+ MEM_writeLEST(bitC->ptr, bitC->bitContainer);
256
+ bitC->ptr += nbBytes;
257
+ if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
258
+ bitC->bitPos &= 7;
259
+ bitC->bitContainer >>= nbBytes*8;
260
+ }
261
+
262
+ /*! BIT_closeCStream() :
263
+ * @return : size of CStream, in bytes,
264
+ * or 0 if it could not fit into dstBuffer */
265
+ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
266
+ {
267
+ BIT_addBitsFast(bitC, 1, 1); /* endMark */
268
+ BIT_flushBits(bitC);
269
+ if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */
270
+ return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
271
+ }
272
+
273
+
274
+ /*-********************************************************
275
+ * bitStream decoding
276
+ **********************************************************/
277
+ /*! BIT_initDStream() :
278
+ * Initialize a BIT_DStream_t.
279
+ * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
280
+ * `srcSize` must be the *exact* size of the bitStream, in bytes.
281
+ * @return : size of stream (== srcSize), or an errorCode if a problem is detected
282
+ */
283
+ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
284
+ {
285
+ if (srcSize < 1) { ZSTD_memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
286
+
287
+ bitD->start = (const char*)srcBuffer;
288
+ bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
289
+
290
+ if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
291
+ bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
292
+ bitD->bitContainer = MEM_readLEST(bitD->ptr);
293
+ { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
294
+ bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
295
+ if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
296
+ } else {
297
+ bitD->ptr = bitD->start;
298
+ bitD->bitContainer = *(const BYTE*)(bitD->start);
299
+ switch(srcSize)
300
+ {
301
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
302
+ ZSTD_FALLTHROUGH;
303
+
304
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
305
+ ZSTD_FALLTHROUGH;
306
+
307
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
308
+ ZSTD_FALLTHROUGH;
309
+
310
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
311
+ ZSTD_FALLTHROUGH;
312
+
313
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
314
+ ZSTD_FALLTHROUGH;
315
+
316
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
317
+ ZSTD_FALLTHROUGH;
318
+
319
+ default: break;
320
+ }
321
+ { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
322
+ bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
323
+ if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
324
+ }
325
+ bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
326
+ }
327
+
328
+ return srcSize;
329
+ }
330
+
331
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
332
+ {
333
+ return bitContainer >> start;
334
+ }
335
+
336
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
337
+ {
338
+ U32 const regMask = sizeof(bitContainer)*8 - 1;
339
+ /* if start > regMask, bitstream is corrupted, and result is undefined */
340
+ assert(nbBits < BIT_MASK_SIZE);
341
+ /* x86 transform & ((1 << nbBits) - 1) to bzhi instruction, it is better
342
+ * than accessing memory. When bmi2 instruction is not present, we consider
343
+ * such cpus old (pre-Haswell, 2013) and their performance is not of that
344
+ * importance.
345
+ */
346
+ #if defined(__x86_64__) || defined(_M_X86)
347
+ return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1);
348
+ #else
349
+ return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
350
+ #endif
351
+ }
352
+
353
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
354
+ {
355
+ #if defined(STATIC_BMI2) && STATIC_BMI2 == 1
356
+ return _bzhi_u64(bitContainer, nbBits);
357
+ #else
358
+ assert(nbBits < BIT_MASK_SIZE);
359
+ return bitContainer & BIT_mask[nbBits];
360
+ #endif
361
+ }
362
+
363
+ /*! BIT_lookBits() :
364
+ * Provides next n bits from local register.
365
+ * local register is not modified.
366
+ * On 32-bits, maxNbBits==24.
367
+ * On 64-bits, maxNbBits==56.
368
+ * @return : value extracted */
369
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
370
+ {
371
+ /* arbitrate between double-shift and shift+mask */
372
+ #if 1
373
+ /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8,
374
+ * bitstream is likely corrupted, and result is undefined */
375
+ return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
376
+ #else
377
+ /* this code path is slower on my os-x laptop */
378
+ U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
379
+ return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask);
380
+ #endif
381
+ }
382
+
383
+ /*! BIT_lookBitsFast() :
384
+ * unsafe version; only works if nbBits >= 1 */
385
+ MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
386
+ {
387
+ U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
388
+ assert(nbBits >= 1);
389
+ return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
390
+ }
391
+
392
+ MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
393
+ {
394
+ bitD->bitsConsumed += nbBits;
395
+ }
396
+
397
+ /*! BIT_readBits() :
398
+ * Read (consume) next n bits from local register and update.
399
+ * Pay attention to not read more than nbBits contained into local register.
400
+ * @return : extracted value. */
401
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
402
+ {
403
+ size_t const value = BIT_lookBits(bitD, nbBits);
404
+ BIT_skipBits(bitD, nbBits);
405
+ return value;
406
+ }
407
+
408
+ /*! BIT_readBitsFast() :
409
+ * unsafe version; only works only if nbBits >= 1 */
410
+ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
411
+ {
412
+ size_t const value = BIT_lookBitsFast(bitD, nbBits);
413
+ assert(nbBits >= 1);
414
+ BIT_skipBits(bitD, nbBits);
415
+ return value;
416
+ }
417
+
418
+ /*! BIT_reloadDStreamFast() :
419
+ * Similar to BIT_reloadDStream(), but with two differences:
420
+ * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
421
+ * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this
422
+ * point you must use BIT_reloadDStream() to reload.
423
+ */
424
+ MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
425
+ {
426
+ if (UNLIKELY(bitD->ptr < bitD->limitPtr))
427
+ return BIT_DStream_overflow;
428
+ assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
429
+ bitD->ptr -= bitD->bitsConsumed >> 3;
430
+ bitD->bitsConsumed &= 7;
431
+ bitD->bitContainer = MEM_readLEST(bitD->ptr);
432
+ return BIT_DStream_unfinished;
433
+ }
434
+
435
+ /*! BIT_reloadDStream() :
436
+ * Refill `bitD` from buffer previously set in BIT_initDStream() .
437
+ * This function is safe, it guarantees it will not read beyond src buffer.
438
+ * @return : status of `BIT_DStream_t` internal register.
439
+ * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
440
+ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
441
+ {
442
+ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
443
+ return BIT_DStream_overflow;
444
+
445
+ if (bitD->ptr >= bitD->limitPtr) {
446
+ return BIT_reloadDStreamFast(bitD);
447
+ }
448
+ if (bitD->ptr == bitD->start) {
449
+ if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
450
+ return BIT_DStream_completed;
451
+ }
452
+ /* start < ptr < limitPtr */
453
+ { U32 nbBytes = bitD->bitsConsumed >> 3;
454
+ BIT_DStream_status result = BIT_DStream_unfinished;
455
+ if (bitD->ptr - nbBytes < bitD->start) {
456
+ nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
457
+ result = BIT_DStream_endOfBuffer;
458
+ }
459
+ bitD->ptr -= nbBytes;
460
+ bitD->bitsConsumed -= nbBytes*8;
461
+ bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */
462
+ return result;
463
+ }
464
+ }
465
+
466
+ /*! BIT_endOfDStream() :
467
+ * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).
468
+ */
469
+ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
470
+ {
471
+ return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
472
+ }
473
+
474
+ #if defined (__cplusplus)
475
+ }
476
+ #endif
477
+
478
+ #endif /* BITSTREAM_H_MODULE */