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.

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