zstd/tests/longmatch.c

102 lines
3.0 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) Yann Collet, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
2016-12-10 19:12:13 -08:00
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
2016-12-10 19:31:55 -08:00
#include <stdint.h>
2016-12-11 15:25:07 -08:00
#include "mem.h"
#define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h"
2016-12-10 19:12:13 -08:00
2018-09-27 18:24:41 -07:00
static int
compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size)
{
2016-12-10 19:12:13 -08: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);
2018-09-27 18:24:41 -07:00
if (ZSTD_isError(rc)) return 1;
2016-12-10 19:12:13 -08:00
}
2018-09-27 18:24:41 -07:00
{ ZSTD_outBuffer tmp = out;
2016-12-10 23:17:36 -08:00
const size_t rc = ZSTD_flushStream(ctx, &tmp);
if (rc != 0) { return 1; }
}
return 0;
2016-12-10 19:12:13 -08:00
}
2018-09-27 18:24:41 -07:00
int main(int argc, const char** argv)
{
ZSTD_CStream* ctx;
2016-12-10 23:17:36 -08:00
ZSTD_parameters params;
2016-12-10 19:12:13 -08:00
size_t rc;
unsigned windowLog;
2016-12-10 23:17:36 -08:00
(void)argc;
(void)argv;
2016-12-10 19:12:13 -08:00
/* Create stream */
ctx = ZSTD_createCStream();
if (!ctx) { return 1; }
/* Set parameters */
2016-12-10 23:17:36 -08:00
memset(&params, 0, sizeof(params));
2016-12-10 19:12:13 -08:00
params.cParams.windowLog = 18;
params.cParams.chainLog = 13;
params.cParams.hashLog = 14;
params.cParams.searchLog = 1;
params.cParams.minMatch = 7;
2016-12-10 19:12:13 -08:00
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-10 19:31:55 -08:00
U64 compressed = 0;
const U64 toCompress = ((U64)1) << 33;
2016-12-10 19:12:13 -08: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-10 23:17:36 -08:00
size_t i;
2016-12-11 15:25:07 -08:00
printf("\n === Long Match Test === \n");
printf("Creating random data to produce long matches \n");
2016-12-10 23:17:36 -08:00
for (i = 0; i < sizeof(match); ++i) {
2016-12-10 19:12:13 -08:00
srcBuffer[i] = match[i];
}
2016-12-10 23:17:36 -08:00
for (i = 0; i < randomData; ++i) {
2016-12-10 19:12:13 -08:00
srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
}
2016-12-10 23:17:36 -08:00
for (i = 0; i < sizeof(match); ++i) {
2016-12-10 19:12:13 -08:00
srcBuffer[sizeof(match) + randomData + i] = match[i];
}
2016-12-11 15:25:07 -08:00
printf("Compressing, trying to generate a segfault \n");
2016-12-10 23:17:36 -08:00
if (compress(ctx, out, srcBuffer, size)) {
return 1;
}
2016-12-10 19:12:13 -08:00
compressed += size;
while (compressed < toCompress) {
const size_t block = rand() % (size - pos + 1);
if (pos == size) { pos = 0; }
2016-12-10 23:17:36 -08:00
if (compress(ctx, out, srcBuffer + pos, block)) {
return 1;
}
2016-12-10 19:12:13 -08:00
pos += block;
compressed += block;
}
2016-12-11 15:25:07 -08:00
printf("Compression completed successfully (no error triggered)\n");
2016-12-10 19:12:13 -08:00
free(srcBuffer);
free(dstBuffer);
}
2016-12-10 23:17:36 -08:00
return 0;
2016-12-10 19:12:13 -08:00
}