3 * Simple standalone example of using the single-file \c zstddeclib.
5 * \note In this simple example we include the amalgamated source and compile
6 * just this single file, but we could equally (and more conventionally)
7 * include \c zstd.h and compile both this file and \c zstddeclib.c (the
8 * resulting binaries differ slightly in size but perform the same).
10 * \author Carl Woffenden, Numfum GmbH (released under a CC0 license)
19 #include "../zstddeclib.c"
21 //************************* Test Data (DXT texture) **************************/
24 * Raw 256x256 DXT1 data (used to compare the result).
26 * See \c testcard.png for the original.
28 static uint8_t const rawDxt1[] = {
29 #include "testcard-dxt1.inl"
33 * Zstd compressed version of \c #rawDxt1.
35 * See \c testcard.png for the original.
37 static uint8_t const srcZstd[] = {
38 #include "testcard-zstd.inl"
42 * Destination for decoding \c #srcZstd.
44 static uint8_t dstDxt1[sizeof rawDxt1] = {};
46 #ifndef ZSTD_VERSION_MAJOR
48 * For the case where the decompression library hasn't been included we add a
49 * dummy function to fake the process and stop the buffers being optimised out.
51 size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) {
52 return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? 0 : dstLen;
56 //****************************************************************************/
59 * Simple single-file test to decompress \c #srcZstd into \c # dstDxt1 then
60 * compare the resulting bytes with \c #rawDxt1.
62 * As a (naive) comparison, removing Zstd and building with "-Os -g0 simple.c"
63 * results in a 44kB binary (macOS 10.14, Clang 10); re-adding Zstd increases
64 * the binary by 56kB (after calling \c strip).
67 size_t size = ZSTD_decompress(dstDxt1, sizeof dstDxt1, srcZstd, sizeof srcZstd);
68 int compare = memcmp(rawDxt1, dstDxt1, sizeof dstDxt1);
69 printf("Decompressed size: %s\n", (size == sizeof dstDxt1) ? "PASSED" : "FAILED");
70 printf("Byte comparison: %s\n", (compare == 0) ? "PASSED" : "FAILED");
71 if (size == sizeof dstDxt1 && compare == 0) {