2 * A basic toolset containing miscellaneous functions for string manipluation,
3 * encoding/decoding, and a bunch of other stuff.
11 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
29 #include "libcitadel.h"
35 typedef unsigned char byte; /* Byte type */
36 static byte dtable[256] = "\0"; /* base64 decode table */
37 static byte etable[256] = "\0"; /* base64 encode table */
39 char *safestrncpy(char *dest, const char *src, size_t n)
43 if (dest == NULL || src == NULL) {
44 fprintf(stderr, "safestrncpy: NULL argument\n");
50 if (dest[i] == 0) return(dest);
60 * num_tokens() - discover number of parameters/tokens in a string
62 int num_tokens(const char *source, char tok)
65 const char *ptr = source;
71 while (*ptr != '\0') {
80 //extern void cit_backtrace(void);
84 * extract_token() - a string tokenizer
85 * returns -1 if not found, or length of token.
87 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
89 const char *s; //* source * /
90 int len = 0; //* running total length of extracted string * /
91 int current_token = 0; //* token currently being processed * /
100 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
110 if (*s == separator) {
113 if ( (current_token == parmnum) &&
119 else if ((current_token > parmnum) || (len >= maxlen)) {
126 if (current_token < parmnum) {
127 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
130 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
137 * extract_token() - a string tokenizer
139 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
142 const char *s; // source
148 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
151 // Locate desired parameter
153 while (count < parmnum) {
154 // End of string, bail!
159 if (*s == separator) {
165 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
166 return -1; // Parameter not found
169 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
173 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
180 * remove_token() - a tokenizer that kills, maims, and destroys
182 void remove_token(char *source, int parmnum, char separator)
184 char *d, *s; /* dest, source */
187 /* Find desired parameter */
189 while (count < parmnum) {
190 /* End of string, bail! */
195 if (*d == separator) {
200 if (!d) return; /* Parameter not found */
202 /* Find next parameter */
204 while (*s && *s != separator) {
211 else if (d == source)
225 * extract_int() - extract an int parm w/o supplying a buffer
227 int extract_int(const char *source, int parmnum)
231 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
238 * extract_long() - extract an long parm w/o supplying a buffer
240 long extract_long(const char *source, int parmnum)
244 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
252 * extract_unsigned_long() - extract an unsigned long parm
254 unsigned long extract_unsigned_long(const char *source, int parmnum)
258 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
259 return strtoul(buf, NULL, 10);
266 void CtdlInitBase64Table(void)
269 /* Fill dtable with character encodings. */
272 for (i = 0; i < 26; i++) {
274 etable[26 + i] = 'a' + i;
276 for (i = 0; i < 10; i++) {
277 etable[52 + i] = '0' + i;
283 for (i = 0; i < 255; i++) {
286 for (i = 'A'; i <= 'Z'; i++) {
287 dtable[i] = 0 + (i - 'A');
289 for (i = 'a'; i <= 'z'; i++) {
290 dtable[i] = 26 + (i - 'a');
292 for (i = '0'; i <= '9'; i++) {
293 dtable[i] = 52 + (i - '0');
302 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by John Walker.
305 size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
307 int i, hiteof = FALSE;
312 /** Fill dtable with character encodings. */
314 for (i = 0; i < 26; i++) {
316 dtable[26 + i] = 'a' + i;
318 for (i = 0; i < 10; i++) {
319 dtable[52 + i] = '0' + i;
325 byte igroup[3], ogroup[4];
328 igroup[0] = igroup[1] = igroup[2] = 0;
329 for (n = 0; n < 3; n++) {
330 if (spos >= sourcelen) {
335 igroup[n] = (byte) c;
338 ogroup[0] = dtable[igroup[0] >> 2];
340 dtable[((igroup[0] & 3) << 4) |
343 dtable[((igroup[1] & 0xF) << 2) |
345 ogroup[3] = dtable[igroup[2] & 0x3F];
348 * Replace characters in output stream with "=" pad
349 * characters if fewer than three characters were
350 * read from the end of the input stream.
359 for (i = 0; i < 4; i++) {
360 dest[dpos++] = ogroup[i];
364 if ( (linebreaks) && (thisline > 70) ) {
372 if ( (linebreaks) && (thisline > 70) ) {
385 * Convert base64-encoded to binary. Returns the length of the decoded data.
386 * It will stop after reading 'length' bytes.
388 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
395 /*CONSTANTCONDITION*/
397 byte a[4], b[4], o[3];
399 for (i = 0; i < 4; i++) {
400 if (spos >= length) {
411 if (dtable[c] & 0x80) {
412 /* Ignoring errors: discard invalid character. */
417 b[i] = (byte) dtable[c];
419 o[0] = (b[0] << 2) | (b[1] >> 4);
420 o[1] = (b[1] << 4) | (b[2] >> 2);
421 o[2] = (b[2] << 6) | b[3];
422 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
423 if (i>=1) dest[dpos++] = o[0];
424 if (i>=2) dest[dpos++] = o[1];
425 if (i>=3) dest[dpos++] = o[2];
435 * if we send out non ascii subjects, we encode it this way.
437 char *rfc2047encode(char *line, long length)
439 char *AlreadyEncoded;
442 #define UTF8_HEADER "=?UTF-8?B?"
444 /* check if we're already done */
445 AlreadyEncoded = strstr(line, "=?");
446 if ((AlreadyEncoded != NULL) &&
447 ((strstr(AlreadyEncoded, "?B?") != NULL)||
448 (strstr(AlreadyEncoded, "?Q?") != NULL)))
453 result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
454 strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
455 CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
456 end = strlen (result);
465 * Strip leading and trailing spaces from a string
467 void striplt(char *buf)
472 if (buf==NULL) return;
473 if (IsEmptyStr(buf)) return;
475 while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
477 if (IsEmptyStr(buf)) return;
479 while ((!IsEmptyStr(buf)) && (isspace(buf[a])))
482 memmove(buf, &buf[a], len - a + 1);
490 * \brief check for the presence of a character within a string (returns count)
491 * \param st the string to examine
492 * \param ch the char to search
493 * \return the position inside of st
495 int haschar(const char *st,int ch)
501 while (!IsEmptyStr(ptr))
515 * Format a date/time stamp for output
516 * seconds is whether to print the seconds
518 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
522 /* Month strings for date conversions ... this needs to be localized eventually */
523 char *fmt_date_months[12] = {
524 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
528 localtime_r(&thetime, &tm);
531 if (hour == 0) hour = 12;
532 else if (hour > 12) hour = hour - 12;
535 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
536 fmt_date_months[tm.tm_mon],
542 ( (tm.tm_hour >= 12) ? "pm" : "am" )
545 snprintf(buf, n, "%s %d %4d %d:%02d%s",
546 fmt_date_months[tm.tm_mon],
551 ( (tm.tm_hour >= 12) ? "pm" : "am" )
559 * Determine whether the specified message number is contained within the
560 * specified sequence set.
562 int is_msg_in_sequence_set(char *mset, long msgnum) {
565 char setstr[128], lostr[128], histr[128];
568 num_sets = num_tokens(mset, ',');
569 for (s=0; s<num_sets; ++s) {
570 extract_token(setstr, mset, s, ',', sizeof setstr);
572 extract_token(lostr, setstr, 0, ':', sizeof lostr);
573 if (num_tokens(setstr, ':') >= 2) {
574 extract_token(histr, setstr, 1, ':', sizeof histr);
575 if (!strcmp(histr, "*")) {
576 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
580 strcpy(histr, lostr);
585 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
592 * \brief Utility function to "readline" from memory
593 * \param start Location in memory from which we are reading.
594 * \param buf the buffer to place the string in.
595 * \param maxlen Size of string buffer
596 * \return Pointer to the source memory right after we stopped reading.
598 char *memreadline(char *start, char *buf, int maxlen)
602 int len = 0; /**< tally our own length to avoid strlen() delays */
608 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
611 if ((ch == 10) || (ch == 0)) {
620 * \brief Utility function to "readline" from memory
621 * \param start Location in memory from which we are reading.
622 * \param buf the buffer to place the string in.
623 * \param maxlen Size of string buffer
624 * \param retlen the length of the returned string
625 * \return Pointer to the source memory right after we stopped reading.
627 char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen)
631 int len = 0; /**< tally our own length to avoid strlen() delays */
637 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
640 if ((ch == 10) || (ch == 0)) {
652 * Strip a boundarized substring out of a string (for example, remove
653 * parentheses and anything inside them).
655 void stripout(char *str, char leftboundary, char rightboundary) {
660 for (a = 0; a < strlen(str); ++a) {
661 if (str[a] == leftboundary) lb = a;
662 if (str[a] == rightboundary) rb = a;
665 if ( (lb > 0) && (rb > lb) ) {
666 strcpy(&str[lb - 1], &str[rb + 1]);
669 else if ( (lb == 0) && (rb > lb) ) {
670 strcpy(str, &str[rb + 1]);
677 * Reduce a string down to a boundarized substring (for example, remove
678 * parentheses and anything outside them).
680 void stripallbut(char *str, char leftboundary, char rightboundary) {
683 for (a = 0; a < strlen(str); ++ a) {
684 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
687 for (a = 0; a < strlen(str); ++ a) {
688 if (str[a] == rightboundary) str[a] = 0;
693 char *myfgets(char *s, int size, FILE *stream) {
694 char *ret = fgets(s, size, stream);
698 nl = strchr(s, '\n');
708 * \brief Escape a string for feeding out as a URL.
709 * \param outbuf the output buffer
710 * \param oblen the size of outbuf to sanitize
711 * \param strbuf the input buffer
713 void urlesc(char *outbuf, size_t oblen, char *strbuf)
715 int a, b, c, len, eclen, olen;
716 char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
719 len = strlen(strbuf);
722 for (a = 0; a < len; ++a) {
724 for (b = 0; b < eclen; ++b) {
725 if (strbuf[a] == ec[b])
729 snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
733 outbuf[olen ++] = strbuf[a];
741 * In our world, we want strcpy() to be able to work with overlapping strings.
746 char *strcpy(char *dest, const char *src) {
747 memmove(dest, src, (strlen(src) + 1) );
753 * Generate a new, globally unique UID parameter for a calendar etc. object
755 void generate_uuid(char *buf) {
758 sprintf(buf, "%lx-%lx-%x",
766 * bmstrcasestr() -- case-insensitive substring search
768 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
769 * The code is roughly based on the strstr() replacement from 'tin' written
772 char *bmstrcasestr(char *text, char *pattern) {
774 register unsigned char *p, *t;
775 register int i, j, *delta;
781 textlen = strlen (text);
782 patlen = strlen (pattern);
784 /* algorithm fails if pattern is empty */
785 if ((p1 = patlen) == 0)
788 /* code below fails (whenever i is unsigned) if pattern too long */
794 for (i = 0; i <= 255; i++)
796 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
797 delta[tolower(*p++)] = i;
800 * From now on, we want patlen - 1.
801 * In the loop below, p points to the end of the pattern,
802 * t points to the end of the text to be tested against the
803 * pattern, and i counts the amount of text remaining, not
804 * including the part to be tested.
807 p = (unsigned char *) pattern + p1;
808 t = (unsigned char *) text + p1;
809 i = textlen - patlen;
811 if (tolower(p[0]) == tolower(t[0])) {
812 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
813 return ((char *)t - p1);
816 j = delta[tolower(t[0])];
828 * Local replacement for controversial C library function that generates
829 * names for temporary files. Included to shut up compiler warnings.
831 void CtdlMakeTempFileName(char *name, int len) {
834 while (i++, i < 100) {
835 snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
839 if (!access(name, F_OK)) {
848 * Determine whether the specified message number is contained within the specified set.
849 * Returns nonzero if the specified message number is in the specified message set string.
851 int is_msg_in_mset(char *mset, long msgnum) {
854 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
858 * Now set it for all specified messages.
860 num_sets = num_tokens(mset, ',');
861 for (s=0; s<num_sets; ++s) {
862 extract_token(setstr, mset, s, ',', sizeof setstr);
864 extract_token(lostr, setstr, 0, ':', sizeof lostr);
865 if (num_tokens(setstr, ':') >= 2) {
866 extract_token(histr, setstr, 1, ':', sizeof histr);
867 if (!strcmp(histr, "*")) {
868 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
872 strcpy(histr, lostr);
877 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
885 * \brief searches for a paternn within asearch string
886 * \param search the string to search
887 * \param patn the pattern to find in string
888 * \returns position in string
890 int pattern2(char *search, char *patn)
894 len = strlen (search);
895 plen = strlen (patn);
896 for (a = 0; a < len; ++a) {
897 if (!strncasecmp(&search[a], patn, plen))
905 * \brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
906 * \param buf the string to modify
907 * \param len length of the string.
909 void stripltlen(char *buf, int *len)
912 if (*len == 0) return;
913 while ((*len > delta) && (isspace(buf[delta]))){
916 memmove (buf, &buf[delta], *len - delta + 1);
919 if (*len == 0) return;
920 while (isspace(buf[(*len) - 1])){
921 buf[--(*len)] = '\0';