2 * A basic toolset containing miscellaneous functions for string manipluation,
3 * encoding/decoding, and a bunch of other stuff.
5 * Copyright (c) 1987-2011 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
45 #include "libcitadel.h"
51 typedef unsigned char byte; /* Byte type */
53 /* Base64 encoding table */
54 const byte etable[256] = {
55 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
56 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103,
57 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
58 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43,
59 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0
70 /* Base64 decoding table */
71 const byte dtable[256] = {
72 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
73 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
74 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
75 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
76 128, 128, 128, 0, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
77 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128,
78 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
79 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128,
80 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
81 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
82 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
83 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
84 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
85 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
86 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
87 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
88 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
93 * copy a string into a buffer of a known size. abort if we exceed the limits
95 * dest the targetbuffer
96 * src the source string
99 * returns the number of characters copied if dest is big enough, -n if not.
101 int safestrncpy(char *dest, const char *src, size_t n)
105 if (dest == NULL || src == NULL) {
106 fprintf(stderr, "safestrncpy: NULL argument\n");
112 if (dest[i] == 0) return i;
122 * num_tokens() - discover number of parameters/tokens in a string
124 int num_tokens(const char *source, char tok)
127 const char *ptr = source;
129 if (source == NULL) {
133 while (*ptr != '\0') {
142 //extern void cit_backtrace(void);
146 * extract_token() - a string tokenizer
147 * returns -1 if not found, or length of token.
149 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
151 const char *s; //* source * /
152 int len = 0; //* running total length of extracted string * /
153 int current_token = 0; //* token currently being processed * /
162 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
172 if (*s == separator) {
175 if ( (current_token == parmnum) &&
181 else if ((current_token > parmnum) || (len >= maxlen)) {
188 if (current_token < parmnum) {
189 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
192 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
199 * extract_token() - a string tokenizer
201 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
204 const char *s; // source
210 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
213 // Locate desired parameter
215 while (count < parmnum) {
216 // End of string, bail!
221 if (*s == separator) {
227 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
228 return -1; // Parameter not found
231 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
235 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
242 * remove_token() - a tokenizer that kills, maims, and destroys
244 void remove_token(char *source, int parmnum, char separator)
246 char *d, *s; /* dest, source */
249 /* Find desired parameter */
251 while (count < parmnum) {
252 /* End of string, bail! */
257 if (*d == separator) {
262 if (!d) return; /* Parameter not found */
264 /* Find next parameter */
266 while (*s && *s != separator) {
273 else if (d == source)
287 * extract_int() - extract an int parm w/o supplying a buffer
289 int extract_int(const char *source, int parmnum)
293 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
300 * extract_long() - extract an long parm w/o supplying a buffer
302 long extract_long(const char *source, int parmnum)
306 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
314 * extract_unsigned_long() - extract an unsigned long parm
316 unsigned long extract_unsigned_long(const char *source, int parmnum)
320 if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
321 return strtoul(buf, NULL, 10);
328 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by John Walker.
331 size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
333 int i, hiteof = FALSE;
339 byte igroup[3], ogroup[4];
342 igroup[0] = igroup[1] = igroup[2] = 0;
343 for (n = 0; n < 3; n++) {
344 if (spos >= sourcelen) {
349 igroup[n] = (byte) c;
352 ogroup[0] = etable[igroup[0] >> 2];
354 etable[((igroup[0] & 3) << 4) |
357 etable[((igroup[1] & 0xF) << 2) |
359 ogroup[3] = etable[igroup[2] & 0x3F];
362 * Replace characters in output stream with "=" pad
363 * characters if fewer than three characters were
364 * read from the end of the input stream.
373 for (i = 0; i < 4; i++) {
374 dest[dpos++] = ogroup[i];
378 if ( (linebreaks) && (thisline > 70) ) {
386 if ( (linebreaks) && (thisline > 70) ) {
399 * Convert base64-encoded to binary. Returns the length of the decoded data.
400 * It will stop after reading 'length' bytes.
402 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
409 byte a[4], b[4], o[3];
411 for (i = 0; i < 4; i++) {
412 if (spos >= length) {
423 if (dtable[c] & 0x80) {
424 /* Ignoring errors: discard invalid character. */
429 b[i] = (byte) dtable[c];
431 o[0] = (b[0] << 2) | (b[1] >> 4);
432 o[1] = (b[1] << 4) | (b[2] >> 2);
433 o[2] = (b[2] << 6) | b[3];
434 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
435 if (i>=1) dest[dpos++] = o[0];
436 if (i>=2) dest[dpos++] = o[1];
437 if (i>=3) dest[dpos++] = o[2];
447 * if we send out non ascii subjects, we encode it this way.
449 char *rfc2047encode(char *line, long length)
451 char *AlreadyEncoded;
454 #define UTF8_HEADER "=?UTF-8?B?"
456 /* check if we're already done */
457 AlreadyEncoded = strstr(line, "=?");
458 if ((AlreadyEncoded != NULL) &&
459 ((strstr(AlreadyEncoded, "?B?") != NULL)||
460 (strstr(AlreadyEncoded, "?Q?") != NULL)))
465 result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
466 strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
467 CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
468 end = strlen (result);
476 * removes double slashes from pathnames
477 * allows / disallows trailing slashes
479 void StripSlashes(char *Dir, int TrailingSlash)
485 while (!IsEmptyStr(a)) {
497 if ((TrailingSlash) && (*(b - 1) != '/')){
506 * Strip leading and trailing spaces from a string
508 size_t striplt(char *buf) {
509 char *first_nonspace = NULL;
510 char *last_nonspace = NULL;
514 if ((buf == NULL) || (*buf == '\0')) {
518 for (ptr=buf; *ptr!=0; ++ptr) {
519 if (!isspace(*ptr)) {
520 if (!first_nonspace) {
521 first_nonspace = ptr;
527 if ((!first_nonspace) || (!last_nonspace)) {
532 new_len = last_nonspace - first_nonspace + 1;
533 memmove(buf, first_nonspace, new_len);
540 * \brief check for the presence of a character within a string (returns count)
541 * \param st the string to examine
542 * \param ch the char to search
543 * \return the number of times ch appears in st
545 int haschar(const char *st, int ch)
551 while (!IsEmptyStr(ptr))
565 * Format a date/time stamp for output
566 * seconds is whether to print the seconds
568 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
572 /* Month strings for date conversions ... this needs to be localized eventually */
573 char *fmt_date_months[12] = {
574 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
578 localtime_r(&thetime, &tm);
581 if (hour == 0) hour = 12;
582 else if (hour > 12) hour = hour - 12;
585 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
586 fmt_date_months[tm.tm_mon],
592 ( (tm.tm_hour >= 12) ? "pm" : "am" )
595 snprintf(buf, n, "%s %d %4d %d:%02d%s",
596 fmt_date_months[tm.tm_mon],
601 ( (tm.tm_hour >= 12) ? "pm" : "am" )
609 * Determine whether the specified message number is contained within the
610 * specified sequence set.
612 int is_msg_in_sequence_set(const char *mset, long msgnum) {
615 char setstr[128], lostr[128], histr[128];
618 num_sets = num_tokens(mset, ',');
619 for (s=0; s<num_sets; ++s) {
620 extract_token(setstr, mset, s, ',', sizeof setstr);
622 extract_token(lostr, setstr, 0, ':', sizeof lostr);
623 if (num_tokens(setstr, ':') >= 2) {
624 extract_token(histr, setstr, 1, ':', sizeof histr);
625 if (!strcmp(histr, "*")) {
626 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
630 strcpy(histr, lostr);
635 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
642 * \brief Utility function to "readline" from memory
643 * \param start Location in memory from which we are reading.
644 * \param buf the buffer to place the string in.
645 * \param maxlen Size of string buffer
646 * \return Pointer to the source memory right after we stopped reading.
648 char *memreadline(char *start, char *buf, int maxlen)
652 int len = 0; /**< tally our own length to avoid strlen() delays */
658 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
661 if ((ch == 10) || (ch == 0)) {
670 * \brief Utility function to "readline" from memory
671 * \param start Location in memory from which we are reading.
672 * \param buf the buffer to place the string in.
673 * \param maxlen Size of string buffer
674 * \param retlen the length of the returned string
675 * \return Pointer to the source memory right after we stopped reading.
677 char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen)
681 int len = 0; /**< tally our own length to avoid strlen() delays */
687 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
690 if ((ch == 10) || (ch == 0)) {
700 * \brief Utility function to "readline" from memory
701 * \param start Location in memory from which we are reading.
702 * \param buf the buffer to place the string in.
703 * \param maxlen Size of string buffer
704 * \return Pointer to the source memory right after we stopped reading.
706 const char *cmemreadline(const char *start, char *buf, int maxlen)
710 int len = 0; /**< tally our own length to avoid strlen() delays */
716 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
719 if ((ch == 10) || (ch == 0)) {
728 * \brief Utility function to "readline" from memory
729 * \param start Location in memory from which we are reading.
730 * \param buf the buffer to place the string in.
731 * \param maxlen Size of string buffer
732 * \param retlen the length of the returned string
733 * \return Pointer to the source memory right after we stopped reading.
735 const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retlen)
739 int len = 0; /**< tally our own length to avoid strlen() delays */
745 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
748 if ((ch == 10) || (ch == 0)) {
760 * Strip a boundarized substring out of a string (for example, remove
761 * parentheses and anything inside them).
763 int stripout(char *str, char leftboundary, char rightboundary) {
768 for (a = 0; a < strlen(str); ++a) {
769 if (str[a] == leftboundary) lb = a;
770 if (str[a] == rightboundary) rb = a;
773 if ( (lb > 0) && (rb > lb) ) {
774 strcpy(&str[lb - 1], &str[rb + 1]);
778 else if ( (lb == 0) && (rb > lb) ) {
779 strcpy(str, &str[rb + 1]);
787 * Reduce a string down to a boundarized substring (for example, remove
788 * parentheses and anything outside them).
790 long stripallbut(char *str, char leftboundary, char rightboundary) {
796 lb = strrchr(str, leftboundary);
799 rb = strchr(str, rightboundary);
800 if ((rb != NULL) && (rb >= lb)) {
803 len = (long)rb - (long)lb;
804 memmove(str, lb, len);
810 return (long)strlen(str);
814 char *myfgets(char *s, int size, FILE *stream) {
815 char *ret = fgets(s, size, stream);
819 nl = strchr(s, '\n');
829 * \brief Escape a string for feeding out as a URL.
830 * \param outbuf the output buffer
831 * \param oblen the size of outbuf to sanitize
832 * \param strbuf the input buffer
834 void urlesc(char *outbuf, size_t oblen, char *strbuf)
836 int a, b, c, len, eclen, olen;
837 char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
840 len = strlen(strbuf);
843 for (a = 0; a < len; ++a) {
845 for (b = 0; b < eclen; ++b) {
846 if (strbuf[a] == ec[b])
850 snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
854 outbuf[olen ++] = strbuf[a];
862 * In our world, we want strcpy() to be able to work with overlapping strings.
867 char *strcpy(char *dest, const char *src) {
868 memmove(dest, src, (strlen(src) + 1) );
874 * Generate a new, globally unique UID parameter for a calendar etc. object
876 void generate_uuid(char *buf) {
877 static int seq = (-1);
878 static int no_kernel_uuid = 0;
880 /* If we are running on Linux then we have a kernelspace uuid generator available */
882 if (no_kernel_uuid == 0) {
884 fp = fopen("/proc/sys/kernel/random/uuid", "rb");
887 rv = fread(buf, 36, 1, fp);
893 /* If the kernel didn't provide us with a uuid, we generate a pseudo-random one */
901 seq = (seq % 0x0FFF) ;
903 sprintf(buf, "%08lx-%04lx-4%03x-a%03x-%012lx",
913 * bmstrcasestr() -- case-insensitive substring search
915 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
916 * The code is roughly based on the strstr() replacement from 'tin' written
919 inline static char *_bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
921 register unsigned char *p, *t;
922 register int i, j, *delta;
926 if (!text) return(NULL);
927 if (!pattern) return(NULL);
929 /* algorithm fails if pattern is empty */
930 if ((p1 = patlen) == 0)
933 /* code below fails (whenever i is unsigned) if pattern too long */
939 for (i = 0; i <= 255; i++)
941 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
942 delta[tolower(*p++)] = i;
945 * From now on, we want patlen - 1.
946 * In the loop below, p points to the end of the pattern,
947 * t points to the end of the text to be tested against the
948 * pattern, and i counts the amount of text remaining, not
949 * including the part to be tested.
952 p = (unsigned char *) pattern + p1;
953 t = (unsigned char *) text + p1;
954 i = textlen - patlen;
956 if (tolower(p[0]) == tolower(t[0])) {
957 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
958 return ((char *)t - p1);
961 j = delta[tolower(t[0])];
971 * bmstrcasestr() -- case-insensitive substring search
973 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
974 * The code is roughly based on the strstr() replacement from 'tin' written
977 char *bmstrcasestr(char *text, const char *pattern) {
981 if (!text) return(NULL);
982 if (!pattern) return(NULL);
984 textlen = strlen (text);
985 patlen = strlen (pattern);
987 return _bmstrcasestr_len(text, textlen, pattern, patlen);
990 char *bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
991 return _bmstrcasestr_len(text, textlen, pattern, patlen);
998 * bmstrcasestr() -- case-insensitive substring search
1000 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
1001 * The code is roughly based on the strstr() replacement from 'tin' written
1004 inline static const char *_cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
1006 register unsigned char *p, *t;
1007 register int i, j, *delta;
1009 int deltaspace[256];
1011 if (!text) return(NULL);
1012 if (!pattern) return(NULL);
1014 /* algorithm fails if pattern is empty */
1015 if ((p1 = patlen) == 0)
1018 /* code below fails (whenever i is unsigned) if pattern too long */
1024 for (i = 0; i <= 255; i++)
1026 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
1027 delta[tolower(*p++)] = i;
1030 * From now on, we want patlen - 1.
1031 * In the loop below, p points to the end of the pattern,
1032 * t points to the end of the text to be tested against the
1033 * pattern, and i counts the amount of text remaining, not
1034 * including the part to be tested.
1037 p = (unsigned char *) pattern + p1;
1038 t = (unsigned char *) text + p1;
1039 i = textlen - patlen;
1041 if (tolower(p[0]) == tolower(t[0])) {
1042 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
1043 return ((char *)t - p1);
1046 j = delta[tolower(t[0])];
1056 * bmstrcasestr() -- case-insensitive substring search
1058 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
1059 * The code is roughly based on the strstr() replacement from 'tin' written
1062 const char *cbmstrcasestr(const char *text, const char *pattern) {
1066 if (!text) return(NULL);
1067 if (!pattern) return(NULL);
1069 textlen = strlen (text);
1070 patlen = strlen (pattern);
1072 return _cbmstrcasestr_len(text, textlen, pattern, patlen);
1075 const char *cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
1076 return _cbmstrcasestr_len(text, textlen, pattern, patlen);
1080 * Local replacement for controversial C library function that generates
1081 * names for temporary files. Included to shut up compiler warnings.
1083 void CtdlMakeTempFileName(char *name, int len) {
1086 while (i++, i < 100) {
1087 snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
1091 if (!access(name, F_OK)) {
1100 * Determine whether the specified message number is contained within the specified set.
1101 * Returns nonzero if the specified message number is in the specified message set string.
1103 int is_msg_in_mset(const char *mset, long msgnum) {
1106 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
1110 * Now set it for all specified messages.
1112 num_sets = num_tokens(mset, ',');
1113 for (s=0; s<num_sets; ++s) {
1114 extract_token(setstr, mset, s, ',', sizeof setstr);
1116 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1117 if (num_tokens(setstr, ':') >= 2) {
1118 extract_token(histr, setstr, 1, ':', sizeof histr);
1119 if (!strcmp(histr, "*")) {
1120 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1124 strcpy(histr, lostr);
1129 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
1137 * searches for a pattern within a search string
1138 * returns position in string
1140 int pattern2(char *search, char *patn)
1144 len = strlen (search);
1145 plen = strlen (patn);
1146 for (a = 0; a < len; ++a) {
1147 if (!strncasecmp(&search[a], patn, plen))
1155 * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
1156 * buf - the string to modify
1157 * len - length of the string.
1159 void stripltlen(char *buf, int *len)
1162 if (*len == 0) return;
1163 while ((*len > delta) && (isspace(buf[delta]))){
1166 memmove (buf, &buf[delta], *len - delta + 1);
1169 if (*len == 0) return;
1170 while (isspace(buf[(*len) - 1])){
1171 buf[--(*len)] = '\0';
1177 * Convert all whitespace characters in a supplied string to underscores
1179 void convert_spaces_to_underscores(char *str)
1187 for (i=0; i<len; ++i) {
1188 if (isspace(str[i])) {
1196 * check whether the provided string needs to be qp encoded or not
1198 int CheckEncode(const char *pch, long len, const char *pche)
1202 while (pch < pche) {
1203 if (((unsigned char) *pch < 32) ||
1204 ((unsigned char) *pch > 126)) {