summaryrefslogtreecommitdiff
path: root/src/lua_binding_function.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua_binding_function.c')
-rw-r--r--src/lua_binding_function.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/lua_binding_function.c b/src/lua_binding_function.c
index 81be039..c308c41 100644
--- a/src/lua_binding_function.c
+++ b/src/lua_binding_function.c
@@ -575,4 +575,97 @@ err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
+}
+
+static const char lua_base64_encode_table[] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '+', '/'};
+
+static int lua_base64_encode(const char *indata, int inlen, char *outdata, int *outlen)
+{
+ if (indata == NULL || inlen <= 0)
+ {
+ return -1;
+ }
+ int i, j;
+ unsigned char num = inlen % 3;
+ if (outdata != NULL)
+ {
+ // 编码,3个字节一组,若数据总长度不是3的倍数,则跳过最后的 num 个字节数据
+ for (i = 0, j = 0; i < inlen - num; i += 3, j += 4)
+ {
+ outdata[j] = lua_base64_encode_table[(unsigned char)indata[i] >> 2];
+ outdata[j + 1] = lua_base64_encode_table[(((unsigned char)indata[i] & 0x03) << 4) | ((unsigned char)indata[i + 1] >> 4)];
+ outdata[j + 2] = lua_base64_encode_table[(((unsigned char)indata[i + 1] & 0x0f) << 2) | ((unsigned char)indata[i + 2] >> 6)];
+ outdata[j + 3] = lua_base64_encode_table[(unsigned char)indata[i + 2] & 0x3f];
+ }
+ // 继续处理最后的 num 个字节的数据
+ if (num == 1)
+ { // 余数为1,需补齐两个字节'='
+ outdata[j] = lua_base64_encode_table[(unsigned char)indata[inlen - 1] >> 2];
+ outdata[j + 1] = lua_base64_encode_table[((unsigned char)indata[inlen - 1] & 0x03) << 4];
+ outdata[j + 2] = '=';
+ outdata[j + 3] = '=';
+ }
+ else if (num == 2)
+ { // 余数为2,需补齐一个字节'='
+ outdata[j] = lua_base64_encode_table[(unsigned char)indata[inlen - 2] >> 2];
+ outdata[j + 1] = lua_base64_encode_table[(((unsigned char)indata[inlen - 2] & 0x03) << 4) | ((unsigned char)indata[inlen - 1] >> 4)];
+ outdata[j + 2] = lua_base64_encode_table[((unsigned char)indata[inlen - 1] & 0x0f) << 2];
+ outdata[j + 3] = '=';
+ }
+ }
+ if (outlen != NULL)
+ {
+ *outlen = (inlen + (num == 0 ? 0 : 3 - num)) * 4 / 3;
+ }
+
+ return 0;
+}
+
+int lua_session_get0_current_payload(struct lua_state *state)
+{
+ lua_State *L = (lua_State *)state;
+ if (lua_gettop(L) != 1)
+ goto err;
+
+ struct session *sess = (struct session *)lua_topointer(L, -1);
+ lua_settop(L, 0);
+
+ size_t payload_len = 0;
+ const char *payload = session_get0_current_payload(sess, &payload_len);
+
+#if 0
+ lua_pushlightuserdata(L, (void *)payload);
+ lua_pushinteger(L, (lua_Integer)payload_len);
+ return 2;
+err:
+ lua_settop(L, 0);
+ lua_pushlightuserdata(L, NULL);
+ lua_pushboolean(L, 0);
+ return 2;
+#else
+ char *payload_base64 = (char *)calloc(2, payload_len);
+ int payload_base64_len = 0;
+ if (!lua_base64_encode(payload, payload_len, payload_base64, &payload_base64_len))
+ {
+ lua_pushstring(L, payload_base64);
+ lua_pushinteger(L, (lua_Integer)payload_len);
+ if (payload_base64)
+ free(payload_base64);
+ return 2;
+ }
+ if (payload_base64)
+ free(payload_base64);
+err:
+ lua_settop(L, 0);
+ lua_pushstring(L, NULL);
+ lua_pushboolean(L, 0);
+ return 2;
+#endif
} \ No newline at end of file