00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
00031
00032
00033
00034
00035 static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
00036 {
00037 unsigned temp;
00038
00039
00040
00041 temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
00042 return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
00043 }
00044
00045
00046
00047
00048 static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
00049 {
00050 (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
00051 (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
00052 (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
00053 {
00054 register int keyshift = (int)((*(pkeys+1)) >> 24);
00055 (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
00056 }
00057 return c;
00058 }
00059
00060
00061
00062
00063
00064
00065 static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
00066 {
00067 *(pkeys+0) = 305419896L;
00068 *(pkeys+1) = 591751049L;
00069 *(pkeys+2) = 878082192L;
00070 while (*passwd != '\0') {
00071 update_keys(pkeys,pcrc_32_tab,(int)*passwd);
00072 passwd++;
00073 }
00074 }
00075
00076 #define zdecode(pkeys,pcrc_32_tab,c) \
00077 (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
00078
00079 #define zencode(pkeys,pcrc_32_tab,c,t) \
00080 (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
00081
00082 #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
00083
00084 #define RAND_HEAD_LEN 12
00085
00086 # ifndef ZCR_SEED2
00087 # define ZCR_SEED2 3141592654UL
00088 # endif
00089
00090 static int crypthead(const char* passwd,
00091 unsigned char* buf,
00092 int bufSize,
00093 unsigned long* pkeys,
00094 const unsigned long* pcrc_32_tab,
00095 unsigned long crcForCrypting)
00096 {
00097 int n;
00098 int t;
00099 int c;
00100 unsigned char header[RAND_HEAD_LEN-2];
00101 static unsigned calls = 0;
00102
00103 if (bufSize<RAND_HEAD_LEN)
00104 return 0;
00105
00106
00107
00108
00109
00110 if (++calls == 1)
00111 {
00112 srand((unsigned)(time(NULL) ^ ZCR_SEED2));
00113 }
00114 init_keys(passwd, pkeys, pcrc_32_tab);
00115 for (n = 0; n < RAND_HEAD_LEN-2; n++)
00116 {
00117 c = (rand() >> 7) & 0xff;
00118 header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
00119 }
00120
00121 init_keys(passwd, pkeys, pcrc_32_tab);
00122 for (n = 0; n < RAND_HEAD_LEN-2; n++)
00123 {
00124 buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
00125 }
00126 buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
00127 buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
00128 return n;
00129 }
00130
00131 #endif