FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lzf.c
Date: 2026-08-26 01:27:24
Exec Total Coverage
Lines: 27 44 61.4%
Functions: 1 2 50.0%
Branches: 11 22 50.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 "defs.h"
36 #include "lzf.h"
37
38 #define LZF_LITERAL_MAX (1 << 5)
39 #define LZF_LONG_BACKREF 7 + 2
40
41
42 static inline int lzf_realloc(uint8_t **buf, size_t new_size, unsigned *allocated_size)
43 {
44 void *ptr = av_fast_realloc(*buf, allocated_size, new_size);
45
46 if (!ptr) {
47 av_freep(buf); //probably not needed
48 return AVERROR(ENOMEM);
49 }
50 *buf = ptr;
51
52 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18284 times.
18284 if (s + AV_INPUT_BUFFER_PADDING_SIZE > *allocated_size - len) {
67 ret = lzf_realloc(buf, len + s + AV_INPUT_BUFFER_PADDING_SIZE, allocated_size);
68 if (ret < 0)
69 return ret;
70 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66872 times.
66872 if (l + AV_INPUT_BUFFER_PADDING_SIZE > *allocated_size - len) {
92 ret = lzf_realloc(buf, len + l + AV_INPUT_BUFFER_PADDING_SIZE, allocated_size);
93 if (ret < 0)
94 return ret;
95 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
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (*buf)
106 4 memset(*buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
107
108 4 *size = len;
109
110 4 return 0;
111 }
112