Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / verhoeff / Verhoeff16.cpp
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-16 strings.
23  *
24  */
25 #include "Verhoeff.h"
26
27 #include <stdint.h>
28 #include <string.h>
29
30 #ifndef VERHOEFF16_NO_MULTIPLY_TABLE
31
32 uint8_t Verhoeff16::sMultiplyTable[] = {
33     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 1,  2,  3,  4,  5,  6,  7,  0,  9,  10, 11, 12, 13, 14, 15, 8,
34     2,  3,  4,  5,  6,  7,  0,  1,  10, 11, 12, 13, 14, 15, 8,  9,  3,  4,  5,  6,  7,  0,  1,  2,  11, 12, 13, 14, 15, 8,  9,  10,
35     4,  5,  6,  7,  0,  1,  2,  3,  12, 13, 14, 15, 8,  9,  10, 11, 5,  6,  7,  0,  1,  2,  3,  4,  13, 14, 15, 8,  9,  10, 11, 12,
36     6,  7,  0,  1,  2,  3,  4,  5,  14, 15, 8,  9,  10, 11, 12, 13, 7,  0,  1,  2,  3,  4,  5,  6,  15, 8,  9,  10, 11, 12, 13, 14,
37     8,  15, 14, 13, 12, 11, 10, 9,  0,  7,  6,  5,  4,  3,  2,  1,  9,  8,  15, 14, 13, 12, 11, 10, 1,  0,  7,  6,  5,  4,  3,  2,
38     10, 9,  8,  15, 14, 13, 12, 11, 2,  1,  0,  7,  6,  5,  4,  3,  11, 10, 9,  8,  15, 14, 13, 12, 3,  2,  1,  0,  7,  6,  5,  4,
39     12, 11, 10, 9,  8,  15, 14, 13, 4,  3,  2,  1,  0,  7,  6,  5,  13, 12, 11, 10, 9,  8,  15, 14, 5,  4,  3,  2,  1,  0,  7,  6,
40     14, 13, 12, 11, 10, 9,  8,  15, 6,  5,  4,  3,  2,  1,  0,  7,  15, 14, 13, 12, 11, 10, 9,  8,  7,  6,  5,  4,  3,  2,  1,  0,
41 };
42
43 #endif // VERHOEFF16_NO_MULTIPLY_TABLE
44
45 uint8_t Verhoeff16::sPermTable[] = { 4, 7, 5, 14, 8, 12, 15, 0, 2, 11, 3, 13, 10, 6, 9, 1 };
46
47 char Verhoeff16::ComputeCheckChar(const char * str)
48 {
49     return ComputeCheckChar(str, strlen(str));
50 }
51
52 char Verhoeff16::ComputeCheckChar(const char * str, size_t strLen)
53 {
54     int c = 0;
55
56     for (size_t i = 1; i <= strLen; i++)
57     {
58         char ch = str[strLen - i];
59
60         int val = CharToVal(ch);
61         if (val < 0)
62             return 0; // invalid character
63
64         int p = Verhoeff::Permute(val, sPermTable, Base, i);
65
66 #ifdef VERHOEFF16_NO_MULTIPLY_TABLE
67         c = Verhoeff::DihedralMultiply(c, p, PolygonSize);
68 #else
69         c = sMultiplyTable[c * Base + p];
70 #endif
71     }
72
73     c = Verhoeff::DihedralInvert(c, PolygonSize);
74
75     return ValToChar(c);
76 }
77
78 bool Verhoeff16::ValidateCheckChar(char checkChar, const char * str)
79 {
80     return ValidateCheckChar(checkChar, str, strlen(str));
81 }
82
83 bool Verhoeff16::ValidateCheckChar(char checkChar, const char * str, size_t strLen)
84 {
85     return (ComputeCheckChar(str, strLen) == checkChar);
86 }
87
88 bool Verhoeff16::ValidateCheckChar(const char * str)
89 {
90     return ValidateCheckChar(str, strlen(str));
91 }
92
93 bool Verhoeff16::ValidateCheckChar(const char * str, size_t strLen)
94 {
95     if (strLen == 0)
96         return false;
97     return ValidateCheckChar(str[strLen - 1], str, strLen - 1);
98 }
99
100 int Verhoeff16::CharToVal(char ch)
101 {
102     if (ch >= '0' && ch <= '9')
103         return ch - '0';
104
105     if (ch >= 'A' && ch <= 'F')
106         return (ch - 'A') + 10;
107
108     if (ch >= 'a' && ch <= 'f')
109         return (ch - 'a') + 10;
110
111     return -1;
112 }
113
114 char Verhoeff16::ValToChar(int val)
115 {
116     if (val >= 0 && val < 10)
117         return static_cast<char>('0' + val);
118
119     if (val >= 10 && val < Base)
120         return static_cast<char>('A' + (val - 10));
121
122     return 0;
123 }