quicklz_sample.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * File : quicklz_test.c
  3. * this example is a very simple test program for the quicklz library,
  4. * using non-stream compress and decompress. If you want to use stream compress,
  5. * you need at least 100K of ROM for history buffer(not recommend), or you can custom
  6. * header to storage the compress block size, and carry out stream compress by non-stream.
  7. *
  8. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. * 2018-02-05 chenyong first version
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <rtthread.h>
  32. #if RT_VER_NUM >= 0x40100
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #else
  38. #include <dfs_fs.h>
  39. #endif
  40. #include "quicklz.h"
  41. #define malloc rt_malloc
  42. #define free rt_free
  43. #define BLOCK_HEADER_SIZE 4
  44. #define COMPRESS_BUFFER_SIZE 4096
  45. #define DCOMPRESS_BUFFER_SIZE 4096
  46. /* Buffer padding for destination buffer, least size + 400 bytes large because incompressible data may increase in size. */
  47. #define BUFFER_PADDING QLZ_BUFFER_PADDING
  48. #if QLZ_STREAMING_BUFFER != 0
  49. #error Define QLZ_STREAMING_BUFFER to a zero value for this demo
  50. #endif
  51. static int quicklz_compress_file(int fd_in, int fd_out)
  52. {
  53. /* Start to compress file */
  54. qlz_state_compress *state_compress = RT_NULL;
  55. rt_uint8_t *cmprs_buffer = RT_NULL, *buffer = RT_NULL;
  56. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  57. size_t cmprs_size = 0, block_size = 0, totle_cmprs_size = 0;
  58. size_t file_size = 0, i = 0;
  59. int ret = 0;
  60. file_size = lseek(fd_in, 0, SEEK_END);
  61. lseek(fd_in, 0, SEEK_SET);
  62. cmprs_buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  63. buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE);
  64. if (!cmprs_buffer || !buffer)
  65. {
  66. rt_kprintf("[qlz] No memory for cmprs_buffer or buffer!\n");
  67. ret = -1;
  68. goto _exit;
  69. }
  70. state_compress = (qlz_state_compress *) malloc(sizeof(qlz_state_compress));
  71. if (!state_compress)
  72. {
  73. rt_kprintf("[qlz] No memory for state_compress struct, need %d byte, or you can change QLZ_HASH_VALUES to 1024 !\n",
  74. sizeof(qlz_state_compress));
  75. ret = -1;
  76. goto _exit;
  77. }
  78. memset(state_compress, 0x00, sizeof(qlz_state_compress));
  79. rt_kprintf("[qlz]compress start : ");
  80. for (i = 0; i < file_size; i += COMPRESS_BUFFER_SIZE)
  81. {
  82. if ((file_size - i) < COMPRESS_BUFFER_SIZE)
  83. {
  84. block_size = file_size - i;
  85. }
  86. else
  87. {
  88. block_size = COMPRESS_BUFFER_SIZE;
  89. }
  90. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE);
  91. memset(cmprs_buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  92. read(fd_in, buffer, block_size);
  93. /* The destination buffer must be at least size + 400 bytes large because incompressible data may increase in size. */
  94. cmprs_size = qlz_compress(buffer, (char *) cmprs_buffer, block_size, state_compress);
  95. /* Store compress block size to the block header (4 byte). */
  96. buffer_hdr[3] = cmprs_size % (1 << 8);
  97. buffer_hdr[2] = (cmprs_size % (1 << 16)) / (1 << 8);
  98. buffer_hdr[1] = (cmprs_size % (1 << 24)) / (1 << 16);
  99. buffer_hdr[0] = cmprs_size / (1 << 24);
  100. write(fd_out, buffer_hdr, BLOCK_HEADER_SIZE);
  101. write(fd_out, cmprs_buffer, cmprs_size);
  102. totle_cmprs_size += cmprs_size + BLOCK_HEADER_SIZE;
  103. rt_kprintf(">");
  104. }
  105. rt_kprintf("\n");
  106. rt_kprintf("[qlz]compressed %d bytes into %d bytes , compression ratio is %d%!\n", file_size, totle_cmprs_size,
  107. (totle_cmprs_size * 100) / file_size);
  108. _exit:
  109. if (cmprs_buffer)
  110. {
  111. free(cmprs_buffer);
  112. }
  113. if (buffer)
  114. {
  115. free(buffer);
  116. }
  117. if (state_compress)
  118. {
  119. free(state_compress);
  120. }
  121. return ret;
  122. }
  123. static int quicklz_decompress_file(int fd_in, int fd_out)
  124. {
  125. /* Start to decompress file */
  126. qlz_state_decompress *state_decompress = RT_NULL;
  127. rt_uint8_t *dcmprs_buffer = RT_NULL, *buffer = RT_NULL;
  128. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  129. size_t dcmprs_size = 0, block_size = 0, total_dcmprs_size = 0;
  130. size_t file_size = 0, i = 0;
  131. int ret = 0;
  132. file_size = lseek(fd_in, 0, SEEK_END);
  133. lseek(fd_in, 0, SEEK_SET);
  134. if (file_size <= BLOCK_HEADER_SIZE)
  135. {
  136. rt_kprintf("[qlz] decomprssion file size : %d error!\n", file_size);
  137. ret = -1;
  138. goto _dcmprs_exit;
  139. }
  140. dcmprs_buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE);
  141. buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  142. if (!dcmprs_buffer || !buffer)
  143. {
  144. rt_kprintf("[qlz] No memory for dcmprs_buffer or buffer!\n");
  145. ret = -1;
  146. goto _dcmprs_exit;
  147. }
  148. state_decompress = (qlz_state_decompress *) malloc(sizeof(qlz_state_decompress));
  149. if (!state_decompress)
  150. {
  151. rt_kprintf("[qlz] No memory for state_decompress struct!\n");
  152. ret = -1;
  153. goto _dcmprs_exit;
  154. }
  155. memset(state_decompress, 0x00, sizeof(qlz_state_decompress));
  156. rt_kprintf("[qlz]decompress start : ");
  157. for (i = 0; i < file_size; i += BLOCK_HEADER_SIZE + block_size)
  158. {
  159. /* Get the decompress block size from the block header. */
  160. read(fd_in, buffer_hdr, BLOCK_HEADER_SIZE);
  161. block_size = buffer_hdr[0] * (1 << 24) + buffer_hdr[1] * (1 << 16) + buffer_hdr[2] * (1 << 8) + buffer_hdr[3];
  162. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  163. memset(dcmprs_buffer, 0x00, DCOMPRESS_BUFFER_SIZE);
  164. read(fd_in, buffer, block_size);
  165. dcmprs_size = qlz_decompress((const char *) buffer, dcmprs_buffer, state_decompress);
  166. write(fd_out, dcmprs_buffer, dcmprs_size);
  167. total_dcmprs_size += dcmprs_size;
  168. rt_kprintf(">");
  169. }
  170. rt_kprintf("\n");
  171. rt_kprintf("decompressed %d bytes into %d bytes !\n", file_size, total_dcmprs_size);
  172. _dcmprs_exit:
  173. if (dcmprs_buffer)
  174. {
  175. free(dcmprs_buffer);
  176. }
  177. if(buffer)
  178. {
  179. free(buffer);
  180. }
  181. if (state_decompress)
  182. {
  183. free(state_decompress);
  184. }
  185. return ret;
  186. }
  187. int quicklz_test(int argc, char ** argv)
  188. {
  189. int fd_in = -1 , fd_out = -1;
  190. int ret = 0;
  191. if (argc != 4)
  192. {
  193. rt_kprintf("Usage:\n");
  194. rt_kprintf("qlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  195. rt_kprintf("qlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  196. ret = -1;
  197. goto _exit;
  198. }
  199. fd_in = open(argv[2], O_RDONLY, 0);
  200. if (fd_in < 0)
  201. {
  202. rt_kprintf("[qlz] open the input file : %s error!\n", argv[2]);
  203. ret = -1;
  204. goto _exit;
  205. }
  206. fd_out = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0);
  207. if (fd_out < 0)
  208. {
  209. rt_kprintf("[qlz] open the output file : %s error!\n", argv[3]);
  210. ret = -1;
  211. goto _exit;
  212. }
  213. if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
  214. {
  215. if(quicklz_compress_file(fd_in, fd_out) < 0)
  216. {
  217. rt_kprintf("[qlz] quciklz compress file error!\n");
  218. }
  219. }
  220. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  221. {
  222. if(quicklz_decompress_file(fd_in, fd_out) < 0)
  223. {
  224. rt_kprintf("[qlz] quciklz decompress file error!\n");
  225. }
  226. }
  227. else
  228. {
  229. rt_kprintf("Usage:\n");
  230. rt_kprintf("qlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  231. rt_kprintf("qlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  232. ret = -1;
  233. goto _exit;
  234. }
  235. _exit:
  236. if(fd_in >= 0)
  237. {
  238. close(fd_in);
  239. }
  240. if(fd_out >= 0)
  241. {
  242. close(fd_out);
  243. }
  244. return ret;
  245. }
  246. #ifdef RT_USING_FINSH
  247. #ifdef FINSH_USING_MSH
  248. #include <finsh.h>
  249. MSH_CMD_EXPORT_ALIAS(quicklz_test, qlz_test, quicklz compress and decompress test);
  250. #endif
  251. #endif