zstd_legacy.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (c) 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 "../common/mem.h" /* MEM_STATIC */
  19. #include "../common/error_private.h" /* ERROR */
  20. #include "../common/zstd_internal.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTD_frameSizeInfo */
  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 ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)
  171. {
  172. ZSTD_frameSizeInfo frameSizeInfo;
  173. U32 const version = ZSTD_isLegacy(src, srcSize);
  174. switch(version)
  175. {
  176. #if (ZSTD_LEGACY_SUPPORT <= 1)
  177. case 1 :
  178. ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,
  179. &frameSizeInfo.compressedSize,
  180. &frameSizeInfo.decompressedBound);
  181. break;
  182. #endif
  183. #if (ZSTD_LEGACY_SUPPORT <= 2)
  184. case 2 :
  185. ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,
  186. &frameSizeInfo.compressedSize,
  187. &frameSizeInfo.decompressedBound);
  188. break;
  189. #endif
  190. #if (ZSTD_LEGACY_SUPPORT <= 3)
  191. case 3 :
  192. ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,
  193. &frameSizeInfo.compressedSize,
  194. &frameSizeInfo.decompressedBound);
  195. break;
  196. #endif
  197. #if (ZSTD_LEGACY_SUPPORT <= 4)
  198. case 4 :
  199. ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,
  200. &frameSizeInfo.compressedSize,
  201. &frameSizeInfo.decompressedBound);
  202. break;
  203. #endif
  204. #if (ZSTD_LEGACY_SUPPORT <= 5)
  205. case 5 :
  206. ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,
  207. &frameSizeInfo.compressedSize,
  208. &frameSizeInfo.decompressedBound);
  209. break;
  210. #endif
  211. #if (ZSTD_LEGACY_SUPPORT <= 6)
  212. case 6 :
  213. ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,
  214. &frameSizeInfo.compressedSize,
  215. &frameSizeInfo.decompressedBound);
  216. break;
  217. #endif
  218. #if (ZSTD_LEGACY_SUPPORT <= 7)
  219. case 7 :
  220. ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,
  221. &frameSizeInfo.compressedSize,
  222. &frameSizeInfo.decompressedBound);
  223. break;
  224. #endif
  225. default :
  226. frameSizeInfo.compressedSize = ERROR(prefix_unknown);
  227. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  228. break;
  229. }
  230. if (!ZSTD_isError(frameSizeInfo.compressedSize) && frameSizeInfo.compressedSize > srcSize) {
  231. frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
  232. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  233. }
  234. return frameSizeInfo;
  235. }
  236. MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)
  237. {
  238. ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);
  239. return frameSizeInfo.compressedSize;
  240. }
  241. MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
  242. {
  243. switch(version)
  244. {
  245. default :
  246. case 1 :
  247. case 2 :
  248. case 3 :
  249. (void)legacyContext;
  250. return ERROR(version_unsupported);
  251. #if (ZSTD_LEGACY_SUPPORT <= 4)
  252. case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
  253. #endif
  254. #if (ZSTD_LEGACY_SUPPORT <= 5)
  255. case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
  256. #endif
  257. #if (ZSTD_LEGACY_SUPPORT <= 6)
  258. case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
  259. #endif
  260. #if (ZSTD_LEGACY_SUPPORT <= 7)
  261. case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
  262. #endif
  263. }
  264. }
  265. MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
  266. const void* dict, size_t dictSize)
  267. {
  268. DEBUGLOG(5, "ZSTD_initLegacyStream for v0.%u", newVersion);
  269. if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
  270. switch(newVersion)
  271. {
  272. default :
  273. case 1 :
  274. case 2 :
  275. case 3 :
  276. (void)dict; (void)dictSize;
  277. return 0;
  278. #if (ZSTD_LEGACY_SUPPORT <= 4)
  279. case 4 :
  280. {
  281. ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
  282. if (dctx==NULL) return ERROR(memory_allocation);
  283. ZBUFFv04_decompressInit(dctx);
  284. ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
  285. *legacyContext = dctx;
  286. return 0;
  287. }
  288. #endif
  289. #if (ZSTD_LEGACY_SUPPORT <= 5)
  290. case 5 :
  291. {
  292. ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
  293. if (dctx==NULL) return ERROR(memory_allocation);
  294. ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
  295. *legacyContext = dctx;
  296. return 0;
  297. }
  298. #endif
  299. #if (ZSTD_LEGACY_SUPPORT <= 6)
  300. case 6 :
  301. {
  302. ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
  303. if (dctx==NULL) return ERROR(memory_allocation);
  304. ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
  305. *legacyContext = dctx;
  306. return 0;
  307. }
  308. #endif
  309. #if (ZSTD_LEGACY_SUPPORT <= 7)
  310. case 7 :
  311. {
  312. ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
  313. if (dctx==NULL) return ERROR(memory_allocation);
  314. ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
  315. *legacyContext = dctx;
  316. return 0;
  317. }
  318. #endif
  319. }
  320. }
  321. MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
  322. ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  323. {
  324. DEBUGLOG(5, "ZSTD_decompressLegacyStream for v0.%u", version);
  325. switch(version)
  326. {
  327. default :
  328. case 1 :
  329. case 2 :
  330. case 3 :
  331. (void)legacyContext; (void)output; (void)input;
  332. return ERROR(version_unsupported);
  333. #if (ZSTD_LEGACY_SUPPORT <= 4)
  334. case 4 :
  335. {
  336. ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
  337. const void* src = (const char*)input->src + input->pos;
  338. size_t readSize = input->size - input->pos;
  339. void* dst = (char*)output->dst + output->pos;
  340. size_t decodedSize = output->size - output->pos;
  341. size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  342. output->pos += decodedSize;
  343. input->pos += readSize;
  344. return hintSize;
  345. }
  346. #endif
  347. #if (ZSTD_LEGACY_SUPPORT <= 5)
  348. case 5 :
  349. {
  350. ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
  351. const void* src = (const char*)input->src + input->pos;
  352. size_t readSize = input->size - input->pos;
  353. void* dst = (char*)output->dst + output->pos;
  354. size_t decodedSize = output->size - output->pos;
  355. size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  356. output->pos += decodedSize;
  357. input->pos += readSize;
  358. return hintSize;
  359. }
  360. #endif
  361. #if (ZSTD_LEGACY_SUPPORT <= 6)
  362. case 6 :
  363. {
  364. ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
  365. const void* src = (const char*)input->src + input->pos;
  366. size_t readSize = input->size - input->pos;
  367. void* dst = (char*)output->dst + output->pos;
  368. size_t decodedSize = output->size - output->pos;
  369. size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  370. output->pos += decodedSize;
  371. input->pos += readSize;
  372. return hintSize;
  373. }
  374. #endif
  375. #if (ZSTD_LEGACY_SUPPORT <= 7)
  376. case 7 :
  377. {
  378. ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
  379. const void* src = (const char*)input->src + input->pos;
  380. size_t readSize = input->size - input->pos;
  381. void* dst = (char*)output->dst + output->pos;
  382. size_t decodedSize = output->size - output->pos;
  383. size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  384. output->pos += decodedSize;
  385. input->pos += readSize;
  386. return hintSize;
  387. }
  388. #endif
  389. }
  390. }
  391. #if defined (__cplusplus)
  392. }
  393. #endif
  394. #endif /* ZSTD_LEGACY_H */