blob: 61b81b359a403a527d316bccb6d81cdbcb984ee4 [file] [log] [blame]
Nick Terrell0acae732016-12-10 19:12:13 -08001#include <stdio.h>
2#include <stddef.h>
3#include <stdlib.h>
Nick Terrell5cc85cf2016-12-10 19:31:55 -08004#include <stdint.h>
Yann Colletc261f712016-12-12 00:25:07 +01005#include "mem.h"
6#define ZSTD_STATIC_LINKING_ONLY
7#include "zstd.h"
Nick Terrell0acae732016-12-10 19:12:13 -08008
Nick Terrellb547d212016-12-10 23:17:36 -08009int compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size) {
Nick Terrell0acae732016-12-10 19:12:13 -080010 ZSTD_inBuffer in = { data, size, 0 };
11 while (in.pos < in.size) {
12 ZSTD_outBuffer tmp = out;
13 const size_t rc = ZSTD_compressStream(ctx, &tmp, &in);
14 if (ZSTD_isError(rc)) {
Nick Terrellb547d212016-12-10 23:17:36 -080015 return 1;
Nick Terrell0acae732016-12-10 19:12:13 -080016 }
17 }
Nick Terrellb547d212016-12-10 23:17:36 -080018 {
19 ZSTD_outBuffer tmp = out;
20 const size_t rc = ZSTD_flushStream(ctx, &tmp);
21 if (rc != 0) { return 1; }
22 }
23 return 0;
Nick Terrell0acae732016-12-10 19:12:13 -080024}
25
Nick Terrellb547d212016-12-10 23:17:36 -080026int main(int argc, const char** argv) {
Nick Terrell0acae732016-12-10 19:12:13 -080027 ZSTD_CStream *ctx;
Nick Terrellb547d212016-12-10 23:17:36 -080028 ZSTD_parameters params;
Nick Terrell0acae732016-12-10 19:12:13 -080029 size_t rc;
30 unsigned windowLog;
Nick Terrellb547d212016-12-10 23:17:36 -080031 (void)argc;
32 (void)argv;
Nick Terrell0acae732016-12-10 19:12:13 -080033 /* Create stream */
34 ctx = ZSTD_createCStream();
35 if (!ctx) { return 1; }
36 /* Set parameters */
Nick Terrellb547d212016-12-10 23:17:36 -080037 memset(&params, 0, sizeof(params));
Nick Terrell0acae732016-12-10 19:12:13 -080038 params.cParams.windowLog = 18;
39 params.cParams.chainLog = 13;
40 params.cParams.hashLog = 14;
41 params.cParams.searchLog = 1;
42 params.cParams.searchLength = 7;
43 params.cParams.targetLength = 16;
44 params.cParams.strategy = ZSTD_fast;
45 windowLog = params.cParams.windowLog;
46 /* Initialize stream */
47 rc = ZSTD_initCStream_advanced(ctx, NULL, 0, params, 0);
48 if (ZSTD_isError(rc)) { return 2; }
49 {
Nick Terrell5cc85cf2016-12-10 19:31:55 -080050 U64 compressed = 0;
51 const U64 toCompress = ((U64)1) << 33;
Nick Terrell0acae732016-12-10 19:12:13 -080052 const size_t size = 1 << windowLog;
53 size_t pos = 0;
54 char *srcBuffer = (char*) malloc(1 << windowLog);
55 char *dstBuffer = (char*) malloc(ZSTD_compressBound(1 << windowLog));
56 ZSTD_outBuffer out = { dstBuffer, ZSTD_compressBound(1 << windowLog), 0 };
57 const char match[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
58 const size_t randomData = (1 << windowLog) - 2*sizeof(match);
Nick Terrellb547d212016-12-10 23:17:36 -080059 size_t i;
Yann Colletc261f712016-12-12 00:25:07 +010060 printf("\n === Long Match Test === \n");
61 printf("Creating random data to produce long matches \n");
Nick Terrellb547d212016-12-10 23:17:36 -080062 for (i = 0; i < sizeof(match); ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080063 srcBuffer[i] = match[i];
64 }
Nick Terrellb547d212016-12-10 23:17:36 -080065 for (i = 0; i < randomData; ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080066 srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
67 }
Nick Terrellb547d212016-12-10 23:17:36 -080068 for (i = 0; i < sizeof(match); ++i) {
Nick Terrell0acae732016-12-10 19:12:13 -080069 srcBuffer[sizeof(match) + randomData + i] = match[i];
70 }
Yann Colletc261f712016-12-12 00:25:07 +010071 printf("Compressing, trying to generate a segfault \n");
Nick Terrellb547d212016-12-10 23:17:36 -080072 if (compress(ctx, out, srcBuffer, size)) {
73 return 1;
74 }
Nick Terrell0acae732016-12-10 19:12:13 -080075 compressed += size;
76 while (compressed < toCompress) {
77 const size_t block = rand() % (size - pos + 1);
78 if (pos == size) { pos = 0; }
Nick Terrellb547d212016-12-10 23:17:36 -080079 if (compress(ctx, out, srcBuffer + pos, block)) {
80 return 1;
81 }
Nick Terrell0acae732016-12-10 19:12:13 -080082 pos += block;
83 compressed += block;
84 }
Yann Colletc261f712016-12-12 00:25:07 +010085 printf("Compression completed successfully (no error triggered)\n");
Nick Terrell0acae732016-12-10 19:12:13 -080086 free(srcBuffer);
87 free(dstBuffer);
88 }
Nick Terrellb547d212016-12-10 23:17:36 -080089 return 0;
Nick Terrell0acae732016-12-10 19:12:13 -080090}