Browse Source

mg.base64_encode for Lua

bel 11 năm trước cách đây
mục cha
commit
6ee0551970
2 tập tin đã thay đổi với 59 bổ sung26 xóa
  1. 28 26
      src/civetweb.c
  2. 31 0
      src/mod_lua.inl

+ 28 - 26
src/civetweb.c

@@ -2274,6 +2274,34 @@ int mg_get_cookie(const char *cookie_header, const char *var_name,
     return len;
 }
 
+#if defined(USE_WEBSOCKET) || defined(USE_LUA)
+static void base64_encode(const unsigned char *src, int src_len, char *dst)
+{
+    static const char *b64 =
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+    int i, j, a, b, c;
+
+    for (i = j = 0; i < src_len; i += 3) {
+        a = src[i];
+        b = i + 1 >= src_len ? 0 : src[i + 1];
+        c = i + 2 >= src_len ? 0 : src[i + 2];
+
+        dst[j++] = b64[a >> 2];
+        dst[j++] = b64[((a & 3) << 4) | (b >> 4)];
+        if (i + 1 < src_len) {
+            dst[j++] = b64[(b & 15) << 2 | (c >> 6)];
+        }
+        if (i + 2 < src_len) {
+            dst[j++] = b64[c & 63];
+        }
+    }
+    while (j % 4 != 0) {
+        dst[j++] = '=';
+    }
+    dst[j++] = '\0';
+}
+#endif
+
 static void convert_uri_to_file_name(struct mg_connection *conn, char *buf,
                                      size_t buf_len, struct file *filep,
                                      int * is_script_ressource)
@@ -4597,32 +4625,6 @@ static void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
 }
 /* END OF SHA1 CODE */
 
-static void base64_encode(const unsigned char *src, int src_len, char *dst)
-{
-    static const char *b64 =
-        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-    int i, j, a, b, c;
-
-    for (i = j = 0; i < src_len; i += 3) {
-        a = src[i];
-        b = i + 1 >= src_len ? 0 : src[i + 1];
-        c = i + 2 >= src_len ? 0 : src[i + 2];
-
-        dst[j++] = b64[a >> 2];
-        dst[j++] = b64[((a & 3) << 4) | (b >> 4)];
-        if (i + 1 < src_len) {
-            dst[j++] = b64[(b & 15) << 2 | (c >> 6)];
-        }
-        if (i + 2 < src_len) {
-            dst[j++] = b64[c & 63];
-        }
-    }
-    while (j % 4 != 0) {
-        dst[j++] = '=';
-    }
-    dst[j++] = '\0';
-}
-
 static void send_websocket_handshake(struct mg_connection *conn)
 {
     static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

+ 31 - 0
src/mod_lua.inl

@@ -559,6 +559,35 @@ static int lsp_url_decode(lua_State *L)
     return 1;
 }
 
+/* mg.base64_encode */
+static int lsp_base64_encode(lua_State *L)
+{
+    int num_args = lua_gettop(L);
+    const char *text;
+    size_t text_len;
+    char *dst;
+
+    if (num_args==1) {
+        text = lua_tolstring(L, 1, &text_len);
+        if (text) {
+            dst = malloc(text_len*8/6+4);
+            if (dst) {
+                base64_encode(text, text_len, dst);
+                lua_pushstring(L, dst);
+                free(dst);
+            } else {
+                return luaL_error(L, "out of memory in base64_encode() call");
+            }
+        } else {
+            lua_pushnil(L);
+        }
+    } else {
+        /* Syntax error */
+        return luaL_error(L, "invalid base64_encode() call");
+    }
+    return 1;
+}
+
 /* mg.write for websockets */
 static int lwebsock_write(lua_State *L)
 {
@@ -677,6 +706,8 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
     reg_function(L, "md5", lsp_md5, conn);
     reg_function(L, "url_encode", lsp_url_encode, conn);
     reg_function(L, "url_decode", lsp_url_decode, conn);
+    reg_function(L, "base64_encode", lsp_base64_encode, conn);
+    //reg_function(L, "base64_decode", lsp_base64_decode, conn);
 
     reg_string(L, "version", CIVETWEB_VERSION);
     reg_string(L, "document_root", conn->ctx->config[DOCUMENT_ROOT]);