7523b17e3647da79ff9d8d2f60f311d67684f39c
[citadel.git] / libcitadel / lib / tools.c
1 // A basic toolset containing miscellaneous functions for string manipluation,
2 // encoding/decoding, and a bunch of other stuff.
3 //
4 // Copyright (c) 1987-2022 by the citadel.org team
5 //
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.
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18 #include <limits.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include "libcitadel.h"
32
33
34 #define TRUE  1
35 #define FALSE 0
36
37 typedef unsigned char byte;           // Byte type
38
39 // copy a string into a buffer of a known size. abort if we exceed the limits
40 //
41 // dest the targetbuffer
42 // src  the source string
43 // n    the size od dest
44 //
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) {
47         int i = 0;
48
49         if (dest == NULL || src == NULL)
50         {
51                 fprintf(stderr, "safestrncpy: NULL argument\n");
52                 abort();
53         }
54
55         do {
56                 dest[i] = src[i];
57                 if (dest[i] == 0) return i;
58                 ++i;
59         } while (i<n);
60         dest[n - 1] = 0;
61         return -i;
62 }
63
64
65 // num_tokens()  -  discover number of parameters/tokens in a string
66 int num_tokens(const char *source, char tok) {
67         int count = 1;
68         const char *ptr = source;
69
70         if (source == NULL) {
71                 return (0);
72         }
73
74         while (*ptr != '\0') {
75                 if (*ptr++ == tok) {
76                         ++count;
77                 }
78         }
79         
80         return (count);
81 }
82
83
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
90
91         s = source;
92
93         if (dest == NULL) {
94                 return(-1);
95         }
96
97         dest[0] = 0;
98
99         if (s == NULL) {
100                 return(-1);
101         }
102         
103         maxlen--;
104
105         while (*s) {
106                 if (*s == separator) {
107                         ++current_token;
108                 }
109                 if ( (current_token == parmnum) && (*s != separator) && (len < maxlen) ) {
110                         dest[len] = *s;
111                         ++len;
112                 }
113                 else if ((current_token > parmnum) || (len >= maxlen)) {
114                         break;
115                 }
116                 ++s;
117         }
118
119         dest[len] = '\0';
120         if (current_token < parmnum) {
121                 return(-1);
122         }
123         return(len);
124 }
125
126
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
130         int count = 0;
131
132         // Find desired parameter
133         d = source;
134         while (count < parmnum) {
135                 // End of string, bail!
136                 if (!*d) {
137                         d = NULL;
138                         break;
139                 }
140                 if (*d == separator) {
141                         count++;
142                 }
143                 d++;
144         }
145         if (!d) return;         // Parameter not found
146
147         // Find next parameter
148         s = d;
149         while (*s && *s != separator) {
150                 s++;
151         }
152
153         // Hack and slash
154         if (*s)
155                 strcpy(d, ++s);
156         else if (d == source)
157                 *d = 0;
158         else
159                 *--d = 0;
160 }
161
162
163 // extract_int()  -  extract an int parm without supplying a buffer
164 int extract_int(const char *source, int parmnum) {
165         char buf[32];
166         
167         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
168                 return(atoi(buf));
169         else
170                 return 0;
171 }
172
173
174 // extract_long()  -  extract an long parm without supplying a buffer
175 long extract_long(const char *source, int parmnum) {
176         char buf[32];
177         
178         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
179                 return(atol(buf));
180         else
181                 return 0;
182 }
183
184
185 // extract_unsigned_long() - extract an unsigned long parm
186 unsigned long extract_unsigned_long(const char *source, int parmnum) {
187         char buf[32];
188
189         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
190                 return strtoul(buf, NULL, 10);
191         else 
192                 return 0;
193 }
194
195
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;
199         char *result;
200         long end;
201 #define UTF8_HEADER "=?UTF-8?B?"
202
203         // check if we're already done
204         AlreadyEncoded = strstr(line, "=?");
205         if ((AlreadyEncoded != NULL) && ((strstr(AlreadyEncoded, "?B?") != NULL)|| (strstr(AlreadyEncoded, "?Q?") != NULL))) {
206                 return strdup(line);
207         }
208
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);
213         result[end]='?';
214         result[end+1]='=';
215         result[end+2]='\0';
216         return result;
217 }
218
219 // removes double slashes from pathnames
220 // allows / disallows trailing slashes
221 void StripSlashes(char *Dir, int TrailingSlash) {
222         char *a, *b;
223
224         a = b = Dir;
225
226         while (!IsEmptyStr(a)) {
227                 if (*a == '/') {
228                         while (*a == '/')
229                                 a++;
230                         *b = '/';
231                         b++;
232                 }
233                 else {
234                         *b = *a;
235                         b++; a++;
236                 }
237         }
238         if ((TrailingSlash) && (*(b - 1) != '/')){
239                 *b = '/';
240                 b++;
241         }
242         *b = '\0';
243
244 }
245
246
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;
251         char *ptr;
252         size_t new_len = 0;
253
254         if ((buf == NULL) || (*buf == '\0')) {
255                 return 0;
256         }
257
258         for (ptr=buf; *ptr!=0; ++ptr) {
259                 if (!isspace(*ptr)) {
260                         if (!first_nonspace) {
261                                 first_nonspace = ptr;
262                         }
263                         last_nonspace = ptr;
264                 }
265         }
266
267         if ((!first_nonspace) || (!last_nonspace)) {
268                 buf[0] = 0;
269                 return 0;
270         }
271
272         new_len = last_nonspace - first_nonspace + 1;
273         memmove(buf, first_nonspace, new_len);
274         buf[new_len] = 0;
275         return new_len;
276 }
277
278
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) {
284         const char *ptr;
285         int b;
286         b = 0;
287         ptr = st;
288         while (!IsEmptyStr(ptr))
289         {
290                 if (*ptr == ch)
291                         ++b;
292                 ptr ++;
293         }
294         return (b);
295 }
296
297
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) {
300         int num_sets;
301         int s;
302         char setstr[128], lostr[128], histr[128];
303         long lo, hi;
304
305         num_sets = num_tokens(mset, ',');
306         for (s=0; s<num_sets; ++s) {
307                 extract_token(setstr, mset, s, ',', sizeof setstr);
308
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);
314                         }
315                 } 
316                 else {
317                         strcpy(histr, lostr);
318                 }
319                 lo = atol(lostr);
320                 hi = atol(histr);
321
322                 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
323         }
324
325         return(0);
326 }
327
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) {
334         char ch;
335         char *ptr;
336         int len = 0;            // tally our own length to avoid strlen() delays
337
338         ptr = start;
339
340         while (1) {
341                 ch = *ptr++;
342                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
343                         buf[len++] = ch;
344                 }
345                 if ((ch == 10) || (ch == 0)) {
346                         buf[len] = 0;
347                         return ptr;
348                 }
349         }
350 }
351
352
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) {
360         char ch;
361         char *ptr;
362         int len = 0;            // tally our own length to avoid strlen() delays
363
364         ptr = start;
365
366         while (1) {
367                 ch = *ptr++;
368                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
369                         buf[len++] = ch;
370                 }
371                 if ((ch == 10) || (ch == 0)) {
372                         buf[len] = 0;
373                         *retlen = len;
374                         return ptr;
375                 }
376         }
377 }
378
379
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) {
386         char ch;
387         const char *ptr;
388         int len = 0;            // tally our own length to avoid strlen() delays
389
390         ptr = start;
391
392         while (1) {
393                 ch = *ptr++;
394                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
395                         buf[len++] = ch;
396                 }
397                 if ((ch == 10) || (ch == 0)) {
398                         buf[len] = 0;
399                         return ptr;
400                 }
401         }
402 }
403
404
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) {
412         char ch;
413         const char *ptr;
414         int len = 0;            // tally our own length to avoid strlen() delays
415
416         ptr = start;
417
418         while (1) {
419                 ch = *ptr++;
420                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
421                         buf[len++] = ch;
422                 }
423                 if ((ch == 10) || (ch == 0)) {
424                         buf[len] = 0;
425                         *retlen = len;
426                         return ptr;
427                 }
428         }
429 }
430
431
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) {
434         int a;
435         int lb = (-1);
436         int rb = (-1);
437
438         for (a = 0; a < strlen(str); ++a) {
439                 if (str[a] == leftboundary) lb = a;
440                 if (str[a] == rightboundary) rb = a;
441         }
442
443         if ( (lb > 0) && (rb > lb) ) {
444                 strcpy(&str[lb - 1], &str[rb + 1]);
445                 return 1;
446         }
447
448         else if ( (lb == 0) && (rb > lb) ) {
449                 strcpy(str, &str[rb + 1]);
450                 return 1;
451         }
452         return 0;
453 }
454
455
456 // Reduce a string down to a boundarized substring (for example, remove
457 // parentheses and anything outside them).
458 long stripallbut(char *str, char leftboundary, char rightboundary) {
459         long len = 0;
460
461         char *lb = NULL;
462         char *rb = NULL;
463
464         lb = strrchr(str, leftboundary);
465         if (lb != NULL) {
466                 ++lb;
467                 rb = strchr(str, rightboundary);
468                 if ((rb != NULL) && (rb >= lb))  {
469                         *rb = 0;
470                         fflush(stderr);
471                         len = (long)rb - (long)lb;
472                         memmove(str, lb, len);
473                         str[len] = 0;
474                         return(len);
475                 }
476         }
477
478         return (long)strlen(str);
479 }
480
481
482 char *myfgets(char *s, int size, FILE *stream) {
483         char *ret = fgets(s, size, stream);
484         char *nl;
485
486         if (ret != NULL) {
487                 nl = strchr(s, '\n');
488
489                 if (nl != NULL)
490                         *nl = 0;
491         }
492
493         return ret;
494 }
495
496
497 // Escape a string for feeding out as a URL.
498 // outbuf the output buffer
499 // oblen the size of outbuf to sanitize
500 // strbuf the input buffer
501 void urlesc(char *outbuf, size_t oblen, char *strbuf) {
502         int a, b, c, len, eclen, olen;
503         char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
504
505         *outbuf = '\0';
506         len = strlen(strbuf);
507         eclen = strlen(ec);
508         olen = 0;
509         for (a = 0; a < len; ++a) {
510                 c = 0;
511                 for (b = 0; b < eclen; ++b) {
512                         if (strbuf[a] == ec[b])
513                                 c = 1;
514                 }
515                 if (c == 1) {
516                         snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
517                         olen += 3;
518                 }
519                 else 
520                         outbuf[olen ++] = strbuf[a];
521         }
522         outbuf[olen] = '\0';
523 }
524
525
526 // In our world, we want strcpy() to be able to work with overlapping strings.
527 #ifdef strcpy
528 #undef strcpy
529 #endif
530 char *strcpy(char *dest, const char *src) {
531         memmove(dest, src, (strlen(src) + 1) );
532         return(dest);
533 }
534
535
536 // Generate a new, globally unique UID parameter for a calendar etc. object
537 void generate_uuid(char *buf) {
538         static int seq = (-1);
539         static int no_kernel_uuid = 0;
540
541         // If we are running on Linux then we have a kernelspace uuid generator available
542
543         if (no_kernel_uuid == 0) {
544                 FILE *fp;
545                 fp = fopen("/proc/sys/kernel/random/uuid", "rb");
546                 if (fp) {
547                         int rv;
548                         rv = fread(buf, 36, 1, fp);
549                         fclose(fp);
550                         if (rv == 1) {
551                                 buf[36] = 0;
552                                 return;
553                         }
554                 }
555         }
556
557         // If the kernel didn't provide us with a uuid, we generate a pseudo-random one
558
559         no_kernel_uuid = 1;
560
561         if (seq == (-1)) {
562                 seq = (int)rand();
563         }
564         ++seq;
565         seq = (seq % 0x0FFF) ;
566
567         sprintf(buf, "%08lx-%04lx-4%03x-a%03x-%012lx",
568                 (long)time(NULL),
569                 (long)getpid(),
570                 seq,
571                 seq,
572                 (long)rand()
573         );
574 }
575
576
577 // bmstrcasestr() -- case-insensitive substring search
578 //
579 // This uses the Boyer-Moore search algorithm and is therefore quite fast.
580 // The code is roughly based on the strstr() replacement from 'tin' written
581 // by Urs Jannsen.
582 inline static char *_bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
583
584         register unsigned char *p, *t;
585         register int i, j, *delta;
586         register size_t p1;
587         int deltaspace[256];
588
589         if (!text) return(NULL);
590         if (!pattern) return(NULL);
591
592         // algorithm fails if pattern is empty
593         if ((p1 = patlen) == 0)
594                 return (text);
595
596         // code below fails (whenever i is unsigned) if pattern too long
597         if (p1 > textlen)
598                 return (NULL);
599
600         // set up deltas
601         delta = deltaspace;
602         for (i = 0; i <= 255; i++)
603                 delta[i] = p1;
604         for (p = (unsigned char *) pattern, i = p1; --i > 0;)
605                 delta[tolower(*p++)] = i;
606
607         // From now on, we want patlen - 1.
608         // In the loop below, p points to the end of the pattern,
609         // t points to the end of the text to be tested against the
610         // pattern, and i counts the amount of text remaining, not
611         // including the part to be tested.
612         p1--;
613         p = (unsigned char *) pattern + p1;
614         t = (unsigned char *) text + p1;
615         i = textlen - patlen;
616         while(1) {
617                 if (tolower(p[0]) == tolower(t[0])) {
618                         if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
619                                 return ((char *)t - p1);
620                         }
621                 }
622                 j = delta[tolower(t[0])];
623                 if (i < j)
624                         break;
625                 i -= j;
626                 t += j;
627         }
628         return (NULL);
629 }
630
631
632 /*
633  * bmstrcasestr() -- case-insensitive substring search
634  *
635  * This uses the Boyer-Moore search algorithm and is therefore quite fast.
636  * The code is roughly based on the strstr() replacement from 'tin' written
637  * by Urs Jannsen.
638  */
639 char *bmstrcasestr(char *text, const char *pattern) {
640         size_t textlen;
641         size_t patlen;
642
643         if (!text) return(NULL);
644         if (!pattern) return(NULL);
645
646         textlen = strlen (text);
647         patlen = strlen (pattern);
648
649         return _bmstrcasestr_len(text, textlen, pattern, patlen);
650 }
651
652 char *bmstrcasestr_len(char *text, size_t textlen, const char *pattern, size_t patlen) {
653         return _bmstrcasestr_len(text, textlen, pattern, patlen);
654 }
655
656
657 /*
658  * bmstrcasestr() -- case-insensitive substring search
659  *
660  * This uses the Boyer-Moore search algorithm and is therefore quite fast.
661  * The code is roughly based on the strstr() replacement from 'tin' written
662  * by Urs Jannsen.
663  */
664 inline static const char *_cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
665
666         register unsigned char *p, *t;
667         register int i, j, *delta;
668         register size_t p1;
669         int deltaspace[256];
670
671         if (!text) return(NULL);
672         if (!pattern) return(NULL);
673
674         /* algorithm fails if pattern is empty */
675         if ((p1 = patlen) == 0)
676                 return (text);
677
678         /* code below fails (whenever i is unsigned) if pattern too long */
679         if (p1 > textlen)
680                 return (NULL);
681
682         /* set up deltas */
683         delta = deltaspace;
684         for (i = 0; i <= 255; i++)
685                 delta[i] = p1;
686         for (p = (unsigned char *) pattern, i = p1; --i > 0;)
687                 delta[tolower(*p++)] = i;
688
689         /*
690          * From now on, we want patlen - 1.
691          * In the loop below, p points to the end of the pattern,
692          * t points to the end of the text to be tested against the
693          * pattern, and i counts the amount of text remaining, not
694          * including the part to be tested.
695          */
696         p1--;
697         p = (unsigned char *) pattern + p1;
698         t = (unsigned char *) text + p1;
699         i = textlen - patlen;
700         while(1) {
701                 if (tolower(p[0]) == tolower(t[0])) {
702                         if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
703                                 return ((char *)t - p1);
704                         }
705                 }
706                 j = delta[tolower(t[0])];
707                 if (i < j)
708                         break;
709                 i -= j;
710                 t += j;
711         }
712         return (NULL);
713 }
714
715
716 /*
717  * bmstrcasestr() -- case-insensitive substring search
718  *
719  * This uses the Boyer-Moore search algorithm and is therefore quite fast.
720  * The code is roughly based on the strstr() replacement from 'tin' written
721  * by Urs Jannsen.
722  */
723 const char *cbmstrcasestr(const char *text, const char *pattern) {
724         size_t textlen;
725         size_t patlen;
726
727         if (!text) return(NULL);
728         if (!pattern) return(NULL);
729
730         textlen = strlen (text);
731         patlen = strlen (pattern);
732
733         return _cbmstrcasestr_len(text, textlen, pattern, patlen);
734 }
735
736
737 const char *cbmstrcasestr_len(const char *text, size_t textlen, const char *pattern, size_t patlen) {
738         return _cbmstrcasestr_len(text, textlen, pattern, patlen);
739 }
740
741
742 /*
743  * Local replacement for controversial C library function that generates
744  * names for temporary files.  Included to shut up compiler warnings.
745  */
746 void CtdlMakeTempFileName(char *name, int len) {
747         int i = 0;
748
749         while (i++, i < 100) {
750                 snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
751                         (long)getpid(),
752                         rand()
753                 );
754                 if (!access(name, F_OK)) {
755                         return;
756                 }
757         }
758 }
759
760
761 /*
762  * Determine whether the specified message number is contained within the specified set.
763  * Returns nonzero if the specified message number is in the specified message set string.
764  */
765 int is_msg_in_mset(const char *mset, long msgnum) {
766         int num_sets;
767         int s;
768         char setstr[SIZ], lostr[SIZ], histr[SIZ];
769         long lo, hi;
770
771         // Now set it for all specified messages.
772         num_sets = num_tokens(mset, ',');
773         for (s=0; s<num_sets; ++s) {
774                 extract_token(setstr, mset, s, ',', sizeof setstr);
775
776                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
777                 if (num_tokens(setstr, ':') >= 2) {
778                         extract_token(histr, setstr, 1, ':', sizeof histr);
779                         if (!strcmp(histr, "*")) {
780                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
781                         }
782                 }
783                 else {
784                         strcpy(histr, lostr);
785                 }
786                 lo = atol(lostr);
787                 hi = atol(histr);
788
789                 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
790         }
791
792         return(0);
793 }
794
795
796 // searches for a pattern within a search string
797 // returns position in string
798 int pattern2(char *search, char *patn) {
799         int a;
800         int len, plen;
801         len = strlen (search);
802         plen = strlen (patn);
803         for (a = 0; a < len; ++a) {
804                 if (!strncasecmp(&search[a], patn, plen))
805                         return (a);
806         }
807         return (-1);
808 }
809
810
811 /*
812  * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
813  * buf - the string to modify
814  * len - length of the string. 
815  */
816 void string_trimlen(char *buf, int *len) {
817         int delta = 0;
818         if (*len == 0) return;
819         while ((*len > delta) && (isspace(buf[delta]))){
820                 delta ++;
821         }
822         memmove (buf, &buf[delta], *len - delta + 1);
823         (*len) -=delta;
824
825         if (*len == 0) return;
826         while (isspace(buf[(*len) - 1])){
827                 buf[--(*len)] = '\0';
828         }
829 }
830
831
832 /*
833  * Convert all whitespace characters in a supplied string to underscores
834  */
835 void convert_spaces_to_underscores(char *str) {
836         int len;
837         int i;
838
839         if (!str) return;
840
841         len = strlen(str);
842         for (i=0; i<len; ++i) {
843                 if (isspace(str[i])) {
844                         str[i] = '_';
845                 }
846         }
847 }
848
849
850 /*
851  * check whether the provided string needs to be qp encoded or not
852  */
853 int CheckEncode(const char *pch, long len, const char *pche) {
854         if (pche == NULL)
855                 pche = pch + len;
856         while (pch < pche) {
857                 if (((unsigned char) *pch < 32) || 
858                     ((unsigned char) *pch > 126)) {
859                         return 1;
860                 }
861                 pch++;
862         }
863         return 0;
864 }
865