3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
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.
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.
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
22 //! avoid e.g. MPlayers fast_memcpy, it slows things down here
27 //! define if we may write up to 12 bytes beyond the output buffer
28 #define OUTBUF_PADDED 1
29 //! define if we may read up to 4 bytes beyond the input buffer
30 #define INBUF_PADDED 1
31 typedef struct LZOContext {
33 uint8_t *out_start, *out, *out_end;
38 * \brief read one byte from input buffer, avoiding overrun
41 static inline int get_byte(LZOContext *c) {
42 if (c->in < c->in_end)
44 c->error |= LZO_INPUT_DEPLETED;
49 * \brief decode a length value in the coding used by lzo
50 * \param x previous byte value
51 * \param mask bits used from x
52 * \return decoded length value
54 static inline int get_len(LZOContext *c, int x, int mask) {
57 while (!(x = get_byte(c))) cnt += 255;
64 * \brief copy bytes from input to output buffer with checking
65 * \param cnt number of bytes to copy, must be > 0
67 static inline void copy(LZOContext *c, int cnt) {
68 register uint8_t *src = c->in;
69 register uint8_t *dst = c->out;
70 if (src + cnt > c->in_end) {
71 cnt = c->in_end - src;
72 c->error |= LZO_INPUT_DEPLETED;
74 if (dst + cnt > c->out_end) {
75 cnt = c->out_end - dst;
76 c->error |= LZO_OUTPUT_FULL;
78 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
88 memcpy(dst, src, cnt);
94 * \brief copy previously decoded bytes to current position
95 * \param back how many bytes back we start
96 * \param cnt number of bytes to copy, must be > 0
98 * cnt > back is valid, this will copy the bytes we just copied,
99 * thus creating a repeating pattern with a period length of back.
101 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
102 register uint8_t *src = &c->out[-back];
103 register uint8_t *dst = c->out;
104 if (src < c->out_start) {
105 c->error |= LZO_INVALID_BACKPTR;
108 if (dst + cnt > c->out_end) {
109 cnt = c->out_end - dst;
110 c->error |= LZO_OUTPUT_FULL;
113 memset(dst, *src, cnt);
140 while (cnt > blocklen) {
141 memcpy(dst, src, blocklen);
146 memcpy(dst, src, cnt);
154 * \brief decode LZO 1x compressed data
155 * \param out output buffer
156 * \param outlen size of output buffer, number of bytes left are returned here
157 * \param in input buffer
158 * \param inlen size of input buffer, number of bytes left are returned here
159 * \return 0 on success, otherwise error flags, see lzo.h
161 * make sure all buffers are appropriately padded, in must provide
162 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
164 int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
165 enum {COPY, BACKPTR} state = COPY;
169 c.in_end = (uint8_t *)in + *inlen;
170 c.out = c.out_start = out;
171 c.out_end = (uint8_t *)out + * outlen;
177 if (x < 16) c.error |= LZO_ERROR;
184 back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
186 cnt = get_len(&c, x, 31);
188 back = (get_byte(&c) << 6) + (x >> 2) + 1;
190 cnt = get_len(&c, x, 7);
191 back = (1 << 14) + ((x & 8) << 11);
193 back += (get_byte(&c) << 6) + (x >> 2);
194 if (back == (1 << 14)) {
196 c.error |= LZO_ERROR;
203 cnt = get_len(&c, x, 15);
209 back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
213 back = (get_byte(&c) << 2) + (x >> 2) + 1;
216 copy_backptr(&c, back, cnt + 2);
218 state = cnt ? BACKPTR : COPY;
223 *inlen = c.in_end - c.in;
224 *outlen = c.out_end - c.out;