luat_lib_zlib.c 7.5 KB

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