zstd-ruby 1.3.1.1 → 1.3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/zstdruby/libzstd/.gitignore +1 -0
  4. data/ext/zstdruby/libzstd/Makefile +40 -26
  5. data/ext/zstdruby/libzstd/README.md +68 -45
  6. data/ext/zstdruby/libzstd/common/bitstream.h +35 -23
  7. data/ext/zstdruby/libzstd/common/compiler.h +1 -0
  8. data/ext/zstdruby/libzstd/common/error_private.c +4 -2
  9. data/ext/zstdruby/libzstd/common/error_private.h +4 -4
  10. data/ext/zstdruby/libzstd/common/fse.h +1 -1
  11. data/ext/zstdruby/libzstd/common/huf.h +1 -1
  12. data/ext/zstdruby/libzstd/common/mem.h +1 -0
  13. data/ext/zstdruby/libzstd/common/pool.c +61 -46
  14. data/ext/zstdruby/libzstd/common/pool.h +4 -0
  15. data/ext/zstdruby/libzstd/common/threading.c +11 -15
  16. data/ext/zstdruby/libzstd/common/threading.h +52 -32
  17. data/ext/zstdruby/libzstd/common/zstd_common.c +2 -2
  18. data/ext/zstdruby/libzstd/common/zstd_errors.h +3 -1
  19. data/ext/zstdruby/libzstd/common/zstd_internal.h +95 -21
  20. data/ext/zstdruby/libzstd/compress/fse_compress.c +3 -1
  21. data/ext/zstdruby/libzstd/compress/huf_compress.c +4 -3
  22. data/ext/zstdruby/libzstd/compress/zstd_compress.c +922 -2102
  23. data/ext/zstdruby/libzstd/compress/zstd_compress.h +307 -0
  24. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +308 -0
  25. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +28 -0
  26. data/ext/zstdruby/libzstd/compress/zstd_fast.c +242 -0
  27. data/ext/zstdruby/libzstd/compress/zstd_fast.h +30 -0
  28. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +749 -0
  29. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +38 -0
  30. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +707 -0
  31. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +67 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_opt.c +957 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +14 -922
  34. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +210 -133
  35. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +20 -3
  36. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +373 -196
  37. data/ext/zstdruby/libzstd/deprecated/zbuff.h +1 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +1 -0
  39. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -0
  40. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/cover.c +33 -22
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +8 -5
  43. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +1 -0
  44. data/ext/zstdruby/libzstd/dll/example/Makefile +5 -5
  45. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +1 -0
  46. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +1 -0
  47. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -0
  48. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +1 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +1 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +1 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +1 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +1 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +1 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +1 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -0
  60. data/ext/zstdruby/libzstd/zstd.h +366 -118
  61. data/lib/zstd-ruby/version.rb +1 -1
  62. metadata +11 -1
@@ -0,0 +1,307 @@
1
+ /*
2
+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
+ #ifndef ZSTD_COMPRESS_H
13
+ #define ZSTD_COMPRESS_H
14
+
15
+ /*-*************************************
16
+ * Dependencies
17
+ ***************************************/
18
+ #include "zstd_internal.h"
19
+ #ifdef ZSTD_MULTITHREAD
20
+ # include "zstdmt_compress.h"
21
+ #endif
22
+
23
+ #if defined (__cplusplus)
24
+ extern "C" {
25
+ #endif
26
+
27
+ /*-*************************************
28
+ * Constants
29
+ ***************************************/
30
+ static const U32 g_searchStrength = 8;
31
+ #define HASH_READ_SIZE 8
32
+
33
+
34
+ /*-*************************************
35
+ * Context memory management
36
+ ***************************************/
37
+ typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
38
+ typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage;
39
+
40
+ typedef struct ZSTD_prefixDict_s {
41
+ const void* dict;
42
+ size_t dictSize;
43
+ ZSTD_dictMode_e dictMode;
44
+ } ZSTD_prefixDict;
45
+
46
+ struct ZSTD_CCtx_s {
47
+ const BYTE* nextSrc; /* next block here to continue on current prefix */
48
+ const BYTE* base; /* All regular indexes relative to this position */
49
+ const BYTE* dictBase; /* extDict indexes relative to this position */
50
+ U32 dictLimit; /* below that point, need extDict */
51
+ U32 lowLimit; /* below that point, no more data */
52
+ U32 nextToUpdate; /* index from which to continue dictionary update */
53
+ U32 nextToUpdate3; /* index from which to continue dictionary update */
54
+ U32 hashLog3; /* dispatch table : larger == faster, more memory */
55
+ U32 loadedDictEnd; /* index of end of dictionary */
56
+ ZSTD_compressionStage_e stage;
57
+ U32 dictID;
58
+ ZSTD_CCtx_params requestedParams;
59
+ ZSTD_CCtx_params appliedParams;
60
+ void* workSpace;
61
+ size_t workSpaceSize;
62
+ size_t blockSize;
63
+ U64 pledgedSrcSizePlusOne; /* this way, 0 (default) == unknown */
64
+ U64 consumedSrcSize;
65
+ XXH64_state_t xxhState;
66
+ ZSTD_customMem customMem;
67
+ size_t staticSize;
68
+
69
+ seqStore_t seqStore; /* sequences storage ptrs */
70
+ optState_t optState;
71
+ ldmState_t ldmState; /* long distance matching state */
72
+ U32* hashTable;
73
+ U32* hashTable3;
74
+ U32* chainTable;
75
+ ZSTD_entropyCTables_t* entropy;
76
+
77
+ /* streaming */
78
+ char* inBuff;
79
+ size_t inBuffSize;
80
+ size_t inToCompress;
81
+ size_t inBuffPos;
82
+ size_t inBuffTarget;
83
+ char* outBuff;
84
+ size_t outBuffSize;
85
+ size_t outBuffContentSize;
86
+ size_t outBuffFlushedSize;
87
+ ZSTD_cStreamStage streamStage;
88
+ U32 frameEnded;
89
+
90
+ /* Dictionary */
91
+ ZSTD_CDict* cdictLocal;
92
+ const ZSTD_CDict* cdict;
93
+ ZSTD_prefixDict prefixDict; /* single-usage dictionary */
94
+
95
+ /* Multi-threading */
96
+ #ifdef ZSTD_MULTITHREAD
97
+ ZSTDMT_CCtx* mtctx;
98
+ #endif
99
+ };
100
+
101
+
102
+ static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7,
103
+ 8, 9, 10, 11, 12, 13, 14, 15,
104
+ 16, 16, 17, 17, 18, 18, 19, 19,
105
+ 20, 20, 20, 20, 21, 21, 21, 21,
106
+ 22, 22, 22, 22, 22, 22, 22, 22,
107
+ 23, 23, 23, 23, 23, 23, 23, 23,
108
+ 24, 24, 24, 24, 24, 24, 24, 24,
109
+ 24, 24, 24, 24, 24, 24, 24, 24 };
110
+
111
+ static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
112
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
113
+ 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
114
+ 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
115
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
116
+ 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
117
+ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
118
+ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
119
+
120
+ /*! ZSTD_storeSeq() :
121
+ Store a sequence (literal length, literals, offset code and match length code) into seqStore_t.
122
+ `offsetCode` : distance to match, or 0 == repCode.
123
+ `matchCode` : matchLength - MINMATCH
124
+ */
125
+ MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const void* literals, U32 offsetCode, size_t matchCode)
126
+ {
127
+ #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 6)
128
+ static const BYTE* g_start = NULL;
129
+ U32 const pos = (U32)((const BYTE*)literals - g_start);
130
+ if (g_start==NULL) g_start = (const BYTE*)literals;
131
+ if ((pos > 0) && (pos < 1000000000))
132
+ DEBUGLOG(6, "Cpos %6u :%5u literals & match %3u bytes at distance %6u",
133
+ pos, (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode);
134
+ #endif
135
+ /* copy Literals */
136
+ assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + 128 KB);
137
+ ZSTD_wildcopy(seqStorePtr->lit, literals, litLength);
138
+ seqStorePtr->lit += litLength;
139
+
140
+ /* literal Length */
141
+ if (litLength>0xFFFF) {
142
+ seqStorePtr->longLengthID = 1;
143
+ seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
144
+ }
145
+ seqStorePtr->sequences[0].litLength = (U16)litLength;
146
+
147
+ /* match offset */
148
+ seqStorePtr->sequences[0].offset = offsetCode + 1;
149
+
150
+ /* match Length */
151
+ if (matchCode>0xFFFF) {
152
+ seqStorePtr->longLengthID = 2;
153
+ seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
154
+ }
155
+ seqStorePtr->sequences[0].matchLength = (U16)matchCode;
156
+
157
+ seqStorePtr->sequences++;
158
+ }
159
+
160
+
161
+ /*-*************************************
162
+ * Match length counter
163
+ ***************************************/
164
+ static unsigned ZSTD_NbCommonBytes (register size_t val)
165
+ {
166
+ if (MEM_isLittleEndian()) {
167
+ if (MEM_64bits()) {
168
+ # if defined(_MSC_VER) && defined(_WIN64)
169
+ unsigned long r = 0;
170
+ _BitScanForward64( &r, (U64)val );
171
+ return (unsigned)(r>>3);
172
+ # elif defined(__GNUC__) && (__GNUC__ >= 4)
173
+ return (__builtin_ctzll((U64)val) >> 3);
174
+ # else
175
+ static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2,
176
+ 0, 3, 1, 3, 1, 4, 2, 7,
177
+ 0, 2, 3, 6, 1, 5, 3, 5,
178
+ 1, 3, 4, 4, 2, 5, 6, 7,
179
+ 7, 0, 1, 2, 3, 3, 4, 6,
180
+ 2, 6, 5, 5, 3, 4, 5, 6,
181
+ 7, 1, 2, 4, 6, 4, 4, 5,
182
+ 7, 2, 6, 5, 7, 6, 7, 7 };
183
+ return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
184
+ # endif
185
+ } else { /* 32 bits */
186
+ # if defined(_MSC_VER)
187
+ unsigned long r=0;
188
+ _BitScanForward( &r, (U32)val );
189
+ return (unsigned)(r>>3);
190
+ # elif defined(__GNUC__) && (__GNUC__ >= 3)
191
+ return (__builtin_ctz((U32)val) >> 3);
192
+ # else
193
+ static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
194
+ 3, 2, 2, 1, 3, 2, 0, 1,
195
+ 3, 3, 1, 2, 2, 2, 2, 0,
196
+ 3, 1, 2, 0, 1, 0, 1, 1 };
197
+ return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
198
+ # endif
199
+ }
200
+ } else { /* Big Endian CPU */
201
+ if (MEM_64bits()) {
202
+ # if defined(_MSC_VER) && defined(_WIN64)
203
+ unsigned long r = 0;
204
+ _BitScanReverse64( &r, val );
205
+ return (unsigned)(r>>3);
206
+ # elif defined(__GNUC__) && (__GNUC__ >= 4)
207
+ return (__builtin_clzll(val) >> 3);
208
+ # else
209
+ unsigned r;
210
+ const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */
211
+ if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; }
212
+ if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
213
+ r += (!val);
214
+ return r;
215
+ # endif
216
+ } else { /* 32 bits */
217
+ # if defined(_MSC_VER)
218
+ unsigned long r = 0;
219
+ _BitScanReverse( &r, (unsigned long)val );
220
+ return (unsigned)(r>>3);
221
+ # elif defined(__GNUC__) && (__GNUC__ >= 3)
222
+ return (__builtin_clz((U32)val) >> 3);
223
+ # else
224
+ unsigned r;
225
+ if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
226
+ r += (!val);
227
+ return r;
228
+ # endif
229
+ } }
230
+ }
231
+
232
+
233
+ MEM_STATIC size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit)
234
+ {
235
+ const BYTE* const pStart = pIn;
236
+ const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
237
+
238
+ while (pIn < pInLoopLimit) {
239
+ size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
240
+ if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
241
+ pIn += ZSTD_NbCommonBytes(diff);
242
+ return (size_t)(pIn - pStart);
243
+ }
244
+ if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
245
+ if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
246
+ if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
247
+ return (size_t)(pIn - pStart);
248
+ }
249
+
250
+ /** ZSTD_count_2segments() :
251
+ * can count match length with `ip` & `match` in 2 different segments.
252
+ * convention : on reaching mEnd, match count continue starting from iStart
253
+ */
254
+ MEM_STATIC size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart)
255
+ {
256
+ const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd);
257
+ size_t const matchLength = ZSTD_count(ip, match, vEnd);
258
+ if (match + matchLength != mEnd) return matchLength;
259
+ return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd);
260
+ }
261
+
262
+
263
+ /*-*************************************
264
+ * Hashes
265
+ ***************************************/
266
+ static const U32 prime3bytes = 506832829U;
267
+ static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; }
268
+ MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */
269
+
270
+ static const U32 prime4bytes = 2654435761U;
271
+ static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; }
272
+ static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); }
273
+
274
+ static const U64 prime5bytes = 889523592379ULL;
275
+ static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64-40)) * prime5bytes) >> (64-h)) ; }
276
+ static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); }
277
+
278
+ static const U64 prime6bytes = 227718039650203ULL;
279
+ static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64-48)) * prime6bytes) >> (64-h)) ; }
280
+ static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); }
281
+
282
+ static const U64 prime7bytes = 58295818150454627ULL;
283
+ static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64-56)) * prime7bytes) >> (64-h)) ; }
284
+ static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); }
285
+
286
+ static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
287
+ static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; }
288
+ static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); }
289
+
290
+ MEM_STATIC size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls)
291
+ {
292
+ switch(mls)
293
+ {
294
+ default:
295
+ case 4: return ZSTD_hash4Ptr(p, hBits);
296
+ case 5: return ZSTD_hash5Ptr(p, hBits);
297
+ case 6: return ZSTD_hash6Ptr(p, hBits);
298
+ case 7: return ZSTD_hash7Ptr(p, hBits);
299
+ case 8: return ZSTD_hash8Ptr(p, hBits);
300
+ }
301
+ }
302
+
303
+ #if defined (__cplusplus)
304
+ }
305
+ #endif
306
+
307
+ #endif /* ZSTD_COMPRESS_H */
@@ -0,0 +1,308 @@
1
+ /*
2
+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
+ #include "zstd_double_fast.h"
12
+
13
+
14
+ void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls)
15
+ {
16
+ U32* const hashLarge = cctx->hashTable;
17
+ U32 const hBitsL = cctx->appliedParams.cParams.hashLog;
18
+ U32* const hashSmall = cctx->chainTable;
19
+ U32 const hBitsS = cctx->appliedParams.cParams.chainLog;
20
+ const BYTE* const base = cctx->base;
21
+ const BYTE* ip = base + cctx->nextToUpdate;
22
+ const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
23
+ const size_t fastHashFillStep = 3;
24
+
25
+ while(ip <= iend) {
26
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
27
+ hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
28
+ ip += fastHashFillStep;
29
+ }
30
+ }
31
+
32
+
33
+ FORCE_INLINE_TEMPLATE
34
+ size_t ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx,
35
+ const void* src, size_t srcSize,
36
+ const U32 mls)
37
+ {
38
+ U32* const hashLong = cctx->hashTable;
39
+ const U32 hBitsL = cctx->appliedParams.cParams.hashLog;
40
+ U32* const hashSmall = cctx->chainTable;
41
+ const U32 hBitsS = cctx->appliedParams.cParams.chainLog;
42
+ seqStore_t* seqStorePtr = &(cctx->seqStore);
43
+ const BYTE* const base = cctx->base;
44
+ const BYTE* const istart = (const BYTE*)src;
45
+ const BYTE* ip = istart;
46
+ const BYTE* anchor = istart;
47
+ const U32 lowestIndex = cctx->dictLimit;
48
+ const BYTE* const lowest = base + lowestIndex;
49
+ const BYTE* const iend = istart + srcSize;
50
+ const BYTE* const ilimit = iend - HASH_READ_SIZE;
51
+ U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
52
+ U32 offsetSaved = 0;
53
+
54
+ /* init */
55
+ ip += (ip==lowest);
56
+ { U32 const maxRep = (U32)(ip-lowest);
57
+ if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
58
+ if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
59
+ }
60
+
61
+ /* Main Search Loop */
62
+ while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
63
+ size_t mLength;
64
+ size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
65
+ size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
66
+ U32 const current = (U32)(ip-base);
67
+ U32 const matchIndexL = hashLong[h2];
68
+ U32 const matchIndexS = hashSmall[h];
69
+ const BYTE* matchLong = base + matchIndexL;
70
+ const BYTE* match = base + matchIndexS;
71
+ hashLong[h2] = hashSmall[h] = current; /* update hash tables */
72
+
73
+ assert(offset_1 <= current); /* supposed guaranteed by construction */
74
+ if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
75
+ /* favor repcode */
76
+ mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
77
+ ip++;
78
+ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
79
+ } else {
80
+ U32 offset;
81
+ if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) {
82
+ mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
83
+ offset = (U32)(ip-matchLong);
84
+ while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
85
+ } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) {
86
+ size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
87
+ U32 const matchIndexL3 = hashLong[hl3];
88
+ const BYTE* matchL3 = base + matchIndexL3;
89
+ hashLong[hl3] = current + 1;
90
+ if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) {
91
+ mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
92
+ ip++;
93
+ offset = (U32)(ip-matchL3);
94
+ while (((ip>anchor) & (matchL3>lowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
95
+ } else {
96
+ mLength = ZSTD_count(ip+4, match+4, iend) + 4;
97
+ offset = (U32)(ip-match);
98
+ while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
99
+ }
100
+ } else {
101
+ ip += ((ip-anchor) >> g_searchStrength) + 1;
102
+ continue;
103
+ }
104
+
105
+ offset_2 = offset_1;
106
+ offset_1 = offset;
107
+
108
+ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
109
+ }
110
+
111
+ /* match found */
112
+ ip += mLength;
113
+ anchor = ip;
114
+
115
+ if (ip <= ilimit) {
116
+ /* Fill Table */
117
+ hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] =
118
+ hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */
119
+ hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] =
120
+ hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
121
+
122
+ /* check immediate repcode */
123
+ while ( (ip <= ilimit)
124
+ && ( (offset_2>0)
125
+ & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
126
+ /* store sequence */
127
+ size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
128
+ { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */
129
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
130
+ hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
131
+ ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH);
132
+ ip += rLength;
133
+ anchor = ip;
134
+ continue; /* faster when present ... (?) */
135
+ } } }
136
+
137
+ /* save reps for next block */
138
+ seqStorePtr->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
139
+ seqStorePtr->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
140
+
141
+ /* Return the last literals size */
142
+ return iend - anchor;
143
+ }
144
+
145
+
146
+ size_t ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize)
147
+ {
148
+ const U32 mls = ctx->appliedParams.cParams.searchLength;
149
+ switch(mls)
150
+ {
151
+ default: /* includes case 3 */
152
+ case 4 :
153
+ return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4);
154
+ case 5 :
155
+ return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5);
156
+ case 6 :
157
+ return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6);
158
+ case 7 :
159
+ return ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7);
160
+ }
161
+ }
162
+
163
+
164
+ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx,
165
+ const void* src, size_t srcSize,
166
+ const U32 mls)
167
+ {
168
+ U32* const hashLong = ctx->hashTable;
169
+ U32 const hBitsL = ctx->appliedParams.cParams.hashLog;
170
+ U32* const hashSmall = ctx->chainTable;
171
+ U32 const hBitsS = ctx->appliedParams.cParams.chainLog;
172
+ seqStore_t* seqStorePtr = &(ctx->seqStore);
173
+ const BYTE* const base = ctx->base;
174
+ const BYTE* const dictBase = ctx->dictBase;
175
+ const BYTE* const istart = (const BYTE*)src;
176
+ const BYTE* ip = istart;
177
+ const BYTE* anchor = istart;
178
+ const U32 lowestIndex = ctx->lowLimit;
179
+ const BYTE* const dictStart = dictBase + lowestIndex;
180
+ const U32 dictLimit = ctx->dictLimit;
181
+ const BYTE* const lowPrefixPtr = base + dictLimit;
182
+ const BYTE* const dictEnd = dictBase + dictLimit;
183
+ const BYTE* const iend = istart + srcSize;
184
+ const BYTE* const ilimit = iend - 8;
185
+ U32 offset_1=seqStorePtr->rep[0], offset_2=seqStorePtr->rep[1];
186
+
187
+ /* Search Loop */
188
+ while (ip < ilimit) { /* < instead of <=, because (ip+1) */
189
+ const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
190
+ const U32 matchIndex = hashSmall[hSmall];
191
+ const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base;
192
+ const BYTE* match = matchBase + matchIndex;
193
+
194
+ const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
195
+ const U32 matchLongIndex = hashLong[hLong];
196
+ const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base;
197
+ const BYTE* matchLong = matchLongBase + matchLongIndex;
198
+
199
+ const U32 current = (U32)(ip-base);
200
+ const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
201
+ const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
202
+ const BYTE* repMatch = repBase + repIndex;
203
+ size_t mLength;
204
+ hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */
205
+
206
+ if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex))
207
+ && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
208
+ const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
209
+ mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
210
+ ip++;
211
+ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH);
212
+ } else {
213
+ if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
214
+ const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
215
+ const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr;
216
+ U32 offset;
217
+ mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8;
218
+ offset = current - matchLongIndex;
219
+ while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
220
+ offset_2 = offset_1;
221
+ offset_1 = offset;
222
+ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
223
+
224
+ } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) {
225
+ size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
226
+ U32 const matchIndex3 = hashLong[h3];
227
+ const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base;
228
+ const BYTE* match3 = match3Base + matchIndex3;
229
+ U32 offset;
230
+ hashLong[h3] = current + 1;
231
+ if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
232
+ const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend;
233
+ const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr;
234
+ mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8;
235
+ ip++;
236
+ offset = current+1 - matchIndex3;
237
+ while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
238
+ } else {
239
+ const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
240
+ const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
241
+ mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4;
242
+ offset = current - matchIndex;
243
+ while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
244
+ }
245
+ offset_2 = offset_1;
246
+ offset_1 = offset;
247
+ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
248
+
249
+ } else {
250
+ ip += ((ip-anchor) >> g_searchStrength) + 1;
251
+ continue;
252
+ } }
253
+
254
+ /* found a match : store it */
255
+ ip += mLength;
256
+ anchor = ip;
257
+
258
+ if (ip <= ilimit) {
259
+ /* Fill Table */
260
+ hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2;
261
+ hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2;
262
+ hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base);
263
+ hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
264
+ /* check immediate repcode */
265
+ while (ip <= ilimit) {
266
+ U32 const current2 = (U32)(ip-base);
267
+ U32 const repIndex2 = current2 - offset_2;
268
+ const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
269
+ if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
270
+ && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
271
+ const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
272
+ size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
273
+ U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
274
+ ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH);
275
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
276
+ hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
277
+ ip += repLength2;
278
+ anchor = ip;
279
+ continue;
280
+ }
281
+ break;
282
+ } } }
283
+
284
+ /* save reps for next block */
285
+ seqStorePtr->repToConfirm[0] = offset_1; seqStorePtr->repToConfirm[1] = offset_2;
286
+
287
+ /* Return the last literals size */
288
+ return iend - anchor;
289
+ }
290
+
291
+
292
+ size_t ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx,
293
+ const void* src, size_t srcSize)
294
+ {
295
+ U32 const mls = ctx->appliedParams.cParams.searchLength;
296
+ switch(mls)
297
+ {
298
+ default: /* includes case 3 */
299
+ case 4 :
300
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4);
301
+ case 5 :
302
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5);
303
+ case 6 :
304
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6);
305
+ case 7 :
306
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7);
307
+ }
308
+ }