/[cvs]/eggdrop1.9/testcode/base64.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/base64.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Apr 25 04:19:54 2002 UTC (16 years, 10 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-chdr
* sclient.c - test program to telnet using a proxy
* http.c/base64.c - http anonymous and authenticated proxies
* socks5.c - socks5 proxy with support for username/password authentication, ipv4, ipv6, and remote hostname resolution
* socks4.c socks4 proxy (no authentication)

1 #include <stdlib.h>
2 #include "base64.h"
3
4 static char *b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5
6 char *b64enc(const unsigned char *data, int len)
7 {
8 char *dest;
9
10 dest = (char *)malloc(4 * len / 3 + 4);
11 b64enc_buf(data, len, dest);
12 return(dest);
13 }
14
15 int b64enc_buf(const unsigned char *data, int len, char *dest)
16 {
17 char *buf = dest;
18
19 /* Encode 3 bytes at a time. */
20 while (len >= 3) {
21 buf[0] = b64chars[(data[0] >> 2) & 0x3f];
22 buf[1] = b64chars[((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf)];
23 buf[2] = b64chars[((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3)];
24 buf[3] = b64chars[data[2] & 0x3f];
25 data += 3;
26 buf += 4;
27 len -= 3;
28 }
29
30 if (len > 0) {
31 buf[0] = b64chars[(data[0] >> 2) & 0x3f];
32 buf[1] = b64chars[(data[0] << 4) & 0x30];
33 if (len > 1) {
34 buf[1] += (data[1] >> 4) & 0xf;
35 buf[2] = b64chars[(data[1] << 2) & 0x3c];
36 }
37 else buf[2] = '=';
38 buf[3] = '=';
39 buf += 4;
40 }
41
42 *buf = '\0';
43 return(buf - dest);
44 }
45
46 char *b64dec(const unsigned char *data, int len)
47 {
48 char *dest;
49
50 dest = (char *)malloc(len+1);
51 b64dec_buf(data, len, dest);
52 return(dest);
53 }
54
55 int b64dec_buf(const unsigned char *data, int len, char *dest)
56 {
57 unsigned char c;
58 int cur = 0, val = 0, i = 0;
59
60 while (len) {
61 c = *data;
62 len--;
63 data++;
64
65 if (c >= 'A' && c <= 'Z') val = c - 'A';
66 else if (c >= 'a' && c <= 'z') val = c - 'a' + 26;
67 else if (c >= '0' && c <= '9') val = c - '0' + 52;
68 else if (c == '+') val = 62;
69 else if (c == '/') val = 63;
70 else if (c == '=') break;
71 else continue;
72
73 switch (cur++) {
74 case 0:
75 dest[i] = (val << 2) & 0xfc;
76 break;
77 case 1:
78 dest[i] |= (val >> 4) & 0x03;
79 i++;
80 dest[i] = (val << 4) & 0xf0;
81 break;
82 case 2:
83 dest[i] |= (val >> 2) & 0x0f;
84 i++;
85 dest[i] = (val << 6) & 0xc0;
86 break;
87 case 3:
88 dest[i] |= val & 0x3f;
89 i++;
90 cur = 0;
91 break;
92 }
93 }
94 dest[i] = 0;
95 return(i);
96 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23