1
/*
2
*******************************************************************************
3
\file dec.c
4
\brief Decimal strings
5
\project bee2 [cryptographic library]
6
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
7
\created 2015.11.09
8
\version 2015.11.11
9
\license This program is released under the GNU General Public License
10
version 3. See Copyright Notices in bee2/info.h.
11
*******************************************************************************
12
*/
13

14
#include "bee2/core/dec.h"
15
#include "bee2/core/mem.h"
16
#include "bee2/core/str.h"
17
#include "bee2/core/util.h"
18
#include "bee2/core/word.h"
19

20
/*
21
*******************************************************************************
22
Проверка
23
*******************************************************************************
24
*/
25

26 1
bool_t decIsValid(const char* dec)
27
{
28 1
	if (!strIsValid(dec))
29 0
		return FALSE;
30 1
	for (; *dec; ++dec)
31 1
		if (*dec < '0' || *dec > '9')
32 0
			return FALSE;
33 1
	return TRUE;
34
}
35

36
/*
37
*******************************************************************************
38
Характеристики
39
*******************************************************************************
40
*/
41

42 0
size_t decCLZ(const char* dec)
43
{
44 0
	register size_t clz = 0;
45 0
	ASSERT(decIsValid(dec));
46 0
	while (*dec == '0')
47 0
		++dec, ++clz;
48 0
	return clz;
49
}
50

51
/*
52
*******************************************************************************
53
Преобразования
54
*******************************************************************************
55
*/
56

57 1
void decFromU32(char* dec, size_t count, register u32 num)
58
{
59 1
	ASSERT(memIsValid(dec, count + 1));
60 1
	dec[count] = '\0';
61 1
	while (count--)
62 1
		dec[count] = num % 10 + '0', num /= 10;
63
}
64

65 1
u32 decToU32(const char* dec)
66
{
67 1
	register u32 num = 0;
68 1
	ASSERT(decIsValid(dec));
69 1
	while (*dec)
70 1
		num *= 10, num += *dec - '0', ++dec;
71 1
	return num;
72
}
73

74
#ifdef U64_SUPPORT
75

76 1
void decFromU64(char* dec, size_t count, register u64 num)
77
{
78 1
	ASSERT(memIsValid(dec, count + 1));
79 1
	dec[count] = '\0';
80 1
	while (count--)
81 1
		dec[count] = num % 10 + '0', num /= 10;
82
}
83

84 1
u64 decToU64(const char* dec)
85
{
86 1
	register u64 num = 0;
87 1
	ASSERT(decIsValid(dec));
88 1
	while (*dec)
89 1
		num *= 10, num += *dec - '0', ++dec;
90 1
	return num;
91
}
92

93
#endif // U64_SUPPORT
94

95
/*
96
*******************************************************************************
97
Контрольные цифры
98
*******************************************************************************
99
*/
100

101
static const word luhn_table[10] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
102

103 1
char decLuhnCalc(const char* dec)
104
{
105 1
	register word cd = 0;
106
	size_t i;
107 1
	ASSERT(decIsValid(dec));
108 1
	for (i = strLen(dec); i--;) 
109
	{
110 1
		cd += luhn_table[dec[i] - '0'];
111 1
		if (i)
112 1
			cd += dec[--i] - '0';
113
	}
114 1
	cd %= 10, cd = ((cd << 3) + cd) % 10;
115 1
	return (char)cd + '0';
116
}
117

118 1
bool_t decLuhnVerify(const char* dec)
119
{
120 1
	register word cd = 0;
121
	size_t i;
122 1
	ASSERT(decIsValid(dec));
123 1
	for (i = strLen(dec); i--;) 
124
	{
125 1
		cd += dec[i] - '0';
126 1
		if (i)
127 1
			cd += luhn_table[dec[--i] - '0'];
128
	}
129 1
	cd %= 10;
130 1
	return wordEq(cd, 0);
131
}
132

133
static const char damm_table[10][10] = {
134
	{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
135
	{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
136
	{4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
137
	{1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
138
	{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
139
	{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
140
	{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
141
	{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
142
	{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
143
	{2, 5, 8, 1, 4, 3, 6, 7, 9, 0},
144
};
145

146 1
char decDammCalc(const char* dec)
147
{
148 1
	register char cd = 0;
149 1
	ASSERT(decIsValid(dec));
150 1
	for (; *dec; ++dec)
151 1
		cd = damm_table[(octet)cd][(octet)(*dec - '0')];
152 1
	return cd + '0';
153
}
154

155 1
bool_t decDammVerify(const char* dec)
156
{
157 1
	return decDammCalc(dec) == '0';
158
}

Read our documentation on viewing source code .

Loading