| 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 | }; | ||
| 36 | |||
| 37 | 33019 | int av_dict_count(const AVDictionary *m) | |
| 38 | { | ||
| 39 |
2/2✓ Branch 0 taken 23221 times.
✓ Branch 1 taken 9798 times.
|
33019 | return m ? m->count : 0; |
| 40 | } | ||
| 41 | |||
| 42 | 6284066 | const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, | |
| 43 | const AVDictionaryEntry *prev) | ||
| 44 | { | ||
| 45 | 6284066 | int i = 0; | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 2808887 times.
✓ Branch 1 taken 3475179 times.
|
6284066 | if (!m) |
| 48 | 2808887 | return NULL; | |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 2256817 times.
✓ Branch 1 taken 1218362 times.
|
3475179 | if (prev) |
| 51 | 2256817 | i = prev - m->elems + 1; | |
| 52 | |||
| 53 | av_assert2(i >= 0); | ||
| 54 |
2/2✓ Branch 0 taken 954475 times.
✓ Branch 1 taken 2520704 times.
|
3475179 | if (i >= m->count) |
| 55 | 954475 | return NULL; | |
| 56 | |||
| 57 | 2520704 | return &m->elems[i]; | |
| 58 | } | ||
| 59 | |||
| 60 | 2020894 | AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, | |
| 61 | const AVDictionaryEntry *prev, int flags) | ||
| 62 | { | ||
| 63 | 2020894 | const AVDictionaryEntry *entry = prev; | |
| 64 | unsigned int j; | ||
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2020893 times.
|
2020894 | if (!key) |
| 67 | 1 | return NULL; | |
| 68 | |||
| 69 |
2/2✓ Branch 1 taken 1963538 times.
✓ Branch 2 taken 1756761 times.
|
3720299 | while ((entry = av_dict_iterate(m, entry))) { |
| 70 | 1963538 | const char *s = entry->key; | |
| 71 |
2/2✓ Branch 0 taken 225912 times.
✓ Branch 1 taken 1737626 times.
|
1963538 | if (flags & AV_DICT_MATCH_CASE) |
| 72 |
4/4✓ Branch 0 taken 271609 times.
✓ Branch 1 taken 215210 times.
✓ Branch 2 taken 260907 times.
✓ Branch 3 taken 10702 times.
|
486819 | for (j = 0; s[j] == key[j] && key[j]; j++) |
| 73 | ; | ||
| 74 | else | ||
| 75 |
4/4✓ Branch 0 taken 6703437 times.
✓ Branch 1 taken 1484440 times.
✓ Branch 2 taken 6450251 times.
✓ Branch 3 taken 253186 times.
|
8187877 | for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++) |
| 76 | ; | ||
| 77 |
2/2✓ Branch 0 taken 1692846 times.
✓ Branch 1 taken 270692 times.
|
1963538 | if (key[j]) |
| 78 | 1692846 | continue; | |
| 79 |
4/4✓ Branch 0 taken 6804 times.
✓ Branch 1 taken 263888 times.
✓ Branch 2 taken 6560 times.
✓ Branch 3 taken 244 times.
|
270692 | if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) |
| 80 | 6560 | continue; | |
| 81 | 264132 | return (AVDictionaryEntry *)entry; | |
| 82 | } | ||
| 83 | 1756761 | return NULL; | |
| 84 | } | ||
| 85 | |||
| 86 | 1335507 | int av_dict_set(AVDictionary **pm, const char *key, const char *value, | |
| 87 | int flags) | ||
| 88 | { | ||
| 89 | 1335507 | AVDictionary *m = *pm; | |
| 90 | 1335507 | AVDictionaryEntry *tag = NULL; | |
| 91 | 1335507 | char *copy_key = NULL, *copy_value = NULL; | |
| 92 | int err; | ||
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 65379 times.
✓ Branch 1 taken 1270128 times.
|
1335507 | if (flags & AV_DICT_DONT_STRDUP_VAL) |
| 95 | 65379 | copy_value = (void *)value; | |
| 96 |
2/2✓ Branch 0 taken 581147 times.
✓ Branch 1 taken 688981 times.
|
1270128 | else if (value) |
| 97 | 581147 | copy_value = av_strdup(value); | |
| 98 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1335503 times.
|
1335507 | if (!key) { |
| 99 | 4 | err = AVERROR(EINVAL); | |
| 100 | 4 | goto err_out; | |
| 101 | } | ||
| 102 |
2/2✓ Branch 0 taken 34203 times.
✓ Branch 1 taken 1301300 times.
|
1335503 | if (flags & AV_DICT_DONT_STRDUP_KEY) |
| 103 | 34203 | copy_key = (void *)key; | |
| 104 | else | ||
| 105 | 1301300 | copy_key = av_strdup(key); | |
| 106 |
4/6✓ Branch 0 taken 1335503 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 646521 times.
✓ Branch 3 taken 688982 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 646521 times.
|
1335503 | if (!copy_key || (value && !copy_value)) |
| 107 | ✗ | goto enomem; | |
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 1221844 times.
✓ Branch 1 taken 113659 times.
|
1335503 | if (!(flags & AV_DICT_MULTIKEY)) { |
| 110 | 1221844 | tag = av_dict_get(m, key, NULL, flags); | |
| 111 |
2/2✓ Branch 0 taken 7659 times.
✓ Branch 1 taken 106000 times.
|
113659 | } else if (flags & AV_DICT_DEDUP) { |
| 112 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 7642 times.
|
7662 | while ((tag = av_dict_get(m, key, tag, flags))) { |
| 113 |
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) || |
| 114 |
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))) { |
| 115 | 17 | av_free(copy_key); | |
| 116 | 17 | av_free(copy_value); | |
| 117 | 17 | return 0; | |
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 |
2/2✓ Branch 0 taken 734430 times.
✓ Branch 1 taken 601056 times.
|
1335486 | if (!m) |
| 122 | 734430 | m = *pm = av_mallocz(sizeof(*m)); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1335486 times.
|
1335486 | if (!m) |
| 124 | ✗ | goto enomem; | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 164836 times.
✓ Branch 1 taken 1170650 times.
|
1335486 | if (tag) { |
| 127 |
2/2✓ Branch 0 taken 851 times.
✓ Branch 1 taken 163985 times.
|
164836 | if (flags & AV_DICT_DONT_OVERWRITE) { |
| 128 | 851 | av_free(copy_key); | |
| 129 | 851 | av_free(copy_value); | |
| 130 | 851 | return 0; | |
| 131 | } | ||
| 132 |
4/4✓ Branch 0 taken 38731 times.
✓ Branch 1 taken 125254 times.
✓ Branch 2 taken 16474 times.
✓ Branch 3 taken 22257 times.
|
163985 | if (copy_value && flags & AV_DICT_APPEND) { |
| 133 | 16474 | size_t oldlen = strlen(tag->value); | |
| 134 | 16474 | size_t new_part_len = strlen(copy_value); | |
| 135 | 16474 | size_t len = oldlen + new_part_len + 1; | |
| 136 | 16474 | char *newval = av_realloc(tag->value, len); | |
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16474 times.
|
16474 | if (!newval) |
| 138 | ✗ | goto enomem; | |
| 139 | 16474 | memcpy(newval + oldlen, copy_value, new_part_len + 1); | |
| 140 | 16474 | av_freep(©_value); | |
| 141 | 16474 | copy_value = newval; | |
| 142 | } else | ||
| 143 | 147511 | av_free(tag->value); | |
| 144 | 163985 | av_free(tag->key); | |
| 145 | 163985 | *tag = m->elems[--m->count]; | |
| 146 |
2/2✓ Branch 0 taken 606922 times.
✓ Branch 1 taken 563728 times.
|
1170650 | } else if (copy_value) { |
| 147 | 606922 | AVDictionaryEntry *tmp = av_realloc_array(m->elems, | |
| 148 | 606922 | m->count + 1, sizeof(*m->elems)); | |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 606922 times.
|
606922 | if (!tmp) |
| 150 | ✗ | goto enomem; | |
| 151 | 606922 | m->elems = tmp; | |
| 152 | } | ||
| 153 |
2/2✓ Branch 0 taken 645653 times.
✓ Branch 1 taken 688982 times.
|
1334635 | if (copy_value) { |
| 154 | 645653 | m->elems[m->count].key = copy_key; | |
| 155 | 645653 | m->elems[m->count].value = copy_value; | |
| 156 | 645653 | m->count++; | |
| 157 | } else { | ||
| 158 | 688982 | err = 0; | |
| 159 | 688982 | goto end; | |
| 160 | } | ||
| 161 | |||
| 162 | 645653 | return 0; | |
| 163 | |||
| 164 | ✗ | enomem: | |
| 165 | ✗ | err = AVERROR(ENOMEM); | |
| 166 | 4 | err_out: | |
| 167 | 4 | av_free(copy_value); | |
| 168 | 688986 | end: | |
| 169 |
4/4✓ Branch 0 taken 688982 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 517689 times.
✓ Branch 3 taken 171293 times.
|
688986 | if (m && !m->count) { |
| 170 | 517689 | av_freep(&m->elems); | |
| 171 | 517689 | av_freep(pm); | |
| 172 | } | ||
| 173 | 688986 | av_free(copy_key); | |
| 174 | 688986 | return err; | |
| 175 | } | ||
| 176 | |||
| 177 | 9671 | int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, | |
| 178 | int flags) | ||
| 179 | { | ||
| 180 | char valuestr[22]; | ||
| 181 | 9671 | snprintf(valuestr, sizeof(valuestr), "%"PRId64, value); | |
| 182 | 9671 | flags &= ~AV_DICT_DONT_STRDUP_VAL; | |
| 183 | 9671 | return av_dict_set(pm, key, valuestr, flags); | |
| 184 | } | ||
| 185 | |||
| 186 | 604 | static int parse_key_value_pair(AVDictionary **pm, const char **buf, | |
| 187 | const char *key_val_sep, const char *pairs_sep, | ||
| 188 | int flags) | ||
| 189 | { | ||
| 190 | 604 | char *key = av_get_token(buf, key_val_sep); | |
| 191 | 604 | char *val = NULL; | |
| 192 | int ret; | ||
| 193 | |||
| 194 |
3/6✓ Branch 0 taken 604 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 604 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 604 times.
✗ Branch 5 not taken.
|
604 | if (key && *key && strspn(*buf, key_val_sep)) { |
| 195 | 604 | (*buf)++; | |
| 196 | 604 | val = av_get_token(buf, pairs_sep); | |
| 197 | } | ||
| 198 | |||
| 199 |
4/8✓ Branch 0 taken 604 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 604 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 604 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 604 times.
✗ Branch 7 not taken.
|
604 | if (key && *key && val && *val) |
| 200 | 604 | ret = av_dict_set(pm, key, val, flags); | |
| 201 | else | ||
| 202 | ✗ | ret = AVERROR(EINVAL); | |
| 203 | |||
| 204 | 604 | av_freep(&key); | |
| 205 | 604 | av_freep(&val); | |
| 206 | |||
| 207 | 604 | return ret; | |
| 208 | } | ||
| 209 | |||
| 210 | 294 | int av_dict_parse_string(AVDictionary **pm, const char *str, | |
| 211 | const char *key_val_sep, const char *pairs_sep, | ||
| 212 | int flags) | ||
| 213 | { | ||
| 214 | int ret; | ||
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 285 times.
|
294 | if (!str) |
| 217 | 9 | return 0; | |
| 218 | |||
| 219 | /* ignore STRDUP flags */ | ||
| 220 | 285 | flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
| 221 | |||
| 222 |
2/2✓ Branch 0 taken 604 times.
✓ Branch 1 taken 285 times.
|
1174 | while (*str) { |
| 223 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 604 times.
|
604 | if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0) |
| 224 | ✗ | return ret; | |
| 225 | |||
| 226 |
2/2✓ Branch 0 taken 283 times.
✓ Branch 1 taken 321 times.
|
604 | if (*str) |
| 227 | 321 | str++; | |
| 228 | } | ||
| 229 | |||
| 230 | 285 | return 0; | |
| 231 | } | ||
| 232 | |||
| 233 | 11041928 | void av_dict_free(AVDictionary **pm) | |
| 234 | { | ||
| 235 | 11041928 | AVDictionary *m = *pm; | |
| 236 | |||
| 237 |
2/2✓ Branch 0 taken 216741 times.
✓ Branch 1 taken 10825187 times.
|
11041928 | if (m) { |
| 238 |
2/2✓ Branch 0 taken 481668 times.
✓ Branch 1 taken 216741 times.
|
698409 | while (m->count--) { |
| 239 | 481668 | av_freep(&m->elems[m->count].key); | |
| 240 | 481668 | av_freep(&m->elems[m->count].value); | |
| 241 | } | ||
| 242 | 216741 | av_freep(&m->elems); | |
| 243 | } | ||
| 244 | 11041928 | av_freep(pm); | |
| 245 | 11041928 | } | |
| 246 | |||
| 247 | 1592143 | int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags) | |
| 248 | { | ||
| 249 | 1592143 | const AVDictionaryEntry *t = NULL; | |
| 250 | |||
| 251 |
2/2✓ Branch 1 taken 110127 times.
✓ Branch 2 taken 1592143 times.
|
1702270 | while ((t = av_dict_iterate(src, t))) { |
| 252 | 110127 | int ret = av_dict_set(dst, t->key, t->value, flags); | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110127 times.
|
110127 | if (ret < 0) |
| 254 | ✗ | return ret; | |
| 255 | } | ||
| 256 | |||
| 257 | 1592143 | return 0; | |
| 258 | } | ||
| 259 | |||
| 260 | 4630 | int av_dict_get_string(const AVDictionary *m, char **buffer, | |
| 261 | const char key_val_sep, const char pairs_sep) | ||
| 262 | { | ||
| 263 | 4630 | const AVDictionaryEntry *t = NULL; | |
| 264 | AVBPrint bprint; | ||
| 265 | 4630 | int cnt = 0; | |
| 266 | 4630 | char special_chars[] = {pairs_sep, key_val_sep, '\0'}; | |
| 267 | |||
| 268 |
4/8✓ Branch 0 taken 4630 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4630 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4630 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4630 times.
✗ Branch 7 not taken.
|
4630 | if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep || |
| 269 |
2/4✓ Branch 0 taken 4630 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4630 times.
|
4630 | pairs_sep == '\\' || key_val_sep == '\\') |
| 270 | ✗ | return AVERROR(EINVAL); | |
| 271 | |||
| 272 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 4589 times.
|
4630 | if (!av_dict_count(m)) { |
| 273 | 41 | *buffer = av_strdup(""); | |
| 274 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | return *buffer ? 0 : AVERROR(ENOMEM); |
| 275 | } | ||
| 276 | |||
| 277 | 4589 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 278 |
2/2✓ Branch 1 taken 4643 times.
✓ Branch 2 taken 4589 times.
|
9232 | while ((t = av_dict_iterate(m, t))) { |
| 279 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 4589 times.
|
4643 | if (cnt++) |
| 280 | 54 | av_bprint_append_data(&bprint, &pairs_sep, 1); | |
| 281 | 4643 | av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 282 | 4643 | av_bprint_append_data(&bprint, &key_val_sep, 1); | |
| 283 | 4643 | av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 284 | } | ||
| 285 | 4589 | return av_bprint_finalize(&bprint, buffer); | |
| 286 | } | ||
| 287 |