5 void aes_set_key(char *key
)
7 AES_set_decrypt_key(key
, 128, &aeskey
);
8 AES_set_encrypt_key(key
, 128, &client
[cs_idx
].aeskey
);
11 void aes_decrypt(uchar
*buf
, int n
)
15 AES_decrypt(buf
+i
, buf
+i
, &aeskey
);
18 void aes_encrypt_idx(int idx
, uchar
*buf
, int n
)
22 AES_encrypt(buf
+i
, buf
+i
, &client
[idx
].aeskey
);
25 char *remote_txt(void)
30 return("remote server");
37 register char *p1
, *p2
;
42 (*p1
==' ') || (*p1
=='\t') || (*p1
=='\n') || (*p1
=='\r');
48 if ((l
=strlen(txt
))>0)
50 (*p1
==' ') || (*p1
=='\t') || (*p1
=='\n') || (*p1
=='\r');
56 char *strtolower(char *txt
)
66 if ((c
>='0') && (c
<='9')) return(c
-'0');
67 if ((c
>='A') && (c
<='F')) return(c
-'A'+10);
68 if ((c
>='a') && (c
<='f')) return(c
-'a'+10);
72 int cs_atob(uchar
*buf
, char *asc
, int n
)
77 if ((rc
=(gethexval(asc
[i
<<1])<<4)|gethexval(asc
[(i
<<1)+1]))&0x100)
84 ulong
cs_atoi(char *asc
, int l
, int val_on_err
)
88 for (i
=((l
-1)<<1), errno
=0; (i
>=0) && (n
<4); i
-=2)
91 b
=(gethexval(asc
[i
])<<4) | gethexval(asc
[i
+1]);
95 rc
=(val_on_err
) ? 0xFFFFFFFF : 0;
104 int byte_atob(char *asc
)
108 if (strlen(trim(asc
))!=2)
111 if ((rc
=(gethexval(asc
[0])<<4)|gethexval(asc
[1]))&0x100)
116 long word_atob(char *asc
)
120 if (strlen(trim(asc
))!=4)
124 rc
=gethexval(asc
[0])<<12 | gethexval(asc
[1])<<8 |
125 gethexval(asc
[2])<<4 | gethexval(asc
[3]);
132 int key_atob(char *asc
, uchar
*bin
)
135 for (i
=rc
=0; i
<32; i
+=2)
137 if ((n1
=gethexval(asc
[i
]))<0) rc
=(-1);
138 if ((n2
=gethexval(asc
[i
+1]))<0) rc
=(-1);
139 bin
[i
>>1]=(n1
<<4)+(n2
&0xff);
144 int key_atob14(char *asc
, uchar
*bin
)
147 for (i
=rc
=0; i
<28; i
+=2)
149 if ((n1
=gethexval(asc
[i
]))<0) rc
=(-1);
150 if ((n2
=gethexval(asc
[i
+1]))<0) rc
=(-1);
151 bin
[i
>>1]=(n1
<<4)+(n2
&0xff);
156 char *key_btoa(char *asc
, uchar
*bin
)
158 int i
;//, n1, n2, rc;
163 sprintf(asc
+(i
<<1), "%02X", bin
[i
]);
167 char *cs_hexdump(int m
, uchar
*buf
, int n
)
170 static char dump
[520];
174 if (m
*n
>=sizeof(dump
)) n
=(sizeof(dump
)/m
)-1;
176 sprintf(dump
+(m
*i
++), "%02X%s", *buf
++, (m
>2)?" ":"");
180 static int inet_byteorder
=0;
181 in_addr_t
cs_inet_order(in_addr_t n
)
184 inet_byteorder
=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
185 switch (inet_byteorder
)
190 n
=((n
&0xff000000) >> 24 ) |
191 ((n
&0x00ff0000) >> 8 ) |
192 ((n
&0x0000ff00) << 8 ) |
193 ((n
&0x000000ff) << 24 );
199 char *cs_inet_ntoa(in_addr_t n
)
202 in
.s_addr
=cs_inet_order(n
);
203 return((char *)inet_ntoa(in
));
206 in_addr_t
cs_inet_addr(char *txt
)
210 inet_byteorder
=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
211 switch (inet_byteorder
)
223 ulong
b2i(int n
, uchar
*b
)
228 return ((b
[0]<<8) | b
[1]);
230 return ((b
[0]<<16) | (b
[1]<<8) | b
[2]);
232 return ((b
[0]<<24) | (b
[1]<<16) | (b
[2]<<8) | b
[3]);
236 ullong
b2ll(int n
, uchar
*b
)
240 for(i
=0; i
<n
; k
+=b
[i
++])
245 uchar
*i2b(int n
, ulong i
)
268 ulong
a2i(char *asc
, int bytes
)
272 for (rc
=i
=0, n
=strlen(trim(asc
))-1; i
<(abs(bytes
)<<1); n
--, i
++)
276 if ((rcl
=gethexval(asc
[n
]))<0)
290 int boundary(int exp
, int n
)
292 return((((n
-1)>>exp
)+1)<<exp
);
295 void cs_ftime(struct timeb
*tp
)
299 gettimeofday(&tv
, (struct timezone
*)0);
301 tp
->millitm
=tv
.tv_usec
/1000;
307 void cs_sleepms(int msec
)
310 tv
.tv_sec
= msec
/ 1000;
311 tv
.tv_usec
= (msec
% 1000) * 1000;
312 select(0, 0, 0, 0, &tv
);
315 int bytes_available(int fd
)
321 if (poll(&pfds
, 1, 0)!=1)
324 return(((pfds
.revents
)&POLLIN
)==POLLIN
);
330 void cs_setpriority(int prio
)
336 case 0: wprio
=REALTIME_PRIORITY_CLASS
; break;
337 case 1: wprio
=HIGH_PRIORITY_CLASS
; break;
338 case 2: wprio
=NORMAL_PRIORITY_CLASS
; break;
339 default: wprio
=IDLE_PRIORITY_CLASS
; break;
341 WinId
=GetCurrentProcess();
342 SetPriorityClass(WinId
, wprio
);
345 void cs_setpriority(int prio
)
348 setpriority(PRIO_PROCESS
, 0, prio
); // ignore errors