Skip to content

Commit

Permalink
Fix for 'Verhoeff code is not using tables as const causing unnecessa…
Browse files Browse the repository at this point in the history
…ry RAM use #9564' (#11726)
  • Loading branch information
michalbudzon-q authored and pull[bot] committed Dec 8, 2021
1 parent 8e99d74 commit 1692241
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lib/support/verhoeff/Verhoeff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int Verhoeff::DihedralInvert(int val, int n)
return val;
}

int Verhoeff::Permute(int val, uint8_t * permTable, int permTableLen, uint64_t iterCount)
int Verhoeff::Permute(int val, const uint8_t * permTable, int permTableLen, uint64_t iterCount)
{
val = val % permTableLen;
if (iterCount == 0)
Expand Down
26 changes: 13 additions & 13 deletions src/lib/support/verhoeff/Verhoeff.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class DLL_EXPORT Verhoeff10
Verhoeff10() = delete;
~Verhoeff10() = delete;

static uint8_t sMultiplyTable[];
static uint8_t sPermTable[];
static const uint8_t sMultiplyTable[];
static const uint8_t sPermTable[];
};

// Verhoeff16 -- Implements Verhoeff's check-digit algorithm for base-16 (hex) strings.
Expand Down Expand Up @@ -103,8 +103,8 @@ class DLL_EXPORT Verhoeff16
Verhoeff16() = delete;
~Verhoeff16() = delete;

static uint8_t sMultiplyTable[];
static uint8_t sPermTable[];
static const uint8_t sMultiplyTable[];
static const uint8_t sPermTable[];
};

// Verhoeff32 -- Implements Verhoeff's check-digit algorithm for base-32 strings.
Expand Down Expand Up @@ -142,10 +142,10 @@ class DLL_EXPORT Verhoeff32
Verhoeff32() = delete;
~Verhoeff32() = delete;

static uint8_t sMultiplyTable[];
static uint8_t sPermTable[];
static int8_t sCharToValTable[];
static char sValToCharTable[];
static const uint8_t sMultiplyTable[];
static const uint8_t sPermTable[];
static const int8_t sCharToValTable[];
static const char sValToCharTable[];
};

// Verhoeff36 -- Implements Verhoeff's check-digit algorithm for base-36 strings.
Expand Down Expand Up @@ -177,10 +177,10 @@ class DLL_EXPORT Verhoeff36
Verhoeff36() = delete;
~Verhoeff36() = delete;

static uint8_t sMultiplyTable[];
static uint8_t sPermTable[];
static int8_t sCharToValTable[];
static char sValToCharTable[];
static const uint8_t sMultiplyTable[];
static const uint8_t sPermTable[];
static const int8_t sCharToValTable[];
static const char sValToCharTable[];
};

// Verhoeff -- Implements core functions for Verhoeff's algorithm.
Expand All @@ -190,5 +190,5 @@ class Verhoeff
public:
static int DihedralMultiply(int x, int y, int n);
static int DihedralInvert(int val, int n);
static int Permute(int val, uint8_t * permTable, int permTableLen, uint64_t iterCount);
static int Permute(int val, const uint8_t * permTable, int permTableLen, uint64_t iterCount);
};
4 changes: 2 additions & 2 deletions src/lib/support/verhoeff/Verhoeff10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@

#ifndef VERHOEFF10_NO_MULTIPLY_TABLE

uint8_t Verhoeff10::sMultiplyTable[] = {
const uint8_t Verhoeff10::sMultiplyTable[] = {
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,
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,
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,
};

#endif

uint8_t Verhoeff10::sPermTable[] = { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 };
const uint8_t Verhoeff10::sPermTable[] = { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 };

char Verhoeff10::ComputeCheckChar(const char * str)
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/support/verhoeff/Verhoeff16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#ifndef VERHOEFF16_NO_MULTIPLY_TABLE

uint8_t Verhoeff16::sMultiplyTable[] = {
const uint8_t Verhoeff16::sMultiplyTable[] = {
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,
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,
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,
Expand All @@ -42,7 +42,7 @@ uint8_t Verhoeff16::sMultiplyTable[] = {

#endif // VERHOEFF16_NO_MULTIPLY_TABLE

uint8_t Verhoeff16::sPermTable[] = { 4, 7, 5, 14, 8, 12, 15, 0, 2, 11, 3, 13, 10, 6, 9, 1 };
const uint8_t Verhoeff16::sPermTable[] = { 4, 7, 5, 14, 8, 12, 15, 0, 2, 11, 3, 13, 10, 6, 9, 1 };

char Verhoeff16::ComputeCheckChar(const char * str)
{
Expand Down
8 changes: 4 additions & 4 deletions src/lib/support/verhoeff/Verhoeff32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#ifndef VERHOEFF32_NO_MULTIPLY_TABLE

uint8_t Verhoeff32::sMultiplyTable[] = {
const uint8_t Verhoeff32::sMultiplyTable[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17,
Expand Down Expand Up @@ -69,19 +69,19 @@ uint8_t Verhoeff32::sMultiplyTable[] = {

#endif // VERHOEFF32_NO_MULTIPLY_TABLE

uint8_t Verhoeff32::sPermTable[] = {
const uint8_t Verhoeff32::sPermTable[] = {
// Detects all single digit and adjacent transposition errors, and 97.076613% of jump transposition errors.
7, 2, 1, 30, 16, 20, 27, 11, 31, 6, 8, 13, 29, 5, 10, 21, 22, 3, 24, 0, 23, 25, 12, 9, 28, 14, 4, 15, 17, 18, 19, 26,
};

int8_t Verhoeff32::sCharToValTable[] = {
const int8_t Verhoeff32::sCharToValTable[] = {
// NOTE: table starts at ASCII 30h
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17,
-1, 18, 19, 20, 21, 22, -1, 23, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, 10,
11, 12, 13, 14, 15, 16, 17, -1, 18, 19, 20, 21, 22, -1, 23, -1, 24, 25, 26, 27, 28, 29, 30, 31
};

char Verhoeff32::sValToCharTable[] = "0123456789ABCDEFGHJKLMNPRSTUVWXY";
const char Verhoeff32::sValToCharTable[] = "0123456789ABCDEFGHJKLMNPRSTUVWXY";

char Verhoeff32::ComputeCheckChar(const char * str)
{
Expand Down
10 changes: 5 additions & 5 deletions src/lib/support/verhoeff/Verhoeff36.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#ifndef VERHOEFF36_NO_MULTIPLY_TABLE

uint8_t Verhoeff36::sMultiplyTable[] = {
const uint8_t Verhoeff36::sMultiplyTable[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 18, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 20, 21, 22, 23, 24, 25,
Expand Down Expand Up @@ -77,17 +77,17 @@ uint8_t Verhoeff36::sMultiplyTable[] = {

#endif // VERHOEFF36_NO_MULTIPLY_TABLE

uint8_t Verhoeff36::sPermTable[] = { 29, 0, 32, 11, 35, 20, 7, 27, 2, 4, 19, 28, 30, 1, 5, 12, 3, 9,
16, 22, 6, 33, 8, 24, 26, 21, 14, 10, 34, 31, 15, 25, 17, 13, 23, 18 };
const uint8_t Verhoeff36::sPermTable[] = { 29, 0, 32, 11, 35, 20, 7, 27, 2, 4, 19, 28, 30, 1, 5, 12, 3, 9,
16, 22, 6, 33, 8, 24, 26, 21, 14, 10, 34, 31, 15, 25, 17, 13, 23, 18 };

int8_t Verhoeff36::sCharToValTable[] = {
const int8_t Verhoeff36::sCharToValTable[] = {
// NOTE: table starts at ASCII 30h
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
};

char Verhoeff36::sValToCharTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char Verhoeff36::sValToCharTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char Verhoeff36::ComputeCheckChar(const char * str)
{
Expand Down

0 comments on commit 1692241

Please sign in to comment.