Rizin
unix-like reverse engineering framework and cli tools
zip_algorithm_zstd.c File Reference
#include "zipint.h"
#include <limits.h>
#include <stdlib.h>
#include <zstd.h>
#include <zstd_errors.h>

Go to the source code of this file.

Classes

struct  ctx
 

Functions

static zip_uint64_t maximum_compressed_size (zip_uint64_t uncompressed_size)
 
static void * allocate (bool compress, int compression_flags, zip_error_t *error)
 
static void * compress_allocate (zip_uint16_t method, int compression_flags, zip_error_t *error)
 
static void * decompress_allocate (zip_uint16_t method, int compression_flags, zip_error_t *error)
 
static void deallocate (void *ud)
 
static zip_uint16_t general_purpose_bit_flags (void *ud)
 
static int map_error (size_t ret)
 
static bool start (void *ud, zip_stat_t *st, zip_file_attributes_t *attributes)
 
static bool end (void *ud)
 
static bool input (void *ud, zip_uint8_t *data, zip_uint64_t length)
 
static void end_of_input (void *ud)
 
static zip_compression_status_t process (void *ud, zip_uint8_t *data, zip_uint64_t *length)
 

Variables

zip_compression_algorithm_t zip_algorithm_zstd_compress
 
zip_compression_algorithm_t zip_algorithm_zstd_decompress
 

Function Documentation

◆ allocate()

static void* allocate ( bool  compress,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 59 of file zip_algorithm_zstd.c.

59  {
60  struct ctx *ctx;
61 
62  /* 0: let zstd choose */
63  if (compression_flags < ZSTD_minCLevel() || compression_flags > ZSTD_maxCLevel()) {
65  }
66 
67  if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
68  return NULL;
69  }
70 
71  ctx->error = error;
74  ctx->end_of_input = false;
75 
76  ctx->zdstream = NULL;
77  ctx->zcstream = NULL;
78  ctx->in.src = NULL;
79  ctx->in.pos = 0;
80  ctx->in.size = 0;
81  ctx->out.dst = NULL;
82  ctx->out.pos = 0;
83  ctx->out.size = 0;
84 
85  return ctx;
86 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
#define NULL
Definition: cris-opc.c:27
void * malloc(size_t size)
Definition: malloc.c:123
ZSTD_CStream * zcstream
ZSTD_outBuffer out
bool end_of_input
ZSTD_DStream * zdstream
int compression_flags
bool compress
zip_error_t * error
ZSTD_inBuffer in
void error(const char *msg)
Definition: untgz.c:593

References ctx::compress, compress(), ctx::compression_flags, ctx::end_of_input, ctx::error, error(), ctx::in, malloc(), NULL, ctx::out, ctx::zcstream, and ctx::zdstream.

Referenced by compress_allocate(), and decompress_allocate().

◆ compress_allocate()

static void* compress_allocate ( zip_uint16_t  method,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 90 of file zip_algorithm_zstd.c.

90  {
91  return allocate(true, compression_flags, error);
92 }
static void * allocate(bool compress, int compression_flags, zip_error_t *error)

References allocate(), ctx::compression_flags, and error().

◆ deallocate()

static void deallocate ( void *  ud)
static

Definition at line 102 of file zip_algorithm_zstd.c.

102  {
103  struct ctx *ctx = (struct ctx *)ud;
104  free(ctx);
105 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

◆ decompress_allocate()

static void* decompress_allocate ( zip_uint16_t  method,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 96 of file zip_algorithm_zstd.c.

96  {
97  return allocate(false, compression_flags, error);
98 }

References allocate(), ctx::compression_flags, and error().

◆ end()

static bool end ( void *  ud)
static

Definition at line 174 of file zip_algorithm_zstd.c.

174  {
175  struct ctx *ctx = (struct ctx *)ud;
176  size_t ret;
177 
178  if (ctx->compress) {
179  ret = ZSTD_freeCStream(ctx->zcstream);
180  ctx->zcstream = NULL;
181  }
182  else {
183  ret = ZSTD_freeDStream(ctx->zdstream);
184  ctx->zdstream = NULL;
185  }
186 
187  if (ZSTD_isError(ret)) {
188  zip_error_set(ctx->error, map_error(ret), 0);
189  return false;
190  }
191 
192  return true;
193 }
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
static int map_error(size_t ret)

References ctx::compress, ctx::error, map_error(), NULL, ctx::zcstream, ctx::zdstream, and zip_error_set().

◆ end_of_input()

static void end_of_input ( void *  ud)
static

Definition at line 211 of file zip_algorithm_zstd.c.

211  {
212  struct ctx *ctx = (struct ctx *)ud;
213 
214  ctx->end_of_input = true;
215 }

References ctx::end_of_input.

◆ general_purpose_bit_flags()

static zip_uint16_t general_purpose_bit_flags ( void *  ud)
static

Definition at line 109 of file zip_algorithm_zstd.c.

109  {
110  /* struct ctx *ctx = (struct ctx *)ud; */
111  return 0;
112 }

◆ input()

static bool input ( void *  ud,
zip_uint8_t data,
zip_uint64_t  length 
)
static

Definition at line 197 of file zip_algorithm_zstd.c.

197  {
198  struct ctx *ctx = (struct ctx *)ud;
199  if (length > SIZE_MAX || ctx->in.pos != ctx->in.size) {
201  return false;
202  }
203  ctx->in.src = (const void *)data;
204  ctx->in.size = (size_t)length;
205  ctx->in.pos = 0;
206  return true;
207 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
#define ZIP_ER_INVAL
Definition: zip.h:123
int size_t
Definition: sftypes.h:40
#define SIZE_MAX

References ctx::error, ctx::in, length, SIZE_MAX, ZIP_ER_INVAL, and zip_error_set().

◆ map_error()

static int map_error ( size_t  ret)
static

Definition at line 115 of file zip_algorithm_zstd.c.

115  {
116  switch (ret) {
117  case ZSTD_error_no_error:
118  return ZIP_ER_OK;
119 
120  case ZSTD_error_corruption_detected:
121  case ZSTD_error_checksum_wrong:
122  case ZSTD_error_dictionary_corrupted:
123  case ZSTD_error_dictionary_wrong:
124  return ZIP_ER_COMPRESSED_DATA;
125 
126  case ZSTD_error_memory_allocation:
127  return ZIP_ER_MEMORY;
128 
129  case ZSTD_error_parameter_unsupported:
130  case ZSTD_error_parameter_outOfBound:
131  return ZIP_ER_INVAL;
132 
133  default:
134  return ZIP_ER_INTERNAL;
135  }
136 }
#define ZIP_ER_INTERNAL
Definition: zip.h:125
#define ZIP_ER_COMPRESSED_DATA
Definition: zip.h:136
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_ER_OK
Definition: zip.h:105

References ZIP_ER_COMPRESSED_DATA, ZIP_ER_INTERNAL, ZIP_ER_INVAL, ZIP_ER_MEMORY, and ZIP_ER_OK.

Referenced by end(), process(), and start().

◆ maximum_compressed_size()

static zip_uint64_t maximum_compressed_size ( zip_uint64_t  uncompressed_size)
static

Definition at line 53 of file zip_algorithm_zstd.c.

53  {
54  return ZSTD_compressBound(uncompressed_size);
55 }
uint64_t uncompressed_size
Definition: list.c:106

References uncompressed_size.

◆ process()

static zip_compression_status_t process ( void *  ud,
zip_uint8_t data,
zip_uint64_t length 
)
static

Definition at line 219 of file zip_algorithm_zstd.c.

219  {
220  struct ctx *ctx = (struct ctx *)ud;
221 
222  size_t ret;
223 
224  if (ctx->in.pos == ctx->in.size && !ctx->end_of_input) {
225  *length = 0;
227  }
228 
229  ctx->out.dst = data;
230  ctx->out.pos = 0;
231  ctx->out.size = ZIP_MIN(SIZE_MAX, *length);
232 
233  if (ctx->compress) {
234  if (ctx->in.pos == ctx->in.size && ctx->end_of_input) {
235  ret = ZSTD_endStream(ctx->zcstream, &ctx->out);
236  if (ret == 0) {
237  *length = ctx->out.pos;
238  return ZIP_COMPRESSION_END;
239  }
240  }
241  else {
242  ret = ZSTD_compressStream(ctx->zcstream, &ctx->out, &ctx->in);
243  }
244  }
245  else {
246  ret = ZSTD_decompressStream(ctx->zdstream, &ctx->out, &ctx->in);
247  }
248  if (ZSTD_isError(ret)) {
249  zip_error_set(ctx->error, map_error(ret), 0);
250  return ZIP_COMPRESSION_ERROR;
251  }
252 
253  *length = ctx->out.pos;
254  if (ctx->in.pos == ctx->in.size) {
256  }
257 
258  return ZIP_COMPRESSION_OK;
259 }
#define ZIP_MIN(a, b)
Definition: zipint.h:473
@ ZIP_COMPRESSION_NEED_DATA
Definition: zipint.h:119
@ ZIP_COMPRESSION_ERROR
Definition: zipint.h:118
@ ZIP_COMPRESSION_END
Definition: zipint.h:117
@ ZIP_COMPRESSION_OK
Definition: zipint.h:116

References ctx::compress, ctx::end_of_input, ctx::error, ctx::in, length, map_error(), ctx::out, SIZE_MAX, ctx::zcstream, ctx::zdstream, ZIP_COMPRESSION_END, ZIP_COMPRESSION_ERROR, ZIP_COMPRESSION_NEED_DATA, ZIP_COMPRESSION_OK, zip_error_set(), and ZIP_MIN.

◆ start()

static bool start ( void *  ud,
zip_stat_t st,
zip_file_attributes_t attributes 
)
static

Definition at line 140 of file zip_algorithm_zstd.c.

140  {
141  struct ctx *ctx = (struct ctx *)ud;
142  ctx->in.src = NULL;
143  ctx->in.pos = 0;
144  ctx->in.size = 0;
145  ctx->out.dst = NULL;
146  ctx->out.pos = 0;
147  ctx->out.size = 0;
148  if (ctx->compress) {
149  size_t ret;
150  ctx->zcstream = ZSTD_createCStream();
151  if (ctx->zcstream == NULL) {
153  return false;
154  }
155  ret = ZSTD_initCStream(ctx->zcstream, ctx->compression_flags);
156  if (ZSTD_isError(ret)) {
158  return false;
159  }
160  }
161  else {
162  ctx->zdstream = ZSTD_createDStream();
163  if (ctx->zdstream == NULL) {
165  return false;
166  }
167  }
168 
169  return true;
170 }
#define ZIP_ER_ZLIB
Definition: zip.h:118

References ctx::compress, ctx::compression_flags, ctx::error, ctx::in, map_error(), NULL, ctx::out, ctx::zcstream, ctx::zdstream, ZIP_ER_MEMORY, ZIP_ER_ZLIB, and zip_error_set().

Variable Documentation

◆ zip_algorithm_zstd_compress

zip_compression_algorithm_t zip_algorithm_zstd_compress
Initial value:
= {
20,
end,
}
static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size)
static void deallocate(void *ud)
static void * compress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error)
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes)
static bool end(void *ud)
static zip_uint16_t general_purpose_bit_flags(void *ud)
static void end_of_input(void *ud)
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length)

Definition at line 267 of file zip_algorithm_zstd.c.

◆ zip_algorithm_zstd_decompress

zip_compression_algorithm_t zip_algorithm_zstd_decompress
Initial value:
= {
20,
end,
}
static void * decompress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error)

Definition at line 281 of file zip_algorithm_zstd.c.