Fix ${srs_encode ..} for mod-1024 day zero
[exim.git] / src / src / expand.c
index baf7134cdc2b79848cd323d7ae154cf7f310935f..26df25795ce5be77d5d612b8da939b085b46087b 100644 (file)
@@ -2,7 +2,7 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) The Exim Maintainers 2020 - 2022 */
+/* Copyright (c) The Exim Maintainers 2020 - 2023 */
 /* 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 */
@@ -27,13 +27,6 @@ typedef unsigned esi_flags;
 # ifndef SUPPORT_CRYPTEQ
 #  define SUPPORT_CRYPTEQ
 # endif
-#else
-
-/* Recursively called function */
-
-static uschar *expand_string_internal(const uschar *, esi_flags, const uschar **, BOOL *, BOOL *);
-static int_eximarith_t expanded_string_integer(const uschar *, BOOL);
-
 #endif /*!STAND_ALONE*/
 
 #ifdef LOOKUP_LDAP
@@ -237,6 +230,7 @@ static uschar *op_table_main[] = {
   US"expand",
   US"h",
   US"hash",
+  US"headerwrap",
   US"hex2b64",
   US"hexquote",
   US"ipv6denorm",
@@ -284,6 +278,7 @@ enum {
   EOP_EXPAND,
   EOP_H,
   EOP_HASH,
+  EOP_HEADERWRAP,
   EOP_HEX2B64,
   EOP_HEXQUOTE,
   EOP_IPV6DENORM,
@@ -940,6 +935,10 @@ static uschar *mtable_sticky[] =
 #define FH_WANT_RAW    BIT(1)
 #define FH_WANT_LIST   BIT(2)
 
+/* Recursively called function */
+static uschar *expand_string_internal(const uschar *, esi_flags, const uschar **, BOOL *, BOOL *);
+static int_eximarith_t expanded_string_integer(const uschar *, BOOL);
+
 
 /*************************************************
 *           Tables for UTF-8 support             *
@@ -3524,7 +3523,7 @@ switch(cond_type = identify_operator(&s, &opname))
 
     /* Match the given local_part against the SRS-encoded pattern */
 
-    re = regex_must_compile(US"^(?i)SRS0=([^=]+)=([A-Z2-7]+)=([^=]*)=(.*)$",
+    re = regex_must_compile(US"^(?i)SRS0=([^=]+)=([A-Z2-7]{2})=([^=]*)=(.*)$",
                            MCS_CASELESS | MCS_CACHEABLE, FALSE);
     md = pcre2_match_data_create(4+1, pcre_gen_ctx);
     if (pcre2_match(re, sub[0], PCRE2_ZERO_TERMINATED, 0, PCRE_EOPT,
@@ -5628,7 +5627,6 @@ while (*s)
       /* Handle options to the "run" */
 
       while (*s == ',')
-       {
        if (Ustrncmp(++s, "preexpand", 9) == 0)
          { late_expand = FALSE; s += 9; }
        else
@@ -5639,7 +5637,6 @@ while (*s)
                                                  (int)(t-s), s);
          goto EXPAND_FAILED;
          }
-       }
       Uskip_whitespace(&s);
 
       if (*s != '{')                                   /*}*/
@@ -7064,13 +7061,11 @@ while (*s)
          {
          struct timeval now;
          unsigned long i;
-         gstring * h = NULL;
 
          gettimeofday(&now, NULL);
-         for (unsigned long i = (now.tv_sec / 86400) & 0x3ff; i; i >>= 5)
-           h = string_catn(h, &base32_chars[i & 0x1f], 1);
-         if (h) while (h->ptr > 0)
-           g = string_catn(g, &h->s[--h->ptr], 1);
+         i = (now.tv_sec / 86400) & 0x3ff;
+         g = string_catn(g, &base32_chars[i >> 5], 1);
+         g = string_catn(g, &base32_chars[i & 0x1f], 1);
          }
        g = string_catn(g, US"=", 1);
 
@@ -7262,7 +7257,7 @@ NOT_ITEM: ;
        {
        uschar *t;
        unsigned long int n = Ustrtoul(sub, &t, 10);
-       if (*t != 0)
+       if (*t)
          {
          expand_string_message = string_sprintf("argument for base62 "
            "operator is \"%s\", which is not a decimal number", sub);
@@ -7278,7 +7273,7 @@ NOT_ITEM: ;
        {
        uschar *tt = sub;
        unsigned long int n = 0;
-       while (*tt != 0)
+       while (*tt)
          {
          uschar *t = Ustrchr(base62_chars, *tt++);
          if (!t)
@@ -7428,6 +7423,29 @@ NOT_ITEM: ;
        goto EXPAND_FAILED;
 #endif
 
+      /* Line-wrap a string as if it is a header line */
+
+      case EOP_HEADERWRAP:
+       {
+       unsigned col = 80, lim = 998;
+       uschar * s;
+
+       if (arg)
+         {
+         const uschar * list = arg;
+         int sep = '_';
+         if ((s = string_nextinlist(&list, &sep, NULL, 0)))
+           {
+           col = atoi(CS s);
+           if ((s = string_nextinlist(&list, &sep, NULL, 0)))
+             lim = atoi(CS s);
+           }
+         }
+         if ((s =  wrap_header(sub, col, lim, US"\t", 8)))
+           yield = string_cat(yield, s);
+       }
+       break;
+
       /* Convert hex encoding to base64 encoding */
 
       case EOP_HEX2B64: