diff options
Diffstat (limited to 'src/reducer/validation_CH_AHV.c')
-rw-r--r-- | src/reducer/validation_CH_AHV.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/reducer/validation_CH_AHV.c b/src/reducer/validation_CH_AHV.c new file mode 100644 index 0000000..e6a2f25 --- /dev/null +++ b/src/reducer/validation_CH_AHV.c | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | This file is part of Anastasis | ||
3 | Copyright (C) 2020, 2021 Taler Systems SA | ||
4 | |||
5 | Anastasis is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU Lesser General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file redux/validation_CH_AHV.c | ||
18 | * @brief anastasis reducer api | ||
19 | * @author Christian Grothoff | ||
20 | * @author Dominik Meister | ||
21 | * @author Dennis Neufeld | ||
22 | */ | ||
23 | #include <string.h> | ||
24 | #include <stdbool.h> | ||
25 | |||
26 | /** | ||
27 | * Function to validate a Swiss AHV number. | ||
28 | * | ||
29 | * @param avh_number ahv number to validate (input) | ||
30 | * @return true if validation passed, else false | ||
31 | */ | ||
32 | bool | ||
33 | CH_AHV_check (const char *ahv_number) | ||
34 | { | ||
35 | unsigned int checknum; | ||
36 | unsigned int next_ten; | ||
37 | const char *pos = &ahv_number[strlen (ahv_number) - 1]; | ||
38 | bool phase = true; | ||
39 | unsigned int calculation = 0; | ||
40 | |||
41 | checknum = *pos - 48; | ||
42 | while (pos > ahv_number) | ||
43 | { | ||
44 | pos--; | ||
45 | if ('.' == *pos) | ||
46 | continue; | ||
47 | if (phase) | ||
48 | calculation += ((*pos - 48) * 3); | ||
49 | else | ||
50 | calculation += *pos - 48; | ||
51 | phase = ! phase; | ||
52 | } | ||
53 | /* round up to the next ten */ | ||
54 | next_ten = ((calculation + 9) / 10) * 10; | ||
55 | calculation = next_ten - calculation; | ||
56 | return (checknum == calculation); | ||
57 | } | ||