5656#endif
5757
5858
59+ /**************************************
60+ * OS-specific Includes
61+ **************************************/
62+ #if defined(MSDOS ) || defined(OS2 ) || defined(WIN32 ) || defined(_WIN32 ) || defined(__CYGWIN__ )
63+ # include <fcntl.h> /* _O_BINARY */
64+ # include <io.h> /* _setmode, _isatty */
65+ # ifdef __MINGW32__
66+ int _fileno (FILE * stream ); /* MINGW somehow forgets to include this windows declaration into <stdio.h> */
67+ # endif
68+ # define SET_BINARY_MODE (file ) _setmode(_fileno(file), _O_BINARY)
69+ # define IS_CONSOLE (stdStream ) _isatty(_fileno(stdStream))
70+ #else
71+ # include <unistd.h> /* isatty */
72+ # define SET_BINARY_MODE (file )
73+ # define IS_CONSOLE (stdStream ) isatty(fileno(stdStream))
74+ #endif
75+
76+
5977/**************************************
6078* Constants
6179**************************************/
85103* Local Parameters
86104**************************************/
87105static unsigned no_prompt = 0 ;
88- static char * programName ;
89106static unsigned displayLevel = 2 ;
90107
91108
@@ -97,7 +114,7 @@ static unsigned int CDG_rand(U32* src)
97114{
98115 U32 rand32 = * src ;
99116 rand32 *= PRIME1 ;
100- rand32 + = PRIME2 ;
117+ rand32 ^ = PRIME2 ;
101118 rand32 = CDG_rotl32 (rand32 , 13 );
102119 * src = rand32 ;
103120 return rand32 ;
@@ -106,14 +123,20 @@ static unsigned int CDG_rand(U32* src)
106123
107124#define LTSIZE 8192
108125#define LTMASK (LTSIZE-1)
109- static const char firstChar = '(' ;
110- static const char lastChar = '}' ;
111126static void * CDG_createLiteralDistrib (double ld )
112127{
113- char * lt = malloc (LTSIZE );
128+ BYTE * lt = malloc (LTSIZE );
114129 U32 i = 0 ;
115- char character = '0' ;
130+ BYTE character = '0' ;
131+ BYTE firstChar = '(' ;
132+ BYTE lastChar = '}' ;
116133
134+ if (ld == 0.0 )
135+ {
136+ character = 0 ;
137+ firstChar = 0 ;
138+ lastChar = 255 ;
139+ }
117140 while (i < LTSIZE )
118141 {
119142 U32 weight = (U32 )((double )(LTSIZE - i ) * ld ) + 1 ;
@@ -137,18 +160,19 @@ static char CDG_genChar(U32* seed, const void* ltctx)
137160#define CDG_RAND15BITS ((CDG_rand(seed) >> 3) & 32767)
138161#define CDG_RANDLENGTH ( ((CDG_rand(seed) >> 7) & 7) ? (CDG_rand(seed) & 15) : (CDG_rand(seed) & 511) + 15)
139162#define CDG_DICTSIZE (32 KB)
140- static void CDG_generate (U64 size , U32 * seed , double matchProba )
163+ static void CDG_generate (U64 size , U32 * seed , double matchProba , double litProba )
141164{
142165 BYTE fullbuff [CDG_DICTSIZE + 128 KB + 1 ];
143166 BYTE * buff = fullbuff + CDG_DICTSIZE ;
144167 U64 total = 0 ;
145168 U32 P32 = (U32 )(32768 * matchProba );
146169 U32 pos = 1 ;
147170 U32 genBlockSize = 128 KB ;
148- double literalDistrib = 0.13 ;
149- void * ldctx = CDG_createLiteralDistrib ( literalDistrib ) ;
171+ void * ldctx = CDG_createLiteralDistrib ( litProba ) ;
172+ FILE * fout = stdout ;
150173
151- /* Build initial prefix */
174+ /* init */
175+ SET_BINARY_MODE (stdout );
152176 fullbuff [0 ] = CDG_genChar (seed , ldctx );
153177 while (pos < 32 KB )
154178 {
@@ -207,11 +231,8 @@ static void CDG_generate(U64 size, U32* seed, double matchProba)
207231 }
208232 }
209233
210- /* output datagen */
211- pos = 0 ;
212- for (;pos + 512 <=genBlockSize ;pos += 512 )
213- printf ("%512.512s" , buff + pos );
214- for (;pos < genBlockSize ;pos ++ ) printf ("%c" , buff [pos ]);
234+ /* output generated data */
235+ fwrite (buff , 1 , genBlockSize , fout );
215236 /* Regenerate prefix */
216237 memcpy (fullbuff , buff + 96 KB , 32 KB );
217238 }
@@ -221,7 +242,7 @@ static void CDG_generate(U64 size, U32* seed, double matchProba)
221242/*********************************************************
222243* Command line
223244*********************************************************/
224- static int CDG_usage (void )
245+ static int CDG_usage (char * programName )
225246{
226247 DISPLAY ( "Compressible data generator\n" );
227248 DISPLAY ( "Usage :\n" );
@@ -239,9 +260,11 @@ static int CDG_usage(void)
239260int main (int argc , char * * argv )
240261{
241262 int argNb ;
242- int proba = CDG_COMPRESSIBILITY_DEFAULT ;
263+ double proba = (double )CDG_COMPRESSIBILITY_DEFAULT / 100 ;
264+ double litProba = proba / 3.6 ;
243265 U64 size = CDG_SIZE_DEFAULT ;
244266 U32 seed = CDG_SEED_DEFAULT ;
267+ char * programName ;
245268
246269 /* Check command line */
247270 programName = argv [0 ];
@@ -262,7 +285,7 @@ int main(int argc, char** argv)
262285 switch (* argument )
263286 {
264287 case 'h' :
265- return CDG_usage ();
288+ return CDG_usage (programName );
266289 case 'g' :
267290 argument ++ ;
268291 size = 0 ;
@@ -287,23 +310,37 @@ int main(int argc, char** argv)
287310 argument ++ ;
288311 }
289312 break ;
290- case 'p ' :
313+ case 'P ' :
291314 argument ++ ;
292- proba = 0 ;
315+ proba = 0.0 ;
293316 while ((* argument >='0' ) && (* argument <='9' ))
294317 {
295318 proba *= 10 ;
296319 proba += * argument - '0' ;
297320 argument ++ ;
298321 }
299- if (proba < 0 ) proba = 0 ;
300- if (proba > 100 ) proba = 100 ;
322+ if (proba > 100. ) proba = 100. ;
323+ proba /= 100. ;
324+ litProba = proba / 4. ;
325+ break ;
326+ case 'L' :
327+ argument ++ ;
328+ litProba = 0. ;
329+ while ((* argument >='0' ) && (* argument <='9' ))
330+ {
331+ litProba *= 10 ;
332+ litProba += * argument - '0' ;
333+ argument ++ ;
334+ }
335+ if (litProba > 100. ) litProba = 100. ;
336+ litProba /= 100. ;
301337 break ;
302338 case 'v' :
303339 displayLevel = 4 ;
304340 argument ++ ;
305341 break ;
306- default : ;
342+ default :
343+ return CDG_usage (programName );
307344 }
308345 }
309346
@@ -312,9 +349,9 @@ int main(int argc, char** argv)
312349
313350 DISPLAYLEVEL (4 , "Data Generator %s \n" , ZSTD_VERSION );
314351 DISPLAYLEVEL (3 , "Seed = %u \n" , seed );
315- if (proba != CDG_COMPRESSIBILITY_DEFAULT ) DISPLAYLEVEL (3 , "Compressibility : %i%%\n" , proba );
352+ if (proba != CDG_COMPRESSIBILITY_DEFAULT ) DISPLAYLEVEL (3 , "Compressibility : %i%%\n" , ( U32 )( proba * 100 ) );
316353
317- CDG_generate (size , & seed , (( double ) proba ) / 100 );
354+ CDG_generate (size , & seed , proba , litProba );
318355
319356 return 0 ;
320357}
0 commit comments