lzo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "avutil.h"
  22. //#include "avassert.h"
  23. //#include "common.h"
  24. //#include "intreadwrite.h"
  25. #include "lzo.h"
  26. #include <string.h>
  27. #include <limits.h>
  28. /// Define if we may write up to 12 bytes beyond the output buffer.
  29. #define OUTBUF_PADDED 1
  30. /// Define if we may read up to 8 bytes beyond the input buffer.
  31. #define INBUF_PADDED 1
  32. typedef struct LZOContext {
  33. const uint8_t *in, *in_end;
  34. uint8_t *out_start, *out, *out_end;
  35. int error;
  36. } LZOContext;
  37. /**
  38. * @brief Reads one byte from the input buffer, avoiding an overrun.
  39. * @return byte read
  40. */
  41. static inline int get_byte(LZOContext *c)
  42. {
  43. if (c->in < c->in_end)
  44. return *c->in++;
  45. c->error |= AV_LZO_INPUT_DEPLETED;
  46. return 1;
  47. }
  48. #ifdef INBUF_PADDED
  49. #define GETB(c) (*(c).in++)
  50. #else
  51. #define GETB(c) get_byte(&(c))
  52. #endif
  53. /**
  54. * @brief Decodes a length value in the coding used by lzo.
  55. * @param x previous byte value
  56. * @param mask bits used from x
  57. * @return decoded length value
  58. */
  59. static inline int get_len(LZOContext *c, int x, int mask)
  60. {
  61. int cnt = x & mask;
  62. if (!cnt) {
  63. while (!(x = get_byte(c))) {
  64. if (cnt >= INT_MAX - 1000) {
  65. c->error |= AV_LZO_ERROR;
  66. break;
  67. }
  68. cnt += 255;
  69. }
  70. cnt += mask + x;
  71. }
  72. return cnt;
  73. }
  74. /**
  75. * @brief Copies bytes from input to output buffer with checking.
  76. * @param cnt number of bytes to copy, must be >= 0
  77. */
  78. static inline void copy(LZOContext *c, int cnt)
  79. {
  80. register const uint8_t *src = c->in;
  81. register uint8_t *dst = c->out;
  82. /* Should never happen */
  83. if (cnt < 0) {
  84. c->error |= AV_LZO_ERROR;
  85. return;
  86. }
  87. if (cnt > c->in_end - src) {
  88. cnt = FFMAX(c->in_end - src, 0);
  89. c->error |= AV_LZO_INPUT_DEPLETED;
  90. }
  91. if (cnt > c->out_end - dst) {
  92. cnt = FFMAX(c->out_end - dst, 0);
  93. c->error |= AV_LZO_OUTPUT_FULL;
  94. }
  95. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  96. AV_COPY32U(dst, src);
  97. src += 4;
  98. dst += 4;
  99. cnt -= 4;
  100. if (cnt > 0)
  101. #endif
  102. memcpy(dst, src, cnt);
  103. c->in = src + cnt;
  104. c->out = dst + cnt;
  105. }
  106. /**
  107. * @brief Copies previously decoded bytes to current position.
  108. * @param back how many bytes back we start, must be > 0
  109. * @param cnt number of bytes to copy, must be > 0
  110. *
  111. * cnt > back is valid, this will copy the bytes we just copied,
  112. * thus creating a repeating pattern with a period length of back.
  113. */
  114. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  115. {
  116. register uint8_t *dst = c->out;
  117. /* Should never happen */
  118. if (cnt <= 0) {
  119. c->error |= AV_LZO_ERROR;
  120. return;
  121. }
  122. if (dst - c->out_start < back) {
  123. c->error |= AV_LZO_INVALID_BACKPTR;
  124. return;
  125. }
  126. if (cnt > c->out_end - dst) {
  127. cnt = FFMAX(c->out_end - dst, 0);
  128. c->error |= AV_LZO_OUTPUT_FULL;
  129. }
  130. av_memcpy_backptr(dst, back, cnt);
  131. c->out = dst + cnt;
  132. }
  133. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
  134. {
  135. int state = 0;
  136. int x;
  137. LZOContext c;
  138. if (*outlen <= 0 || *inlen <= 0) {
  139. int res = 0;
  140. if (*outlen <= 0)
  141. res |= AV_LZO_OUTPUT_FULL;
  142. if (*inlen <= 0)
  143. res |= AV_LZO_INPUT_DEPLETED;
  144. return res;
  145. }
  146. c.in = in;
  147. c.in_end = (const uint8_t *)in + *inlen;
  148. c.out = c.out_start = out;
  149. c.out_end = (uint8_t *)out + *outlen;
  150. c.error = 0;
  151. x = GETB(c);
  152. if (x > 17) {
  153. copy(&c, x - 17);
  154. x = GETB(c);
  155. if (x < 16)
  156. c.error |= AV_LZO_ERROR;
  157. }
  158. if (c.in > c.in_end)
  159. c.error |= AV_LZO_INPUT_DEPLETED;
  160. while (!c.error) {
  161. int cnt, back;
  162. if (x > 15) {
  163. if (x > 63) { /* cccbbbnn BBBBBBBB */
  164. cnt = (x >> 5) - 1;
  165. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  166. } else if (x > 31) { /* 001ccccc (cccccccc...) bbbbbbnn BBBBBBBB */
  167. cnt = get_len(&c, x, 31);
  168. x = GETB(c);
  169. back = (GETB(c) << 6) + (x >> 2) + 1;
  170. } else { /* 0001bccc (cccccccc...) bbbbbbnn BBBBBBBB */
  171. cnt = get_len(&c, x, 7);
  172. back = (1 << 14) + ((x & 8) << 11);
  173. x = GETB(c);
  174. back += (GETB(c) << 6) + (x >> 2);
  175. if (back == (1 << 14)) {
  176. if (cnt != 1)
  177. c.error |= AV_LZO_ERROR;
  178. break;
  179. }
  180. }
  181. } else if (!state) { /* 0000llll (llllllll...) { literal... } ( 0000bbnn BBBBBBBB ) */
  182. cnt = get_len(&c, x, 15);
  183. copy(&c, cnt + 3);
  184. x = GETB(c);
  185. if (x > 15)
  186. continue;
  187. cnt = 1;
  188. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  189. } else { /* 0000bbnn BBBBBBBB ) */
  190. cnt = 0;
  191. back = (GETB(c) << 2) + (x >> 2) + 1;
  192. }
  193. copy_backptr(&c, back, cnt + 2);
  194. state =
  195. cnt = x & 3;
  196. copy(&c, cnt);
  197. x = GETB(c);
  198. }
  199. *inlen = c.in_end - c.in;
  200. if (c.in > c.in_end)
  201. *inlen = 0;
  202. *outlen = c.out_end - c.out;
  203. return c.error;
  204. }