1 /*
2 datagen.c - compressible data generator test tool
3 Copyright (C) Yann Collet 2012-2020
4
5 GPL v2 License
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 You can contact the author at :
22 - LZ4 source repository : https://github.com/lz4/lz4
23 - Public forum : https://groups.google.com/forum/#!forum/lz4c
24 */
25
26 /**************************************
27 * Includes
28 **************************************/
29 #include "platform.h" /* Compiler options, SET_BINARY_MODE */
30 #include "util.h" /* U32 */
31 #include <stdlib.h> /* malloc */
32 #include <stdio.h> /* FILE, fwrite */
33 #include <string.h> /* memcpy */
34 #include <assert.h>
35
36
37 /**************************************
38 * Constants
39 **************************************/
40 #define KB *(1 <<10)
41
42 #define PRIME1 2654435761U
43 #define PRIME2 2246822519U
44
45
46 /**************************************
47 * Local types
48 **************************************/
49 #define LTLOG 13
50 #define LTSIZE (1<<LTLOG)
51 #define LTMASK (LTSIZE-1)
52 typedef BYTE litDistribTable[LTSIZE];
53
54
55
56 /*********************************************************
57 * Local Functions
58 *********************************************************/
59 #define MIN(a,b) ( (a) < (b) ? (a) :(b) )
60 #define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r)))
RDG_rand(U32 * src)61 static unsigned int RDG_rand(U32* src)
62 {
63 U32 rand32 = *src;
64 rand32 *= PRIME1;
65 rand32 ^= PRIME2;
66 rand32 = RDG_rotl32(rand32, 13);
67 *src = rand32;
68 return rand32;
69 }
70
71
RDG_fillLiteralDistrib(litDistribTable lt,double ld)72 static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
73 {
74 BYTE const firstChar = ld <= 0.0 ? 0 : '(';
75 BYTE const lastChar = ld <= 0.0 ? 255 : '}';
76 BYTE character = ld <= 0.0 ? 0 : '0';
77 U32 u = 0;
78
79 while (u<LTSIZE) {
80 U32 const weight = (U32)((double)(LTSIZE - u) * ld) + 1;
81 U32 const end = MIN(u+weight, LTSIZE);
82 while (u < end) {
83 assert(u<LTSIZE); /* try to ease static analyzer. u < end <= LTSIZE */
84 lt[u++] = character;
85 }
86 character++;
87 if (character > lastChar) character = firstChar;
88 }
89 }
90
91
RDG_genChar(U32 * seed,const litDistribTable lt)92 static BYTE RDG_genChar(U32* seed, const litDistribTable lt)
93 {
94 U32 id = RDG_rand(seed) & LTMASK;
95 return (lt[id]);
96 }
97
98
99 #define RDG_DICTSIZE (32 KB)
100 #define RDG_RAND15BITS ((RDG_rand(seed) >> 3) & 32767)
101 #define RDG_RANDLENGTH ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)
RDG_genBlock(void * buffer,size_t buffSize,size_t prefixSize,double matchProba,litDistribTable lt,unsigned * seedPtr)102 void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned* seedPtr)
103 {
104 BYTE* buffPtr = (BYTE*)buffer;
105 const U32 matchProba32 = (U32)(32768 * matchProba);
106 size_t pos = prefixSize;
107 U32* seed = seedPtr;
108
109 /* special case */
110 while (matchProba >= 1.0) {
111 size_t size0 = RDG_rand(seed) & 3;
112 size0 = (size_t)1 << (16 + size0 * 2);
113 size0 += RDG_rand(seed) & (size0-1); /* because size0 is power of 2*/
114 if (buffSize < pos + size0) {
115 memset(buffPtr+pos, 0, buffSize-pos);
116 return;
117 }
118 memset(buffPtr+pos, 0, size0);
119 pos += size0;
120 buffPtr[pos-1] = RDG_genChar(seed, lt);
121 }
122
123 /* init */
124 if (pos==0) {
125 buffPtr[0] = RDG_genChar(seed, lt);
126 pos=1;
127 }
128
129 /* Generate compressible data */
130 while (pos < buffSize) {
131 /* Select : Literal (char) or Match (within 32K) */
132 if (RDG_RAND15BITS < matchProba32) {
133 /* Copy (within 32K) */
134 size_t match;
135 size_t d;
136 int length = RDG_RANDLENGTH + 4;
137 U32 offset = RDG_RAND15BITS + 1;
138 if (offset > pos) offset = (U32)pos;
139 match = pos - offset;
140 d = pos + length;
141 if (d > buffSize) d = buffSize;
142 while (pos < d) buffPtr[pos++] = buffPtr[match++];
143 } else {
144 /* Literal (noise) */
145 size_t d;
146 size_t length = RDG_RANDLENGTH;
147 d = pos + length;
148 if (d > buffSize) d = buffSize;
149 while (pos < d) buffPtr[pos++] = RDG_genChar(seed, lt);
150 }
151 }
152 }
153
154
RDG_genBuffer(void * buffer,size_t size,double matchProba,double litProba,unsigned seed)155 void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed)
156 {
157 litDistribTable lt;
158 if (litProba==0.0) litProba = matchProba / 4.5;
159 RDG_fillLiteralDistrib(lt, litProba);
160 RDG_genBlock(buffer, size, 0, matchProba, lt, &seed);
161 }
162
163
164 #define RDG_BLOCKSIZE (128 KB)
RDG_genOut(unsigned long long size,double matchProba,double litProba,unsigned seed)165 void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed)
166 {
167 BYTE buff[RDG_DICTSIZE + RDG_BLOCKSIZE];
168 U64 total = 0;
169 size_t genBlockSize = RDG_BLOCKSIZE;
170 litDistribTable lt;
171
172 /* init */
173 if (litProba==0.0) litProba = matchProba / 4.5;
174 RDG_fillLiteralDistrib(lt, litProba);
175 SET_BINARY_MODE(stdout);
176
177 /* Generate dict */
178 RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, lt, &seed);
179
180 /* Generate compressible data */
181 while (total < size) {
182 RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, lt, &seed);
183 if (size-total < RDG_BLOCKSIZE) genBlockSize = (size_t)(size-total);
184 total += genBlockSize;
185 fwrite(buff, 1, genBlockSize, stdout); /* should check potential write error */
186 /* update dict */
187 memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE);
188 }
189 }
190