zlib_sample.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* zutil.c -- target dependent utility functions for the compression library
  2. * Copyright (C) 1995-2005 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.
  4. *
  5. * RT-Thread Development Team port
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2019-02-14 SummerGift first version
  10. */
  11. #include <rtconfig.h>
  12. #include <rtthread.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <dfs_posix.h>
  19. #include "zlib.h"
  20. #define CHUNK 4096
  21. /* Compress from file source to file dest until EOF on source.
  22. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  23. allocated for processing, Z_STREAM_ERROR if an invalid compression
  24. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  25. version of the library linked do not match, or Z_ERRNO if there is
  26. an error reading or writing the files. */
  27. int def(FILE *source, FILE *dest, int level)
  28. {
  29. int ret, flush;
  30. unsigned have;
  31. z_stream strm;
  32. char in[CHUNK];
  33. char out[CHUNK];
  34. /* allocate deflate state */
  35. strm.zalloc = Z_NULL;
  36. strm.zfree = Z_NULL;
  37. strm.opaque = Z_NULL;
  38. ret = deflateInit(&strm, level);
  39. if (ret != Z_OK)
  40. return ret;
  41. /* compress until end of file */
  42. do {
  43. strm.avail_in = fread(in, 1, CHUNK, source);
  44. if (ferror(source)) {
  45. (void)deflateEnd(&strm);
  46. return Z_ERRNO;
  47. }
  48. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  49. strm.next_in = in;
  50. /* run deflate() on input until output buffer not full, finish
  51. compression if all of source has been read in */
  52. do {
  53. strm.avail_out = CHUNK;
  54. strm.next_out = out;
  55. ret = deflate(&strm, flush); /* no bad return value */
  56. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  57. have = CHUNK - strm.avail_out;
  58. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  59. (void)deflateEnd(&strm);
  60. return Z_ERRNO;
  61. }
  62. } while (strm.avail_out == 0);
  63. assert(strm.avail_in == 0); /* all input will be used */
  64. /* done when last data in file processed */
  65. } while (flush != Z_FINISH);
  66. assert(ret == Z_STREAM_END); /* stream will be complete */
  67. /* clean up and return */
  68. (void)deflateEnd(&strm);
  69. return Z_OK;
  70. }
  71. /* Decompress from file source to file dest until stream ends or EOF.
  72. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  73. allocated for processing, Z_DATA_ERROR if the deflate data is
  74. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  75. the version of the library linked do not match, or Z_ERRNO if there
  76. is an error reading or writing the files. */
  77. int inf(FILE *source, FILE *dest)
  78. {
  79. int ret;
  80. unsigned have;
  81. z_stream strm;
  82. char in[CHUNK];
  83. char out[CHUNK];
  84. /* allocate inflate state */
  85. strm.zalloc = Z_NULL;
  86. strm.zfree = Z_NULL;
  87. strm.opaque = Z_NULL;
  88. strm.avail_in = 0;
  89. strm.next_in = Z_NULL;
  90. ret = inflateInit(&strm);
  91. if (ret != Z_OK)
  92. return ret;
  93. /* decompress until deflate stream ends or end of file */
  94. do {
  95. strm.avail_in = fread(in, 1, CHUNK, source);
  96. if (ferror(source)) {
  97. (void)inflateEnd(&strm);
  98. return Z_ERRNO;
  99. }
  100. if (strm.avail_in == 0)
  101. break;
  102. strm.next_in = in;
  103. /* run inflate() on input until output buffer not full */
  104. do {
  105. strm.avail_out = CHUNK;
  106. strm.next_out = out;
  107. ret = inflate(&strm, Z_NO_FLUSH);
  108. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  109. switch (ret) {
  110. case Z_NEED_DICT:
  111. ret = Z_DATA_ERROR; /* and fall through */
  112. case Z_DATA_ERROR:
  113. case Z_MEM_ERROR:
  114. (void)inflateEnd(&strm);
  115. return ret;
  116. }
  117. have = CHUNK - strm.avail_out;
  118. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  119. (void)inflateEnd(&strm);
  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. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  128. }
  129. /* report a zlib or i/o error */
  130. void zerr(int ret)
  131. {
  132. fputs("zpipe: ", stderr);
  133. switch (ret) {
  134. case Z_ERRNO:
  135. if (ferror(stdin))
  136. fputs("error reading stdin\n", stderr);
  137. if (ferror(stdout))
  138. fputs("error writing stdout\n", stderr);
  139. break;
  140. case Z_STREAM_ERROR:
  141. fputs("invalid compression level\n", stderr);
  142. break;
  143. case Z_DATA_ERROR:
  144. fputs("invalid or incomplete deflate data\n", stderr);
  145. break;
  146. case Z_MEM_ERROR:
  147. fputs("out of memory\n", stderr);
  148. break;
  149. case Z_VERSION_ERROR:
  150. fputs("zlib version mismatch!\n", stderr);
  151. }
  152. }
  153. int zlib_test(int argc, char ** argv)
  154. {
  155. FILE *fd_in, *fd_out;
  156. int ret = 0;
  157. if (argc != 4)
  158. {
  159. rt_kprintf("Usage:\n");
  160. rt_kprintf("zlib_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  161. rt_kprintf("zlib_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  162. ret = -1;
  163. goto _exit;
  164. }
  165. fd_in = fopen(argv[2], "r");
  166. if (fd_in == NULL)
  167. {
  168. rt_kprintf("[zlib] open the input file : %s error!\n", argv[2]);
  169. ret = -1;
  170. goto _exit;
  171. }
  172. fd_out = fopen(argv[3], "w+");
  173. if (fd_out == NULL)
  174. {
  175. rt_kprintf("[zlib] open the output file : %s error!\n", argv[3]);
  176. ret = -1;
  177. goto _exit;
  178. }
  179. if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
  180. {
  181. ret = def(fd_in, fd_out, Z_BEST_COMPRESSION);
  182. if (ret != Z_OK)
  183. {
  184. zerr(ret);
  185. rt_kprintf("[zlib] compress file error!\n");
  186. return ret;
  187. }
  188. }
  189. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  190. {
  191. ret = inf(fd_in, fd_out);
  192. if (ret != Z_OK)
  193. {
  194. zerr(ret);
  195. rt_kprintf("[zlib] decompress file error!\n");
  196. return ret;
  197. }
  198. }
  199. else
  200. {
  201. rt_kprintf("Usage:\n");
  202. rt_kprintf("zlib_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  203. rt_kprintf("zlib_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  204. ret = -1;
  205. goto _exit;
  206. }
  207. _exit:
  208. if(fd_in != NULL)
  209. {
  210. fclose(fd_in);
  211. }
  212. if(fd_out != NULL)
  213. {
  214. fclose(fd_out);
  215. }
  216. return ret;
  217. }
  218. #ifdef RT_USING_FINSH
  219. #ifdef FINSH_USING_MSH
  220. #include <finsh.h>
  221. MSH_CMD_EXPORT(zlib_test, zlib_test compress and decompress test);
  222. #endif
  223. #endif