zstd_legacy.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 2016-present, 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. #ifndef ZSTD_LEGACY_H
  11. #define ZSTD_LEGACY_H
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /* *************************************
  16. * Includes
  17. ***************************************/
  18. #include "mem.h" /* MEM_STATIC */
  19. #include "error_private.h" /* ERROR */
  20. #include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer */
  21. #if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
  22. # undef ZSTD_LEGACY_SUPPORT
  23. # define ZSTD_LEGACY_SUPPORT 8
  24. #endif
  25. #if (ZSTD_LEGACY_SUPPORT <= 1)
  26. # include "zstd_v01.h"
  27. #endif
  28. #if (ZSTD_LEGACY_SUPPORT <= 2)
  29. # include "zstd_v02.h"
  30. #endif
  31. #if (ZSTD_LEGACY_SUPPORT <= 3)
  32. # include "zstd_v03.h"
  33. #endif
  34. #if (ZSTD_LEGACY_SUPPORT <= 4)
  35. # include "zstd_v04.h"
  36. #endif
  37. #if (ZSTD_LEGACY_SUPPORT <= 5)
  38. # include "zstd_v05.h"
  39. #endif
  40. #if (ZSTD_LEGACY_SUPPORT <= 6)
  41. # include "zstd_v06.h"
  42. #endif
  43. #if (ZSTD_LEGACY_SUPPORT <= 7)
  44. # include "zstd_v07.h"
  45. #endif
  46. /** ZSTD_isLegacy() :
  47. @return : > 0 if supported by legacy decoder. 0 otherwise.
  48. return value is the version.
  49. */
  50. MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
  51. {
  52. U32 magicNumberLE;
  53. if (srcSize<4) return 0;
  54. magicNumberLE = MEM_readLE32(src);
  55. switch(magicNumberLE)
  56. {
  57. #if (ZSTD_LEGACY_SUPPORT <= 1)
  58. case ZSTDv01_magicNumberLE:return 1;
  59. #endif
  60. #if (ZSTD_LEGACY_SUPPORT <= 2)
  61. case ZSTDv02_magicNumber : return 2;
  62. #endif
  63. #if (ZSTD_LEGACY_SUPPORT <= 3)
  64. case ZSTDv03_magicNumber : return 3;
  65. #endif
  66. #if (ZSTD_LEGACY_SUPPORT <= 4)
  67. case ZSTDv04_magicNumber : return 4;
  68. #endif
  69. #if (ZSTD_LEGACY_SUPPORT <= 5)
  70. case ZSTDv05_MAGICNUMBER : return 5;
  71. #endif
  72. #if (ZSTD_LEGACY_SUPPORT <= 6)
  73. case ZSTDv06_MAGICNUMBER : return 6;
  74. #endif
  75. #if (ZSTD_LEGACY_SUPPORT <= 7)
  76. case ZSTDv07_MAGICNUMBER : return 7;
  77. #endif
  78. default : return 0;
  79. }
  80. }
  81. MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
  82. {
  83. U32 const version = ZSTD_isLegacy(src, srcSize);
  84. if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */
  85. #if (ZSTD_LEGACY_SUPPORT <= 5)
  86. if (version==5) {
  87. ZSTDv05_parameters fParams;
  88. size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);
  89. if (frResult != 0) return 0;
  90. return fParams.srcSize;
  91. }
  92. #endif
  93. #if (ZSTD_LEGACY_SUPPORT <= 6)
  94. if (version==6) {
  95. ZSTDv06_frameParams fParams;
  96. size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);
  97. if (frResult != 0) return 0;
  98. return fParams.frameContentSize;
  99. }
  100. #endif
  101. #if (ZSTD_LEGACY_SUPPORT <= 7)
  102. if (version==7) {
  103. ZSTDv07_frameParams fParams;
  104. size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize);
  105. if (frResult != 0) return 0;
  106. return fParams.frameContentSize;
  107. }
  108. #endif
  109. return 0; /* should not be possible */
  110. }
  111. MEM_STATIC size_t ZSTD_decompressLegacy(
  112. void* dst, size_t dstCapacity,
  113. const void* src, size_t compressedSize,
  114. const void* dict,size_t dictSize)
  115. {
  116. U32 const version = ZSTD_isLegacy(src, compressedSize);
  117. (void)dst; (void)dstCapacity; (void)dict; (void)dictSize; /* unused when ZSTD_LEGACY_SUPPORT >= 8 */
  118. switch(version)
  119. {
  120. #if (ZSTD_LEGACY_SUPPORT <= 1)
  121. case 1 :
  122. return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
  123. #endif
  124. #if (ZSTD_LEGACY_SUPPORT <= 2)
  125. case 2 :
  126. return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
  127. #endif
  128. #if (ZSTD_LEGACY_SUPPORT <= 3)
  129. case 3 :
  130. return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
  131. #endif
  132. #if (ZSTD_LEGACY_SUPPORT <= 4)
  133. case 4 :
  134. return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
  135. #endif
  136. #if (ZSTD_LEGACY_SUPPORT <= 5)
  137. case 5 :
  138. { size_t result;
  139. ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
  140. if (zd==NULL) return ERROR(memory_allocation);
  141. result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  142. ZSTDv05_freeDCtx(zd);
  143. return result;
  144. }
  145. #endif
  146. #if (ZSTD_LEGACY_SUPPORT <= 6)
  147. case 6 :
  148. { size_t result;
  149. ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
  150. if (zd==NULL) return ERROR(memory_allocation);
  151. result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  152. ZSTDv06_freeDCtx(zd);
  153. return result;
  154. }
  155. #endif
  156. #if (ZSTD_LEGACY_SUPPORT <= 7)
  157. case 7 :
  158. { size_t result;
  159. ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();
  160. if (zd==NULL) return ERROR(memory_allocation);
  161. result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  162. ZSTDv07_freeDCtx(zd);
  163. return result;
  164. }
  165. #endif
  166. default :
  167. return ERROR(prefix_unknown);
  168. }
  169. }
  170. MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src,
  171. size_t compressedSize)
  172. {
  173. U32 const version = ZSTD_isLegacy(src, compressedSize);
  174. switch(version)
  175. {
  176. #if (ZSTD_LEGACY_SUPPORT <= 1)
  177. case 1 :
  178. return ZSTDv01_findFrameCompressedSize(src, compressedSize);
  179. #endif
  180. #if (ZSTD_LEGACY_SUPPORT <= 2)
  181. case 2 :
  182. return ZSTDv02_findFrameCompressedSize(src, compressedSize);
  183. #endif
  184. #if (ZSTD_LEGACY_SUPPORT <= 3)
  185. case 3 :
  186. return ZSTDv03_findFrameCompressedSize(src, compressedSize);
  187. #endif
  188. #if (ZSTD_LEGACY_SUPPORT <= 4)
  189. case 4 :
  190. return ZSTDv04_findFrameCompressedSize(src, compressedSize);
  191. #endif
  192. #if (ZSTD_LEGACY_SUPPORT <= 5)
  193. case 5 :
  194. return ZSTDv05_findFrameCompressedSize(src, compressedSize);
  195. #endif
  196. #if (ZSTD_LEGACY_SUPPORT <= 6)
  197. case 6 :
  198. return ZSTDv06_findFrameCompressedSize(src, compressedSize);
  199. #endif
  200. #if (ZSTD_LEGACY_SUPPORT <= 7)
  201. case 7 :
  202. return ZSTDv07_findFrameCompressedSize(src, compressedSize);
  203. #endif
  204. default :
  205. return ERROR(prefix_unknown);
  206. }
  207. }
  208. MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
  209. {
  210. switch(version)
  211. {
  212. default :
  213. case 1 :
  214. case 2 :
  215. case 3 :
  216. (void)legacyContext;
  217. return ERROR(version_unsupported);
  218. #if (ZSTD_LEGACY_SUPPORT <= 4)
  219. case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
  220. #endif
  221. #if (ZSTD_LEGACY_SUPPORT <= 5)
  222. case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
  223. #endif
  224. #if (ZSTD_LEGACY_SUPPORT <= 6)
  225. case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
  226. #endif
  227. #if (ZSTD_LEGACY_SUPPORT <= 7)
  228. case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
  229. #endif
  230. }
  231. }
  232. MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
  233. const void* dict, size_t dictSize)
  234. {
  235. DEBUGLOG(5, "ZSTD_initLegacyStream for v0.%u", newVersion);
  236. if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
  237. switch(newVersion)
  238. {
  239. default :
  240. case 1 :
  241. case 2 :
  242. case 3 :
  243. (void)dict; (void)dictSize;
  244. return 0;
  245. #if (ZSTD_LEGACY_SUPPORT <= 4)
  246. case 4 :
  247. {
  248. ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
  249. if (dctx==NULL) return ERROR(memory_allocation);
  250. ZBUFFv04_decompressInit(dctx);
  251. ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
  252. *legacyContext = dctx;
  253. return 0;
  254. }
  255. #endif
  256. #if (ZSTD_LEGACY_SUPPORT <= 5)
  257. case 5 :
  258. {
  259. ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
  260. if (dctx==NULL) return ERROR(memory_allocation);
  261. ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
  262. *legacyContext = dctx;
  263. return 0;
  264. }
  265. #endif
  266. #if (ZSTD_LEGACY_SUPPORT <= 6)
  267. case 6 :
  268. {
  269. ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
  270. if (dctx==NULL) return ERROR(memory_allocation);
  271. ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
  272. *legacyContext = dctx;
  273. return 0;
  274. }
  275. #endif
  276. #if (ZSTD_LEGACY_SUPPORT <= 7)
  277. case 7 :
  278. {
  279. ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
  280. if (dctx==NULL) return ERROR(memory_allocation);
  281. ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
  282. *legacyContext = dctx;
  283. return 0;
  284. }
  285. #endif
  286. }
  287. }
  288. MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
  289. ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  290. {
  291. DEBUGLOG(5, "ZSTD_decompressLegacyStream for v0.%u", version);
  292. switch(version)
  293. {
  294. default :
  295. case 1 :
  296. case 2 :
  297. case 3 :
  298. (void)legacyContext; (void)output; (void)input;
  299. return ERROR(version_unsupported);
  300. #if (ZSTD_LEGACY_SUPPORT <= 4)
  301. case 4 :
  302. {
  303. ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
  304. const void* src = (const char*)input->src + input->pos;
  305. size_t readSize = input->size - input->pos;
  306. void* dst = (char*)output->dst + output->pos;
  307. size_t decodedSize = output->size - output->pos;
  308. size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  309. output->pos += decodedSize;
  310. input->pos += readSize;
  311. return hintSize;
  312. }
  313. #endif
  314. #if (ZSTD_LEGACY_SUPPORT <= 5)
  315. case 5 :
  316. {
  317. ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
  318. const void* src = (const char*)input->src + input->pos;
  319. size_t readSize = input->size - input->pos;
  320. void* dst = (char*)output->dst + output->pos;
  321. size_t decodedSize = output->size - output->pos;
  322. size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  323. output->pos += decodedSize;
  324. input->pos += readSize;
  325. return hintSize;
  326. }
  327. #endif
  328. #if (ZSTD_LEGACY_SUPPORT <= 6)
  329. case 6 :
  330. {
  331. ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
  332. const void* src = (const char*)input->src + input->pos;
  333. size_t readSize = input->size - input->pos;
  334. void* dst = (char*)output->dst + output->pos;
  335. size_t decodedSize = output->size - output->pos;
  336. size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  337. output->pos += decodedSize;
  338. input->pos += readSize;
  339. return hintSize;
  340. }
  341. #endif
  342. #if (ZSTD_LEGACY_SUPPORT <= 7)
  343. case 7 :
  344. {
  345. ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
  346. const void* src = (const char*)input->src + input->pos;
  347. size_t readSize = input->size - input->pos;
  348. void* dst = (char*)output->dst + output->pos;
  349. size_t decodedSize = output->size - output->pos;
  350. size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  351. output->pos += decodedSize;
  352. input->pos += readSize;
  353. return hintSize;
  354. }
  355. #endif
  356. }
  357. }
  358. #if defined (__cplusplus)
  359. }
  360. #endif
  361. #endif /* ZSTD_LEGACY_H */