FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/dict.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 144 157 91.7%
Functions: 11 11 100.0%
Branches: 98 124 79.0%

Line Branch Exec Source
1 /*
2 * copyright (c) 2009 Michael Niedermayer
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "avassert.h"
26 #include "avstring.h"
27 #include "dict.h"
28 #include "dict_internal.h"
29 #include "error.h"
30 #include "mem.h"
31 #include "time_internal.h"
32 #include "bprint.h"
33
34 struct AVDictionary {
35 int count;
36 AVDictionaryEntry *elems;
37 };
38
39 27641 int av_dict_count(const AVDictionary *m)
40 {
41
2/2
✓ Branch 0 taken 18879 times.
✓ Branch 1 taken 8762 times.
27641 return m ? m->count : 0;
42 }
43
44 4671818 const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m,
45 const AVDictionaryEntry *prev)
46 {
47 4671818 int i = 0;
48
49
2/2
✓ Branch 0 taken 2397550 times.
✓ Branch 1 taken 2274268 times.
4671818 if (!m)
50 2397550 return NULL;
51
52
2/2
✓ Branch 0 taken 1452822 times.
✓ Branch 1 taken 821446 times.
2274268 if (prev)
53 1452822 i = prev - m->elems + 1;
54
55 av_assert2(i >= 0);
56
2/2
✓ Branch 0 taken 607865 times.
✓ Branch 1 taken 1666403 times.
2274268 if (i >= m->count)
57 607865 return NULL;
58
59 1666403 return &m->elems[i];
60 }
61
62 1482590 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
63 const AVDictionaryEntry *prev, int flags)
64 {
65 1482590 const AVDictionaryEntry *entry = prev;
66 unsigned int j;
67
68
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1482589 times.
1482590 if (!key)
69 1 return NULL;
70
71
2/2
✓ Branch 1 taken 1188496 times.
✓ Branch 2 taken 1268850 times.
2457346 while ((entry = av_dict_iterate(m, entry))) {
72 1188496 const char *s = entry->key;
73
2/2
✓ Branch 0 taken 41241 times.
✓ Branch 1 taken 1147255 times.
1188496 if (flags & AV_DICT_MATCH_CASE)
74
4/4
✓ Branch 0 taken 126395 times.
✓ Branch 1 taken 33509 times.
✓ Branch 2 taken 118663 times.
✓ Branch 3 taken 7732 times.
159904 for (j = 0; s[j] == key[j] && key[j]; j++)
75 ;
76 else
77
4/4
✓ Branch 0 taken 4609675 times.
✓ Branch 1 taken 941404 times.
✓ Branch 2 taken 4403824 times.
✓ Branch 3 taken 205851 times.
5551079 for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
78 ;
79
2/2
✓ Branch 0 taken 970191 times.
✓ Branch 1 taken 218305 times.
1188496 if (key[j])
80 970191 continue;
81
4/4
✓ Branch 0 taken 4722 times.
✓ Branch 1 taken 213583 times.
✓ Branch 2 taken 4566 times.
✓ Branch 3 taken 156 times.
218305 if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
82 4566 continue;
83 213739 return (AVDictionaryEntry *)entry;
84 }
85 1268850 return NULL;
86 }
87
88 1166423 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
89 int flags)
90 {
91 1166423 AVDictionary *m = *pm;
92 1166423 AVDictionaryEntry *tag = NULL;
93 1166423 char *copy_key = NULL, *copy_value = NULL;
94 int err;
95
96
2/2
✓ Branch 0 taken 8519 times.
✓ Branch 1 taken 1157904 times.
1166423 if (flags & AV_DICT_DONT_STRDUP_VAL)
97 8519 copy_value = (void *)value;
98
2/2
✓ Branch 0 taken 520097 times.
✓ Branch 1 taken 637807 times.
1157904 else if (value)
99 520097 copy_value = av_strdup(value);
100
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1166419 times.
1166423 if (!key) {
101 4 err = AVERROR(EINVAL);
102 4 goto err_out;
103 }
104
2/2
✓ Branch 0 taken 1019358 times.
✓ Branch 1 taken 147061 times.
1166419 if (!(flags & AV_DICT_MULTIKEY)) {
105 1019358 tag = av_dict_get(m, key, NULL, flags);
106 }
107
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 1165953 times.
1166419 if (flags & AV_DICT_DONT_STRDUP_KEY)
108 466 copy_key = (void *)key;
109 else
110 1165953 copy_key = av_strdup(key);
111
2/2
✓ Branch 0 taken 623288 times.
✓ Branch 1 taken 543131 times.
1166419 if (!m)
112 623288 m = *pm = av_mallocz(sizeof(*m));
113
5/8
✓ Branch 0 taken 1166419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1166419 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 528613 times.
✓ Branch 5 taken 637806 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 528613 times.
1166419 if (!m || !copy_key || (value && !copy_value))
114 goto enomem;
115
116
2/2
✓ Branch 0 taken 169521 times.
✓ Branch 1 taken 996898 times.
1166419 if (tag) {
117
2/2
✓ Branch 0 taken 501 times.
✓ Branch 1 taken 169020 times.
169521 if (flags & AV_DICT_DONT_OVERWRITE) {
118 501 av_free(copy_key);
119 501 av_free(copy_value);
120 501 return 0;
121 }
122
4/4
✓ Branch 0 taken 25891 times.
✓ Branch 1 taken 143129 times.
✓ Branch 2 taken 11236 times.
✓ Branch 3 taken 14655 times.
169020 if (copy_value && flags & AV_DICT_APPEND) {
123 11236 size_t oldlen = strlen(tag->value);
124 11236 size_t new_part_len = strlen(copy_value);
125 11236 size_t len = oldlen + new_part_len + 1;
126 11236 char *newval = av_realloc(tag->value, len);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11236 times.
11236 if (!newval)
128 goto enomem;
129 11236 memcpy(newval + oldlen, copy_value, new_part_len + 1);
130 11236 av_freep(&copy_value);
131 11236 copy_value = newval;
132 } else
133 157784 av_free(tag->value);
134 169020 av_free(tag->key);
135 169020 *tag = m->elems[--m->count];
136
2/2
✓ Branch 0 taken 502221 times.
✓ Branch 1 taken 494677 times.
996898 } else if (copy_value) {
137 502221 AVDictionaryEntry *tmp = av_realloc_array(m->elems,
138 502221 m->count + 1, sizeof(*m->elems));
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 502221 times.
502221 if (!tmp)
140 goto enomem;
141 502221 m->elems = tmp;
142 }
143
2/2
✓ Branch 0 taken 528112 times.
✓ Branch 1 taken 637806 times.
1165918 if (copy_value) {
144 528112 m->elems[m->count].key = copy_key;
145 528112 m->elems[m->count].value = copy_value;
146 528112 m->count++;
147 } else {
148
2/2
✓ Branch 0 taken 468021 times.
✓ Branch 1 taken 169785 times.
637806 if (!m->count) {
149 468021 av_freep(&m->elems);
150 468021 av_freep(pm);
151 }
152 637806 av_freep(&copy_key);
153 }
154
155 1165918 return 0;
156
157 enomem:
158 err = AVERROR(ENOMEM);
159 4 err_out:
160
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (m && !m->count) {
161 av_freep(&m->elems);
162 av_freep(pm);
163 }
164 4 av_free(copy_key);
165 4 av_free(copy_value);
166 4 return err;
167 }
168
169 1069 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
170 int flags)
171 {
172 char valuestr[22];
173 1069 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
174 1069 flags &= ~AV_DICT_DONT_STRDUP_VAL;
175 1069 return av_dict_set(pm, key, valuestr, flags);
176 }
177
178 213 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
179 const char *key_val_sep, const char *pairs_sep,
180 int flags)
181 {
182 213 char *key = av_get_token(buf, key_val_sep);
183 213 char *val = NULL;
184 int ret;
185
186
3/6
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
✗ Branch 5 not taken.
213 if (key && *key && strspn(*buf, key_val_sep)) {
187 213 (*buf)++;
188 213 val = av_get_token(buf, pairs_sep);
189 }
190
191
4/8
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 213 times.
✗ Branch 7 not taken.
213 if (key && *key && val && *val)
192 213 ret = av_dict_set(pm, key, val, flags);
193 else
194 ret = AVERROR(EINVAL);
195
196 213 av_freep(&key);
197 213 av_freep(&val);
198
199 213 return ret;
200 }
201
202 107 int av_dict_parse_string(AVDictionary **pm, const char *str,
203 const char *key_val_sep, const char *pairs_sep,
204 int flags)
205 {
206 int ret;
207
208
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 105 times.
107 if (!str)
209 2 return 0;
210
211 /* ignore STRDUP flags */
212 105 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
213
214
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 105 times.
423 while (*str) {
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
213 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
216 return ret;
217
218
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 110 times.
213 if (*str)
219 110 str++;
220 }
221
222 105 return 0;
223 }
224
225 8953081 void av_dict_free(AVDictionary **pm)
226 {
227 8953081 AVDictionary *m = *pm;
228
229
2/2
✓ Branch 0 taken 155267 times.
✓ Branch 1 taken 8797814 times.
8953081 if (m) {
230
2/2
✓ Branch 0 taken 359092 times.
✓ Branch 1 taken 155267 times.
514359 while (m->count--) {
231 359092 av_freep(&m->elems[m->count].key);
232 359092 av_freep(&m->elems[m->count].value);
233 }
234 155267 av_freep(&m->elems);
235 }
236 8953081 av_freep(pm);
237 8953081 }
238
239 1403880 int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
240 {
241 1403880 const AVDictionaryEntry *t = NULL;
242
243
2/2
✓ Branch 1 taken 47944 times.
✓ Branch 2 taken 1403880 times.
1451824 while ((t = av_dict_iterate(src, t))) {
244 47944 int ret = av_dict_set(dst, t->key, t->value, flags);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47944 times.
47944 if (ret < 0)
246 return ret;
247 }
248
249 1403880 return 0;
250 }
251
252 3976 int av_dict_get_string(const AVDictionary *m, char **buffer,
253 const char key_val_sep, const char pairs_sep)
254 {
255 3976 const AVDictionaryEntry *t = NULL;
256 AVBPrint bprint;
257 3976 int cnt = 0;
258 3976 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
259
260
4/8
✓ Branch 0 taken 3976 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3976 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3976 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3976 times.
✗ Branch 7 not taken.
3976 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
261
2/4
✓ Branch 0 taken 3976 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3976 times.
3976 pairs_sep == '\\' || key_val_sep == '\\')
262 return AVERROR(EINVAL);
263
264
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3971 times.
3976 if (!av_dict_count(m)) {
265 5 *buffer = av_strdup("");
266
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return *buffer ? 0 : AVERROR(ENOMEM);
267 }
268
269 3971 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
270
2/2
✓ Branch 1 taken 3995 times.
✓ Branch 2 taken 3971 times.
7966 while ((t = av_dict_iterate(m, t))) {
271
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3971 times.
3995 if (cnt++)
272 24 av_bprint_append_data(&bprint, &pairs_sep, 1);
273 3995 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
274 3995 av_bprint_append_data(&bprint, &key_val_sep, 1);
275 3995 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
276 }
277 3971 return av_bprint_finalize(&bprint, buffer);
278 }
279
280 551 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
281 {
282 551 time_t seconds = timestamp / 1000000;
283 struct tm *ptm, tmbuf;
284 551 ptm = gmtime_r(&seconds, &tmbuf);
285
1/2
✓ Branch 0 taken 551 times.
✗ Branch 1 not taken.
551 if (ptm) {
286 char buf[32];
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
551 if (!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", ptm))
288 return AVERROR_EXTERNAL;
289 551 av_strlcatf(buf, sizeof(buf), ".%06dZ", (int)(timestamp % 1000000));
290 551 return av_dict_set(dict, key, buf, 0);
291 } else {
292 return AVERROR_EXTERNAL;
293 }
294 }
295