#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*RHP for tolower etc*/
#include <ctype.h>
#include "lprintf.h"

typedef unsigned char uuchar;
char *key_make(int month, int year, int users, int flags, char *host);
int key_test(char *k, int month, int year, int *users, int *flags, char *host, char *expires);
char *key_toascii(char *s, int len);
uuchar *key_tobin(char *s);
#ifndef TRUE
#define FALSE 0
#define TRUE (!FALSE)
#endif

#ifdef TEST
void main()
{
 	char name[100];
	char month[100],year[100],users[100];
	char tmp[200],ex[200];
	int u,f;

again:
	printf("Enter Name "); gets(name);
	printf("Enter Month "); gets(month);
	printf("Enter year "); gets(year);
	printf("Enter users "); gets(users);
	strcpy(tmp,key_make(atoi(month),atoi(year),atoi(users),123,name));
	printf("Key {%s} \n",tmp);
	printf("Enter Name "); gets(name);
	printf("Enter Month "); gets(month);
	printf("Enter year "); gets(year);
	if (key_test(tmp,atoi(month),atoi(year),&u,&f,name,ex)) {
		printf("License matched\n");
	}
	goto again;
}
#endif

char *key_make(int month, int year, int users, int flags, char *host)
{
	static uuchar key[40];
	int i,name,chk;
	if (year>1900) year-=1900;
	key[0] = (uuchar) month;
	key[1] = (uuchar) year;
	key[2] = (uuchar) users;
	key[3] = (uuchar) flags;
	for (i=0,name=0; i<4 && host[i]!=0; i++) name += ((tolower(host[i])*(i+1)));
	key[4] = (uuchar) (name % 128);
	if (strcmp(host,"*")==0) key[4] = 0;
	for (i=0,chk=0; i<5; i++) {chk = ((chk + (key[i]+i)*(i+1)*10) % 32000); }
	key[5] = (uuchar) (chk % 256);
	key[6] = (uuchar) (chk / 256);
	return key_toascii((char *) key,7);
}
int key_test(char *k, int month, int year, int *users, int *flags, char *host, char *ex)
{
	uuchar *key = key_tobin(k);
	int i,chk,name;
	char allowed[120];

	lprintf(allowed,120-1,"(%d users allowed)",key[2]);
	if (key[2]==0) lprintf(allowed,120-1,"(unlimited users)");
	lprintf(ex,100,"Temporary License expires after 31-%d-%d, %s",key[0]+1,key[1]+1900,allowed);
	if (key[1]==0) strcpy(ex,"");
	if (year>1900) year-=1900;
	if ((month>key[0]) && (year==key[1])) {lprintf(ex,100,"License expired"); return FALSE; }
	if ((year>key[1]) && (key[1]!=0)) {lprintf(ex,100,"License expired"); return FALSE; }
	*users = key[2];
	*flags = key[3];

	for (i=0,name=0; i<4 && host[i]!=0; i++) name += ((tolower(host[i])*(i+1)));
	name = name % 128;
	for (i=0,chk=0; i<5; i++) {chk = ((chk + (key[i]+i)*(i+1)*10) % 32000); }
	if ((chk % 256)!=key[5] || (chk / 256)!=key[6] ) 
		{lprintf(ex,100,"Checksum didn't match"); return FALSE; }
	if ((key[4] != name) && (key[4]!=0)) {lprintf(ex,100,"License for wrong pc {%s} ",host); return FALSE; }

	return TRUE;	
}

char *key_toascii(char *s, int len)
{
	static uuchar bf[100];
	int i;
	uuchar *out=bf;

	for (i=0; i<len ;i++)  {
 		*out++ = (uuchar) (((unsigned char) s[i]) / 10 + 'a');
		*out++ = (uuchar) (((unsigned char) s[i]) % 10 + '0');
	}
 	*(out++) = 0;
 	*(out++) = 0;
	return (char *) bf;
}
uuchar *key_tobin(char *sss)
{
	static uuchar bf[100];
        uuchar *out=bf;
        uuchar *s=(uuchar *)sss;


	for (;*s!=0;) {
		*out++ = (uuchar) (((*s - 'a') * 10) + (*(s+1) - '0'));
		s++;
		if (*s==0) break;
		s++;
	}
	return  bf;
}
