aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/base64.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
commit1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (patch)
treecd08258054db80bb9a11b1061bb091c70b76926a /src/nvim/lua/base64.c
parenteaa89c11d0f8aefbb512de769c6c82f61a8baca3 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.gz
rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.bz2
rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.zip
Merge remote-tracking branch 'upstream/master' into aucmd_textputpostaucmd_textputpost
Diffstat (limited to 'src/nvim/lua/base64.c')
-rw-r--r--src/nvim/lua/base64.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/nvim/lua/base64.c b/src/nvim/lua/base64.c
new file mode 100644
index 0000000000..c1f43a37d7
--- /dev/null
+++ b/src/nvim/lua/base64.c
@@ -0,0 +1,70 @@
+#include <assert.h>
+#include <lauxlib.h>
+#include <lua.h>
+#include <stddef.h>
+
+#include "nvim/base64.h"
+#include "nvim/lua/base64.h"
+#include "nvim/memory.h"
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+# include "lua/base64.c.generated.h"
+#endif
+
+static int nlua_base64_encode(lua_State *L)
+{
+ if (lua_gettop(L) < 1) {
+ return luaL_error(L, "Expected 1 argument");
+ }
+
+ if (lua_type(L, 1) != LUA_TSTRING) {
+ luaL_argerror(L, 1, "expected string");
+ }
+
+ size_t src_len = 0;
+ const char *src = lua_tolstring(L, 1, &src_len);
+
+ const char *ret = base64_encode(src, src_len);
+ assert(ret != NULL);
+ lua_pushstring(L, ret);
+ xfree((void *)ret);
+
+ return 1;
+}
+
+static int nlua_base64_decode(lua_State *L)
+{
+ if (lua_gettop(L) < 1) {
+ return luaL_error(L, "Expected 1 argument");
+ }
+
+ if (lua_type(L, 1) != LUA_TSTRING) {
+ luaL_argerror(L, 1, "expected string");
+ }
+
+ size_t src_len = 0;
+ const char *src = lua_tolstring(L, 1, &src_len);
+
+ const char *ret = base64_decode(src, src_len);
+ if (ret == NULL) {
+ return luaL_error(L, "Invalid input");
+ }
+
+ lua_pushstring(L, ret);
+ xfree((void *)ret);
+
+ return 1;
+}
+
+static const luaL_Reg base64_functions[] = {
+ { "encode", nlua_base64_encode },
+ { "decode", nlua_base64_decode },
+ { NULL, NULL },
+};
+
+int luaopen_base64(lua_State *L)
+{
+ lua_newtable(L);
+ luaL_register(L, NULL, base64_functions);
+ return 1;
+}