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 | 28056 | int av_dict_count(const AVDictionary *m) | |
40 | { | ||
41 |
2/2✓ Branch 0 taken 19203 times.
✓ Branch 1 taken 8853 times.
|
28056 | return m ? m->count : 0; |
42 | } | ||
43 | |||
44 | 4423269 | const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, | |
45 | const AVDictionaryEntry *prev) | ||
46 | { | ||
47 | 4423269 | int i = 0; | |
48 | |||
49 |
2/2✓ Branch 0 taken 2185215 times.
✓ Branch 1 taken 2238054 times.
|
4423269 | if (!m) |
50 | 2185215 | return NULL; | |
51 | |||
52 |
2/2✓ Branch 0 taken 1428445 times.
✓ Branch 1 taken 809609 times.
|
2238054 | if (prev) |
53 | 1428445 | i = prev - m->elems + 1; | |
54 | |||
55 | av_assert2(i >= 0); | ||
56 |
2/2✓ Branch 0 taken 601345 times.
✓ Branch 1 taken 1636709 times.
|
2238054 | if (i >= m->count) |
57 | 601345 | return NULL; | |
58 | |||
59 | 1636709 | return &m->elems[i]; | |
60 | } | ||
61 | |||
62 | 1444509 | AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, | |
63 | const AVDictionaryEntry *prev, int flags) | ||
64 | { | ||
65 | 1444509 | const AVDictionaryEntry *entry = prev; | |
66 | unsigned int j; | ||
67 | |||
68 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1444508 times.
|
1444509 | if (!key) |
69 | 1 | return NULL; | |
70 | |||
71 |
2/2✓ Branch 1 taken 1227291 times.
✓ Branch 2 taken 1236053 times.
|
2463344 | while ((entry = av_dict_iterate(m, entry))) { |
72 | 1227291 | const char *s = entry->key; | |
73 |
2/2✓ Branch 0 taken 41416 times.
✓ Branch 1 taken 1185875 times.
|
1227291 | if (flags & AV_DICT_MATCH_CASE) |
74 |
4/4✓ Branch 0 taken 127596 times.
✓ Branch 1 taken 33602 times.
✓ Branch 2 taken 119782 times.
✓ Branch 3 taken 7814 times.
|
161198 | for (j = 0; s[j] == key[j] && key[j]; j++) |
75 | ; | ||
76 | else | ||
77 |
4/4✓ Branch 0 taken 4537454 times.
✓ Branch 1 taken 985382 times.
✓ Branch 2 taken 4336961 times.
✓ Branch 3 taken 200493 times.
|
5522836 | for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++) |
78 | ; | ||
79 |
2/2✓ Branch 0 taken 1014289 times.
✓ Branch 1 taken 213002 times.
|
1227291 | if (key[j]) |
80 | 1014289 | continue; | |
81 |
4/4✓ Branch 0 taken 4695 times.
✓ Branch 1 taken 208307 times.
✓ Branch 2 taken 4547 times.
✓ Branch 3 taken 148 times.
|
213002 | if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) |
82 | 4547 | continue; | |
83 | 208455 | return (AVDictionaryEntry *)entry; | |
84 | } | ||
85 | 1236053 | return NULL; | |
86 | } | ||
87 | |||
88 | 1103721 | int av_dict_set(AVDictionary **pm, const char *key, const char *value, | |
89 | int flags) | ||
90 | { | ||
91 | 1103721 | AVDictionary *m = *pm; | |
92 | 1103721 | AVDictionaryEntry *tag = NULL; | |
93 | 1103721 | char *copy_key = NULL, *copy_value = NULL; | |
94 | int err; | ||
95 | |||
96 |
2/2✓ Branch 0 taken 77089 times.
✓ Branch 1 taken 1026632 times.
|
1103721 | if (flags & AV_DICT_DONT_STRDUP_VAL) |
97 | 77089 | copy_value = (void *)value; | |
98 |
2/2✓ Branch 0 taken 425103 times.
✓ Branch 1 taken 601529 times.
|
1026632 | else if (value) |
99 | 425103 | copy_value = av_strdup(value); | |
100 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1103717 times.
|
1103721 | if (!key) { |
101 | 4 | err = AVERROR(EINVAL); | |
102 | 4 | goto err_out; | |
103 | } | ||
104 |
2/2✓ Branch 0 taken 992392 times.
✓ Branch 1 taken 111325 times.
|
1103717 | if (!(flags & AV_DICT_MULTIKEY)) { |
105 | 992392 | tag = av_dict_get(m, key, NULL, flags); | |
106 | } | ||
107 |
2/2✓ Branch 0 taken 51911 times.
✓ Branch 1 taken 1051806 times.
|
1103717 | if (flags & AV_DICT_DONT_STRDUP_KEY) |
108 | 51911 | copy_key = (void *)key; | |
109 | else | ||
110 | 1051806 | copy_key = av_strdup(key); | |
111 |
2/2✓ Branch 0 taken 618310 times.
✓ Branch 1 taken 485407 times.
|
1103717 | if (!m) |
112 | 618310 | m = *pm = av_mallocz(sizeof(*m)); | |
113 |
5/8✓ Branch 0 taken 1103717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1103717 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 502189 times.
✓ Branch 5 taken 601528 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 502189 times.
|
1103717 | if (!m || !copy_key || (value && !copy_value)) |
114 | ✗ | goto enomem; | |
115 | |||
116 |
2/2✓ Branch 0 taken 135394 times.
✓ Branch 1 taken 968323 times.
|
1103717 | if (tag) { |
117 |
2/2✓ Branch 0 taken 506 times.
✓ Branch 1 taken 134888 times.
|
135394 | 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 28152 times.
✓ Branch 1 taken 106736 times.
✓ Branch 2 taken 11321 times.
✓ Branch 3 taken 16831 times.
|
134888 | if (copy_value && flags & AV_DICT_APPEND) { |
123 | 11321 | size_t oldlen = strlen(tag->value); | |
124 | 11321 | size_t new_part_len = strlen(copy_value); | |
125 | 11321 | size_t len = oldlen + new_part_len + 1; | |
126 | 11321 | char *newval = av_realloc(tag->value, len); | |
127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11321 times.
|
11321 | if (!newval) |
128 | ✗ | goto enomem; | |
129 | 11321 | memcpy(newval + oldlen, copy_value, new_part_len + 1); | |
130 | 11321 | av_freep(©_value); | |
131 | 11321 | copy_value = newval; | |
132 | } else | ||
133 | 123567 | av_free(tag->value); | |
134 | 134888 | av_free(tag->key); | |
135 | 134888 | *tag = m->elems[--m->count]; | |
136 |
2/2✓ Branch 0 taken 473531 times.
✓ Branch 1 taken 494792 times.
|
968323 | } else if (copy_value) { |
137 | 473531 | AVDictionaryEntry *tmp = av_realloc_array(m->elems, | |
138 | 473531 | m->count + 1, sizeof(*m->elems)); | |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 473531 times.
|
473531 | if (!tmp) |
140 | ✗ | goto enomem; | |
141 | 473531 | m->elems = tmp; | |
142 | } | ||
143 |
2/2✓ Branch 0 taken 501683 times.
✓ Branch 1 taken 601528 times.
|
1103211 | if (copy_value) { |
144 | 501683 | m->elems[m->count].key = copy_key; | |
145 | 501683 | m->elems[m->count].value = copy_value; | |
146 | 501683 | m->count++; | |
147 | } else { | ||
148 | 601528 | err = 0; | |
149 | 601528 | goto end; | |
150 | } | ||
151 | |||
152 | 501683 | return 0; | |
153 | |||
154 | ✗ | enomem: | |
155 | ✗ | err = AVERROR(ENOMEM); | |
156 | 4 | err_out: | |
157 | 4 | av_free(copy_value); | |
158 | 601532 | end: | |
159 |
4/4✓ Branch 0 taken 601528 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 457919 times.
✓ Branch 3 taken 143609 times.
|
601532 | if (m && !m->count) { |
160 | 457919 | av_freep(&m->elems); | |
161 | 457919 | av_freep(pm); | |
162 | } | ||
163 | 601532 | av_free(copy_key); | |
164 | 601532 | return err; | |
165 | } | ||
166 | |||
167 | 8478 | int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, | |
168 | int flags) | ||
169 | { | ||
170 | char valuestr[22]; | ||
171 | 8478 | snprintf(valuestr, sizeof(valuestr), "%"PRId64, value); | |
172 | 8478 | flags &= ~AV_DICT_DONT_STRDUP_VAL; | |
173 | 8478 | return av_dict_set(pm, key, valuestr, flags); | |
174 | } | ||
175 | |||
176 | 436 | 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 | 436 | char *key = av_get_token(buf, key_val_sep); | |
181 | 436 | char *val = NULL; | |
182 | int ret; | ||
183 | |||
184 |
3/6✓ Branch 0 taken 436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 436 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 436 times.
✗ Branch 5 not taken.
|
436 | if (key && *key && strspn(*buf, key_val_sep)) { |
185 | 436 | (*buf)++; | |
186 | 436 | val = av_get_token(buf, pairs_sep); | |
187 | } | ||
188 | |||
189 |
4/8✓ Branch 0 taken 436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 436 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 436 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 436 times.
✗ Branch 7 not taken.
|
436 | if (key && *key && val && *val) |
190 | 436 | ret = av_dict_set(pm, key, val, flags); | |
191 | else | ||
192 | ✗ | ret = AVERROR(EINVAL); | |
193 | |||
194 | 436 | av_freep(&key); | |
195 | 436 | av_freep(&val); | |
196 | |||
197 | 436 | return ret; | |
198 | } | ||
199 | |||
200 | 214 | 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 209 times.
|
214 | if (!str) |
207 | 5 | return 0; | |
208 | |||
209 | /* ignore STRDUP flags */ | ||
210 | 209 | flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
211 | |||
212 |
2/2✓ Branch 0 taken 436 times.
✓ Branch 1 taken 209 times.
|
854 | while (*str) { |
213 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 436 times.
|
436 | 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 207 times.
✓ Branch 1 taken 229 times.
|
436 | if (*str) |
217 | 229 | str++; | |
218 | } | ||
219 | |||
220 | 209 | return 0; | |
221 | } | ||
222 | |||
223 | 7676266 | void av_dict_free(AVDictionary **pm) | |
224 | { | ||
225 | 7676266 | AVDictionary *m = *pm; | |
226 | |||
227 |
2/2✓ Branch 0 taken 160391 times.
✓ Branch 1 taken 7515875 times.
|
7676266 | if (m) { |
228 |
2/2✓ Branch 0 taken 366795 times.
✓ Branch 1 taken 160391 times.
|
527186 | while (m->count--) { |
229 | 366795 | av_freep(&m->elems[m->count].key); | |
230 | 366795 | av_freep(&m->elems[m->count].value); | |
231 | } | ||
232 | 160391 | av_freep(&m->elems); | |
233 | } | ||
234 | 7676266 | av_freep(pm); | |
235 | 7676266 | } | |
236 | |||
237 | 1204521 | int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags) | |
238 | { | ||
239 | 1204521 | const AVDictionaryEntry *t = NULL; | |
240 | |||
241 |
2/2✓ Branch 1 taken 48400 times.
✓ Branch 2 taken 1204521 times.
|
1252921 | while ((t = av_dict_iterate(src, t))) { |
242 | 48400 | int ret = av_dict_set(dst, t->key, t->value, flags); | |
243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48400 times.
|
48400 | if (ret < 0) |
244 | ✗ | return ret; | |
245 | } | ||
246 | |||
247 | 1204521 | return 0; | |
248 | } | ||
249 | |||
250 | 4007 | int av_dict_get_string(const AVDictionary *m, char **buffer, | |
251 | const char key_val_sep, const char pairs_sep) | ||
252 | { | ||
253 | 4007 | const AVDictionaryEntry *t = NULL; | |
254 | AVBPrint bprint; | ||
255 | 4007 | int cnt = 0; | |
256 | 4007 | char special_chars[] = {pairs_sep, key_val_sep, '\0'}; | |
257 | |||
258 |
4/8✓ Branch 0 taken 4007 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4007 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4007 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4007 times.
✗ Branch 7 not taken.
|
4007 | if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep || |
259 |
2/4✓ Branch 0 taken 4007 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4007 times.
|
4007 | pairs_sep == '\\' || key_val_sep == '\\') |
260 | ✗ | return AVERROR(EINVAL); | |
261 | |||
262 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4002 times.
|
4007 | 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 | 4002 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
268 |
2/2✓ Branch 1 taken 4032 times.
✓ Branch 2 taken 4002 times.
|
8034 | while ((t = av_dict_iterate(m, t))) { |
269 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 4002 times.
|
4032 | if (cnt++) |
270 | 30 | av_bprint_append_data(&bprint, &pairs_sep, 1); | |
271 | 4032 | av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
272 | 4032 | av_bprint_append_data(&bprint, &key_val_sep, 1); | |
273 | 4032 | av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
274 | } | ||
275 | 4002 | 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 |