Created
September 3, 2018 03:49
-
-
Save nicolasnoble/982804719ecb71c851779c4571669391 to your computer and use it in GitHub Desktop.
Generate random, yet compressible data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void gen_pre_buf(uint8_t * pre_buf, uint8_t (*get_byte)(void *), void * opaque) { | |
int i; | |
memset(pre_buf, 0, 1024); | |
for (i = 0; i < 256; i++) { | |
pre_buf[i + 256] = i; | |
pre_buf[i * 2 + 512] = i; | |
} | |
for (i = 0; i < 384; i++) { | |
uint8_t l = get_byte(opaque); | |
uint8_t b = get_byte(opaque); | |
pre_buf[l * 4 + (i & 3)] = b; | |
} | |
} | |
void gen_data(uint8_t * out, size_t out_size, uint8_t (*get_byte)(void *), void * opaque) { | |
gen_pre_buf(out, get_byte, opaque); | |
out += 1024; | |
while (1) { | |
uint8_t mask; | |
int i; | |
mask = get_byte(opaque); | |
for (i = 0; i < 8; i++) { | |
if (mask & 1) { | |
uint8_t b1, b2; | |
uint16_t n; | |
uint16_t offset; | |
uint8_t * ptr; | |
b1 = get_byte(opaque); | |
b2 = get_byte(opaque); | |
n = (b1 << 8) | b2; | |
offset = (n >> 6) + 1; | |
n &= 0x3f; | |
n += 3; | |
ptr = out - offset; | |
if (n > out_size) | |
n = out_size; | |
out_size -= n; | |
while (n--) { | |
*out++ = *ptr++; | |
} | |
} else { | |
*out++ = get_byte(opaque); | |
out_size--; | |
} | |
if (out_size == 0) | |
return; | |
mask >>= 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment