Rizin
unix-like reverse engineering framework and cli tools
infback9.c File Reference
#include "zutil.h"
#include "infback9.h"
#include "inftree9.h"
#include "inflate9.h"
#include "inffix9.h"

Go to the source code of this file.

Macros

#define WSIZE   65536UL
 
#define INITBITS()
 
#define PULL()
 
#define PULLBYTE()
 
#define NEEDBITS(n)
 
#define BITS(n)    ((unsigned)hold & ((1U << (n)) - 1))
 
#define DROPBITS(n)
 
#define BYTEBITS()
 
#define ROOM()
 

Functions

int ZEXPORT inflateBack9Init_ (z_stream FAR *strm, unsigned char FAR *window, const char *version, int stream_size)
 
int ZEXPORT inflateBack9 (z_stream FAR *strm, in_func in, void FAR *in_desc, out_func out, void FAR *out_desc)
 
int ZEXPORT inflateBack9End (z_stream FAR *strm)
 

Macro Definition Documentation

◆ BITS

#define BITS (   n)     ((unsigned)hold & ((1U << (n)) - 1))

Definition at line 157 of file infback9.c.

◆ BYTEBITS

#define BYTEBITS ( )
Value:
do { \
hold >>= bits & 7; \
bits -= bits & 7; \
} while (0)
int bits(struct state *s, int need)
Definition: blast.c:72

Definition at line 168 of file infback9.c.

◆ DROPBITS

#define DROPBITS (   n)
Value:
do { \
hold >>= (n); \
bits -= (unsigned)(n); \
} while (0)
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
Definition: sflib.h:97
int n
Definition: mipsasm.c:19

Definition at line 161 of file infback9.c.

◆ INITBITS

#define INITBITS ( )
Value:
do { \
hold = 0; \
bits = 0; \
} while (0)

Definition at line 117 of file infback9.c.

◆ NEEDBITS

#define NEEDBITS (   n)
Value:
do { \
while (bits < (unsigned)(n)) \
PULLBYTE(); \
} while (0)

Definition at line 150 of file infback9.c.

◆ PULL

#define PULL ( )
Value:
do { \
if (have == 0) { \
have = in(in_desc, &next); \
if (have == 0) { \
next = Z_NULL; \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
const lzma_allocator const uint8_t * in
Definition: block.h:527
#define Z_BUF_ERROR
Definition: zlib.h:184
#define Z_NULL
Definition: zlib.h:212

Definition at line 125 of file infback9.c.

◆ PULLBYTE

#define PULLBYTE ( )
Value:
do { \
PULL(); \
have--; \
hold += (unsigned long)(*next++) << bits; \
bits += 8; \
} while (0)
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 long
Definition: sflib.h:79

Definition at line 139 of file infback9.c.

◆ ROOM

#define ROOM ( )
Value:
do { \
if (left == 0) { \
put = window; \
left = WSIZE; \
wrap = 1; \
if (out(out_desc, put, (unsigned)left)) { \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define WSIZE
Definition: infback9.c:11
struct _window window

Definition at line 177 of file infback9.c.

◆ WSIZE

#define WSIZE   65536UL

Definition at line 11 of file infback9.c.

Function Documentation

◆ inflateBack9()

int ZEXPORT inflateBack9 ( z_stream FAR strm,
in_func  in,
void FAR in_desc,
out_func  out,
void FAR out_desc 
)

Definition at line 217 of file infback9.c.

223 {
224  struct inflate_state FAR *state;
225  z_const unsigned char FAR *next; /* next input */
226  unsigned char FAR *put; /* next output */
227  unsigned have; /* available input */
228  unsigned long left; /* available output */
229  inflate_mode mode; /* current inflate mode */
230  int lastblock; /* true if processing last block */
231  int wrap; /* true if the window has wrapped */
232  unsigned char FAR *window; /* allocated sliding window, if needed */
233  unsigned long hold; /* bit buffer */
234  unsigned bits; /* bits in bit buffer */
235  unsigned extra; /* extra bits needed */
236  unsigned long length; /* literal or length of data to copy */
237  unsigned long offset; /* distance back to copy string from */
238  unsigned long copy; /* number of stored or match bytes to copy */
239  unsigned char FAR *from; /* where to copy match bytes from */
240  code const FAR *lencode; /* starting table for length/literal codes */
241  code const FAR *distcode; /* starting table for distance codes */
242  unsigned lenbits; /* index bits for lencode */
243  unsigned distbits; /* index bits for distcode */
244  code here; /* current decoding table entry */
245  code last; /* parent table entry */
246  unsigned len; /* length to copy for repeats, bits to drop */
247  int ret; /* return code */
248  static const unsigned short order[19] = /* permutation of code lengths */
249  {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
250 #include "inffix9.h"
251 
252  /* Check that the strm exists and that the state was initialized */
253  if (strm == Z_NULL || strm->state == Z_NULL)
254  return Z_STREAM_ERROR;
255  state = (struct inflate_state FAR *)strm->state;
256 
257  /* Reset the state */
258  strm->msg = Z_NULL;
259  mode = TYPE;
260  lastblock = 0;
261  wrap = 0;
262  window = state->window;
263  next = strm->next_in;
264  have = next != Z_NULL ? strm->avail_in : 0;
265  hold = 0;
266  bits = 0;
267  put = window;
268  left = WSIZE;
269  lencode = Z_NULL;
270  distcode = Z_NULL;
271 
272  /* Inflate until end of block marked as last */
273  for (;;)
274  switch (mode) {
275  case TYPE:
276  /* determine and dispatch block type */
277  if (lastblock) {
278  BYTEBITS();
279  mode = DONE;
280  break;
281  }
282  NEEDBITS(3);
283  lastblock = BITS(1);
284  DROPBITS(1);
285  switch (BITS(2)) {
286  case 0: /* stored block */
287  Tracev((stderr, "inflate: stored block%s\n",
288  lastblock ? " (last)" : ""));
289  mode = STORED;
290  break;
291  case 1: /* fixed block */
292  lencode = lenfix;
293  lenbits = 9;
294  distcode = distfix;
295  distbits = 5;
296  Tracev((stderr, "inflate: fixed codes block%s\n",
297  lastblock ? " (last)" : ""));
298  mode = LEN; /* decode codes */
299  break;
300  case 2: /* dynamic block */
301  Tracev((stderr, "inflate: dynamic codes block%s\n",
302  lastblock ? " (last)" : ""));
303  mode = TABLE;
304  break;
305  case 3:
306  strm->msg = (char *)"invalid block type";
307  mode = BAD;
308  }
309  DROPBITS(2);
310  break;
311 
312  case STORED:
313  /* get and verify stored block length */
314  BYTEBITS(); /* go to byte boundary */
315  NEEDBITS(32);
316  if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
317  strm->msg = (char *)"invalid stored block lengths";
318  mode = BAD;
319  break;
320  }
321  length = (unsigned)hold & 0xffff;
322  Tracev((stderr, "inflate: stored length %lu\n",
323  length));
324  INITBITS();
325 
326  /* copy stored block from input to output */
327  while (length != 0) {
328  copy = length;
329  PULL();
330  ROOM();
331  if (copy > have) copy = have;
332  if (copy > left) copy = left;
333  zmemcpy(put, next, copy);
334  have -= copy;
335  next += copy;
336  left -= copy;
337  put += copy;
338  length -= copy;
339  }
340  Tracev((stderr, "inflate: stored end\n"));
341  mode = TYPE;
342  break;
343 
344  case TABLE:
345  /* get dynamic table entries descriptor */
346  NEEDBITS(14);
347  state->nlen = BITS(5) + 257;
348  DROPBITS(5);
349  state->ndist = BITS(5) + 1;
350  DROPBITS(5);
351  state->ncode = BITS(4) + 4;
352  DROPBITS(4);
353  if (state->nlen > 286) {
354  strm->msg = (char *)"too many length symbols";
355  mode = BAD;
356  break;
357  }
358  Tracev((stderr, "inflate: table sizes ok\n"));
359 
360  /* get code length code lengths (not a typo) */
361  state->have = 0;
362  while (state->have < state->ncode) {
363  NEEDBITS(3);
364  state->lens[order[state->have++]] = (unsigned short)BITS(3);
365  DROPBITS(3);
366  }
367  while (state->have < 19)
368  state->lens[order[state->have++]] = 0;
369  state->next = state->codes;
370  lencode = (code const FAR *)(state->next);
371  lenbits = 7;
372  ret = inflate_table9(CODES, state->lens, 19, &(state->next),
373  &(lenbits), state->work);
374  if (ret) {
375  strm->msg = (char *)"invalid code lengths set";
376  mode = BAD;
377  break;
378  }
379  Tracev((stderr, "inflate: code lengths ok\n"));
380 
381  /* get length and distance code code lengths */
382  state->have = 0;
383  while (state->have < state->nlen + state->ndist) {
384  for (;;) {
385  here = lencode[BITS(lenbits)];
386  if ((unsigned)(here.bits) <= bits) break;
387  PULLBYTE();
388  }
389  if (here.val < 16) {
390  NEEDBITS(here.bits);
391  DROPBITS(here.bits);
392  state->lens[state->have++] = here.val;
393  }
394  else {
395  if (here.val == 16) {
396  NEEDBITS(here.bits + 2);
397  DROPBITS(here.bits);
398  if (state->have == 0) {
399  strm->msg = (char *)"invalid bit length repeat";
400  mode = BAD;
401  break;
402  }
403  len = (unsigned)(state->lens[state->have - 1]);
404  copy = 3 + BITS(2);
405  DROPBITS(2);
406  }
407  else if (here.val == 17) {
408  NEEDBITS(here.bits + 3);
409  DROPBITS(here.bits);
410  len = 0;
411  copy = 3 + BITS(3);
412  DROPBITS(3);
413  }
414  else {
415  NEEDBITS(here.bits + 7);
416  DROPBITS(here.bits);
417  len = 0;
418  copy = 11 + BITS(7);
419  DROPBITS(7);
420  }
421  if (state->have + copy > state->nlen + state->ndist) {
422  strm->msg = (char *)"invalid bit length repeat";
423  mode = BAD;
424  break;
425  }
426  while (copy--)
427  state->lens[state->have++] = (unsigned short)len;
428  }
429  }
430 
431  /* handle error breaks in while */
432  if (mode == BAD) break;
433 
434  /* check for end-of-block code (better have one) */
435  if (state->lens[256] == 0) {
436  strm->msg = (char *)"invalid code -- missing end-of-block";
437  mode = BAD;
438  break;
439  }
440 
441  /* build code tables -- note: do not change the lenbits or distbits
442  values here (9 and 6) without reading the comments in inftree9.h
443  concerning the ENOUGH constants, which depend on those values */
444  state->next = state->codes;
445  lencode = (code const FAR *)(state->next);
446  lenbits = 9;
447  ret = inflate_table9(LENS, state->lens, state->nlen,
448  &(state->next), &(lenbits), state->work);
449  if (ret) {
450  strm->msg = (char *)"invalid literal/lengths set";
451  mode = BAD;
452  break;
453  }
454  distcode = (code const FAR *)(state->next);
455  distbits = 6;
456  ret = inflate_table9(DISTS, state->lens + state->nlen,
457  state->ndist, &(state->next), &(distbits),
458  state->work);
459  if (ret) {
460  strm->msg = (char *)"invalid distances set";
461  mode = BAD;
462  break;
463  }
464  Tracev((stderr, "inflate: codes ok\n"));
465  mode = LEN;
466 
467  case LEN:
468  /* get a literal, length, or end-of-block code */
469  for (;;) {
470  here = lencode[BITS(lenbits)];
471  if ((unsigned)(here.bits) <= bits) break;
472  PULLBYTE();
473  }
474  if (here.op && (here.op & 0xf0) == 0) {
475  last = here;
476  for (;;) {
477  here = lencode[last.val +
478  (BITS(last.bits + last.op) >> last.bits)];
479  if ((unsigned)(last.bits + here.bits) <= bits) break;
480  PULLBYTE();
481  }
482  DROPBITS(last.bits);
483  }
484  DROPBITS(here.bits);
485  length = (unsigned)here.val;
486 
487  /* process literal */
488  if (here.op == 0) {
489  Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
490  "inflate: literal '%c'\n" :
491  "inflate: literal 0x%02x\n", here.val));
492  ROOM();
493  *put++ = (unsigned char)(length);
494  left--;
495  mode = LEN;
496  break;
497  }
498 
499  /* process end of block */
500  if (here.op & 32) {
501  Tracevv((stderr, "inflate: end of block\n"));
502  mode = TYPE;
503  break;
504  }
505 
506  /* invalid code */
507  if (here.op & 64) {
508  strm->msg = (char *)"invalid literal/length code";
509  mode = BAD;
510  break;
511  }
512 
513  /* length code -- get extra bits, if any */
514  extra = (unsigned)(here.op) & 31;
515  if (extra != 0) {
516  NEEDBITS(extra);
517  length += BITS(extra);
518  DROPBITS(extra);
519  }
520  Tracevv((stderr, "inflate: length %lu\n", length));
521 
522  /* get distance code */
523  for (;;) {
524  here = distcode[BITS(distbits)];
525  if ((unsigned)(here.bits) <= bits) break;
526  PULLBYTE();
527  }
528  if ((here.op & 0xf0) == 0) {
529  last = here;
530  for (;;) {
531  here = distcode[last.val +
532  (BITS(last.bits + last.op) >> last.bits)];
533  if ((unsigned)(last.bits + here.bits) <= bits) break;
534  PULLBYTE();
535  }
536  DROPBITS(last.bits);
537  }
538  DROPBITS(here.bits);
539  if (here.op & 64) {
540  strm->msg = (char *)"invalid distance code";
541  mode = BAD;
542  break;
543  }
544  offset = (unsigned)here.val;
545 
546  /* get distance extra bits, if any */
547  extra = (unsigned)(here.op) & 15;
548  if (extra != 0) {
549  NEEDBITS(extra);
550  offset += BITS(extra);
551  DROPBITS(extra);
552  }
553  if (offset > WSIZE - (wrap ? 0: left)) {
554  strm->msg = (char *)"invalid distance too far back";
555  mode = BAD;
556  break;
557  }
558  Tracevv((stderr, "inflate: distance %lu\n", offset));
559 
560  /* copy match from window to output */
561  do {
562  ROOM();
563  copy = WSIZE - offset;
564  if (copy < left) {
565  from = put + copy;
566  copy = left - copy;
567  }
568  else {
569  from = put - offset;
570  copy = left;
571  }
572  if (copy > length) copy = length;
573  length -= copy;
574  left -= copy;
575  do {
576  *put++ = *from++;
577  } while (--copy);
578  } while (length != 0);
579  break;
580 
581  case DONE:
582  /* inflate stream terminated properly -- write leftover output */
583  ret = Z_STREAM_END;
584  if (left < WSIZE) {
585  if (out(out_desc, window, (unsigned)(WSIZE - left)))
586  ret = Z_BUF_ERROR;
587  }
588  goto inf_leave;
589 
590  case BAD:
591  ret = Z_DATA_ERROR;
592  goto inf_leave;
593 
594  default: /* can't happen, but makes compilers happy */
595  ret = Z_STREAM_ERROR;
596  goto inf_leave;
597  }
598 
599  /* Return unused input */
600  inf_leave:
601  strm->next_in = next;
602  strm->avail_in = have;
603  return ret;
604 }
size_t len
Definition: 6502dis.c:15
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
static lzma_stream strm
Definition: full_flush.c:20
#define PULL()
Definition: infback9.c:125
#define INITBITS()
Definition: infback9.c:117
#define BITS(n)
Definition: infback9.c:157
#define DROPBITS(n)
Definition: infback9.c:161
#define BYTEBITS()
Definition: infback9.c:168
#define ROOM()
Definition: infback9.c:177
#define NEEDBITS(n)
Definition: infback9.c:150
#define PULLBYTE()
Definition: infback9.c:139
static const code distfix[32]
Definition: inffix9.h:99
static const code lenfix[512]
Definition: inffix9.h:10
inflate_mode
Definition: inflate9.h:12
@ TABLE
Definition: inflate9.h:15
@ STORED
Definition: inflate9.h:14
@ TYPE
Definition: inflate9.h:13
@ LEN
Definition: inflate9.h:16
int inflate_table9(codetype type, unsigned short FAR *lens, unsigned codes, code FAR *FAR *table, unsigned FAR *bits, unsigned short FAR *work)
Definition: inftree9.c:32
@ CODES
Definition: inftree9.h:54
@ LENS
Definition: inftree9.h:55
@ DISTS
Definition: inftree9.h:56
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
#define BAD
Definition: regex2.h:141
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
Definition: inftree9.h:24
unsigned char op
Definition: inftree9.h:25
unsigned char bits
Definition: inftree9.h:26
unsigned short val
Definition: inftree9.h:27
code const FAR * distcode
Definition: inflate.h:111
unsigned lenbits
Definition: inflate.h:112
code const FAR * lencode
Definition: inflate.h:110
unsigned have
Definition: inflate9.h:42
unsigned long hold
Definition: inflate.h:102
unsigned extra
Definition: inflate.h:108
code FAR * next
Definition: inflate9.h:43
unsigned distbits
Definition: inflate.h:113
const uint8_t * next_in
Definition: base.h:486
size_t avail_in
Definition: base.h:487
Definition: dis.h:43
unsigned next
Definition: blast.c:56
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
#define z_const
Definition: zconf.h:237
#define FAR
Definition: zconf.h:387
@ DONE
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_DATA_ERROR
Definition: zlib.h:182
#define Z_STREAM_ERROR
Definition: zlib.h:181
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:149
#define Tracev(x)
Definition: zutil.h:253
#define Tracevv(x)
Definition: zutil.h:254

References lzma_stream::avail_in, BAD, BITS, bits(), code::bits, BYTEBITS, CODES, inflate_state::distbits, inflate_state::distcode, distfix, DISTS, DONE, DROPBITS, inflate_state::extra, FAR, from, inflate_state::have, inflate_state::hold, if(), inflate_table9(), INITBITS, inflate_state::last, len, LEN, inflate_state::lenbits, inflate_state::lencode, lenfix, length, LENS, NEEDBITS, state::next, inflate_state::next, lzma_stream::next_in, code::op, out, PULL, PULLBYTE, ROOM, STORED, strm, TABLE, Tracev, Tracevv, TYPE, unsigned, code::val, inflate_state::wrap, WSIZE, Z_BUF_ERROR, z_const, Z_DATA_ERROR, Z_NULL, Z_STREAM_END, Z_STREAM_ERROR, and zmemcpy().

◆ inflateBack9End()

int ZEXPORT inflateBack9End ( z_stream FAR strm)

Definition at line 606 of file infback9.c.

608 {
609  if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
610  return Z_STREAM_ERROR;
611  ZFREE(strm, strm->state);
612  strm->state = Z_NULL;
613  Tracev((stderr, "inflate: end\n"));
614  return Z_OK;
615 }
#define Z_OK
Definition: zlib.h:177
#define ZFREE(strm, addr)
Definition: zutil.h:267

References strm, Tracev, Z_NULL, Z_OK, Z_STREAM_ERROR, and ZFREE.

◆ inflateBack9Init_()

int ZEXPORT inflateBack9Init_ ( z_stream FAR strm,
unsigned char FAR window,
const char *  version,
int  stream_size 
)

Definition at line 19 of file infback9.c.

24 {
25  struct inflate_state FAR *state;
26 
27  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
28  stream_size != (int)(sizeof(z_stream)))
29  return Z_VERSION_ERROR;
30  if (strm == Z_NULL || window == Z_NULL)
31  return Z_STREAM_ERROR;
32  strm->msg = Z_NULL; /* in case we return an error */
33  if (strm->zalloc == (alloc_func)0) {
34  strm->zalloc = zcalloc;
35  strm->opaque = (voidpf)0;
36  }
37  if (strm->zfree == (free_func)0) strm->zfree = zcfree;
38  state = (struct inflate_state FAR *)ZALLOC(strm, 1,
39  sizeof(struct inflate_state));
40  if (state == Z_NULL) return Z_MEM_ERROR;
41  Tracev((stderr, "inflate: allocated\n"));
42  strm->state = (voidpf)state;
43  state->window = window;
44  return Z_OK;
45 }
Byte FAR * voidpf
Definition: zconf.h:413
#define ZLIB_VERSION
Definition: zlib.h:40
#define Z_VERSION_ERROR
Definition: zlib.h:185
#define Z_MEM_ERROR
Definition: zlib.h:183
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
Definition: zutil.c:315
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: zutil.c:305
#define ZALLOC(strm, items, size)
Definition: zutil.h:265

References FAR, strm, Tracev, Z_MEM_ERROR, Z_NULL, Z_OK, Z_STREAM_ERROR, Z_VERSION_ERROR, ZALLOC, zcalloc(), zcfree(), and ZLIB_VERSION.