diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-09-30 15:00:58 +0200 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2021-10-01 18:37:59 +0200 |
commit | c291d70a2e05409738ae30db468438fffc4e63e2 (patch) | |
tree | 151ebd07e633674cf6aeb4bbd40e2bcf99d55e49 | |
parent | a905a3703ccca00498c02f4fce124ffa9fe06242 (diff) | |
download | anastasis-c291d70a2e05409738ae30db468438fffc4e63e2.tar.gz anastasis-c291d70a2e05409738ae30db468438fffc4e63e2.zip |
draft DNI validation logic
-rw-r--r-- | src/reducer/validation_ES_DNI.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/reducer/validation_ES_DNI.c b/src/reducer/validation_ES_DNI.c new file mode 100644 index 0000000..06bf143 --- /dev/null +++ b/src/reducer/validation_ES_DNI.c | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | This file is part of Anastasis | ||
3 | Copyright (C) 2021 Anastasis SARL | ||
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 Affero General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU Affero General Public License along with | ||
14 | Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file reducer/validation_ES_DNI.c | ||
18 | * @brief validation logic for Spanish Documento Nacional de Identidad numbers, and Número de Identificación de Extranjeros | ||
19 | * @author Christian Grothoff | ||
20 | */ | ||
21 | #include <string.h> | ||
22 | #include <stdbool.h> | ||
23 | |||
24 | |||
25 | /** | ||
26 | * Function to validate a Spanish DNI number. | ||
27 | * | ||
28 | * See https://www.ordenacionjuego.es/en/calculo-digito-control | ||
29 | * | ||
30 | * @param dni_number number to validate (input) | ||
31 | * @return true if validation passed, else false | ||
32 | */ | ||
33 | bool | ||
34 | ES_DNI_check (const char *dni_number) | ||
35 | { | ||
36 | const char map[] = "TRWAGMYFPDXBNJZSQVHLCKE"; | ||
37 | unsigned int num; | ||
38 | char chksum; | ||
39 | unsigned int fact; | ||
40 | char dummy; | ||
41 | |||
42 | if (strlen (dni_number) < 8) | ||
43 | return false; | ||
44 | switch (dni_number[0]) | ||
45 | { | ||
46 | case 'X': | ||
47 | fact = 0; | ||
48 | dni_number++; | ||
49 | break; | ||
50 | case 'Y': | ||
51 | fact = 10000000; | ||
52 | dni_number++; | ||
53 | break; | ||
54 | case 'Z': | ||
55 | fact = 20000000; | ||
56 | dni_number++; | ||
57 | break; | ||
58 | default: | ||
59 | fact = 0; | ||
60 | /* domestic */ | ||
61 | } | ||
62 | |||
63 | if (2 != sscanf (dni_number, | ||
64 | "%7u%c%c" | ||
65 | & num, | ||
66 | &chksum, | ||
67 | &dummy)) | ||
68 | return false; | ||
69 | num += fact; | ||
70 | if (map[num % 23] != chksum) | ||
71 | return false; | ||
72 | if (map[num % 23] != chksum) | ||
73 | return false; | ||
74 | return true; | ||
75 | } | ||