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