luat_lib_zlib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. @module sfud
  3. @summary SPI FLASH sfud软件包
  4. @version 1.0
  5. @date 2021.09.23
  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. static char in[CHUNK];
  19. static char out[CHUNK];
  20. /* Compress from file source to file dest until EOF on source.
  21. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  22. allocated for processing, Z_STREAM_ERROR if an invalid compression
  23. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  24. version of the library linked do not match, or Z_ERRNO if there is
  25. an error reading or writing the files. */
  26. int def(FILE *source, FILE *dest, int level)
  27. {
  28. int ret, flush;
  29. unsigned have;
  30. z_stream strm;
  31. /* allocate deflate state */
  32. strm.zalloc = Z_NULL;
  33. strm.zfree = Z_NULL;
  34. strm.opaque = Z_NULL;
  35. ret = deflateInit(&strm, level);
  36. if (ret != Z_OK)
  37. return ret;
  38. /* compress until end of file */
  39. do {
  40. strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
  41. if (luat_fs_ferror(source)) {
  42. (void)deflateEnd(&strm);
  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. return Z_ERRNO;
  58. }
  59. } while (strm.avail_out == 0);
  60. assert(strm.avail_in == 0); /* all input will be used */
  61. /* done when last data in file processed */
  62. } while (flush != Z_FINISH);
  63. assert(ret == Z_STREAM_END); /* stream will be complete */
  64. /* clean up and return */
  65. (void)deflateEnd(&strm);
  66. return Z_OK;
  67. }
  68. /* Decompress from file source to file dest until stream ends or EOF.
  69. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  70. allocated for processing, Z_DATA_ERROR if the deflate data is
  71. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  72. the version of the library linked do not match, or Z_ERRNO if there
  73. is an error reading or writing the files. */
  74. int inf(FILE *source, FILE *dest)
  75. {
  76. int ret;
  77. unsigned have;
  78. z_stream strm;
  79. /* allocate inflate state */
  80. strm.zalloc = Z_NULL;
  81. strm.zfree = Z_NULL;
  82. strm.opaque = Z_NULL;
  83. strm.avail_in = 0;
  84. strm.next_in = Z_NULL;
  85. ret = inflateInit(&strm);
  86. if (ret != Z_OK)
  87. return ret;
  88. /* decompress until deflate stream ends or end of file */
  89. do {
  90. strm.avail_in = luat_fs_fread(in, 1, CHUNK, source);
  91. if (luat_fs_ferror(source)) {
  92. (void)inflateEnd(&strm);
  93. return Z_ERRNO;
  94. }
  95. if (strm.avail_in == 0)
  96. break;
  97. strm.next_in = in;
  98. /* run inflate() on input until output buffer not full */
  99. do {
  100. strm.avail_out = CHUNK;
  101. strm.next_out = out;
  102. ret = inflate(&strm, Z_NO_FLUSH);
  103. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  104. switch (ret) {
  105. case Z_NEED_DICT:
  106. ret = Z_DATA_ERROR; /* and fall through */
  107. case Z_DATA_ERROR:
  108. case Z_MEM_ERROR:
  109. (void)inflateEnd(&strm);
  110. return ret;
  111. }
  112. have = CHUNK - strm.avail_out;
  113. if (luat_fs_fwrite(out, 1, have, dest) != have || luat_fs_ferror(dest)) {
  114. (void)inflateEnd(&strm);
  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. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  123. }
  124. /* report a zlib or i/o error */
  125. void zerr(int ret)
  126. {
  127. fputs("zpipe: ", stderr);
  128. switch (ret) {
  129. case Z_ERRNO:
  130. if (luat_fs_ferror(stdin))
  131. fputs("error reading stdin\n", stderr);
  132. if (luat_fs_ferror(stdout))
  133. fputs("error writing stdout\n", stderr);
  134. break;
  135. case Z_STREAM_ERROR:
  136. fputs("invalid compression level\n", stderr);
  137. break;
  138. case Z_DATA_ERROR:
  139. fputs("invalid or incomplete deflate data\n", stderr);
  140. break;
  141. case Z_MEM_ERROR:
  142. fputs("out of memory\n", stderr);
  143. break;
  144. case Z_VERSION_ERROR:
  145. fputs("zlib version mismatch!\n", stderr);
  146. }
  147. }
  148. static int luat_zlib_compress(lua_State *L){
  149. FILE *fd_in, *fd_out;
  150. int ret = 0;
  151. size_t size = 0;
  152. const char* input_file = luaL_checklstring(L, 1, &size);
  153. const char* output_file = luaL_checklstring(L, 2, &size);
  154. fd_in = luat_fs_fopen(input_file, "r");
  155. if (fd_in == NULL){
  156. LLOGE("[zlib] open the input file : %s error!", input_file);
  157. ret = -1;
  158. goto _exit;
  159. }
  160. fd_out = luat_fs_fopen(output_file, "w+");
  161. if (fd_out == NULL){
  162. LLOGE("[zlib] open the output file : %s error!", output_file);
  163. ret = -1;
  164. goto _exit;
  165. }
  166. ret = def(fd_in, fd_out, Z_BEST_COMPRESSION);
  167. if (ret != Z_OK){
  168. zerr(ret);
  169. LLOGE("[zlib] compress file error!\n");
  170. return ret;
  171. }
  172. lua_pushboolean(L, 1);
  173. return 1;
  174. _exit:
  175. if(fd_in != NULL){
  176. luat_fs_fclose(fd_in);
  177. }
  178. if(fd_out != NULL){
  179. luat_fs_fclose(fd_out);
  180. }
  181. lua_pushboolean(L, 0);
  182. return 1;
  183. }
  184. static int luat_zlib_dcompress(lua_State *L){
  185. FILE *fd_in, *fd_out;
  186. int ret = 0;
  187. size_t size = 0;
  188. const char* input_file = luaL_checklstring(L, 1, &size);
  189. const char* output_file = luaL_checklstring(L, 2, &size);
  190. fd_in = luat_fs_fopen(input_file, "r");
  191. if (fd_in == NULL){
  192. LLOGE("[zlib] open the input file : %s error!", input_file);
  193. ret = -1;
  194. goto _exit;
  195. }
  196. fd_out = luat_fs_fopen(output_file, "w+");
  197. if (fd_out == NULL){
  198. LLOGE("[zlib] open the output file : %s error!", output_file);
  199. ret = -1;
  200. goto _exit;
  201. }
  202. ret = inf(fd_in, fd_out);
  203. if (ret != Z_OK)
  204. {
  205. zerr(ret);
  206. LLOGE("[zlib] decompress file error!");
  207. return ret;
  208. }
  209. lua_pushboolean(L, 1);
  210. return 1;
  211. _exit:
  212. if(fd_in != NULL){
  213. luat_fs_fclose(fd_in);
  214. }
  215. if(fd_out != NULL){
  216. luat_fs_fclose(fd_out);
  217. }
  218. lua_pushboolean(L, 0);
  219. return 1;
  220. }
  221. #include "rotable.h"
  222. static const rotable_Reg reg_zlib[] =
  223. {
  224. { "c", luat_zlib_compress, 0},
  225. { "d", luat_zlib_dcompress, 0},
  226. { NULL, NULL, 0}
  227. };
  228. LUAMOD_API int luaopen_zlib( lua_State *L ) {
  229. luat_newlib(L, reg_zlib);
  230. return 1;
  231. }