Rizin
unix-like reverse engineering framework and cli tools
datagen.c File Reference
#include "platform.h"
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

Go to the source code of this file.

Macros

#define KB   *(1 <<10)
 
#define PRIME1   2654435761U
 
#define PRIME2   2246822519U
 
#define LTLOG   13
 
#define LTSIZE   (1<<LTLOG)
 
#define LTMASK   (LTSIZE-1)
 
#define MIN(a, b)   ( (a) < (b) ? (a) :(b) )
 
#define RDG_rotl32(x, r)   ((x << r) | (x >> (32 - r)))
 
#define RDG_DICTSIZE   (32 KB)
 
#define RDG_RAND15BITS   ((RDG_rand(seed) >> 3) & 32767)
 
#define RDG_RANDLENGTH   ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)
 
#define RDG_BLOCKSIZE   (128 KB)
 

Typedefs

typedef BYTE litDistribTable[LTSIZE]
 

Functions

static unsigned int RDG_rand (U32 *src)
 
static void RDG_fillLiteralDistrib (litDistribTable lt, double ld)
 
static BYTE RDG_genChar (U32 *seed, const litDistribTable lt)
 
void RDG_genBlock (void *buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned *seedPtr)
 
void RDG_genBuffer (void *buffer, size_t size, double matchProba, double litProba, unsigned seed)
 
void RDG_genOut (unsigned long long size, double matchProba, double litProba, unsigned seed)
 

Macro Definition Documentation

◆ KB

#define KB   *(1 <<10)

Definition at line 40 of file datagen.c.

◆ LTLOG

#define LTLOG   13

Definition at line 49 of file datagen.c.

◆ LTMASK

#define LTMASK   (LTSIZE-1)

Definition at line 51 of file datagen.c.

◆ LTSIZE

#define LTSIZE   (1<<LTLOG)

Definition at line 50 of file datagen.c.

◆ MIN

#define MIN (   a,
  b 
)    ( (a) < (b) ? (a) :(b) )

Definition at line 59 of file datagen.c.

◆ PRIME1

#define PRIME1   2654435761U

Definition at line 42 of file datagen.c.

◆ PRIME2

#define PRIME2   2246822519U

Definition at line 43 of file datagen.c.

◆ RDG_BLOCKSIZE

#define RDG_BLOCKSIZE   (128 KB)

Definition at line 164 of file datagen.c.

◆ RDG_DICTSIZE

#define RDG_DICTSIZE   (32 KB)

Definition at line 99 of file datagen.c.

◆ RDG_RAND15BITS

#define RDG_RAND15BITS   ((RDG_rand(seed) >> 3) & 32767)

Definition at line 100 of file datagen.c.

◆ RDG_RANDLENGTH

#define RDG_RANDLENGTH   ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)

Definition at line 101 of file datagen.c.

◆ RDG_rotl32

#define RDG_rotl32 (   x,
  r 
)    ((x << r) | (x >> (32 - r)))

Definition at line 60 of file datagen.c.

Typedef Documentation

◆ litDistribTable

typedef BYTE litDistribTable[LTSIZE]

Definition at line 52 of file datagen.c.

Function Documentation

◆ RDG_fillLiteralDistrib()

static void RDG_fillLiteralDistrib ( litDistribTable  lt,
double  ld 
)
static

Definition at line 72 of file datagen.c.

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 }
#define MIN(a, b)
Definition: datagen.c:59
#define LTSIZE
Definition: datagen.c:50
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

References assert(), test_evm::end, lastChar(), lt, LTSIZE, MIN, and U32.

Referenced by RDG_genBuffer(), and RDG_genOut().

◆ RDG_genBlock()

void RDG_genBlock ( void *  buffer,
size_t  buffSize,
size_t  prefixSize,
double  matchProba,
litDistribTable  lt,
unsigned seedPtr 
)

Definition at line 102 of file datagen.c.

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 }
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
static unsigned int RDG_rand(U32 *src)
Definition: datagen.c:61
#define RDG_RANDLENGTH
Definition: datagen.c:101
#define RDG_RAND15BITS
Definition: datagen.c:100
static BYTE RDG_genChar(U32 *seed, const litDistribTable lt)
Definition: datagen.c:92
unsigned char match[65280+2]
Definition: gun.c:165
voidpf uLong offset
Definition: ioapi.h:144
return memset(p, 0, total)
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

References d, length, lt, match, memset(), pos, RDG_genChar(), RDG_rand(), RDG_RAND15BITS, RDG_RANDLENGTH, and U32.

Referenced by RDG_genBuffer(), and RDG_genOut().

◆ RDG_genBuffer()

void RDG_genBuffer ( void *  buffer,
size_t  size,
double  matchProba,
double  litProba,
unsigned  seed 
)

Definition at line 155 of file datagen.c.

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 }
void RDG_genBlock(void *buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned *seedPtr)
Definition: datagen.c:102
BYTE litDistribTable[LTSIZE]
Definition: datagen.c:52
static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
Definition: datagen.c:72
voidpf void uLong size
Definition: ioapi.h:138

References lt, RDG_fillLiteralDistrib(), and RDG_genBlock().

Referenced by BMK_benchMem(), and BMK_syntheticTest().

◆ RDG_genChar()

static BYTE RDG_genChar ( U32 seed,
const litDistribTable  lt 
)
static

Definition at line 92 of file datagen.c.

93 {
94  U32 id = RDG_rand(seed) & LTMASK;
95  return (lt[id]);
96 }
#define LTMASK
Definition: datagen.c:51

References lt, LTMASK, and RDG_rand().

Referenced by RDG_genBlock().

◆ RDG_genOut()

void RDG_genOut ( unsigned long long  size,
double  matchProba,
double  litProba,
unsigned  seed 
)

Definition at line 165 of file datagen.c.

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 }
#define RDG_BLOCKSIZE
Definition: datagen.c:164
#define RDG_DICTSIZE
Definition: datagen.c:99
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

References lt, memcpy(), RDG_BLOCKSIZE, RDG_DICTSIZE, RDG_fillLiteralDistrib(), RDG_genBlock(), and SET_BINARY_MODE.

Referenced by main().

◆ RDG_rand()

static unsigned int RDG_rand ( U32 src)
static

Definition at line 61 of file datagen.c.

62 {
63  U32 rand32 = *src;
64  rand32 *= PRIME1;
65  rand32 ^= PRIME2;
66  rand32 = RDG_rotl32(rand32, 13);
67  *src = rand32;
68  return rand32;
69 }
lzma_index * src
Definition: index.h:567
#define PRIME2
Definition: datagen.c:43
#define RDG_rotl32(x, r)
Definition: datagen.c:60
#define PRIME1
Definition: datagen.c:42

References PRIME1, PRIME2, RDG_rotl32, and src.

Referenced by RDG_genBlock(), and RDG_genChar().