/*******************************************************************/
/*                     MODULE VOTE.C                               */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"

#include "vote.h"

char     szVoteContent[76]; 
char     Statut_Vote;
char     Statut_Val;
char     Crc_Calcul[5],Crc_Recup[5];

char _Code[] = "0EC3ZN678LAB2DFRH1IJK9M5OPQGSTUVWXY4";

/***********************************************/
/*
   <FUNCTION>
		<NAME>Recupere_Jeton</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Recover the token of the polling station registered on the card
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte, Jeton</NAME>
			<TYPE>char *, char *</TYPE>
			<IO>IN,INOUT</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Recupere_Jeton(char *Carte, char *Jeton)
{
  int i;

  for (i = 0; i < _C_JETON_MAX_BYTE;
                          Jeton[i] = Carte[_C_JETON_PLACE + i], i++);
  Jeton[_C_JETON_MAX_BYTE] = '\0';
} /* Recupere_Jeton */

/***********************************************/
/*
   <FUNCTION>
		<NAME>Calcul_Jeton</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Calculate the token of the polling station registered on the card
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Jeton_Etendu, Jeton</NAME>
			<TYPE>char *, char *</TYPE>
			<IO>IN,INOUT</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Calcul_Jeton(char *Jeton_Etendu, char *Jeton)
{
    int jj, mm,aa;
    int cant,buv1,buv2,tmp;
    char Cjj[3],Cmm[3],Caa[3];
    char Ccant[4], Cbuv1[4],Cbuv2[4];

    strncpy(Ccant,Jeton_Etendu + 3,3);
    strncpy(Cbuv1,Jeton_Etendu  + 6,2);
    strncpy(Cbuv2,Jeton_Etendu  + 8,1);
    strncpy(Cjj,Jeton_Etendu   + 9,2);
    strncpy(Cmm,Jeton_Etendu  + 11,2);
    strncpy(Caa,Jeton_Etendu  + 13,2);

    Ccant[3] = '\0';
    Cbuv1[2] = '\0';
    Cbuv2[1] = '\0';
    Cjj[2]   = '\0';
    Cmm[2]   = '\0';
    Caa[2]   = '\0';

    cant     = atoi(Ccant);
    buv1     = atoi(Cbuv1);
    buv2     = atoi(Cbuv2);
    jj       = atoi(Cjj);
    mm       = atoi(Cmm);
    aa       = atoi(Caa);


    Jeton[1] = _Code[buv1 % 35];
    Jeton[3] = _Code[buv2 % 35];
    Jeton[2] = _Code[(jj + cant) % 35];
    Jeton[0] = _Code[mm   % 35];
    Jeton[4] = _Code[aa   % 35];

    Jeton[5] = '\0';
}
/***********************************************/
/*
   <FUNCTION>
		<NAME>Calcul_Jeton</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Write the token of the polling station on the card
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte, Jeton</NAME>
			<TYPE>char *, char *</TYPE>
			<IO>IN,IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Encode_Jeton(char *Carte, char *Jeton)
{
   int i;

   for (i = 0; i < _C_JETON_MAX_BYTE;
                          Carte[_C_JETON_PLACE + i] = Jeton[i], i++);
} /* Encode_Jeton */

/***********************************************/
/*
   <FUNCTION>
		<NAME>Recupere_Vote</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Recover vote contents from the card
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte</NAME>
			<TYPE>char *</TYPE>
			<IO>IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Recupere_Vote(char *Carte)
{
   int  debut, i;

   debut = _C_CRC_PLACE ;
   memset(szVoteContent, 0, sizeof(szVoteContent));
   for (i = 0; i < (_C_CRC_MAX_BYTE + _C_TEST_MAX_BYTE + C_VOTE_MAX_BYTE);
                             szVoteContent[i] = Carte[debut++], i++);
   szVoteContent[i] = '\0';
   
} /* Recupere_Vote */
/***********************************************/
/*
   <FUNCTION>
		<NAME>Recupere_stat</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Recover the type of voter registered on a card (Urn reader card)
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte</NAME>
			<TYPE>char *</TYPE>
			<IO>IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Recupere_stat(char *Carte)  
{
  Statut_Vote =  Carte[_C_TEST_PLACE];
} /*  */

/***********************************************/
/*
   <FUNCTION>
		<NAME>Recupere_stat_Val</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Recover the type of voter registered on a card (Validator reader card)
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte</NAME>
			<TYPE>char *</TYPE>
			<IO>IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/

void Recupere_stat_Val(char *Carte) 
{
  Statut_Val =  Carte[_C_TEST_PLACE];
} /* Recupere_Statut_Valideuse */
/***********************************************/
/*
   <FUNCTION>
		<NAME>Initialise_Carte</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Initialize a card with blank
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte</NAME>
			<TYPE>char *</TYPE>
			<IO>INOUT</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Initialise_Carte(char *Carte)
{
   int i;

   for (i = 0; i < (C_VOTE_MAX_BYTE  + _C_VOTE_PLACE); i++)  
     Carte[_C_DATA_PLACE + i] = ' ';
} /* Initialise_Carte */

/***********************************************/
/*
   <FUNCTION>
		<NAME>Encode_Crc</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Write CRC on the card
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte</NAME>
			<TYPE>char *</TYPE>
			<IO>INOUT</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Encode_Crc(char *Carte)
{
  int i;

  for (i = 0; i < _C_CRC_MAX_BYTE; 
                         Carte[_C_CRC_PLACE + i] = Crc_Calcul[i],i++);
}
/***********************************************/
/*
   <FUNCTION>
		<NAME>Recupere_Crc</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Recover CRC on the card</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte, Place</NAME>
			<TYPE>char *, int</TYPE>
			<IO>IN,IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Recupere_Crc(char *Carte,int Place)
{
  int i;

  for (i = 0; i < _C_CRC_MAX_BYTE;
                        Crc_Recup[i] = Carte[Place + i], i++);
  Crc_Recup[4] = 0x00;
} 
/*******************************************************************/
/* END OF MODULE VOTE.C                                           */
/*******************************************************************/

