4 * Miscellaneous routines
13 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
30 typedef unsigned char byte;
36 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
37 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
41 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
44 static byte dtable[256]; /* base64 encode / decode table */
46 char *safestrncpy(char *dest, const char *src, size_t n)
48 if (dest == NULL || src == NULL) {
51 strncpy(dest, src, n);
59 * num_tokens() - discover number of parameters/tokens in a string
61 int num_tokens(char *source, char tok)
68 for (a = 0; a < strlen(source); ++a) {
76 * extract_token() - a string tokenizer
78 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
81 const char *s; /* source */
87 /* Locate desired parameter */
89 while (count < parmnum) {
90 /* End of string, bail! */
95 if (*s == separator) {
100 if (!s) return; /* Parameter not found */
102 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
111 * remove_token() - a tokenizer that kills, maims, and destroys
113 void remove_token(char *source, int parmnum, char separator)
125 if (strlen(source) == 0) {
129 for (i = 0; i < strlen(source); ++i) {
130 if ((start < 0) && (curr_parm == parmnum)) {
134 if ((end < 0) && (curr_parm == (parmnum + 1))) {
138 if (source[i] == separator) {
144 end = strlen(source);
146 strcpy(&source[start], &source[end]);
153 * extract_int() - extract an int parm w/o supplying a buffer
155 int extract_int(const char *source, int parmnum)
159 extract_token(buf, source, parmnum, '|', sizeof buf);
164 * extract_long() - extract an long parm w/o supplying a buffer
166 long extract_long(const char *source, int parmnum)
170 extract_token(buf, source, parmnum, '|', sizeof buf);
180 * check for the presence of a character within a string (returns count)
188 for (a = 0; a < strlen(st); ++a)
196 * Format a date/time stamp for output
198 void fmt_date(char *buf, time_t thetime, int brief)
205 today_timet = time(NULL);
206 localtime_r(&today_timet, &today_tm);
208 localtime_r(&thetime, &tm);
219 if ((tm.tm_year == today_tm.tm_year)
220 &&(tm.tm_mon == today_tm.tm_mon)
221 &&(tm.tm_mday == today_tm.tm_mday)) {
222 sprintf(buf, "%2d:%02d%s",
224 ((tm.tm_hour >= 12) ? "pm" : "am")
228 sprintf(buf, "%s %d %d",
229 ascmonths[tm.tm_mon],
236 sprintf(buf, "%s %d %d %2d:%02d%s",
237 ascmonths[tm.tm_mon],
240 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
248 * Format TIME ONLY for output
250 void fmt_time(char *buf, time_t thetime)
256 tm = localtime(&thetime);
263 sprintf(buf, "%d:%02d%s",
264 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
272 * Format a date/time stamp to the format used in HTTP headers
274 void httpdate(char *buf, time_t thetime)
279 tm = localtime(&thetime);
281 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
282 ascdays[tm->tm_wday],
284 ascmonths[tm->tm_mon],
285 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
293 * Utility function to "readline" from memory
294 * (returns new pointer)
296 char *memreadline(char *start, char *buf, int maxlen)
300 int len = 0; /* tally our own length to avoid strlen() delays */
303 memset(buf, 0, maxlen);
307 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
308 buf[strlen(buf) + 1] = 0;
309 buf[strlen(buf)] = ch;
312 if ((ch == 10) || (ch == 0)) {
321 * pattern2() - searches for patn within search string, returns pos
323 int pattern2(char *search, char *patn)
326 for (a = 0; a < strlen(search); ++a) {
327 if (!strncasecmp(&search[a], patn, strlen(patn)))
335 * Strip leading and trailing spaces from a string
337 void striplt(char *buf)
339 if (strlen(buf) == 0) return;
340 while ((strlen(buf) > 0) && (isspace(buf[0])))
341 strcpy(buf, &buf[1]);
342 if (strlen(buf) == 0) return;
343 while (isspace(buf[strlen(buf) - 1]))
344 buf[strlen(buf) - 1] = 0;
349 * Determine whether the specified message number is contained within the
352 int is_msg_in_mset(char *mset, long msgnum) {
355 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
359 * Now set it for all specified messages.
361 num_sets = num_tokens(mset, ',');
362 for (s=0; s<num_sets; ++s) {
363 extract_token(setstr, mset, s, ',', sizeof setstr);
365 extract_token(lostr, setstr, 0, ':', sizeof lostr);
366 if (num_tokens(setstr, ':') >= 2) {
367 extract_token(histr, setstr, 1, ':', sizeof histr);
368 if (!strcmp(histr, "*")) {
369 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
373 strcpy(histr, lostr);
378 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
387 * Strip a boundarized substring out of a string (for example, remove
388 * parentheses and anything inside them).
390 * This improved version can strip out *multiple* boundarized substrings.
392 void stripout(char *str, char leftboundary, char rightboundary)
402 for (a = 0; a < strlen(str); ++a) {
403 if (str[a] == leftboundary)
405 if (str[a] == rightboundary)
409 if ((lb > 0) && (rb > lb)) {
410 strcpy(&str[lb - 1], &str[rb + 1]);
413 } while ((lb > 0) && (rb > lb));
420 * Replacement for sleep() that uses select() in order to avoid SIGALRM
422 void sleeeeeeeeeep(int seconds)
428 select(0, NULL, NULL, NULL, &tv);
434 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
435 * John Walker, copied over from the Citadel server.
438 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
440 int i, hiteof = FALSE;
445 /* Fill dtable with character encodings. */
447 for (i = 0; i < 26; i++) {
449 dtable[26 + i] = 'a' + i;
451 for (i = 0; i < 10; i++) {
452 dtable[52 + i] = '0' + i;
458 byte igroup[3], ogroup[4];
461 igroup[0] = igroup[1] = igroup[2] = 0;
462 for (n = 0; n < 3; n++) {
463 if (spos >= sourcelen) {
468 igroup[n] = (byte) c;
471 ogroup[0] = dtable[igroup[0] >> 2];
473 dtable[((igroup[0] & 3) << 4) |
476 dtable[((igroup[1] & 0xF) << 2) |
478 ogroup[3] = dtable[igroup[2] & 0x3F];
480 /* Replace characters in output stream with "=" pad
481 characters if fewer than three characters were
482 read from the end of the input stream. */
490 for (i = 0; i < 4; i++) {
491 dest[dpos++] = ogroup[i];
513 * Convert base64-encoded to binary. Returns the length of the decoded data.
514 * It will stop after reading 'length' bytes.
516 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
522 for (i = 0; i < 255; i++) {
525 for (i = 'A'; i <= 'Z'; i++) {
526 dtable[i] = 0 + (i - 'A');
528 for (i = 'a'; i <= 'z'; i++) {
529 dtable[i] = 26 + (i - 'a');
531 for (i = '0'; i <= '9'; i++) {
532 dtable[i] = 52 + (i - '0');
538 /*CONSTANTCONDITION*/ while (TRUE) {
539 byte a[4], b[4], o[3];
541 for (i = 0; i < 4; i++) {
542 if (spos >= length) {
553 if (dtable[c] & 0x80) {
554 /* Ignoring errors: discard invalid character */
559 b[i] = (byte) dtable[c];
561 o[0] = (b[0] << 2) | (b[1] >> 4);
562 o[1] = (b[1] << 4) | (b[2] >> 2);
563 o[2] = (b[2] << 6) | b[3];
564 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
581 * Generate a new, globally unique UID parameter for a calendar etc. object
583 void generate_uuid(char *buf) {
586 sprintf(buf, "%s-%lx-%x-%x",
587 serv_info.serv_nodename,