1
0

lzo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #endif
  21. #include <dfs_posix.h>
  22. /* the worst of allocation */
  23. #define LZO1X_WORST(x) ( (x) + ((x)/16) + 64 + 3 )
  24. #define HEAP_ALLOC(var,size) \
  25. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  26. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
  27. char* parse_lzo_error_code(int error_code)
  28. {
  29. switch(error_code)
  30. {
  31. case LZO_E_ERROR: return "error";
  32. case LZO_E_OUT_OF_MEMORY: return "out of memory";
  33. case LZO_E_NOT_COMPRESSIBLE: return "not compressible";
  34. case LZO_E_INPUT_OVERRUN: return "input overrun";
  35. case LZO_E_OUTPUT_OVERRUN: return "output overrun";
  36. case LZO_E_LOOKBEHIND_OVERRUN: return "lookbehind overrun";
  37. case LZO_E_EOF_NOT_FOUND: return "eof not found";
  38. case LZO_E_INPUT_NOT_CONSUMED: return "input not consumed";
  39. case LZO_E_NOT_YET_IMPLEMENTED: return "not yet implemented"; /* [not used right now] */
  40. case LZO_E_INVALID_ARGUMENT: return "invalid argument";
  41. default: return "none";
  42. }
  43. }
  44. int lzo(char *srcfile, char *destfile)
  45. {
  46. int result;
  47. int fd;
  48. struct stat s;
  49. lzo_bytep in;
  50. lzo_bytep out;
  51. lzo_uint in_len, out_len;
  52. rt_memset(&s, 0, sizeof(struct stat));
  53. stat(srcfile, &s);
  54. in_len = s.st_size;
  55. in = rt_malloc(in_len);
  56. if (in == RT_NULL) return -1;
  57. out = rt_malloc(LZO1X_WORST(in_len));
  58. if (out == RT_NULL) return -1;
  59. fd = open(srcfile, O_RDONLY, 0);
  60. if(fd < 0)
  61. {
  62. result = -1;
  63. goto _exit;
  64. }
  65. read(fd, in, in_len);
  66. close(fd);
  67. result = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
  68. if(result != LZO_E_OK)
  69. {
  70. rt_kprintf("internal error - compression failed: \nerr_code:(%d) %s, %s.\n",
  71. result, parse_lzo_error_code(result), "Please use the binary access");
  72. goto _exit;
  73. }
  74. fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
  75. if(fd < 0)
  76. {
  77. result = -1;
  78. goto _exit;
  79. }
  80. write(fd, &in_len, sizeof(lzo_uint)); /* source file len */
  81. write(fd, out, out_len);
  82. close(fd);
  83. rt_kprintf("compress lzo ok!\n");
  84. result = 0;
  85. _exit:
  86. rt_free(in);
  87. rt_free(out);
  88. return result;
  89. }
  90. #ifdef RT_USING_FINSH
  91. FINSH_FUNCTION_EXPORT(lzo, compress a file. usage:lzo(src, dest));
  92. #endif
  93. int lzode(char *srcfile, char *destfile)
  94. {
  95. int result;
  96. int fd;
  97. struct stat s;
  98. lzo_bytep in=RT_NULL;
  99. lzo_bytep out=RT_NULL;
  100. lzo_uint in_len, out_len;
  101. rt_memset(&s, 0, sizeof(struct stat));
  102. stat(srcfile, &s);
  103. in_len = s.st_size;
  104. fd = open(srcfile, O_RDONLY, 0);
  105. if(fd < 0) return 0;
  106. read(fd, &out_len, sizeof(lzo_uint)); /* source file len */
  107. in_len -= sizeof(lzo_uint);
  108. in = rt_malloc(in_len);
  109. if (in == RT_NULL) return -1;
  110. out = rt_malloc(out_len);
  111. if (out == RT_NULL) return -1;
  112. read(fd, in, in_len);
  113. close(fd);
  114. result = lzo1x_decompress(in, in_len, out, &out_len, RT_NULL);
  115. if(result != LZO_E_OK)
  116. {
  117. rt_kprintf("internal error - decompression failed: \nerr_code:(%d) %s, %s.\n",
  118. result, parse_lzo_error_code(result), "Please use the binary access");
  119. goto _exit;
  120. }
  121. fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
  122. if(fd < 0)
  123. {
  124. result = -1;
  125. goto _exit;
  126. }
  127. write(fd, out, out_len);
  128. close(fd);
  129. rt_kprintf("decompress lzo ok!\n");
  130. result = 0;
  131. _exit:
  132. rt_free(in);
  133. rt_free(out);
  134. return result;
  135. }
  136. #ifdef RT_USING_FINSH
  137. FINSH_FUNCTION_EXPORT(lzode, decompress a file. usage:lzode(src, dest));
  138. #endif
  139. #endif