Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // Chess Giants key generator
  2.  
  3. #include <windows.h>
  4. #include <commctrl.h>
  5. #include "rsrc.h"
  6.  
  7.  
  8. // global variables used in this module only
  9. static HINSTANCE hAppInstance;
  10.  
  11.  
  12. // function prototypes
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow);
  14. static int WINAPI MainDialogProc (HWND hWnd, unsigned int message, unsigned int wParam, long lParam);
  15. static unsigned long ComputeRegistrationCode (const char *email);
  16. static void CenterWindow (HWND hWnd);
  17.  
  18.  
  19. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
  20. {
  21.    // program entrypoint
  22.  
  23.    hAppInstance = hInstance; // save application instance
  24.  
  25.    InitCommonControls ();
  26.    OleInitialize (NULL); // initialize OLE (for XP style)
  27.  
  28.    // fire up the main dialog box
  29.    DialogBox (hAppInstance, (char *) DIALOG_MAIN, 0, MainDialogProc);
  30.  
  31.    OleUninitialize ();
  32.  
  33.    return (0); // return to Windows
  34. }
  35.  
  36.  
  37. static int WINAPI MainDialogProc (HWND hWnd, unsigned int message, unsigned int wParam, long lParam)
  38. {
  39.    // mesage handler for main dialog box
  40.  
  41.    static char email[256];
  42.  
  43.    // have we just fired up this window ?
  44.    if (message == WM_INITDIALOG)
  45.    {
  46.       // center window on screen
  47.       CenterWindow (hWnd);
  48.  
  49.       // set dialog icons (small one for title bar & big one for task manager)
  50.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, (char *) ICON_MAIN));
  51.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, (char *) ICON_MAIN));
  52.  
  53.       return (1);
  54.    }
  55.  
  56.    // else did we click the close button on the title bar ?
  57.    else if (message == WM_CLOSE)
  58.    {
  59.       EndDialog (hWnd, 0); // if so, tell the dialog box to exit
  60.       return (1);
  61.    }
  62.  
  63.    // else did we alter a control INSIDE the window ?
  64.    else if (message == WM_COMMAND)
  65.    {
  66.       // was something entered in the edit box ?
  67.       if ((HIWORD (wParam) == EN_CHANGE) && (LOWORD (wParam) == EDITBOX_EMAIL))
  68.       {
  69.          GetDlgItemText (hWnd, EDITBOX_EMAIL, email, sizeof (email));
  70.          if (strlen (email) > sizeof ("a@b.c") - 1)
  71.             SetDlgItemInt (hWnd, EDITBOX_ACTIVATIONCODE, ComputeRegistrationCode (email), 0);
  72.          else
  73.             SetDlgItemText (hWnd, EDITBOX_ACTIVATIONCODE, "More chars!");
  74.  
  75.          return (1);
  76.       }
  77.  
  78.    }
  79.  
  80.    return (0);
  81. }
  82.  
  83.  
  84. static unsigned long ComputeRegistrationCode (const char *email)
  85. {
  86.    // quick helper to see if the program is registered. It contains an address to potential crackers.
  87.    // Notice: user's email address may be a wchar_t array, and thus may contain Unicode characters.
  88.    // /!\ WARNING: THE CRACKER MESSAGE SHOULD NEVER CHANGE, AND NEITHER SHOULD THE ALGORITHM BELOW /!\
  89.  
  90.    static const char crackermsg[] = "Please, respect my work. DON'T PUBLISH if you crack my program. Thank you and happy cracking :)";
  91.  
  92.    unsigned long correct_activationcode;
  93.    int byte_index;
  94.    int length;
  95.  
  96.    // compute the maximal length of the string for which we need to checksum
  97.    length = strlen (email);
  98.    if (length > sizeof (crackermsg) - 1)
  99.       length = sizeof (crackermsg) - 1; // bound it to the length of the cracker message
  100.  
  101.    // hash the supplied e-mail
  102.    correct_activationcode = 5381; // start value
  103.    for (byte_index = 0; byte_index < sizeof (crackermsg) - 1; byte_index++)
  104.       correct_activationcode = ((correct_activationcode << 5) + correct_activationcode)
  105.                                  + ((unsigned long) (length > 0 ? tolower (email[byte_index % length]) : 1) // prevent zero divide
  106.                                     ^ (unsigned long) crackermsg[byte_index]); // hash = hash * 33 + (char(email) ^ char(crackermsg))
  107.    correct_activationcode &= 0x7FFFFFFF; // make sure the results remain positive
  108.  
  109.    // return the correct code
  110.    return (correct_activationcode);
  111. }
  112.  
  113.  
  114. static void CenterWindow (HWND hWnd)
  115. {
  116.    // this function centers the specified window on screen.
  117.  
  118.    RECT rRect;
  119.    int width;
  120.    int height;
  121.    int x;
  122.    int y;
  123.  
  124.    // get the current rectangle of the current window
  125.    GetWindowRect (hWnd, &rRect);
  126.    width = rRect.right - rRect.left;
  127.    height = rRect.bottom - rRect.top;
  128.  
  129.    // draw window in the center of the screen
  130.    x = GetSystemMetrics (SM_CXSCREEN) / 2 - width / 2;
  131.    y = GetSystemMetrics (SM_CYSCREEN) / 2 - height / 2;
  132.  
  133.    // now ask to change the position of the window
  134.    SetWindowPos (hWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  135.  
  136.    return; // finished
  137. }
  138.