2015-01-24 00:58:16 +00:00
|
|
|
/*
|
|
|
|
datagen.c - compressible data generator test tool
|
|
|
|
Copyright (C) Yann Collet 2012-2015
|
|
|
|
|
|
|
|
GPL v2 License
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
You can contact the author at :
|
|
|
|
- ZSTD source repository : https://github.com/Cyan4973/zstd
|
|
|
|
- Public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
*/
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Includes
|
|
|
|
**************************************/
|
|
|
|
#include <stdlib.h> /* malloc */
|
2015-02-09 07:00:26 +00:00
|
|
|
#include <stdio.h> /* FILE, fwrite */
|
|
|
|
#include <string.h> /* memcpy */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Basic Types
|
|
|
|
**************************************/
|
|
|
|
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
|
|
|
|
# include <stdint.h>
|
|
|
|
typedef uint8_t BYTE;
|
|
|
|
typedef uint16_t U16;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef int32_t S32;
|
|
|
|
typedef uint64_t U64;
|
|
|
|
#else
|
|
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef unsigned short U16;
|
|
|
|
typedef unsigned int U32;
|
|
|
|
typedef signed int S32;
|
|
|
|
typedef unsigned long long U64;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-************************************
|
2015-01-24 12:31:55 +00:00
|
|
|
* OS-specific Includes
|
|
|
|
**************************************/
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _isatty */
|
2015-07-05 07:20:56 +00:00
|
|
|
# define SET_BINARY_MODE(file) {int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; }
|
2015-01-24 12:31:55 +00:00
|
|
|
#else
|
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Constants
|
|
|
|
**************************************/
|
|
|
|
#define KB *(1 <<10)
|
|
|
|
|
|
|
|
#define PRIME1 2654435761U
|
|
|
|
#define PRIME2 2246822519U
|
|
|
|
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-************************************
|
2015-07-05 00:23:52 +00:00
|
|
|
* Local types
|
|
|
|
**************************************/
|
|
|
|
#define LTLOG 13
|
|
|
|
#define LTSIZE (1<<LTLOG)
|
|
|
|
#define LTMASK (LTSIZE-1)
|
|
|
|
typedef BYTE litDistribTable[LTSIZE];
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/*-*******************************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Local Functions
|
|
|
|
*********************************************************/
|
2015-02-09 07:00:26 +00:00
|
|
|
#define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r)))
|
2015-07-05 00:23:52 +00:00
|
|
|
static unsigned int RDG_rand(U32* src)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
U32 rand32 = *src;
|
|
|
|
rand32 *= PRIME1;
|
2015-01-24 12:31:55 +00:00
|
|
|
rand32 ^= PRIME2;
|
2015-02-09 07:00:26 +00:00
|
|
|
rand32 = RDG_rotl32(rand32, 13);
|
2015-01-24 00:58:16 +00:00
|
|
|
*src = rand32;
|
|
|
|
return rand32;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-05 00:23:52 +00:00
|
|
|
static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
|
|
|
U32 i = 0;
|
2015-01-24 12:31:55 +00:00
|
|
|
BYTE character = '0';
|
|
|
|
BYTE firstChar = '(';
|
|
|
|
BYTE lastChar = '}';
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
if (ld==0.0) {
|
2015-07-05 00:23:52 +00:00
|
|
|
character = 0;
|
2015-01-24 12:31:55 +00:00
|
|
|
firstChar = 0;
|
2015-07-05 00:23:52 +00:00
|
|
|
lastChar =255;
|
2015-01-24 12:31:55 +00:00
|
|
|
}
|
2016-02-02 13:36:49 +00:00
|
|
|
while (i<LTSIZE) {
|
2015-01-24 00:58:16 +00:00
|
|
|
U32 weight = (U32)((double)(LTSIZE - i) * ld) + 1;
|
|
|
|
U32 end;
|
|
|
|
if (weight + i > LTSIZE) weight = LTSIZE-i;
|
|
|
|
end = i + weight;
|
|
|
|
while (i < end) lt[i++] = character;
|
|
|
|
character++;
|
|
|
|
if (character > lastChar) character = firstChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 00:23:52 +00:00
|
|
|
|
|
|
|
static BYTE RDG_genChar(U32* seed, const litDistribTable lt)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-02-09 07:00:26 +00:00
|
|
|
U32 id = RDG_rand(seed) & LTMASK;
|
2015-07-05 00:23:52 +00:00
|
|
|
return (lt[id]);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 00:23:52 +00:00
|
|
|
|
2015-11-20 11:46:08 +00:00
|
|
|
#define RDG_RAND15BITS ((RDG_rand(seed) >> 3) & 0x7FFF)
|
2015-02-09 07:00:26 +00:00
|
|
|
#define RDG_RANDLENGTH ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)
|
2015-07-05 00:23:52 +00:00
|
|
|
void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned* seedPtr)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-03-09 12:17:36 +00:00
|
|
|
BYTE* buffPtr = (BYTE*)buffer;
|
2015-02-10 17:15:20 +00:00
|
|
|
const U32 matchProba32 = (U32)(32768 * matchProba);
|
|
|
|
size_t pos = prefixSize;
|
|
|
|
U32* seed = seedPtr;
|
2015-11-20 11:00:25 +00:00
|
|
|
U32 prevOffset = 1;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-07-08 01:26:17 +00:00
|
|
|
/* special case : sparse content */
|
2016-02-02 13:36:49 +00:00
|
|
|
while (matchProba >= 1.0) {
|
2015-03-09 12:17:36 +00:00
|
|
|
size_t size0 = RDG_rand(seed) & 3;
|
2015-07-05 00:23:52 +00:00
|
|
|
size0 = (size_t)1 << (16 + size0 * 2);
|
2015-03-09 12:17:36 +00:00
|
|
|
size0 += RDG_rand(seed) & (size0-1); /* because size0 is power of 2*/
|
2016-02-02 13:36:49 +00:00
|
|
|
if (buffSize < pos + size0) {
|
2015-03-09 12:17:36 +00:00
|
|
|
memset(buffPtr+pos, 0, buffSize-pos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(buffPtr+pos, 0, size0);
|
|
|
|
pos += size0;
|
2015-07-05 00:23:52 +00:00
|
|
|
buffPtr[pos-1] = RDG_genChar(seed, lt);
|
2015-08-23 22:13:49 +00:00
|
|
|
continue;
|
2015-03-09 12:17:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 12:31:55 +00:00
|
|
|
/* init */
|
2015-07-05 00:23:52 +00:00
|
|
|
if (pos==0) buffPtr[0] = RDG_genChar(seed, lt), pos=1;
|
2015-02-10 17:15:20 +00:00
|
|
|
|
|
|
|
/* Generate compressible data */
|
2016-02-02 13:36:49 +00:00
|
|
|
while (pos < buffSize) {
|
2015-01-24 00:58:16 +00:00
|
|
|
/* Select : Literal (char) or Match (within 32K) */
|
2016-02-02 13:36:49 +00:00
|
|
|
if (RDG_RAND15BITS < matchProba32) {
|
2015-02-10 17:15:20 +00:00
|
|
|
/* Copy (within 32K) */
|
2015-07-05 00:23:52 +00:00
|
|
|
size_t match;
|
2015-11-20 11:46:08 +00:00
|
|
|
size_t d;
|
|
|
|
int length = RDG_RANDLENGTH + 4;
|
2015-02-09 07:00:26 +00:00
|
|
|
U32 offset = RDG_RAND15BITS + 1;
|
2015-11-20 11:00:25 +00:00
|
|
|
U32 repeatOffset = (RDG_rand(seed) & 15) == 2;
|
|
|
|
if (repeatOffset) offset = prevOffset;
|
2015-07-05 00:23:52 +00:00
|
|
|
if (offset > pos) offset = (U32)pos;
|
2015-12-01 00:31:17 +00:00
|
|
|
prevOffset = offset;
|
2015-02-10 17:15:20 +00:00
|
|
|
match = pos - offset;
|
2015-11-20 11:46:08 +00:00
|
|
|
d = pos + length;
|
|
|
|
if (d > buffSize) d = buffSize;
|
|
|
|
while (pos < d) buffPtr[pos++] = buffPtr[match++]; /* correctly manages overlaps */
|
2016-02-02 13:36:49 +00:00
|
|
|
} else {
|
2015-01-24 00:58:16 +00:00
|
|
|
/* Literal (noise) */
|
2015-03-09 12:17:36 +00:00
|
|
|
size_t d;
|
|
|
|
size_t length = RDG_RANDLENGTH;
|
2015-02-10 17:15:20 +00:00
|
|
|
d = pos + length;
|
2015-03-09 12:17:36 +00:00
|
|
|
if (d > buffSize) d = buffSize;
|
2015-07-05 00:23:52 +00:00
|
|
|
while (pos < d) buffPtr[pos++] = RDG_genChar(seed, lt);
|
2016-02-02 13:36:49 +00:00
|
|
|
} }
|
2015-02-10 17:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed)
|
|
|
|
{
|
2015-07-05 00:23:52 +00:00
|
|
|
litDistribTable lt;
|
2015-03-09 12:17:36 +00:00
|
|
|
if (litProba==0.0) litProba = matchProba / 4.5;
|
2015-07-05 00:23:52 +00:00
|
|
|
RDG_fillLiteralDistrib(lt, litProba);
|
|
|
|
RDG_genBlock(buffer, size, 0, matchProba, lt, &seed);
|
2015-02-10 17:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-08 01:26:17 +00:00
|
|
|
#define RDG_DICTSIZE (32 KB)
|
2015-02-10 17:15:20 +00:00
|
|
|
#define RDG_BLOCKSIZE (128 KB)
|
2015-08-23 22:13:49 +00:00
|
|
|
void RDG_genStdout(unsigned long long size, double matchProba, double litProba, unsigned seed)
|
2015-02-10 17:15:20 +00:00
|
|
|
{
|
2015-07-08 01:26:17 +00:00
|
|
|
BYTE* buff = (BYTE*)malloc(RDG_DICTSIZE + RDG_BLOCKSIZE);
|
2015-02-10 17:15:20 +00:00
|
|
|
U64 total = 0;
|
2015-03-09 12:17:36 +00:00
|
|
|
size_t genBlockSize = RDG_BLOCKSIZE;
|
2015-07-05 00:23:52 +00:00
|
|
|
litDistribTable lt;
|
2015-02-10 17:15:20 +00:00
|
|
|
|
|
|
|
/* init */
|
2015-07-08 01:26:17 +00:00
|
|
|
if (buff==NULL) { fprintf(stdout, "not enough memory\n"); exit(1); }
|
2015-03-09 12:17:36 +00:00
|
|
|
if (litProba==0.0) litProba = matchProba / 4.5;
|
2015-07-05 00:23:52 +00:00
|
|
|
RDG_fillLiteralDistrib(lt, litProba);
|
2015-02-10 17:15:20 +00:00
|
|
|
SET_BINARY_MODE(stdout);
|
|
|
|
|
2015-07-08 01:26:17 +00:00
|
|
|
/* Generate initial dict */
|
2015-07-05 00:23:52 +00:00
|
|
|
RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, lt, &seed);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* Generate compressible data */
|
2016-02-02 13:36:49 +00:00
|
|
|
while (total < size) {
|
2015-07-05 00:23:52 +00:00
|
|
|
RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, lt, &seed);
|
2015-03-09 12:17:36 +00:00
|
|
|
if (size-total < RDG_BLOCKSIZE) genBlockSize = (size_t)(size-total);
|
2015-01-24 00:58:16 +00:00
|
|
|
total += genBlockSize;
|
2015-02-10 17:15:20 +00:00
|
|
|
fwrite(buff, 1, genBlockSize, stdout);
|
|
|
|
/* update dict */
|
2015-03-09 12:17:36 +00:00
|
|
|
memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
2015-07-08 01:26:17 +00:00
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
/* cleanup */
|
2015-07-08 01:26:17 +00:00
|
|
|
free(buff);
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|