2 * Copyright (c) 2017-present, Yann Collet, Facebook, Inc.
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).
8 * You may select, at your option, one of the above-listed licenses.
17 #define ZSTD_STATIC_LINKING_ONLY
21 compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size)
23 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);
27 if (ZSTD_isError(rc)) return 1;
29 { ZSTD_outBuffer tmp = out;
30 const size_t rc = ZSTD_flushStream(ctx, &tmp);
31 if (rc != 0) { return 1; }
36 int main(int argc, const char** argv)
39 ZSTD_parameters params;
45 ctx = ZSTD_createCStream();
46 if (!ctx) { return 1; }
48 memset(¶ms, 0, sizeof(params));
49 params.cParams.windowLog = 18;
50 params.cParams.chainLog = 13;
51 params.cParams.hashLog = 14;
52 params.cParams.searchLog = 1;
53 params.cParams.minMatch = 7;
54 params.cParams.targetLength = 16;
55 params.cParams.strategy = ZSTD_fast;
56 windowLog = params.cParams.windowLog;
57 /* Initialize stream */
58 rc = ZSTD_initCStream_advanced(ctx, NULL, 0, params, 0);
59 if (ZSTD_isError(rc)) { return 2; }
62 const U64 toCompress = ((U64)1) << 33;
63 const size_t size = 1 << windowLog;
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);
71 printf("\n === Long Match Test === \n");
72 printf("Creating random data to produce long matches \n");
73 for (i = 0; i < sizeof(match); ++i) {
74 srcBuffer[i] = match[i];
76 for (i = 0; i < randomData; ++i) {
77 srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
79 for (i = 0; i < sizeof(match); ++i) {
80 srcBuffer[sizeof(match) + randomData + i] = match[i];
82 printf("Compressing, trying to generate a segfault \n");
83 if (compress(ctx, out, srcBuffer, size)) {
87 while (compressed < toCompress) {
88 const size_t block = rand() % (size - pos + 1);
89 if (pos == size) { pos = 0; }
90 if (compress(ctx, out, srcBuffer + pos, block)) {
96 printf("Compression completed successfully (no error triggered)\n");