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
|
/**
* @file
* ZLIB 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_zlib ZLIB compression
*
* ZLIB compression.
* https://www.zlib.net/
*/
#include "config.h"
#include <stddef.h>
#include <zconf.h>
#include <zlib.h>
#include "private.h"
#include "mutt/lib.h"
#include "lib.h"
#define MIN_COMP_LEVEL 1 ///< Minimum compression level for zlib
#define MAX_COMP_LEVEL 9 ///< Maximum compression level for zlib
/**
* struct ZlibComprData - Private Zlib Compression Data
*/
struct ZlibComprData
{
void *buf; ///< Temporary buffer
short level; ///< Compression Level to be used
};
/**
* zlib_cdata_free - Free Zlib Compression Data
* @param ptr Zlib Compression Data to free
*/
void zlib_cdata_free(struct ZlibComprData **ptr)
{
if (!ptr || !*ptr)
return;
struct ZlibComprData *cdata = *ptr;
FREE(&cdata->buf);
FREE(ptr);
}
/**
* zlib_cdata_new - Create new Zlib Compression Data
* @retval ptr New Zlib Compression Data
*/
static struct ZlibComprData *zlib_cdata_new(void)
{
return MUTT_MEM_CALLOC(1, struct ZlibComprData);
}
/**
* compr_zlib_open - Open a compression context - Implements ComprOps::open() - @ingroup compress_open
*/
static ComprHandle *compr_zlib_open(short level)
{
struct ZlibComprData *cdata = zlib_cdata_new();
cdata->buf = mutt_mem_calloc(compressBound(1024 * 32), 1);
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_zlib_ops.name, MIN_COMP_LEVEL, MAX_COMP_LEVEL);
level = MIN_COMP_LEVEL;
}
cdata->level = level;
// Return an opaque pointer
return (ComprHandle *) cdata;
}
/**
* compr_zlib_compress - Compress header cache data - Implements ComprOps::compress() - @ingroup compress_compress
*/
static void *compr_zlib_compress(ComprHandle *handle, const char *data,
size_t dlen, size_t *clen)
{
if (!handle)
return NULL;
// Decloak an opaque pointer
struct ZlibComprData *cdata = handle;
uLong len = compressBound(dlen);
mutt_mem_realloc(&cdata->buf, len + 4);
Bytef *cbuf = (unsigned char *) cdata->buf + 4;
const void *ubuf = data;
int rc = compress2(cbuf, &len, ubuf, dlen, cdata->level);
if (rc != Z_OK)
return NULL; // LCOV_EXCL_LINE
*clen = len + 4;
/* save ulen to first 4 bytes */
unsigned char *cs = cdata->buf;
cs[0] = dlen & 0xff;
dlen >>= 8;
cs[1] = dlen & 0xff;
dlen >>= 8;
cs[2] = dlen & 0xff;
dlen >>= 8;
cs[3] = dlen & 0xff;
return cdata->buf;
}
/**
* compr_zlib_decompress - Decompress header cache data - Implements ComprOps::decompress() - @ingroup compress_decompress
*/
static void *compr_zlib_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
{
if (!handle)
return NULL;
// Decloak an opaque pointer
struct ZlibComprData *cdata = handle;
/* first 4 bytes store the size */
const unsigned char *cs = (const unsigned char *) cbuf;
if (clen < 4)
return NULL;
uLong ulen = cs[0] + (cs[1] << 8) + (cs[2] << 16) + ((uLong) cs[3] << 24);
if (ulen == 0)
return NULL;
mutt_mem_realloc(&cdata->buf, ulen);
Bytef *ubuf = cdata->buf;
cs = (const unsigned char *) cbuf;
int rc = uncompress(ubuf, &ulen, cs + 4, clen - 4);
if (rc != Z_OK)
return NULL;
return ubuf;
}
/**
* compr_zlib_close - Close a compression context - Implements ComprOps::close() - @ingroup compress_close
*/
static void compr_zlib_close(ComprHandle **ptr)
{
if (!ptr || !*ptr)
return;
// Decloak an opaque pointer
zlib_cdata_free((struct ZlibComprData **) ptr);
}
COMPRESS_OPS(zlib, MIN_COMP_LEVEL, MAX_COMP_LEVEL)
|