Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / verhoeff / Verhoeff.h
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 defines objects for Verhoeff's check-digit algorithm
22  *      for strings with various bases and an object for the core
23  *      algorithm operations.
24  *
25  */
26
27 #pragma once
28
29 #include <stdint.h>
30 #include <stdlib.h>
31
32 #include <support/DLLUtil.h>
33
34 class VerhoeffTest;
35
36 // Verhoeff10 -- Implements Verhoeff's check-digit algorithm for base-10 strings.
37 //
38 class DLL_EXPORT Verhoeff10
39 {
40     friend class VerhoeffTest;
41
42 public:
43     enum
44     {
45         Base        = 10,
46         PolygonSize = 5
47     };
48
49     // Compute a check character for a given string.
50     static char ComputeCheckChar(const char * str);
51     static char ComputeCheckChar(const char * str, size_t strLen);
52
53     // Verify a check character against a given string.
54     static bool ValidateCheckChar(char checkChar, const char * str);
55     static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
56
57     // Verify a check character at the end of a given string.
58     static bool ValidateCheckChar(const char * str);
59     static bool ValidateCheckChar(const char * str, size_t strLen);
60
61     // Convert between a character and its corresponding value.
62     static int CharToVal(char ch);
63     static char ValToChar(int val);
64
65 private:
66     Verhoeff10()  = delete;
67     ~Verhoeff10() = delete;
68
69     static uint8_t sMultiplyTable[];
70     static uint8_t sPermTable[];
71 };
72
73 // Verhoeff16 -- Implements Verhoeff's check-digit algorithm for base-16 (hex) strings.
74 //
75 class DLL_EXPORT Verhoeff16
76 {
77     friend class VerhoeffTest;
78
79 public:
80     enum
81     {
82         Base        = 16,
83         PolygonSize = 8
84     };
85
86     // Compute a check character for a given string.
87     static char ComputeCheckChar(const char * str);
88     static char ComputeCheckChar(const char * str, size_t strLen);
89
90     // Verify a check character against a given string.
91     static bool ValidateCheckChar(char checkChar, const char * str);
92     static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
93
94     // Verify a check character at the end of a given string.
95     static bool ValidateCheckChar(const char * str);
96     static bool ValidateCheckChar(const char * str, size_t strLen);
97
98     // Convert between a character and its corresponding value.
99     static int CharToVal(char ch);
100     static char ValToChar(int val);
101
102 private:
103     Verhoeff16()  = delete;
104     ~Verhoeff16() = delete;
105
106     static uint8_t sMultiplyTable[];
107     static uint8_t sPermTable[];
108 };
109
110 // Verhoeff32 -- Implements Verhoeff's check-digit algorithm for base-32 strings.
111 //
112 // Character Set (any case): 0-9, A-H, J-N, P, R-Y (excludes I, O, Q and Z).
113 //
114 class DLL_EXPORT Verhoeff32
115 {
116     friend class VerhoeffTest;
117
118 public:
119     enum
120     {
121         Base        = 32,
122         PolygonSize = 16
123     };
124
125     // Compute a check character for a given string.
126     static char ComputeCheckChar(const char * str);
127     static char ComputeCheckChar(const char * str, size_t strLen);
128
129     // Verify a check character against a given string.
130     static bool ValidateCheckChar(char checkChar, const char * str);
131     static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
132
133     // Verify a check character at the end of a given string.
134     static bool ValidateCheckChar(const char * str);
135     static bool ValidateCheckChar(const char * str, size_t strLen);
136
137     // Convert between a character and its corresponding value.
138     static int CharToVal(char ch);
139     static char ValToChar(int val);
140
141 private:
142     Verhoeff32()  = delete;
143     ~Verhoeff32() = delete;
144
145     static uint8_t sMultiplyTable[];
146     static uint8_t sPermTable[];
147     static int8_t sCharToValTable[];
148     static char sValToCharTable[];
149 };
150
151 // Verhoeff36 -- Implements Verhoeff's check-digit algorithm for base-36 strings.
152 //
153 // Character Set (any case) : 0-9, A-Z.
154 //
155 class DLL_EXPORT Verhoeff36
156 {
157     friend class VerhoeffTest;
158
159 public:
160     enum
161     {
162         Base        = 36,
163         PolygonSize = 18
164     };
165
166     static char ComputeCheckChar(const char * str);
167     static char ComputeCheckChar(const char * str, size_t strLen);
168     static bool ValidateCheckChar(char checkChar, const char * str);
169     static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
170     static bool ValidateCheckChar(const char * str);
171     static bool ValidateCheckChar(const char * str, size_t strLen);
172
173     static int CharToVal(char ch);
174     static char ValToChar(int val);
175
176 private:
177     Verhoeff36()  = delete;
178     ~Verhoeff36() = delete;
179
180     static uint8_t sMultiplyTable[];
181     static uint8_t sPermTable[];
182     static int8_t sCharToValTable[];
183     static char sValToCharTable[];
184 };
185
186 // Verhoeff -- Implements core functions for Verhoeff's algorithm.
187 //
188 class Verhoeff
189 {
190 public:
191     static int DihedralMultiply(int x, int y, int n);
192     static int DihedralInvert(int val, int n);
193     static int Permute(int val, uint8_t * permTable, int permTableLen, uint64_t iterCount);
194 };