Rizin
unix-like reverse engineering framework and cli tools
puff.c File Reference
#include <setjmp.h>
#include "puff.h"

Go to the source code of this file.

Classes

struct  state
 
struct  huffman
 

Macros

#define local   static /* for local function definitions */
 
#define MAXBITS   15 /* maximum bits in a code */
 
#define MAXLCODES   286 /* maximum number of literal/length codes */
 
#define MAXDCODES   30 /* maximum number of distance codes */
 
#define MAXCODES   (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */
 
#define FIXLCODES   288 /* number of fixed literal/length codes */
 

Functions

int bits (struct state *s, int need)
 
int stored (struct state *s)
 
int decode (struct state *s, const struct huffman *h)
 
int construct (struct huffman *h, const short *length, int n)
 
int codes (struct state *s, const struct huffman *lencode, const struct huffman *distcode)
 
int fixed (struct state *s)
 
int dynamic (struct state *s)
 
int puff (unsigned char *dest, unsigned long *destlen, const unsigned char *source, unsigned long *sourcelen)
 

Macro Definition Documentation

◆ FIXLCODES

#define FIXLCODES   288 /* number of fixed literal/length codes */

Definition at line 95 of file puff.c.

◆ local

#define local   static /* for local function definitions */

Definition at line 85 of file puff.c.

◆ MAXBITS

#define MAXBITS   15 /* maximum bits in a code */

Definition at line 91 of file puff.c.

◆ MAXCODES

#define MAXCODES   (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */

Definition at line 94 of file puff.c.

◆ MAXDCODES

#define MAXDCODES   30 /* maximum number of distance codes */

Definition at line 93 of file puff.c.

◆ MAXLCODES

#define MAXLCODES   286 /* maximum number of literal/length codes */

Definition at line 92 of file puff.c.

Function Documentation

◆ bits()

int bits ( struct state s,
int  need 
)

Definition at line 126 of file puff.c.

127 {
128  long val; /* bit accumulator (can use up to 20 bits) */
129 
130  /* load at least need bits into val */
131  val = s->bitbuf;
132  while (s->bitcnt < need) {
133  if (s->incnt == s->inlen)
134  longjmp(s->env, 1); /* out of input */
135  val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */
136  s->bitcnt += 8;
137  }
138 
139  /* drop need bits and update buffer, always zero to seven bits left */
140  s->bitbuf = (int)(val >> need);
141  s->bitcnt -= need;
142 
143  /* return need bits, zeroing the bits above that */
144  return (int)(val & ((1L << need) - 1));
145 }
ut16 val
Definition: armass64_const.h:6
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
static RzSocket * s
Definition: rtr.c:28
static int
Definition: sfsocketcall.h:114

References int, long, s, and val.

Referenced by codes(), dynamic(), and puff().

◆ codes()

int codes ( struct state s,
const struct huffman lencode,
const struct huffman distcode 
)

Definition at line 436 of file puff.c.

439 {
440  int symbol; /* decoded symbol */
441  int len; /* length for copy */
442  unsigned dist; /* distance for copy */
443  static const short lens[29] = { /* Size base for length codes 257..285 */
444  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
445  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
446  static const short lext[29] = { /* Extra bits for length codes 257..285 */
447  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
448  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
449  static const short dists[30] = { /* Offset base for distance codes 0..29 */
450  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
451  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
452  8193, 12289, 16385, 24577};
453  static const short dext[30] = { /* Extra bits for distance codes 0..29 */
454  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
455  7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
456  12, 12, 13, 13};
457 
458  /* decode literals and length/distance pairs */
459  do {
460  symbol = decode(s, lencode);
461  if (symbol < 0)
462  return symbol; /* invalid symbol */
463  if (symbol < 256) { /* literal: symbol is the byte */
464  /* write out the literal */
465  if (s->out != NIL) {
466  if (s->outcnt == s->outlen)
467  return 1;
468  s->out[s->outcnt] = symbol;
469  }
470  s->outcnt++;
471  }
472  else if (symbol > 256) { /* length */
473  /* get and compute length */
474  symbol -= 257;
475  if (symbol >= 29)
476  return -10; /* invalid fixed code */
477  len = lens[symbol] + bits(s, lext[symbol]);
478 
479  /* get and check distance */
480  symbol = decode(s, distcode);
481  if (symbol < 0)
482  return symbol; /* invalid symbol */
483  dist = dists[symbol] + bits(s, dext[symbol]);
484 #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
485  if (dist > s->outcnt)
486  return -11; /* distance too far back */
487 #endif
488 
489  /* copy length bytes from distance bytes back */
490  if (s->out != NIL) {
491  if (s->outcnt + len > s->outlen)
492  return 1;
493  while (len--) {
494  s->out[s->outcnt] =
495 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
496  dist > s->outcnt ?
497  0 :
498 #endif
499  s->out[s->outcnt - dist];
500  s->outcnt++;
501  }
502  }
503  else
504  s->outcnt += len;
505  }
506  } while (symbol != 256); /* end of block symbol */
507 
508  /* done with a valid fixed or dynamic block */
509  return 0;
510 }
size_t len
Definition: 6502dis.c:15
int decode(struct state *s, const struct huffman *h)
Definition: puff.c:263
int bits(struct state *s, int need)
Definition: puff.c:126
#define NIL
Definition: puff.h:29

References bits(), decode(), len, NIL, and s.

Referenced by dynamic(), entries(), fixed(), inflate_table(), inflate_table9(), rz_bin_wasm_get_codes(), and symbols().

◆ construct()

int construct ( struct huffman h,
const short *  length,
int  n 
)

Definition at line 340 of file puff.c.

341 {
342  int symbol; /* current symbol when stepping through length[] */
343  int len; /* current length when stepping through h->count[] */
344  int left; /* number of possible codes left of current length */
345  short offs[MAXBITS+1]; /* offsets in symbol table for each length */
346 
347  /* count number of codes of each length */
348  for (len = 0; len <= MAXBITS; len++)
349  h->count[len] = 0;
350  for (symbol = 0; symbol < n; symbol++)
351  (h->count[length[symbol]])++; /* assumes lengths are within bounds */
352  if (h->count[0] == n) /* no codes! */
353  return 0; /* complete, but decode() will fail */
354 
355  /* check for an over-subscribed or incomplete set of lengths */
356  left = 1; /* one possible code of zero length */
357  for (len = 1; len <= MAXBITS; len++) {
358  left <<= 1; /* one more bit, double codes left */
359  left -= h->count[len]; /* deduct count from possible codes */
360  if (left < 0)
361  return left; /* over-subscribed--return negative */
362  } /* left > 0 means incomplete */
363 
364  /* generate offsets into symbol table for each length for sorting */
365  offs[1] = 0;
366  for (len = 1; len < MAXBITS; len++)
367  offs[len + 1] = offs[len] + h->count[len];
368 
369  /*
370  * put symbols in table sorted by length, by symbol order within each
371  * length
372  */
373  for (symbol = 0; symbol < n; symbol++)
374  if (length[symbol] != 0)
375  h->symbol[offs[length[symbol]]++] = symbol;
376 
377  /* return zero for complete set, positive for incomplete set */
378  return left;
379 }
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
int n
Definition: mipsasm.c:19
#define MAXBITS
Definition: puff.c:91
#define h(i)
Definition: sha256.c:48

References h, len, length, MAXBITS, and n.

Referenced by dynamic(), and fixed().

◆ decode()

int decode ( struct state s,
const struct huffman h 
)

Definition at line 263 of file puff.c.

264 {
265  int len; /* current number of bits in code */
266  int code; /* len bits being decoded */
267  int first; /* first code of length len */
268  int count; /* number of codes of length len */
269  int index; /* index of first code of length len in symbol table */
270  int bitbuf; /* bits from stream */
271  int left; /* bits left in next or left to process */
272  short *next; /* next number of codes */
273 
274  bitbuf = s->bitbuf;
275  left = s->bitcnt;
276  code = first = index = 0;
277  len = 1;
278  next = h->count + 1;
279  while (1) {
280  while (left--) {
281  code |= bitbuf & 1;
282  bitbuf >>= 1;
283  count = *next++;
284  if (code - count < first) { /* if length len, return symbol */
285  s->bitbuf = bitbuf;
286  s->bitcnt = (s->bitcnt - len) & 7;
287  return h->symbol[index + (code - first)];
288  }
289  index += count; /* else update for next length */
290  first += count;
291  first <<= 1;
292  code <<= 1;
293  len++;
294  }
295  left = (MAXBITS+1) - len;
296  if (left == 0)
297  break;
298  if (s->incnt == s->inlen)
299  longjmp(s->env, 1); /* out of input */
300  bitbuf = s->in[s->incnt++];
301  if (left > 8)
302  left = 8;
303  }
304  return -10; /* ran out of codes */
305 }
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 count
Definition: sflib.h:98
const char * code
Definition: pal.c:98
Definition: inftree9.h:24

References code, count, h, len, MAXBITS, and s.

Referenced by codes(), and dynamic().

◆ dynamic()

int dynamic ( struct state s)

Definition at line 665 of file puff.c.

666 {
667  int nlen, ndist, ncode; /* number of lengths in descriptor */
668  int index; /* index of lengths[] */
669  int err; /* construct() return value */
670  short lengths[MAXCODES]; /* descriptor code lengths */
671  short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
672  short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
673  struct huffman lencode, distcode; /* length and distance codes */
674  static const short order[19] = /* permutation of code length codes */
675  {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
676 
677  /* construct lencode and distcode */
678  lencode.count = lencnt;
679  lencode.symbol = lensym;
680  distcode.count = distcnt;
681  distcode.symbol = distsym;
682 
683  /* get number of lengths in each table, check lengths */
684  nlen = bits(s, 5) + 257;
685  ndist = bits(s, 5) + 1;
686  ncode = bits(s, 4) + 4;
687  if (nlen > MAXLCODES || ndist > MAXDCODES)
688  return -3; /* bad counts */
689 
690  /* read code length code lengths (really), missing lengths are zero */
691  for (index = 0; index < ncode; index++)
692  lengths[order[index]] = bits(s, 3);
693  for (; index < 19; index++)
694  lengths[order[index]] = 0;
695 
696  /* build huffman table for code lengths codes (use lencode temporarily) */
697  err = construct(&lencode, lengths, 19);
698  if (err != 0) /* require complete code set here */
699  return -4;
700 
701  /* read length/literal and distance code length tables */
702  index = 0;
703  while (index < nlen + ndist) {
704  int symbol; /* decoded value */
705  int len; /* last length to repeat */
706 
707  symbol = decode(s, &lencode);
708  if (symbol < 0)
709  return symbol; /* invalid symbol */
710  if (symbol < 16) /* length in 0..15 */
711  lengths[index++] = symbol;
712  else { /* repeat instruction */
713  len = 0; /* assume repeating zeros */
714  if (symbol == 16) { /* repeat last length 3..6 times */
715  if (index == 0)
716  return -5; /* no last length! */
717  len = lengths[index - 1]; /* last length */
718  symbol = 3 + bits(s, 2);
719  }
720  else if (symbol == 17) /* repeat zero 3..10 times */
721  symbol = 3 + bits(s, 3);
722  else /* == 18, repeat zero 11..138 times */
723  symbol = 11 + bits(s, 7);
724  if (index + symbol > nlen + ndist)
725  return -6; /* too many lengths! */
726  while (symbol--) /* repeat last or zero symbol times */
727  lengths[index++] = len;
728  }
729  }
730 
731  /* check for end-of-block code -- there better be one! */
732  if (lengths[256] == 0)
733  return -9;
734 
735  /* build huffman table for literal/length codes */
736  err = construct(&lencode, lengths, nlen);
737  if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
738  return -7; /* incomplete code ok only for single length 1 code */
739 
740  /* build huffman table for distance codes */
741  err = construct(&distcode, lengths + nlen, ndist);
742  if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
743  return -8; /* incomplete code ok only for single length 1 code */
744 
745  /* decode data until end-of-block code */
746  return codes(s, &lencode, &distcode);
747 }
static bool err
Definition: armass.c:435
#define MAXDCODES
Definition: puff.c:93
int construct(struct huffman *h, const short *length, int n)
Definition: puff.c:340
#define MAXCODES
Definition: puff.c:94
#define MAXLCODES
Definition: puff.c:92
int codes(struct state *s, const struct huffman *lencode, const struct huffman *distcode)
Definition: puff.c:436
Definition: blast.c:103
short * count
Definition: blast.c:104
short * symbol
Definition: blast.c:105

References bits(), codes(), construct(), huffman::count, decode(), err, len, MAXBITS, MAXCODES, MAXDCODES, MAXLCODES, s, and huffman::symbol.

Referenced by parseMod(), and puff().

◆ fixed()

int fixed ( struct state s)

Definition at line 536 of file puff.c.

537 {
538  static int virgin = 1;
539  static short lencnt[MAXBITS+1], lensym[FIXLCODES];
540  static short distcnt[MAXBITS+1], distsym[MAXDCODES];
541  static struct huffman lencode, distcode;
542 
543  /* build fixed huffman tables if first call (may not be thread safe) */
544  if (virgin) {
545  int symbol;
546  short lengths[FIXLCODES];
547 
548  /* construct lencode and distcode */
549  lencode.count = lencnt;
550  lencode.symbol = lensym;
551  distcode.count = distcnt;
552  distcode.symbol = distsym;
553 
554  /* literal/length table */
555  for (symbol = 0; symbol < 144; symbol++)
556  lengths[symbol] = 8;
557  for (; symbol < 256; symbol++)
558  lengths[symbol] = 9;
559  for (; symbol < 280; symbol++)
560  lengths[symbol] = 7;
561  for (; symbol < FIXLCODES; symbol++)
562  lengths[symbol] = 8;
563  construct(&lencode, lengths, FIXLCODES);
564 
565  /* distance table */
566  for (symbol = 0; symbol < MAXDCODES; symbol++)
567  lengths[symbol] = 5;
568  construct(&distcode, lengths, MAXDCODES);
569 
570  /* do this just once */
571  virgin = 0;
572  }
573 
574  /* decode data until end-of-block code */
575  return codes(s, &lencode, &distcode);
576 }
#define FIXLCODES
Definition: puff.c:95

References codes(), construct(), huffman::count, FIXLCODES, MAXBITS, MAXDCODES, s, and huffman::symbol.

Referenced by fixedtables(), and puff().

◆ puff()

int puff ( unsigned char *  dest,
unsigned long destlen,
const unsigned char *  source,
unsigned long sourcelen 
)

Definition at line 793 of file puff.c.

797 {
798  struct state s; /* input/output state */
799  int last, type; /* block information */
800  int err; /* return value */
801 
802  /* initialize output state */
803  s.out = dest;
804  s.outlen = *destlen; /* ignored if dest is NIL */
805  s.outcnt = 0;
806 
807  /* initialize input state */
808  s.in = source;
809  s.inlen = *sourcelen;
810  s.incnt = 0;
811  s.bitbuf = 0;
812  s.bitcnt = 0;
813 
814  /* return if bits() or decode() tries to read past available input */
815  if (setjmp(s.env) != 0) /* if came back here via longjmp() */
816  err = 2; /* then skip do-loop, return error */
817  else {
818  /* process blocks until last block or error */
819  do {
820  last = bits(&s, 1); /* one if last block */
821  type = bits(&s, 2); /* block type 0..3 */
822  err = type == 0 ?
823  stored(&s) :
824  (type == 1 ?
825  fixed(&s) :
826  (type == 2 ?
827  dynamic(&s) :
828  -1)); /* type == 3, invalid */
829  if (err != 0)
830  break; /* return with error */
831  } while (!last);
832  }
833 
834  /* update the lengths and return */
835  if (err <= 0) {
836  *destlen = s.outcnt;
837  *sourcelen = s.incnt;
838  }
839  return err;
840 }
const char * source
Definition: lz4.h:699
char * dest
Definition: lz4.h:697
int type
Definition: mipsasm.c:17
int stored(struct state *s)
Definition: puff.c:164
int dynamic(struct state *s)
Definition: puff.c:665
int fixed(struct state *s)
Definition: puff.c:536
Definition: dis.h:43

References bits(), dest, dynamic(), err, fixed(), s, source, stored(), and type.

Referenced by main().

◆ stored()

int stored ( struct state s)

Definition at line 164 of file puff.c.

165 {
166  unsigned len; /* length of stored block */
167 
168  /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
169  s->bitbuf = 0;
170  s->bitcnt = 0;
171 
172  /* get length and check against its one's complement */
173  if (s->incnt + 4 > s->inlen)
174  return 2; /* not enough input */
175  len = s->in[s->incnt++];
176  len |= s->in[s->incnt++] << 8;
177  if (s->in[s->incnt++] != (~len & 0xff) ||
178  s->in[s->incnt++] != ((~len >> 8) & 0xff))
179  return -2; /* didn't match complement! */
180 
181  /* copy len bytes from in to out */
182  if (s->incnt + len > s->inlen)
183  return 2; /* not enough input */
184  if (s->out != NIL) {
185  if (s->outcnt + len > s->outlen)
186  return 1; /* not enough output space */
187  while (len--)
188  s->out[s->outcnt++] = s->in[s->incnt++];
189  }
190  else { /* just scanning */
191  s->outcnt += len;
192  s->incnt += len;
193  }
194 
195  /* done with a valid stored block */
196  return 0;
197 }

References len, NIL, and s.

Referenced by bin_pe_init_sections(), and puff().