Coverage Report

Created: 2026-02-14 06:27

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