2 * tools.c -- Miscellaneous routines used by both the client and server.
16 typedef unsigned char byte; /* Byte type */
17 static byte dtable[256]; /* base64 encode / decode table */
20 char *safestrncpy(char *dest, const char *src, size_t n)
22 if (dest == NULL || src == NULL)
24 fprintf(stderr, "safestrncpy: NULL argument\n");
27 strncpy(dest, src, n);
34 * num_tokens() - discover number of parameters/tokens in a string
36 int num_tokens(char *source, char tok) {
40 for (a=0; a<strlen(source); ++a) {
41 if (source[a]==tok) ++count;
47 * extract_token() - a smarter string tokenizer
49 void extract_token(char *dest, char *source, int parmnum, char separator)
59 if (strlen(source)==0) {
63 for (i=0; i<strlen(source); ++i) {
64 if (source[i]==separator) {
67 else if (curr_parm == parmnum) {
69 dest[len++] = source[i];
77 * remove_token() - a tokenizer that kills, maims, and destroys
79 void remove_token(char *source, int parmnum, char separator)
91 if (strlen(source)==0) {
95 for (i=0; i<strlen(source); ++i) {
96 if ( (start < 0) && (curr_parm == parmnum) ) {
100 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
104 if (source[i]==separator) {
109 if (end < 0) end = strlen(source);
111 printf("%d .. %d\n", start, end);
113 strcpy(&source[start], &source[end]);
120 * extract_int() - extract an int parm w/o supplying a buffer
122 int extract_int(char *source, int parmnum)
126 extract_token(buf, source, parmnum, '|');
131 * extract_long() - extract an long parm w/o supplying a buffer
133 long extract_long(char *source, long int parmnum)
137 extract_token(buf, source, parmnum, '|');
144 * decode_base64() and encode_base64() are adaptations of code by
145 * John Walker, found in full in the file "base64.c" included with this
146 * distribution. The difference between those functions and these is that
147 * these are intended to encode/decode small string buffers, and those are
148 * intended to encode/decode entire MIME parts.
151 void encode_base64(char *dest, char *source)
153 int i, hiteof = FALSE;
157 /* Fill dtable with character encodings. */
159 for (i = 0; i < 26; i++) {
161 dtable[26 + i] = 'a' + i;
163 for (i = 0; i < 10; i++) {
164 dtable[52 + i] = '0' + i;
170 byte igroup[3], ogroup[4];
173 igroup[0] = igroup[1] = igroup[2] = 0;
174 for (n = 0; n < 3; n++) {
180 igroup[n] = (byte) c;
183 ogroup[0] = dtable[igroup[0] >> 2];
184 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
185 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
186 ogroup[3] = dtable[igroup[2] & 0x3F];
188 /* Replace characters in output stream with "=" pad
189 characters if fewer than three characters were
190 read from the end of the input stream. */
198 for (i = 0; i < 4; i++) {
199 dest[dpos++] = ogroup[i];
208 void decode_base64(char *dest, char *source)
214 for (i = 0; i < 255; i++) {
217 for (i = 'A'; i <= 'Z'; i++) {
218 dtable[i] = 0 + (i - 'A');
220 for (i = 'a'; i <= 'z'; i++) {
221 dtable[i] = 26 + (i - 'a');
223 for (i = '0'; i <= '9'; i++) {
224 dtable[i] = 52 + (i - '0');
230 /*CONSTANTCONDITION*/
232 byte a[4], b[4], o[3];
234 for (i = 0; i < 4; i++) {
235 int c = source[spos++];
243 if (dtable[c] & 0x80) {
244 /* Ignoring errors: discard invalid character. */
249 b[i] = (byte) dtable[c];
251 o[0] = (b[0] << 2) | (b[1] >> 4);
252 o[1] = (b[1] << 4) | (b[2] >> 2);
253 o[2] = (b[2] << 6) | b[3];
254 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
255 if (i>=1) dest[dpos++] = o[0];
256 if (i>=2) dest[dpos++] = o[1];
257 if (i>=3) dest[dpos++] = o[2];
268 * Strip leading and trailing spaces from a string
270 void striplt(char *buf)
272 while ((strlen(buf) > 0) && (buf[0] == 32))
273 strcpy(buf, &buf[1]);
274 while (buf[strlen(buf) - 1] == 32)
275 buf[strlen(buf) - 1] = 0;
283 * Return the number of occurances of character ch in string st
285 int haschar(char *st, int ch)
289 for (a = 0; a < strlen(st); ++a)
299 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
301 int collapsed_strcmp(char *s1, char *s2) {
305 c1 = malloc(strlen(s1)+1);
306 c2 = malloc(strlen(s2)+1);
311 for (i=0; i<strlen(s1); ++i) {
312 if (isalnum(s1[i])) {
313 c1[pos] = tolower(s1[i]);
319 for (i=0; i<strlen(s2); ++i) {
320 if (isalnum(s2[i])) {
321 c2[pos] = tolower(s2[i]);
326 ret = strcmp(c1, c2);
335 * Format a date/time stamp for output
337 void fmt_date(char *buf, time_t thetime) {
341 char *ascmonths[] = {
342 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
343 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
347 tm = localtime(&thetime);
350 if (hour == 0) hour = 12;
351 else if (hour > 12) hour = hour - 12;
353 sprintf(buf, "%s %d %4d %d:%02d%s",
354 ascmonths[tm->tm_mon],
359 ( (tm->tm_hour >= 12) ? "pm" : "am" )