linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7] inflate pt1: lindent and manual formatting changes
       [not found] <0.399206195@selenic.com>
  2006-02-24 20:12 ` [PATCH 3/7] inflate pt1: clean up input logic Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 20:12 ` [PATCH 4/7] inflate pt1: start moving globals into iostate Matt Mackall
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: lindent and general reformatting

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:11:45.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:00.000000000 -0600
@@ -1,17 +1,17 @@
 #define DEBG(x)
 #define DEBG1(x)
 /* inflate.c -- Not copyrighted 1992 by Mark Adler
-   version c10p1, 10 January 1993 */
-
-/* 
+ * version c10p1, 10 January 1993
+ *
  * Adapted for booting Linux by Hannu Savolainen 1993
- * based on gzip-1.0.3 
+ * based on gzip-1.0.3
  *
  * Nicolas Pitre <nico@cam.org>, 1999/04/14 :
- *   Little mods for all variable to reside either into rodata or bss segments
- *   by marking constant variables with 'const' and initializing all the others
- *   at run-time only.  This allows for the kernel uncompressor to run
- *   directly from Flash or ROM memory on embedded systems.
+ *   Little mods for all variable to reside either into rodata or bss
+ *   segments by marking constant variables with 'const' and
+ *   initializing all the others at run-time only. This allows for the
+ *   kernel uncompressor to run directly from Flash or ROM memory on
+ *   embedded systems.
  */
 
 /*
@@ -50,14 +50,13 @@
    chunks), otherwise the dynamic method is used.  In the latter case, the
    codes are customized to the probabilities in the current block, and so
    can code it much better than the pre-determined fixed codes.
- 
+
    The Huffman codes themselves are decoded using a multi-level table
    lookup, in order to maximize the speed of decoding plus the speed of
    building the decoding tables.  See the comments below that precede the
    lbits and dbits tuning parameters.
  */
 
-
 /*
    Notes beyond the 1.93a appnote.txt:
 
@@ -122,7 +121,7 @@ static char rcsid[] = "#Id: inflate.c,v 
 #ifndef INIT
 #define INIT
 #endif
-	
+
 #define slide window
 
 /* Huffman code lookup table entry--this entry is four bytes for machines
@@ -133,17 +132,16 @@ static char rcsid[] = "#Id: inflate.c,v 
    an unused code.  If a code with e == 99 is looked up, this implies an
    error in the data. */
 struct huft {
-  uch e;                /* number of extra bits or operation */
-  uch b;                /* number of bits in this code or subcode */
-  union {
-    ush n;              /* literal, length base, or distance base */
-    struct huft *t;     /* pointer to next level of table */
-  } v;
+	uch e;			/* number of extra bits or operation */
+	uch b;			/* number of bits in this code or subcode */
+	union {
+		ush n;		/* literal, length base, or distance base */
+		struct huft *t;	/* pointer to next level of table */
+	} v;
 };
 
-
 /* Function prototypes */
-STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned, 
+STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
 		const ush *, const ush *, struct huft **, int *));
 STATIC int INIT huft_free OF((struct huft *));
 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
@@ -153,7 +151,6 @@ STATIC int INIT inflate_dynamic OF((void
 STATIC int INIT inflate_block OF((int *));
 STATIC int INIT inflate OF((void));
 
-
 /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
    stream to find repeated byte strings.  This is implemented here as a
    circular buffer.  The index is updated simply by incrementing and then
@@ -167,29 +164,44 @@ STATIC int INIT inflate OF((void));
 #define flush_output(w) (wp=(w),flush_window())
 
 /* Tables for deflate from PKZIP's appnote.txt. */
-static const unsigned border[] = {    /* Order of the bit length code lengths */
-        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
-static const ush cplens[] = {         /* Copy lengths for literal codes 257..285 */
-        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, 0, 0};
-        /* note: see note #13 above about the 258 in this list. */
-static const ush cplext[] = {         /* Extra bits for literal codes 257..285 */
-        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, 99, 99}; /* 99==invalid */
-static const ush cpdist[] = {         /* Copy offsets for distance codes 0..29 */
-        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};
-static const ush cpdext[] = {         /* Extra bits for distance codes */
-        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};
 
+/* Order of the bit length code lengths */
+static const unsigned border[] = {
+	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+};
+
+/* Copy lengths for literal codes 257..285 */
+static const ush cplens[] = {
+	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, 0, 0
+};
+
+/* Extra bits for literal codes 257..285
+ * note: see note #13 above about the 258 in this list.
+ * 99==invalid
+ */
+static const ush cplext[] = {
+	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, 99, 99
+};
+
+/* Copy offsets for distance codes 0..29 */
+static const ush cpdist[] = {
+	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
+};
 
+/* Extra bits for distance codes */
+static const ush cpdext[] = {
+	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
+};
 
 /* Macros for inflate() bit peeking and grabbing.
    The usage is:
-   
+
         NEEDBITS(j)
         x = b & mask_bits[j];
         DUMPBITS(j)
@@ -217,19 +229,18 @@ static const ush cpdext[] = {         /*
    the stream.
  */
 
-STATIC ulg bb;                         /* bit buffer */
-STATIC unsigned bk;                    /* bits in bit buffer */
+STATIC ulg bb;			/* bit buffer */
+STATIC unsigned bk;		/* bits in bit buffer */
 
 STATIC const ush mask_bits[] = {
-    0x0000,
-    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
-    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
+	0x0000,
+	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
+	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
 };
 
 #define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
-#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
-#define DUMPBITS(n) {b>>=(n);k-=(n);}
-
+#define NEEDBITS(n) do {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}} while(0)
+#define DUMPBITS(n) do {b>>=(n);k-=(n);} while(0)
 
 /*
    Huffman code decoding is performed using a multi-level table lookup.
@@ -263,771 +274,757 @@ STATIC const ush mask_bits[] = {
    possibly even between compilers.  Your mileage may vary.
  */
 
-
-STATIC const int lbits = 9;          /* bits in base literal/length lookup table */
-STATIC const int dbits = 6;          /* bits in base distance lookup table */
-
+STATIC const int lbits = 9;	/* bits in base literal/length lookup table */
+STATIC const int dbits = 6;	/* bits in base distance lookup table */
 
 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
-#define BMAX 16         /* maximum bit length of any code (16 for explode) */
-#define N_MAX 288       /* maximum number of codes in any set */
-
+#define BMAX 16		/* maximum bit length of any code (16 for explode) */
+#define N_MAX 288	/* maximum number of codes in any set */
 
-STATIC unsigned hufts;         /* track memory usage */
+STATIC unsigned hufts;		/* track memory usage */
 
-
-STATIC int INIT huft_build(
-	unsigned *b,            /* code lengths in bits (all assumed <= BMAX) */
-	unsigned n,             /* number of codes (assumed <= N_MAX) */
-	unsigned s,             /* number of simple-valued codes (0..s-1) */
-	const ush *d,           /* list of base values for non-simple codes */
-	const ush *e,           /* list of extra bits for non-simple codes */
-	struct huft **t,        /* result: starting table */
-	int *m                  /* maximum lookup bits, returns actual */
-	)
-/* Given a list of code lengths and a maximum table size, make a set of
-   tables to decode that set of codes.  Return zero on success, one if
-   the given code set is incomplete (the tables are still built in this
-   case), two if the input is invalid (all zero length codes or an
-   oversubscribed set of lengths), and three if not enough memory. */
+/*
+ * huft-build - build a huffman decoding table
+ * @b: code lengths in bits (all assumed <= BMAX)
+ * @n: number of codes (assumed <= N_MAX)
+ * @s: number of simple-valued codes (0..s-1)
+ * @d: list of base values for non-simple codes
+ * @e: list of extra bits for non-simple codes
+ * @t: returns pointer to starting table
+ * @m: maximum lookup bits, returns actual
+ *
+ * Given a list of code lengths and a maximum table size, make a set
+ * of tables to decode that set of codes. Return zero on success, one
+ * if the given code set is incomplete (the tables are still built in
+ * this case), two if the input is invalid (all zero length codes or
+ * an oversubscribed set of lengths), and three if not enough
+ * memory.
+ */
+STATIC int INIT huft_build(unsigned *b, unsigned n, unsigned s, const ush * d,
+		      const ush * e, struct huft **t, int *m)
 {
-  unsigned a;                   /* counter for codes of length k */
-  unsigned c[BMAX+1];           /* bit length count table */
-  unsigned f;                   /* i repeats in table every f entries */
-  int g;                        /* maximum code length */
-  int h;                        /* table level */
-  register unsigned i;          /* counter, current code */
-  register unsigned j;          /* counter */
-  register int k;               /* number of bits in current code */
-  int l;                        /* bits per table (returned in m) */
-  register unsigned *p;         /* pointer into c[], b[], or v[] */
-  register struct huft *q;      /* points to current table */
-  struct huft r;                /* table entry for structure assignment */
-  struct huft *u[BMAX];         /* table stack */
-  unsigned v[N_MAX];            /* values in order of bit length */
-  register int w;               /* bits before this table == (l * h) */
-  unsigned x[BMAX+1];           /* bit offsets, then code stack */
-  unsigned *xp;                 /* pointer into x */
-  int y;                        /* number of dummy codes added */
-  unsigned z;                   /* number of entries in current table */
-
-DEBG("huft1 ");
-
-  /* Generate counts for each bit length */
-  memzero(c, sizeof(c));
-  p = b;  i = n;
-  do {
-    Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"), 
-	    n-i, *p));
-    c[*p]++;                    /* assume all entries <= BMAX */
-    p++;                      /* Can't combine with above line (Solaris bug) */
-  } while (--i);
-  if (c[0] == n)                /* null input--all zero length codes */
-  {
-    *t = (struct huft *)NULL;
-    *m = 0;
-    return 2;
-  }
-
-DEBG("huft2 ");
-
-  /* Find minimum and maximum length, bound *m by those */
-  l = *m;
-  for (j = 1; j <= BMAX; j++)
-    if (c[j])
-      break;
-  k = j;                        /* minimum code length */
-  if ((unsigned)l < j)
-    l = j;
-  for (i = BMAX; i; i--)
-    if (c[i])
-      break;
-  g = i;                        /* maximum code length */
-  if ((unsigned)l > i)
-    l = i;
-  *m = l;
-
-DEBG("huft3 ");
-
-  /* Adjust last length count to fill out codes, if needed */
-  for (y = 1 << j; j < i; j++, y <<= 1)
-    if ((y -= c[j]) < 0)
-      return 2;                 /* bad input: more codes than bits */
-  if ((y -= c[i]) < 0)
-    return 2;
-  c[i] += y;
-
-DEBG("huft4 ");
-
-  /* Generate starting offsets into the value table for each length */
-  x[1] = j = 0;
-  p = c + 1;  xp = x + 2;
-  while (--i) {                 /* note that i == g from above */
-    *xp++ = (j += *p++);
-  }
-
-DEBG("huft5 ");
-
-  /* Make a table of values in order of bit lengths */
-  p = b;  i = 0;
-  do {
-    if ((j = *p++) != 0)
-      v[x[j]++] = i;
-  } while (++i < n);
-  n = x[g];                   /* set n to length of v */
-
-DEBG("h6 ");
-
-  /* Generate the Huffman codes and for each, make the table entries */
-  x[0] = i = 0;                 /* first Huffman code is zero */
-  p = v;                        /* grab values in bit order */
-  h = -1;                       /* no tables yet--level -1 */
-  w = -l;                       /* bits decoded == (l * h) */
-  u[0] = (struct huft *)NULL;   /* just to keep compilers happy */
-  q = (struct huft *)NULL;      /* ditto */
-  z = 0;                        /* ditto */
-DEBG("h6a ");
-
-  /* go through the bit lengths (k already is bits in shortest code) */
-  for (; k <= g; k++)
-  {
-DEBG("h6b ");
-    a = c[k];
-    while (a--)
-    {
-DEBG("h6b1 ");
-      /* here i is the Huffman code of length k bits for value *p */
-      /* make tables up to required level */
-      while (k > w + l)
-      {
-DEBG1("1 ");
-        h++;
-        w += l;                 /* previous table always l bits */
-
-        /* compute minimum size table less than or equal to l bits */
-        z = (z = g - w) > (unsigned)l ? l : z;  /* upper limit on table size */
-        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
-        {                       /* too few codes for k-w bit table */
-DEBG1("2 ");
-          f -= a + 1;           /* deduct codes from patterns left */
-          xp = c + k;
-          if (j < z)
-            while (++j < z)       /* try smaller tables up to z bits */
-            {
-              if ((f <<= 1) <= *++xp)
-                break;            /* enough codes to use up j bits */
-              f -= *xp;           /* else deduct codes from patterns */
-            }
-        }
-DEBG1("3 ");
-        z = 1 << j;             /* table entries for j-bit table */
-
-        /* allocate and link in new table */
-        if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
-            (struct huft *)NULL)
-        {
-          if (h)
-            huft_free(u[0]);
-          return 3;             /* not enough memory */
-        }
-DEBG1("4 ");
-        hufts += z + 1;         /* track memory usage */
-        *t = q + 1;             /* link to list for huft_free() */
-        *(t = &(q->v.t)) = (struct huft *)NULL;
-        u[h] = ++q;             /* table starts after link */
-
-DEBG1("5 ");
-        /* connect to last table, if there is one */
-        if (h)
-        {
-          x[h] = i;             /* save pattern for backing up */
-          r.b = (uch)l;         /* bits to dump before this table */
-          r.e = (uch)(16 + j);  /* bits in this table */
-          r.v.t = q;            /* pointer to this table */
-          j = i >> (w - l);     /* (get around Turbo C bug) */
-          u[h-1][j] = r;        /* connect to last table */
-        }
-DEBG1("6 ");
-      }
-DEBG("h6c ");
-
-      /* set up table entry in r */
-      r.b = (uch)(k - w);
-      if (p >= v + n)
-        r.e = 99;               /* out of values--invalid code */
-      else if (*p < s)
-      {
-        r.e = (uch)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
-        r.v.n = (ush)(*p);             /* simple code is just the value */
-	p++;                           /* one compiler does not like *p++ */
-      }
-      else
-      {
-        r.e = (uch)e[*p - s];   /* non-simple--look up in lists */
-        r.v.n = d[*p++ - s];
-      }
-DEBG("h6d ");
-
-      /* fill code-like entries with r */
-      f = 1 << (k - w);
-      for (j = i >> w; j < z; j += f)
-        q[j] = r;
-
-      /* backwards increment the k-bit code i */
-      for (j = 1 << (k - 1); i & j; j >>= 1)
-        i ^= j;
-      i ^= j;
-
-      /* backup over finished tables */
-      while ((i & ((1 << w) - 1)) != x[h])
-      {
-        h--;                    /* don't need to update q */
-        w -= l;
-      }
-DEBG("h6e ");
-    }
-DEBG("h6f ");
-  }
+	unsigned a;		/* counter for codes of length k */
+	unsigned c[BMAX + 1];	/* bit length count table */
+	unsigned f;		/* i repeats in table every f entries */
+	int g;			/* maximum code length */
+	int h;			/* table level */
+	register unsigned i;	/* counter, current code */
+	register unsigned j;	/* counter */
+	register int k;		/* number of bits in current code */
+	int l;			/* bits per table (returned in m) */
+	register unsigned *p;	/* pointer into c[], b[], or v[] */
+	register struct huft *q;	/* points to current table */
+	struct huft r;		/* table entry for structure assignment */
+	struct huft *u[BMAX];	/* table stack */
+	unsigned v[N_MAX];	/* values in order of bit length */
+	register int w;		/* bits before this table == (l * h) */
+	unsigned x[BMAX + 1];	/* bit offsets, then code stack */
+	unsigned *xp;		/* pointer into x */
+	int y;			/* number of dummy codes added */
+	unsigned z;		/* number of entries in current table */
+
+	DEBG("huft1 ");
+
+	/* Generate counts for each bit length */
+	memzero(c, sizeof(c));
+	p = b;
+	i = n;
+	do {
+		Tracecv(*p, (stderr, (n - i >= ' ' && n - i <= '~' ?
+				      "%c %d\n" : "0x%x %d\n"), n - i, *p));
+		c[*p]++;	/* assume all entries <= BMAX */
+		p++;
+	} while (--i);
+
+	if (c[0] == n) {	/* null input--all zero length codes */
+		*t = (struct huft *)NULL;
+		*m = 0;
+		return 2;
+	}
+
+	DEBG("huft2 ");
+
+	/* Find minimum and maximum length, bound *m by those */
+	l = *m;
+	for (j = 1; j <= BMAX; j++)
+		if (c[j])
+			break;
+	k = j; /* minimum code length */
+
+	if ((unsigned)l < j)
+		l = j;
+	for (i = BMAX; i; i--)
+		if (c[i])
+			break;
+	g = i; /* maximum code length */
+
+	if ((unsigned)l > i)
+		l = i;
+	*m = l;
+
+	DEBG("huft3 ");
+
+	/* Adjust last length count to fill out codes, if needed */
+	for (y = 1 << j; j < i; j++, y <<= 1)
+		if ((y -= c[j]) < 0)
+			return 2; /* bad input: more codes than bits */
+
+	if ((y -= c[i]) < 0)
+		return 2;
+	c[i] += y;
+
+	DEBG("huft4 ");
+
+	/* Generate starting offsets into the value table for each length */
+	x[1] = j = 0;
+	p = c + 1;
+	xp = x + 2;
+	/* note that i == g from above */
+	while (--i)
+		*xp++ = (j += *p++);
+
+	DEBG("huft5 ");
+
+	/* Make a table of values in order of bit lengths */
+	p = b;
+	i = 0;
+	do {
+		if ((j = *p++) != 0)
+			v[x[j]++] = i;
+	} while (++i < n);
+
+	n = x[g];                   /* set n to length of v */
+
+	DEBG("h6 ");
+
+	/* Generate the Huffman codes and for each, make the table entries */
+	x[0] = i = 0; /* first Huffman code is zero */
+	p = v; /* grab values in bit order */
+	h = -1; /* no tables yet--level -1 */
+	w = -l;	/* bits decoded == (l * h) */
+	u[0] = (struct huft *)NULL; /* just to keep compilers happy */
+	q = (struct huft *)NULL; /* ditto */
+	z = 0; /* ditto */
+	DEBG("h6a ");
+
+	/* go through the bit lengths (k already is bits in shortest code) */
+	for (; k <= g; k++) {
+		DEBG("h6b ");
+		a = c[k];
+		while (a--) {
+			DEBG("h6b1 ");
+			/* i is the Huffman code of length k for value *p */
+			/* make tables up to required level */
+			while (k > w + l) {
+				DEBG1("1 ");
+				h++;
+				w += l;	/* previous table always l bits */
+
+				/* compute min size <= l bits */
+				/* upper limit on table size */
+				z = (z = g - w) > (unsigned)l ? l : z;
+
+				/* try a k-w bit table */
+				if ((f = 1 << (j = k - w)) > a + 1) {
+					/* too few codes for k-w bit table */
+					DEBG1("2 ");
+					/* deduct codes from patterns left */
+					f -= a + 1;
+					xp = c + k;
+					/* try smaller tables up to z bits */
+					if (j < z) {
+						/* enough codes for j bits? */
+						while (++j < z) {
+							if ((f <<= 1) <= *++xp)
+								break;
+							/* deduct from pats */
+							f -= *xp;
+						}
+					}
+    				}
+
+				DEBG1("3 ");
+				/* table entries for j-bit table */
+				z = 1 << j;
+
+				/* allocate and link in new table */
+				if ((q = (struct huft *)malloc(
+					     (z + 1) * sizeof(struct huft)))
+				    == (struct huft *)NULL) {
+					if (h)
+						huft_free(u[0]);
+					return 3;	/* not enough memory */
+				}
+				DEBG1("4 ");
+				hufts += z + 1;	/* track memory usage */
+				*t = q + 1; /* link to list for huft_free */
+				*(t = &(q->v.t)) = (struct huft *)NULL;
+				u[h] = ++q;	/* table starts after link */
+
+				DEBG1("5 ");
+				/* connect to last table, if there is one */
+				if (h) {
+					/* save pattern for backing up */
+					x[h] = i;
+					/* bits to dump before this table */
+					r.b = (uch)l;
+					/* bits in this table */
+					r.e = (uch)(16 + j);
+					/* pointer to this table */
+					r.v.t = q;
+					/* (get around Turbo C bug) */
+					j = i >> (w - l);
+					/* connect to last table */
+					u[h - 1][j] = r;
+				}
+				DEBG1("6 ");
+			}
+			DEBG("h6c ");
+
+			/* set up table entry in r */
+			r.b = (uch) (k - w);
+			if (p >= v + n)
+				r.e = 99; /* out of values--invalid code */
+			else if (*p < s) {
+				/* 256 is end-of-block code */
+				r.e = (uch) (*p < 256 ? 16 : 15);
+				/* simple code is just the value */
+				r.v.n = (ush) (*p);
+				/* one compiler does not like *p++ */
+				p++;
+			} else {
+				/* non-simple--look up in lists */
+				r.e = (uch)e[*p - s];
+				r.v.n = d[*p++ - s];
+			}
+			DEBG("h6d ");
+
+			/* fill code-like entries with r */
+			f = 1 << (k - w);
+			for (j = i >> w; j < z; j += f)
+				q[j] = r;
+
+			/* backwards increment the k-bit code i */
+			for (j = 1 << (k - 1); i & j; j >>= 1)
+				i ^= j;
+			i ^= j;
+
+			/* backup over finished tables */
+			while ((i & ((1 << w) - 1)) != x[h]) {
+				h--;	/* don't need to update q */
+				w -= l;
+			}
+			DEBG("h6e ");
+		}
+		DEBG("h6f ");
+	}
 
-DEBG("huft7 ");
+	DEBG("huft7 ");
 
-  /* Return true (1) if we were given an incomplete table */
-  return y != 0 && g != 1;
+	/* Return true (1) if we were given an incomplete table */
+	return y != 0 && g != 1;
 }
 
-
-
-STATIC int INIT huft_free(
-	struct huft *t         /* table to free */
-	)
-/* Free the malloc'ed tables built by huft_build(), which makes a linked
-   list of the tables it made, with the links in a dummy first entry of
-   each table. */
+/*
+ * huft_free - free a huffman table
+ * @t: table to free
+ *
+ * Free the malloc'ed tables built by huft_build(), which makes a
+ * linked list of the tables it made, with the links in a dummy first
+ * entry of each table.
+ */
+STATIC int INIT huft_free(struct huft *t)
 {
-  register struct huft *p, *q;
+	register struct huft *p, *q;
 
-
-  /* Go through linked list, freeing from the malloced (t[-1]) address. */
-  p = t;
-  while (p != (struct huft *)NULL)
-  {
-    q = (--p)->v.t;
-    free((char*)p);
-    p = q;
-  } 
-  return 0;
+	/* Go through list, freeing from the malloced (t[-1]) address. */
+	p = t;
+	while (p != (struct huft *)NULL) {
+		q = (--p)->v.t;
+		free((char *)p);
+		p = q;
+	}
+	return 0;
 }
 
-
-STATIC int INIT inflate_codes(
-	struct huft *tl,    /* literal/length decoder tables */
-	struct huft *td,    /* distance decoder tables */
-	int bl,             /* number of bits decoded by tl[] */
-	int bd              /* number of bits decoded by td[] */
-	)
-/* inflate (decompress) the codes in a deflated (compressed) block.
-   Return an error code or zero if it all goes ok. */
+/*
+ * inflate_codes - decompress the codes in a deflated block
+ * @tl: literal/length decoder tables
+ * @td: distance decoder tables
+ * @bl: number of bits decoded by tl
+ * @bd: number of bits decoded by td
+ *
+ * inflate (decompress) the codes in a deflated (compressed) block.
+ * Return an error code or zero if it all goes ok.
+ */
+STATIC int inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
 {
-  register unsigned e;  /* table entry flag/number of extra bits */
-  unsigned n, d;        /* length and index for copy */
-  unsigned w;           /* current window position */
-  struct huft *t;       /* pointer to table entry */
-  unsigned ml, md;      /* masks for bl and bd bits */
-  register ulg b;       /* bit buffer */
-  register unsigned k;  /* number of bits in bit buffer */
-
-
-  /* make local copies of globals */
-  b = bb;                       /* initialize bit buffer */
-  k = bk;
-  w = wp;                       /* initialize window position */
-
-  /* inflate the coded data */
-  ml = mask_bits[bl];           /* precompute masks for speed */
-  md = mask_bits[bd];
-  for (;;)                      /* do until end of block */
-  {
-    NEEDBITS((unsigned)bl)
-    if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
-      do {
-        if (e == 99)
-          return 1;
-        DUMPBITS(t->b)
-        e -= 16;
-        NEEDBITS(e)
-      } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
-    DUMPBITS(t->b)
-    if (e == 16)                /* then it's a literal */
-    {
-      slide[w++] = (uch)t->v.n;
-      Tracevv((stderr, "%c", slide[w-1]));
-      if (w == WSIZE)
-      {
-        flush_output(w);
-        w = 0;
-      }
-    }
-    else                        /* it's an EOB or a length */
-    {
-      /* exit if end of block */
-      if (e == 15)
-        break;
-
-      /* get length of block to copy */
-      NEEDBITS(e)
-      n = t->v.n + ((unsigned)b & mask_bits[e]);
-      DUMPBITS(e);
-
-      /* decode distance of block to copy */
-      NEEDBITS((unsigned)bd)
-      if ((e = (t = td + ((unsigned)b & md))->e) > 16)
-        do {
-          if (e == 99)
-            return 1;
-          DUMPBITS(t->b)
-          e -= 16;
-          NEEDBITS(e)
-        } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
-      DUMPBITS(t->b)
-      NEEDBITS(e)
-      d = w - t->v.n - ((unsigned)b & mask_bits[e]);
-      DUMPBITS(e)
-      Tracevv((stderr,"\\[%d,%d]", w-d, n));
-
-      /* do the copy */
-      do {
-        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
+	register unsigned e;	/* table entry flag/number of extra bits */
+	unsigned n, d;		/* length and index for copy */
+	unsigned w;		/* current window position */
+	struct huft *t;		/* pointer to table entry */
+	unsigned ml, md;	/* masks for bl and bd bits */
+	register ulg b;		/* bit buffer */
+	register unsigned k;	/* number of bits in bit buffer */
+
+	/* make local copies of globals */
+	b = bb;			/* initialize bit buffer */
+	k = bk;
+	w = wp;			/* initialize window position */
+
+	/* inflate the coded data */
+	ml = mask_bits[bl];	/* precompute masks for speed */
+	md = mask_bits[bd];
+	for (;;) {		/* do until end of block */
+		NEEDBITS((unsigned)bl);
+		if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
+			do {
+				if (e == 99)
+					return 1;
+				DUMPBITS(t->b);
+				e -= 16;
+				NEEDBITS(e);
+			} while ((e = (t = t->v.t + ((unsigned)b &
+						     mask_bits[e]))->e) > 16);
+		DUMPBITS(t->b);
+		if (e == 16) {	/* then it's a literal */
+			slide[w++] = (uch)t->v.n;
+			Tracevv((stderr, "%c", slide[w - 1]));
+			if (w == WSIZE) {
+				flush_output(w);
+				w = 0;
+			}
+		} else {	/* it's an EOB or a length */
+			/* exit if end of block */
+			if (e == 15)
+				break;
+
+			/* get length of block to copy */
+			NEEDBITS(e);
+			n = t->v.n + ((unsigned)b & mask_bits[e]);
+			DUMPBITS(e);
+
+			/* decode distance of block to copy */
+			NEEDBITS((unsigned)bd);
+			if ((e = (t = td + ((unsigned)b & md))->e) > 16)
+				do {
+					if (e == 99)
+						return 1;
+					DUMPBITS(t->b);
+					e -= 16;
+					NEEDBITS(e);
+				} while ((e = (t = t->v.t + ((unsigned)b
+					   & mask_bits[e]))->e) > 16);
+			DUMPBITS(t->b);
+			NEEDBITS(e);
+			d = w - t->v.n - ((unsigned)b & mask_bits[e]);
+			DUMPBITS(e)
+			    Tracevv((stderr, "\\[%d,%d]", w - d, n));
+
+			/* do the copy */
+			do {
+				n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ?
+							d : w)) > n ? n : e);
 #if !defined(NOMEMCPY) && !defined(DEBUG)
-        if (w - d >= e)         /* (this test assumes unsigned comparison) */
-        {
-          memcpy(slide + w, slide + d, e);
-          w += e;
-          d += e;
-        }
-        else                      /* do it slow to avoid memcpy() overlap */
-#endif /* !NOMEMCPY */
-          do {
-            slide[w++] = slide[d++];
-	    Tracevv((stderr, "%c", slide[w-1]));
-          } while (--e);
-        if (w == WSIZE)
-        {
-          flush_output(w);
-          w = 0;
-        }
-      } while (n);
-    }
-  }
-
-
-  /* restore the globals from the locals */
-  wp = w;                       /* restore global window pointer */
-  bb = b;                       /* restore global bit buffer */
-  bk = k;
+				/* (this test assumes unsigned comparison) */
+				if (w - d >= e) {
+					memcpy(slide + w, slide + d, e);
+					w += e;
+					d += e;
+				} else
+#endif				/* !NOMEMCPY */
+					/* avoid memcpy() overlap */
+					do {
+						slide[w++] = slide[d++];
+						Tracevv((stderr, "%c",
+							 slide[w - 1]));
+					} while (--e);
+				if (w == WSIZE) {
+					flush_output(w);
+					w = 0;
+				}
+			} while (n);
+		}
+	}
+
+	/* restore the globals from the locals */
+	wp = w;			/* restore global window pointer */
+	bb = b;			/* restore global bit buffer */
+	bk = k;
 
-  /* done */
-  return 0;
+	/* done */
+	return 0;
 
- underrun:
-  return 4;			/* Input underrun */
+      underrun:
+	return 4;		/* Input underrun */
 }
 
-
-
+/* inflate_stored - "decompress" an inflated type 0 (stored) block. */
 STATIC int INIT inflate_stored(void)
-/* "decompress" an inflated type 0 (stored) block. */
 {
-  unsigned n;           /* number of bytes in block */
-  unsigned w;           /* current window position */
-  register ulg b;       /* bit buffer */
-  register unsigned k;  /* number of bits in bit buffer */
-
-DEBG("<stor");
-
-  /* make local copies of globals */
-  b = bb;                       /* initialize bit buffer */
-  k = bk;
-  w = wp;                       /* initialize window position */
-
-
-  /* go to byte boundary */
-  n = k & 7;
-  DUMPBITS(n);
-
-
-  /* get the length and its complement */
-  NEEDBITS(16)
-  n = ((unsigned)b & 0xffff);
-  DUMPBITS(16)
-  NEEDBITS(16)
-  if (n != (unsigned)((~b) & 0xffff))
-    return 1;                   /* error in compressed data */
-  DUMPBITS(16)
-
-
-  /* read and output the compressed data */
-  while (n--)
-  {
-    NEEDBITS(8)
-    slide[w++] = (uch)b;
-    if (w == WSIZE)
-    {
-      flush_output(w);
-      w = 0;
-    }
-    DUMPBITS(8)
-  }
-
-
-  /* restore the globals from the locals */
-  wp = w;                       /* restore global window pointer */
-  bb = b;                       /* restore global bit buffer */
-  bk = k;
+	unsigned n;		/* number of bytes in block */
+	unsigned w;		/* current window position */
+	register ulg b;		/* bit buffer */
+	register unsigned k;	/* number of bits in bit buffer */
+
+	DEBG("<stor");
+
+	/* make local copies of globals */
+	b = bb;			/* initialize bit buffer */
+	k = bk;
+	w = wp;			/* initialize window position */
+
+	/* go to byte boundary */
+	n = k & 7;
+	DUMPBITS(n);
+
+	/* get the length and its complement */
+	NEEDBITS(16);
+	n = ((unsigned)b & 0xffff);
+	DUMPBITS(16);
+	NEEDBITS(16);
+	if (n != (unsigned)((~b) & 0xffff))
+		return 1;	/* error in compressed data */
+	DUMPBITS(16);
+
+	/* read and output the compressed data */
+	while (n--) {
+		NEEDBITS(8);
+		slide[w++] = (uch)b;
+		if (w == WSIZE) {
+			flush_output(w);
+			w = 0;
+		}
+		DUMPBITS(8);
+	}
+
+	/* restore the globals from the locals */
+	wp = w;			/* restore global window pointer */
+	bb = b;			/* restore global bit buffer */
+	bk = k;
 
-  DEBG(">");
-  return 0;
+	DEBG(">");
+	return 0;
 
- underrun:
-  return 4;			/* Input underrun */
+      underrun:
+	return 4;		/* Input underrun */
 }
 
 
-/*
+/* inflate_fixed - decompress a block with fixed Huffman codes
+ *
+ * decompress an inflated type 1 (fixed Huffman codes) block. We
+ * should either replace this with a custom decoder, or at least
+ * precompute the Huffman tables.
+ *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
 STATIC int noinline INIT inflate_fixed(void)
-/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
-   either replace this with a custom decoder, or at least precompute the
-   Huffman tables. */
 {
-  int i;                /* temporary variable */
-  struct huft *tl;      /* literal/length code table */
-  struct huft *td;      /* distance code table */
-  int bl;               /* lookup bits for tl */
-  int bd;               /* lookup bits for td */
-  unsigned l[288];      /* length list for huft_build */
-
-DEBG("<fix");
-
-  /* set up literal table */
-  for (i = 0; i < 144; i++)
-    l[i] = 8;
-  for (; i < 256; i++)
-    l[i] = 9;
-  for (; i < 280; i++)
-    l[i] = 7;
-  for (; i < 288; i++)          /* make a complete, but wrong code set */
-    l[i] = 8;
-  bl = 7;
-  if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
-    return i;
-
-
-  /* set up distance table */
-  for (i = 0; i < 30; i++)      /* make an incomplete code set */
-    l[i] = 5;
-  bd = 5;
-  if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
-  {
-    huft_free(tl);
-
-    DEBG(">");
-    return i;
-  }
-
-
-  /* decompress until an end-of-block code */
-  if (inflate_codes(tl, td, bl, bd))
-    return 1;
-
-
-  /* free the decoding tables, return */
-  huft_free(tl);
-  huft_free(td);
-  return 0;
+	int i;			/* temporary variable */
+	struct huft *tl;	/* literal/length code table */
+	struct huft *td;	/* distance code table */
+	int bl;			/* lookup bits for tl */
+	int bd;			/* lookup bits for td */
+	unsigned l[288];	/* length list for huft_build */
+
+	DEBG("<fix");
+
+	/* set up literal table */
+	for (i = 0; i < 144; i++)
+		l[i] = 8;
+	for (; i < 256; i++)
+		l[i] = 9;
+	for (; i < 280; i++)
+		l[i] = 7;
+	for (; i < 288; i++)	/* make a complete, but wrong code set */
+		l[i] = 8;
+	bl = 7;
+	if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
+		return i;
+
+	/* set up distance table */
+	for (i = 0; i < 30; i++)	/* make an incomplete code set */
+		l[i] = 5;
+	bd = 5;
+	if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
+		huft_free(tl);
+
+		DEBG(">");
+		return i;
+	}
+
+	/* decompress until an end-of-block code */
+	if (inflate_codes(tl, td, bl, bd))
+		return 1;
+
+	/* free the decoding tables, return */
+	huft_free(tl);
+	huft_free(td);
+	return 0;
 }
 
 
-/*
+/* inflate_dynamic - decompress a type 2 (dynamic Huffman codes) block.
+ *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
 STATIC int noinline INIT inflate_dynamic(void)
-/* decompress an inflated type 2 (dynamic Huffman codes) block. */
 {
-  int i;                /* temporary variables */
-  unsigned j;
-  unsigned l;           /* last length */
-  unsigned m;           /* mask for bit lengths table */
-  unsigned n;           /* number of lengths to get */
-  struct huft *tl;      /* literal/length code table */
-  struct huft *td;      /* distance code table */
-  int bl;               /* lookup bits for tl */
-  int bd;               /* lookup bits for td */
-  unsigned nb;          /* number of bit length codes */
-  unsigned nl;          /* number of literal/length codes */
-  unsigned nd;          /* number of distance codes */
+	int i;			/* temporary variables */
+	unsigned j;
+	unsigned l;		/* last length */
+	unsigned m;		/* mask for bit lengths table */
+	unsigned n;		/* number of lengths to get */
+	struct huft *tl;	/* literal/length code table */
+	struct huft *td;	/* distance code table */
+	int bl;			/* lookup bits for tl */
+	int bd;			/* lookup bits for td */
+	unsigned nb;		/* number of bit length codes */
+	unsigned nl;		/* number of literal/length codes */
+	unsigned nd;		/* number of distance codes */
 #ifdef PKZIP_BUG_WORKAROUND
-  unsigned ll[288+32];  /* literal/length and distance code lengths */
+	unsigned ll[288 + 32];	/* literal/length and distance code lengths */
 #else
-  unsigned ll[286+30];  /* literal/length and distance code lengths */
+	unsigned ll[286 + 30];	/* literal/length and distance code lengths */
 #endif
-  register ulg b;       /* bit buffer */
-  register unsigned k;  /* number of bits in bit buffer */
+	register ulg b;		/* bit buffer */
+	register unsigned k;	/* number of bits in bit buffer */
 
-DEBG("<dyn");
+	DEBG("<dyn");
 
-  /* make local bit buffer */
-  b = bb;
-  k = bk;
-
-
-  /* read in table lengths */
-  NEEDBITS(5)
-  nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
-  DUMPBITS(5)
-  NEEDBITS(5)
-  nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
-  DUMPBITS(5)
-  NEEDBITS(4)
-  nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
-  DUMPBITS(4)
+	/* make local bit buffer */
+	b = bb;
+	k = bk;
+
+	/* read in table lengths */
+	NEEDBITS(5);
+	nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
+	DUMPBITS(5);
+	NEEDBITS(5);
+	nd = 1 + ((unsigned)b & 0x1f);	/* number of distance codes */
+	DUMPBITS(5);
+	NEEDBITS(4);
+	nb = 4 + ((unsigned)b & 0xf);	/* number of bit length codes */
+	DUMPBITS(4);
 #ifdef PKZIP_BUG_WORKAROUND
-  if (nl > 288 || nd > 32)
+	if (nl > 288 || nd > 32)
 #else
-  if (nl > 286 || nd > 30)
+	if (nl > 286 || nd > 30)
 #endif
-    return 1;                   /* bad lengths */
+		return 1;	/* bad lengths */
 
-DEBG("dyn1 ");
+	DEBG("dyn1 ");
 
-  /* read in bit-length-code lengths */
-  for (j = 0; j < nb; j++)
-  {
-    NEEDBITS(3)
-    ll[border[j]] = (unsigned)b & 7;
-    DUMPBITS(3)
-  }
-  for (; j < 19; j++)
-    ll[border[j]] = 0;
-
-DEBG("dyn2 ");
-
-  /* build decoding table for trees--single level, 7 bit lookup */
-  bl = 7;
-  if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
-  {
-    if (i == 1)
-      huft_free(tl);
-    return i;                   /* incomplete code set */
-  }
-
-DEBG("dyn3 ");
-
-  /* read in literal and distance code lengths */
-  n = nl + nd;
-  m = mask_bits[bl];
-  i = l = 0;
-  while ((unsigned)i < n)
-  {
-    NEEDBITS((unsigned)bl)
-    j = (td = tl + ((unsigned)b & m))->b;
-    DUMPBITS(j)
-    j = td->v.n;
-    if (j < 16)                 /* length of code in bits (0..15) */
-      ll[i++] = l = j;          /* save last length in l */
-    else if (j == 16)           /* repeat last length 3 to 6 times */
-    {
-      NEEDBITS(2)
-      j = 3 + ((unsigned)b & 3);
-      DUMPBITS(2)
-      if ((unsigned)i + j > n)
-        return 1;
-      while (j--)
-        ll[i++] = l;
-    }
-    else if (j == 17)           /* 3 to 10 zero length codes */
-    {
-      NEEDBITS(3)
-      j = 3 + ((unsigned)b & 7);
-      DUMPBITS(3)
-      if ((unsigned)i + j > n)
-        return 1;
-      while (j--)
-        ll[i++] = 0;
-      l = 0;
-    }
-    else                        /* j == 18: 11 to 138 zero length codes */
-    {
-      NEEDBITS(7)
-      j = 11 + ((unsigned)b & 0x7f);
-      DUMPBITS(7)
-      if ((unsigned)i + j > n)
-        return 1;
-      while (j--)
-        ll[i++] = 0;
-      l = 0;
-    }
-  }
-
-DEBG("dyn4 ");
-
-  /* free decoding table for trees */
-  huft_free(tl);
-
-DEBG("dyn5 ");
-
-  /* restore the global bit buffer */
-  bb = b;
-  bk = k;
-
-DEBG("dyn5a ");
-
-  /* build the decoding tables for literal/length and distance codes */
-  bl = lbits;
-  if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
-  {
-DEBG("dyn5b ");
-    if (i == 1) {
-      error("incomplete literal tree");
-      huft_free(tl);
-    }
-    return i;                   /* incomplete code set */
-  }
-DEBG("dyn5c ");
-  bd = dbits;
-  if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
-  {
-DEBG("dyn5d ");
-    if (i == 1) {
-      error("incomplete distance tree");
+	/* read in bit-length-code lengths */
+	for (j = 0; j < nb; j++) {
+		NEEDBITS(3);
+		ll[border[j]] = (unsigned)b & 7;
+		DUMPBITS(3);
+	}
+	for (; j < 19; j++)
+		ll[border[j]] = 0;
+
+	DEBG("dyn2 ");
+
+	/* build decoding table for trees--single level, 7 bit lookup */
+	bl = 7;
+	if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
+		if (i == 1)
+			huft_free(tl);
+		return i;	/* incomplete code set */
+	}
+
+	DEBG("dyn3 ");
+
+	/* read in literal and distance code lengths */
+	n = nl + nd;
+	m = mask_bits[bl];
+	i = l = 0;
+	while ((unsigned)i < n) {
+		NEEDBITS((unsigned)bl);
+		j = (td = tl + ((unsigned)b & m))->b;
+		DUMPBITS(j);
+		j = td->v.n;
+		if (j < 16)	/* length of code in bits (0..15) */
+			ll[i++] = l = j;	/* save last length in l */
+		else if (j == 16) {	/* repeat last length 3 to 6 times */
+			NEEDBITS(2);
+			j = 3 + ((unsigned)b & 3);
+			DUMPBITS(2);
+			    if ((unsigned)i + j > n)
+				return 1;
+			while (j--)
+				ll[i++] = l;
+		} else if (j == 17) {	/* 3 to 10 zero length codes */
+			NEEDBITS(3);
+			j = 3 + ((unsigned)b & 7);
+			DUMPBITS(3);
+			if ((unsigned)i + j > n)
+				return 1;
+			while (j--)
+				ll[i++] = 0;
+			l = 0;
+		} else {	/* j == 18: 11 to 138 zero length codes */
+			NEEDBITS(7);
+			j = 11 + ((unsigned)b & 0x7f);
+			DUMPBITS(7);
+			if ((unsigned)i + j > n)
+				return 1;
+			while (j--)
+				ll[i++] = 0;
+			l = 0;
+		}
+	}
+
+	DEBG("dyn4 ");
+
+	/* free decoding table for trees */
+	huft_free(tl);
+
+	DEBG("dyn5 ");
+
+	/* restore the global bit buffer */
+	bb = b;
+	bk = k;
+
+	DEBG("dyn5a ");
+
+	/* build the decoding tables for literal/length and distance codes */
+	bl = lbits;
+	if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
+		DEBG("dyn5b ");
+		if (i == 1) {
+			error("incomplete literal tree");
+			huft_free(tl);
+		}
+		return i;	/* incomplete code set */
+	}
+	DEBG("dyn5c ");
+	bd = dbits;
+	if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
+		DEBG("dyn5d ");
+		if (i == 1) {
+			error("incomplete distance tree");
 #ifdef PKZIP_BUG_WORKAROUND
-      i = 0;
-    }
+			i = 0;
+		}
 #else
-      huft_free(td);
-    }
-    huft_free(tl);
-    return i;                   /* incomplete code set */
+			huft_free(td);
+		}
+		huft_free(tl);
+		return i;	/* incomplete code set */
 #endif
-  }
+	}
 
-DEBG("dyn6 ");
+	DEBG("dyn6 ");
 
-  /* decompress until an end-of-block code */
-  if (inflate_codes(tl, td, bl, bd))
-    return 1;
+	/* decompress until an end-of-block code */
+	if (inflate_codes(tl, td, bl, bd))
+		return 1;
 
-DEBG("dyn7 ");
+	DEBG("dyn7 ");
 
-  /* free the decoding tables, return */
-  huft_free(tl);
-  huft_free(td);
+	/* free the decoding tables, return */
+	huft_free(tl);
+	huft_free(td);
 
-  DEBG(">");
-  return 0;
+	DEBG(">");
+	return 0;
 
- underrun:
-  return 4;			/* Input underrun */
+      underrun:
+	return 4;		/* Input underrun */
 }
 
-
-
-STATIC int INIT inflate_block(
-	int *e                  /* last block flag */
-	)
-/* decompress an inflated block */
+/* inflate_block - decompress a deflated block
+ * @e: last block flag
+ */
+STATIC int INIT inflate_block(int *e)
 {
-  unsigned t;           /* block type */
-  register ulg b;       /* bit buffer */
-  register unsigned k;  /* number of bits in bit buffer */
+	unsigned t;		/* block type */
+	register ulg b;		/* bit buffer */
+	register unsigned k;	/* number of bits in bit buffer */
+
+	DEBG("<blk");
+
+	/* make local bit buffer */
+	b = bb;
+	k = bk;
+
+	/* read in last block bit */
+	NEEDBITS(1);
+	*e = (int)b & 1;
+	DUMPBITS(1);
+
+	/* read in block type */
+	NEEDBITS(2);
+	t = (unsigned)b & 3;
+	DUMPBITS(2);
+
+	/* restore the global bit buffer */
+	bb = b;
+	bk = k;
+
+	/* inflate that block type */
+	if (t == 2)
+		return inflate_dynamic();
+	if (t == 0)
+		return inflate_stored();
+	if (t == 1)
+		return inflate_fixed();
 
-  DEBG("<blk");
+	DEBG(">");
 
-  /* make local bit buffer */
-  b = bb;
-  k = bk;
+	/* bad block type */
+	return 2;
 
-
-  /* read in last block bit */
-  NEEDBITS(1)
-  *e = (int)b & 1;
-  DUMPBITS(1)
-
-
-  /* read in block type */
-  NEEDBITS(2)
-  t = (unsigned)b & 3;
-  DUMPBITS(2)
-
-
-  /* restore the global bit buffer */
-  bb = b;
-  bk = k;
-
-  /* inflate that block type */
-  if (t == 2)
-    return inflate_dynamic();
-  if (t == 0)
-    return inflate_stored();
-  if (t == 1)
-    return inflate_fixed();
-
-  DEBG(">");
-
-  /* bad block type */
-  return 2;
-
- underrun:
-  return 4;			/* Input underrun */
+      underrun:
+	return 4;		/* Input underrun */
 }
 
-
-
+/* inflate - decompress an inflated entry */
 STATIC int INIT inflate(void)
-/* decompress an inflated entry */
 {
-  int e;                /* last block flag */
-  int r;                /* result code */
-  unsigned h;           /* maximum struct huft's malloc'ed */
-  void *ptr;
-
-  /* initialize window, bit buffer */
-  wp = 0;
-  bk = 0;
-  bb = 0;
-
-
-  /* decompress until the last block */
-  h = 0;
-  do {
-    hufts = 0;
-    gzip_mark(&ptr);
-    if ((r = inflate_block(&e)) != 0) {
-      gzip_release(&ptr);	    
-      return r;
-    }
-    gzip_release(&ptr);
-    if (hufts > h)
-      h = hufts;
-  } while (!e);
-
-  /* Undo too much lookahead. The next read will be byte aligned so we
-   * can discard unused bits in the last meaningful byte.
-   */
-  while (bk >= 8) {
-    bk -= 8;
-    inptr--;
-  }
+	int e;			/* last block flag */
+	int r;			/* result code */
+	unsigned h;		/* maximum struct huft's malloc'ed */
+	void *ptr;
+
+	/* initialize window, bit buffer */
+	wp = 0;
+	bk = 0;
+	bb = 0;
+
+	/* decompress until the last block */
+	h = 0;
+	do {
+		hufts = 0;
+		gzip_mark(&ptr);
+		if ((r = inflate_block(&e)) != 0) {
+			gzip_release(&ptr);
+			return r;
+		}
+		gzip_release(&ptr);
+		if (hufts > h)
+			h = hufts;
+	} while (!e);
+
+	/* Undo too much lookahead. The next read will be byte aligned so we
+	 * can discard unused bits in the last meaningful byte.
+	 */
+	while (bk >= 8) {
+		bk -= 8;
+		inptr--;
+	}
 
-  /* flush out slide */
-  flush_output(wp);
+	/* flush out slide */
+	flush_output(wp);
 
-
-  /* return success */
+	/* return success */
 #ifdef DEBUG
-  fprintf(stderr, "<%u> ", h);
-#endif /* DEBUG */
-  return 0;
+	fprintf(stderr, "<%u> ", h);
+#endif				/* DEBUG */
+	return 0;
 }
 
 /**********************************************************************
@@ -1041,44 +1038,42 @@ static ulg crc;		/* initialized in makec
 #define CRC_VALUE (crc ^ 0xffffffffUL)
 
 /*
- * Code to compute the CRC-32 table. Borrowed from 
+ * Code to compute the CRC-32 table. Borrowed from
  * gzip-1.0.3/makecrc.c.
+ * Not copyrighted 1990 Mark Adler
  */
 
-static void INIT
-makecrc(void)
+static void INIT makecrc(void)
 {
-/* Not copyrighted 1990 Mark Adler	*/
 
-  unsigned long c;      /* crc shift register */
-  unsigned long e;      /* polynomial exclusive-or pattern */
-  int i;                /* counter for all possible eight bit values */
-  int k;                /* byte being shifted into crc apparatus */
-
-  /* terms of polynomial defining this crc (except x^32): */
-  static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
-
-  /* Make exclusive-or pattern from polynomial */
-  e = 0;
-  for (i = 0; i < sizeof(p)/sizeof(int); i++)
-    e |= 1L << (31 - p[i]);
-
-  crc_32_tab[0] = 0;
-
-  for (i = 1; i < 256; i++)
-  {
-    c = 0;
-    for (k = i | 256; k != 1; k >>= 1)
-    {
-      c = c & 1 ? (c >> 1) ^ e : c >> 1;
-      if (k & 1)
-        c ^= e;
-    }
-    crc_32_tab[i] = c;
-  }
+	unsigned long c;	/* crc shift register */
+	unsigned long e;	/* polynomial exclusive-or pattern */
+	int i;			/* counter for all possible eight bit values */
+	int k;			/* byte being shifted into crc apparatus */
+
+	/* terms of polynomial defining this crc (except x^32): */
+	static const int p[] =
+	    { 0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26 };
+
+	/* Make exclusive-or pattern from polynomial */
+	e = 0;
+	for (i = 0; i < sizeof(p) / sizeof(int); i++)
+		e |= 1L << (31 - p[i]);
+
+	crc_32_tab[0] = 0;
+
+	for (i = 1; i < 256; i++) {
+		c = 0;
+		for (k = i | 256; k != 1; k >>= 1) {
+			c = c & 1 ? (c >> 1) ^ e : c >> 1;
+			if (k & 1)
+				c ^= e;
+		}
+		crc_32_tab[i] = c;
+	}
 
-  /* this is initialized here so this code could reside in ROM */
-  crc = (ulg)0xffffffffUL; /* shift register contents */
+	/* this is initialized here so this code could reside in ROM */
+	crc = (ulg)0xffffffffUL;	/* shift register contents */
 }
 
 /* gzip flag byte */
@@ -1095,118 +1090,118 @@ makecrc(void)
  */
 static int INIT gunzip(void)
 {
-    uch flags;
-    unsigned char magic[2]; /* magic header */
-    char method;
-    ulg orig_crc = 0;       /* original crc */
-    ulg orig_len = 0;       /* original uncompressed length */
-    int res;
-
-    magic[0] = NEXTBYTE();
-    magic[1] = NEXTBYTE();
-    method   = NEXTBYTE();
-
-    if (magic[0] != 037 ||
-	((magic[1] != 0213) && (magic[1] != 0236))) {
-	    error("bad gzip magic numbers");
-	    return -1;
-    }
-
-    /* We only support method #8, DEFLATED */
-    if (method != 8)  {
-	    error("internal error, invalid method");
-	    return -1;
-    }
-
-    flags  = (uch)get_byte();
-    if ((flags & ENCRYPTED) != 0) {
-	    error("Input is encrypted");
-	    return -1;
-    }
-    if ((flags & CONTINUATION) != 0) {
-	    error("Multi part input");
-	    return -1;
-    }
-    if ((flags & RESERVED) != 0) {
-	    error("Input has invalid flags");
-	    return -1;
-    }
-    NEXTBYTE();	/* Get timestamp */
-    NEXTBYTE();
-    NEXTBYTE();
-    NEXTBYTE();
-
-    (void)NEXTBYTE();  /* Ignore extra flags for the moment */
-    (void)NEXTBYTE();  /* Ignore OS type for the moment */
-
-    if ((flags & EXTRA_FIELD) != 0) {
-	    unsigned len = (unsigned)NEXTBYTE();
-	    len |= ((unsigned)NEXTBYTE())<<8;
-	    while (len--) (void)NEXTBYTE();
-    }
-
-    /* Get original file name if it was truncated */
-    if ((flags & ORIG_NAME) != 0) {
-	    /* Discard the old name */
-	    while (NEXTBYTE() != 0) /* null */ ;
-    } 
-
-    /* Discard file comment if any */
-    if ((flags & COMMENT) != 0) {
-	    while (NEXTBYTE() != 0) /* null */ ;
-    }
-
-    /* Decompress */
-    if ((res = inflate())) {
-	    switch (res) {
-	    case 0:
-		    break;
-	    case 1:
-		    error("invalid compressed format (err=1)");
-		    break;
-	    case 2:
-		    error("invalid compressed format (err=2)");
-		    break;
-	    case 3:
-		    error("out of memory");
-		    break;
-	    case 4:
-		    error("out of input data");
-		    break;
-	    default:
-		    error("invalid compressed format (other)");
-	    }
-	    return -1;
-    }
-	    
-    /* Get the crc and original length */
-    /* crc32  (see algorithm.doc)
-     * uncompressed input size modulo 2^32
-     */
-    orig_crc = (ulg) NEXTBYTE();
-    orig_crc |= (ulg) NEXTBYTE() << 8;
-    orig_crc |= (ulg) NEXTBYTE() << 16;
-    orig_crc |= (ulg) NEXTBYTE() << 24;
-    
-    orig_len = (ulg) NEXTBYTE();
-    orig_len |= (ulg) NEXTBYTE() << 8;
-    orig_len |= (ulg) NEXTBYTE() << 16;
-    orig_len |= (ulg) NEXTBYTE() << 24;
-    
-    /* Validate decompression */
-    if (orig_crc != CRC_VALUE) {
-	    error("crc error");
-	    return -1;
-    }
-    if (orig_len != bytes_out) {
-	    error("length error");
-	    return -1;
-    }
-    return 0;
-
- underrun:			/* NEXTBYTE() goto's here if needed */
-    error("out of input data");
-    return -1;
+	uch flags;
+	unsigned char magic[2];	/* magic header */
+	char method;
+	ulg orig_crc = 0;	/* original crc */
+	ulg orig_len = 0;	/* original uncompressed length */
+	int res;
+
+	magic[0] = NEXTBYTE();
+	magic[1] = NEXTBYTE();
+	method = NEXTBYTE();
+
+	if (magic[0] != 037 || ((magic[1] != 0213) && (magic[1] != 0236))) {
+		error("bad gzip magic numbers");
+		return -1;
+	}
+
+	/* We only support method #8, DEFLATED */
+	if (method != 8) {
+		error("internal error, invalid method");
+		return -1;
+	}
+
+	flags = (uch)get_byte();
+	if ((flags & ENCRYPTED) != 0) {
+		error("Input is encrypted");
+		return -1;
+	}
+	if ((flags & CONTINUATION) != 0) {
+		error("Multi part input");
+		return -1;
+	}
+	if ((flags & RESERVED) != 0) {
+		error("Input has invalid flags");
+		return -1;
+	}
+	NEXTBYTE();		/* Get timestamp */
+	NEXTBYTE();
+	NEXTBYTE();
+	NEXTBYTE();
+
+	(void)NEXTBYTE();	/* Ignore extra flags for the moment */
+	(void)NEXTBYTE();	/* Ignore OS type for the moment */
+
+	if ((flags & EXTRA_FIELD) != 0) {
+		unsigned len = (unsigned)NEXTBYTE();
+		len |= ((unsigned)NEXTBYTE()) << 8;
+		while (len--)
+			(void)NEXTBYTE();
+	}
+
+	/* Get original file name if it was truncated */
+	if ((flags & ORIG_NAME) != 0) {
+		/* Discard the old name */
+		while (NEXTBYTE() != 0)	/* null */
+			;
+	}
+
+	/* Discard file comment if any */
+	if ((flags & COMMENT) != 0) {
+		while (NEXTBYTE() != 0)	/* null */
+			;
+	}
+
+	/* Decompress */
+	if ((res = inflate())) {
+		switch (res) {
+		case 0:
+			break;
+		case 1:
+			error("invalid compressed format (err=1)");
+			break;
+		case 2:
+			error("invalid compressed format (err=2)");
+			break;
+		case 3:
+			error("out of memory");
+			break;
+		case 4:
+			error("out of input data");
+			break;
+		default:
+			error("invalid compressed format (other)");
+		}
+		return -1;
+	}
+
+	/* Get the crc and original length */
+	/* crc32  (see algorithm.doc)
+	 * uncompressed input size modulo 2^32
+	 */
+	orig_crc = (ulg)NEXTBYTE();
+	orig_crc |= (ulg)NEXTBYTE() << 8;
+	orig_crc |= (ulg)NEXTBYTE() << 16;
+	orig_crc |= (ulg)NEXTBYTE() << 24;
+
+	orig_len = (ulg)NEXTBYTE();
+	orig_len |= (ulg)NEXTBYTE() << 8;
+	orig_len |= (ulg)NEXTBYTE() << 16;
+	orig_len |= (ulg)NEXTBYTE() << 24;
+
+	/* Validate decompression */
+	if (orig_crc != CRC_VALUE) {
+		error("crc error");
+		return -1;
+	}
+	if (orig_len != bytes_out) {
+		error("length error");
+		return -1;
+	}
+	return 0;
+
+      underrun:		/* NEXTBYTE() goto's here if needed */
+	error("out of input data");
+	return -1;
 }
-
-

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 2/7] inflate pt1: kill legacy bits
       [not found] <0.399206195@selenic.com>
                   ` (2 preceding siblings ...)
  2006-02-24 20:12 ` [PATCH 4/7] inflate pt1: start moving globals into iostate Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 20:12 ` [PATCH 6/7] inflate pt1: internalize CRC calculation, cleanup table calculation Matt Mackall
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: kill legacy bits

Kill RCSID
Kill old includes and defines
Kill screwy types
Kill unused memory usage tracking
Kill 'register' usage
Kill unused tracing calls

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:00.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:03.000000000 -0600
@@ -103,26 +103,11 @@
  */
 #include <linux/compiler.h>
 
-#ifdef RCSID
-static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
-#endif
-
-#ifndef STATIC
-
-#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
-#  include <sys/types.h>
-#  include <stdlib.h>
-#endif
-
-#include "gzip.h"
-#define STATIC
-#endif /* !STATIC */
-
 #ifndef INIT
 #define INIT
 #endif
 
-#define slide window
+#include <asm/types.h>
 
 /* Huffman code lookup table entry--this entry is four bytes for machines
    that have 16-bit pointers (e.g. PC's in the small or medium model).
@@ -132,36 +117,35 @@ static char rcsid[] = "#Id: inflate.c,v 
    an unused code.  If a code with e == 99 is looked up, this implies an
    error in the data. */
 struct huft {
-	uch e;			/* number of extra bits or operation */
-	uch b;			/* number of bits in this code or subcode */
+	u8 e;			/* number of extra bits or operation */
+	u8 b;			/* number of bits in this code or subcode */
 	union {
-		ush n;		/* literal, length base, or distance base */
+		u16 n;		/* literal, length base, or distance base */
 		struct huft *t;	/* pointer to next level of table */
 	} v;
 };
 
 /* Function prototypes */
-STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
-		const ush *, const ush *, struct huft **, int *));
-STATIC int INIT huft_free OF((struct huft *));
-STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
-STATIC int INIT inflate_stored OF((void));
-STATIC int INIT inflate_fixed OF((void));
-STATIC int INIT inflate_dynamic OF((void));
-STATIC int INIT inflate_block OF((int *));
-STATIC int INIT inflate OF((void));
+static int INIT huft_build(unsigned *, unsigned, unsigned,
+			  const u16 *, const u16 *, struct huft **, int *);
+static int INIT huft_free(struct huft *);
+static int INIT inflate_codes(struct huft *, struct huft *, int, int);
+static int INIT inflate_stored(void);
+static int INIT inflate_fixed(void);
+static int INIT inflate_dynamic(void);
+static int INIT inflate_block(int *);
+static int INIT inflate(void);
 
 /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
    stream to find repeated byte strings.  This is implemented here as a
    circular buffer.  The index is updated simply by incrementing and then
    ANDing with 0x7fff (32K-1). */
 /* It is left to other modules to supply the 32 K area.  It is assumed
-   to be usable as if it were declared "uch slide[32768];" or as just
-   "uch *slide;" and then malloc'ed in the latter case.  The definition
+   to be usable as if it were declared "u8 window[32768];" or as just
+   "u8 *window;" and then malloc'ed in the latter case.  The definition
    must be in unzip.h, included above. */
-/* unsigned wp;             current position in slide */
-#define wp outcnt
-#define flush_output(w) (wp=(w),flush_window())
+
+#define flush_output(w) (outcnt=(w),flush_window())
 
 /* Tables for deflate from PKZIP's appnote.txt. */
 
@@ -171,7 +155,7 @@ static const unsigned border[] = {
 };
 
 /* Copy lengths for literal codes 257..285 */
-static const ush cplens[] = {
+static const u16 cplens[] = {
 	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, 0, 0
 };
@@ -180,20 +164,20 @@ static const ush cplens[] = {
  * note: see note #13 above about the 258 in this list.
  * 99==invalid
  */
-static const ush cplext[] = {
+static const u16 cplext[] = {
 	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, 99, 99
 };
 
 /* Copy offsets for distance codes 0..29 */
-static const ush cpdist[] = {
+static const u16 cpdist[] = {
 	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
 };
 
 /* Extra bits for distance codes */
-static const ush cpdext[] = {
+static const u16 cpdext[] = {
 	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
@@ -207,10 +191,10 @@ static const ush cpdext[] = {
         DUMPBITS(j)
 
    where NEEDBITS makes sure that b has at least j bits in it, and
-   DUMPBITS removes the bits from b.  The macros use the variable k
-   for the number of bits in b.  Normally, b and k are register
-   variables for speed, and are initialized at the beginning of a
-   routine that uses these macros from a global bit buffer and count.
+   DUMPBITS removes the bits from b. The macros use the variable k for
+   the number of bits in b. Normally, b and k are initialized at the
+   beginning of a routine that uses these macros from a global bit
+   buffer and count.
 
    If we assume that EOB will be the longest code, then we will never
    ask for bits with NEEDBITS that are beyond the end of the stream.
@@ -229,17 +213,17 @@ static const ush cpdext[] = {
    the stream.
  */
 
-STATIC ulg bb;			/* bit buffer */
-STATIC unsigned bk;		/* bits in bit buffer */
+static u32 bb;			/* bit buffer */
+static unsigned bk;		/* bits in bit buffer */
 
-STATIC const ush mask_bits[] = {
+static const u16 mask_bits[] = {
 	0x0000,
 	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
 	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
 };
 
-#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
-#define NEEDBITS(n) do {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}} while(0)
+#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (u8)v; })
+#define NEEDBITS(n) do {while(k<(n)){b|=((u32)NEXTBYTE())<<k;k+=8;}} while(0)
 #define DUMPBITS(n) do {b>>=(n);k-=(n);} while(0)
 
 /*
@@ -274,15 +258,13 @@ STATIC const ush mask_bits[] = {
    possibly even between compilers.  Your mileage may vary.
  */
 
-STATIC const int lbits = 9;	/* bits in base literal/length lookup table */
-STATIC const int dbits = 6;	/* bits in base distance lookup table */
+static const int lbits = 9;	/* bits in base literal/length lookup table */
+static const int dbits = 6;	/* bits in base distance lookup table */
 
-/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
+/* If BMAX needs to be larger than 16, then h and x[] should be u32. */
 #define BMAX 16		/* maximum bit length of any code (16 for explode) */
 #define N_MAX 288	/* maximum number of codes in any set */
 
-STATIC unsigned hufts;		/* track memory usage */
-
 /*
  * huft-build - build a huffman decoding table
  * @b: code lengths in bits (all assumed <= BMAX)
@@ -300,24 +282,24 @@ STATIC unsigned hufts;		/* track memory 
  * an oversubscribed set of lengths), and three if not enough
  * memory.
  */
-STATIC int INIT huft_build(unsigned *b, unsigned n, unsigned s, const ush * d,
-		      const ush * e, struct huft **t, int *m)
+static int INIT huft_build(unsigned *b, unsigned n, unsigned s, const u16 * d,
+		      const u16 * e, struct huft **t, int *m)
 {
 	unsigned a;		/* counter for codes of length k */
 	unsigned c[BMAX + 1];	/* bit length count table */
 	unsigned f;		/* i repeats in table every f entries */
 	int g;			/* maximum code length */
 	int h;			/* table level */
-	register unsigned i;	/* counter, current code */
-	register unsigned j;	/* counter */
-	register int k;		/* number of bits in current code */
+	unsigned i;	/* counter, current code */
+	unsigned j;	/* counter */
+	int k;		/* number of bits in current code */
 	int l;			/* bits per table (returned in m) */
-	register unsigned *p;	/* pointer into c[], b[], or v[] */
-	register struct huft *q;	/* points to current table */
+	unsigned *p;	/* pointer into c[], b[], or v[] */
+	struct huft *q;	/* points to current table */
 	struct huft r;		/* table entry for structure assignment */
 	struct huft *u[BMAX];	/* table stack */
 	unsigned v[N_MAX];	/* values in order of bit length */
-	register int w;		/* bits before this table == (l * h) */
+	int w;		/* bits before this table == (l * h) */
 	unsigned x[BMAX + 1];	/* bit offsets, then code stack */
 	unsigned *xp;		/* pointer into x */
 	int y;			/* number of dummy codes added */
@@ -330,14 +312,12 @@ STATIC int INIT huft_build(unsigned *b, 
 	p = b;
 	i = n;
 	do {
-		Tracecv(*p, (stderr, (n - i >= ' ' && n - i <= '~' ?
-				      "%c %d\n" : "0x%x %d\n"), n - i, *p));
 		c[*p]++;	/* assume all entries <= BMAX */
 		p++;
 	} while (--i);
 
 	if (c[0] == n) {	/* null input--all zero length codes */
-		*t = (struct huft *)NULL;
+		*t = 0;
 		*m = 0;
 		return 2;
 	}
@@ -389,7 +369,7 @@ STATIC int INIT huft_build(unsigned *b, 
 	p = b;
 	i = 0;
 	do {
-		if ((j = *p++) != 0)
+		if ((j = *p++))
 			v[x[j]++] = i;
 	} while (++i < n);
 
@@ -402,8 +382,8 @@ STATIC int INIT huft_build(unsigned *b, 
 	p = v; /* grab values in bit order */
 	h = -1; /* no tables yet--level -1 */
 	w = -l;	/* bits decoded == (l * h) */
-	u[0] = (struct huft *)NULL; /* just to keep compilers happy */
-	q = (struct huft *)NULL; /* ditto */
+	u[0] = NULL; /* just to keep compilers happy */
+	q = NULL; /* ditto */
 	z = 0; /* ditto */
 	DEBG("h6a ");
 
@@ -448,17 +428,15 @@ STATIC int INIT huft_build(unsigned *b, 
 				z = 1 << j;
 
 				/* allocate and link in new table */
-				if ((q = (struct huft *)malloc(
-					     (z + 1) * sizeof(struct huft)))
-				    == (struct huft *)NULL) {
+				if (!(q = (struct huft *)malloc(
+					     (z + 1) * sizeof(struct huft)))) {
 					if (h)
 						huft_free(u[0]);
 					return 3;	/* not enough memory */
 				}
 				DEBG1("4 ");
-				hufts += z + 1;	/* track memory usage */
 				*t = q + 1; /* link to list for huft_free */
-				*(t = &(q->v.t)) = (struct huft *)NULL;
+				*(t = &(q->v.t)) = 0;
 				u[h] = ++q;	/* table starts after link */
 
 				DEBG1("5 ");
@@ -467,9 +445,9 @@ STATIC int INIT huft_build(unsigned *b, 
 					/* save pattern for backing up */
 					x[h] = i;
 					/* bits to dump before this table */
-					r.b = (uch)l;
+					r.b = (u8)l;
 					/* bits in this table */
-					r.e = (uch)(16 + j);
+					r.e = (u8)(16 + j);
 					/* pointer to this table */
 					r.v.t = q;
 					/* (get around Turbo C bug) */
@@ -482,19 +460,19 @@ STATIC int INIT huft_build(unsigned *b, 
 			DEBG("h6c ");
 
 			/* set up table entry in r */
-			r.b = (uch) (k - w);
+			r.b = (u8) (k - w);
 			if (p >= v + n)
 				r.e = 99; /* out of values--invalid code */
 			else if (*p < s) {
 				/* 256 is end-of-block code */
-				r.e = (uch) (*p < 256 ? 16 : 15);
+				r.e = (u8)(*p < 256 ? 16 : 15);
 				/* simple code is just the value */
-				r.v.n = (ush) (*p);
+				r.v.n = (u16)(*p);
 				/* one compiler does not like *p++ */
 				p++;
 			} else {
 				/* non-simple--look up in lists */
-				r.e = (uch)e[*p - s];
+				r.e = (u8)e[*p - s];
 				r.v.n = d[*p++ - s];
 			}
 			DEBG("h6d ");
@@ -522,7 +500,7 @@ STATIC int INIT huft_build(unsigned *b, 
 	DEBG("huft7 ");
 
 	/* Return true (1) if we were given an incomplete table */
-	return y != 0 && g != 1;
+	return y && g != 1;
 }
 
 /*
@@ -535,11 +513,11 @@ STATIC int INIT huft_build(unsigned *b, 
  */
 STATIC int INIT huft_free(struct huft *t)
 {
-	register struct huft *p, *q;
+	struct huft *p, *q;
 
 	/* Go through list, freeing from the malloced (t[-1]) address. */
 	p = t;
-	while (p != (struct huft *)NULL) {
+	while (p) {
 		q = (--p)->v.t;
 		free((char *)p);
 		p = q;
@@ -557,20 +535,20 @@ STATIC int INIT huft_free(struct huft *t
  * inflate (decompress) the codes in a deflated (compressed) block.
  * Return an error code or zero if it all goes ok.
  */
-STATIC int inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
+static int inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
 {
-	register unsigned e;	/* table entry flag/number of extra bits */
+	unsigned e;	/* table entry flag/number of extra bits */
 	unsigned n, d;		/* length and index for copy */
 	unsigned w;		/* current window position */
 	struct huft *t;		/* pointer to table entry */
 	unsigned ml, md;	/* masks for bl and bd bits */
-	register ulg b;		/* bit buffer */
-	register unsigned k;	/* number of bits in bit buffer */
+	u32 b;		/* bit buffer */
+	unsigned k;	/* number of bits in bit buffer */
 
 	/* make local copies of globals */
 	b = bb;			/* initialize bit buffer */
 	k = bk;
-	w = wp;			/* initialize window position */
+	w = outcnt;			/* initialize window position */
 
 	/* inflate the coded data */
 	ml = mask_bits[bl];	/* precompute masks for speed */
@@ -588,8 +566,7 @@ STATIC int inflate_codes(struct huft *tl
 						     mask_bits[e]))->e) > 16);
 		DUMPBITS(t->b);
 		if (e == 16) {	/* then it's a literal */
-			slide[w++] = (uch)t->v.n;
-			Tracevv((stderr, "%c", slide[w - 1]));
+			window[w++] = (u8)t->v.n;
 			if (w == WSIZE) {
 				flush_output(w);
 				w = 0;
@@ -618,8 +595,7 @@ STATIC int inflate_codes(struct huft *tl
 			DUMPBITS(t->b);
 			NEEDBITS(e);
 			d = w - t->v.n - ((unsigned)b & mask_bits[e]);
-			DUMPBITS(e)
-			    Tracevv((stderr, "\\[%d,%d]", w - d, n));
+			DUMPBITS(e);
 
 			/* do the copy */
 			do {
@@ -628,16 +604,14 @@ STATIC int inflate_codes(struct huft *tl
 #if !defined(NOMEMCPY) && !defined(DEBUG)
 				/* (this test assumes unsigned comparison) */
 				if (w - d >= e) {
-					memcpy(slide + w, slide + d, e);
+					memcpy(window + w, window + d, e);
 					w += e;
 					d += e;
 				} else
 #endif				/* !NOMEMCPY */
 					/* avoid memcpy() overlap */
 					do {
-						slide[w++] = slide[d++];
-						Tracevv((stderr, "%c",
-							 slide[w - 1]));
+						window[w++] = window[d++];
 					} while (--e);
 				if (w == WSIZE) {
 					flush_output(w);
@@ -648,7 +622,7 @@ STATIC int inflate_codes(struct huft *tl
 	}
 
 	/* restore the globals from the locals */
-	wp = w;			/* restore global window pointer */
+	outcnt = w;			/* restore global window pointer */
 	bb = b;			/* restore global bit buffer */
 	bk = k;
 
@@ -660,19 +634,19 @@ STATIC int inflate_codes(struct huft *tl
 }
 
 /* inflate_stored - "decompress" an inflated type 0 (stored) block. */
-STATIC int INIT inflate_stored(void)
+static int INIT inflate_stored(void)
 {
 	unsigned n;		/* number of bytes in block */
 	unsigned w;		/* current window position */
-	register ulg b;		/* bit buffer */
-	register unsigned k;	/* number of bits in bit buffer */
+	u32 b;		/* bit buffer */
+	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<stor");
 
 	/* make local copies of globals */
 	b = bb;			/* initialize bit buffer */
 	k = bk;
-	w = wp;			/* initialize window position */
+	w = outcnt;			/* initialize window position */
 
 	/* go to byte boundary */
 	n = k & 7;
@@ -690,7 +664,7 @@ STATIC int INIT inflate_stored(void)
 	/* read and output the compressed data */
 	while (n--) {
 		NEEDBITS(8);
-		slide[w++] = (uch)b;
+		window[w++] = (u8)b;
 		if (w == WSIZE) {
 			flush_output(w);
 			w = 0;
@@ -699,7 +673,7 @@ STATIC int INIT inflate_stored(void)
 	}
 
 	/* restore the globals from the locals */
-	wp = w;			/* restore global window pointer */
+	outcnt = w;			/* restore global window pointer */
 	bb = b;			/* restore global bit buffer */
 	bk = k;
 
@@ -719,14 +693,14 @@ STATIC int INIT inflate_stored(void)
  *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
-STATIC int noinline INIT inflate_fixed(void)
+static int noinline INIT inflate_fixed(void)
 {
 	int i;			/* temporary variable */
 	struct huft *tl;	/* literal/length code table */
 	struct huft *td;	/* distance code table */
 	int bl;			/* lookup bits for tl */
 	int bd;			/* lookup bits for td */
-	unsigned l[288];	/* length list for huft_build */
+	unsigned l[N_MAX];	/* length list for huft_build */
 
 	DEBG("<fix");
 
@@ -737,10 +711,10 @@ STATIC int noinline INIT inflate_fixed(v
 		l[i] = 9;
 	for (; i < 280; i++)
 		l[i] = 7;
-	for (; i < 288; i++)	/* make a complete, but wrong code set */
+	for (; i < N_MAX; i++)	/* make a complete, but wrong code set */
 		l[i] = 8;
 	bl = 7;
-	if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
+	if ((i = huft_build(l, N_MAX, 257, cplens, cplext, &tl, &bl)))
 		return i;
 
 	/* set up distance table */
@@ -769,7 +743,7 @@ STATIC int noinline INIT inflate_fixed(v
  *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
-STATIC int noinline INIT inflate_dynamic(void)
+static int noinline INIT inflate_dynamic(void)
 {
 	int i;			/* temporary variables */
 	unsigned j;
@@ -783,13 +757,9 @@ STATIC int noinline INIT inflate_dynamic
 	unsigned nb;		/* number of bit length codes */
 	unsigned nl;		/* number of literal/length codes */
 	unsigned nd;		/* number of distance codes */
-#ifdef PKZIP_BUG_WORKAROUND
-	unsigned ll[288 + 32];	/* literal/length and distance code lengths */
-#else
 	unsigned ll[286 + 30];	/* literal/length and distance code lengths */
-#endif
-	register ulg b;		/* bit buffer */
-	register unsigned k;	/* number of bits in bit buffer */
+	u32 b;		/* bit buffer */
+	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<dyn");
 
@@ -807,11 +777,7 @@ STATIC int noinline INIT inflate_dynamic
 	NEEDBITS(4);
 	nb = 4 + ((unsigned)b & 0xf);	/* number of bit length codes */
 	DUMPBITS(4);
-#ifdef PKZIP_BUG_WORKAROUND
-	if (nl > 288 || nd > 32)
-#else
 	if (nl > 286 || nd > 30)
-#endif
 		return 1;	/* bad lengths */
 
 	DEBG("dyn1 ");
@@ -829,7 +795,7 @@ STATIC int noinline INIT inflate_dynamic
 
 	/* build decoding table for trees--single level, 7 bit lookup */
 	bl = 7;
-	if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
+	if ((i = huft_build(ll, 19, 19, 0, 0, &tl, &bl))) {
 		if (i == 1)
 			huft_free(tl);
 		return i;	/* incomplete code set */
@@ -892,7 +858,7 @@ STATIC int noinline INIT inflate_dynamic
 
 	/* build the decoding tables for literal/length and distance codes */
 	bl = lbits;
-	if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
+	if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl))) {
 		DEBG("dyn5b ");
 		if (i == 1) {
 			error("incomplete literal tree");
@@ -902,19 +868,14 @@ STATIC int noinline INIT inflate_dynamic
 	}
 	DEBG("dyn5c ");
 	bd = dbits;
-	if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
+	if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd))) {
 		DEBG("dyn5d ");
 		if (i == 1) {
 			error("incomplete distance tree");
-#ifdef PKZIP_BUG_WORKAROUND
-			i = 0;
-		}
-#else
 			huft_free(td);
 		}
 		huft_free(tl);
 		return i;	/* incomplete code set */
-#endif
 	}
 
 	DEBG("dyn6 ");
@@ -939,11 +900,11 @@ STATIC int noinline INIT inflate_dynamic
 /* inflate_block - decompress a deflated block
  * @e: last block flag
  */
-STATIC int INIT inflate_block(int *e)
+static int INIT inflate_block(int *e)
 {
 	unsigned t;		/* block type */
-	register ulg b;		/* bit buffer */
-	register unsigned k;	/* number of bits in bit buffer */
+	u32 b;		/* bit buffer */
+	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<blk");
 
@@ -983,30 +944,25 @@ STATIC int INIT inflate_block(int *e)
 }
 
 /* inflate - decompress an inflated entry */
-STATIC int INIT inflate(void)
+static int INIT inflate(void)
 {
 	int e;			/* last block flag */
 	int r;			/* result code */
-	unsigned h;		/* maximum struct huft's malloc'ed */
 	void *ptr;
 
 	/* initialize window, bit buffer */
-	wp = 0;
+	outcnt = 0;
 	bk = 0;
 	bb = 0;
 
 	/* decompress until the last block */
-	h = 0;
 	do {
-		hufts = 0;
 		gzip_mark(&ptr);
-		if ((r = inflate_block(&e)) != 0) {
+		if ((r = inflate_block(&e))) {
 			gzip_release(&ptr);
 			return r;
 		}
 		gzip_release(&ptr);
-		if (hufts > h)
-			h = hufts;
 	} while (!e);
 
 	/* Undo too much lookahead. The next read will be byte aligned so we
@@ -1017,13 +973,10 @@ STATIC int INIT inflate(void)
 		inptr--;
 	}
 
-	/* flush out slide */
-	flush_output(wp);
+	/* flush out window */
+	flush_output(outcnt);
 
 	/* return success */
-#ifdef DEBUG
-	fprintf(stderr, "<%u> ", h);
-#endif				/* DEBUG */
 	return 0;
 }
 
@@ -1033,8 +986,8 @@ STATIC int INIT inflate(void)
  *
  **********************************************************************/
 
-static ulg crc_32_tab[256];
-static ulg crc;		/* initialized in makecrc() so it'll reside in bss */
+static u32 crc_32_tab[256];
+static u32 crc;		/* initialized in makecrc() so it'll reside in bss */
 #define CRC_VALUE (crc ^ 0xffffffffUL)
 
 /*
@@ -1073,7 +1026,7 @@ static void INIT makecrc(void)
 	}
 
 	/* this is initialized here so this code could reside in ROM */
-	crc = (ulg)0xffffffffUL;	/* shift register contents */
+	crc = 0xffffffffUL;	/* shift register contents */
 }
 
 /* gzip flag byte */
@@ -1090,11 +1043,11 @@ static void INIT makecrc(void)
  */
 static int INIT gunzip(void)
 {
-	uch flags;
+	u8 flags;
 	unsigned char magic[2];	/* magic header */
 	char method;
-	ulg orig_crc = 0;	/* original crc */
-	ulg orig_len = 0;	/* original uncompressed length */
+	u32 orig_crc = 0;	/* original crc */
+	u32 orig_len = 0;	/* original uncompressed length */
 	int res;
 
 	magic[0] = NEXTBYTE();
@@ -1112,16 +1065,16 @@ static int INIT gunzip(void)
 		return -1;
 	}
 
-	flags = (uch)get_byte();
-	if ((flags & ENCRYPTED) != 0) {
+	flags = (u8)get_byte();
+	if (flags & ENCRYPTED) {
 		error("Input is encrypted");
 		return -1;
 	}
-	if ((flags & CONTINUATION) != 0) {
+	if (flags & CONTINUATION) {
 		error("Multi part input");
 		return -1;
 	}
-	if ((flags & RESERVED) != 0) {
+	if (flags & RESERVED) {
 		error("Input has invalid flags");
 		return -1;
 	}
@@ -1133,25 +1086,22 @@ static int INIT gunzip(void)
 	(void)NEXTBYTE();	/* Ignore extra flags for the moment */
 	(void)NEXTBYTE();	/* Ignore OS type for the moment */
 
-	if ((flags & EXTRA_FIELD) != 0) {
+	if (flags & EXTRA_FIELD) {
 		unsigned len = (unsigned)NEXTBYTE();
 		len |= ((unsigned)NEXTBYTE()) << 8;
 		while (len--)
 			(void)NEXTBYTE();
 	}
 
-	/* Get original file name if it was truncated */
-	if ((flags & ORIG_NAME) != 0) {
-		/* Discard the old name */
-		while (NEXTBYTE() != 0)	/* null */
+	/* Discard original file name if it was truncated */
+	if (flags & ORIG_NAME)
+		while (NEXTBYTE())
 			;
-	}
 
 	/* Discard file comment if any */
-	if ((flags & COMMENT) != 0) {
-		while (NEXTBYTE() != 0)	/* null */
+	if (flags & COMMENT)
+		while (NEXTBYTE())
 			;
-	}
 
 	/* Decompress */
 	if ((res = inflate())) {
@@ -1180,15 +1130,15 @@ static int INIT gunzip(void)
 	/* crc32  (see algorithm.doc)
 	 * uncompressed input size modulo 2^32
 	 */
-	orig_crc = (ulg)NEXTBYTE();
-	orig_crc |= (ulg)NEXTBYTE() << 8;
-	orig_crc |= (ulg)NEXTBYTE() << 16;
-	orig_crc |= (ulg)NEXTBYTE() << 24;
-
-	orig_len = (ulg)NEXTBYTE();
-	orig_len |= (ulg)NEXTBYTE() << 8;
-	orig_len |= (ulg)NEXTBYTE() << 16;
-	orig_len |= (ulg)NEXTBYTE() << 24;
+	orig_crc = (u32)NEXTBYTE();
+	orig_crc |= (u32)NEXTBYTE() << 8;
+	orig_crc |= (u32)NEXTBYTE() << 16;
+	orig_crc |= (u32)NEXTBYTE() << 24;
+
+	orig_len = (u32)NEXTBYTE();
+	orig_len |= (u32)NEXTBYTE() << 8;
+	orig_len |= (u32)NEXTBYTE() << 16;
+	orig_len |= (u32)NEXTBYTE() << 24;
 
 	/* Validate decompression */
 	if (orig_crc != CRC_VALUE) {

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/7] inflate pt1: clean up input logic
       [not found] <0.399206195@selenic.com>
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 22:19   ` Russell King
  2006-02-24 20:12 ` [PATCH 1/7] inflate pt1: lindent and manual formatting changes Matt Mackall
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: cleanup input logic

Transform ugly macros to inlines
Kill mask_bits table
Eliminate magic underrun handling (dealt with by getbyte())

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:03.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:05.000000000 -0600
@@ -183,24 +183,23 @@ static const u16 cpdext[] = {
 	12, 12, 13, 13
 };
 
-/* Macros for inflate() bit peeking and grabbing.
+/* Inlines for inflate() bit peeking and grabbing.
    The usage is:
 
-        NEEDBITS(j)
-        x = b & mask_bits[j];
-        DUMPBITS(j)
-
-   where NEEDBITS makes sure that b has at least j bits in it, and
-   DUMPBITS removes the bits from b. The macros use the variable k for
-   the number of bits in b. Normally, b and k are initialized at the
-   beginning of a routine that uses these macros from a global bit
-   buffer and count.
+        x = readbits(&b, &k, j);
+	dumpbits(&b, &k, j);
+
+	x = pullbits(&b, &k, j);
+
+   where readbits makes sure that b has at least j bits in it, and
+   dumpbits removes the bits from b, while k tracks the number of bits
+   in b.
 
    If we assume that EOB will be the longest code, then we will never
-   ask for bits with NEEDBITS that are beyond the end of the stream.
-   So, NEEDBITS should not read any more bytes than are needed to
-   meet the request.  Then no bytes need to be "returned" to the buffer
-   at the end of the last block.
+   ask for bits that are beyond the end of the stream. So, readbits
+   should not read any more bytes than are needed to meet the request.
+   Then no bytes need to be "returned" to the buffer at the end of the
+   last block.
 
    However, this assumption is not true for fixed blocks--the EOB code
    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
@@ -216,15 +215,25 @@ static const u16 cpdext[] = {
 static u32 bb;			/* bit buffer */
 static unsigned bk;		/* bits in bit buffer */
 
-static const u16 mask_bits[] = {
-	0x0000,
-	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
-	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
-};
+static inline u32 readbits(u32 *b, u32 *k, int n)
+{
+	for( ; *k < n; *k += 8)
+		*b |= (u32)get_byte() << *k;
+	return *b & ((1 << n) - 1);
+}
 
-#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (u8)v; })
-#define NEEDBITS(n) do {while(k<(n)){b|=((u32)NEXTBYTE())<<k;k+=8;}} while(0)
-#define DUMPBITS(n) do {b>>=(n);k-=(n);} while(0)
+static inline void dumpbits(u32 *b, u32 *k, int n)
+{
+	*b >>= n;
+	*k -= n;
+}
+
+static inline u32 pullbits(u32 *b, u32 *k, int n)
+{
+	u32 r = readbits(b, k, n);
+	dumpbits(b, k, n);
+	return r;
+}
 
 /*
    Huffman code decoding is performed using a multi-level table lookup.
@@ -541,7 +550,6 @@ static int inflate_codes(struct huft *tl
 	unsigned n, d;		/* length and index for copy */
 	unsigned w;		/* current window position */
 	struct huft *t;		/* pointer to table entry */
-	unsigned ml, md;	/* masks for bl and bd bits */
 	u32 b;		/* bit buffer */
 	unsigned k;	/* number of bits in bit buffer */
 
@@ -551,20 +559,17 @@ static int inflate_codes(struct huft *tl
 	w = outcnt;			/* initialize window position */
 
 	/* inflate the coded data */
-	ml = mask_bits[bl];	/* precompute masks for speed */
-	md = mask_bits[bd];
 	for (;;) {		/* do until end of block */
-		NEEDBITS((unsigned)bl);
-		if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
-			do {
-				if (e == 99)
-					return 1;
-				DUMPBITS(t->b);
-				e -= 16;
-				NEEDBITS(e);
-			} while ((e = (t = t->v.t + ((unsigned)b &
-						     mask_bits[e]))->e) > 16);
-		DUMPBITS(t->b);
+		t = tl + readbits(&b, &k, bl);
+		e = t->e;
+		while (e > 16) {
+			if (e == 99)
+				return 1;
+			dumpbits(&b, &k, t->b);
+			t = t->v.t + readbits(&b, &k, e - 16);
+			e = t->e;
+		}
+		dumpbits(&b, &k, t->b);
 		if (e == 16) {	/* then it's a literal */
 			window[w++] = (u8)t->v.n;
 			if (w == WSIZE) {
@@ -577,25 +582,21 @@ static int inflate_codes(struct huft *tl
 				break;
 
 			/* get length of block to copy */
-			NEEDBITS(e);
-			n = t->v.n + ((unsigned)b & mask_bits[e]);
-			DUMPBITS(e);
+			n = t->v.n + pullbits(&b, &k, e);
 
 			/* decode distance of block to copy */
-			NEEDBITS((unsigned)bd);
-			if ((e = (t = td + ((unsigned)b & md))->e) > 16)
-				do {
-					if (e == 99)
-						return 1;
-					DUMPBITS(t->b);
-					e -= 16;
-					NEEDBITS(e);
-				} while ((e = (t = t->v.t + ((unsigned)b
-					   & mask_bits[e]))->e) > 16);
-			DUMPBITS(t->b);
-			NEEDBITS(e);
-			d = w - t->v.n - ((unsigned)b & mask_bits[e]);
-			DUMPBITS(e);
+			t = td + readbits(&b, &k, bd);
+			e = t->e;
+			while (e > 16) {
+				if (e == 99)
+					return 1;
+				dumpbits(&b, &k, t->b);
+				t = t->v.t + readbits(&b, &k, e - 16);
+				e = t->e;
+			}
+			dumpbits(&b, &k, t->b);
+
+			d = w - t->v.n - pullbits(&b, &k, e);
 
 			/* do the copy */
 			do {
@@ -628,9 +629,6 @@ static int inflate_codes(struct huft *tl
 
 	/* done */
 	return 0;
-
-      underrun:
-	return 4;		/* Input underrun */
 }
 
 /* inflate_stored - "decompress" an inflated type 0 (stored) block. */
@@ -649,27 +647,20 @@ static int INIT inflate_stored(void)
 	w = outcnt;			/* initialize window position */
 
 	/* go to byte boundary */
-	n = k & 7;
-	DUMPBITS(n);
+	dumpbits(&b, &k, k & 7);
 
 	/* get the length and its complement */
-	NEEDBITS(16);
-	n = ((unsigned)b & 0xffff);
-	DUMPBITS(16);
-	NEEDBITS(16);
-	if (n != (unsigned)((~b) & 0xffff))
+	n = pullbits(&b, &k, 16);
+	if (n != (~pullbits(&b, &k, 16) & 0xffff))
 		return 1;	/* error in compressed data */
-	DUMPBITS(16);
 
 	/* read and output the compressed data */
 	while (n--) {
-		NEEDBITS(8);
-		window[w++] = (u8)b;
+		window[w++] = (u8)get_byte();
 		if (w == WSIZE) {
 			flush_output(w);
 			w = 0;
 		}
-		DUMPBITS(8);
 	}
 
 	/* restore the globals from the locals */
@@ -679,9 +670,6 @@ static int INIT inflate_stored(void)
 
 	DEBG(">");
 	return 0;
-
-      underrun:
-	return 4;		/* Input underrun */
 }
 
 
@@ -748,7 +736,6 @@ static int noinline INIT inflate_dynamic
 	int i;			/* temporary variables */
 	unsigned j;
 	unsigned l;		/* last length */
-	unsigned m;		/* mask for bit lengths table */
 	unsigned n;		/* number of lengths to get */
 	struct huft *tl;	/* literal/length code table */
 	struct huft *td;	/* distance code table */
@@ -768,26 +755,17 @@ static int noinline INIT inflate_dynamic
 	k = bk;
 
 	/* read in table lengths */
-	NEEDBITS(5);
-	nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
-	DUMPBITS(5);
-	NEEDBITS(5);
-	nd = 1 + ((unsigned)b & 0x1f);	/* number of distance codes */
-	DUMPBITS(5);
-	NEEDBITS(4);
-	nb = 4 + ((unsigned)b & 0xf);	/* number of bit length codes */
-	DUMPBITS(4);
+	nl = 257 + pullbits(&b, &k, 5); /* number of literal/length codes */
+	nd = 1 + pullbits(&b, &k, 5); /* number of distance codes */
+	nb = 4 + pullbits(&b, &k, 4); /* number of bit length codes */
 	if (nl > 286 || nd > 30)
 		return 1;	/* bad lengths */
 
 	DEBG("dyn1 ");
 
 	/* read in bit-length-code lengths */
-	for (j = 0; j < nb; j++) {
-		NEEDBITS(3);
-		ll[border[j]] = (unsigned)b & 7;
-		DUMPBITS(3);
-	}
+	for (j = 0; j < nb; j++)
+		ll[border[j]] = pullbits(&b, &k, 3);
 	for (; j < 19; j++)
 		ll[border[j]] = 0;
 
@@ -805,36 +783,28 @@ static int noinline INIT inflate_dynamic
 
 	/* read in literal and distance code lengths */
 	n = nl + nd;
-	m = mask_bits[bl];
 	i = l = 0;
 	while ((unsigned)i < n) {
-		NEEDBITS((unsigned)bl);
-		j = (td = tl + ((unsigned)b & m))->b;
-		DUMPBITS(j);
+		td = tl + readbits(&b, &k, bl);
+		dumpbits(&b, &k, td->b);
 		j = td->v.n;
 		if (j < 16)	/* length of code in bits (0..15) */
 			ll[i++] = l = j;	/* save last length in l */
 		else if (j == 16) {	/* repeat last length 3 to 6 times */
-			NEEDBITS(2);
-			j = 3 + ((unsigned)b & 3);
-			DUMPBITS(2);
-			    if ((unsigned)i + j > n)
+			j = 3 + pullbits(&b, &k, 2);
+			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
 				ll[i++] = l;
 		} else if (j == 17) {	/* 3 to 10 zero length codes */
-			NEEDBITS(3);
-			j = 3 + ((unsigned)b & 7);
-			DUMPBITS(3);
+			j = 3 + pullbits(&b, &k, 3);
 			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
 				ll[i++] = 0;
 			l = 0;
 		} else {	/* j == 18: 11 to 138 zero length codes */
-			NEEDBITS(7);
-			j = 11 + ((unsigned)b & 0x7f);
-			DUMPBITS(7);
+			j = 11 + pullbits(&b, &k, 7);
 			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
@@ -892,9 +862,6 @@ static int noinline INIT inflate_dynamic
 
 	DEBG(">");
 	return 0;
-
-      underrun:
-	return 4;		/* Input underrun */
 }
 
 /* inflate_block - decompress a deflated block
@@ -903,28 +870,11 @@ static int noinline INIT inflate_dynamic
 static int INIT inflate_block(int *e)
 {
 	unsigned t;		/* block type */
-	u32 b;		/* bit buffer */
-	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<blk");
 
-	/* make local bit buffer */
-	b = bb;
-	k = bk;
-
-	/* read in last block bit */
-	NEEDBITS(1);
-	*e = (int)b & 1;
-	DUMPBITS(1);
-
-	/* read in block type */
-	NEEDBITS(2);
-	t = (unsigned)b & 3;
-	DUMPBITS(2);
-
-	/* restore the global bit buffer */
-	bb = b;
-	bk = k;
+	*e = pullbits(&bb, &bk, 1); /* read in last block bit */
+	t = pullbits(&bb, &bk, 2); /* read in block type */
 
 	/* inflate that block type */
 	if (t == 2)
@@ -938,9 +888,6 @@ static int INIT inflate_block(int *e)
 
 	/* bad block type */
 	return 2;
-
-      underrun:
-	return 4;		/* Input underrun */
 }
 
 /* inflate - decompress an inflated entry */
@@ -1050,9 +997,9 @@ static int INIT gunzip(void)
 	u32 orig_len = 0;	/* original uncompressed length */
 	int res;
 
-	magic[0] = NEXTBYTE();
-	magic[1] = NEXTBYTE();
-	method = NEXTBYTE();
+	magic[0] = get_byte();
+	magic[1] = get_byte();
+	method = get_byte();
 
 	if (magic[0] != 037 || ((magic[1] != 0213) && (magic[1] != 0236))) {
 		error("bad gzip magic numbers");
@@ -1078,29 +1025,29 @@ static int INIT gunzip(void)
 		error("Input has invalid flags");
 		return -1;
 	}
-	NEXTBYTE();		/* Get timestamp */
-	NEXTBYTE();
-	NEXTBYTE();
-	NEXTBYTE();
+	get_byte();		/* Get timestamp */
+	get_byte();
+	get_byte();
+	get_byte();
 
-	(void)NEXTBYTE();	/* Ignore extra flags for the moment */
-	(void)NEXTBYTE();	/* Ignore OS type for the moment */
+	get_byte();	/* Ignore extra flags for the moment */
+	get_byte();	/* Ignore OS type for the moment */
 
 	if (flags & EXTRA_FIELD) {
-		unsigned len = (unsigned)NEXTBYTE();
-		len |= ((unsigned)NEXTBYTE()) << 8;
+		unsigned len = (unsigned)get_byte();
+		len |= ((unsigned)get_byte()) << 8;
 		while (len--)
-			(void)NEXTBYTE();
+			get_byte();
 	}
 
 	/* Discard original file name if it was truncated */
 	if (flags & ORIG_NAME)
-		while (NEXTBYTE())
+		while (get_byte())
 			;
 
 	/* Discard file comment if any */
 	if (flags & COMMENT)
-		while (NEXTBYTE())
+		while (get_byte())
 			;
 
 	/* Decompress */
@@ -1130,15 +1077,15 @@ static int INIT gunzip(void)
 	/* crc32  (see algorithm.doc)
 	 * uncompressed input size modulo 2^32
 	 */
-	orig_crc = (u32)NEXTBYTE();
-	orig_crc |= (u32)NEXTBYTE() << 8;
-	orig_crc |= (u32)NEXTBYTE() << 16;
-	orig_crc |= (u32)NEXTBYTE() << 24;
-
-	orig_len = (u32)NEXTBYTE();
-	orig_len |= (u32)NEXTBYTE() << 8;
-	orig_len |= (u32)NEXTBYTE() << 16;
-	orig_len |= (u32)NEXTBYTE() << 24;
+	orig_crc = (u32)get_byte();
+	orig_crc |= (u32)get_byte() << 8;
+	orig_crc |= (u32)get_byte() << 16;
+	orig_crc |= (u32)get_byte() << 24;
+
+	orig_len = (u32)get_byte();
+	orig_len |= (u32)get_byte() << 8;
+	orig_len |= (u32)get_byte() << 16;
+	orig_len |= (u32)get_byte() << 24;
 
 	/* Validate decompression */
 	if (orig_crc != CRC_VALUE) {
@@ -1150,8 +1097,4 @@ static int INIT gunzip(void)
 		return -1;
 	}
 	return 0;
-
-      underrun:		/* NEXTBYTE() goto's here if needed */
-	error("out of input data");
-	return -1;
 }

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/7] inflate pt1: start moving globals into iostate
       [not found] <0.399206195@selenic.com>
  2006-02-24 20:12 ` [PATCH 3/7] inflate pt1: clean up input logic Matt Mackall
  2006-02-24 20:12 ` [PATCH 1/7] inflate pt1: lindent and manual formatting changes Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 20:12 ` [PATCH 2/7] inflate pt1: kill legacy bits Matt Mackall
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: move globals into a state structure

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:05.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:07.000000000 -0600
@@ -125,16 +125,23 @@ struct huft {
 	} v;
 };
 
+struct iostate {
+	u8 *window;
+	int opos, osize, bits;
+	u32 buf;
+};
+
 /* Function prototypes */
 static int INIT huft_build(unsigned *, unsigned, unsigned,
 			  const u16 *, const u16 *, struct huft **, int *);
 static int INIT huft_free(struct huft *);
-static int INIT inflate_codes(struct huft *, struct huft *, int, int);
-static int INIT inflate_stored(void);
-static int INIT inflate_fixed(void);
-static int INIT inflate_dynamic(void);
-static int INIT inflate_block(int *);
-static int INIT inflate(void);
+static int INIT inflate_codes(struct iostate *, struct huft *, struct huft *,
+			 int, int);
+static int INIT inflate_stored(struct iostate *);
+static int INIT inflate_fixed(struct iostate *);
+static int INIT inflate_dynamic(struct iostate *);
+static int INIT inflate_block(struct iostate *, int *);
+static int INIT inflate(struct iostate *);
 
 /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
    stream to find repeated byte strings.  This is implemented here as a
@@ -145,7 +152,36 @@ static int INIT inflate(void);
    "u8 *window;" and then malloc'ed in the latter case.  The definition
    must be in unzip.h, included above. */
 
-#define flush_output(w) (outcnt=(w),flush_window())
+static void flush_output(struct iostate *io)
+{
+	outcnt = io->opos;
+	flush_window();
+	io->opos = 0;
+}
+
+static inline void put_byte(struct iostate *io, u8 byte)
+{
+	io->window[io->opos++] = byte;
+	if (io->opos == io->osize)
+		flush_output(io);
+}
+
+static void copy_bytes(struct iostate *io, int len, int dist)
+{
+	int part, pos = io->opos - dist;
+
+	do {
+		pos &= io->osize - 1;
+		part = min(len, io->osize - max(pos, io->opos));
+		len -= part;
+
+		while (part--)
+			io->window[io->opos++] = io->window[pos++];
+
+		if (io->opos == io->osize)
+			flush_output(io);
+	} while (len);
+}
 
 /* Tables for deflate from PKZIP's appnote.txt. */
 
@@ -186,10 +222,10 @@ static const u16 cpdext[] = {
 /* Inlines for inflate() bit peeking and grabbing.
    The usage is:
 
-        x = readbits(&b, &k, j);
-	dumpbits(&b, &k, j);
+        x = readbits(io, j);
+	dumpbits(io, j);
 
-	x = pullbits(&b, &k, j);
+	x = pullbits(io, j);
 
    where readbits makes sure that b has at least j bits in it, and
    dumpbits removes the bits from b, while k tracks the number of bits
@@ -212,29 +248,32 @@ static const u16 cpdext[] = {
    the stream.
  */
 
-static u32 bb;			/* bit buffer */
-static unsigned bk;		/* bits in bit buffer */
-
-static inline u32 readbits(u32 *b, u32 *k, int n)
+static inline u32 readbits(struct iostate *io, int n)
 {
-	for( ; *k < n; *k += 8)
-		*b |= (u32)get_byte() << *k;
-	return *b & ((1 << n) - 1);
+	for( ; io->bits < n; io->bits += 8)
+		io->buf |= (u32)get_byte() << io->bits;
+	return io->buf & ((1 << n) - 1);
 }
 
-static inline void dumpbits(u32 *b, u32 *k, int n)
+static inline void dumpbits(struct iostate *io, int n)
 {
-	*b >>= n;
-	*k -= n;
+	io->buf >>= n;
+	io->bits -= n;
 }
 
-static inline u32 pullbits(u32 *b, u32 *k, int n)
+static inline u32 pullbits(struct iostate *io, int n)
 {
-	u32 r = readbits(b, k, n);
-	dumpbits(b, k, n);
+	u32 r = readbits(io, n);
+	dumpbits(io, n);
 	return r;
 }
 
+static inline void popbytes(struct iostate *io)
+{
+	inptr -= (io->bits >> 3);
+	io->bits &= 7;
+}
+
 /*
    Huffman code decoding is performed using a multi-level table lookup.
    The fastest way to decode is to simply build a lookup table whose
@@ -536,6 +575,7 @@ STATIC int INIT huft_free(struct huft *t
 
 /*
  * inflate_codes - decompress the codes in a deflated block
+ * @io: current i/o state
  * @tl: literal/length decoder tables
  * @td: distance decoder tables
  * @bl: number of bits decoded by tl
@@ -544,129 +584,75 @@ STATIC int INIT huft_free(struct huft *t
  * inflate (decompress) the codes in a deflated (compressed) block.
  * Return an error code or zero if it all goes ok.
  */
-static int inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
+static int INIT inflate_codes(struct iostate *io, struct huft *tl, struct huft *td,
+			 int bl, int bd)
 {
 	unsigned e;	/* table entry flag/number of extra bits */
-	unsigned n, d;		/* length and index for copy */
-	unsigned w;		/* current window position */
+	unsigned len, dist;
 	struct huft *t;		/* pointer to table entry */
-	u32 b;		/* bit buffer */
-	unsigned k;	/* number of bits in bit buffer */
-
-	/* make local copies of globals */
-	b = bb;			/* initialize bit buffer */
-	k = bk;
-	w = outcnt;			/* initialize window position */
 
 	/* inflate the coded data */
 	for (;;) {		/* do until end of block */
-		t = tl + readbits(&b, &k, bl);
+		t = tl + readbits(io, bl);
 		e = t->e;
 		while (e > 16) {
 			if (e == 99)
 				return 1;
-			dumpbits(&b, &k, t->b);
-			t = t->v.t + readbits(&b, &k, e - 16);
+			dumpbits(io, t->b);
+			t = t->v.t + readbits(io, e - 16);
 			e = t->e;
 		}
-		dumpbits(&b, &k, t->b);
+		dumpbits(io, t->b);
 		if (e == 16) {	/* then it's a literal */
-			window[w++] = (u8)t->v.n;
-			if (w == WSIZE) {
-				flush_output(w);
-				w = 0;
-			}
+			put_byte(io, t->v.n);
 		} else {	/* it's an EOB or a length */
 			/* exit if end of block */
 			if (e == 15)
 				break;
 
 			/* get length of block to copy */
-			n = t->v.n + pullbits(&b, &k, e);
+			len = t->v.n + pullbits(io, e);
 
 			/* decode distance of block to copy */
-			t = td + readbits(&b, &k, bd);
+			t = td + readbits(io, bd);
 			e = t->e;
 			while (e > 16) {
 				if (e == 99)
 					return 1;
-				dumpbits(&b, &k, t->b);
-				t = t->v.t + readbits(&b, &k, e - 16);
+				dumpbits(io, t->b);
+				t = t->v.t + readbits(io, e - 16);
 				e = t->e;
 			}
-			dumpbits(&b, &k, t->b);
+			dumpbits(io, t->b);
 
-			d = w - t->v.n - pullbits(&b, &k, e);
-
-			/* do the copy */
-			do {
-				n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ?
-							d : w)) > n ? n : e);
-#if !defined(NOMEMCPY) && !defined(DEBUG)
-				/* (this test assumes unsigned comparison) */
-				if (w - d >= e) {
-					memcpy(window + w, window + d, e);
-					w += e;
-					d += e;
-				} else
-#endif				/* !NOMEMCPY */
-					/* avoid memcpy() overlap */
-					do {
-						window[w++] = window[d++];
-					} while (--e);
-				if (w == WSIZE) {
-					flush_output(w);
-					w = 0;
-				}
-			} while (n);
+			dist = t->v.n + pullbits(io, e);
+			copy_bytes(io, len, dist);
 		}
 	}
 
-	/* restore the globals from the locals */
-	outcnt = w;			/* restore global window pointer */
-	bb = b;			/* restore global bit buffer */
-	bk = k;
-
-	/* done */
 	return 0;
 }
 
-/* inflate_stored - "decompress" an inflated type 0 (stored) block. */
-static int INIT inflate_stored(void)
+/* inflate_stored - "decompress" an inflated type 0 (stored) block.
+ * @io: current i/o state
+ */
+static int INIT inflate_stored(struct iostate *io)
 {
 	unsigned n;		/* number of bytes in block */
-	unsigned w;		/* current window position */
-	u32 b;		/* bit buffer */
-	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<stor");
 
-	/* make local copies of globals */
-	b = bb;			/* initialize bit buffer */
-	k = bk;
-	w = outcnt;			/* initialize window position */
-
 	/* go to byte boundary */
-	dumpbits(&b, &k, k & 7);
+	dumpbits(io, io->bits & 7);
 
 	/* get the length and its complement */
-	n = pullbits(&b, &k, 16);
-	if (n != (~pullbits(&b, &k, 16) & 0xffff))
+	n = pullbits(io, 16);
+	if (n != (~pullbits(io, 16) & 0xffff))
 		return 1;	/* error in compressed data */
 
 	/* read and output the compressed data */
-	while (n--) {
-		window[w++] = (u8)get_byte();
-		if (w == WSIZE) {
-			flush_output(w);
-			w = 0;
-		}
-	}
-
-	/* restore the globals from the locals */
-	outcnt = w;			/* restore global window pointer */
-	bb = b;			/* restore global bit buffer */
-	bk = k;
+	while (n--)
+		put_byte(io, get_byte());
 
 	DEBG(">");
 	return 0;
@@ -674,6 +660,7 @@ static int INIT inflate_stored(void)
 
 
 /* inflate_fixed - decompress a block with fixed Huffman codes
+ * @io: current i/o state
  *
  * decompress an inflated type 1 (fixed Huffman codes) block. We
  * should either replace this with a custom decoder, or at least
@@ -681,7 +668,7 @@ static int INIT inflate_stored(void)
  *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
-static int noinline INIT inflate_fixed(void)
+static int noinline INIT inflate_fixed(struct iostate *io)
 {
 	int i;			/* temporary variable */
 	struct huft *tl;	/* literal/length code table */
@@ -717,7 +704,7 @@ static int noinline INIT inflate_fixed(v
 	}
 
 	/* decompress until an end-of-block code */
-	if (inflate_codes(tl, td, bl, bd))
+	if (inflate_codes(io, tl, td, bl, bd))
 		return 1;
 
 	/* free the decoding tables, return */
@@ -731,7 +718,7 @@ static int noinline INIT inflate_fixed(v
  *
  * We use `noinline' here to prevent gcc-3.5 from using too much stack space
  */
-static int noinline INIT inflate_dynamic(void)
+static int noinline INIT inflate_dynamic(struct iostate *io)
 {
 	int i;			/* temporary variables */
 	unsigned j;
@@ -745,19 +732,13 @@ static int noinline INIT inflate_dynamic
 	unsigned nl;		/* number of literal/length codes */
 	unsigned nd;		/* number of distance codes */
 	unsigned ll[286 + 30];	/* literal/length and distance code lengths */
-	u32 b;		/* bit buffer */
-	unsigned k;	/* number of bits in bit buffer */
 
 	DEBG("<dyn");
 
-	/* make local bit buffer */
-	b = bb;
-	k = bk;
-
 	/* read in table lengths */
-	nl = 257 + pullbits(&b, &k, 5); /* number of literal/length codes */
-	nd = 1 + pullbits(&b, &k, 5); /* number of distance codes */
-	nb = 4 + pullbits(&b, &k, 4); /* number of bit length codes */
+	nl = 257 + pullbits(io, 5); /* number of literal/length codes */
+	nd = 1 + pullbits(io, 5); /* number of distance codes */
+	nb = 4 + pullbits(io, 4); /* number of bit length codes */
 	if (nl > 286 || nd > 30)
 		return 1;	/* bad lengths */
 
@@ -765,7 +746,7 @@ static int noinline INIT inflate_dynamic
 
 	/* read in bit-length-code lengths */
 	for (j = 0; j < nb; j++)
-		ll[border[j]] = pullbits(&b, &k, 3);
+		ll[border[j]] = pullbits(io, 3);
 	for (; j < 19; j++)
 		ll[border[j]] = 0;
 
@@ -785,26 +766,26 @@ static int noinline INIT inflate_dynamic
 	n = nl + nd;
 	i = l = 0;
 	while ((unsigned)i < n) {
-		td = tl + readbits(&b, &k, bl);
-		dumpbits(&b, &k, td->b);
+		td = tl + readbits(io, bl);
+		dumpbits(io, td->b);
 		j = td->v.n;
 		if (j < 16)	/* length of code in bits (0..15) */
 			ll[i++] = l = j;	/* save last length in l */
 		else if (j == 16) {	/* repeat last length 3 to 6 times */
-			j = 3 + pullbits(&b, &k, 2);
+			j = 3 + pullbits(io, 2);
 			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
 				ll[i++] = l;
 		} else if (j == 17) {	/* 3 to 10 zero length codes */
-			j = 3 + pullbits(&b, &k, 3);
+			j = 3 + pullbits(io, 3);
 			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
 				ll[i++] = 0;
 			l = 0;
 		} else {	/* j == 18: 11 to 138 zero length codes */
-			j = 11 + pullbits(&b, &k, 7);
+			j = 11 + pullbits(io, 7);
 			if ((unsigned)i + j > n)
 				return 1;
 			while (j--)
@@ -820,10 +801,6 @@ static int noinline INIT inflate_dynamic
 
 	DEBG("dyn5 ");
 
-	/* restore the global bit buffer */
-	bb = b;
-	bk = k;
-
 	DEBG("dyn5a ");
 
 	/* build the decoding tables for literal/length and distance codes */
@@ -851,7 +828,7 @@ static int noinline INIT inflate_dynamic
 	DEBG("dyn6 ");
 
 	/* decompress until an end-of-block code */
-	if (inflate_codes(tl, td, bl, bd))
+	if (inflate_codes(io, tl, td, bl, bd))
 		return 1;
 
 	DEBG("dyn7 ");
@@ -865,24 +842,25 @@ static int noinline INIT inflate_dynamic
 }
 
 /* inflate_block - decompress a deflated block
+ * @io: current i/o state
  * @e: last block flag
  */
-static int INIT inflate_block(int *e)
+static int INIT inflate_block(struct iostate *io, int *e)
 {
 	unsigned t;		/* block type */
 
 	DEBG("<blk");
 
-	*e = pullbits(&bb, &bk, 1); /* read in last block bit */
-	t = pullbits(&bb, &bk, 2); /* read in block type */
+	*e = pullbits(io, 1); /* read in last block bit */
+	t = pullbits(io, 2); /* read in block type */
 
 	/* inflate that block type */
 	if (t == 2)
-		return inflate_dynamic();
+		return inflate_dynamic(io);
 	if (t == 0)
-		return inflate_stored();
+		return inflate_stored(io);
 	if (t == 1)
-		return inflate_fixed();
+		return inflate_fixed(io);
 
 	DEBG(">");
 
@@ -890,40 +868,27 @@ static int INIT inflate_block(int *e)
 	return 2;
 }
 
-/* inflate - decompress an inflated entry */
-static int INIT inflate(void)
+/* inflate - decompress an inflated entry
+ * @io: current i/o state
+ */
+static int INIT inflate(struct iostate *io)
 {
 	int e;			/* last block flag */
 	int r;			/* result code */
 	void *ptr;
 
-	/* initialize window, bit buffer */
-	outcnt = 0;
-	bk = 0;
-	bb = 0;
-
 	/* decompress until the last block */
 	do {
 		gzip_mark(&ptr);
-		if ((r = inflate_block(&e))) {
+		if ((r = inflate_block(io, &e))) {
 			gzip_release(&ptr);
 			return r;
 		}
 		gzip_release(&ptr);
 	} while (!e);
 
-	/* Undo too much lookahead. The next read will be byte aligned so we
-	 * can discard unused bits in the last meaningful byte.
-	 */
-	while (bk >= 8) {
-		bk -= 8;
-		inptr--;
-	}
-
-	/* flush out window */
-	flush_output(outcnt);
-
-	/* return success */
+	popbytes(io);
+	flush_output(io);
 	return 0;
 }
 
@@ -996,6 +961,11 @@ static int INIT gunzip(void)
 	u32 orig_crc = 0;	/* original crc */
 	u32 orig_len = 0;	/* original uncompressed length */
 	int res;
+	struct iostate io;
+
+	io.window = window;
+	io.osize = WSIZE;
+	io.opos = io.bits = io.buf = 0;
 
 	magic[0] = get_byte();
 	magic[1] = get_byte();
@@ -1051,7 +1021,7 @@ static int INIT gunzip(void)
 			;
 
 	/* Decompress */
-	if ((res = inflate())) {
+	if ((res = inflate(&io))) {
 		switch (res) {
 		case 0:
 			break;

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 6/7] inflate pt1: internalize CRC calculation, cleanup table calculation
       [not found] <0.399206195@selenic.com>
                   ` (3 preceding siblings ...)
  2006-02-24 20:12 ` [PATCH 2/7] inflate pt1: kill legacy bits Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 20:12 ` [PATCH 5/7] inflate pt1: cleanup Huffman table code Matt Mackall
  2006-02-24 20:12 ` [PATCH 7/7] inflate pt1: eliminate memzero usage Matt Mackall
  6 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: cleanup CRC calculation

Move CRC calculation into inflate code
Cleanup table calculation code

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:08.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:10.000000000 -0600
@@ -109,6 +109,10 @@
 
 #include <asm/types.h>
 
+static u32 crc_32_tab[256];
+static u32 crc;		/* dummy var until users get cleaned up */
+#define CRCPOLY_LE 0xedb88320
+
 /* Huffman code lookup table entry--this entry is four bytes for machines
    that have 16-bit pointers (e.g. PC's in the small or medium model).
    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
@@ -128,7 +132,7 @@ struct huft {
 struct iostate {
 	u8 *window;
 	int opos, osize, bits;
-	u32 buf;
+	u32 buf, crc;
 };
 
 /* Function prototypes */
@@ -154,6 +158,12 @@ static int INIT inflate(struct iostate *
 
 static void flush_output(struct iostate *io)
 {
+	int i;
+
+	for (i = 0; i < io->opos; i++)
+		io->crc = crc_32_tab[(io->window[i] ^ (int)io->crc) & 0xff]
+			^ (io->crc >> 8);
+
 	outcnt = io->opos;
 	flush_window();
 	io->opos = 0;
@@ -906,47 +916,16 @@ static int INIT inflate(struct iostate *
  *
  **********************************************************************/
 
-static u32 crc_32_tab[256];
-static u32 crc;		/* initialized in makecrc() so it'll reside in bss */
-#define CRC_VALUE (crc ^ 0xffffffffUL)
-
-/*
- * Code to compute the CRC-32 table. Borrowed from
- * gzip-1.0.3/makecrc.c.
- * Not copyrighted 1990 Mark Adler
- */
-
 static void INIT makecrc(void)
 {
+	unsigned i, j;
+	u32 c = 1;
 
-	unsigned long c;	/* crc shift register */
-	unsigned long e;	/* polynomial exclusive-or pattern */
-	int i;			/* counter for all possible eight bit values */
-	int k;			/* byte being shifted into crc apparatus */
-
-	/* terms of polynomial defining this crc (except x^32): */
-	static const int p[] =
-	    { 0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26 };
-
-	/* Make exclusive-or pattern from polynomial */
-	e = 0;
-	for (i = 0; i < sizeof(p) / sizeof(int); i++)
-		e |= 1L << (31 - p[i]);
-
-	crc_32_tab[0] = 0;
-
-	for (i = 1; i < 256; i++) {
-		c = 0;
-		for (k = i | 256; k != 1; k >>= 1) {
-			c = c & 1 ? (c >> 1) ^ e : c >> 1;
-			if (k & 1)
-				c ^= e;
-		}
-		crc_32_tab[i] = c;
+	for (i = 128; i; i >>= 1) {
+		c = (c >> 1) ^ ((c & 1) ? CRCPOLY_LE : 0);
+		for (j = 0; j < 256; j += 2 * i)
+			crc_32_tab[i + j] = c ^ crc_32_tab[j];
 	}
-
-	/* this is initialized here so this code could reside in ROM */
-	crc = 0xffffffffUL;	/* shift register contents */
 }
 
 /* gzip flag byte */
@@ -966,14 +945,15 @@ static int INIT gunzip(void)
 	u8 flags;
 	unsigned char magic[2];	/* magic header */
 	char method;
-	u32 orig_crc = 0;	/* original crc */
-	u32 orig_len = 0;	/* original uncompressed length */
+	u32 orig_crc;
+	u32 orig_len;
 	int res;
 	struct iostate io;
 
 	io.window = window;
 	io.osize = WSIZE;
 	io.opos = io.bits = io.buf = 0;
+	io.crc = 0xffffffffUL;
 
 	magic[0] = get_byte();
 	magic[1] = get_byte();
@@ -1051,8 +1031,7 @@ static int INIT gunzip(void)
 		return -1;
 	}
 
-	/* Get the crc and original length */
-	/* crc32  (see algorithm.doc)
+	/* Get the crc and original length
 	 * uncompressed input size modulo 2^32
 	 */
 	orig_crc = (u32)get_byte();
@@ -1066,7 +1045,7 @@ static int INIT gunzip(void)
 	orig_len |= (u32)get_byte() << 24;
 
 	/* Validate decompression */
-	if (orig_crc != CRC_VALUE) {
+	if (orig_crc != ~io.crc) {
 		error("crc error");
 		return -1;
 	}

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/7] inflate pt1: cleanup Huffman table code
       [not found] <0.399206195@selenic.com>
                   ` (4 preceding siblings ...)
  2006-02-24 20:12 ` [PATCH 6/7] inflate pt1: internalize CRC calculation, cleanup table calculation Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  2006-02-24 21:52   ` John Reiser
  2006-02-24 20:12 ` [PATCH 7/7] inflate pt1: eliminate memzero usage Matt Mackall
  6 siblings, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: cleanup huffman table code

sensible names for huft struct members
get rid of assignment-in-expression usage

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:07.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:08.000000000 -0600
@@ -117,12 +117,12 @@
    an unused code.  If a code with e == 99 is looked up, this implies an
    error in the data. */
 struct huft {
-	u8 e;			/* number of extra bits or operation */
-	u8 b;			/* number of bits in this code or subcode */
 	union {
-		u16 n;		/* literal, length base, or distance base */
-		struct huft *t;	/* pointer to next level of table */
-	} v;
+		u16 val; /* literal, length base, or distance base */
+		struct huft *next; /* pointer to next level of table */
+	};
+	u8 extra; /* number of extra bits or operation */
+	u8 bits; /* number of bits in this code or subcode */
 };
 
 struct iostate {
@@ -393,11 +393,14 @@ static int INIT huft_build(unsigned *b, 
 	DEBG("huft3 ");
 
 	/* Adjust last length count to fill out codes, if needed */
-	for (y = 1 << j; j < i; j++, y <<= 1)
-		if ((y -= c[j]) < 0)
+	for (y = 1 << j; j < i; j++, y <<= 1) {
+		y -= c[j];
+		if (y < 0)
 			return 2; /* bad input: more codes than bits */
+	}
 
-	if ((y -= c[i]) < 0)
+	y -= c[i];
+	if (y < 0)
 		return 2;
 	c[i] += y;
 
@@ -408,8 +411,12 @@ static int INIT huft_build(unsigned *b, 
 	p = c + 1;
 	xp = x + 2;
 	/* note that i == g from above */
-	while (--i)
-		*xp++ = (j += *p++);
+	while (--i) {
+		j += *p;
+		*xp = j;
+		p++;
+		xp++;
+	}
 
 	DEBG("huft5 ");
 
@@ -417,7 +424,8 @@ static int INIT huft_build(unsigned *b, 
 	p = b;
 	i = 0;
 	do {
-		if ((j = *p++))
+		j = *p++;
+		if (j)
 			v[x[j]++] = i;
 	} while (++i < n);
 
@@ -450,10 +458,12 @@ static int INIT huft_build(unsigned *b, 
 
 				/* compute min size <= l bits */
 				/* upper limit on table size */
-				z = (z = g - w) > (unsigned)l ? l : z;
+				z = min(l, g - w);
 
 				/* try a k-w bit table */
-				if ((f = 1 << (j = k - w)) > a + 1) {
+				j = k - w;
+				f = 1 << j;
+				if (f > a + 1) {
 					/* too few codes for k-w bit table */
 					DEBG1("2 ");
 					/* deduct codes from patterns left */
@@ -463,7 +473,8 @@ static int INIT huft_build(unsigned *b, 
 					if (j < z) {
 						/* enough codes for j bits? */
 						while (++j < z) {
-							if ((f <<= 1) <= *++xp)
+							f <<= 1;
+							if (f <= *++xp)
 								break;
 							/* deduct from pats */
 							f -= *xp;
@@ -476,15 +487,17 @@ static int INIT huft_build(unsigned *b, 
 				z = 1 << j;
 
 				/* allocate and link in new table */
-				if (!(q = (struct huft *)malloc(
-					     (z + 1) * sizeof(struct huft)))) {
+				q = malloc((z + 1) * sizeof(struct huft));
+				if (!q) {
 					if (h)
 						huft_free(u[0]);
 					return 3;	/* not enough memory */
 				}
+
 				DEBG1("4 ");
 				*t = q + 1; /* link to list for huft_free */
-				*(t = &(q->v.t)) = 0;
+				t = &q->next;
+				*t = NULL;
 				u[h] = ++q;	/* table starts after link */
 
 				DEBG1("5 ");
@@ -493,11 +506,11 @@ static int INIT huft_build(unsigned *b, 
 					/* save pattern for backing up */
 					x[h] = i;
 					/* bits to dump before this table */
-					r.b = (u8)l;
+					r.bits = (u8)l;
 					/* bits in this table */
-					r.e = (u8)(16 + j);
+					r.extra = (u8)(16 + j);
 					/* pointer to this table */
-					r.v.t = q;
+					r.next = q;
 					/* (get around Turbo C bug) */
 					j = i >> (w - l);
 					/* connect to last table */
@@ -508,20 +521,20 @@ static int INIT huft_build(unsigned *b, 
 			DEBG("h6c ");
 
 			/* set up table entry in r */
-			r.b = (u8) (k - w);
+			r.bits = (u8)(k - w);
 			if (p >= v + n)
-				r.e = 99; /* out of values--invalid code */
+				r.extra = 99; /* out of values--invalid code */
 			else if (*p < s) {
 				/* 256 is end-of-block code */
-				r.e = (u8)(*p < 256 ? 16 : 15);
+				r.extra = (u8)(*p < 256 ? 16 : 15);
 				/* simple code is just the value */
-				r.v.n = (u16)(*p);
+				r.val = (u16)(*p);
 				/* one compiler does not like *p++ */
 				p++;
 			} else {
 				/* non-simple--look up in lists */
-				r.e = (u8)e[*p - s];
-				r.v.n = d[*p++ - s];
+				r.extra = (u8)e[*p - s];
+				r.val = d[*p++ - s];
 			}
 			DEBG("h6d ");
 
@@ -559,16 +572,16 @@ static int INIT huft_build(unsigned *b, 
  * linked list of the tables it made, with the links in a dummy first
  * entry of each table.
  */
-STATIC int INIT huft_free(struct huft *t)
+static int INIT huft_free(struct huft *t)
 {
-	struct huft *p, *q;
+	struct huft *q;
 
 	/* Go through list, freeing from the malloced (t[-1]) address. */
-	p = t;
-	while (p) {
-		q = (--p)->v.t;
-		free((char *)p);
-		p = q;
+	while (t) {
+		t -= 1;
+		q = t->next;
+		free(t);
+		t = q;
 	}
 	return 0;
 }
@@ -587,45 +600,40 @@ STATIC int INIT huft_free(struct huft *t
 static int INIT inflate_codes(struct iostate *io, struct huft *tl, struct huft *td,
 			 int bl, int bd)
 {
-	unsigned e;	/* table entry flag/number of extra bits */
 	unsigned len, dist;
 	struct huft *t;		/* pointer to table entry */
 
 	/* inflate the coded data */
 	for (;;) {		/* do until end of block */
 		t = tl + readbits(io, bl);
-		e = t->e;
-		while (e > 16) {
-			if (e == 99)
+		while (t->extra > 16) {
+			if (t->extra == 99)
 				return 1;
-			dumpbits(io, t->b);
-			t = t->v.t + readbits(io, e - 16);
-			e = t->e;
+			dumpbits(io, t->bits);
+			t = &t->next[readbits(io, t->extra - 16)];
 		}
-		dumpbits(io, t->b);
-		if (e == 16) {	/* then it's a literal */
-			put_byte(io, t->v.n);
+		dumpbits(io, t->bits);
+		if (t->extra == 16) {	/* then it's a literal */
+			put_byte(io, t->val);
 		} else {	/* it's an EOB or a length */
 			/* exit if end of block */
-			if (e == 15)
+			if (t->extra == 15)
 				break;
 
 			/* get length of block to copy */
-			len = t->v.n + pullbits(io, e);
+			len = t->val + pullbits(io, t->extra);
 
 			/* decode distance of block to copy */
 			t = td + readbits(io, bd);
-			e = t->e;
-			while (e > 16) {
-				if (e == 99)
+			while (t->extra > 16) {
+				if (t->extra == 99)
 					return 1;
-				dumpbits(io, t->b);
-				t = t->v.t + readbits(io, e - 16);
-				e = t->e;
+				dumpbits(io, t->bits);
+				t = &t->next[readbits(io, t->extra - 16)];
 			}
-			dumpbits(io, t->b);
+			dumpbits(io, t->bits);
 
-			dist = t->v.n + pullbits(io, e);
+			dist = t->val + pullbits(io, t->extra);
 			copy_bytes(io, len, dist);
 		}
 	}
@@ -767,8 +775,8 @@ static int noinline INIT inflate_dynamic
 	i = l = 0;
 	while ((unsigned)i < n) {
 		td = tl + readbits(io, bl);
-		dumpbits(io, td->b);
-		j = td->v.n;
+		dumpbits(io, td->bits);
+		j = td->val;
 		if (j < 16)	/* length of code in bits (0..15) */
 			ll[i++] = l = j;	/* save last length in l */
 		else if (j == 16) {	/* repeat last length 3 to 6 times */

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 7/7] inflate pt1: eliminate memzero usage
       [not found] <0.399206195@selenic.com>
                   ` (5 preceding siblings ...)
  2006-02-24 20:12 ` [PATCH 5/7] inflate pt1: cleanup Huffman table code Matt Mackall
@ 2006-02-24 20:12 ` Matt Mackall
  6 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 20:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

inflate: replace call to memzero with simple loop

This is the only user of memzero in the inflate code and it's only for
16 bytes. Removing this lets us drop a copy of memzero from most
lib/inflate users.

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: 2.6.16-rc4-inflate/lib/inflate.c
===================================================================
--- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:10.000000000 -0600
+++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:12.000000000 -0600
@@ -365,8 +365,10 @@ static int INIT huft_build(unsigned *b, 
 
 	DEBG("huft1 ");
 
+	for (i = 0; i < BMAX + 1; i++)
+		c[i] = 0;
+
 	/* Generate counts for each bit length */
-	memzero(c, sizeof(c));
 	p = b;
 	i = n;
 	do {

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 5/7] inflate pt1: cleanup Huffman table code
  2006-02-24 20:12 ` [PATCH 5/7] inflate pt1: cleanup Huffman table code Matt Mackall
@ 2006-02-24 21:52   ` John Reiser
  2006-02-24 22:06     ` Matt Mackall
  0 siblings, 1 reply; 31+ messages in thread
From: John Reiser @ 2006-02-24 21:52 UTC (permalink / raw)
  To: Matt Mackall; +Cc: linux-kernel

> Index: 2.6.16-rc4-inflate/lib/inflate.c
> ===================================================================
> --- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:07.000000000 -0600
> +++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:08.000000000 -0600
> @@ -117,12 +117,12 @@
>     an unused code.  If a code with e == 99 is looked up, this implies an
>     error in the data. */
>  struct huft {
> -	u8 e;			/* number of extra bits or operation */
> -	u8 b;			/* number of bits in this code or subcode */
>  	union {
> -		u16 n;		/* literal, length base, or distance base */
> -		struct huft *t;	/* pointer to next level of table */
> -	} v;
> +		u16 val; /* literal, length base, or distance base */
> +		struct huft *next; /* pointer to next level of table */
> +	};
> +	u8 extra; /* number of extra bits or operation */
> +	u8 bits; /* number of bits in this code or subcode */
>  };

How aggressive do you want to be?  About 3.7 years ago in
http://freshmeat.net/projects/gzip_x86/  I published:
-----
typedef unsigned short huft_ndx;  /* index>>1 */
struct huft {
  uch e;                /* number of extra bits or operation */
  uch b;                /* number of bits in this code or subcode */
  union {
    ush n;              /* literal, length base, or distance base */
    huft_ndx t;         /* index>>1 of next level of table */
  } v;
};
-----
which makes 4==sizeof(struct huft) and enables manipulation by 32-bit
fetch, exposing 'e' [extra] and 'b' [bits] in x86 byte registers
for fewer shift-and-mask operations.  The struct huft can be managed
as a stack of maximum length 1014.

-- 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 5/7] inflate pt1: cleanup Huffman table code
  2006-02-24 21:52   ` John Reiser
@ 2006-02-24 22:06     ` Matt Mackall
  0 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-24 22:06 UTC (permalink / raw)
  To: John Reiser; +Cc: linux-kernel

On Fri, Feb 24, 2006 at 01:52:05PM -0800, John Reiser wrote:
> > Index: 2.6.16-rc4-inflate/lib/inflate.c
> > ===================================================================
> > --- 2.6.16-rc4-inflate.orig/lib/inflate.c	2006-02-22 17:16:07.000000000 -0600
> > +++ 2.6.16-rc4-inflate/lib/inflate.c	2006-02-22 17:16:08.000000000 -0600
> > @@ -117,12 +117,12 @@
> >     an unused code.  If a code with e == 99 is looked up, this implies an
> >     error in the data. */
> >  struct huft {
> > -	u8 e;			/* number of extra bits or operation */
> > -	u8 b;			/* number of bits in this code or subcode */
> >  	union {
> > -		u16 n;		/* literal, length base, or distance base */
> > -		struct huft *t;	/* pointer to next level of table */
> > -	} v;
> > +		u16 val; /* literal, length base, or distance base */
> > +		struct huft *next; /* pointer to next level of table */
> > +	};
> > +	u8 extra; /* number of extra bits or operation */
> > +	u8 bits; /* number of bits in this code or subcode */
> >  };
> 
> How aggressive do you want to be?  About 3.7 years ago in
> http://freshmeat.net/projects/gzip_x86/  I published:
> -----
> typedef unsigned short huft_ndx;  /* index>>1 */
> struct huft {
>   uch e;                /* number of extra bits or operation */
>   uch b;                /* number of bits in this code or subcode */
>   union {
>     ush n;              /* literal, length base, or distance base */
>     huft_ndx t;         /* index>>1 of next level of table */
>   } v;
> };
> -----
> which makes 4==sizeof(struct huft) and enables manipulation by 32-bit
> fetch, exposing 'e' [extra] and 'b' [bits] in x86 byte registers
> for fewer shift-and-mask operations.  The struct huft can be managed
> as a stack of maximum length 1014.

Interesting. The current stuff is mostly obvious cleanups, but I'd
consider something like that after I get the current queue merged.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-24 20:12 ` [PATCH 3/7] inflate pt1: clean up input logic Matt Mackall
@ 2006-02-24 22:19   ` Russell King
  2006-02-25  6:51     ` Matt Mackall
  0 siblings, 1 reply; 31+ messages in thread
From: Russell King @ 2006-02-24 22:19 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Fri, Feb 24, 2006 at 02:12:16PM -0600, Matt Mackall wrote:
> -static const u16 mask_bits[] = {
> -	0x0000,
> -	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
> -	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
> -};
> +static inline u32 readbits(u32 *b, u32 *k, int n)
> +{
> +	for( ; *k < n; *k += 8)
> +		*b |= (u32)get_byte() << *k;
> +	return *b & ((1 << n) - 1);
> +}
>  
> -#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (u8)v; })

How does this change handle the case where we run out of input data?
This condition needs to be handled explicitly because the inflate
functions can infinitely loop.

Relying on a bit pattern returned by get_byte() is how this code
pre-fix used to work, and it caused several confused bug reports.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-24 22:19   ` Russell King
@ 2006-02-25  6:51     ` Matt Mackall
  2006-02-25  8:49       ` Russell King
  0 siblings, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-25  6:51 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Fri, Feb 24, 2006 at 10:19:09PM +0000, Russell King wrote:
> On Fri, Feb 24, 2006 at 02:12:16PM -0600, Matt Mackall wrote:
> > -static const u16 mask_bits[] = {
> > -	0x0000,
> > -	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
> > -	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
> > -};
> > +static inline u32 readbits(u32 *b, u32 *k, int n)
> > +{
> > +	for( ; *k < n; *k += 8)
> > +		*b |= (u32)get_byte() << *k;
> > +	return *b & ((1 << n) - 1);
> > +}
> >  
> > -#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (u8)v; })
> 
> How does this change handle the case where we run out of input data?
> This condition needs to be handled explicitly because the inflate
> functions can infinitely loop.

And if you look at the current users, you'll see that only two of 15
actually use it.

> Relying on a bit pattern returned by get_byte() is how this code
> pre-fix used to work, and it caused several confused bug reports.

Just about everywhere, get_byte prints an error message and halts. In
the final refactoring, get_byte goes away and is replaced by a
->fill() method that's only called when the input buffer is emptied,
rather than byte by byte. Most of the users leave this null (since
they have the entire contents in memory already), which will give us a
nice oops. I can add another patch in the next batch that makes an
attempt to call a null ->fill instead do ->error("gunzip underrun").

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25  6:51     ` Matt Mackall
@ 2006-02-25  8:49       ` Russell King
  2006-02-25  8:55         ` Russell King
  2006-02-25 14:54         ` Matt Mackall
  0 siblings, 2 replies; 31+ messages in thread
From: Russell King @ 2006-02-25  8:49 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 12:51:36AM -0600, Matt Mackall wrote:
> On Fri, Feb 24, 2006 at 10:19:09PM +0000, Russell King wrote:
> > How does this change handle the case where we run out of input data?
> > This condition needs to be handled explicitly because the inflate
> > functions can infinitely loop.
> 
> And if you look at the current users, you'll see that only two of 15
> actually use it.

Sorry, I don't understand the relevance of your comment.

As the code stands in mainline, if we run out of input data, we are
guaranteed to exit from inflate.

With your change in this patch set, we no longer guaranteed to exit,
but will in some circumstances loop indefinitely.

The problem this causes is that if the ramdisk decompression runs out
of data, the kernel will just silently hang.

Please do not back out this fix.

> > Relying on a bit pattern returned by get_byte() is how this code
> > pre-fix used to work, and it caused several confused bug reports.
> 
> Just about everywhere, get_byte prints an error message and halts.

And the cases where it doesn't halt is the important case.

Sorry, but I hope that this code does not get merged as is.  It's
backing out a fix that I was involved in getting in, and therefore
I'm completely opposed to your code as it stands.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25  8:49       ` Russell King
@ 2006-02-25  8:55         ` Russell King
  2006-02-25  9:04           ` Andrew Morton
  2006-02-25 14:54         ` Matt Mackall
  1 sibling, 1 reply; 31+ messages in thread
From: Russell King @ 2006-02-25  8:55 UTC (permalink / raw)
  To: Matt Mackall, Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 08:49:55AM +0000, Russell King wrote:
> On Sat, Feb 25, 2006 at 12:51:36AM -0600, Matt Mackall wrote:
> > On Fri, Feb 24, 2006 at 10:19:09PM +0000, Russell King wrote:
> > > How does this change handle the case where we run out of input data?
> > > This condition needs to be handled explicitly because the inflate
> > > functions can infinitely loop.
> > 
> > And if you look at the current users, you'll see that only two of 15
> > actually use it.
> 
> Sorry, I don't understand the relevance of your comment.
> 
> As the code stands in mainline, if we run out of input data, we are
> guaranteed to exit from inflate.
> 
> With your change in this patch set, we no longer guaranteed to exit,
> but will in some circumstances loop indefinitely.
> 
> The problem this causes is that if the ramdisk decompression runs out
> of data, the kernel will just silently hang.
> 
> Please do not back out this fix.
> 
> > > Relying on a bit pattern returned by get_byte() is how this code
> > > pre-fix used to work, and it caused several confused bug reports.
> > 
> > Just about everywhere, get_byte prints an error message and halts.
> 
> And the cases where it doesn't halt is the important case.
> 
> Sorry, but I hope that this code does not get merged as is.  It's
> backing out a fix that I was involved in getting in, and therefore
> I'm completely opposed to your code as it stands.

FYI, here's the bk delta which introduced this fix.

http://linux.bkbits.net:8080/linux-2.6/diffs/lib/inflate.c@1.6?nav=index.html|src/.|src/lib|hist/lib/inflate.c

Of course, not having per-file comments in BK means that we can't get
at the cset comments which explain _why_ it is necessary.  Maybe akpm
keeps an archive of such things?

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25  8:55         ` Russell King
@ 2006-02-25  9:04           ` Andrew Morton
  2006-02-25  9:09             ` Russell King
  0 siblings, 1 reply; 31+ messages in thread
From: Andrew Morton @ 2006-02-25  9:04 UTC (permalink / raw)
  To: Russell King; +Cc: mpm, linux-kernel

Russell King <rmk+lkml@arm.linux.org.uk> wrote:
>
> FYI, here's the bk delta which introduced this fix.
> 
>  http://linux.bkbits.net:8080/linux-2.6/diffs/lib/inflate.c@1.6?nav=index.html|src/.|src/lib|hist/lib/inflate.c
> 
>  Of course, not having per-file comments in BK means that we can't get
>  at the cset comments which explain _why_ it is necessary.  Maybe akpm
>  keeps an archive of such things?

It's easier to just google for well-chosen hunks of the patch.

http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-12/4615.html

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25  9:04           ` Andrew Morton
@ 2006-02-25  9:09             ` Russell King
  0 siblings, 0 replies; 31+ messages in thread
From: Russell King @ 2006-02-25  9:09 UTC (permalink / raw)
  To: Andrew Morton; +Cc: mpm, linux-kernel

On Sat, Feb 25, 2006 at 01:04:36AM -0800, Andrew Morton wrote:
> Russell King <rmk+lkml@arm.linux.org.uk> wrote:
> >
> > FYI, here's the bk delta which introduced this fix.
> > 
> >  http://linux.bkbits.net:8080/linux-2.6/diffs/lib/inflate.c@1.6?nav=index.html|src/.|src/lib|hist/lib/inflate.c
> > 
> >  Of course, not having per-file comments in BK means that we can't get
> >  at the cset comments which explain _why_ it is necessary.  Maybe akpm
> >  keeps an archive of such things?
> 
> It's easier to just google for well-chosen hunks of the patch.
> 
> http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-12/4615.html

Ah, thanks.  I eventually found my original mail which gives a full
description of the problem:

http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25  8:49       ` Russell King
  2006-02-25  8:55         ` Russell King
@ 2006-02-25 14:54         ` Matt Mackall
  2006-02-25 18:05           ` Russell King
  1 sibling, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-25 14:54 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 08:49:55AM +0000, Russell King wrote:
> On Sat, Feb 25, 2006 at 12:51:36AM -0600, Matt Mackall wrote:
> > On Fri, Feb 24, 2006 at 10:19:09PM +0000, Russell King wrote:
> > > How does this change handle the case where we run out of input data?
> > > This condition needs to be handled explicitly because the inflate
> > > functions can infinitely loop.
> > 
> > And if you look at the current users, you'll see that only two of 15
> > actually use it.
> 
> Sorry, I don't understand the relevance of your comment.

The other 13 did the right thing, namely halt in get_byte. Without
adding a magic goto inside of a macro.

> Please do not back out this fix.

The backing out is only temporary, as I stated in my message. That
said, it should have never gone in.
 
> > > Relying on a bit pattern returned by get_byte() is how this code
> > > pre-fix used to work, and it caused several confused bug reports.
> > 
> > Just about everywhere, get_byte prints an error message and halts.
> 
> And the cases where it doesn't halt is the important case.

Again, current state of things. Did you read the rest of my message?

The end result is that it will halt in all cases. This code _will_not_
infinitely loop. Instead, it will dereference null or print a
diagnostic. I can add another patch to make sure it prints a nice
diagnostic in all cases if you care, but I don't think it's terribly
important.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 14:54         ` Matt Mackall
@ 2006-02-25 18:05           ` Russell King
  2006-02-25 21:04             ` Matt Mackall
  0 siblings, 1 reply; 31+ messages in thread
From: Russell King @ 2006-02-25 18:05 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 08:54:12AM -0600, Matt Mackall wrote:
> On Sat, Feb 25, 2006 at 08:49:55AM +0000, Russell King wrote:
> > On Sat, Feb 25, 2006 at 12:51:36AM -0600, Matt Mackall wrote:
> > > On Fri, Feb 24, 2006 at 10:19:09PM +0000, Russell King wrote:
> > > > How does this change handle the case where we run out of input data?
> > > > This condition needs to be handled explicitly because the inflate
> > > > functions can infinitely loop.
> > > 
> > > And if you look at the current users, you'll see that only two of 15
> > > actually use it.
> > 
> > Sorry, I don't understand the relevance of your comment.
> 
> The other 13 did the right thing, namely halt in get_byte. Without
> adding a magic goto inside of a macro.
> 
> > Please do not back out this fix.
> 
> The backing out is only temporary, as I stated in my message. That
> said, it should have never gone in.

Nevertheless, it's a wilful re-introduction of a real bug.

> > > > Relying on a bit pattern returned by get_byte() is how this code
> > > > pre-fix used to work, and it caused several confused bug reports.
> > > 
> > > Just about everywhere, get_byte prints an error message and halts.
> > 
> > And the cases where it doesn't halt is the important case.
> 
> Again, current state of things. Did you read the rest of my message?

And you're failing to see the problem.

> The end result is that it will halt in all cases. This code _will_not_
> infinitely loop. Instead, it will dereference null or print a
> diagnostic. I can add another patch to make sure it prints a nice
> diagnostic in all cases if you care, but I don't think it's terribly
> important.

Not acceptable.

We DO NOT want to halt in ALL cases.  There is ONE case where we
definitely do want to GRACEFULLY fail and _NOT_ halt the kernel.

It seems that you're missing this case - the case where lib/inflate.c
is used elsewhere in the kernel apart from the boot time decompressors.
The behaviour you describe is perfectly reasonable for the boot time
decompressors, but _NOT_ once the kernel has been decompressed.

Sorry, I'm disgusted that it's come to this to get my point across.
Do I really have to use capital letters in an attempt to convey the
point?

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 18:05           ` Russell King
@ 2006-02-25 21:04             ` Matt Mackall
  2006-02-25 21:22               ` Russell King
  0 siblings, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-25 21:04 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 06:05:21PM +0000, Russell King wrote:
> It seems that you're missing this case - the case where lib/inflate.c
> is used elsewhere in the kernel apart from the boot time decompressors.

I think you must be getting confused with lib/zlib. lib/inflate.c is
only used at boot.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:04             ` Matt Mackall
@ 2006-02-25 21:22               ` Russell King
  2006-02-25 21:47                 ` Matt Mackall
  2006-03-07 23:26                 ` H. Peter Anvin
  0 siblings, 2 replies; 31+ messages in thread
From: Russell King @ 2006-02-25 21:22 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 03:04:54PM -0600, Matt Mackall wrote:
> On Sat, Feb 25, 2006 at 06:05:21PM +0000, Russell King wrote:
> > It seems that you're missing this case - the case where lib/inflate.c
> > is used elsewhere in the kernel apart from the boot time decompressors.
> 
> I think you must be getting confused with lib/zlib. lib/inflate.c is
> only used at boot.

No I'm not.  Look:

$ grep -r '#include.*lib/inflate.c' [a-z]*
arch/alpha/boot/misc.c:#include "../../../lib/inflate.c"
arch/arm/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/arm26/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/cris/arch-v10/boot/compressed/misc.c:#include "../../../../../lib/inflate.c"
arch/cris/arch-v32/boot/compressed/misc.c:#include "../../../../../lib/inflate.c"
arch/i386/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/m32r/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/sh/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/sh64/boot/compressed/misc.c:#include "../../../../lib/inflate.c"
arch/x86_64/boot/compressed/misc.c:#include "../../../../lib/inflate.c"

All these are to do with decompressing a compressed kernel.  If they
fail, halting is perfectly reasonable because we probably don't have
an executable kernel.  Your arguments are fine for these.  But, that's
not the full story - there are two more places where this code is
used:

init/do_mounts_rd.c:#include "../lib/inflate.c"
init/initramfs.c:#include "../lib/inflate.c"

for these your arguments that halting is fine is _NOT_ correct nor is it
desirable.  The first of these is the cause of the problems both myself
and others saw, as detailed in the URL I posted previously in this thread.
Did you read that post?

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:22               ` Russell King
@ 2006-02-25 21:47                 ` Matt Mackall
  2006-02-25 21:58                   ` Russell King
  2006-02-25 22:25                   ` John Reiser
  2006-03-07 23:26                 ` H. Peter Anvin
  1 sibling, 2 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-25 21:47 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 09:22:48PM +0000, Russell King wrote:
> init/do_mounts_rd.c:#include "../lib/inflate.c"
> init/initramfs.c:#include "../lib/inflate.c"
> 
> for these your arguments that halting is fine is _NOT_ correct nor is it
> desirable.

If you have an argument for why we shouldn't halt on failed
init{rd,ramfs} decompression, I look forward to hearing it.

> Did you read that post?

This? http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html

"The firmware then calls the kernel decompressor, which dutifully
decompresses the image, and calls the kernel. This image ends up
getting corrupted at some point."

Is your argument that we shouldn't halt after encountering a corrupt
image?

In my mind, being unable to decompress init* is every bit as fatal as
being unable to mount root.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:47                 ` Matt Mackall
@ 2006-02-25 21:58                   ` Russell King
  2006-02-25 22:37                     ` Matt Mackall
  2006-02-25 22:25                   ` John Reiser
  1 sibling, 1 reply; 31+ messages in thread
From: Russell King @ 2006-02-25 21:58 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 03:47:04PM -0600, Matt Mackall wrote:
> On Sat, Feb 25, 2006 at 09:22:48PM +0000, Russell King wrote:
> > init/do_mounts_rd.c:#include "../lib/inflate.c"
> > init/initramfs.c:#include "../lib/inflate.c"
> > 
> > for these your arguments that halting is fine is _NOT_ correct nor is it
> > desirable.
> 
> If you have an argument for why we shouldn't halt on failed
> init{rd,ramfs} decompression, I look forward to hearing it.
> 
> > Did you read that post?
> 
> This? http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html

Yes, that.

> "The firmware then calls the kernel decompressor, which dutifully
> decompresses the image, and calls the kernel. This image ends up
> getting corrupted at some point."
> 
> Is your argument that we shouldn't halt after encountering a corrupt
> image?

You're getting very confused.

1. kernel is loaded.
2. firmware scans loaded kernel, finds gzip magic numbers (the compressed
   kernel.)
3. firmware sets initrd pointeres to point at the compressed kernel.
4. firmware calls kernel decompressor.
5. kernel decompresses and self-relocates.
6. compressed kernel image is thereby partly corrupted.
7. kernel boots.
8. kernel tries to decompress the compressed kernel image.
9. decompressor gets confused and tries to gobble more data than is
   available.
10. kernel sits there being a dumb fuck.

> In my mind, being unable to decompress init* is every bit as fatal as
> being unable to mount root.

It's very simple.  With fix, the kernel successfully boots on these
machines.  Without fix, the kernel hangs on these machines for _no_
good reason other than the firmware did something that was stupid.

I'm sorry, I just do not see why you're being soo bloody difficult
over this.

The buggest bloody thing about this is that _lots_ of time was wasted
on working out what the fuck was going on.  I have no intention of
allowing any supposed "cleanup" to return us to the days where we have
to go though such crap again - and I will keep bitching about it until
the message gets through.

As far as I'm concerned, we're better off keeping the existing code if
this is the extent to which folk have to go to in order to preserve
needed bug fixes.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:47                 ` Matt Mackall
  2006-02-25 21:58                   ` Russell King
@ 2006-02-25 22:25                   ` John Reiser
  1 sibling, 0 replies; 31+ messages in thread
From: John Reiser @ 2006-02-25 22:25 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

Matt Mackall wrote:
> On Sat, Feb 25, 2006 at 09:22:48PM +0000, Russell King wrote:
> 
>>init/do_mounts_rd.c:#include "../lib/inflate.c"
>>init/initramfs.c:#include "../lib/inflate.c"
>>
>>for these your arguments that halting is fine is _NOT_ correct nor is it
>>desirable.
> 
> 
> If you have an argument for why we shouldn't halt on failed
> init{rd,ramfs} decompression, I look forward to hearing it.

It depends on the nature of the error, and which parts were decompressed
successfully.  Gzip has optional "re-sync" capability, and ideally a
decompression failure might invoke some kind of bad-block tagging
for the output instead of halting the machine.  Some init{rd,ramfs}
have all the network drivers, all the SCSI drivers, all the sound
drivers, etc., but the user may care only about those for the current
machine.  Other init{rd,ramfs} contain only "essential" pieces.
Even then, the pieces that are deemed more important can be at the
beginning.  It might be possible to work without a sound driver,
but perhaps not without a SCSI driver.

> In my mind, being unable to decompress init* is every bit as fatal as
> being unable to mount root.

It may be possible to recover, at least partially, from one error
more than from another.  It would be nice if the decompressor itself
was a minor influence instead of a major one.

-- 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:58                   ` Russell King
@ 2006-02-25 22:37                     ` Matt Mackall
  2006-02-25 22:57                       ` Russell King
  0 siblings, 1 reply; 31+ messages in thread
From: Matt Mackall @ 2006-02-25 22:37 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 09:58:50PM +0000, Russell King wrote:
> On Sat, Feb 25, 2006 at 03:47:04PM -0600, Matt Mackall wrote:
> > On Sat, Feb 25, 2006 at 09:22:48PM +0000, Russell King wrote:
> > > init/do_mounts_rd.c:#include "../lib/inflate.c"
> > > init/initramfs.c:#include "../lib/inflate.c"
> > > 
> > > for these your arguments that halting is fine is _NOT_ correct nor is it
> > > desirable.
> > 
> > If you have an argument for why we shouldn't halt on failed
> > init{rd,ramfs} decompression, I look forward to hearing it.
> > 
> > > Did you read that post?
> > 
> > This? http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> 
> Yes, that.
> 
> > "The firmware then calls the kernel decompressor, which dutifully
> > decompresses the image, and calls the kernel. This image ends up
> > getting corrupted at some point."
> > 
> > Is your argument that we shouldn't halt after encountering a corrupt
> > image?
> 
> You're getting very confused.
> 
> 1. kernel is loaded.
> 2. firmware scans loaded kernel, finds gzip magic numbers (the compressed
>    kernel.)
> 3. firmware sets initrd pointeres to point at the compressed kernel.
> 4. firmware calls kernel decompressor.
> 5. kernel decompresses and self-relocates.
> 6. compressed kernel image is thereby partly corrupted.
> 7. kernel boots.
> 8. kernel tries to decompress the compressed kernel image.
> 9. decompressor gets confused and tries to gobble more data than is
>    available.
> 10. kernel sits there being a dumb fuck.

Why are we attempting to decompress the kernel image again? Accident?

> > In my mind, being unable to decompress init* is every bit as fatal as
> > being unable to mount root.
> 
> It's very simple.  With fix, the kernel successfully boots on these
> machines.  Without fix, the kernel hangs on these machines for _no_
> good reason other than the firmware did something that was stupid.

And how does this work currently? We attempt to decompress the kernel
a second time, give up and move on?

Assuming we can write to the compressed image (and thereby corrupt
it), wouldn't it be better to just stomp on the gzip magic and spare
us the overhead of decompressing it twice?

> I'm sorry, I just do not see why you're being soo bloody difficult
> over this.

Because it's taken this long to get close to an explanation of what
the problem is.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 22:37                     ` Matt Mackall
@ 2006-02-25 22:57                       ` Russell King
  2006-02-27  1:18                         ` Johannes Stezenbach
  2006-02-27  9:06                         ` Matt Mackall
  0 siblings, 2 replies; 31+ messages in thread
From: Russell King @ 2006-02-25 22:57 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 04:37:37PM -0600, Matt Mackall wrote:
> On Sat, Feb 25, 2006 at 09:58:50PM +0000, Russell King wrote:
> > 1. kernel is loaded.
> > 2. firmware scans loaded kernel, finds gzip magic numbers (the compressed
> >    kernel.)
> > 3. firmware sets initrd pointeres to point at the compressed kernel.
> > 4. firmware calls kernel decompressor.
> > 5. kernel decompresses and self-relocates.
> > 6. compressed kernel image is thereby partly corrupted.
> > 7. kernel boots.
> > 8. kernel tries to decompress the compressed kernel image.
> > 9. decompressor gets confused and tries to gobble more data than is
> >    available.
> > 10. kernel sits there being a dumb fuck.
> 
> Why are we attempting to decompress the kernel image again? Accident?

The firmware is trying to be "clever" - looking in the object it TFTP'd
for the gzip magic numbers and assuming that any it finds are an initrd.
The firmware then points the kernel at the start of that gzipped image.

The fact that a zImage contains the gzip magic numbers never occurred to
the people who implemented this misfeature in the firmware.  It is a
misfeature because:

1. this exact problem - that a zImage contents can be mistaken for an initrd.
2. an initrd doesn't have to be compressed with gzip.
3. an initrd may contain gzip markers which do not relate to the start of
   the initrd.

> > > In my mind, being unable to decompress init* is every bit as fatal as
> > > being unable to mount root.
> > 
> > It's very simple.  With fix, the kernel successfully boots on these
> > machines.  Without fix, the kernel hangs on these machines for _no_
> > good reason other than the firmware did something that was stupid.
> 
> And how does this work currently? We attempt to decompress the kernel
> a second time, give up and move on?

Yes.

> Assuming we can write to the compressed image (and thereby corrupt
> it), wouldn't it be better to just stomp on the gzip magic and spare
> us the overhead of decompressing it twice?

That's not guaranteed, so is impossible to do in the boot time
decompressor.

> > I'm sorry, I just do not see why you're being soo bloody difficult
> > over this.
> 
> Because it's taken this long to get close to an explanation of what
> the problem is.

$#%@%#$%#@!!!  I really think you're intentionally trying to wind me up
through this whole thread.

The email:

  http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html

contains a full and clear explaination of the situation.  The second
paragraph of that email is key to understanding the problem and makes
it absolutely clear what is trying to be decompressed as the initrd
(the corrupted compressed piggy).

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 22:57                       ` Russell King
@ 2006-02-27  1:18                         ` Johannes Stezenbach
  2006-02-27  8:32                           ` Russell King
  2006-02-27  9:06                         ` Matt Mackall
  1 sibling, 1 reply; 31+ messages in thread
From: Johannes Stezenbach @ 2006-02-27  1:18 UTC (permalink / raw)
  To: Russell King; +Cc: Matt Mackall, Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 10:57:49PM +0000, Russell King wrote:
> On Sat, Feb 25, 2006 at 04:37:37PM -0600, Matt Mackall wrote:
> > On Sat, Feb 25, 2006 at 09:58:50PM +0000, Russell King wrote:
> > > I'm sorry, I just do not see why you're being soo bloody difficult
> > > over this.
> > 
> > Because it's taken this long to get close to an explanation of what
> > the problem is.
> 
> $#%@%#$%#@!!!  I really think you're intentionally trying to wind me up
> through this whole thread.
> 
> The email:
> 
>   http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> 
> contains a full and clear explaination of the situation.  The second
> paragraph of that email is key to understanding the problem and makes
> it absolutely clear what is trying to be decompressed as the initrd
> (the corrupted compressed piggy).

FWIW, I didn't it either. "Work around broken boot firmware which passes
invalid initrd to kernel" would have been a simpler description.

I agree that it would be nice if inflate.c would fail gracefully
instead of halting, but why can't you just use CONFIG_BLK_DEV_INITRD=n?


Johannes

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-27  1:18                         ` Johannes Stezenbach
@ 2006-02-27  8:32                           ` Russell King
  2006-02-27 12:07                             ` Johannes Stezenbach
  0 siblings, 1 reply; 31+ messages in thread
From: Russell King @ 2006-02-27  8:32 UTC (permalink / raw)
  To: Johannes Stezenbach, Matt Mackall, Andrew Morton, linux-kernel

On Mon, Feb 27, 2006 at 02:18:44AM +0100, Johannes Stezenbach wrote:
> On Sat, Feb 25, 2006 at 10:57:49PM +0000, Russell King wrote:
> > The email:
> > 
> >   http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> > 
> > contains a full and clear explaination of the situation.  The second
> > paragraph of that email is key to understanding the problem and makes
> > it absolutely clear what is trying to be decompressed as the initrd
> > (the corrupted compressed piggy).
> 
> FWIW, I didn't it either. "Work around broken boot firmware which passes
> invalid initrd to kernel" would have been a simpler description.

Sigh, I'm sick of this crap.  I'm not going to debate it any further.

> I agree that it would be nice if inflate.c would fail gracefully
> instead of halting,

IT _DOES_ FAIL GRACEFULLY TODAY.  WITH MATT'S PATCHES, IT _DOESN'T_.
THAT'S A REGRESSION.  WHAT IS IT ABOUT THAT WHICH PEOPLE DON'T
UNDERSTAND?  DO I HAVE TO SPELL IT OUT IN ONE SYLLABLE WORDS?

> but why can't you just use CONFIG_BLK_DEV_INITRD=n?

Because you might want to use an initrd for real (for installation
purposes) and therefore distributions (eg Debian) want it turned on?

Okay, this does it - I'm ignoring further discussion on this stupid
idiotic topic which is soo bloody difficult for others to understand.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 22:57                       ` Russell King
  2006-02-27  1:18                         ` Johannes Stezenbach
@ 2006-02-27  9:06                         ` Matt Mackall
  1 sibling, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-02-27  9:06 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel

On Sat, Feb 25, 2006 at 10:57:49PM +0000, Russell King wrote:
> On Sat, Feb 25, 2006 at 04:37:37PM -0600, Matt Mackall wrote:
> > On Sat, Feb 25, 2006 at 09:58:50PM +0000, Russell King wrote:
> > > 1. kernel is loaded.
> > > 2. firmware scans loaded kernel, finds gzip magic numbers (the compressed
> > >    kernel.)
> > > 3. firmware sets initrd pointeres to point at the compressed kernel.
> > > 4. firmware calls kernel decompressor.
> > > 5. kernel decompresses and self-relocates.
> > > 6. compressed kernel image is thereby partly corrupted.
> > > 7. kernel boots.
> > > 8. kernel tries to decompress the compressed kernel image.
> > > 9. decompressor gets confused and tries to gobble more data than is
> > >    available.
> > > 10. kernel sits there being a dumb fuck.
> > 
> > Why are we attempting to decompress the kernel image again? Accident?
> 
> The firmware is trying to be "clever" - looking in the object it TFTP'd
> for the gzip magic numbers and assuming that any it finds are an initrd.
> The firmware then points the kernel at the start of that gzipped image.
> 
> The fact that a zImage contains the gzip magic numbers never occurred to
> the people who implemented this misfeature in the firmware.  It is a
> misfeature because:
> 
> 1. this exact problem - that a zImage contents can be mistaken for an initrd.
> 2. an initrd doesn't have to be compressed with gzip.
> 3. an initrd may contain gzip markers which do not relate to the start of
>    the initrd.
> 
> > > > In my mind, being unable to decompress init* is every bit as fatal as
> > > > being unable to mount root.
> > > 
> > > It's very simple.  With fix, the kernel successfully boots on these
> > > machines.  Without fix, the kernel hangs on these machines for _no_
> > > good reason other than the firmware did something that was stupid.
> > 
> > And how does this work currently? We attempt to decompress the kernel
> > a second time, give up and move on?
> 
> Yes.
> 
> > Assuming we can write to the compressed image (and thereby corrupt
> > it), wouldn't it be better to just stomp on the gzip magic and spare
> > us the overhead of decompressing it twice?
> 
> That's not guaranteed, so is impossible to do in the boot time
> decompressor.
> 
> > > I'm sorry, I just do not see why you're being soo bloody difficult
> > > over this.
> > 
> > Because it's taken this long to get close to an explanation of what
> > the problem is.
> 
> $#%@%#$%#@!!!  I really think you're intentionally trying to wind me up
> through this whole thread.
>
> The email:
> 
>   http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> 
> contains a full and clear explaination of the situation.  The second
> paragraph of that email is key to understanding the problem and makes
> it absolutely clear what is trying to be decompressed as the initrd
> (the corrupted compressed piggy).

It was neither full nor clear to me at least. But the above clarifies
it enough that I understand what all the fuss is about, thanks. I'm
back to the drawing board; I'll add an underflow path back in.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-27  8:32                           ` Russell King
@ 2006-02-27 12:07                             ` Johannes Stezenbach
  2006-02-27 15:47                               ` Russell King
  0 siblings, 1 reply; 31+ messages in thread
From: Johannes Stezenbach @ 2006-02-27 12:07 UTC (permalink / raw)
  To: Russell King; +Cc: Matt Mackall, Andrew Morton, linux-kernel

On Mon, Feb 27, 2006, Russell King wrote:
> On Mon, Feb 27, 2006 at 02:18:44AM +0100, Johannes Stezenbach wrote:
> > On Sat, Feb 25, 2006 at 10:57:49PM +0000, Russell King wrote:
> > > The email:
> > > 
> > >   http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> > > 
> > > contains a full and clear explaination of the situation.  The second
> > > paragraph of that email is key to understanding the problem and makes
> > > it absolutely clear what is trying to be decompressed as the initrd
> > > (the corrupted compressed piggy).
> > 
> > FWIW, I didn't it either. "Work around broken boot firmware which passes
> > invalid initrd to kernel" would have been a simpler description.
> 
> Sigh, I'm sick of this crap.  I'm not going to debate it any further.
> 
> > I agree that it would be nice if inflate.c would fail gracefully
> > instead of halting,
> 
> IT _DOES_ FAIL GRACEFULLY TODAY.  WITH MATT'S PATCHES, IT _DOESN'T_.
> THAT'S A REGRESSION.  WHAT IS IT ABOUT THAT WHICH PEOPLE DON'T
> UNDERSTAND?  DO I HAVE TO SPELL IT OUT IN ONE SYLLABLE WORDS?

I got that already, no need to shout. I just wanted to point
out that from the information you provided so far it
looks like your problem could be fixed in a more straight
forward fashion.

Problem:  Boot firmware passes invalid arguments.
Solution: Ignore invalid boot firmware arguments.

> > but why can't you just use CONFIG_BLK_DEV_INITRD=n?
> 
> Because you might want to use an initrd for real (for installation
> purposes) and therefore distributions (eg Debian) want it turned on?

If you use a distribution kernel which contains one, you
could simply add "noinitrd" to the kernel command line
to ignore it, no?

> Okay, this does it - I'm ignoring further discussion on this stupid
> idiotic topic which is soo bloody difficult for others to understand.

I don't understand your aggressiveness, there must be a dark
secret behind all this. Or maybe it's just the season
for flame wars.


I'm sorry to have bothered you,
Johannes

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-27 12:07                             ` Johannes Stezenbach
@ 2006-02-27 15:47                               ` Russell King
  0 siblings, 0 replies; 31+ messages in thread
From: Russell King @ 2006-02-27 15:47 UTC (permalink / raw)
  To: Johannes Stezenbach, Matt Mackall, Andrew Morton, linux-kernel

On Mon, Feb 27, 2006 at 01:07:29PM +0100, Johannes Stezenbach wrote:
> On Mon, Feb 27, 2006, Russell King wrote:
> > On Mon, Feb 27, 2006 at 02:18:44AM +0100, Johannes Stezenbach wrote:
> > > On Sat, Feb 25, 2006 at 10:57:49PM +0000, Russell King wrote:
> > > > The email:
> > > > 
> > > >   http://www.ussg.iu.edu/hypermail/linux/kernel/0312.2/1024.html
> > > > 
> > > > contains a full and clear explaination of the situation.  The second
> > > > paragraph of that email is key to understanding the problem and makes
> > > > it absolutely clear what is trying to be decompressed as the initrd
> > > > (the corrupted compressed piggy).
> > > 
> > > FWIW, I didn't it either. "Work around broken boot firmware which passes
> > > invalid initrd to kernel" would have been a simpler description.
> > 
> > Sigh, I'm sick of this crap.  I'm not going to debate it any further.
> > 
> > > I agree that it would be nice if inflate.c would fail gracefully
> > > instead of halting,
> > 
> > IT _DOES_ FAIL GRACEFULLY TODAY.  WITH MATT'S PATCHES, IT _DOESN'T_.
> > THAT'S A REGRESSION.  WHAT IS IT ABOUT THAT WHICH PEOPLE DON'T
> > UNDERSTAND?  DO I HAVE TO SPELL IT OUT IN ONE SYLLABLE WORDS?
> 
> I got that already, no need to shout. I just wanted to point
> out that from the information you provided so far it
> looks like your problem could be fixed in a more straight
> forward fashion.
> 
> Problem:  Boot firmware passes invalid arguments.
> Solution: Ignore invalid boot firmware arguments.

Let me try to explain - but I doubt it'll do any good because folk don't
seem to understand plain English here anymore (or at least that's what
it seems like from _my_ perspective.)

In order to detect that the arguments are invalid, you'd need to validate
the initrd.

In order to validate a compressed initrd, you'd have to trial-inflate it,
just like gunzip -t.

(a) gunzip -t is able to work because it has setjmp/longjmp, so when it
    runs out of data, it can sanely exit from the data reading function
    when it encounters insufficient data.

    The kernel does not have such functionality, and it has been determined
    long ago that the kernel shall not have such functionality - it was
    discussed at the time when this problem first came up and the resounding
    answer was precisely as I state.

(b) if we have to have separate code to validate a compressed image, that is
    a complete waste of code and resources - we already have something which
    tests whether a compressed image is valid by inflating it - called
    lib/inflate.c.

So, your suggestion isn't a really a solution when the simple solution
is to keep the original _simple_ fix for a buggy integration of the gzip
inflate code.

> > > but why can't you just use CONFIG_BLK_DEV_INITRD=n?
> > 
> > Because you might want to use an initrd for real (for installation
> > purposes) and therefore distributions (eg Debian) want it turned on?
> 
> If you use a distribution kernel which contains one, you
> could simply add "noinitrd" to the kernel command line
> to ignore it, no?

Tell that to all the people who have complained in the past about it.

> > Okay, this does it - I'm ignoring further discussion on this stupid
> > idiotic topic which is soo bloody difficult for others to understand.
> 
> I don't understand your aggressiveness, there must be a dark
> secret behind all this. Or maybe it's just the season
> for flame wars.

I'm completely and utterly pissed off with this thread, having to almost
go back to kindergarten type explainations to get the point across.
_That's_ what has been soo infuriating about this whole saga.

And what's even more stupid is the attitude that required fixes can be
thrown out of the kernel, and then a massive argument is required to
re-explain wtf they're necessary.

Are we doomed to have to repeatedly explain why bug fixes are necessary?
If that's the case, let's pack up this Linux kernel thing because we're
on a route to insanity.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-02-25 21:22               ` Russell King
  2006-02-25 21:47                 ` Matt Mackall
@ 2006-03-07 23:26                 ` H. Peter Anvin
  2006-03-10 18:55                   ` Matt Mackall
  1 sibling, 1 reply; 31+ messages in thread
From: H. Peter Anvin @ 2006-03-07 23:26 UTC (permalink / raw)
  To: linux-kernel

Followup to:  <20060225212247.GC15276@flint.arm.linux.org.uk>
By author:    Russell King <rmk+lkml@arm.linux.org.uk>
In newsgroup: linux.dev.kernel
> 
> All these are to do with decompressing a compressed kernel.  If they
> fail, halting is perfectly reasonable because we probably don't have
> an executable kernel.  Your arguments are fine for these.  But, that's
> not the full story - there are two more places where this code is
> used:
> 
> init/do_mounts_rd.c:#include "../lib/inflate.c"
> init/initramfs.c:#include "../lib/inflate.c"
> 
> for these your arguments that halting is fine is _NOT_ correct nor is it
> desirable.  The first of these is the cause of the problems both myself
> and others saw, as detailed in the URL I posted previously in this thread.
> Did you read that post?
> 

These probably should use lib/zlib instead...

	-hpa

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 3/7] inflate pt1: clean up input logic
  2006-03-07 23:26                 ` H. Peter Anvin
@ 2006-03-10 18:55                   ` Matt Mackall
  0 siblings, 0 replies; 31+ messages in thread
From: Matt Mackall @ 2006-03-10 18:55 UTC (permalink / raw)
  To: H. Peter Anvin; +Cc: linux-kernel

On Tue, Mar 07, 2006 at 03:26:05PM -0800, H. Peter Anvin wrote:
> Followup to:  <20060225212247.GC15276@flint.arm.linux.org.uk>
> By author:    Russell King <rmk+lkml@arm.linux.org.uk>
> In newsgroup: linux.dev.kernel
> > 
> > All these are to do with decompressing a compressed kernel.  If they
> > fail, halting is perfectly reasonable because we probably don't have
> > an executable kernel.  Your arguments are fine for these.  But, that's
> > not the full story - there are two more places where this code is
> > used:
> > 
> > init/do_mounts_rd.c:#include "../lib/inflate.c"
> > init/initramfs.c:#include "../lib/inflate.c"
> > 
> > for these your arguments that halting is fine is _NOT_ correct nor is it
> > desirable.  The first of these is the cause of the problems both myself
> > and others saw, as detailed in the URL I posted previously in this thread.
> > Did you read that post?
> > 
> 
> These probably should use lib/zlib instead...

That's a reasonable suggestion. I have an eventual goal of getting
down to only one (cleaned up) zlib instance in the kernel and I'm
somewhat more appalled by the lib/zlib/inflate code so I haven't gone
that route.

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2006-03-10 18:56 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <0.399206195@selenic.com>
2006-02-24 20:12 ` [PATCH 3/7] inflate pt1: clean up input logic Matt Mackall
2006-02-24 22:19   ` Russell King
2006-02-25  6:51     ` Matt Mackall
2006-02-25  8:49       ` Russell King
2006-02-25  8:55         ` Russell King
2006-02-25  9:04           ` Andrew Morton
2006-02-25  9:09             ` Russell King
2006-02-25 14:54         ` Matt Mackall
2006-02-25 18:05           ` Russell King
2006-02-25 21:04             ` Matt Mackall
2006-02-25 21:22               ` Russell King
2006-02-25 21:47                 ` Matt Mackall
2006-02-25 21:58                   ` Russell King
2006-02-25 22:37                     ` Matt Mackall
2006-02-25 22:57                       ` Russell King
2006-02-27  1:18                         ` Johannes Stezenbach
2006-02-27  8:32                           ` Russell King
2006-02-27 12:07                             ` Johannes Stezenbach
2006-02-27 15:47                               ` Russell King
2006-02-27  9:06                         ` Matt Mackall
2006-02-25 22:25                   ` John Reiser
2006-03-07 23:26                 ` H. Peter Anvin
2006-03-10 18:55                   ` Matt Mackall
2006-02-24 20:12 ` [PATCH 1/7] inflate pt1: lindent and manual formatting changes Matt Mackall
2006-02-24 20:12 ` [PATCH 4/7] inflate pt1: start moving globals into iostate Matt Mackall
2006-02-24 20:12 ` [PATCH 2/7] inflate pt1: kill legacy bits Matt Mackall
2006-02-24 20:12 ` [PATCH 6/7] inflate pt1: internalize CRC calculation, cleanup table calculation Matt Mackall
2006-02-24 20:12 ` [PATCH 5/7] inflate pt1: cleanup Huffman table code Matt Mackall
2006-02-24 21:52   ` John Reiser
2006-02-24 22:06     ` Matt Mackall
2006-02-24 20:12 ` [PATCH 7/7] inflate pt1: eliminate memzero usage Matt Mackall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).