robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <assert.h> 2#include <lauxlib.h> 3#include <lua.h> 4#include <stddef.h> 5 6#include "nvim/base64.h" 7#include "nvim/lua/base64.h" 8#include "nvim/memory.h" 9 10#include "lua/base64.c.generated.h" 11 12static int nlua_base64_encode (lua_State * L ) 13{ 14if (lua_gettop (L )< 1 ) { 15return luaL_error (L ,"Expected 1 argument" ); 16 } 17 18if (lua_type (L ,1 )!= LUA_TSTRING ) { 19luaL_argerror (L ,1 ,"expected string" ); 20 } 21 22size_t src_len = 0 ; 23const char * src = lua_tolstring (L ,1 ,& src_len ); 24 25const char * ret = base64_encode (src ,src_len ); 26assert (ret != NULL ); 27lua_pushstring (L ,ret ); 28xfree ((void * )ret ); 29 30return 1 ; 31} 32 33static int nlua_base64_decode (lua_State * L ) 34{ 35if (lua_gettop (L )< 1 ) { 36return luaL_error (L ,"Expected 1 argument" ); 37 } 38 39if (lua_type (L ,1 )!= LUA_TSTRING ) { 40luaL_argerror (L ,1 ,"expected string" ); 41 } 42 43size_t src_len = 0 ; 44const char * src = lua_tolstring (L ,1 ,& src_len ); 45 46size_t out_len = 0 ; 47const char * ret = base64_decode (src ,src_len ,& out_len ); 48if (ret == NULL ) { 49return luaL_error (L ,"Invalid input" ); 50 } 51 52lua_pushlstring (L ,ret ,out_len ); 53xfree ((void * )ret ); 54 55return 1 ; 56} 57 58static const luaL_Reg base64_functions []= { 59 {"encode" ,nlua_base64_encode }, 60 {"decode" ,nlua_base64_decode }, 61 {NULL ,NULL }, 62}; 63 64int luaopen_base64 (lua_State * L ) 65{ 66lua_newtable (L ); 67luaL_register (L ,NULL ,base64_functions ); 68return 1 ; 69}