You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
11KB

  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. #include <string.h>
  21. #include "avstring.h"
  22. #include "dict.h"
  23. #include "internal.h"
  24. #include "mem.h"
  25. #include "bprint.h"
  26. struct AVDictionary {
  27. int count;
  28. AVDictionaryEntry *elems;
  29. };
  30. int av_dict_count(const AVDictionary *m)
  31. {
  32. return m ? m->count : 0;
  33. }
  34. AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
  35. const AVDictionaryEntry *prev, int flags)
  36. {
  37. unsigned int i, j;
  38. if (!m)
  39. return NULL;
  40. if (prev)
  41. i = prev - m->elems + 1;
  42. else
  43. i = 0;
  44. for (; i < m->count; i++) {
  45. const char *s = m->elems[i].key;
  46. if (flags & AV_DICT_MATCH_CASE)
  47. for (j = 0; s[j] == key[j] && key[j]; j++)
  48. ;
  49. else
  50. for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
  51. ;
  52. if (key[j])
  53. continue;
  54. if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
  55. continue;
  56. return &m->elems[i];
  57. }
  58. return NULL;
  59. }
  60. int av_dict_set(AVDictionary **pm, const char *key, const char *value,
  61. int flags)
  62. {
  63. AVDictionary *m = *pm;
  64. AVDictionaryEntry *tag = NULL;
  65. char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
  66. if (!(flags & AV_DICT_MULTIKEY)) {
  67. tag = av_dict_get(m, key, NULL, flags);
  68. }
  69. if (flags & AV_DICT_DONT_STRDUP_KEY)
  70. copy_key = (void *)key;
  71. else
  72. copy_key = av_strdup(key);
  73. if (flags & AV_DICT_DONT_STRDUP_VAL)
  74. copy_value = (void *)value;
  75. else if (copy_key)
  76. copy_value = av_strdup(value);
  77. if (!m)
  78. m = *pm = av_mallocz(sizeof(*m));
  79. if (!m || (key && !copy_key) || (value && !copy_value))
  80. goto err_out;
  81. if (tag) {
  82. if (flags & AV_DICT_DONT_OVERWRITE) {
  83. av_free(copy_key);
  84. av_free(copy_value);
  85. return 0;
  86. }
  87. if (flags & AV_DICT_APPEND)
  88. oldval = tag->value;
  89. else
  90. av_free(tag->value);
  91. av_free(tag->key);
  92. *tag = m->elems[--m->count];
  93. } else if (copy_value) {
  94. AVDictionaryEntry *tmp = av_realloc(m->elems,
  95. (m->count + 1) * sizeof(*m->elems));
  96. if (!tmp)
  97. goto err_out;
  98. m->elems = tmp;
  99. }
  100. if (copy_value) {
  101. m->elems[m->count].key = copy_key;
  102. m->elems[m->count].value = copy_value;
  103. if (oldval && flags & AV_DICT_APPEND) {
  104. size_t len = strlen(oldval) + strlen(copy_value) + 1;
  105. char *newval = av_mallocz(len);
  106. if (!newval)
  107. goto err_out;
  108. av_strlcat(newval, oldval, len);
  109. av_freep(&oldval);
  110. av_strlcat(newval, copy_value, len);
  111. m->elems[m->count].value = newval;
  112. av_freep(&copy_value);
  113. }
  114. m->count++;
  115. } else {
  116. av_freep(&copy_key);
  117. }
  118. if (!m->count) {
  119. av_freep(&m->elems);
  120. av_freep(pm);
  121. }
  122. return 0;
  123. err_out:
  124. if (m && !m->count) {
  125. av_freep(&m->elems);
  126. av_freep(pm);
  127. }
  128. av_free(copy_key);
  129. av_free(copy_value);
  130. return AVERROR(ENOMEM);
  131. }
  132. int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
  133. int flags)
  134. {
  135. char valuestr[22];
  136. snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
  137. flags &= ~AV_DICT_DONT_STRDUP_VAL;
  138. return av_dict_set(pm, key, valuestr, flags);
  139. }
  140. static int parse_key_value_pair(AVDictionary **pm, const char **buf,
  141. const char *key_val_sep, const char *pairs_sep,
  142. int flags)
  143. {
  144. char *key = av_get_token(buf, key_val_sep);
  145. char *val = NULL;
  146. int ret;
  147. if (key && *key && strspn(*buf, key_val_sep)) {
  148. (*buf)++;
  149. val = av_get_token(buf, pairs_sep);
  150. }
  151. if (key && *key && val && *val)
  152. ret = av_dict_set(pm, key, val, flags);
  153. else
  154. ret = AVERROR(EINVAL);
  155. av_freep(&key);
  156. av_freep(&val);
  157. return ret;
  158. }
  159. int av_dict_parse_string(AVDictionary **pm, const char *str,
  160. const char *key_val_sep, const char *pairs_sep,
  161. int flags)
  162. {
  163. int ret;
  164. if (!str)
  165. return 0;
  166. /* ignore STRDUP flags */
  167. flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  168. while (*str) {
  169. if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
  170. return ret;
  171. if (*str)
  172. str++;
  173. }
  174. return 0;
  175. }
  176. void av_dict_free(AVDictionary **pm)
  177. {
  178. AVDictionary *m = *pm;
  179. if (m) {
  180. while (m->count--) {
  181. av_freep(&m->elems[m->count].key);
  182. av_freep(&m->elems[m->count].value);
  183. }
  184. av_freep(&m->elems);
  185. }
  186. av_freep(pm);
  187. }
  188. int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
  189. {
  190. AVDictionaryEntry *t = NULL;
  191. while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
  192. int ret = av_dict_set(dst, t->key, t->value, flags);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. int av_dict_get_string(const AVDictionary *m, char **buffer,
  199. const char key_val_sep, const char pairs_sep)
  200. {
  201. AVDictionaryEntry *t = NULL;
  202. AVBPrint bprint;
  203. int cnt = 0;
  204. char special_chars[] = {pairs_sep, key_val_sep, '\0'};
  205. if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
  206. pairs_sep == '\\' || key_val_sep == '\\')
  207. return AVERROR(EINVAL);
  208. if (!av_dict_count(m)) {
  209. *buffer = av_strdup("");
  210. return *buffer ? 0 : AVERROR(ENOMEM);
  211. }
  212. av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
  213. while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
  214. if (cnt++)
  215. av_bprint_append_data(&bprint, &pairs_sep, 1);
  216. av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  217. av_bprint_append_data(&bprint, &key_val_sep, 1);
  218. av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
  219. }
  220. return av_bprint_finalize(&bprint, buffer);
  221. }
  222. #ifdef TEST
  223. static void print_dict(const AVDictionary *m)
  224. {
  225. AVDictionaryEntry *t = NULL;
  226. while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
  227. printf("%s %s ", t->key, t->value);
  228. printf("\n");
  229. }
  230. static void test_separators(const AVDictionary *m, const char pair, const char val)
  231. {
  232. AVDictionary *dict = NULL;
  233. char pairs[] = {pair , '\0'};
  234. char vals[] = {val, '\0'};
  235. char *buffer = NULL;
  236. av_dict_copy(&dict, m, 0);
  237. print_dict(dict);
  238. av_dict_get_string(dict, &buffer, val, pair);
  239. printf("%s\n", buffer);
  240. av_dict_free(&dict);
  241. av_dict_parse_string(&dict, buffer, vals, pairs, 0);
  242. av_freep(&buffer);
  243. print_dict(dict);
  244. av_dict_free(&dict);
  245. }
  246. int main(void)
  247. {
  248. AVDictionary *dict = NULL;
  249. AVDictionaryEntry *e;
  250. char *buffer = NULL;
  251. printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
  252. av_dict_get_string(dict, &buffer, '=', ',');
  253. printf("%s\n", buffer);
  254. av_freep(&buffer);
  255. av_dict_set(&dict, "aaa", "aaa", 0);
  256. av_dict_set(&dict, "b,b", "bbb", 0);
  257. av_dict_set(&dict, "c=c", "ccc", 0);
  258. av_dict_set(&dict, "ddd", "d,d", 0);
  259. av_dict_set(&dict, "eee", "e=e", 0);
  260. av_dict_set(&dict, "f,f", "f=f", 0);
  261. av_dict_set(&dict, "g=g", "g,g", 0);
  262. test_separators(dict, ',', '=');
  263. av_dict_free(&dict);
  264. av_dict_set(&dict, "aaa", "aaa", 0);
  265. av_dict_set(&dict, "bbb", "bbb", 0);
  266. av_dict_set(&dict, "ccc", "ccc", 0);
  267. av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
  268. test_separators(dict, '"', '=');
  269. test_separators(dict, '\'', '=');
  270. test_separators(dict, ',', '"');
  271. test_separators(dict, ',', '\'');
  272. test_separators(dict, '\'', '"');
  273. test_separators(dict, '"', '\'');
  274. av_dict_free(&dict);
  275. printf("\nTesting av_dict_set()\n");
  276. av_dict_set(&dict, "a", "a", 0);
  277. av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
  278. av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
  279. av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  280. av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
  281. av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
  282. av_dict_set(&dict, "f", "f", 0);
  283. av_dict_set(&dict, "f", NULL, 0);
  284. av_dict_set(&dict, "ff", "f", 0);
  285. av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
  286. e = NULL;
  287. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  288. printf("%s %s\n", e->key, e->value);
  289. av_dict_free(&dict);
  290. av_dict_set(&dict, NULL, "a", 0);
  291. av_dict_set(&dict, NULL, "b", 0);
  292. av_dict_get(dict, NULL, NULL, 0);
  293. e = NULL;
  294. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  295. printf("'%s' '%s'\n", e->key, e->value);
  296. av_dict_free(&dict);
  297. //valgrind sensible test
  298. printf("\nTesting av_dict_set_int()\n");
  299. av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
  300. av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
  301. av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  302. av_dict_set_int(&dict, "4", 4, 0);
  303. av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
  304. av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
  305. av_dict_set_int(&dict, "12", 1, 0);
  306. av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
  307. e = NULL;
  308. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
  309. printf("%s %s\n", e->key, e->value);
  310. av_dict_free(&dict);
  311. //valgrind sensible test
  312. printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
  313. av_dict_set(&dict, "key", "old", 0);
  314. e = av_dict_get(dict, "key", NULL, 0);
  315. av_dict_set(&dict, e->key, "new val OK", 0);
  316. e = av_dict_get(dict, "key", NULL, 0);
  317. printf("%s\n", e->value);
  318. av_dict_set(&dict, e->key, e->value, 0);
  319. e = av_dict_get(dict, "key", NULL, 0);
  320. printf("%s\n", e->value);
  321. av_dict_free(&dict);
  322. return 0;
  323. }
  324. #endif