Rizin
unix-like reverse engineering framework and cli tools
mszip.h File Reference

Go to the source code of this file.

Classes

struct  mszipd_stream
 

Macros

#define MSZIP_FRAME_SIZE   (32768) /* size of LZ history window */
 
#define MSZIP_LITERAL_MAXSYMBOLS   (288) /* literal/length huffman tree */
 
#define MSZIP_LITERAL_TABLEBITS   (9)
 
#define MSZIP_DISTANCE_MAXSYMBOLS   (32) /* distance huffman tree */
 
#define MSZIP_DISTANCE_TABLEBITS   (6)
 
#define MSZIP_LITERAL_TABLESIZE   (MSZIP_LITERAL_MAXSYMBOLS * 4)
 
#define MSZIP_DISTANCE_TABLESIZE
 

Functions

struct mszipd_streammszipd_init (struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int input_buffer_size, int repair_mode)
 
int mszipd_decompress (struct mszipd_stream *zip, off_t out_bytes)
 
int mszipd_decompress_kwaj (struct mszipd_stream *zip)
 
void mszipd_free (struct mszipd_stream *zip)
 

Macro Definition Documentation

◆ MSZIP_DISTANCE_MAXSYMBOLS

#define MSZIP_DISTANCE_MAXSYMBOLS   (32) /* distance huffman tree */

Definition at line 25 of file mszip.h.

◆ MSZIP_DISTANCE_TABLEBITS

#define MSZIP_DISTANCE_TABLEBITS   (6)

Definition at line 26 of file mszip.h.

◆ MSZIP_DISTANCE_TABLESIZE

#define MSZIP_DISTANCE_TABLESIZE
Value:
#define MSZIP_DISTANCE_MAXSYMBOLS
Definition: mszip.h:25
#define MSZIP_DISTANCE_TABLEBITS
Definition: mszip.h:26

Definition at line 41 of file mszip.h.

◆ MSZIP_FRAME_SIZE

#define MSZIP_FRAME_SIZE   (32768) /* size of LZ history window */

Definition at line 22 of file mszip.h.

◆ MSZIP_LITERAL_MAXSYMBOLS

#define MSZIP_LITERAL_MAXSYMBOLS   (288) /* literal/length huffman tree */

Definition at line 23 of file mszip.h.

◆ MSZIP_LITERAL_TABLEBITS

#define MSZIP_LITERAL_TABLEBITS   (9)

Definition at line 24 of file mszip.h.

◆ MSZIP_LITERAL_TABLESIZE

#define MSZIP_LITERAL_TABLESIZE   (MSZIP_LITERAL_MAXSYMBOLS * 4)

Definition at line 32 of file mszip.h.

Function Documentation

◆ mszipd_decompress()

int mszipd_decompress ( struct mszipd_stream zip,
off_t  out_bytes 
)

Definition at line 384 of file mszipd.c.

384  {
385  /* for the bit buffer */
386  register unsigned int bit_buffer;
387  register int bits_left;
388  unsigned char *i_ptr, *i_end;
389 
390  int i, state, error;
391 
392  /* easy answers */
393  if (!zip || (out_bytes < 0)) return MSPACK_ERR_ARGS;
394  if (zip->error) return zip->error;
395 
396  /* flush out any stored-up bytes before we begin */
397  i = zip->o_end - zip->o_ptr;
398  if ((off_t) i > out_bytes) i = (int) out_bytes;
399  if (i) {
400  if (zip->sys->write(zip->output, zip->o_ptr, i) != i) {
401  return zip->error = MSPACK_ERR_WRITE;
402  }
403  zip->o_ptr += i;
404  out_bytes -= i;
405  }
406  if (out_bytes == 0) return MSPACK_ERR_OK;
407 
408 
409  while (out_bytes > 0) {
410  /* unpack another block */
411  RESTORE_BITS;
412 
413  /* skip to next read 'CK' header */
414  i = bits_left & 7; REMOVE_BITS(i); /* align to bytestream */
415  state = 0;
416  do {
417  READ_BITS(i, 8);
418  if (i == 'C') state = 1;
419  else if ((state == 1) && (i == 'K')) state = 2;
420  else state = 0;
421  } while (state != 2);
422 
423  /* inflate a block, repair and realign if necessary */
424  zip->window_posn = 0;
425  zip->bytes_output = 0;
426  STORE_BITS;
427  if ((error = inflate(zip))) {
428  D(("inflate error %d", error))
429  if (zip->repair_mode) {
430  /* recover partially-inflated buffers */
431  if (zip->bytes_output == 0 && zip->window_posn > 0) {
432  zip->flush_window(zip, zip->window_posn);
433  }
434  zip->sys->message(NULL, "MSZIP error, %u bytes of data lost.",
435  MSZIP_FRAME_SIZE - zip->bytes_output);
436  for (i = zip->bytes_output; i < MSZIP_FRAME_SIZE; i++) {
437  zip->window[i] = '\0';
438  }
439  zip->bytes_output = MSZIP_FRAME_SIZE;
440  }
441  else {
442  return zip->error = (error > 0) ? error : MSPACK_ERR_DECRUNCH;
443  }
444  }
445  zip->o_ptr = &zip->window[0];
446  zip->o_end = &zip->o_ptr[zip->bytes_output];
447 
448  /* write a frame */
449  i = (out_bytes < (off_t)zip->bytes_output) ?
450  (int)out_bytes : zip->bytes_output;
451  if (zip->sys->write(zip->output, zip->o_ptr, i) != i) {
452  return zip->error = MSPACK_ERR_WRITE;
453  }
454 
455  /* mspack errors (i.e. read errors) are fatal and can't be recovered */
456  if ((error > 0) && zip->repair_mode) return error;
457 
458  zip->o_ptr += i;
459  out_bytes -= i;
460  }
461 
462  if (out_bytes) {
463  D(("bytes left to output"))
464  return zip->error = MSPACK_ERR_DECRUNCH;
465  }
466  return MSPACK_ERR_OK;
467 }
lzma_index ** i
Definition: index.h:629
#define D
Definition: block.c:38
#define MSPACK_ERR_OK
Definition: mspack.h:485
#define MSPACK_ERR_WRITE
Definition: mspack.h:493
#define MSPACK_ERR_ARGS
Definition: mspack.h:487
#define MSPACK_ERR_DECRUNCH
Definition: mspack.h:507
#define MSZIP_FRAME_SIZE
Definition: mszip.h:22
static int inflate(struct mszipd_stream *zip)
Definition: mszipd.c:157
#define RESTORE_BITS
Definition: readbits.h:118
#define STORE_BITS
Definition: readbits.h:111
#define REMOVE_BITS(nbits)
Definition: readbits.h:154
#define READ_BITS(val, nbits)
Definition: readbits.h:129
#define NULL
Definition: cris-opc.c:27
static int
Definition: sfsocketcall.h:114
int off_t
Definition: sftypes.h:41
Definition: dis.h:43
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
void error(const char *msg)
Definition: untgz.c:593
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References mszipd_stream::bit_buffer, mszipd_stream::bits_left, D, zip::error, error(), i, mszipd_stream::i_end, mszipd_stream::i_ptr, if(), inflate(), int, MSPACK_ERR_ARGS, MSPACK_ERR_DECRUNCH, MSPACK_ERR_OK, MSPACK_ERR_WRITE, MSZIP_FRAME_SIZE, NULL, READ_BITS, REMOVE_BITS, RESTORE_BITS, and STORE_BITS.

Referenced by cabd_init_decomp().

◆ mszipd_decompress_kwaj()

int mszipd_decompress_kwaj ( struct mszipd_stream zip)

Definition at line 469 of file mszipd.c.

469  {
470  /* for the bit buffer */
471  register unsigned int bit_buffer;
472  register int bits_left;
473  unsigned char *i_ptr, *i_end;
474 
475  int i, error, block_len;
476 
477  /* unpack blocks until block_len == 0 */
478  for (;;) {
479  RESTORE_BITS;
480 
481  /* align to bytestream, read block_len */
482  i = bits_left & 7; REMOVE_BITS(i);
483  READ_BITS(block_len, 8);
484  READ_BITS(i, 8); block_len |= i << 8;
485 
486  if (block_len == 0) break;
487 
488  /* read "CK" header */
489  READ_BITS(i, 8); if (i != 'C') return MSPACK_ERR_DATAFORMAT;
490  READ_BITS(i, 8); if (i != 'K') return MSPACK_ERR_DATAFORMAT;
491 
492  /* inflate block */
493  zip->window_posn = 0;
494  zip->bytes_output = 0;
495  STORE_BITS;
496  if ((error = inflate(zip))) {
497  D(("inflate error %d", error))
498  return zip->error = (error > 0) ? error : MSPACK_ERR_DECRUNCH;
499  }
500 
501  /* write inflated block */
502  if (zip->sys->write(zip->output, &zip->window[0], zip->bytes_output)
503  != zip->bytes_output) return zip->error = MSPACK_ERR_WRITE;
504  }
505  return MSPACK_ERR_OK;
506 }
#define MSPACK_ERR_DATAFORMAT
Definition: mspack.h:501

References mszipd_stream::bit_buffer, mszipd_stream::bits_left, D, zip::error, error(), i, mszipd_stream::i_end, mszipd_stream::i_ptr, inflate(), MSPACK_ERR_DATAFORMAT, MSPACK_ERR_DECRUNCH, MSPACK_ERR_OK, MSPACK_ERR_WRITE, READ_BITS, REMOVE_BITS, RESTORE_BITS, and STORE_BITS.

Referenced by kwajd_extract().

◆ mszipd_free()

void mszipd_free ( struct mszipd_stream zip)

Definition at line 508 of file mszipd.c.

508  {
509  struct mspack_system *sys;
510  if (zip) {
511  sys = zip->sys;
512  sys->free(zip->inbuf);
513  sys->free(zip);
514  }
515 }
void(* free)(void *ptr)
Definition: mspack.h:430

References mspack_system::free.

Referenced by cabd_free_decomp(), and kwajd_extract().

◆ mszipd_init()

struct mszipd_stream* mszipd_init ( struct mspack_system system,
struct mspack_file input,
struct mspack_file output,
int  input_buffer_size,
int  repair_mode 
)

Definition at line 342 of file mszipd.c.

347 {
348  struct mszipd_stream *zip;
349 
350  if (!system) return NULL;
351 
352  /* round up input buffer size to multiple of two */
353  input_buffer_size = (input_buffer_size + 1) & -2;
354  if (input_buffer_size < 2) return NULL;
355 
356  /* allocate decompression state */
357  if (!(zip = (struct mszipd_stream *) system->alloc(system, sizeof(struct mszipd_stream)))) {
358  return NULL;
359  }
360 
361  /* allocate input buffer */
362  zip->inbuf = (unsigned char *) system->alloc(system, (size_t) input_buffer_size);
363  if (!zip->inbuf) {
364  system->free(zip);
365  return NULL;
366  }
367 
368  /* initialise decompression state */
369  zip->sys = system;
370  zip->input = input;
371  zip->output = output;
372  zip->inbuf_size = input_buffer_size;
373  zip->input_end = 0;
375  zip->repair_mode = repair_mode;
376  zip->flush_window = &mszipd_flush_window;
377 
378  zip->i_ptr = zip->i_end = &zip->inbuf[0];
379  zip->o_ptr = zip->o_end = NULL;
380  zip->bit_buffer = 0; zip->bits_left = 0;
381  return zip;
382 }
static int mszipd_flush_window(struct mszipd_stream *zip, unsigned int data_flushed)
Definition: mszipd.c:330
void *(* alloc)(struct mspack_system *self, size_t bytes)
Definition: mspack.h:421
int repair_mode
Definition: mszip.h:54
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237

References mspack_system::alloc, zip::error, mspack_system::free, input(), MSPACK_ERR_OK, mszipd_flush_window(), NULL, output, and mszipd_stream::repair_mode.

Referenced by cabd_init_decomp(), and kwajd_extract().