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 */
42 char *safestrncpy(char *dest, const char *src, size_t n)
44 if (dest == NULL || src == NULL) {
45 fprintf(stderr, "safestrncpy: NULL argument\n");
48 strncpy(dest, src, n);
55 #ifndef HAVE_STRNCASECMP
56 int strncasecmp(char *lstr, char *rstr, int len)
61 lc=tolower(lstr[pos]);
62 rc=tolower(rstr[pos]);
63 if ((lc==0)&&(rc==0)) return(0);
64 if (lc<rc) return(-1);
75 * num_tokens() - discover number of parameters/tokens in a string
77 int num_tokens(const char *source, char tok) {
81 if (source == NULL) return(0);
82 for (a=0; a<strlen(source); ++a) {
83 if (source[a]==tok) ++count;
89 /* extract_token() - a smarter string tokenizer */
90 void extract_token(char *dest, const char *source, unsigned long parmnum, char separator)
93 const char *s; /* source */
98 /* Locate desired parameter */
100 while (count < parmnum) {
101 /* End of string, bail! */
106 if (*s == separator) {
111 if (!s) return; /* Parameter not found */
113 for (d = dest; *s && *s != separator; s++, d++) {
120 /* remove_token() - a tokenizer that kills, maims, and destroys fast */
121 void remove_token(char *source, unsigned long parmnum, char separator)
123 char *d, *s; /* dest, source */
126 /* Find desired parameter */
128 while (count < parmnum) {
129 /* End of string, bail! */
134 if (*d == separator) {
139 if (!d) return; /* Parameter not found */
141 /* Find next parameter */
143 while (*s && *s != separator) {
150 else if (d == source)
164 * extract_int() - extract an int parm w/o supplying a buffer
166 int extract_int(const char *source, unsigned long parmnum)
170 extract_token(buf, source, parmnum, '|');
175 * extract_long() - extract an long parm w/o supplying a buffer
177 long extract_long(const char *source, unsigned long parmnum)
181 extract_token(buf, source, parmnum, '|');
187 * extract_unsigned_long() - extract an unsigned long parm
189 unsigned long extract_unsigned_long(const char *source, unsigned long parmnum)
193 extract_token(buf, source, parmnum, '|');
194 return strtoul(buf, NULL, 10);
199 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
200 * John Walker, found in full in the file "base64.c" included with this
201 * distribution. We are moving in the direction of eventually discarding
202 * the separate executables, and using the ones in our code exclusively.
205 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
207 int i, hiteof = FALSE;
211 /* Fill dtable with character encodings. */
213 for (i = 0; i < 26; i++) {
215 dtable[26 + i] = 'a' + i;
217 for (i = 0; i < 10; i++) {
218 dtable[52 + i] = '0' + i;
224 byte igroup[3], ogroup[4];
227 igroup[0] = igroup[1] = igroup[2] = 0;
228 for (n = 0; n < 3; n++) {
229 if (spos >= sourcelen) {
234 igroup[n] = (byte) c;
237 ogroup[0] = dtable[igroup[0] >> 2];
238 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
239 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
240 ogroup[3] = dtable[igroup[2] & 0x3F];
242 /* Replace characters in output stream with "=" pad
243 characters if fewer than three characters were
244 read from the end of the input stream. */
252 for (i = 0; i < 4; i++) {
253 dest[dpos++] = ogroup[i];
262 * Convert base64-encoded to binary. Returns the length of the decoded data.
263 * It will stop after reading 'length' bytes.
265 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
271 for (i = 0; i < 255; i++) {
274 for (i = 'A'; i <= 'Z'; i++) {
275 dtable[i] = 0 + (i - 'A');
277 for (i = 'a'; i <= 'z'; i++) {
278 dtable[i] = 26 + (i - 'a');
280 for (i = '0'; i <= '9'; i++) {
281 dtable[i] = 52 + (i - '0');
287 /*CONSTANTCONDITION*/
289 byte a[4], b[4], o[3];
291 for (i = 0; i < 4; i++) {
292 if (spos >= length) {
303 if (dtable[c] & 0x80) {
304 /* Ignoring errors: discard invalid character. */
309 b[i] = (byte) dtable[c];
311 o[0] = (b[0] << 2) | (b[1] >> 4);
312 o[1] = (b[1] << 4) | (b[2] >> 2);
313 o[2] = (b[2] << 6) | b[3];
314 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
315 if (i>=1) dest[dpos++] = o[0];
316 if (i>=2) dest[dpos++] = o[1];
317 if (i>=3) dest[dpos++] = o[2];
328 * Strip leading and trailing spaces from a string
330 void striplt(char *buf)
332 while ((strlen(buf) > 0) && (isspace(buf[0])))
333 strcpy(buf, &buf[1]);
334 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
335 buf[strlen(buf) - 1] = 0;
343 * Return the number of occurances of character ch in string st
345 int haschar(const char *st, int ch)
349 for (a = 0; a < strlen(st); ++a)
360 * Format a date/time stamp for output
361 * seconds is whether to print the seconds
363 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
367 char *ascmonths[] = {
368 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
369 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
373 tm = localtime(&thetime);
376 if (hour == 0) hour = 12;
377 else if (hour > 12) hour = hour - 12;
380 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
381 ascmonths[tm->tm_mon],
387 ( (tm->tm_hour >= 12) ? "pm" : "am" )
390 snprintf(buf, n, "%s %d %4d %d:%02d%s",
391 ascmonths[tm->tm_mon],
396 ( (tm->tm_hour >= 12) ? "pm" : "am" )
404 * Determine whether the specified message number is contained within the
407 int is_msg_in_mset(char *mset, long msgnum) {
410 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
414 * Now set it for all specified messages.
416 num_sets = num_tokens(mset, ',');
417 for (s=0; s<num_sets; ++s) {
418 extract_token(setstr, mset, s, ',');
420 extract_token(lostr, setstr, 0, ':');
421 if (num_tokens(setstr, ':') >= 2) {
422 extract_token(histr, setstr, 1, ':');
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]);
489 * Reduce a string down to a boundarized substring (for example, remove
490 * parentheses and anything outside them).
492 void stripallbut(char *str, char leftboundary, char rightboundary) {
495 for (a = 0; a < strlen(str); ++ a) {
496 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
499 for (a = 0; a < strlen(str); ++ a) {
500 if (str[a] == rightboundary) str[a] = 0;
505 char *myfgets(char *s, int size, FILE *stream) {
506 char *ret = fgets(s, size, stream);
510 nl = strchr(s, '\n');
520 * Escape a string for feeding out as a URL.
521 * Output buffer must be big enough to handle escape expansion!
523 void urlesc(char *outbuf, char *strbuf)
526 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
530 for (a = 0; a < strlen(strbuf); ++a) {
532 for (b = 0; b < strlen(ec); ++b) {
533 if (strbuf[a] == ec[b])
538 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
540 sprintf(&outbuf[b], "%c", strbuf[a]);
546 * Citadelian replacement for tmpnam()
548 char *CtdlTempFileName(char *prefix1, int prefix2) {
550 static char buf[SIZ];
552 sprintf(buf, "/tmp/Citadel-%s-%d-%04x-%04x",
564 * Citadelian replacement for tmpfile()
566 FILE *CtdlTempFile(void) {
570 strcpy(filename, tmpnam(NULL));
571 fp = fopen(filename, "w+b");