4 * Miscellaneous routines
11 typedef unsigned char byte;
16 static byte dtable[256]; /* base64 encode / decode table */
18 char *safestrncpy(char *dest, const char *src, size_t n)
20 if (dest == NULL || src == NULL) {
23 strncpy(dest, src, n);
31 * num_tokens() - discover number of parameters/tokens in a string
33 int num_tokens(char *source, char tok)
40 for (a = 0; a < strlen(source); ++a) {
48 * extract_token() - a string tokenizer
50 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
53 const char *s; /* source */
59 /* Locate desired parameter */
61 while (count < parmnum) {
62 /* End of string, bail! */
67 if (*s == separator) {
72 if (!s) return; /* Parameter not found */
74 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
83 * remove_token() - a tokenizer that kills, maims, and destroys
85 void remove_token(char *source, int parmnum, char separator)
97 if (strlen(source) == 0) {
101 for (i = 0; i < strlen(source); ++i) {
102 if ((start < 0) && (curr_parm == parmnum)) {
106 if ((end < 0) && (curr_parm == (parmnum + 1))) {
110 if (source[i] == separator) {
116 end = strlen(source);
118 strcpy(&source[start], &source[end]);
125 * extract_int() - extract an int parm w/o supplying a buffer
127 int extract_int(const char *source, int parmnum)
131 extract_token(buf, source, parmnum, '|', sizeof buf);
136 * extract_long() - extract an long parm w/o supplying a buffer
138 long extract_long(const char *source, int parmnum)
142 extract_token(buf, source, parmnum, '|', sizeof buf);
152 * check for the presence of a character within a string (returns count)
160 for (a = 0; a < strlen(st); ++a)
168 * Utility function to "readline" from memory
169 * (returns new pointer)
171 char *memreadline(char *start, char *buf, int maxlen)
175 int len = 0; /* tally our own length to avoid strlen() delays */
178 memset(buf, 0, maxlen);
182 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
183 buf[strlen(buf) + 1] = 0;
184 buf[strlen(buf)] = ch;
187 if ((ch == 10) || (ch == 0)) {
196 * pattern2() - searches for patn within search string, returns pos
198 int pattern2(char *search, char *patn)
201 for (a = 0; a < strlen(search); ++a) {
202 if (!strncasecmp(&search[a], patn, strlen(patn)))
210 * Strip leading and trailing spaces from a string
212 void striplt(char *buf)
214 if (strlen(buf) == 0) return;
215 while ((strlen(buf) > 0) && (isspace(buf[0])))
216 strcpy(buf, &buf[1]);
217 if (strlen(buf) == 0) return;
218 while (isspace(buf[strlen(buf) - 1]))
219 buf[strlen(buf) - 1] = 0;
224 * Determine whether the specified message number is contained within the
227 int is_msg_in_mset(char *mset, long msgnum) {
230 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
234 * Now set it for all specified messages.
236 num_sets = num_tokens(mset, ',');
237 for (s=0; s<num_sets; ++s) {
238 extract_token(setstr, mset, s, ',', sizeof setstr);
240 extract_token(lostr, setstr, 0, ':', sizeof lostr);
241 if (num_tokens(setstr, ':') >= 2) {
242 extract_token(histr, setstr, 1, ':', sizeof histr);
243 if (!strcmp(histr, "*")) {
244 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
248 strcpy(histr, lostr);
253 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
262 * Strip a boundarized substring out of a string (for example, remove
263 * parentheses and anything inside them).
265 * This improved version can strip out *multiple* boundarized substrings.
267 void stripout(char *str, char leftboundary, char rightboundary)
277 for (a = 0; a < strlen(str); ++a) {
278 if (str[a] == leftboundary)
280 if (str[a] == rightboundary)
284 if ((lb > 0) && (rb > lb)) {
285 strcpy(&str[lb - 1], &str[rb + 1]);
288 } while ((lb > 0) && (rb > lb));
295 * Replacement for sleep() that uses select() in order to avoid SIGALRM
297 void sleeeeeeeeeep(int seconds)
303 select(0, NULL, NULL, NULL, &tv);
309 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
310 * John Walker, copied over from the Citadel server.
313 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
315 int i, hiteof = FALSE;
320 /* Fill dtable with character encodings. */
322 for (i = 0; i < 26; i++) {
324 dtable[26 + i] = 'a' + i;
326 for (i = 0; i < 10; i++) {
327 dtable[52 + i] = '0' + i;
333 byte igroup[3], ogroup[4];
336 igroup[0] = igroup[1] = igroup[2] = 0;
337 for (n = 0; n < 3; n++) {
338 if (spos >= sourcelen) {
343 igroup[n] = (byte) c;
346 ogroup[0] = dtable[igroup[0] >> 2];
348 dtable[((igroup[0] & 3) << 4) |
351 dtable[((igroup[1] & 0xF) << 2) |
353 ogroup[3] = dtable[igroup[2] & 0x3F];
355 /* Replace characters in output stream with "=" pad
356 characters if fewer than three characters were
357 read from the end of the input stream. */
365 for (i = 0; i < 4; i++) {
366 dest[dpos++] = ogroup[i];
388 * Convert base64-encoded to binary. Returns the length of the decoded data.
389 * It will stop after reading 'length' bytes.
391 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
397 for (i = 0; i < 255; i++) {
400 for (i = 'A'; i <= 'Z'; i++) {
401 dtable[i] = 0 + (i - 'A');
403 for (i = 'a'; i <= 'z'; i++) {
404 dtable[i] = 26 + (i - 'a');
406 for (i = '0'; i <= '9'; i++) {
407 dtable[i] = 52 + (i - '0');
413 /*CONSTANTCONDITION*/ while (TRUE) {
414 byte a[4], b[4], o[3];
416 for (i = 0; i < 4; i++) {
417 if (spos >= length) {
428 if (dtable[c] & 0x80) {
429 /* Ignoring errors: discard invalid character */
434 b[i] = (byte) dtable[c];
436 o[0] = (b[0] << 2) | (b[1] >> 4);
437 o[1] = (b[1] << 4) | (b[2] >> 2);
438 o[2] = (b[2] << 6) | b[3];
439 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
456 * Generate a new, globally unique UID parameter for a calendar etc. object
458 void generate_uuid(char *buf) {
461 sprintf(buf, "%s-%lx-%x-%x",
462 serv_info.serv_nodename,
471 * Local replacement for controversial C library function that generates
472 * names for temporary files. Included to shut up compiler warnings.
474 void CtdlMakeTempFileName(char *name, int len) {
477 while (i++, i < 100) {
478 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
482 if (!access(name, F_OK)) {