3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of Libav.
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.
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.
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
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
33 typedef struct LZOContext {
34 const uint8_t *in, *in_end;
35 uint8_t *out_start, *out, *out_end;
40 * @brief Reads one byte from the input buffer, avoiding an overrun.
43 static inline int get_byte(LZOContext *c)
45 if (c->in < c->in_end)
47 c->error |= AV_LZO_INPUT_DEPLETED;
52 #define GETB(c) (*(c).in++)
54 #define GETB(c) get_byte(&(c))
58 * @brief Decodes a length value in the coding used by lzo.
59 * @param x previous byte value
60 * @param mask bits used from x
61 * @return decoded length value
63 static inline int get_len(LZOContext *c, int x, int mask)
67 while (!(x = get_byte(c)))
74 //#define UNALIGNED_LOADSTORE
75 #define BUILTIN_MEMCPY
76 #ifdef UNALIGNED_LOADSTORE
77 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
78 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
79 #elif defined(BUILTIN_MEMCPY)
80 #define COPY2(d, s) memcpy(d, s, 2);
81 #define COPY4(d, s) memcpy(d, s, 4);
83 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
84 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
88 * @brief Copies bytes from input to output buffer with checking.
89 * @param cnt number of bytes to copy, must be >= 0
91 static inline void copy(LZOContext *c, int cnt)
93 register const uint8_t *src = c->in;
94 register uint8_t *dst = c->out;
95 if (cnt > c->in_end - src) {
96 cnt = FFMAX(c->in_end - src, 0);
97 c->error |= AV_LZO_INPUT_DEPLETED;
99 if (cnt > c->out_end - dst) {
100 cnt = FFMAX(c->out_end - dst, 0);
101 c->error |= AV_LZO_OUTPUT_FULL;
103 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
110 memcpy(dst, src, cnt);
115 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
118 * @brief Copies previously decoded bytes to current position.
119 * @param back how many bytes back we start
120 * @param cnt number of bytes to copy, must be >= 0
122 * cnt > back is valid, this will copy the bytes we just copied,
123 * thus creating a repeating pattern with a period length of back.
125 static inline void copy_backptr(LZOContext *c, int back, int cnt)
127 register const uint8_t *src = &c->out[-back];
128 register uint8_t *dst = c->out;
129 if (src < c->out_start || src > dst) {
130 c->error |= AV_LZO_INVALID_BACKPTR;
133 if (cnt > c->out_end - dst) {
134 cnt = FFMAX(c->out_end - dst, 0);
135 c->error |= AV_LZO_OUTPUT_FULL;
137 memcpy_backptr(dst, back, cnt);
141 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt)
143 const uint8_t *src = &dst[-back];
145 memset(dst, *src, cnt);
149 COPY2(dst + 2, src + 2);
155 COPY2(dst + 2, src + 2);
156 COPY2(dst + 4, src + 4);
157 COPY2(dst + 6, src + 6);
165 while (cnt > blocklen) {
166 memcpy(dst, src, blocklen);
171 memcpy(dst, src, cnt);
176 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
178 memcpy_backptr(dst, back, cnt);
181 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
186 if (!*outlen || !*inlen) {
189 res |= AV_LZO_OUTPUT_FULL;
191 res |= AV_LZO_INPUT_DEPLETED;
195 c.in_end = (const uint8_t *)in + *inlen;
196 c.out = c.out_start = out;
197 c.out_end = (uint8_t *)out + *outlen;
204 c.error |= AV_LZO_ERROR;
207 c.error |= AV_LZO_INPUT_DEPLETED;
213 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
215 cnt = get_len(&c, x, 31);
217 back = (GETB(c) << 6) + (x >> 2) + 1;
219 cnt = get_len(&c, x, 7);
220 back = (1 << 14) + ((x & 8) << 11);
222 back += (GETB(c) << 6) + (x >> 2);
223 if (back == (1 << 14)) {
225 c.error |= AV_LZO_ERROR;
230 cnt = get_len(&c, x, 15);
236 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
239 back = (GETB(c) << 2) + (x >> 2) + 1;
241 copy_backptr(&c, back, cnt + 2);
247 *inlen = c.in_end - c.in;
250 *outlen = c.out_end - c.out;