| 1 |
#include "codec_zstd.h" |
| 2 |
|
| 3 |
#include <stddef.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
chd_error zstd_codec_init(void* codec, uint32_t hunkbytes) |
| 13 |
{ |
| 14 |
zstd_codec_data* zstd_codec = (zstd_codec_data*) codec; |
| 15 |
|
| 16 |
(void)hunkbytes; |
| 17 |
zstd_codec->dstream = ZSTD_createDStream(); |
| 18 |
if (!zstd_codec->dstream) { |
| 19 |
#if 0 |
| 20 |
printf("NO DSTREAM CREATED!\n"); |
| 21 |
#endif |
| 22 |
return CHDERR_DECOMPRESSION_ERROR; |
| 23 |
} |
| 24 |
return CHDERR_NONE; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
void zstd_codec_free(void* codec) |
| 33 |
{ |
| 34 |
zstd_codec_data* zstd_codec = (zstd_codec_data*) codec; |
| 35 |
|
| 36 |
ZSTD_freeDStream(zstd_codec->dstream); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
chd_error zstd_codec_decompress(void* codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) |
| 45 |
{ |
| 46 |
ZSTD_inBuffer input; |
| 47 |
ZSTD_outBuffer output; |
| 48 |
|
| 49 |
|
| 50 |
zstd_codec_data* zstd_codec = (zstd_codec_data*) codec; |
| 51 |
|
| 52 |
|
| 53 |
size_t zstd_res = ZSTD_initDStream(zstd_codec->dstream); |
| 54 |
|
| 55 |
if (ZSTD_isError(zstd_res)) |
| 56 |
{ |
| 57 |
#if 0 |
| 58 |
printf("INITI DSTREAM FAILED!\n"); |
| 59 |
#endif |
| 60 |
return CHDERR_DECOMPRESSION_ERROR; |
| 61 |
} |
| 62 |
|
| 63 |
input.src = src; |
| 64 |
input.size = complen; |
| 65 |
input.pos = 0; |
| 66 |
|
| 67 |
output.dst = dest; |
| 68 |
output.size = destlen; |
| 69 |
output.pos = 0; |
| 70 |
|
| 71 |
while ((input.pos < input.size) && (output.pos < output.size)) |
| 72 |
{ |
| 73 |
zstd_res = ZSTD_decompressStream(zstd_codec->dstream, &output, &input); |
| 74 |
if (ZSTD_isError(zstd_res)) |
| 75 |
{ |
| 76 |
#if 0 |
| 77 |
printf("DECOMPRESSION ERROR IN LOOP\n"); |
| 78 |
#endif |
| 79 |
return CHDERR_DECOMPRESSION_ERROR; |
| 80 |
} |
| 81 |
} |
| 82 |
if (output.pos != output.size) |
| 83 |
{ |
| 84 |
#if 0 |
| 85 |
printf("OUTPUT DOESN'T MATCH!\n"); |
| 86 |
#endif |
| 87 |
return CHDERR_DECOMPRESSION_ERROR; |
| 88 |
} |
| 89 |
return CHDERR_NONE; |
| 90 |
|
| 91 |
} |