anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

validation_CZ_BN.c (1708B)


      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_CZ_BN.c
     18  * @brief validation of Czeck Birth Numbers
     19  * @author Christian Grothoff
     20  */
     21 #include <string.h>
     22 #include <stdbool.h>
     23 #include <stdio.h>
     24 #include <gcrypt.h>
     25 
     26 /**
     27  * Function to validate a Check birth number. Basically,
     28  * if it has 10 digits, it must be divisible by 11.
     29  *
     30  * @param b_number birth number to validate (input)
     31  * @return true if b_number is valid
     32  */
     33 bool
     34 CZ_BN_check (const char *b_number);
     35 
     36 /* declaration to fix compiler warning */
     37 bool
     38 CZ_BN_check (const char *b_number)
     39 {
     40   unsigned long long n;
     41   char dummy;
     42   char in[11];
     43 
     44   if (10 == strlen (b_number))
     45     return true;
     46   if (11 != strlen (b_number))
     47     return false;
     48   if (b_number[6] != '/')
     49     return false;
     50   memcpy (in,
     51           b_number,
     52           6);
     53   memcpy (&in[6],
     54           &b_number[7],
     55           4);
     56   in[10] = '\0';
     57   if (1 != sscanf (in,
     58                    "%llu%c",
     59                    &n,
     60                    &dummy))
     61     return false;
     62   return 0 == (n % 11);
     63 }