FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lzf.c
Date: 2026-04-24 05:04:01
Exec Total Coverage
Lines: 25 42 59.5%
Functions: 1 2 50.0%
Branches: 10 20 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 "lzf.h"
36
37 #define LZF_LITERAL_MAX (1 << 5)
38 #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 {
43 void *ptr = av_fast_realloc(*buf, allocated_size, new_size);
44
45 if (!ptr) {
46 av_freep(buf); //probably not needed
47 return AVERROR(ENOMEM);
48 }
49 *buf = ptr;
50
51 return 0;
52 }
53
54 4 int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, size_t *size, unsigned *allocated_size)
55 {
56 4 int ret = 0;
57 4 uint8_t *p = *buf;
58 4 int64_t len = 0;
59
60
2/2
✓ Branch 1 taken 85156 times.
✓ Branch 2 taken 4 times.
85160 while (bytestream2_get_bytes_left(gb) > 2) {
61 85156 uint8_t s = bytestream2_get_byte(gb);
62
63
2/2
✓ Branch 0 taken 18284 times.
✓ Branch 1 taken 66872 times.
85156 if (s < LZF_LITERAL_MAX) {
64 18284 s++;
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18284 times.
18284 if (s > *allocated_size - len) {
66 ret = lzf_realloc(buf, len + s, allocated_size);
67 if (ret < 0)
68 return ret;
69 p = *buf + len;
70 }
71
72 18284 int s2 = bytestream2_get_buffer(gb, p, s);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18284 times.
18284 if (s2 != s)
74 return AVERROR_INVALIDDATA;
75
76 18284 p += s;
77 18284 len += s;
78 } else {
79 66872 int l = 2 + (s >> 5);
80 66872 int off = ((s & 0x1f) << 8) + 1;
81
82
2/2
✓ Branch 0 taken 39906 times.
✓ Branch 1 taken 26966 times.
66872 if (l == LZF_LONG_BACKREF)
83 39906 l += bytestream2_get_byte(gb);
84
85 66872 off += bytestream2_get_byte(gb);
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66872 times.
66872 if (off > len)
88 return AVERROR_INVALIDDATA;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66872 times.
66872 if (l > *allocated_size - len) {
91 ret = lzf_realloc(buf, len + l, allocated_size);
92 if (ret < 0)
93 return ret;
94 p = *buf + len;
95 }
96
97 66872 av_memcpy_backptr(p, off, l);
98
99 66872 p += l;
100 66872 len += l;
101 }
102 }
103
104 4 *size = len;
105
106 4 return 0;
107 }
108