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