NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lz4.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <limits.h>
34#include <lz4.h>
35#include <stddef.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 12
42
47{
48 void *buf;
49 short level;
50};
51
56void lz4_cdata_free(struct Lz4ComprData **ptr)
57{
58 if (!ptr || !*ptr)
59 return;
60
61 struct Lz4ComprData *cdata = *ptr;
62 FREE(&cdata->buf);
63
64 FREE(ptr);
65}
66
71static struct Lz4ComprData *lz4_cdata_new(void)
72{
73 return MUTT_MEM_CALLOC(1, struct Lz4ComprData);
74}
75
80{
81 struct Lz4ComprData *cdata = lz4_cdata_new();
82
83 cdata->buf = mutt_mem_calloc(LZ4_compressBound(1024 * 32), 1);
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_lz4_compress(ComprHandle *handle, const char *data,
102 size_t dlen, size_t *clen)
103{
104 if (!handle || (dlen > INT_MAX))
105 return NULL;
106
107 // Decloak an opaque pointer
108 struct Lz4ComprData *cdata = handle;
109
110 int datalen = dlen;
111 int len = LZ4_compressBound(dlen);
112 if (len > (INT_MAX - 4))
113 return NULL; // LCOV_EXCL_LINE
114 mutt_mem_realloc(&cdata->buf, len + 4);
115 char *cbuf = cdata->buf;
116
117 len = LZ4_compress_fast(data, cbuf + 4, datalen, len, cdata->level);
118 if (len == 0)
119 return NULL; // LCOV_EXCL_LINE
120 *clen = len + 4;
121
122 /* save ulen to first 4 bytes */
123 unsigned char *cs = cdata->buf;
124 cs[0] = dlen & 0xff;
125 dlen >>= 8;
126 cs[1] = dlen & 0xff;
127 dlen >>= 8;
128 cs[2] = dlen & 0xff;
129 dlen >>= 8;
130 cs[3] = dlen & 0xff;
131
132 return cdata->buf;
133}
134
138static void *compr_lz4_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
139{
140 if (!handle)
141 return NULL;
142
143 // Decloak an opaque pointer
144 struct Lz4ComprData *cdata = handle;
145
146 /* first 4 bytes store the size */
147 const unsigned char *cs = (const unsigned char *) cbuf;
148 if (clen < 4)
149 return NULL;
150 size_t ulen = cs[0] + (cs[1] << 8) + (cs[2] << 16) + ((size_t) cs[3] << 24);
151 if (ulen > INT_MAX)
152 return NULL; // LCOV_EXCL_LINE
153 if (ulen == 0)
154 return (void *) cbuf;
155
156 mutt_mem_realloc(&cdata->buf, ulen);
157 void *ubuf = cdata->buf;
158 const char *data = cbuf;
159 int rc = LZ4_decompress_safe(data + 4, ubuf, clen - 4, ulen);
160 if (rc < 0)
161 return NULL;
162
163 return ubuf;
164}
165
169static void compr_lz4_close(ComprHandle **ptr)
170{
171 if (!ptr || !*ptr)
172 return;
173
174 // Decloak an opaque pointer
175 lz4_cdata_free((struct Lz4ComprData **) ptr);
176}
177
void ComprHandle
Opaque type for compression data.
Definition: lib.h:56
const struct ComprOps compr_lz4_ops
#define COMPRESS_OPS(_name, _min_level, _max_level)
Definition: private.h:26
static void compr_lz4_close(ComprHandle **ptr)
Close a compression context - Implements ComprOps::close() -.
Definition: lz4.c:169
static void * compr_lz4_compress(ComprHandle *handle, const char *data, size_t dlen, size_t *clen)
Compress header cache data - Implements ComprOps::compress() -.
Definition: lz4.c:101
static void * compr_lz4_decompress(ComprHandle *handle, const char *cbuf, size_t clen)
Decompress header cache data - Implements ComprOps::decompress() -.
Definition: lz4.c:138
static ComprHandle * compr_lz4_open(short level)
Open a compression context - Implements ComprOps::open() -.
Definition: lz4.c:79
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void lz4_cdata_free(struct Lz4ComprData **ptr)
Free Lz4 Compression Data.
Definition: lz4.c:56
#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
static struct Lz4ComprData * lz4_cdata_new(void)
Create new Lz4 Compression Data.
Definition: lz4.c:71
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 Lz4 Compression Data.
Definition: lz4.c:47
void * buf
Temporary buffer.
Definition: lz4.c:48
short level
Compression Level to be used.
Definition: lz4.c:49