Basic gzip/gunzip in memory buffer examples using zlib code.

Dependencies:   mbed-rtos mbed

There are small changes needed to the zconf.h file in the zlib distribution (I used 1.2.7). The zlib license applies to the zlib code - I have only imported a subset of the source.

The MBED has limited memory, so we need the following (near the top of zconf.h) to restrict memory allocation sizes:

#define    MAX_MEM_LEVEL    3
#define    MAX_WBITS        10

Because MAX_MEM_LEVEL and MAX_WBITS are so much lower than the default, there is a danger that the mbed cannot gunzip data compressed by a 'normal' zlib build. My use-case is to gzip on the mbed more than gunzip on the mbed so I have not given much time to this issue.

I also included this (also near the top of zconf.h) to prefix defines with Z_

#define    Z_PREFIX

In zconf.h, in the zlib distribution, the includes for <fcntl.h> and <sys/types.h> need commenting out when using the online compiler. No need when using GCC4MBED.

I also looked at miniz. I chose zlib because I needed the gzip headers and miniz does not implement them.

The sample main.cpp reads source data, compresses it, decompresses it, and finally compares the input data with the output data to confirm they are the same.

    unsigned char input_data[2048];
    unsigned long input_data_length = 0;
    FILE *ifp = fopen("/local/src.txt", "r");
    if (ifp) {
        int br = fread(input_data, 1, sizeof(input_data), ifp);
        fclose(ifp);
        input_data_length = br;
    }
    printf("%s:%d: input_data_length:%lu%s", __FILE__, __LINE__, input_data_length, newline);
 
 
    unsigned char gzip_data[2048];
    unsigned long gzip_data_length = 0;
    if (input_data_length > 0) {
        gzip_data_length = sizeof(gzip_data);
        int rv = gzip(gzip_data, &gzip_data_length, input_data, input_data_length);
        if (Z_OK == rv) {
            FILE *ofp = fopen("/local/dst.gz", "w");
            if (ofp) {
                int bw = fwrite(gzip_data, 1, gzip_data_length, ofp);
                fclose(ofp);
            }
        } else {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: gzip_data_length:%lu%s", __FILE__, __LINE__, gzip_data_length, newline);
 
 
    unsigned char output_data[2048];
    unsigned long output_data_length = 0;
    if (gzip_data_length > 0) {
        output_data_length = sizeof(output_data);
        int rv = gunzip(output_data, &output_data_length, gzip_data, gzip_data_length);
        if (Z_OK != rv) {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: output_data_length:%lu%s", __FILE__, __LINE__, output_data_length, newline);
 
 
    if (input_data_length > 0 and input_data_length > 0) {
        bool input_matches_output = false;
        if (input_data_length == output_data_length) {
            input_matches_output = true;
            for ( size_t i = 0 ; input_matches_output && i < input_data_length ; i++ ) {
                if (input_data[i] != output_data[i]) {
                    input_matches_output = false;
                }
            }
        }
        printf("%s:%d: input (%lu bytes) %s output (%lu bytes)%s", __FILE__, __LINE__, input_data_length, input_matches_output?"matches":"does not match", output_data_length, newline);
    } else {
        printf("%s:%d: input and/or output length is 0%s", __FILE__, __LINE__, newline);
    }
Committer:
jonathonfletcher
Date:
Sun Oct 21 07:46:41 2012 +0000
Revision:
0:54f5be781526
initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonathonfletcher 0:54f5be781526 1 /* crc32.c -- compute the CRC-32 of a data stream
jonathonfletcher 0:54f5be781526 2 * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
jonathonfletcher 0:54f5be781526 3 * For conditions of distribution and use, see copyright notice in zlib.h
jonathonfletcher 0:54f5be781526 4 *
jonathonfletcher 0:54f5be781526 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
jonathonfletcher 0:54f5be781526 6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
jonathonfletcher 0:54f5be781526 7 * tables for updating the shift register in one step with three exclusive-ors
jonathonfletcher 0:54f5be781526 8 * instead of four steps with four exclusive-ors. This results in about a
jonathonfletcher 0:54f5be781526 9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
jonathonfletcher 0:54f5be781526 10 */
jonathonfletcher 0:54f5be781526 11
jonathonfletcher 0:54f5be781526 12 /* @(#) $Id$ */
jonathonfletcher 0:54f5be781526 13
jonathonfletcher 0:54f5be781526 14 /*
jonathonfletcher 0:54f5be781526 15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
jonathonfletcher 0:54f5be781526 16 protection on the static variables used to control the first-use generation
jonathonfletcher 0:54f5be781526 17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
jonathonfletcher 0:54f5be781526 18 first call get_crc_table() to initialize the tables before allowing more than
jonathonfletcher 0:54f5be781526 19 one thread to use crc32().
jonathonfletcher 0:54f5be781526 20
jonathonfletcher 0:54f5be781526 21 DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
jonathonfletcher 0:54f5be781526 22 */
jonathonfletcher 0:54f5be781526 23
jonathonfletcher 0:54f5be781526 24 #ifdef MAKECRCH
jonathonfletcher 0:54f5be781526 25 # include <stdio.h>
jonathonfletcher 0:54f5be781526 26 # ifndef DYNAMIC_CRC_TABLE
jonathonfletcher 0:54f5be781526 27 # define DYNAMIC_CRC_TABLE
jonathonfletcher 0:54f5be781526 28 # endif /* !DYNAMIC_CRC_TABLE */
jonathonfletcher 0:54f5be781526 29 #endif /* MAKECRCH */
jonathonfletcher 0:54f5be781526 30
jonathonfletcher 0:54f5be781526 31 #include "zutil.h" /* for STDC and FAR definitions */
jonathonfletcher 0:54f5be781526 32
jonathonfletcher 0:54f5be781526 33 #define local static
jonathonfletcher 0:54f5be781526 34
jonathonfletcher 0:54f5be781526 35 /* Definitions for doing the crc four data bytes at a time. */
jonathonfletcher 0:54f5be781526 36 #if !defined(NOBYFOUR) && defined(Z_U4)
jonathonfletcher 0:54f5be781526 37 # define BYFOUR
jonathonfletcher 0:54f5be781526 38 #endif
jonathonfletcher 0:54f5be781526 39 #ifdef BYFOUR
jonathonfletcher 0:54f5be781526 40 local unsigned long crc32_little OF((unsigned long,
jonathonfletcher 0:54f5be781526 41 const unsigned char FAR *, unsigned));
jonathonfletcher 0:54f5be781526 42 local unsigned long crc32_big OF((unsigned long,
jonathonfletcher 0:54f5be781526 43 const unsigned char FAR *, unsigned));
jonathonfletcher 0:54f5be781526 44 # define TBLS 8
jonathonfletcher 0:54f5be781526 45 #else
jonathonfletcher 0:54f5be781526 46 # define TBLS 1
jonathonfletcher 0:54f5be781526 47 #endif /* BYFOUR */
jonathonfletcher 0:54f5be781526 48
jonathonfletcher 0:54f5be781526 49 /* Local functions for crc concatenation */
jonathonfletcher 0:54f5be781526 50 local unsigned long gf2_matrix_times OF((unsigned long *mat,
jonathonfletcher 0:54f5be781526 51 unsigned long vec));
jonathonfletcher 0:54f5be781526 52 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
jonathonfletcher 0:54f5be781526 53 local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
jonathonfletcher 0:54f5be781526 54
jonathonfletcher 0:54f5be781526 55
jonathonfletcher 0:54f5be781526 56 #ifdef DYNAMIC_CRC_TABLE
jonathonfletcher 0:54f5be781526 57
jonathonfletcher 0:54f5be781526 58 local volatile int crc_table_empty = 1;
jonathonfletcher 0:54f5be781526 59 local z_crc_t FAR crc_table[TBLS][256];
jonathonfletcher 0:54f5be781526 60 local void make_crc_table OF((void));
jonathonfletcher 0:54f5be781526 61 #ifdef MAKECRCH
jonathonfletcher 0:54f5be781526 62 local void write_table OF((FILE *, const z_crc_t FAR *));
jonathonfletcher 0:54f5be781526 63 #endif /* MAKECRCH */
jonathonfletcher 0:54f5be781526 64 /*
jonathonfletcher 0:54f5be781526 65 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
jonathonfletcher 0:54f5be781526 66 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.
jonathonfletcher 0:54f5be781526 67
jonathonfletcher 0:54f5be781526 68 Polynomials over GF(2) are represented in binary, one bit per coefficient,
jonathonfletcher 0:54f5be781526 69 with the lowest powers in the most significant bit. Then adding polynomials
jonathonfletcher 0:54f5be781526 70 is just exclusive-or, and multiplying a polynomial by x is a right shift by
jonathonfletcher 0:54f5be781526 71 one. If we call the above polynomial p, and represent a byte as the
jonathonfletcher 0:54f5be781526 72 polynomial q, also with the lowest power in the most significant bit (so the
jonathonfletcher 0:54f5be781526 73 byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
jonathonfletcher 0:54f5be781526 74 where a mod b means the remainder after dividing a by b.
jonathonfletcher 0:54f5be781526 75
jonathonfletcher 0:54f5be781526 76 This calculation is done using the shift-register method of multiplying and
jonathonfletcher 0:54f5be781526 77 taking the remainder. The register is initialized to zero, and for each
jonathonfletcher 0:54f5be781526 78 incoming bit, x^32 is added mod p to the register if the bit is a one (where
jonathonfletcher 0:54f5be781526 79 x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
jonathonfletcher 0:54f5be781526 80 x (which is shifting right by one and adding x^32 mod p if the bit shifted
jonathonfletcher 0:54f5be781526 81 out is a one). We start with the highest power (least significant bit) of
jonathonfletcher 0:54f5be781526 82 q and repeat for all eight bits of q.
jonathonfletcher 0:54f5be781526 83
jonathonfletcher 0:54f5be781526 84 The first table is simply the CRC of all possible eight bit values. This is
jonathonfletcher 0:54f5be781526 85 all the information needed to generate CRCs on data a byte at a time for all
jonathonfletcher 0:54f5be781526 86 combinations of CRC register values and incoming bytes. The remaining tables
jonathonfletcher 0:54f5be781526 87 allow for word-at-a-time CRC calculation for both big-endian and little-
jonathonfletcher 0:54f5be781526 88 endian machines, where a word is four bytes.
jonathonfletcher 0:54f5be781526 89 */
jonathonfletcher 0:54f5be781526 90 local void make_crc_table()
jonathonfletcher 0:54f5be781526 91 {
jonathonfletcher 0:54f5be781526 92 z_crc_t c;
jonathonfletcher 0:54f5be781526 93 int n, k;
jonathonfletcher 0:54f5be781526 94 z_crc_t poly; /* polynomial exclusive-or pattern */
jonathonfletcher 0:54f5be781526 95 /* terms of polynomial defining this crc (except x^32): */
jonathonfletcher 0:54f5be781526 96 static volatile int first = 1; /* flag to limit concurrent making */
jonathonfletcher 0:54f5be781526 97 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
jonathonfletcher 0:54f5be781526 98
jonathonfletcher 0:54f5be781526 99 /* See if another task is already doing this (not thread-safe, but better
jonathonfletcher 0:54f5be781526 100 than nothing -- significantly reduces duration of vulnerability in
jonathonfletcher 0:54f5be781526 101 case the advice about DYNAMIC_CRC_TABLE is ignored) */
jonathonfletcher 0:54f5be781526 102 if (first) {
jonathonfletcher 0:54f5be781526 103 first = 0;
jonathonfletcher 0:54f5be781526 104
jonathonfletcher 0:54f5be781526 105 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
jonathonfletcher 0:54f5be781526 106 poly = 0;
jonathonfletcher 0:54f5be781526 107 for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
jonathonfletcher 0:54f5be781526 108 poly |= (z_crc_t)1 << (31 - p[n]);
jonathonfletcher 0:54f5be781526 109
jonathonfletcher 0:54f5be781526 110 /* generate a crc for every 8-bit value */
jonathonfletcher 0:54f5be781526 111 for (n = 0; n < 256; n++) {
jonathonfletcher 0:54f5be781526 112 c = (z_crc_t)n;
jonathonfletcher 0:54f5be781526 113 for (k = 0; k < 8; k++)
jonathonfletcher 0:54f5be781526 114 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
jonathonfletcher 0:54f5be781526 115 crc_table[0][n] = c;
jonathonfletcher 0:54f5be781526 116 }
jonathonfletcher 0:54f5be781526 117
jonathonfletcher 0:54f5be781526 118 #ifdef BYFOUR
jonathonfletcher 0:54f5be781526 119 /* generate crc for each value followed by one, two, and three zeros,
jonathonfletcher 0:54f5be781526 120 and then the byte reversal of those as well as the first table */
jonathonfletcher 0:54f5be781526 121 for (n = 0; n < 256; n++) {
jonathonfletcher 0:54f5be781526 122 c = crc_table[0][n];
jonathonfletcher 0:54f5be781526 123 crc_table[4][n] = ZSWAP32(c);
jonathonfletcher 0:54f5be781526 124 for (k = 1; k < 4; k++) {
jonathonfletcher 0:54f5be781526 125 c = crc_table[0][c & 0xff] ^ (c >> 8);
jonathonfletcher 0:54f5be781526 126 crc_table[k][n] = c;
jonathonfletcher 0:54f5be781526 127 crc_table[k + 4][n] = ZSWAP32(c);
jonathonfletcher 0:54f5be781526 128 }
jonathonfletcher 0:54f5be781526 129 }
jonathonfletcher 0:54f5be781526 130 #endif /* BYFOUR */
jonathonfletcher 0:54f5be781526 131
jonathonfletcher 0:54f5be781526 132 crc_table_empty = 0;
jonathonfletcher 0:54f5be781526 133 }
jonathonfletcher 0:54f5be781526 134 else { /* not first */
jonathonfletcher 0:54f5be781526 135 /* wait for the other guy to finish (not efficient, but rare) */
jonathonfletcher 0:54f5be781526 136 while (crc_table_empty)
jonathonfletcher 0:54f5be781526 137 ;
jonathonfletcher 0:54f5be781526 138 }
jonathonfletcher 0:54f5be781526 139
jonathonfletcher 0:54f5be781526 140 #ifdef MAKECRCH
jonathonfletcher 0:54f5be781526 141 /* write out CRC tables to crc32.h */
jonathonfletcher 0:54f5be781526 142 {
jonathonfletcher 0:54f5be781526 143 FILE *out;
jonathonfletcher 0:54f5be781526 144
jonathonfletcher 0:54f5be781526 145 out = fopen("crc32.h", "w");
jonathonfletcher 0:54f5be781526 146 if (out == NULL) return;
jonathonfletcher 0:54f5be781526 147 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
jonathonfletcher 0:54f5be781526 148 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
jonathonfletcher 0:54f5be781526 149 fprintf(out, "local const z_crc_t FAR ");
jonathonfletcher 0:54f5be781526 150 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
jonathonfletcher 0:54f5be781526 151 write_table(out, crc_table[0]);
jonathonfletcher 0:54f5be781526 152 # ifdef BYFOUR
jonathonfletcher 0:54f5be781526 153 fprintf(out, "#ifdef BYFOUR\n");
jonathonfletcher 0:54f5be781526 154 for (k = 1; k < 8; k++) {
jonathonfletcher 0:54f5be781526 155 fprintf(out, " },\n {\n");
jonathonfletcher 0:54f5be781526 156 write_table(out, crc_table[k]);
jonathonfletcher 0:54f5be781526 157 }
jonathonfletcher 0:54f5be781526 158 fprintf(out, "#endif\n");
jonathonfletcher 0:54f5be781526 159 # endif /* BYFOUR */
jonathonfletcher 0:54f5be781526 160 fprintf(out, " }\n};\n");
jonathonfletcher 0:54f5be781526 161 fclose(out);
jonathonfletcher 0:54f5be781526 162 }
jonathonfletcher 0:54f5be781526 163 #endif /* MAKECRCH */
jonathonfletcher 0:54f5be781526 164 }
jonathonfletcher 0:54f5be781526 165
jonathonfletcher 0:54f5be781526 166 #ifdef MAKECRCH
jonathonfletcher 0:54f5be781526 167 local void write_table(out, table)
jonathonfletcher 0:54f5be781526 168 FILE *out;
jonathonfletcher 0:54f5be781526 169 const z_crc_t FAR *table;
jonathonfletcher 0:54f5be781526 170 {
jonathonfletcher 0:54f5be781526 171 int n;
jonathonfletcher 0:54f5be781526 172
jonathonfletcher 0:54f5be781526 173 for (n = 0; n < 256; n++)
jonathonfletcher 0:54f5be781526 174 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
jonathonfletcher 0:54f5be781526 175 (unsigned long)(table[n]),
jonathonfletcher 0:54f5be781526 176 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
jonathonfletcher 0:54f5be781526 177 }
jonathonfletcher 0:54f5be781526 178 #endif /* MAKECRCH */
jonathonfletcher 0:54f5be781526 179
jonathonfletcher 0:54f5be781526 180 #else /* !DYNAMIC_CRC_TABLE */
jonathonfletcher 0:54f5be781526 181 /* ========================================================================
jonathonfletcher 0:54f5be781526 182 * Tables of CRC-32s of all single-byte values, made by make_crc_table().
jonathonfletcher 0:54f5be781526 183 */
jonathonfletcher 0:54f5be781526 184 #include "crc32.h"
jonathonfletcher 0:54f5be781526 185 #endif /* DYNAMIC_CRC_TABLE */
jonathonfletcher 0:54f5be781526 186
jonathonfletcher 0:54f5be781526 187 /* =========================================================================
jonathonfletcher 0:54f5be781526 188 * This function can be used by asm versions of crc32()
jonathonfletcher 0:54f5be781526 189 */
jonathonfletcher 0:54f5be781526 190 const z_crc_t FAR * ZEXPORT get_crc_table()
jonathonfletcher 0:54f5be781526 191 {
jonathonfletcher 0:54f5be781526 192 #ifdef DYNAMIC_CRC_TABLE
jonathonfletcher 0:54f5be781526 193 if (crc_table_empty)
jonathonfletcher 0:54f5be781526 194 make_crc_table();
jonathonfletcher 0:54f5be781526 195 #endif /* DYNAMIC_CRC_TABLE */
jonathonfletcher 0:54f5be781526 196 return (const z_crc_t FAR *)crc_table;
jonathonfletcher 0:54f5be781526 197 }
jonathonfletcher 0:54f5be781526 198
jonathonfletcher 0:54f5be781526 199 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 200 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
jonathonfletcher 0:54f5be781526 201 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
jonathonfletcher 0:54f5be781526 202
jonathonfletcher 0:54f5be781526 203 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 204 unsigned long ZEXPORT crc32(crc, buf, len)
jonathonfletcher 0:54f5be781526 205 unsigned long crc;
jonathonfletcher 0:54f5be781526 206 const unsigned char FAR *buf;
jonathonfletcher 0:54f5be781526 207 uInt len;
jonathonfletcher 0:54f5be781526 208 {
jonathonfletcher 0:54f5be781526 209 if (buf == Z_NULL) return 0UL;
jonathonfletcher 0:54f5be781526 210
jonathonfletcher 0:54f5be781526 211 #ifdef DYNAMIC_CRC_TABLE
jonathonfletcher 0:54f5be781526 212 if (crc_table_empty)
jonathonfletcher 0:54f5be781526 213 make_crc_table();
jonathonfletcher 0:54f5be781526 214 #endif /* DYNAMIC_CRC_TABLE */
jonathonfletcher 0:54f5be781526 215
jonathonfletcher 0:54f5be781526 216 #ifdef BYFOUR
jonathonfletcher 0:54f5be781526 217 if (sizeof(void *) == sizeof(ptrdiff_t)) {
jonathonfletcher 0:54f5be781526 218 z_crc_t endian;
jonathonfletcher 0:54f5be781526 219
jonathonfletcher 0:54f5be781526 220 endian = 1;
jonathonfletcher 0:54f5be781526 221 if (*((unsigned char *)(&endian)))
jonathonfletcher 0:54f5be781526 222 return crc32_little(crc, buf, len);
jonathonfletcher 0:54f5be781526 223 else
jonathonfletcher 0:54f5be781526 224 return crc32_big(crc, buf, len);
jonathonfletcher 0:54f5be781526 225 }
jonathonfletcher 0:54f5be781526 226 #endif /* BYFOUR */
jonathonfletcher 0:54f5be781526 227 crc = crc ^ 0xffffffffUL;
jonathonfletcher 0:54f5be781526 228 while (len >= 8) {
jonathonfletcher 0:54f5be781526 229 DO8;
jonathonfletcher 0:54f5be781526 230 len -= 8;
jonathonfletcher 0:54f5be781526 231 }
jonathonfletcher 0:54f5be781526 232 if (len) do {
jonathonfletcher 0:54f5be781526 233 DO1;
jonathonfletcher 0:54f5be781526 234 } while (--len);
jonathonfletcher 0:54f5be781526 235 return crc ^ 0xffffffffUL;
jonathonfletcher 0:54f5be781526 236 }
jonathonfletcher 0:54f5be781526 237
jonathonfletcher 0:54f5be781526 238 #ifdef BYFOUR
jonathonfletcher 0:54f5be781526 239
jonathonfletcher 0:54f5be781526 240 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 241 #define DOLIT4 c ^= *buf4++; \
jonathonfletcher 0:54f5be781526 242 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
jonathonfletcher 0:54f5be781526 243 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
jonathonfletcher 0:54f5be781526 244 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
jonathonfletcher 0:54f5be781526 245
jonathonfletcher 0:54f5be781526 246 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 247 local unsigned long crc32_little(crc, buf, len)
jonathonfletcher 0:54f5be781526 248 unsigned long crc;
jonathonfletcher 0:54f5be781526 249 const unsigned char FAR *buf;
jonathonfletcher 0:54f5be781526 250 unsigned len;
jonathonfletcher 0:54f5be781526 251 {
jonathonfletcher 0:54f5be781526 252 register z_crc_t c;
jonathonfletcher 0:54f5be781526 253 register const z_crc_t FAR *buf4;
jonathonfletcher 0:54f5be781526 254
jonathonfletcher 0:54f5be781526 255 c = (z_crc_t)crc;
jonathonfletcher 0:54f5be781526 256 c = ~c;
jonathonfletcher 0:54f5be781526 257 while (len && ((ptrdiff_t)buf & 3)) {
jonathonfletcher 0:54f5be781526 258 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
jonathonfletcher 0:54f5be781526 259 len--;
jonathonfletcher 0:54f5be781526 260 }
jonathonfletcher 0:54f5be781526 261
jonathonfletcher 0:54f5be781526 262 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
jonathonfletcher 0:54f5be781526 263 while (len >= 32) {
jonathonfletcher 0:54f5be781526 264 DOLIT32;
jonathonfletcher 0:54f5be781526 265 len -= 32;
jonathonfletcher 0:54f5be781526 266 }
jonathonfletcher 0:54f5be781526 267 while (len >= 4) {
jonathonfletcher 0:54f5be781526 268 DOLIT4;
jonathonfletcher 0:54f5be781526 269 len -= 4;
jonathonfletcher 0:54f5be781526 270 }
jonathonfletcher 0:54f5be781526 271 buf = (const unsigned char FAR *)buf4;
jonathonfletcher 0:54f5be781526 272
jonathonfletcher 0:54f5be781526 273 if (len) do {
jonathonfletcher 0:54f5be781526 274 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
jonathonfletcher 0:54f5be781526 275 } while (--len);
jonathonfletcher 0:54f5be781526 276 c = ~c;
jonathonfletcher 0:54f5be781526 277 return (unsigned long)c;
jonathonfletcher 0:54f5be781526 278 }
jonathonfletcher 0:54f5be781526 279
jonathonfletcher 0:54f5be781526 280 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 281 #define DOBIG4 c ^= *++buf4; \
jonathonfletcher 0:54f5be781526 282 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
jonathonfletcher 0:54f5be781526 283 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
jonathonfletcher 0:54f5be781526 284 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
jonathonfletcher 0:54f5be781526 285
jonathonfletcher 0:54f5be781526 286 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 287 local unsigned long crc32_big(crc, buf, len)
jonathonfletcher 0:54f5be781526 288 unsigned long crc;
jonathonfletcher 0:54f5be781526 289 const unsigned char FAR *buf;
jonathonfletcher 0:54f5be781526 290 unsigned len;
jonathonfletcher 0:54f5be781526 291 {
jonathonfletcher 0:54f5be781526 292 register z_crc_t c;
jonathonfletcher 0:54f5be781526 293 register const z_crc_t FAR *buf4;
jonathonfletcher 0:54f5be781526 294
jonathonfletcher 0:54f5be781526 295 c = ZSWAP32((z_crc_t)crc);
jonathonfletcher 0:54f5be781526 296 c = ~c;
jonathonfletcher 0:54f5be781526 297 while (len && ((ptrdiff_t)buf & 3)) {
jonathonfletcher 0:54f5be781526 298 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
jonathonfletcher 0:54f5be781526 299 len--;
jonathonfletcher 0:54f5be781526 300 }
jonathonfletcher 0:54f5be781526 301
jonathonfletcher 0:54f5be781526 302 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
jonathonfletcher 0:54f5be781526 303 buf4--;
jonathonfletcher 0:54f5be781526 304 while (len >= 32) {
jonathonfletcher 0:54f5be781526 305 DOBIG32;
jonathonfletcher 0:54f5be781526 306 len -= 32;
jonathonfletcher 0:54f5be781526 307 }
jonathonfletcher 0:54f5be781526 308 while (len >= 4) {
jonathonfletcher 0:54f5be781526 309 DOBIG4;
jonathonfletcher 0:54f5be781526 310 len -= 4;
jonathonfletcher 0:54f5be781526 311 }
jonathonfletcher 0:54f5be781526 312 buf4++;
jonathonfletcher 0:54f5be781526 313 buf = (const unsigned char FAR *)buf4;
jonathonfletcher 0:54f5be781526 314
jonathonfletcher 0:54f5be781526 315 if (len) do {
jonathonfletcher 0:54f5be781526 316 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
jonathonfletcher 0:54f5be781526 317 } while (--len);
jonathonfletcher 0:54f5be781526 318 c = ~c;
jonathonfletcher 0:54f5be781526 319 return (unsigned long)(ZSWAP32(c));
jonathonfletcher 0:54f5be781526 320 }
jonathonfletcher 0:54f5be781526 321
jonathonfletcher 0:54f5be781526 322 #endif /* BYFOUR */
jonathonfletcher 0:54f5be781526 323
jonathonfletcher 0:54f5be781526 324 #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
jonathonfletcher 0:54f5be781526 325
jonathonfletcher 0:54f5be781526 326 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 327 local unsigned long gf2_matrix_times(mat, vec)
jonathonfletcher 0:54f5be781526 328 unsigned long *mat;
jonathonfletcher 0:54f5be781526 329 unsigned long vec;
jonathonfletcher 0:54f5be781526 330 {
jonathonfletcher 0:54f5be781526 331 unsigned long sum;
jonathonfletcher 0:54f5be781526 332
jonathonfletcher 0:54f5be781526 333 sum = 0;
jonathonfletcher 0:54f5be781526 334 while (vec) {
jonathonfletcher 0:54f5be781526 335 if (vec & 1)
jonathonfletcher 0:54f5be781526 336 sum ^= *mat;
jonathonfletcher 0:54f5be781526 337 vec >>= 1;
jonathonfletcher 0:54f5be781526 338 mat++;
jonathonfletcher 0:54f5be781526 339 }
jonathonfletcher 0:54f5be781526 340 return sum;
jonathonfletcher 0:54f5be781526 341 }
jonathonfletcher 0:54f5be781526 342
jonathonfletcher 0:54f5be781526 343 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 344 local void gf2_matrix_square(square, mat)
jonathonfletcher 0:54f5be781526 345 unsigned long *square;
jonathonfletcher 0:54f5be781526 346 unsigned long *mat;
jonathonfletcher 0:54f5be781526 347 {
jonathonfletcher 0:54f5be781526 348 int n;
jonathonfletcher 0:54f5be781526 349
jonathonfletcher 0:54f5be781526 350 for (n = 0; n < GF2_DIM; n++)
jonathonfletcher 0:54f5be781526 351 square[n] = gf2_matrix_times(mat, mat[n]);
jonathonfletcher 0:54f5be781526 352 }
jonathonfletcher 0:54f5be781526 353
jonathonfletcher 0:54f5be781526 354 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 355 local uLong crc32_combine_(crc1, crc2, len2)
jonathonfletcher 0:54f5be781526 356 uLong crc1;
jonathonfletcher 0:54f5be781526 357 uLong crc2;
jonathonfletcher 0:54f5be781526 358 z_off64_t len2;
jonathonfletcher 0:54f5be781526 359 {
jonathonfletcher 0:54f5be781526 360 int n;
jonathonfletcher 0:54f5be781526 361 unsigned long row;
jonathonfletcher 0:54f5be781526 362 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
jonathonfletcher 0:54f5be781526 363 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
jonathonfletcher 0:54f5be781526 364
jonathonfletcher 0:54f5be781526 365 /* degenerate case (also disallow negative lengths) */
jonathonfletcher 0:54f5be781526 366 if (len2 <= 0)
jonathonfletcher 0:54f5be781526 367 return crc1;
jonathonfletcher 0:54f5be781526 368
jonathonfletcher 0:54f5be781526 369 /* put operator for one zero bit in odd */
jonathonfletcher 0:54f5be781526 370 odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
jonathonfletcher 0:54f5be781526 371 row = 1;
jonathonfletcher 0:54f5be781526 372 for (n = 1; n < GF2_DIM; n++) {
jonathonfletcher 0:54f5be781526 373 odd[n] = row;
jonathonfletcher 0:54f5be781526 374 row <<= 1;
jonathonfletcher 0:54f5be781526 375 }
jonathonfletcher 0:54f5be781526 376
jonathonfletcher 0:54f5be781526 377 /* put operator for two zero bits in even */
jonathonfletcher 0:54f5be781526 378 gf2_matrix_square(even, odd);
jonathonfletcher 0:54f5be781526 379
jonathonfletcher 0:54f5be781526 380 /* put operator for four zero bits in odd */
jonathonfletcher 0:54f5be781526 381 gf2_matrix_square(odd, even);
jonathonfletcher 0:54f5be781526 382
jonathonfletcher 0:54f5be781526 383 /* apply len2 zeros to crc1 (first square will put the operator for one
jonathonfletcher 0:54f5be781526 384 zero byte, eight zero bits, in even) */
jonathonfletcher 0:54f5be781526 385 do {
jonathonfletcher 0:54f5be781526 386 /* apply zeros operator for this bit of len2 */
jonathonfletcher 0:54f5be781526 387 gf2_matrix_square(even, odd);
jonathonfletcher 0:54f5be781526 388 if (len2 & 1)
jonathonfletcher 0:54f5be781526 389 crc1 = gf2_matrix_times(even, crc1);
jonathonfletcher 0:54f5be781526 390 len2 >>= 1;
jonathonfletcher 0:54f5be781526 391
jonathonfletcher 0:54f5be781526 392 /* if no more bits set, then done */
jonathonfletcher 0:54f5be781526 393 if (len2 == 0)
jonathonfletcher 0:54f5be781526 394 break;
jonathonfletcher 0:54f5be781526 395
jonathonfletcher 0:54f5be781526 396 /* another iteration of the loop with odd and even swapped */
jonathonfletcher 0:54f5be781526 397 gf2_matrix_square(odd, even);
jonathonfletcher 0:54f5be781526 398 if (len2 & 1)
jonathonfletcher 0:54f5be781526 399 crc1 = gf2_matrix_times(odd, crc1);
jonathonfletcher 0:54f5be781526 400 len2 >>= 1;
jonathonfletcher 0:54f5be781526 401
jonathonfletcher 0:54f5be781526 402 /* if no more bits set, then done */
jonathonfletcher 0:54f5be781526 403 } while (len2 != 0);
jonathonfletcher 0:54f5be781526 404
jonathonfletcher 0:54f5be781526 405 /* return combined crc */
jonathonfletcher 0:54f5be781526 406 crc1 ^= crc2;
jonathonfletcher 0:54f5be781526 407 return crc1;
jonathonfletcher 0:54f5be781526 408 }
jonathonfletcher 0:54f5be781526 409
jonathonfletcher 0:54f5be781526 410 /* ========================================================================= */
jonathonfletcher 0:54f5be781526 411 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
jonathonfletcher 0:54f5be781526 412 uLong crc1;
jonathonfletcher 0:54f5be781526 413 uLong crc2;
jonathonfletcher 0:54f5be781526 414 z_off_t len2;
jonathonfletcher 0:54f5be781526 415 {
jonathonfletcher 0:54f5be781526 416 return crc32_combine_(crc1, crc2, len2);
jonathonfletcher 0:54f5be781526 417 }
jonathonfletcher 0:54f5be781526 418
jonathonfletcher 0:54f5be781526 419 uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
jonathonfletcher 0:54f5be781526 420 uLong crc1;
jonathonfletcher 0:54f5be781526 421 uLong crc2;
jonathonfletcher 0:54f5be781526 422 z_off64_t len2;
jonathonfletcher 0:54f5be781526 423 {
jonathonfletcher 0:54f5be781526 424 return crc32_combine_(crc1, crc2, len2);
jonathonfletcher 0:54f5be781526 425 }