| 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 "error.h" | ||
| 29 | #include "mem.h" | ||
| 30 | #include "bprint.h" | ||
| 31 | |||
| 32 | struct AVDictionary { | ||
| 33 | int count; | ||
| 34 | AVDictionaryEntry *elems; | ||
| 35 | int duplicate_keys; | ||
| 36 | }; | ||
| 37 | |||
| 38 | 33387 | int av_dict_count(const AVDictionary *m) | |
| 39 | { | ||
| 40 |
2/2✓ Branch 0 taken 23538 times.
✓ Branch 1 taken 9849 times.
|
33387 | return m ? m->count : 0; |
| 41 | } | ||
| 42 | |||
| 43 | 6057906 | const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, | |
| 44 | const AVDictionaryEntry *prev) | ||
| 45 | { | ||
| 46 | 6057906 | int i = 0; | |
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 2931731 times.
✓ Branch 1 taken 3126175 times.
|
6057906 | if (!m) |
| 49 | 2931731 | return NULL; | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 1992261 times.
✓ Branch 1 taken 1133914 times.
|
3126175 | if (prev) |
| 52 | 1992261 | i = prev - m->elems + 1; | |
| 53 | |||
| 54 | av_assert2(i >= 0); | ||
| 55 |
2/2✓ Branch 0 taken 867649 times.
✓ Branch 1 taken 2258526 times.
|
3126175 | if (i >= m->count) |
| 56 | 867649 | return NULL; | |
| 57 | |||
| 58 | 2258526 | return &m->elems[i]; | |
| 59 | } | ||
| 60 | |||
| 61 | 1937771 | AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, | |
| 62 | const AVDictionaryEntry *prev, int flags) | ||
| 63 | { | ||
| 64 | 1937771 | const AVDictionaryEntry *entry = prev; | |
| 65 | unsigned int j; | ||
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1937770 times.
|
1937771 | if (!key) |
| 68 | 1 | return NULL; | |
| 69 | |||
| 70 |
2/2✓ Branch 1 taken 1791305 times.
✓ Branch 2 taken 1671258 times.
|
3462563 | while ((entry = av_dict_iterate(m, entry))) { |
| 71 | 1791305 | const char *s = entry->key; | |
| 72 |
2/2✓ Branch 0 taken 229431 times.
✓ Branch 1 taken 1561874 times.
|
1791305 | if (flags & AV_DICT_MATCH_CASE) |
| 73 |
4/4✓ Branch 0 taken 275982 times.
✓ Branch 1 taken 218576 times.
✓ Branch 2 taken 265127 times.
✓ Branch 3 taken 10855 times.
|
494558 | for (j = 0; s[j] == key[j] && key[j]; j++) |
| 74 | ; | ||
| 75 | else | ||
| 76 |
4/4✓ Branch 0 taken 5842147 times.
✓ Branch 1 taken 1306461 times.
✓ Branch 2 taken 5586734 times.
✓ Branch 3 taken 255413 times.
|
7148608 | for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++) |
| 77 | ; | ||
| 78 |
2/2✓ Branch 0 taken 1519697 times.
✓ Branch 1 taken 271608 times.
|
1791305 | if (key[j]) |
| 79 | 1519697 | continue; | |
| 80 |
4/4✓ Branch 0 taken 5340 times.
✓ Branch 1 taken 266268 times.
✓ Branch 2 taken 5096 times.
✓ Branch 3 taken 244 times.
|
271608 | if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) |
| 81 | 5096 | continue; | |
| 82 | 266512 | return (AVDictionaryEntry *)entry; | |
| 83 | } | ||
| 84 | 1671258 | return NULL; | |
| 85 | } | ||
| 86 | |||
| 87 | 1245705 | int av_dict_set(AVDictionary **pm, const char *key, const char *value, | |
| 88 | int flags) | ||
| 89 | { | ||
| 90 | 1245705 | AVDictionary *m = *pm; | |
| 91 | 1245705 | AVDictionaryEntry *tag = NULL; | |
| 92 | 1245705 | char *copy_key = NULL, *copy_value = NULL; | |
| 93 | int err; | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 67578 times.
✓ Branch 1 taken 1178127 times.
|
1245705 | if (flags & AV_DICT_DONT_STRDUP_VAL) |
| 96 | 67578 | copy_value = (void *)value; | |
| 97 |
2/2✓ Branch 0 taken 487610 times.
✓ Branch 1 taken 690517 times.
|
1178127 | else if (value) |
| 98 | 487610 | copy_value = av_strdup(value); | |
| 99 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1245701 times.
|
1245705 | if (!key) { |
| 100 | 4 | err = AVERROR(EINVAL); | |
| 101 | 4 | goto err_out; | |
| 102 | } | ||
| 103 |
2/2✓ Branch 0 taken 36024 times.
✓ Branch 1 taken 1209677 times.
|
1245701 | if (flags & AV_DICT_DONT_STRDUP_KEY) |
| 104 | 36024 | copy_key = (void *)key; | |
| 105 | else | ||
| 106 | 1209677 | copy_key = av_strdup(key); | |
| 107 |
4/6✓ Branch 0 taken 1245701 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 555183 times.
✓ Branch 3 taken 690518 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 555183 times.
|
1245701 | if (!copy_key || (value && !copy_value)) |
| 108 | ✗ | goto enomem; | |
| 109 | |||
| 110 |
2/2✓ Branch 0 taken 1129588 times.
✓ Branch 1 taken 116113 times.
|
1245701 | if (!(flags & AV_DICT_MULTIKEY)) { |
| 111 | 1129588 | tag = av_dict_get(m, key, NULL, flags); | |
| 112 |
2/2✓ Branch 0 taken 7663 times.
✓ Branch 1 taken 108450 times.
|
116113 | } else if (flags & AV_DICT_DEDUP) { |
| 113 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 7646 times.
|
7666 | while ((tag = av_dict_get(m, key, tag, flags))) { |
| 114 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
20 | if ((!value && !tag->value) || |
| 115 |
3/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 3 times.
|
20 | (value && tag->value && !strcmp(value, tag->value))) { |
| 116 | 17 | av_free(copy_key); | |
| 117 | 17 | av_free(copy_value); | |
| 118 | 17 | return 0; | |
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 |
2/2✓ Branch 0 taken 697085 times.
✓ Branch 1 taken 548599 times.
|
1245684 | if (!m) |
| 123 | 697085 | m = *pm = av_mallocz(sizeof(*m)); | |
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1245684 times.
|
1245684 | if (!m) |
| 125 | ✗ | goto enomem; | |
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 165625 times.
✓ Branch 1 taken 1080059 times.
|
1245684 | if (tag) { |
| 128 |
2/2✓ Branch 0 taken 851 times.
✓ Branch 1 taken 164774 times.
|
165625 | if (flags & AV_DICT_DONT_OVERWRITE) { |
| 129 | 851 | av_free(copy_key); | |
| 130 | 851 | av_free(copy_value); | |
| 131 | 851 | return 0; | |
| 132 | } | ||
| 133 |
4/4✓ Branch 0 taken 39338 times.
✓ Branch 1 taken 125436 times.
✓ Branch 2 taken 16501 times.
✓ Branch 3 taken 22837 times.
|
164774 | if (copy_value && flags & AV_DICT_APPEND) { |
| 134 | 16501 | size_t oldlen = strlen(tag->value); | |
| 135 | 16501 | size_t new_part_len = strlen(copy_value); | |
| 136 | 16501 | size_t len = oldlen + new_part_len + 1; | |
| 137 | 16501 | char *newval = av_realloc(tag->value, len); | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16501 times.
|
16501 | if (!newval) |
| 139 | ✗ | goto enomem; | |
| 140 | 16501 | memcpy(newval + oldlen, copy_value, new_part_len + 1); | |
| 141 | 16501 | av_freep(©_value); | |
| 142 | 16501 | copy_value = newval; | |
| 143 | } else | ||
| 144 | 148273 | av_free(tag->value); | |
| 145 | 164774 | av_free(tag->key); | |
| 146 | 164774 | *tag = m->elems[--m->count]; | |
| 147 |
2/2✓ Branch 0 taken 514977 times.
✓ Branch 1 taken 565082 times.
|
1080059 | } else if (copy_value) { |
| 148 | 514977 | AVDictionaryEntry *tmp = av_realloc_array(m->elems, | |
| 149 | 514977 | m->count + 1, sizeof(*m->elems)); | |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 514977 times.
|
514977 | if (!tmp) |
| 151 | ✗ | goto enomem; | |
| 152 | 514977 | m->elems = tmp; | |
| 153 | } | ||
| 154 |
2/2✓ Branch 0 taken 554315 times.
✓ Branch 1 taken 690518 times.
|
1244833 | if (copy_value) { |
| 155 | 554315 | m->elems[m->count].key = copy_key; | |
| 156 | 554315 | m->elems[m->count].value = copy_value; | |
| 157 | 554315 | m->count++; | |
| 158 |
2/2✓ Branch 0 taken 116096 times.
✓ Branch 1 taken 438219 times.
|
554315 | if (flags & (AV_DICT_MATCH_CASE | AV_DICT_IGNORE_SUFFIX | AV_DICT_MULTIKEY)) |
| 159 | 116096 | m->duplicate_keys = 1; | |
| 160 | } else { | ||
| 161 | 690518 | err = 0; | |
| 162 | 690518 | goto end; | |
| 163 | } | ||
| 164 | |||
| 165 | 554315 | return 0; | |
| 166 | |||
| 167 | ✗ | enomem: | |
| 168 | ✗ | err = AVERROR(ENOMEM); | |
| 169 | 4 | err_out: | |
| 170 | 4 | av_free(copy_value); | |
| 171 | 690522 | end: | |
| 172 |
4/4✓ Branch 0 taken 690518 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 518785 times.
✓ Branch 3 taken 171733 times.
|
690522 | if (m && !m->count) { |
| 173 | 518785 | av_freep(&m->elems); | |
| 174 | 518785 | av_freep(pm); | |
| 175 | } | ||
| 176 | 690522 | av_free(copy_key); | |
| 177 | 690522 | return err; | |
| 178 | } | ||
| 179 | |||
| 180 | 9927 | int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, | |
| 181 | int flags) | ||
| 182 | { | ||
| 183 | char valuestr[22]; | ||
| 184 | 9927 | snprintf(valuestr, sizeof(valuestr), "%"PRId64, value); | |
| 185 | 9927 | flags &= ~AV_DICT_DONT_STRDUP_VAL; | |
| 186 | 9927 | return av_dict_set(pm, key, valuestr, flags); | |
| 187 | } | ||
| 188 | |||
| 189 | 627 | static int parse_key_value_pair(AVDictionary **pm, const char **buf, | |
| 190 | const char *key_val_sep, const char *pairs_sep, | ||
| 191 | int flags) | ||
| 192 | { | ||
| 193 | 627 | char *key = av_get_token(buf, key_val_sep); | |
| 194 | 627 | char *val = NULL; | |
| 195 | int ret; | ||
| 196 | |||
| 197 |
3/6✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 627 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 627 times.
✗ Branch 5 not taken.
|
627 | if (key && *key && strspn(*buf, key_val_sep)) { |
| 198 | 627 | (*buf)++; | |
| 199 | 627 | val = av_get_token(buf, pairs_sep); | |
| 200 | } | ||
| 201 | |||
| 202 |
4/8✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 627 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 627 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 627 times.
✗ Branch 7 not taken.
|
627 | if (key && *key && val && *val) |
| 203 | 627 | ret = av_dict_set(pm, key, val, flags); | |
| 204 | else | ||
| 205 | ✗ | ret = AVERROR(EINVAL); | |
| 206 | |||
| 207 | 627 | av_freep(&key); | |
| 208 | 627 | av_freep(&val); | |
| 209 | |||
| 210 | 627 | return ret; | |
| 211 | } | ||
| 212 | |||
| 213 | 312 | int av_dict_parse_string(AVDictionary **pm, const char *str, | |
| 214 | const char *key_val_sep, const char *pairs_sep, | ||
| 215 | int flags) | ||
| 216 | { | ||
| 217 | int ret; | ||
| 218 | |||
| 219 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 300 times.
|
312 | if (!str) |
| 220 | 12 | return 0; | |
| 221 | |||
| 222 | /* ignore STRDUP flags */ | ||
| 223 | 300 | flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
| 224 | |||
| 225 |
2/2✓ Branch 0 taken 627 times.
✓ Branch 1 taken 300 times.
|
1227 | while (*str) { |
| 226 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 627 times.
|
627 | if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0) |
| 227 | ✗ | return ret; | |
| 228 | |||
| 229 |
2/2✓ Branch 0 taken 298 times.
✓ Branch 1 taken 329 times.
|
627 | if (*str) |
| 230 | 329 | str++; | |
| 231 | } | ||
| 232 | |||
| 233 | 300 | return 0; | |
| 234 | } | ||
| 235 | |||
| 236 | 11490607 | void av_dict_free(AVDictionary **pm) | |
| 237 | { | ||
| 238 | 11490607 | AVDictionary *m = *pm; | |
| 239 | |||
| 240 |
2/2✓ Branch 0 taken 219407 times.
✓ Branch 1 taken 11271200 times.
|
11490607 | if (m) { |
| 241 |
2/2✓ Branch 0 taken 492185 times.
✓ Branch 1 taken 219407 times.
|
711592 | while (m->count--) { |
| 242 | 492185 | av_freep(&m->elems[m->count].key); | |
| 243 | 492185 | av_freep(&m->elems[m->count].value); | |
| 244 | } | ||
| 245 | 219407 | av_freep(&m->elems); | |
| 246 | } | ||
| 247 | 11490607 | av_freep(pm); | |
| 248 | 11490607 | } | |
| 249 | |||
| 250 | 1750689 | int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags) | |
| 251 | { | ||
| 252 | 1750689 | const AVDictionaryEntry *t = NULL; | |
| 253 | |||
| 254 |
7/8✓ Branch 0 taken 1743321 times.
✓ Branch 1 taken 7368 times.
✓ Branch 2 taken 47338 times.
✓ Branch 3 taken 1695983 times.
✓ Branch 4 taken 47338 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 41107 times.
✓ Branch 7 taken 6231 times.
|
1750689 | if (!*dst && src && src->count && !src->duplicate_keys && |
| 255 |
1/2✓ Branch 0 taken 41107 times.
✗ Branch 1 not taken.
|
41107 | !(flags & AV_DICT_IGNORE_SUFFIX)) { |
| 256 | 41107 | AVDictionary *m = av_mallocz(sizeof(*m)); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41107 times.
|
41107 | if (!m) |
| 258 | ✗ | return AVERROR(ENOMEM); | |
| 259 | 41107 | m->elems = av_calloc(src->count, sizeof(*m->elems)); | |
| 260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41107 times.
|
41107 | if (!m->elems) { |
| 261 | ✗ | av_free(m); | |
| 262 | ✗ | return AVERROR(ENOMEM); | |
| 263 | } | ||
| 264 | 41107 | *dst = m; | |
| 265 |
2/2✓ Branch 0 taken 102644 times.
✓ Branch 1 taken 41107 times.
|
143751 | for (int i = 0; i < src->count; i++) { |
| 266 | 102644 | m->elems[i].key = av_strdup(src->elems[i].key); | |
| 267 | 102644 | m->elems[i].value = av_strdup(src->elems[i].value); | |
| 268 | 102644 | m->count++; | |
| 269 |
2/4✓ Branch 0 taken 102644 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 102644 times.
|
102644 | if (!m->elems[i].key || !m->elems[i].value) { |
| 270 | ✗ | av_dict_free(dst); | |
| 271 | ✗ | return AVERROR(ENOMEM); | |
| 272 | } | ||
| 273 | } | ||
| 274 | 41107 | return 0; | |
| 275 | } | ||
| 276 | |||
| 277 |
2/2✓ Branch 1 taken 9726 times.
✓ Branch 2 taken 1709582 times.
|
1719308 | while ((t = av_dict_iterate(src, t))) { |
| 278 | 9726 | int ret = av_dict_set(dst, t->key, t->value, flags); | |
| 279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9726 times.
|
9726 | if (ret < 0) |
| 280 | ✗ | return ret; | |
| 281 | } | ||
| 282 | |||
| 283 | 1709582 | return 0; | |
| 284 | } | ||
| 285 | |||
| 286 | 4645 | int av_dict_get_string(const AVDictionary *m, char **buffer, | |
| 287 | const char key_val_sep, const char pairs_sep) | ||
| 288 | { | ||
| 289 | 4645 | const AVDictionaryEntry *t = NULL; | |
| 290 | AVBPrint bprint; | ||
| 291 | 4645 | int cnt = 0; | |
| 292 | 4645 | char special_chars[] = {pairs_sep, key_val_sep, '\0'}; | |
| 293 | |||
| 294 |
4/8✓ Branch 0 taken 4645 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4645 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4645 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4645 times.
✗ Branch 7 not taken.
|
4645 | if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep || |
| 295 |
2/4✓ Branch 0 taken 4645 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4645 times.
|
4645 | pairs_sep == '\\' || key_val_sep == '\\') |
| 296 | ✗ | return AVERROR(EINVAL); | |
| 297 | |||
| 298 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 4604 times.
|
4645 | if (!av_dict_count(m)) { |
| 299 | 41 | *buffer = av_strdup(""); | |
| 300 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | return *buffer ? 0 : AVERROR(ENOMEM); |
| 301 | } | ||
| 302 | |||
| 303 | 4604 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 304 |
2/2✓ Branch 1 taken 4661 times.
✓ Branch 2 taken 4604 times.
|
9265 | while ((t = av_dict_iterate(m, t))) { |
| 305 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4604 times.
|
4661 | if (cnt++) |
| 306 | 57 | av_bprint_append_data(&bprint, &pairs_sep, 1); | |
| 307 | 4661 | av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 308 | 4661 | av_bprint_append_data(&bprint, &key_val_sep, 1); | |
| 309 | 4661 | av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 310 | } | ||
| 311 | 4604 | return av_bprint_finalize(&bprint, buffer); | |
| 312 | } | ||
| 313 |