blob: 547b26190106381fa9f383456ec7585855f49831 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet32fb4072017-08-18 16:52:05 -07003 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet32fb4072017-08-18 16:52:05 -07009 */
10
11
Nick Terrell0acae732016-12-10 19:12:13 -080012#include <stdio.h>
13#include <stddef.h>
14#include <stdlib.h>
Nick Terrell5cc85cf2016-12-10 19:31:55 -080015#include <stdint.h>
Yann Colletc261f712016-12-12 00:25:07 +010016#include "mem.h"
17#define ZSTD_STATIC_LINKING_ONLY
18#include "zstd.h"
Nick Terrell0acae732016-12-10 19:12:13 -080019
Yann Colletff365132018-09-27 18:24:41 -070020static int
21compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size)
22{
Nick Terrell0acae732016-12-10 19:12:13 -080023 ZSTD_inBuffer in = { data, size, 0 };
24 while (in.pos < in.size) {
25 ZSTD_outBuffer tmp = out;
26 const size_t rc = ZSTD_compressStream(ctx, &tmp, &in);
Yann Colletff365132018-09-27 18:24:41 -070027 if (ZSTD_isError(rc)) return 1;
Nick Terrell0acae732016-12-10 19:12:13 -080028 }
Yann Colletff365132018-09-27 18:24:41 -070029 { ZSTD_outBuffer tmp = out;
Nick Terrellb547d212016-12-10 23:17:36 -080030 const size_t rc = ZSTD_flushStream(ctx, &tmp);
31 if (rc != 0) { return 1; }
32 }
33 return 0;
Nick Terrell0acae732016-12-10 19:12:13 -080034}
35
Yann Colletff365132018-09-27 18:24:41 -070036int main(int argc, const char** argv)
37{
38 ZSTD_CStream* ctx;
Elliott Hughes44aba642023-09-12 20:18:59 +000039 unsigned windowLog = 18;
Nick Terrellb547d212016-12-10 23:17:36 -080040 (void)argc;
41 (void)argv;
Nick Terrell0acae732016-12-10 19:12:13 -080042 /* Create stream */
Elliott Hughes44aba642023-09-12 20:18:59 +000043 ctx = ZSTD_createCCtx();
Nick Terrell0acae732016-12-10 19:12:13 -080044 if (!ctx) { return 1; }
45 /* Set parameters */
Elliott Hughes44aba642023-09-12 20:18:59 +000046 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, windowLog)))
47 return 2;
48 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, 13)))
49 return 2;
50 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, 14)))
51 return 2;
52 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, 1)))
53 return 2;
54 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, 7)))
55 return 2;
56 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, 16)))
57 return 2;
58 if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, ZSTD_fast)))
59 return 2;
Nick Terrell0acae732016-12-10 19:12:13 -080060 {
Nick Terrell5cc85cf2016-12-10 19:31:55 -080061 U64 compressed = 0;
62 const U64 toCompress = ((U64)1) << 33;
Nick Terrell0acae732016-12-10 19:12:13 -080063 const size_t size = 1 << windowLog;
64 size_t pos = 0;
65 char *srcBuffer = (char*) malloc(1 << windowLog);
66 char *dstBuffer = (char*) malloc(ZSTD_compressBound(1 << windowLog));
67 ZSTD_outBuffer out = { dstBuffer, ZSTD_compressBound(1 << windowLog), 0 };
68 const char match[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
69 const size_t randomData = (1 << windowLog) - 2*sizeof(match);
Nick Terrellb547d212016-12-10 23:17:36 -080070 size_t i;
Yann Colletc261f712016-12-12 00:25:07 +010071 printf("\n === Long Match Test === \n");
72 printf("Creating random data to produce long matches \n");
Nick Terrellb547d212016-12-10 23:17:36 -080073 for (i = 0; i < sizeof(match); ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080074 srcBuffer[i] = match[i];
75 }
Nick Terrellb547d212016-12-10 23:17:36 -080076 for (i = 0; i < randomData; ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080077 srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
78 }
Nick Terrellb547d212016-12-10 23:17:36 -080079 for (i = 0; i < sizeof(match); ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080080 srcBuffer[sizeof(match) + randomData + i] = match[i];
81 }
Yann Colletc261f712016-12-12 00:25:07 +010082 printf("Compressing, trying to generate a segfault \n");
Nick Terrellb547d212016-12-10 23:17:36 -080083 if (compress(ctx, out, srcBuffer, size)) {
84 return 1;
85 }
Nick Terrell0acae732016-12-10 19:12:13 -080086 compressed += size;
87 while (compressed < toCompress) {
88 const size_t block = rand() % (size - pos + 1);
89 if (pos == size) { pos = 0; }
Nick Terrellb547d212016-12-10 23:17:36 -080090 if (compress(ctx, out, srcBuffer + pos, block)) {
91 return 1;
92 }
Nick Terrell0acae732016-12-10 19:12:13 -080093 pos += block;
94 compressed += block;
95 }
Yann Colletc261f712016-12-12 00:25:07 +010096 printf("Compression completed successfully (no error triggered)\n");
Nick Terrell0acae732016-12-10 19:12:13 -080097 free(srcBuffer);
98 free(dstBuffer);
99 }
Elliott Hughes44aba642023-09-12 20:18:59 +0000100 ZSTD_freeCCtx(ctx);
Nick Terrellb547d212016-12-10 23:17:36 -0800101 return 0;
Nick Terrell0acae732016-12-10 19:12:13 -0800102}