quicklz_sample.c 8.5 KB

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