simple.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "../zstddeclib.c"
  18. //************************* Test Data (DXT texture) **************************/
  19. /**
  20. * Raw 256x256 DXT1 data (used to compare the result).
  21. * \n
  22. * See \c testcard.png for the original.
  23. */
  24. static uint8_t const rawDxt1[] = {
  25. #include "testcard-dxt1.inl"
  26. };
  27. /**
  28. * Zstd compressed version of \c #rawDxt1.
  29. * \n
  30. * See \c testcard.png for the original.
  31. */
  32. static uint8_t const srcZstd[] = {
  33. #include "testcard-zstd.inl"
  34. };
  35. /**
  36. * Destination for decoding \c #srcZstd.
  37. */
  38. static uint8_t dstDxt1[sizeof rawDxt1] = {};
  39. #ifndef ZSTD_VERSION_MAJOR
  40. /**
  41. * For the case where the decompression library hasn't been included we add a
  42. * dummy function to fake the process and stop the buffers being optimised out.
  43. */
  44. size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) {
  45. return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? 0 : dstLen;
  46. }
  47. #endif
  48. //****************************************************************************/
  49. /**
  50. * Simple single-file test to decompress \c #srcZstd into \c # dstDxt1 then
  51. * compare the resulting bytes with \c #rawDxt1.
  52. * \n
  53. * As a (naive) comparison, removing Zstd and building with "-Os -g0 simple.c"
  54. * results in a 44kB binary (macOS 10.14, Clang 10); re-adding Zstd increases
  55. * the binary by 56kB (after calling \c strip).
  56. */
  57. int main() {
  58. size_t size = ZSTD_decompress(dstDxt1, sizeof dstDxt1, srcZstd, sizeof srcZstd);
  59. int compare = memcmp(rawDxt1, dstDxt1, sizeof dstDxt1);
  60. printf("Decompressed size: %s\n", (size == sizeof dstDxt1) ? "PASSED" : "FAILED");
  61. printf("Byte comparison: %s\n", (compare == 0) ? "PASSED" : "FAILED");
  62. if (size == sizeof dstDxt1 && compare == 0) {
  63. return EXIT_SUCCESS;
  64. }
  65. return EXIT_FAILURE;
  66. }