4 * Utility functions that are used by both the client and server.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
36 typedef unsigned char byte; /* Byte type */
37 static byte dtable[256]; /* base64 encode / decode table */
40 char *safestrncpy(char *dest, const char *src, size_t n)
42 if (dest == NULL || src == NULL) {
43 fprintf(stderr, "safestrncpy: NULL argument\n");
46 strncpy(dest, src, n);
53 #ifndef HAVE_STRNCASECMP
54 int strncasecmp(char *lstr, char *rstr, int len)
59 lc=tolower(lstr[pos]);
60 rc=tolower(rstr[pos]);
61 if ((lc==0)&&(rc==0)) return(0);
62 if (lc<rc) return(-1);
73 * num_tokens() - discover number of parameters/tokens in a string
75 int num_tokens(char *source, char tok) {
79 if (source == NULL) return(0);
80 for (a=0; a<strlen(source); ++a) {
81 if (source[a]==tok) ++count;
87 /* extract_token_fast() - a smarter string tokenizer */
88 void extract_token(char *dest, char *source, int parmnum, char separator)
90 char *d, *s; /* dest, source */
95 /* Locate desired parameter */
97 while (count < parmnum) {
98 /* End of string, bail! */
103 if (*s == separator) {
108 if (!s) return; /* Parameter not found */
110 for (d = dest; *s && *s != separator; s++, d++) {
118 * extract_token() - a smarter string tokenizer
120 void extract_token_old(char *dest, char *source, int parmnum, char separator)
130 if (strlen(source)==0) {
134 for (i=0; i<strlen(source); ++i) {
135 if (source[i]==separator) {
138 else if (curr_parm == parmnum) {
140 dest[len++] = source[i];
147 /* remove_token_fast() - a tokenizer that kills, maims, and destroys fast */
148 void remove_token(char *source, int parmnum, char separator)
150 char *d, *s; /* dest, source */
153 /* Find desired parameter */
155 while (count < parmnum) {
156 /* End of string, bail! */
161 if (*d == separator) {
166 if (!d) return; /* Parameter not found */
168 /* Find next parameter */
170 while (*s && *s != separator) {
177 else if (d == source)
191 * remove_token() - a tokenizer that kills, maims, and destroys
193 void remove_token_old(char *source, int parmnum, char separator)
205 if (strlen(source)==0) {
209 for (i=0; i<strlen(source); ++i) {
210 if ( (start < 0) && (curr_parm == parmnum) ) {
214 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
218 if (source[i]==separator) {
223 if (end < 0) end = strlen(source);
224 strcpy(&source[start], &source[end]);
231 * extract_int() - extract an int parm w/o supplying a buffer
233 int extract_int(char *source, int parmnum)
237 extract_token(buf, source, parmnum, '|');
242 * extract_long() - extract an long parm w/o supplying a buffer
244 long extract_long(char *source, long int parmnum)
248 extract_token(buf, source, parmnum, '|');
255 * CtdlDecodeBase64() and encode_base64() are adaptations of code by
256 * John Walker, found in full in the file "base64.c" included with this
257 * distribution. The difference between those functions and these is that
258 * these are intended to encode/decode small string buffers, and those are
259 * intended to encode/decode entire MIME parts.
262 void encode_base64(char *dest, char *source)
264 int i, hiteof = FALSE;
268 /* Fill dtable with character encodings. */
270 for (i = 0; i < 26; i++) {
272 dtable[26 + i] = 'a' + i;
274 for (i = 0; i < 10; i++) {
275 dtable[52 + i] = '0' + i;
281 byte igroup[3], ogroup[4];
284 igroup[0] = igroup[1] = igroup[2] = 0;
285 for (n = 0; n < 3; n++) {
291 igroup[n] = (byte) c;
294 ogroup[0] = dtable[igroup[0] >> 2];
295 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
296 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
297 ogroup[3] = dtable[igroup[2] & 0x3F];
299 /* Replace characters in output stream with "=" pad
300 characters if fewer than three characters were
301 read from the end of the input stream. */
309 for (i = 0; i < 4; i++) {
310 dest[dpos++] = ogroup[i];
319 * Convert base64-encoded to binary. Returns the length of the decoded data.
320 * It will stop after reading 'length' bytes.
322 int CtdlDecodeBase64(char *dest, char *source, size_t length)
328 for (i = 0; i < 255; i++) {
331 for (i = 'A'; i <= 'Z'; i++) {
332 dtable[i] = 0 + (i - 'A');
334 for (i = 'a'; i <= 'z'; i++) {
335 dtable[i] = 26 + (i - 'a');
337 for (i = '0'; i <= '9'; i++) {
338 dtable[i] = 52 + (i - '0');
344 /*CONSTANTCONDITION*/
346 byte a[4], b[4], o[3];
348 for (i = 0; i < 4; i++) {
349 if (spos >= length) {
360 if (dtable[c] & 0x80) {
361 /* Ignoring errors: discard invalid character. */
366 b[i] = (byte) dtable[c];
368 o[0] = (b[0] << 2) | (b[1] >> 4);
369 o[1] = (b[1] << 4) | (b[2] >> 2);
370 o[2] = (b[2] << 6) | b[3];
371 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
372 if (i>=1) dest[dpos++] = o[0];
373 if (i>=2) dest[dpos++] = o[1];
374 if (i>=3) dest[dpos++] = o[2];
385 * Strip leading and trailing spaces from a string
387 void striplt(char *buf)
389 while ((strlen(buf) > 0) && (isspace(buf[0])))
390 strcpy(buf, &buf[1]);
391 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
392 buf[strlen(buf) - 1] = 0;
400 * Return the number of occurances of character ch in string st
402 int haschar(const char *st, int ch)
406 for (a = 0; a < strlen(st); ++a)
416 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
418 int collapsed_strcmp(char *s1, char *s2) {
422 c1 = malloc(strlen(s1)+1);
423 c2 = malloc(strlen(s2)+1);
428 for (i=0; i<strlen(s1); ++i) {
429 if (isalnum(s1[i])) {
430 c1[pos] = tolower(s1[i]);
436 for (i=0; i<strlen(s2); ++i) {
437 if (isalnum(s2[i])) {
438 c2[pos] = tolower(s2[i]);
443 ret = strcmp(c1, c2);
452 * Format a date/time stamp for output
453 * seconds is whether to print the seconds
455 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
459 char *ascmonths[] = {
460 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
461 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
465 tm = localtime(&thetime);
468 if (hour == 0) hour = 12;
469 else if (hour > 12) hour = hour - 12;
472 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
473 ascmonths[tm->tm_mon],
479 ( (tm->tm_hour >= 12) ? "pm" : "am" )
482 snprintf(buf, n, "%s %d %4d %d:%02d%s",
483 ascmonths[tm->tm_mon],
488 ( (tm->tm_hour >= 12) ? "pm" : "am" )
496 * Determine whether the specified message number is contained within the
499 int is_msg_in_mset(char *mset, long msgnum) {
502 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
506 * Now set it for all specified messages.
508 num_sets = num_tokens(mset, ',');
509 for (s=0; s<num_sets; ++s) {
510 extract_token(setstr, mset, s, ',');
512 extract_token(lostr, setstr, 0, ':');
513 if (num_tokens(setstr, ':') >= 2) {
514 extract_token(histr, setstr, 1, ':');
515 if (!strcmp(histr, "*")) {
516 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
520 strcpy(histr, lostr);
525 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
533 * Utility function to "readline" from memory
534 * (returns new pointer)
536 char *memreadline(char *start, char *buf, int maxlen)
540 int len = 0; /* tally our own length to avoid strlen() delays */
543 memset(buf, 0, maxlen);
547 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
548 buf[strlen(buf) + 1] = 0;
549 buf[strlen(buf)] = ch;
552 if ((ch == 10) || (ch == 0)) {
560 * Strip a boundarized substring out of a string (for example, remove
561 * parentheses and anything inside them).
563 void stripout(char *str, char leftboundary, char rightboundary) {
568 for (a = 0; a < strlen(str); ++a) {
569 if (str[a] == leftboundary) lb = a;
570 if (str[a] == rightboundary) rb = a;
573 if ( (lb > 0) && (rb > lb) ) {
574 strcpy(&str[lb - 1], &str[rb + 1]);
581 * Reduce a string down to a boundarized substring (for example, remove
582 * parentheses and anything outside them).
584 void stripallbut(char *str, char leftboundary, char rightboundary) {
587 for (a = 0; a < strlen(str); ++ a) {
588 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
591 for (a = 0; a < strlen(str); ++ a) {
592 if (str[a] == rightboundary) str[a] = 0;
597 char *myfgets(char *s, int size, FILE *stream) {
598 char *ret = fgets(s, size, stream);
602 nl = strchr(s, '\n');
612 * Escape a string for feeding out as a URL.
613 * Output buffer must be big enough to handle escape expansion!
615 void urlesc(char *outbuf, char *strbuf)
618 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
622 for (a = 0; a < strlen(strbuf); ++a) {
624 for (b = 0; b < strlen(ec); ++b) {
625 if (strbuf[a] == ec[b])
630 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
632 sprintf(&outbuf[b], "%c", strbuf[a]);