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 smarter string tokenizer
78 void extract_token(char *dest, char *source, int parmnum, char separator)
88 if (strlen(source) == 0) {
92 for (i = 0; i < strlen(source); ++i) {
93 if (source[i] == separator) {
95 } else if (curr_parm == parmnum) {
97 dest[len++] = source[i];
105 * remove_token() - a tokenizer that kills, maims, and destroys
107 void remove_token(char *source, int parmnum, char separator)
119 if (strlen(source) == 0) {
123 for (i = 0; i < strlen(source); ++i) {
124 if ((start < 0) && (curr_parm == parmnum)) {
128 if ((end < 0) && (curr_parm == (parmnum + 1))) {
132 if (source[i] == separator) {
138 end = strlen(source);
140 strcpy(&source[start], &source[end]);
147 * extract_int() - extract an int parm w/o supplying a buffer
149 int extract_int(char *source, int parmnum)
153 extract_token(buf, source, parmnum, '|');
158 * extract_long() - extract an long parm w/o supplying a buffer
160 long extract_long(char *source, long int parmnum)
164 extract_token(buf, source, parmnum, '|');
183 * check for the presence of a character within a string (returns count)
191 for (a = 0; a < strlen(st); ++a)
199 * Format a date/time stamp for output
201 void fmt_date(char *buf, time_t thetime)
207 tm = localtime(&thetime);
214 sprintf(buf, "%s %d %d %2d:%02d%s",
215 ascmonths[tm->tm_mon],
218 hour, tm->tm_min, ((tm->tm_hour >= 12) ? "pm" : "am")
225 * Format TIME ONLY for output
227 void fmt_time(char *buf, time_t thetime)
233 tm = localtime(&thetime);
240 sprintf(buf, "%d:%02d%s",
241 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
249 * Format a date/time stamp to the format used in HTTP headers
251 void httpdate(char *buf, time_t thetime)
256 tm = localtime(&thetime);
258 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
259 ascdays[tm->tm_wday],
261 ascmonths[tm->tm_mon],
262 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
270 * Utility function to "readline" from memory
271 * (returns new pointer)
273 char *memreadline(char *start, char *buf, int maxlen)
277 int len = 0; /* tally our own length to avoid strlen() delays */
280 memset(buf, 0, maxlen);
284 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
285 buf[strlen(buf) + 1] = 0;
286 buf[strlen(buf)] = ch;
289 if ((ch == 10) || (ch == 0)) {
298 * pattern2() - searches for patn within search string, returns pos
300 int pattern2(char *search, char *patn)
303 for (a = 0; a < strlen(search); ++a) {
304 if (!strncasecmp(&search[a], patn, strlen(patn)))
312 * Strip leading and trailing spaces from a string
314 void striplt(char *buf)
316 if (strlen(buf) == 0) return;
317 while ((strlen(buf) > 0) && (isspace(buf[0])))
318 strcpy(buf, &buf[1]);
319 if (strlen(buf) == 0) return;
320 while (isspace(buf[strlen(buf) - 1]))
321 buf[strlen(buf) - 1] = 0;
326 * Determine whether the specified message number is contained within the
329 int is_msg_in_mset(char *mset, long msgnum)
333 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
337 * Now set it for all specified messages.
339 num_sets = num_tokens(mset, ',');
340 for (s = 0; s < num_sets; ++s) {
341 extract_token(setstr, mset, s, ',');
343 extract_token(lostr, setstr, 0, ':');
344 if (num_tokens(setstr, ':') >= 2) {
345 extract_token(histr, setstr, 1, ':');
346 if (!strcmp(histr, "*")) {
347 snprintf(histr, sizeof histr, "%ld",
351 strcpy(histr, lostr);
356 if ((msgnum >= lo) && (msgnum <= hi))
365 * Strip a boundarized substring out of a string (for example, remove
366 * parentheses and anything inside them).
368 * This improved version can strip out *multiple* boundarized substrings.
370 void stripout(char *str, char leftboundary, char rightboundary)
380 for (a = 0; a < strlen(str); ++a) {
381 if (str[a] == leftboundary)
383 if (str[a] == rightboundary)
387 if ((lb > 0) && (rb > lb)) {
388 strcpy(&str[lb - 1], &str[rb + 1]);
391 } while ((lb > 0) && (rb > lb));
398 * Replacement for sleep() that uses select() in order to avoid SIGALRM
400 void sleeeeeeeeeep(int seconds)
406 select(0, NULL, NULL, NULL, &tv);
412 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
413 * John Walker, copied over from the Citadel server.
416 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
418 int i, hiteof = FALSE;
423 /* Fill dtable with character encodings. */
425 for (i = 0; i < 26; i++) {
427 dtable[26 + i] = 'a' + i;
429 for (i = 0; i < 10; i++) {
430 dtable[52 + i] = '0' + i;
436 byte igroup[3], ogroup[4];
439 igroup[0] = igroup[1] = igroup[2] = 0;
440 for (n = 0; n < 3; n++) {
441 if (spos >= sourcelen) {
446 igroup[n] = (byte) c;
449 ogroup[0] = dtable[igroup[0] >> 2];
451 dtable[((igroup[0] & 3) << 4) |
454 dtable[((igroup[1] & 0xF) << 2) |
456 ogroup[3] = dtable[igroup[2] & 0x3F];
458 /* Replace characters in output stream with "=" pad
459 characters if fewer than three characters were
460 read from the end of the input stream. */
468 for (i = 0; i < 4; i++) {
469 dest[dpos++] = ogroup[i];
491 * Convert base64-encoded to binary. Returns the length of the decoded data.
492 * It will stop after reading 'length' bytes.
494 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
500 for (i = 0; i < 255; i++) {
503 for (i = 'A'; i <= 'Z'; i++) {
504 dtable[i] = 0 + (i - 'A');
506 for (i = 'a'; i <= 'z'; i++) {
507 dtable[i] = 26 + (i - 'a');
509 for (i = '0'; i <= '9'; i++) {
510 dtable[i] = 52 + (i - '0');
516 /*CONSTANTCONDITION*/ while (TRUE) {
517 byte a[4], b[4], o[3];
519 for (i = 0; i < 4; i++) {
520 if (spos >= length) {
531 if (dtable[c] & 0x80) {
532 /* Ignoring errors: discard invalid character. */
537 b[i] = (byte) dtable[c];
539 o[0] = (b[0] << 2) | (b[1] >> 4);
540 o[1] = (b[1] << 4) | (b[2] >> 2);
541 o[2] = (b[2] << 6) | b[3];
542 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
558 * Generate a new, globally unique UID parameter for a calendar etc. object
560 void generate_uuid(char *buf) {
563 sprintf(buf, "{%08x-%04x-%04x-%04x-%012x}",