2016-12-11 03:12:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-11 03:31:55 +00:00
|
|
|
#include <stdint.h>
|
2016-12-11 23:25:07 +00:00
|
|
|
#include "mem.h"
|
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
|
|
|
#include "zstd.h"
|
2016-12-11 03:12:13 +00:00
|
|
|
|
2016-12-11 07:17:36 +00:00
|
|
|
int compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size) {
|
2016-12-11 03:12:13 +00:00
|
|
|
ZSTD_inBuffer in = { data, size, 0 };
|
|
|
|
while (in.pos < in.size) {
|
|
|
|
ZSTD_outBuffer tmp = out;
|
|
|
|
const size_t rc = ZSTD_compressStream(ctx, &tmp, &in);
|
|
|
|
if (ZSTD_isError(rc)) {
|
2016-12-11 07:17:36 +00:00
|
|
|
return 1;
|
2016-12-11 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 07:17:36 +00:00
|
|
|
{
|
|
|
|
ZSTD_outBuffer tmp = out;
|
|
|
|
const size_t rc = ZSTD_flushStream(ctx, &tmp);
|
|
|
|
if (rc != 0) { return 1; }
|
|
|
|
}
|
|
|
|
return 0;
|
2016-12-11 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 07:17:36 +00:00
|
|
|
int main(int argc, const char** argv) {
|
2016-12-11 03:12:13 +00:00
|
|
|
ZSTD_CStream *ctx;
|
2016-12-11 07:17:36 +00:00
|
|
|
ZSTD_parameters params;
|
2016-12-11 03:12:13 +00:00
|
|
|
size_t rc;
|
|
|
|
unsigned windowLog;
|
2016-12-11 07:17:36 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2016-12-11 03:12:13 +00:00
|
|
|
/* Create stream */
|
|
|
|
ctx = ZSTD_createCStream();
|
|
|
|
if (!ctx) { return 1; }
|
|
|
|
/* Set parameters */
|
2016-12-11 07:17:36 +00:00
|
|
|
memset(¶ms, 0, sizeof(params));
|
2016-12-11 03:12:13 +00:00
|
|
|
params.cParams.windowLog = 18;
|
|
|
|
params.cParams.chainLog = 13;
|
|
|
|
params.cParams.hashLog = 14;
|
|
|
|
params.cParams.searchLog = 1;
|
|
|
|
params.cParams.searchLength = 7;
|
|
|
|
params.cParams.targetLength = 16;
|
|
|
|
params.cParams.strategy = ZSTD_fast;
|
|
|
|
windowLog = params.cParams.windowLog;
|
|
|
|
/* Initialize stream */
|
|
|
|
rc = ZSTD_initCStream_advanced(ctx, NULL, 0, params, 0);
|
|
|
|
if (ZSTD_isError(rc)) { return 2; }
|
|
|
|
{
|
2016-12-11 03:31:55 +00:00
|
|
|
U64 compressed = 0;
|
|
|
|
const U64 toCompress = ((U64)1) << 33;
|
2016-12-11 03:12:13 +00:00
|
|
|
const size_t size = 1 << windowLog;
|
|
|
|
size_t pos = 0;
|
|
|
|
char *srcBuffer = (char*) malloc(1 << windowLog);
|
|
|
|
char *dstBuffer = (char*) malloc(ZSTD_compressBound(1 << windowLog));
|
|
|
|
ZSTD_outBuffer out = { dstBuffer, ZSTD_compressBound(1 << windowLog), 0 };
|
|
|
|
const char match[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
const size_t randomData = (1 << windowLog) - 2*sizeof(match);
|
2016-12-11 07:17:36 +00:00
|
|
|
size_t i;
|
2016-12-11 23:25:07 +00:00
|
|
|
printf("\n === Long Match Test === \n");
|
|
|
|
printf("Creating random data to produce long matches \n");
|
2016-12-11 07:17:36 +00:00
|
|
|
for (i = 0; i < sizeof(match); ++i) {
|
2016-12-11 03:12:13 +00:00
|
|
|
srcBuffer[i] = match[i];
|
|
|
|
}
|
2016-12-11 07:17:36 +00:00
|
|
|
for (i = 0; i < randomData; ++i) {
|
2016-12-11 03:12:13 +00:00
|
|
|
srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
|
|
|
|
}
|
2016-12-11 07:17:36 +00:00
|
|
|
for (i = 0; i < sizeof(match); ++i) {
|
2016-12-11 03:12:13 +00:00
|
|
|
srcBuffer[sizeof(match) + randomData + i] = match[i];
|
|
|
|
}
|
2016-12-11 23:25:07 +00:00
|
|
|
printf("Compressing, trying to generate a segfault \n");
|
2016-12-11 07:17:36 +00:00
|
|
|
if (compress(ctx, out, srcBuffer, size)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-11 03:12:13 +00:00
|
|
|
compressed += size;
|
|
|
|
while (compressed < toCompress) {
|
|
|
|
const size_t block = rand() % (size - pos + 1);
|
|
|
|
if (pos == size) { pos = 0; }
|
2016-12-11 07:17:36 +00:00
|
|
|
if (compress(ctx, out, srcBuffer + pos, block)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-11 03:12:13 +00:00
|
|
|
pos += block;
|
|
|
|
compressed += block;
|
|
|
|
}
|
2016-12-11 23:25:07 +00:00
|
|
|
printf("Compression completed successfully (no error triggered)\n");
|
2016-12-11 03:12:13 +00:00
|
|
|
free(srcBuffer);
|
|
|
|
free(dstBuffer);
|
|
|
|
}
|
2016-12-11 07:17:36 +00:00
|
|
|
return 0;
|
2016-12-11 03:12:13 +00:00
|
|
|
}
|