NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
zstd.c
Go to the documentation of this file.
1 
30 #include "config.h"
31 #include <stdio.h>
32 #include <zstd.h>
33 #include "private.h"
34 #include "mutt/lib.h"
35 #include "lib.h"
36 
37 #define MIN_COMP_LEVEL 1
38 #define MAX_COMP_LEVEL 22
39 
40 
44 {
45  void *buf;
46  short level;
47 
48  ZSTD_CCtx *cctx;
49  ZSTD_DCtx *dctx;
50 };
51 
55 static void *compr_zstd_open(short level)
56 {
57  struct ComprZstdCtx *ctx = mutt_mem_malloc(sizeof(struct ComprZstdCtx));
58 
59  ctx->buf = mutt_mem_malloc(ZSTD_compressBound(1024 * 128));
60  ctx->cctx = ZSTD_createCCtx();
61  ctx->dctx = ZSTD_createDCtx();
62 
63  if (!ctx->cctx || !ctx->dctx)
64  {
65  // LCOV_EXCL_START
66  ZSTD_freeCCtx(ctx->cctx);
67  ZSTD_freeDCtx(ctx->dctx);
68  FREE(&ctx->buf);
69  FREE(&ctx);
70  return NULL;
71  // LCOV_EXCL_STOP
72  }
73 
74  if ((level < MIN_COMP_LEVEL) || (level > MAX_COMP_LEVEL))
75  {
76  mutt_debug(LL_DEBUG1, "The compression level for %s should be between %d and %d",
78  level = MIN_COMP_LEVEL;
79  }
80 
81  ctx->level = level;
82 
83  return ctx;
84 }
85 
89 static void *compr_zstd_compress(void *cctx, const char *data, size_t dlen, size_t *clen)
90 {
91  if (!cctx)
92  return NULL;
93 
94  struct ComprZstdCtx *ctx = cctx;
95 
96  size_t len = ZSTD_compressBound(dlen);
97  mutt_mem_realloc(&ctx->buf, len);
98 
99  size_t ret = ZSTD_compressCCtx(ctx->cctx, ctx->buf, len, data, dlen, ctx->level);
100  if (ZSTD_isError(ret))
101  return NULL; // LCOV_EXCL_LINE
102 
103  *clen = ret;
104 
105  return ctx->buf;
106 }
107 
111 static void *compr_zstd_decompress(void *cctx, const char *cbuf, size_t clen)
112 {
113  struct ComprZstdCtx *ctx = cctx;
114 
115  if (!cctx)
116  return NULL;
117 
118  unsigned long long len = ZSTD_getFrameContentSize(cbuf, clen);
119  if (len == ZSTD_CONTENTSIZE_UNKNOWN)
120  return NULL; // LCOV_EXCL_LINE
121  else if (len == ZSTD_CONTENTSIZE_ERROR)
122  return NULL;
123  else if (len == 0)
124  return NULL; // LCOV_EXCL_LINE
125  mutt_mem_realloc(&ctx->buf, len);
126 
127  size_t ret = ZSTD_decompressDCtx(ctx->dctx, ctx->buf, len, cbuf, clen);
128  if (ZSTD_isError(ret))
129  return NULL; // LCOV_EXCL_LINE
130 
131  return ctx->buf;
132 }
133 
137 static void compr_zstd_close(void **cctx)
138 {
139  if (!cctx || !*cctx)
140  return;
141 
142  struct ComprZstdCtx *ctx = *cctx;
143 
144  if (ctx->cctx)
145  ZSTD_freeCCtx(ctx->cctx);
146 
147  if (ctx->dctx)
148  ZSTD_freeDCtx(ctx->dctx);
149 
150  FREE(&ctx->buf);
151  FREE(cctx);
152 }
153 
#define COMPRESS_OPS(_name, _min_level, _max_level)
Definition: private.h:26
static void * compr_zstd_compress(void *cctx, const char *data, size_t dlen, size_t *clen)
Implements ComprOps::compress() -.
Definition: zstd.c:89
ZSTD_CCtx * cctx
Compression context.
Definition: zstd.c:48
const char * name
Compression name.
Definition: lib.h:61
Shared constants/structs that are private to Compression.
#define MAX_COMP_LEVEL
Maximum compression level for zstd.
Definition: zstd.c:38
API for the header cache compression.
Private Zstandard Compression Context.
Definition: zstd.c:43
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
const struct ComprOps compr_zstd_ops
static void * compr_zstd_decompress(void *cctx, const char *cbuf, size_t clen)
Implements ComprOps::decompress() -.
Definition: zstd.c:111
#define MIN_COMP_LEVEL
Minimum compression level for zstd.
Definition: zstd.c:37
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
ZSTD_DCtx * dctx
Decompression context.
Definition: zstd.c:49
void * buf
Temporary buffer.
Definition: zstd.c:45
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
#define FREE(x)
Definition: memory.h:40
static void compr_zstd_close(void **cctx)
Implements ComprOps::close() -.
Definition: zstd.c:137
static void * compr_zstd_open(short level)
Implements ComprOps::open() -.
Definition: zstd.c:55
Convenience wrapper for the library headers.
short level
Compression Level to be used.
Definition: zstd.c:46