]> code.citadel.org Git - citadel.git/blob - citadel/tools.c
* 5.70b1
[citadel.git] / citadel / tools.c
1 /*
2  * tools.c -- Miscellaneous routines used by both the client and server.
3  * $Id$
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "tools.h"
11
12 #define TRUE  1
13 #define FALSE 0
14
15 typedef unsigned char byte;           /* Byte type */
16 static byte dtable[256];              /* base64 encode / decode table */
17
18
19 char *safestrncpy(char *dest, const char *src, size_t n)
20 {
21   if (dest == NULL || src == NULL)
22     {
23       fprintf(stderr, "safestrncpy: NULL argument\n");
24       abort();
25     }
26   strncpy(dest, src, n);
27   dest[n - 1] = 0;
28   return dest;
29 }
30
31
32 /*
33  * num_tokens()  -  discover number of parameters/tokens in a string
34  */
35 int num_tokens(char *source, char tok) {
36         int a;
37         int count = 1;
38
39         for (a=0; a<strlen(source); ++a) {
40                 if (source[a]==tok) ++count;
41         }
42         return(count);
43 }
44
45 /*
46  * extract_token()  -  a smarter string tokenizer
47  */
48 void extract_token(char *dest, char *source, int parmnum, char separator) 
49 {
50         int i;
51         int len;
52         int curr_parm;
53
54         strcpy(dest,"");
55         len = 0;
56         curr_parm = 0;
57
58         if (strlen(source)==0) {
59                 return;
60                 }
61
62         for (i=0; i<strlen(source); ++i) {
63                 if (source[i]==separator) {
64                         ++curr_parm;
65                 }
66                 else if (curr_parm == parmnum) {
67                         dest[len+1] = 0;
68                         dest[len++] = source[i];
69                 }
70         }
71 }
72
73
74
75 /*
76  * remove_token()  -  a tokenizer that kills, maims, and destroys
77  */
78 void remove_token(char *source, int parmnum, char separator)
79 {
80         int i;
81         int len;
82         int curr_parm;
83         int start, end;
84
85         len = 0;
86         curr_parm = 0;
87         start = (-1);
88         end = (-1);
89
90         if (strlen(source)==0) {
91                 return;
92                 }
93
94         for (i=0; i<strlen(source); ++i) {
95                 if ( (start < 0) && (curr_parm == parmnum) ) {
96                         start = i;
97                 }
98
99                 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
100                         end = i;
101                 }
102
103                 if (source[i]==separator) {
104                         ++curr_parm;
105                 }
106         }
107
108         if (end < 0) end = strlen(source);
109
110         printf("%d .. %d\n", start, end);
111
112         strcpy(&source[start], &source[end]);
113 }
114
115
116
117
118 /*
119  * extract_int()  -  extract an int parm w/o supplying a buffer
120  */
121 int extract_int(char *source, int parmnum)
122 {
123         char buf[256];
124         
125         extract_token(buf, source, parmnum, '|');
126         return(atoi(buf));
127 }
128
129 /*
130  * extract_long()  -  extract an long parm w/o supplying a buffer
131  */
132 long extract_long(char *source, long int parmnum)
133 {
134         char buf[256];
135         
136         extract_token(buf, source, parmnum, '|');
137         return(atol(buf));
138 }
139
140
141
142 /*
143  * decode_base64() and encode_base64() are adaptations of code by
144  * John Walker, found in full in the file "base64.c" included with this
145  * distribution.  The difference between those functions and these is that
146  * these are intended to encode/decode small string buffers, and those are
147  * intended to encode/decode entire MIME parts.
148  */
149
150 void encode_base64(char *dest, char *source)
151 {
152     int i, hiteof = FALSE;
153     int spos = 0;
154     int dpos = 0;
155
156     /*  Fill dtable with character encodings.  */
157
158     for (i = 0; i < 26; i++) {
159         dtable[i] = 'A' + i;
160         dtable[26 + i] = 'a' + i;
161     }
162     for (i = 0; i < 10; i++) {
163         dtable[52 + i] = '0' + i;
164     }
165     dtable[62] = '+';
166     dtable[63] = '/';
167
168     while (!hiteof) {
169         byte igroup[3], ogroup[4];
170         int c, n;
171
172         igroup[0] = igroup[1] = igroup[2] = 0;
173         for (n = 0; n < 3; n++) {
174             c = source[spos++];
175             if (c == 0) {
176                 hiteof = TRUE;
177                 break;
178             }
179             igroup[n] = (byte) c;
180         }
181         if (n > 0) {
182             ogroup[0] = dtable[igroup[0] >> 2];
183             ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
184             ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
185             ogroup[3] = dtable[igroup[2] & 0x3F];
186
187             /* Replace characters in output stream with "=" pad
188                characters if fewer than three characters were
189                read from the end of the input stream. */
190
191             if (n < 3) {
192                 ogroup[3] = '=';
193                 if (n < 2) {
194                     ogroup[2] = '=';
195                 }
196             }
197             for (i = 0; i < 4; i++) {
198                 dest[dpos++] = ogroup[i];
199                 dest[dpos] = 0;
200             }
201         }
202     }
203 }
204
205
206
207 void decode_base64(char *dest, char *source)
208 {
209     int i;
210     int dpos = 0;
211     int spos = 0;
212
213     for (i = 0; i < 255; i++) {
214         dtable[i] = 0x80;
215     }
216     for (i = 'A'; i <= 'Z'; i++) {
217         dtable[i] = 0 + (i - 'A');
218     }
219     for (i = 'a'; i <= 'z'; i++) {
220         dtable[i] = 26 + (i - 'a');
221     }
222     for (i = '0'; i <= '9'; i++) {
223         dtable[i] = 52 + (i - '0');
224     }
225     dtable['+'] = 62;
226     dtable['/'] = 63;
227     dtable['='] = 0;
228
229     /*CONSTANTCONDITION*/
230     while (TRUE) {
231         byte a[4], b[4], o[3];
232
233         for (i = 0; i < 4; i++) {
234             int c = source[spos++];
235
236             if (c == 0) {
237                 if (i > 0) {
238                     return;
239                 }
240                 return;
241             }
242             if (dtable[c] & 0x80) {
243                 /* Ignoring errors: discard invalid character. */
244                 i--;
245                 continue;
246             }
247             a[i] = (byte) c;
248             b[i] = (byte) dtable[c];
249         }
250         o[0] = (b[0] << 2) | (b[1] >> 4);
251         o[1] = (b[1] << 4) | (b[2] >> 2);
252         o[2] = (b[2] << 6) | b[3];
253         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
254         if (i>=1) dest[dpos++] = o[0];
255         if (i>=2) dest[dpos++] = o[1];
256         if (i>=3) dest[dpos++] = o[2];
257         dest[dpos] = 0;
258         if (i < 3) {
259             return;
260         }
261     }
262 }
263
264
265
266 /*
267  * Strip leading and trailing spaces from a string
268  */
269 void striplt(char *buf)
270 {
271         while ((strlen(buf) > 0) && (buf[0] == 32))
272                 strcpy(buf, &buf[1]);
273         while (buf[strlen(buf) - 1] == 32)
274                 buf[strlen(buf) - 1] = 0;
275 }
276
277
278
279
280
281 /* 
282  * Return the number of occurances of character ch in string st
283  */ 
284 int haschar(char *st, int ch)
285 {
286         int a, b;
287         b = 0;
288         for (a = 0; a < strlen(st); ++a)
289                 if (st[a] == ch)
290                         ++b;
291         return (b);
292 }
293
294
295
296
297 /*
298  * Compare two strings, insensitive to case, punctuation, and non-alnum chars
299  */
300 int collapsed_strcmp(char *s1, char *s2) {
301         char *c1, *c2;
302         int i, ret, pos;
303
304         c1 = malloc(strlen(s1)+1);
305         c2 = malloc(strlen(s2)+1);
306         c1[0] = 0;
307         c2[0] = 0;
308
309         pos = 0;
310         for (i=0; i<strlen(s1); ++i) {
311                 if (isalnum(s1[i])) {
312                         c1[pos] = tolower(s1[i]);
313                         c1[++pos] = 0;
314                 }
315         }
316
317         pos = 0;
318         for (i=0; i<strlen(s2); ++i) {
319                 if (isalnum(s2[i])) {
320                         c2[pos] = tolower(s2[i]);
321                         c2[++pos] = 0;
322                 }
323         }
324
325         ret = strcmp(c1, c2);
326         free(c1);
327         free(c2);
328         return(ret);
329 }