zstd_legacy.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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. char x;
  118. /* Avoid passing NULL to legacy decoding. */
  119. if (dst == NULL) {
  120. assert(dstCapacity == 0);
  121. dst = &x;
  122. }
  123. if (src == NULL) {
  124. assert(compressedSize == 0);
  125. src = &x;
  126. }
  127. if (dict == NULL) {
  128. assert(dictSize == 0);
  129. dict = &x;
  130. }
  131. (void)dst; (void)dstCapacity; (void)dict; (void)dictSize; /* unused when ZSTD_LEGACY_SUPPORT >= 8 */
  132. switch(version)
  133. {
  134. #if (ZSTD_LEGACY_SUPPORT <= 1)
  135. case 1 :
  136. return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
  137. #endif
  138. #if (ZSTD_LEGACY_SUPPORT <= 2)
  139. case 2 :
  140. return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
  141. #endif
  142. #if (ZSTD_LEGACY_SUPPORT <= 3)
  143. case 3 :
  144. return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
  145. #endif
  146. #if (ZSTD_LEGACY_SUPPORT <= 4)
  147. case 4 :
  148. return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
  149. #endif
  150. #if (ZSTD_LEGACY_SUPPORT <= 5)
  151. case 5 :
  152. { size_t result;
  153. ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
  154. if (zd==NULL) return ERROR(memory_allocation);
  155. result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  156. ZSTDv05_freeDCtx(zd);
  157. return result;
  158. }
  159. #endif
  160. #if (ZSTD_LEGACY_SUPPORT <= 6)
  161. case 6 :
  162. { size_t result;
  163. ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
  164. if (zd==NULL) return ERROR(memory_allocation);
  165. result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  166. ZSTDv06_freeDCtx(zd);
  167. return result;
  168. }
  169. #endif
  170. #if (ZSTD_LEGACY_SUPPORT <= 7)
  171. case 7 :
  172. { size_t result;
  173. ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();
  174. if (zd==NULL) return ERROR(memory_allocation);
  175. result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
  176. ZSTDv07_freeDCtx(zd);
  177. return result;
  178. }
  179. #endif
  180. default :
  181. return ERROR(prefix_unknown);
  182. }
  183. }
  184. MEM_STATIC ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)
  185. {
  186. ZSTD_frameSizeInfo frameSizeInfo;
  187. U32 const version = ZSTD_isLegacy(src, srcSize);
  188. switch(version)
  189. {
  190. #if (ZSTD_LEGACY_SUPPORT <= 1)
  191. case 1 :
  192. ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,
  193. &frameSizeInfo.compressedSize,
  194. &frameSizeInfo.decompressedBound);
  195. break;
  196. #endif
  197. #if (ZSTD_LEGACY_SUPPORT <= 2)
  198. case 2 :
  199. ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,
  200. &frameSizeInfo.compressedSize,
  201. &frameSizeInfo.decompressedBound);
  202. break;
  203. #endif
  204. #if (ZSTD_LEGACY_SUPPORT <= 3)
  205. case 3 :
  206. ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,
  207. &frameSizeInfo.compressedSize,
  208. &frameSizeInfo.decompressedBound);
  209. break;
  210. #endif
  211. #if (ZSTD_LEGACY_SUPPORT <= 4)
  212. case 4 :
  213. ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,
  214. &frameSizeInfo.compressedSize,
  215. &frameSizeInfo.decompressedBound);
  216. break;
  217. #endif
  218. #if (ZSTD_LEGACY_SUPPORT <= 5)
  219. case 5 :
  220. ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,
  221. &frameSizeInfo.compressedSize,
  222. &frameSizeInfo.decompressedBound);
  223. break;
  224. #endif
  225. #if (ZSTD_LEGACY_SUPPORT <= 6)
  226. case 6 :
  227. ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,
  228. &frameSizeInfo.compressedSize,
  229. &frameSizeInfo.decompressedBound);
  230. break;
  231. #endif
  232. #if (ZSTD_LEGACY_SUPPORT <= 7)
  233. case 7 :
  234. ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,
  235. &frameSizeInfo.compressedSize,
  236. &frameSizeInfo.decompressedBound);
  237. break;
  238. #endif
  239. default :
  240. frameSizeInfo.compressedSize = ERROR(prefix_unknown);
  241. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  242. break;
  243. }
  244. if (!ZSTD_isError(frameSizeInfo.compressedSize) && frameSizeInfo.compressedSize > srcSize) {
  245. frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
  246. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  247. }
  248. /* In all cases, decompressedBound == nbBlocks * ZSTD_BLOCKSIZE_MAX.
  249. * So we can compute nbBlocks without having to change every function.
  250. */
  251. if (frameSizeInfo.decompressedBound != ZSTD_CONTENTSIZE_ERROR) {
  252. assert((frameSizeInfo.decompressedBound & (ZSTD_BLOCKSIZE_MAX - 1)) == 0);
  253. frameSizeInfo.nbBlocks = (size_t)(frameSizeInfo.decompressedBound / ZSTD_BLOCKSIZE_MAX);
  254. }
  255. return frameSizeInfo;
  256. }
  257. MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)
  258. {
  259. ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);
  260. return frameSizeInfo.compressedSize;
  261. }
  262. MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
  263. {
  264. switch(version)
  265. {
  266. default :
  267. case 1 :
  268. case 2 :
  269. case 3 :
  270. (void)legacyContext;
  271. return ERROR(version_unsupported);
  272. #if (ZSTD_LEGACY_SUPPORT <= 4)
  273. case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
  274. #endif
  275. #if (ZSTD_LEGACY_SUPPORT <= 5)
  276. case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
  277. #endif
  278. #if (ZSTD_LEGACY_SUPPORT <= 6)
  279. case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
  280. #endif
  281. #if (ZSTD_LEGACY_SUPPORT <= 7)
  282. case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
  283. #endif
  284. }
  285. }
  286. MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
  287. const void* dict, size_t dictSize)
  288. {
  289. char x;
  290. /* Avoid passing NULL to legacy decoding. */
  291. if (dict == NULL) {
  292. assert(dictSize == 0);
  293. dict = &x;
  294. }
  295. DEBUGLOG(5, "ZSTD_initLegacyStream for v0.%u", newVersion);
  296. if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
  297. switch(newVersion)
  298. {
  299. default :
  300. case 1 :
  301. case 2 :
  302. case 3 :
  303. (void)dict; (void)dictSize;
  304. return 0;
  305. #if (ZSTD_LEGACY_SUPPORT <= 4)
  306. case 4 :
  307. {
  308. ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
  309. if (dctx==NULL) return ERROR(memory_allocation);
  310. ZBUFFv04_decompressInit(dctx);
  311. ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
  312. *legacyContext = dctx;
  313. return 0;
  314. }
  315. #endif
  316. #if (ZSTD_LEGACY_SUPPORT <= 5)
  317. case 5 :
  318. {
  319. ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
  320. if (dctx==NULL) return ERROR(memory_allocation);
  321. ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
  322. *legacyContext = dctx;
  323. return 0;
  324. }
  325. #endif
  326. #if (ZSTD_LEGACY_SUPPORT <= 6)
  327. case 6 :
  328. {
  329. ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
  330. if (dctx==NULL) return ERROR(memory_allocation);
  331. ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
  332. *legacyContext = dctx;
  333. return 0;
  334. }
  335. #endif
  336. #if (ZSTD_LEGACY_SUPPORT <= 7)
  337. case 7 :
  338. {
  339. ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
  340. if (dctx==NULL) return ERROR(memory_allocation);
  341. ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
  342. *legacyContext = dctx;
  343. return 0;
  344. }
  345. #endif
  346. }
  347. }
  348. MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
  349. ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  350. {
  351. static char x;
  352. /* Avoid passing NULL to legacy decoding. */
  353. if (output->dst == NULL) {
  354. assert(output->size == 0);
  355. output->dst = &x;
  356. }
  357. if (input->src == NULL) {
  358. assert(input->size == 0);
  359. input->src = &x;
  360. }
  361. DEBUGLOG(5, "ZSTD_decompressLegacyStream for v0.%u", version);
  362. switch(version)
  363. {
  364. default :
  365. case 1 :
  366. case 2 :
  367. case 3 :
  368. (void)legacyContext; (void)output; (void)input;
  369. return ERROR(version_unsupported);
  370. #if (ZSTD_LEGACY_SUPPORT <= 4)
  371. case 4 :
  372. {
  373. ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
  374. const void* src = (const char*)input->src + input->pos;
  375. size_t readSize = input->size - input->pos;
  376. void* dst = (char*)output->dst + output->pos;
  377. size_t decodedSize = output->size - output->pos;
  378. size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  379. output->pos += decodedSize;
  380. input->pos += readSize;
  381. return hintSize;
  382. }
  383. #endif
  384. #if (ZSTD_LEGACY_SUPPORT <= 5)
  385. case 5 :
  386. {
  387. ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
  388. const void* src = (const char*)input->src + input->pos;
  389. size_t readSize = input->size - input->pos;
  390. void* dst = (char*)output->dst + output->pos;
  391. size_t decodedSize = output->size - output->pos;
  392. size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  393. output->pos += decodedSize;
  394. input->pos += readSize;
  395. return hintSize;
  396. }
  397. #endif
  398. #if (ZSTD_LEGACY_SUPPORT <= 6)
  399. case 6 :
  400. {
  401. ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
  402. const void* src = (const char*)input->src + input->pos;
  403. size_t readSize = input->size - input->pos;
  404. void* dst = (char*)output->dst + output->pos;
  405. size_t decodedSize = output->size - output->pos;
  406. size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  407. output->pos += decodedSize;
  408. input->pos += readSize;
  409. return hintSize;
  410. }
  411. #endif
  412. #if (ZSTD_LEGACY_SUPPORT <= 7)
  413. case 7 :
  414. {
  415. ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
  416. const void* src = (const char*)input->src + input->pos;
  417. size_t readSize = input->size - input->pos;
  418. void* dst = (char*)output->dst + output->pos;
  419. size_t decodedSize = output->size - output->pos;
  420. size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
  421. output->pos += decodedSize;
  422. input->pos += readSize;
  423. return hintSize;
  424. }
  425. #endif
  426. }
  427. }
  428. #if defined (__cplusplus)
  429. }
  430. #endif
  431. #endif /* ZSTD_LEGACY_H */