New format for exim_message_id
[exim.git] / src / src / string.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
9
10 /* Miscellaneous string-handling functions. Some are not required for
11 utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
12
13
14 #include "exim.h"
15 #include <assert.h>
16
17
18 #ifndef COMPILE_UTILITY
19 /*************************************************
20 *            Test for IP address                 *
21 *************************************************/
22
23 /* This used just to be a regular expression, but with IPv6 things are a bit
24 more complicated. If the address contains a colon, it is assumed to be a v6
25 address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present,
26 and maskptr is not NULL, its offset is placed there.
27
28 Arguments:
29   s         a string
30   maskptr   NULL if no mask is permitted to follow
31             otherwise, points to an int where the offset of '/' is placed
32             if there is no / followed by trailing digits, *maskptr is set 0
33
34 Returns:    0 if the string is not a textual representation of an IP address
35             4 if it is an IPv4 address
36             6 if it is an IPv6 address
37 */
38
39 int
40 string_is_ip_address(const uschar *s, int *maskptr)
41 {
42 int yield = 4;
43
44 /* If an optional mask is permitted, check for it. If found, pass back the
45 offset. */
46
47 if (maskptr)
48   {
49   const uschar *ss = s + Ustrlen(s);
50   *maskptr = 0;
51   if (s != ss && isdigit(*(--ss)))
52     {
53     while (ss > s && isdigit(ss[-1])) ss--;
54     if (ss > s && *(--ss) == '/') *maskptr = ss - s;
55     }
56   }
57
58 /* A colon anywhere in the string => IPv6 address */
59
60 if (Ustrchr(s, ':') != NULL)
61   {
62   BOOL had_double_colon = FALSE;
63   BOOL v4end = FALSE;
64
65   yield = 6;
66
67   /* An IPv6 address must start with hex digit or double colon. A single
68   colon is invalid. */
69
70   if (*s == ':' && *(++s) != ':') return 0;
71
72   /* Now read up to 8 components consisting of up to 4 hex digits each. There
73   may be one and only one appearance of double colon, which implies any number
74   of binary zero bits. The number of preceding components is held in count. */
75
76   for (int count = 0; count < 8; count++)
77     {
78     /* If the end of the string is reached before reading 8 components, the
79     address is valid provided a double colon has been read. This also applies
80     if we hit the / that introduces a mask or the % that introduces the
81     interface specifier (scope id) of a link-local address. */
82
83     if (*s == 0 || *s == '%' || *s == '/') return had_double_colon ? yield : 0;
84
85     /* If a component starts with an additional colon, we have hit a double
86     colon. This is permitted to appear once only, and counts as at least
87     one component. The final component may be of this form. */
88
89     if (*s == ':')
90       {
91       if (had_double_colon) return 0;
92       had_double_colon = TRUE;
93       s++;
94       continue;
95       }
96
97     /* If the remainder of the string contains a dot but no colons, we
98     can expect a trailing IPv4 address. This is valid if either there has
99     been no double-colon and this is the 7th component (with the IPv4 address
100     being the 7th & 8th components), OR if there has been a double-colon
101     and fewer than 6 components. */
102
103     if (Ustrchr(s, ':') == NULL && Ustrchr(s, '.') != NULL)
104       {
105       if ((!had_double_colon && count != 6) ||
106           (had_double_colon && count > 6)) return 0;
107       v4end = TRUE;
108       yield = 6;
109       break;
110       }
111
112     /* Check for at least one and not more than 4 hex digits for this
113     component. */
114
115     if (!isxdigit(*s++)) return 0;
116     if (isxdigit(*s) && isxdigit(*(++s)) && isxdigit(*(++s))) s++;
117
118     /* If the component is terminated by colon and there is more to
119     follow, skip over the colon. If there is no more to follow the address is
120     invalid. */
121
122     if (*s == ':' && *(++s) == 0) return 0;
123     }
124
125   /* If about to handle a trailing IPv4 address, drop through. Otherwise
126   all is well if we are at the end of the string or at the mask or at a percent
127   sign, which introduces the interface specifier (scope id) of a link local
128   address. */
129
130   if (!v4end)
131     return (*s == 0 || *s == '%' ||
132            (*s == '/' && maskptr != NULL && *maskptr != 0))? yield : 0;
133   }
134
135 /* Test for IPv4 address, which may be the tail-end of an IPv6 address. */
136
137 for (int i = 0; i < 4; i++)
138   {
139   long n;
140   uschar * end;
141
142   if (i != 0 && *s++ != '.') return 0;
143   n = strtol(CCS s, CSS &end, 10);
144   if (n > 255 || n < 0 || end <= s || end > s+3) return 0;
145   s = end;
146   }
147
148 return !*s || (*s == '/' && maskptr && *maskptr != 0) ? yield : 0;
149 }
150 #endif  /* COMPILE_UTILITY */
151
152
153 /*************************************************
154 *              Format message size               *
155 *************************************************/
156
157 /* Convert a message size in bytes to printing form, rounding
158 according to the magnitude of the number. A value of zero causes
159 a string of spaces to be returned.
160
161 Arguments:
162   size        the message size in bytes
163   buffer      where to put the answer
164
165 Returns:      pointer to the buffer
166               a string of exactly 5 characters is normally returned
167 */
168
169 uschar *
170 string_format_size(int size, uschar *buffer)
171 {
172 if (size == 0) Ustrcpy(buffer, US"     ");
173 else if (size < 1024) sprintf(CS buffer, "%5d", size);
174 else if (size < 10*1024)
175   sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
176 else if (size < 1024*1024)
177   sprintf(CS buffer, "%4dK", (size + 512)/1024);
178 else if (size < 10*1024*1024)
179   sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
180 else
181   sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
182 return buffer;
183 }
184
185
186
187 #ifndef COMPILE_UTILITY
188 /*************************************************
189 *       Convert a number to base 62 format       *
190 *************************************************/
191
192 /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
193 BASE_62 is actually 36. Always return exactly 6 characters plus a NUL, in a
194 static area.  This is enough for a 32b input, for 62  (for 64b we would want 11+nul);
195 but with 36 we lose half the input range of a 32b input.
196
197 Argument: a long integer
198 Returns:  pointer to base 62 string
199 */
200
201 uschar *
202 string_base62_32(unsigned long int value)
203 {
204 static uschar yield[7];
205 uschar * p = yield + sizeof(yield) - 1;
206 *p = 0;
207 while (p > yield)
208   {
209   *--p = base62_chars[value % BASE_62];
210   value /= BASE_62;
211   }
212 return yield;
213 }
214
215 uschar *
216 string_base62_64(unsigned long int value)
217 {
218 static uschar yield[12];
219 uschar * p = yield + sizeof(yield) - 1;
220 *p = '\0';
221 while (p > yield)
222   if (value)
223     {
224     *--p = base62_chars[value % BASE_62];
225     value /= BASE_62;
226     }
227   else
228     *--p = '0';
229 return yield;
230 }
231 #endif  /* COMPILE_UTILITY */
232
233
234
235 /*************************************************
236 *          Interpret escape sequence             *
237 *************************************************/
238
239 /* This function is called from several places where escape sequences are to be
240 interpreted in strings.
241
242 Arguments:
243   pp       points a pointer to the initiating "\" in the string;
244            the pointer gets updated to point to the final character
245            If the backslash is the last character in the string, it
246            is not interpreted.
247 Returns:   the value of the character escape
248 */
249
250 int
251 string_interpret_escape(const uschar **pp)
252 {
253 #ifdef COMPILE_UTILITY
254 const uschar *hex_digits= CUS"0123456789abcdef";
255 #endif
256 int ch;
257 const uschar *p = *pp;
258 ch = *(++p);
259 if (ch == '\0') return **pp;
260 if (isdigit(ch) && ch != '8' && ch != '9')
261   {
262   ch -= '0';
263   if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
264     {
265     ch = ch * 8 + *(++p) - '0';
266     if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
267       ch = ch * 8 + *(++p) - '0';
268     }
269   }
270 else switch(ch)
271   {
272   case 'b':  ch = '\b'; break;
273   case 'f':  ch = '\f'; break;
274   case 'n':  ch = '\n'; break;
275   case 'r':  ch = '\r'; break;
276   case 't':  ch = '\t'; break;
277   case 'v':  ch = '\v'; break;
278   case 'x':
279   ch = 0;
280   if (isxdigit(p[1]))
281     {
282     ch = ch * 16 +
283       Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
284     if (isxdigit(p[1])) ch = ch * 16 +
285       Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
286     }
287   break;
288   }
289 *pp = p;
290 return ch;
291 }
292
293
294
295 #ifndef COMPILE_UTILITY
296 /*************************************************
297 *          Ensure string is printable            *
298 *************************************************/
299
300 /* This function is called for critical strings. It checks for any
301 non-printing characters, and if any are found, it makes a new copy
302 of the string with suitable escape sequences. It is most often called by the
303 macro string_printing(), which sets flags to 0.
304
305 Arguments:
306   s             the input string
307   flags         Bit 0: convert tabs.  Bit 1: convert spaces.
308
309 Returns:        string with non-printers encoded as printing sequences
310 */
311
312 const uschar *
313 string_printing2(const uschar *s, int flags)
314 {
315 int nonprintcount = 0;
316 int length = 0;
317 const uschar *t = s;
318 uschar *ss, *tt;
319
320 while (*t)
321   {
322   int c = *t++;
323   if (  !mac_isprint(c)
324      || flags & SP_TAB && c == '\t'
325      || flags & SP_SPACE && c == ' '
326      ) nonprintcount++;
327   length++;
328   }
329
330 if (nonprintcount == 0) return s;
331
332 /* Get a new block of store guaranteed big enough to hold the
333 expanded string. */
334
335 tt = ss = store_get(length + nonprintcount * 3 + 1, s);
336
337 /* Copy everything, escaping non printers. */
338
339 for (t = s; *t; )
340   {
341   int c = *t;
342   if (  mac_isprint(c)
343      && (!(flags & SP_TAB) || c != '\t')
344      && (!(flags & SP_SPACE) || c != ' ')
345      )
346     *tt++ = *t++;
347   else
348     {
349     *tt++ = '\\';
350     switch (*t)
351       {
352       case '\n': *tt++ = 'n'; break;
353       case '\r': *tt++ = 'r'; break;
354       case '\b': *tt++ = 'b'; break;
355       case '\v': *tt++ = 'v'; break;
356       case '\f': *tt++ = 'f'; break;
357       case '\t': *tt++ = 't'; break;
358       default: sprintf(CS tt, "%03o", *t); tt += 3; break;
359       }
360     t++;
361     }
362   }
363 *tt = 0;
364 return ss;
365 }
366 #endif  /* COMPILE_UTILITY */
367
368 /*************************************************
369 *        Undo printing escapes in string         *
370 *************************************************/
371
372 /* This function is the reverse of string_printing2.  It searches for
373 backslash characters and if any are found, it makes a new copy of the
374 string with escape sequences parsed.  Otherwise it returns the original
375 string.
376
377 Arguments:
378   s             the input string
379
380 Returns:        string with printing escapes parsed back
381 */
382
383 uschar *
384 string_unprinting(uschar *s)
385 {
386 uschar *p, *q, *r, *ss;
387 int len, off;
388
389 p = Ustrchr(s, '\\');
390 if (!p) return s;
391
392 len = Ustrlen(s) + 1;
393 ss = store_get(len, s);
394
395 q = ss;
396 off = p - s;
397 if (off)
398   {
399   memcpy(q, s, off);
400   q += off;
401   }
402
403 while (*p)
404   {
405   if (*p == '\\')
406     {
407     *q++ = string_interpret_escape((const uschar **)&p);
408     p++;
409     }
410   else
411     {
412     r = Ustrchr(p, '\\');
413     if (!r)
414       {
415       off = Ustrlen(p);
416       memcpy(q, p, off);
417       p += off;
418       q += off;
419       break;
420       }
421     else
422       {
423       off = r - p;
424       memcpy(q, p, off);
425       q += off;
426       p = r;
427       }
428     }
429   }
430 *q = '\0';
431
432 return ss;
433 }
434
435
436
437
438 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
439         && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
440 /*************************************************
441 *            Copy and save string                *
442 *************************************************/
443
444 /*
445 Argument: string to copy
446 Returns:  copy of string in new store with the same taint status
447 */
448
449 uschar *
450 string_copy_function(const uschar * s)
451 {
452 return string_copy_taint(s, s);
453 }
454
455 /* As above, but explicitly specifying the result taint status
456 */
457
458 uschar *
459 string_copy_taint_function(const uschar * s, const void * proto_mem)
460 {
461 return string_copy_taint(s, proto_mem);
462 }
463
464
465
466 /*************************************************
467 *       Copy and save string, given length       *
468 *************************************************/
469
470 /* It is assumed the data contains no zeros. A zero is added
471 onto the end.
472
473 Arguments:
474   s         string to copy
475   n         number of characters
476
477 Returns:    copy of string in new store
478 */
479
480 uschar *
481 string_copyn_function(const uschar * s, int n)
482 {
483 return string_copyn(s, n);
484 }
485 #endif
486
487
488 /*************************************************
489 *     Copy and save string in malloc'd store     *
490 *************************************************/
491
492 /* This function assumes that memcpy() is faster than strcpy().
493
494 Argument: string to copy
495 Returns:  copy of string in new store
496 */
497
498 uschar *
499 string_copy_malloc(const uschar * s)
500 {
501 int len = Ustrlen(s) + 1;
502 uschar * ss = store_malloc(len);
503 memcpy(ss, s, len);
504 return ss;
505 }
506
507
508
509 /*************************************************
510 *    Copy string if long, inserting newlines     *
511 *************************************************/
512
513 /* If the given string is longer than 75 characters, it is copied, and within
514 the copy, certain space characters are converted into newlines.
515
516 Argument:  pointer to the string
517 Returns:   pointer to the possibly altered string
518 */
519
520 uschar *
521 string_split_message(uschar * msg)
522 {
523 uschar *s, *ss;
524
525 if (!msg || Ustrlen(msg) <= 75) return msg;
526 s = ss = msg = string_copy(msg);
527
528 for (;;)
529   {
530   int i = 0;
531   while (i < 75 && *ss && *ss != '\n') ss++, i++;
532   if (!*ss) break;
533   if (*ss == '\n')
534     s = ++ss;
535   else
536     {
537     uschar * t = ss + 1;
538     uschar * tt = NULL;
539     while (--t > s + 35)
540       {
541       if (*t == ' ')
542         {
543         if (t[-1] == ':') { tt = t; break; }
544         if (!tt) tt = t;
545         }
546       }
547
548     if (!tt)          /* Can't split behind - try ahead */
549       {
550       t = ss + 1;
551       while (*t)
552         {
553         if (*t == ' ' || *t == '\n')
554           { tt = t; break; }
555         t++;
556         }
557       }
558
559     if (!tt) break;   /* Can't find anywhere to split */
560     *tt = '\n';
561     s = ss = tt+1;
562     }
563   }
564
565 return msg;
566 }
567
568
569
570 /*************************************************
571 *   Copy returned DNS domain name, de-escaping   *
572 *************************************************/
573
574 /* If a domain name contains top-bit characters, some resolvers return
575 the fully qualified name with those characters turned into escapes. The
576 convention is a backslash followed by _decimal_ digits. We convert these
577 back into the original binary values. This will be relevant when
578 allow_utf8_domains is set true and UTF-8 characters are used in domain
579 names. Backslash can also be used to escape other characters, though we
580 shouldn't come across them in domain names.
581
582 Argument:   the domain name string
583 Returns:    copy of string in new store, de-escaped
584 */
585
586 uschar *
587 string_copy_dnsdomain(uschar * s)
588 {
589 uschar * yield;
590 uschar * ss = yield = store_get(Ustrlen(s) + 1, GET_TAINTED);   /* always treat as tainted */
591
592 while (*s)
593   {
594   if (*s != '\\')
595     *ss++ = *s++;
596   else if (isdigit(s[1]))
597     {
598     *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
599     s += 4;
600     }
601   else if (*++s)
602     *ss++ = *s++;
603   }
604
605 *ss = 0;
606 return yield;
607 }
608
609
610 #ifndef COMPILE_UTILITY
611 /*************************************************
612 *     Copy space-terminated or quoted string     *
613 *************************************************/
614
615 /* This function copies from a string until its end, or until whitespace is
616 encountered, unless the string begins with a double quote, in which case the
617 terminating quote is sought, and escaping within the string is done. The length
618 of a de-quoted string can be no longer than the original, since escaping always
619 turns n characters into 1 character.
620
621 Argument:  pointer to the pointer to the first character, which gets updated
622 Returns:   the new string
623 */
624
625 uschar *
626 string_dequote(const uschar ** sptr)
627 {
628 const uschar * s = * sptr;
629 uschar * t, * yield;
630
631 /* First find the end of the string */
632
633 if (*s != '\"')
634   while (*s && !isspace(*s)) s++;
635 else
636   {
637   s++;
638   while (*s && *s != '\"')
639     {
640     if (*s == '\\') (void)string_interpret_escape(&s);
641     s++;
642     }
643   if (*s) s++;
644   }
645
646 /* Get enough store to copy into */
647
648 t = yield = store_get(s - *sptr + 1, *sptr);
649 s = *sptr;
650
651 /* Do the copy */
652
653 if (*s != '\"')
654   while (*s && !isspace(*s)) *t++ = *s++;
655 else
656   {
657   s++;
658   while (*s && *s != '\"')
659     {
660     *t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
661     s++;
662     }
663   if (*s) s++;
664   }
665
666 /* Update the pointer and return the terminated copy */
667
668 *sptr = s;
669 *t = 0;
670 return yield;
671 }
672 #endif  /* COMPILE_UTILITY */
673
674
675
676 /*************************************************
677 *          Format a string and save it           *
678 *************************************************/
679
680 /* The formatting is done by string_vformat, which checks the length of
681 everything.  Taint is taken from the worst of the arguments.
682
683 Arguments:
684   format    a printf() format - deliberately char * rather than uschar *
685               because it will most usually be a literal string
686   func      caller, for debug
687   line      caller, for debug
688   ...       arguments for format
689
690 Returns:    pointer to fresh piece of store containing sprintf'ed string
691 */
692
693 uschar *
694 string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
695 {
696 #ifdef COMPILE_UTILITY
697 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
698 gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
699 gstring * g = &gs;
700 unsigned flags = 0;
701 #else
702 gstring * g = NULL;
703 unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
704 #endif
705
706 va_list ap;
707 va_start(ap, line);
708 g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
709         flags, format, ap);
710 va_end(ap);
711
712 if (!g)
713   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
714     "string_sprintf expansion was longer than %d; format string was (%s)\n"
715     " called from %s %d\n",
716     STRING_SPRINTF_BUFFER_SIZE, format, func, line);
717
718 #ifdef COMPILE_UTILITY
719 return string_copyn(g->s, g->ptr);
720 #else
721 gstring_release_unused(g);
722 return string_from_gstring(g);
723 #endif
724 }
725
726
727
728 /*************************************************
729 *         Case-independent strncmp() function    *
730 *************************************************/
731
732 /*
733 Arguments:
734   s         first string
735   t         second string
736   n         number of characters to compare
737
738 Returns:    < 0, = 0, or > 0, according to the comparison
739 */
740
741 int
742 strncmpic(const uschar * s, const uschar * t, int n)
743 {
744 while (n--)
745   {
746   int c = tolower(*s++) - tolower(*t++);
747   if (c) return c;
748   }
749 return 0;
750 }
751
752
753 /*************************************************
754 *         Case-independent strcmp() function     *
755 *************************************************/
756
757 /*
758 Arguments:
759   s         first string
760   t         second string
761
762 Returns:    < 0, = 0, or > 0, according to the comparison
763 */
764
765 int
766 strcmpic(const uschar * s, const uschar * t)
767 {
768 while (*s)
769   {
770   int c = tolower(*s++) - tolower(*t++);
771   if (c != 0) return c;
772   }
773 return *t;
774 }
775
776
777 /*************************************************
778 *         Case-independent strstr() function     *
779 *************************************************/
780
781 /* The third argument specifies whether whitespace is required
782 to follow the matched string.
783
784 Arguments:
785   s              string to search
786   t              substring to search for
787   space_follows  if TRUE, match only if whitespace follows
788
789 Returns:         pointer to substring in string, or NULL if not found
790 */
791
792 const uschar *
793 strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
794 {
795 const uschar * p = t;
796 const uschar * yield = NULL;
797 int cl = tolower(*p);
798 int cu = toupper(*p);
799
800 while (*s)
801   {
802   if (*s == cl || *s == cu)
803     {
804     if (!yield) yield = s;
805     if (!*++p)
806       {
807       if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
808       yield = NULL;
809       p = t;
810       }
811     cl = tolower(*p);
812     cu = toupper(*p);
813     s++;
814     }
815   else if (yield)
816     {
817     yield = NULL;
818     p = t;
819     cl = tolower(*p);
820     cu = toupper(*p);
821     }
822   else s++;
823   }
824 return NULL;
825 }
826
827 uschar *
828 strstric(uschar * s, uschar * t, BOOL space_follows)
829 {
830 return US strstric_c(s, t, space_follows);
831 }
832
833
834 #ifdef COMPILE_UTILITY
835 /* Dummy version for this function; it should never be called */
836 static void
837 gstring_grow(gstring * g, int count)
838 {
839 assert(FALSE);
840 }
841 #endif
842
843
844
845 #ifndef COMPILE_UTILITY
846 /*************************************************
847 *       Get next string from separated list      *
848 *************************************************/
849
850 /* Leading and trailing space is removed from each item. The separator in the
851 list is controlled by the int pointed to by the separator argument as follows:
852
853   If the value is > 0 it is used as the separator. This is typically used for
854   sublists such as slash-separated options. The value is always a printing
855   character.
856
857     (If the value is actually > UCHAR_MAX there is only one item in the list.
858     This is used for some cases when called via functions that sometimes
859     plough through lists, and sometimes are given single items.)
860
861   If the value is <= 0, the string is inspected for a leading <x, where x is an
862   ispunct() or an iscntrl() character. If found, x is used as the separator. If
863   not found:
864
865       (a) if separator == 0, ':' is used
866       (b) if separator <0, -separator is used
867
868   In all cases the value of the separator that is used is written back to the
869   int so that it is used on subsequent calls as we progress through the list.
870
871 A literal ispunct() separator can be represented in an item by doubling, but
872 there is no way to include an iscntrl() separator as part of the data.
873
874 Arguments:
875   listptr    points to a pointer to the current start of the list; the
876              pointer gets updated to point after the end of the next item
877   separator  a pointer to the separator character in an int (see above)
878   buffer     where to put a copy of the next string in the list; or
879                NULL if the next string is returned in new memory
880              Note that if the list is tainted then a provided buffer must be
881              also (else we trap, with a message referencing the callsite).
882              If we do the allocation, taint is handled there.
883   buflen     when buffer is not NULL, the size of buffer; otherwise ignored
884
885   func       caller, for debug
886   line       caller, for debug
887
888 Returns:     pointer to buffer, containing the next substring,
889              or NULL if no more substrings
890 */
891
892 uschar *
893 string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
894   int buflen, const uschar * func, int line)
895 {
896 int sep = *separator;
897 const uschar * s = *listptr;
898 BOOL sep_is_special;
899
900 if (!s) return NULL;
901
902 /* This allows for a fixed specified separator to be an iscntrl() character,
903 but at the time of implementation, this is never the case. However, it's best
904 to be conservative. */
905
906 while (isspace(*s) && *s != sep) s++;
907
908 /* A change of separator is permitted, so look for a leading '<' followed by an
909 allowed character. */
910
911 if (sep <= 0)
912   {
913   if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
914     {
915     sep = s[1];
916     if (*++s) ++s;
917     while (isspace(*s) && *s != sep) s++;
918     }
919   else
920     sep = sep ? -sep : ':';
921   *separator = sep;
922   }
923
924 /* An empty string has no list elements */
925
926 if (!*s) return NULL;
927
928 /* Note whether whether or not the separator is an iscntrl() character. */
929
930 sep_is_special = iscntrl(sep);
931
932 /* Handle the case when a buffer is provided. */
933 /*XXX need to also deal with qouted-requirements mismatch */
934
935 if (buffer)
936   {
937   int p = 0;
938   if (is_tainted(s) && !is_tainted(buffer))
939     die_tainted(US"string_nextinlist", func, line);
940   for (; *s; s++)
941     {
942     if (*s == sep && (*(++s) != sep || sep_is_special)) break;
943     if (p < buflen - 1) buffer[p++] = *s;
944     }
945   while (p > 0 && isspace(buffer[p-1])) p--;
946   buffer[p] = '\0';
947   }
948
949 /* Handle the case when a buffer is not provided. */
950
951 else
952   {
953   gstring * g = NULL;
954
955   /* We know that *s != 0 at this point. However, it might be pointing to a
956   separator, which could indicate an empty string, or (if an ispunct()
957   character) could be doubled to indicate a separator character as data at the
958   start of a string. Avoid getting working memory for an empty item. */
959
960   if (*s == sep)
961     if (*++s != sep || sep_is_special)
962       {
963       *listptr = s;
964       return string_copy(US"");
965       }
966
967   /* Not an empty string; the first character is guaranteed to be a data
968   character. */
969
970   for (;;)
971     {
972     const uschar * ss;
973     for (ss = s + 1; *ss && *ss != sep; ) ss++;
974     g = string_catn(g, s, ss-s);
975     s = ss;
976     if (!*s || *++s != sep || sep_is_special) break;
977     }
978
979   /* Trim trailing spaces from the returned string */
980
981   /* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
982   while (  g->ptr > 0 && isspace(g->s[g->ptr-1])
983         && (g->ptr == 1 || g->s[g->ptr-2] != '\\') )
984     g->ptr--;
985   buffer = string_from_gstring(g);
986   gstring_release_unused_trc(g, CCS func, line);
987   }
988
989 /* Update the current pointer and return the new string */
990
991 *listptr = s;
992 return buffer;
993 }
994
995
996 static const uschar *
997 Ustrnchr(const uschar * s, int c, unsigned * len)
998 {
999 unsigned siz = *len;
1000 while (siz)
1001   {
1002   if (!*s) return NULL;
1003   if (*s == c)
1004     {
1005     *len = siz;
1006     return s;
1007     }
1008   s++;
1009   siz--;
1010   }
1011 return NULL;
1012 }
1013
1014
1015 /************************************************
1016 *       Add element to separated list           *
1017 ************************************************/
1018 /* This function is used to build a list, returning an allocated null-terminated
1019 growable string. The given element has any embedded separator characters
1020 doubled.
1021
1022 Despite having the same growable-string interface as string_cat() the list is
1023 always returned null-terminated.
1024
1025 Arguments:
1026   list  expanding-string for the list that is being built, or NULL
1027         if this is a new list that has no contents yet
1028   sep   list separator character
1029   ele   new element to be appended to the list
1030
1031 Returns:  pointer to the start of the list, changed if copied for expansion.
1032 */
1033
1034 gstring *
1035 string_append_listele(gstring * list, uschar sep, const uschar * ele)
1036 {
1037 uschar * sp;
1038
1039 if (list && list->ptr)
1040   list = string_catn(list, &sep, 1);
1041
1042 while((sp = Ustrchr(ele, sep)))
1043   {
1044   list = string_catn(list, ele, sp-ele+1);
1045   list = string_catn(list, &sep, 1);
1046   ele = sp+1;
1047   }
1048 list = string_cat(list, ele);
1049 (void) string_from_gstring(list);
1050 return list;
1051 }
1052
1053
1054 gstring *
1055 string_append_listele_n(gstring * list, uschar sep, const uschar * ele,
1056  unsigned len)
1057 {
1058 const uschar * sp;
1059
1060 if (list && list->ptr)
1061   list = string_catn(list, &sep, 1);
1062
1063 while((sp = Ustrnchr(ele, sep, &len)))
1064   {
1065   list = string_catn(list, ele, sp-ele+1);
1066   list = string_catn(list, &sep, 1);
1067   ele = sp+1;
1068   len--;
1069   }
1070 list = string_catn(list, ele, len);
1071 (void) string_from_gstring(list);
1072 return list;
1073 }
1074
1075
1076
1077 /* A slightly-bogus listmaker utility; the separator is a string so
1078 can be multiple chars - there is no checking for the element content
1079 containing any of the separator. */
1080
1081 gstring *
1082 string_append2_listele_n(gstring * list, const uschar * sepstr,
1083  const uschar * ele, unsigned len)
1084 {
1085 if (list && list->ptr)
1086   list = string_cat(list, sepstr);
1087
1088 list = string_catn(list, ele, len);
1089 (void) string_from_gstring(list);
1090 return list;
1091 }
1092
1093
1094
1095 /************************************************/
1096 /* Add more space to a growable-string.  The caller should check
1097 first if growth is required.  The gstring struct is modified on
1098 return; specifically, the string-base-pointer may have been changed.
1099
1100 Arguments:
1101   g             the growable-string
1102   count         amount needed for g->ptr to increase by
1103 */
1104
1105 static void
1106 gstring_grow(gstring * g, int count)
1107 {
1108 int p = g->ptr;
1109 int oldsize = g->size;
1110
1111 /* Mostly, string_cat() is used to build small strings of a few hundred
1112 characters at most. There are times, however, when the strings are very much
1113 longer (for example, a lookup that returns a vast number of alias addresses).
1114 To try to keep things reasonable, we use increments whose size depends on the
1115 existing length of the string. */
1116
1117 unsigned inc = oldsize < 4096 ? 127 : 1023;
1118
1119 if (g->ptr < 0 || g->ptr > g->size || g->size >= INT_MAX/2)
1120   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1121     "internal error in gstring_grow (ptr %d size %d)", g->ptr, g->size);
1122
1123 if (count <= 0) return;
1124
1125 if (count >= INT_MAX/2 - g->ptr)
1126   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1127     "internal error in gstring_grow (ptr %d count %d)", g->ptr, count);
1128
1129 g->size = (p + count + inc + 1) & ~inc;         /* one for a NUL */
1130
1131 /* Try to extend an existing allocation. If the result of calling
1132 store_extend() is false, either there isn't room in the current memory block,
1133 or this string is not the top item on the dynamic store stack. We then have
1134 to get a new chunk of store and copy the old string. When building large
1135 strings, it is helpful to call store_release() on the old string, to release
1136 memory blocks that have become empty. (The block will be freed if the string
1137 is at its start.) However, we can do this only if we know that the old string
1138 was the last item on the dynamic memory stack. This is the case if it matches
1139 store_last_get. */
1140
1141 if (!store_extend(g->s, oldsize, g->size))
1142   g->s = store_newblock(g->s, g->size, p);
1143 }
1144
1145
1146
1147 /*************************************************
1148 *             Add chars to string                *
1149 *************************************************/
1150 /* This function is used when building up strings of unknown length. Room is
1151 always left for a terminating zero to be added to the string that is being
1152 built. This function does not require the string that is being added to be NUL
1153 terminated, because the number of characters to add is given explicitly. It is
1154 sometimes called to extract parts of other strings.
1155
1156 Arguments:
1157   g        growable-string that is being built, or NULL if not assigned yet
1158   s        points to characters to add
1159   count    count of characters to add; must not exceed the length of s, if s
1160              is a C string.
1161
1162 Returns:   growable string, changed if copied for expansion.
1163            Note that a NUL is not added, though space is left for one. This is
1164            because string_cat() is often called multiple times to build up a
1165            string - there's no point adding the NUL till the end.
1166            NULL is a possible return.
1167
1168 */
1169 /* coverity[+alloc] */
1170
1171 gstring *
1172 string_catn(gstring * g, const uschar * s, int count)
1173 {
1174 int p;
1175
1176 if (count < 0)
1177   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1178     "internal error in string_catn (count %d)", count);
1179 if (count == 0) return g;
1180
1181 /*debug_printf("string_catn '%.*s'\n", count, s);*/
1182 if (!g)
1183   {
1184   unsigned inc = count < 4096 ? 127 : 1023;
1185   unsigned size = ((count + inc) &  ~inc) + 1;  /* round up requested count */
1186   g = string_get_tainted(size, s);
1187   }
1188 else if (!g->s)                 /* should not happen */
1189   {
1190   g->s = string_copyn(s, count);
1191   g->ptr = count;
1192   g->size = count;      /*XXX suboptimal*/
1193   return g;
1194   }
1195 else if (is_incompatible(g->s, s))
1196   {
1197 /* debug_printf("rebuf A\n"); */
1198   gstring_rebuffer(g, s);
1199   }
1200
1201 if (g->ptr < 0 || g->ptr > g->size)
1202   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1203     "internal error in string_catn (ptr %d size %d)", g->ptr, g->size);
1204
1205 p = g->ptr;
1206 if (count >= g->size - p)
1207   gstring_grow(g, count);
1208
1209 /* Because we always specify the exact number of characters to copy, we can
1210 use memcpy(), which is likely to be more efficient than strncopy() because the
1211 latter has to check for zero bytes. */
1212
1213 memcpy(g->s + p, s, count);
1214 g->ptr = p + count;
1215 return g;
1216 }
1217
1218
1219 gstring *
1220 string_cat(gstring * g, const uschar * s)
1221 {
1222 return string_catn(g, s, Ustrlen(s));
1223 }
1224
1225
1226
1227 /*************************************************
1228 *        Append strings to another string        *
1229 *************************************************/
1230
1231 /* This function can be used to build a string from many other strings.
1232 It calls string_cat() to do the dirty work.
1233
1234 Arguments:
1235   g        growable-string that is being built, or NULL if not yet assigned
1236   count    the number of strings to append
1237   ...      "count" uschar* arguments, which must be valid zero-terminated
1238              C strings
1239
1240 Returns:   growable string, changed if copied for expansion.
1241            The string is not zero-terminated - see string_cat() above.
1242 */
1243
1244 __inline__ gstring *
1245 string_append(gstring * g, int count, ...)
1246 {
1247 va_list ap;
1248
1249 va_start(ap, count);
1250 while (count-- > 0)
1251   {
1252   uschar * t = va_arg(ap, uschar *);
1253   g = string_cat(g, t);
1254   }
1255 va_end(ap);
1256
1257 return g;
1258 }
1259 #endif
1260
1261
1262
1263 /*************************************************
1264 *        Format a string with length checks      *
1265 *************************************************/
1266
1267 /* This function is used to format a string with checking of the length of the
1268 output for all conversions. It protects Exim from absent-mindedness when
1269 calling functions like debug_printf and string_sprintf, and elsewhere. There
1270 are two different entry points to what is actually the same function, depending
1271 on whether the variable length list of data arguments are given explicitly or
1272 as a va_list item.
1273
1274 The formats are the usual printf() ones, with some omissions (never used) and
1275 three additions for strings: %S forces lower case, %T forces upper case, and
1276 %#s or %#S prints nothing for a NULL string. Without the # "NULL" is printed
1277 (useful in debugging). There is also the addition of %D and %M, which insert
1278 the date in the form used for datestamped log files.
1279
1280 Arguments:
1281   buffer       a buffer in which to put the formatted string
1282   buflen       the length of the buffer
1283   format       the format string - deliberately char * and not uschar *
1284   ... or ap    variable list of supplementary arguments
1285
1286 Returns:       TRUE if the result fitted in the buffer
1287 */
1288
1289 BOOL
1290 string_format_trc(uschar * buffer, int buflen,
1291   const uschar * func, unsigned line, const char * format, ...)
1292 {
1293 gstring g = { .size = buflen, .ptr = 0, .s = buffer }, * gp;
1294 va_list ap;
1295 va_start(ap, format);
1296 gp = string_vformat_trc(&g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1297         0, format, ap);
1298 va_end(ap);
1299 g.s[g.ptr] = '\0';
1300 return !!gp;
1301 }
1302
1303
1304
1305
1306 /* Build or append to a growing-string, sprintf-style.
1307
1308 Arguments:
1309         g       a growable-string
1310         func    called-from function name, for debug
1311         line    called-from file line number, for debug
1312         limit   maximum string size
1313         flags   see below
1314         format  printf-like format string
1315         ap      variable-args pointer
1316
1317 Flags:
1318         SVFMT_EXTEND            buffer can be created or exteded as needed
1319         SVFMT_REBUFFER          buffer can be recopied to tainted mem as needed
1320         SVFMT_TAINT_NOCHK       do not check inputs for taint
1321
1322 If the "extend" flag is true, the string passed in can be NULL,
1323 empty, or non-empty.  Growing is subject to an overall limit given
1324 by the limit argument.
1325
1326 If the "extend" flag is false, the string passed in may not be NULL,
1327 will not be grown, and is usable in the original place after return.
1328 The return value can be NULL to signify overflow.
1329
1330 Returns the possibly-new (if copy for growth or taint-handling was needed)
1331 string, not nul-terminated.
1332 */
1333
1334 gstring *
1335 string_vformat_trc(gstring * g, const uschar * func, unsigned line,
1336   unsigned size_limit, unsigned flags, const char * format, va_list ap)
1337 {
1338 enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
1339
1340 int width, precision, off, lim, need;
1341 const char * fp = format;       /* Deliberately not unsigned */
1342
1343 string_datestamp_offset = -1;   /* Datestamp not inserted */
1344 string_datestamp_length = 0;    /* Datestamp not inserted */
1345 string_datestamp_type = 0;      /* Datestamp not inserted */
1346
1347 #ifdef COMPILE_UTILITY
1348 assert(!(flags & SVFMT_EXTEND));
1349 assert(g);
1350 #else
1351
1352 /* Ensure we have a string, to save on checking later */
1353 if (!g) g = string_get(16);
1354
1355 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, format))
1356   {
1357 #ifndef MACRO_PREDEF
1358   if (!(flags & SVFMT_REBUFFER))
1359     die_tainted(US"string_vformat", func, line);
1360 #endif
1361 /* debug_printf("rebuf B\n"); */
1362   gstring_rebuffer(g, format);
1363   }
1364 #endif  /*!COMPILE_UTILITY*/
1365
1366 lim = g->size - 1;      /* leave one for a nul */
1367 off = g->ptr;           /* remember initial offset in gstring */
1368
1369 /* Scan the format and handle the insertions */
1370
1371 while (*fp)
1372   {
1373   int length = L_NORMAL;
1374   int * nptr;
1375   int slen;
1376   const char *null = "NULL";            /* ) These variables */
1377   const char *item_start, *s;           /* ) are deliberately */
1378   char newformat[16];                   /* ) not unsigned */
1379   char * gp = CS g->s + g->ptr;         /* ) */
1380
1381   /* Non-% characters just get copied verbatim */
1382
1383   if (*fp != '%')
1384     {
1385     /* Avoid string_copyn() due to COMPILE_UTILITY */
1386     if ((need = g->ptr + 1) > lim)
1387       {
1388       if (!(flags & SVFMT_EXTEND) || need > size_limit) return NULL;
1389       gstring_grow(g, 1);
1390       lim = g->size - 1;
1391       }
1392     g->s[g->ptr++] = (uschar) *fp++;
1393     continue;
1394     }
1395
1396   /* Deal with % characters. Pick off the width and precision, for checking
1397   strings, skipping over the flag and modifier characters. */
1398
1399   item_start = fp;
1400   width = precision = -1;
1401
1402   if (strchr("-+ #0", *(++fp)) != NULL)
1403     {
1404     if (*fp == '#') null = "";
1405     fp++;
1406     }
1407
1408   if (isdigit((uschar)*fp))
1409     {
1410     width = *fp++ - '0';
1411     while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
1412     }
1413   else if (*fp == '*')
1414     {
1415     width = va_arg(ap, int);
1416     fp++;
1417     }
1418
1419   if (*fp == '.')
1420     if (*(++fp) == '*')
1421       {
1422       precision = va_arg(ap, int);
1423       fp++;
1424       }
1425     else
1426       for (precision = 0; isdigit((uschar)*fp); fp++)
1427         precision = precision*10 + *fp - '0';
1428
1429   /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
1430
1431   if (*fp == 'h')
1432     { fp++; length = L_SHORT; }
1433   else if (*fp == 'L')
1434     { fp++; length = L_LONGDOUBLE; }
1435   else if (*fp == 'l')
1436     if (fp[1] == 'l')
1437       { fp += 2; length = L_LONGLONG; }
1438     else
1439       { fp++; length = L_LONG; }
1440   else if (*fp == 'z')
1441     { fp++; length = L_SIZE; }
1442
1443   /* Handle each specific format type. */
1444
1445   switch (*fp++)
1446     {
1447     case 'n':
1448       nptr = va_arg(ap, int *);
1449       *nptr = g->ptr - off;
1450       break;
1451
1452     case 'd':
1453     case 'o':
1454     case 'u':
1455     case 'x':
1456     case 'X':
1457       width = length > L_LONG ? 24 : 12;
1458       if ((need = g->ptr + width) > lim)
1459         {
1460         if (!(flags & SVFMT_EXTEND) || need >= size_limit) return NULL;
1461         gstring_grow(g, width);
1462         lim = g->size - 1;
1463         gp = CS g->s + g->ptr;
1464         }
1465       strncpy(newformat, item_start, fp - item_start);
1466       newformat[fp - item_start] = 0;
1467
1468       /* Short int is promoted to int when passing through ..., so we must use
1469       int for va_arg(). */
1470
1471       switch(length)
1472         {
1473         case L_SHORT:
1474         case L_NORMAL:
1475           g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break;
1476         case L_LONG:
1477           g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break;
1478         case L_LONGLONG:
1479           g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break;
1480         case L_SIZE:
1481           g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break;
1482         }
1483       break;
1484
1485     case 'p':
1486       {
1487       void * ptr;
1488       if ((need = g->ptr + 24) > lim)
1489         {
1490         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1491         gstring_grow(g, 24);
1492         lim = g->size - 1;
1493         gp = CS g->s + g->ptr;
1494         }
1495       /* sprintf() saying "(nil)" for a null pointer seems unreliable.
1496       Handle it explicitly. */
1497       if ((ptr = va_arg(ap, void *)))
1498         {
1499         strncpy(newformat, item_start, fp - item_start);
1500         newformat[fp - item_start] = 0;
1501         g->ptr += sprintf(gp, newformat, ptr);
1502         }
1503       else
1504         g->ptr += sprintf(gp, "(nil)");
1505       }
1506     break;
1507
1508     /* %f format is inherently insecure if the numbers that it may be
1509     handed are unknown (e.g. 1e300). However, in Exim, %f is used for
1510     printing load averages, and these are actually stored as integers
1511     (load average * 1000) so the size of the numbers is constrained.
1512     It is also used for formatting sending rates, where the simplicity
1513     of the format prevents overflow. */
1514
1515     case 'f':
1516     case 'e':
1517     case 'E':
1518     case 'g':
1519     case 'G':
1520       if (precision < 0) precision = 6;
1521       if ((need = g->ptr + precision + 8) > lim)
1522         {
1523         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1524         gstring_grow(g, precision+8);
1525         lim = g->size - 1;
1526         gp = CS g->s + g->ptr;
1527         }
1528       strncpy(newformat, item_start, fp - item_start);
1529       newformat[fp-item_start] = 0;
1530       if (length == L_LONGDOUBLE)
1531         g->ptr += sprintf(gp, newformat, va_arg(ap, long double));
1532       else
1533         g->ptr += sprintf(gp, newformat, va_arg(ap, double));
1534       break;
1535
1536     /* String types */
1537
1538     case '%':
1539       if ((need = g->ptr + 1) > lim)
1540         {
1541         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1542         gstring_grow(g, 1);
1543         lim = g->size - 1;
1544         }
1545       g->s[g->ptr++] = (uschar) '%';
1546       break;
1547
1548     case 'c':
1549       if ((need = g->ptr + 1) > lim)
1550         {
1551         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1552         gstring_grow(g, 1);
1553         lim = g->size - 1;
1554         }
1555       g->s[g->ptr++] = (uschar) va_arg(ap, int);
1556       break;
1557
1558     case 'D':                   /* Insert daily datestamp for log file names */
1559       s = CS tod_stamp(tod_log_datestamp_daily);
1560       string_datestamp_offset = g->ptr;         /* Passed back via global */
1561       string_datestamp_length = Ustrlen(s);     /* Passed back via global */
1562       string_datestamp_type = tod_log_datestamp_daily;
1563       slen = string_datestamp_length;
1564       goto INSERT_STRING;
1565
1566     case 'M':                   /* Insert monthly datestamp for log file names */
1567       s = CS tod_stamp(tod_log_datestamp_monthly);
1568       string_datestamp_offset = g->ptr;         /* Passed back via global */
1569       string_datestamp_length = Ustrlen(s);     /* Passed back via global */
1570       string_datestamp_type = tod_log_datestamp_monthly;
1571       slen = string_datestamp_length;
1572       goto INSERT_STRING;
1573
1574     case 's':
1575     case 'S':                   /* Forces *lower* case */
1576     case 'T':                   /* Forces *upper* case */
1577       s = va_arg(ap, char *);
1578
1579       if (!s) s = null;
1580       slen = Ustrlen(s);
1581
1582       if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
1583         if (flags & SVFMT_REBUFFER)
1584           {
1585 /* debug_printf("%s %d: untainted workarea, tainted %%s :- rebuffer\n", __FUNCTION__, __LINE__); */
1586           gstring_rebuffer(g, s);
1587           gp = CS g->s + g->ptr;
1588           }
1589 #ifndef MACRO_PREDEF
1590         else
1591           die_tainted(US"string_vformat", func, line);
1592 #endif
1593
1594     INSERT_STRING:              /* Come to from %D or %M above */
1595
1596       {
1597       BOOL truncated = FALSE;
1598
1599       /* If the width is specified, check that there is a precision
1600       set; if not, set it to the width to prevent overruns of long
1601       strings. */
1602
1603       if (width >= 0)
1604         {
1605         if (precision < 0) precision = width;
1606         }
1607
1608       /* If a width is not specified and the precision is specified, set
1609       the width to the precision, or the string length if shorted. */
1610
1611       else if (precision >= 0)
1612         width = precision < slen ? precision : slen;
1613
1614       /* If neither are specified, set them both to the string length. */
1615
1616       else
1617         width = precision = slen;
1618
1619       if ((need = g->ptr + width) >= size_limit || !(flags & SVFMT_EXTEND))
1620         {
1621         if (g->ptr == lim) return NULL;
1622         if (need > lim)
1623           {
1624           truncated = TRUE;
1625           width = precision = lim - g->ptr - 1;
1626           if (width < 0) width = 0;
1627           if (precision < 0) precision = 0;
1628           }
1629         }
1630       else if (need > lim)
1631         {
1632         gstring_grow(g, width);
1633         lim = g->size - 1;
1634         gp = CS g->s + g->ptr;
1635         }
1636
1637       g->ptr += sprintf(gp, "%*.*s", width, precision, s);
1638       if (fp[-1] == 'S')
1639         while (*gp) { *gp = tolower(*gp); gp++; }
1640       else if (fp[-1] == 'T')
1641         while (*gp) { *gp = toupper(*gp); gp++; }
1642
1643       if (truncated) return NULL;
1644       break;
1645       }
1646
1647     /* Some things are never used in Exim; also catches junk. */
1648
1649     default:
1650       strncpy(newformat, item_start, fp - item_start);
1651       newformat[fp-item_start] = 0;
1652       log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
1653         "in \"%s\" in \"%s\"", newformat, format);
1654       break;
1655     }
1656   }
1657
1658 if (g->ptr > g->size)
1659   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1660     "string_format internal error: caller %s %d", func, line);
1661 return g;
1662 }
1663
1664
1665
1666 #ifndef COMPILE_UTILITY
1667 /*************************************************
1668 *       Generate an "open failed" message        *
1669 *************************************************/
1670
1671 /* This function creates a message after failure to open a file. It includes a
1672 string supplied as data, adds the strerror() text, and if the failure was
1673 "Permission denied", reads and includes the euid and egid.
1674
1675 Arguments:
1676   format        a text format string - deliberately not uschar *
1677   func          caller, for debug
1678   line          caller, for debug
1679   ...           arguments for the format string
1680
1681 Returns:        a message, in dynamic store
1682 */
1683
1684 uschar *
1685 string_open_failed_trc(const uschar * func, unsigned line,
1686   const char * format, ...)
1687 {
1688 va_list ap;
1689 gstring * g = string_get(1024);
1690
1691 g = string_catn(g, US"failed to open ", 15);
1692
1693 /* Use the checked formatting routine to ensure that the buffer
1694 does not overflow. It should not, since this is called only for internally
1695 specified messages. If it does, the message just gets truncated, and there
1696 doesn't seem much we can do about that. */
1697
1698 va_start(ap, format);
1699 (void) string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1700         SVFMT_REBUFFER, format, ap);
1701 va_end(ap);
1702
1703 g = string_catn(g, US": ", 2);
1704 g = string_cat(g, US strerror(errno));
1705
1706 if (errno == EACCES)
1707   {
1708   int save_errno = errno;
1709   g = string_fmt_append(g, " (euid=%ld egid=%ld)",
1710     (long int)geteuid(), (long int)getegid());
1711   errno = save_errno;
1712   }
1713 gstring_release_unused(g);
1714 return string_from_gstring(g);
1715 }
1716
1717
1718
1719
1720
1721 /* qsort(3), currently used to sort the environment variables
1722 for -bP environment output, needs a function to compare two pointers to string
1723 pointers. Here it is. */
1724
1725 int
1726 string_compare_by_pointer(const void *a, const void *b)
1727 {
1728 return Ustrcmp(* CUSS a, * CUSS b);
1729 }
1730 #endif /* COMPILE_UTILITY */
1731
1732
1733
1734
1735 /*************************************************
1736 **************************************************
1737 *             Stand-alone test program           *
1738 **************************************************
1739 *************************************************/
1740
1741 #ifdef STAND_ALONE
1742 int main(void)
1743 {
1744 uschar buffer[256];
1745
1746 printf("Testing is_ip_address\n");
1747 store_init();
1748
1749 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1750   {
1751   int offset;
1752   buffer[Ustrlen(buffer) - 1] = 0;
1753   printf("%d\n", string_is_ip_address(buffer, NULL));
1754   printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
1755   }
1756
1757 printf("Testing string_nextinlist\n");
1758
1759 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1760   {
1761   uschar *list = buffer;
1762   uschar *lp1, *lp2;
1763   uschar item[256];
1764   int sep1 = 0;
1765   int sep2 = 0;
1766
1767   if (*list == '<')
1768     {
1769     sep1 = sep2 = list[1];
1770     list += 2;
1771     }
1772
1773   lp1 = lp2 = list;
1774   for (;;)
1775     {
1776     uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
1777     uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
1778
1779     if (item1 == NULL && item2 == NULL) break;
1780     if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
1781       {
1782       printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
1783         (item1 == NULL)? "NULL" : CS item1,
1784         (item2 == NULL)? "NULL" : CS item2);
1785       break;
1786       }
1787     else printf("  \"%s\"\n", CS item1);
1788     }
1789   }
1790
1791 /* This is a horrible lash-up, but it serves its purpose. */
1792
1793 printf("Testing string_format\n");
1794
1795 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1796   {
1797   void *args[3];
1798   long long llargs[3];
1799   double dargs[3];
1800   int dflag = 0;
1801   int llflag = 0;
1802   int n = 0;
1803   int count;
1804   BOOL countset = FASE;
1805   uschar format[256];
1806   uschar outbuf[256];
1807   uschar *s;
1808   buffer[Ustrlen(buffer) - 1] = 0;
1809
1810   s = Ustrchr(buffer, ',');
1811   if (s == NULL) s = buffer + Ustrlen(buffer);
1812
1813   Ustrncpy(format, buffer, s - buffer);
1814   format[s-buffer] = 0;
1815
1816   if (*s == ',') s++;
1817
1818   while (*s != 0)
1819     {
1820     uschar *ss = s;
1821     s = Ustrchr(ss, ',');
1822     if (s == NULL) s = ss + Ustrlen(ss);
1823
1824     if (isdigit(*ss))
1825       {
1826       Ustrncpy(outbuf, ss, s-ss);
1827       if (Ustrchr(outbuf, '.') != NULL)
1828         {
1829         dflag = 1;
1830         dargs[n++] = Ustrtod(outbuf, NULL);
1831         }
1832       else if (Ustrstr(outbuf, "ll") != NULL)
1833         {
1834         llflag = 1;
1835         llargs[n++] = strtoull(CS outbuf, NULL, 10);
1836         }
1837       else
1838         {
1839         args[n++] = (void *)Uatoi(outbuf);
1840         }
1841       }
1842
1843     else if (Ustrcmp(ss, "*") == 0)
1844       {
1845       args[n++] = (void *)(&count);
1846       countset = TRUE;
1847       }
1848
1849     else
1850       {
1851       uschar *sss = malloc(s - ss + 1);
1852       Ustrncpy(sss, ss, s-ss);
1853       args[n++] = sss;
1854       }
1855
1856     if (*s == ',') s++;
1857     }
1858
1859   if (!dflag && !llflag)
1860     printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1861       args[0], args[1], args[2])? "True" : "False");
1862
1863   else if (dflag)
1864     printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1865       dargs[0], dargs[1], dargs[2])? "True" : "False");
1866
1867   else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1868     llargs[0], llargs[1], llargs[2])? "True" : "False");
1869
1870   printf("%s\n", CS outbuf);
1871   if (countset) printf("count=%d\n", count);
1872   }
1873
1874 return 0;
1875 }
1876 #endif
1877
1878 /* End of string.c */