validation_NL_BSN.c (1710B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2021 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU 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 reducer/validation_NL_BSN.c 18 * @brief Validation for Dutch Buergerservicenummern 19 * @author Christian Grothoff 20 */ 21 #include <string.h> 22 #include <stdbool.h> 23 24 25 /** 26 * Function to validate a Dutch Social Security number. 27 * 28 * See https://nl.wikipedia.org/wiki/Burgerservicenummer 29 * 30 * @param bsn_number social security number to validate (input) 31 * @return true if validation passed, else false 32 */ 33 bool 34 NL_BSN_check (const char *bsn_number); 35 36 /* declaration to fix compiler warning */ 37 bool 38 NL_BSN_check (const char *bsn_number) 39 { 40 static const int factors[] = { 41 9, 8, 7, 6, 5, 4, 3, 2, -1 42 }; 43 unsigned int sum = 0; 44 45 if (strlen (bsn_number) != 9) 46 return false; 47 for (unsigned int i = 0; i<8; i++) 48 { 49 unsigned char c = (unsigned char) bsn_number[i]; 50 51 if ( ('0' > c) || ('9' < c) ) 52 return false; 53 sum += (c - '0') * factors[i]; 54 } 55 { 56 unsigned char c = (unsigned char) bsn_number[8]; 57 unsigned int v = (c - '0'); 58 59 return (sum % 11 == v); 60 } 61 }