4 * Utility functions that are used by both the client and server.
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
34 typedef unsigned char byte; /* Byte type */
35 static byte dtable[256]; /* base64 encode / decode table */
37 /* Month strings for date conversions */
38 char *ascmonths[12] = {
39 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
40 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
43 char *safestrncpy(char *dest, const char *src, size_t n)
45 if (dest == NULL || src == NULL) {
46 fprintf(stderr, "safestrncpy: NULL argument\n");
49 strncpy(dest, src, n);
56 #ifndef HAVE_STRNCASECMP
57 int strncasecmp(char *lstr, char *rstr, int len)
62 lc=tolower(lstr[pos]);
63 rc=tolower(rstr[pos]);
64 if ((lc==0)&&(rc==0)) return(0);
65 if (lc<rc) return(-1);
76 * num_tokens() - discover number of parameters/tokens in a string
78 int num_tokens(const char *source, char tok) {
82 if (source == NULL) return(0);
83 for (a=0; a<strlen(source); ++a) {
84 if (source[a]==tok) ++count;
91 * extract_token() - a string tokenizer
93 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
96 const char *s; /* source */
102 /* Locate desired parameter */
104 while (count < parmnum) {
105 /* End of string, bail! */
110 if (*s == separator) {
115 if (!s) return; /* Parameter not found */
117 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
125 * remove_token() - a tokenizer that kills, maims, and destroys
127 void remove_token(char *source, int parmnum, char separator)
129 char *d, *s; /* dest, source */
132 /* Find desired parameter */
134 while (count < parmnum) {
135 /* End of string, bail! */
140 if (*d == separator) {
145 if (!d) return; /* Parameter not found */
147 /* Find next parameter */
149 while (*s && *s != separator) {
156 else if (d == source)
170 * extract_int() - extract an int parm w/o supplying a buffer
172 int extract_int(const char *source, int parmnum)
176 extract_token(buf, source, parmnum, '|', sizeof buf);
181 * extract_long() - extract an long parm w/o supplying a buffer
183 long extract_long(const char *source, int parmnum)
187 extract_token(buf, source, parmnum, '|', sizeof buf);
193 * extract_unsigned_long() - extract an unsigned long parm
195 unsigned long extract_unsigned_long(const char *source, int parmnum)
199 extract_token(buf, source, parmnum, '|', sizeof buf);
200 return strtoul(buf, NULL, 10);
205 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
206 * John Walker, found in full in the file "base64.c" included with this
207 * distribution. We are moving in the direction of eventually discarding
208 * the separate executables, and using the ones in our code exclusively.
211 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
213 int i, hiteof = FALSE;
217 /* Fill dtable with character encodings. */
219 for (i = 0; i < 26; i++) {
221 dtable[26 + i] = 'a' + i;
223 for (i = 0; i < 10; i++) {
224 dtable[52 + i] = '0' + i;
230 byte igroup[3], ogroup[4];
233 igroup[0] = igroup[1] = igroup[2] = 0;
234 for (n = 0; n < 3; n++) {
235 if (spos >= sourcelen) {
240 igroup[n] = (byte) c;
243 ogroup[0] = dtable[igroup[0] >> 2];
244 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
245 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
246 ogroup[3] = dtable[igroup[2] & 0x3F];
248 /* Replace characters in output stream with "=" pad
249 characters if fewer than three characters were
250 read from the end of the input stream. */
258 for (i = 0; i < 4; i++) {
259 dest[dpos++] = ogroup[i];
268 * Convert base64-encoded to binary. Returns the length of the decoded data.
269 * It will stop after reading 'length' bytes.
271 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
277 for (i = 0; i < 255; i++) {
280 for (i = 'A'; i <= 'Z'; i++) {
281 dtable[i] = 0 + (i - 'A');
283 for (i = 'a'; i <= 'z'; i++) {
284 dtable[i] = 26 + (i - 'a');
286 for (i = '0'; i <= '9'; i++) {
287 dtable[i] = 52 + (i - '0');
293 /*CONSTANTCONDITION*/
295 byte a[4], b[4], o[3];
297 for (i = 0; i < 4; i++) {
298 if (spos >= length) {
309 if (dtable[c] & 0x80) {
310 /* Ignoring errors: discard invalid character. */
315 b[i] = (byte) dtable[c];
317 o[0] = (b[0] << 2) | (b[1] >> 4);
318 o[1] = (b[1] << 4) | (b[2] >> 2);
319 o[2] = (b[2] << 6) | b[3];
320 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
321 if (i>=1) dest[dpos++] = o[0];
322 if (i>=2) dest[dpos++] = o[1];
323 if (i>=3) dest[dpos++] = o[2];
334 * Strip leading and trailing spaces from a string
336 void striplt(char *buf)
338 if (strlen(buf) == 0) return;
339 while ((strlen(buf) > 0) && (isspace(buf[0])))
340 strcpy(buf, &buf[1]);
341 if (strlen(buf) == 0) return;
342 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
343 buf[strlen(buf) - 1] = 0;
351 * Return the number of occurances of character ch in string st
353 int haschar(const char *st, int ch)
357 for (a = 0; a < strlen(st); ++a)
368 * Format a date/time stamp for output
369 * seconds is whether to print the seconds
371 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
376 localtime_r(&thetime, &tm);
379 if (hour == 0) hour = 12;
380 else if (hour > 12) hour = hour - 12;
383 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
384 ascmonths[tm.tm_mon],
390 ( (tm.tm_hour >= 12) ? "pm" : "am" )
393 snprintf(buf, n, "%s %d %4d %d:%02d%s",
394 ascmonths[tm.tm_mon],
399 ( (tm.tm_hour >= 12) ? "pm" : "am" )
407 * Determine whether the specified message number is contained within the
408 * specified sequence set.
410 int is_msg_in_sequence_set(char *mset, long msgnum) {
413 char setstr[128], lostr[128], histr[128];
416 num_sets = num_tokens(mset, ',');
417 for (s=0; s<num_sets; ++s) {
418 extract_token(setstr, mset, s, ',', sizeof setstr);
420 extract_token(lostr, setstr, 0, ':', sizeof lostr);
421 if (num_tokens(setstr, ':') >= 2) {
422 extract_token(histr, setstr, 1, ':', sizeof histr);
423 if (!strcmp(histr, "*")) {
424 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
428 strcpy(histr, lostr);
433 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
441 * Utility function to "readline" from memory
442 * (returns new pointer)
444 char *memreadline(char *start, char *buf, int maxlen)
448 int len = 0; /* tally our own length to avoid strlen() delays */
451 memset(buf, 0, maxlen);
455 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
456 buf[strlen(buf) + 1] = 0;
457 buf[strlen(buf)] = ch;
460 if ((ch == 10) || (ch == 0)) {
468 * Strip a boundarized substring out of a string (for example, remove
469 * parentheses and anything inside them).
471 void stripout(char *str, char leftboundary, char rightboundary) {
476 for (a = 0; a < strlen(str); ++a) {
477 if (str[a] == leftboundary) lb = a;
478 if (str[a] == rightboundary) rb = a;
481 if ( (lb > 0) && (rb > lb) ) {
482 strcpy(&str[lb - 1], &str[rb + 1]);
485 else if ( (lb == 0) && (rb > lb) ) {
486 strcpy(str, &str[rb + 1]);
493 * Reduce a string down to a boundarized substring (for example, remove
494 * parentheses and anything outside them).
496 void stripallbut(char *str, char leftboundary, char rightboundary) {
499 for (a = 0; a < strlen(str); ++ a) {
500 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
503 for (a = 0; a < strlen(str); ++ a) {
504 if (str[a] == rightboundary) str[a] = 0;
509 char *myfgets(char *s, int size, FILE *stream) {
510 char *ret = fgets(s, size, stream);
514 nl = strchr(s, '\n');
524 * Escape a string for feeding out as a URL.
525 * Output buffer must be big enough to handle escape expansion!
527 void urlesc(char *outbuf, char *strbuf)
530 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
534 for (a = 0; a < (int)strlen(strbuf); ++a) {
536 for (b = 0; b < strlen(ec); ++b) {
537 if (strbuf[a] == ec[b])
542 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
544 sprintf(&outbuf[b], "%c", strbuf[a]);
551 * In our world, we want strcpy() to be able to work with overlapping strings.
556 char *strcpy(char *dest, const char *src) {
557 memmove(dest, src, (strlen(src) + 1) );
563 * Generate a new, globally unique UID parameter for a calendar etc. object
565 void generate_uuid(char *buf) {
568 sprintf(buf, "%lx-%x-%x",
576 * bmstrcasestr() -- case-insensitive substring search
578 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
579 * The code is roughly based on the strstr() replacement from 'tin' written
582 char *bmstrcasestr(char *text, char *pattern) {
584 register unsigned char *p, *t;
585 register int i, j, *delta;
591 textlen = strlen (text);
592 patlen = strlen (pattern);
594 /* algorithm fails if pattern is empty */
595 if ((p1 = patlen) == 0)
598 /* code below fails (whenever i is unsigned) if pattern too long */
604 for (i = 0; i <= 255; i++)
606 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
607 delta[tolower(*p++)] = i;
610 * From now on, we want patlen - 1.
611 * In the loop below, p points to the end of the pattern,
612 * t points to the end of the text to be tested against the
613 * pattern, and i counts the amount of text remaining, not
614 * including the part to be tested.
617 p = (unsigned char *) pattern + p1;
618 t = (unsigned char *) text + p1;
619 i = textlen - patlen;
621 if (tolower(p[0]) == tolower(t[0])) {
622 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
623 return ((char *)t - p1);
626 j = delta[tolower(t[0])];