legacy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2016-2021, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /*
  11. This program uses hard-coded data compressed with Zstd legacy versions
  12. and tests that the API decompresses them correctly
  13. */
  14. /*===========================================
  15. * Dependencies
  16. *==========================================*/
  17. #include <stddef.h> /* size_t */
  18. #include <stdlib.h> /* malloc, free */
  19. #include <stdio.h> /* fprintf */
  20. #include <string.h> /* strlen */
  21. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_decompressBound */
  22. #include "zstd.h"
  23. #include "zstd_errors.h"
  24. /*===========================================
  25. * Macros
  26. *==========================================*/
  27. #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
  28. /*===========================================
  29. * Precompressed frames
  30. *==========================================*/
  31. const char* const COMPRESSED; /* content is at end of file */
  32. size_t const COMPRESSED_SIZE = 917;
  33. const char* const EXPECTED; /* content is at end of file */
  34. static int testSimpleAPI(void)
  35. {
  36. size_t const size = strlen(EXPECTED);
  37. char* const output = malloc(size);
  38. if (!output) {
  39. DISPLAY("ERROR: Not enough memory!\n");
  40. return 1;
  41. }
  42. {
  43. size_t const ret = ZSTD_decompress(output, size, COMPRESSED, COMPRESSED_SIZE);
  44. if (ZSTD_isError(ret)) {
  45. if (ret == ZSTD_error_prefix_unknown) {
  46. DISPLAY("ERROR: Invalid frame magic number, was this compiled "
  47. "without legacy support?\n");
  48. } else {
  49. DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret));
  50. }
  51. return 1;
  52. }
  53. if (ret != size) {
  54. DISPLAY("ERROR: Wrong decoded size\n");
  55. }
  56. }
  57. if (memcmp(EXPECTED, output, size) != 0) {
  58. DISPLAY("ERROR: Wrong decoded output produced\n");
  59. return 1;
  60. }
  61. free(output);
  62. DISPLAY("Simple API OK\n");
  63. return 0;
  64. }
  65. static int testStreamingAPI(void)
  66. {
  67. int error_code = 0;
  68. size_t const outBuffSize = ZSTD_DStreamOutSize();
  69. char* const outBuff = malloc(outBuffSize);
  70. ZSTD_DStream* const stream = ZSTD_createDStream();
  71. ZSTD_inBuffer input = { COMPRESSED, COMPRESSED_SIZE, 0 };
  72. size_t outputPos = 0;
  73. int needsInit = 1;
  74. if (outBuff == NULL) {
  75. DISPLAY("ERROR: Could not allocate memory\n");
  76. return 1;
  77. }
  78. if (stream == NULL) {
  79. DISPLAY("ERROR: Could not create dstream\n");
  80. free(outBuff);
  81. return 1;
  82. }
  83. while (1) {
  84. ZSTD_outBuffer output = {outBuff, outBuffSize, 0};
  85. if (needsInit) {
  86. size_t const ret = ZSTD_initDStream(stream);
  87. if (ZSTD_isError(ret)) {
  88. DISPLAY("ERROR: ZSTD_initDStream: %s\n", ZSTD_getErrorName(ret));
  89. error_code = 1;
  90. break;
  91. } }
  92. { size_t const ret = ZSTD_decompressStream(stream, &output, &input);
  93. if (ZSTD_isError(ret)) {
  94. DISPLAY("ERROR: ZSTD_decompressStream: %s\n", ZSTD_getErrorName(ret));
  95. error_code = 1;
  96. break;
  97. }
  98. if (ret == 0) {
  99. needsInit = 1;
  100. } }
  101. if (memcmp(outBuff, EXPECTED + outputPos, output.pos) != 0) {
  102. DISPLAY("ERROR: Wrong decoded output produced\n");
  103. error_code = 1;
  104. break;
  105. }
  106. outputPos += output.pos;
  107. if (input.pos == input.size && output.pos < output.size) {
  108. break;
  109. }
  110. }
  111. free(outBuff);
  112. ZSTD_freeDStream(stream);
  113. if (error_code == 0) DISPLAY("Streaming API OK\n");
  114. return error_code;
  115. }
  116. static int testFrameDecoding(void)
  117. {
  118. if (strlen(EXPECTED) > ZSTD_decompressBound(COMPRESSED, COMPRESSED_SIZE)) {
  119. DISPLAY("ERROR: ZSTD_decompressBound: decompressed bound too small\n");
  120. return 1;
  121. }
  122. { const char* ip = COMPRESSED;
  123. size_t remainingSize = COMPRESSED_SIZE;
  124. while (1) {
  125. size_t frameSize = ZSTD_findFrameCompressedSize(ip, remainingSize);
  126. if (ZSTD_isError(frameSize)) {
  127. DISPLAY("ERROR: ZSTD_findFrameCompressedSize: %s\n", ZSTD_getErrorName(frameSize));
  128. return 1;
  129. }
  130. if (frameSize > remainingSize) {
  131. DISPLAY("ERROR: ZSTD_findFrameCompressedSize: expected frameSize to align with src buffer");
  132. return 1;
  133. }
  134. ip += frameSize;
  135. remainingSize -= frameSize;
  136. if (remainingSize == 0) break;
  137. }
  138. }
  139. DISPLAY("Frame Decoding OK\n");
  140. return 0;
  141. }
  142. int main(void)
  143. {
  144. { int const ret = testSimpleAPI();
  145. if (ret) return ret; }
  146. { int const ret = testStreamingAPI();
  147. if (ret) return ret; }
  148. { int const ret = testFrameDecoding();
  149. if (ret) return ret; }
  150. DISPLAY("OK\n");
  151. return 0;
  152. }
  153. /* Consists of the "EXPECTED" string compressed with default settings on
  154. - v0.4.3
  155. - v0.5.0
  156. - v0.6.0
  157. - v0.7.0
  158. - v0.8.0
  159. */
  160. const char* const COMPRESSED =
  161. "\x24\xB5\x2F\xFD\x00\x00\x00\xBB\xB0\x02\xC0\x10\x00\x1E\xB0\x01"
  162. "\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF\xE9\xF3\xEF\x53"
  163. "\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89\x17\x00\x18\x00"
  164. "\x18\x00\x3F\xE6\xE2\xE3\x74\xD6\xEC\xC9\x4A\xE0\x71\x71\x42\x3E"
  165. "\x64\x4F\x6A\x45\x4E\x78\xEC\x49\x03\x3F\xC6\x80\xAB\x8F\x75\x5E"
  166. "\x6F\x2E\x3E\x7E\xC6\xDC\x45\x69\x6C\xC5\xFD\xC7\x40\xB8\x84\x8A"
  167. "\x01\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B\x67"
  168. "\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\x19\x03\x01\x50\x67\x56\xF5\x9F"
  169. "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF"
  170. "\xC6\xBA\x01\x0E\x00\x54\x00\x00\x19\x00\x00\x54\x14\x00\x24\x24"
  171. "\x04\xFE\x04\x84\x4E\x41\x00\x27\xE2\x02\xC4\xB1\x00\xD2\x51\x00"
  172. "\x79\x58\x41\x28\x00\xE0\x0C\x01\x68\x65\x00\x04\x13\x0C\xDA\x0C"
  173. "\x80\x22\x06\xC0\x00\x00\x25\xB5\x2F\xFD\x00\x00\x00\xAD\x12\xB0"
  174. "\x7D\x1E\xB0\x01\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF"
  175. "\xE9\xF3\xEF\x53\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89"
  176. "\x03\x01\x50\x67\x56\xF5\x9F\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC"
  177. "\xAB\xAB\xE0\xE2\x81\xFA\xCF\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C"
  178. "\x64\xF8\xEB\x53\xE6\x18\x0B\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A"
  179. "\xF9\x63\x0C\xB8\xFA\x58\xE7\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6"
  180. "\x56\xDC\x7F\x0C\x84\x4B\xA8\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC"
  181. "\x04\x1E\x17\x27\xE4\x43\xF6\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00"
  182. "\x00\x32\x40\x80\xA8\x00\x01\x49\x81\xE0\x3C\x01\x29\x1D\x00\x87"
  183. "\xCE\x80\x75\x08\x80\x72\x24\x00\x7B\x52\x00\x94\x00\x20\xCC\x01"
  184. "\x86\xD2\x00\x81\x09\x83\xC1\x34\xA0\x88\x01\xC0\x00\x00\x26\xB5"
  185. "\x2F\xFD\x42\xEF\x00\x00\xA6\x12\xB0\x7D\x1E\xB0\x01\x02\x00\x00"
  186. "\x54\xA0\xBA\x24\x8D\xC4\x25\xF2\x77\xFA\xFC\xFB\x94\x7A\xC7\xC9"
  187. "\x13\x03\x11\x24\x43\x63\x3C\x6D\x22\x03\x01\x50\x67\x56\xF5\x9F"
  188. "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF"
  189. "\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B"
  190. "\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\xF9\x63\x0C\xB8\xFA\x58\xE7"
  191. "\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6\x56\xDC\x7F\x0C\x84\x4B\xA8"
  192. "\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC\x04\x1E\x17\x27\xE4\x43\xF6"
  193. "\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00\x35\x0B\x71\xB5\xC0\x2A\x5C"
  194. "\x26\x94\x22\x20\x8B\x4C\x8D\x13\x47\x58\x67\x15\x6C\xF1\x1C\x4B"
  195. "\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0\x00\x00"
  196. "\x27\xB5\x2F\xFD\x20\xEF\x00\x00\xA6\x12\xE4\x84\x1F\xB0\x01\x10"
  197. "\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF\x9F\xEF"
  198. "\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21\x80\x32"
  199. "\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20\x5F\x45"
  200. "\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB\x44\x14"
  201. "\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB\x89\x51"
  202. "\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F\x59\xDB"
  203. "\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E\xDF\x78"
  204. "\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55\x4A\xD4"
  205. "\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39\x43\xF1"
  206. "\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0"
  207. "\x00\x00\x28\xB5\x2F\xFD\x24\xEF\x35\x05\x00\x92\x0B\x21\x1F\xB0"
  208. "\x01\x10\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF"
  209. "\x9F\xEF\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21"
  210. "\x80\x32\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20"
  211. "\x5F\x45\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB"
  212. "\x44\x14\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB"
  213. "\x89\x51\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F"
  214. "\x59\xDB\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E"
  215. "\xDF\x78\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55"
  216. "\x4A\xD4\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39"
  217. "\x43\xF1\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D"
  218. "\x01\xD2\x2F\x21\x80";
  219. const char* const EXPECTED =
  220. "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
  221. "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
  222. "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
  223. "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
  224. "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
  225. "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
  226. "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
  227. "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
  228. "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
  229. "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
  230. "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
  231. "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
  232. "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
  233. "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
  234. "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n";