]> git.sesse.net Git - ffmpeg/blob - libavutil/lzo.c
lzo: Drop obsolete fast_memcpy reference
[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 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 /**
39  * @brief Reads one byte from the input buffer, avoiding an overrun.
40  * @return byte read
41  */
42 static inline int get_byte(LZOContext *c) {
43     if (c->in < c->in_end)
44         return *c->in++;
45     c->error |= AV_LZO_INPUT_DEPLETED;
46     return 1;
47 }
48
49 #ifdef INBUF_PADDED
50 #define GETB(c) (*(c).in++)
51 #else
52 #define GETB(c) get_byte(&(c))
53 #endif
54
55 /**
56  * @brief Decodes a length value in the coding used by lzo.
57  * @param x previous byte value
58  * @param mask bits used from x
59  * @return decoded length value
60  */
61 static inline int get_len(LZOContext *c, int x, int mask) {
62     int cnt = x & mask;
63     if (!cnt) {
64         while (!(x = get_byte(c))) cnt += 255;
65         cnt += mask + x;
66     }
67     return cnt;
68 }
69
70 //#define UNALIGNED_LOADSTORE
71 #define BUILTIN_MEMCPY
72 #ifdef UNALIGNED_LOADSTORE
73 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
74 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
75 #elif defined(BUILTIN_MEMCPY)
76 #define COPY2(d, s) memcpy(d, s, 2);
77 #define COPY4(d, s) memcpy(d, s, 4);
78 #else
79 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
80 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
81 #endif
82
83 /**
84  * @brief Copies bytes from input to output buffer with checking.
85  * @param cnt number of bytes to copy, must be >= 0
86  */
87 static inline void copy(LZOContext *c, int cnt) {
88     register const uint8_t *src = c->in;
89     register uint8_t *dst = c->out;
90     if (cnt > c->in_end - src) {
91         cnt = FFMAX(c->in_end - src, 0);
92         c->error |= AV_LZO_INPUT_DEPLETED;
93     }
94     if (cnt > c->out_end - dst) {
95         cnt = FFMAX(c->out_end - dst, 0);
96         c->error |= AV_LZO_OUTPUT_FULL;
97     }
98 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
99     COPY4(dst, src);
100     src += 4;
101     dst += 4;
102     cnt -= 4;
103     if (cnt > 0)
104 #endif
105         memcpy(dst, src, cnt);
106     c->in = src + cnt;
107     c->out = dst + cnt;
108 }
109
110 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
111
112 /**
113  * @brief Copies previously decoded bytes to current position.
114  * @param back how many bytes back we start
115  * @param cnt number of bytes to copy, must be >= 0
116  *
117  * cnt > back is valid, this will copy the bytes we just copied,
118  * thus creating a repeating pattern with a period length of back.
119  */
120 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
121     register const uint8_t *src = &c->out[-back];
122     register uint8_t *dst = c->out;
123     if (src < c->out_start || src > dst) {
124         c->error |= AV_LZO_INVALID_BACKPTR;
125         return;
126     }
127     if (cnt > c->out_end - dst) {
128         cnt = FFMAX(c->out_end - dst, 0);
129         c->error |= AV_LZO_OUTPUT_FULL;
130     }
131     memcpy_backptr(dst, back, cnt);
132     c->out = dst + cnt;
133 }
134
135 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
136     const uint8_t *src = &dst[-back];
137     if (back == 1) {
138         memset(dst, *src, cnt);
139     } else {
140 #ifdef OUTBUF_PADDED
141         COPY2(dst, src);
142         COPY2(dst + 2, src + 2);
143         src += 4;
144         dst += 4;
145         cnt -= 4;
146         if (cnt > 0) {
147             COPY2(dst, src);
148             COPY2(dst + 2, src + 2);
149             COPY2(dst + 4, src + 4);
150             COPY2(dst + 6, src + 6);
151             src += 8;
152             dst += 8;
153             cnt -= 8;
154         }
155 #endif
156         if (cnt > 0) {
157             int blocklen = back;
158             while (cnt > blocklen) {
159                 memcpy(dst, src, blocklen);
160                 dst += blocklen;
161                 cnt -= blocklen;
162                 blocklen <<= 1;
163             }
164             memcpy(dst, src, cnt);
165         }
166     }
167 }
168
169 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
170     memcpy_backptr(dst, back, cnt);
171 }
172
173 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
174     int state= 0;
175     int x;
176     LZOContext c;
177     if (!*outlen || !*inlen) {
178         int res = 0;
179         if (!*outlen)
180             res |= AV_LZO_OUTPUT_FULL;
181         if (!*inlen)
182             res |= AV_LZO_INPUT_DEPLETED;
183         return res;
184     }
185     c.in = in;
186     c.in_end = (const uint8_t *)in + *inlen;
187     c.out = c.out_start = out;
188     c.out_end = (uint8_t *)out + * outlen;
189     c.error = 0;
190     x = GETB(c);
191     if (x > 17) {
192         copy(&c, x - 17);
193         x = GETB(c);
194         if (x < 16) c.error |= AV_LZO_ERROR;
195     }
196     if (c.in > c.in_end)
197         c.error |= AV_LZO_INPUT_DEPLETED;
198     while (!c.error) {
199         int cnt, back;
200         if (x > 15) {
201             if (x > 63) {
202                 cnt = (x >> 5) - 1;
203                 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
204             } else if (x > 31) {
205                 cnt = get_len(&c, x, 31);
206                 x = GETB(c);
207                 back = (GETB(c) << 6) + (x >> 2) + 1;
208             } else {
209                 cnt = get_len(&c, x, 7);
210                 back = (1 << 14) + ((x & 8) << 11);
211                 x = GETB(c);
212                 back += (GETB(c) << 6) + (x >> 2);
213                 if (back == (1 << 14)) {
214                     if (cnt != 1)
215                         c.error |= AV_LZO_ERROR;
216                     break;
217                 }
218             }
219         } else if(!state){
220                 cnt = get_len(&c, x, 15);
221                 copy(&c, cnt + 3);
222                 x = GETB(c);
223                 if (x > 15)
224                     continue;
225                 cnt = 1;
226                 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
227         } else {
228                 cnt = 0;
229                 back = (GETB(c) << 2) + (x >> 2) + 1;
230         }
231         copy_backptr(&c, back, cnt + 2);
232         state=
233         cnt = x & 3;
234         copy(&c, cnt);
235         x = GETB(c);
236     }
237     *inlen = c.in_end - c.in;
238     if (c.in > c.in_end)
239         *inlen = 0;
240     *outlen = c.out_end - c.out;
241     return c.error;
242 }