#include "register.hpp"

unsigned long TestRegistration(char* uszName,char* uszCode)
{
   unsigned long flOk;

   if (strlen(uszName) ==  0 || strlen(uszCode) == 0 )
      return 0;

   flOk = !strcmp(uszCode,GenRegString(uszName));
   return flOk;
}

char* GenRegString(char* uszInString)
{
   unsigned long ulOrgKey,ulTemp,ulKey,ulRegKey = 0;
   unsigned long ulCounter = 0;
   unsigned long ulCount;
   char* uszOutString;
   char uszTemp[5] = {0,0,0,0,0};

   #ifdef __TOS_OS2__
   char uszKonv[41] = {'A','S','D','E','H','T','H','Y','B','M',
                       'D','O','E','Z','X','W','T','Q','J','K',
                       'A','S','D','E','H','T','H','Y','B','M',
                       'D','O','E','Z','X','W','T','Q','J','K'};
   #else
   char uszKonv[41] = {'T','G','Y','N','P','A','S','R','V','T',
                       'Q','Z','X','U','M','V','B','R','T','K',
                       'T','G','Y','N','P','A','S','R','V','T',
                       'Q','Z','X','U','M','V','B','R','T','K'};
   #endif

   while (uszInString[ulCounter] != 0)
   {
     #ifdef __TOS_OS2__
     ulRegKey += ((unsigned long) uszInString[ulCounter++]) * ((ulCounter % 2 == 0) ? 23 : 47   );
     ulRegKey += (ulRegKey % 2 == 0) ? 5 : 9;
     #else
     ulRegKey += ((unsigned long) uszInString[ulCounter++]) * ((ulCounter % 2 == 0) ? 32 : 56   );
     ulRegKey += (ulRegKey % 2 == 0) ? 7 : 6;
     #endif
   }

   ulKey = ulRegKey;

   uszOutString = (char*) calloc(400,1);
   ulCount = 0;

   while ( ulKey != 0)
   {
      ulTemp = ulKey - ((unsigned long) (ulKey / 10) * 10);
      if ( (ulTemp % 3 == 0) )
      {
         uszTemp[0] = uszKonv[ulTemp+ulCount];
         sprintf(uszOutString+strlen(uszOutString),"%s",uszTemp);
         ulCount++;
      }
      #ifdef __TOS_OS2__
      if ((ulTemp % 4 == 0) )
      #else
      if ((ulTemp % 5 == 0) )
      #endif
      {
         uszTemp[0] = uszKonv[ulTemp+ulCount];
         sprintf(uszOutString+strlen(uszOutString),"%s",uszTemp);
         uszTemp[0] = uszKonv[ulTemp+10+ulCount];
         sprintf(uszOutString+strlen(uszOutString),"%s",uszTemp);
         ulCount++;
      }
      sprintf(uszOutString+strlen(uszOutString),"%i",ulTemp);

      ulKey = ulKey / 10;
   }
   return uszOutString;
}
