SPDX: Mass-update to GPL-2.0-or-later
[exim.git] / src / src / imap_utf7.c
index 7fa03aa50818ea76fa0471ee22178b254245d744..1c09db6215ad466b10301829d548aa2ed418385e 100644 (file)
@@ -1,6 +1,10 @@
+/* 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 */
+
 #include "exim.h"
 
-#ifdef EXPERIMENTAL_INTERNATIONAL
+#ifdef SUPPORT_I18N
 
 uschar *
 imap_utf7_encode(uschar *string, const uschar *charset, uschar sep,
@@ -8,11 +12,10 @@ imap_utf7_encode(uschar *string, const uschar *charset, uschar sep,
 {
 static uschar encode_base64[64] =
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
-int ptr = 0;
-int size = 0;
 size_t slen;
-uschar *sptr, *yield = NULL;
-int i = 0, j;  /* compiler quietening */
+uschar *sptr;
+gstring * yield = NULL;
+int i = 0;     /* compiler quietening */
 uschar c = 0;  /* compiler quietening */
 BOOL base64mode = FALSE;
 BOOL lastsep = FALSE;
@@ -57,7 +60,7 @@ if ((icd = iconv_open("UTF-16BE", CCS charset)) == (iconv_t)-1)
     errno == EINVAL ? " (maybe unsupported conversion)" : "");
   return NULL;
   }
-#endif  
+#endif
 
 while (slen > 0)
   {
@@ -76,7 +79,7 @@ while (slen > 0)
     return NULL;
     }
 #else
-  for (utf16ptr = utf16buf; 
+  for (utf16ptr = utf16buf;
        slen > 0 && (utf16ptr - utf16buf) < sizeof(utf16buf);
        utf16ptr += 2, slen--, sptr++)
     {
@@ -97,14 +100,14 @@ while (slen > 0)
       {
       lastsep = FALSE;
       /* Encode as modified BASE64 */
-      if (!base64mode) 
+      if (!base64mode)
         {
         *outptr++ = '&';
         base64mode = TRUE;
         i = 0;
         }
 
-      for (j = 0; j < 2; j++, s++) switch (i++) 
+      for (int j = 0; j < 2; j++, s++) switch (i++)
        {
        case 0:
          /* Top 6 bits of the first octet */
@@ -130,7 +133,7 @@ while (slen > 0)
       /* Encode as self (almost) */
       if (base64mode)
         {
-        switch (i) 
+        switch (i)
           {
           case 1:
                /* Remaining bottom 2 bits of the last octet */
@@ -165,22 +168,21 @@ while (slen > 0)
     else
       {
       *error = string_sprintf("imapfolder: illegal character '%c'", s[1]);
-      if (yield) store_reset(yield);
       return NULL;
       }
 
     if (outptr > outbuf + sizeof(outbuf) - 3)
       {
-      yield = string_cat(yield, &size, &ptr, outbuf, outptr - outbuf);
+      yield = string_catn(yield, outbuf, outptr - outbuf);
       outptr = outbuf;
       }
 
     }
-  } /* End of input string */  
+  } /* End of input string */
 
-if (base64mode) 
+if (base64mode)
   {
-  switch (i) 
+  switch (i)
     {
     case 1:
       /* Remaining bottom 2 bits of the last octet */
@@ -197,12 +199,12 @@ if (base64mode)
 iconv_close(icd);
 #endif
 
-yield = string_cat(yield, &size, &ptr, outbuf, outptr - outbuf);
-if (yield[ptr-1] == '.')
-  ptr--;
-yield[ptr] = '\0';
+yield = string_catn(yield, outbuf, outptr - outbuf);
+
+if (yield->s[yield->ptr-1] == '.')
+  yield->ptr--;
 
-return yield;
+return string_from_gstring(yield);
 }
 
 #endif /* whole file */