]> git.sesse.net Git - ffmpeg/blob - libavutil/lzo.c
avutil/lzo: K&R formatting cosmetics
[ffmpeg] / libavutil / lzo.c
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
22 #include <string.h>
23
24 #include "avutil.h"
25 #include "common.h"
26 #include "lzo.h"
27
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
33 typedef struct LZOContext {
34     const uint8_t *in, *in_end;
35     uint8_t *out_start, *out, *out_end;
36     int error;
37 } LZOContext;
38
39 /**
40  * @brief Reads one byte from the input buffer, avoiding an overrun.
41  * @return byte read
42  */
43 static inline int get_byte(LZOContext *c)
44 {
45     if (c->in < c->in_end)
46         return *c->in++;
47     c->error |= AV_LZO_INPUT_DEPLETED;
48     return 1;
49 }
50
51 #ifdef INBUF_PADDED
52 #define GETB(c) (*(c).in++)
53 #else
54 #define GETB(c) get_byte(&(c))
55 #endif
56
57 /**
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
62  */
63 static inline int get_len(LZOContext *c, int x, int mask)
64 {
65     int cnt = x & mask;
66     if (!cnt) {
67         while (!(x = get_byte(c)))
68             cnt += 255;
69         cnt += mask + x;
70     }
71     return cnt;
72 }
73
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);
82 #else
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];
85 #endif
86
87 /**
88  * @brief Copies bytes from input to output buffer with checking.
89  * @param cnt number of bytes to copy, must be >= 0
90  */
91 static inline void copy(LZOContext *c, int cnt)
92 {
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;
98     }
99     if (cnt > c->out_end - dst) {
100         cnt       = FFMAX(c->out_end - dst, 0);
101         c->error |= AV_LZO_OUTPUT_FULL;
102     }
103 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
104     COPY4(dst, src);
105     src += 4;
106     dst += 4;
107     cnt -= 4;
108     if (cnt > 0)
109 #endif
110     memcpy(dst, src, cnt);
111     c->in  = src + cnt;
112     c->out = dst + cnt;
113 }
114
115 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
116
117 /**
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
121  *
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.
124  */
125 static inline void copy_backptr(LZOContext *c, int back, int cnt)
126 {
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;
131         return;
132     }
133     if (cnt > c->out_end - dst) {
134         cnt       = FFMAX(c->out_end - dst, 0);
135         c->error |= AV_LZO_OUTPUT_FULL;
136     }
137     memcpy_backptr(dst, back, cnt);
138     c->out = dst + cnt;
139 }
140
141 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt)
142 {
143     const uint8_t *src = &dst[-back];
144     if (back == 1) {
145         memset(dst, *src, cnt);
146     } else {
147 #ifdef OUTBUF_PADDED
148         COPY2(dst, src);
149         COPY2(dst + 2, src + 2);
150         src += 4;
151         dst += 4;
152         cnt -= 4;
153         if (cnt > 0) {
154             COPY2(dst, src);
155             COPY2(dst + 2, src + 2);
156             COPY2(dst + 4, src + 4);
157             COPY2(dst + 6, src + 6);
158             src += 8;
159             dst += 8;
160             cnt -= 8;
161         }
162 #endif
163         if (cnt > 0) {
164             int blocklen = back;
165             while (cnt > blocklen) {
166                 memcpy(dst, src, blocklen);
167                 dst       += blocklen;
168                 cnt       -= blocklen;
169                 blocklen <<= 1;
170             }
171             memcpy(dst, src, cnt);
172         }
173     }
174 }
175
176 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
177 {
178     memcpy_backptr(dst, back, cnt);
179 }
180
181 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
182 {
183     int state = 0;
184     int x;
185     LZOContext c;
186     if (!*outlen || !*inlen) {
187         int res = 0;
188         if (!*outlen)
189             res |= AV_LZO_OUTPUT_FULL;
190         if (!*inlen)
191             res |= AV_LZO_INPUT_DEPLETED;
192         return res;
193     }
194     c.in      = in;
195     c.in_end  = (const uint8_t *)in + *inlen;
196     c.out     = c.out_start = out;
197     c.out_end = (uint8_t *)out + *outlen;
198     c.error   = 0;
199     x         = GETB(c);
200     if (x > 17) {
201         copy(&c, x - 17);
202         x = GETB(c);
203         if (x < 16)
204             c.error |= AV_LZO_ERROR;
205     }
206     if (c.in > c.in_end)
207         c.error |= AV_LZO_INPUT_DEPLETED;
208     while (!c.error) {
209         int cnt, back;
210         if (x > 15) {
211             if (x > 63) {
212                 cnt  = (x >> 5) - 1;
213                 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
214             } else if (x > 31) {
215                 cnt  = get_len(&c, x, 31);
216                 x    = GETB(c);
217                 back = (GETB(c) << 6) + (x >> 2) + 1;
218             } else {
219                 cnt   = get_len(&c, x, 7);
220                 back  = (1 << 14) + ((x & 8) << 11);
221                 x     = GETB(c);
222                 back += (GETB(c) << 6) + (x >> 2);
223                 if (back == (1 << 14)) {
224                     if (cnt != 1)
225                         c.error |= AV_LZO_ERROR;
226                     break;
227                 }
228             }
229         } else if (!state) {
230             cnt = get_len(&c, x, 15);
231             copy(&c, cnt + 3);
232             x = GETB(c);
233             if (x > 15)
234                 continue;
235             cnt  = 1;
236             back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
237         } else {
238             cnt  = 0;
239             back = (GETB(c) << 2) + (x >> 2) + 1;
240         }
241         copy_backptr(&c, back, cnt + 2);
242         state =
243         cnt   = x & 3;
244         copy(&c, cnt);
245         x = GETB(c);
246     }
247     *inlen = c.in_end - c.in;
248     if (c.in > c.in_end)
249         *inlen = 0;
250     *outlen = c.out_end - c.out;
251     return c.error;
252 }