1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang * Copyright (c) 2017-2020, Yann Collet, Facebook, Inc.
3*22ce4affSfengbojiang * All rights reserved.
4*22ce4affSfengbojiang *
5*22ce4affSfengbojiang * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang */
10*22ce4affSfengbojiang
11*22ce4affSfengbojiang
12*22ce4affSfengbojiang /*=== Tuning parameter ===*/
13*22ce4affSfengbojiang #ifndef MAX_TESTED_LEVEL
14*22ce4affSfengbojiang #define MAX_TESTED_LEVEL 12
15*22ce4affSfengbojiang #endif
16*22ce4affSfengbojiang
17*22ce4affSfengbojiang
18*22ce4affSfengbojiang /*=== Dependencies ===*/
19*22ce4affSfengbojiang #include <stdio.h> // printf
20*22ce4affSfengbojiang #define ZSTD_STATIC_LINKING_ONLY
21*22ce4affSfengbojiang #include <zstd.h> // presumes zstd library is installed
22*22ce4affSfengbojiang #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
23*22ce4affSfengbojiang
24*22ce4affSfengbojiang
25*22ce4affSfengbojiang /*=== functions ===*/
26*22ce4affSfengbojiang
27*22ce4affSfengbojiang /*! readU32FromChar() :
28*22ce4affSfengbojiang @return : unsigned integer value read from input in `char` format
29*22ce4affSfengbojiang allows and interprets K, KB, KiB, M, MB and MiB suffix.
30*22ce4affSfengbojiang Will also modify `*stringPtr`, advancing it to position where it stopped reading.
31*22ce4affSfengbojiang Note : function result can overflow if digit string > MAX_UINT */
readU32FromChar(const char ** stringPtr)32*22ce4affSfengbojiang static unsigned readU32FromChar(const char** stringPtr)
33*22ce4affSfengbojiang {
34*22ce4affSfengbojiang unsigned result = 0;
35*22ce4affSfengbojiang while ((**stringPtr >='0') && (**stringPtr <='9'))
36*22ce4affSfengbojiang result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
37*22ce4affSfengbojiang if ((**stringPtr=='K') || (**stringPtr=='M')) {
38*22ce4affSfengbojiang result <<= 10;
39*22ce4affSfengbojiang if (**stringPtr=='M') result <<= 10;
40*22ce4affSfengbojiang (*stringPtr)++ ;
41*22ce4affSfengbojiang if (**stringPtr=='i') (*stringPtr)++;
42*22ce4affSfengbojiang if (**stringPtr=='B') (*stringPtr)++;
43*22ce4affSfengbojiang }
44*22ce4affSfengbojiang return result;
45*22ce4affSfengbojiang }
46*22ce4affSfengbojiang
47*22ce4affSfengbojiang
main(int argc,char const * argv[])48*22ce4affSfengbojiang int main(int argc, char const *argv[]) {
49*22ce4affSfengbojiang
50*22ce4affSfengbojiang printf("\n Zstandard (v%s) memory usage for streaming : \n\n", ZSTD_versionString());
51*22ce4affSfengbojiang
52*22ce4affSfengbojiang unsigned wLog = 0;
53*22ce4affSfengbojiang if (argc > 1) {
54*22ce4affSfengbojiang const char* valStr = argv[1];
55*22ce4affSfengbojiang wLog = readU32FromChar(&valStr);
56*22ce4affSfengbojiang }
57*22ce4affSfengbojiang
58*22ce4affSfengbojiang int compressionLevel;
59*22ce4affSfengbojiang for (compressionLevel = 1; compressionLevel <= MAX_TESTED_LEVEL; compressionLevel++) {
60*22ce4affSfengbojiang #define INPUT_SIZE 5
61*22ce4affSfengbojiang #define COMPRESSED_SIZE 128
62*22ce4affSfengbojiang char const dataToCompress[INPUT_SIZE] = "abcde";
63*22ce4affSfengbojiang char compressedData[COMPRESSED_SIZE];
64*22ce4affSfengbojiang char decompressedData[INPUT_SIZE];
65*22ce4affSfengbojiang /* the ZSTD_CCtx_params structure is a way to save parameters and use
66*22ce4affSfengbojiang * them across multiple contexts. We use them here so we can call the
67*22ce4affSfengbojiang * function ZSTD_estimateCStreamSize_usingCCtxParams().
68*22ce4affSfengbojiang */
69*22ce4affSfengbojiang ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams();
70*22ce4affSfengbojiang CHECK(cctxParams != NULL, "ZSTD_createCCtxParams() failed!");
71*22ce4affSfengbojiang
72*22ce4affSfengbojiang /* Set the compression level. */
73*22ce4affSfengbojiang CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_compressionLevel, compressionLevel) );
74*22ce4affSfengbojiang /* Set the window log.
75*22ce4affSfengbojiang * The value 0 means use the default window log, which is equivalent to
76*22ce4affSfengbojiang * not setting it.
77*22ce4affSfengbojiang */
78*22ce4affSfengbojiang CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, wLog) );
79*22ce4affSfengbojiang
80*22ce4affSfengbojiang /* Force the compressor to allocate the maximum memory size for a given
81*22ce4affSfengbojiang * level by not providing the pledged source size, or calling
82*22ce4affSfengbojiang * ZSTD_compressStream2() with ZSTD_e_end.
83*22ce4affSfengbojiang */
84*22ce4affSfengbojiang ZSTD_CCtx* const cctx = ZSTD_createCCtx();
85*22ce4affSfengbojiang CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
86*22ce4affSfengbojiang CHECK_ZSTD( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) );
87*22ce4affSfengbojiang size_t compressedSize;
88*22ce4affSfengbojiang {
89*22ce4affSfengbojiang ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
90*22ce4affSfengbojiang ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
91*22ce4affSfengbojiang CHECK_ZSTD( ZSTD_compressStream(cctx, &outBuff, &inBuff) );
92*22ce4affSfengbojiang size_t const remaining = ZSTD_endStream(cctx, &outBuff);
93*22ce4affSfengbojiang CHECK_ZSTD(remaining);
94*22ce4affSfengbojiang CHECK(remaining == 0, "Frame not flushed!");
95*22ce4affSfengbojiang compressedSize = outBuff.pos;
96*22ce4affSfengbojiang }
97*22ce4affSfengbojiang
98*22ce4affSfengbojiang ZSTD_DCtx* const dctx = ZSTD_createDCtx();
99*22ce4affSfengbojiang CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
100*22ce4affSfengbojiang /* Set the maximum allowed window log.
101*22ce4affSfengbojiang * The value 0 means use the default window log, which is equivalent to
102*22ce4affSfengbojiang * not setting it.
103*22ce4affSfengbojiang */
104*22ce4affSfengbojiang CHECK_ZSTD( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, wLog) );
105*22ce4affSfengbojiang /* forces decompressor to use maximum memory size, since the
106*22ce4affSfengbojiang * decompressed size is not stored in the frame header.
107*22ce4affSfengbojiang */
108*22ce4affSfengbojiang { ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
109*22ce4affSfengbojiang ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
110*22ce4affSfengbojiang size_t const remaining = ZSTD_decompressStream(dctx, &outBuff, &inBuff);
111*22ce4affSfengbojiang CHECK_ZSTD(remaining);
112*22ce4affSfengbojiang CHECK(remaining == 0, "Frame not complete!");
113*22ce4affSfengbojiang CHECK(outBuff.pos == sizeof(dataToCompress), "Bad decompression!");
114*22ce4affSfengbojiang }
115*22ce4affSfengbojiang
116*22ce4affSfengbojiang size_t const cstreamSize = ZSTD_sizeof_CStream(cctx);
117*22ce4affSfengbojiang size_t const cstreamEstimatedSize = ZSTD_estimateCStreamSize_usingCCtxParams(cctxParams);
118*22ce4affSfengbojiang size_t const dstreamSize = ZSTD_sizeof_DStream(dctx);
119*22ce4affSfengbojiang size_t const dstreamEstimatedSize = ZSTD_estimateDStreamSize_fromFrame(compressedData, compressedSize);
120*22ce4affSfengbojiang
121*22ce4affSfengbojiang CHECK(cstreamSize <= cstreamEstimatedSize, "Compression mem (%u) > estimated (%u)",
122*22ce4affSfengbojiang (unsigned)cstreamSize, (unsigned)cstreamEstimatedSize);
123*22ce4affSfengbojiang CHECK(dstreamSize <= dstreamEstimatedSize, "Decompression mem (%u) > estimated (%u)",
124*22ce4affSfengbojiang (unsigned)dstreamSize, (unsigned)dstreamEstimatedSize);
125*22ce4affSfengbojiang
126*22ce4affSfengbojiang printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB (estimated : %5u KB)\n",
127*22ce4affSfengbojiang compressionLevel,
128*22ce4affSfengbojiang (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10),
129*22ce4affSfengbojiang (unsigned)(dstreamSize>>10), (unsigned)(dstreamEstimatedSize>>10));
130*22ce4affSfengbojiang
131*22ce4affSfengbojiang ZSTD_freeDCtx(dctx);
132*22ce4affSfengbojiang ZSTD_freeCCtx(cctx);
133*22ce4affSfengbojiang ZSTD_freeCCtxParams(cctxParams);
134*22ce4affSfengbojiang if (wLog) break; /* single test */
135*22ce4affSfengbojiang }
136*22ce4affSfengbojiang return 0;
137*22ce4affSfengbojiang }
138