2 * Copyright (c) Yann Collet, Meta Platforms, 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.
16 #define ZSTD_STATIC_LINKING_ONLY
18 #include "zstd_errors.h"
19 #include "sequence_producer.h" // simpleSequenceProducer
23 if (ZSTD_isError(res)) { \
24 printf("ERROR: %s\n", ZSTD_getErrorName(res)); \
29 int main(int argc, char *argv[]) {
31 printf("Usage: externalSequenceProducer <file>\n");
35 ZSTD_CCtx* const zc = ZSTD_createCCtx();
37 int simpleSequenceProducerState = 0xdeadbeef;
39 // Here is the crucial bit of code!
40 ZSTD_registerSequenceProducer(
42 &simpleSequenceProducerState,
43 simpleSequenceProducer
47 size_t const res = ZSTD_CCtx_setParameter(zc, ZSTD_c_enableSeqProducerFallback, 1);
51 FILE *f = fopen(argv[1], "rb");
54 int const ret = fseek(f, 0, SEEK_END);
57 size_t const srcSize = ftell(f);
59 int const ret = fseek(f, 0, SEEK_SET);
63 char* const src = malloc(srcSize + 1);
66 size_t const ret = fread(src, srcSize, 1, f);
68 int const ret2 = fclose(f);
72 size_t const dstSize = ZSTD_compressBound(srcSize);
73 char* const dst = malloc(dstSize);
76 size_t const cSize = ZSTD_compress2(zc, dst, dstSize, src, srcSize);
79 char* const val = malloc(srcSize);
83 size_t const res = ZSTD_decompress(val, srcSize, dst, cSize);
87 if (memcmp(src, val, srcSize) == 0) {
88 printf("Compression and decompression were successful!\n");
89 printf("Original size: %lu\n", srcSize);
90 printf("Compressed size: %lu\n", cSize);
92 printf("ERROR: input and validation buffers don't match!\n");
93 for (size_t i = 0; i < srcSize; i++) {
94 if (src[i] != val[i]) {
95 printf("First bad index: %zu\n", i);