summaryrefslogtreecommitdiff
path: root/src/reducer/validation_IT_CF.c
blob: 80d42a13297f871aca1a5902a95281306a2233bd (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
  This file is part of Anastasis
  Copyright (C) 2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file reducer/validation_IT_CF.c
 * @brief validation of Italian Code Fiscales
 * @author Christian Grothoff
 */
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>

struct MapEntry
{
  char in;
  unsigned int out;
};


static const struct MapEntry odd[] = {
  { '0',  1},
  { '9',  21},
  { 'I',  19 },
  { 'R',  8},
  { '1',  0},
  { 'A',  1},
  { 'J',  21},
  { 'S',  12},
  {'2',   5},
  {'B',   0},
  {'K',   2},
  {'T',   14},
  {'3',   7},
  {'C',   5},
  {'L',   4},
  {'U',   16},
  {'4',   9},
  {'D',   7},
  {'M',   18},
  {'V',   10},
  {'5',   13},
  {'E',   9},
  {'N',   20},
  {'W',   22},
  {'6',   15},
  {'F',   13},
  {'O',   11},
  {'X',   25},
  {'7',   17},
  {'G',   15},
  {'P',   3},
  {'Y',   24},
  {'8',   19},
  {'H',   17},
  {'Q',   6},
  {'Z',   23},
  {'\0', 0}
};


static const struct MapEntry even[] = {
  { '0',  0},
  { '1',   1},
  { '2',  2  },
  { '3',  3},
  { '4',  4},
  { '5',  5},
  { '6',  6 },
  { '7',  7 },
  {'8',   8},
  {'9',   9},
  {'A',   0},
  {'B',   1},
  {'C',   2},
  {'D',   3},
  {'E',   4},
  {'F',   5},
  {'G',   6},
  {'H',   7},
  {'I',   8},
  {'J',   9},
  {'K',   10},
  {'L',   11},
  {'M',   12},
  {'N',   13},
  {'O',   14},
  {'P',   15},
  {'Q',   16},
  {'R',   17},
  {'S',   18},
  {'T',   19},
  {'U',   20},
  {'V',   21},
  {'W',   22},
  {'X',   23},
  {'Y',   24},
  {'Z',   25},
  {'\0', 0}
};


static const struct MapEntry rem[] = {
  {'A',   0},
  {'B',   1},
  {'C',   2},
  {'D',   3},
  {'E',   4},
  {'F',   5},
  {'G',   6},
  {'H',   7},
  {'I',   8},
  {'J',   9},
  {'K',   10},
  {'L',   11},
  {'M',   12},
  {'N',   13},
  {'O',   14},
  {'P',   15},
  {'Q',   16},
  {'R',   17},
  {'S',   18},
  {'T',   19},
  {'U',   20},
  {'V',   21},
  {'W',   22},
  {'X',   23},
  {'Y',   24},
  {'Z',   25},
  {'\0', 0}
};


/**
 * Lookup @a in in @a map. Set @a fail to true if @a in is not found.
 *
 * @param map character map to search
 * @param in character to search for
 * @param[out] fail set to true on error
 * @return map value, 0 on error
 */
static unsigned int
lookup (const struct MapEntry *map,
        char in,
        bool *fail)
{
  for (unsigned int i = 0; '\0' != map[i].in; i++)
    if (in == map[i].in)
      return map[i].out;
  *fail = true;
  return 0;
}


/**
 * Function to validate an italian code fiscale number.
 * See https://en.wikipedia.org/wiki/Italian_fiscal_code
 *
 * @param cf_number square number to validate (input)
 * @return true if @a cf_number is a valid, else false
 */
bool
IT_CF_check (const char *cf_number)
{
  unsigned int sum = 0;
  bool fail = false;

  if (strlen (cf_number) != 16)
    return false;
  for (unsigned int i = 0; i<15; i += 2)
    sum += lookup (odd,
                   cf_number[i],
                   &fail);
  for (unsigned int i = 1; i<15; i += 2)
    sum += lookup (even,
                   cf_number[i],
                   &fail);
  sum %= 26;
  if (sum != lookup (rem,
                     cf_number[15],
                     &fail))
    return false;
  if (fail)
    return false;
  return true;
}