10 #include <sys/socket.h>
13 #include <netinet/in.h>
26 typedef unsigned char byte; /* Byte type */
27 static byte dtable[SIZ]; /* base64 encode / decode table */
30 * decode_base64() and encode_base64() are adaptations of code by
31 * John Walker, found in full in the file "base64.c" included with the Citadel
32 * server. The difference between those functions and these is that
33 * these are intended to encode/decode small string buffers, and those are
34 * intended to encode/decode entire MIME parts.
37 void encode_base64(char *dest, char *source)
39 int i, hiteof = FALSE;
43 /* Fill dtable with character encodings. */
45 for (i = 0; i < 26; i++) {
47 dtable[26 + i] = 'a' + i;
49 for (i = 0; i < 10; i++) {
50 dtable[52 + i] = '0' + i;
56 byte igroup[3], ogroup[4];
59 igroup[0] = igroup[1] = igroup[2] = 0;
60 for (n = 0; n < 3; n++) {
69 ogroup[0] = dtable[igroup[0] >> 2];
70 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
71 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
72 ogroup[3] = dtable[igroup[2] & 0x3F];
74 /* Replace characters in output stream with "=" pad
75 characters if fewer than three characters were
76 read from the end of the input stream. */
84 for (i = 0; i < 4; i++) {
85 dest[dpos++] = ogroup[i];
94 void decode_base64(char *dest, char *source)
100 for (i = 0; i < 255; i++) {
103 for (i = 'A'; i <= 'Z'; i++) {
104 dtable[i] = 0 + (i - 'A');
106 for (i = 'a'; i <= 'z'; i++) {
107 dtable[i] = 26 + (i - 'a');
109 for (i = '0'; i <= '9'; i++) {
110 dtable[i] = 52 + (i - '0');
116 /*CONSTANTCONDITION*/
118 byte a[4], b[4], o[3];
120 for (i = 0; i < 4; i++) {
121 int c = source[spos++];
129 if (dtable[c] & 0x80) {
130 /* Ignoring errors: discard invalid character. */
135 b[i] = (byte) dtable[c];
137 o[0] = (b[0] << 2) | (b[1] >> 4);
138 o[1] = (b[1] << 4) | (b[2] >> 2);
139 o[2] = (b[2] << 6) | b[3];
140 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
141 if (i>=1) dest[dpos++] = o[0];
142 if (i>=2) dest[dpos++] = o[1];
143 if (i>=3) dest[dpos++] = o[2];
156 * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
158 void stuff_to_cookie(char *cookie, int session, char *user, char *pass, char *room)
162 sprintf(buf, "%d|%s|%s|%s", session, user, pass, room);
163 encode_base64(cookie, buf);
168 * Extract all that fun stuff out of the cookie.
170 void cookie_to_stuff(char *cookie, int *session, char *user, char *pass, char *room)
174 decode_base64(buf, cookie);
177 *session = extract_int(buf, 0);
179 extract(user, buf, 1);
181 extract(pass, buf, 2);
183 extract(room, buf, 3);