Rizin
unix-like reverse engineering framework and cli tools
mszipd.c File Reference
#include <system.h>
#include <mszip.h>
#include <readbits.h>
#include <readhuff.h>

Go to the source code of this file.

Macros

#define BITS_TYPE   struct mszipd_stream
 
#define BITS_VAR   zip
 
#define BITS_ORDER_LSB
 
#define BITS_LSB_TABLE
 
#define READ_BYTES
 
#define TABLEBITS(tbl)   MSZIP_##tbl##_TABLEBITS
 
#define MAXSYMBOLS(tbl)   MSZIP_##tbl##_MAXSYMBOLS
 
#define HUFF_TABLE(tbl, idx)   zip->tbl##_table[idx]
 
#define HUFF_LEN(tbl, idx)   zip->tbl##_len[idx]
 
#define HUFF_ERROR   return INF_ERR_HUFFSYM
 
#define FLUSH_IF_NEEDED
 
#define INF_ERR_BLOCKTYPE   (-1) /* unknown block type */
 
#define INF_ERR_COMPLEMENT   (-2) /* block size complement mismatch */
 
#define INF_ERR_FLUSH   (-3) /* error from flush_window() callback */
 
#define INF_ERR_BITBUF   (-4) /* too many bits in bit buffer */
 
#define INF_ERR_SYMLENS   (-5) /* too many symbols in blocktype 2 header */
 
#define INF_ERR_BITLENTBL   (-6) /* failed to build bitlens huffman table */
 
#define INF_ERR_LITERALTBL   (-7) /* failed to build literals huffman table */
 
#define INF_ERR_DISTANCETBL   (-8) /* failed to build distance huffman table */
 
#define INF_ERR_BITOVERRUN   (-9) /* bitlen RLE code goes over table size */
 
#define INF_ERR_BADBITLEN   (-10) /* invalid bit-length code */
 
#define INF_ERR_LITCODE   (-11) /* out-of-range literal code */
 
#define INF_ERR_DISTCODE   (-12) /* out-of-range distance code */
 
#define INF_ERR_DISTANCE   (-13) /* somehow, distance is beyond 32k */
 
#define INF_ERR_HUFFSYM   (-14) /* out of bits decoding huffman symbol */
 

Functions

static int zip_read_lens (struct mszipd_stream *zip)
 
static int inflate (struct mszipd_stream *zip)
 
static int mszipd_flush_window (struct mszipd_stream *zip, unsigned int data_flushed)
 
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)
 

Variables

static const unsigned short lit_lengths [29]
 
static const unsigned short dist_offsets [30]
 
static const unsigned char lit_extrabits [29]
 
static const unsigned char dist_extrabits [30]
 
static const unsigned char bitlen_order [19]
 

Macro Definition Documentation

◆ BITS_LSB_TABLE

#define BITS_LSB_TABLE

Definition at line 22 of file mszipd.c.

◆ BITS_ORDER_LSB

#define BITS_ORDER_LSB

Definition at line 21 of file mszipd.c.

◆ BITS_TYPE

#define BITS_TYPE   struct mszipd_stream

Definition at line 19 of file mszipd.c.

◆ BITS_VAR

#define BITS_VAR   zip

Definition at line 20 of file mszipd.c.

◆ FLUSH_IF_NEEDED

#define FLUSH_IF_NEEDED
Value:
do { \
if (zip->window_posn == MSZIP_FRAME_SIZE) { \
if (zip->flush_window(zip, MSZIP_FRAME_SIZE)) { \
return INF_ERR_FLUSH; \
} \
zip->window_posn = 0; \
} \
} while (0)
#define MSZIP_FRAME_SIZE
Definition: mszip.h:22
#define INF_ERR_FLUSH
Definition: mszipd.c:78
Definition: zipint.h:278

Definition at line 37 of file mszipd.c.

◆ HUFF_ERROR

#define HUFF_ERROR   return INF_ERR_HUFFSYM

Definition at line 34 of file mszipd.c.

◆ HUFF_LEN

#define HUFF_LEN (   tbl,
  idx 
)    zip->tbl##_len[idx]

Definition at line 33 of file mszipd.c.

◆ HUFF_TABLE

#define HUFF_TABLE (   tbl,
  idx 
)    zip->tbl##_table[idx]

Definition at line 32 of file mszipd.c.

◆ INF_ERR_BADBITLEN

#define INF_ERR_BADBITLEN   (-10) /* invalid bit-length code */

Definition at line 85 of file mszipd.c.

◆ INF_ERR_BITBUF

#define INF_ERR_BITBUF   (-4) /* too many bits in bit buffer */

Definition at line 79 of file mszipd.c.

◆ INF_ERR_BITLENTBL

#define INF_ERR_BITLENTBL   (-6) /* failed to build bitlens huffman table */

Definition at line 81 of file mszipd.c.

◆ INF_ERR_BITOVERRUN

#define INF_ERR_BITOVERRUN   (-9) /* bitlen RLE code goes over table size */

Definition at line 84 of file mszipd.c.

◆ INF_ERR_BLOCKTYPE

#define INF_ERR_BLOCKTYPE   (-1) /* unknown block type */

Definition at line 76 of file mszipd.c.

◆ INF_ERR_COMPLEMENT

#define INF_ERR_COMPLEMENT   (-2) /* block size complement mismatch */

Definition at line 77 of file mszipd.c.

◆ INF_ERR_DISTANCE

#define INF_ERR_DISTANCE   (-13) /* somehow, distance is beyond 32k */

Definition at line 88 of file mszipd.c.

◆ INF_ERR_DISTANCETBL

#define INF_ERR_DISTANCETBL   (-8) /* failed to build distance huffman table */

Definition at line 83 of file mszipd.c.

◆ INF_ERR_DISTCODE

#define INF_ERR_DISTCODE   (-12) /* out-of-range distance code */

Definition at line 87 of file mszipd.c.

◆ INF_ERR_FLUSH

#define INF_ERR_FLUSH   (-3) /* error from flush_window() callback */

Definition at line 78 of file mszipd.c.

◆ INF_ERR_HUFFSYM

#define INF_ERR_HUFFSYM   (-14) /* out of bits decoding huffman symbol */

Definition at line 89 of file mszipd.c.

◆ INF_ERR_LITCODE

#define INF_ERR_LITCODE   (-11) /* out-of-range literal code */

Definition at line 86 of file mszipd.c.

◆ INF_ERR_LITERALTBL

#define INF_ERR_LITERALTBL   (-7) /* failed to build literals huffman table */

Definition at line 82 of file mszipd.c.

◆ INF_ERR_SYMLENS

#define INF_ERR_SYMLENS   (-5) /* too many symbols in blocktype 2 header */

Definition at line 80 of file mszipd.c.

◆ MAXSYMBOLS

#define MAXSYMBOLS (   tbl)    MSZIP_##tbl##_MAXSYMBOLS

Definition at line 31 of file mszipd.c.

◆ READ_BYTES

#define READ_BYTES
Value:
do { \
READ_IF_NEEDED; \
INJECT_BITS(*i_ptr++, 8); \
} while (0)

Definition at line 23 of file mszipd.c.

◆ TABLEBITS

#define TABLEBITS (   tbl)    MSZIP_##tbl##_TABLEBITS

Definition at line 30 of file mszipd.c.

Function Documentation

◆ inflate()

static int inflate ( struct mszipd_stream zip)
static

Definition at line 157 of file mszipd.c.

157  {
158  unsigned int last_block, block_type, distance, length, this_run, i;
159 
160  /* for the bit buffer and huffman decoding */
161  register unsigned int bit_buffer;
162  register int bits_left;
163  register unsigned short sym;
164  unsigned char *i_ptr, *i_end;
165 
166  RESTORE_BITS;
167 
168  do {
169  /* read in last block bit */
170  READ_BITS(last_block, 1);
171 
172  /* read in block type */
173  READ_BITS(block_type, 2);
174 
175  if (block_type == 0) {
176  /* uncompressed block */
177  unsigned char lens_buf[4];
178 
179  /* go to byte boundary */
180  i = bits_left & 7; REMOVE_BITS(i);
181 
182  /* read 4 bytes of data, emptying the bit-buffer if necessary */
183  for (i = 0; (bits_left >= 8); i++) {
184  if (i == 4) return INF_ERR_BITBUF;
185  lens_buf[i] = PEEK_BITS(8);
186  REMOVE_BITS(8);
187  }
188  if (bits_left != 0) return INF_ERR_BITBUF;
189  while (i < 4) {
191  lens_buf[i++] = *i_ptr++;
192  }
193 
194  /* get the length and its complement */
195  length = lens_buf[0] | (lens_buf[1] << 8);
196  i = lens_buf[2] | (lens_buf[3] << 8);
197  if (length != (~i & 0xFFFF)) return INF_ERR_COMPLEMENT;
198 
199  /* read and copy the uncompressed data into the window */
200  while (length > 0) {
202 
203  this_run = length;
204  if (this_run > (unsigned int)(i_end - i_ptr)) this_run = i_end - i_ptr;
205  if (this_run > (MSZIP_FRAME_SIZE - zip->window_posn))
206  this_run = MSZIP_FRAME_SIZE - zip->window_posn;
207 
208  zip->sys->copy(i_ptr, &zip->window[zip->window_posn], this_run);
209  zip->window_posn += this_run;
210  i_ptr += this_run;
211  length -= this_run;
213  }
214  }
215  else if ((block_type == 1) || (block_type == 2)) {
216  /* Huffman-compressed LZ77 block */
217  unsigned int match_posn, code;
218 
219  if (block_type == 1) {
220  /* block with fixed Huffman codes */
221  i = 0;
222  while (i < 144) zip->LITERAL_len[i++] = 8;
223  while (i < 256) zip->LITERAL_len[i++] = 9;
224  while (i < 280) zip->LITERAL_len[i++] = 7;
225  while (i < 288) zip->LITERAL_len[i++] = 8;
226  for (i = 0; i < 32; i++) zip->DISTANCE_len[i] = 5;
227  }
228  else {
229  /* block with dynamic Huffman codes */
230  STORE_BITS;
231  if ((i = zip_read_lens(zip))) return i;
232  RESTORE_BITS;
233  }
234 
235  /* now huffman lengths are read for either kind of block,
236  * create huffman decoding tables */
238  &zip->LITERAL_len[0], &zip->LITERAL_table[0]))
239  {
240  return INF_ERR_LITERALTBL;
241  }
242 
244  &zip->DISTANCE_len[0], &zip->DISTANCE_table[0]))
245  {
246  return INF_ERR_DISTANCETBL;
247  }
248 
249  /* decode forever until end of block code */
250  for (;;) {
251  READ_HUFFSYM(LITERAL, code);
252  if (code < 256) {
253  zip->window[zip->window_posn++] = (unsigned char) code;
255  }
256  else if (code == 256) {
257  /* END OF BLOCK CODE: loop break point */
258  break;
259  }
260  else {
261  code -= 257; /* codes 257-285 are matches */
262  if (code >= 29) return INF_ERR_LITCODE; /* codes 286-287 are illegal */
263  READ_BITS_T(length, lit_extrabits[code]);
264  length += lit_lengths[code];
265 
266  READ_HUFFSYM(DISTANCE, code);
267  if (code >= 30) return INF_ERR_DISTCODE;
268  READ_BITS_T(distance, dist_extrabits[code]);
269  distance += dist_offsets[code];
270 
271  /* match position is window position minus distance. If distance
272  * is more than window position numerically, it must 'wrap
273  * around' the frame size. */
274  match_posn = ((distance > zip->window_posn) ? MSZIP_FRAME_SIZE : 0)
275  + zip->window_posn - distance;
276 
277  /* copy match */
278  if (length < 12) {
279  /* short match, use slower loop but no loop setup code */
280  while (length--) {
281  zip->window[zip->window_posn++] = zip->window[match_posn++];
282  match_posn &= MSZIP_FRAME_SIZE - 1;
284  }
285  }
286  else {
287  /* longer match, use faster loop but with setup expense */
288  unsigned char *runsrc, *rundest;
289  do {
290  this_run = length;
291  if ((match_posn + this_run) > MSZIP_FRAME_SIZE)
292  this_run = MSZIP_FRAME_SIZE - match_posn;
293  if ((zip->window_posn + this_run) > MSZIP_FRAME_SIZE)
294  this_run = MSZIP_FRAME_SIZE - zip->window_posn;
295 
296  rundest = &zip->window[zip->window_posn]; zip->window_posn += this_run;
297  runsrc = &zip->window[match_posn]; match_posn += this_run;
298  length -= this_run;
299  while (this_run--) *rundest++ = *runsrc++;
300  if (match_posn == MSZIP_FRAME_SIZE) match_posn = 0;
302  } while (length > 0);
303  }
304 
305  } /* else (code >= 257) */
306 
307  } /* for(;;) -- break point at 'code == 256' */
308  }
309  else {
310  /* block_type == 3 -- bad block type */
311  return INF_ERR_BLOCKTYPE;
312  }
313  } while (!last_block);
314 
315  /* flush the remaining data */
316  if (zip->window_posn) {
317  if (zip->flush_window(zip, zip->window_posn)) return INF_ERR_FLUSH;
318  }
319  STORE_BITS;
320 
321  /* return success */
322  return 0;
323 }
lzma_index ** i
Definition: index.h:629
#define MSZIP_DISTANCE_MAXSYMBOLS
Definition: mszip.h:25
#define MSZIP_LITERAL_MAXSYMBOLS
Definition: mszip.h:23
#define MSZIP_DISTANCE_TABLEBITS
Definition: mszip.h:26
#define MSZIP_LITERAL_TABLEBITS
Definition: mszip.h:24
#define INF_ERR_BITBUF
Definition: mszipd.c:79
static const unsigned short lit_lengths[29]
Definition: mszipd.c:47
#define INF_ERR_DISTANCETBL
Definition: mszipd.c:83
#define INF_ERR_LITERALTBL
Definition: mszipd.c:82
#define INF_ERR_DISTCODE
Definition: mszipd.c:87
static const unsigned short dist_offsets[30]
Definition: mszipd.c:53
#define INF_ERR_LITCODE
Definition: mszipd.c:86
#define FLUSH_IF_NEEDED
Definition: mszipd.c:37
static int zip_read_lens(struct mszipd_stream *zip)
Definition: mszipd.c:91
#define INF_ERR_BLOCKTYPE
Definition: mszipd.c:76
static const unsigned char lit_extrabits[29]
Definition: mszipd.c:59
#define INF_ERR_COMPLEMENT
Definition: mszipd.c:77
static const unsigned char dist_extrabits[30]
Definition: mszipd.c:65
#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 READ_IF_NEEDED
Definition: readbits.h:174
#define PEEK_BITS(nbits)
Definition: readbits.h:153
#define READ_HUFFSYM(tbl, var)
Definition: readhuff.h:34
static int make_decode_table(unsigned int nsyms, unsigned int nbits, unsigned char *length, unsigned short *table)
Definition: readhuff.h:78
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
while(len< limit &&buf1[len]==buf2[len])++len
const char * code
Definition: pal.c:98
Definition: inftree9.h:24

References code, dist_extrabits, dist_offsets, FLUSH_IF_NEEDED, i, INF_ERR_BITBUF, INF_ERR_BLOCKTYPE, INF_ERR_COMPLEMENT, INF_ERR_DISTANCETBL, INF_ERR_DISTCODE, INF_ERR_FLUSH, INF_ERR_LITCODE, INF_ERR_LITERALTBL, length, lit_extrabits, lit_lengths, make_decode_table(), MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, MSZIP_FRAME_SIZE, MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, PEEK_BITS, READ_BITS, READ_HUFFSYM, READ_IF_NEEDED, REMOVE_BITS, RESTORE_BITS, STORE_BITS, while(), and zip_read_lens().

Referenced by mszipd_decompress(), and mszipd_decompress_kwaj().

◆ 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 }
#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
static int inflate(struct mszipd_stream *zip)
Definition: mszipd.c:157
#define NULL
Definition: cris-opc.c:27
static int
Definition: sfsocketcall.h:114
int off_t
Definition: sftypes.h:41
Definition: dis.h:43
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

◆ 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

◆ mszipd_flush_window()

static int mszipd_flush_window ( struct mszipd_stream zip,
unsigned int  data_flushed 
)
static

Definition at line 330 of file mszipd.c.

332 {
333  zip->bytes_output += data_flushed;
334  if (zip->bytes_output > MSZIP_FRAME_SIZE) {
335  D(("overflow: %u bytes flushed, total is now %u",
336  data_flushed, zip->bytes_output))
337  return 1;
338  }
339  return 0;
340 }

References D, and MSZIP_FRAME_SIZE.

Referenced by mszipd_init().

◆ 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

◆ 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

◆ zip_read_lens()

static int zip_read_lens ( struct mszipd_stream zip)
static

Definition at line 91 of file mszipd.c.

91  {
92  /* for the bit buffer and huffman decoding */
93  register unsigned int bit_buffer;
94  register int bits_left;
95  unsigned char *i_ptr, *i_end;
96 
97  /* bitlen Huffman codes -- immediate lookup, 7 bit max code length */
98  unsigned short bl_table[(1 << 7)];
99  unsigned char bl_len[19];
100 
102  unsigned int lit_codes, dist_codes, code, last_code=0, bitlen_codes, i, run;
103 
104  RESTORE_BITS;
105 
106  /* read the number of codes */
107  READ_BITS(lit_codes, 5); lit_codes += 257;
108  READ_BITS(dist_codes, 5); dist_codes += 1;
109  READ_BITS(bitlen_codes, 4); bitlen_codes += 4;
110  if (lit_codes > MSZIP_LITERAL_MAXSYMBOLS) return INF_ERR_SYMLENS;
111  if (dist_codes > MSZIP_DISTANCE_MAXSYMBOLS) return INF_ERR_SYMLENS;
112 
113  /* read in the bit lengths in their unusual order */
114  for (i = 0; i < bitlen_codes; i++) READ_BITS(bl_len[bitlen_order[i]], 3);
115  while (i < 19) bl_len[bitlen_order[i++]] = 0;
116 
117  /* create decoding table with an immediate lookup */
118  if (make_decode_table(19, 7, &bl_len[0], &bl_table[0])) {
119  return INF_ERR_BITLENTBL;
120  }
121 
122  /* read literal / distance code lengths */
123  for (i = 0; i < (lit_codes + dist_codes); i++) {
124  /* single-level huffman lookup */
125  ENSURE_BITS(7);
126  code = bl_table[PEEK_BITS(7)];
127  REMOVE_BITS(bl_len[code]);
128 
129  if (code < 16) lens[i] = last_code = code;
130  else {
131  switch (code) {
132  case 16: READ_BITS(run, 2); run += 3; code = last_code; break;
133  case 17: READ_BITS(run, 3); run += 3; code = 0; break;
134  case 18: READ_BITS(run, 7); run += 11; code = 0; break;
135  default: D(("bad code!: %u", code)) return INF_ERR_BADBITLEN;
136  }
137  if ((i + run) > (lit_codes + dist_codes)) return INF_ERR_BITOVERRUN;
138  while (run--) lens[i++] = code;
139  i--;
140  }
141  }
142 
143  /* copy LITERAL code lengths and clear any remaining */
144  i = lit_codes;
145  zip->sys->copy(&lens[0], &zip->LITERAL_len[0], i);
146  while (i < MSZIP_LITERAL_MAXSYMBOLS) zip->LITERAL_len[i++] = 0;
147 
148  i = dist_codes;
149  zip->sys->copy(&lens[lit_codes], &zip->DISTANCE_len[0], i);
150  while (i < MSZIP_DISTANCE_MAXSYMBOLS) zip->DISTANCE_len[i++] = 0;
151 
152  STORE_BITS;
153  return 0;
154 }
static const unsigned char bitlen_order[19]
Definition: mszipd.c:71
#define INF_ERR_BITLENTBL
Definition: mszipd.c:81
#define INF_ERR_SYMLENS
Definition: mszipd.c:80
#define INF_ERR_BADBITLEN
Definition: mszipd.c:85
#define INF_ERR_BITOVERRUN
Definition: mszipd.c:84
#define ENSURE_BITS(nbits)
Definition: readbits.h:125
static int run(int i, const char *arg)
Definition: rz-bb.c:19

References bitlen_order, code, D, ENSURE_BITS, i, INF_ERR_BADBITLEN, INF_ERR_BITLENTBL, INF_ERR_BITOVERRUN, INF_ERR_SYMLENS, make_decode_table(), MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_LITERAL_MAXSYMBOLS, PEEK_BITS, READ_BITS, REMOVE_BITS, RESTORE_BITS, run(), and STORE_BITS.

Referenced by inflate().

Variable Documentation

◆ bitlen_order

const unsigned char bitlen_order[19]
static
Initial value:
= {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
}

Definition at line 71 of file mszipd.c.

Referenced by zip_read_lens().

◆ dist_extrabits

const unsigned char dist_extrabits[30]
static
Initial value:
= {
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6,
6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
}

Definition at line 65 of file mszipd.c.

Referenced by inflate().

◆ dist_offsets

const unsigned short dist_offsets[30]
static
Initial value:
= {
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
}

Definition at line 53 of file mszipd.c.

Referenced by inflate().

◆ lit_extrabits

const unsigned char lit_extrabits[29]
static
Initial value:
= {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2,
2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
}

Definition at line 59 of file mszipd.c.

Referenced by inflate().

◆ lit_lengths

const unsigned short lit_lengths[29]
static
Initial value:
= {
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27,
31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
}

Definition at line 47 of file mszipd.c.

Referenced by inflate().