Drop in tinflate lib.
authorTobias Diedrich <ranma+coreboot@tdiedrich.de>
Tue, 20 Oct 2015 22:20:35 +0000 (00:20 +0200)
committerTobias Diedrich <ranma+coreboot@tdiedrich.de>
Tue, 20 Oct 2015 22:25:44 +0000 (00:25 +0200)
Adds compatibility-macro for crc32.
Setting first arg (crc) to anything but 0 is not supported, but all
callers already conform to this.
This is asserted at compile time through macro trickery.

Reduces uncompressed code size by ~1KB:
Without this change: Data Size: 127348 Bytes = 124.36 kB = 0.12 MB
After this change: Data Size: 126084 Bytes = 123.13 kB = 0.12 MB

u-boot/include/common.h
u-boot/include/crc32.h [deleted file]
u-boot/include/tinf.h [new file with mode: 0644]
u-boot/lib_generic/crc32.c
u-boot/lib_generic/tinfcrc32.c [new file with mode: 0644]
u-boot/lib_generic/tinfgzip.c [new file with mode: 0644]
u-boot/lib_generic/tinflate.c [new file with mode: 0644]
u-boot/tools/mkimage.c

index 652e731ed296c824aa6d932c016015cf6191b3f8..3ff025531fc9c821bcbe280bf9076c78bffa73d3 100644 (file)
@@ -535,8 +535,9 @@ int sprintf(char * buf, const char *fmt, ...);
 int    vsprintf(char *buf, const char *fmt, va_list args);
 
 /* lib_generic/crc32.c */
-ulong crc32 (ulong, const unsigned char *, uint);
-ulong crc32_no_comp (ulong, const unsigned char *, uint);
+/* Hack: Forces compilation failure if must_be_zero is not zero. */
+#define crc32(must_be_zero, buf, len) \
+       tinf_crc32(buf, len*(sizeof(char[1 - 2*!!(must_be_zero)])))
 
 /* common/console.c */
 int    console_init_f(void);   /* Before relocation; uses the serial  stuff    */
diff --git a/u-boot/include/crc32.h b/u-boot/include/crc32.h
deleted file mode 100644 (file)
index 2406d95..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef _CRC32_H_
-#define _CRC32_H_
-
-extern unsigned long crc32 (unsigned long crc, const unsigned char *buf, unsigned int len);
-
-#endif /* _CRC32_H_ */
diff --git a/u-boot/include/tinf.h b/u-boot/include/tinf.h
new file mode 100644 (file)
index 0000000..1dc1666
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * tinf  -  tiny inflate library (inflate, gzip, zlib)
+ *
+ * version 1.00
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ */
+
+#ifndef TINF_H_INCLUDED
+#define TINF_H_INCLUDED
+
+/* calling convention */
+#ifndef TINFCC
+ #ifdef __WATCOMC__
+  #define TINFCC __cdecl
+ #else
+  #define TINFCC
+ #endif
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define TINF_OK             0
+#define TINF_DATA_ERROR    (-3)
+
+/* function prototypes */
+
+void TINFCC tinf_init();
+
+int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
+                           const void *source, unsigned int sourceLen);
+
+int TINFCC tinf_gzip_uncompress(void *dest, unsigned int *destLen,
+                                const void *source, unsigned int sourceLen);
+
+int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen,
+                                const void *source, unsigned int sourceLen);
+
+unsigned int TINFCC tinf_adler32(const void *data, unsigned int length);
+
+unsigned int TINFCC tinf_crc32(const void *data, unsigned int length);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* TINF_H_INCLUDED */
index a7f20d6dd933523494402d5d78df4b055c89348a..fce7d338971fce3130be6a36db003b13dcf061a7 100644 (file)
 /*
- * This file is derived from crc32.c from the zlib-1.1.3 distribution
- * by Jean-loup Gailly and Mark Adler.
+ * CRC32 checksum
+ *
+ * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty.  In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ *    misrepresented; you must not claim that you
+ *    wrote the original software. If you use this
+ *    software in a product, an acknowledgment in
+ *    the product documentation would be appreciated
+ *    but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ *    as such, and must not be misrepresented as
+ *    being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ *    any source distribution.
  */
 
-/* crc32.c -- compute the CRC-32 of a data stream
- * Copyright (C) 1995-1998 Mark Adler
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
-
-#ifndef USE_HOSTCC     /* Shut down "ANSI does not permit..." warnings */
-#include <common.h>    /* to get command definitions like CFG_CMD_JFFS2 */
-#endif
-
-#include "crc32.h"
-
-#define local static
-#define ZEXPORT        /* empty */
-#define uLong unsigned long
-#define uLongf uLong
-#define uInt unsigned int
-#define Bytef unsigned char
-unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
-
-#ifdef DYNAMIC_CRC_TABLE
-
-local int crc_table_empty = 1;
-local uLongf crc_table[256];
-local void make_crc_table OF((void));
-
 /*
-  Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
-  x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
-
-  Polynomials over GF(2) are represented in binary, one bit per coefficient,
-  with the lowest powers in the most significant bit.  Then adding polynomials
-  is just exclusive-or, and multiplying a polynomial by x is a right shift by
-  one.  If we call the above polynomial p, and represent a byte as the
-  polynomial q, also with the lowest power in the most significant bit (so the
-  byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
-  where a mod b means the remainder after dividing a by b.
-
-  This calculation is done using the shift-register method of multiplying and
-  taking the remainder.  The register is initialized to zero, and for each
-  incoming bit, x^32 is added mod p to the register if the bit is a one (where
-  x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
-  x (which is shifting right by one and adding x^32 mod p if the bit shifted
-  out is a one).  We start with the highest power (least significant bit) of
-  q and repeat for all eight bits of q.
-
-  The table is simply the CRC of all possible eight bit values.  This is all
-  the information needed to generate CRC's on data a byte at a time for all
-  combinations of CRC register values and incoming bytes.
-*/
-local void make_crc_table()
-{
-  uLong c;
-  int n, k;
-  uLong poly;            /* polynomial exclusive-or pattern */
-  /* terms of polynomial defining this crc (except x^32): */
-  static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+ * CRC32 algorithm taken from the zlib source, which is
+ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
+ */
 
-  /* make exclusive-or pattern from polynomial (0xedb88320L) */
-  poly = 0L;
-  for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
-    poly |= 1L << (31 - p[n]);
+#include "tinf.h"
 
-  for (n = 0; n < 256; n++)
-  {
-    c = (uLong)n;
-    for (k = 0; k < 8; k++)
-      c = c & 1 ? poly ^ (c >> 1) : c >> 1;
-    crc_table[n] = c;
-  }
-  crc_table_empty = 0;
-}
-#else
-/* ========================================================================
- * Table of CRC-32's of all single-byte values (made by make_crc_table)
- */
-local const uLongf crc_table[256] = {
-  0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
-  0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
-  0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
-  0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
-  0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
-  0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
-  0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
-  0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
-  0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
-  0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
-  0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
-  0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
-  0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
-  0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
-  0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
-  0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
-  0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
-  0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
-  0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
-  0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
-  0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
-  0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
-  0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
-  0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
-  0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
-  0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
-  0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
-  0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
-  0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
-  0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
-  0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
-  0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
-  0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
-  0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
-  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
-  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
-  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
-  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
-  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
-  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
-  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
-  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
-  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
-  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
-  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
-  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
-  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
-  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
-  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
-  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
-  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
-  0x2d02ef8dL
+static const unsigned int tinf_crc32tab[16] = {
+   0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190,
+   0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344,
+   0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278,
+   0xbdbdf21c
 };
-#endif
 
-#if 0
-/* =========================================================================
- * This function can be used by asm versions of crc32()
- */
-const uLongf * ZEXPORT get_crc_table()
+unsigned int tinf_crc32(const void *data, unsigned int length)
 {
-#ifdef DYNAMIC_CRC_TABLE
-  if (crc_table_empty) make_crc_table();
-#endif
-  return (const uLongf *)crc_table;
-}
-#endif
+   const unsigned char *buf = (const unsigned char *)data;
+   unsigned int crc = 0xffffffff;
+   unsigned int i;
 
-/* ========================================================================= */
-#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
-#define DO2(buf)  DO1(buf); DO1(buf);
-#define DO4(buf)  DO2(buf); DO2(buf);
-#define DO8(buf)  DO4(buf); DO4(buf);
+   if (length == 0) return 0;
 
-/* ========================================================================= */
-uLong ZEXPORT crc32(crc, buf, len)
-    uLong crc;
-    const Bytef *buf;
-    uInt len;
-{
-#ifdef DYNAMIC_CRC_TABLE
-    if (crc_table_empty)
-      make_crc_table();
-#endif
-    crc = crc ^ 0xffffffffL;
-    while (len >= 8)
-    {
-      DO8(buf);
-      len -= 8;
-    }
-    if (len) do {
-      DO1(buf);
-    } while (--len);
-    return crc ^ 0xffffffffL;
+   for (i = 0; i < length; ++i)
+   {
+      crc ^= buf[i];
+      crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+      crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+   }
+
+   return crc ^ 0xffffffff;
 }
diff --git a/u-boot/lib_generic/tinfcrc32.c b/u-boot/lib_generic/tinfcrc32.c
new file mode 100644 (file)
index 0000000..fce7d33
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * CRC32 checksum
+ *
+ * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty.  In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ *    misrepresented; you must not claim that you
+ *    wrote the original software. If you use this
+ *    software in a product, an acknowledgment in
+ *    the product documentation would be appreciated
+ *    but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ *    as such, and must not be misrepresented as
+ *    being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ *    any source distribution.
+ */
+
+/*
+ * CRC32 algorithm taken from the zlib source, which is
+ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
+ */
+
+#include "tinf.h"
+
+static const unsigned int tinf_crc32tab[16] = {
+   0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190,
+   0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344,
+   0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278,
+   0xbdbdf21c
+};
+
+unsigned int tinf_crc32(const void *data, unsigned int length)
+{
+   const unsigned char *buf = (const unsigned char *)data;
+   unsigned int crc = 0xffffffff;
+   unsigned int i;
+
+   if (length == 0) return 0;
+
+   for (i = 0; i < length; ++i)
+   {
+      crc ^= buf[i];
+      crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+      crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
+   }
+
+   return crc ^ 0xffffffff;
+}
diff --git a/u-boot/lib_generic/tinfgzip.c b/u-boot/lib_generic/tinfgzip.c
new file mode 100644 (file)
index 0000000..ebc4d9e
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * tinfgzip  -  tiny gzip decompressor
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty.  In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ *    misrepresented; you must not claim that you
+ *    wrote the original software. If you use this
+ *    software in a product, an acknowledgment in
+ *    the product documentation would be appreciated
+ *    but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ *    as such, and must not be misrepresented as
+ *    being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ *    any source distribution.
+ */
+
+#include "tinf.h"
+#include <stdio.h>
+
+#define FTEXT    1
+#define FHCRC    2
+#define FEXTRA   4
+#define FNAME    8
+#define FCOMMENT 16
+
+int tinf_gzip_uncompress(void *dest, unsigned int *destLen,
+                         const void *source, unsigned int sourceLen)
+{
+    unsigned char *src = (unsigned char *)source;
+    unsigned char *dst = (unsigned char *)dest;
+    unsigned char *start;
+    unsigned int dlen, crc32;
+    int res;
+    unsigned char flg;
+
+    /* -- check format -- */
+
+    /* check id bytes */
+    if (src[0] != 0x1f || src[1] != 0x8b) return TINF_DATA_ERROR;
+
+    /* check method is deflate */
+    if (src[2] != 8) return TINF_DATA_ERROR;
+
+    /* get flag byte */
+    flg = src[3];
+
+    /* check that reserved bits are zero */
+    if (flg & 0xe0) return TINF_DATA_ERROR;
+
+    /* -- find start of compressed data -- */
+
+    /* skip base header of 10 bytes */
+    start = src + 10;
+
+    /* skip extra data if present */
+    if (flg & FEXTRA)
+    {
+       unsigned int xlen = start[1];
+       xlen = 256*xlen + start[0];
+       start += xlen + 2;
+    }
+
+    /* skip file name if present */
+    if (flg & FNAME) { while (*start) ++start; ++start; }
+
+    /* skip file comment if present */
+    if (flg & FCOMMENT) { while (*start) ++start; ++start; }
+
+    /* check header crc if present */
+    if (flg & FHCRC)
+    {
+       unsigned int hcrc = start[1];
+       hcrc = 256*hcrc + start[0];
+
+       if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
+          return TINF_DATA_ERROR;
+
+       start += 2;
+    }
+
+    /* -- get decompressed length -- */
+
+    dlen =            src[sourceLen - 1];
+    dlen = 256*dlen + src[sourceLen - 2];
+    dlen = 256*dlen + src[sourceLen - 3];
+    dlen = 256*dlen + src[sourceLen - 4];
+
+    /* -- get crc32 of decompressed data -- */
+
+    crc32 =             src[sourceLen - 5];
+    crc32 = 256*crc32 + src[sourceLen - 6];
+    crc32 = 256*crc32 + src[sourceLen - 7];
+    crc32 = 256*crc32 + src[sourceLen - 8];
+
+    /* -- decompress data -- */
+
+    res = tinf_uncompress(dst, destLen, start, src + sourceLen - start - 8);
+
+    if (res != TINF_OK) {
+       puts("uncompress error");
+       return TINF_DATA_ERROR;
+    }
+
+#if 0
+    if (*destLen != dlen) {
+       printf("len error %d != %d", *destLen, dlen);
+       return TINF_DATA_ERROR;
+    }
+
+    /* -- check CRC32 checksum -- */
+
+    if (crc32 != tinf_crc32(dst, *destLen)) {
+       return TINF_DATA_ERROR;
+    }
+#endif
+    printf("expected len %08x vs decoded %08x\n", dlen, *destLen);
+    printf("expected crc32 %08x vs calculated %08x\n", crc32, tinf_crc32(dst, *destLen));
+
+    return TINF_OK;
+}
diff --git a/u-boot/lib_generic/tinflate.c b/u-boot/lib_generic/tinflate.c
new file mode 100644 (file)
index 0000000..b2bec56
--- /dev/null
@@ -0,0 +1,457 @@
+/*
+ * tinflate  -  tiny inflate
+ *
+ * Copyright (c) 2003 by Joergen Ibsen / Jibz
+ * All Rights Reserved
+ *
+ * http://www.ibsensoftware.com/
+ *
+ * This software is provided 'as-is', without any express
+ * or implied warranty.  In no event will the authors be
+ * held liable for any damages arising from the use of
+ * this software.
+ *
+ * Permission is granted to anyone to use this software
+ * for any purpose, including commercial applications,
+ * and to alter it and redistribute it freely, subject to
+ * the following restrictions:
+ *
+ * 1. The origin of this software must not be
+ *    misrepresented; you must not claim that you
+ *    wrote the original software. If you use this
+ *    software in a product, an acknowledgment in
+ *    the product documentation would be appreciated
+ *    but is not required.
+ *
+ * 2. Altered source versions must be plainly marked
+ *    as such, and must not be misrepresented as
+ *    being the original software.
+ *
+ * 3. This notice may not be removed or altered from
+ *    any source distribution.
+ */
+
+#include "tinf.h"
+
+/* ------------------------------ *
+ * -- internal data structures -- *
+ * ------------------------------ */
+
+typedef struct {
+   unsigned short table[16];  /* table of code length counts */
+   unsigned short trans[288]; /* code -> symbol translation table */
+} TINF_TREE;
+
+typedef struct {
+   const unsigned char *source;
+   unsigned int tag;
+   unsigned int bitcount;
+
+   unsigned char *dest;
+   unsigned int *destLen;
+
+   TINF_TREE ltree; /* dynamic length/symbol tree */
+   TINF_TREE dtree; /* dynamic distance tree */
+} TINF_DATA;
+
+/* --------------------------------------------------- *
+ * -- uninitialized global data (static structures) -- *
+ * --------------------------------------------------- */
+
+TINF_TREE sltree; /* fixed length/symbol tree */
+TINF_TREE sdtree; /* fixed distance tree */
+
+/* extra bits and base tables for length codes */
+unsigned char length_bits[30];
+unsigned short length_base[30];
+
+/* extra bits and base tables for distance codes */
+unsigned char dist_bits[30];
+unsigned short dist_base[30];
+
+/* special ordering of code length codes */
+const unsigned char clcidx[] = {
+   16, 17, 18, 0, 8, 7, 9, 6,
+   10, 5, 11, 4, 12, 3, 13, 2,
+   14, 1, 15
+};
+
+/* ----------------------- *
+ * -- utility functions -- *
+ * ----------------------- */
+
+/* build extra bits and base tables */
+static void tinf_build_bits_base(unsigned char *bits, unsigned short *base, int delta, int first)
+{
+   int i, sum;
+
+   /* build bits table */
+   for (i = 0; i < delta; ++i) bits[i] = 0;
+   for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta;
+
+   /* build base table */
+   for (sum = first, i = 0; i < 30; ++i)
+   {
+      base[i] = sum;
+      sum += 1 << bits[i];
+   }
+}
+
+/* build the fixed huffman trees */
+static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt)
+{
+   int i;
+
+   /* build fixed length tree */
+   for (i = 0; i < 7; ++i) lt->table[i] = 0;
+
+   lt->table[7] = 24;
+   lt->table[8] = 152;
+   lt->table[9] = 112;
+
+   for (i = 0; i < 24; ++i) lt->trans[i] = 256 + i;
+   for (i = 0; i < 144; ++i) lt->trans[24 + i] = i;
+   for (i = 0; i < 8; ++i) lt->trans[24 + 144 + i] = 280 + i;
+   for (i = 0; i < 112; ++i) lt->trans[24 + 144 + 8 + i] = 144 + i;
+
+   /* build fixed distance tree */
+   for (i = 0; i < 5; ++i) dt->table[i] = 0;
+
+   dt->table[5] = 32;
+
+   for (i = 0; i < 32; ++i) dt->trans[i] = i;
+}
+
+/* given an array of code lengths, build a tree */
+static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned int num)
+{
+   unsigned short offs[16];
+   unsigned int i, sum;
+
+   /* clear code length count table */
+   for (i = 0; i < 16; ++i) t->table[i] = 0;
+
+   /* scan symbol lengths, and sum code length counts */
+   for (i = 0; i < num; ++i) t->table[lengths[i]]++;
+
+   t->table[0] = 0;
+
+   /* compute offset table for distribution sort */
+   for (sum = 0, i = 0; i < 16; ++i)
+   {
+      offs[i] = sum;
+      sum += t->table[i];
+   }
+
+   /* create code->symbol translation table (symbols sorted by code) */
+   for (i = 0; i < num; ++i)
+   {
+      if (lengths[i]) t->trans[offs[lengths[i]]++] = i;
+   }
+}
+
+/* ---------------------- *
+ * -- decode functions -- *
+ * ---------------------- */
+
+/* get one bit from source stream */
+static int tinf_getbit(TINF_DATA *d)
+{
+   unsigned int bit;
+
+   /* check if tag is empty */
+   if (!d->bitcount--)
+   {
+      /* load next tag */
+      d->tag = *d->source++;
+      d->bitcount = 7;
+   }
+
+   /* shift bit out of tag */
+   bit = d->tag & 0x01;
+   d->tag >>= 1;
+
+   return bit;
+}
+
+/* read a num bit value from a stream and add base */
+static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base)
+{
+   unsigned int val = 0;
+
+   /* read num bits */
+   if (num)
+   {
+      unsigned int limit = 1 << (num);
+      unsigned int mask;
+
+      for (mask = 1; mask < limit; mask *= 2)
+         if (tinf_getbit(d)) val += mask;
+   }
+
+   return val + base;
+}
+
+/* given a data stream and a tree, decode a symbol */
+static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
+{
+   int sum = 0, cur = 0, len = 0;
+
+   /* get more bits while code value is above sum */
+   do {
+
+      cur = 2*cur + tinf_getbit(d);
+
+      ++len;
+
+      sum += t->table[len];
+      cur -= t->table[len];
+
+   } while (cur >= 0);
+
+   return t->trans[sum + cur];
+}
+
+/* given a data stream, decode dynamic trees from it */
+static void tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
+{
+   TINF_TREE code_tree;
+   unsigned char lengths[288+32];
+   unsigned int hlit, hdist, hclen;
+   unsigned int i, num, length;
+
+   /* get 5 bits HLIT (257-286) */
+   hlit = tinf_read_bits(d, 5, 257);
+
+   /* get 5 bits HDIST (1-32) */
+   hdist = tinf_read_bits(d, 5, 1);
+
+   /* get 4 bits HCLEN (4-19) */
+   hclen = tinf_read_bits(d, 4, 4);
+
+   for (i = 0; i < 19; ++i) lengths[i] = 0;
+
+   /* read code lengths for code length alphabet */
+   for (i = 0; i < hclen; ++i)
+   {
+      /* get 3 bits code length (0-7) */
+      unsigned int clen = tinf_read_bits(d, 3, 0);
+
+      lengths[clcidx[i]] = clen;
+   }
+
+   /* build code length tree */
+   tinf_build_tree(&code_tree, lengths, 19);
+
+   /* decode code lengths for the dynamic trees */
+   for (num = 0; num < hlit + hdist; )
+   {
+      int sym = tinf_decode_symbol(d, &code_tree);
+
+      switch (sym)
+      {
+      case 16:
+         /* copy previous code length 3-6 times (read 2 bits) */
+         {
+            unsigned char prev = lengths[num - 1];
+            for (length = tinf_read_bits(d, 2, 3); length; --length)
+            {
+               lengths[num++] = prev;
+            }
+         }
+         break;
+      case 17:
+         /* repeat code length 0 for 3-10 times (read 3 bits) */
+         for (length = tinf_read_bits(d, 3, 3); length; --length)
+         {
+            lengths[num++] = 0;
+         }
+         break;
+      case 18:
+         /* repeat code length 0 for 11-138 times (read 7 bits) */
+         for (length = tinf_read_bits(d, 7, 11); length; --length)
+         {
+            lengths[num++] = 0;
+         }
+         break;
+      default:
+         /* values 0-15 represent the actual code lengths */
+         lengths[num++] = sym;
+         break;
+      }
+   }
+
+   /* build dynamic trees */
+   tinf_build_tree(lt, lengths, hlit);
+   tinf_build_tree(dt, lengths + hlit, hdist);
+}
+
+/* ----------------------------- *
+ * -- block inflate functions -- *
+ * ----------------------------- */
+
+/* given a stream and two trees, inflate a block of data */
+static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt)
+{
+   /* remember current output position */
+   unsigned char *start = d->dest;
+
+   while (1)
+   {
+      int sym = tinf_decode_symbol(d, lt);
+
+      /* check for end of block */
+      if (sym == 256)
+      {
+         *d->destLen += d->dest - start;
+         return TINF_OK;
+      }
+
+      if (sym < 256)
+      {
+         *d->dest++ = sym;
+
+      } else {
+
+         int length, dist, offs;
+         int i;
+
+         sym -= 257;
+
+         /* possibly get more bits from length code */
+         length = tinf_read_bits(d, length_bits[sym], length_base[sym]);
+
+         dist = tinf_decode_symbol(d, dt);
+
+         /* possibly get more bits from distance code */
+         offs = tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
+
+         /* copy match */
+         for (i = 0; i < length; ++i)
+         {
+            d->dest[i] = d->dest[i - offs];
+         }
+
+         d->dest += length;
+      }
+   }
+}
+
+/* inflate an uncompressed block of data */
+static int tinf_inflate_uncompressed_block(TINF_DATA *d)
+{
+   unsigned int length, invlength;
+   unsigned int i;
+
+   /* get length */
+   length = d->source[1];
+   length = 256*length + d->source[0];
+
+   /* get one's complement of length */
+   invlength = d->source[3];
+   invlength = 256*invlength + d->source[2];
+
+   /* check length */
+   if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR;
+
+   d->source += 4;
+
+   /* copy block */
+   for (i = length; i; --i) *d->dest++ = *d->source++;
+
+   /* make sure we start next block on a byte boundary */
+   d->bitcount = 0;
+
+   *d->destLen += length;
+
+   return TINF_OK;
+}
+
+/* inflate a block of data compressed with fixed huffman trees */
+static int tinf_inflate_fixed_block(TINF_DATA *d)
+{
+   /* decode block using fixed trees */
+   return tinf_inflate_block_data(d, &sltree, &sdtree);
+}
+
+/* inflate a block of data compressed with dynamic huffman trees */
+static int tinf_inflate_dynamic_block(TINF_DATA *d)
+{
+   /* decode trees from stream */
+   tinf_decode_trees(d, &d->ltree, &d->dtree);
+
+   /* decode block using decoded trees */
+   return tinf_inflate_block_data(d, &d->ltree, &d->dtree);
+}
+
+/* ---------------------- *
+ * -- public functions -- *
+ * ---------------------- */
+
+/* initialize global (static) data */
+void tinf_init()
+{
+   /* build fixed huffman trees */
+   tinf_build_fixed_trees(&sltree, &sdtree);
+
+   /* build extra bits and base tables */
+   tinf_build_bits_base(length_bits, length_base, 4, 3);
+   tinf_build_bits_base(dist_bits, dist_base, 2, 1);
+
+   /* fix a special case */
+   length_bits[28] = 0;
+   length_base[28] = 258;
+}
+
+/* inflate stream from source to dest */
+int tinf_uncompress(void *dest, unsigned int *destLen,
+                    const void *source, unsigned int sourceLen)
+{
+   TINF_DATA d;
+   int bfinal;
+
+   /* initialise data */
+   d.source = (const unsigned char *)source;
+   d.bitcount = 0;
+
+   d.dest = (unsigned char *)dest;
+   d.destLen = destLen;
+
+   *destLen = 0;
+
+   do {
+
+      unsigned int btype;
+      int res;
+
+      /* read final block flag */
+      bfinal = tinf_getbit(&d);
+
+      /* read block type (2 bits) */
+      btype = tinf_read_bits(&d, 2, 0);
+
+      /* decompress block */
+      switch (btype)
+      {
+      case 0:
+         /* decompress uncompressed block */
+         res = tinf_inflate_uncompressed_block(&d);
+         break;
+      case 1:
+         /* decompress block with fixed huffman trees */
+         res = tinf_inflate_fixed_block(&d);
+         break;
+      case 2:
+         /* decompress block with dynamic huffman trees */
+         res = tinf_inflate_dynamic_block(&d);
+         break;
+      default:
+         return TINF_DATA_ERROR;
+      }
+
+      if (res != TINF_OK) return TINF_DATA_ERROR;
+
+   } while (!bfinal);
+
+   return TINF_OK;
+}
index 4ecda49bf95faceb977925131f11d026c2594ab8..de8af90d1afbb8d032998f27f33c113649631358 100644 (file)
@@ -68,7 +68,10 @@ extern int errno;
 
 char *cmdname;
 
-extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
+#include <tinf.h>
+/* Hack: Forces compilation failure if must_be_zero is not zero. */
+#define crc32(must_be_zero, buf, len) \
+       tinf_crc32(buf, len*(sizeof(char[1 - 2*!!(must_be_zero)])))
 
 typedef struct table_entry {
        int     val;            /* as defined in image.h        */