summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-10-01 22:38:21 +0200
committerChristian Grothoff <christian@grothoff.org>2021-10-01 22:38:21 +0200
commit0e60574acfb81d6bc279aac469e023e5c689a21d (patch)
tree32f7e2aabba2bdfcf3d113c2ca3430d9d21d3747 /src
parentc291d70a2e05409738ae30db468438fffc4e63e2 (diff)
downloadanastasis-0e60574acfb81d6bc279aac469e023e5c689a21d.tar.gz
anastasis-0e60574acfb81d6bc279aac469e023e5c689a21d.tar.bz2
anastasis-0e60574acfb81d6bc279aac469e023e5c689a21d.zip
let there be Spain
Diffstat (limited to 'src')
-rw-r--r--src/reducer/Makefile.am1
-rw-r--r--src/reducer/validation_ES_DNI.c147
2 files changed, 129 insertions, 19 deletions
diff --git a/src/reducer/Makefile.am b/src/reducer/Makefile.am
index 5cbe6f7..3b7edb6 100644
--- a/src/reducer/Makefile.am
+++ b/src/reducer/Makefile.am
@@ -22,6 +22,7 @@ libanastasisredux_la_SOURCES = \
validation_CZ_BN.c \
validation_DE_SVN.c \
validation_DE_TIN.c \
+ validation_ES_DNI.c \
validation_IN_AADHAR.c \
validation_IT_CF.c \
validation_XX_SQUARE.c \
diff --git a/src/reducer/validation_ES_DNI.c b/src/reducer/validation_ES_DNI.c
index 06bf143..6679f45 100644
--- a/src/reducer/validation_ES_DNI.c
+++ b/src/reducer/validation_ES_DNI.c
@@ -17,9 +17,75 @@
* @file reducer/validation_ES_DNI.c
* @brief validation logic for Spanish Documento Nacional de Identidad numbers, and Número de Identificación de Extranjeros
* @author Christian Grothoff
+ *
+ * Examples:
+ * 12345678Z, 39740191D, 14741806W, X8095495R
*/
#include <string.h>
#include <stdbool.h>
+#include <stdio.h>
+
+/**
+ * Function to validate a Spanish CIF number.
+ *
+ * @param civ number to validate
+ * @return true if validation passed, else false
+ */
+static bool
+validate_cif (const char *cif)
+{
+ size_t slen = strlen (cif);
+ char letter = cif[0];
+ const char *number = &cif[1];
+ char control = cif[slen - 1];
+ unsigned int sum = 0;
+
+ if (9 != slen)
+ return false;
+
+ for (unsigned int i = 0; i < slen - 2; i++)
+ {
+ unsigned int n = number[i] - '0';
+
+ if (n >= 10)
+ return false;
+ if (0 == (i % 2))
+ {
+ n *= 2;
+ sum += n < 10 ? n : n - 9;
+ }
+ else
+ {
+ sum += n;
+ }
+ }
+ sum %= 10;
+ if (0 != sum)
+ sum = 10 - sum;
+ {
+ char control_digit = "0123456789"[sum];
+ char control_letter = "JABCDEFGHI"[sum];
+
+ switch (letter)
+ {
+ case 'A':
+ case 'B':
+ case 'E':
+ case 'H':
+ return control == control_digit;
+ case 'N':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'W':
+ return control == control_letter;
+ default:
+ return (control == control_letter) ||
+ (control == control_digit);
+ }
+ }
+}
/**
@@ -43,32 +109,75 @@ ES_DNI_check (const char *dni_number)
return false;
switch (dni_number[0])
{
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ /* CIF: [A-W]\d{7}[0-9A-J] */
+ /* CIF is for companies, we only take those
+ of individuals here! */
+ return false; /* CIV is not allowed! */
+ case 'M':
+ /* special NIE, with CIF validation (?),
+ but for individuals, see
+ https://www.strongabogados.com/tax-id-spain.php */
+ return validate_cif (dni_number);
case 'X':
- fact = 0;
- dni_number++;
- break;
case 'Y':
- fact = 10000000;
- dni_number++;
- break;
case 'Z':
- fact = 20000000;
- dni_number++;
+ /* NIE */
+ fact = dni_number[0] - 'X';
+ /* 7 or 8 digits */
+ if (2 == sscanf (&dni_number[1],
+ "%8u%c%c",
+ &num,
+ &chksum,
+ &dummy))
+ {
+ num += fact * 100000000;
+ }
+ else if (2 == sscanf (&dni_number[1],
+ "%7u%c%c",
+ &num,
+ &chksum,
+ &dummy))
+ {
+ num += fact * 10000000;
+ }
+ else
+ {
+ return false;
+ }
break;
default:
fact = 0;
- /* domestic */
+ /* DNI */
+ if (2 != sscanf (dni_number,
+ "%8u%c%c",
+ &num,
+ &chksum,
+ &dummy))
+ return false;
+ break;
}
-
- if (2 != sscanf (dni_number,
- "%7u%c%c"
- & num,
- &chksum,
- &dummy))
- return false;
- num += fact;
- if (map[num % 23] != chksum)
- return false;
if (map[num % 23] != chksum)
return false;
return true;