You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.4KB

  1. /*
  2. * LZO 1x decompression
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "avutil.h"
  23. #include "common.h"
  24. #include "intreadwrite.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 Reads one byte from the input buffer, avoiding an overrun.
  37. * @return byte read
  38. */
  39. static inline int get_byte(LZOContext *c)
  40. {
  41. if (c->in < c->in_end)
  42. return *c->in++;
  43. c->error |= AV_LZO_INPUT_DEPLETED;
  44. return 1;
  45. }
  46. #ifdef INBUF_PADDED
  47. #define GETB(c) (*(c).in++)
  48. #else
  49. #define GETB(c) get_byte(&(c))
  50. #endif
  51. /**
  52. * @brief Decodes a length value in the coding used by lzo.
  53. * @param x previous byte value
  54. * @param mask bits used from x
  55. * @return decoded length value
  56. */
  57. static inline int get_len(LZOContext *c, int x, int mask)
  58. {
  59. int cnt = x & mask;
  60. if (!cnt) {
  61. while (!(x = get_byte(c)))
  62. cnt += 255;
  63. cnt += mask + x;
  64. }
  65. return cnt;
  66. }
  67. /**
  68. * @brief Copies bytes from input to output buffer with checking.
  69. * @param cnt number of bytes to copy, must be >= 0
  70. */
  71. static inline void copy(LZOContext *c, int cnt)
  72. {
  73. register const uint8_t *src = c->in;
  74. register uint8_t *dst = c->out;
  75. if (cnt > c->in_end - src) {
  76. cnt = FFMAX(c->in_end - src, 0);
  77. c->error |= AV_LZO_INPUT_DEPLETED;
  78. }
  79. if (cnt > c->out_end - dst) {
  80. cnt = FFMAX(c->out_end - dst, 0);
  81. c->error |= AV_LZO_OUTPUT_FULL;
  82. }
  83. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  84. AV_COPY32U(dst, src);
  85. src += 4;
  86. dst += 4;
  87. cnt -= 4;
  88. if (cnt > 0)
  89. #endif
  90. memcpy(dst, src, cnt);
  91. c->in = src + cnt;
  92. c->out = dst + cnt;
  93. }
  94. /**
  95. * @brief Copies previously decoded bytes to current position.
  96. * @param back how many bytes back we start
  97. * @param cnt number of bytes to copy, must be >= 0
  98. *
  99. * cnt > back is valid, this will copy the bytes we just copied,
  100. * thus creating a repeating pattern with a period length of back.
  101. */
  102. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  103. {
  104. register const uint8_t *src = &c->out[-back];
  105. register uint8_t *dst = c->out;
  106. if (src < c->out_start || src > dst) {
  107. c->error |= AV_LZO_INVALID_BACKPTR;
  108. return;
  109. }
  110. if (cnt > c->out_end - dst) {
  111. cnt = FFMAX(c->out_end - dst, 0);
  112. c->error |= AV_LZO_OUTPUT_FULL;
  113. }
  114. av_memcpy_backptr(dst, back, cnt);
  115. c->out = dst + cnt;
  116. }
  117. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
  118. {
  119. int state = 0;
  120. int x;
  121. LZOContext c;
  122. if (!*outlen || !*inlen) {
  123. int res = 0;
  124. if (!*outlen)
  125. res |= AV_LZO_OUTPUT_FULL;
  126. if (!*inlen)
  127. res |= AV_LZO_INPUT_DEPLETED;
  128. return res;
  129. }
  130. c.in = in;
  131. c.in_end = (const uint8_t *)in + *inlen;
  132. c.out = c.out_start = out;
  133. c.out_end = (uint8_t *)out + *outlen;
  134. c.error = 0;
  135. x = GETB(c);
  136. if (x > 17) {
  137. copy(&c, x - 17);
  138. x = GETB(c);
  139. if (x < 16)
  140. c.error |= AV_LZO_ERROR;
  141. }
  142. if (c.in > c.in_end)
  143. c.error |= AV_LZO_INPUT_DEPLETED;
  144. while (!c.error) {
  145. int cnt, back;
  146. if (x > 15) {
  147. if (x > 63) {
  148. cnt = (x >> 5) - 1;
  149. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  150. } else if (x > 31) {
  151. cnt = get_len(&c, x, 31);
  152. x = GETB(c);
  153. back = (GETB(c) << 6) + (x >> 2) + 1;
  154. } else {
  155. cnt = get_len(&c, x, 7);
  156. back = (1 << 14) + ((x & 8) << 11);
  157. x = GETB(c);
  158. back += (GETB(c) << 6) + (x >> 2);
  159. if (back == (1 << 14)) {
  160. if (cnt != 1)
  161. c.error |= AV_LZO_ERROR;
  162. break;
  163. }
  164. }
  165. } else if (!state) {
  166. cnt = get_len(&c, x, 15);
  167. copy(&c, cnt + 3);
  168. x = GETB(c);
  169. if (x > 15)
  170. continue;
  171. cnt = 1;
  172. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  173. } else {
  174. cnt = 0;
  175. back = (GETB(c) << 2) + (x >> 2) + 1;
  176. }
  177. copy_backptr(&c, back, cnt + 2);
  178. state =
  179. cnt = x & 3;
  180. copy(&c, cnt);
  181. x = GETB(c);
  182. }
  183. *inlen = c.in_end - c.in;
  184. if (c.in > c.in_end)
  185. *inlen = 0;
  186. *outlen = c.out_end - c.out;
  187. return c.error;
  188. }