Coverage Report

Created: 2026-01-09 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib/crc32.c
Line
Count
Source
1
/* crc32.c -- compute the CRC-32 of a data stream
2
 * Copyright (C) 1995-2022 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 *
5
 * This interleaved implementation of a CRC makes use of pipelined multiple
6
 * arithmetic-logic units, commonly found in modern CPU cores. It is due to
7
 * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
8
 */
9
10
/* @(#) $Id$ */
11
12
/*
13
  Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
14
  protection on the static variables used to control the first-use generation
15
  of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
16
  first call get_crc_table() to initialize the tables before allowing more than
17
  one thread to use crc32().
18
19
  MAKECRCH can be #defined to write out crc32.h. A main() routine is also
20
  produced, so that this one source file can be compiled to an executable.
21
 */
22
23
#ifdef MAKECRCH
24
#  include <stdio.h>
25
#  ifndef DYNAMIC_CRC_TABLE
26
#    define DYNAMIC_CRC_TABLE
27
#  endif /* !DYNAMIC_CRC_TABLE */
28
#endif /* MAKECRCH */
29
30
#include "zutil.h"      /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */
31
32
 /*
33
  A CRC of a message is computed on N braids of words in the message, where
34
  each word consists of W bytes (4 or 8). If N is 3, for example, then three
35
  running sparse CRCs are calculated respectively on each braid, at these
36
  indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
37
  This is done starting at a word boundary, and continues until as many blocks
38
  of N * W bytes as are available have been processed. The results are combined
39
  into a single CRC at the end. For this code, N must be in the range 1..6 and
40
  W must be 4 or 8. The upper limit on N can be increased if desired by adding
41
  more #if blocks, extending the patterns apparent in the code. In addition,
42
  crc32.h would need to be regenerated, if the maximum N value is increased.
43
44
  N and W are chosen empirically by benchmarking the execution time on a given
45
  processor. The choices for N and W below were based on testing on Intel Kaby
46
  Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64
47
  Octeon II processors. The Intel, AMD, and ARM processors were all fastest
48
  with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4.
49
  They were all tested with either gcc or clang, all using the -O3 optimization
50
  level. Your mileage may vary.
51
 */
52
53
/* Define N */
54
#ifdef Z_TESTN
55
#  define N Z_TESTN
56
#else
57
0
#  define N 5
58
#endif
59
#if N < 1 || N > 6
60
#  error N must be in 1..6
61
#endif
62
63
/*
64
  z_crc_t must be at least 32 bits. z_word_t must be at least as long as
65
  z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and
66
  that bytes are eight bits.
67
 */
68
69
/*
70
  Define W and the associated z_word_t type. If W is not defined, then a
71
  braided calculation is not used, and the associated tables and code are not
72
  compiled.
73
 */
74
#ifdef Z_TESTW
75
#  if Z_TESTW-1 != -1
76
#    define W Z_TESTW
77
#  endif
78
#else
79
#  ifdef MAKECRCH
80
#    define W 8         /* required for MAKECRCH */
81
#  else
82
#    if defined(__x86_64__) || defined(__aarch64__)
83
0
#      define W 8
84
#    else
85
#      define W 4
86
#    endif
87
#  endif
88
#endif
89
#ifdef W
90
#  if W == 8 && defined(Z_U8)
91
     typedef Z_U8 z_word_t;
92
#  elif defined(Z_U4)
93
#    undef W
94
#    define W 4
95
     typedef Z_U4 z_word_t;
96
#  else
97
#    undef W
98
#  endif
99
#endif
100
101
/* If available, use the ARM processor CRC32 instruction. */
102
#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
103
#  define ARMCRC32
104
#endif
105
106
#if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
107
/*
108
  Swap the bytes in a z_word_t to convert between little and big endian. Any
109
  self-respecting compiler will optimize this to a single machine byte-swap
110
  instruction, if one is available. This assumes that word_t is either 32 bits
111
  or 64 bits.
112
 */
113
0
local z_word_t byte_swap(z_word_t word) {
114
0
#  if W == 8
115
0
    return
116
0
        (word & 0xff00000000000000) >> 56 |
117
0
        (word & 0xff000000000000) >> 40 |
118
0
        (word & 0xff0000000000) >> 24 |
119
0
        (word & 0xff00000000) >> 8 |
120
0
        (word & 0xff000000) << 8 |
121
0
        (word & 0xff0000) << 24 |
122
0
        (word & 0xff00) << 40 |
123
0
        (word & 0xff) << 56;
124
#  else   /* W == 4 */
125
    return
126
        (word & 0xff000000) >> 24 |
127
        (word & 0xff0000) >> 8 |
128
        (word & 0xff00) << 8 |
129
        (word & 0xff) << 24;
130
#  endif
131
0
}
132
#endif
133
134
#ifdef DYNAMIC_CRC_TABLE
135
/* =========================================================================
136
 * Table of powers of x for combining CRC-32s, filled in by make_crc_table()
137
 * below.
138
 */
139
   local z_crc_t FAR x2n_table[32];
140
#else
141
/* =========================================================================
142
 * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
143
 * of x for combining CRC-32s, all made by make_crc_table().
144
 */
145
#  include "crc32.h"
146
#endif
147
148
/* CRC polynomial. */
149
0
#define POLY 0xedb88320         /* p(x) reflected, with x^32 implied */
150
151
/*
152
  Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
153
  reflected. For speed, this requires that a not be zero.
154
 */
155
0
local z_crc_t multmodp(z_crc_t a, z_crc_t b) {
156
0
    z_crc_t m, p;
157
158
0
    m = (z_crc_t)1 << 31;
159
0
    p = 0;
160
0
    for (;;) {
161
0
        if (a & m) {
162
0
            p ^= b;
163
0
            if ((a & (m - 1)) == 0)
164
0
                break;
165
0
        }
166
0
        m >>= 1;
167
0
        b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
168
0
    }
169
0
    return p;
170
0
}
171
172
/*
173
  Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
174
  initialized.
175
 */
176
0
local z_crc_t x2nmodp(z_off64_t n, unsigned k) {
177
0
    z_crc_t p;
178
179
0
    p = (z_crc_t)1 << 31;           /* x^0 == 1 */
180
0
    while (n) {
181
0
        if (n & 1)
182
0
            p = multmodp(x2n_table[k & 31], p);
183
0
        n >>= 1;
184
0
        k++;
185
0
    }
186
0
    return p;
187
0
}
188
189
#ifdef DYNAMIC_CRC_TABLE
190
/* =========================================================================
191
 * Build the tables for byte-wise and braided CRC-32 calculations, and a table
192
 * of powers of x for combining CRC-32s.
193
 */
194
local z_crc_t FAR crc_table[256];
195
#ifdef W
196
   local z_word_t FAR crc_big_table[256];
197
   local z_crc_t FAR crc_braid_table[W][256];
198
   local z_word_t FAR crc_braid_big_table[W][256];
199
   local void braid(z_crc_t [][256], z_word_t [][256], int, int);
200
#endif
201
#ifdef MAKECRCH
202
   local void write_table(FILE *, const z_crc_t FAR *, int);
203
   local void write_table32hi(FILE *, const z_word_t FAR *, int);
204
   local void write_table64(FILE *, const z_word_t FAR *, int);
205
#endif /* MAKECRCH */
206
207
/* State for once(). */
208
local z_once_t made = Z_ONCE_INIT;
209
210
/*
211
  Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
212
  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.
213
214
  Polynomials over GF(2) are represented in binary, one bit per coefficient,
215
  with the lowest powers in the most significant bit. Then adding polynomials
216
  is just exclusive-or, and multiplying a polynomial by x is a right shift by
217
  one. If we call the above polynomial p, and represent a byte as the
218
  polynomial q, also with the lowest power in the most significant bit (so the
219
  byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p,
220
  where a mod b means the remainder after dividing a by b.
221
222
  This calculation is done using the shift-register method of multiplying and
223
  taking the remainder. The register is initialized to zero, and for each
224
  incoming bit, x^32 is added mod p to the register if the bit is a one (where
225
  x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x
226
  (which is shifting right by one and adding x^32 mod p if the bit shifted out
227
  is a one). We start with the highest power (least significant bit) of q and
228
  repeat for all eight bits of q.
229
230
  The table is simply the CRC of all possible eight bit values. This is all the
231
  information needed to generate CRCs on data a byte at a time for all
232
  combinations of CRC register values and incoming bytes.
233
 */
234
235
local void make_crc_table(void) {
236
    unsigned i, j, n;
237
    z_crc_t p;
238
239
    /* initialize the CRC of bytes tables */
240
    for (i = 0; i < 256; i++) {
241
        p = i;
242
        for (j = 0; j < 8; j++)
243
            p = p & 1 ? (p >> 1) ^ POLY : p >> 1;
244
        crc_table[i] = p;
245
#ifdef W
246
        crc_big_table[i] = byte_swap(p);
247
#endif
248
    }
249
250
    /* initialize the x^2^n mod p(x) table */
251
    p = (z_crc_t)1 << 30;         /* x^1 */
252
    x2n_table[0] = p;
253
    for (n = 1; n < 32; n++)
254
        x2n_table[n] = p = multmodp(p, p);
255
256
#ifdef W
257
    /* initialize the braiding tables -- needs x2n_table[] */
258
    braid(crc_braid_table, crc_braid_big_table, N, W);
259
#endif
260
261
#ifdef MAKECRCH
262
    {
263
        /*
264
          The crc32.h header file contains tables for both 32-bit and 64-bit
265
          z_word_t's, and so requires a 64-bit type be available. In that case,
266
          z_word_t must be defined to be 64-bits. This code then also generates
267
          and writes out the tables for the case that z_word_t is 32 bits.
268
         */
269
#if !defined(W) || W != 8
270
#  error Need a 64-bit integer type in order to generate crc32.h.
271
#endif
272
        FILE *out;
273
        int k, n;
274
        z_crc_t ltl[8][256];
275
        z_word_t big[8][256];
276
277
        out = fopen("crc32.h", "w");
278
        if (out == NULL) return;
279
280
        /* write out little-endian CRC table to crc32.h */
281
        fprintf(out,
282
            "/* crc32.h -- tables for rapid CRC calculation\n"
283
            " * Generated automatically by crc32.c\n */\n"
284
            "\n"
285
            "local const z_crc_t FAR crc_table[] = {\n"
286
            "    ");
287
        write_table(out, crc_table, 256);
288
        fprintf(out,
289
            "};\n");
290
291
        /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */
292
        fprintf(out,
293
            "\n"
294
            "#ifdef W\n"
295
            "\n"
296
            "#if W == 8\n"
297
            "\n"
298
            "local const z_word_t FAR crc_big_table[] = {\n"
299
            "    ");
300
        write_table64(out, crc_big_table, 256);
301
        fprintf(out,
302
            "};\n");
303
304
        /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */
305
        fprintf(out,
306
            "\n"
307
            "#else /* W == 4 */\n"
308
            "\n"
309
            "local const z_word_t FAR crc_big_table[] = {\n"
310
            "    ");
311
        write_table32hi(out, crc_big_table, 256);
312
        fprintf(out,
313
            "};\n"
314
            "\n"
315
            "#endif\n");
316
317
        /* write out braid tables for each value of N */
318
        for (n = 1; n <= 6; n++) {
319
            fprintf(out,
320
            "\n"
321
            "#if N == %d\n", n);
322
323
            /* compute braid tables for this N and 64-bit word_t */
324
            braid(ltl, big, n, 8);
325
326
            /* write out braid tables for 64-bit z_word_t to crc32.h */
327
            fprintf(out,
328
            "\n"
329
            "#if W == 8\n"
330
            "\n"
331
            "local const z_crc_t FAR crc_braid_table[][256] = {\n");
332
            for (k = 0; k < 8; k++) {
333
                fprintf(out, "   {");
334
                write_table(out, ltl[k], 256);
335
                fprintf(out, "}%s", k < 7 ? ",\n" : "");
336
            }
337
            fprintf(out,
338
            "};\n"
339
            "\n"
340
            "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
341
            for (k = 0; k < 8; k++) {
342
                fprintf(out, "   {");
343
                write_table64(out, big[k], 256);
344
                fprintf(out, "}%s", k < 7 ? ",\n" : "");
345
            }
346
            fprintf(out,
347
            "};\n");
348
349
            /* compute braid tables for this N and 32-bit word_t */
350
            braid(ltl, big, n, 4);
351
352
            /* write out braid tables for 32-bit z_word_t to crc32.h */
353
            fprintf(out,
354
            "\n"
355
            "#else /* W == 4 */\n"
356
            "\n"
357
            "local const z_crc_t FAR crc_braid_table[][256] = {\n");
358
            for (k = 0; k < 4; k++) {
359
                fprintf(out, "   {");
360
                write_table(out, ltl[k], 256);
361
                fprintf(out, "}%s", k < 3 ? ",\n" : "");
362
            }
363
            fprintf(out,
364
            "};\n"
365
            "\n"
366
            "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
367
            for (k = 0; k < 4; k++) {
368
                fprintf(out, "   {");
369
                write_table32hi(out, big[k], 256);
370
                fprintf(out, "}%s", k < 3 ? ",\n" : "");
371
            }
372
            fprintf(out,
373
            "};\n"
374
            "\n"
375
            "#endif\n"
376
            "\n"
377
            "#endif\n");
378
        }
379
        fprintf(out,
380
            "\n"
381
            "#endif\n");
382
383
        /* write out zeros operator table to crc32.h */
384
        fprintf(out,
385
            "\n"
386
            "local const z_crc_t FAR x2n_table[] = {\n"
387
            "    ");
388
        write_table(out, x2n_table, 32);
389
        fprintf(out,
390
            "};\n");
391
        fclose(out);
392
    }
393
#endif /* MAKECRCH */
394
}
395
396
#ifdef MAKECRCH
397
398
/*
399
   Write the 32-bit values in table[0..k-1] to out, five per line in
400
   hexadecimal separated by commas.
401
 */
402
local void write_table(FILE *out, const z_crc_t FAR *table, int k) {
403
    int n;
404
405
    for (n = 0; n < k; n++)
406
        fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : "    ",
407
                (unsigned long)(table[n]),
408
                n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
409
}
410
411
/*
412
   Write the high 32-bits of each value in table[0..k-1] to out, five per line
413
   in hexadecimal separated by commas.
414
 */
415
local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) {
416
    int n;
417
418
    for (n = 0; n < k; n++)
419
        fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : "    ",
420
                (unsigned long)(table[n] >> 32),
421
                n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
422
}
423
424
/*
425
  Write the 64-bit values in table[0..k-1] to out, three per line in
426
  hexadecimal separated by commas. This assumes that if there is a 64-bit
427
  type, then there is also a long long integer type, and it is at least 64
428
  bits. If not, then the type cast and format string can be adjusted
429
  accordingly.
430
 */
431
local void write_table64(FILE *out, const z_word_t FAR *table, int k) {
432
    int n;
433
434
    for (n = 0; n < k; n++)
435
        fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : "    ",
436
                (unsigned long long)(table[n]),
437
                n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", "));
438
}
439
440
/* Actually do the deed. */
441
int main(void) {
442
    make_crc_table();
443
    return 0;
444
}
445
446
#endif /* MAKECRCH */
447
448
#ifdef W
449
/*
450
  Generate the little and big-endian braid tables for the given n and z_word_t
451
  size w. Each array must have room for w blocks of 256 elements.
452
 */
453
local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) {
454
    int k;
455
    z_crc_t i, p, q;
456
    for (k = 0; k < w; k++) {
457
        p = x2nmodp((n * w + 3 - k) << 3, 0);
458
        ltl[k][0] = 0;
459
        big[w - 1 - k][0] = 0;
460
        for (i = 1; i < 256; i++) {
461
            ltl[k][i] = q = multmodp(i << 24, p);
462
            big[w - 1 - k][i] = byte_swap(q);
463
        }
464
    }
465
}
466
#endif
467
468
#endif /* DYNAMIC_CRC_TABLE */
469
470
/* =========================================================================
471
 * This function can be used by asm versions of crc32(), and to force the
472
 * generation of the CRC tables in a threaded application.
473
 */
474
0
const z_crc_t FAR * ZEXPORT get_crc_table(void) {
475
#ifdef DYNAMIC_CRC_TABLE
476
    z_once(&made, make_crc_table);
477
#endif /* DYNAMIC_CRC_TABLE */
478
0
    return (const z_crc_t FAR *)crc_table;
479
0
}
480
481
/* =========================================================================
482
 * Use ARM machine instructions if available. This will compute the CRC about
483
 * ten times faster than the braided calculation. This code does not check for
484
 * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will
485
 * only be defined if the compilation specifies an ARM processor architecture
486
 * that has the instructions. For example, compiling with -march=armv8.1-a or
487
 * -march=armv8-a+crc, or -march=native if the compile machine has the crc32
488
 * instructions.
489
 */
490
#ifdef ARMCRC32
491
492
/*
493
   Constants empirically determined to maximize speed. These values are from
494
   measurements on a Cortex-A57. Your mileage may vary.
495
 */
496
#define Z_BATCH 3990                /* number of words in a batch */
497
#define Z_BATCH_ZEROS 0xa10d3d0c    /* computed from Z_BATCH = 3990 */
498
#define Z_BATCH_MIN 800             /* fewest words in a final batch */
499
500
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
501
                              z_size_t len) {
502
    z_crc_t val;
503
    z_word_t crc1, crc2;
504
    const z_word_t *word;
505
    z_word_t val0, val1, val2;
506
    z_size_t last, last2, i;
507
    z_size_t num;
508
509
    /* Return initial CRC, if requested. */
510
    if (buf == Z_NULL) return 0;
511
512
#ifdef DYNAMIC_CRC_TABLE
513
    z_once(&made, make_crc_table);
514
#endif /* DYNAMIC_CRC_TABLE */
515
516
    /* Pre-condition the CRC */
517
    crc = (~crc) & 0xffffffff;
518
519
    /* Compute the CRC up to a word boundary. */
520
    while (len && ((z_size_t)buf & 7) != 0) {
521
        len--;
522
        val = *buf++;
523
        __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
524
    }
525
526
    /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */
527
    word = (z_word_t const *)buf;
528
    num = len >> 3;
529
    len &= 7;
530
531
    /* Do three interleaved CRCs to realize the throughput of one crc32x
532
       instruction per cycle. Each CRC is calculated on Z_BATCH words. The
533
       three CRCs are combined into a single CRC after each set of batches. */
534
    while (num >= 3 * Z_BATCH) {
535
        crc1 = 0;
536
        crc2 = 0;
537
        for (i = 0; i < Z_BATCH; i++) {
538
            val0 = word[i];
539
            val1 = word[i + Z_BATCH];
540
            val2 = word[i + 2 * Z_BATCH];
541
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
542
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
543
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
544
        }
545
        word += 3 * Z_BATCH;
546
        num -= 3 * Z_BATCH;
547
        crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
548
        crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
549
    }
550
551
    /* Do one last smaller batch with the remaining words, if there are enough
552
       to pay for the combination of CRCs. */
553
    last = num / 3;
554
    if (last >= Z_BATCH_MIN) {
555
        last2 = last << 1;
556
        crc1 = 0;
557
        crc2 = 0;
558
        for (i = 0; i < last; i++) {
559
            val0 = word[i];
560
            val1 = word[i + last];
561
            val2 = word[i + last2];
562
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
563
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
564
            __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
565
        }
566
        word += 3 * last;
567
        num -= 3 * last;
568
        val = x2nmodp(last, 6);
569
        crc = multmodp(val, crc) ^ crc1;
570
        crc = multmodp(val, crc) ^ crc2;
571
    }
572
573
    /* Compute the CRC on any remaining words. */
574
    for (i = 0; i < num; i++) {
575
        val0 = word[i];
576
        __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
577
    }
578
    word += num;
579
580
    /* Complete the CRC on any remaining bytes. */
581
    buf = (const unsigned char FAR *)word;
582
    while (len) {
583
        len--;
584
        val = *buf++;
585
        __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
586
    }
587
588
    /* Return the CRC, post-conditioned. */
589
    return crc ^ 0xffffffff;
590
}
591
592
#else
593
594
#ifdef W
595
596
/*
597
  Return the CRC of the W bytes in the word_t data, taking the
598
  least-significant byte of the word as the first byte of data, without any pre
599
  or post conditioning. This is used to combine the CRCs of each braid.
600
 */
601
0
local z_crc_t crc_word(z_word_t data) {
602
0
    int k;
603
0
    for (k = 0; k < W; k++)
604
0
        data = (data >> 8) ^ crc_table[data & 0xff];
605
0
    return (z_crc_t)data;
606
0
}
607
608
0
local z_word_t crc_word_big(z_word_t data) {
609
0
    int k;
610
0
    for (k = 0; k < W; k++)
611
0
        data = (data << 8) ^
612
0
            crc_big_table[(data >> ((W - 1) << 3)) & 0xff];
613
0
    return data;
614
0
}
615
616
#endif
617
618
/* ========================================================================= */
619
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
620
0
                              z_size_t len) {
621
    /* Return initial CRC, if requested. */
622
0
    if (buf == Z_NULL) return 0;
623
624
#ifdef DYNAMIC_CRC_TABLE
625
    z_once(&made, make_crc_table);
626
#endif /* DYNAMIC_CRC_TABLE */
627
628
    /* Pre-condition the CRC */
629
0
    crc = (~crc) & 0xffffffff;
630
631
0
#ifdef W
632
633
    /* If provided enough bytes, do a braided CRC calculation. */
634
0
    if (len >= N * W + W - 1) {
635
0
        z_size_t blks;
636
0
        z_word_t const *words;
637
0
        unsigned endian;
638
0
        int k;
639
640
        /* Compute the CRC up to a z_word_t boundary. */
641
0
        while (len && ((z_size_t)buf & (W - 1)) != 0) {
642
0
            len--;
643
0
            crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
644
0
        }
645
646
        /* Compute the CRC on as many N z_word_t blocks as are available. */
647
0
        blks = len / (N * W);
648
0
        len -= blks * N * W;
649
0
        words = (z_word_t const *)buf;
650
651
        /* Do endian check at execution time instead of compile time, since ARM
652
           processors can change the endianness at execution time. If the
653
           compiler knows what the endianness will be, it can optimize out the
654
           check and the unused branch. */
655
0
        endian = 1;
656
0
        if (*(unsigned char *)&endian) {
657
            /* Little endian. */
658
659
0
            z_crc_t crc0;
660
0
            z_word_t word0;
661
0
#if N > 1
662
0
            z_crc_t crc1;
663
0
            z_word_t word1;
664
0
#if N > 2
665
0
            z_crc_t crc2;
666
0
            z_word_t word2;
667
0
#if N > 3
668
0
            z_crc_t crc3;
669
0
            z_word_t word3;
670
0
#if N > 4
671
0
            z_crc_t crc4;
672
0
            z_word_t word4;
673
#if N > 5
674
            z_crc_t crc5;
675
            z_word_t word5;
676
#endif
677
0
#endif
678
0
#endif
679
0
#endif
680
0
#endif
681
682
            /* Initialize the CRC for each braid. */
683
0
            crc0 = crc;
684
0
#if N > 1
685
0
            crc1 = 0;
686
0
#if N > 2
687
0
            crc2 = 0;
688
0
#if N > 3
689
0
            crc3 = 0;
690
0
#if N > 4
691
0
            crc4 = 0;
692
#if N > 5
693
            crc5 = 0;
694
#endif
695
0
#endif
696
0
#endif
697
0
#endif
698
0
#endif
699
700
            /*
701
              Process the first blks-1 blocks, computing the CRCs on each braid
702
              independently.
703
             */
704
0
            while (--blks) {
705
                /* Load the word for each braid into registers. */
706
0
                word0 = crc0 ^ words[0];
707
0
#if N > 1
708
0
                word1 = crc1 ^ words[1];
709
0
#if N > 2
710
0
                word2 = crc2 ^ words[2];
711
0
#if N > 3
712
0
                word3 = crc3 ^ words[3];
713
0
#if N > 4
714
0
                word4 = crc4 ^ words[4];
715
#if N > 5
716
                word5 = crc5 ^ words[5];
717
#endif
718
0
#endif
719
0
#endif
720
0
#endif
721
0
#endif
722
0
                words += N;
723
724
                /* Compute and update the CRC for each word. The loop should
725
                   get unrolled. */
726
0
                crc0 = crc_braid_table[0][word0 & 0xff];
727
0
#if N > 1
728
0
                crc1 = crc_braid_table[0][word1 & 0xff];
729
0
#if N > 2
730
0
                crc2 = crc_braid_table[0][word2 & 0xff];
731
0
#if N > 3
732
0
                crc3 = crc_braid_table[0][word3 & 0xff];
733
0
#if N > 4
734
0
                crc4 = crc_braid_table[0][word4 & 0xff];
735
#if N > 5
736
                crc5 = crc_braid_table[0][word5 & 0xff];
737
#endif
738
0
#endif
739
0
#endif
740
0
#endif
741
0
#endif
742
0
                for (k = 1; k < W; k++) {
743
0
                    crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
744
0
#if N > 1
745
0
                    crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
746
0
#if N > 2
747
0
                    crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
748
0
#if N > 3
749
0
                    crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
750
0
#if N > 4
751
0
                    crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
752
#if N > 5
753
                    crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
754
#endif
755
0
#endif
756
0
#endif
757
0
#endif
758
0
#endif
759
0
                }
760
0
            }
761
762
            /*
763
              Process the last block, combining the CRCs of the N braids at the
764
              same time.
765
             */
766
0
            crc = crc_word(crc0 ^ words[0]);
767
0
#if N > 1
768
0
            crc = crc_word(crc1 ^ words[1] ^ crc);
769
0
#if N > 2
770
0
            crc = crc_word(crc2 ^ words[2] ^ crc);
771
0
#if N > 3
772
0
            crc = crc_word(crc3 ^ words[3] ^ crc);
773
0
#if N > 4
774
0
            crc = crc_word(crc4 ^ words[4] ^ crc);
775
#if N > 5
776
            crc = crc_word(crc5 ^ words[5] ^ crc);
777
#endif
778
0
#endif
779
0
#endif
780
0
#endif
781
0
#endif
782
0
            words += N;
783
0
        }
784
0
        else {
785
            /* Big endian. */
786
787
0
            z_word_t crc0, word0, comb;
788
0
#if N > 1
789
0
            z_word_t crc1, word1;
790
0
#if N > 2
791
0
            z_word_t crc2, word2;
792
0
#if N > 3
793
0
            z_word_t crc3, word3;
794
0
#if N > 4
795
0
            z_word_t crc4, word4;
796
#if N > 5
797
            z_word_t crc5, word5;
798
#endif
799
0
#endif
800
0
#endif
801
0
#endif
802
0
#endif
803
804
            /* Initialize the CRC for each braid. */
805
0
            crc0 = byte_swap(crc);
806
0
#if N > 1
807
0
            crc1 = 0;
808
0
#if N > 2
809
0
            crc2 = 0;
810
0
#if N > 3
811
0
            crc3 = 0;
812
0
#if N > 4
813
0
            crc4 = 0;
814
#if N > 5
815
            crc5 = 0;
816
#endif
817
0
#endif
818
0
#endif
819
0
#endif
820
0
#endif
821
822
            /*
823
              Process the first blks-1 blocks, computing the CRCs on each braid
824
              independently.
825
             */
826
0
            while (--blks) {
827
                /* Load the word for each braid into registers. */
828
0
                word0 = crc0 ^ words[0];
829
0
#if N > 1
830
0
                word1 = crc1 ^ words[1];
831
0
#if N > 2
832
0
                word2 = crc2 ^ words[2];
833
0
#if N > 3
834
0
                word3 = crc3 ^ words[3];
835
0
#if N > 4
836
0
                word4 = crc4 ^ words[4];
837
#if N > 5
838
                word5 = crc5 ^ words[5];
839
#endif
840
0
#endif
841
0
#endif
842
0
#endif
843
0
#endif
844
0
                words += N;
845
846
                /* Compute and update the CRC for each word. The loop should
847
                   get unrolled. */
848
0
                crc0 = crc_braid_big_table[0][word0 & 0xff];
849
0
#if N > 1
850
0
                crc1 = crc_braid_big_table[0][word1 & 0xff];
851
0
#if N > 2
852
0
                crc2 = crc_braid_big_table[0][word2 & 0xff];
853
0
#if N > 3
854
0
                crc3 = crc_braid_big_table[0][word3 & 0xff];
855
0
#if N > 4
856
0
                crc4 = crc_braid_big_table[0][word4 & 0xff];
857
#if N > 5
858
                crc5 = crc_braid_big_table[0][word5 & 0xff];
859
#endif
860
0
#endif
861
0
#endif
862
0
#endif
863
0
#endif
864
0
                for (k = 1; k < W; k++) {
865
0
                    crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
866
0
#if N > 1
867
0
                    crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
868
0
#if N > 2
869
0
                    crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
870
0
#if N > 3
871
0
                    crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
872
0
#if N > 4
873
0
                    crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
874
#if N > 5
875
                    crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
876
#endif
877
0
#endif
878
0
#endif
879
0
#endif
880
0
#endif
881
0
                }
882
0
            }
883
884
            /*
885
              Process the last block, combining the CRCs of the N braids at the
886
              same time.
887
             */
888
0
            comb = crc_word_big(crc0 ^ words[0]);
889
0
#if N > 1
890
0
            comb = crc_word_big(crc1 ^ words[1] ^ comb);
891
0
#if N > 2
892
0
            comb = crc_word_big(crc2 ^ words[2] ^ comb);
893
0
#if N > 3
894
0
            comb = crc_word_big(crc3 ^ words[3] ^ comb);
895
0
#if N > 4
896
0
            comb = crc_word_big(crc4 ^ words[4] ^ comb);
897
#if N > 5
898
            comb = crc_word_big(crc5 ^ words[5] ^ comb);
899
#endif
900
0
#endif
901
0
#endif
902
0
#endif
903
0
#endif
904
0
            words += N;
905
0
            crc = byte_swap(comb);
906
0
        }
907
908
        /*
909
          Update the pointer to the remaining bytes to process.
910
         */
911
0
        buf = (unsigned char const *)words;
912
0
    }
913
914
0
#endif /* W */
915
916
    /* Complete the computation of the CRC on any remaining bytes. */
917
0
    while (len >= 8) {
918
0
        len -= 8;
919
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
920
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
921
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
922
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
923
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
924
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
925
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
926
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
927
0
    }
928
0
    while (len) {
929
0
        len--;
930
0
        crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
931
0
    }
932
933
    /* Return the CRC, post-conditioned. */
934
0
    return crc ^ 0xffffffff;
935
0
}
936
937
#endif
938
939
/* ========================================================================= */
940
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf,
941
0
                            uInt len) {
942
0
    return crc32_z(crc, buf, len);
943
0
}
944
945
/* ========================================================================= */
946
0
uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) {
947
0
    if (len2 < 0)
948
0
        return 0;
949
#ifdef DYNAMIC_CRC_TABLE
950
    z_once(&made, make_crc_table);
951
#endif /* DYNAMIC_CRC_TABLE */
952
0
    return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff);
953
0
}
954
955
/* ========================================================================= */
956
0
uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) {
957
0
    return crc32_combine64(crc1, crc2, (z_off64_t)len2);
958
0
}
959
960
/* ========================================================================= */
961
0
uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) {
962
0
    if (len2 < 0)
963
0
        return 0;
964
#ifdef DYNAMIC_CRC_TABLE
965
    z_once(&made, make_crc_table);
966
#endif /* DYNAMIC_CRC_TABLE */
967
0
    return x2nmodp(len2, 3);
968
0
}
969
970
/* ========================================================================= */
971
0
uLong ZEXPORT crc32_combine_gen(z_off_t len2) {
972
0
    return crc32_combine_gen64((z_off64_t)len2);
973
0
}
974
975
/* ========================================================================= */
976
0
uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) {
977
0
    return multmodp(op, crc1) ^ (crc2 & 0xffffffff);
978
0
}