Przeglądaj źródła

fix: miniz升级后,头文件变更导致压缩功能不可用,修正之,但仅bsp/win32上有启用的可能性

Wendal Chen 3 lat temu
rodzic
commit
3cd681bcf2
2 zmienionych plików z 19 dodań i 6 usunięć
  1. 18 5
      components/miniz/luat_lib_miniz.c
  2. 1 1
      components/miniz/miniz.h

+ 18 - 5
components/miniz/luat_lib_miniz.c

@@ -27,6 +27,11 @@ end
 
 #include "miniz.h"
 
+static mz_bool luat_output_buffer_putter(const void *pBuf, int len, void *pUser) {
+    luaL_addlstring((luaL_Buffer*)pUser, pBuf, len);
+    return MZ_TRUE;
+}
+
 /*
 快速压缩,需要165kb的系统内存和32kb的LuaVM内存
 @api miniz.compress(data, flags)
@@ -45,6 +50,8 @@ end
 */
 static int l_miniz_compress(lua_State* L) {
     size_t len = 0;
+    tdefl_compressor *pComp;
+    mz_bool succeeded;
     const char* data = luaL_checklstring(L, 1, &len);
     int flags = luaL_optinteger(L, 2, TDEFL_WRITE_ZLIB_HEADER);
     if (len > 32* 1024) {
@@ -52,17 +59,23 @@ static int l_miniz_compress(lua_State* L) {
         return 0;
     }
     luaL_Buffer buff;
-    char* dst = luaL_buffinitsize(L, &buff, TDEFL_OUT_BUF_SIZE);
-    if (dst == NULL) {
+    if (NULL == luaL_buffinitsize(L, &buff, 4096)) {
         LLOGE("out of memory when malloc dst buff");
         return 0;
     }
-    int ret = tdefl_compress_mem_to_mem(dst, TDEFL_OUT_BUF_SIZE, data, len, flags);
-    if (ret == 0) {
+    pComp = (tdefl_compressor *)luat_heap_malloc(sizeof(tdefl_compressor));
+    if (!pComp) {
+        LLOGE("out of memory when malloc tdefl_compressor size 0x%04X", sizeof(tdefl_compressor));
+        return 0;
+    }
+    succeeded = (tdefl_init(pComp, luat_output_buffer_putter, &buff, flags) == TDEFL_STATUS_OKAY);
+    succeeded = succeeded && (tdefl_compress_buffer(pComp, data, len, TDEFL_FINISH) == TDEFL_STATUS_DONE);
+    luat_heap_free(pComp);
+    if (!succeeded) {
         LLOGW("compress fail ret=0");
         return 0;
     }
-    luaL_pushresultsize(&buff, ret);
+    luaL_pushresult(&buff);
     return 1;
 }
 

+ 1 - 1
components/miniz/miniz.h

@@ -640,7 +640,7 @@ extern "C" {
 /* ------------------- Low-level Compression API Definitions */
 
 /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). */
-#define TDEFL_LESS_MEMORY 0
+#define TDEFL_LESS_MEMORY 1
 
 /* tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): */
 /* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). */