Skip to content

Instantly share code, notes, and snippets.

@terrelln
Created December 10, 2016 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrelln/6b7c01740fc05b0bcf5b3572056c9869 to your computer and use it in GitHub Desktop.
Save terrelln/6b7c01740fc05b0bcf5b3572056c9869 to your computer and use it in GitHub Desktop.
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
void compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size) {
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)) {
exit(5);
}
}
#if 1
ZSTD_outBuffer tmp = out;
const size_t rc = ZSTD_flushStream(ctx, &tmp);
if (rc != 0) { exit(6); }
#endif
}
int main() {
ZSTD_CStream *ctx = ZSTD_createCStream();
if (!ctx) { return 1; }
ZSTD_parameters params = {};
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;
const unsigned windowLog = params.cParams.windowLog;
size_t rc = ZSTD_initCStream_advanced(ctx, NULL, 0, params, 0);
if (ZSTD_isError(rc)) { return 2; }
const char match[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const size_t randomData = (1 << windowLog) - 2*sizeof(match);
char *srcBuffer = (char*) malloc(1 << windowLog);
char *dstBuffer = (char*) malloc(ZSTD_compressBound(1 << windowLog));
ZSTD_outBuffer out = { dstBuffer, ZSTD_compressBound(1 << windowLog), 0 };
for (size_t i = 0; i < sizeof(match); ++i) {
srcBuffer[i] = match[i];
}
for (size_t i = 0; i < randomData; ++i) {
srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
}
for (size_t i = 0; i < sizeof(match); ++i) {
srcBuffer[sizeof(match) + randomData + i] = match[i];
}
const size_t size = 1 << windowLog;
compress(ctx, out, srcBuffer, size);
size_t pos = 0;
while (1) {
if (pos == size) { pos = 0; }
const size_t block = rand() % (size - pos + 1);
compress(ctx, out, srcBuffer + pos, block);
pos += block;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment