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) {
47 lprintf(1, "safestrncpy: NULL argument\n");
50 strncpy(dest, src, n);
58 * num_tokens() - discover number of parameters/tokens in a string
60 int num_tokens(char *source, char tok)
67 for (a = 0; a < strlen(source); ++a) {
75 * extract_token() - a smarter string tokenizer
77 void extract_token(char *dest, char *source, int parmnum, char separator)
87 if (strlen(source) == 0) {
91 for (i = 0; i < strlen(source); ++i) {
92 if (source[i] == separator) {
94 } else if (curr_parm == parmnum) {
96 dest[len++] = source[i];
104 * remove_token() - a tokenizer that kills, maims, and destroys
106 void remove_token(char *source, int parmnum, char separator)
118 if (strlen(source) == 0) {
122 for (i = 0; i < strlen(source); ++i) {
123 if ((start < 0) && (curr_parm == parmnum)) {
127 if ((end < 0) && (curr_parm == (parmnum + 1))) {
131 if (source[i] == separator) {
137 end = strlen(source);
139 strcpy(&source[start], &source[end]);
146 * extract_int() - extract an int parm w/o supplying a buffer
148 int extract_int(char *source, int parmnum)
152 extract_token(buf, source, parmnum, '|');
157 * extract_long() - extract an long parm w/o supplying a buffer
159 long extract_long(char *source, long int parmnum)
163 extract_token(buf, source, parmnum, '|');
182 * check for the presence of a character within a string (returns count)
190 for (a = 0; a < strlen(st); ++a)
198 * Format a date/time stamp for output
200 void fmt_date(char *buf, time_t thetime)
206 tm = localtime(&thetime);
213 sprintf(buf, "%s %d %d %2d:%02d%s",
214 ascmonths[tm->tm_mon],
217 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
224 * Format TIME ONLY for output
226 void fmt_time(char *buf, time_t thetime)
232 tm = localtime(&thetime);
239 sprintf(buf, "%d:%02d%s",
240 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
248 * Format a date/time stamp to the format used in HTTP headers
250 void httpdate(char *buf, time_t thetime)
255 tm = localtime(&thetime);
257 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
258 ascdays[tm->tm_wday],
260 ascmonths[tm->tm_mon],
261 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
269 * Utility function to "readline" from memory
270 * (returns new pointer)
272 char *memreadline(char *start, char *buf, int maxlen)
276 int len = 0; /* tally our own length to avoid strlen() delays */
279 memset(buf, 0, maxlen);
283 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
284 buf[strlen(buf) + 1] = 0;
285 buf[strlen(buf)] = ch;
288 if ((ch == 10) || (ch == 0)) {
297 * pattern2() - searches for patn within search string, returns pos
299 int pattern2(char *search, char *patn)
302 for (a = 0; a < strlen(search); ++a) {
303 if (!strncasecmp(&search[a], patn, strlen(patn)))
311 * Strip leading and trailing spaces from a string
313 void striplt(char *buf)
315 while ((strlen(buf) > 0) && (isspace(buf[0])))
316 strcpy(buf, &buf[1]);
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)
330 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
334 * Now set it for all specified messages.
336 num_sets = num_tokens(mset, ',');
337 for (s = 0; s < num_sets; ++s) {
338 extract_token(setstr, mset, s, ',');
340 extract_token(lostr, setstr, 0, ':');
341 if (num_tokens(setstr, ':') >= 2) {
342 extract_token(histr, setstr, 1, ':');
343 if (!strcmp(histr, "*")) {
344 snprintf(histr, sizeof histr, "%ld",
348 strcpy(histr, lostr);
353 if ((msgnum >= lo) && (msgnum <= hi))
362 * Strip a boundarized substring out of a string (for example, remove
363 * parentheses and anything inside them).
365 * This improved version can strip out *multiple* boundarized substrings.
367 void stripout(char *str, char leftboundary, char rightboundary)
377 for (a = 0; a < strlen(str); ++a) {
378 if (str[a] == leftboundary)
380 if (str[a] == rightboundary)
384 if ((lb > 0) && (rb > lb)) {
385 strcpy(&str[lb - 1], &str[rb + 1]);
388 } while ((lb > 0) && (rb > lb));
395 * Replacement for sleep() that uses select() in order to avoid SIGALRM
397 void sleeeeeeeeeep(int seconds)
403 select(0, NULL, NULL, NULL, &tv);
409 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
410 * John Walker, copied over from the Citadel server.
413 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
415 int i, hiteof = FALSE;
420 /* Fill dtable with character encodings. */
422 for (i = 0; i < 26; i++) {
424 dtable[26 + i] = 'a' + i;
426 for (i = 0; i < 10; i++) {
427 dtable[52 + i] = '0' + i;
433 byte igroup[3], ogroup[4];
436 igroup[0] = igroup[1] = igroup[2] = 0;
437 for (n = 0; n < 3; n++) {
438 if (spos >= sourcelen) {
443 igroup[n] = (byte) c;
446 ogroup[0] = dtable[igroup[0] >> 2];
448 dtable[((igroup[0] & 3) << 4) |
451 dtable[((igroup[1] & 0xF) << 2) |
453 ogroup[3] = dtable[igroup[2] & 0x3F];
455 /* Replace characters in output stream with "=" pad
456 characters if fewer than three characters were
457 read from the end of the input stream. */
465 for (i = 0; i < 4; i++) {
466 dest[dpos++] = ogroup[i];
488 * Convert base64-encoded to binary. Returns the length of the decoded data.
489 * It will stop after reading 'length' bytes.
491 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
497 for (i = 0; i < 255; i++) {
500 for (i = 'A'; i <= 'Z'; i++) {
501 dtable[i] = 0 + (i - 'A');
503 for (i = 'a'; i <= 'z'; i++) {
504 dtable[i] = 26 + (i - 'a');
506 for (i = '0'; i <= '9'; i++) {
507 dtable[i] = 52 + (i - '0');
513 /*CONSTANTCONDITION*/ while (TRUE) {
514 byte a[4], b[4], o[3];
516 for (i = 0; i < 4; i++) {
517 if (spos >= length) {
528 if (dtable[c] & 0x80) {
529 /* Ignoring errors: discard invalid character. */
534 b[i] = (byte) dtable[c];
536 o[0] = (b[0] << 2) | (b[1] >> 4);
537 o[1] = (b[1] << 4) | (b[2] >> 2);
538 o[2] = (b[2] << 6) | b[3];
539 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);