0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "zstd_compress_superblock.h"
0015
0016 #include "../common/zstd_internal.h" /* ZSTD_getSequenceLength */
0017 #include "hist.h" /* HIST_countFast_wksp */
0018 #include "zstd_compress_internal.h"
0019 #include "zstd_compress_sequences.h"
0020 #include "zstd_compress_literals.h"
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 typedef struct {
0031 symbolEncodingType_e hType;
0032 BYTE hufDesBuffer[ZSTD_MAX_HUF_HEADER_SIZE];
0033 size_t hufDesSize;
0034 } ZSTD_hufCTablesMetadata_t;
0035
0036
0037
0038
0039
0040
0041 typedef struct {
0042 symbolEncodingType_e llType;
0043 symbolEncodingType_e ofType;
0044 symbolEncodingType_e mlType;
0045 BYTE fseTablesBuffer[ZSTD_MAX_FSE_HEADERS_SIZE];
0046 size_t fseTablesSize;
0047 size_t lastCountSize;
0048 } ZSTD_fseCTablesMetadata_t;
0049
0050 typedef struct {
0051 ZSTD_hufCTablesMetadata_t hufMetadata;
0052 ZSTD_fseCTablesMetadata_t fseMetadata;
0053 } ZSTD_entropyCTablesMetadata_t;
0054
0055
0056
0057
0058
0059
0060
0061 static size_t ZSTD_buildSuperBlockEntropy_literal(void* const src, size_t srcSize,
0062 const ZSTD_hufCTables_t* prevHuf,
0063 ZSTD_hufCTables_t* nextHuf,
0064 ZSTD_hufCTablesMetadata_t* hufMetadata,
0065 const int disableLiteralsCompression,
0066 void* workspace, size_t wkspSize)
0067 {
0068 BYTE* const wkspStart = (BYTE*)workspace;
0069 BYTE* const wkspEnd = wkspStart + wkspSize;
0070 BYTE* const countWkspStart = wkspStart;
0071 unsigned* const countWksp = (unsigned*)workspace;
0072 const size_t countWkspSize = (HUF_SYMBOLVALUE_MAX + 1) * sizeof(unsigned);
0073 BYTE* const nodeWksp = countWkspStart + countWkspSize;
0074 const size_t nodeWkspSize = wkspEnd-nodeWksp;
0075 unsigned maxSymbolValue = 255;
0076 unsigned huffLog = HUF_TABLELOG_DEFAULT;
0077 HUF_repeat repeat = prevHuf->repeatMode;
0078
0079 DEBUGLOG(5, "ZSTD_buildSuperBlockEntropy_literal (srcSize=%zu)", srcSize);
0080
0081
0082 ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
0083
0084 if (disableLiteralsCompression) {
0085 DEBUGLOG(5, "set_basic - disabled");
0086 hufMetadata->hType = set_basic;
0087 return 0;
0088 }
0089
0090
0091 # define COMPRESS_LITERALS_SIZE_MIN 63
0092 { size_t const minLitSize = (prevHuf->repeatMode == HUF_repeat_valid) ? 6 : COMPRESS_LITERALS_SIZE_MIN;
0093 if (srcSize <= minLitSize) {
0094 DEBUGLOG(5, "set_basic - too small");
0095 hufMetadata->hType = set_basic;
0096 return 0;
0097 }
0098 }
0099
0100
0101 { size_t const largest = HIST_count_wksp (countWksp, &maxSymbolValue, (const BYTE*)src, srcSize, workspace, wkspSize);
0102 FORWARD_IF_ERROR(largest, "HIST_count_wksp failed");
0103 if (largest == srcSize) {
0104 DEBUGLOG(5, "set_rle");
0105 hufMetadata->hType = set_rle;
0106 return 0;
0107 }
0108 if (largest <= (srcSize >> 7)+4) {
0109 DEBUGLOG(5, "set_basic - no gain");
0110 hufMetadata->hType = set_basic;
0111 return 0;
0112 }
0113 }
0114
0115
0116 if (repeat == HUF_repeat_check && !HUF_validateCTable((HUF_CElt const*)prevHuf->CTable, countWksp, maxSymbolValue)) {
0117 repeat = HUF_repeat_none;
0118 }
0119
0120
0121 ZSTD_memset(nextHuf->CTable, 0, sizeof(nextHuf->CTable));
0122 huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
0123 { size_t const maxBits = HUF_buildCTable_wksp((HUF_CElt*)nextHuf->CTable, countWksp,
0124 maxSymbolValue, huffLog,
0125 nodeWksp, nodeWkspSize);
0126 FORWARD_IF_ERROR(maxBits, "HUF_buildCTable_wksp");
0127 huffLog = (U32)maxBits;
0128 {
0129 size_t const newCSize = HUF_estimateCompressedSize(
0130 (HUF_CElt*)nextHuf->CTable, countWksp, maxSymbolValue);
0131 size_t const hSize = HUF_writeCTable_wksp(
0132 hufMetadata->hufDesBuffer, sizeof(hufMetadata->hufDesBuffer),
0133 (HUF_CElt*)nextHuf->CTable, maxSymbolValue, huffLog,
0134 nodeWksp, nodeWkspSize);
0135
0136 if (repeat != HUF_repeat_none) {
0137 size_t const oldCSize = HUF_estimateCompressedSize(
0138 (HUF_CElt const*)prevHuf->CTable, countWksp, maxSymbolValue);
0139 if (oldCSize < srcSize && (oldCSize <= hSize + newCSize || hSize + 12 >= srcSize)) {
0140 DEBUGLOG(5, "set_repeat - smaller");
0141 ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
0142 hufMetadata->hType = set_repeat;
0143 return 0;
0144 }
0145 }
0146 if (newCSize + hSize >= srcSize) {
0147 DEBUGLOG(5, "set_basic - no gains");
0148 ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
0149 hufMetadata->hType = set_basic;
0150 return 0;
0151 }
0152 DEBUGLOG(5, "set_compressed (hSize=%u)", (U32)hSize);
0153 hufMetadata->hType = set_compressed;
0154 nextHuf->repeatMode = HUF_repeat_check;
0155 return hSize;
0156 }
0157 }
0158 }
0159
0160
0161
0162
0163
0164 static size_t ZSTD_buildSuperBlockEntropy_sequences(seqStore_t* seqStorePtr,
0165 const ZSTD_fseCTables_t* prevEntropy,
0166 ZSTD_fseCTables_t* nextEntropy,
0167 const ZSTD_CCtx_params* cctxParams,
0168 ZSTD_fseCTablesMetadata_t* fseMetadata,
0169 void* workspace, size_t wkspSize)
0170 {
0171 BYTE* const wkspStart = (BYTE*)workspace;
0172 BYTE* const wkspEnd = wkspStart + wkspSize;
0173 BYTE* const countWkspStart = wkspStart;
0174 unsigned* const countWksp = (unsigned*)workspace;
0175 const size_t countWkspSize = (MaxSeq + 1) * sizeof(unsigned);
0176 BYTE* const cTableWksp = countWkspStart + countWkspSize;
0177 const size_t cTableWkspSize = wkspEnd-cTableWksp;
0178 ZSTD_strategy const strategy = cctxParams->cParams.strategy;
0179 FSE_CTable* CTable_LitLength = nextEntropy->litlengthCTable;
0180 FSE_CTable* CTable_OffsetBits = nextEntropy->offcodeCTable;
0181 FSE_CTable* CTable_MatchLength = nextEntropy->matchlengthCTable;
0182 const BYTE* const ofCodeTable = seqStorePtr->ofCode;
0183 const BYTE* const llCodeTable = seqStorePtr->llCode;
0184 const BYTE* const mlCodeTable = seqStorePtr->mlCode;
0185 size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart;
0186 BYTE* const ostart = fseMetadata->fseTablesBuffer;
0187 BYTE* const oend = ostart + sizeof(fseMetadata->fseTablesBuffer);
0188 BYTE* op = ostart;
0189
0190 assert(cTableWkspSize >= (1 << MaxFSELog) * sizeof(FSE_FUNCTION_TYPE));
0191 DEBUGLOG(5, "ZSTD_buildSuperBlockEntropy_sequences (nbSeq=%zu)", nbSeq);
0192 ZSTD_memset(workspace, 0, wkspSize);
0193
0194 fseMetadata->lastCountSize = 0;
0195
0196 ZSTD_seqToCodes(seqStorePtr);
0197
0198 { U32 LLtype;
0199 unsigned max = MaxLL;
0200 size_t const mostFrequent = HIST_countFast_wksp(countWksp, &max, llCodeTable, nbSeq, workspace, wkspSize);
0201 DEBUGLOG(5, "Building LL table");
0202 nextEntropy->litlength_repeatMode = prevEntropy->litlength_repeatMode;
0203 LLtype = ZSTD_selectEncodingType(&nextEntropy->litlength_repeatMode,
0204 countWksp, max, mostFrequent, nbSeq,
0205 LLFSELog, prevEntropy->litlengthCTable,
0206 LL_defaultNorm, LL_defaultNormLog,
0207 ZSTD_defaultAllowed, strategy);
0208 assert(set_basic < set_compressed && set_rle < set_compressed);
0209 assert(!(LLtype < set_compressed && nextEntropy->litlength_repeatMode != FSE_repeat_none));
0210 { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_LitLength, LLFSELog, (symbolEncodingType_e)LLtype,
0211 countWksp, max, llCodeTable, nbSeq, LL_defaultNorm, LL_defaultNormLog, MaxLL,
0212 prevEntropy->litlengthCTable, sizeof(prevEntropy->litlengthCTable),
0213 cTableWksp, cTableWkspSize);
0214 FORWARD_IF_ERROR(countSize, "ZSTD_buildCTable for LitLens failed");
0215 if (LLtype == set_compressed)
0216 fseMetadata->lastCountSize = countSize;
0217 op += countSize;
0218 fseMetadata->llType = (symbolEncodingType_e) LLtype;
0219 } }
0220
0221 { U32 Offtype;
0222 unsigned max = MaxOff;
0223 size_t const mostFrequent = HIST_countFast_wksp(countWksp, &max, ofCodeTable, nbSeq, workspace, wkspSize);
0224
0225 ZSTD_defaultPolicy_e const defaultPolicy = (max <= DefaultMaxOff) ? ZSTD_defaultAllowed : ZSTD_defaultDisallowed;
0226 DEBUGLOG(5, "Building OF table");
0227 nextEntropy->offcode_repeatMode = prevEntropy->offcode_repeatMode;
0228 Offtype = ZSTD_selectEncodingType(&nextEntropy->offcode_repeatMode,
0229 countWksp, max, mostFrequent, nbSeq,
0230 OffFSELog, prevEntropy->offcodeCTable,
0231 OF_defaultNorm, OF_defaultNormLog,
0232 defaultPolicy, strategy);
0233 assert(!(Offtype < set_compressed && nextEntropy->offcode_repeatMode != FSE_repeat_none));
0234 { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_OffsetBits, OffFSELog, (symbolEncodingType_e)Offtype,
0235 countWksp, max, ofCodeTable, nbSeq, OF_defaultNorm, OF_defaultNormLog, DefaultMaxOff,
0236 prevEntropy->offcodeCTable, sizeof(prevEntropy->offcodeCTable),
0237 cTableWksp, cTableWkspSize);
0238 FORWARD_IF_ERROR(countSize, "ZSTD_buildCTable for Offsets failed");
0239 if (Offtype == set_compressed)
0240 fseMetadata->lastCountSize = countSize;
0241 op += countSize;
0242 fseMetadata->ofType = (symbolEncodingType_e) Offtype;
0243 } }
0244
0245 { U32 MLtype;
0246 unsigned max = MaxML;
0247 size_t const mostFrequent = HIST_countFast_wksp(countWksp, &max, mlCodeTable, nbSeq, workspace, wkspSize);
0248 DEBUGLOG(5, "Building ML table (remaining space : %i)", (int)(oend-op));
0249 nextEntropy->matchlength_repeatMode = prevEntropy->matchlength_repeatMode;
0250 MLtype = ZSTD_selectEncodingType(&nextEntropy->matchlength_repeatMode,
0251 countWksp, max, mostFrequent, nbSeq,
0252 MLFSELog, prevEntropy->matchlengthCTable,
0253 ML_defaultNorm, ML_defaultNormLog,
0254 ZSTD_defaultAllowed, strategy);
0255 assert(!(MLtype < set_compressed && nextEntropy->matchlength_repeatMode != FSE_repeat_none));
0256 { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_MatchLength, MLFSELog, (symbolEncodingType_e)MLtype,
0257 countWksp, max, mlCodeTable, nbSeq, ML_defaultNorm, ML_defaultNormLog, MaxML,
0258 prevEntropy->matchlengthCTable, sizeof(prevEntropy->matchlengthCTable),
0259 cTableWksp, cTableWkspSize);
0260 FORWARD_IF_ERROR(countSize, "ZSTD_buildCTable for MatchLengths failed");
0261 if (MLtype == set_compressed)
0262 fseMetadata->lastCountSize = countSize;
0263 op += countSize;
0264 fseMetadata->mlType = (symbolEncodingType_e) MLtype;
0265 } }
0266 assert((size_t) (op-ostart) <= sizeof(fseMetadata->fseTablesBuffer));
0267 return op-ostart;
0268 }
0269
0270
0271
0272
0273
0274 static size_t
0275 ZSTD_buildSuperBlockEntropy(seqStore_t* seqStorePtr,
0276 const ZSTD_entropyCTables_t* prevEntropy,
0277 ZSTD_entropyCTables_t* nextEntropy,
0278 const ZSTD_CCtx_params* cctxParams,
0279 ZSTD_entropyCTablesMetadata_t* entropyMetadata,
0280 void* workspace, size_t wkspSize)
0281 {
0282 size_t const litSize = seqStorePtr->lit - seqStorePtr->litStart;
0283 DEBUGLOG(5, "ZSTD_buildSuperBlockEntropy");
0284 entropyMetadata->hufMetadata.hufDesSize =
0285 ZSTD_buildSuperBlockEntropy_literal(seqStorePtr->litStart, litSize,
0286 &prevEntropy->huf, &nextEntropy->huf,
0287 &entropyMetadata->hufMetadata,
0288 ZSTD_disableLiteralsCompression(cctxParams),
0289 workspace, wkspSize);
0290 FORWARD_IF_ERROR(entropyMetadata->hufMetadata.hufDesSize, "ZSTD_buildSuperBlockEntropy_literal failed");
0291 entropyMetadata->fseMetadata.fseTablesSize =
0292 ZSTD_buildSuperBlockEntropy_sequences(seqStorePtr,
0293 &prevEntropy->fse, &nextEntropy->fse,
0294 cctxParams,
0295 &entropyMetadata->fseMetadata,
0296 workspace, wkspSize);
0297 FORWARD_IF_ERROR(entropyMetadata->fseMetadata.fseTablesSize, "ZSTD_buildSuperBlockEntropy_sequences failed");
0298 return 0;
0299 }
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320 static size_t ZSTD_compressSubBlock_literal(const HUF_CElt* hufTable,
0321 const ZSTD_hufCTablesMetadata_t* hufMetadata,
0322 const BYTE* literals, size_t litSize,
0323 void* dst, size_t dstSize,
0324 const int bmi2, int writeEntropy, int* entropyWritten)
0325 {
0326 size_t const header = writeEntropy ? 200 : 0;
0327 size_t const lhSize = 3 + (litSize >= (1 KB - header)) + (litSize >= (16 KB - header));
0328 BYTE* const ostart = (BYTE*)dst;
0329 BYTE* const oend = ostart + dstSize;
0330 BYTE* op = ostart + lhSize;
0331 U32 const singleStream = lhSize == 3;
0332 symbolEncodingType_e hType = writeEntropy ? hufMetadata->hType : set_repeat;
0333 size_t cLitSize = 0;
0334
0335 (void)bmi2;
0336
0337 DEBUGLOG(5, "ZSTD_compressSubBlock_literal (litSize=%zu, lhSize=%zu, writeEntropy=%d)", litSize, lhSize, writeEntropy);
0338
0339 *entropyWritten = 0;
0340 if (litSize == 0 || hufMetadata->hType == set_basic) {
0341 DEBUGLOG(5, "ZSTD_compressSubBlock_literal using raw literal");
0342 return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
0343 } else if (hufMetadata->hType == set_rle) {
0344 DEBUGLOG(5, "ZSTD_compressSubBlock_literal using rle literal");
0345 return ZSTD_compressRleLiteralsBlock(dst, dstSize, literals, litSize);
0346 }
0347
0348 assert(litSize > 0);
0349 assert(hufMetadata->hType == set_compressed || hufMetadata->hType == set_repeat);
0350
0351 if (writeEntropy && hufMetadata->hType == set_compressed) {
0352 ZSTD_memcpy(op, hufMetadata->hufDesBuffer, hufMetadata->hufDesSize);
0353 op += hufMetadata->hufDesSize;
0354 cLitSize += hufMetadata->hufDesSize;
0355 DEBUGLOG(5, "ZSTD_compressSubBlock_literal (hSize=%zu)", hufMetadata->hufDesSize);
0356 }
0357
0358
0359 { const size_t cSize = singleStream ? HUF_compress1X_usingCTable(op, oend-op, literals, litSize, hufTable)
0360 : HUF_compress4X_usingCTable(op, oend-op, literals, litSize, hufTable);
0361 op += cSize;
0362 cLitSize += cSize;
0363 if (cSize == 0 || ERR_isError(cSize)) {
0364 DEBUGLOG(5, "Failed to write entropy tables %s", ZSTD_getErrorName(cSize));
0365 return 0;
0366 }
0367
0368 if (!writeEntropy && cLitSize >= litSize) {
0369 DEBUGLOG(5, "ZSTD_compressSubBlock_literal using raw literal because uncompressible");
0370 return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
0371 }
0372
0373 if (lhSize < (size_t)(3 + (cLitSize >= 1 KB) + (cLitSize >= 16 KB))) {
0374 assert(cLitSize > litSize);
0375 DEBUGLOG(5, "Literals expanded beyond allowed header size");
0376 return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize);
0377 }
0378 DEBUGLOG(5, "ZSTD_compressSubBlock_literal (cSize=%zu)", cSize);
0379 }
0380
0381
0382 switch(lhSize)
0383 {
0384 case 3:
0385 { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<14);
0386 MEM_writeLE24(ostart, lhc);
0387 break;
0388 }
0389 case 4:
0390 { U32 const lhc = hType + (2 << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<18);
0391 MEM_writeLE32(ostart, lhc);
0392 break;
0393 }
0394 case 5:
0395 { U32 const lhc = hType + (3 << 2) + ((U32)litSize<<4) + ((U32)cLitSize<<22);
0396 MEM_writeLE32(ostart, lhc);
0397 ostart[4] = (BYTE)(cLitSize >> 10);
0398 break;
0399 }
0400 default:
0401 assert(0);
0402 }
0403 *entropyWritten = 1;
0404 DEBUGLOG(5, "Compressed literals: %u -> %u", (U32)litSize, (U32)(op-ostart));
0405 return op-ostart;
0406 }
0407
0408 static size_t ZSTD_seqDecompressedSize(seqStore_t const* seqStore, const seqDef* sequences, size_t nbSeq, size_t litSize, int lastSequence) {
0409 const seqDef* const sstart = sequences;
0410 const seqDef* const send = sequences + nbSeq;
0411 const seqDef* sp = sstart;
0412 size_t matchLengthSum = 0;
0413 size_t litLengthSum = 0;
0414
0415 (void)litLengthSum;
0416 while (send-sp > 0) {
0417 ZSTD_sequenceLength const seqLen = ZSTD_getSequenceLength(seqStore, sp);
0418 litLengthSum += seqLen.litLength;
0419 matchLengthSum += seqLen.matchLength;
0420 sp++;
0421 }
0422 assert(litLengthSum <= litSize);
0423 if (!lastSequence) {
0424 assert(litLengthSum == litSize);
0425 }
0426 return matchLengthSum + litSize;
0427 }
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439 static size_t ZSTD_compressSubBlock_sequences(const ZSTD_fseCTables_t* fseTables,
0440 const ZSTD_fseCTablesMetadata_t* fseMetadata,
0441 const seqDef* sequences, size_t nbSeq,
0442 const BYTE* llCode, const BYTE* mlCode, const BYTE* ofCode,
0443 const ZSTD_CCtx_params* cctxParams,
0444 void* dst, size_t dstCapacity,
0445 const int bmi2, int writeEntropy, int* entropyWritten)
0446 {
0447 const int longOffsets = cctxParams->cParams.windowLog > STREAM_ACCUMULATOR_MIN;
0448 BYTE* const ostart = (BYTE*)dst;
0449 BYTE* const oend = ostart + dstCapacity;
0450 BYTE* op = ostart;
0451 BYTE* seqHead;
0452
0453 DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (nbSeq=%zu, writeEntropy=%d, longOffsets=%d)", nbSeq, writeEntropy, longOffsets);
0454
0455 *entropyWritten = 0;
0456
0457 RETURN_ERROR_IF((oend-op) < 3 + 1 ,
0458 dstSize_tooSmall, "");
0459 if (nbSeq < 0x7F)
0460 *op++ = (BYTE)nbSeq;
0461 else if (nbSeq < LONGNBSEQ)
0462 op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
0463 else
0464 op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3;
0465 if (nbSeq==0) {
0466 return op - ostart;
0467 }
0468
0469
0470 seqHead = op++;
0471
0472 DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (seqHeadSize=%u)", (unsigned)(op-ostart));
0473
0474 if (writeEntropy) {
0475 const U32 LLtype = fseMetadata->llType;
0476 const U32 Offtype = fseMetadata->ofType;
0477 const U32 MLtype = fseMetadata->mlType;
0478 DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (fseTablesSize=%zu)", fseMetadata->fseTablesSize);
0479 *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
0480 ZSTD_memcpy(op, fseMetadata->fseTablesBuffer, fseMetadata->fseTablesSize);
0481 op += fseMetadata->fseTablesSize;
0482 } else {
0483 const U32 repeat = set_repeat;
0484 *seqHead = (BYTE)((repeat<<6) + (repeat<<4) + (repeat<<2));
0485 }
0486
0487 { size_t const bitstreamSize = ZSTD_encodeSequences(
0488 op, oend - op,
0489 fseTables->matchlengthCTable, mlCode,
0490 fseTables->offcodeCTable, ofCode,
0491 fseTables->litlengthCTable, llCode,
0492 sequences, nbSeq,
0493 longOffsets, bmi2);
0494 FORWARD_IF_ERROR(bitstreamSize, "ZSTD_encodeSequences failed");
0495 op += bitstreamSize;
0496
0497
0498
0499
0500
0501
0502
0503
0504 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
0505 if (writeEntropy && fseMetadata->lastCountSize && fseMetadata->lastCountSize + bitstreamSize < 4) {
0506
0507 assert(fseMetadata->lastCountSize + bitstreamSize == 3);
0508 DEBUGLOG(5, "Avoiding bug in zstd decoder in versions <= 1.3.4 by "
0509 "emitting an uncompressed block.");
0510 return 0;
0511 }
0512 #endif
0513 DEBUGLOG(5, "ZSTD_compressSubBlock_sequences (bitstreamSize=%zu)", bitstreamSize);
0514 }
0515
0516
0517
0518
0519
0520
0521
0522
0523 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
0524 if (op-seqHead < 4) {
0525 DEBUGLOG(5, "Avoiding bug in zstd decoder in versions <= 1.4.0 by emitting "
0526 "an uncompressed block when sequences are < 4 bytes");
0527 return 0;
0528 }
0529 #endif
0530
0531 *entropyWritten = 1;
0532 return op - ostart;
0533 }
0534
0535
0536
0537
0538
0539 static size_t ZSTD_compressSubBlock(const ZSTD_entropyCTables_t* entropy,
0540 const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
0541 const seqDef* sequences, size_t nbSeq,
0542 const BYTE* literals, size_t litSize,
0543 const BYTE* llCode, const BYTE* mlCode, const BYTE* ofCode,
0544 const ZSTD_CCtx_params* cctxParams,
0545 void* dst, size_t dstCapacity,
0546 const int bmi2,
0547 int writeLitEntropy, int writeSeqEntropy,
0548 int* litEntropyWritten, int* seqEntropyWritten,
0549 U32 lastBlock)
0550 {
0551 BYTE* const ostart = (BYTE*)dst;
0552 BYTE* const oend = ostart + dstCapacity;
0553 BYTE* op = ostart + ZSTD_blockHeaderSize;
0554 DEBUGLOG(5, "ZSTD_compressSubBlock (litSize=%zu, nbSeq=%zu, writeLitEntropy=%d, writeSeqEntropy=%d, lastBlock=%d)",
0555 litSize, nbSeq, writeLitEntropy, writeSeqEntropy, lastBlock);
0556 { size_t cLitSize = ZSTD_compressSubBlock_literal((const HUF_CElt*)entropy->huf.CTable,
0557 &entropyMetadata->hufMetadata, literals, litSize,
0558 op, oend-op, bmi2, writeLitEntropy, litEntropyWritten);
0559 FORWARD_IF_ERROR(cLitSize, "ZSTD_compressSubBlock_literal failed");
0560 if (cLitSize == 0) return 0;
0561 op += cLitSize;
0562 }
0563 { size_t cSeqSize = ZSTD_compressSubBlock_sequences(&entropy->fse,
0564 &entropyMetadata->fseMetadata,
0565 sequences, nbSeq,
0566 llCode, mlCode, ofCode,
0567 cctxParams,
0568 op, oend-op,
0569 bmi2, writeSeqEntropy, seqEntropyWritten);
0570 FORWARD_IF_ERROR(cSeqSize, "ZSTD_compressSubBlock_sequences failed");
0571 if (cSeqSize == 0) return 0;
0572 op += cSeqSize;
0573 }
0574
0575 { size_t cSize = (op-ostart)-ZSTD_blockHeaderSize;
0576 U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3);
0577 MEM_writeLE24(ostart, cBlockHeader24);
0578 }
0579 return op-ostart;
0580 }
0581
0582 static size_t ZSTD_estimateSubBlockSize_literal(const BYTE* literals, size_t litSize,
0583 const ZSTD_hufCTables_t* huf,
0584 const ZSTD_hufCTablesMetadata_t* hufMetadata,
0585 void* workspace, size_t wkspSize,
0586 int writeEntropy)
0587 {
0588 unsigned* const countWksp = (unsigned*)workspace;
0589 unsigned maxSymbolValue = 255;
0590 size_t literalSectionHeaderSize = 3;
0591
0592 if (hufMetadata->hType == set_basic) return litSize;
0593 else if (hufMetadata->hType == set_rle) return 1;
0594 else if (hufMetadata->hType == set_compressed || hufMetadata->hType == set_repeat) {
0595 size_t const largest = HIST_count_wksp (countWksp, &maxSymbolValue, (const BYTE*)literals, litSize, workspace, wkspSize);
0596 if (ZSTD_isError(largest)) return litSize;
0597 { size_t cLitSizeEstimate = HUF_estimateCompressedSize((const HUF_CElt*)huf->CTable, countWksp, maxSymbolValue);
0598 if (writeEntropy) cLitSizeEstimate += hufMetadata->hufDesSize;
0599 return cLitSizeEstimate + literalSectionHeaderSize;
0600 } }
0601 assert(0);
0602 return 0;
0603 }
0604
0605 static size_t ZSTD_estimateSubBlockSize_symbolType(symbolEncodingType_e type,
0606 const BYTE* codeTable, unsigned maxCode,
0607 size_t nbSeq, const FSE_CTable* fseCTable,
0608 const U32* additionalBits,
0609 short const* defaultNorm, U32 defaultNormLog, U32 defaultMax,
0610 void* workspace, size_t wkspSize)
0611 {
0612 unsigned* const countWksp = (unsigned*)workspace;
0613 const BYTE* ctp = codeTable;
0614 const BYTE* const ctStart = ctp;
0615 const BYTE* const ctEnd = ctStart + nbSeq;
0616 size_t cSymbolTypeSizeEstimateInBits = 0;
0617 unsigned max = maxCode;
0618
0619 HIST_countFast_wksp(countWksp, &max, codeTable, nbSeq, workspace, wkspSize);
0620 if (type == set_basic) {
0621
0622 assert(max <= defaultMax);
0623 cSymbolTypeSizeEstimateInBits = max <= defaultMax
0624 ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max)
0625 : ERROR(GENERIC);
0626 } else if (type == set_rle) {
0627 cSymbolTypeSizeEstimateInBits = 0;
0628 } else if (type == set_compressed || type == set_repeat) {
0629 cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max);
0630 }
0631 if (ZSTD_isError(cSymbolTypeSizeEstimateInBits)) return nbSeq * 10;
0632 while (ctp < ctEnd) {
0633 if (additionalBits) cSymbolTypeSizeEstimateInBits += additionalBits[*ctp];
0634 else cSymbolTypeSizeEstimateInBits += *ctp;
0635 ctp++;
0636 }
0637 return cSymbolTypeSizeEstimateInBits / 8;
0638 }
0639
0640 static size_t ZSTD_estimateSubBlockSize_sequences(const BYTE* ofCodeTable,
0641 const BYTE* llCodeTable,
0642 const BYTE* mlCodeTable,
0643 size_t nbSeq,
0644 const ZSTD_fseCTables_t* fseTables,
0645 const ZSTD_fseCTablesMetadata_t* fseMetadata,
0646 void* workspace, size_t wkspSize,
0647 int writeEntropy)
0648 {
0649 size_t sequencesSectionHeaderSize = 3;
0650 size_t cSeqSizeEstimate = 0;
0651 cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, MaxOff,
0652 nbSeq, fseTables->offcodeCTable, NULL,
0653 OF_defaultNorm, OF_defaultNormLog, DefaultMaxOff,
0654 workspace, wkspSize);
0655 cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->llType, llCodeTable, MaxLL,
0656 nbSeq, fseTables->litlengthCTable, LL_bits,
0657 LL_defaultNorm, LL_defaultNormLog, MaxLL,
0658 workspace, wkspSize);
0659 cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, MaxML,
0660 nbSeq, fseTables->matchlengthCTable, ML_bits,
0661 ML_defaultNorm, ML_defaultNormLog, MaxML,
0662 workspace, wkspSize);
0663 if (writeEntropy) cSeqSizeEstimate += fseMetadata->fseTablesSize;
0664 return cSeqSizeEstimate + sequencesSectionHeaderSize;
0665 }
0666
0667 static size_t ZSTD_estimateSubBlockSize(const BYTE* literals, size_t litSize,
0668 const BYTE* ofCodeTable,
0669 const BYTE* llCodeTable,
0670 const BYTE* mlCodeTable,
0671 size_t nbSeq,
0672 const ZSTD_entropyCTables_t* entropy,
0673 const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
0674 void* workspace, size_t wkspSize,
0675 int writeLitEntropy, int writeSeqEntropy) {
0676 size_t cSizeEstimate = 0;
0677 cSizeEstimate += ZSTD_estimateSubBlockSize_literal(literals, litSize,
0678 &entropy->huf, &entropyMetadata->hufMetadata,
0679 workspace, wkspSize, writeLitEntropy);
0680 cSizeEstimate += ZSTD_estimateSubBlockSize_sequences(ofCodeTable, llCodeTable, mlCodeTable,
0681 nbSeq, &entropy->fse, &entropyMetadata->fseMetadata,
0682 workspace, wkspSize, writeSeqEntropy);
0683 return cSizeEstimate + ZSTD_blockHeaderSize;
0684 }
0685
0686 static int ZSTD_needSequenceEntropyTables(ZSTD_fseCTablesMetadata_t const* fseMetadata)
0687 {
0688 if (fseMetadata->llType == set_compressed || fseMetadata->llType == set_rle)
0689 return 1;
0690 if (fseMetadata->mlType == set_compressed || fseMetadata->mlType == set_rle)
0691 return 1;
0692 if (fseMetadata->ofType == set_compressed || fseMetadata->ofType == set_rle)
0693 return 1;
0694 return 0;
0695 }
0696
0697
0698
0699
0700
0701
0702
0703
0704 static size_t ZSTD_compressSubBlock_multi(const seqStore_t* seqStorePtr,
0705 const ZSTD_compressedBlockState_t* prevCBlock,
0706 ZSTD_compressedBlockState_t* nextCBlock,
0707 const ZSTD_entropyCTablesMetadata_t* entropyMetadata,
0708 const ZSTD_CCtx_params* cctxParams,
0709 void* dst, size_t dstCapacity,
0710 const void* src, size_t srcSize,
0711 const int bmi2, U32 lastBlock,
0712 void* workspace, size_t wkspSize)
0713 {
0714 const seqDef* const sstart = seqStorePtr->sequencesStart;
0715 const seqDef* const send = seqStorePtr->sequences;
0716 const seqDef* sp = sstart;
0717 const BYTE* const lstart = seqStorePtr->litStart;
0718 const BYTE* const lend = seqStorePtr->lit;
0719 const BYTE* lp = lstart;
0720 BYTE const* ip = (BYTE const*)src;
0721 BYTE const* const iend = ip + srcSize;
0722 BYTE* const ostart = (BYTE*)dst;
0723 BYTE* const oend = ostart + dstCapacity;
0724 BYTE* op = ostart;
0725 const BYTE* llCodePtr = seqStorePtr->llCode;
0726 const BYTE* mlCodePtr = seqStorePtr->mlCode;
0727 const BYTE* ofCodePtr = seqStorePtr->ofCode;
0728 size_t targetCBlockSize = cctxParams->targetCBlockSize;
0729 size_t litSize, seqCount;
0730 int writeLitEntropy = entropyMetadata->hufMetadata.hType == set_compressed;
0731 int writeSeqEntropy = 1;
0732 int lastSequence = 0;
0733
0734 DEBUGLOG(5, "ZSTD_compressSubBlock_multi (litSize=%u, nbSeq=%u)",
0735 (unsigned)(lend-lp), (unsigned)(send-sstart));
0736
0737 litSize = 0;
0738 seqCount = 0;
0739 do {
0740 size_t cBlockSizeEstimate = 0;
0741 if (sstart == send) {
0742 lastSequence = 1;
0743 } else {
0744 const seqDef* const sequence = sp + seqCount;
0745 lastSequence = sequence == send - 1;
0746 litSize += ZSTD_getSequenceLength(seqStorePtr, sequence).litLength;
0747 seqCount++;
0748 }
0749 if (lastSequence) {
0750 assert(lp <= lend);
0751 assert(litSize <= (size_t)(lend - lp));
0752 litSize = (size_t)(lend - lp);
0753 }
0754
0755
0756
0757
0758
0759 cBlockSizeEstimate = ZSTD_estimateSubBlockSize(lp, litSize, ofCodePtr, llCodePtr, mlCodePtr, seqCount,
0760 &nextCBlock->entropy, entropyMetadata,
0761 workspace, wkspSize, writeLitEntropy, writeSeqEntropy);
0762 if (cBlockSizeEstimate > targetCBlockSize || lastSequence) {
0763 int litEntropyWritten = 0;
0764 int seqEntropyWritten = 0;
0765 const size_t decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, lastSequence);
0766 const size_t cSize = ZSTD_compressSubBlock(&nextCBlock->entropy, entropyMetadata,
0767 sp, seqCount,
0768 lp, litSize,
0769 llCodePtr, mlCodePtr, ofCodePtr,
0770 cctxParams,
0771 op, oend-op,
0772 bmi2, writeLitEntropy, writeSeqEntropy,
0773 &litEntropyWritten, &seqEntropyWritten,
0774 lastBlock && lastSequence);
0775 FORWARD_IF_ERROR(cSize, "ZSTD_compressSubBlock failed");
0776 if (cSize > 0 && cSize < decompressedSize) {
0777 DEBUGLOG(5, "Committed the sub-block");
0778 assert(ip + decompressedSize <= iend);
0779 ip += decompressedSize;
0780 sp += seqCount;
0781 lp += litSize;
0782 op += cSize;
0783 llCodePtr += seqCount;
0784 mlCodePtr += seqCount;
0785 ofCodePtr += seqCount;
0786 litSize = 0;
0787 seqCount = 0;
0788
0789 if (litEntropyWritten) {
0790 writeLitEntropy = 0;
0791 }
0792 if (seqEntropyWritten) {
0793 writeSeqEntropy = 0;
0794 }
0795 }
0796 }
0797 } while (!lastSequence);
0798 if (writeLitEntropy) {
0799 DEBUGLOG(5, "ZSTD_compressSubBlock_multi has literal entropy tables unwritten");
0800 ZSTD_memcpy(&nextCBlock->entropy.huf, &prevCBlock->entropy.huf, sizeof(prevCBlock->entropy.huf));
0801 }
0802 if (writeSeqEntropy && ZSTD_needSequenceEntropyTables(&entropyMetadata->fseMetadata)) {
0803
0804
0805
0806 DEBUGLOG(5, "ZSTD_compressSubBlock_multi has sequence entropy tables unwritten");
0807 return 0;
0808 }
0809 if (ip < iend) {
0810 size_t const cSize = ZSTD_noCompressBlock(op, oend - op, ip, iend - ip, lastBlock);
0811 DEBUGLOG(5, "ZSTD_compressSubBlock_multi last sub-block uncompressed, %zu bytes", (size_t)(iend - ip));
0812 FORWARD_IF_ERROR(cSize, "ZSTD_noCompressBlock failed");
0813 assert(cSize != 0);
0814 op += cSize;
0815
0816 if (sp < send) {
0817 seqDef const* seq;
0818 repcodes_t rep;
0819 ZSTD_memcpy(&rep, prevCBlock->rep, sizeof(rep));
0820 for (seq = sstart; seq < sp; ++seq) {
0821 rep = ZSTD_updateRep(rep.rep, seq->offset - 1, ZSTD_getSequenceLength(seqStorePtr, seq).litLength == 0);
0822 }
0823 ZSTD_memcpy(nextCBlock->rep, &rep, sizeof(rep));
0824 }
0825 }
0826 DEBUGLOG(5, "ZSTD_compressSubBlock_multi compressed");
0827 return op-ostart;
0828 }
0829
0830 size_t ZSTD_compressSuperBlock(ZSTD_CCtx* zc,
0831 void* dst, size_t dstCapacity,
0832 void const* src, size_t srcSize,
0833 unsigned lastBlock) {
0834 ZSTD_entropyCTablesMetadata_t entropyMetadata;
0835
0836 FORWARD_IF_ERROR(ZSTD_buildSuperBlockEntropy(&zc->seqStore,
0837 &zc->blockState.prevCBlock->entropy,
0838 &zc->blockState.nextCBlock->entropy,
0839 &zc->appliedParams,
0840 &entropyMetadata,
0841 zc->entropyWorkspace, ENTROPY_WORKSPACE_SIZE ), "");
0842
0843 return ZSTD_compressSubBlock_multi(&zc->seqStore,
0844 zc->blockState.prevCBlock,
0845 zc->blockState.nextCBlock,
0846 &entropyMetadata,
0847 &zc->appliedParams,
0848 dst, dstCapacity,
0849 src, srcSize,
0850 zc->bmi2, lastBlock,
0851 zc->entropyWorkspace, ENTROPY_WORKSPACE_SIZE );
0852 }