FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/dict.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 145 156 92.9%
Functions: 11 11 100.0%
Branches: 99 122 81.1%

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 27982 int av_dict_count(const AVDictionary *m)
40 {
41
2/2
✓ Branch 0 taken 19139 times.
✓ Branch 1 taken 8843 times.
27982 return m ? m->count : 0;
42 }
43
44 4421714 const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m,
45 const AVDictionaryEntry *prev)
46 {
47 4421714 int i = 0;
48
49
2/2
✓ Branch 0 taken 2180058 times.
✓ Branch 1 taken 2241656 times.
4421714 if (!m)
50 2180058 return NULL;
51
52
2/2
✓ Branch 0 taken 1432106 times.
✓ Branch 1 taken 809550 times.
2241656 if (prev)
53 1432106 i = prev - m->elems + 1;
54
55 av_assert2(i >= 0);
56
2/2
✓ Branch 0 taken 601698 times.
✓ Branch 1 taken 1639958 times.
2241656 if (i >= m->count)
57 601698 return NULL;
58
59 1639958 return &m->elems[i];
60 }
61
62 1441688 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
63 const AVDictionaryEntry *prev, int flags)
64 {
65 1441688 const AVDictionaryEntry *entry = prev;
66 unsigned int j;
67
68
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1441687 times.
1441688 if (!key)
69 1 return NULL;
70
71
2/2
✓ Branch 1 taken 1223296 times.
✓ Branch 2 taken 1233652 times.
2456948 while ((entry = av_dict_iterate(m, entry))) {
72 1223296 const char *s = entry->key;
73
2/2
✓ Branch 0 taken 41396 times.
✓ Branch 1 taken 1181900 times.
1223296 if (flags & AV_DICT_MATCH_CASE)
74
4/4
✓ Branch 0 taken 127376 times.
✓ Branch 1 taken 33599 times.
✓ Branch 2 taken 119579 times.
✓ Branch 3 taken 7797 times.
160975 for (j = 0; s[j] == key[j] && key[j]; j++)
75 ;
76 else
77
4/4
✓ Branch 0 taken 4533761 times.
✓ Branch 1 taken 981810 times.
✓ Branch 2 taken 4333671 times.
✓ Branch 3 taken 200090 times.
5515571 for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
78 ;
79
2/2
✓ Branch 0 taken 1010715 times.
✓ Branch 1 taken 212581 times.
1223296 if (key[j])
80 1010715 continue;
81
4/4
✓ Branch 0 taken 4694 times.
✓ Branch 1 taken 207887 times.
✓ Branch 2 taken 4546 times.
✓ Branch 3 taken 148 times.
212581 if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
82 4546 continue;
83 208035 return (AVDictionaryEntry *)entry;
84 }
85 1233652 return NULL;
86 }
87
88 1110748 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
89 int flags)
90 {
91 1110748 AVDictionary *m = *pm;
92 1110748 AVDictionaryEntry *tag = NULL;
93 1110748 char *copy_key = NULL, *copy_value = NULL;
94 int err;
95
96
2/2
✓ Branch 0 taken 76949 times.
✓ Branch 1 taken 1033799 times.
1110748 if (flags & AV_DICT_DONT_STRDUP_VAL)
97 76949 copy_value = (void *)value;
98
2/2
✓ Branch 0 taken 432460 times.
✓ Branch 1 taken 601339 times.
1033799 else if (value)
99 432460 copy_value = av_strdup(value);
100
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1110744 times.
1110748 if (!key) {
101 4 err = AVERROR(EINVAL);
102 4 goto err_out;
103 }
104
2/2
✓ Branch 0 taken 991405 times.
✓ Branch 1 taken 119339 times.
1110744 if (!(flags & AV_DICT_MULTIKEY)) {
105 991405 tag = av_dict_get(m, key, NULL, flags);
106 }
107
2/2
✓ Branch 0 taken 51792 times.
✓ Branch 1 taken 1058952 times.
1110744 if (flags & AV_DICT_DONT_STRDUP_KEY)
108 51792 copy_key = (void *)key;
109 else
110 1058952 copy_key = av_strdup(key);
111
2/2
✓ Branch 0 taken 620185 times.
✓ Branch 1 taken 490559 times.
1110744 if (!m)
112 620185 m = *pm = av_mallocz(sizeof(*m));
113
5/8
✓ Branch 0 taken 1110744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1110744 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 509406 times.
✓ Branch 5 taken 601338 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 509406 times.
1110744 if (!m || !copy_key || (value && !copy_value))
114 goto enomem;
115
116
2/2
✓ Branch 0 taken 135197 times.
✓ Branch 1 taken 975547 times.
1110744 if (tag) {
117
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 134691 times.
135197 if (flags & AV_DICT_DONT_OVERWRITE) {
118 506 av_free(copy_key);
119 506 av_free(copy_value);
120 506 return 0;
121 }
122
4/4
✓ Branch 0 taken 27989 times.
✓ Branch 1 taken 106702 times.
✓ Branch 2 taken 11288 times.
✓ Branch 3 taken 16701 times.
134691 if (copy_value && flags & AV_DICT_APPEND) {
123 11288 size_t oldlen = strlen(tag->value);
124 11288 size_t new_part_len = strlen(copy_value);
125 11288 size_t len = oldlen + new_part_len + 1;
126 11288 char *newval = av_realloc(tag->value, len);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11288 times.
11288 if (!newval)
128 goto enomem;
129 11288 memcpy(newval + oldlen, copy_value, new_part_len + 1);
130 11288 av_freep(&copy_value);
131 11288 copy_value = newval;
132 } else
133 123403 av_free(tag->value);
134 134691 av_free(tag->key);
135 134691 *tag = m->elems[--m->count];
136
2/2
✓ Branch 0 taken 480911 times.
✓ Branch 1 taken 494636 times.
975547 } else if (copy_value) {
137 480911 AVDictionaryEntry *tmp = av_realloc_array(m->elems,
138 480911 m->count + 1, sizeof(*m->elems));
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480911 times.
480911 if (!tmp)
140 goto enomem;
141 480911 m->elems = tmp;
142 }
143
2/2
✓ Branch 0 taken 508900 times.
✓ Branch 1 taken 601338 times.
1110238 if (copy_value) {
144 508900 m->elems[m->count].key = copy_key;
145 508900 m->elems[m->count].value = copy_value;
146 508900 m->count++;
147 } else {
148 601338 err = 0;
149 601338 goto end;
150 }
151
152 508900 return 0;
153
154 enomem:
155 err = AVERROR(ENOMEM);
156 4 err_out:
157 4 av_free(copy_value);
158 601342 end:
159
4/4
✓ Branch 0 taken 601338 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 457859 times.
✓ Branch 3 taken 143479 times.
601342 if (m && !m->count) {
160 457859 av_freep(&m->elems);
161 457859 av_freep(pm);
162 }
163 601342 av_free(copy_key);
164 601342 return err;
165 }
166
167 8455 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
168 int flags)
169 {
170 char valuestr[22];
171 8455 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
172 8455 flags &= ~AV_DICT_DONT_STRDUP_VAL;
173 8455 return av_dict_set(pm, key, valuestr, flags);
174 }
175
176 397 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
177 const char *key_val_sep, const char *pairs_sep,
178 int flags)
179 {
180 397 char *key = av_get_token(buf, key_val_sep);
181 397 char *val = NULL;
182 int ret;
183
184
3/6
✓ Branch 0 taken 397 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 397 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 397 times.
✗ Branch 5 not taken.
397 if (key && *key && strspn(*buf, key_val_sep)) {
185 397 (*buf)++;
186 397 val = av_get_token(buf, pairs_sep);
187 }
188
189
4/8
✓ Branch 0 taken 397 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 397 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 397 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 397 times.
✗ Branch 7 not taken.
397 if (key && *key && val && *val)
190 397 ret = av_dict_set(pm, key, val, flags);
191 else
192 ret = AVERROR(EINVAL);
193
194 397 av_freep(&key);
195 397 av_freep(&val);
196
197 397 return ret;
198 }
199
200 195 int av_dict_parse_string(AVDictionary **pm, const char *str,
201 const char *key_val_sep, const char *pairs_sep,
202 int flags)
203 {
204 int ret;
205
206
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 190 times.
195 if (!str)
207 5 return 0;
208
209 /* ignore STRDUP flags */
210 190 flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
211
212
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 190 times.
777 while (*str) {
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 397 times.
397 if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
214 return ret;
215
216
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 209 times.
397 if (*str)
217 209 str++;
218 }
219
220 190 return 0;
221 }
222
223 7663403 void av_dict_free(AVDictionary **pm)
224 {
225 7663403 AVDictionary *m = *pm;
226
227
2/2
✓ Branch 0 taken 162326 times.
✓ Branch 1 taken 7501077 times.
7663403 if (m) {
228
2/2
✓ Branch 0 taken 374209 times.
✓ Branch 1 taken 162326 times.
536535 while (m->count--) {
229 374209 av_freep(&m->elems[m->count].key);
230 374209 av_freep(&m->elems[m->count].value);
231 }
232 162326 av_freep(&m->elems);
233 }
234 7663403 av_freep(pm);
235 7663403 }
236
237 1202849 int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
238 {
239 1202849 const AVDictionaryEntry *t = NULL;
240
241
2/2
✓ Branch 1 taken 48273 times.
✓ Branch 2 taken 1202849 times.
1251122 while ((t = av_dict_iterate(src, t))) {
242 48273 int ret = av_dict_set(dst, t->key, t->value, flags);
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48273 times.
48273 if (ret < 0)
244 return ret;
245 }
246
247 1202849 return 0;
248 }
249
250 3997 int av_dict_get_string(const AVDictionary *m, char **buffer,
251 const char key_val_sep, const char pairs_sep)
252 {
253 3997 const AVDictionaryEntry *t = NULL;
254 AVBPrint bprint;
255 3997 int cnt = 0;
256 3997 char special_chars[] = {pairs_sep, key_val_sep, '\0'};
257
258
4/8
✓ Branch 0 taken 3997 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3997 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3997 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3997 times.
✗ Branch 7 not taken.
3997 if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
259
2/4
✓ Branch 0 taken 3997 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3997 times.
3997 pairs_sep == '\\' || key_val_sep == '\\')
260 return AVERROR(EINVAL);
261
262
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3992 times.
3997 if (!av_dict_count(m)) {
263 5 *buffer = av_strdup("");
264
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return *buffer ? 0 : AVERROR(ENOMEM);
265 }
266
267 3992 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
268
2/2
✓ Branch 1 taken 4022 times.
✓ Branch 2 taken 3992 times.
8014 while ((t = av_dict_iterate(m, t))) {
269
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3992 times.
4022 if (cnt++)
270 30 av_bprint_append_data(&bprint, &pairs_sep, 1);
271 4022 av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
272 4022 av_bprint_append_data(&bprint, &key_val_sep, 1);
273 4022 av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
274 }
275 3992 return av_bprint_finalize(&bprint, buffer);
276 }
277
278 561 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
279 {
280 561 time_t seconds = timestamp / 1000000;
281 struct tm *ptm, tmbuf;
282 561 ptm = gmtime_r(&seconds, &tmbuf);
283
1/2
✓ Branch 0 taken 561 times.
✗ Branch 1 not taken.
561 if (ptm) {
284 char buf[32];
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 561 times.
561 if (!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", ptm))
286 return AVERROR_EXTERNAL;
287 561 av_strlcatf(buf, sizeof(buf), ".%06dZ", (int)(timestamp % 1000000));
288 561 return av_dict_set(dict, key, buf, 0);
289 } else {
290 return AVERROR_EXTERNAL;
291 }
292 }
293