luat_lib_zlib.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. @module zlib
  3. @summary zlib压缩/解压缩(已废弃)
  4. @version 1.0
  5. @date 2022.01.06
  6. @tag LUAT_USE_ZLIB
  7. @deprecated 请使用miniz或者fastlz库
  8. @usage
  9. -- 这个库已经废弃了, 请使用miniz或者fastlz库
  10. */
  11. #include "luat_base.h"
  12. #include "luat_mem.h"
  13. #include "luat_fs.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include "zlib.h"
  20. #define LUAT_LOG_TAG "zlib"
  21. #include "luat_log.h"
  22. #define CHUNK 4096
  23. int zlib_compress(FILE *source, FILE *dest, int level){
  24. int ret, flush;
  25. unsigned have;
  26. z_stream strm;
  27. /* allocate deflate state */
  28. strm.zalloc = Z_NULL;
  29. strm.zfree = Z_NULL;
  30. strm.opaque = Z_NULL;
  31. ret = deflateInit(&strm, level);
  32. if (ret != Z_OK)
  33. return ret;
  34. char* in = (char*)luat_heap_malloc(CHUNK * sizeof(char));
  35. char* out = (char*)luat_heap_malloc(CHUNK * sizeof(char));
  36. /* compress until end of file */
  37. do {
  38. strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
  39. if (luat_fs_ferror(source)) {
  40. (void)deflateEnd(&strm);
  41. luat_heap_free(in);
  42. luat_heap_free(out);
  43. return Z_ERRNO;
  44. }
  45. flush = luat_fs_feof(source) ? Z_FINISH : Z_NO_FLUSH;
  46. strm.next_in = in;
  47. /* run deflate() on input until output buffer not full, finish
  48. compression if all of source has been read in */
  49. do {
  50. strm.avail_out = CHUNK;
  51. strm.next_out = out;
  52. ret = deflate(&strm, flush); /* no bad return value */
  53. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  54. have = CHUNK - strm.avail_out;
  55. if (luat_fs_fwrite(out, 1, have, dest) != have || luat_fs_ferror(dest)) {
  56. (void)deflateEnd(&strm);
  57. luat_heap_free(in);
  58. luat_heap_free(out);
  59. return Z_ERRNO;
  60. }
  61. } while (strm.avail_out == 0);
  62. assert(strm.avail_in == 0); /* all input will be used */
  63. /* done when last data in file processed */
  64. } while (flush != Z_FINISH);
  65. assert(ret == Z_STREAM_END); /* stream will be complete */
  66. /* clean up and return */
  67. (void)deflateEnd(&strm);
  68. luat_heap_free(in);
  69. luat_heap_free(out);
  70. return Z_OK;
  71. }
  72. int zlib_decompress(FILE *source, FILE *dest){
  73. int ret;
  74. unsigned have;
  75. z_stream strm;
  76. /* allocate inflate state */
  77. strm.zalloc = Z_NULL;
  78. strm.zfree = Z_NULL;
  79. strm.opaque = Z_NULL;
  80. strm.avail_in = 0;
  81. strm.next_in = Z_NULL;
  82. ret = inflateInit(&strm);
  83. if (ret != Z_OK)
  84. return ret;
  85. char* in = (char*)luat_heap_malloc(CHUNK * sizeof(char));
  86. char* out = (char*)luat_heap_malloc(CHUNK * sizeof(char));
  87. /* decompress until deflate stream ends or end of file */
  88. do {
  89. strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
  90. if (luat_fs_ferror(source)) {
  91. (void)inflateEnd(&strm);
  92. luat_heap_free(in);
  93. luat_heap_free(out);
  94. return Z_ERRNO;
  95. }
  96. if (strm.avail_in == 0)
  97. break;
  98. strm.next_in = in;
  99. /* run inflate() on input until output buffer not full */
  100. do {
  101. strm.avail_out = CHUNK;
  102. strm.next_out = out;
  103. ret = inflate(&strm, Z_NO_FLUSH);
  104. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  105. switch (ret) {
  106. case Z_NEED_DICT:
  107. ret = Z_DATA_ERROR; /* and fall through */
  108. case Z_DATA_ERROR:
  109. case Z_MEM_ERROR:
  110. (void)inflateEnd(&strm);
  111. luat_heap_free(in);
  112. luat_heap_free(out);
  113. return ret;
  114. }
  115. have = CHUNK - strm.avail_out;
  116. if (luat_fs_fwrite(out, 1, have, dest) != have || luat_fs_ferror(dest)) {
  117. (void)inflateEnd(&strm);
  118. luat_heap_free(in);
  119. luat_heap_free(out);
  120. return Z_ERRNO;
  121. }
  122. } while (strm.avail_out == 0);
  123. /* done when inflate() says it's done */
  124. } while (ret != Z_STREAM_END);
  125. /* clean up and return */
  126. (void)inflateEnd(&strm);
  127. luat_heap_free(in);
  128. luat_heap_free(out);
  129. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  130. }
  131. /* report a zlib or i/o error */
  132. static void zerr(int ret){
  133. fputs("zpipe: ", stderr);
  134. switch (ret) {
  135. case Z_ERRNO:
  136. if (luat_fs_ferror(stdin))
  137. fputs("error reading stdin\n", stderr);
  138. if (luat_fs_ferror(stdout))
  139. fputs("error writing stdout\n", stderr);
  140. break;
  141. case Z_STREAM_ERROR:
  142. fputs("invalid compression level\n", stderr);
  143. break;
  144. case Z_DATA_ERROR:
  145. fputs("invalid or incomplete deflate data\n", stderr);
  146. break;
  147. case Z_MEM_ERROR:
  148. fputs("out of memory\n", stderr);
  149. break;
  150. case Z_VERSION_ERROR:
  151. fputs("zlib version mismatch!\n", stderr);
  152. }
  153. }
  154. /*
  155. zlib压缩(需要大约270k内存,大部分mcu不支持)
  156. @api zlib.c(input_file,output_file)
  157. @string input_file 输入文件
  158. @string output_file 输出文件
  159. @return bool 正常返回 ture 失败返回 false
  160. @usage
  161. zlib.c("/sd/1.txt","/sd/zlib")
  162. */
  163. static int luat_zlib_compress(lua_State *L){
  164. FILE *fd_in, *fd_out;
  165. int ret = 0;
  166. size_t size = 0;
  167. const char* input_file = luaL_checklstring(L, 1, &size);
  168. const char* output_file = luaL_checklstring(L, 2, &size);
  169. fd_in = luat_fs_fopen(input_file, "r");
  170. if (fd_in == NULL){
  171. LLOGE("[zlib] open the input file : %s error!", input_file);
  172. ret = -1;
  173. goto _exit;
  174. }
  175. fd_out = luat_fs_fopen(output_file, "w+");
  176. if (fd_out == NULL){
  177. LLOGE("[zlib] open the output file : %s error!", output_file);
  178. ret = -1;
  179. goto _exit;
  180. }
  181. ret = zlib_compress(fd_in, fd_out, Z_BEST_COMPRESSION);
  182. if (ret != Z_OK){
  183. zerr(ret);
  184. LLOGE("[zlib] compress file error!\n");
  185. return ret;
  186. }
  187. luat_fs_fclose(fd_in);
  188. luat_fs_fclose(fd_out);
  189. lua_pushboolean(L, 1);
  190. return 1;
  191. _exit:
  192. if(fd_in != NULL){
  193. luat_fs_fclose(fd_in);
  194. }
  195. if(fd_out != NULL){
  196. luat_fs_fclose(fd_out);
  197. }
  198. lua_pushboolean(L, 0);
  199. return 1;
  200. }
  201. /*
  202. zlib解压缩(需要大约18k内存,大部分mcu都支持)
  203. @api zlib.d(input_file,output_file)
  204. @string input_file 输入文件
  205. @string output_file 输出文件
  206. @return bool 正常返回 ture 失败返回 false
  207. @usage
  208. zlib.d("/sd/zlib","/sd/1.txt")
  209. */
  210. static int luat_zlib_decompress(lua_State *L){
  211. FILE *fd_in, *fd_out;
  212. int ret = 0;
  213. size_t size = 0;
  214. const char* input_file = luaL_checklstring(L, 1, &size);
  215. const char* output_file = luaL_checklstring(L, 2, &size);
  216. fd_in = luat_fs_fopen(input_file, "r");
  217. if (fd_in == NULL){
  218. LLOGE("[zlib] open the input file : %s error!", input_file);
  219. ret = -1;
  220. goto _exit;
  221. }
  222. fd_out = luat_fs_fopen(output_file, "w+");
  223. if (fd_out == NULL){
  224. LLOGE("[zlib] open the output file : %s error!", output_file);
  225. ret = -1;
  226. goto _exit;
  227. }
  228. ret = zlib_decompress(fd_in, fd_out);
  229. if (ret != Z_OK)
  230. {
  231. zerr(ret);
  232. LLOGE("[zlib] decompress file error!");
  233. return ret;
  234. }
  235. luat_fs_fclose(fd_in);
  236. luat_fs_fclose(fd_out);
  237. lua_pushboolean(L, 1);
  238. return 1;
  239. _exit:
  240. if(fd_in != NULL){
  241. luat_fs_fclose(fd_in);
  242. }
  243. if(fd_out != NULL){
  244. luat_fs_fclose(fd_out);
  245. }
  246. lua_pushboolean(L, 0);
  247. return 1;
  248. }
  249. #include "rotable2.h"
  250. static const rotable_Reg_t reg_zlib[] =
  251. {
  252. { "c", ROREG_FUNC(luat_zlib_compress)},
  253. { "d", ROREG_FUNC(luat_zlib_decompress)},
  254. { NULL, {}}
  255. };
  256. LUAMOD_API int luaopen_zlib( lua_State *L ) {
  257. luat_newlib2(L, reg_zlib);
  258. return 1;
  259. }