extzstd 0.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/README.md +38 -56
  4. data/contrib/zstd/CHANGELOG +613 -0
  5. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  6. data/contrib/zstd/CONTRIBUTING.md +406 -0
  7. data/contrib/zstd/COPYING +339 -0
  8. data/contrib/zstd/Makefile +420 -0
  9. data/contrib/zstd/README.md +179 -41
  10. data/contrib/zstd/TESTING.md +44 -0
  11. data/contrib/zstd/appveyor.yml +292 -0
  12. data/contrib/zstd/lib/BUCK +234 -0
  13. data/contrib/zstd/lib/Makefile +451 -0
  14. data/contrib/zstd/lib/README.md +207 -0
  15. data/contrib/zstd/{common → lib/common}/bitstream.h +187 -138
  16. data/contrib/zstd/lib/common/compiler.h +288 -0
  17. data/contrib/zstd/lib/common/cpu.h +213 -0
  18. data/contrib/zstd/lib/common/debug.c +24 -0
  19. data/contrib/zstd/lib/common/debug.h +107 -0
  20. data/contrib/zstd/lib/common/entropy_common.c +362 -0
  21. data/contrib/zstd/{common → lib/common}/error_private.c +25 -12
  22. data/contrib/zstd/{common → lib/common}/error_private.h +14 -10
  23. data/contrib/zstd/{common → lib/common}/fse.h +173 -92
  24. data/contrib/zstd/{common → lib/common}/fse_decompress.c +149 -85
  25. data/contrib/zstd/lib/common/huf.h +361 -0
  26. data/contrib/zstd/{common → lib/common}/mem.h +115 -59
  27. data/contrib/zstd/lib/common/pool.c +350 -0
  28. data/contrib/zstd/lib/common/pool.h +84 -0
  29. data/contrib/zstd/lib/common/threading.c +122 -0
  30. data/contrib/zstd/lib/common/threading.h +155 -0
  31. data/contrib/zstd/{common → lib/common}/xxhash.c +55 -96
  32. data/contrib/zstd/{common → lib/common}/xxhash.h +23 -47
  33. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  34. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  35. data/contrib/zstd/lib/common/zstd_errors.h +95 -0
  36. data/contrib/zstd/lib/common/zstd_internal.h +478 -0
  37. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +214 -319
  38. data/contrib/zstd/lib/compress/hist.c +181 -0
  39. data/contrib/zstd/lib/compress/hist.h +75 -0
  40. data/contrib/zstd/lib/compress/huf_compress.c +913 -0
  41. data/contrib/zstd/lib/compress/zstd_compress.c +5208 -0
  42. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1203 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +433 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +849 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  49. data/contrib/zstd/lib/compress/zstd_cwksp.h +561 -0
  50. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  52. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  54. data/contrib/zstd/lib/compress/zstd_lazy.c +1412 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.h +87 -0
  56. data/contrib/zstd/lib/compress/zstd_ldm.c +660 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.h +116 -0
  58. data/contrib/zstd/lib/compress/zstd_opt.c +1345 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  60. data/contrib/zstd/lib/compress/zstdmt_compress.c +1811 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.h +110 -0
  62. data/contrib/zstd/lib/decompress/huf_decompress.c +1350 -0
  63. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  65. data/contrib/zstd/lib/decompress/zstd_decompress.c +1930 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1540 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +62 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +190 -0
  69. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +68 -45
  70. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  71. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  73. data/contrib/zstd/lib/dictBuilder/cover.c +1245 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  75. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
  77. data/contrib/zstd/lib/dictBuilder/fastcover.c +758 -0
  78. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +318 -194
  79. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  80. data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +171 -15
  81. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +191 -124
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +19 -5
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +125 -125
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +19 -5
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +125 -124
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +20 -6
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +151 -299
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +19 -5
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +237 -243
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +19 -6
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +130 -143
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +18 -5
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +158 -157
  94. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +19 -5
  95. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  96. data/contrib/zstd/lib/zstd.h +2391 -0
  97. data/ext/depend +2 -0
  98. data/ext/extconf.rb +15 -6
  99. data/ext/extzstd.c +76 -145
  100. data/ext/extzstd.h +80 -31
  101. data/ext/extzstd_stream.c +417 -142
  102. data/ext/libzstd_conf.h +8 -0
  103. data/ext/zstd_common.c +10 -7
  104. data/ext/zstd_compress.c +14 -5
  105. data/ext/zstd_decompress.c +5 -4
  106. data/ext/zstd_dictbuilder.c +9 -4
  107. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  108. data/ext/zstd_legacy_v01.c +3 -1
  109. data/ext/zstd_legacy_v02.c +3 -1
  110. data/ext/zstd_legacy_v03.c +3 -1
  111. data/ext/zstd_legacy_v04.c +3 -1
  112. data/ext/zstd_legacy_v05.c +3 -1
  113. data/ext/zstd_legacy_v06.c +3 -1
  114. data/ext/zstd_legacy_v07.c +3 -1
  115. data/gemstub.rb +10 -24
  116. data/lib/extzstd.rb +64 -179
  117. data/lib/extzstd/version.rb +6 -1
  118. data/test/test_basic.rb +9 -6
  119. metadata +113 -57
  120. data/HISTORY.ja +0 -5
  121. data/contrib/zstd/common/entropy_common.c +0 -225
  122. data/contrib/zstd/common/huf.h +0 -228
  123. data/contrib/zstd/common/zstd_common.c +0 -83
  124. data/contrib/zstd/common/zstd_errors.h +0 -60
  125. data/contrib/zstd/common/zstd_internal.h +0 -267
  126. data/contrib/zstd/compress/huf_compress.c +0 -533
  127. data/contrib/zstd/compress/zbuff_compress.c +0 -319
  128. data/contrib/zstd/compress/zstd_compress.c +0 -3264
  129. data/contrib/zstd/compress/zstd_opt.h +0 -900
  130. data/contrib/zstd/decompress/huf_decompress.c +0 -883
  131. data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
  132. data/contrib/zstd/decompress/zstd_decompress.c +0 -1842
  133. data/contrib/zstd/dictBuilder/zdict.h +0 -111
  134. data/contrib/zstd/zstd.h +0 -640
@@ -0,0 +1,521 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, 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_compress_internal.h"
12
+ #include "zstd_double_fast.h"
13
+
14
+
15
+ void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
16
+ void const* end, ZSTD_dictTableLoadMethod_e dtlm)
17
+ {
18
+ const ZSTD_compressionParameters* const cParams = &ms->cParams;
19
+ U32* const hashLarge = ms->hashTable;
20
+ U32 const hBitsL = cParams->hashLog;
21
+ U32 const mls = cParams->minMatch;
22
+ U32* const hashSmall = ms->chainTable;
23
+ U32 const hBitsS = cParams->chainLog;
24
+ const BYTE* const base = ms->window.base;
25
+ const BYTE* ip = base + ms->nextToUpdate;
26
+ const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
27
+ const U32 fastHashFillStep = 3;
28
+
29
+ /* Always insert every fastHashFillStep position into the hash tables.
30
+ * Insert the other positions into the large hash table if their entry
31
+ * is empty.
32
+ */
33
+ for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
34
+ U32 const curr = (U32)(ip - base);
35
+ U32 i;
36
+ for (i = 0; i < fastHashFillStep; ++i) {
37
+ size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
38
+ size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
39
+ if (i == 0)
40
+ hashSmall[smHash] = curr + i;
41
+ if (i == 0 || hashLarge[lgHash] == 0)
42
+ hashLarge[lgHash] = curr + i;
43
+ /* Only load extra positions for ZSTD_dtlm_full */
44
+ if (dtlm == ZSTD_dtlm_fast)
45
+ break;
46
+ } }
47
+ }
48
+
49
+
50
+ FORCE_INLINE_TEMPLATE
51
+ size_t ZSTD_compressBlock_doubleFast_generic(
52
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
53
+ void const* src, size_t srcSize,
54
+ U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
55
+ {
56
+ ZSTD_compressionParameters const* cParams = &ms->cParams;
57
+ U32* const hashLong = ms->hashTable;
58
+ const U32 hBitsL = cParams->hashLog;
59
+ U32* const hashSmall = ms->chainTable;
60
+ const U32 hBitsS = cParams->chainLog;
61
+ const BYTE* const base = ms->window.base;
62
+ const BYTE* const istart = (const BYTE*)src;
63
+ const BYTE* ip = istart;
64
+ const BYTE* anchor = istart;
65
+ const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
66
+ /* presumes that, if there is a dictionary, it must be using Attach mode */
67
+ const U32 prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog);
68
+ const BYTE* const prefixLowest = base + prefixLowestIndex;
69
+ const BYTE* const iend = istart + srcSize;
70
+ const BYTE* const ilimit = iend - HASH_READ_SIZE;
71
+ U32 offset_1=rep[0], offset_2=rep[1];
72
+ U32 offsetSaved = 0;
73
+
74
+ const ZSTD_matchState_t* const dms = ms->dictMatchState;
75
+ const ZSTD_compressionParameters* const dictCParams =
76
+ dictMode == ZSTD_dictMatchState ?
77
+ &dms->cParams : NULL;
78
+ const U32* const dictHashLong = dictMode == ZSTD_dictMatchState ?
79
+ dms->hashTable : NULL;
80
+ const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
81
+ dms->chainTable : NULL;
82
+ const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ?
83
+ dms->window.dictLimit : 0;
84
+ const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
85
+ dms->window.base : NULL;
86
+ const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ?
87
+ dictBase + dictStartIndex : NULL;
88
+ const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
89
+ dms->window.nextSrc : NULL;
90
+ const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
91
+ prefixLowestIndex - (U32)(dictEnd - dictBase) :
92
+ 0;
93
+ const U32 dictHBitsL = dictMode == ZSTD_dictMatchState ?
94
+ dictCParams->hashLog : hBitsL;
95
+ const U32 dictHBitsS = dictMode == ZSTD_dictMatchState ?
96
+ dictCParams->chainLog : hBitsS;
97
+ const U32 dictAndPrefixLength = (U32)((ip - prefixLowest) + (dictEnd - dictStart));
98
+
99
+ DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic");
100
+
101
+ assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
102
+
103
+ /* if a dictionary is attached, it must be within window range */
104
+ if (dictMode == ZSTD_dictMatchState) {
105
+ assert(ms->window.dictLimit + (1U << cParams->windowLog) >= endIndex);
106
+ }
107
+
108
+ /* init */
109
+ ip += (dictAndPrefixLength == 0);
110
+ if (dictMode == ZSTD_noDict) {
111
+ U32 const curr = (U32)(ip - base);
112
+ U32 const windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog);
113
+ U32 const maxRep = curr - windowLow;
114
+ if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
115
+ if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
116
+ }
117
+ if (dictMode == ZSTD_dictMatchState) {
118
+ /* dictMatchState repCode checks don't currently handle repCode == 0
119
+ * disabling. */
120
+ assert(offset_1 <= dictAndPrefixLength);
121
+ assert(offset_2 <= dictAndPrefixLength);
122
+ }
123
+
124
+ /* Main Search Loop */
125
+ while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
126
+ size_t mLength;
127
+ U32 offset;
128
+ size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
129
+ size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
130
+ size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
131
+ size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
132
+ U32 const curr = (U32)(ip-base);
133
+ U32 const matchIndexL = hashLong[h2];
134
+ U32 matchIndexS = hashSmall[h];
135
+ const BYTE* matchLong = base + matchIndexL;
136
+ const BYTE* match = base + matchIndexS;
137
+ const U32 repIndex = curr + 1 - offset_1;
138
+ const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
139
+ && repIndex < prefixLowestIndex) ?
140
+ dictBase + (repIndex - dictIndexDelta) :
141
+ base + repIndex;
142
+ hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
143
+
144
+ /* check dictMatchState repcode */
145
+ if (dictMode == ZSTD_dictMatchState
146
+ && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
147
+ && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
148
+ const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
149
+ mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
150
+ ip++;
151
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
152
+ goto _match_stored;
153
+ }
154
+
155
+ /* check noDict repcode */
156
+ if ( dictMode == ZSTD_noDict
157
+ && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
158
+ mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
159
+ ip++;
160
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
161
+ goto _match_stored;
162
+ }
163
+
164
+ if (matchIndexL > prefixLowestIndex) {
165
+ /* check prefix long match */
166
+ if (MEM_read64(matchLong) == MEM_read64(ip)) {
167
+ mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
168
+ offset = (U32)(ip-matchLong);
169
+ while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
170
+ goto _match_found;
171
+ }
172
+ } else if (dictMode == ZSTD_dictMatchState) {
173
+ /* check dictMatchState long match */
174
+ U32 const dictMatchIndexL = dictHashLong[dictHL];
175
+ const BYTE* dictMatchL = dictBase + dictMatchIndexL;
176
+ assert(dictMatchL < dictEnd);
177
+
178
+ if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
179
+ mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
180
+ offset = (U32)(curr - dictMatchIndexL - dictIndexDelta);
181
+ while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
182
+ goto _match_found;
183
+ } }
184
+
185
+ if (matchIndexS > prefixLowestIndex) {
186
+ /* check prefix short match */
187
+ if (MEM_read32(match) == MEM_read32(ip)) {
188
+ goto _search_next_long;
189
+ }
190
+ } else if (dictMode == ZSTD_dictMatchState) {
191
+ /* check dictMatchState short match */
192
+ U32 const dictMatchIndexS = dictHashSmall[dictHS];
193
+ match = dictBase + dictMatchIndexS;
194
+ matchIndexS = dictMatchIndexS + dictIndexDelta;
195
+
196
+ if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
197
+ goto _search_next_long;
198
+ } }
199
+
200
+ ip += ((ip-anchor) >> kSearchStrength) + 1;
201
+ #if defined(__aarch64__)
202
+ PREFETCH_L1(ip+256);
203
+ #endif
204
+ continue;
205
+
206
+ _search_next_long:
207
+
208
+ { size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
209
+ size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
210
+ U32 const matchIndexL3 = hashLong[hl3];
211
+ const BYTE* matchL3 = base + matchIndexL3;
212
+ hashLong[hl3] = curr + 1;
213
+
214
+ /* check prefix long +1 match */
215
+ if (matchIndexL3 > prefixLowestIndex) {
216
+ if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
217
+ mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
218
+ ip++;
219
+ offset = (U32)(ip-matchL3);
220
+ while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
221
+ goto _match_found;
222
+ }
223
+ } else if (dictMode == ZSTD_dictMatchState) {
224
+ /* check dict long +1 match */
225
+ U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
226
+ const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
227
+ assert(dictMatchL3 < dictEnd);
228
+ if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
229
+ mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
230
+ ip++;
231
+ offset = (U32)(curr + 1 - dictMatchIndexL3 - dictIndexDelta);
232
+ while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
233
+ goto _match_found;
234
+ } } }
235
+
236
+ /* if no long +1 match, explore the short match we found */
237
+ if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
238
+ mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
239
+ offset = (U32)(curr - matchIndexS);
240
+ while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
241
+ } else {
242
+ mLength = ZSTD_count(ip+4, match+4, iend) + 4;
243
+ offset = (U32)(ip - match);
244
+ while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
245
+ }
246
+
247
+ /* fall-through */
248
+
249
+ _match_found:
250
+ offset_2 = offset_1;
251
+ offset_1 = offset;
252
+
253
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
254
+
255
+ _match_stored:
256
+ /* match found */
257
+ ip += mLength;
258
+ anchor = ip;
259
+
260
+ if (ip <= ilimit) {
261
+ /* Complementary insertion */
262
+ /* done after iLimit test, as candidates could be > iend-8 */
263
+ { U32 const indexToInsert = curr+2;
264
+ hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
265
+ hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
266
+ hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
267
+ hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
268
+ }
269
+
270
+ /* check immediate repcode */
271
+ if (dictMode == ZSTD_dictMatchState) {
272
+ while (ip <= ilimit) {
273
+ U32 const current2 = (U32)(ip-base);
274
+ U32 const repIndex2 = current2 - offset_2;
275
+ const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
276
+ && repIndex2 < prefixLowestIndex ?
277
+ dictBase + repIndex2 - dictIndexDelta :
278
+ base + repIndex2;
279
+ if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
280
+ && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
281
+ const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
282
+ size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
283
+ U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
284
+ ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
285
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
286
+ hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
287
+ ip += repLength2;
288
+ anchor = ip;
289
+ continue;
290
+ }
291
+ break;
292
+ } }
293
+
294
+ if (dictMode == ZSTD_noDict) {
295
+ while ( (ip <= ilimit)
296
+ && ( (offset_2>0)
297
+ & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
298
+ /* store sequence */
299
+ size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
300
+ U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
301
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
302
+ hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
303
+ ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength-MINMATCH);
304
+ ip += rLength;
305
+ anchor = ip;
306
+ continue; /* faster when present ... (?) */
307
+ } } }
308
+ } /* while (ip < ilimit) */
309
+
310
+ /* save reps for next block */
311
+ rep[0] = offset_1 ? offset_1 : offsetSaved;
312
+ rep[1] = offset_2 ? offset_2 : offsetSaved;
313
+
314
+ /* Return the last literals size */
315
+ return (size_t)(iend - anchor);
316
+ }
317
+
318
+
319
+ size_t ZSTD_compressBlock_doubleFast(
320
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
321
+ void const* src, size_t srcSize)
322
+ {
323
+ const U32 mls = ms->cParams.minMatch;
324
+ switch(mls)
325
+ {
326
+ default: /* includes case 3 */
327
+ case 4 :
328
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
329
+ case 5 :
330
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
331
+ case 6 :
332
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
333
+ case 7 :
334
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
335
+ }
336
+ }
337
+
338
+
339
+ size_t ZSTD_compressBlock_doubleFast_dictMatchState(
340
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
341
+ void const* src, size_t srcSize)
342
+ {
343
+ const U32 mls = ms->cParams.minMatch;
344
+ switch(mls)
345
+ {
346
+ default: /* includes case 3 */
347
+ case 4 :
348
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
349
+ case 5 :
350
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
351
+ case 6 :
352
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
353
+ case 7 :
354
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
355
+ }
356
+ }
357
+
358
+
359
+ static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
360
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
361
+ void const* src, size_t srcSize,
362
+ U32 const mls /* template */)
363
+ {
364
+ ZSTD_compressionParameters const* cParams = &ms->cParams;
365
+ U32* const hashLong = ms->hashTable;
366
+ U32 const hBitsL = cParams->hashLog;
367
+ U32* const hashSmall = ms->chainTable;
368
+ U32 const hBitsS = cParams->chainLog;
369
+ const BYTE* const istart = (const BYTE*)src;
370
+ const BYTE* ip = istart;
371
+ const BYTE* anchor = istart;
372
+ const BYTE* const iend = istart + srcSize;
373
+ const BYTE* const ilimit = iend - 8;
374
+ const BYTE* const base = ms->window.base;
375
+ const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
376
+ const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
377
+ const U32 dictStartIndex = lowLimit;
378
+ const U32 dictLimit = ms->window.dictLimit;
379
+ const U32 prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
380
+ const BYTE* const prefixStart = base + prefixStartIndex;
381
+ const BYTE* const dictBase = ms->window.dictBase;
382
+ const BYTE* const dictStart = dictBase + dictStartIndex;
383
+ const BYTE* const dictEnd = dictBase + prefixStartIndex;
384
+ U32 offset_1=rep[0], offset_2=rep[1];
385
+
386
+ DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
387
+
388
+ /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
389
+ if (prefixStartIndex == dictStartIndex)
390
+ return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
391
+
392
+ /* Search Loop */
393
+ while (ip < ilimit) { /* < instead of <=, because (ip+1) */
394
+ const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
395
+ const U32 matchIndex = hashSmall[hSmall];
396
+ const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
397
+ const BYTE* match = matchBase + matchIndex;
398
+
399
+ const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
400
+ const U32 matchLongIndex = hashLong[hLong];
401
+ const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
402
+ const BYTE* matchLong = matchLongBase + matchLongIndex;
403
+
404
+ const U32 curr = (U32)(ip-base);
405
+ const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
406
+ const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
407
+ const BYTE* const repMatch = repBase + repIndex;
408
+ size_t mLength;
409
+ hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
410
+
411
+ if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
412
+ & (repIndex > dictStartIndex))
413
+ && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
414
+ const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
415
+ mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
416
+ ip++;
417
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
418
+ } else {
419
+ if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
420
+ const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
421
+ const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
422
+ U32 offset;
423
+ mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
424
+ offset = curr - matchLongIndex;
425
+ while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
426
+ offset_2 = offset_1;
427
+ offset_1 = offset;
428
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
429
+
430
+ } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
431
+ size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
432
+ U32 const matchIndex3 = hashLong[h3];
433
+ const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
434
+ const BYTE* match3 = match3Base + matchIndex3;
435
+ U32 offset;
436
+ hashLong[h3] = curr + 1;
437
+ if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
438
+ const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
439
+ const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
440
+ mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
441
+ ip++;
442
+ offset = curr+1 - matchIndex3;
443
+ while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
444
+ } else {
445
+ const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
446
+ const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
447
+ mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
448
+ offset = curr - matchIndex;
449
+ while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
450
+ }
451
+ offset_2 = offset_1;
452
+ offset_1 = offset;
453
+ ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
454
+
455
+ } else {
456
+ ip += ((ip-anchor) >> kSearchStrength) + 1;
457
+ continue;
458
+ } }
459
+
460
+ /* move to next sequence start */
461
+ ip += mLength;
462
+ anchor = ip;
463
+
464
+ if (ip <= ilimit) {
465
+ /* Complementary insertion */
466
+ /* done after iLimit test, as candidates could be > iend-8 */
467
+ { U32 const indexToInsert = curr+2;
468
+ hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
469
+ hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
470
+ hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
471
+ hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
472
+ }
473
+
474
+ /* check immediate repcode */
475
+ while (ip <= ilimit) {
476
+ U32 const current2 = (U32)(ip-base);
477
+ U32 const repIndex2 = current2 - offset_2;
478
+ const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
479
+ if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
480
+ & (repIndex2 > dictStartIndex))
481
+ && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
482
+ const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
483
+ size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
484
+ U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
485
+ ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
486
+ hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
487
+ hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
488
+ ip += repLength2;
489
+ anchor = ip;
490
+ continue;
491
+ }
492
+ break;
493
+ } } }
494
+
495
+ /* save reps for next block */
496
+ rep[0] = offset_1;
497
+ rep[1] = offset_2;
498
+
499
+ /* Return the last literals size */
500
+ return (size_t)(iend - anchor);
501
+ }
502
+
503
+
504
+ size_t ZSTD_compressBlock_doubleFast_extDict(
505
+ ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
506
+ void const* src, size_t srcSize)
507
+ {
508
+ U32 const mls = ms->cParams.minMatch;
509
+ switch(mls)
510
+ {
511
+ default: /* includes case 3 */
512
+ case 4 :
513
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
514
+ case 5 :
515
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
516
+ case 6 :
517
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
518
+ case 7 :
519
+ return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
520
+ }
521
+ }