validation_DE_TIN.c (1749B)
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_DE_TIN.c 18 * @brief validation logic for German taxpayer identification numbers 19 * @author Christian Grothoff 20 */ 21 #include <string.h> 22 #include <stdbool.h> 23 24 25 /** 26 * Function to validate a German Taxpayer identification number. 27 * 28 * See https://de.wikipedia.org/wiki/Steuerliche_Identifikationsnummer 29 * for the structure! 30 * 31 * @param tin_number tax number to validate (input) 32 * @return true if validation passed, else false 33 */ 34 bool 35 DE_TIN_check (const char *tin_number); 36 37 /* declaration to fix compiler warning */ 38 bool 39 DE_TIN_check (const char *tin_number) 40 { 41 unsigned int csum; 42 unsigned int product = 10; 43 44 if (strlen (tin_number) != 11) 45 return false; 46 for (unsigned int i = 0; i<10; i++) 47 { 48 unsigned int sum = ((tin_number[i] - '0') + product) % 10; 49 if (0 == sum) 50 sum = 10; 51 product = sum * 2 % 11; 52 } 53 csum = 11 - product; 54 if (10 == csum) 55 csum = 0; 56 if (tin_number[10] != '0' + csum) 57 return false; 58 if (tin_number[0] == '0') 59 return false; 60 return true; 61 }