lzo.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
  103. /**
  104. * \brief copy previously decoded bytes to current position
  105. * \param back how many bytes back we start
  106. * \param cnt number of bytes to copy, must be >= 0
  107. *
  108. * cnt > back is valid, this will copy the bytes we just copied,
  109. * thus creating a repeating pattern with a period length of back.
  110. */
  111. static inline void copy_backptr(LZOContext *c, int back, int cnt) {
  112. register const uint8_t *src = &c->out[-back];
  113. register uint8_t *dst = c->out;
  114. if (src < c->out_start || src > dst) {
  115. c->error |= LZO_INVALID_BACKPTR;
  116. return;
  117. }
  118. if (cnt > c->out_end - dst) {
  119. cnt = FFMAX(c->out_end - dst, 0);
  120. c->error |= LZO_OUTPUT_FULL;
  121. }
  122. memcpy_backptr(dst, back, cnt);
  123. c->out = dst + cnt;
  124. }
  125. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
  126. const uint8_t *src = &dst[-back];
  127. if (back == 1) {
  128. memset(dst, *src, cnt);
  129. } else {
  130. #ifdef OUTBUF_PADDED
  131. COPY2(dst, src);
  132. COPY2(dst + 2, src + 2);
  133. src += 4;
  134. dst += 4;
  135. cnt -= 4;
  136. if (cnt > 0) {
  137. COPY2(dst, src);
  138. COPY2(dst + 2, src + 2);
  139. COPY2(dst + 4, src + 4);
  140. COPY2(dst + 6, src + 6);
  141. src += 8;
  142. dst += 8;
  143. cnt -= 8;
  144. }
  145. #endif
  146. if (cnt > 0) {
  147. int blocklen = back;
  148. while (cnt > blocklen) {
  149. memcpy(dst, src, blocklen);
  150. dst += blocklen;
  151. cnt -= blocklen;
  152. blocklen <<= 1;
  153. }
  154. memcpy(dst, src, cnt);
  155. }
  156. }
  157. }
  158. /**
  159. * \brief deliberately overlapping memcpy implementation
  160. * \param dst destination buffer; must be padded with 12 additional bytes
  161. * \param back how many bytes back we start (the initial size of the overlapping window)
  162. * \param cnt number of bytes to copy, must be >= 0
  163. *
  164. * cnt > back is valid, this will copy the bytes we just copied,
  165. * thus creating a repeating pattern with a period length of back.
  166. */
  167. void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
  168. memcpy_backptr(dst, back, cnt);
  169. }
  170. /**
  171. * \brief decode LZO 1x compressed data
  172. * \param out output buffer
  173. * \param outlen size of output buffer, number of bytes left are returned here
  174. * \param in input buffer
  175. * \param inlen size of input buffer, number of bytes left are returned here
  176. * \return 0 on success, otherwise error flags, see lzo.h
  177. *
  178. * make sure all buffers are appropriately padded, in must provide
  179. * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
  180. */
  181. int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
  182. int state= 0;
  183. int x;
  184. LZOContext c;
  185. c.in = in;
  186. c.in_end = (const uint8_t *)in + *inlen;
  187. c.out = c.out_start = out;
  188. c.out_end = (uint8_t *)out + * outlen;
  189. c.error = 0;
  190. x = GETB(c);
  191. if (x > 17) {
  192. copy(&c, x - 17);
  193. x = GETB(c);
  194. if (x < 16) c.error |= LZO_ERROR;
  195. }
  196. if (c.in > c.in_end)
  197. c.error |= LZO_INPUT_DEPLETED;
  198. while (!c.error) {
  199. int cnt, back;
  200. if (x > 15) {
  201. if (x > 63) {
  202. cnt = (x >> 5) - 1;
  203. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  204. } else if (x > 31) {
  205. cnt = get_len(&c, x, 31);
  206. x = GETB(c);
  207. back = (GETB(c) << 6) + (x >> 2) + 1;
  208. } else {
  209. cnt = get_len(&c, x, 7);
  210. back = (1 << 14) + ((x & 8) << 11);
  211. x = GETB(c);
  212. back += (GETB(c) << 6) + (x >> 2);
  213. if (back == (1 << 14)) {
  214. if (cnt != 1)
  215. c.error |= LZO_ERROR;
  216. break;
  217. }
  218. }
  219. } else if(!state){
  220. cnt = get_len(&c, x, 15);
  221. copy(&c, cnt + 3);
  222. x = GETB(c);
  223. if (x > 15)
  224. continue;
  225. cnt = 1;
  226. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  227. } else {
  228. cnt = 0;
  229. back = (GETB(c) << 2) + (x >> 2) + 1;
  230. }
  231. copy_backptr(&c, back, cnt + 2);
  232. state=
  233. cnt = x & 3;
  234. copy(&c, cnt);
  235. x = GETB(c);
  236. }
  237. *inlen = c.in_end - c.in;
  238. if (c.in > c.in_end)
  239. *inlen = 0;
  240. *outlen = c.out_end - c.out;
  241. return c.error;
  242. }
  243. #ifdef TEST
  244. #include <stdio.h>
  245. #include <lzo/lzo1x.h>
  246. #include "log.h"
  247. #define MAXSZ (10*1024*1024)
  248. int main(int argc, char *argv[]) {
  249. FILE *in = fopen(argv[1], "rb");
  250. uint8_t *orig = av_malloc(MAXSZ + 16);
  251. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  252. uint8_t *decomp = av_malloc(MAXSZ + 16);
  253. size_t s = fread(orig, 1, MAXSZ, in);
  254. lzo_uint clen = 0;
  255. long tmp[LZO1X_MEM_COMPRESS];
  256. int inlen, outlen;
  257. int i;
  258. av_log_level = AV_LOG_DEBUG;
  259. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  260. for (i = 0; i < 300; i++) {
  261. START_TIMER
  262. inlen = clen; outlen = MAXSZ;
  263. #ifdef LIBLZO
  264. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  265. #elif defined(LIBLZO_UNSAFE)
  266. if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
  267. #else
  268. if (lzo1x_decode(decomp, &outlen, comp, &inlen))
  269. #endif
  270. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  271. STOP_TIMER("lzod")
  272. }
  273. if (memcmp(orig, decomp, s))
  274. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  275. else
  276. av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
  277. return 0;
  278. }
  279. #endif