validation_CH_AHV.c (1742B)
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_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 ahv_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 /* declaration to fix compiler warning */ 36 bool 37 CH_AHV_check (const char *ahv_number) 38 { 39 unsigned int checknum; 40 unsigned int next_ten; 41 const char *pos = &ahv_number[strlen (ahv_number) - 1]; 42 bool phase = true; 43 unsigned int calculation = 0; 44 45 checknum = *pos - 48; 46 while (pos > ahv_number) 47 { 48 pos--; 49 if ('.' == *pos) 50 continue; 51 if (phase) 52 calculation += ((*pos - 48) * 3); 53 else 54 calculation += *pos - 48; 55 phase = ! phase; 56 } 57 /* round up to the next ten */ 58 next_ten = ((calculation + 9) / 10) * 10; 59 calculation = next_ten - calculation; 60 return (checknum == calculation); 61 }