fastlz_sample.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * File : fastlz_sample.c
  3. * this example is a very simple test program for the fastlz 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. * 2018-02-11 Murphy Adapted fastlz
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <rtthread.h>
  33. #include <dfs_posix.h>
  34. #include "fastlz.h"
  35. #define malloc rt_malloc
  36. #define free rt_free
  37. #define BLOCK_HEADER_SIZE 4
  38. #define COMPRESS_BUFFER_SIZE 4096
  39. #define DCOMPRESS_BUFFER_SIZE 4096
  40. /* The output buffer must be at least 5% larger than the input buffer and can not be smaller than 66 bytes */
  41. #define BUFFER_PADDING FASTLZ_BUFFER_PADDING(COMPRESS_BUFFER_SIZE)
  42. /* compress level: 1 or 2 */
  43. #define FASTLZ_COMPRESS_LEVEL FASTLZ_SAMPLE_COMPRESSION_LEVEL
  44. static int fastlz_compress_file(int fd_in, int fd_out)
  45. {
  46. /* Start to compress file */
  47. rt_uint8_t *cmprs_buffer = RT_NULL, *buffer = RT_NULL;
  48. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  49. int 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 + BUFFER_PADDING);
  55. buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE);
  56. if (!cmprs_buffer || !buffer)
  57. {
  58. rt_kprintf("[fastlz] No memory for cmprs_buffer or buffer!\n");
  59. ret = -1;
  60. goto _exit;
  61. }
  62. rt_kprintf("[fastlz]compress start : ");
  63. for (i = 0; i < file_size; i += COMPRESS_BUFFER_SIZE)
  64. {
  65. if ((file_size - i) < COMPRESS_BUFFER_SIZE)
  66. {
  67. block_size = file_size - i;
  68. }
  69. else
  70. {
  71. block_size = COMPRESS_BUFFER_SIZE;
  72. }
  73. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE);
  74. memset(cmprs_buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  75. read(fd_in, buffer, block_size);
  76. /* The destination buffer must be at least size + 400 bytes large because incompressible data may increase in size. */
  77. cmprs_size = fastlz_compress_level(FASTLZ_COMPRESS_LEVEL, buffer, block_size, (char *) cmprs_buffer);
  78. if (cmprs_size < 0)
  79. {
  80. ret = -1;
  81. goto _exit;
  82. }
  83. /* Store compress block size to the block header (4 byte). */
  84. buffer_hdr[3] = cmprs_size % (1 << 8);
  85. buffer_hdr[2] = (cmprs_size % (1 << 16)) / (1 << 8);
  86. buffer_hdr[1] = (cmprs_size % (1 << 24)) / (1 << 16);
  87. buffer_hdr[0] = cmprs_size / (1 << 24);
  88. write(fd_out, buffer_hdr, BLOCK_HEADER_SIZE);
  89. write(fd_out, cmprs_buffer, cmprs_size);
  90. totle_cmprs_size += cmprs_size + BLOCK_HEADER_SIZE;
  91. rt_kprintf(">");
  92. }
  93. rt_kprintf("\n");
  94. rt_kprintf("[fastlz]compressed %d bytes into %d bytes , compression ratio is %d%!\n", file_size, totle_cmprs_size,
  95. (totle_cmprs_size * 100) / file_size);
  96. _exit:
  97. if (cmprs_buffer)
  98. {
  99. free(cmprs_buffer);
  100. }
  101. if (buffer)
  102. {
  103. free(buffer);
  104. }
  105. return ret;
  106. }
  107. static int fastlz_decompress_file(int fd_in, int fd_out)
  108. {
  109. /* Start to decompress file */
  110. rt_uint8_t *dcmprs_buffer = RT_NULL, *buffer = RT_NULL;
  111. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  112. size_t dcmprs_size = 0, block_size = 0, total_dcmprs_size = 0;
  113. size_t file_size = 0, i = 0;
  114. int ret = 0;
  115. file_size = lseek(fd_in, 0, SEEK_END);
  116. lseek(fd_in, 0, SEEK_SET);
  117. if (file_size <= BLOCK_HEADER_SIZE)
  118. {
  119. rt_kprintf("[fastlz] decomprssion file size : %d error!\n", file_size);
  120. ret = -1;
  121. goto _dcmprs_exit;
  122. }
  123. dcmprs_buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE);
  124. buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  125. if (!dcmprs_buffer || !buffer)
  126. {
  127. rt_kprintf("[fastlz] No memory for dcmprs_buffer or buffer!\n");
  128. ret = -1;
  129. goto _dcmprs_exit;
  130. }
  131. rt_kprintf("[fastlz]decompress start : ");
  132. for (i = 0; i < file_size; i += BLOCK_HEADER_SIZE + block_size)
  133. {
  134. /* Get the decompress block size from the block header. */
  135. read(fd_in, buffer_hdr, BLOCK_HEADER_SIZE);
  136. block_size = buffer_hdr[0] * (1 << 24) + buffer_hdr[1] * (1 << 16) + buffer_hdr[2] * (1 << 8) + buffer_hdr[3];
  137. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  138. memset(dcmprs_buffer, 0x00, DCOMPRESS_BUFFER_SIZE);
  139. read(fd_in, buffer, block_size);
  140. dcmprs_size = fastlz_decompress((const void *) buffer, block_size, dcmprs_buffer, DCOMPRESS_BUFFER_SIZE);
  141. write(fd_out, dcmprs_buffer, dcmprs_size);
  142. total_dcmprs_size += dcmprs_size;
  143. rt_kprintf(">");
  144. }
  145. rt_kprintf("\n");
  146. rt_kprintf("decompressed %d bytes into %d bytes !\n", file_size, total_dcmprs_size);
  147. _dcmprs_exit:
  148. if (dcmprs_buffer)
  149. {
  150. free(dcmprs_buffer);
  151. }
  152. if(buffer)
  153. {
  154. free(buffer);
  155. }
  156. return ret;
  157. }
  158. int fastlz_test(int argc, char ** argv)
  159. {
  160. int fd_in = -1 , fd_out = -1;
  161. int ret = 0;
  162. if (argc != 4)
  163. {
  164. rt_kprintf("Usage:\n");
  165. rt_kprintf("fastlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  166. rt_kprintf("fastlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  167. ret = -1;
  168. goto _exit;
  169. }
  170. fd_in = open(argv[2], O_RDONLY, 0);
  171. if (fd_in < 0)
  172. {
  173. rt_kprintf("[fastlz] open the input file : %s error!\n", argv[2]);
  174. ret = -1;
  175. goto _exit;
  176. }
  177. fd_out = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0);
  178. if (fd_out < 0)
  179. {
  180. rt_kprintf("[fastlz] open the output file : %s error!\n", argv[3]);
  181. ret = -1;
  182. goto _exit;
  183. }
  184. if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
  185. {
  186. if(fastlz_compress_file(fd_in, fd_out) < 0)
  187. {
  188. rt_kprintf("[fastlz] fastlz compress file error!\n");
  189. }
  190. }
  191. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  192. {
  193. if(fastlz_decompress_file(fd_in, fd_out) < 0)
  194. {
  195. rt_kprintf("[fastlz] fastlz decompress file error!\n");
  196. }
  197. }
  198. else
  199. {
  200. rt_kprintf("Usage:\n");
  201. rt_kprintf("fastlz_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  202. rt_kprintf("fastlz_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  203. ret = -1;
  204. goto _exit;
  205. }
  206. _exit:
  207. if(fd_in >= 0)
  208. {
  209. close(fd_in);
  210. }
  211. if(fd_out >= 0)
  212. {
  213. close(fd_out);
  214. }
  215. return ret;
  216. }
  217. #ifdef RT_USING_FINSH
  218. #ifdef FINSH_USING_MSH
  219. #include <finsh.h>
  220. MSH_CMD_EXPORT(fastlz_test, fastlz compress and decompress test);
  221. #endif
  222. #endif