Rizin
unix-like reverse engineering framework and cli tools
datagen.c
Go to the documentation of this file.
1 /*
2  datagen.c - compressible data generator test tool
3  Copyright (C) Yann Collet 2012-2016
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)
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)))
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 
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 
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)
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 
155 void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed)
156 {
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)
165 void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed)
166 {
168  U64 total = 0;
169  size_t genBlockSize = RDG_BLOCKSIZE;
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 }
lzma_index * src
Definition: index.h:567
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
void RDG_genBuffer(void *buffer, size_t size, double matchProba, double litProba, unsigned seed)
Definition: datagen.c:155
#define PRIME2
Definition: datagen.c:43
static unsigned int RDG_rand(U32 *src)
Definition: datagen.c:61
#define MIN(a, b)
Definition: datagen.c:59
#define RDG_BLOCKSIZE
Definition: datagen.c:164
#define LTSIZE
Definition: datagen.c:50
void RDG_genBlock(void *buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned *seedPtr)
Definition: datagen.c:102
#define RDG_RANDLENGTH
Definition: datagen.c:101
#define LTMASK
Definition: datagen.c:51
#define RDG_DICTSIZE
Definition: datagen.c:99
#define RDG_rotl32(x, r)
Definition: datagen.c:60
BYTE litDistribTable[LTSIZE]
Definition: datagen.c:52
static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
Definition: datagen.c:72
#define RDG_RAND15BITS
Definition: datagen.c:100
#define PRIME1
Definition: datagen.c:42
void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed)
Definition: datagen.c:165
static BYTE RDG_genChar(U32 *seed, const litDistribTable lt)
Definition: datagen.c:92
unsigned char match[65280+2]
Definition: gun.c:165
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define SET_BINARY_MODE(file)
Definition: platform.h:145
unsigned long long U64
Definition: lz4.c:290
unsigned char BYTE
Definition: lz4.c:286
unsigned int U32
Definition: lz4.c:288
assert(limit<=UINT32_MAX/2)
#define U32(val)
static char lastChar(const char *str)
Definition: sdb.c:445
int size_t
Definition: sftypes.h:40
#define d(i)
Definition: sha256.c:44
Definition: buffer.h:15
Definition: engine.c:71
int pos
Definition: main.c:11
Miscellaneous utility functions.