1 /** 2 * \file simple.c 3 * Simple standalone example of using the single-file \c zstddeclib. 4 * 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). 9 * 10 * \author Carl Woffenden, Numfum GmbH (released under a CC0 license) 11 */ 12 13 #include <stddef.h> 14 #include <stdint.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #include "../zstddeclib.c" 20 21 //************************* Test Data (DXT texture) **************************/ 22 23 /** 24 * Raw 256x256 DXT1 data (used to compare the result). 25 * \n 26 * See \c testcard.png for the original. 27 */ 28 static uint8_t const rawDxt1[] = { 29 #include "testcard-dxt1.inl" 30 }; 31 32 /** 33 * Zstd compressed version of \c #rawDxt1. 34 * \n 35 * See \c testcard.png for the original. 36 */ 37 static uint8_t const srcZstd[] = { 38 #include "testcard-zstd.inl" 39 }; 40 41 /** 42 * Destination for decoding \c #srcZstd. 43 */ 44 static uint8_t dstDxt1[sizeof rawDxt1] = {}; 45 46 #ifndef ZSTD_VERSION_MAJOR 47 /** 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. 50 */ 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; 53 } 54 #endif 55 56 //****************************************************************************/ 57 58 /** 59 * Simple single-file test to decompress \c #srcZstd into \c # dstDxt1 then 60 * compare the resulting bytes with \c #rawDxt1. 61 * \n 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). 65 */ 66 int main() { 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) { 72 return EXIT_SUCCESS; 73 } 74 return EXIT_FAILURE; 75 } 76