-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64.c
37 lines (31 loc) · 805 Bytes
/
base64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
static int l_b64e(lua_State *L)
{
const char *s3 = luaL_checkstring(L, 1);
const char *base64_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char c[5];
if(s3 == NULL || strlen(s3) != 3)
{
lua_pushnil(L);
lua_pushstring(L, "you have to supply a 3 byte string");
return 2;
}
c[0] = base64_alpha[(s3[0] & 0xfc) >> 2];
c[1] = base64_alpha[((s3[0] & 0x03) << 4) + ((s3[1] & 0xf0) >> 4)];
c[2] = base64_alpha[((s3[1] & 0x0f) << 2) + ((s3[2] & 0xc0) >> 6)];
c[3] = base64_alpha[s3[2] & 0x3f];
c[4] = '\0';
lua_pushstring(L, c);
return 1;
}
static const struct luaL_Reg base64[] = {
{"e", l_b64e},
{NULL, NULL}
};
int luaopen_base64(lua_State *L)
{
luaL_register(L, "base64", base64);
return 1;
}