/src/ffmpeg/libavcodec/lzf.c
Line | Count | Source |
1 | | /* |
2 | | * lzf decompression algorithm |
3 | | * Copyright (c) 2015 Luca Barbato |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
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. |
11 | | * |
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. |
16 | | * |
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 |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * lzf decompression |
25 | | * |
26 | | * LZF is a fast compression/decompression algorithm that takes very little |
27 | | * code space and working memory, ideal for real-time and block compression. |
28 | | * |
29 | | * https://en.wikibooks.org/wiki/Data_Compression/Dictionary_compression#LZF |
30 | | */ |
31 | | |
32 | | #include "libavutil/mem.h" |
33 | | |
34 | | #include "bytestream.h" |
35 | | #include "lzf.h" |
36 | | |
37 | 6.37M | #define LZF_LITERAL_MAX (1 << 5) |
38 | 5.88M | #define LZF_LONG_BACKREF 7 + 2 |
39 | | |
40 | | |
41 | | static inline int lzf_realloc(uint8_t **buf, size_t new_size, unsigned *allocated_size) |
42 | 18.0k | { |
43 | 18.0k | void *ptr = av_fast_realloc(*buf, allocated_size, new_size); |
44 | | |
45 | 18.0k | if (!ptr) { |
46 | 0 | av_freep(buf); //probably not needed |
47 | 0 | return AVERROR(ENOMEM); |
48 | 0 | } |
49 | 18.0k | *buf = ptr; |
50 | | |
51 | 18.0k | return 0; |
52 | 18.0k | } |
53 | | |
54 | | int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, size_t *size, unsigned *allocated_size) |
55 | 72.4k | { |
56 | 72.4k | int ret = 0; |
57 | 72.4k | uint8_t *p = *buf; |
58 | 72.4k | int64_t len = 0; |
59 | | |
60 | 6.44M | while (bytestream2_get_bytes_left(gb) > 2) { |
61 | 6.37M | uint8_t s = bytestream2_get_byte(gb); |
62 | | |
63 | 6.37M | if (s < LZF_LITERAL_MAX) { |
64 | 486k | s++; |
65 | 486k | if (s > *allocated_size - len) { |
66 | 886 | ret = lzf_realloc(buf, len + s, allocated_size); |
67 | 886 | if (ret < 0) |
68 | 0 | return ret; |
69 | 886 | p = *buf + len; |
70 | 886 | } |
71 | | |
72 | 486k | int s2 = bytestream2_get_buffer(gb, p, s); |
73 | 486k | if (s2 != s) |
74 | 811 | return AVERROR_INVALIDDATA; |
75 | | |
76 | 485k | p += s; |
77 | 485k | len += s; |
78 | 5.88M | } else { |
79 | 5.88M | int l = 2 + (s >> 5); |
80 | 5.88M | int off = ((s & 0x1f) << 8) + 1; |
81 | | |
82 | 5.88M | if (l == LZF_LONG_BACKREF) |
83 | 5.76M | l += bytestream2_get_byte(gb); |
84 | | |
85 | 5.88M | off += bytestream2_get_byte(gb); |
86 | | |
87 | 5.88M | if (off > len) |
88 | 1.15k | return AVERROR_INVALIDDATA; |
89 | | |
90 | 5.88M | if (l > *allocated_size - len) { |
91 | 17.1k | ret = lzf_realloc(buf, len + l, allocated_size); |
92 | 17.1k | if (ret < 0) |
93 | 0 | return ret; |
94 | 17.1k | p = *buf + len; |
95 | 17.1k | } |
96 | | |
97 | 5.88M | av_memcpy_backptr(p, off, l); |
98 | | |
99 | 5.88M | p += l; |
100 | 5.88M | len += l; |
101 | 5.88M | } |
102 | 6.37M | } |
103 | | |
104 | 70.4k | *size = len; |
105 | | |
106 | 70.4k | return 0; |
107 | 72.4k | } |