1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/**
* @file
* Zstandard (zstd) compression
*
* @authors
* Copyright (C) 2019-2020 Tino Reichardt <milky-neomutt@mcmilk.de>
* Copyright (C) 2020 Pietro Cerutti <gahr@gahr.ch>
* Copyright (C) 2020-2023 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page compress_zstd Zstandard (zstd) compression
*
* Zstandard (zstd) compression.
* https://www.zstd.net
*/
#include "config.h"
#include <stdio.h>
#include <zstd.h>
#include "private.h"
#include "mutt/lib.h"
#include "lib.h"
#define MIN_COMP_LEVEL 1 ///< Minimum compression level for zstd
#define MAX_COMP_LEVEL 22 ///< Maximum compression level for zstd
/**
* struct ZstdComprData - Private Zstandard Compression Data
*/
struct ZstdComprData
{
void *buf; ///< Temporary buffer
short level; ///< Compression Level to be used
ZSTD_CCtx *cctx; ///< Compression context
ZSTD_DCtx *dctx; ///< Decompression context
};
/**
* zstd_cdata_free - Free Zstandard Compression Data
* @param ptr Zstandard Compression Data to free
*/
void zstd_cdata_free(struct ZstdComprData **ptr)
{
if (!ptr || !*ptr)
return;
struct ZstdComprData *cdata = *ptr;
FREE(&cdata->buf);
FREE(ptr);
}
/**
* zstd_cdata_new - Create new Zstandard Compression Data
* @retval ptr New Zstandard Compression Data
*/
static struct ZstdComprData *zstd_cdata_new(void)
{
return MUTT_MEM_CALLOC(1, struct ZstdComprData);
}
/**
* compr_zstd_open - Open a compression context - Implements ComprOps::open() - @ingroup compress_open
*/
static ComprHandle *compr_zstd_open(short level)
{
struct ZstdComprData *cdata = zstd_cdata_new();
cdata->buf = mutt_mem_calloc(ZSTD_compressBound(1024 * 128), 1);
cdata->cctx = ZSTD_createCCtx();
cdata->dctx = ZSTD_createDCtx();
if (!cdata->cctx || !cdata->dctx)
{
// LCOV_EXCL_START
ZSTD_freeCCtx(cdata->cctx);
ZSTD_freeDCtx(cdata->dctx);
zstd_cdata_free(&cdata);
return NULL;
// LCOV_EXCL_STOP
}
if ((level < MIN_COMP_LEVEL) || (level > MAX_COMP_LEVEL))
{
mutt_debug(LL_DEBUG1, "The compression level for %s should be between %d and %d\n",
compr_zstd_ops.name, MIN_COMP_LEVEL, MAX_COMP_LEVEL);
level = MIN_COMP_LEVEL;
}
cdata->level = level;
// Return an opaque pointer
return (ComprHandle *) cdata;
}
/**
* compr_zstd_compress - Compress header cache data - Implements ComprOps::compress() - @ingroup compress_compress
*/
static void *compr_zstd_compress(ComprHandle *handle, const char *data,
size_t dlen, size_t *clen)
{
if (!handle)
return NULL;
// Decloak an opaque pointer
struct ZstdComprData *cdata = handle;
size_t len = ZSTD_compressBound(dlen);
mutt_mem_realloc(&cdata->buf, len);
size_t rc = ZSTD_compressCCtx(cdata->cctx, cdata->buf, len, data, dlen, cdata->level);
if (ZSTD_isError(rc))
return NULL; // LCOV_EXCL_LINE
*clen = rc;
return cdata->buf;
}
/**
* compr_zstd_decompress - Decompress header cache data - Implements ComprOps::decompress() - @ingroup compress_decompress
*/
static void *compr_zstd_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
{
if (!handle)
return NULL;
// Decloak an opaque pointer
struct ZstdComprData *cdata = handle;
unsigned long long len = ZSTD_getFrameContentSize(cbuf, clen);
if (len == ZSTD_CONTENTSIZE_UNKNOWN)
return NULL; // LCOV_EXCL_LINE
else if (len == ZSTD_CONTENTSIZE_ERROR)
return NULL;
else if (len == 0)
return NULL; // LCOV_EXCL_LINE
mutt_mem_realloc(&cdata->buf, len);
size_t rc = ZSTD_decompressDCtx(cdata->dctx, cdata->buf, len, cbuf, clen);
if (ZSTD_isError(rc))
return NULL; // LCOV_EXCL_LINE
return cdata->buf;
}
/**
* compr_zstd_close - Close a compression context - Implements ComprOps::close() - @ingroup compress_close
*/
static void compr_zstd_close(ComprHandle **ptr)
{
if (!ptr || !*ptr)
return;
// Decloak an opaque pointer
struct ZstdComprData *cdata = *ptr;
if (cdata->cctx)
ZSTD_freeCCtx(cdata->cctx);
if (cdata->dctx)
ZSTD_freeDCtx(cdata->dctx);
zstd_cdata_free((struct ZstdComprData **) ptr);
}
COMPRESS_OPS(zstd, MIN_COMP_LEVEL, MAX_COMP_LEVEL)
|