2 * tools.c -- Miscellaneous routines
11 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <netinet/in.h>
26 #include "webserver.h"
28 typedef unsigned char byte;
34 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
39 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
42 static byte dtable[256]; /* base64 encode / decode table */
44 char *safestrncpy(char *dest, const char *src, size_t n)
46 if (dest == NULL || src == NULL) {
49 strncpy(dest, src, n);
57 * num_tokens() - discover number of parameters/tokens in a string
59 int num_tokens(char *source, char tok)
66 for (a = 0; a < strlen(source); ++a) {
74 * extract_token() - a smarter string tokenizer
76 void extract_token(char *dest, char *source, int parmnum, char separator)
86 if (strlen(source) == 0) {
90 for (i = 0; i < strlen(source); ++i) {
91 if (source[i] == separator) {
93 } else if (curr_parm == parmnum) {
95 dest[len++] = source[i];
103 * remove_token() - a tokenizer that kills, maims, and destroys
105 void remove_token(char *source, int parmnum, char separator)
117 if (strlen(source) == 0) {
121 for (i = 0; i < strlen(source); ++i) {
122 if ((start < 0) && (curr_parm == parmnum)) {
126 if ((end < 0) && (curr_parm == (parmnum + 1))) {
130 if (source[i] == separator) {
136 end = strlen(source);
138 strcpy(&source[start], &source[end]);
145 * extract_int() - extract an int parm w/o supplying a buffer
147 int extract_int(char *source, int parmnum)
151 extract_token(buf, source, parmnum, '|');
156 * extract_long() - extract an long parm w/o supplying a buffer
158 long extract_long(char *source, long int parmnum)
162 extract_token(buf, source, parmnum, '|');
181 * check for the presence of a character within a string (returns count)
189 for (a = 0; a < strlen(st); ++a)
197 * Format a date/time stamp for output
199 void fmt_date(char *buf, time_t thetime)
205 tm = localtime(&thetime);
212 sprintf(buf, "%s %d %d %2d:%02d%s",
213 ascmonths[tm->tm_mon],
216 hour, tm->tm_min, ((tm->tm_hour >= 12) ? "pm" : "am")
223 * Format TIME ONLY for output
225 void fmt_time(char *buf, time_t thetime)
231 tm = localtime(&thetime);
238 sprintf(buf, "%d:%02d%s",
239 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
247 * Format a date/time stamp to the format used in HTTP headers
249 void httpdate(char *buf, time_t thetime)
254 tm = localtime(&thetime);
256 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
257 ascdays[tm->tm_wday],
259 ascmonths[tm->tm_mon],
260 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
268 * Utility function to "readline" from memory
269 * (returns new pointer)
271 char *memreadline(char *start, char *buf, int maxlen)
275 int len = 0; /* tally our own length to avoid strlen() delays */
278 memset(buf, 0, maxlen);
282 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
283 buf[strlen(buf) + 1] = 0;
284 buf[strlen(buf)] = ch;
287 if ((ch == 10) || (ch == 0)) {
296 * pattern2() - searches for patn within search string, returns pos
298 int pattern2(char *search, char *patn)
301 for (a = 0; a < strlen(search); ++a) {
302 if (!strncasecmp(&search[a], patn, strlen(patn)))
310 * Strip leading and trailing spaces from a string
312 void striplt(char *buf)
314 if (strlen(buf) == 0) return;
315 while ((strlen(buf) > 0) && (isspace(buf[0])))
316 strcpy(buf, &buf[1]);
317 if (strlen(buf) == 0) return;
318 while (isspace(buf[strlen(buf) - 1]))
319 buf[strlen(buf) - 1] = 0;
324 * Determine whether the specified message number is contained within the
327 int is_msg_in_mset(char *mset, long msgnum)
331 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
335 * Now set it for all specified messages.
337 num_sets = num_tokens(mset, ',');
338 for (s = 0; s < num_sets; ++s) {
339 extract_token(setstr, mset, s, ',');
341 extract_token(lostr, setstr, 0, ':');
342 if (num_tokens(setstr, ':') >= 2) {
343 extract_token(histr, setstr, 1, ':');
344 if (!strcmp(histr, "*")) {
345 snprintf(histr, sizeof histr, "%ld",
349 strcpy(histr, lostr);
354 if ((msgnum >= lo) && (msgnum <= hi))
363 * Strip a boundarized substring out of a string (for example, remove
364 * parentheses and anything inside them).
366 * This improved version can strip out *multiple* boundarized substrings.
368 void stripout(char *str, char leftboundary, char rightboundary)
378 for (a = 0; a < strlen(str); ++a) {
379 if (str[a] == leftboundary)
381 if (str[a] == rightboundary)
385 if ((lb > 0) && (rb > lb)) {
386 strcpy(&str[lb - 1], &str[rb + 1]);
389 } while ((lb > 0) && (rb > lb));
396 * Replacement for sleep() that uses select() in order to avoid SIGALRM
398 void sleeeeeeeeeep(int seconds)
404 select(0, NULL, NULL, NULL, &tv);
410 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
411 * John Walker, copied over from the Citadel server.
414 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
416 int i, hiteof = FALSE;
421 /* Fill dtable with character encodings. */
423 for (i = 0; i < 26; i++) {
425 dtable[26 + i] = 'a' + i;
427 for (i = 0; i < 10; i++) {
428 dtable[52 + i] = '0' + i;
434 byte igroup[3], ogroup[4];
437 igroup[0] = igroup[1] = igroup[2] = 0;
438 for (n = 0; n < 3; n++) {
439 if (spos >= sourcelen) {
444 igroup[n] = (byte) c;
447 ogroup[0] = dtable[igroup[0] >> 2];
449 dtable[((igroup[0] & 3) << 4) |
452 dtable[((igroup[1] & 0xF) << 2) |
454 ogroup[3] = dtable[igroup[2] & 0x3F];
456 /* Replace characters in output stream with "=" pad
457 characters if fewer than three characters were
458 read from the end of the input stream. */
466 for (i = 0; i < 4; i++) {
467 dest[dpos++] = ogroup[i];
489 * Convert base64-encoded to binary. Returns the length of the decoded data.
490 * It will stop after reading 'length' bytes.
492 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
498 for (i = 0; i < 255; i++) {
501 for (i = 'A'; i <= 'Z'; i++) {
502 dtable[i] = 0 + (i - 'A');
504 for (i = 'a'; i <= 'z'; i++) {
505 dtable[i] = 26 + (i - 'a');
507 for (i = '0'; i <= '9'; i++) {
508 dtable[i] = 52 + (i - '0');
514 /*CONSTANTCONDITION*/ while (TRUE) {
515 byte a[4], b[4], o[3];
517 for (i = 0; i < 4; i++) {
518 if (spos >= length) {
529 if (dtable[c] & 0x80) {
530 /* Ignoring errors: discard invalid character. */
535 b[i] = (byte) dtable[c];
537 o[0] = (b[0] << 2) | (b[1] >> 4);
538 o[1] = (b[1] << 4) | (b[2] >> 2);
539 o[2] = (b[2] << 6) | b[3];
540 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
556 * Generate a new, globally unique UID parameter for a calendar etc. object
558 void generate_uuid(char *buf) {
561 sprintf(buf, "{%08x-%04x-%04x-%04x-%012x}",