quicklz_sample.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include <dfs_posix.h>
  33. #include "quicklz.h"
  34. #define malloc rt_malloc
  35. #define free rt_free
  36. #define BLOCK_HEADER_SIZE 4
  37. #define COMPRESS_BUFFER_PADDING 400
  38. #define COMPRESS_BUFFER_SIZE 4096
  39. #define DCOMPRESS_BUFFER_SIZE 4096
  40. #if QLZ_STREAMING_BUFFER != 0
  41. #error Define QLZ_STREAMING_BUFFER to a zero value for this demo
  42. #endif
  43. static int quicklz_compress_file(int fd_in, int fd_out)
  44. {
  45. /* Start to compress file */
  46. qlz_state_compress *state_compress = RT_NULL;
  47. rt_uint8_t *cmprs_buffer = RT_NULL, *buffer = RT_NULL;
  48. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  49. size_t cmprs_size = 0, block_size = 0, totle_cmprs_size = 0;
  50. size_t file_size = 0, i = 0;
  51. int ret = 0;
  52. file_size = lseek(fd_in, 0, SEEK_END);
  53. lseek(fd_in, 0, SEEK_SET);
  54. cmprs_buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
  55. buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE);
  56. if (!cmprs_buffer || !buffer)
  57. {
  58. rt_kprintf("[qlz] No memory for cmprs_buffer or buffer!\n");
  59. ret = -1;
  60. goto _exit;
  61. }
  62. state_compress = (qlz_state_compress *) malloc(sizeof(qlz_state_compress));
  63. if (!state_compress)
  64. {
  65. rt_kprintf("[qlz] No memory for state_compress struct, need %d byte, or you can change QLZ_HASH_VALUES to 1024 !\n",
  66. sizeof(qlz_state_compress));
  67. ret = -1;
  68. goto _exit;
  69. }
  70. memset(state_compress, 0x00, sizeof(qlz_state_compress));
  71. rt_kprintf("[qlz]compress start : ");
  72. for (i = 0; i < file_size; i += COMPRESS_BUFFER_SIZE)
  73. {
  74. if ((file_size - i) < COMPRESS_BUFFER_SIZE)
  75. {
  76. block_size = file_size - i;
  77. }
  78. else
  79. {
  80. block_size = COMPRESS_BUFFER_SIZE;
  81. }
  82. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE);
  83. memset(cmprs_buffer, 0x00, COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
  84. read(fd_in, buffer, block_size);
  85. /* The destination buffer must be at least size + 400 bytes large because incompressible data may increase in size. */
  86. cmprs_size = qlz_compress(buffer, (char *) cmprs_buffer, block_size, state_compress);
  87. /* Store compress block size to the block header (4 byte). */
  88. buffer_hdr[3] = cmprs_size % (1 << 8);
  89. buffer_hdr[2] = (cmprs_size % (1 << 16)) / (1 << 8);
  90. buffer_hdr[1] = (cmprs_size % (1 << 24)) / (1 << 16);
  91. buffer_hdr[0] = cmprs_size / (1 << 24);
  92. write(fd_out, buffer_hdr, BLOCK_HEADER_SIZE);
  93. write(fd_out, cmprs_buffer, cmprs_size);
  94. totle_cmprs_size += cmprs_size + BLOCK_HEADER_SIZE;
  95. rt_kprintf(">");
  96. }
  97. rt_kprintf("\n");
  98. rt_kprintf("[qlz]compressed %d bytes into %d bytes , compression ratio is %d%!\n", file_size, totle_cmprs_size,
  99. (totle_cmprs_size * 100) / file_size);
  100. _exit:
  101. if (cmprs_buffer)
  102. {
  103. free(cmprs_buffer);
  104. }
  105. if (buffer)
  106. {
  107. free(buffer);
  108. }
  109. if (state_compress)
  110. {
  111. free(state_compress);
  112. }
  113. return ret;
  114. }
  115. static int quicklz_decompress_file(int fd_in, int fd_out)
  116. {
  117. /* Start to decompress file */
  118. qlz_state_decompress *state_decompress = RT_NULL;
  119. rt_uint8_t *dcmprs_buffer = RT_NULL, *buffer = RT_NULL;
  120. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  121. size_t dcmprs_size = 0, block_size = 0, total_dcmprs_size = 0;
  122. size_t file_size = 0, i = 0;
  123. int ret = 0;
  124. file_size = lseek(fd_in, 0, SEEK_END);
  125. lseek(fd_in, 0, SEEK_SET);
  126. if (file_size <= BLOCK_HEADER_SIZE)
  127. {
  128. rt_kprintf("[qlz] decomprssion file size : %d error!\n", file_size);
  129. ret = -1;
  130. goto _dcmprs_exit;
  131. }
  132. dcmprs_buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE);
  133. buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
  134. if (!dcmprs_buffer || !buffer)
  135. {
  136. rt_kprintf("[qlz] No memory for dcmprs_buffer or buffer!\n");
  137. ret = -1;
  138. goto _dcmprs_exit;
  139. }
  140. state_decompress = (qlz_state_decompress *) malloc(sizeof(qlz_state_decompress));
  141. if (!state_decompress)
  142. {
  143. rt_kprintf("[qlz] No memory for state_decompress struct!\n");
  144. ret = -1;
  145. goto _dcmprs_exit;
  146. }
  147. memset(state_decompress, 0x00, sizeof(qlz_state_decompress));
  148. rt_kprintf("[qlz]decompress start : ");
  149. for (i = 0; i < file_size; i += BLOCK_HEADER_SIZE + block_size)
  150. {
  151. /* Get the decompress block size from the block header. */
  152. read(fd_in, buffer_hdr, BLOCK_HEADER_SIZE);
  153. block_size = buffer_hdr[0] * (1 << 24) + buffer_hdr[1] * (1 << 16) + buffer_hdr[2] * (1 << 8) + buffer_hdr[3];
  154. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
  155. memset(dcmprs_buffer, 0x00, DCOMPRESS_BUFFER_SIZE);
  156. read(fd_in, buffer, block_size);
  157. dcmprs_size = qlz_decompress((const char *) buffer, dcmprs_buffer, state_decompress);
  158. write(fd_out, dcmprs_buffer, dcmprs_size);
  159. total_dcmprs_size += dcmprs_size;
  160. rt_kprintf(">");
  161. }
  162. rt_kprintf("\n");
  163. rt_kprintf("decompressed %d bytes into %d bytes !\n", file_size, total_dcmprs_size);
  164. _dcmprs_exit:
  165. if (dcmprs_buffer)
  166. {
  167. free(dcmprs_buffer);
  168. }
  169. if(buffer)
  170. {
  171. free(buffer);
  172. }
  173. if (state_decompress)
  174. {
  175. free(state_decompress);
  176. }
  177. return ret;
  178. }
  179. int quicklz_test(int argc, char ** argv)
  180. {
  181. int fd_in = -1 , fd_out = -1;
  182. int ret = 0;
  183. if (argc != 4)
  184. {
  185. rt_kprintf("Usage:\n");
  186. rt_kprintf("qlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  187. rt_kprintf("qlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  188. ret = -1;
  189. goto _exit;
  190. }
  191. fd_in = open(argv[2], O_RDONLY, 0);
  192. if (fd_in < 0)
  193. {
  194. rt_kprintf("[qlz] open the input file : %s error!\n", argv[2]);
  195. ret = -1;
  196. goto _exit;
  197. }
  198. fd_out = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0);
  199. if (fd_out < 0)
  200. {
  201. rt_kprintf("[qlz] open the output file : %s error!\n", argv[3]);
  202. ret = -1;
  203. goto _exit;
  204. }
  205. if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
  206. {
  207. if(quicklz_compress_file(fd_in, fd_out) < 0)
  208. {
  209. rt_kprintf("[qlz] quciklz compress file error!\n");
  210. }
  211. }
  212. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  213. {
  214. if(quicklz_decompress_file(fd_in, fd_out) < 0)
  215. {
  216. rt_kprintf("[qlz] quciklz decompress file error!\n");
  217. }
  218. }
  219. else
  220. {
  221. rt_kprintf("Usage:\n");
  222. rt_kprintf("qlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  223. rt_kprintf("qlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  224. ret = -1;
  225. goto _exit;
  226. }
  227. _exit:
  228. if(fd_in >= 0)
  229. {
  230. close(fd_in);
  231. }
  232. if(fd_out >= 0)
  233. {
  234. close(fd_out);
  235. }
  236. return ret;
  237. }
  238. #ifdef RT_USING_FINSH
  239. #ifdef FINSH_USING_MSH
  240. #include <finsh.h>
  241. MSH_CMD_EXPORT_ALIAS(quicklz_test, qlz_test, quicklz compress and decompress test);
  242. #endif
  243. #endif