Quellcode durchsuchen

add:zlib静态数组改为动态申请

Dozingfiretruck vor 4 Jahren
Ursprung
Commit
171297e305
1 geänderte Dateien mit 21 neuen und 19 gelöschten Zeilen
  1. 21 19
      luat/packages/zlib/luat_lib_zlib.c

+ 21 - 19
luat/packages/zlib/luat_lib_zlib.c

@@ -19,15 +19,11 @@
 #include "luat_log.h"
 
 #define CHUNK 4096
-static char in[CHUNK];
-static char out[CHUNK];
 
-static int zlib_compress(FILE *source, FILE *dest, int level)
-{
+static int zlib_compress(FILE *source, FILE *dest, int level){
     int ret, flush;
     unsigned have;
     z_stream strm;
-
     /* allocate deflate state */
     strm.zalloc = Z_NULL;
     strm.zfree = Z_NULL;
@@ -35,17 +31,19 @@ static int zlib_compress(FILE *source, FILE *dest, int level)
     ret = deflateInit(&strm, level);
     if (ret != Z_OK)
         return ret;
-
+    char* in = (char*)luat_heap_malloc(CHUNK * sizeof(char));
+    char* out = (char*)luat_heap_malloc(CHUNK * sizeof(char));
     /* compress until end of file */
     do {
         strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
         if (luat_fs_ferror(source)) {
             (void)deflateEnd(&strm);
+            luat_heap_free(in);
+            luat_heap_free(out);
             return Z_ERRNO;
         }
         flush = luat_fs_feof(source) ? Z_FINISH : Z_NO_FLUSH;
         strm.next_in = in;
-
         /* run deflate() on input until output buffer not full, finish
            compression if all of source has been read in */
         do {
@@ -56,26 +54,26 @@ static int zlib_compress(FILE *source, FILE *dest, int level)
             have = CHUNK - strm.avail_out;
             if (luat_fs_fwrite(out, 1, have, dest) != have || luat_fs_ferror(dest)) {
                 (void)deflateEnd(&strm);
+                luat_heap_free(in);
+                luat_heap_free(out);
                 return Z_ERRNO;
             }
         } while (strm.avail_out == 0);
         assert(strm.avail_in == 0);     /* all input will be used */
-
         /* done when last data in file processed */
     } while (flush != Z_FINISH);
     assert(ret == Z_STREAM_END);        /* stream will be complete */
-
     /* clean up and return */
     (void)deflateEnd(&strm);
+    luat_heap_free(in);
+    luat_heap_free(out);
     return Z_OK;
 }
 
-static int zlib_decompress(FILE *source, FILE *dest)
-{
+static int zlib_decompress(FILE *source, FILE *dest){
     int ret;
     unsigned have;
     z_stream strm;
-
     /* allocate inflate state */
     strm.zalloc = Z_NULL;
     strm.zfree = Z_NULL;
@@ -85,18 +83,20 @@ static int zlib_decompress(FILE *source, FILE *dest)
     ret = inflateInit(&strm);
     if (ret != Z_OK)
         return ret;
-
+    char* in = (char*)luat_heap_malloc(CHUNK * sizeof(char));
+    char* out = (char*)luat_heap_malloc(CHUNK * sizeof(char));
     /* decompress until deflate stream ends or end of file */
     do {
         strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
         if (luat_fs_ferror(source)) {
             (void)inflateEnd(&strm);
+            luat_heap_free(in);
+            luat_heap_free(out);
             return Z_ERRNO;
         }
         if (strm.avail_in == 0)
             break;
         strm.next_in = in;
-
         /* run inflate() on input until output buffer not full */
         do {
             strm.avail_out = CHUNK;
@@ -109,26 +109,29 @@ static int zlib_decompress(FILE *source, FILE *dest)
             case Z_DATA_ERROR:
             case Z_MEM_ERROR:
                 (void)inflateEnd(&strm);
+                luat_heap_free(in);
+                luat_heap_free(out);
                 return ret;
             }
             have = CHUNK - strm.avail_out;
             if (luat_fs_fwrite(out, 1, have, dest) != have || luat_fs_ferror(dest)) {
                 (void)inflateEnd(&strm);
+                luat_heap_free(in);
+                luat_heap_free(out);
                 return Z_ERRNO;
             }
         } while (strm.avail_out == 0);
-
         /* done when inflate() says it's done */
     } while (ret != Z_STREAM_END);
-
     /* clean up and return */
     (void)inflateEnd(&strm);
+    luat_heap_free(in);
+    luat_heap_free(out);
     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
 }
 
 /* report a zlib or i/o error */
-static void zerr(int ret)
-{
+static void zerr(int ret){
     fputs("zpipe: ", stderr);
     switch (ret) {
     case Z_ERRNO:
@@ -160,7 +163,6 @@ zlib压缩(需要大约270k内存,大部分mcu不支持)
 @usage
 zlib.c("/sd/1.txt","/sd/zlib")
 */
-
 static int luat_zlib_compress(lua_State *L){
     FILE *fd_in, *fd_out;
     int ret  = 0;