roundtrip.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * \file roundtrip.c
  3. * In this example we include \c zstd.h and compile separately the amalgamated
  4. * \c zstd.c:
  5. * \code
  6. * cc -Wall -Wextra -Werror -I. -Os -g0 zstd.c examples/roundtrip.c
  7. * \endcode
  8. *
  9. * \author Carl Woffenden, Numfum GmbH (released under a CC0 license)
  10. */
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "zstd.h"
  17. //************************** Test Data (DXT texture) **************************/
  18. /**
  19. * Raw test data (borrowed from the Emscripten example).
  20. * \n
  21. * See \c testcard.png for the original.
  22. */
  23. static uint8_t const rawData[] = {
  24. #include "testcard-dxt1.inl"
  25. };
  26. #ifndef ZSTD_VERSION_MAJOR
  27. /*
  28. * For the case where the decompression library hasn't been included we add
  29. * dummy functions to fake the process and stop the buffers being optimised out.
  30. */
  31. size_t ZSTD_compressBound(size_t maxSrc) {
  32. return maxSrc + 32;
  33. }
  34. int ZSTD_maxCLevel(void) {
  35. return 20;
  36. }
  37. size_t ZSTD_compress(void* dst, size_t dstLen, const void* src, size_t srcLen, int level) {
  38. return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? level : dstLen;
  39. }
  40. unsigned ZSTD_isError(size_t code) {
  41. return ((int) code) < 0;
  42. }
  43. size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) {
  44. return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? 0 : dstLen;
  45. }
  46. #endif
  47. //*****************************************************************************/
  48. /**
  49. * Simple single-file test to compress \c rawData, decompress the result, then
  50. * compare the decompressed version with the original.
  51. */
  52. int main() {
  53. size_t bounds = ZSTD_compressBound(sizeof rawData);
  54. void* compBuf = malloc(bounds);
  55. void* testBuf = malloc(sizeof rawData);
  56. int compare = -1;
  57. if (compBuf && testBuf) {
  58. size_t compSize = ZSTD_compress(compBuf, bounds, rawData, sizeof rawData, ZSTD_maxCLevel());
  59. if (!ZSTD_isError(compSize)) {
  60. printf("Compression: PASSED (size: %lu, uncompressed: %lu)\n", (unsigned long) compSize, (unsigned long) (sizeof rawData));
  61. size_t decSize = ZSTD_decompress(testBuf, sizeof rawData, compBuf, compSize);
  62. if (!ZSTD_isError(decSize)) {
  63. printf("Decompression: PASSED\n");
  64. compare = memcmp(rawData, testBuf, decSize);
  65. printf("Byte comparison: %s\n", (compare == 0) ? "PASSED" : "FAILED");
  66. } else {
  67. printf("Decompression: FAILED\n");
  68. }
  69. } else {
  70. printf("Compression: FAILED\n");
  71. }
  72. free(compBuf);
  73. free(testBuf);
  74. }
  75. return (compare == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  76. }