Matter SDK Coverage Report
Current view: top level - lib/support/verhoeff - Verhoeff10.cpp (source / functions) Coverage Total Hit
Test: SHA:f84fe08d06f240e801b5d923f8a938a9938ca110 Lines: 93.5 % 31 29
Test Date: 2025-02-22 08:08:07 Functions: 100.0 % 8 8

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : 
      19              : /**
      20              :  *    @file
      21              :  *      This file implements an object for Verhoeff's check-digit
      22              :  *      algorithm for base-10 strings.
      23              :  *
      24              :  */
      25              : 
      26              : #include "Verhoeff.h"
      27              : 
      28              : #include <stdint.h>
      29              : #include <string.h>
      30              : 
      31              : const uint8_t Verhoeff10::sMultiplyTable[] = {
      32              :     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 0, 6, 7, 8, 9, 5, 2, 3, 4, 0, 1, 7, 8, 9, 5, 6, 3, 4, 0, 1,
      33              :     2, 8, 9, 5, 6, 7, 4, 0, 1, 2, 3, 9, 5, 6, 7, 8, 5, 9, 8, 7, 6, 0, 4, 3, 2, 1, 6, 5, 9, 8, 7, 1, 0, 4,
      34              :     3, 2, 7, 6, 5, 9, 8, 2, 1, 0, 4, 3, 8, 7, 6, 5, 9, 3, 2, 1, 0, 4, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
      35              : };
      36              : 
      37              : const uint8_t Verhoeff10::sPermTable[] = { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 };
      38              : 
      39           61 : char Verhoeff10::ComputeCheckChar(const char * str)
      40              : {
      41           61 :     return ComputeCheckChar(str, strlen(str));
      42              : }
      43              : 
      44           93 : char Verhoeff10::ComputeCheckChar(const char * str, size_t strLen)
      45              : {
      46           93 :     int c = 0;
      47              : 
      48         1161 :     for (size_t i = 1; i <= strLen; i++)
      49              :     {
      50         1074 :         char ch = str[strLen - i];
      51              : 
      52         1074 :         int val = CharToVal(ch);
      53         1074 :         if (val < 0)
      54            6 :             return 0; // invalid character
      55              : 
      56         1068 :         int p = Verhoeff::Permute(val, sPermTable, Base, i);
      57              : 
      58         1068 :         c = sMultiplyTable[c * Base + p];
      59              :     }
      60              : 
      61           87 :     c = Verhoeff::DihedralInvert(c, PolygonSize);
      62              : 
      63           87 :     return ValToChar(c);
      64              : }
      65              : 
      66           26 : bool Verhoeff10::ValidateCheckChar(char checkChar, const char * str)
      67              : {
      68           26 :     return ValidateCheckChar(checkChar, str, strlen(str));
      69              : }
      70              : 
      71           32 : bool Verhoeff10::ValidateCheckChar(char checkChar, const char * str, size_t strLen)
      72              : {
      73           32 :     return (ComputeCheckChar(str, strLen) == checkChar);
      74              : }
      75              : 
      76            6 : bool Verhoeff10::ValidateCheckChar(const char * str)
      77              : {
      78            6 :     return ValidateCheckChar(str, strlen(str));
      79              : }
      80              : 
      81            6 : bool Verhoeff10::ValidateCheckChar(const char * str, size_t strLen)
      82              : {
      83            6 :     if (strLen == 0)
      84            0 :         return false;
      85            6 :     return ValidateCheckChar(str[strLen - 1], str, strLen - 1);
      86              : }
      87              : 
      88         1086 : int Verhoeff10::CharToVal(char ch)
      89              : {
      90         1086 :     if (ch >= '0' && ch <= '9')
      91         1080 :         return ch - '0';
      92            6 :     return -1;
      93              : }
      94              : 
      95           87 : char Verhoeff10::ValToChar(int val)
      96              : {
      97           87 :     if (val >= 0 && val <= Base)
      98           87 :         return static_cast<char>('0' + val);
      99            0 :     return 0;
     100              : }
        

Generated by: LCOV version 2.0-1