secu.c
00001 #include <stdio.h>
00002 #include <string.h>
00003
00004 struct numero_SS_t {
00005 unsigned char sexe;
00006 unsigned char annee;
00007 unsigned char mois;
00008 unsigned char dept;
00009 unsigned int commune;
00010 unsigned int ordre;
00011 unsigned char clef;
00012 };
00013
00014 void print_mois(char m) {
00015 const char * tab[] = {
00016 "janvier", "février", "mars", "avril", "mai", "juin", "juillet",
00017 "août", "septembre", "octobre", "novembre", "décembre"
00018 };
00019 if ((m < 1) || (m > 12)) printf("INCONNU");
00020 else printf("%s", tab[m]);
00021 }
00022
00023 int str2int(char * s, unsigned inf, unsigned sup) {
00024 if ((inf > sup) || (sup >= strlen(s))) return -1;
00025 int res = 0, i;
00026 for (i=inf; i<= sup; i++) res = res*10 + (s[i] - '0');
00027 return res;
00028 }
00029
00030 int compute_key(struct numero_SS_t * nss) {
00031 unsigned long r1 = (nss->dept + (nss->mois * 100) + (nss->annee * 10000) +
00032 (nss->sexe * 1000000)) % 97;
00033 unsigned long res =
00034 97 - ((r1 * 1000000) + (nss->commune * 1000) + nss->ordre) % 97;
00035 return res;
00036 }
00037
00038 int check_bad_NSS_format(char * s) {
00039 if ((s == NULL) || (strlen(s) != 15)) return -1;
00040 int i;
00041 for (i=0; i<15; i++)
00042 if ((s[i] < '0') || s[i] > '9') return -1;
00043 return 0;
00044 }
00045
00046 int getNSSfrom(char * s, struct numero_SS_t * nss) {
00047 if ((nss == NULL) || check_bad_NSS_format(s)) return -1;
00048 nss->sexe = s[0] - '0';
00049 nss->annee = str2int(s,1,2);
00050 nss->mois = str2int(s,3,4);
00051 nss->dept = str2int(s,5,6);
00052 nss->commune = str2int(s,7,9);
00053 nss->ordre = str2int(s,10,12);
00054 nss->clef = str2int(s,13,14);
00055 return 0;
00056 }
00057
00058 void getInfoFrom(struct numero_SS_t * nss) {
00059 if (compute_key(nss) == nss->clef) {
00060 printf("%s en ",(nss->sexe == 1)?"Homme né":"Femme née");
00061 print_mois(nss->mois);
00062 printf(" %d dans le département %d\n",
00063 nss->annee, nss->dept);
00064 } else printf("Clef de contrôle incorrecte\n");
00065 }
00066
00067 void printNSS(struct numero_SS_t * nss) {
00068 printf("%d %d %d %d %d %d -- %d\n", nss->sexe, nss->annee,
00069 nss->mois,nss->dept ,nss->commune,nss->ordre,nss->clef);
00070 }
00071
00072 int main()
00073 {
00074 struct numero_SS_t nss;
00075 char * s = "253077507300483";
00076 if (getNSSfrom(s,&nss) == -1) return 1;
00077 printNSS(&nss);
00078 getInfoFrom(&nss);
00079 return 0;
00080 }