4 * Miscellaneous routines
17 #include <sys/types.h>
19 #include <sys/socket.h>
20 #ifdef HAVE_SYS_TIME_H
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
36 #include "webserver.h"
39 typedef unsigned char byte;
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 * Utility function to "readline" from memory
197 * (returns new pointer)
199 char *memreadline(char *start, char *buf, int maxlen)
203 int len = 0; /* tally our own length to avoid strlen() delays */
206 memset(buf, 0, maxlen);
210 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
211 buf[strlen(buf) + 1] = 0;
212 buf[strlen(buf)] = ch;
215 if ((ch == 10) || (ch == 0)) {
224 * pattern2() - searches for patn within search string, returns pos
226 int pattern2(char *search, char *patn)
229 for (a = 0; a < strlen(search); ++a) {
230 if (!strncasecmp(&search[a], patn, strlen(patn)))
238 * Strip leading and trailing spaces from a string
240 void striplt(char *buf)
242 if (strlen(buf) == 0) return;
243 while ((strlen(buf) > 0) && (isspace(buf[0])))
244 strcpy(buf, &buf[1]);
245 if (strlen(buf) == 0) return;
246 while (isspace(buf[strlen(buf) - 1]))
247 buf[strlen(buf) - 1] = 0;
252 * Determine whether the specified message number is contained within the
255 int is_msg_in_mset(char *mset, long msgnum) {
258 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
262 * Now set it for all specified messages.
264 num_sets = num_tokens(mset, ',');
265 for (s=0; s<num_sets; ++s) {
266 extract_token(setstr, mset, s, ',', sizeof setstr);
268 extract_token(lostr, setstr, 0, ':', sizeof lostr);
269 if (num_tokens(setstr, ':') >= 2) {
270 extract_token(histr, setstr, 1, ':', sizeof histr);
271 if (!strcmp(histr, "*")) {
272 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
276 strcpy(histr, lostr);
281 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
290 * Strip a boundarized substring out of a string (for example, remove
291 * parentheses and anything inside them).
293 * This improved version can strip out *multiple* boundarized substrings.
295 void stripout(char *str, char leftboundary, char rightboundary)
305 for (a = 0; a < strlen(str); ++a) {
306 if (str[a] == leftboundary)
308 if (str[a] == rightboundary)
312 if ((lb > 0) && (rb > lb)) {
313 strcpy(&str[lb - 1], &str[rb + 1]);
316 } while ((lb > 0) && (rb > lb));
323 * Replacement for sleep() that uses select() in order to avoid SIGALRM
325 void sleeeeeeeeeep(int seconds)
331 select(0, NULL, NULL, NULL, &tv);
337 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
338 * John Walker, copied over from the Citadel server.
341 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
343 int i, hiteof = FALSE;
348 /* Fill dtable with character encodings. */
350 for (i = 0; i < 26; i++) {
352 dtable[26 + i] = 'a' + i;
354 for (i = 0; i < 10; i++) {
355 dtable[52 + i] = '0' + i;
361 byte igroup[3], ogroup[4];
364 igroup[0] = igroup[1] = igroup[2] = 0;
365 for (n = 0; n < 3; n++) {
366 if (spos >= sourcelen) {
371 igroup[n] = (byte) c;
374 ogroup[0] = dtable[igroup[0] >> 2];
376 dtable[((igroup[0] & 3) << 4) |
379 dtable[((igroup[1] & 0xF) << 2) |
381 ogroup[3] = dtable[igroup[2] & 0x3F];
383 /* Replace characters in output stream with "=" pad
384 characters if fewer than three characters were
385 read from the end of the input stream. */
393 for (i = 0; i < 4; i++) {
394 dest[dpos++] = ogroup[i];
416 * Convert base64-encoded to binary. Returns the length of the decoded data.
417 * It will stop after reading 'length' bytes.
419 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
425 for (i = 0; i < 255; i++) {
428 for (i = 'A'; i <= 'Z'; i++) {
429 dtable[i] = 0 + (i - 'A');
431 for (i = 'a'; i <= 'z'; i++) {
432 dtable[i] = 26 + (i - 'a');
434 for (i = '0'; i <= '9'; i++) {
435 dtable[i] = 52 + (i - '0');
441 /*CONSTANTCONDITION*/ while (TRUE) {
442 byte a[4], b[4], o[3];
444 for (i = 0; i < 4; i++) {
445 if (spos >= length) {
456 if (dtable[c] & 0x80) {
457 /* Ignoring errors: discard invalid character */
462 b[i] = (byte) dtable[c];
464 o[0] = (b[0] << 2) | (b[1] >> 4);
465 o[1] = (b[1] << 4) | (b[2] >> 2);
466 o[2] = (b[2] << 6) | b[3];
467 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
484 * Generate a new, globally unique UID parameter for a calendar etc. object
486 void generate_uuid(char *buf) {
489 sprintf(buf, "%s-%lx-%x-%x",
490 serv_info.serv_nodename,