Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 9 | |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 10 | #ifndef ZSTD_LEGACY_H |
| 11 | #define ZSTD_LEGACY_H |
| 12 | |
| 13 | #if defined (__cplusplus) |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | /* ************************************* |
| 18 | * Includes |
| 19 | ***************************************/ |
Yann Collet | ffec740 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 20 | #include "mem.h" /* MEM_STATIC */ |
| 21 | #include "error_private.h" /* ERROR */ |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 22 | #include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer */ |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 23 | #include "zstd_v01.h" |
| 24 | #include "zstd_v02.h" |
Yann Collet | 29a2c83 | 2015-11-26 16:02:04 +0100 | [diff] [blame] | 25 | #include "zstd_v03.h" |
Yann Collet | 464fa99 | 2016-02-03 01:09:46 +0100 | [diff] [blame] | 26 | #include "zstd_v04.h" |
Yann Collet | 029267a | 2016-04-09 09:42:27 +0200 | [diff] [blame] | 27 | #include "zstd_v05.h" |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 28 | #include "zstd_v06.h" |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame] | 29 | #include "zstd_v07.h" |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 30 | |
Yann Collet | 18dedec | 2016-05-06 16:43:23 +0200 | [diff] [blame] | 31 | |
| 32 | /** ZSTD_isLegacy() : |
| 33 | @return : > 0 if supported by legacy decoder. 0 otherwise. |
| 34 | return value is the version. |
| 35 | */ |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 36 | MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 37 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 38 | U32 magicNumberLE; |
| 39 | if (srcSize<4) return 0; |
| 40 | magicNumberLE = MEM_readLE32(src); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 41 | switch(magicNumberLE) |
| 42 | { |
| 43 | case ZSTDv01_magicNumberLE:return 1; |
| 44 | case ZSTDv02_magicNumber : return 2; |
| 45 | case ZSTDv03_magicNumber : return 3; |
| 46 | case ZSTDv04_magicNumber : return 4; |
| 47 | case ZSTDv05_MAGICNUMBER : return 5; |
| 48 | case ZSTDv06_MAGICNUMBER : return 6; |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame] | 49 | case ZSTDv07_MAGICNUMBER : return 7; |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 50 | default : return 0; |
| 51 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 55 | MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize) |
| 56 | { |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 57 | U32 const version = ZSTD_isLegacy(src, srcSize); |
| 58 | if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */ |
| 59 | if (version==5) { |
| 60 | ZSTDv05_parameters fParams; |
| 61 | size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize); |
| 62 | if (frResult != 0) return 0; |
| 63 | return fParams.srcSize; |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 64 | } |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 65 | if (version==6) { |
| 66 | ZSTDv06_frameParams fParams; |
| 67 | size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize); |
| 68 | if (frResult != 0) return 0; |
| 69 | return fParams.frameContentSize; |
| 70 | } |
| 71 | if (version==7) { |
| 72 | ZSTDv07_frameParams fParams; |
| 73 | size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize); |
| 74 | if (frResult != 0) return 0; |
| 75 | return fParams.frameContentSize; |
| 76 | } |
| 77 | return 0; /* should not be possible */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 80 | |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 81 | MEM_STATIC size_t ZSTD_decompressLegacy( |
Yann Collet | 029267a | 2016-04-09 09:42:27 +0200 | [diff] [blame] | 82 | void* dst, size_t dstCapacity, |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 83 | const void* src, size_t compressedSize, |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 84 | const void* dict,size_t dictSize) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 85 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 86 | U32 const version = ZSTD_isLegacy(src, compressedSize); |
| 87 | switch(version) |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 88 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 89 | case 1 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 90 | return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 91 | case 2 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 92 | return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 93 | case 3 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 94 | return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 95 | case 4 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 96 | return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 97 | case 5 : |
Yann Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 98 | { size_t result; |
| 99 | ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 100 | if (zd==NULL) return ERROR(memory_allocation); |
| 101 | result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize); |
| 102 | ZSTDv05_freeDCtx(zd); |
| 103 | return result; |
| 104 | } |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 105 | case 6 : |
Yann Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 106 | { size_t result; |
| 107 | ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 108 | if (zd==NULL) return ERROR(memory_allocation); |
| 109 | result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize); |
| 110 | ZSTDv06_freeDCtx(zd); |
| 111 | return result; |
| 112 | } |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame] | 113 | case 7 : |
| 114 | { size_t result; |
| 115 | ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx(); |
| 116 | if (zd==NULL) return ERROR(memory_allocation); |
| 117 | result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize); |
| 118 | ZSTDv07_freeDCtx(zd); |
| 119 | return result; |
| 120 | } |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 121 | default : |
| 122 | return ERROR(prefix_unknown); |
| 123 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 127 | MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version) |
| 128 | { |
| 129 | switch(version) |
| 130 | { |
| 131 | default : |
| 132 | case 1 : |
| 133 | case 2 : |
| 134 | case 3 : |
| 135 | return ERROR(version_unsupported); |
| 136 | case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext); |
| 137 | case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext); |
| 138 | case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext); |
| 139 | case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 144 | MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion, |
| 145 | const void* dict, size_t dictSize) |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 146 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 147 | if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion); |
| 148 | switch(newVersion) |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 149 | { |
| 150 | default : |
| 151 | case 1 : |
| 152 | case 2 : |
| 153 | case 3 : |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 154 | return 0; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 155 | case 4 : |
| 156 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 157 | ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext; |
| 158 | if (dctx==NULL) return ERROR(memory_allocation); |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 159 | ZBUFFv04_decompressInit(dctx); |
| 160 | ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize); |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 161 | *legacyContext = dctx; |
| 162 | return 0; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 163 | } |
| 164 | case 5 : |
| 165 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 166 | ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext; |
| 167 | if (dctx==NULL) return ERROR(memory_allocation); |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 168 | ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize); |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 169 | *legacyContext = dctx; |
| 170 | return 0; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 171 | } |
| 172 | case 6 : |
| 173 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 174 | ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext; |
| 175 | if (dctx==NULL) return ERROR(memory_allocation); |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 176 | ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize); |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 177 | *legacyContext = dctx; |
| 178 | return 0; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 179 | } |
| 180 | case 7 : |
| 181 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 182 | ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext; |
| 183 | if (dctx==NULL) return ERROR(memory_allocation); |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 184 | ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize); |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 185 | *legacyContext = dctx; |
| 186 | return 0; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 193 | MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version, |
| 194 | ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 195 | { |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 196 | switch(version) |
| 197 | { |
| 198 | default : |
| 199 | case 1 : |
| 200 | case 2 : |
| 201 | case 3 : |
| 202 | return ERROR(version_unsupported); |
| 203 | case 4 : |
| 204 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 205 | ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 206 | const void* src = (const char*)input->src + input->pos; |
| 207 | size_t readSize = input->size - input->pos; |
| 208 | void* dst = (char*)output->dst + output->pos; |
| 209 | size_t decodedSize = output->size - output->pos; |
| 210 | size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize); |
| 211 | output->pos += decodedSize; |
| 212 | input->pos += readSize; |
| 213 | return hintSize; |
| 214 | } |
| 215 | case 5 : |
| 216 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 217 | ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 218 | const void* src = (const char*)input->src + input->pos; |
| 219 | size_t readSize = input->size - input->pos; |
| 220 | void* dst = (char*)output->dst + output->pos; |
| 221 | size_t decodedSize = output->size - output->pos; |
| 222 | size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize); |
| 223 | output->pos += decodedSize; |
| 224 | input->pos += readSize; |
| 225 | return hintSize; |
| 226 | } |
| 227 | case 6 : |
| 228 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 229 | ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 230 | const void* src = (const char*)input->src + input->pos; |
| 231 | size_t readSize = input->size - input->pos; |
| 232 | void* dst = (char*)output->dst + output->pos; |
| 233 | size_t decodedSize = output->size - output->pos; |
| 234 | size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize); |
| 235 | output->pos += decodedSize; |
| 236 | input->pos += readSize; |
| 237 | return hintSize; |
| 238 | } |
| 239 | case 7 : |
| 240 | { |
Yann Collet | 767d8f6 | 2016-08-28 08:19:47 -0700 | [diff] [blame] | 241 | ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext; |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 242 | const void* src = (const char*)input->src + input->pos; |
| 243 | size_t readSize = input->size - input->pos; |
| 244 | void* dst = (char*)output->dst + output->pos; |
| 245 | size_t decodedSize = output->size - output->pos; |
| 246 | size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize); |
| 247 | output->pos += decodedSize; |
| 248 | input->pos += readSize; |
| 249 | return hintSize; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 254 | |
| 255 | #if defined (__cplusplus) |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | #endif /* ZSTD_LEGACY_H */ |