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)
204 tm = localtime(&thetime);
211 sprintf(buf, "%s %d %d %2d:%02d%s",
212 ascmonths[tm->tm_mon],
215 hour, tm->tm_min, ((tm->tm_hour >= 12) ? "pm" : "am")
222 * Format TIME ONLY for output
224 void fmt_time(char *buf, time_t thetime)
230 tm = localtime(&thetime);
237 sprintf(buf, "%d:%02d%s",
238 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
246 * Format a date/time stamp to the format used in HTTP headers
248 void httpdate(char *buf, time_t thetime)
253 tm = localtime(&thetime);
255 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
256 ascdays[tm->tm_wday],
258 ascmonths[tm->tm_mon],
259 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
267 * Utility function to "readline" from memory
268 * (returns new pointer)
270 char *memreadline(char *start, char *buf, int maxlen)
274 int len = 0; /* tally our own length to avoid strlen() delays */
277 memset(buf, 0, maxlen);
281 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
282 buf[strlen(buf) + 1] = 0;
283 buf[strlen(buf)] = ch;
286 if ((ch == 10) || (ch == 0)) {
295 * pattern2() - searches for patn within search string, returns pos
297 int pattern2(char *search, char *patn)
300 for (a = 0; a < strlen(search); ++a) {
301 if (!strncasecmp(&search[a], patn, strlen(patn)))
309 * Strip leading and trailing spaces from a string
311 void striplt(char *buf)
313 if (strlen(buf) == 0) return;
314 while ((strlen(buf) > 0) && (isspace(buf[0])))
315 strcpy(buf, &buf[1]);
316 if (strlen(buf) == 0) return;
317 while (isspace(buf[strlen(buf) - 1]))
318 buf[strlen(buf) - 1] = 0;
323 * Determine whether the specified message number is contained within the
326 int is_msg_in_mset(char *mset, long msgnum) {
329 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
333 * Now set it for all specified messages.
335 num_sets = num_tokens(mset, ',');
336 for (s=0; s<num_sets; ++s) {
337 extract_token(setstr, mset, s, ',', sizeof setstr);
339 extract_token(lostr, setstr, 0, ':', sizeof lostr);
340 if (num_tokens(setstr, ':') >= 2) {
341 extract_token(histr, setstr, 1, ':', sizeof histr);
342 if (!strcmp(histr, "*")) {
343 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
347 strcpy(histr, lostr);
352 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
361 * Strip a boundarized substring out of a string (for example, remove
362 * parentheses and anything inside them).
364 * This improved version can strip out *multiple* boundarized substrings.
366 void stripout(char *str, char leftboundary, char rightboundary)
376 for (a = 0; a < strlen(str); ++a) {
377 if (str[a] == leftboundary)
379 if (str[a] == rightboundary)
383 if ((lb > 0) && (rb > lb)) {
384 strcpy(&str[lb - 1], &str[rb + 1]);
387 } while ((lb > 0) && (rb > lb));
394 * Replacement for sleep() that uses select() in order to avoid SIGALRM
396 void sleeeeeeeeeep(int seconds)
402 select(0, NULL, NULL, NULL, &tv);
408 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
409 * John Walker, copied over from the Citadel server.
412 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
414 int i, hiteof = FALSE;
419 /* Fill dtable with character encodings. */
421 for (i = 0; i < 26; i++) {
423 dtable[26 + i] = 'a' + i;
425 for (i = 0; i < 10; i++) {
426 dtable[52 + i] = '0' + i;
432 byte igroup[3], ogroup[4];
435 igroup[0] = igroup[1] = igroup[2] = 0;
436 for (n = 0; n < 3; n++) {
437 if (spos >= sourcelen) {
442 igroup[n] = (byte) c;
445 ogroup[0] = dtable[igroup[0] >> 2];
447 dtable[((igroup[0] & 3) << 4) |
450 dtable[((igroup[1] & 0xF) << 2) |
452 ogroup[3] = dtable[igroup[2] & 0x3F];
454 /* Replace characters in output stream with "=" pad
455 characters if fewer than three characters were
456 read from the end of the input stream. */
464 for (i = 0; i < 4; i++) {
465 dest[dpos++] = ogroup[i];
487 * Convert base64-encoded to binary. Returns the length of the decoded data.
488 * It will stop after reading 'length' bytes.
490 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
496 for (i = 0; i < 255; i++) {
499 for (i = 'A'; i <= 'Z'; i++) {
500 dtable[i] = 0 + (i - 'A');
502 for (i = 'a'; i <= 'z'; i++) {
503 dtable[i] = 26 + (i - 'a');
505 for (i = '0'; i <= '9'; i++) {
506 dtable[i] = 52 + (i - '0');
512 /*CONSTANTCONDITION*/ while (TRUE) {
513 byte a[4], b[4], o[3];
515 for (i = 0; i < 4; i++) {
516 if (spos >= length) {
527 if (dtable[c] & 0x80) {
528 /* Ignoring errors: discard invalid character. */
533 b[i] = (byte) dtable[c];
535 o[0] = (b[0] << 2) | (b[1] >> 4);
536 o[1] = (b[1] << 4) | (b[2] >> 2);
537 o[2] = (b[2] << 6) | b[3];
538 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
554 * Generate a new, globally unique UID parameter for a calendar etc. object
556 void generate_uuid(char *buf) {
559 sprintf(buf, "{%08x-%04x-%04x-%04x-%012x}",