NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
zlib.c
Go to the documentation of this file.
1
24
31
32#include "config.h"
33#include <stddef.h>
34#include <stdint.h>
35#include <zconf.h>
36#include <zlib.h>
37#include "private.h"
38#include "mutt/lib.h"
39#include "lib.h"
40
41#define MIN_COMP_LEVEL 1
42#define MAX_COMP_LEVEL 9
43
48{
49 void *buf;
50 short level;
51};
52
58{
59 if (!ptr || !*ptr)
60 return;
61
62 struct ZlibComprData *cdata = *ptr;
63 FREE(&cdata->buf);
64
65 FREE(ptr);
66}
67
72static struct ZlibComprData *zlib_cdata_new(void)
73{
74 return MUTT_MEM_CALLOC(1, struct ZlibComprData);
75}
76
81{
82 struct ZlibComprData *cdata = zlib_cdata_new();
83
84 cdata->buf = mutt_mem_calloc(compressBound(1024 * 32), 1);
85
87 {
88 mutt_debug(LL_DEBUG1, "The compression level for %s should be between %d and %d\n",
91 }
92
93 cdata->level = level;
94
95 // Return an opaque pointer
96 return (ComprHandle *) cdata;
97}
98
102static void *compr_zlib_compress(ComprHandle *handle, const char *data,
103 size_t dlen, size_t *clen)
104{
105 if (!handle || (dlen > UINT32_MAX))
106 return NULL;
107
108 // Decloak an opaque pointer
109 struct ZlibComprData *cdata = handle;
110
111 uLong len = compressBound(dlen);
112 mutt_mem_realloc(&cdata->buf, len + 4);
113 Bytef *cbuf = (unsigned char *) cdata->buf + 4;
114 const void *ubuf = data;
115 int rc = compress2(cbuf, &len, ubuf, dlen, cdata->level);
116 if (rc != Z_OK)
117 return NULL; // LCOV_EXCL_LINE
118 *clen = len + 4;
119
120 /* save ulen to first 4 bytes */
121 unsigned char *cs = cdata->buf;
122 cs[0] = dlen & 0xff;
123 dlen >>= 8;
124 cs[1] = dlen & 0xff;
125 dlen >>= 8;
126 cs[2] = dlen & 0xff;
127 dlen >>= 8;
128 cs[3] = dlen & 0xff;
129
130 return cdata->buf;
131}
132
136static void *compr_zlib_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
137{
138 if (!handle)
139 return NULL;
140
141 // Decloak an opaque pointer
142 struct ZlibComprData *cdata = handle;
143
144 /* first 4 bytes store the size */
145 const unsigned char *cs = (const unsigned char *) cbuf;
146 if (clen < 4)
147 return NULL;
148 uLong ulen = cs[0] + (cs[1] << 8) + (cs[2] << 16) + ((uLong) cs[3] << 24);
149 if (ulen == 0)
150 return NULL;
151
152 mutt_mem_realloc(&cdata->buf, ulen);
153 Bytef *ubuf = cdata->buf;
154 cs = (const unsigned char *) cbuf;
155 int rc = uncompress(ubuf, &ulen, cs + 4, clen - 4);
156 if (rc != Z_OK)
157 return NULL;
158
159 return ubuf;
160}
161
166{
167 if (!ptr || !*ptr)
168 return;
169
170 // Decloak an opaque pointer
171 zlib_cdata_free((struct ZlibComprData **) ptr);
172}
173
API for the header cache compression.
const struct ComprOps compr_zlib_ops
void ComprHandle
Opaque type for compression data.
Definition lib.h:57
Shared constants/structs that are private to Compression.
#define COMPRESS_OPS(_name, _min_level, _max_level)
Definition private.h:26
static void compr_zlib_close(ComprHandle **ptr)
Close a compression context - Implements ComprOps::close() -.
Definition zlib.c:165
static void * compr_zlib_compress(ComprHandle *handle, const char *data, size_t dlen, size_t *clen)
Compress header cache data - Implements ComprOps::compress() -.
Definition zlib.c:102
static void * compr_zlib_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
Decompress header cache data - Implements ComprOps::decompress() -.
Definition zlib.c:136
static ComprHandle * compr_zlib_open(short level)
Open a compression context - Implements ComprOps::open() -.
Definition zlib.c:80
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define MAX_COMP_LEVEL
Maximum compression level for lz4.
Definition lz4.c:41
#define MIN_COMP_LEVEL
Minimum compression level for lz4.
Definition lz4.c:40
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:76
void mutt_mem_realloc(void *pptr, size_t size)
Resize a block of memory on the heap.
Definition memory.c:146
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
Convenience wrapper for the library headers.
Private Zlib Compression Data.
Definition zlib.c:48
short level
Compression Level to be used.
Definition zlib.c:50
void * buf
Temporary buffer.
Definition zlib.c:49
void zlib_cdata_free(struct ZlibComprData **ptr)
Free Zlib Compression Data.
Definition zlib.c:57
static struct ZlibComprData * zlib_cdata_new(void)
Create new Zlib Compression Data.
Definition zlib.c:72