1 // A basic toolset containing miscellaneous functions for string manipluation,
2 // encoding/decoding, and a bunch of other stuff.
4 // Copyright (c) 1987-2022 by the citadel.org team
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
13 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
31 #include "libcitadel.h"
37 typedef unsigned char byte; // Byte type
39 // copy a string into a buffer of a known size. abort if we exceed the limits
41 // dest the targetbuffer
42 // src the source string
45 // returns the number of characters copied if dest is big enough, -n if not.
46 int safestrncpy(char *dest, const char *src, size_t n) {
49 if (dest == NULL || src == NULL)
51 fprintf(stderr, "safestrncpy: NULL argument\n");
57 if (dest[i] == 0) return i;
65 // num_tokens() - discover number of parameters/tokens in a string
66 int num_tokens(const char *source, char tok) {
68 const char *ptr = source;
74 while (*ptr != '\0') {
84 // extract_token() - a string tokenizer
85 // returns -1 if not found, or length of token.
86 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen) {
87 const char *s; // source
88 int len = 0; // running total length of extracted string
89 int current_token = 0; // token currently being processed
106 if (*s == separator) {
109 if ( (current_token == parmnum) && (*s != separator) && (len < maxlen) ) {
113 else if ((current_token > parmnum) || (len >= maxlen)) {
120 if (current_token < parmnum) {
127 // remove_token() - a tokenizer that kills, maims, and destroys
128 void remove_token(char *source, int parmnum, char separator) {
129 char *d, *s; // dest, source
132 // Find desired parameter
134 while (count < parmnum) {
135 // End of string, bail!
140 if (*d == separator) {
145 if (!d) return; // Parameter not found
147 // Find next parameter
149 while (*s && *s != separator) {
156 else if (d == source)
163 // extract_int() - extract an int parm without supplying a buffer
164 int extract_int(const char *source, int parmnum) {
167 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
174 // extract_long() - extract an long parm without supplying a buffer
175 long extract_long(const char *source, int parmnum) {
178 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
185 // extract_unsigned_long() - extract an unsigned long parm
186 unsigned long extract_unsigned_long(const char *source, int parmnum) {
189 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
190 return strtoul(buf, NULL, 10);
196 // if we send out non ascii subjects, we encode it this way.
197 char *rfc2047encode(const char *line, long length) {
198 const char *AlreadyEncoded;
201 #define UTF8_HEADER "=?UTF-8?B?"
203 // check if we're already done
204 AlreadyEncoded = strstr(line, "=?");
205 if ((AlreadyEncoded != NULL) && ((strstr(AlreadyEncoded, "?B?") != NULL)|| (strstr(AlreadyEncoded, "?Q?") != NULL))) {
209 result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
210 strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
211 CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, BASE64_NO_LINEBREAKS);
212 end = strlen (result);
219 // removes double slashes from pathnames
220 // allows / disallows trailing slashes
221 void StripSlashes(char *Dir, int TrailingSlash) {
226 while (!IsEmptyStr(a)) {
238 if ((TrailingSlash) && (*(b - 1) != '/')){
247 // Trim leading and trailing whitespace from a string
248 size_t string_trim(char *buf) {
249 char *first_nonspace = NULL;
250 char *last_nonspace = NULL;
254 if ((buf == NULL) || (*buf == '\0')) {
258 for (ptr=buf; *ptr!=0; ++ptr) {
259 if (!isspace(*ptr)) {
260 if (!first_nonspace) {
261 first_nonspace = ptr;
267 if ((!first_nonspace) || (!last_nonspace)) {
272 new_len = last_nonspace - first_nonspace + 1;
273 memmove(buf, first_nonspace, new_len);
279 // check for the presence of a character within a string (returns count)
280 // st the string to examine
281 // ch the char to search
282 // returns the number of times ch appears in st
283 int haschar(const char *st, int ch) {
288 while (!IsEmptyStr(ptr))
298 // Determine whether the specified message number is contained within the specified sequence set.
299 int is_msg_in_sequence_set(const char *mset, long msgnum) {
302 char setstr[128], lostr[128], histr[128];
305 num_sets = num_tokens(mset, ',');
306 for (s=0; s<num_sets; ++s) {
307 extract_token(setstr, mset, s, ',', sizeof setstr);
309 extract_token(lostr, setstr, 0, ':', sizeof lostr);
310 if (num_tokens(setstr, ':') >= 2) {
311 extract_token(histr, setstr, 1, ':', sizeof histr);
312 if (!strcmp(histr, "*")) {
313 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
317 strcpy(histr, lostr);
322 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
328 // Utility function to "readline" from memory
329 // start Location in memory from which we are reading.
330 // buf the buffer to place the string in.
331 // maxlen Size of string buffer
332 // returns pointer to the source memory right after we stopped reading.
333 char *memreadline(char *start, char *buf, int maxlen) {
336 int len = 0; // tally our own length to avoid strlen() delays
342 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
345 if ((ch == 10) || (ch == 0)) {
353 // Utility function to "readline" from memory
354 // start Location in memory from which we are reading.
355 // buf the buffer to place the string in.
356 // maxlen Size of string buffer
357 // retlen the length of the returned string
358 // returns a pointer to the source memory right after we stopped reading.
359 char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen) {
362 int len = 0; // tally our own length to avoid strlen() delays
368 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
371 if ((ch == 10) || (ch == 0)) {
380 // Utility function to "readline" from memory
381 // start Location in memory from which we are reading.
382 // buf the buffer to place the string in.
383 // maxlen Size of string buffer
384 // return Pointer to the source memory right after we stopped reading.
385 const char *cmemreadline(const char *start, char *buf, int maxlen) {
388 int len = 0; // tally our own length to avoid strlen() delays
394 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
397 if ((ch == 10) || (ch == 0)) {
405 // Utility function to "readline" from memory
406 // start Location in memory from which we are reading.
407 // buf the buffer to place the string in.
408 // maxlen Size of string buffer
409 // retlen the length of the returned string
410 // return Pointer to the source memory right after we stopped reading.
411 const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retlen) {
414 int len = 0; // tally our own length to avoid strlen() delays
420 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
423 if ((ch == 10) || (ch == 0)) {
432 // Strip a boundarized substring out of a string (for example, remove parentheses and anything inside them).
433 int stripout(char *str, char leftboundary, char rightboundary) {
441 for (int a = 0; str[a]; ++a) {
442 if ((lb==-1) && (str[a] == leftboundary)) {
444 } else if (str[a] == rightboundary) {
449 if ((lb==-1) || (rb <= lb)) {
453 strcpy(str + lb, str + rb + 1);
457 // Reduce a string down to a boundarized substring (for example, remove
458 // parentheses and anything outside them).
459 long stripallbut(char *str, char leftboundary, char rightboundary) {
468 while (str[orig_len]) {
469 if ((lb==-1) && (str[orig_len] == leftboundary)) {
471 } else if (str[orig_len] == rightboundary) {
477 if ((lb==-1) || (rb <= lb)) {
483 long new_len = rb - lb - 1;
484 memmove(str, str + lb + 1, new_len);
489 char *myfgets(char *s, int size, FILE *stream) {
490 char *ret = fgets(s, size, stream);
494 nl = strchr(s, '\n');
504 // Escape a string for feeding out as a URL.
505 // outbuf the output buffer
506 // oblen the size of outbuf to sanitize
507 // strbuf the input buffer
508 void urlesc(char *outbuf, size_t oblen, char *strbuf) {
509 int a, b, c, len, eclen, olen;
510 char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
513 len = strlen(strbuf);
516 for (a = 0; a < len; ++a) {
518 for (b = 0; b < eclen; ++b) {
519 if (strbuf[a] == ec[b])
523 snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
527 outbuf[olen ++] = strbuf[a];
533 // In our world, we want strcpy() to be able to work with overlapping strings.
537 char *strcpy(char *dest, const char *src) {
538 memmove(dest, src, (strlen(src) + 1) );
543 // Generate a new, globally unique UID parameter for a calendar etc. object
544 void generate_uuid(char *buf) {
545 static int seq = (-1);
546 static int no_kernel_uuid = 0;
548 // If we are running on Linux then we have a kernelspace uuid generator available
550 if (no_kernel_uuid == 0) {
552 fp = fopen("/proc/sys/kernel/random/uuid", "rb");
555 rv = fread(buf, 36, 1, fp);
564 // If the kernel didn't provide us with a uuid, we generate a pseudo-random one
572 seq = (seq % 0x0FFF) ;
574 sprintf(buf, "%08lx-%04lx-4%03x-a%03x-%012lx",
584 // bmstrcasestr() -- case-insensitive substring search
586 // This uses the Boyer-Moore search algorithm and is therefore quite fast.
587 // The code is roughly based on the strstr() replacement from 'tin' written
589 inline static char *_bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
591 register unsigned char *p, *t;
592 register int i, j, *delta;
596 if (!text) return(NULL);
597 if (!pattern) return(NULL);
599 // algorithm fails if pattern is empty
600 if ((p1 = patlen) == 0)
603 // code below fails (whenever i is unsigned) if pattern too long
609 for (i = 0; i <= 255; i++)
611 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
612 delta[tolower(*p++)] = i;
614 // From now on, we want patlen - 1.
615 // In the loop below, p points to the end of the pattern,
616 // t points to the end of the text to be tested against the
617 // pattern, and i counts the amount of text remaining, not
618 // including the part to be tested.
620 p = (unsigned char *) pattern + p1;
621 t = (unsigned char *) text + p1;
622 i = textlen - patlen;
624 if (tolower(p[0]) == tolower(t[0])) {
625 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
626 return ((char *)t - p1);
629 j = delta[tolower(t[0])];
640 * bmstrcasestr() -- case-insensitive substring search
642 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
643 * The code is roughly based on the strstr() replacement from 'tin' written
646 char *bmstrcasestr(char *text, const char *pattern) {
650 if (!text) return(NULL);
651 if (!pattern) return(NULL);
653 textlen = strlen (text);
654 patlen = strlen (pattern);
656 return _bmstrcasestr_len(text, textlen, pattern, patlen);
659 char *bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
660 return _bmstrcasestr_len(text, textlen, pattern, patlen);
665 * bmstrcasestr() -- case-insensitive substring search
667 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
668 * The code is roughly based on the strstr() replacement from 'tin' written
671 inline static const char *_cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
673 register unsigned char *p, *t;
674 register int i, j, *delta;
678 if (!text) return(NULL);
679 if (!pattern) return(NULL);
681 /* algorithm fails if pattern is empty */
682 if ((p1 = patlen) == 0)
685 /* code below fails (whenever i is unsigned) if pattern too long */
691 for (i = 0; i <= 255; i++)
693 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
694 delta[tolower(*p++)] = i;
697 * From now on, we want patlen - 1.
698 * In the loop below, p points to the end of the pattern,
699 * t points to the end of the text to be tested against the
700 * pattern, and i counts the amount of text remaining, not
701 * including the part to be tested.
704 p = (unsigned char *) pattern + p1;
705 t = (unsigned char *) text + p1;
706 i = textlen - patlen;
708 if (tolower(p[0]) == tolower(t[0])) {
709 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
710 return ((char *)t - p1);
713 j = delta[tolower(t[0])];
724 * bmstrcasestr() -- case-insensitive substring search
726 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
727 * The code is roughly based on the strstr() replacement from 'tin' written
730 const char *cbmstrcasestr(const char *text, const char *pattern) {
734 if (!text) return(NULL);
735 if (!pattern) return(NULL);
737 textlen = strlen (text);
738 patlen = strlen (pattern);
740 return _cbmstrcasestr_len(text, textlen, pattern, patlen);
744 const char *cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
745 return _cbmstrcasestr_len(text, textlen, pattern, patlen);
750 * Local replacement for controversial C library function that generates
751 * names for temporary files. Included to shut up compiler warnings.
753 void CtdlMakeTempFileName(char *name, int len) {
756 while (i++, i < 100) {
757 snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
761 if (!access(name, F_OK)) {
769 * Determine whether the specified message number is contained within the specified set.
770 * Returns nonzero if the specified message number is in the specified message set string.
772 int is_msg_in_mset(const char *mset, long msgnum) {
775 char setstr[SIZ], lostr[SIZ], histr[SIZ];
778 // Now set it for all specified messages.
779 num_sets = num_tokens(mset, ',');
780 for (s=0; s<num_sets; ++s) {
781 extract_token(setstr, mset, s, ',', sizeof setstr);
783 extract_token(lostr, setstr, 0, ':', sizeof lostr);
784 if (num_tokens(setstr, ':') >= 2) {
785 extract_token(histr, setstr, 1, ':', sizeof histr);
786 if (!strcmp(histr, "*")) {
787 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
791 strcpy(histr, lostr);
796 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
803 // searches for a pattern within a search string
804 // returns position in string
805 int pattern2(char *search, char *patn) {
808 len = strlen (search);
809 plen = strlen (patn);
810 for (a = 0; a < len; ++a) {
811 if (!strncasecmp(&search[a], patn, plen))
819 * Convert all whitespace characters in a supplied string to underscores
821 void convert_spaces_to_underscores(char *str) {
828 for (i=0; i<len; ++i) {
829 if (isspace(str[i])) {
837 * check whether the provided string needs to be qp encoded or not
839 int CheckEncode(const char *pch, long len, const char *pche) {
843 if (((unsigned char) *pch < 32) ||
844 ((unsigned char) *pch > 126)) {