luat_lib_zlib.c 7.6 KB

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