Bohdan Kovalchuk | 7460ae5 | 2020-07-21 17:03:57 +0300 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2020 Cypress Semiconductors |
| 5 | * |
| 6 | * Original license: |
| 7 | * |
| 8 | * Licensed to the Apache Software Foundation (ASF) under one |
| 9 | * or more contributor license agreements. See the NOTICE file |
| 10 | * distributed with this work for additional information |
| 11 | * regarding copyright ownership. The ASF licenses this file |
| 12 | * to you under the Apache License, Version 2.0 (the |
| 13 | * "License"); you may not use this file except in compliance |
| 14 | * with the License. You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, |
| 19 | * software distributed under the License is distributed on an |
| 20 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | * KIND, either express or implied. See the License for the |
| 22 | * specific language governing permissions and limitations |
| 23 | * under the License. |
| 24 | */ |
| 25 | |
| 26 | #include "crc32c.h" |
| 27 | |
| 28 | #define NIBBLE_POS (4u) |
| 29 | #define NIBBLE_MSK (0xFu) |
| 30 | #define CRC_TABLE_SIZE (16u) /* A number of uint32_t elements in the CRC32 table */ |
| 31 | #define CRC_INIT (0xFFFFFFFFu) |
| 32 | |
| 33 | |
| 34 | /******************************************************************************* |
| 35 | * Function Name: crc32c_checksum |
| 36 | ****************************************************************************//** |
| 37 | * |
| 38 | * This function computes a CRC-32C for the provided number of bytes contained |
| 39 | * in the provided buffer. |
| 40 | * |
| 41 | * \param address The pointer to a buffer containing the data to compute |
| 42 | * the checksum for. |
| 43 | * \param length The number of bytes in the buffer to compute the checksum |
| 44 | * for. |
| 45 | * |
| 46 | * \return CRC-32C for the provided data. |
| 47 | * |
| 48 | *******************************************************************************/ |
| 49 | uint32_t crc32c_checksum(const uint8_t *address, uint32_t length) |
| 50 | { |
| 51 | /* Contains generated values to calculate CRC-32C by 4 bits per iteration*/ |
| 52 | static const uint32_t crcTable[CRC_TABLE_SIZE] = |
| 53 | { |
| 54 | 0x00000000u, 0x105ec76fu, 0x20bd8edeu, 0x30e349b1u, |
| 55 | 0x417b1dbcu, 0x5125dad3u, 0x61c69362u, 0x7198540du, |
| 56 | 0x82f63b78u, 0x92a8fc17u, 0xa24bb5a6u, 0xb21572c9u, |
| 57 | 0xc38d26c4u, 0xd3d3e1abu, 0xe330a81au, 0xf36e6f75u, |
| 58 | }; |
| 59 | |
| 60 | uint32_t crc = CRC_INIT; |
| 61 | if (length != 0u) |
| 62 | { |
| 63 | do |
| 64 | { |
| 65 | crc = crc ^ *address; |
| 66 | crc = (crc >> NIBBLE_POS) ^ crcTable[crc & NIBBLE_MSK]; |
| 67 | crc = (crc >> NIBBLE_POS) ^ crcTable[crc & NIBBLE_MSK]; |
| 68 | --length; |
| 69 | ++address; |
| 70 | } while (length != 0u); |
| 71 | } |
| 72 | return (~crc); |
| 73 | } |