Rizin
unix-like reverse engineering framework and cli tools
zip_algorithm_bzip2.c File Reference
#include "zipint.h"
#include <bzlib.h>
#include <limits.h>
#include <stdlib.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 (int 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_bzip2_compress
 
zip_compression_algorithm_t zip_algorithm_bzip2_decompress
 

Function Documentation

◆ allocate()

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

Definition at line 61 of file zip_algorithm_bzip2.c.

61  {
62  struct ctx *ctx;
63 
64  if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
65  return NULL;
66  }
67 
68  ctx->error = error;
71  if (ctx->compression_flags < 1 || ctx->compression_flags > 9) {
73  }
74  ctx->end_of_input = false;
75 
76  ctx->zstr.bzalloc = NULL;
77  ctx->zstr.bzfree = NULL;
78  ctx->zstr.opaque = NULL;
79 
80  return ctx;
81 }
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
bool end_of_input
bz_stream zstr
int compression_flags
bool compress
zip_error_t * error
void error(const char *msg)
Definition: untgz.c:593

References ctx::compress, compress(), ctx::compression_flags, ctx::end_of_input, ctx::error, error(), malloc(), NULL, and ctx::zstr.

Referenced by compress_allocate(), decompress_allocate(), gzfilebuf::overflow(), and gzfilebuf::underflow().

◆ compress_allocate()

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

Definition at line 85 of file zip_algorithm_bzip2.c.

85  {
86  return allocate(true, compression_flags, error);
87 }
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 97 of file zip_algorithm_bzip2.c.

97  {
98  struct ctx *ctx = (struct ctx *)ud;
99 
100  free(ctx);
101 }
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 91 of file zip_algorithm_bzip2.c.

91  {
92  return allocate(false, compression_flags, error);
93 }

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

◆ end()

static bool end ( void *  ud)
static

Definition at line 169 of file zip_algorithm_bzip2.c.

169  {
170  struct ctx *ctx = (struct ctx *)ud;
171  int err;
172 
173  if (ctx->compress) {
174  err = BZ2_bzCompressEnd(&ctx->zstr);
175  }
176  else {
177  err = BZ2_bzDecompressEnd(&ctx->zstr);
178  }
179 
180  if (err != BZ_OK) {
182  return false;
183  }
184 
185  return true;
186 }
static bool err
Definition: armass.c:435
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
static int map_error(int ret)

References ctx::compress, err, ctx::error, map_error(), zip_error_set(), and ctx::zstr.

◆ end_of_input()

static void end_of_input ( void *  ud)
static

Definition at line 206 of file zip_algorithm_bzip2.c.

206  {
207  struct ctx *ctx = (struct ctx *)ud;
208 
209  ctx->end_of_input = true;
210 }

References ctx::end_of_input.

◆ general_purpose_bit_flags()

static zip_uint16_t general_purpose_bit_flags ( void *  ud)
static

Definition at line 105 of file zip_algorithm_bzip2.c.

105  {
106  return 0;
107 }

◆ input()

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

Definition at line 190 of file zip_algorithm_bzip2.c.

190  {
191  struct ctx *ctx = (struct ctx *)ud;
192 
193  if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
195  return false;
196  }
197 
198  ctx->zstr.avail_in = (unsigned int)length;
199  ctx->zstr.next_in = (char *)data;
200 
201  return true;
202 }
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
static int
Definition: sfsocketcall.h:114
#define UINT_MAX
Definition: md5.h:55

References ctx::error, int, length, UINT_MAX, ZIP_ER_INVAL, zip_error_set(), and ctx::zstr.

Referenced by __add_cmdf_panel(), __load_cmdf(), __printPattern(), __set_filter(), autocmplt_cmd_arg_file(), autocompleteFilename(), avr_assembler(), calculate_luhn(), cmd_agraph_edge(), cmd_agraph_node(), cmd_analysis_esil(), cmd_analysis_graph(), cmd_analysis_trampoline(), cmd_dbg_map_jemalloc(), cmd_dcu(), cmd_debug_pid(), cmd_pCd(), cmd_pCD(), cmd_pCx(), cmd_print_bars(), cmd_print_blocks(), cmd_print_format(), cmd_print_fromage(), cmd_print_gadget(), cmd_print_op(), cmd_print_pv(), cmd_pxb_p(), cmd_Quit(), cmd_seek_opcode(), convert_offset_from_input(), core_cmd_tsrzcmd(), cqcheck(), deflate_index_build(), deflate_index_extract(), des_decrypt(), des_encrypt(), disasm_strings(), do_analysis_search(), do_asm_search(), do_esil_search(), do_section_search(), egg_option(), encode_addr16(), encode_addr32(), encode_atype(), encode_const_pool16(), encode_const_pool16_ut8(), encode_const_pool8(), encode_st16(), encode_st8(), encode_ut8(), encode_ut8x2(), func_walk_blocks(), FUZ_AddressOverflow(), get_bit(), get_lines(), getlistmask(), getnummemend(), getnummemendbang(), getreglist(), getregmembang(), getregmemend(), getregmemstart(), getregmemstartend(), getshiftmemend(), hash_context_create_desc_io_string(), hash_parse_cmdline(), iflag(), indx(), is_in_input(), itmask(), java_assembler(), jemalloc_get_bins(), jemalloc_get_chunks(), jemalloc_print_narenas(), kwajd_decompress(), lang_pipe_run(), lua53_assembly(), lua54_assembly(), lzxd_init(), lzxd_set_reference_data(), main(), mpcf_escape_new(), mpcf_unescape_new(), mszipd_init(), oabd_decompress(), oabd_decompress_incremental(), opmask(), parse_hints(), parser__start(), parser_parse(), print_heap_bin(), print_heap_fastbin(), print_main_arena_bins(), qtmd_init(), rop_kuery(), rz_asm_pseudo_align(), rz_asm_pseudo_arch(), rz_asm_pseudo_bits(), rz_asm_pseudo_byte(), rz_asm_pseudo_fill(), rz_asm_pseudo_incbin(), rz_asm_pseudo_int16(), rz_asm_pseudo_int32(), rz_asm_pseudo_int64(), rz_asm_pseudo_intN(), rz_asm_pseudo_org(), rz_asm_pseudo_string(), rz_cmd_alias(), rz_cmd_analysis(), rz_cmd_call(), rz_cmd_cmp_hexpair_string_handler(), rz_cmd_debug(), rz_cmd_debug_continue_syscall(), rz_cmd_debug_continue_until(), rz_cmd_debug_dmi(), rz_cmd_debug_heap_jemalloc(), rz_cmd_debug_trace_addr(), rz_cmd_heap_arena_bins_print_handler(), rz_cmd_heap_bins_list_print(), rz_cmd_heap_fastbins_print(), rz_cmd_help(), rz_cmd_hexdump(), rz_cmd_info_kuery(), rz_cmd_kuery(), rz_cmd_macro(), rz_cmd_panels(), rz_cmd_print(), rz_cmd_remote(), rz_cmd_search(), rz_cmd_shell_mkdir_handler(), rz_cmd_shell_mv_handler(), rz_cmd_visual(), rz_cons_filter(), rz_core_agraph_print(), rz_core_asm_search(), rz_core_asm_strsearch(), rz_core_debug_esil(), rz_core_debug_kill(), rz_core_graph_print(), rz_core_rtr_add(), rz_core_rtr_cmd(), rz_core_rtr_pushout(), rz_core_rtr_rap_run(), rz_core_rtr_remove(), rz_core_rtr_session(), rz_core_visual(), rz_core_visual_analysis(), rz_core_visual_browse(), rz_core_visual_debugtraces(), rz_core_yank_hud_file(), rz_core_yank_hud_path(), rz_equal_g_handler_old(), rz_equal_h_handler_old(), rz_equal_H_handler_old(), rz_hash_cfg_randomart(), rz_hash_xxhash(), rz_io_system_run_oldhandler(), rz_MD5Update(), rz_push_escaped_handler(), rz_seek_search(), rz_sys_cmd_str(), rz_sys_cmd_str_full(), rz_syscmd_ls(), sanitize_cab_filename(), stream_reset(), system_exec_stdin(), szddd_decompress(), tokens_new(), ts_lexer_set_input(), ts_parser_parse(), ts_parser_parse_string_encoding(), ts_parser_parse_wasm(), update(), utf32len(), utf32toutf8(), utf8toutf32(), XXH32(), XXH32_endian_align(), XXH32_round(), XXH32_update(), XXH32_update_endian(), XXH64(), XXH64_endian_align(), XXH64_round(), XXH64_update(), and XXH64_update_endian().

◆ map_error()

static int map_error ( int  ret)
static

Definition at line 111 of file zip_algorithm_bzip2.c.

111  {
112  switch (ret) {
113  case BZ_FINISH_OK:
114  case BZ_FLUSH_OK:
115  case BZ_OK:
116  case BZ_RUN_OK:
117  case BZ_STREAM_END:
118  return ZIP_ER_OK;
119 
120  case BZ_DATA_ERROR:
121  case BZ_DATA_ERROR_MAGIC:
122  case BZ_UNEXPECTED_EOF:
123  return ZIP_ER_COMPRESSED_DATA;
124 
125  case BZ_MEM_ERROR:
126  return ZIP_ER_MEMORY;
127 
128  case BZ_PARAM_ERROR:
129  return ZIP_ER_INVAL;
130 
131  case BZ_CONFIG_ERROR: /* actually, bzip2 miscompiled */
132  case BZ_IO_ERROR:
133  case BZ_OUTBUFF_FULL:
134  case BZ_SEQUENCE_ERROR:
135  return ZIP_ER_INTERNAL;
136 
137  default:
138  return ZIP_ER_INTERNAL;
139  }
140 }
#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 50 of file zip_algorithm_bzip2.c.

50  {
52 
54  return ZIP_UINT64_MAX;
55  }
56  return compressed_size;
57 }
uint64_t compressed_size
Definition: list.c:105
uint64_t uncompressed_size
Definition: list.c:106
uint64_t zip_uint64_t
Definition: zipconf.h:39
#define ZIP_UINT64_MAX
Definition: zipconf.h:55

References compressed_size, uncompressed_size, and ZIP_UINT64_MAX.

◆ process()

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

Definition at line 214 of file zip_algorithm_bzip2.c.

214  {
215  struct ctx *ctx = (struct ctx *)ud;
216 
217  int ret;
218 
219  if (ctx->zstr.avail_in == 0 && !ctx->end_of_input) {
220  *length = 0;
222  }
223 
224  ctx->zstr.avail_out = (unsigned int)ZIP_MIN(UINT_MAX, *length);
225  ctx->zstr.next_out = (char *)data;
226 
227  if (ctx->compress) {
228  ret = BZ2_bzCompress(&ctx->zstr, ctx->end_of_input ? BZ_FINISH : BZ_RUN);
229  }
230  else {
231  ret = BZ2_bzDecompress(&ctx->zstr);
232  }
233 
234  *length = *length - ctx->zstr.avail_out;
235 
236  switch (ret) {
237  case BZ_FINISH_OK: /* compression */
238  return ZIP_COMPRESSION_OK;
239 
240  case BZ_OK: /* decompression */
241  case BZ_RUN_OK: /* compression */
242  if (ctx->zstr.avail_in == 0) {
244  }
245  return ZIP_COMPRESSION_OK;
246 
247  case BZ_STREAM_END:
248  return ZIP_COMPRESSION_END;
249 
250  default:
251  zip_error_set(ctx->error, map_error(ret), 0);
252  return ZIP_COMPRESSION_ERROR;
253  }
254 }
#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, int, length, map_error(), UINT_MAX, ZIP_COMPRESSION_END, ZIP_COMPRESSION_ERROR, ZIP_COMPRESSION_NEED_DATA, ZIP_COMPRESSION_OK, zip_error_set(), ZIP_MIN, and ctx::zstr.

Referenced by exit_wait_callback(), rz_debug_dmp_attach(), rz_debug_dmp_init(), rz_debug_dmp_pids(), uidFromPid(), uv__chld(), uv_process_kill(), and uv_spawn().

◆ start()

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

Definition at line 143 of file zip_algorithm_bzip2.c.

143  {
144  struct ctx *ctx = (struct ctx *)ud;
145  int ret;
146 
147  ctx->zstr.avail_in = 0;
148  ctx->zstr.next_in = NULL;
149  ctx->zstr.avail_out = 0;
150  ctx->zstr.next_out = NULL;
151 
152  if (ctx->compress) {
153  ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30);
154  }
155  else {
156  ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0);
157  }
158 
159  if (ret != BZ_OK) {
160  zip_error_set(ctx->error, map_error(ret), 0);
161  return false;
162  }
163 
164  return true;
165 }

References ctx::compress, ctx::compression_flags, ctx::error, map_error(), NULL, zip_error_set(), and ctx::zstr.

Variable Documentation

◆ zip_algorithm_bzip2_compress

zip_compression_algorithm_t zip_algorithm_bzip2_compress
Initial value:
= {
46,
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 258 of file zip_algorithm_bzip2.c.

◆ zip_algorithm_bzip2_decompress

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

Definition at line 272 of file zip_algorithm_bzip2.c.