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**************************************/
7088#define CDG_SIZE_DEFAULT (64 KB)
7189#define CDG_SEED_DEFAULT 0
7290#define CDG_COMPRESSIBILITY_DEFAULT 50
73- #define CDG_LITDENSITY_DEFAULT 12
7491#define PRIME1 2654435761U
7592#define PRIME2 2246822519U
7693
@@ -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 ;
@@ -146,8 +169,10 @@ static void CDG_generate(U64 size, U32* seed, double matchProba, double litProba
146169 U32 pos = 1 ;
147170 U32 genBlockSize = 128 KB ;
148171 void * ldctx = CDG_createLiteralDistrib (litProba );
172+ FILE * fout = stdout ;
149173
150- /* Build initial prefix */
174+ /* init */
175+ SET_BINARY_MODE (stdout );
151176 fullbuff [0 ] = CDG_genChar (seed , ldctx );
152177 while (pos < 32 KB )
153178 {
@@ -206,11 +231,8 @@ static void CDG_generate(U64 size, U32* seed, double matchProba, double litProba
206231 }
207232 }
208233
209- /* output datagen */
210- pos = 0 ;
211- for (;pos + 512 <=genBlockSize ;pos += 512 )
212- printf ("%512.512s" , buff + pos );
213- for (;pos < genBlockSize ;pos ++ ) printf ("%c" , buff [pos ]);
234+ /* output generated data */
235+ fwrite (buff , 1 , genBlockSize , fout );
214236 /* Regenerate prefix */
215237 memcpy (fullbuff , buff + 96 KB , 32 KB );
216238 }
@@ -238,8 +260,8 @@ static int CDG_usage(char* programName)
238260int main (int argc , char * * argv )
239261{
240262 int argNb ;
241- U32 proba = CDG_COMPRESSIBILITY_DEFAULT ;
242- U32 litProba = CDG_LITDENSITY_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 ;
245267 char * programName ;
@@ -290,25 +312,28 @@ int main(int argc, char** argv)
290312 break ;
291313 case 'P' :
292314 argument ++ ;
293- proba = 0 ;
315+ proba = 0.0 ;
294316 while ((* argument >='0' ) && (* argument <='9' ))
295317 {
296318 proba *= 10 ;
297319 proba += * argument - '0' ;
298320 argument ++ ;
299321 }
300- if (proba > 100 ) proba = 100 ;
322+ if (proba > 100. ) proba = 100. ;
323+ proba /= 100. ;
324+ litProba = proba / 4. ;
301325 break ;
302326 case 'L' :
303327 argument ++ ;
304- litProba = 0 ;
328+ litProba = 0. ;
305329 while ((* argument >='0' ) && (* argument <='9' ))
306330 {
307331 litProba *= 10 ;
308332 litProba += * argument - '0' ;
309333 argument ++ ;
310334 }
311- if (litProba > 100 ) litProba = 100 ;
335+ if (litProba > 100. ) litProba = 100. ;
336+ litProba /= 100. ;
312337 break ;
313338 case 'v' :
314339 displayLevel = 4 ;
@@ -324,9 +349,9 @@ int main(int argc, char** argv)
324349
325350 DISPLAYLEVEL (4 , "Data Generator %s \n" , ZSTD_VERSION );
326351 DISPLAYLEVEL (3 , "Seed = %u \n" , seed );
327- 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 ) );
328353
329- CDG_generate (size , & seed , (( double ) proba ) / 100 , (( double ) litProba ) / 100 );
354+ CDG_generate (size , & seed , proba , litProba );
330355
331356 return 0 ;
332357}
0 commit comments