blob: 016b34a523b8952d53f7caf1dffb470903e349a1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek BehĂșn85d8bf52017-09-03 17:00:23 +02002/*
3 * Copied from Linux kernel crypto/crc32c.c
4 * Copyright (c) 2004 Cisco Systems, Inc.
5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
Marek BehĂșn85d8bf52017-09-03 17:00:23 +020011 */
12
13#include <common.h>
14#include <compiler.h>
15
16uint32_t crc32c_cal(uint32_t crc, const char *data, int length,
17 uint32_t *crc32c_table)
18{
19 while (length--)
20 crc = crc32c_table[(u8)(crc ^ *data++)] ^ (crc >> 8);
21
22 return crc;
23}
24
25void crc32c_init(uint32_t *crc32c_table, uint32_t pol)
26{
27 int i, j;
28 uint32_t v;
29 const uint32_t poly = pol; /* Bit-reflected CRC32C polynomial */
30
31 for (i = 0; i < 256; i++) {
32 v = i;
33 for (j = 0; j < 8; j++)
34 v = (v >> 1) ^ ((v & 1) ? poly : 0);
35
36 crc32c_table[i] = v;
37 }
38}