/* showpin.c */

/********************/
/* XDA PIN-VIEWER   */
/* for MS-Windows   */
/********************/

/*------------*/
/*  INCLUDES  */
/*------------*/

#include "windows.h"
#include "resource.h"

/*-----------*/
/*  DEFINES  */
/*-----------*/

#define K_PINMSG_LEN  (22)
#define K_VALBUF_LEN  (256)

/*--------------*/
/*  STRUCTURES  */
/*--------------*/

/*--------------*/
/*  PROTOTYPES  */
/*--------------*/

static signed short read_valbuf (void);

/*-------------*/
/*  VARIABLEN  */
/*-------------*/

static TCHAR         showpin_pinmsg[K_PINMSG_LEN];
static unsigned char showpin_valbuf[K_VALBUF_LEN];

/*--------------*/
/*  KONSTANTEN  */
/*--------------*/

/* Text-Messages */
static const TCHAR showpin_msg_title[] = TEXT ("Show PIN");
static const TCHAR showpin_msg_info[]  = TEXT ("Your PIN is ");
static const TCHAR showpin_msg_err[]   = TEXT ("Unknown PIN (Error X)");

/* Registry-Keys */
static const TCHAR showpin_txt_regkey1[] = \
  TEXT ("ControlPanel\\PhoneExtendFunction");
static const TCHAR showpin_txt_regkey2[] = \
  TEXT ("ExtendData");

/*============================================================================*/
/*                       S   H   O   W   -   P   I   N                        */
/*============================================================================*/

/*-----------------*/
/*  WINDOWS-ENTRY  */
/*-----------------*/
                                                               /* Entry-Point */
int APIENTRY WinMain (HINSTANCE inst, HINSTANCE pinst,
                      LPTSTR cmdline, int show)
{
  signed short n;

  /* Read value-buffer */
  n = read_valbuf ();

  /* Build result-string */
  if (!n)
  {
    lstrcpy (showpin_pinmsg, showpin_msg_info);
    showpin_pinmsg[12] = showpin_valbuf[0];
    showpin_pinmsg[13] = showpin_valbuf[1];
    showpin_pinmsg[14] = showpin_valbuf[2];
    showpin_pinmsg[15] = showpin_valbuf[3];
    showpin_pinmsg[16] = 0;
  }
  else
  {
    lstrcpy (showpin_pinmsg, showpin_msg_err);
    showpin_pinmsg[19] = 0x30 + n;
  }

  /* Display result */
  MessageBox (NULL, showpin_pinmsg, showpin_msg_title, \
              MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND);
  return n;
}

/*----*/

static signed short read_valbuf (void)                           /* Read Data */
{
  signed short n;
  DWORD type;
  DWORD len;
  HKEY key;
  LONG rc;

  /* Open key */
  rc = RegOpenKeyEx (HKEY_CURRENT_USER, showpin_txt_regkey1, 0L, 0L, &key);
  if (rc)
    return 1;

  /* Read data */
  n = 0;
  rc = RegQueryValueEx (key, showpin_txt_regkey2, NULL, &type, NULL, &len);
  if (rc)
    n = 2;
  else if (type != REG_BINARY)
    n = 3;
  else if (len != K_VALBUF_LEN)
    n = 4;
  if (!n)
  {
    rc = RegQueryValueEx (key, showpin_txt_regkey2, NULL, &type, \
                          showpin_valbuf, &len);
    if (rc)
      n = 5;
    else if (type != REG_BINARY)
      n = 6;
    else if (len != K_VALBUF_LEN)
      n = 7;
  }
  RegCloseKey (key);
  return n;
}


/*** EOF ***/
