]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
mime: reuse the base64 string from the base64 code
authorDaniel Stenberg <daniel@haxx.se>
Sat, 10 May 2025 09:12:22 +0000 (11:12 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sat, 10 May 2025 21:01:14 +0000 (23:01 +0200)
Avoids duplicating an identical string here.

Closes #17309

lib/curlx/base64.c
lib/curlx/base64.h
lib/mime.c

index cadebb4f3e24dc65f2635691bd4fe177e91d27c4..80624cc296cee96a5fa58e1df51976010c606164 100644 (file)
@@ -44,7 +44,7 @@
 #include "../memdebug.h"
 
 /* ---- Base64 Encoding/Decoding Table --- */
-static const char base64encdec[]=
+const char Curl_base64encdec[]=
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 /* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
@@ -257,7 +257,8 @@ static CURLcode base64_encode(const char *table64,
 CURLcode curlx_base64_encode(const char *inputbuff, size_t insize,
                              char **outptr, size_t *outlen)
 {
-  return base64_encode(base64encdec, '=', inputbuff, insize, outptr, outlen);
+  return base64_encode(Curl_base64encdec, '=',
+                       inputbuff, insize, outptr, outlen);
 }
 
 /*
index faa7f71d21fb5ae5ce2e2b8bad1cf4e91c3a2718..026f80e4d37fdaf200b7203394dee4dfe81fdf80 100644 (file)
@@ -30,4 +30,7 @@ CURLcode curlx_base64url_encode(const char *inputbuff, size_t insize,
                                 char **outptr, size_t *outlen);
 CURLcode curlx_base64_decode(const char *src,
                              unsigned char **outptr, size_t *outlen);
+
+extern const char Curl_base64encdec[];
+
 #endif /* HEADER_CURL_BASE64_H */
index 1a107b746006afddaf3384d236e8583711db4f5b..c90c34898d598a7b9ba2cb46fe7cf3d5fd0d1a4b 100644 (file)
@@ -33,6 +33,7 @@ struct Curl_easy;
 #include "urldata.h"
 #include "sendf.h"
 #include "strdup.h"
+#include "curlx/base64.h"
 
 #if !defined(CURL_DISABLE_MIME) && (!defined(CURL_DISABLE_HTTP) ||      \
                                     !defined(CURL_DISABLE_SMTP) ||      \
@@ -87,10 +88,6 @@ static const struct mime_encoder encoders[] = {
   {ZERO_NULL, ZERO_NULL, ZERO_NULL}
 };
 
-/* Base64 encoding table */
-static const char base64enc[] =
-  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
 /* Quoted-printable character class table.
  *
  * We cannot rely on ctype functions since quoted-printable input data
@@ -472,10 +469,10 @@ static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
     i = st->buf[st->bufbeg++] & 0xFF;
     i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
     i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
-    *ptr++ = base64enc[(i >> 18) & 0x3F];
-    *ptr++ = base64enc[(i >> 12) & 0x3F];
-    *ptr++ = base64enc[(i >> 6) & 0x3F];
-    *ptr++ = base64enc[i & 0x3F];
+    *ptr++ = Curl_base64encdec[(i >> 18) & 0x3F];
+    *ptr++ = Curl_base64encdec[(i >> 12) & 0x3F];
+    *ptr++ = Curl_base64encdec[(i >> 6) & 0x3F];
+    *ptr++ = Curl_base64encdec[i & 0x3F];
     cursize += 4;
     st->pos += 4;
     size -= 4;
@@ -499,10 +496,10 @@ static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
           i = (st->buf[st->bufbeg + 1] & 0xFF) << 8;
 
         i |= (st->buf[st->bufbeg] & 0xFF) << 16;
-        ptr[0] = base64enc[(i >> 18) & 0x3F];
-        ptr[1] = base64enc[(i >> 12) & 0x3F];
+        ptr[0] = Curl_base64encdec[(i >> 18) & 0x3F];
+        ptr[1] = Curl_base64encdec[(i >> 12) & 0x3F];
         if(++st->bufbeg != st->bufend) {
-          ptr[2] = base64enc[(i >> 6) & 0x3F];
+          ptr[2] = Curl_base64encdec[(i >> 6) & 0x3F];
           st->bufbeg++;
         }
         cursize += 4;