zlib_sample.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. static char in[CHUNK];
  22. static char out[CHUNK];
  23. /* Compress from file source to file dest until EOF on source.
  24. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  25. allocated for processing, Z_STREAM_ERROR if an invalid compression
  26. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  27. version of the library linked do not match, or Z_ERRNO if there is
  28. an error reading or writing the files. */
  29. int def(FILE *source, FILE *dest, int level)
  30. {
  31. int ret, flush;
  32. unsigned have;
  33. z_stream strm;
  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. /* allocate inflate state */
  83. strm.zalloc = Z_NULL;
  84. strm.zfree = Z_NULL;
  85. strm.opaque = Z_NULL;
  86. strm.avail_in = 0;
  87. strm.next_in = Z_NULL;
  88. ret = inflateInit(&strm);
  89. if (ret != Z_OK)
  90. return ret;
  91. /* decompress until deflate stream ends or end of file */
  92. do {
  93. strm.avail_in = fread(in, 1, CHUNK, source);
  94. if (ferror(source)) {
  95. (void)inflateEnd(&strm);
  96. return Z_ERRNO;
  97. }
  98. if (strm.avail_in == 0)
  99. break;
  100. strm.next_in = in;
  101. /* run inflate() on input until output buffer not full */
  102. do {
  103. strm.avail_out = CHUNK;
  104. strm.next_out = out;
  105. ret = inflate(&strm, Z_NO_FLUSH);
  106. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  107. switch (ret) {
  108. case Z_NEED_DICT:
  109. ret = Z_DATA_ERROR; /* and fall through */
  110. case Z_DATA_ERROR:
  111. case Z_MEM_ERROR:
  112. (void)inflateEnd(&strm);
  113. return ret;
  114. }
  115. have = CHUNK - strm.avail_out;
  116. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  117. (void)inflateEnd(&strm);
  118. return Z_ERRNO;
  119. }
  120. } while (strm.avail_out == 0);
  121. /* done when inflate() says it's done */
  122. } while (ret != Z_STREAM_END);
  123. /* clean up and return */
  124. (void)inflateEnd(&strm);
  125. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  126. }
  127. /* report a zlib or i/o error */
  128. void zerr(int ret)
  129. {
  130. fputs("zpipe: ", stderr);
  131. switch (ret) {
  132. case Z_ERRNO:
  133. if (ferror(stdin))
  134. fputs("error reading stdin\n", stderr);
  135. if (ferror(stdout))
  136. fputs("error writing stdout\n", stderr);
  137. break;
  138. case Z_STREAM_ERROR:
  139. fputs("invalid compression level\n", stderr);
  140. break;
  141. case Z_DATA_ERROR:
  142. fputs("invalid or incomplete deflate data\n", stderr);
  143. break;
  144. case Z_MEM_ERROR:
  145. fputs("out of memory\n", stderr);
  146. break;
  147. case Z_VERSION_ERROR:
  148. fputs("zlib version mismatch!\n", stderr);
  149. }
  150. }
  151. int zlib_test(int argc, char ** argv)
  152. {
  153. FILE *fd_in, *fd_out;
  154. int ret = 0;
  155. if (argc != 4)
  156. {
  157. rt_kprintf("Usage:\n");
  158. rt_kprintf("zlib_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  159. rt_kprintf("zlib_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  160. ret = -1;
  161. goto _exit;
  162. }
  163. fd_in = fopen(argv[2], "r");
  164. if (fd_in == NULL)
  165. {
  166. rt_kprintf("[zlib] open the input file : %s error!\n", argv[2]);
  167. ret = -1;
  168. goto _exit;
  169. }
  170. fd_out = fopen(argv[3], "w+");
  171. if (fd_out == NULL)
  172. {
  173. rt_kprintf("[zlib] open the output file : %s error!\n", argv[3]);
  174. ret = -1;
  175. goto _exit;
  176. }
  177. if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
  178. {
  179. ret = def(fd_in, fd_out, Z_BEST_COMPRESSION);
  180. if (ret != Z_OK)
  181. {
  182. zerr(ret);
  183. rt_kprintf("[zlib] compress file error!\n");
  184. return ret;
  185. }
  186. }
  187. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  188. {
  189. ret = inf(fd_in, fd_out);
  190. if (ret != Z_OK)
  191. {
  192. zerr(ret);
  193. rt_kprintf("[zlib] decompress file error!\n");
  194. return ret;
  195. }
  196. }
  197. else
  198. {
  199. rt_kprintf("Usage:\n");
  200. rt_kprintf("zlib_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  201. rt_kprintf("zlib_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  202. ret = -1;
  203. goto _exit;
  204. }
  205. _exit:
  206. if(fd_in != NULL)
  207. {
  208. fclose(fd_in);
  209. }
  210. if(fd_out != NULL)
  211. {
  212. fclose(fd_out);
  213. }
  214. return ret;
  215. }
  216. #ifdef RT_USING_FINSH
  217. #ifdef FINSH_USING_MSH
  218. #include <finsh.h>
  219. MSH_CMD_EXPORT(zlib_test, zlib_test compress and decompress test);
  220. #endif
  221. #endif