zstd-ruby 1.4.4.0 → 1.5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/README.md +78 -5
  4. data/Rakefile +8 -2
  5. data/ext/zstdruby/common.h +15 -0
  6. data/ext/zstdruby/extconf.rb +3 -2
  7. data/ext/zstdruby/libzstd/common/allocations.h +55 -0
  8. data/ext/zstdruby/libzstd/common/bits.h +200 -0
  9. data/ext/zstdruby/libzstd/common/bitstream.h +74 -97
  10. data/ext/zstdruby/libzstd/common/compiler.h +219 -20
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  13. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +184 -80
  15. data/ext/zstdruby/libzstd/common/error_private.c +11 -2
  16. data/ext/zstdruby/libzstd/common/error_private.h +87 -4
  17. data/ext/zstdruby/libzstd/common/fse.h +47 -116
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +127 -127
  19. data/ext/zstdruby/libzstd/common/huf.h +112 -197
  20. data/ext/zstdruby/libzstd/common/mem.h +124 -142
  21. data/ext/zstdruby/libzstd/common/pool.c +54 -27
  22. data/ext/zstdruby/libzstd/common/pool.h +11 -5
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +156 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +78 -22
  25. data/ext/zstdruby/libzstd/common/threading.h +9 -13
  26. data/ext/zstdruby/libzstd/common/xxhash.c +15 -873
  27. data/ext/zstdruby/libzstd/common/xxhash.h +5572 -191
  28. data/ext/zstdruby/libzstd/common/zstd_common.c +2 -37
  29. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  30. data/ext/zstdruby/libzstd/common/zstd_internal.h +186 -144
  31. data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
  32. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  33. data/ext/zstdruby/libzstd/compress/fse_compress.c +99 -196
  34. data/ext/zstdruby/libzstd/compress/hist.c +41 -63
  35. data/ext/zstdruby/libzstd/compress/hist.h +13 -33
  36. data/ext/zstdruby/libzstd/compress/huf_compress.c +968 -331
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +4120 -1191
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +688 -159
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +121 -40
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -6
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +62 -35
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +577 -0
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +322 -115
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +394 -154
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +4 -3
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +729 -253
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +4 -3
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1289 -247
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +61 -1
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +339 -212
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
  54. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.c +508 -282
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +217 -466
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +35 -114
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1220 -572
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +576 -0
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +23 -19
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +859 -273
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1244 -375
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +21 -7
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +74 -11
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +75 -54
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +55 -36
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +126 -110
  72. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +248 -56
  73. data/ext/zstdruby/libzstd/zstd.h +1277 -306
  74. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +29 -8
  75. data/ext/zstdruby/main.c +20 -0
  76. data/ext/zstdruby/skippable_frame.c +63 -0
  77. data/ext/zstdruby/streaming_compress.c +177 -0
  78. data/ext/zstdruby/streaming_compress.h +5 -0
  79. data/ext/zstdruby/streaming_decompress.c +123 -0
  80. data/ext/zstdruby/zstdruby.c +114 -32
  81. data/lib/zstd-ruby/version.rb +1 -1
  82. data/lib/zstd-ruby.rb +0 -1
  83. data/zstd-ruby.gemspec +1 -1
  84. metadata +24 -39
  85. data/.travis.yml +0 -14
  86. data/ext/zstdruby/libzstd/.gitignore +0 -3
  87. data/ext/zstdruby/libzstd/BUCK +0 -234
  88. data/ext/zstdruby/libzstd/Makefile +0 -289
  89. data/ext/zstdruby/libzstd/README.md +0 -159
  90. data/ext/zstdruby/libzstd/deprecated/zbuff.h +0 -214
  91. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +0 -26
  92. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +0 -147
  93. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +0 -75
  94. data/ext/zstdruby/libzstd/dll/example/Makefile +0 -47
  95. data/ext/zstdruby/libzstd/dll/example/README.md +0 -69
  96. data/ext/zstdruby/libzstd/dll/example/build_package.bat +0 -20
  97. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +0 -25
  98. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +0 -181
  99. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +0 -415
  100. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +0 -2152
  101. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
  102. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3514
  103. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
  104. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3156
  105. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
  106. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3641
  107. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
  108. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4046
  109. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
  110. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4150
  111. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
  112. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4533
  113. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +0 -187
  114. data/ext/zstdruby/libzstd/libzstd.pc.in +0 -15
  115. data/ext/zstdruby/zstdruby.h +0 -6
@@ -0,0 +1,577 @@
1
+ /*
2
+ * Copyright (c) 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
+ * Dependencies
13
+ ***************************************/
14
+ #include "zstd_compress_superblock.h"
15
+
16
+ #include "../common/zstd_internal.h" /* ZSTD_getSequenceLength */
17
+ #include "hist.h" /* HIST_countFast_wksp */
18
+ #include "zstd_compress_internal.h" /* ZSTD_[huf|fse|entropy]CTablesMetadata_t */
19
+ #include "zstd_compress_sequences.h"
20
+ #include "zstd_compress_literals.h"
21
+
22
+ /** ZSTD_compressSubBlock_literal() :
23
+ * Compresses literals section for a sub-block.
24
+ * When we have to write the Huffman table we will sometimes choose a header
25
+ * size larger than necessary. This is because we have to pick the header size
26
+ * before we know the table size + compressed size, so we have a bound on the
27
+ * table size. If we guessed incorrectly, we fall back to uncompressed literals.
28
+ *
29
+ * We write the header when writeEntropy=1 and set entropyWritten=1 when we succeeded
30
+ * in writing the header, otherwise it is set to 0.
31
+ *
32
+ * hufMetadata->hType has literals block type info.
33
+ * If it is set_basic, all sub-blocks literals section will be Raw_Literals_Block.
34
+ * If it is set_rle, all sub-blocks literals section will be RLE_Literals_Block.
35
+ * If it is set_compressed, first sub-block's literals section will be Compressed_Literals_Block
36
+ * If it is set_compressed, first sub-block's literals section will be Treeless_Literals_Block
37
+ * and the following sub-blocks' literals sections will be Treeless_Literals_Block.
38
+ * @return : compressed size of literals section of a sub-block
39
+ * Or 0 if unable to compress.
40
+ * Or error code */
41
+ static size_t
42
+ ZSTD_compressSubBlock_literal(const HUF_CElt* hufTable,
43
+ const ZSTD_hufCTablesMetadata_t* hufMetadata,
44
+ const BYTE* literals, size_t litSize,
45
+ void* dst, size_t dstSize,
46
+ const int bmi2, int writeEntropy, int* entropyWritten)
47
+ {
48
+ size_t const header = writeEntropy ? 200 : 0;
49
+ size_t const lhSize = 3 + (litSize >= (1 KB - header)) + (litSize >= (16 KB - header));
50
+ BYTE* const ostart = (BYTE*)dst;
51
+ BYTE* const oend = ostart + dstSize;
52
+ BYTE* op = ostart + lhSize;
53
+ U32 const singleStream = lhSize == 3;
54
+ symbolEncodingType_e hType = writeEntropy ? hufMetadata->hType : set_repeat;
55
+ size_t cLitSize = 0;
56
+
57
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal (litSize=%zu, lhSize=%zu, writeEntropy=%d)", litSize, lhSize, writeEntropy);
58
+
59
+ *entropyWritten = 0;
60
+ if (litSize == 0 || hufMetadata->hType == set_basic) {
61
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal using raw literal");
62
+ return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
63
+ } else if (hufMetadata->hType == set_rle) {
64
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal using rle literal");
65
+ return ZSTD_compressRleLiteralsBlock(dst, dstSize, literals, litSize);
66
+ }
67
+
68
+ assert(litSize > 0);
69
+ assert(hufMetadata->hType == set_compressed || hufMetadata->hType == set_repeat);
70
+
71
+ if (writeEntropy && hufMetadata->hType == set_compressed) {
72
+ ZSTD_memcpy(op, hufMetadata->hufDesBuffer, hufMetadata->hufDesSize);
73
+ op += hufMetadata->hufDesSize;
74
+ cLitSize += hufMetadata->hufDesSize;
75
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal (hSize=%zu)", hufMetadata->hufDesSize);
76
+ }
77
+
78
+ { int const flags = bmi2 ? HUF_flags_bmi2 : 0;
79
+ const size_t cSize = singleStream ? HUF_compress1X_usingCTable(op, oend-op, literals, litSize, hufTable, flags)
80
+ : HUF_compress4X_usingCTable(op, oend-op, literals, litSize, hufTable, flags);
81
+ op += cSize;
82
+ cLitSize += cSize;
83
+ if (cSize == 0 || ERR_isError(cSize)) {
84
+ DEBUGLOG(5, "Failed to write entropy tables %s", ZSTD_getErrorName(cSize));
85
+ return 0;
86
+ }
87
+ /* If we expand and we aren't writing a header then emit uncompressed */
88
+ if (!writeEntropy && cLitSize >= litSize) {
89
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal using raw literal because uncompressible");
90
+ return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
91
+ }
92
+ /* If we are writing headers then allow expansion that doesn't change our header size. */
93
+ if (lhSize < (size_t)(3 + (cLitSize >= 1 KB) + (cLitSize >= 16 KB))) {
94
+ assert(cLitSize > litSize);
95
+ DEBUGLOG(5, "Literals expanded beyond allowed header size");
96
+ return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
97
+ }
98
+ DEBUGLOG(5, "ZSTD_compressSubBlock_literal (cSize=%zu)", cSize);
99
+ }
100
+
101
+ /* Build header */
102
+ switch(lhSize)
103
+ {
104
+ case 3: /* 2 - 2 - 10 - 10 */
105
+ { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<14);
106
+ MEM_writeLE24(ostart, lhc);
107
+ break;
108
+ }
109
+ case 4: /* 2 - 2 - 14 - 14 */
110
+ { U32 const lhc = hType + (2 << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<18);
111
+ MEM_writeLE32(ostart, lhc);
112
+ break;
113
+ }
114
+ case 5: /* 2 - 2 - 18 - 18 */
115
+ { U32 const lhc = hType + (3 << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<22);
116
+ MEM_writeLE32(ostart, lhc);
117
+ ostart[4] = (BYTE)(cLitSize >> 10);
118
+ break;
119
+ }
120
+ default: /* not possible : lhSize is {3,4,5} */
121
+ assert(0);
122
+ }
123
+ *entropyWritten = 1;
124
+ DEBUGLOG(5, "Compressed literals: %u -> %u", (U32)litSize, (U32)(op-ostart));
125
+ return op-ostart;
126
+ }
127
+
128
+ static size_t
129
+ ZSTD_seqDecompressedSize(seqStore_t const* seqStore,
130
+ const seqDef* sequences, size_t nbSeq,
131
+ size_t litSize, int lastSequence)
132
+ {
133
+ const seqDef* const sstart = sequences;
134
+ const seqDef* const send = sequences + nbSeq;
135
+ const seqDef* sp = sstart;
136
+ size_t matchLengthSum = 0;
137
+ size_t litLengthSum = 0;
138
+ (void)(litLengthSum); /* suppress unused variable warning on some environments */
139
+ while (send-sp > 0) {
140
+ ZSTD_sequenceLength const seqLen = ZSTD_getSequenceLength(seqStore, sp);
141
+ litLengthSum += seqLen.litLength;
142
+ matchLengthSum += seqLen.matchLength;
143
+ sp++;
144
+ }
145
+ assert(litLengthSum <= litSize);
146
+ if (!lastSequence) {
147
+ assert(litLengthSum == litSize);
148
+ }
149
+ return matchLengthSum + litSize;
150
+ }
151
+
152
+ /** ZSTD_compressSubBlock_sequences() :
153
+ * Compresses sequences section for a sub-block.
154
+ * fseMetadata->llType, fseMetadata->ofType, and fseMetadata->mlType have
155
+ * symbol compression modes for the super-block.
156
+ * The first successfully compressed block will have these in its header.
157
+ * We set entropyWritten=1 when we succeed in compressing the sequences.
158
+ * The following sub-blocks will always have repeat mode.
159
+ * @return : compressed size of sequences section of a sub-block
160
+ * Or 0 if it is unable to compress
161
+ * Or error code. */
162
+ static size_t
163
+ ZSTD_compressSubBlock_sequences(const ZSTD_fseCTables_t* fseTables,
164
+ const ZSTD_fseCTablesMetadata_t* fseMetadata,
165
+ const seqDef* sequences, size_t nbSeq,
166
+ const BYTE* llCode, const BYTE* mlCode, const BYTE* ofCode,
167
+ const ZSTD_CCtx_params* cctxParams,
168
+ void* dst, size_t dstCapacity,
169
+ const int bmi2, int writeEntropy, int* entropyWritten)
170
+ {
171
+ const int longOffsets = cctxParams->cParams.windowLog > STREAM_ACCUMULATOR_MIN;
172
+ BYTE* const ostart = (BYTE*)dst;
173
+ BYTE* const oend = ostart + dstCapacity;
174
+ BYTE* op = ostart;
175
+ BYTE* seqHead;
176
+
177
+ DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (nbSeq=%zu, writeEntropy=%d, longOffsets=%d)", nbSeq, writeEntropy, longOffsets);
178
+
179
+ *entropyWritten = 0;
180
+ /* Sequences Header */
181
+ RETURN_ERROR_IF((oend-op) < 3 /*max nbSeq Size*/ + 1 /*seqHead*/,
182
+ dstSize_tooSmall, "");
183
+ if (nbSeq < 0x7F)
184
+ *op++ = (BYTE)nbSeq;
185
+ else if (nbSeq < LONGNBSEQ)
186
+ op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
187
+ else
188
+ op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3;
189
+ if (nbSeq==0) {
190
+ return op - ostart;
191
+ }
192
+
193
+ /* seqHead : flags for FSE encoding type */
194
+ seqHead = op++;
195
+
196
+ DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (seqHeadSize=%u)", (unsigned)(op-ostart));
197
+
198
+ if (writeEntropy) {
199
+ const U32 LLtype = fseMetadata->llType;
200
+ const U32 Offtype = fseMetadata->ofType;
201
+ const U32 MLtype = fseMetadata->mlType;
202
+ DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (fseTablesSize=%zu)", fseMetadata->fseTablesSize);
203
+ *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
204
+ ZSTD_memcpy(op, fseMetadata->fseTablesBuffer, fseMetadata->fseTablesSize);
205
+ op += fseMetadata->fseTablesSize;
206
+ } else {
207
+ const U32 repeat = set_repeat;
208
+ *seqHead = (BYTE)((repeat<<6) + (repeat<<4) + (repeat<<2));
209
+ }
210
+
211
+ { size_t const bitstreamSize = ZSTD_encodeSequences(
212
+ op, oend - op,
213
+ fseTables->matchlengthCTable, mlCode,
214
+ fseTables->offcodeCTable, ofCode,
215
+ fseTables->litlengthCTable, llCode,
216
+ sequences, nbSeq,
217
+ longOffsets, bmi2);
218
+ FORWARD_IF_ERROR(bitstreamSize, "ZSTD_encodeSequences failed");
219
+ op += bitstreamSize;
220
+ /* zstd versions <= 1.3.4 mistakenly report corruption when
221
+ * FSE_readNCount() receives a buffer < 4 bytes.
222
+ * Fixed by https://github.com/facebook/zstd/pull/1146.
223
+ * This can happen when the last set_compressed table present is 2
224
+ * bytes and the bitstream is only one byte.
225
+ * In this exceedingly rare case, we will simply emit an uncompressed
226
+ * block, since it isn't worth optimizing.
227
+ */
228
+ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
+ if (writeEntropy && fseMetadata->lastCountSize && fseMetadata->lastCountSize + bitstreamSize < 4) {
230
+ /* NCountSize >= 2 && bitstreamSize > 0 ==> lastCountSize == 3 */
231
+ assert(fseMetadata->lastCountSize + bitstreamSize == 3);
232
+ DEBUGLOG(5, "Avoiding bug in zstd decoder in versions <= 1.3.4 by "
233
+ "emitting an uncompressed block.");
234
+ return 0;
235
+ }
236
+ #endif
237
+ DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (bitstreamSize=%zu)", bitstreamSize);
238
+ }
239
+
240
+ /* zstd versions <= 1.4.0 mistakenly report error when
241
+ * sequences section body size is less than 3 bytes.
242
+ * Fixed by https://github.com/facebook/zstd/pull/1664.
243
+ * This can happen when the previous sequences section block is compressed
244
+ * with rle mode and the current block's sequences section is compressed
245
+ * with repeat mode where sequences section body size can be 1 byte.
246
+ */
247
+ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
248
+ if (op-seqHead < 4) {
249
+ DEBUGLOG(5, "Avoiding bug in zstd decoder in versions <= 1.4.0 by emitting "
250
+ "an uncompressed block when sequences are < 4 bytes");
251
+ return 0;
252
+ }
253
+ #endif
254
+
255
+ *entropyWritten = 1;
256
+ return op - ostart;
257
+ }
258
+
259
+ /** ZSTD_compressSubBlock() :
260
+ * Compresses a single sub-block.
261
+ * @return : compressed size of the sub-block
262
+ * Or 0 if it failed to compress. */
263
+ static size_t ZSTD_compressSubBlock(const ZSTD_entropyCTables_t* entropy,
264
+ const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
265
+ const seqDef* sequences, size_t nbSeq,
266
+ const BYTE* literals, size_t litSize,
267
+ const BYTE* llCode, const BYTE* mlCode, const BYTE* ofCode,
268
+ const ZSTD_CCtx_params* cctxParams,
269
+ void* dst, size_t dstCapacity,
270
+ const int bmi2,
271
+ int writeLitEntropy, int writeSeqEntropy,
272
+ int* litEntropyWritten, int* seqEntropyWritten,
273
+ U32 lastBlock)
274
+ {
275
+ BYTE* const ostart = (BYTE*)dst;
276
+ BYTE* const oend = ostart + dstCapacity;
277
+ BYTE* op = ostart + ZSTD_blockHeaderSize;
278
+ DEBUGLOG(5, "ZSTD_compressSubBlock (litSize=%zu, nbSeq=%zu, writeLitEntropy=%d, writeSeqEntropy=%d, lastBlock=%d)",
279
+ litSize, nbSeq, writeLitEntropy, writeSeqEntropy, lastBlock);
280
+ { size_t cLitSize = ZSTD_compressSubBlock_literal((const HUF_CElt*)entropy->huf.CTable,
281
+ &entropyMetadata->hufMetadata, literals, litSize,
282
+ op, oend-op, bmi2, writeLitEntropy, litEntropyWritten);
283
+ FORWARD_IF_ERROR(cLitSize, "ZSTD_compressSubBlock_literal failed");
284
+ if (cLitSize == 0) return 0;
285
+ op += cLitSize;
286
+ }
287
+ { size_t cSeqSize = ZSTD_compressSubBlock_sequences(&entropy->fse,
288
+ &entropyMetadata->fseMetadata,
289
+ sequences, nbSeq,
290
+ llCode, mlCode, ofCode,
291
+ cctxParams,
292
+ op, oend-op,
293
+ bmi2, writeSeqEntropy, seqEntropyWritten);
294
+ FORWARD_IF_ERROR(cSeqSize, "ZSTD_compressSubBlock_sequences failed");
295
+ if (cSeqSize == 0) return 0;
296
+ op += cSeqSize;
297
+ }
298
+ /* Write block header */
299
+ { size_t cSize = (op-ostart)-ZSTD_blockHeaderSize;
300
+ U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3);
301
+ MEM_writeLE24(ostart, cBlockHeader24);
302
+ }
303
+ return op-ostart;
304
+ }
305
+
306
+ static size_t ZSTD_estimateSubBlockSize_literal(const BYTE* literals, size_t litSize,
307
+ const ZSTD_hufCTables_t* huf,
308
+ const ZSTD_hufCTablesMetadata_t* hufMetadata,
309
+ void* workspace, size_t wkspSize,
310
+ int writeEntropy)
311
+ {
312
+ unsigned* const countWksp = (unsigned*)workspace;
313
+ unsigned maxSymbolValue = 255;
314
+ size_t literalSectionHeaderSize = 3; /* Use hard coded size of 3 bytes */
315
+
316
+ if (hufMetadata->hType == set_basic) return litSize;
317
+ else if (hufMetadata->hType == set_rle) return 1;
318
+ else if (hufMetadata->hType == set_compressed || hufMetadata->hType == set_repeat) {
319
+ size_t const largest = HIST_count_wksp (countWksp, &maxSymbolValue, (const BYTE*)literals, litSize, workspace, wkspSize);
320
+ if (ZSTD_isError(largest)) return litSize;
321
+ { size_t cLitSizeEstimate = HUF_estimateCompressedSize((const HUF_CElt*)huf->CTable, countWksp, maxSymbolValue);
322
+ if (writeEntropy) cLitSizeEstimate += hufMetadata->hufDesSize;
323
+ return cLitSizeEstimate + literalSectionHeaderSize;
324
+ } }
325
+ assert(0); /* impossible */
326
+ return 0;
327
+ }
328
+
329
+ static size_t ZSTD_estimateSubBlockSize_symbolType(symbolEncodingType_e type,
330
+ const BYTE* codeTable, unsigned maxCode,
331
+ size_t nbSeq, const FSE_CTable* fseCTable,
332
+ const U8* additionalBits,
333
+ short const* defaultNorm, U32 defaultNormLog, U32 defaultMax,
334
+ void* workspace, size_t wkspSize)
335
+ {
336
+ unsigned* const countWksp = (unsigned*)workspace;
337
+ const BYTE* ctp = codeTable;
338
+ const BYTE* const ctStart = ctp;
339
+ const BYTE* const ctEnd = ctStart + nbSeq;
340
+ size_t cSymbolTypeSizeEstimateInBits = 0;
341
+ unsigned max = maxCode;
342
+
343
+ HIST_countFast_wksp(countWksp, &max, codeTable, nbSeq, workspace, wkspSize); /* can't fail */
344
+ if (type == set_basic) {
345
+ /* We selected this encoding type, so it must be valid. */
346
+ assert(max <= defaultMax);
347
+ cSymbolTypeSizeEstimateInBits = max <= defaultMax
348
+ ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max)
349
+ : ERROR(GENERIC);
350
+ } else if (type == set_rle) {
351
+ cSymbolTypeSizeEstimateInBits = 0;
352
+ } else if (type == set_compressed || type == set_repeat) {
353
+ cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max);
354
+ }
355
+ if (ZSTD_isError(cSymbolTypeSizeEstimateInBits)) return nbSeq * 10;
356
+ while (ctp < ctEnd) {
357
+ if (additionalBits) cSymbolTypeSizeEstimateInBits += additionalBits[*ctp];
358
+ else cSymbolTypeSizeEstimateInBits += *ctp; /* for offset, offset code is also the number of additional bits */
359
+ ctp++;
360
+ }
361
+ return cSymbolTypeSizeEstimateInBits / 8;
362
+ }
363
+
364
+ static size_t ZSTD_estimateSubBlockSize_sequences(const BYTE* ofCodeTable,
365
+ const BYTE* llCodeTable,
366
+ const BYTE* mlCodeTable,
367
+ size_t nbSeq,
368
+ const ZSTD_fseCTables_t* fseTables,
369
+ const ZSTD_fseCTablesMetadata_t* fseMetadata,
370
+ void* workspace, size_t wkspSize,
371
+ int writeEntropy)
372
+ {
373
+ size_t const sequencesSectionHeaderSize = 3; /* Use hard coded size of 3 bytes */
374
+ size_t cSeqSizeEstimate = 0;
375
+ if (nbSeq == 0) return sequencesSectionHeaderSize;
376
+ cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, MaxOff,
377
+ nbSeq, fseTables->offcodeCTable, NULL,
378
+ OF_defaultNorm, OF_defaultNormLog, DefaultMaxOff,
379
+ workspace, wkspSize);
380
+ cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->llType, llCodeTable, MaxLL,
381
+ nbSeq, fseTables->litlengthCTable, LL_bits,
382
+ LL_defaultNorm, LL_defaultNormLog, MaxLL,
383
+ workspace, wkspSize);
384
+ cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, MaxML,
385
+ nbSeq, fseTables->matchlengthCTable, ML_bits,
386
+ ML_defaultNorm, ML_defaultNormLog, MaxML,
387
+ workspace, wkspSize);
388
+ if (writeEntropy) cSeqSizeEstimate += fseMetadata->fseTablesSize;
389
+ return cSeqSizeEstimate + sequencesSectionHeaderSize;
390
+ }
391
+
392
+ static size_t ZSTD_estimateSubBlockSize(const BYTE* literals, size_t litSize,
393
+ const BYTE* ofCodeTable,
394
+ const BYTE* llCodeTable,
395
+ const BYTE* mlCodeTable,
396
+ size_t nbSeq,
397
+ const ZSTD_entropyCTables_t* entropy,
398
+ const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
399
+ void* workspace, size_t wkspSize,
400
+ int writeLitEntropy, int writeSeqEntropy) {
401
+ size_t cSizeEstimate = 0;
402
+ cSizeEstimate += ZSTD_estimateSubBlockSize_literal(literals, litSize,
403
+ &entropy->huf, &entropyMetadata->hufMetadata,
404
+ workspace, wkspSize, writeLitEntropy);
405
+ cSizeEstimate += ZSTD_estimateSubBlockSize_sequences(ofCodeTable, llCodeTable, mlCodeTable,
406
+ nbSeq, &entropy->fse, &entropyMetadata->fseMetadata,
407
+ workspace, wkspSize, writeSeqEntropy);
408
+ return cSizeEstimate + ZSTD_blockHeaderSize;
409
+ }
410
+
411
+ static int ZSTD_needSequenceEntropyTables(ZSTD_fseCTablesMetadata_t const* fseMetadata)
412
+ {
413
+ if (fseMetadata->llType == set_compressed || fseMetadata->llType == set_rle)
414
+ return 1;
415
+ if (fseMetadata->mlType == set_compressed || fseMetadata->mlType == set_rle)
416
+ return 1;
417
+ if (fseMetadata->ofType == set_compressed || fseMetadata->ofType == set_rle)
418
+ return 1;
419
+ return 0;
420
+ }
421
+
422
+ /** ZSTD_compressSubBlock_multi() :
423
+ * Breaks super-block into multiple sub-blocks and compresses them.
424
+ * Entropy will be written to the first block.
425
+ * The following blocks will use repeat mode to compress.
426
+ * All sub-blocks are compressed blocks (no raw or rle blocks).
427
+ * @return : compressed size of the super block (which is multiple ZSTD blocks)
428
+ * Or 0 if it failed to compress. */
429
+ static size_t ZSTD_compressSubBlock_multi(const seqStore_t* seqStorePtr,
430
+ const ZSTD_compressedBlockState_t* prevCBlock,
431
+ ZSTD_compressedBlockState_t* nextCBlock,
432
+ const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
433
+ const ZSTD_CCtx_params* cctxParams,
434
+ void* dst, size_t dstCapacity,
435
+ const void* src, size_t srcSize,
436
+ const int bmi2, U32 lastBlock,
437
+ void* workspace, size_t wkspSize)
438
+ {
439
+ const seqDef* const sstart = seqStorePtr->sequencesStart;
440
+ const seqDef* const send = seqStorePtr->sequences;
441
+ const seqDef* sp = sstart;
442
+ const BYTE* const lstart = seqStorePtr->litStart;
443
+ const BYTE* const lend = seqStorePtr->lit;
444
+ const BYTE* lp = lstart;
445
+ BYTE const* ip = (BYTE const*)src;
446
+ BYTE const* const iend = ip + srcSize;
447
+ BYTE* const ostart = (BYTE*)dst;
448
+ BYTE* const oend = ostart + dstCapacity;
449
+ BYTE* op = ostart;
450
+ const BYTE* llCodePtr = seqStorePtr->llCode;
451
+ const BYTE* mlCodePtr = seqStorePtr->mlCode;
452
+ const BYTE* ofCodePtr = seqStorePtr->ofCode;
453
+ size_t targetCBlockSize = cctxParams->targetCBlockSize;
454
+ size_t litSize, seqCount;
455
+ int writeLitEntropy = entropyMetadata->hufMetadata.hType == set_compressed;
456
+ int writeSeqEntropy = 1;
457
+ int lastSequence = 0;
458
+
459
+ DEBUGLOG(5, "ZSTD_compressSubBlock_multi (litSize=%u, nbSeq=%u)",
460
+ (unsigned)(lend-lp), (unsigned)(send-sstart));
461
+
462
+ litSize = 0;
463
+ seqCount = 0;
464
+ do {
465
+ size_t cBlockSizeEstimate = 0;
466
+ if (sstart == send) {
467
+ lastSequence = 1;
468
+ } else {
469
+ const seqDef* const sequence = sp + seqCount;
470
+ lastSequence = sequence == send - 1;
471
+ litSize += ZSTD_getSequenceLength(seqStorePtr, sequence).litLength;
472
+ seqCount++;
473
+ }
474
+ if (lastSequence) {
475
+ assert(lp <= lend);
476
+ assert(litSize <= (size_t)(lend - lp));
477
+ litSize = (size_t)(lend - lp);
478
+ }
479
+ /* I think there is an optimization opportunity here.
480
+ * Calling ZSTD_estimateSubBlockSize for every sequence can be wasteful
481
+ * since it recalculates estimate from scratch.
482
+ * For example, it would recount literal distribution and symbol codes every time.
483
+ */
484
+ cBlockSizeEstimate = ZSTD_estimateSubBlockSize(lp, litSize, ofCodePtr, llCodePtr, mlCodePtr, seqCount,
485
+ &nextCBlock->entropy, entropyMetadata,
486
+ workspace, wkspSize, writeLitEntropy, writeSeqEntropy);
487
+ if (cBlockSizeEstimate > targetCBlockSize || lastSequence) {
488
+ int litEntropyWritten = 0;
489
+ int seqEntropyWritten = 0;
490
+ const size_t decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, lastSequence);
491
+ const size_t cSize = ZSTD_compressSubBlock(&nextCBlock->entropy, entropyMetadata,
492
+ sp, seqCount,
493
+ lp, litSize,
494
+ llCodePtr, mlCodePtr, ofCodePtr,
495
+ cctxParams,
496
+ op, oend-op,
497
+ bmi2, writeLitEntropy, writeSeqEntropy,
498
+ &litEntropyWritten, &seqEntropyWritten,
499
+ lastBlock && lastSequence);
500
+ FORWARD_IF_ERROR(cSize, "ZSTD_compressSubBlock failed");
501
+ if (cSize > 0 && cSize < decompressedSize) {
502
+ DEBUGLOG(5, "Committed the sub-block");
503
+ assert(ip + decompressedSize <= iend);
504
+ ip += decompressedSize;
505
+ sp += seqCount;
506
+ lp += litSize;
507
+ op += cSize;
508
+ llCodePtr += seqCount;
509
+ mlCodePtr += seqCount;
510
+ ofCodePtr += seqCount;
511
+ litSize = 0;
512
+ seqCount = 0;
513
+ /* Entropy only needs to be written once */
514
+ if (litEntropyWritten) {
515
+ writeLitEntropy = 0;
516
+ }
517
+ if (seqEntropyWritten) {
518
+ writeSeqEntropy = 0;
519
+ }
520
+ }
521
+ }
522
+ } while (!lastSequence);
523
+ if (writeLitEntropy) {
524
+ DEBUGLOG(5, "ZSTD_compressSubBlock_multi has literal entropy tables unwritten");
525
+ ZSTD_memcpy(&nextCBlock->entropy.huf, &prevCBlock->entropy.huf, sizeof(prevCBlock->entropy.huf));
526
+ }
527
+ if (writeSeqEntropy && ZSTD_needSequenceEntropyTables(&entropyMetadata->fseMetadata)) {
528
+ /* If we haven't written our entropy tables, then we've violated our contract and
529
+ * must emit an uncompressed block.
530
+ */
531
+ DEBUGLOG(5, "ZSTD_compressSubBlock_multi has sequence entropy tables unwritten");
532
+ return 0;
533
+ }
534
+ if (ip < iend) {
535
+ size_t const cSize = ZSTD_noCompressBlock(op, oend - op, ip, iend - ip, lastBlock);
536
+ DEBUGLOG(5, "ZSTD_compressSubBlock_multi last sub-block uncompressed, %zu bytes", (size_t)(iend - ip));
537
+ FORWARD_IF_ERROR(cSize, "ZSTD_noCompressBlock failed");
538
+ assert(cSize != 0);
539
+ op += cSize;
540
+ /* We have to regenerate the repcodes because we've skipped some sequences */
541
+ if (sp < send) {
542
+ seqDef const* seq;
543
+ repcodes_t rep;
544
+ ZSTD_memcpy(&rep, prevCBlock->rep, sizeof(rep));
545
+ for (seq = sstart; seq < sp; ++seq) {
546
+ ZSTD_updateRep(rep.rep, seq->offBase, ZSTD_getSequenceLength(seqStorePtr, seq).litLength == 0);
547
+ }
548
+ ZSTD_memcpy(nextCBlock->rep, &rep, sizeof(rep));
549
+ }
550
+ }
551
+ DEBUGLOG(5, "ZSTD_compressSubBlock_multi compressed");
552
+ return op-ostart;
553
+ }
554
+
555
+ size_t ZSTD_compressSuperBlock(ZSTD_CCtx* zc,
556
+ void* dst, size_t dstCapacity,
557
+ void const* src, size_t srcSize,
558
+ unsigned lastBlock) {
559
+ ZSTD_entropyCTablesMetadata_t entropyMetadata;
560
+
561
+ FORWARD_IF_ERROR(ZSTD_buildBlockEntropyStats(&zc->seqStore,
562
+ &zc->blockState.prevCBlock->entropy,
563
+ &zc->blockState.nextCBlock->entropy,
564
+ &zc->appliedParams,
565
+ &entropyMetadata,
566
+ zc->entropyWorkspace, ENTROPY_WORKSPACE_SIZE /* statically allocated in resetCCtx */), "");
567
+
568
+ return ZSTD_compressSubBlock_multi(&zc->seqStore,
569
+ zc->blockState.prevCBlock,
570
+ zc->blockState.nextCBlock,
571
+ &entropyMetadata,
572
+ &zc->appliedParams,
573
+ dst, dstCapacity,
574
+ src, srcSize,
575
+ zc->bmi2, lastBlock,
576
+ zc->entropyWorkspace, ENTROPY_WORKSPACE_SIZE /* statically allocated in resetCCtx */);
577
+ }
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright (c) 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
+ #ifndef ZSTD_COMPRESS_ADVANCED_H
12
+ #define ZSTD_COMPRESS_ADVANCED_H
13
+
14
+ /*-*************************************
15
+ * Dependencies
16
+ ***************************************/
17
+
18
+ #include "../zstd.h" /* ZSTD_CCtx */
19
+
20
+ /*-*************************************
21
+ * Target Compressed Block Size
22
+ ***************************************/
23
+
24
+ /* ZSTD_compressSuperBlock() :
25
+ * Used to compress a super block when targetCBlockSize is being used.
26
+ * The given block will be compressed into multiple sub blocks that are around targetCBlockSize. */
27
+ size_t ZSTD_compressSuperBlock(ZSTD_CCtx* zc,
28
+ void* dst, size_t dstCapacity,
29
+ void const* src, size_t srcSize,
30
+ unsigned lastBlock);
31
+
32
+ #endif /* ZSTD_COMPRESS_ADVANCED_H */