2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
17 #include <time.h> /* time(), for seed random initialization */
20 #include "timefn.h" /* UTIL_clockSpanMicro, SEC_TO_MICRO, UTIL_TIME_INITIALIZER */
22 #include "zstd_internal.h"
24 #define ZDICT_STATIC_LINKING_ONLY
27 /* Direct access to internal compression functions is required */
28 #include "compress/zstd_compress.c" /* ZSTD_resetSeqStore, ZSTD_storeSeq, *_TO_OFFBASE, HIST_countFast_wksp, HIST_isError */
29 #include "decompress/zstd_decompress_block.h" /* ZSTD_decompressBlock_deprecated */
31 #define XXH_STATIC_LINKING_ONLY
32 #include "xxhash.h" /* XXH64 */
34 #if !(defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */))
35 # define inline /* disable */
38 /*-************************************
40 **************************************/
41 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
42 #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
43 static U32 g_displayLevel = 2;
45 #define DISPLAYUPDATE(...) \
47 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || \
48 (g_displayLevel >= 4)) { \
49 g_displayClock = UTIL_getTime(); \
50 DISPLAY(__VA_ARGS__); \
51 if (g_displayLevel >= 4) fflush(stderr); \
55 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
56 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
58 #define CHECKERR(code) \
60 if (ZSTD_isError(code)) { \
61 DISPLAY("Error occurred while generating data: %s\n", \
62 ZSTD_getErrorName(code)); \
68 /*-*******************************************************
70 *********************************************************/
71 static U32 RAND(U32* src)
73 #define RAND_rotl32(x,r) ((x << r) | (x >> (32 - r)))
74 static const U32 prime1 = 2654435761U;
75 static const U32 prime2 = 2246822519U;
79 rand32 = RAND_rotl32(rand32, 13);
81 return RAND_rotl32(rand32, 27);
85 #define DISTSIZE (8192)
87 /* Write `size` bytes into `ptr`, all of which are less than or equal to `maxSymb` */
88 static void RAND_bufferMaxSymb(U32* seed, void* ptr, size_t size, int maxSymb)
93 for (i = 0; i < size; i++) {
94 op[i] = (BYTE) (RAND(seed) % (maxSymb + 1));
98 /* Write `size` random bytes into `ptr` */
99 static void RAND_buffer(U32* seed, void* ptr, size_t size)
104 for (i = 0; i + 4 <= size; i += 4) {
105 MEM_writeLE32(op + i, RAND(seed));
107 for (; i < size; i++) {
108 op[i] = RAND(seed) & 0xff;
112 /* Write `size` bytes into `ptr` following the distribution `dist` */
113 static void RAND_bufferDist(U32* seed, BYTE* dist, void* ptr, size_t size)
118 for (i = 0; i < size; i++) {
119 op[i] = dist[RAND(seed) % DISTSIZE];
123 /* Generate a random distribution where the frequency of each symbol follows a
124 * geometric distribution defined by `weight`
125 * `dist` should have size at least `DISTSIZE` */
126 static void RAND_genDist(U32* seed, BYTE* dist, double weight)
129 size_t statesLeft = DISTSIZE;
130 BYTE symb = (BYTE) (RAND(seed) % 256);
131 BYTE step = (BYTE) ((RAND(seed) % 256) | 1); /* force it to be odd so it's relatively prime to 256 */
133 while (i < DISTSIZE) {
134 size_t states = ((size_t)(weight * (double)statesLeft)) + 1;
136 for (j = 0; j < states && i < DISTSIZE; j++, i++) {
141 statesLeft -= states;
145 /* Generates a random number in the range [min, max) */
146 static inline U32 RAND_range(U32* seed, U32 min, U32 max)
148 return (RAND(seed) % (max-min)) + min;
151 #define ROUND(x) ((U32)(x + 0.5))
153 /* Generates a random number in an exponential distribution with mean `mean` */
154 static double RAND_exp(U32* seed, double mean)
156 double const u = RAND(seed) / (double) UINT_MAX;
157 return log(1-u) * (-mean);
160 /*-*******************************************************
161 * Constants and Structs
162 *********************************************************/
163 const char* BLOCK_TYPES[] = {"raw", "rle", "compressed"};
165 #define MAX_DECOMPRESSED_SIZE_LOG 20
166 #define MAX_DECOMPRESSED_SIZE (1ULL << MAX_DECOMPRESSED_SIZE_LOG)
168 #define MAX_WINDOW_LOG 22 /* Recommended support is 8MB, so limit to 4MB + mantissa */
170 #define MIN_SEQ_LEN (3)
171 #define MAX_NB_SEQ ((ZSTD_BLOCKSIZE_MAX + MIN_SEQ_LEN - 1) / MIN_SEQ_LEN)
175 #define MAX_PATH PATH_MAX
181 BYTE CONTENT_BUFFER[MAX_DECOMPRESSED_SIZE];
182 BYTE FRAME_BUFFER[MAX_DECOMPRESSED_SIZE * 2];
183 BYTE LITERAL_BUFFER[ZSTD_BLOCKSIZE_MAX];
185 seqDef SEQUENCE_BUFFER[MAX_NB_SEQ];
186 BYTE SEQUENCE_LITERAL_BUFFER[ZSTD_BLOCKSIZE_MAX]; /* storeSeq expects a place to copy literals to */
187 BYTE SEQUENCE_LLCODE[ZSTD_BLOCKSIZE_MAX];
188 BYTE SEQUENCE_MLCODE[ZSTD_BLOCKSIZE_MAX];
189 BYTE SEQUENCE_OFCODE[ZSTD_BLOCKSIZE_MAX];
191 U64 WKSP[HUF_WORKSPACE_SIZE_U64];
194 size_t contentSize; /* 0 means unknown (unless contentSize == windowSize == 0) */
195 unsigned windowSize; /* contentSize >= windowSize means single segment */
198 /* For repeat modes */
200 U32 rep[ZSTD_REP_NUM];
203 /* the distribution used in the previous block for repeat mode */
204 BYTE hufDist[DISTSIZE];
205 HUF_CElt hufTable [HUF_CTABLE_SIZE_ST(255)];
208 FSE_CTable offcodeCTable [FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
209 FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
210 FSE_CTable litlengthCTable [FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
212 /* Symbols that were present in the previous distribution, for use with
214 BYTE litlengthSymbolSet[36];
215 BYTE offsetSymbolSet[29];
216 BYTE matchlengthSymbolSet[53];
228 frameHeader_t header;
231 cblockStats_t oldStats; /* so they can be rolled back if uncompressible */
237 size_t dictContentSize;
242 gt_frame = 0, /* generate frames */
243 gt_block, /* generate compressed blocks without block/frame headers */
247 #define MIN(a, b) ((a) < (b) ? (a) : (b))
250 /*-*******************************************************
251 * Global variables (set from command line)
252 *********************************************************/
253 U32 g_maxDecompressedSizeLog = MAX_DECOMPRESSED_SIZE_LOG; /* <= 20 */
254 U32 g_maxBlockSize = ZSTD_BLOCKSIZE_MAX; /* <= 128 KB */
256 /*-*******************************************************
257 * Generator Functions
258 *********************************************************/
261 int contentSize; /* force the content size to be present */
262 } opts; /* advanced options on generation */
264 /* Generate and write a random frame header */
265 static void writeFrameHeader(U32* seed, frame_t* frame, dictInfo info)
267 BYTE* const op = frame->data;
273 int singleSegment = 0;
274 int contentSizeFlag = 0;
277 memset(&fh, 0, sizeof(fh));
279 /* generate window size */
281 /* Follow window algorithm from specification */
282 int const exponent = RAND(seed) % (MAX_WINDOW_LOG - 10);
283 int const mantissa = RAND(seed) % 8;
284 windowByte = (BYTE) ((exponent << 3) | mantissa);
285 fh.windowSize = (1U << (exponent + 10));
286 fh.windowSize += fh.windowSize / 8 * mantissa;
290 /* Generate random content size */
292 if (RAND(seed) & 7 && g_maxDecompressedSizeLog > 7) {
293 /* do content of at least 128 bytes */
294 highBit = 1ULL << RAND_range(seed, 7, g_maxDecompressedSizeLog);
295 } else if (RAND(seed) & 3) {
296 /* do small content */
297 highBit = 1ULL << RAND_range(seed, 0, MIN(7, 1U << g_maxDecompressedSizeLog));
302 fh.contentSize = highBit ? highBit + (RAND(seed) % highBit) : 0;
304 /* provide size sometimes */
305 contentSizeFlag = opts.contentSize | (RAND(seed) & 1);
307 if (contentSizeFlag && (fh.contentSize == 0 || !(RAND(seed) & 7))) {
308 /* do single segment sometimes */
309 fh.windowSize = (U32) fh.contentSize;
314 if (contentSizeFlag) {
315 /* Determine how large fcs field has to be */
316 int minFcsCode = (fh.contentSize >= 256) +
317 (fh.contentSize >= 65536 + 256) +
318 (fh.contentSize > 0xFFFFFFFFU);
319 if (!singleSegment && !minFcsCode) {
322 fcsCode = minFcsCode + (RAND(seed) % (4 - minFcsCode));
323 if (fcsCode == 1 && fh.contentSize < 256) fcsCode++;
326 /* write out the header */
327 MEM_writeLE32(op + pos, ZSTD_MAGICNUMBER);
332 * fcsCode: 2-bit flag specifying how many bytes used to represent Frame_Content_Size (bits 7-6)
333 * singleSegment: 1-bit flag describing if data must be regenerated within a single continuous memory segment. (bit 5)
334 * contentChecksumFlag: 1-bit flag that is set if frame includes checksum at the end -- set to 1 below (bit 2)
335 * dictBits: 2-bit flag describing how many bytes Dictionary_ID uses -- set to 3 (bits 1-0)
336 * For more information: https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#frame_header
338 int const dictBits = info.useDict ? 3 : 0;
339 BYTE const frameHeaderDescriptor =
340 (BYTE) ((fcsCode << 6) | (singleSegment << 5) | (1 << 2) | dictBits);
341 op[pos++] = frameHeaderDescriptor;
344 if (!singleSegment) {
345 op[pos++] = windowByte;
348 MEM_writeLE32(op + pos, (U32) info.dictID);
351 if (contentSizeFlag) {
353 default: /* Impossible */
354 case 0: op[pos++] = (BYTE) fh.contentSize; break;
355 case 1: MEM_writeLE16(op + pos, (U16) (fh.contentSize - 256)); pos += 2; break;
356 case 2: MEM_writeLE32(op + pos, (U32) fh.contentSize); pos += 4; break;
357 case 3: MEM_writeLE64(op + pos, (U64) fh.contentSize); pos += 8; break;
361 DISPLAYLEVEL(3, " frame content size:\t%u\n", (unsigned)fh.contentSize);
362 DISPLAYLEVEL(3, " frame window size:\t%u\n", fh.windowSize);
363 DISPLAYLEVEL(3, " content size flag:\t%d\n", contentSizeFlag);
364 DISPLAYLEVEL(3, " single segment flag:\t%d\n", singleSegment);
366 frame->data = op + pos;
370 /* Write a literal block in either raw or RLE form, return the literals size */
371 static size_t writeLiteralsBlockSimple(U32* seed, frame_t* frame, size_t contentSize)
373 BYTE* op = (BYTE*)frame->data;
374 int const type = RAND(seed) % 2;
375 int const sizeFormatDesc = RAND(seed) % 8;
377 size_t maxLitSize = MIN(contentSize, g_maxBlockSize);
379 if (sizeFormatDesc == 0) {
380 /* Size_FormatDesc = ?0 */
381 maxLitSize = MIN(maxLitSize, 31);
382 } else if (sizeFormatDesc <= 4) {
383 /* Size_FormatDesc = 01 */
384 maxLitSize = MIN(maxLitSize, 4095);
386 /* Size_Format = 11 */
387 maxLitSize = MIN(maxLitSize, 1048575);
390 litSize = RAND(seed) % (maxLitSize + 1);
391 if (frame->src == frame->srcStart && litSize == 0) {
392 litSize = 1; /* no empty literals if there's nothing preceding this block */
394 if (litSize + 3 > contentSize) {
395 litSize = contentSize; /* no matches shorter than 3 are allowed */
397 /* use smallest size format that fits */
399 op[0] = (type | (0 << 2) | (litSize << 3)) & 0xff;
401 } else if (litSize < 4096) {
402 op[0] = (type | (1 << 2) | (litSize << 4)) & 0xff;
403 op[1] = (litSize >> 4) & 0xff;
406 op[0] = (type | (3 << 2) | (litSize << 4)) & 0xff;
407 op[1] = (litSize >> 4) & 0xff;
408 op[2] = (litSize >> 12) & 0xff;
414 DISPLAYLEVEL(4, " raw literals\n");
416 RAND_buffer(seed, LITERAL_BUFFER, litSize);
417 memcpy(op, LITERAL_BUFFER, litSize);
421 BYTE const symb = (BYTE) (RAND(seed) % 256);
423 DISPLAYLEVEL(4, " rle literals: 0x%02x\n", (unsigned)symb);
425 memset(LITERAL_BUFFER, symb, litSize);
435 /* Generate a Huffman header for the given source */
436 static size_t writeHufHeader(U32* seed, HUF_CElt* hufTable, void* dst, size_t dstSize,
437 const void* src, size_t srcSize)
439 BYTE* const ostart = (BYTE*)dst;
442 unsigned huffLog = 11;
443 unsigned maxSymbolValue = 255;
445 unsigned count[HUF_SYMBOLVALUE_MAX+1];
447 /* Scan input and build symbol stats */
448 { size_t const largest = HIST_count_wksp (count, &maxSymbolValue, (const BYTE*)src, srcSize, WKSP, sizeof(WKSP));
449 assert(!HIST_isError(largest));
450 if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 0; } /* single symbol, rle */
451 if (largest <= (srcSize >> 7)+1) return 0; /* Fast heuristic : not compressible enough */
454 /* Build Huffman Tree */
455 /* Max Huffman log is 11, min is highbit(maxSymbolValue)+1 */
456 huffLog = RAND_range(seed, ZSTD_highbit32(maxSymbolValue)+1, huffLog+1);
457 DISPLAYLEVEL(6, " huffman log: %u\n", huffLog);
458 { size_t const maxBits = HUF_buildCTable_wksp (hufTable, count, maxSymbolValue, huffLog, WKSP, sizeof(WKSP));
460 huffLog = (U32)maxBits;
463 /* Write table description header */
464 { size_t const hSize = HUF_writeCTable_wksp (op, dstSize, hufTable, maxSymbolValue, huffLog, WKSP, sizeof(WKSP));
465 if (hSize + 12 >= srcSize) return 0; /* not useful to try compression */
472 /* Write a Huffman coded literals block and return the literals size */
473 static size_t writeLiteralsBlockCompressed(U32* seed, frame_t* frame, size_t contentSize)
475 BYTE* origop = (BYTE*)frame->data;
476 BYTE* opend = (BYTE*)frame->dataEnd;
478 BYTE* const ostart = origop;
479 int const sizeFormat = RAND(seed) % 4;
481 size_t hufHeaderSize = 0;
482 size_t compressedSize = 0;
483 size_t maxLitSize = MIN(contentSize-3, g_maxBlockSize);
485 symbolEncodingType_e hType;
487 if (contentSize < 64) {
488 /* make sure we get reasonably-sized literals for compression */
489 return ERROR(GENERIC);
492 DISPLAYLEVEL(4, " compressed literals\n");
494 switch (sizeFormat) {
495 case 0: /* fall through, size is the same as case 1 */
497 maxLitSize = MIN(maxLitSize, 1023);
501 maxLitSize = MIN(maxLitSize, 16383);
505 maxLitSize = MIN(maxLitSize, 262143);
508 default:; /* impossible */
514 litSize = RAND(seed) % (maxLitSize + 1);
515 } while (litSize < 32); /* avoid small literal sizes */
516 if (litSize + 3 > contentSize) {
517 litSize = contentSize; /* no matches shorter than 3 are allowed */
520 /* most of the time generate a new distribution */
521 if ((RAND(seed) & 3) || !frame->stats.hufInit) {
523 if (RAND(seed) & 3) {
524 /* add 10 to ensure some compressibility */
525 double const weight = ((RAND(seed) % 90) + 10) / 100.0;
527 DISPLAYLEVEL(5, " distribution weight: %d%%\n",
528 (int)(weight * 100));
530 RAND_genDist(seed, frame->stats.hufDist, weight);
532 /* sometimes do restricted range literals to force
533 * non-huffman headers */
534 DISPLAYLEVEL(5, " small range literals\n");
535 RAND_bufferMaxSymb(seed, frame->stats.hufDist, DISTSIZE,
538 RAND_bufferDist(seed, frame->stats.hufDist, LITERAL_BUFFER,
541 /* generate the header from the distribution instead of the
542 * actual data to avoid bugs with symbols that were in the
543 * distribution but never showed up in the output */
544 hufHeaderSize = writeHufHeader(
545 seed, frame->stats.hufTable, op, opend - op,
546 frame->stats.hufDist, DISTSIZE);
547 CHECKERR(hufHeaderSize);
548 /* repeat until a valid header is written */
549 } while (hufHeaderSize == 0);
551 hType = set_compressed;
553 frame->stats.hufInit = 1;
555 /* repeat the distribution/table from last time */
556 DISPLAYLEVEL(5, " huffman repeat stats\n");
557 RAND_bufferDist(seed, frame->stats.hufDist, LITERAL_BUFFER,
566 ? HUF_compress1X_usingCTable(
567 op, opend - op, LITERAL_BUFFER, litSize,
568 frame->stats.hufTable, /* flags */ 0)
569 : HUF_compress4X_usingCTable(
570 op, opend - op, LITERAL_BUFFER, litSize,
571 frame->stats.hufTable, /* flags */ 0);
572 CHECKERR(compressedSize);
573 /* this only occurs when it could not compress or similar */
574 } while (compressedSize <= 0);
576 op += compressedSize;
578 compressedSize += hufHeaderSize;
579 DISPLAYLEVEL(5, " regenerated size: %u\n", (unsigned)litSize);
580 DISPLAYLEVEL(5, " compressed size: %u\n", (unsigned)compressedSize);
581 if (compressedSize >= litSize) {
582 DISPLAYLEVEL(5, " trying again\n");
583 /* if we have to try again, reset the stats so we don't accidentally
584 * try to repeat a distribution we just made */
585 frame->stats = frame->oldStats;
592 switch (sizeFormat) {
593 case 0: /* fall through, size is the same as case 1 */
595 U32 const header = hType | (sizeFormat << 2) | ((U32)litSize << 4) |
596 ((U32)compressedSize << 14);
597 MEM_writeLE24(ostart, header);
601 U32 const header = hType | (sizeFormat << 2) | ((U32)litSize << 4) |
602 ((U32)compressedSize << 18);
603 MEM_writeLE32(ostart, header);
607 U32 const header = hType | (sizeFormat << 2) | ((U32)litSize << 4) |
608 ((U32)compressedSize << 22);
609 MEM_writeLE32(ostart, header);
610 ostart[4] = (BYTE)(compressedSize >> 10);
613 default:; /* impossible */
620 static size_t writeLiteralsBlock(U32* seed, frame_t* frame, size_t contentSize)
622 /* only do compressed for larger segments to avoid compressibility issues */
623 if (RAND(seed) & 7 && contentSize >= 64) {
624 return writeLiteralsBlockCompressed(seed, frame, contentSize);
626 return writeLiteralsBlockSimple(seed, frame, contentSize);
630 static inline void initSeqStore(seqStore_t *seqStore) {
631 seqStore->maxNbSeq = MAX_NB_SEQ;
632 seqStore->maxNbLit = ZSTD_BLOCKSIZE_MAX;
633 seqStore->sequencesStart = SEQUENCE_BUFFER;
634 seqStore->litStart = SEQUENCE_LITERAL_BUFFER;
635 seqStore->llCode = SEQUENCE_LLCODE;
636 seqStore->mlCode = SEQUENCE_MLCODE;
637 seqStore->ofCode = SEQUENCE_OFCODE;
639 ZSTD_resetSeqStore(seqStore);
642 /* Randomly generate sequence commands */
644 generateSequences(U32* seed, frame_t* frame, seqStore_t* seqStore,
645 size_t contentSize, size_t literalsSize, dictInfo info)
647 /* The total length of all the matches */
648 size_t const remainingMatch = contentSize - literalsSize;
649 size_t excessMatch = 0;
650 U32 numSequences = 0;
653 const BYTE* literals = LITERAL_BUFFER;
654 BYTE* srcPtr = frame->src;
656 if (literalsSize != contentSize) {
657 /* each match must be at least MIN_SEQ_LEN, so this is the maximum
658 * number of sequences we can have */
659 U32 const maxSequences = (U32)remainingMatch / MIN_SEQ_LEN;
660 numSequences = (RAND(seed) % maxSequences) + 1;
662 /* the extra match lengths we have to allocate to each sequence */
663 excessMatch = remainingMatch - numSequences * MIN_SEQ_LEN;
666 DISPLAYLEVEL(5, " total match lengths: %u\n", (unsigned)remainingMatch);
667 for (i = 0; i < numSequences; i++) {
668 /* Generate match and literal lengths by exponential distribution to
669 * ensure nice numbers */
672 ROUND(RAND_exp(seed, (double)excessMatch / (double)(numSequences - i)));
675 ? ROUND(RAND_exp(seed,
676 (double)literalsSize /
677 (double)(numSequences - i)))
679 /* actual offset, code to send, and point to copy up to when shifting
680 * codes in the repeat offsets history */
681 U32 offset, offBase, repIndex;
684 matchLen = (U32) MIN(matchLen, excessMatch + MIN_SEQ_LEN);
685 literalLen = MIN(literalLen, (U32) literalsSize);
686 if (i == 0 && srcPtr == frame->srcStart && literalLen == 0) literalLen = 1;
687 if (i + 1 == numSequences) matchLen = MIN_SEQ_LEN + (U32) excessMatch;
689 memcpy(srcPtr, literals, literalLen);
690 srcPtr += literalLen;
692 if (RAND(seed) & 7) {
693 /* do a normal offset */
694 U32 const dataDecompressed = (U32)((BYTE*)srcPtr-(BYTE*)frame->srcStart);
695 offset = (RAND(seed) %
696 MIN(frame->header.windowSize,
697 (size_t)((BYTE*)srcPtr - (BYTE*)frame->srcStart))) +
699 if (info.useDict && (RAND(seed) & 1) && i + 1 != numSequences && dataDecompressed < frame->header.windowSize) {
700 /* need to occasionally generate offsets that go past the start */
701 /* including i+1 != numSequences because the last sequences has to adhere to predetermined contentSize */
702 U32 lenPastStart = (RAND(seed) % info.dictContentSize) + 1;
703 offset = (U32)((BYTE*)srcPtr - (BYTE*)frame->srcStart)+lenPastStart;
704 if (offset > frame->header.windowSize) {
705 if (lenPastStart < MIN_SEQ_LEN) {
706 /* when offset > windowSize, matchLen bound by end of dictionary (lenPastStart) */
707 /* this also means that lenPastStart must be greater than MIN_SEQ_LEN */
708 /* make sure lenPastStart does not go past dictionary start though */
709 lenPastStart = MIN(lenPastStart+MIN_SEQ_LEN, (U32)info.dictContentSize);
710 offset = (U32)((BYTE*)srcPtr - (BYTE*)frame->srcStart) + lenPastStart;
712 { U32 const matchLenBound = MIN(frame->header.windowSize, lenPastStart);
713 matchLen = MIN(matchLen, matchLenBound);
717 offBase = OFFSET_TO_OFFBASE(offset);
720 /* do a repeat offset */
721 U32 const randomRepIndex = RAND(seed) % 3;
722 offBase = REPCODE_TO_OFFBASE(randomRepIndex + 1); /* expects values between 1 & 3 */
723 if (literalLen > 0) {
724 offset = frame->stats.rep[randomRepIndex];
725 repIndex = randomRepIndex;
727 /* special case : literalLen == 0 */
728 offset = randomRepIndex == 2 ? frame->stats.rep[0] - 1
729 : frame->stats.rep[randomRepIndex + 1];
730 repIndex = MIN(2, randomRepIndex + 1);
733 } while (((!info.useDict) && (offset > (size_t)((BYTE*)srcPtr - (BYTE*)frame->srcStart))) || offset == 0);
735 { BYTE* const dictEnd = info.dictContent + info.dictContentSize;
737 for (j = 0; j < matchLen; j++) {
738 if ((U32)((BYTE*)srcPtr - (BYTE*)frame->srcStart) < offset) {
739 /* copy from dictionary instead of literals */
740 size_t const dictOffset = offset - (srcPtr - (BYTE*)frame->srcStart);
741 *srcPtr = *(dictEnd - dictOffset);
744 *srcPtr = *(srcPtr-offset);
750 for (r = repIndex; r > 0; r--) {
751 frame->stats.rep[r] = frame->stats.rep[r - 1];
753 frame->stats.rep[0] = offset;
756 DISPLAYLEVEL(6, " LL: %5u OF: %5u ML: %5u",
757 (unsigned)literalLen, (unsigned)offset, (unsigned)matchLen);
758 DISPLAYLEVEL(7, " srcPos: %8u seqNb: %3u",
759 (unsigned)((BYTE*)srcPtr - (BYTE*)frame->srcStart), (unsigned)i);
760 DISPLAYLEVEL(6, "\n");
761 if (OFFBASE_IS_REPCODE(offBase)) { /* expects sumtype numeric representation of ZSTD_storeSeq() */
762 DISPLAYLEVEL(7, " repeat offset: %d\n", (int)repIndex);
764 /* use libzstd sequence handling */
765 ZSTD_storeSeq(seqStore, literalLen, literals, literals + literalLen,
768 literalsSize -= literalLen;
769 excessMatch -= (matchLen - MIN_SEQ_LEN);
770 literals += literalLen;
773 memcpy(srcPtr, literals, literalsSize);
774 srcPtr += literalsSize;
775 DISPLAYLEVEL(6, " excess literals: %5u ", (unsigned)literalsSize);
776 DISPLAYLEVEL(7, "srcPos: %8u ", (unsigned)((BYTE*)srcPtr - (BYTE*)frame->srcStart));
777 DISPLAYLEVEL(6, "\n");
782 static void initSymbolSet(const BYTE* symbols, size_t len, BYTE* set, BYTE maxSymbolValue)
786 memset(set, 0, (size_t)maxSymbolValue+1);
788 for (i = 0; i < len; i++) {
793 static int isSymbolSubset(const BYTE* symbols, size_t len, const BYTE* set, BYTE maxSymbolValue)
797 for (i = 0; i < len; i++) {
798 if (symbols[i] > maxSymbolValue || !set[symbols[i]]) {
805 static size_t writeSequences(U32* seed, frame_t* frame, seqStore_t* seqStorePtr,
808 /* This code is mostly copied from ZSTD_compressSequences in zstd_compress.c */
809 unsigned count[MaxSeq+1];
811 FSE_CTable* CTable_LitLength = frame->stats.litlengthCTable;
812 FSE_CTable* CTable_OffsetBits = frame->stats.offcodeCTable;
813 FSE_CTable* CTable_MatchLength = frame->stats.matchlengthCTable;
814 U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */
815 const seqDef* const sequences = seqStorePtr->sequencesStart;
816 const BYTE* const ofCodeTable = seqStorePtr->ofCode;
817 const BYTE* const llCodeTable = seqStorePtr->llCode;
818 const BYTE* const mlCodeTable = seqStorePtr->mlCode;
819 BYTE* const oend = (BYTE*)frame->dataEnd;
820 BYTE* op = (BYTE*)frame->data;
822 BYTE scratchBuffer[FSE_BUILD_CTABLE_WORKSPACE_SIZE(MaxSeq, MaxFSELog)];
824 /* literals compressing block removed so that can be done separately */
826 /* Sequences Header */
827 if ((oend-op) < 3 /*max nbSeq Size*/ + 1 /*seqHead */) return ERROR(dstSize_tooSmall);
828 if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq;
829 else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
830 else op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3;
837 /* seqHead : flags for FSE encoding type */
840 /* convert length/distances into codes */
841 ZSTD_seqToCodes(seqStorePtr);
843 /* CTable for Literal Lengths */
844 { unsigned max = MaxLL;
845 size_t const mostFrequent = HIST_countFast_wksp(count, &max, llCodeTable, nbSeq, WKSP, sizeof(WKSP)); /* cannot fail */
846 assert(!HIST_isError(mostFrequent));
847 if (frame->stats.fseInit && !(RAND(seed) & 3) &&
848 isSymbolSubset(llCodeTable, nbSeq,
849 frame->stats.litlengthSymbolSet, 35)) {
850 /* maybe do repeat mode if we're allowed to */
852 } else if (mostFrequent == nbSeq) {
853 /* do RLE if we have the chance */
854 *op++ = llCodeTable[0];
855 FSE_buildCTable_rle(CTable_LitLength, (BYTE)max);
857 } else if (!(RAND(seed) & 3)) {
858 /* maybe use the default distribution */
859 CHECKERR(FSE_buildCTable_wksp(CTable_LitLength, LL_defaultNorm, MaxLL, LL_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)));
862 /* fall back on a full table */
863 size_t nbSeq_1 = nbSeq;
864 const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max);
865 if (count[llCodeTable[nbSeq-1]]>1) { count[llCodeTable[nbSeq-1]]--; nbSeq_1--; }
866 FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max, nbSeq >= 2048);
867 { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
868 if (FSE_isError(NCountSize)) return ERROR(GENERIC);
870 CHECKERR(FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)));
871 LLtype = set_compressed;
874 /* CTable for Offsets */
875 /* see Literal Lengths for descriptions of mode choices */
876 { unsigned max = MaxOff;
877 size_t const mostFrequent = HIST_countFast_wksp(count, &max, ofCodeTable, nbSeq, WKSP, sizeof(WKSP)); /* cannot fail */
878 assert(!HIST_isError(mostFrequent));
879 if (frame->stats.fseInit && !(RAND(seed) & 3) &&
880 isSymbolSubset(ofCodeTable, nbSeq,
881 frame->stats.offsetSymbolSet, 28)) {
882 Offtype = set_repeat;
883 } else if (mostFrequent == nbSeq) {
884 *op++ = ofCodeTable[0];
885 FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
887 } else if (!(RAND(seed) & 3)) {
888 FSE_buildCTable_wksp(CTable_OffsetBits, OF_defaultNorm, DefaultMaxOff, OF_defaultNormLog, scratchBuffer, sizeof(scratchBuffer));
891 size_t nbSeq_1 = nbSeq;
892 const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max);
893 if (count[ofCodeTable[nbSeq-1]]>1) { count[ofCodeTable[nbSeq-1]]--; nbSeq_1--; }
894 FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max, nbSeq >= 2048);
895 { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
896 if (FSE_isError(NCountSize)) return ERROR(GENERIC);
898 FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer));
899 Offtype = set_compressed;
902 /* CTable for MatchLengths */
903 /* see Literal Lengths for descriptions of mode choices */
904 { unsigned max = MaxML;
905 size_t const mostFrequent = HIST_countFast_wksp(count, &max, mlCodeTable, nbSeq, WKSP, sizeof(WKSP)); /* cannot fail */
906 assert(!HIST_isError(mostFrequent));
907 if (frame->stats.fseInit && !(RAND(seed) & 3) &&
908 isSymbolSubset(mlCodeTable, nbSeq,
909 frame->stats.matchlengthSymbolSet, 52)) {
911 } else if (mostFrequent == nbSeq) {
912 *op++ = *mlCodeTable;
913 FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
915 } else if (!(RAND(seed) & 3)) {
916 /* sometimes do default distribution */
917 FSE_buildCTable_wksp(CTable_MatchLength, ML_defaultNorm, MaxML, ML_defaultNormLog, scratchBuffer, sizeof(scratchBuffer));
920 /* fall back on table */
921 size_t nbSeq_1 = nbSeq;
922 const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max);
923 if (count[mlCodeTable[nbSeq-1]]>1) { count[mlCodeTable[nbSeq-1]]--; nbSeq_1--; }
924 FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max, nbSeq >= 2048);
925 { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */
926 if (FSE_isError(NCountSize)) return ERROR(GENERIC);
928 FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer));
929 MLtype = set_compressed;
931 frame->stats.fseInit = 1;
932 initSymbolSet(llCodeTable, nbSeq, frame->stats.litlengthSymbolSet, 35);
933 initSymbolSet(ofCodeTable, nbSeq, frame->stats.offsetSymbolSet, 28);
934 initSymbolSet(mlCodeTable, nbSeq, frame->stats.matchlengthSymbolSet, 52);
936 DISPLAYLEVEL(5, " LL type: %d OF type: %d ML type: %d\n", (unsigned)LLtype, (unsigned)Offtype, (unsigned)MLtype);
938 *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
940 /* Encoding Sequences */
941 { BIT_CStream_t blockStream;
942 FSE_CState_t stateMatchLength;
943 FSE_CState_t stateOffsetBits;
944 FSE_CState_t stateLitLength;
947 ERR_isError(BIT_initCStream(&blockStream, op, oend-op)),
948 dstSize_tooSmall, "not enough space remaining");
951 FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]);
952 FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]);
953 FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]);
954 BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]);
955 if (MEM_32bits()) BIT_flushBits(&blockStream);
956 BIT_addBits(&blockStream, sequences[nbSeq-1].mlBase, ML_bits[mlCodeTable[nbSeq-1]]);
957 if (MEM_32bits()) BIT_flushBits(&blockStream);
958 BIT_addBits(&blockStream, sequences[nbSeq-1].offBase, ofCodeTable[nbSeq-1]);
959 BIT_flushBits(&blockStream);
962 for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */
963 BYTE const llCode = llCodeTable[n];
964 BYTE const ofCode = ofCodeTable[n];
965 BYTE const mlCode = mlCodeTable[n];
966 U32 const llBits = LL_bits[llCode];
967 U32 const ofBits = ofCode; /* 32b*/ /* 64b*/
968 U32 const mlBits = ML_bits[mlCode];
970 FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */
971 FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */
972 if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/
973 FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */
974 if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog)))
975 BIT_flushBits(&blockStream); /* (7)*/
976 BIT_addBits(&blockStream, sequences[n].litLength, llBits);
977 if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream);
978 BIT_addBits(&blockStream, sequences[n].mlBase, mlBits);
979 if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/
980 BIT_addBits(&blockStream, sequences[n].offBase, ofBits); /* 31 */
981 BIT_flushBits(&blockStream); /* (7)*/
984 FSE_flushCState(&blockStream, &stateMatchLength);
985 FSE_flushCState(&blockStream, &stateOffsetBits);
986 FSE_flushCState(&blockStream, &stateLitLength);
988 { size_t const streamSize = BIT_closeCStream(&blockStream);
989 if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */
998 static size_t writeSequencesBlock(U32* seed, frame_t* frame, size_t contentSize,
999 size_t literalsSize, dictInfo info)
1001 seqStore_t seqStore;
1002 size_t numSequences;
1005 initSeqStore(&seqStore);
1007 /* randomly generate sequences */
1008 numSequences = generateSequences(seed, frame, &seqStore, contentSize, literalsSize, info);
1009 /* write them out to the frame data */
1010 CHECKERR(writeSequences(seed, frame, &seqStore, numSequences));
1012 return numSequences;
1015 static size_t writeCompressedBlock(U32* seed, frame_t* frame, size_t contentSize, dictInfo info)
1017 BYTE* const blockStart = (BYTE*)frame->data;
1018 size_t literalsSize;
1021 DISPLAYLEVEL(4, " compressed block:\n");
1023 literalsSize = writeLiteralsBlock(seed, frame, contentSize);
1025 DISPLAYLEVEL(4, " literals size: %u\n", (unsigned)literalsSize);
1027 nbSeq = writeSequencesBlock(seed, frame, contentSize, literalsSize, info);
1029 DISPLAYLEVEL(4, " number of sequences: %u\n", (unsigned)nbSeq);
1031 return (BYTE*)frame->data - blockStart;
1034 static void writeBlock(U32* seed, frame_t* frame, size_t contentSize,
1035 int lastBlock, dictInfo info)
1037 int const blockTypeDesc = RAND(seed) % 8;
1041 BYTE *const header = (BYTE*)frame->data;
1042 BYTE *op = header + 3;
1044 DISPLAYLEVEL(4, " block:\n");
1045 DISPLAYLEVEL(4, " block content size: %u\n", (unsigned)contentSize);
1046 DISPLAYLEVEL(4, " last block: %s\n", lastBlock ? "yes" : "no");
1048 if (blockTypeDesc == 0) {
1049 /* Raw data frame */
1051 RAND_buffer(seed, frame->src, contentSize);
1052 memcpy(op, frame->src, contentSize);
1056 blockSize = contentSize;
1057 } else if (blockTypeDesc == 1 && frame->header.contentSize > 0) {
1058 /* RLE (Don't create RLE block if frame content is 0 since block size of 1 may exceed max block size)*/
1059 BYTE const symbol = RAND(seed) & 0xff;
1062 memset(frame->src, symbol, contentSize);
1066 blockSize = contentSize;
1068 /* compressed, most common */
1069 size_t compressedSize;
1072 frame->oldStats = frame->stats;
1075 compressedSize = writeCompressedBlock(seed, frame, contentSize, info);
1076 if (compressedSize >= contentSize) { /* compressed block must be strictly smaller than uncompressed one */
1078 memcpy(op, frame->src, contentSize);
1081 blockSize = contentSize; /* fall back on raw block if data doesn't
1084 frame->stats = frame->oldStats; /* don't update the stats */
1086 op += compressedSize;
1087 blockSize = compressedSize;
1090 frame->src = (BYTE*)frame->src + contentSize;
1092 DISPLAYLEVEL(4, " block type: %s\n", BLOCK_TYPES[blockType]);
1093 DISPLAYLEVEL(4, " block size field: %u\n", (unsigned)blockSize);
1095 header[0] = (BYTE) ((lastBlock | (blockType << 1) | (blockSize << 3)) & 0xff);
1096 MEM_writeLE16(header + 1, (U16) (blockSize >> 5));
1101 static void writeBlocks(U32* seed, frame_t* frame, dictInfo info)
1103 size_t contentLeft = frame->header.contentSize;
1104 size_t const maxBlockSize = MIN(g_maxBlockSize, frame->header.windowSize);
1106 /* 1 in 4 chance of ending frame */
1107 int const lastBlock = contentLeft > maxBlockSize ? 0 : !(RAND(seed) & 3);
1108 size_t blockContentSize;
1110 blockContentSize = contentLeft;
1112 if (contentLeft > 0 && (RAND(seed) & 7)) {
1113 /* some variable size block */
1114 blockContentSize = RAND(seed) % (MIN(maxBlockSize, contentLeft)+1);
1115 } else if (contentLeft > maxBlockSize && (RAND(seed) & 1)) {
1116 /* some full size block */
1117 blockContentSize = maxBlockSize;
1119 /* some empty block */
1120 blockContentSize = 0;
1124 writeBlock(seed, frame, blockContentSize, lastBlock, info);
1126 contentLeft -= blockContentSize;
1127 if (lastBlock) break;
1131 static void writeChecksum(frame_t* frame)
1133 /* write checksum so implementations can verify their output */
1134 U64 digest = XXH64(frame->srcStart, (BYTE*)frame->src-(BYTE*)frame->srcStart, 0);
1135 DISPLAYLEVEL(3, " checksum: %08x\n", (unsigned)digest);
1136 MEM_writeLE32(frame->data, (U32)digest);
1137 frame->data = (BYTE*)frame->data + 4;
1140 static void outputBuffer(const void* buf, size_t size, const char* const path)
1142 /* write data out to file */
1143 const BYTE* ip = (const BYTE*)buf;
1146 out = fopen(path, "wb");
1151 fprintf(stderr, "Failed to open file at %s: ", path);
1156 { size_t fsize = size;
1158 while (written < fsize) {
1159 written += fwrite(ip + written, 1, fsize - written, out);
1161 fprintf(stderr, "Failed to write to file at %s: ", path);
1173 static void initFrame(frame_t* fr)
1175 memset(fr, 0, sizeof(*fr));
1176 fr->data = fr->dataStart = FRAME_BUFFER;
1177 fr->dataEnd = FRAME_BUFFER + sizeof(FRAME_BUFFER);
1178 fr->src = fr->srcStart = CONTENT_BUFFER;
1179 fr->srcEnd = CONTENT_BUFFER + sizeof(CONTENT_BUFFER);
1181 /* init repeat codes */
1182 fr->stats.rep[0] = 1;
1183 fr->stats.rep[1] = 4;
1184 fr->stats.rep[2] = 8;
1188 * Generated a single zstd compressed block with no block/frame header.
1189 * Returns the final seed.
1191 static U32 generateCompressedBlock(U32 seed, frame_t* frame, dictInfo info)
1193 size_t blockContentSize;
1194 int blockWritten = 0;
1196 DISPLAYLEVEL(4, "block seed: %u\n", (unsigned)seed);
1198 op = (BYTE*)frame->data;
1200 while (!blockWritten) {
1202 /* generate window size */
1203 { int const exponent = RAND(&seed) % (MAX_WINDOW_LOG - 10);
1204 int const mantissa = RAND(&seed) % 8;
1205 frame->header.windowSize = (1U << (exponent + 10));
1206 frame->header.windowSize += (frame->header.windowSize / 8) * mantissa;
1209 /* generate content size */
1210 { size_t const maxBlockSize = MIN(g_maxBlockSize, frame->header.windowSize);
1211 if (RAND(&seed) & 15) {
1212 /* some full size blocks */
1213 blockContentSize = maxBlockSize;
1214 } else if (RAND(&seed) & 7 && g_maxBlockSize >= (1U << 7)) {
1215 /* some small blocks <= 128 bytes*/
1216 blockContentSize = RAND(&seed) % (1U << 7);
1218 /* some variable size blocks */
1219 blockContentSize = RAND(&seed) % maxBlockSize;
1223 /* try generating a compressed block */
1224 frame->oldStats = frame->stats;
1226 cSize = writeCompressedBlock(&seed, frame, blockContentSize, info);
1227 if (cSize >= blockContentSize) { /* compressed size must be strictly smaller than decompressed size : https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#blocks */
1228 /* data doesn't compress -- try again */
1229 frame->stats = frame->oldStats; /* don't update the stats */
1230 DISPLAYLEVEL(5, " can't compress block : try again \n");
1233 DISPLAYLEVEL(4, " block size: %u \n", (unsigned)cSize);
1234 frame->src = (BYTE*)frame->src + blockContentSize;
1240 /* Return the final seed */
1241 static U32 generateFrame(U32 seed, frame_t* fr, dictInfo info)
1243 /* generate a complete frame */
1244 DISPLAYLEVEL(3, "frame seed: %u\n", (unsigned)seed);
1247 writeFrameHeader(&seed, fr, info);
1248 writeBlocks(&seed, fr, info);
1254 /*_*******************************************************
1255 * Dictionary Helper Functions
1256 *********************************************************/
1257 /* returns 0 if successful, otherwise returns 1 upon error */
1258 static int genRandomDict(U32 dictID, U32 seed, size_t dictSize, BYTE* fullDict)
1260 /* allocate space for samples */
1262 unsigned const numSamples = 4;
1263 size_t sampleSizes[4];
1264 BYTE* const samples = malloc(5000*sizeof(BYTE));
1265 if (samples == NULL) {
1266 DISPLAY("Error: could not allocate space for samples\n");
1270 /* generate samples */
1271 { unsigned literalValue = 1;
1272 unsigned samplesPos = 0;
1273 size_t currSize = 1;
1274 while (literalValue <= 4) {
1275 sampleSizes[literalValue - 1] = currSize;
1277 for (k = 0; k < currSize; k++) {
1278 *(samples + (samplesPos++)) = (BYTE)literalValue;
1284 { size_t dictWriteSize = 0;
1285 ZDICT_params_t zdictParams;
1286 size_t const headerSize = MAX(dictSize/4, 256);
1287 size_t const dictContentSize = dictSize - headerSize;
1288 BYTE* const dictContent = fullDict + headerSize;
1289 if (dictContentSize < ZDICT_CONTENTSIZE_MIN || dictSize < ZDICT_DICTSIZE_MIN) {
1290 DISPLAY("Error: dictionary size is too small\n");
1292 goto exitGenRandomDict;
1295 /* init dictionary params */
1296 memset(&zdictParams, 0, sizeof(zdictParams));
1297 zdictParams.dictID = dictID;
1298 zdictParams.notificationLevel = 1;
1300 /* fill in dictionary content */
1301 RAND_buffer(&seed, (void*)dictContent, dictContentSize);
1303 /* finalize dictionary with random samples */
1304 dictWriteSize = ZDICT_finalizeDictionary(fullDict, dictSize,
1305 dictContent, dictContentSize,
1306 samples, sampleSizes, numSamples,
1309 if (ZDICT_isError(dictWriteSize)) {
1310 DISPLAY("Could not finalize dictionary: %s\n", ZDICT_getErrorName(dictWriteSize));
1320 static dictInfo initDictInfo(int useDict, size_t dictContentSize, BYTE* dictContent, U32 dictID){
1321 /* allocate space statically */
1323 memset(&dictOp, 0, sizeof(dictOp));
1324 dictOp.useDict = useDict;
1325 dictOp.dictContentSize = dictContentSize;
1326 dictOp.dictContent = dictContent;
1327 dictOp.dictID = dictID;
1331 /*-*******************************************************
1333 *********************************************************/
1335 BYTE DECOMPRESSED_BUFFER[MAX_DECOMPRESSED_SIZE];
1337 static size_t testDecodeSimple(frame_t* fr)
1339 /* test decoding the generated data with the simple API */
1340 size_t const ret = ZSTD_decompress(DECOMPRESSED_BUFFER, MAX_DECOMPRESSED_SIZE,
1341 fr->dataStart, (BYTE*)fr->data - (BYTE*)fr->dataStart);
1343 if (ZSTD_isError(ret)) return ret;
1345 if (memcmp(DECOMPRESSED_BUFFER, fr->srcStart,
1346 (BYTE*)fr->src - (BYTE*)fr->srcStart) != 0) {
1347 return ERROR(corruption_detected);
1353 static size_t testDecodeStreaming(frame_t* fr)
1355 /* test decoding the generated data with the streaming API */
1356 ZSTD_DStream* zd = ZSTD_createDStream();
1361 if (!zd) return ERROR(memory_allocation);
1363 in.src = fr->dataStart;
1365 in.size = (BYTE*)fr->data - (BYTE*)fr->dataStart;
1367 out.dst = DECOMPRESSED_BUFFER;
1369 out.size = ZSTD_DStreamOutSize();
1371 ZSTD_initDStream(zd);
1373 ret = ZSTD_decompressStream(zd, &out, &in);
1374 if (ZSTD_isError(ret)) goto cleanup; /* error */
1375 if (ret == 0) break; /* frame is done */
1377 /* force decoding to be done in chunks */
1378 out.size += MIN(ZSTD_DStreamOutSize(), MAX_DECOMPRESSED_SIZE - out.size);
1383 if (memcmp(out.dst, fr->srcStart, out.pos) != 0) {
1384 return ERROR(corruption_detected);
1388 ZSTD_freeDStream(zd);
1392 static size_t testDecodeWithDict(U32 seed, genType_e genType)
1394 /* create variables */
1395 size_t const dictSize = RAND(&seed) % (10 << 20) + ZDICT_DICTSIZE_MIN + ZDICT_CONTENTSIZE_MIN;
1396 U32 const dictID = RAND(&seed);
1397 size_t errorDetected = 0;
1398 BYTE* const fullDict = malloc(dictSize);
1399 if (fullDict == NULL) {
1400 return ERROR(GENERIC);
1403 /* generate random dictionary */
1404 if (genRandomDict(dictID, seed, dictSize, fullDict)) { /* return 0 on success */
1405 errorDetected = ERROR(GENERIC);
1406 goto dictTestCleanup;
1412 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
1416 { size_t const headerSize = MAX(dictSize/4, 256);
1417 size_t const dictContentSize = dictSize-headerSize;
1418 BYTE* const dictContent = fullDict+headerSize;
1419 info = initDictInfo(1, dictContentSize, dictContent, dictID);
1422 /* manually decompress and check difference */
1423 if (genType == gt_frame) {
1425 generateFrame(seed, &fr, info);
1426 ret = ZSTD_decompress_usingDict(dctx, DECOMPRESSED_BUFFER, MAX_DECOMPRESSED_SIZE,
1427 fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart,
1428 fullDict, dictSize);
1431 generateCompressedBlock(seed, &fr, info);
1432 ret = ZSTD_decompressBegin_usingDict(dctx, fullDict, dictSize);
1433 if (ZSTD_isError(ret)) {
1434 errorDetected = ret;
1435 ZSTD_freeDCtx(dctx);
1436 goto dictTestCleanup;
1438 ret = ZSTD_decompressBlock_deprecated(dctx, DECOMPRESSED_BUFFER, MAX_DECOMPRESSED_SIZE,
1439 fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart);
1441 ZSTD_freeDCtx(dctx);
1443 if (ZSTD_isError(ret)) {
1444 errorDetected = ret;
1445 goto dictTestCleanup;
1448 if (memcmp(DECOMPRESSED_BUFFER, fr.srcStart, (BYTE*)fr.src - (BYTE*)fr.srcStart) != 0) {
1449 errorDetected = ERROR(corruption_detected);
1450 goto dictTestCleanup;
1456 return errorDetected;
1459 static size_t testDecodeRawBlock(frame_t* fr)
1461 ZSTD_DCtx* dctx = ZSTD_createDCtx();
1462 size_t ret = ZSTD_decompressBegin(dctx);
1463 if (ZSTD_isError(ret)) return ret;
1465 ret = ZSTD_decompressBlock_deprecated(
1467 DECOMPRESSED_BUFFER, MAX_DECOMPRESSED_SIZE,
1468 fr->dataStart, (BYTE*)fr->data - (BYTE*)fr->dataStart);
1469 ZSTD_freeDCtx(dctx);
1470 if (ZSTD_isError(ret)) return ret;
1472 if (memcmp(DECOMPRESSED_BUFFER, fr->srcStart,
1473 (BYTE*)fr->src - (BYTE*)fr->srcStart) != 0) {
1474 return ERROR(corruption_detected);
1480 static int runBlockTest(U32* seed)
1483 U32 const seedCopy = *seed;
1484 { dictInfo const info = initDictInfo(0, 0, NULL, 0);
1485 *seed = generateCompressedBlock(*seed, &fr, info);
1488 { size_t const r = testDecodeRawBlock(&fr);
1489 if (ZSTD_isError(r)) {
1490 DISPLAY("Error in block mode on test seed %u: %s\n",
1491 (unsigned)seedCopy, ZSTD_getErrorName(r));
1496 { size_t const r = testDecodeWithDict(*seed, gt_block);
1497 if (ZSTD_isError(r)) {
1498 DISPLAY("Error in block mode with dictionary on test seed %u: %s\n",
1499 (unsigned)seedCopy, ZSTD_getErrorName(r));
1506 static int runFrameTest(U32* seed)
1509 U32 const seedCopy = *seed;
1510 { dictInfo const info = initDictInfo(0, 0, NULL, 0);
1511 *seed = generateFrame(*seed, &fr, info);
1514 { size_t const r = testDecodeSimple(&fr);
1515 if (ZSTD_isError(r)) {
1516 DISPLAY("Error in simple mode on test seed %u: %s\n",
1517 (unsigned)seedCopy, ZSTD_getErrorName(r));
1521 { size_t const r = testDecodeStreaming(&fr);
1522 if (ZSTD_isError(r)) {
1523 DISPLAY("Error in streaming mode on test seed %u: %s\n",
1524 (unsigned)seedCopy, ZSTD_getErrorName(r));
1528 { size_t const r = testDecodeWithDict(*seed, gt_frame); /* avoid big dictionaries */
1529 if (ZSTD_isError(r)) {
1530 DISPLAY("Error in dictionary mode on test seed %u: %s\n",
1531 (unsigned)seedCopy, ZSTD_getErrorName(r));
1538 static int runTestMode(U32 seed, unsigned numFiles, unsigned const testDurationS,
1543 UTIL_time_t const startClock = UTIL_getTime();
1544 U64 const maxClockSpan = testDurationS * SEC_TO_MICRO;
1546 if (numFiles == 0 && !testDurationS) numFiles = 1;
1548 DISPLAY("seed: %u\n", (unsigned)seed);
1550 for (fnum = 0; fnum < numFiles || UTIL_clockSpanMicro(startClock) < maxClockSpan; fnum++) {
1551 if (fnum < numFiles)
1552 DISPLAYUPDATE("\r%u/%u ", fnum, numFiles);
1554 DISPLAYUPDATE("\r%u ", fnum);
1556 { int const ret = (genType == gt_frame) ?
1557 runFrameTest(&seed) :
1558 runBlockTest(&seed);
1559 if (ret) return ret;
1563 DISPLAY("\r%u tests completed: ", fnum);
1569 /*-*******************************************************
1571 *********************************************************/
1573 static int generateFile(U32 seed, const char* const path,
1574 const char* const origPath, genType_e genType)
1578 DISPLAY("seed: %u\n", (unsigned)seed);
1580 { dictInfo const info = initDictInfo(0, 0, NULL, 0);
1581 if (genType == gt_frame) {
1582 generateFrame(seed, &fr, info);
1584 generateCompressedBlock(seed, &fr, info);
1587 outputBuffer(fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart, path);
1589 outputBuffer(fr.srcStart, (BYTE*)fr.src - (BYTE*)fr.srcStart, origPath);
1594 static int generateCorpus(U32 seed, unsigned numFiles, const char* const path,
1595 const char* const origPath, genType_e genType)
1597 char outPath[MAX_PATH];
1600 DISPLAY("seed: %u\n", (unsigned)seed);
1602 for (fnum = 0; fnum < numFiles; fnum++) {
1605 DISPLAYUPDATE("\r%u/%u ", fnum, numFiles);
1607 { dictInfo const info = initDictInfo(0, 0, NULL, 0);
1608 if (genType == gt_frame) {
1609 seed = generateFrame(seed, &fr, info);
1611 seed = generateCompressedBlock(seed, &fr, info);
1615 if (snprintf(outPath, MAX_PATH, "%s/z%06u.zst", path, fnum) + 1 > MAX_PATH) {
1616 DISPLAY("Error: path too long\n");
1619 outputBuffer(fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart, outPath);
1622 if (snprintf(outPath, MAX_PATH, "%s/z%06u", origPath, fnum) + 1 > MAX_PATH) {
1623 DISPLAY("Error: path too long\n");
1626 outputBuffer(fr.srcStart, (BYTE*)fr.src - (BYTE*)fr.srcStart, outPath);
1630 DISPLAY("\r%u/%u \n", fnum, numFiles);
1635 static int generateCorpusWithDict(U32 seed, unsigned numFiles, const char* const path,
1636 const char* const origPath, const size_t dictSize,
1639 char outPath[MAX_PATH];
1641 U32 const dictID = RAND(&seed);
1642 int errorDetected = 0;
1644 if (snprintf(outPath, MAX_PATH, "%s/dictionary", path) + 1 > MAX_PATH) {
1645 DISPLAY("Error: path too long\n");
1649 /* allocate space for the dictionary */
1650 fullDict = malloc(dictSize);
1651 if (fullDict == NULL) {
1652 DISPLAY("Error: could not allocate space for full dictionary.\n");
1656 /* randomly generate the dictionary */
1657 { int const ret = genRandomDict(dictID, seed, dictSize, fullDict);
1659 errorDetected = ret;
1664 /* write out dictionary */
1665 if (numFiles != 0) {
1666 if (snprintf(outPath, MAX_PATH, "%s/dictionary", path) + 1 > MAX_PATH) {
1667 DISPLAY("Error: dictionary path too long\n");
1671 outputBuffer(fullDict, dictSize, outPath);
1674 outputBuffer(fullDict, dictSize, "dictionary");
1677 /* generate random compressed/decompressed files */
1679 for (fnum = 0; fnum < MAX(numFiles, 1); fnum++) {
1681 DISPLAYUPDATE("\r%u/%u ", fnum, numFiles);
1683 size_t const headerSize = MAX(dictSize/4, 256);
1684 size_t const dictContentSize = dictSize-headerSize;
1685 BYTE* const dictContent = fullDict+headerSize;
1686 dictInfo const info = initDictInfo(1, dictContentSize, dictContent, dictID);
1687 if (genType == gt_frame) {
1688 seed = generateFrame(seed, &fr, info);
1690 seed = generateCompressedBlock(seed, &fr, info);
1694 if (numFiles != 0) {
1695 if (snprintf(outPath, MAX_PATH, "%s/z%06u.zst", path, fnum) + 1 > MAX_PATH) {
1696 DISPLAY("Error: path too long\n");
1700 outputBuffer(fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart, outPath);
1703 if (snprintf(outPath, MAX_PATH, "%s/z%06u", origPath, fnum) + 1 > MAX_PATH) {
1704 DISPLAY("Error: path too long\n");
1708 outputBuffer(fr.srcStart, (BYTE*)fr.src - (BYTE*)fr.srcStart, outPath);
1712 outputBuffer(fr.dataStart, (BYTE*)fr.data - (BYTE*)fr.dataStart, path);
1714 outputBuffer(fr.srcStart, (BYTE*)fr.src - (BYTE*)fr.srcStart, origPath);
1722 return errorDetected;
1726 /*_*******************************************************
1728 *********************************************************/
1729 static U32 makeSeed(void)
1731 U32 t = (U32) time(NULL);
1732 return XXH32(&t, sizeof(t), 0) % 65536;
1735 static unsigned readInt(const char** argument)
1738 while ((**argument>='0') && (**argument<='9')) {
1740 val += **argument - '0';
1746 static void usage(const char* programName)
1748 DISPLAY( "Usage :\n");
1749 DISPLAY( " %s [args]\n", programName);
1751 DISPLAY( "Arguments :\n");
1752 DISPLAY( " -p<path> : select output path (default:stdout)\n");
1753 DISPLAY( " in multiple files mode this should be a directory\n");
1754 DISPLAY( " -o<path> : select path to output original file (default:no output)\n");
1755 DISPLAY( " in multiple files mode this should be a directory\n");
1756 DISPLAY( " -s# : select seed (default:random based on time)\n");
1757 DISPLAY( " -n# : number of files to generate (default:1)\n");
1758 DISPLAY( " -t : activate test mode (test files against libzstd instead of outputting them)\n");
1759 DISPLAY( " -T# : length of time to run tests for\n");
1760 DISPLAY( " -v : increase verbosity level (default:0, max:7)\n");
1761 DISPLAY( " -h/H : display help/long help and exit\n");
1764 static void advancedUsage(const char* programName)
1768 DISPLAY( "Advanced arguments :\n");
1769 DISPLAY( " --content-size : always include the content size in the frame header\n");
1770 DISPLAY( " --use-dict=# : include a dictionary used to decompress the corpus\n");
1771 DISPLAY( " --gen-blocks : generate raw compressed blocks without block/frame headers\n");
1772 DISPLAY( " --max-block-size-log=# : max block size log, must be in range [2, 17]\n");
1773 DISPLAY( " --max-content-size-log=# : max content size log, must be <= 20\n");
1774 DISPLAY( " (this is ignored with gen-blocks)\n");
1777 /*! readU32FromChar() :
1778 @return : unsigned integer value read from input in `char` format
1779 allows and interprets K, KB, KiB, M, MB and MiB suffix.
1780 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
1781 Note : function result can overflow if digit string > MAX_UINT */
1782 static unsigned readU32FromChar(const char** stringPtr)
1784 unsigned result = 0;
1785 while ((**stringPtr >='0') && (**stringPtr <='9'))
1786 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
1787 if ((**stringPtr=='K') || (**stringPtr=='M')) {
1789 if (**stringPtr=='M') result <<= 10;
1791 if (**stringPtr=='i') (*stringPtr)++;
1792 if (**stringPtr=='B') (*stringPtr)++;
1797 /** longCommandWArg() :
1798 * check if *stringPtr is the same as longCommand.
1799 * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
1800 * @return 0 and doesn't modify *stringPtr otherwise.
1802 static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
1804 size_t const comSize = strlen(longCommand);
1805 int const result = !strncmp(*stringPtr, longCommand, comSize);
1806 if (result) *stringPtr += comSize;
1810 int main(int argc, char** argv)
1814 unsigned numFiles = 0;
1815 unsigned testDuration = 0;
1817 const char* path = NULL;
1818 const char* origPath = NULL;
1820 unsigned dictSize = (10 << 10); /* 10 kB default */
1821 genType_e genType = gt_frame;
1825 /* Check command line */
1826 for (argNb=1; argNb<argc; argNb++) {
1827 const char* argument = argv[argNb];
1828 if(!argument) continue; /* Protection if argument empty */
1830 /* Handle commands. Aggregated commands are allowed */
1831 if (argument[0]=='-') {
1833 while (*argument!=0) {
1840 advancedUsage(argv[0]);
1849 seed = readInt(&argument);
1853 numFiles = readInt(&argument);
1857 testDuration = readInt(&argument);
1858 if (*argument == 'm') {
1861 if (*argument == 'n') argument++;
1866 origPath = argument;
1867 argument += strlen(argument);
1872 argument += strlen(argument);
1880 if (strcmp(argument, "content-size") == 0) {
1881 opts.contentSize = 1;
1882 } else if (longCommandWArg(&argument, "use-dict=")) {
1883 dictSize = readU32FromChar(&argument);
1885 } else if (strcmp(argument, "gen-blocks") == 0) {
1887 } else if (longCommandWArg(&argument, "max-block-size-log=")) {
1888 U32 value = readU32FromChar(&argument);
1889 if (value >= 2 && value <= ZSTD_BLOCKSIZE_MAX) {
1890 g_maxBlockSize = 1U << value;
1892 } else if (longCommandWArg(&argument, "max-content-size-log=")) {
1893 U32 value = readU32FromChar(&argument);
1894 g_maxDecompressedSizeLog =
1895 MIN(MAX_DECOMPRESSED_SIZE_LOG, value);
1897 advancedUsage(argv[0]);
1900 argument += strlen(argument);
1905 } } } } /* for (argNb=1; argNb<argc; argNb++) */
1912 return runTestMode(seed, numFiles, testDuration, genType);
1915 DISPLAY("Error: -T requires test mode (-t)\n\n");
1922 DISPLAY("Error: path is required in file generation mode\n");
1927 if (numFiles == 0 && useDict == 0) {
1928 return generateFile(seed, path, origPath, genType);
1929 } else if (useDict == 0){
1930 return generateCorpus(seed, numFiles, path, origPath, genType);
1932 /* should generate files with a dictionary */
1933 return generateCorpusWithDict(seed, numFiles, path, origPath, dictSize, genType);