From feadfedd5dcce16a24c8b7d5981e02a9f92feca6 Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov@tarantool.org>
Date: Wed, 21 Jun 2023 14:17:47 +0300
Subject: [PATCH] yaml: use standard base64 encoder

Let's drop yaml/b64 in favor of the base64 encoder used everywhere else
in the Tarantool source code.

yaml/b64 is also used by serialize_lua to print MP_BIN values. Let's
print MP_BIN values as MP_STR there. This doesn't have any user-visible
changes because since commit 890a821c3be9 ("yaml: don't encode
unprintable strings as binary blobs") luaL_tofield never creates MP_BIN
values. However, when we introduce the varbinary type to Lua, we will
use the MP_BIN value type for it, and printing it in the Lua format as a
string with unprintable characters escaped is going to be less confusing
than encoding it in base64 without any tags or markers.

While we're at it, let's use the luaL_field.sval.data in the encoders
instead of extracting the string from the Lua stack again.

Needed for #1629

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
---
 src/CMakeLists.txt            |  1 -
 src/box/lua/serialize_lua.c   | 10 +---
 third_party/lua-yaml/b64.c    | 94 -----------------------------------
 third_party/lua-yaml/b64.h    |  4 --
 third_party/lua-yaml/lyaml.cc | 12 +----
 5 files changed, 3 insertions(+), 118 deletions(-)
 delete mode 100644 third_party/lua-yaml/b64.c
 delete mode 100644 third_party/lua-yaml/b64.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 547ee68f7e..a524d76070 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -222,7 +222,6 @@ set (server_sources
      lua/xml.c
      ${lua_sources}
      ${PROJECT_SOURCE_DIR}/third_party/lua-yaml/lyaml.cc
-     ${PROJECT_SOURCE_DIR}/third_party/lua-yaml/b64.c
      ${PROJECT_SOURCE_DIR}/third_party/lua-cjson/lua_cjson.c
      ${PROJECT_SOURCE_DIR}/third_party/lua-cjson/strbuf.c
      ${COMPRESS_MODULE_SOURCES}
diff --git a/src/box/lua/serialize_lua.c b/src/box/lua/serialize_lua.c
index e4f20c792b..49fddb9193 100644
--- a/src/box/lua/serialize_lua.c
+++ b/src/box/lua/serialize_lua.c
@@ -41,8 +41,6 @@
 #include "mp_extension_types.h"
 #include "tt_uuid.h"
 
-#include "lua-yaml/b64.h"
-
 #include "serialize_lua.h"
 
 #if 0
@@ -811,14 +809,10 @@ dump_node(struct lua_dumper *d, struct node *nd, int indent)
 		str = buf;
 		break;
 	case MP_STR:
-		nd->mask |= NODE_QUOTE;
-		str = lua_tolstring(d->L, -1, &len);
-		break;
 	case MP_BIN:
 		nd->mask |= NODE_QUOTE;
-		tobase64(d->L, -1);
-		str = lua_tolstring(d->L, -1, &len);
-		lua_pop(d->L, 1);
+		str = field->sval.data;
+		len = field->sval.len;
 		break;
 	case MP_ARRAY:
 		dump_array(d, nd, indent);
diff --git a/third_party/lua-yaml/b64.c b/third_party/lua-yaml/b64.c
deleted file mode 100644
index 6d850eacbc..0000000000
--- a/third_party/lua-yaml/b64.c
+++ /dev/null
@@ -1,94 +0,0 @@
-#include <lua.h>
-#include <lauxlib.h>
-
-#include "b64.h"
-
-int frombase64(lua_State *L, const unsigned char *str, unsigned int len) {
-    int d = 0, dlast = 0, phase = 0;
-    unsigned char c;
-    static int table[256] = {
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
-        52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
-        -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
-        15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
-        -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
-        41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
-        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
-    };
-   luaL_Buffer b;
-
-   luaL_buffinit(L, &b);
-   for (; len--; ++str) {
-      d = table[(int)*str];
-      if (d == -1) continue;
-      switch(phase) {
-         case 0:
-            ++phase;
-            break;
-         case 1:
-            c = ((dlast << 2) | ((d & 0x30) >> 4));
-            luaL_addchar(&b, c);
-            ++phase;
-            break;
-         case 2:
-            c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2));
-            luaL_addchar(&b, c);
-            ++phase;
-            break;
-         case 3:
-            c = (((dlast & 0x03 ) << 6) | d);
-            luaL_addchar(&b, c);
-            phase = 0;
-            break;
-      }
-      dlast = d;
-   }
-   luaL_pushresult(&b);
-   return 1;
-}
-
-static void b64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) {
-   static const char code[] =
-      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-   unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1);
-   int i;
-   char s[4];
-
-   for (i = 0; i < 4; i++) {
-      s[3-i] = code[tuple % 64];
-      tuple /= 64;
-   }
-   for (i = n+1; i < 4; i++) s[i] = '=';
-   luaL_addlstring(b, s, 4);
-}
-
-int tobase64(lua_State *L, int pos) {
-   size_t l;
-   const unsigned char *s = (const unsigned char*)luaL_checklstring(L, pos, &l);
-   luaL_Buffer b;
-   int n;
-
-   luaL_buffinit(L, &b);
-   for (n = l / 3; n--; s += 3)
-      b64_encode(&b, s[0], s[1], s[2], 3);
-
-   switch (l % 3) {
-      case 1:
-         b64_encode(&b, s[0], 0, 0, 1);
-         break;
-      case 2:
-         b64_encode(&b, s[0], s[1], 0, 2);
-         break;
-   }
-   luaL_pushresult(&b);
-   return 1;
-}
diff --git a/third_party/lua-yaml/b64.h b/third_party/lua-yaml/b64.h
deleted file mode 100644
index 84460a1dfe..0000000000
--- a/third_party/lua-yaml/b64.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#include <lua.h>
-
-int frombase64(lua_State *, const unsigned char *, unsigned int);
-int tobase64(lua_State *, int);
diff --git a/third_party/lua-yaml/lyaml.cc b/third_party/lua-yaml/lyaml.cc
index 90de31510f..ec7790403e 100644
--- a/third_party/lua-yaml/lyaml.cc
+++ b/third_party/lua-yaml/lyaml.cc
@@ -109,13 +109,6 @@ struct lua_yaml_dumper {
 static bool yaml_pretty_multiline = true;
 TWEAK_BOOL(yaml_pretty_multiline);
 
-/**
- * If this flag is set, a binary data field will be decoded to a plain Lua
- * string, not a varbinary object.
- */
-static bool yaml_decode_binary_as_string = false;
-TWEAK_BOOL(yaml_decode_binary_as_string);
-
 /**
  * Verify whether a string represents a boolean literal in YAML.
  *
@@ -288,10 +281,7 @@ static void load_scalar(struct lua_yaml_loader *loader) {
          int bufsize = base64_decode_bufsize(length);
          char *buf = (char *)xmalloc(bufsize);
          int size = base64_decode(str, length, buf, bufsize);
-         if (yaml_decode_binary_as_string)
-            lua_pushlstring(loader->L, buf, size);
-         else
-            luaT_pushvarbinary(loader->L, buf, size);
+         lua_pushlstring(loader->L, buf, size);
          free(buf);
          return;
       }
-- 
GitLab