Merge branch 'exim-4.96+security' into master+security
[exim.git] / src / src / string.c
index 9aefc2b581114be5db73a06a0d029ca2e3f27512..055a37fd66f366af68c480e90db66ec99f9c80ab 100644 (file)
@@ -5,6 +5,7 @@
 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
 /* Copyright (c) University of Cambridge 1995 - 2018 */
 /* See the file NOTICE for conditions of use and distribution. */
+/* SPDX-License-Identifier: GPL-2.0-or-later */
 
 /* Miscellaneous string-handling functions. Some are not required for
 utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
@@ -199,26 +200,44 @@ return buffer;
 *************************************************/
 
 /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
-BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a
-static area.
+BASE_62 is actually 36. Always return exactly 6 characters plus a NUL, in a
+static area.  This is enough for a 32b input, for 62  (for 64b we would want 11+nul);
+but with 36 we lose half the input range of a 32b input.
 
 Argument: a long integer
 Returns:  pointer to base 62 string
 */
 
 uschar *
-string_base62(unsigned long int value)
+string_base62_32(unsigned long int value)
 {
 static uschar yield[7];
-uschar *p = yield + sizeof(yield) - 1;
+uschar * p = yield + sizeof(yield) - 1;
 *p = 0;
 while (p > yield)
   {
-  *(--p) = base62_chars[value % BASE_62];
+  *--p = base62_chars[value % BASE_62];
   value /= BASE_62;
   }
 return yield;
 }
+
+uschar *
+string_base62_64(unsigned long int value)
+{
+static uschar yield[12];
+uschar * p = yield + sizeof(yield) - 1;
+*p = '\0';
+while (p > yield)
+  if (value)
+    {
+    *--p = base62_chars[value % BASE_62];
+    value /= BASE_62;
+    }
+  else
+    *--p = '0';
+return yield;
+}
 #endif  /* COMPILE_UTILITY */
 
 
@@ -1318,6 +1337,11 @@ If the "extend" flag is false, the string passed in may not be NULL,
 will not be grown, and is usable in the original place after return.
 The return value can be NULL to signify overflow.
 
+Field width:           decimal digits, or *
+Precision:             dot, followed by decimal digits or *
+Length modifiers:      h  L  l  ll  z
+Conversion specifiers: n d o u x X p f e E g G % c s S T Y D M
+
 Returns the possibly-new (if copy for growth or taint-handling was needed)
 string, not nul-terminated.
 */
@@ -1562,6 +1586,14 @@ while (*fp)
       slen = string_datestamp_length;
       goto INSERT_STRING;
 
+    case 'Y':                  /* gstring pointer */
+      {
+      gstring * zg = va_arg(ap, gstring *);
+      if (zg) { s = CS zg->s; slen = zg->ptr;    }
+      else    { s = null;     slen = Ustrlen(s); }
+      goto INSERT_GSTRING;
+      }
+
     case 's':
     case 'S':                   /* Forces *lower* case */
     case 'T':                   /* Forces *upper* case */
@@ -1570,6 +1602,8 @@ while (*fp)
       if (!s) s = null;
       slen = Ustrlen(s);
 
+    INSERT_GSTRING:            /* Coome to from %Y above */
+
       if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
        if (flags & SVFMT_REBUFFER)
          {
@@ -1792,7 +1826,7 @@ while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
   int llflag = 0;
   int n = 0;
   int count;
-  int countset = 0;
+  BOOL countset = FASE;
   uschar format[256];
   uschar outbuf[256];
   uschar *s;
@@ -1834,7 +1868,7 @@ while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
     else if (Ustrcmp(ss, "*") == 0)
       {
       args[n++] = (void *)(&count);
-      countset = 1;
+      countset = TRUE;
       }
 
     else