~ubuntu-branches/ubuntu/trusty/rsync/trusty

« back to all changes in this revision

Viewing changes to zlib/crc32.c

  • Committer: Package Import Robot
  • Author(s): Paul Slootman
  • Date: 2013-10-27 12:01:10 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20131027120110-mpr03n5scqmf40mi
Tags: 3.1.0-2
fix build failure if zlib1g-dev package is not installed;
solved by building without the included zlib source and adding a
build-depends on zlib1g-dev >= 1:1.2.8
closes:32379

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* crc32.c -- compute the CRC-32 of a data stream
2
 
 * Copyright (C) 1995-2005 Mark Adler
 
2
 * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
3
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
4
 *
5
5
 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
17
17
  of the crc tables.  Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18
18
  first call get_crc_table() to initialize the tables before allowing more than
19
19
  one thread to use crc32().
 
20
 
 
21
  DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
20
22
 */
21
23
 
22
24
#ifdef MAKECRCH
30
32
 
31
33
#define local static
32
34
 
33
 
/* Find a four-byte integer type for crc32_little() and crc32_big(). */
34
 
#ifndef NOBYFOUR
35
 
#  ifdef STDC           /* need ANSI C limits.h to determine sizes */
36
 
#    include <limits.h>
37
 
#    define BYFOUR
38
 
#    if (UINT_MAX == 0xffffffffUL)
39
 
       typedef unsigned int u4;
40
 
#    else
41
 
#      if (ULONG_MAX == 0xffffffffUL)
42
 
         typedef unsigned long u4;
43
 
#      else
44
 
#        if (USHRT_MAX == 0xffffffffUL)
45
 
           typedef unsigned short u4;
46
 
#        else
47
 
#          undef BYFOUR     /* can't find a four-byte integer type! */
48
 
#        endif
49
 
#      endif
50
 
#    endif
51
 
#  endif /* STDC */
52
 
#endif /* !NOBYFOUR */
53
 
 
54
35
/* Definitions for doing the crc four data bytes at a time. */
 
36
#if !defined(NOBYFOUR) && defined(Z_U4)
 
37
#  define BYFOUR
 
38
#endif
55
39
#ifdef BYFOUR
56
 
#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
57
 
                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58
40
   local unsigned long crc32_little OF((unsigned long,
59
41
                        const unsigned char FAR *, unsigned));
60
42
   local unsigned long crc32_big OF((unsigned long,
68
50
local unsigned long gf2_matrix_times OF((unsigned long *mat,
69
51
                                         unsigned long vec));
70
52
local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
 
53
local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
 
54
 
71
55
 
72
56
#ifdef DYNAMIC_CRC_TABLE
73
57
 
74
58
local volatile int crc_table_empty = 1;
75
 
local unsigned long FAR crc_table[TBLS][256];
 
59
local z_crc_t FAR crc_table[TBLS][256];
76
60
local void make_crc_table OF((void));
77
61
#ifdef MAKECRCH
78
 
   local void write_table OF((FILE *, const unsigned long FAR *));
 
62
   local void write_table OF((FILE *, const z_crc_t FAR *));
79
63
#endif /* MAKECRCH */
80
64
/*
81
65
  Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
105
89
*/
106
90
local void make_crc_table()
107
91
{
108
 
    unsigned long c;
 
92
    z_crc_t c;
109
93
    int n, k;
110
 
    unsigned long poly;                 /* polynomial exclusive-or pattern */
 
94
    z_crc_t poly;                       /* polynomial exclusive-or pattern */
111
95
    /* terms of polynomial defining this crc (except x^32): */
112
96
    static volatile int first = 1;      /* flag to limit concurrent making */
113
97
    static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
119
103
        first = 0;
120
104
 
121
105
        /* make exclusive-or pattern from polynomial (0xedb88320UL) */
122
 
        poly = 0UL;
123
 
        for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
124
 
            poly |= 1UL << (31 - p[n]);
 
106
        poly = 0;
 
107
        for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
 
108
            poly |= (z_crc_t)1 << (31 - p[n]);
125
109
 
126
110
        /* generate a crc for every 8-bit value */
127
111
        for (n = 0; n < 256; n++) {
128
 
            c = (unsigned long)n;
 
112
            c = (z_crc_t)n;
129
113
            for (k = 0; k < 8; k++)
130
114
                c = c & 1 ? poly ^ (c >> 1) : c >> 1;
131
115
            crc_table[0][n] = c;
136
120
           and then the byte reversal of those as well as the first table */
137
121
        for (n = 0; n < 256; n++) {
138
122
            c = crc_table[0][n];
139
 
            crc_table[4][n] = REV(c);
 
123
            crc_table[4][n] = ZSWAP32(c);
140
124
            for (k = 1; k < 4; k++) {
141
125
                c = crc_table[0][c & 0xff] ^ (c >> 8);
142
126
                crc_table[k][n] = c;
143
 
                crc_table[k + 4][n] = REV(c);
 
127
                crc_table[k + 4][n] = ZSWAP32(c);
144
128
            }
145
129
        }
146
130
#endif /* BYFOUR */
162
146
        if (out == NULL) return;
163
147
        fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
164
148
        fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
165
 
        fprintf(out, "local const unsigned long FAR ");
 
149
        fprintf(out, "local const z_crc_t FAR ");
166
150
        fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
167
151
        write_table(out, crc_table[0]);
168
152
#  ifdef BYFOUR
182
166
#ifdef MAKECRCH
183
167
local void write_table(out, table)
184
168
    FILE *out;
185
 
    const unsigned long FAR *table;
 
169
    const z_crc_t FAR *table;
186
170
{
187
171
    int n;
188
172
 
189
173
    for (n = 0; n < 256; n++)
190
 
        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
 
174
        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ",
 
175
                (unsigned long)(table[n]),
191
176
                n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
192
177
}
193
178
#endif /* MAKECRCH */
202
187
/* =========================================================================
203
188
 * This function can be used by asm versions of crc32()
204
189
 */
205
 
const unsigned long FAR * ZEXPORT get_crc_table()
 
190
const z_crc_t FAR * ZEXPORT get_crc_table()
206
191
{
207
192
#ifdef DYNAMIC_CRC_TABLE
208
193
    if (crc_table_empty)
209
194
        make_crc_table();
210
195
#endif /* DYNAMIC_CRC_TABLE */
211
 
    return (const unsigned long FAR *)crc_table;
 
196
    return (const z_crc_t FAR *)crc_table;
212
197
}
213
198
 
214
199
/* ========================================================================= */
219
204
unsigned long ZEXPORT crc32(crc, buf, len)
220
205
    unsigned long crc;
221
206
    const unsigned char FAR *buf;
222
 
    unsigned len;
 
207
    uInt len;
223
208
{
224
209
    if (buf == Z_NULL) return 0UL;
225
210
 
230
215
 
231
216
#ifdef BYFOUR
232
217
    if (sizeof(void *) == sizeof(ptrdiff_t)) {
233
 
        u4 endian;
 
218
        z_crc_t endian;
234
219
 
235
220
        endian = 1;
236
221
        if (*((unsigned char *)(&endian)))
264
249
    const unsigned char FAR *buf;
265
250
    unsigned len;
266
251
{
267
 
    register u4 c;
268
 
    register const u4 FAR *buf4;
 
252
    register z_crc_t c;
 
253
    register const z_crc_t FAR *buf4;
269
254
 
270
 
    c = (u4)crc;
 
255
    c = (z_crc_t)crc;
271
256
    c = ~c;
272
257
    while (len && ((ptrdiff_t)buf & 3)) {
273
258
        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
274
259
        len--;
275
260
    }
276
261
 
277
 
    buf4 = (const u4 FAR *)(const void FAR *)buf;
 
262
    buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
278
263
    while (len >= 32) {
279
264
        DOLIT32;
280
265
        len -= 32;
304
289
    const unsigned char FAR *buf;
305
290
    unsigned len;
306
291
{
307
 
    register u4 c;
308
 
    register const u4 FAR *buf4;
 
292
    register z_crc_t c;
 
293
    register const z_crc_t FAR *buf4;
309
294
 
310
 
    c = REV((u4)crc);
 
295
    c = ZSWAP32((z_crc_t)crc);
311
296
    c = ~c;
312
297
    while (len && ((ptrdiff_t)buf & 3)) {
313
298
        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
314
299
        len--;
315
300
    }
316
301
 
317
 
    buf4 = (const u4 FAR *)(const void FAR *)buf;
 
302
    buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
318
303
    buf4--;
319
304
    while (len >= 32) {
320
305
        DOBIG32;
331
316
        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
332
317
    } while (--len);
333
318
    c = ~c;
334
 
    return (unsigned long)(REV(c));
 
319
    return (unsigned long)(ZSWAP32(c));
335
320
}
336
321
 
337
322
#endif /* BYFOUR */
367
352
}
368
353
 
369
354
/* ========================================================================= */
370
 
uLong ZEXPORT crc32_combine(crc1, crc2, len2)
 
355
local uLong crc32_combine_(crc1, crc2, len2)
371
356
    uLong crc1;
372
357
    uLong crc2;
373
 
    z_off_t len2;
 
358
    z_off64_t len2;
374
359
{
375
360
    int n;
376
361
    unsigned long row;
377
362
    unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
378
363
    unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
379
364
 
380
 
    /* degenerate case */
381
 
    if (len2 == 0)
 
365
    /* degenerate case (also disallow negative lengths) */
 
366
    if (len2 <= 0)
382
367
        return crc1;
383
368
 
384
369
    /* put operator for one zero bit in odd */
385
 
    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
 
370
    odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
386
371
    row = 1;
387
372
    for (n = 1; n < GF2_DIM; n++) {
388
373
        odd[n] = row;
421
406
    crc1 ^= crc2;
422
407
    return crc1;
423
408
}
 
409
 
 
410
/* ========================================================================= */
 
411
uLong ZEXPORT crc32_combine(crc1, crc2, len2)
 
412
    uLong crc1;
 
413
    uLong crc2;
 
414
    z_off_t len2;
 
415
{
 
416
    return crc32_combine_(crc1, crc2, len2);
 
417
}
 
418
 
 
419
uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
 
420
    uLong crc1;
 
421
    uLong crc2;
 
422
    z_off64_t len2;
 
423
{
 
424
    return crc32_combine_(crc1, crc2, len2);
 
425
}