FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lzf.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 37 43 86.0%
Functions: 2 2 100.0%
Branches: 15 20 75.0%

Line Branch Exec 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 #define LZF_LITERAL_MAX (1 << 5)
38 #define LZF_LONG_BACKREF 7 + 2
39
40
41 194 static inline int lzf_realloc(uint8_t **buf, size_t *size, int addition, unsigned *allocated_size)
42 {
43 194 void *ptr = av_fast_realloc(*buf, allocated_size, *size + addition);
44
45
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (!ptr) {
46 av_freep(buf); //probably not needed
47 return AVERROR(ENOMEM);
48 }
49 194 *buf = ptr;
50 194 *size += addition;
51
52 194 return 0;
53 }
54
55 4 int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, size_t *size, unsigned *allocated_size)
56 {
57 4 int ret = 0;
58 4 uint8_t *p = *buf;
59 4 int64_t len = 0;
60
61
2/2
✓ Branch 1 taken 85156 times.
✓ Branch 2 taken 4 times.
85160 while (bytestream2_get_bytes_left(gb) > 2) {
62 85156 uint8_t s = bytestream2_get_byte(gb);
63
64
2/2
✓ Branch 0 taken 18284 times.
✓ Branch 1 taken 66872 times.
85156 if (s < LZF_LITERAL_MAX) {
65 18284 s++;
66
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18280 times.
18284 if (s > *size - len) {
67 4 ret = lzf_realloc(buf, size, s, allocated_size);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
69 return ret;
70 4 p = *buf + len;
71 }
72
73 18284 int s2 = bytestream2_get_buffer(gb, p, s);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18284 times.
18284 if (s2 != s)
75 return AVERROR_INVALIDDATA;
76
77 18284 p += s;
78 18284 len += s;
79 } else {
80 66872 int l = 2 + (s >> 5);
81 66872 int off = ((s & 0x1f) << 8) + 1;
82
83
2/2
✓ Branch 0 taken 39906 times.
✓ Branch 1 taken 26966 times.
66872 if (l == LZF_LONG_BACKREF)
84 39906 l += bytestream2_get_byte(gb);
85
86 66872 off += bytestream2_get_byte(gb);
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66872 times.
66872 if (off > len)
89 return AVERROR_INVALIDDATA;
90
91
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 66682 times.
66872 if (l > *size - len) {
92 190 ret = lzf_realloc(buf, size, l, allocated_size);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 if (ret < 0)
94 return ret;
95 190 p = *buf + len;
96 }
97
98 66872 av_memcpy_backptr(p, off, l);
99
100 66872 p += l;
101 66872 len += l;
102 }
103 }
104
105 4 *size = len;
106
107 4 return 0;
108 }
109