/**************************************************************************
<MODULE NAME>
<FILENAME> mod_pwd.c (password) </FILENAME>
<HISTORY>
  <MODIFICATION>
    <DATE> </DATE>
    <AUTHOR>  </AUTHOR>
    <DESCRIPTION>
    Password - catching screen - validation - Rijndael init.
    </DESCRIPTION>
  </MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/


/**************************************************************************
INCLUDE FILES
**************************************************************************/
#include <bios.h>
#include <io.h>
#include <fcntl.h>
#include <alloc.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <graphics.h>

#include "defs.h"
#include "fullspec.h"
#include "..\CrdReade.r\LibCrdRd.h"
#include "..\LibAlrBx\LIBALRBX.H"

/**************************************************************************
EXTERNAL VARIABLES
**************************************************************************/
extern int gaiPosv[10][4];
extern int giCodeVoteEnd;
extern unsigned char gucMavType;

/**************************************************************************
FUNCTION PROTOTYPES
**************************************************************************/
extern int  Aff_Clavier(void);
extern int  Initialise_Rijndael(void);
extern void Initialise_RijndaelMini(void);
extern int  Pen_Capture(int *, int *);
extern int (*ptfunct)(void);
extern void Msg_Error_Exit(char *);
extern void Screen_Pwd(void);
extern void Set_Cursor(int);
extern int  Verifie_Password(void);
extern int  Vote_End(void);

/**************************************************************************
GLOBAL VARIABLES
**************************************************************************/
char fullPassword[fullPasswordLength + 1];
char gszMinipassword[12];

/**************************************************************************
LOCAL VARIABLES
**************************************************************************/
static char Minicodage[5];

/**************************************************************************
<FUNCTION>
  <NAME> Pwd_Read </NAME>
  <TYPE> GLOBAL </TYPE>
  <DESCRIPTION>
  </DESCRIPTION>
</FUNCTION>
**************************************************************************/
int Pwd_Read(void)
{
 char bfs[2];
 int i, x;
 int iPenX, iPenY, iPenCapture, iHwdDisconnected;

 /* Reset the pwd output area */
 setfillstyle(SOLID_FILL,BLACK);
 bar(0,95,639,120);

 settextjustify(LEFT_TEXT,LEFT_TEXT);
 settextstyle(SMALL_FONT, HORIZ_DIR, 7);
 setcolor(WHITE);
 strcpy(fullPassword,"");

 Set_Cursor(CURSOR_ON);

 for(x = 0;x < 10;)
 {
    iHwdDisconnected = iPenCapture = Pen_Capture(&iPenX, &iPenY);
    if(iPenCapture == 1)
    {
      for(i = 0; i < 11;i++)
      {
       if((iPenX > gaiPosv[i][0] + 2) && (iPenX < gaiPosv[i][1] - 2))
       {
         if ((iPenY > gaiPosv[i][2] + 2) && (iPenY < gaiPosv[i][3] - 2))
         {
           if(i == 10)
           {
             strcpy(fullPassword,"");
             x = 0;
           }
           else
           {
             if(i == 9) itoa(0,bfs,10);
             else       itoa(i + 1,bfs,10);
             bfs[1] = 0x00;
             strcat(fullPassword,bfs);
             x++;
           }
         }
         setfillstyle(SOLID_FILL,BLACK);
         bar(0,95,639,120);
         outtextxy(265,110, fullPassword);
       }
      }
    }
    PEN_OR_ALARMBOX_DISCONNECTED
 }

 Set_Cursor(CURSOR_OFF);

 /* Black Screen */
 setfillstyle(SOLID_FILL,BLACK);
 bar(0,0,639,479);

 return(1);
}

/**************************************************************************
<FUNCTION>
  <NAME> Pwd </NAME>
  <TYPE> GLOBAL </TYPE>
  <DESCRIPTION>
  Asking for the password + validation + initialization of Rijndael
  </DESCRIPTION>
</FUNCTION>
**************************************************************************/
void Pwd(void)
{
  strcpy(Minicodage,"4630");

  /* Ask Password */
  strcpy(fullPassword,"1111111111");
  while(!Verifie_Password())
  {
    Screen_Pwd();
    Pwd_Read();
  }

  /*  Create new password  */
  gszMinipassword[0] = fullPassword[0];
  gszMinipassword[1] = fullPassword[2];
  gszMinipassword[2] = fullPassword[4];
  gszMinipassword[3] = fullPassword[6];
  gszMinipassword[4] = 0x00;
  strcat(gszMinipassword,Minicodage);

  /* looking for a:Floppy.be */
  if(access("a:Floppy.be", 0))
    Msg_Error_Exit("a:Floppy.be FILE ERROR");

  /* Initialise_Rijndael */
  if(!Initialise_Rijndael())
    Msg_Error_Exit("Initialise_Rijndael FILE ERROR");
  Initialise_RijndaelMini();

  /* looking for a:Machvots.tbl */
  if(access("Machvots.tbl", 0))
    Msg_Error_Exit("Machvots.tbl FILE ERROR");
}
