2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
13 /*-************************************
15 **************************************/
17 #include "platform.h" /* SET_BINARY_MODE */
18 #include <stdlib.h> /* malloc, free */
19 #include <stdio.h> /* FILE, fwrite, fprintf */
20 #include <string.h> /* memcpy */
21 #include "mem.h" /* U32 */
24 /*-************************************
26 **************************************/
28 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
31 #define TRACE(...) if (RDG_DEBUG) fprintf(stderr, __VA_ARGS__ )
34 /*-************************************
36 **************************************/
38 #define LTSIZE (1<<LTLOG)
39 #define LTMASK (LTSIZE-1)
42 /*-*******************************************************
44 *********************************************************/
45 #define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r)))
46 static U32 RDG_rand(U32* src)
48 static const U32 prime1 = 2654435761U;
49 static const U32 prime2 = 2246822519U;
53 rand32 = RDG_rotl32(rand32, 13);
59 static void RDG_fillLiteralDistrib(BYTE* ldt, double ld)
61 BYTE const firstChar = (ld<=0.0) ? 0 : '(';
62 BYTE const lastChar = (ld<=0.0) ? 255 : '}';
63 BYTE character = (ld<=0.0) ? 0 : '0';
66 if (ld<=0.0) ld = 0.0;
67 for (u=0; u<LTSIZE; ) {
68 U32 const weight = (U32)((double)(LTSIZE - u) * ld) + 1;
69 U32 const end = MIN ( u + weight , LTSIZE);
70 while (u < end) ldt[u++] = character;
72 if (character > lastChar) character = firstChar;
77 static BYTE RDG_genChar(U32* seed, const BYTE* ldt)
79 U32 const id = RDG_rand(seed) & LTMASK;
80 return ldt[id]; /* memory-sanitizer fails here, stating "uninitialized value" when table initialized with P==0.0. Checked : table is fully initialized */
84 static U32 RDG_rand15Bits (U32* seedPtr)
86 return RDG_rand(seedPtr) & 0x7FFF;
89 static U32 RDG_randLength(U32* seedPtr)
91 if (RDG_rand(seedPtr) & 7) return (RDG_rand(seedPtr) & 0xF); /* small length */
92 return (RDG_rand(seedPtr) & 0x1FF) + 0xF;
95 static void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, const BYTE* ldt, U32* seedPtr)
97 BYTE* const buffPtr = (BYTE*)buffer;
98 U32 const matchProba32 = (U32)(32768 * matchProba);
99 size_t pos = prefixSize;
102 /* special case : sparse content */
103 while (matchProba >= 1.0) {
104 size_t size0 = RDG_rand(seedPtr) & 3;
105 size0 = (size_t)1 << (16 + size0 * 2);
106 size0 += RDG_rand(seedPtr) & (size0-1); /* because size0 is power of 2*/
107 if (buffSize < pos + size0) {
108 memset(buffPtr+pos, 0, buffSize-pos);
111 memset(buffPtr+pos, 0, size0);
113 buffPtr[pos-1] = RDG_genChar(seedPtr, ldt);
118 if (pos==0) buffPtr[0] = RDG_genChar(seedPtr, ldt), pos=1;
120 /* Generate compressible data */
121 while (pos < buffSize) {
122 /* Select : Literal (char) or Match (within 32K) */
123 if (RDG_rand15Bits(seedPtr) < matchProba32) {
124 /* Copy (within 32K) */
125 U32 const length = RDG_randLength(seedPtr) + 4;
126 U32 const d = (U32) MIN(pos + length , buffSize);
127 U32 const repeatOffset = (RDG_rand(seedPtr) & 15) == 2;
128 U32 const randOffset = RDG_rand15Bits(seedPtr) + 1;
129 U32 const offset = repeatOffset ? prevOffset : (U32) MIN(randOffset , pos);
130 size_t match = pos - offset;
131 while (pos < d) buffPtr[pos++] = buffPtr[match++]; /* correctly manages overlaps */
134 /* Literal (noise) */
135 U32 const length = RDG_randLength(seedPtr);
136 U32 const d = (U32) MIN(pos + length, buffSize);
137 while (pos < d) buffPtr[pos++] = RDG_genChar(seedPtr, ldt);
142 void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed)
146 memset(ldt, '0', sizeof(ldt)); /* yes, character '0', this is intentional */
147 if (litProba<=0.0) litProba = matchProba / 4.5;
148 RDG_fillLiteralDistrib(ldt, litProba);
149 RDG_genBlock(buffer, size, 0, matchProba, ldt, &seed32);
153 void RDG_genStdout(unsigned long long size, double matchProba, double litProba, unsigned seed)
156 size_t const stdBlockSize = 128 KB;
157 size_t const stdDictSize = 32 KB;
158 BYTE* const buff = (BYTE*)malloc(stdDictSize + stdBlockSize);
160 BYTE ldt[LTSIZE]; /* literals distribution table */
163 if (buff==NULL) { perror("datagen"); exit(1); }
164 if (litProba<=0.0) litProba = matchProba / 4.5;
165 memset(ldt, '0', sizeof(ldt)); /* yes, character '0', this is intentional */
166 RDG_fillLiteralDistrib(ldt, litProba);
167 SET_BINARY_MODE(stdout);
169 /* Generate initial dict */
170 RDG_genBlock(buff, stdDictSize, 0, matchProba, ldt, &seed32);
172 /* Generate compressible data */
173 while (total < size) {
174 size_t const genBlockSize = (size_t) (MIN (stdBlockSize, size-total));
175 RDG_genBlock(buff, stdDictSize+stdBlockSize, stdDictSize, matchProba, ldt, &seed32);
176 total += genBlockSize;
177 { size_t const unused = fwrite(buff, 1, genBlockSize, stdout); (void)unused; }
179 memcpy(buff, buff + stdBlockSize, stdDictSize);