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 while ((strlen(buf) > 0) && (isspace(buf[0])))
315 strcpy(buf, &buf[1]);
316 while (isspace(buf[strlen(buf) - 1]))
317 buf[strlen(buf) - 1] = 0;
322 * Determine whether the specified message number is contained within the
325 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, ',');
339 extract_token(lostr, setstr, 0, ':');
340 if (num_tokens(setstr, ':') >= 2) {
341 extract_token(histr, setstr, 1, ':');
342 if (!strcmp(histr, "*")) {
343 snprintf(histr, sizeof histr, "%ld",
347 strcpy(histr, lostr);
352 if ((msgnum >= lo) && (msgnum <= hi))
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);