[PATCH 20/20] inflate: make in-core inflate share common CRC

From: Matt Mackall
Date: Thu Dec 22 2005 - 13:28:21 EST


inflate: make in-core inflate share common CRC code

Signed-off-by: Matt Mackall <mpm@xxxxxxxxxxx>

Index: 2.6.14-inflate/lib/inflate.c
===================================================================
--- 2.6.14-inflate.orig/lib/inflate.c 2005-12-21 21:19:36.000000000 -0600
+++ 2.6.14-inflate/lib/inflate.c 2005-12-21 21:20:51.000000000 -0600
@@ -142,24 +142,46 @@ static void free(void *where)

static u8 window[0x8000]; /* use a statically allocated window */

+/* simple CRC calculation */
+static u32 crc_32_tab[256];
+#define CRCPOLY_LE 0xedb88320
+
#define INIT
#define INITDATA const

+static void makecrc(void)
+{
+ unsigned i, j;
+ u32 c = 1;
+
+ 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];
+ }
+}
+
+static u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
+{
+ while (len--)
+ crc = crc_32_tab[(*p++ ^ crc) & 0xff] ^ (crc >> 8);
+
+ return crc;
+}
#else

#include <linux/module.h>
+#include <linux/crc32.h>

static u8 *window; /* dynamically allocate */
#define malloc(a) kmalloc(a, GFP_KERNEL)
#define free(a) kfree(a)
+#define makecrc()
#define INIT __init
#define INITDATA __initdata

#endif

-static u32 crc_32_tab[256];
-#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
@@ -204,12 +226,7 @@ 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);
-
+ io->crc = crc32_le(io->crc, io->window, io->opos);
io->flush(io->window, io->opos);
io->ototal += io->opos;
io->opos = 0;
@@ -905,24 +922,6 @@ static int INIT inflate(struct iostate *
return 0;
}

-/**********************************************************************
- *
- * The following are support routines for inflate.c
- *
- **********************************************************************/
-
-static void INIT makecrc(void)
-{
- unsigned i, j;
- u32 c = 1;
-
- 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];
- }
-}
-
/* gzip flag byte */
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/