lzo.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * LZO 1x decompression
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "common.h"
  22. //! avoid e.g. MPlayers fast_memcpy, it slows things down here
  23. #undef memcpy
  24. #include <string.h>
  25. #include "lzo.h"
  26. //! define if we may write up to 12 bytes beyond the output buffer
  27. #define OUTBUF_PADDED 1
  28. //! define if we may read up to 8 bytes beyond the input buffer
  29. #define INBUF_PADDED 1
  30. typedef struct LZOContext {
  31. const uint8_t *in, *in_end;
  32. uint8_t *out_start, *out, *out_end;
  33. int error;
  34. } LZOContext;
  35. /**
  36. * \brief read one byte from input buffer, avoiding overrun
  37. * \return byte read
  38. */
  39. static inline int get_byte(LZOContext *c) {
  40. if (c->in < c->in_end)
  41. return *c->in++;
  42. c->error |= LZO_INPUT_DEPLETED;
  43. return 1;
  44. }
  45. #ifdef INBUF_PADDED
  46. #define GETB(c) (*(c).in++)
  47. #else
  48. #define GETB(c) get_byte(&(c))
  49. #endif
  50. /**
  51. * \brief decode a length value in the coding used by lzo
  52. * \param x previous byte value
  53. * \param mask bits used from x
  54. * \return decoded length value
  55. */
  56. static inline int get_len(LZOContext *c, int x, int mask) {
  57. int cnt = x & mask;
  58. if (!cnt) {
  59. while (!(x = get_byte(c))) cnt += 255;
  60. cnt += mask + x;
  61. }
  62. return cnt;
  63. }
  64. //#define UNALIGNED_LOADSTORE
  65. #define BUILTIN_MEMCPY
  66. #ifdef UNALIGNED_LOADSTORE
  67. #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
  68. #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
  69. #elif defined(BUILTIN_MEMCPY)
  70. #define COPY2(d, s) memcpy(d, s, 2);
  71. #define COPY4(d, s) memcpy(d, s, 4);
  72. #else
  73. #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
  74. #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
  75. #endif
  76. /**
  77. * \brief copy bytes from input to output buffer with checking
  78. * \param cnt number of bytes to copy, must be >= 0
  79. */
  80. static inline void copy(LZOContext *c, int cnt) {
  81. register const uint8_t *src = c->in;
  82. register uint8_t *dst = c->out;
  83. if (cnt > c->in_end - src) {
  84. cnt = FFMAX(c->in_end - src, 0);
  85. c->error |= LZO_INPUT_DEPLETED;
  86. }
  87. if (cnt > c->out_end - dst) {
  88. cnt = FFMAX(c->out_end - dst, 0);
  89. c->error |= LZO_OUTPUT_FULL;
  90. }
  91. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  92. COPY4(dst, src);
  93. src += 4;
  94. dst += 4;
  95. cnt -= 4;
  96. if (cnt > 0)
  97. #endif
  98. memcpy(dst, src, cnt);
  99. c->in = src + cnt;
  100. c->out = dst + cnt;
  101. }
  102. /**
  103. * \brief copy previously decoded bytes to current position
  104. * \param back how many bytes back we start
  105. * \param cnt number of bytes to copy, must be >= 0
  106. *
  107. * cnt > back is valid, this will copy the bytes we just copied,
  108. * thus creating a repeating pattern with a period length of back.
  109. */
  110. static inline void copy_backptr(LZOContext *c, int back, int cnt) {
  111. register const uint8_t *src = &c->out[-back];
  112. register uint8_t *dst = c->out;
  113. if (src < c->out_start || src > dst) {
  114. c->error |= LZO_INVALID_BACKPTR;
  115. return;
  116. }
  117. if (cnt > c->out_end - dst) {
  118. cnt = FFMAX(c->out_end - dst, 0);
  119. c->error |= LZO_OUTPUT_FULL;
  120. }
  121. if (back == 1) {
  122. memset(dst, *src, cnt);
  123. dst += cnt;
  124. } else {
  125. #ifdef OUTBUF_PADDED
  126. COPY2(dst, src);
  127. COPY2(dst + 2, src + 2);
  128. src += 4;
  129. dst += 4;
  130. cnt -= 4;
  131. if (cnt > 0) {
  132. COPY2(dst, src);
  133. COPY2(dst + 2, src + 2);
  134. COPY2(dst + 4, src + 4);
  135. COPY2(dst + 6, src + 6);
  136. src += 8;
  137. dst += 8;
  138. cnt -= 8;
  139. }
  140. #endif
  141. if (cnt > 0) {
  142. int blocklen = back;
  143. while (cnt > blocklen) {
  144. memcpy(dst, src, blocklen);
  145. dst += blocklen;
  146. cnt -= blocklen;
  147. blocklen <<= 1;
  148. }
  149. memcpy(dst, src, cnt);
  150. }
  151. dst += cnt;
  152. }
  153. c->out = dst;
  154. }
  155. /**
  156. * \brief decode LZO 1x compressed data
  157. * \param out output buffer
  158. * \param outlen size of output buffer, number of bytes left are returned here
  159. * \param in input buffer
  160. * \param inlen size of input buffer, number of bytes left are returned here
  161. * \return 0 on success, otherwise error flags, see lzo.h
  162. *
  163. * make sure all buffers are appropriately padded, in must provide
  164. * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
  165. */
  166. int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
  167. int state= 0;
  168. int x;
  169. LZOContext c;
  170. c.in = in;
  171. c.in_end = (const uint8_t *)in + *inlen;
  172. c.out = c.out_start = out;
  173. c.out_end = (uint8_t *)out + * outlen;
  174. c.error = 0;
  175. x = GETB(c);
  176. if (x > 17) {
  177. copy(&c, x - 17);
  178. x = GETB(c);
  179. if (x < 16) c.error |= LZO_ERROR;
  180. }
  181. if (c.in > c.in_end)
  182. c.error |= LZO_INPUT_DEPLETED;
  183. while (!c.error) {
  184. int cnt, back;
  185. if (x > 15) {
  186. if (x > 63) {
  187. cnt = (x >> 5) - 1;
  188. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  189. } else if (x > 31) {
  190. cnt = get_len(&c, x, 31);
  191. x = GETB(c);
  192. back = (GETB(c) << 6) + (x >> 2) + 1;
  193. } else {
  194. cnt = get_len(&c, x, 7);
  195. back = (1 << 14) + ((x & 8) << 11);
  196. x = GETB(c);
  197. back += (GETB(c) << 6) + (x >> 2);
  198. if (back == (1 << 14)) {
  199. if (cnt != 1)
  200. c.error |= LZO_ERROR;
  201. break;
  202. }
  203. }
  204. } else if(!state){
  205. cnt = get_len(&c, x, 15);
  206. copy(&c, cnt + 3);
  207. x = GETB(c);
  208. if (x > 15)
  209. continue;
  210. cnt = 1;
  211. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  212. } else {
  213. cnt = 0;
  214. back = (GETB(c) << 2) + (x >> 2) + 1;
  215. }
  216. copy_backptr(&c, back, cnt + 2);
  217. state=
  218. cnt = x & 3;
  219. copy(&c, cnt);
  220. x = GETB(c);
  221. }
  222. *inlen = c.in_end - c.in;
  223. if (c.in > c.in_end)
  224. *inlen = 0;
  225. *outlen = c.out_end - c.out;
  226. return c.error;
  227. }
  228. #ifdef TEST
  229. #include <stdio.h>
  230. #include <lzo/lzo1x.h>
  231. #include "log.h"
  232. #define MAXSZ (10*1024*1024)
  233. int main(int argc, char *argv[]) {
  234. FILE *in = fopen(argv[1], "rb");
  235. uint8_t *orig = av_malloc(MAXSZ + 16);
  236. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  237. uint8_t *decomp = av_malloc(MAXSZ + 16);
  238. size_t s = fread(orig, 1, MAXSZ, in);
  239. lzo_uint clen = 0;
  240. long tmp[LZO1X_MEM_COMPRESS];
  241. int inlen, outlen;
  242. int i;
  243. av_log_level = AV_LOG_DEBUG;
  244. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  245. for (i = 0; i < 300; i++) {
  246. START_TIMER
  247. inlen = clen; outlen = MAXSZ;
  248. #ifdef LIBLZO
  249. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  250. #elif defined(LIBLZO_UNSAFE)
  251. if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
  252. #else
  253. if (lzo1x_decode(decomp, &outlen, comp, &inlen))
  254. #endif
  255. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  256. STOP_TIMER("lzod")
  257. }
  258. if (memcmp(orig, decomp, s))
  259. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  260. else
  261. av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
  262. return 0;
  263. }
  264. #endif