minilzo_sample.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * File : minilzo_sample.c
  3. * this example is a very simple test program for the minilzo 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 minilzo
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <rtthread.h>
  33. #include <dfs_posix.h>
  34. #include "minilzo.h"
  35. #define malloc rt_malloc
  36. #define free rt_free
  37. #define BLOCK_HEADER_SIZE 4
  38. /* The output buffer can not be smaller than 66 bytes */
  39. #define COMPRESS_BUFFER_SIZE 4096
  40. #define DCOMPRESS_BUFFER_SIZE 4096
  41. /* we must provide a little more output space in case that compression is not possible */
  42. #define BUFFER_PADDING MINILZO_BUFFER_PADDING(COMPRESS_BUFFER_SIZE)
  43. static int minilzo_compress_file(int fd_in, int fd_out)
  44. {
  45. /* Start to compress file */
  46. rt_uint8_t *cmprs_buffer = RT_NULL, *buffer = RT_NULL;
  47. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  48. int cmprs_size = 0, block_size = 0, totle_cmprs_size = 0;
  49. size_t file_size = 0, i = 0;
  50. int ret = 0;
  51. /* Work-memory needed for compression */
  52. lzo_voidp wrkmem = (lzo_voidp)malloc(LZO1X_1_MEM_COMPRESS);
  53. memset(wrkmem, 0x00, LZO1X_1_MEM_COMPRESS);
  54. file_size = lseek(fd_in, 0, SEEK_END);
  55. lseek(fd_in, 0, SEEK_SET);
  56. cmprs_buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  57. buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE);
  58. if (!cmprs_buffer || !buffer)
  59. {
  60. rt_kprintf("[minilzo] No memory for cmprs_buffer or buffer!\n");
  61. ret = -1;
  62. goto _exit;
  63. }
  64. rt_kprintf("[minilzo]compress start : ");
  65. for (i = 0; i < file_size; i += COMPRESS_BUFFER_SIZE)
  66. {
  67. if ((file_size - i) < COMPRESS_BUFFER_SIZE)
  68. {
  69. block_size = file_size - i;
  70. }
  71. else
  72. {
  73. block_size = COMPRESS_BUFFER_SIZE;
  74. }
  75. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE);
  76. memset(cmprs_buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  77. read(fd_in, buffer, block_size);
  78. /* The destination buffer must be at least size + 400 bytes large because incompressible data may increase in size. */
  79. ret = lzo1x_1_compress(buffer, block_size, cmprs_buffer, &cmprs_size, wrkmem);
  80. if (ret != LZO_E_OK)
  81. {
  82. ret = -1;
  83. goto _exit;
  84. }
  85. /* Store compress block size to the block header (4 byte). */
  86. buffer_hdr[3] = cmprs_size % (1 << 8);
  87. buffer_hdr[2] = (cmprs_size % (1 << 16)) / (1 << 8);
  88. buffer_hdr[1] = (cmprs_size % (1 << 24)) / (1 << 16);
  89. buffer_hdr[0] = cmprs_size / (1 << 24);
  90. write(fd_out, buffer_hdr, BLOCK_HEADER_SIZE);
  91. write(fd_out, cmprs_buffer, cmprs_size);
  92. totle_cmprs_size += cmprs_size + BLOCK_HEADER_SIZE;
  93. rt_kprintf(">");
  94. }
  95. rt_kprintf("\n");
  96. rt_kprintf("[minilzo]compressed %d bytes into %d bytes , compression ratio is %d%!\n", file_size, totle_cmprs_size,
  97. (totle_cmprs_size * 100) / file_size);
  98. _exit:
  99. if (cmprs_buffer)
  100. {
  101. free(cmprs_buffer);
  102. }
  103. if (buffer)
  104. {
  105. free(buffer);
  106. }
  107. if (wrkmem)
  108. {
  109. free(wrkmem);
  110. }
  111. return ret;
  112. }
  113. static int minilzo_decompress_file(int fd_in, int fd_out)
  114. {
  115. /* Start to decompress file */
  116. rt_uint8_t *dcmprs_buffer = RT_NULL, *buffer = RT_NULL;
  117. rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
  118. size_t dcmprs_size = 0, block_size = 0, total_dcmprs_size = 0;
  119. size_t file_size = 0, i = 0;
  120. int ret = 0;
  121. file_size = lseek(fd_in, 0, SEEK_END);
  122. lseek(fd_in, 0, SEEK_SET);
  123. if (file_size <= BLOCK_HEADER_SIZE)
  124. {
  125. rt_kprintf("[minilzo] decomprssion file size : %d error!\n", file_size);
  126. ret = -1;
  127. goto _dcmprs_exit;
  128. }
  129. dcmprs_buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE);
  130. buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  131. if (!dcmprs_buffer || !buffer)
  132. {
  133. rt_kprintf("[minilzo] No memory for dcmprs_buffer or buffer!\n");
  134. ret = -1;
  135. goto _dcmprs_exit;
  136. }
  137. rt_kprintf("[minilzo]decompress start : ");
  138. for (i = 0; i < file_size; i += BLOCK_HEADER_SIZE + block_size)
  139. {
  140. /* Get the decompress block size from the block header. */
  141. read(fd_in, buffer_hdr, BLOCK_HEADER_SIZE);
  142. block_size = buffer_hdr[0] * (1 << 24) + buffer_hdr[1] * (1 << 16) + buffer_hdr[2] * (1 << 8) + buffer_hdr[3];
  143. memset(buffer, 0x00, COMPRESS_BUFFER_SIZE + BUFFER_PADDING);
  144. memset(dcmprs_buffer, 0x00, DCOMPRESS_BUFFER_SIZE);
  145. read(fd_in, buffer, block_size);
  146. ret = lzo1x_decompress(buffer, block_size, dcmprs_buffer, &dcmprs_size, NULL);
  147. if (ret != LZO_E_OK)
  148. {
  149. ret = -1;
  150. goto _dcmprs_exit;
  151. }
  152. write(fd_out, dcmprs_buffer, dcmprs_size);
  153. total_dcmprs_size += dcmprs_size;
  154. rt_kprintf(">");
  155. }
  156. rt_kprintf("\n");
  157. rt_kprintf("decompressed %d bytes into %d bytes !\n", file_size, total_dcmprs_size);
  158. _dcmprs_exit:
  159. if (dcmprs_buffer)
  160. {
  161. free(dcmprs_buffer);
  162. }
  163. if(buffer)
  164. {
  165. free(buffer);
  166. }
  167. return ret;
  168. }
  169. int minilzo_test(int argc, char ** argv)
  170. {
  171. int fd_in = -1 , fd_out = -1;
  172. int ret = 0;
  173. if (argc != 4)
  174. {
  175. rt_kprintf("Usage:\n");
  176. rt_kprintf("minilzo_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  177. rt_kprintf("minilzo_test -d [cmprs_file] [dcmprs_file] -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
  178. ret = -1;
  179. goto _exit;
  180. }
  181. rt_kprintf("\nminiLZO real-time data compression library (v%s, %s).\n",
  182. lzo_version_string(), lzo_version_date());
  183. /*
  184. * Initialize the LZO library
  185. */
  186. if (lzo_init() != LZO_E_OK)
  187. {
  188. rt_kprintf("internal error - lzo_init() failed !!!\n");
  189. return 3;
  190. }
  191. fd_in = open(argv[2], O_RDONLY, 0);
  192. if (fd_in < 0)
  193. {
  194. rt_kprintf("[minilzo] 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("[minilzo] 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(minilzo_compress_file(fd_in, fd_out) < 0)
  208. {
  209. rt_kprintf("[minilzo] minilzo compress file error!\n");
  210. }
  211. }
  212. else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
  213. {
  214. if(minilzo_decompress_file(fd_in, fd_out) < 0)
  215. {
  216. rt_kprintf("[minilzo] minilzo decompress file error!\n");
  217. }
  218. }
  219. else
  220. {
  221. rt_kprintf("Usage:\n");
  222. rt_kprintf("minilzo_test -c [file] [cmprs_file] -compress \"file\" to \"cmprs_file\" \n");
  223. rt_kprintf("minilzo_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(minilzo_test, minilzo compress and decompress test);
  242. #endif
  243. #endif