1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
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 */
10 /* Miscellaneous string-handling functions. Some are not required for
11 utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
18 #ifndef COMPILE_UTILITY
19 /*************************************************
20 * Test for IP address *
21 *************************************************/
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.
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 errp NULL if no diagnostic information is required, and if the netmask
34 length should not be checked. Otherwise it is set pointing to a short
37 Returns: 0 if the string is not a textual representation of an IP address
38 4 if it is an IPv4 address
39 6 if it is an IPv6 address
41 The legacy string_is_ip_address() function follows below.
45 string_is_ip_addressX(const uschar * ip_addr, int * maskptr, const uschar ** errp)
47 struct addrinfo hints, * res;
48 uschar * slash, * percent, * endp = NULL;
50 const uschar * addr = NULL;
52 union { /* we do not need this, but inet_pton() needs a place for storage */
57 /* If there is a slash, but we didn't request a (optional) netmask,
58 we return failure, as we do if the mask isn't a pure numerical value,
59 or if it is negative. The actual length is checked later, once we know
60 the address family. */
61 if (slash = Ustrchr(ip_addr, '/'))
67 if (errp) *errp = US"netmask found, but not requested";
71 mask = Ustrtol(slash+1, &rest, 10);
72 if (*rest || mask < 0)
74 if (errp) *errp = US"netmask not numeric or <0";
78 *maskptr = slash - ip_addr; /* offset of the slash */
81 else if (maskptr) *maskptr = 0; /* no slash found */
83 /* The interface-ID suffix (%<id>) is optional (for IPv6). If it
84 exists, we check it syntactically. Later, if we know the address
85 family is IPv4, we might reject it.
86 The interface-ID is mutually exclusive with the netmask, to the
87 best of my knowledge. */
89 if (percent = Ustrchr(ip_addr, '%'))
93 if (errp) *errp = US"interface-ID and netmask are mutually exclusive";
96 for (uschar *p = percent+1; *p; p++)
97 if (!isalnum(*p) && !ispunct(*p))
99 if (errp) *errp = US"interface-ID must match [[:alnum:][:punct:]]";
105 /* inet_pton() can't parse netmasks and interface IDs, so work on a shortened copy
106 allocated on the current stack */
110 ptrdiff_t l = endp - ip_addr;
113 if (errp) *errp = US"rudiculous long ip address string";
116 addr = string_copyn(ip_addr, l);
121 af = Ustrchr(addr, ':') ? AF_INET6 : AF_INET;
122 if (!inet_pton(af, CCS addr, &sa))
124 if (errp) *errp = af == AF_INET6 ? US"IP address string not parsable as IPv6"
125 : US"IP address string not parsable IPv4";
129 /* we do not check the values of the mask here, as
130 this is done on the callers side (but I don't understand why), so
131 actually I'd like to do it here, but it breaks at least testcase 0002 */
136 if (errp && mask > 128)
138 *errp = US"IPv6 netmask value must not be >128";
145 if (errp) *errp = US"IPv4 address string must not have an interface-ID";
148 if (errp && mask > 32)
150 *errp = US"IPv4 netmask value must not be >32";
155 if (errp) *errp = US"unknown address family (should not happen)";
162 string_is_ip_address(const uschar * ip_addr, int * maskptr)
164 return string_is_ip_addressX(ip_addr, maskptr, 0);
167 #endif /* COMPILE_UTILITY */
170 /*************************************************
171 * Format message size *
172 *************************************************/
174 /* Convert a message size in bytes to printing form, rounding
175 according to the magnitude of the number. A value of zero causes
176 a string of spaces to be returned.
179 size the message size in bytes
180 buffer where to put the answer
182 Returns: pointer to the buffer
183 a string of exactly 5 characters is normally returned
187 string_format_size(int size, uschar *buffer)
189 if (size == 0) Ustrcpy(buffer, US" ");
190 else if (size < 1024) sprintf(CS buffer, "%5d", size);
191 else if (size < 10*1024)
192 sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
193 else if (size < 1024*1024)
194 sprintf(CS buffer, "%4dK", (size + 512)/1024);
195 else if (size < 10*1024*1024)
196 sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
198 sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
204 #ifndef COMPILE_UTILITY
205 /*************************************************
206 * Convert a number to base 62 format *
207 *************************************************/
209 /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
210 BASE_62 is actually 36. Always return exactly 6 characters plus a NUL, in a
211 static area. This is enough for a 32b input, for 62 (for 64b we would want 11+nul);
212 but with 36 we lose half the input range of a 32b input.
214 Argument: a long integer
215 Returns: pointer to base 62 string
219 string_base62_32(unsigned long int value)
221 static uschar yield[7];
222 uschar * p = yield + sizeof(yield) - 1;
226 *--p = base62_chars[value % BASE_62];
233 string_base62_64(unsigned long int value)
235 static uschar yield[12];
236 uschar * p = yield + sizeof(yield) - 1;
241 *--p = base62_chars[value % BASE_62];
248 #endif /* COMPILE_UTILITY */
252 /*************************************************
253 * Interpret escape sequence *
254 *************************************************/
256 /* This function is called from several places where escape sequences are to be
257 interpreted in strings.
260 pp points a pointer to the initiating "\" in the string;
261 the pointer gets updated to point to the final character
262 If the backslash is the last character in the string, it
264 Returns: the value of the character escape
268 string_interpret_escape(const uschar **pp)
270 #ifdef COMPILE_UTILITY
271 const uschar *hex_digits= CUS"0123456789abcdef";
274 const uschar *p = *pp;
276 if (ch == '\0') return **pp;
277 if (isdigit(ch) && ch != '8' && ch != '9')
280 if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
282 ch = ch * 8 + *(++p) - '0';
283 if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
284 ch = ch * 8 + *(++p) - '0';
289 case 'b': ch = '\b'; break;
290 case 'f': ch = '\f'; break;
291 case 'n': ch = '\n'; break;
292 case 'r': ch = '\r'; break;
293 case 't': ch = '\t'; break;
294 case 'v': ch = '\v'; break;
300 Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
301 if (isxdigit(p[1])) ch = ch * 16 +
302 Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
312 #ifndef COMPILE_UTILITY
313 /*************************************************
314 * Ensure string is printable *
315 *************************************************/
317 /* This function is called for critical strings. It checks for any
318 non-printing characters, and if any are found, it makes a new copy
319 of the string with suitable escape sequences. It is most often called by the
320 macro string_printing(), which sets flags to 0.
324 flags Bit 0: convert tabs. Bit 1: convert spaces.
326 Returns: string with non-printers encoded as printing sequences
330 string_printing2(const uschar *s, int flags)
332 int nonprintcount = 0;
341 || flags & SP_TAB && c == '\t'
342 || flags & SP_SPACE && c == ' '
347 if (nonprintcount == 0) return s;
349 /* Get a new block of store guaranteed big enough to hold the
352 tt = ss = store_get(length + nonprintcount * 3 + 1, s);
354 /* Copy everything, escaping non printers. */
360 && (!(flags & SP_TAB) || c != '\t')
361 && (!(flags & SP_SPACE) || c != ' ')
369 case '\n': *tt++ = 'n'; break;
370 case '\r': *tt++ = 'r'; break;
371 case '\b': *tt++ = 'b'; break;
372 case '\v': *tt++ = 'v'; break;
373 case '\f': *tt++ = 'f'; break;
374 case '\t': *tt++ = 't'; break;
375 default: sprintf(CS tt, "%03o", *t); tt += 3; break;
383 #endif /* COMPILE_UTILITY */
385 /*************************************************
386 * Undo printing escapes in string *
387 *************************************************/
389 /* This function is the reverse of string_printing2. It searches for
390 backslash characters and if any are found, it makes a new copy of the
391 string with escape sequences parsed. Otherwise it returns the original
397 Returns: string with printing escapes parsed back
401 string_unprinting(uschar *s)
403 uschar *p, *q, *r, *ss;
406 p = Ustrchr(s, '\\');
409 len = Ustrlen(s) + 1;
410 ss = store_get(len, s);
424 *q++ = string_interpret_escape((const uschar **)&p);
429 r = Ustrchr(p, '\\');
455 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
456 && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
457 /*************************************************
458 * Copy and save string *
459 *************************************************/
462 Argument: string to copy
463 Returns: copy of string in new store with the same taint status
467 string_copy_function(const uschar * s)
469 return string_copy_taint(s, s);
472 /* As above, but explicitly specifying the result taint status
476 string_copy_taint_function(const uschar * s, const void * proto_mem)
478 return string_copy_taint(s, proto_mem);
483 /*************************************************
484 * Copy and save string, given length *
485 *************************************************/
487 /* It is assumed the data contains no zeros. A zero is added
492 n number of characters
494 Returns: copy of string in new store
498 string_copyn_function(const uschar * s, int n)
500 return string_copyn(s, n);
505 /*************************************************
506 * Copy and save string in malloc'd store *
507 *************************************************/
509 /* This function assumes that memcpy() is faster than strcpy().
511 Argument: string to copy
512 Returns: copy of string in new store
516 string_copy_malloc(const uschar * s)
518 int len = Ustrlen(s) + 1;
519 uschar * ss = store_malloc(len);
526 /*************************************************
527 * Copy string if long, inserting newlines *
528 *************************************************/
530 /* If the given string is longer than 75 characters, it is copied, and within
531 the copy, certain space characters are converted into newlines.
533 Argument: pointer to the string
534 Returns: pointer to the possibly altered string
538 string_split_message(uschar * msg)
542 if (!msg || Ustrlen(msg) <= 75) return msg;
543 s = ss = msg = string_copy(msg);
548 while (i < 75 && *ss && *ss != '\n') ss++, i++;
560 if (t[-1] == ':') { tt = t; break; }
565 if (!tt) /* Can't split behind - try ahead */
570 if (*t == ' ' || *t == '\n')
576 if (!tt) break; /* Can't find anywhere to split */
587 /*************************************************
588 * Copy returned DNS domain name, de-escaping *
589 *************************************************/
591 /* If a domain name contains top-bit characters, some resolvers return
592 the fully qualified name with those characters turned into escapes. The
593 convention is a backslash followed by _decimal_ digits. We convert these
594 back into the original binary values. This will be relevant when
595 allow_utf8_domains is set true and UTF-8 characters are used in domain
596 names. Backslash can also be used to escape other characters, though we
597 shouldn't come across them in domain names.
599 Argument: the domain name string
600 Returns: copy of string in new store, de-escaped
604 string_copy_dnsdomain(uschar * s)
607 uschar * ss = yield = store_get(Ustrlen(s) + 1, GET_TAINTED); /* always treat as tainted */
613 else if (isdigit(s[1]))
615 *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
627 #ifndef COMPILE_UTILITY
628 /*************************************************
629 * Copy space-terminated or quoted string *
630 *************************************************/
632 /* This function copies from a string until its end, or until whitespace is
633 encountered, unless the string begins with a double quote, in which case the
634 terminating quote is sought, and escaping within the string is done. The length
635 of a de-quoted string can be no longer than the original, since escaping always
636 turns n characters into 1 character.
638 Argument: pointer to the pointer to the first character, which gets updated
639 Returns: the new string
643 string_dequote(const uschar ** sptr)
645 const uschar * s = * sptr;
648 /* First find the end of the string */
651 while (*s && !isspace(*s)) s++;
655 while (*s && *s != '\"')
657 if (*s == '\\') (void)string_interpret_escape(&s);
663 /* Get enough store to copy into */
665 t = yield = store_get(s - *sptr + 1, *sptr);
671 while (*s && !isspace(*s)) *t++ = *s++;
675 while (*s && *s != '\"')
677 *t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
683 /* Update the pointer and return the terminated copy */
689 #endif /* COMPILE_UTILITY */
693 /*************************************************
694 * Format a string and save it *
695 *************************************************/
697 /* The formatting is done by string_vformat, which checks the length of
698 everything. Taint is taken from the worst of the arguments.
701 format a printf() format - deliberately char * rather than uschar *
702 because it will most usually be a literal string
703 func caller, for debug
704 line caller, for debug
705 ... arguments for format
707 Returns: pointer to fresh piece of store containing sprintf'ed string
711 string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
713 #ifdef COMPILE_UTILITY
714 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
715 gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
720 unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
725 g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
730 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
731 "string_sprintf expansion was longer than %d; format string was (%s)\n"
732 " called from %s %d\n",
733 STRING_SPRINTF_BUFFER_SIZE, format, func, line);
735 #ifdef COMPILE_UTILITY
736 return string_copyn(g->s, g->ptr);
738 gstring_release_unused(g);
739 return string_from_gstring(g);
745 /*************************************************
746 * Case-independent strncmp() function *
747 *************************************************/
753 n number of characters to compare
755 Returns: < 0, = 0, or > 0, according to the comparison
759 strncmpic(const uschar * s, const uschar * t, int n)
763 int c = tolower(*s++) - tolower(*t++);
770 /*************************************************
771 * Case-independent strcmp() function *
772 *************************************************/
779 Returns: < 0, = 0, or > 0, according to the comparison
783 strcmpic(const uschar * s, const uschar * t)
787 int c = tolower(*s++) - tolower(*t++);
788 if (c != 0) return c;
794 /*************************************************
795 * Case-independent strstr() function *
796 *************************************************/
798 /* The third argument specifies whether whitespace is required
799 to follow the matched string.
803 t substring to search for
804 space_follows if TRUE, match only if whitespace follows
806 Returns: pointer to substring in string, or NULL if not found
810 strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
812 const uschar * p = t;
813 const uschar * yield = NULL;
814 int cl = tolower(*p);
815 int cu = toupper(*p);
819 if (*s == cl || *s == cu)
821 if (!yield) yield = s;
824 if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
845 strstric(uschar * s, uschar * t, BOOL space_follows)
847 return US strstric_c(s, t, space_follows);
851 #ifdef COMPILE_UTILITY
852 /* Dummy version for this function; it should never be called */
854 gstring_grow(gstring * g, int count)
862 #ifndef COMPILE_UTILITY
863 /*************************************************
864 * Get next string from separated list *
865 *************************************************/
867 /* Leading and trailing space is removed from each item. The separator in the
868 list is controlled by the int pointed to by the separator argument as follows:
870 If the value is > 0 it is used as the separator. This is typically used for
871 sublists such as slash-separated options. The value is always a printing
874 (If the value is actually > UCHAR_MAX there is only one item in the list.
875 This is used for some cases when called via functions that sometimes
876 plough through lists, and sometimes are given single items.)
878 If the value is <= 0, the string is inspected for a leading <x, where x is an
879 ispunct() or an iscntrl() character. If found, x is used as the separator. If
882 (a) if separator == 0, ':' is used
883 (b) if separator <0, -separator is used
885 In all cases the value of the separator that is used is written back to the
886 int so that it is used on subsequent calls as we progress through the list.
888 A literal ispunct() separator can be represented in an item by doubling, but
889 there is no way to include an iscntrl() separator as part of the data.
892 listptr points to a pointer to the current start of the list; the
893 pointer gets updated to point after the end of the next item
894 separator a pointer to the separator character in an int (see above)
895 buffer where to put a copy of the next string in the list; or
896 NULL if the next string is returned in new memory
897 Note that if the list is tainted then a provided buffer must be
898 also (else we trap, with a message referencing the callsite).
899 If we do the allocation, taint is handled there.
900 buflen when buffer is not NULL, the size of buffer; otherwise ignored
902 func caller, for debug
903 line caller, for debug
905 Returns: pointer to buffer, containing the next substring,
906 or NULL if no more substrings
910 string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
911 int buflen, const uschar * func, int line)
913 int sep = *separator;
914 const uschar * s = *listptr;
919 /* This allows for a fixed specified separator to be an iscntrl() character,
920 but at the time of implementation, this is never the case. However, it's best
921 to be conservative. */
923 while (isspace(*s) && *s != sep) s++;
925 /* A change of separator is permitted, so look for a leading '<' followed by an
926 allowed character. */
930 if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
934 while (isspace(*s) && *s != sep) s++;
937 sep = sep ? -sep : ':';
941 /* An empty string has no list elements */
943 if (!*s) return NULL;
945 /* Note whether whether or not the separator is an iscntrl() character. */
947 sep_is_special = iscntrl(sep);
949 /* Handle the case when a buffer is provided. */
950 /*XXX need to also deal with qouted-requirements mismatch */
955 if (is_tainted(s) && !is_tainted(buffer))
956 die_tainted(US"string_nextinlist", func, line);
959 if (*s == sep && (*(++s) != sep || sep_is_special)) break;
960 if (p < buflen - 1) buffer[p++] = *s;
962 while (p > 0 && isspace(buffer[p-1])) p--;
966 /* Handle the case when a buffer is not provided. */
972 /* We know that *s != 0 at this point. However, it might be pointing to a
973 separator, which could indicate an empty string, or (if an ispunct()
974 character) could be doubled to indicate a separator character as data at the
975 start of a string. Avoid getting working memory for an empty item. */
978 if (*++s != sep || sep_is_special)
981 return string_copy(US"");
984 /* Not an empty string; the first character is guaranteed to be a data
990 for (ss = s + 1; *ss && *ss != sep; ) ss++;
991 g = string_catn(g, s, ss-s);
993 if (!*s || *++s != sep || sep_is_special) break;
996 /* Trim trailing spaces from the returned string */
998 /* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
999 while ( g->ptr > 0 && isspace(g->s[g->ptr-1])
1000 && (g->ptr == 1 || g->s[g->ptr-2] != '\\') )
1002 buffer = string_from_gstring(g);
1003 gstring_release_unused_trc(g, CCS func, line);
1006 /* Update the current pointer and return the new string */
1013 static const uschar *
1014 Ustrnchr(const uschar * s, int c, unsigned * len)
1016 unsigned siz = *len;
1019 if (!*s) return NULL;
1032 /************************************************
1033 * Add element to separated list *
1034 ************************************************/
1035 /* This function is used to build a list, returning an allocated null-terminated
1036 growable string. The given element has any embedded separator characters
1039 Despite having the same growable-string interface as string_cat() the list is
1040 always returned null-terminated.
1043 list expanding-string for the list that is being built, or NULL
1044 if this is a new list that has no contents yet
1045 sep list separator character
1046 ele new element to be appended to the list
1048 Returns: pointer to the start of the list, changed if copied for expansion.
1052 string_append_listele(gstring * list, uschar sep, const uschar * ele)
1056 if (list && list->ptr)
1057 list = string_catn(list, &sep, 1);
1059 while((sp = Ustrchr(ele, sep)))
1061 list = string_catn(list, ele, sp-ele+1);
1062 list = string_catn(list, &sep, 1);
1065 list = string_cat(list, ele);
1066 (void) string_from_gstring(list);
1072 string_append_listele_n(gstring * list, uschar sep, const uschar * ele,
1077 if (list && list->ptr)
1078 list = string_catn(list, &sep, 1);
1080 while((sp = Ustrnchr(ele, sep, &len)))
1082 list = string_catn(list, ele, sp-ele+1);
1083 list = string_catn(list, &sep, 1);
1087 list = string_catn(list, ele, len);
1088 (void) string_from_gstring(list);
1094 /* A slightly-bogus listmaker utility; the separator is a string so
1095 can be multiple chars - there is no checking for the element content
1096 containing any of the separator. */
1099 string_append2_listele_n(gstring * list, const uschar * sepstr,
1100 const uschar * ele, unsigned len)
1102 if (list && list->ptr)
1103 list = string_cat(list, sepstr);
1105 list = string_catn(list, ele, len);
1106 (void) string_from_gstring(list);
1112 /************************************************/
1113 /* Add more space to a growable-string. The caller should check
1114 first if growth is required. The gstring struct is modified on
1115 return; specifically, the string-base-pointer may have been changed.
1118 g the growable-string
1119 count amount needed for g->ptr to increase by
1123 gstring_grow(gstring * g, int count)
1126 int oldsize = g->size;
1128 /* Mostly, string_cat() is used to build small strings of a few hundred
1129 characters at most. There are times, however, when the strings are very much
1130 longer (for example, a lookup that returns a vast number of alias addresses).
1131 To try to keep things reasonable, we use increments whose size depends on the
1132 existing length of the string. */
1134 unsigned inc = oldsize < 4096 ? 127 : 1023;
1136 if (g->ptr < 0 || g->ptr > g->size || g->size >= INT_MAX/2)
1137 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1138 "internal error in gstring_grow (ptr %d size %d)", g->ptr, g->size);
1140 if (count <= 0) return;
1142 if (count >= INT_MAX/2 - g->ptr)
1143 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1144 "internal error in gstring_grow (ptr %d count %d)", g->ptr, count);
1146 g->size = (p + count + inc + 1) & ~inc; /* one for a NUL */
1148 /* Try to extend an existing allocation. If the result of calling
1149 store_extend() is false, either there isn't room in the current memory block,
1150 or this string is not the top item on the dynamic store stack. We then have
1151 to get a new chunk of store and copy the old string. When building large
1152 strings, it is helpful to call store_release() on the old string, to release
1153 memory blocks that have become empty. (The block will be freed if the string
1154 is at its start.) However, we can do this only if we know that the old string
1155 was the last item on the dynamic memory stack. This is the case if it matches
1158 if (!store_extend(g->s, oldsize, g->size))
1159 g->s = store_newblock(g->s, g->size, p);
1164 /*************************************************
1165 * Add chars to string *
1166 *************************************************/
1167 /* This function is used when building up strings of unknown length. Room is
1168 always left for a terminating zero to be added to the string that is being
1169 built. This function does not require the string that is being added to be NUL
1170 terminated, because the number of characters to add is given explicitly. It is
1171 sometimes called to extract parts of other strings.
1174 g growable-string that is being built, or NULL if not assigned yet
1175 s points to characters to add
1176 count count of characters to add; must not exceed the length of s, if s
1179 Returns: growable string, changed if copied for expansion.
1180 Note that a NUL is not added, though space is left for one. This is
1181 because string_cat() is often called multiple times to build up a
1182 string - there's no point adding the NUL till the end.
1183 NULL is a possible return.
1186 /* coverity[+alloc] */
1189 string_catn(gstring * g, const uschar * s, int count)
1194 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1195 "internal error in string_catn (count %d)", count);
1196 if (count == 0) return g;
1198 /*debug_printf("string_catn '%.*s'\n", count, s);*/
1201 unsigned inc = count < 4096 ? 127 : 1023;
1202 unsigned size = ((count + inc) & ~inc) + 1; /* round up requested count */
1203 g = string_get_tainted(size, s);
1205 else if (!g->s) /* should not happen */
1207 g->s = string_copyn(s, count);
1209 g->size = count; /*XXX suboptimal*/
1212 else if (is_incompatible(g->s, s))
1214 /* debug_printf("rebuf A\n"); */
1215 gstring_rebuffer(g, s);
1218 if (g->ptr < 0 || g->ptr > g->size)
1219 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1220 "internal error in string_catn (ptr %d size %d)", g->ptr, g->size);
1223 if (count >= g->size - p)
1224 gstring_grow(g, count);
1226 /* Because we always specify the exact number of characters to copy, we can
1227 use memcpy(), which is likely to be more efficient than strncopy() because the
1228 latter has to check for zero bytes. */
1230 memcpy(g->s + p, s, count);
1237 string_cat(gstring * g, const uschar * s)
1239 return string_catn(g, s, Ustrlen(s));
1244 /*************************************************
1245 * Append strings to another string *
1246 *************************************************/
1248 /* This function can be used to build a string from many other strings.
1249 It calls string_cat() to do the dirty work.
1252 g growable-string that is being built, or NULL if not yet assigned
1253 count the number of strings to append
1254 ... "count" uschar* arguments, which must be valid zero-terminated
1257 Returns: growable string, changed if copied for expansion.
1258 The string is not zero-terminated - see string_cat() above.
1261 __inline__ gstring *
1262 string_append(gstring * g, int count, ...)
1266 va_start(ap, count);
1269 uschar * t = va_arg(ap, uschar *);
1270 g = string_cat(g, t);
1280 /*************************************************
1281 * Format a string with length checks *
1282 *************************************************/
1284 /* This function is used to format a string with checking of the length of the
1285 output for all conversions. It protects Exim from absent-mindedness when
1286 calling functions like debug_printf and string_sprintf, and elsewhere. There
1287 are two different entry points to what is actually the same function, depending
1288 on whether the variable length list of data arguments are given explicitly or
1291 The formats are the usual printf() ones, with some omissions (never used) and
1292 three additions for strings: %S forces lower case, %T forces upper case, and
1293 %#s or %#S prints nothing for a NULL string. Without the # "NULL" is printed
1294 (useful in debugging). There is also the addition of %D and %M, which insert
1295 the date in the form used for datestamped log files.
1298 buffer a buffer in which to put the formatted string
1299 buflen the length of the buffer
1300 format the format string - deliberately char * and not uschar *
1301 ... or ap variable list of supplementary arguments
1303 Returns: TRUE if the result fitted in the buffer
1307 string_format_trc(uschar * buffer, int buflen,
1308 const uschar * func, unsigned line, const char * format, ...)
1310 gstring g = { .size = buflen, .ptr = 0, .s = buffer }, * gp;
1312 va_start(ap, format);
1313 gp = string_vformat_trc(&g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1323 /* Build or append to a growing-string, sprintf-style.
1327 func called-from function name, for debug
1328 line called-from file line number, for debug
1329 limit maximum string size
1331 format printf-like format string
1332 ap variable-args pointer
1335 SVFMT_EXTEND buffer can be created or exteded as needed
1336 SVFMT_REBUFFER buffer can be recopied to tainted mem as needed
1337 SVFMT_TAINT_NOCHK do not check inputs for taint
1339 If the "extend" flag is true, the string passed in can be NULL,
1340 empty, or non-empty. Growing is subject to an overall limit given
1341 by the limit argument.
1343 If the "extend" flag is false, the string passed in may not be NULL,
1344 will not be grown, and is usable in the original place after return.
1345 The return value can be NULL to signify overflow.
1347 Field width: decimal digits, or *
1348 Precision: dot, followed by decimal digits or *
1349 Length modifiers: h L l ll z
1350 Conversion specifiers: n d o u x X p f e E g G % c s S T Y D M
1352 Returns the possibly-new (if copy for growth or taint-handling was needed)
1353 string, not nul-terminated.
1357 string_vformat_trc(gstring * g, const uschar * func, unsigned line,
1358 unsigned size_limit, unsigned flags, const char * format, va_list ap)
1360 enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
1362 int width, precision, off, lim, need;
1363 const char * fp = format; /* Deliberately not unsigned */
1365 string_datestamp_offset = -1; /* Datestamp not inserted */
1366 string_datestamp_length = 0; /* Datestamp not inserted */
1367 string_datestamp_type = 0; /* Datestamp not inserted */
1369 #ifdef COMPILE_UTILITY
1370 assert(!(flags & SVFMT_EXTEND));
1374 /* Ensure we have a string, to save on checking later */
1375 if (!g) g = string_get(16);
1377 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, format))
1379 #ifndef MACRO_PREDEF
1380 if (!(flags & SVFMT_REBUFFER))
1381 die_tainted(US"string_vformat", func, line);
1383 /* debug_printf("rebuf B\n"); */
1384 gstring_rebuffer(g, format);
1386 #endif /*!COMPILE_UTILITY*/
1388 lim = g->size - 1; /* leave one for a nul */
1389 off = g->ptr; /* remember initial offset in gstring */
1391 /* Scan the format and handle the insertions */
1395 int length = L_NORMAL;
1398 const char *null = "NULL"; /* ) These variables */
1399 const char *item_start, *s; /* ) are deliberately */
1400 char newformat[16]; /* ) not unsigned */
1401 char * gp = CS g->s + g->ptr; /* ) */
1403 /* Non-% characters just get copied verbatim */
1407 /* Avoid string_copyn() due to COMPILE_UTILITY */
1408 if ((need = g->ptr + 1) > lim)
1410 if (!(flags & SVFMT_EXTEND) || need > size_limit) return NULL;
1414 g->s[g->ptr++] = (uschar) *fp++;
1418 /* Deal with % characters. Pick off the width and precision, for checking
1419 strings, skipping over the flag and modifier characters. */
1422 width = precision = -1;
1424 if (strchr("-+ #0", *(++fp)) != NULL)
1426 if (*fp == '#') null = "";
1430 if (isdigit((uschar)*fp))
1432 width = *fp++ - '0';
1433 while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
1435 else if (*fp == '*')
1437 width = va_arg(ap, int);
1444 precision = va_arg(ap, int);
1448 for (precision = 0; isdigit((uschar)*fp); fp++)
1449 precision = precision*10 + *fp - '0';
1451 /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
1454 { fp++; length = L_SHORT; }
1455 else if (*fp == 'L')
1456 { fp++; length = L_LONGDOUBLE; }
1457 else if (*fp == 'l')
1459 { fp += 2; length = L_LONGLONG; }
1461 { fp++; length = L_LONG; }
1462 else if (*fp == 'z')
1463 { fp++; length = L_SIZE; }
1465 /* Handle each specific format type. */
1470 nptr = va_arg(ap, int *);
1471 *nptr = g->ptr - off;
1479 width = length > L_LONG ? 24 : 12;
1480 if ((need = g->ptr + width) > lim)
1482 if (!(flags & SVFMT_EXTEND) || need >= size_limit) return NULL;
1483 gstring_grow(g, width);
1485 gp = CS g->s + g->ptr;
1487 strncpy(newformat, item_start, fp - item_start);
1488 newformat[fp - item_start] = 0;
1490 /* Short int is promoted to int when passing through ..., so we must use
1491 int for va_arg(). */
1497 g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break;
1499 g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break;
1501 g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break;
1503 g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break;
1510 if ((need = g->ptr + 24) > lim)
1512 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1513 gstring_grow(g, 24);
1515 gp = CS g->s + g->ptr;
1517 /* sprintf() saying "(nil)" for a null pointer seems unreliable.
1518 Handle it explicitly. */
1519 if ((ptr = va_arg(ap, void *)))
1521 strncpy(newformat, item_start, fp - item_start);
1522 newformat[fp - item_start] = 0;
1523 g->ptr += sprintf(gp, newformat, ptr);
1526 g->ptr += sprintf(gp, "(nil)");
1530 /* %f format is inherently insecure if the numbers that it may be
1531 handed are unknown (e.g. 1e300). However, in Exim, %f is used for
1532 printing load averages, and these are actually stored as integers
1533 (load average * 1000) so the size of the numbers is constrained.
1534 It is also used for formatting sending rates, where the simplicity
1535 of the format prevents overflow. */
1542 if (precision < 0) precision = 6;
1543 if ((need = g->ptr + precision + 8) > lim)
1545 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1546 gstring_grow(g, precision+8);
1548 gp = CS g->s + g->ptr;
1550 strncpy(newformat, item_start, fp - item_start);
1551 newformat[fp-item_start] = 0;
1552 if (length == L_LONGDOUBLE)
1553 g->ptr += sprintf(gp, newformat, va_arg(ap, long double));
1555 g->ptr += sprintf(gp, newformat, va_arg(ap, double));
1561 if ((need = g->ptr + 1) > lim)
1563 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1567 g->s[g->ptr++] = (uschar) '%';
1571 if ((need = g->ptr + 1) > lim)
1573 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1577 g->s[g->ptr++] = (uschar) va_arg(ap, int);
1580 case 'D': /* Insert daily datestamp for log file names */
1581 s = CS tod_stamp(tod_log_datestamp_daily);
1582 string_datestamp_offset = g->ptr; /* Passed back via global */
1583 string_datestamp_length = Ustrlen(s); /* Passed back via global */
1584 string_datestamp_type = tod_log_datestamp_daily;
1585 slen = string_datestamp_length;
1588 case 'M': /* Insert monthly datestamp for log file names */
1589 s = CS tod_stamp(tod_log_datestamp_monthly);
1590 string_datestamp_offset = g->ptr; /* Passed back via global */
1591 string_datestamp_length = Ustrlen(s); /* Passed back via global */
1592 string_datestamp_type = tod_log_datestamp_monthly;
1593 slen = string_datestamp_length;
1596 case 'Y': /* gstring pointer */
1598 gstring * zg = va_arg(ap, gstring *);
1599 if (zg) { s = CS zg->s; slen = zg->ptr; }
1600 else { s = null; slen = Ustrlen(s); }
1601 goto INSERT_GSTRING;
1605 case 'S': /* Forces *lower* case */
1606 case 'T': /* Forces *upper* case */
1607 s = va_arg(ap, char *);
1612 INSERT_GSTRING: /* Coome to from %Y above */
1614 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
1615 if (flags & SVFMT_REBUFFER)
1617 /* debug_printf("%s %d: untainted workarea, tainted %%s :- rebuffer\n", __FUNCTION__, __LINE__); */
1618 gstring_rebuffer(g, s);
1619 gp = CS g->s + g->ptr;
1621 #ifndef MACRO_PREDEF
1623 die_tainted(US"string_vformat", func, line);
1626 INSERT_STRING: /* Come to from %D or %M above */
1629 BOOL truncated = FALSE;
1631 /* If the width is specified, check that there is a precision
1632 set; if not, set it to the width to prevent overruns of long
1637 if (precision < 0) precision = width;
1640 /* If a width is not specified and the precision is specified, set
1641 the width to the precision, or the string length if shorted. */
1643 else if (precision >= 0)
1644 width = precision < slen ? precision : slen;
1646 /* If neither are specified, set them both to the string length. */
1649 width = precision = slen;
1651 if ((need = g->ptr + width) >= size_limit || !(flags & SVFMT_EXTEND))
1653 if (g->ptr == lim) return NULL;
1657 width = precision = lim - g->ptr - 1;
1658 if (width < 0) width = 0;
1659 if (precision < 0) precision = 0;
1662 else if (need > lim)
1664 gstring_grow(g, width);
1666 gp = CS g->s + g->ptr;
1669 g->ptr += sprintf(gp, "%*.*s", width, precision, s);
1671 while (*gp) { *gp = tolower(*gp); gp++; }
1672 else if (fp[-1] == 'T')
1673 while (*gp) { *gp = toupper(*gp); gp++; }
1675 if (truncated) return NULL;
1679 /* Some things are never used in Exim; also catches junk. */
1682 strncpy(newformat, item_start, fp - item_start);
1683 newformat[fp-item_start] = 0;
1684 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
1685 "in \"%s\" in \"%s\"", newformat, format);
1690 if (g->ptr > g->size)
1691 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1692 "string_format internal error: caller %s %d", func, line);
1698 #ifndef COMPILE_UTILITY
1699 /*************************************************
1700 * Generate an "open failed" message *
1701 *************************************************/
1703 /* This function creates a message after failure to open a file. It includes a
1704 string supplied as data, adds the strerror() text, and if the failure was
1705 "Permission denied", reads and includes the euid and egid.
1708 format a text format string - deliberately not uschar *
1709 func caller, for debug
1710 line caller, for debug
1711 ... arguments for the format string
1713 Returns: a message, in dynamic store
1717 string_open_failed_trc(const uschar * func, unsigned line,
1718 const char * format, ...)
1721 gstring * g = string_get(1024);
1723 g = string_catn(g, US"failed to open ", 15);
1725 /* Use the checked formatting routine to ensure that the buffer
1726 does not overflow. It should not, since this is called only for internally
1727 specified messages. If it does, the message just gets truncated, and there
1728 doesn't seem much we can do about that. */
1730 va_start(ap, format);
1731 (void) string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1732 SVFMT_REBUFFER, format, ap);
1735 g = string_catn(g, US": ", 2);
1736 g = string_cat(g, US strerror(errno));
1738 if (errno == EACCES)
1740 int save_errno = errno;
1741 g = string_fmt_append(g, " (euid=%ld egid=%ld)",
1742 (long int)geteuid(), (long int)getegid());
1745 gstring_release_unused(g);
1746 return string_from_gstring(g);
1753 /* qsort(3), currently used to sort the environment variables
1754 for -bP environment output, needs a function to compare two pointers to string
1755 pointers. Here it is. */
1758 string_compare_by_pointer(const void *a, const void *b)
1760 return Ustrcmp(* CUSS a, * CUSS b);
1762 #endif /* COMPILE_UTILITY */
1767 /*************************************************
1768 **************************************************
1769 * Stand-alone test program *
1770 **************************************************
1771 *************************************************/
1778 printf("Testing is_ip_address\n");
1781 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1784 buffer[Ustrlen(buffer) - 1] = 0;
1785 printf("%d\n", string_is_ip_address(buffer, NULL));
1786 printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
1789 printf("Testing string_nextinlist\n");
1791 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1793 uschar *list = buffer;
1801 sep1 = sep2 = list[1];
1808 uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
1809 uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
1811 if (item1 == NULL && item2 == NULL) break;
1812 if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
1814 printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
1815 (item1 == NULL)? "NULL" : CS item1,
1816 (item2 == NULL)? "NULL" : CS item2);
1819 else printf(" \"%s\"\n", CS item1);
1823 /* This is a horrible lash-up, but it serves its purpose. */
1825 printf("Testing string_format\n");
1827 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1830 long long llargs[3];
1836 BOOL countset = FASE;
1840 buffer[Ustrlen(buffer) - 1] = 0;
1842 s = Ustrchr(buffer, ',');
1843 if (s == NULL) s = buffer + Ustrlen(buffer);
1845 Ustrncpy(format, buffer, s - buffer);
1846 format[s-buffer] = 0;
1853 s = Ustrchr(ss, ',');
1854 if (s == NULL) s = ss + Ustrlen(ss);
1858 Ustrncpy(outbuf, ss, s-ss);
1859 if (Ustrchr(outbuf, '.') != NULL)
1862 dargs[n++] = Ustrtod(outbuf, NULL);
1864 else if (Ustrstr(outbuf, "ll") != NULL)
1867 llargs[n++] = strtoull(CS outbuf, NULL, 10);
1871 args[n++] = (void *)Uatoi(outbuf);
1875 else if (Ustrcmp(ss, "*") == 0)
1877 args[n++] = (void *)(&count);
1883 uschar *sss = malloc(s - ss + 1);
1884 Ustrncpy(sss, ss, s-ss);
1891 if (!dflag && !llflag)
1892 printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1893 args[0], args[1], args[2])? "True" : "False");
1896 printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1897 dargs[0], dargs[1], dargs[2])? "True" : "False");
1899 else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1900 llargs[0], llargs[1], llargs[2])? "True" : "False");
1902 printf("%s\n", CS outbuf);
1903 if (countset) printf("count=%d\n", count);
1910 /* End of string.c */