| 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 | 32031 | int av_dict_count(const AVDictionary *m) | |
| 38 | { | ||
| 39 |
2/2✓ Branch 0 taken 22461 times.
✓ Branch 1 taken 9570 times.
|
32031 | return m ? m->count : 0; |
| 40 | } | ||
| 41 | |||
| 42 | 5189308 | const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, | |
| 43 | const AVDictionaryEntry *prev) | ||
| 44 | { | ||
| 45 | 5189308 | int i = 0; | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 2427024 times.
✓ Branch 1 taken 2762284 times.
|
5189308 | if (!m) |
| 48 | 2427024 | return NULL; | |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 1782014 times.
✓ Branch 1 taken 980270 times.
|
2762284 | if (prev) |
| 51 | 1782014 | i = prev - m->elems + 1; | |
| 52 | |||
| 53 | av_assert2(i >= 0); | ||
| 54 |
2/2✓ Branch 0 taken 726285 times.
✓ Branch 1 taken 2035999 times.
|
2762284 | if (i >= m->count) |
| 55 | 726285 | return NULL; | |
| 56 | |||
| 57 | 2035999 | return &m->elems[i]; | |
| 58 | } | ||
| 59 | |||
| 60 | 1698774 | AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, | |
| 61 | const AVDictionaryEntry *prev, int flags) | ||
| 62 | { | ||
| 63 | 1698774 | const AVDictionaryEntry *entry = prev; | |
| 64 | unsigned int j; | ||
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1698773 times.
|
1698774 | if (!key) |
| 67 | 1 | return NULL; | |
| 68 | |||
| 69 |
2/2✓ Branch 1 taken 1573310 times.
✓ Branch 2 taken 1444575 times.
|
3017885 | while ((entry = av_dict_iterate(m, entry))) { |
| 70 | 1573310 | const char *s = entry->key; | |
| 71 |
2/2✓ Branch 0 taken 42793 times.
✓ Branch 1 taken 1530517 times.
|
1573310 | if (flags & AV_DICT_MATCH_CASE) |
| 72 |
4/4✓ Branch 0 taken 137292 times.
✓ Branch 1 taken 34287 times.
✓ Branch 2 taken 128786 times.
✓ Branch 3 taken 8506 times.
|
171579 | for (j = 0; s[j] == key[j] && key[j]; j++) |
| 73 | ; | ||
| 74 | else | ||
| 75 |
4/4✓ Branch 0 taken 6376455 times.
✓ Branch 1 taken 1284983 times.
✓ Branch 2 taken 6130921 times.
✓ Branch 3 taken 245534 times.
|
7661438 | for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++) |
| 76 | ; | ||
| 77 |
2/2✓ Branch 0 taken 1314392 times.
✓ Branch 1 taken 258918 times.
|
1573310 | if (key[j]) |
| 78 | 1314392 | continue; | |
| 79 |
4/4✓ Branch 0 taken 4878 times.
✓ Branch 1 taken 254040 times.
✓ Branch 2 taken 4720 times.
✓ Branch 3 taken 158 times.
|
258918 | if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) |
| 80 | 4720 | continue; | |
| 81 | 254198 | return (AVDictionaryEntry *)entry; | |
| 82 | } | ||
| 83 | 1444575 | return NULL; | |
| 84 | } | ||
| 85 | |||
| 86 | 1282679 | int av_dict_set(AVDictionary **pm, const char *key, const char *value, | |
| 87 | int flags) | ||
| 88 | { | ||
| 89 | 1282679 | AVDictionary *m = *pm; | |
| 90 | 1282679 | AVDictionaryEntry *tag = NULL; | |
| 91 | 1282679 | char *copy_key = NULL, *copy_value = NULL; | |
| 92 | int err; | ||
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 64060 times.
✓ Branch 1 taken 1218619 times.
|
1282679 | if (flags & AV_DICT_DONT_STRDUP_VAL) |
| 95 | 64060 | copy_value = (void *)value; | |
| 96 |
2/2✓ Branch 0 taken 514173 times.
✓ Branch 1 taken 704446 times.
|
1218619 | else if (value) |
| 97 | 514173 | copy_value = av_strdup(value); | |
| 98 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1282675 times.
|
1282679 | if (!key) { |
| 99 | 4 | err = AVERROR(EINVAL); | |
| 100 | 4 | goto err_out; | |
| 101 | } | ||
| 102 |
2/2✓ Branch 0 taken 32859 times.
✓ Branch 1 taken 1249816 times.
|
1282675 | if (flags & AV_DICT_DONT_STRDUP_KEY) |
| 103 | 32859 | copy_key = (void *)key; | |
| 104 | else | ||
| 105 | 1249816 | copy_key = av_strdup(key); | |
| 106 |
4/6✓ Branch 0 taken 1282675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 578230 times.
✓ Branch 3 taken 704445 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 578230 times.
|
1282675 | if (!copy_key || (value && !copy_value)) |
| 107 | ✗ | goto enomem; | |
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 1174451 times.
✓ Branch 1 taken 108224 times.
|
1282675 | if (!(flags & AV_DICT_MULTIKEY)) { |
| 110 | 1174451 | tag = av_dict_get(m, key, NULL, flags); | |
| 111 |
2/2✓ Branch 0 taken 5819 times.
✓ Branch 1 taken 102405 times.
|
108224 | } else if (flags & AV_DICT_DEDUP) { |
| 112 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 5811 times.
|
5820 | while ((tag = av_dict_get(m, key, tag, flags))) { |
| 113 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
|
9 | if ((!value && !tag->value) || |
| 114 |
3/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1 times.
|
9 | (value && tag->value && !strcmp(value, tag->value))) { |
| 115 | 8 | av_free(copy_key); | |
| 116 | 8 | av_free(copy_value); | |
| 117 | 8 | return 0; | |
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 |
2/2✓ Branch 0 taken 729340 times.
✓ Branch 1 taken 553327 times.
|
1282667 | if (!m) |
| 122 | 729340 | m = *pm = av_mallocz(sizeof(*m)); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1282667 times.
|
1282667 | if (!m) |
| 124 | ✗ | goto enomem; | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 164906 times.
✓ Branch 1 taken 1117761 times.
|
1282667 | if (tag) { |
| 127 |
2/2✓ Branch 0 taken 788 times.
✓ Branch 1 taken 164118 times.
|
164906 | if (flags & AV_DICT_DONT_OVERWRITE) { |
| 128 | 788 | av_free(copy_key); | |
| 129 | 788 | av_free(copy_value); | |
| 130 | 788 | return 0; | |
| 131 | } | ||
| 132 |
4/4✓ Branch 0 taken 37407 times.
✓ Branch 1 taken 126711 times.
✓ Branch 2 taken 16451 times.
✓ Branch 3 taken 20956 times.
|
164118 | if (copy_value && flags & AV_DICT_APPEND) { |
| 133 | 16451 | size_t oldlen = strlen(tag->value); | |
| 134 | 16451 | size_t new_part_len = strlen(copy_value); | |
| 135 | 16451 | size_t len = oldlen + new_part_len + 1; | |
| 136 | 16451 | char *newval = av_realloc(tag->value, len); | |
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16451 times.
|
16451 | if (!newval) |
| 138 | ✗ | goto enomem; | |
| 139 | 16451 | memcpy(newval + oldlen, copy_value, new_part_len + 1); | |
| 140 | 16451 | av_freep(©_value); | |
| 141 | 16451 | copy_value = newval; | |
| 142 | } else | ||
| 143 | 147667 | av_free(tag->value); | |
| 144 | 164118 | av_free(tag->key); | |
| 145 | 164118 | *tag = m->elems[--m->count]; | |
| 146 |
2/2✓ Branch 0 taken 540027 times.
✓ Branch 1 taken 577734 times.
|
1117761 | } else if (copy_value) { |
| 147 | 540027 | AVDictionaryEntry *tmp = av_realloc_array(m->elems, | |
| 148 | 540027 | m->count + 1, sizeof(*m->elems)); | |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 540027 times.
|
540027 | if (!tmp) |
| 150 | ✗ | goto enomem; | |
| 151 | 540027 | m->elems = tmp; | |
| 152 | } | ||
| 153 |
2/2✓ Branch 0 taken 577434 times.
✓ Branch 1 taken 704445 times.
|
1281879 | if (copy_value) { |
| 154 | 577434 | m->elems[m->count].key = copy_key; | |
| 155 | 577434 | m->elems[m->count].value = copy_value; | |
| 156 | 577434 | m->count++; | |
| 157 | } else { | ||
| 158 | 704445 | err = 0; | |
| 159 | 704445 | goto end; | |
| 160 | } | ||
| 161 | |||
| 162 | 577434 | return 0; | |
| 163 | |||
| 164 | ✗ | enomem: | |
| 165 | ✗ | err = AVERROR(ENOMEM); | |
| 166 | 4 | err_out: | |
| 167 | 4 | av_free(copy_value); | |
| 168 | 704449 | end: | |
| 169 |
4/4✓ Branch 0 taken 704445 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 533352 times.
✓ Branch 3 taken 171093 times.
|
704449 | if (m && !m->count) { |
| 170 | 533352 | av_freep(&m->elems); | |
| 171 | 533352 | av_freep(pm); | |
| 172 | } | ||
| 173 | 704449 | av_free(copy_key); | |
| 174 | 704449 | return err; | |
| 175 | } | ||
| 176 | |||
| 177 | 9233 | int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, | |
| 178 | int flags) | ||
| 179 | { | ||
| 180 | char valuestr[22]; | ||
| 181 | 9233 | snprintf(valuestr, sizeof(valuestr), "%"PRId64, value); | |
| 182 | 9233 | flags &= ~AV_DICT_DONT_STRDUP_VAL; | |
| 183 | 9233 | return av_dict_set(pm, key, valuestr, flags); | |
| 184 | } | ||
| 185 | |||
| 186 | 485 | 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 | 485 | char *key = av_get_token(buf, key_val_sep); | |
| 191 | 485 | char *val = NULL; | |
| 192 | int ret; | ||
| 193 | |||
| 194 |
3/6✓ Branch 0 taken 485 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 485 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 485 times.
✗ Branch 5 not taken.
|
485 | if (key && *key && strspn(*buf, key_val_sep)) { |
| 195 | 485 | (*buf)++; | |
| 196 | 485 | val = av_get_token(buf, pairs_sep); | |
| 197 | } | ||
| 198 | |||
| 199 |
4/8✓ Branch 0 taken 485 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 485 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 485 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 485 times.
✗ Branch 7 not taken.
|
485 | if (key && *key && val && *val) |
| 200 | 485 | ret = av_dict_set(pm, key, val, flags); | |
| 201 | else | ||
| 202 | ✗ | ret = AVERROR(EINVAL); | |
| 203 | |||
| 204 | 485 | av_freep(&key); | |
| 205 | 485 | av_freep(&val); | |
| 206 | |||
| 207 | 485 | return ret; | |
| 208 | } | ||
| 209 | |||
| 210 | 233 | 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 5 times.
✓ Branch 1 taken 228 times.
|
233 | if (!str) |
| 217 | 5 | return 0; | |
| 218 | |||
| 219 | /* ignore STRDUP flags */ | ||
| 220 | 228 | flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
| 221 | |||
| 222 |
2/2✓ Branch 0 taken 485 times.
✓ Branch 1 taken 228 times.
|
941 | while (*str) { |
| 223 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 485 times.
|
485 | 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 226 times.
✓ Branch 1 taken 259 times.
|
485 | if (*str) |
| 227 | 259 | str++; | |
| 228 | } | ||
| 229 | |||
| 230 | 228 | return 0; | |
| 231 | } | ||
| 232 | |||
| 233 | 8215858 | void av_dict_free(AVDictionary **pm) | |
| 234 | { | ||
| 235 | 8215858 | AVDictionary *m = *pm; | |
| 236 | |||
| 237 |
2/2✓ Branch 0 taken 195988 times.
✓ Branch 1 taken 8019870 times.
|
8215858 | if (m) { |
| 238 |
2/2✓ Branch 0 taken 413316 times.
✓ Branch 1 taken 195988 times.
|
609304 | while (m->count--) { |
| 239 | 413316 | av_freep(&m->elems[m->count].key); | |
| 240 | 413316 | av_freep(&m->elems[m->count].value); | |
| 241 | } | ||
| 242 | 195988 | av_freep(&m->elems); | |
| 243 | } | ||
| 244 | 8215858 | av_freep(pm); | |
| 245 | 8215858 | } | |
| 246 | |||
| 247 | 1317687 | int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags) | |
| 248 | { | ||
| 249 | 1317687 | const AVDictionaryEntry *t = NULL; | |
| 250 | |||
| 251 |
2/2✓ Branch 1 taken 54220 times.
✓ Branch 2 taken 1317687 times.
|
1371907 | while ((t = av_dict_iterate(src, t))) { |
| 252 | 54220 | int ret = av_dict_set(dst, t->key, t->value, flags); | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54220 times.
|
54220 | if (ret < 0) |
| 254 | ✗ | return ret; | |
| 255 | } | ||
| 256 | |||
| 257 | 1317687 | return 0; | |
| 258 | } | ||
| 259 | |||
| 260 | 4648 | int av_dict_get_string(const AVDictionary *m, char **buffer, | |
| 261 | const char key_val_sep, const char pairs_sep) | ||
| 262 | { | ||
| 263 | 4648 | const AVDictionaryEntry *t = NULL; | |
| 264 | AVBPrint bprint; | ||
| 265 | 4648 | int cnt = 0; | |
| 266 | 4648 | char special_chars[] = {pairs_sep, key_val_sep, '\0'}; | |
| 267 | |||
| 268 |
4/8✓ Branch 0 taken 4648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4648 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4648 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4648 times.
✗ Branch 7 not taken.
|
4648 | if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep || |
| 269 |
2/4✓ Branch 0 taken 4648 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4648 times.
|
4648 | pairs_sep == '\\' || key_val_sep == '\\') |
| 270 | ✗ | return AVERROR(EINVAL); | |
| 271 | |||
| 272 |
2/2✓ Branch 1 taken 23 times.
✓ Branch 2 taken 4625 times.
|
4648 | if (!av_dict_count(m)) { |
| 273 | 23 | *buffer = av_strdup(""); | |
| 274 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | return *buffer ? 0 : AVERROR(ENOMEM); |
| 275 | } | ||
| 276 | |||
| 277 | 4625 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 278 |
2/2✓ Branch 1 taken 4667 times.
✓ Branch 2 taken 4625 times.
|
9292 | while ((t = av_dict_iterate(m, t))) { |
| 279 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4625 times.
|
4667 | if (cnt++) |
| 280 | 42 | av_bprint_append_data(&bprint, &pairs_sep, 1); | |
| 281 | 4667 | av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 282 | 4667 | av_bprint_append_data(&bprint, &key_val_sep, 1); | |
| 283 | 4667 | av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0); | |
| 284 | } | ||
| 285 | 4625 | return av_bprint_finalize(&bprint, buffer); | |
| 286 | } | ||
| 287 |