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;
35 static byte dtable[256]; /* base64 encode / decode table */
37 char *safestrncpy(char *dest, const char *src, size_t n)
39 if (dest == NULL || src == NULL) {
42 strncpy(dest, src, n);
50 * num_tokens() - discover number of parameters/tokens in a string
52 int num_tokens(char *source, char tok)
59 for (a = 0; a < strlen(source); ++a) {
67 * extract_token() - a string tokenizer
69 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
72 const char *s; /* source */
78 /* Locate desired parameter */
80 while (count < parmnum) {
81 /* End of string, bail! */
86 if (*s == separator) {
91 if (!s) return; /* Parameter not found */
93 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
102 * remove_token() - a tokenizer that kills, maims, and destroys
104 void remove_token(char *source, int parmnum, char separator)
116 if (strlen(source) == 0) {
120 for (i = 0; i < strlen(source); ++i) {
121 if ((start < 0) && (curr_parm == parmnum)) {
125 if ((end < 0) && (curr_parm == (parmnum + 1))) {
129 if (source[i] == separator) {
135 end = strlen(source);
137 strcpy(&source[start], &source[end]);
144 * extract_int() - extract an int parm w/o supplying a buffer
146 int extract_int(const char *source, int parmnum)
150 extract_token(buf, source, parmnum, '|', sizeof buf);
155 * extract_long() - extract an long parm w/o supplying a buffer
157 long extract_long(const char *source, int parmnum)
161 extract_token(buf, source, parmnum, '|', sizeof buf);
171 * check for the presence of a character within a string (returns count)
179 for (a = 0; a < strlen(st); ++a)
187 * Utility function to "readline" from memory
188 * (returns new pointer)
190 char *memreadline(char *start, char *buf, int maxlen)
194 int len = 0; /* tally our own length to avoid strlen() delays */
197 memset(buf, 0, maxlen);
201 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
202 buf[strlen(buf) + 1] = 0;
203 buf[strlen(buf)] = ch;
206 if ((ch == 10) || (ch == 0)) {
215 * pattern2() - searches for patn within search string, returns pos
217 int pattern2(char *search, char *patn)
220 for (a = 0; a < strlen(search); ++a) {
221 if (!strncasecmp(&search[a], patn, strlen(patn)))
229 * Strip leading and trailing spaces from a string
231 void striplt(char *buf)
233 if (strlen(buf) == 0) return;
234 while ((strlen(buf) > 0) && (isspace(buf[0])))
235 strcpy(buf, &buf[1]);
236 if (strlen(buf) == 0) return;
237 while (isspace(buf[strlen(buf) - 1]))
238 buf[strlen(buf) - 1] = 0;
243 * Determine whether the specified message number is contained within the
246 int is_msg_in_mset(char *mset, long msgnum) {
249 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
253 * Now set it for all specified messages.
255 num_sets = num_tokens(mset, ',');
256 for (s=0; s<num_sets; ++s) {
257 extract_token(setstr, mset, s, ',', sizeof setstr);
259 extract_token(lostr, setstr, 0, ':', sizeof lostr);
260 if (num_tokens(setstr, ':') >= 2) {
261 extract_token(histr, setstr, 1, ':', sizeof histr);
262 if (!strcmp(histr, "*")) {
263 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
267 strcpy(histr, lostr);
272 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
281 * Strip a boundarized substring out of a string (for example, remove
282 * parentheses and anything inside them).
284 * This improved version can strip out *multiple* boundarized substrings.
286 void stripout(char *str, char leftboundary, char rightboundary)
296 for (a = 0; a < strlen(str); ++a) {
297 if (str[a] == leftboundary)
299 if (str[a] == rightboundary)
303 if ((lb > 0) && (rb > lb)) {
304 strcpy(&str[lb - 1], &str[rb + 1]);
307 } while ((lb > 0) && (rb > lb));
314 * Replacement for sleep() that uses select() in order to avoid SIGALRM
316 void sleeeeeeeeeep(int seconds)
322 select(0, NULL, NULL, NULL, &tv);
328 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
329 * John Walker, copied over from the Citadel server.
332 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
334 int i, hiteof = FALSE;
339 /* Fill dtable with character encodings. */
341 for (i = 0; i < 26; i++) {
343 dtable[26 + i] = 'a' + i;
345 for (i = 0; i < 10; i++) {
346 dtable[52 + i] = '0' + i;
352 byte igroup[3], ogroup[4];
355 igroup[0] = igroup[1] = igroup[2] = 0;
356 for (n = 0; n < 3; n++) {
357 if (spos >= sourcelen) {
362 igroup[n] = (byte) c;
365 ogroup[0] = dtable[igroup[0] >> 2];
367 dtable[((igroup[0] & 3) << 4) |
370 dtable[((igroup[1] & 0xF) << 2) |
372 ogroup[3] = dtable[igroup[2] & 0x3F];
374 /* Replace characters in output stream with "=" pad
375 characters if fewer than three characters were
376 read from the end of the input stream. */
384 for (i = 0; i < 4; i++) {
385 dest[dpos++] = ogroup[i];
407 * Convert base64-encoded to binary. Returns the length of the decoded data.
408 * It will stop after reading 'length' bytes.
410 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
416 for (i = 0; i < 255; i++) {
419 for (i = 'A'; i <= 'Z'; i++) {
420 dtable[i] = 0 + (i - 'A');
422 for (i = 'a'; i <= 'z'; i++) {
423 dtable[i] = 26 + (i - 'a');
425 for (i = '0'; i <= '9'; i++) {
426 dtable[i] = 52 + (i - '0');
432 /*CONSTANTCONDITION*/ while (TRUE) {
433 byte a[4], b[4], o[3];
435 for (i = 0; i < 4; i++) {
436 if (spos >= length) {
447 if (dtable[c] & 0x80) {
448 /* Ignoring errors: discard invalid character */
453 b[i] = (byte) dtable[c];
455 o[0] = (b[0] << 2) | (b[1] >> 4);
456 o[1] = (b[1] << 4) | (b[2] >> 2);
457 o[2] = (b[2] << 6) | b[3];
458 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
475 * Generate a new, globally unique UID parameter for a calendar etc. object
477 void generate_uuid(char *buf) {
480 sprintf(buf, "%s-%lx-%x-%x",
481 serv_info.serv_nodename,