--- mog-svn-20081018/sources/passwords.cpp 2008-02-25 22:59:14.000000000 +0100 +++ mog-svn-20081018-BD/sources/passwords.cpp 2008-10-18 13:33:03.000000000 +0200 @@ -1,3 +1,5 @@ +#include +//#include /* Datos necesarios para guardar los codigos: */ extern int n_arrows,n_coins,n_keys; extern int player_max[2],player_energy[2],player_denergy[2]; @@ -12,47 +14,47 @@ -void digit2BCD(int digit,int BCD[4]) +void digit2BCD(int digit,bool BCD[4]) { - if ((digit&0x08)==0) BCD[0]=0; - else BCD[0]=1; - if ((digit&0x04)==0) BCD[1]=0; - else BCD[1]=1; - if ((digit&0x02)==0) BCD[2]=0; - else BCD[2]=1; - if ((digit&0x01)==0) BCD[3]=0; - else BCD[3]=1; + BCD[0]=((digit&0x08)!=0); + BCD[1]=((digit&0x04)!=0); + BCD[2]=((digit&0x02)!=0); + BCD[3]=((digit&0x01)!=0); } /* digit2BCD */ -int BCD2digit(int BCD[4]) +int BCD2digit(bool BCD[4]) { - return BCD[3]+BCD[2]*2+BCD[1]*4+BCD[0]*8; + return + (BCD[3]<<0)+ + (BCD[2]<<1)+ + (BCD[1]<<2)+ + (BCD[0]<<3); } /* BCD2digit */ -void value2block(int val,int block[4]) +void value2block(int val,bool block[5]) { - if ((val&0x10)==0) block[0]=0; - else block[0]=1; - if ((val&0x08)==0) block[1]=0; - else block[1]=1; - if ((val&0x04)==0) block[2]=0; - else block[2]=1; - if ((val&0x02)==0) block[3]=0; - else block[3]=1; - if ((val&0x01)==0) block[4]=0; - else block[4]=1; + block[0]=((val&0x10)!=0); + block[1]=((val&0x08)!=0); + block[2]=((val&0x04)!=0); + block[3]=((val&0x02)!=0); + block[4]=((val&0x01)!=0); } /* value2block */ -int block2value(int block[5]) +int block2value(bool block[5]) { - return block[4]+block[3]*2+block[2]*4+block[1]*8+block[0]*16; + return + (block[4]<<0)+ + (block[3]<<1)+ + (block[2]<<2)+ + (block[1]<<3)+ + (block[0]<<4); } /* block2value */ -void writebits(int *bits,int *dest,int start,int len) +void writebits(bool *bits,bool *dest,int start,int len) { int i; @@ -62,7 +64,7 @@ } /* writebits */ -void readbits(int *bits,int *data,int start,int len) +void readbits(bool *bits,bool *data,int start,int len) { int i; @@ -72,12 +74,12 @@ } /* readbits */ -void bits2characters(int *bits,char *passwd) +void bits2characters(bool *bits,char *passwd) { int i; int pos,val; - int lbits[5]; - char *str="F3RU72G4X5BKN90PQTHVE6OMWAJ18DSILYCZ"; + bool lbits[5]; + char str[]="F3RU72G4X5BKN90PQTHVE6OMWAJ18DSILYCZ"; for(i=0;i<45;i++) { pos=i&0x03; @@ -88,46 +90,50 @@ } /* bits2characters */ -void characters2bits(int *bits,char *passwd) +void characters2bits(bool *bits,char *passwd) { int i,j; int pos,val; - int lbits[5]; - char *str="F3RU72G4X5BKN90PQTHVE6OMWAJ18DSILYCZ"; + bool lbits[5]; + char str[]="F3RU72G4X5BKN90PQTHVE6OMWAJ18DSILYCZ.... "; for(i=0;i<45;i++) { pos=i&0x03; val=0; - for(j=0;j<36;j++) { - if (str[j]==passwd[i]) val=j+pos-3; - } /* for */ + char *idx=strchr(str,passwd[i]); + if (idx) val=(idx-str)+pos-3; value2block(val,lbits); writebits(lbits,bits,i*5,5); } /* for */ } /* bits2characters */ - -void addchecksum(char *passwd) +inline unsigned char pw_char2num(char ch) { - char *str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - char *hex="0123456789ABCDEF"; + if (ch == ' ') return 0; + if (ch >= '0' && ch <= '9') return ch - '0' + 1; + if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 1 + 10; + return 0; // Fallback for unknown characters. +} - int sum=0; - int pos=0,i; +inline unsigned char pw_checksumchars2num (char ch1, char ch2) +{ + return ((pw_char2num(ch1) - 1) << 4) | (pw_char2num(ch2) - 1); +} +void addchecksum(char *passwd) +{ + char hex[]="0123456789ABCDEF"; + unsigned char sum=0; - for(pos=0;pos<43;pos++) { - for(i=0;i<36;i++) { - if (passwd[pos]==str[i]) { - sum+=i+1; - } /* if */ - } /* for */ - } /* while */ + for(int pos=0;pos<43;pos++) { + sum+=pw_char2num (passwd[pos]); + } /* for */ passwd[43]=hex[(sum>>4)%16]; passwd[44]=hex[sum%16]; +//fprintf(stderr, "addchecksum():Checksum=%02x\n", sum); } /* addchecksum */ @@ -135,8 +141,9 @@ { int i; int val; - int bits[45*5]; - int BCD[4],block[5]; + bool bits[45*5]; + bool BCD[4]; + bool block[5]; for(i=0;i<45*5;i++) bits[i]=0; @@ -252,14 +259,16 @@ { int i; char pt[45]; - int bits[45*5]; - int BCD[4]; - int block[5]; + bool bits[45*5]; + bool BCD[4]; + bool block[5]; int val; for(i=0;i<45;i++) pt[i]=passwd[i]; addchecksum(pt); - if (pt[43]!=passwd[43] || pt[44]!=passwd[44]) return false; + unsigned char computed_checksum = pw_checksumchars2num (pt[43], pt[44]); + unsigned char entered_checksum = pw_checksumchars2num (passwd[43], passwd[44]); + if (computed_checksum - entered_checksum) return false; characters2bits(bits,passwd);