NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
zstd.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <stdio.h>
34#include <zstd.h>
35#include "private.h"
36#include "mutt/lib.h"
37#include "lib.h"
38
39#define MIN_COMP_LEVEL 1
40#define MAX_COMP_LEVEL 22
41
46{
47 void *buf;
48 short level;
49
50 ZSTD_CCtx *cctx;
51 ZSTD_DCtx *dctx;
52};
53
59{
60 if (!ptr || !*ptr)
61 return;
62
63 struct ZstdComprData *cdata = *ptr;
64 FREE(&cdata->buf);
65
66 FREE(ptr);
67}
68
73static struct ZstdComprData *zstd_cdata_new(void)
74{
75 return MUTT_MEM_CALLOC(1, struct ZstdComprData);
76}
77
82{
83 struct ZstdComprData *cdata = zstd_cdata_new();
84
85 cdata->buf = mutt_mem_calloc(ZSTD_compressBound(1024 * 128), 1);
86 cdata->cctx = ZSTD_createCCtx();
87 cdata->dctx = ZSTD_createDCtx();
88
89 if (!cdata->cctx || !cdata->dctx)
90 {
91 // LCOV_EXCL_START
92 ZSTD_freeCCtx(cdata->cctx);
93 ZSTD_freeDCtx(cdata->dctx);
94 zstd_cdata_free(&cdata);
95 return NULL;
96 // LCOV_EXCL_STOP
97 }
98
100 {
101 mutt_debug(LL_DEBUG1, "The compression level for %s should be between %d and %d",
104 }
105
106 cdata->level = level;
107
108 // Return an opaque pointer
109 return (ComprHandle *) cdata;
110}
111
115static void *compr_zstd_compress(ComprHandle *handle, const char *data,
116 size_t dlen, size_t *clen)
117{
118 if (!handle)
119 return NULL;
120
121 // Decloak an opaque pointer
122 struct ZstdComprData *cdata = handle;
123
124 size_t len = ZSTD_compressBound(dlen);
125 mutt_mem_realloc(&cdata->buf, len);
126
127 size_t rc = ZSTD_compressCCtx(cdata->cctx, cdata->buf, len, data, dlen, cdata->level);
128 if (ZSTD_isError(rc))
129 return NULL; // LCOV_EXCL_LINE
130
131 *clen = rc;
132
133 return cdata->buf;
134}
135
139static void *compr_zstd_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
140{
141 if (!handle)
142 return NULL;
143
144 // Decloak an opaque pointer
145 struct ZstdComprData *cdata = handle;
146
147 unsigned long long len = ZSTD_getFrameContentSize(cbuf, clen);
148 if (len == ZSTD_CONTENTSIZE_UNKNOWN)
149 return NULL; // LCOV_EXCL_LINE
150 else if (len == ZSTD_CONTENTSIZE_ERROR)
151 return NULL;
152 else if (len == 0)
153 return NULL; // LCOV_EXCL_LINE
154 mutt_mem_realloc(&cdata->buf, len);
155
156 size_t rc = ZSTD_decompressDCtx(cdata->dctx, cdata->buf, len, cbuf, clen);
157 if (ZSTD_isError(rc))
158 return NULL; // LCOV_EXCL_LINE
159
160 return cdata->buf;
161}
162
167{
168 if (!ptr || !*ptr)
169 return;
170
171 // Decloak an opaque pointer
172 struct ZstdComprData *cdata = *ptr;
173
174 if (cdata->cctx)
175 ZSTD_freeCCtx(cdata->cctx);
176
177 if (cdata->dctx)
178 ZSTD_freeDCtx(cdata->dctx);
179
180 zstd_cdata_free((struct ZstdComprData **) ptr);
181}
182
void ComprHandle
Opaque type for compression data.
Definition: lib.h:56
const struct ComprOps compr_zstd_ops
#define COMPRESS_OPS(_name, _min_level, _max_level)
Definition: private.h:26
static void compr_zstd_close(ComprHandle **ptr)
Close a compression context - Implements ComprOps::close() -.
Definition: zstd.c:166
static void * compr_zstd_compress(ComprHandle *handle, const char *data, size_t dlen, size_t *clen)
Compress header cache data - Implements ComprOps::compress() -.
Definition: zstd.c:115
static void * compr_zstd_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
Decompress header cache data - Implements ComprOps::decompress() -.
Definition: zstd.c:139
static ComprHandle * compr_zstd_open(short level)
Open a compression context - Implements ComprOps::open() -.
Definition: zstd.c:81
#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:77
void mutt_mem_realloc(void *pptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:147
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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 Zstandard Compression Data.
Definition: zstd.c:46
short level
Compression Level to be used.
Definition: zstd.c:48
ZSTD_CCtx * cctx
Compression context.
Definition: zstd.c:50
ZSTD_DCtx * dctx
Decompression context.
Definition: zstd.c:51
void * buf
Temporary buffer.
Definition: zstd.c:47
void zstd_cdata_free(struct ZstdComprData **ptr)
Free Zstandard Compression Data.
Definition: zstd.c:58
static struct ZstdComprData * zstd_cdata_new(void)
Create new Zstandard Compression Data.
Definition: zstd.c:73
#define MAX_COMP_LEVEL
Maximum compression level for zstd.
Definition: zstd.c:40
#define MIN_COMP_LEVEL
Minimum compression level for zstd.
Definition: zstd.c:39