4 * Utility functions that are used by both the client and server.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
38 typedef unsigned char byte; /* Byte type */
39 static byte dtable[256]; /* base64 encode / decode table */
41 /* Month strings for date conversions */
42 char *ascmonths[12] = {
43 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
44 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
47 char *safestrncpy(char *dest, const char *src, size_t n)
49 if (dest == NULL || src == NULL) {
50 fprintf(stderr, "safestrncpy: NULL argument\n");
53 strncpy(dest, src, n);
60 #ifndef HAVE_STRNCASECMP
61 int strncasecmp(char *lstr, char *rstr, int len)
66 lc=tolower(lstr[pos]);
67 rc=tolower(rstr[pos]);
68 if ((lc==0)&&(rc==0)) return(0);
69 if (lc<rc) return(-1);
80 * num_tokens() - discover number of parameters/tokens in a string
82 int num_tokens(const char *source, char tok) {
86 if (source == NULL) return(0);
87 for (a=0; a<strlen(source); ++a) {
88 if (source[a]==tok) ++count;
95 * extract_token() - a string tokenizer
97 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
100 const char *s; /* source */
106 /* Locate desired parameter */
108 while (count < parmnum) {
109 /* End of string, bail! */
114 if (*s == separator) {
119 if (!s) return; /* Parameter not found */
121 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
129 * remove_token() - a tokenizer that kills, maims, and destroys
131 void remove_token(char *source, int parmnum, char separator)
133 char *d, *s; /* dest, source */
136 /* Find desired parameter */
138 while (count < parmnum) {
139 /* End of string, bail! */
144 if (*d == separator) {
149 if (!d) return; /* Parameter not found */
151 /* Find next parameter */
153 while (*s && *s != separator) {
160 else if (d == source)
174 * extract_int() - extract an int parm w/o supplying a buffer
176 int extract_int(const char *source, int parmnum)
180 extract_token(buf, source, parmnum, '|', sizeof buf);
185 * extract_long() - extract an long parm w/o supplying a buffer
187 long extract_long(const char *source, int parmnum)
191 extract_token(buf, source, parmnum, '|', sizeof buf);
197 * extract_unsigned_long() - extract an unsigned long parm
199 unsigned long extract_unsigned_long(const char *source, int parmnum)
203 extract_token(buf, source, parmnum, '|', sizeof buf);
204 return strtoul(buf, NULL, 10);
209 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
210 * John Walker, found in full in the file "base64.c" included with this
211 * distribution. We are moving in the direction of eventually discarding
212 * the separate executables, and using the ones in our code exclusively.
215 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
217 int i, hiteof = FALSE;
221 /* Fill dtable with character encodings. */
223 for (i = 0; i < 26; i++) {
225 dtable[26 + i] = 'a' + i;
227 for (i = 0; i < 10; i++) {
228 dtable[52 + i] = '0' + i;
234 byte igroup[3], ogroup[4];
237 igroup[0] = igroup[1] = igroup[2] = 0;
238 for (n = 0; n < 3; n++) {
239 if (spos >= sourcelen) {
244 igroup[n] = (byte) c;
247 ogroup[0] = dtable[igroup[0] >> 2];
248 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
249 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
250 ogroup[3] = dtable[igroup[2] & 0x3F];
252 /* Replace characters in output stream with "=" pad
253 characters if fewer than three characters were
254 read from the end of the input stream. */
262 for (i = 0; i < 4; i++) {
263 dest[dpos++] = ogroup[i];
272 * Convert base64-encoded to binary. Returns the length of the decoded data.
273 * It will stop after reading 'length' bytes.
275 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
281 for (i = 0; i < 255; i++) {
284 for (i = 'A'; i <= 'Z'; i++) {
285 dtable[i] = 0 + (i - 'A');
287 for (i = 'a'; i <= 'z'; i++) {
288 dtable[i] = 26 + (i - 'a');
290 for (i = '0'; i <= '9'; i++) {
291 dtable[i] = 52 + (i - '0');
297 /*CONSTANTCONDITION*/
299 byte a[4], b[4], o[3];
301 for (i = 0; i < 4; i++) {
302 if (spos >= length) {
313 if (dtable[c] & 0x80) {
314 /* Ignoring errors: discard invalid character. */
319 b[i] = (byte) dtable[c];
321 o[0] = (b[0] << 2) | (b[1] >> 4);
322 o[1] = (b[1] << 4) | (b[2] >> 2);
323 o[2] = (b[2] << 6) | b[3];
324 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
325 if (i>=1) dest[dpos++] = o[0];
326 if (i>=2) dest[dpos++] = o[1];
327 if (i>=3) dest[dpos++] = o[2];
338 * Strip leading and trailing spaces from a string
340 void striplt(char *buf)
342 if (strlen(buf) == 0) return;
343 while ((strlen(buf) > 0) && (isspace(buf[0])))
344 strcpy(buf, &buf[1]);
345 if (strlen(buf) == 0) return;
346 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
347 buf[strlen(buf) - 1] = 0;
355 * Return the number of occurances of character ch in string st
357 int haschar(const char *st, int ch)
361 for (a = 0; a < strlen(st); ++a)
372 * Format a date/time stamp for output
373 * seconds is whether to print the seconds
375 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
380 localtime_r(&thetime, &tm);
383 if (hour == 0) hour = 12;
384 else if (hour > 12) hour = hour - 12;
387 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
388 ascmonths[tm.tm_mon],
394 ( (tm.tm_hour >= 12) ? "pm" : "am" )
397 snprintf(buf, n, "%s %d %4d %d:%02d%s",
398 ascmonths[tm.tm_mon],
403 ( (tm.tm_hour >= 12) ? "pm" : "am" )
411 * Determine whether the specified message number is contained within the
412 * specified sequence set.
414 int is_msg_in_sequence_set(char *mset, long msgnum) {
417 char setstr[128], lostr[128], histr[128];
420 num_sets = num_tokens(mset, ',');
421 for (s=0; s<num_sets; ++s) {
422 extract_token(setstr, mset, s, ',', sizeof setstr);
424 extract_token(lostr, setstr, 0, ':', sizeof lostr);
425 if (num_tokens(setstr, ':') >= 2) {
426 extract_token(histr, setstr, 1, ':', sizeof histr);
427 if (!strcmp(histr, "*")) {
428 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
432 strcpy(histr, lostr);
437 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
445 * Utility function to "readline" from memory
446 * (returns new pointer)
448 char *memreadline(char *start, char *buf, int maxlen)
452 int len = 0; /* tally our own length to avoid strlen() delays */
455 memset(buf, 0, maxlen);
459 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
460 buf[strlen(buf) + 1] = 0;
461 buf[strlen(buf)] = ch;
464 if ((ch == 10) || (ch == 0)) {
472 * Strip a boundarized substring out of a string (for example, remove
473 * parentheses and anything inside them).
475 void stripout(char *str, char leftboundary, char rightboundary) {
480 for (a = 0; a < strlen(str); ++a) {
481 if (str[a] == leftboundary) lb = a;
482 if (str[a] == rightboundary) rb = a;
485 if ( (lb > 0) && (rb > lb) ) {
486 strcpy(&str[lb - 1], &str[rb + 1]);
489 else if ( (lb == 0) && (rb > lb) ) {
490 strcpy(str, &str[rb + 1]);
497 * Reduce a string down to a boundarized substring (for example, remove
498 * parentheses and anything outside them).
500 void stripallbut(char *str, char leftboundary, char rightboundary) {
503 for (a = 0; a < strlen(str); ++ a) {
504 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
507 for (a = 0; a < strlen(str); ++ a) {
508 if (str[a] == rightboundary) str[a] = 0;
513 char *myfgets(char *s, int size, FILE *stream) {
514 char *ret = fgets(s, size, stream);
518 nl = strchr(s, '\n');
528 * Escape a string for feeding out as a URL.
529 * Output buffer must be big enough to handle escape expansion!
531 void urlesc(char *outbuf, char *strbuf)
534 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
538 for (a = 0; a < (int)strlen(strbuf); ++a) {
540 for (b = 0; b < strlen(ec); ++b) {
541 if (strbuf[a] == ec[b])
546 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
548 sprintf(&outbuf[b], "%c", strbuf[a]);
554 * bmstrstr() is a variant of strstr() that uses the Boyer-Moore search
555 * algorithm, and can use any caller-supplied string compare function whose
556 * calling syntax is similar to strncmp(). For example, we can supply it
557 * with strncasecmp() to do a case-insensitive search.
559 * Original code: copyright (c) 1997-1998 by Urs Janssen <urs@tin.org>
560 * Modifications: copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
562 char *bmstrstr(char *text, char *pattern,
563 int (*cmpfunc)(const char *, const char *, size_t) )
565 register unsigned char *p, *t;
566 register int i, j, *delta;
572 if (text == NULL) return(NULL);
573 if (pattern == NULL) return(NULL);
575 textlen = strlen(text);
576 patlen = strlen(pattern);
578 /* algorithm fails if pattern is empty */
579 if ((p1 = patlen) == 0)
582 /* code below fails (whenever i is unsigned) if pattern too long */
588 for (i = 0; i <= 255; i++)
590 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
594 * From now on, we want patlen - 1.
595 * In the loop below, p points to the end of the pattern,
596 * t points to the end of the text to be tested against the
597 * pattern, and i counts the amount of text remaining, not
598 * including the part to be tested.
601 p = (unsigned char *) pattern + p1;
602 t = (unsigned char *) text + p1;
603 i = textlen - patlen;
605 if (tolower(*p) == tolower(*t)
606 && cmpfunc((p - p1), (t - p1), p1) == 0)
607 return ((char *) t - p1);
619 * In our world, we want strcpy() to be able to work with overlapping strings.
621 char *strcpy(char *dest, const char *src) {
622 memmove(dest, src, (strlen(src) + 1) );
628 * Generate a new, globally unique UID parameter for a calendar etc. object
630 void generate_uuid(char *buf) {
633 sprintf(buf, "{%08x-%04x-%04x-%04x-%012x}",