blob: d4c3248d98415be15a342144df7427505ea3d87a [file] [log] [blame]
initial.commit3d533e02008-07-27 00:38:33 +00001/* crc32.c -- compute the CRC-32 of a data stream
mark13dc2462017-02-14 22:15:29 -08002 * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
initial.commit3d533e02008-07-27 00:38:33 +00003 * For conditions of distribution and use, see copyright notice in zlib.h
4 *
5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
7 * tables for updating the shift register in one step with three exclusive-ors
8 * instead of four steps with four exclusive-ors. This results in about a
9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
10 */
11
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +000012/* @(#) $Id$ */
initial.commit3d533e02008-07-27 00:38:33 +000013
14/*
15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
16 protection on the static variables used to control the first-use generation
17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18 first call get_crc_table() to initialize the tables before allowing more than
19 one thread to use crc32().
jiadong.zhu6c142162016-06-22 21:22:18 -070020
21 DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
initial.commit3d533e02008-07-27 00:38:33 +000022 */
23
24#ifdef MAKECRCH
25# include <stdio.h>
26# ifndef DYNAMIC_CRC_TABLE
27# define DYNAMIC_CRC_TABLE
28# endif /* !DYNAMIC_CRC_TABLE */
29#endif /* MAKECRCH */
30
robert.bradford10dd6862014-11-05 06:59:34 -080031#include "deflate.h"
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +000032#include "cpu_features.h"
initial.commit3d533e02008-07-27 00:38:33 +000033#include "zutil.h" /* for STDC and FAR definitions */
34
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +000035#if defined(CRC32_SIMD_SSE42_PCLMUL) || defined(CRC32_ARMV8_CRC32)
Adenilson Cavalcanti72356722018-02-16 03:41:14 +000036#include "crc32_simd.h"
Noel Gordond3709812018-02-10 03:32:48 +000037#endif
38
initial.commit3d533e02008-07-27 00:38:33 +000039/* Definitions for doing the crc four data bytes at a time. */
jiadong.zhu6c142162016-06-22 21:22:18 -070040#if !defined(NOBYFOUR) && defined(Z_U4)
41# define BYFOUR
42#endif
initial.commit3d533e02008-07-27 00:38:33 +000043#ifdef BYFOUR
initial.commit3d533e02008-07-27 00:38:33 +000044 local unsigned long crc32_little OF((unsigned long,
mark13dc2462017-02-14 22:15:29 -080045 const unsigned char FAR *, z_size_t));
initial.commit3d533e02008-07-27 00:38:33 +000046 local unsigned long crc32_big OF((unsigned long,
mark13dc2462017-02-14 22:15:29 -080047 const unsigned char FAR *, z_size_t));
initial.commit3d533e02008-07-27 00:38:33 +000048# define TBLS 8
49#else
50# define TBLS 1
51#endif /* BYFOUR */
52
53/* Local functions for crc concatenation */
54local unsigned long gf2_matrix_times OF((unsigned long *mat,
55 unsigned long vec));
56local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
jiadong.zhu6c142162016-06-22 21:22:18 -070057local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +000058
initial.commit3d533e02008-07-27 00:38:33 +000059
60#ifdef DYNAMIC_CRC_TABLE
61
62local volatile int crc_table_empty = 1;
jiadong.zhu6c142162016-06-22 21:22:18 -070063local z_crc_t FAR crc_table[TBLS][256];
initial.commit3d533e02008-07-27 00:38:33 +000064local void make_crc_table OF((void));
65#ifdef MAKECRCH
jiadong.zhu6c142162016-06-22 21:22:18 -070066 local void write_table OF((FILE *, const z_crc_t FAR *));
initial.commit3d533e02008-07-27 00:38:33 +000067#endif /* MAKECRCH */
68/*
69 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
70 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.
71
72 Polynomials over GF(2) are represented in binary, one bit per coefficient,
73 with the lowest powers in the most significant bit. Then adding polynomials
74 is just exclusive-or, and multiplying a polynomial by x is a right shift by
75 one. If we call the above polynomial p, and represent a byte as the
76 polynomial q, also with the lowest power in the most significant bit (so the
77 byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
78 where a mod b means the remainder after dividing a by b.
79
80 This calculation is done using the shift-register method of multiplying and
81 taking the remainder. The register is initialized to zero, and for each
82 incoming bit, x^32 is added mod p to the register if the bit is a one (where
83 x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
84 x (which is shifting right by one and adding x^32 mod p if the bit shifted
85 out is a one). We start with the highest power (least significant bit) of
86 q and repeat for all eight bits of q.
87
88 The first table is simply the CRC of all possible eight bit values. This is
89 all the information needed to generate CRCs on data a byte at a time for all
90 combinations of CRC register values and incoming bytes. The remaining tables
91 allow for word-at-a-time CRC calculation for both big-endian and little-
92 endian machines, where a word is four bytes.
93*/
94local void make_crc_table()
95{
jiadong.zhu6c142162016-06-22 21:22:18 -070096 z_crc_t c;
initial.commit3d533e02008-07-27 00:38:33 +000097 int n, k;
jiadong.zhu6c142162016-06-22 21:22:18 -070098 z_crc_t poly; /* polynomial exclusive-or pattern */
initial.commit3d533e02008-07-27 00:38:33 +000099 /* terms of polynomial defining this crc (except x^32): */
100 static volatile int first = 1; /* flag to limit concurrent making */
101 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
102
103 /* See if another task is already doing this (not thread-safe, but better
104 than nothing -- significantly reduces duration of vulnerability in
105 case the advice about DYNAMIC_CRC_TABLE is ignored) */
106 if (first) {
107 first = 0;
108
109 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
jiadong.zhu6c142162016-06-22 21:22:18 -0700110 poly = 0;
111 for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
112 poly |= (z_crc_t)1 << (31 - p[n]);
initial.commit3d533e02008-07-27 00:38:33 +0000113
114 /* generate a crc for every 8-bit value */
115 for (n = 0; n < 256; n++) {
jiadong.zhu6c142162016-06-22 21:22:18 -0700116 c = (z_crc_t)n;
initial.commit3d533e02008-07-27 00:38:33 +0000117 for (k = 0; k < 8; k++)
118 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
119 crc_table[0][n] = c;
120 }
121
122#ifdef BYFOUR
123 /* generate crc for each value followed by one, two, and three zeros,
124 and then the byte reversal of those as well as the first table */
125 for (n = 0; n < 256; n++) {
126 c = crc_table[0][n];
jiadong.zhu6c142162016-06-22 21:22:18 -0700127 crc_table[4][n] = ZSWAP32(c);
initial.commit3d533e02008-07-27 00:38:33 +0000128 for (k = 1; k < 4; k++) {
129 c = crc_table[0][c & 0xff] ^ (c >> 8);
130 crc_table[k][n] = c;
jiadong.zhu6c142162016-06-22 21:22:18 -0700131 crc_table[k + 4][n] = ZSWAP32(c);
initial.commit3d533e02008-07-27 00:38:33 +0000132 }
133 }
134#endif /* BYFOUR */
135
136 crc_table_empty = 0;
137 }
138 else { /* not first */
139 /* wait for the other guy to finish (not efficient, but rare) */
140 while (crc_table_empty)
141 ;
142 }
143
144#ifdef MAKECRCH
145 /* write out CRC tables to crc32.h */
146 {
147 FILE *out;
148
149 out = fopen("crc32.h", "w");
150 if (out == NULL) return;
151 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
152 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
jiadong.zhu6c142162016-06-22 21:22:18 -0700153 fprintf(out, "local const z_crc_t FAR ");
initial.commit3d533e02008-07-27 00:38:33 +0000154 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
155 write_table(out, crc_table[0]);
156# ifdef BYFOUR
157 fprintf(out, "#ifdef BYFOUR\n");
158 for (k = 1; k < 8; k++) {
159 fprintf(out, " },\n {\n");
160 write_table(out, crc_table[k]);
161 }
162 fprintf(out, "#endif\n");
163# endif /* BYFOUR */
164 fprintf(out, " }\n};\n");
165 fclose(out);
166 }
167#endif /* MAKECRCH */
168}
169
170#ifdef MAKECRCH
171local void write_table(out, table)
172 FILE *out;
jiadong.zhu6c142162016-06-22 21:22:18 -0700173 const z_crc_t FAR *table;
initial.commit3d533e02008-07-27 00:38:33 +0000174{
175 int n;
176
177 for (n = 0; n < 256; n++)
jiadong.zhu6c142162016-06-22 21:22:18 -0700178 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
179 (unsigned long)(table[n]),
initial.commit3d533e02008-07-27 00:38:33 +0000180 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
181}
182#endif /* MAKECRCH */
183
184#else /* !DYNAMIC_CRC_TABLE */
185/* ========================================================================
186 * Tables of CRC-32s of all single-byte values, made by make_crc_table().
187 */
188#include "crc32.h"
189#endif /* DYNAMIC_CRC_TABLE */
190
191/* =========================================================================
192 * This function can be used by asm versions of crc32()
193 */
jiadong.zhu6c142162016-06-22 21:22:18 -0700194const z_crc_t FAR * ZEXPORT get_crc_table()
initial.commit3d533e02008-07-27 00:38:33 +0000195{
196#ifdef DYNAMIC_CRC_TABLE
197 if (crc_table_empty)
198 make_crc_table();
199#endif /* DYNAMIC_CRC_TABLE */
jiadong.zhu6c142162016-06-22 21:22:18 -0700200 return (const z_crc_t FAR *)crc_table;
initial.commit3d533e02008-07-27 00:38:33 +0000201}
202
203/* ========================================================================= */
204#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
205#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
206
207/* ========================================================================= */
mark13dc2462017-02-14 22:15:29 -0800208unsigned long ZEXPORT crc32_z(crc, buf, len)
initial.commit3d533e02008-07-27 00:38:33 +0000209 unsigned long crc;
210 const unsigned char FAR *buf;
mark13dc2462017-02-14 22:15:29 -0800211 z_size_t len;
initial.commit3d533e02008-07-27 00:38:33 +0000212{
Noel Gordon2f8b1de2018-02-11 00:18:26 +0000213 /*
214 * zlib convention is to call crc32(0, NULL, 0); before making
215 * calls to crc32(). So this is a good, early (and infrequent)
216 * place to cache CPU features if needed for those later, more
217 * interesting crc32() calls.
218 */
Noel Gordon4c976bd2018-01-30 02:03:17 +0000219#if defined(CRC32_SIMD_SSE42_PCLMUL)
220 /*
221 * Use x86 sse4.2+pclmul SIMD to compute the crc32. Since this
Noel Gordon2f8b1de2018-02-11 00:18:26 +0000222 * routine can be freely used, check CPU features here.
Noel Gordon4c976bd2018-01-30 02:03:17 +0000223 */
224 if (buf == Z_NULL) {
225 if (!len) /* Assume user is calling crc32(0, NULL, 0); */
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000226 cpu_check_features();
Noel Gordon4c976bd2018-01-30 02:03:17 +0000227 return 0UL;
228 }
229
230 if (x86_cpu_enable_simd && len >= Z_CRC32_SSE42_MINIMUM_LENGTH) {
231 /* crc32 16-byte chunks */
232 z_size_t chunk_size = len & ~Z_CRC32_SSE42_CHUNKSIZE_MASK;
233 crc = ~crc32_sse42_simd_(buf, chunk_size, ~(uint32_t)crc);
234 /* check remaining data */
235 len -= chunk_size;
236 if (!len)
237 return crc;
238 /* Fall into the default crc32 for the remaining data. */
239 buf += chunk_size;
240 }
241#else
242 if (buf == Z_NULL) {
243 return 0UL;
244 }
245#endif /* CRC32_SIMD_SSE42_PCLMUL */
initial.commit3d533e02008-07-27 00:38:33 +0000246
247#ifdef DYNAMIC_CRC_TABLE
248 if (crc_table_empty)
249 make_crc_table();
250#endif /* DYNAMIC_CRC_TABLE */
251
252#ifdef BYFOUR
253 if (sizeof(void *) == sizeof(ptrdiff_t)) {
jiadong.zhu6c142162016-06-22 21:22:18 -0700254 z_crc_t endian;
initial.commit3d533e02008-07-27 00:38:33 +0000255
256 endian = 1;
257 if (*((unsigned char *)(&endian)))
258 return crc32_little(crc, buf, len);
259 else
260 return crc32_big(crc, buf, len);
261 }
262#endif /* BYFOUR */
263 crc = crc ^ 0xffffffffUL;
264 while (len >= 8) {
265 DO8;
266 len -= 8;
267 }
268 if (len) do {
269 DO1;
270 } while (--len);
271 return crc ^ 0xffffffffUL;
272}
273
mark13dc2462017-02-14 22:15:29 -0800274/* ========================================================================= */
275unsigned long ZEXPORT crc32(crc, buf, len)
276 unsigned long crc;
277 const unsigned char FAR *buf;
278 uInt len;
279{
Adenilson Cavalcanti72356722018-02-16 03:41:14 +0000280#if defined(CRC32_ARMV8_CRC32)
281 /* We got to verify ARM CPU features, so exploit the common usage pattern
282 * of calling this function with Z_NULL for an initial valid crc value.
283 * This allows to cache the result of the feature check and avoid extraneous
284 * function calls.
285 * TODO: try to move this to crc32_z if we don't loose performance on ARM.
286 */
287 if (buf == Z_NULL) {
288 if (!len) /* Assume user is calling crc32(0, NULL, 0); */
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000289 cpu_check_features();
Adenilson Cavalcanti72356722018-02-16 03:41:14 +0000290 return 0UL;
291 }
292
293 if (arm_cpu_enable_crc32)
294 return armv8_crc32_little(crc, buf, len);
295#endif
mark13dc2462017-02-14 22:15:29 -0800296 return crc32_z(crc, buf, len);
297}
298
initial.commit3d533e02008-07-27 00:38:33 +0000299#ifdef BYFOUR
300
mark13dc2462017-02-14 22:15:29 -0800301/*
302 This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
303 integer pointer type. This violates the strict aliasing rule, where a
304 compiler can assume, for optimization purposes, that two pointers to
305 fundamentally different types won't ever point to the same memory. This can
306 manifest as a problem only if one of the pointers is written to. This code
307 only reads from those pointers. So long as this code remains isolated in
308 this compilation unit, there won't be a problem. For this reason, this code
309 should not be copied and pasted into a compilation unit in which other code
310 writes to the buffer that is passed to these routines.
311 */
312
initial.commit3d533e02008-07-27 00:38:33 +0000313/* ========================================================================= */
314#define DOLIT4 c ^= *buf4++; \
315 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
316 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
317#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
318
319/* ========================================================================= */
320local unsigned long crc32_little(crc, buf, len)
321 unsigned long crc;
322 const unsigned char FAR *buf;
mark13dc2462017-02-14 22:15:29 -0800323 z_size_t len;
initial.commit3d533e02008-07-27 00:38:33 +0000324{
jiadong.zhu6c142162016-06-22 21:22:18 -0700325 register z_crc_t c;
326 register const z_crc_t FAR *buf4;
initial.commit3d533e02008-07-27 00:38:33 +0000327
jiadong.zhu6c142162016-06-22 21:22:18 -0700328 c = (z_crc_t)crc;
initial.commit3d533e02008-07-27 00:38:33 +0000329 c = ~c;
330 while (len && ((ptrdiff_t)buf & 3)) {
331 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
332 len--;
333 }
334
jiadong.zhu6c142162016-06-22 21:22:18 -0700335 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
initial.commit3d533e02008-07-27 00:38:33 +0000336 while (len >= 32) {
337 DOLIT32;
338 len -= 32;
339 }
340 while (len >= 4) {
341 DOLIT4;
342 len -= 4;
343 }
344 buf = (const unsigned char FAR *)buf4;
345
346 if (len) do {
347 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
348 } while (--len);
349 c = ~c;
350 return (unsigned long)c;
351}
352
353/* ========================================================================= */
mark13dc2462017-02-14 22:15:29 -0800354#define DOBIG4 c ^= *buf4++; \
initial.commit3d533e02008-07-27 00:38:33 +0000355 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
356 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
357#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
358
359/* ========================================================================= */
360local unsigned long crc32_big(crc, buf, len)
361 unsigned long crc;
362 const unsigned char FAR *buf;
mark13dc2462017-02-14 22:15:29 -0800363 z_size_t len;
initial.commit3d533e02008-07-27 00:38:33 +0000364{
jiadong.zhu6c142162016-06-22 21:22:18 -0700365 register z_crc_t c;
366 register const z_crc_t FAR *buf4;
initial.commit3d533e02008-07-27 00:38:33 +0000367
jiadong.zhu6c142162016-06-22 21:22:18 -0700368 c = ZSWAP32((z_crc_t)crc);
initial.commit3d533e02008-07-27 00:38:33 +0000369 c = ~c;
370 while (len && ((ptrdiff_t)buf & 3)) {
371 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
372 len--;
373 }
374
jiadong.zhu6c142162016-06-22 21:22:18 -0700375 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
initial.commit3d533e02008-07-27 00:38:33 +0000376 while (len >= 32) {
377 DOBIG32;
378 len -= 32;
379 }
380 while (len >= 4) {
381 DOBIG4;
382 len -= 4;
383 }
initial.commit3d533e02008-07-27 00:38:33 +0000384 buf = (const unsigned char FAR *)buf4;
385
386 if (len) do {
387 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
388 } while (--len);
389 c = ~c;
jiadong.zhu6c142162016-06-22 21:22:18 -0700390 return (unsigned long)(ZSWAP32(c));
initial.commit3d533e02008-07-27 00:38:33 +0000391}
392
393#endif /* BYFOUR */
394
395#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
396
397/* ========================================================================= */
398local unsigned long gf2_matrix_times(mat, vec)
399 unsigned long *mat;
400 unsigned long vec;
401{
402 unsigned long sum;
403
404 sum = 0;
405 while (vec) {
406 if (vec & 1)
407 sum ^= *mat;
408 vec >>= 1;
409 mat++;
410 }
411 return sum;
412}
413
414/* ========================================================================= */
415local void gf2_matrix_square(square, mat)
416 unsigned long *square;
417 unsigned long *mat;
418{
419 int n;
420
421 for (n = 0; n < GF2_DIM; n++)
422 square[n] = gf2_matrix_times(mat, mat[n]);
423}
424
425/* ========================================================================= */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000426local uLong crc32_combine_(crc1, crc2, len2)
initial.commit3d533e02008-07-27 00:38:33 +0000427 uLong crc1;
428 uLong crc2;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000429 z_off64_t len2;
initial.commit3d533e02008-07-27 00:38:33 +0000430{
431 int n;
432 unsigned long row;
433 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
434 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
435
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000436 /* degenerate case (also disallow negative lengths) */
437 if (len2 <= 0)
initial.commit3d533e02008-07-27 00:38:33 +0000438 return crc1;
439
440 /* put operator for one zero bit in odd */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000441 odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
initial.commit3d533e02008-07-27 00:38:33 +0000442 row = 1;
443 for (n = 1; n < GF2_DIM; n++) {
444 odd[n] = row;
445 row <<= 1;
446 }
447
448 /* put operator for two zero bits in even */
449 gf2_matrix_square(even, odd);
450
451 /* put operator for four zero bits in odd */
452 gf2_matrix_square(odd, even);
453
454 /* apply len2 zeros to crc1 (first square will put the operator for one
455 zero byte, eight zero bits, in even) */
456 do {
457 /* apply zeros operator for this bit of len2 */
458 gf2_matrix_square(even, odd);
459 if (len2 & 1)
460 crc1 = gf2_matrix_times(even, crc1);
461 len2 >>= 1;
462
463 /* if no more bits set, then done */
464 if (len2 == 0)
465 break;
466
467 /* another iteration of the loop with odd and even swapped */
468 gf2_matrix_square(odd, even);
469 if (len2 & 1)
470 crc1 = gf2_matrix_times(odd, crc1);
471 len2 >>= 1;
472
473 /* if no more bits set, then done */
474 } while (len2 != 0);
475
476 /* return combined crc */
477 crc1 ^= crc2;
478 return crc1;
479}
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000480
481/* ========================================================================= */
482uLong ZEXPORT crc32_combine(crc1, crc2, len2)
483 uLong crc1;
484 uLong crc2;
485 z_off_t len2;
486{
487 return crc32_combine_(crc1, crc2, len2);
488}
489
490uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
491 uLong crc1;
492 uLong crc2;
493 z_off64_t len2;
494{
495 return crc32_combine_(crc1, crc2, len2);
496}
robert.bradford10dd6862014-11-05 06:59:34 -0800497
498ZLIB_INTERNAL void crc_reset(deflate_state *const s)
499{
Noel Gordon2ed8ded2020-04-21 12:11:09 +0000500#ifdef CRC32_SIMD_SSE42_PCLMUL
robert.bradford10dd6862014-11-05 06:59:34 -0800501 if (x86_cpu_enable_simd) {
502 crc_fold_init(s);
503 return;
504 }
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000505#endif
robert.bradford10dd6862014-11-05 06:59:34 -0800506 s->strm->adler = crc32(0L, Z_NULL, 0);
507}
508
509ZLIB_INTERNAL void crc_finalize(deflate_state *const s)
510{
Noel Gordon2ed8ded2020-04-21 12:11:09 +0000511#ifdef CRC32_SIMD_SSE42_PCLMUL
robert.bradford10dd6862014-11-05 06:59:34 -0800512 if (x86_cpu_enable_simd)
513 s->strm->adler = crc_fold_512to32(s);
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000514#endif
robert.bradford10dd6862014-11-05 06:59:34 -0800515}
516
517ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size)
518{
Noel Gordon2ed8ded2020-04-21 12:11:09 +0000519#ifdef CRC32_SIMD_SSE42_PCLMUL
robert.bradford10dd6862014-11-05 06:59:34 -0800520 if (x86_cpu_enable_simd) {
521 crc_fold_copy(strm->state, dst, strm->next_in, size);
522 return;
523 }
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000524#endif
robert.bradford10dd6862014-11-05 06:59:34 -0800525 zmemcpy(dst, strm->next_in, size);
526 strm->adler = crc32(strm->adler, dst, size);
527}