lzo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* lzo.c - location for general purpose minilzo functions
  2. * This file is part of RT-Thread RTOS
  3. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  4. *
  5. * The license and distribution terms for this file may be
  6. * found in the file LICENSE in this distribution or at
  7. * http://www.rt-thread.org/license/LICENSE
  8. * Change Logs:
  9. * Date Author Notes
  10. */
  11. #include <rtthread.h>
  12. #include "minilzo.h"
  13. #ifdef RT_USING_FINSH
  14. #include <finsh.h>
  15. #endif
  16. #define RT_USING_LZO
  17. #if defined(RT_USING_LZO) && defined(RT_USING_DFS)
  18. #ifdef _WIN32
  19. #pragma warning(disable: 4996)
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <io.h>
  24. #else
  25. #include <dfs_posix.h>
  26. #endif
  27. /* the worst of allocation */
  28. #define LZO1X_WORST(x) ( (x) + ((x)/16) + 64 + 3 )
  29. #define HEAP_ALLOC(var,size) \
  30. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  31. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
  32. char* parse_lzo_error_code(int error_code)
  33. {
  34. switch(error_code)
  35. {
  36. case LZO_E_ERROR: return "error";
  37. case LZO_E_OUT_OF_MEMORY: return "out of memory";
  38. case LZO_E_NOT_COMPRESSIBLE: return "not compressible";
  39. case LZO_E_INPUT_OVERRUN: return "input overrun";
  40. case LZO_E_OUTPUT_OVERRUN: return "output overrun";
  41. case LZO_E_LOOKBEHIND_OVERRUN: return "lookbehind overrun";
  42. case LZO_E_EOF_NOT_FOUND: return "eof not found";
  43. case LZO_E_INPUT_NOT_CONSUMED: return "input not consumed";
  44. case LZO_E_NOT_YET_IMPLEMENTED: return "not yet implemented"; /* [not used right now] */
  45. case LZO_E_INVALID_ARGUMENT: return "invalid argument";
  46. default: return "none";
  47. }
  48. }
  49. int lzo(char *srcfile, char *destfile)
  50. {
  51. int result;
  52. int fd;
  53. struct stat s;
  54. lzo_bytep in;
  55. lzo_bytep out;
  56. lzo_uint in_len, out_len;
  57. rt_memset(&s, 0, sizeof(struct stat));
  58. stat(srcfile, &s);
  59. in_len = s.st_size;
  60. in = rt_malloc(in_len);
  61. if (in == RT_NULL) return -1;
  62. out = rt_malloc(LZO1X_WORST(in_len));
  63. if (out == RT_NULL) return -1;
  64. fd = open(srcfile, O_RDONLY, 0);
  65. if(fd < 0)
  66. {
  67. result = -1;
  68. goto _exit;
  69. }
  70. read(fd, in, in_len);
  71. close(fd);
  72. result = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
  73. if(result != LZO_E_OK)
  74. {
  75. rt_kprintf("internal error - compression failed: \nerr_code:(%d) %s, %s.\n",
  76. result, parse_lzo_error_code(result), "Please use the binary access");
  77. goto _exit;
  78. }
  79. fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
  80. if(fd < 0)
  81. {
  82. result = -1;
  83. goto _exit;
  84. }
  85. write(fd, &in_len, sizeof(lzo_uint)); /* source file len */
  86. write(fd, out, out_len);
  87. close(fd);
  88. rt_kprintf("compress lzo ok!\n");
  89. result = 0;
  90. _exit:
  91. rt_free(in);
  92. rt_free(out);
  93. return result;
  94. }
  95. #ifdef RT_USING_FINSH
  96. FINSH_FUNCTION_EXPORT(lzo, compress a file. usage:lzo(src, dest));
  97. #endif
  98. int lzode(char *srcfile, char *destfile)
  99. {
  100. int result;
  101. int fd;
  102. struct stat s;
  103. lzo_bytep in=RT_NULL;
  104. lzo_bytep out=RT_NULL;
  105. lzo_uint in_len, out_len;
  106. rt_memset(&s, 0, sizeof(struct stat));
  107. stat(srcfile, &s);
  108. in_len = s.st_size;
  109. fd = open(srcfile, O_RDONLY, 0);
  110. if(fd < 0) return 0;
  111. read(fd, &out_len, sizeof(lzo_uint)); /* source file len */
  112. in_len -= sizeof(lzo_uint);
  113. in = rt_malloc(in_len);
  114. if (in == RT_NULL) return -1;
  115. out = rt_malloc(out_len);
  116. if (out == RT_NULL) return -1;
  117. read(fd, in, in_len);
  118. close(fd);
  119. result = lzo1x_decompress(in, in_len, out, &out_len, RT_NULL);
  120. if(result != LZO_E_OK)
  121. {
  122. rt_kprintf("internal error - decompression failed: \nerr_code:(%d) %s, %s.\n",
  123. result, parse_lzo_error_code(result), "Please use the binary access");
  124. goto _exit;
  125. }
  126. fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
  127. if(fd < 0)
  128. {
  129. result = -1;
  130. goto _exit;
  131. }
  132. write(fd, out, out_len);
  133. close(fd);
  134. rt_kprintf("decompress lzo ok!\n");
  135. result = 0;
  136. _exit:
  137. rt_free(in);
  138. rt_free(out);
  139. return result;
  140. }
  141. #ifdef RT_USING_FINSH
  142. FINSH_FUNCTION_EXPORT(lzode, decompress a file. usage:lzode(src, dest));
  143. #endif
  144. #endif