LCOV - code coverage report
Current view: top level - lib/os - crc8_sw.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 20 0.0 %
Date: 2022-08-18 11:36:24 Functions: 0 2 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 12 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2020 Intel Corporation
       3                 :            :  * Copyright (c) 2017 Nordic Semiconductor ASA
       4                 :            :  * Copyright (c) 2015 Runtime Inc
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: Apache-2.0
       7                 :            :  */
       8                 :            : 
       9                 :            : #include <sys/crc.h>
      10                 :            : 
      11                 :            : static const uint8_t crc8_ccitt_small_table[16] = {
      12                 :            :         0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
      13                 :            :         0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d
      14                 :            : };
      15                 :            : 
      16                 :          0 : uint8_t crc8_ccitt(uint8_t val, const void *buf, size_t cnt)
      17                 :            : {
      18                 :            :         size_t i;
      19                 :          0 :         const uint8_t *p = buf;
      20                 :            : 
      21         [ #  # ]:          0 :         for (i = 0; i < cnt; i++) {
      22                 :          0 :                 val ^= p[i];
      23                 :          0 :                 val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
      24                 :          0 :                 val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
      25                 :            :         }
      26                 :          0 :         return val;
      27                 :            : }
      28                 :            : 
      29                 :          0 : uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
      30                 :            :           bool reversed)
      31                 :            : {
      32                 :          0 :         uint8_t crc = initial_value;
      33                 :            :         size_t i, j;
      34                 :            : 
      35         [ #  # ]:          0 :         for (i = 0; i < len; i++) {
      36                 :          0 :                 crc ^= src[i];
      37                 :            : 
      38         [ #  # ]:          0 :                 for (j = 0; j < 8; j++) {
      39         [ #  # ]:          0 :                         if (reversed) {
      40         [ #  # ]:          0 :                                 if (crc & 0x01) {
      41                 :          0 :                                         crc = (crc >> 1) ^ polynomial;
      42                 :            :                                 } else {
      43                 :          0 :                                         crc >>= 1;
      44                 :            :                                 }
      45                 :            :                         } else {
      46         [ #  # ]:          0 :                                 if (crc & 0x80) {
      47                 :          0 :                                         crc = (crc << 1) ^ polynomial;
      48                 :            :                                 } else {
      49                 :          0 :                                         crc <<= 1;
      50                 :            :                                 }
      51                 :            :                         }
      52                 :            :                 }
      53                 :            :         }
      54                 :            : 
      55                 :          0 :         return crc;
      56                 :            : }

Generated by: LCOV version 1.14