| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) The FFmpeg developers | ||
| 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 <limits.h> | ||
| 22 | #include <stdarg.h> | ||
| 23 | #include <stdint.h> | ||
| 24 | #include <stdio.h> | ||
| 25 | #include <string.h> | ||
| 26 | |||
| 27 | #include "libavutil/mem.h" | ||
| 28 | #include "libavutil/avassert.h" | ||
| 29 | #include "libavutil/base64.h" | ||
| 30 | #include "libavutil/bprint.h" | ||
| 31 | #include "libavutil/error.h" | ||
| 32 | #include "libavutil/hash.h" | ||
| 33 | #include "libavutil/intreadwrite.h" | ||
| 34 | #include "libavutil/macros.h" | ||
| 35 | #include "libavutil/opt.h" | ||
| 36 | #include "avtextformat.h" | ||
| 37 | |||
| 38 | #define SECTION_ID_NONE (-1) | ||
| 39 | |||
| 40 | #define SHOW_OPTIONAL_FIELDS_AUTO (-1) | ||
| 41 | #define SHOW_OPTIONAL_FIELDS_NEVER 0 | ||
| 42 | #define SHOW_OPTIONAL_FIELDS_ALWAYS 1 | ||
| 43 | |||
| 44 | static const struct { | ||
| 45 | double bin_val; | ||
| 46 | double dec_val; | ||
| 47 | char bin_str[4]; | ||
| 48 | char dec_str[4]; | ||
| 49 | } si_prefixes[] = { | ||
| 50 | { 1.0, 1.0, "", "" }, | ||
| 51 | { 1.024e3, 1e3, "Ki", "K" }, | ||
| 52 | { 1.048576e6, 1e6, "Mi", "M" }, | ||
| 53 | { 1.073741824e9, 1e9, "Gi", "G" }, | ||
| 54 | { 1.099511627776e12, 1e12, "Ti", "T" }, | ||
| 55 | { 1.125899906842624e15, 1e15, "Pi", "P" }, | ||
| 56 | }; | ||
| 57 | |||
| 58 | ✗ | static const char *textcontext_get_formatter_name(void *p) | |
| 59 | { | ||
| 60 | ✗ | AVTextFormatContext *tctx = p; | |
| 61 | ✗ | return tctx->formatter->name; | |
| 62 | } | ||
| 63 | |||
| 64 | #define OFFSET(x) offsetof(AVTextFormatContext, x) | ||
| 65 | |||
| 66 | static const AVOption textcontext_options[] = { | ||
| 67 | { "string_validation", "set string validation mode", | ||
| 68 | OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" }, | ||
| 69 | { "sv", "set string validation mode", | ||
| 70 | OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" }, | ||
| 71 | { "ignore", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_IGNORE }, .unit = "sv" }, | ||
| 72 | { "replace", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, .unit = "sv" }, | ||
| 73 | { "fail", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_FAIL }, .unit = "sv" }, | ||
| 74 | { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "" } }, | ||
| 75 | { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "\xEF\xBF\xBD" } }, | ||
| 76 | { NULL } | ||
| 77 | }; | ||
| 78 | |||
| 79 | 34 | static void *textcontext_child_next(void *obj, void *prev) | |
| 80 | { | ||
| 81 | 34 | AVTextFormatContext *ctx = obj; | |
| 82 |
4/8✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
|
34 | if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv) |
| 83 | 34 | return ctx->priv; | |
| 84 | ✗ | return NULL; | |
| 85 | } | ||
| 86 | |||
| 87 | static const AVClass textcontext_class = { | ||
| 88 | .class_name = "AVTextContext", | ||
| 89 | .item_name = textcontext_get_formatter_name, | ||
| 90 | .option = textcontext_options, | ||
| 91 | .version = LIBAVUTIL_VERSION_INT, | ||
| 92 | .child_next = textcontext_child_next, | ||
| 93 | }; | ||
| 94 | |||
| 95 | ✗ | static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size) | |
| 96 | { | ||
| 97 | ✗ | av_bprintf(bp, "0X"); | |
| 98 | ✗ | for (unsigned i = 0; i < ubuf_size; i++) | |
| 99 | ✗ | av_bprintf(bp, "%02X", ubuf[i]); | |
| 100 | ✗ | } | |
| 101 | |||
| 102 | 198 | int avtext_context_close(AVTextFormatContext **ptctx) | |
| 103 | { | ||
| 104 | 198 | AVTextFormatContext *tctx = *ptctx; | |
| 105 | 198 | int ret = 0; | |
| 106 | |||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (!tctx) |
| 108 | ✗ | return AVERROR(EINVAL); | |
| 109 | |||
| 110 | 198 | av_hash_freep(&tctx->hash); | |
| 111 | |||
| 112 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (tctx->formatter) { |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (tctx->formatter->uninit) |
| 114 | ✗ | ret = tctx->formatter->uninit(tctx); | |
| 115 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (tctx->formatter->priv_class) |
| 116 | 198 | av_opt_free(tctx->priv); | |
| 117 | } | ||
| 118 |
2/2✓ Branch 0 taken 2376 times.
✓ Branch 1 taken 198 times.
|
2574 | for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++) |
| 119 | 2376 | av_bprint_finalize(&tctx->section_pbuf[i], NULL); | |
| 120 | 198 | av_freep(&tctx->priv); | |
| 121 | 198 | av_opt_free(tctx); | |
| 122 | 198 | av_freep(ptctx); | |
| 123 | 198 | return ret; | |
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | 198 | int avtext_context_open(AVTextFormatContext **ptctx, const AVTextFormatter *formatter, AVTextWriterContext *writer_context, const char *args, | |
| 128 | const AVTextFormatSection *sections, int nb_sections, AVTextFormatOptions options, char *show_data_hash) | ||
| 129 | { | ||
| 130 | AVTextFormatContext *tctx; | ||
| 131 | 198 | int ret = 0; | |
| 132 | |||
| 133 |
2/4✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 198 times.
|
198 | av_assert0(ptctx && formatter); |
| 134 | |||
| 135 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
|
198 | if (!(tctx = av_mallocz(sizeof(AVTextFormatContext)))) { |
| 136 | ✗ | ret = AVERROR(ENOMEM); | |
| 137 | ✗ | goto fail; | |
| 138 | } | ||
| 139 | |||
| 140 |
2/2✓ Branch 0 taken 2376 times.
✓ Branch 1 taken 198 times.
|
2574 | for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++) |
| 141 | 2376 | av_bprint_init(&tctx->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED); | |
| 142 | |||
| 143 | 198 | tctx->class = &textcontext_class; | |
| 144 | 198 | av_opt_set_defaults(tctx); | |
| 145 | |||
| 146 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
|
198 | if (!(tctx->priv = av_mallocz(formatter->priv_size))) { |
| 147 | ✗ | ret = AVERROR(ENOMEM); | |
| 148 | ✗ | goto fail; | |
| 149 | } | ||
| 150 | |||
| 151 | 198 | tctx->opts = options; | |
| 152 | |||
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (nb_sections > SECTION_MAX_NB_SECTIONS) { |
| 154 | ✗ | av_log(tctx, AV_LOG_ERROR, "The number of section definitions (%d) is larger than the maximum allowed (%d)\n", nb_sections, SECTION_MAX_NB_SECTIONS); | |
| 155 | ✗ | ret = AVERROR(EINVAL); | |
| 156 | ✗ | goto fail; | |
| 157 | } | ||
| 158 | |||
| 159 | 198 | tctx->formatter = formatter; | |
| 160 | 198 | tctx->level = -1; | |
| 161 | 198 | tctx->sections = sections; | |
| 162 | 198 | tctx->nb_sections = nb_sections; | |
| 163 | 198 | tctx->writer = writer_context; | |
| 164 | |||
| 165 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (formatter->priv_class) { |
| 166 | 198 | void *priv_ctx = tctx->priv; | |
| 167 | 198 | *(const AVClass **)priv_ctx = formatter->priv_class; | |
| 168 | 198 | av_opt_set_defaults(priv_ctx); | |
| 169 | } | ||
| 170 | |||
| 171 | /* convert options to dictionary */ | ||
| 172 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 174 times.
|
198 | if (args) { |
| 173 | 24 | AVDictionary *opts = NULL; | |
| 174 | 24 | const AVDictionaryEntry *opt = NULL; | |
| 175 | |||
| 176 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) { |
| 177 | ✗ | av_log(tctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to textformat context\n", args); | |
| 178 | ✗ | av_dict_free(&opts); | |
| 179 | ✗ | goto fail; | |
| 180 | } | ||
| 181 | |||
| 182 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 2 taken 24 times.
|
58 | while ((opt = av_dict_iterate(opts, opt))) { |
| 183 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
|
34 | if ((ret = av_opt_set(tctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) { |
| 184 | ✗ | av_log(tctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to textformat context\n", | |
| 185 | ✗ | opt->key, opt->value); | |
| 186 | ✗ | av_dict_free(&opts); | |
| 187 | ✗ | goto fail; | |
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | 24 | av_dict_free(&opts); | |
| 192 | } | ||
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 185 times.
|
198 | if (show_data_hash) { |
| 195 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = av_hash_alloc(&tctx->hash, show_data_hash)) < 0) { |
| 196 | ✗ | if (ret == AVERROR(EINVAL)) { | |
| 197 | const char *n; | ||
| 198 | ✗ | av_log(NULL, AV_LOG_ERROR, "Unknown hash algorithm '%s'\nKnown algorithms:", show_data_hash); | |
| 199 | ✗ | for (unsigned i = 0; (n = av_hash_names(i)); i++) | |
| 200 | ✗ | av_log(NULL, AV_LOG_ERROR, " %s", n); | |
| 201 | ✗ | av_log(NULL, AV_LOG_ERROR, "\n"); | |
| 202 | } | ||
| 203 | ✗ | goto fail; | |
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | /* validate replace string */ | ||
| 208 | { | ||
| 209 | 198 | const uint8_t *p = (uint8_t *)tctx->string_validation_replacement; | |
| 210 | 198 | const uint8_t *endp = p + strlen((const char *)p); | |
| 211 |
2/2✓ Branch 0 taken 198 times.
✓ Branch 1 taken 198 times.
|
396 | while (*p) { |
| 212 | 198 | const uint8_t *p0 = p; | |
| 213 | int32_t code; | ||
| 214 | 198 | ret = av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags); | |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (ret < 0) { |
| 216 | AVBPrint bp; | ||
| 217 | ✗ | av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC); | |
| 218 | ✗ | bprint_bytes(&bp, p0, p - p0); | |
| 219 | ✗ | av_log(tctx, AV_LOG_ERROR, | |
| 220 | "Invalid UTF8 sequence %s found in string validation replace '%s'\n", | ||
| 221 | ✗ | bp.str, tctx->string_validation_replacement); | |
| 222 | ✗ | goto fail; | |
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 147 times.
|
198 | if (tctx->formatter->init) |
| 228 | 51 | ret = tctx->formatter->init(tctx); | |
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (ret < 0) |
| 230 | ✗ | goto fail; | |
| 231 | |||
| 232 | 198 | *ptctx = tctx; | |
| 233 | |||
| 234 | 198 | return 0; | |
| 235 | |||
| 236 | ✗ | fail: | |
| 237 | ✗ | avtext_context_close(&tctx); | |
| 238 | ✗ | return ret; | |
| 239 | } | ||
| 240 | |||
| 241 | /* Temporary definitions during refactoring */ | ||
| 242 | static const char unit_second_str[] = "s"; | ||
| 243 | static const char unit_hertz_str[] = "Hz"; | ||
| 244 | static const char unit_byte_str[] = "byte"; | ||
| 245 | static const char unit_bit_per_second_str[] = "bit/s"; | ||
| 246 | |||
| 247 | |||
| 248 | 14148 | void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id) | |
| 249 | { | ||
| 250 |
2/4✓ Branch 0 taken 14148 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14148 times.
|
14148 | if (section_id < 0 || section_id >= tctx->nb_sections) { |
| 251 | ✗ | av_log(tctx, AV_LOG_ERROR, "Invalid section_id for section_header: %d\n", section_id); | |
| 252 | ✗ | return; | |
| 253 | } | ||
| 254 | |||
| 255 | 14148 | tctx->level++; | |
| 256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14148 times.
|
14148 | av_assert0(tctx->level < SECTION_MAX_NB_LEVELS); |
| 257 | |||
| 258 | 14148 | tctx->nb_item[tctx->level] = 0; | |
| 259 | 14148 | memset(tctx->nb_item_type[tctx->level], 0, sizeof(tctx->nb_item_type[tctx->level])); | |
| 260 | 14148 | tctx->section[tctx->level] = &tctx->sections[section_id]; | |
| 261 | |||
| 262 |
1/2✓ Branch 0 taken 14148 times.
✗ Branch 1 not taken.
|
14148 | if (tctx->formatter->print_section_header) |
| 263 | 14148 | tctx->formatter->print_section_header(tctx, data); | |
| 264 | } | ||
| 265 | |||
| 266 | 14148 | void avtext_print_section_footer(AVTextFormatContext *tctx) | |
| 267 | { | ||
| 268 |
2/4✓ Branch 0 taken 14148 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14148 times.
|
14148 | if (tctx->level < 0 || tctx->level >= SECTION_MAX_NB_LEVELS) { |
| 269 | ✗ | av_log(tctx, AV_LOG_ERROR, "Invalid level for section_footer: %d\n", tctx->level); | |
| 270 | ✗ | return; | |
| 271 | } | ||
| 272 | |||
| 273 | 14148 | int section_id = tctx->section[tctx->level]->id; | |
| 274 | 28296 | int parent_section_id = tctx->level ? | |
| 275 |
2/2✓ Branch 0 taken 13950 times.
✓ Branch 1 taken 198 times.
|
14148 | tctx->section[tctx->level - 1]->id : SECTION_ID_NONE; |
| 276 | |||
| 277 |
2/2✓ Branch 0 taken 13950 times.
✓ Branch 1 taken 198 times.
|
14148 | if (parent_section_id != SECTION_ID_NONE) { |
| 278 | 13950 | tctx->nb_item[tctx->level - 1]++; | |
| 279 | 13950 | tctx->nb_item_type[tctx->level - 1][section_id]++; | |
| 280 | } | ||
| 281 | |||
| 282 |
2/2✓ Branch 0 taken 14058 times.
✓ Branch 1 taken 90 times.
|
14148 | if (tctx->formatter->print_section_footer) |
| 283 | 14058 | tctx->formatter->print_section_footer(tctx); | |
| 284 | 14148 | tctx->level--; | |
| 285 | } | ||
| 286 | |||
| 287 | 63286 | void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t val, int flags) | |
| 288 | { | ||
| 289 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63286 times.
|
63286 | av_assert0(tctx); |
| 290 | |||
| 291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63286 times.
|
63286 | if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER) |
| 292 | ✗ | return; | |
| 293 | |||
| 294 |
1/2✓ Branch 0 taken 63286 times.
✗ Branch 1 not taken.
|
63286 | if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO |
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63286 times.
|
63286 | && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL) |
| 296 | ✗ | && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS)) | |
| 297 | ✗ | return; | |
| 298 | |||
| 299 |
3/6✓ Branch 0 taken 63286 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63286 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 63286 times.
|
63286 | av_assert0(key && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS); |
| 300 | |||
| 301 |
3/4✓ Branch 0 taken 63286 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 41535 times.
✓ Branch 4 taken 21751 times.
|
63286 | if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) { |
| 302 | 41535 | tctx->formatter->print_integer(tctx, key, val); | |
| 303 | 41535 | tctx->nb_item[tctx->level]++; | |
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | 6656 | static inline int validate_string(AVTextFormatContext *tctx, char **dstp, const char *src) | |
| 308 | { | ||
| 309 | 6656 | const uint8_t *p, *endp, *srcp = (const uint8_t *)src; | |
| 310 | AVBPrint dstbuf; | ||
| 311 | AVBPrint invalid_seq; | ||
| 312 | 6656 | int invalid_chars_nb = 0, ret = 0; | |
| 313 | |||
| 314 | 6656 | *dstp = NULL; | |
| 315 | 6656 | av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 316 | 6656 | av_bprint_init(&invalid_seq, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 317 | |||
| 318 | 6656 | endp = srcp + strlen(src); | |
| 319 |
2/2✓ Branch 0 taken 225903 times.
✓ Branch 1 taken 6656 times.
|
232559 | for (p = srcp; *p;) { |
| 320 | int32_t code; | ||
| 321 | 225903 | int invalid = 0; | |
| 322 | 225903 | const uint8_t *p0 = p; | |
| 323 | |||
| 324 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 225903 times.
|
225903 | if (av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags) < 0) { |
| 325 | |||
| 326 | ✗ | av_bprint_clear(&invalid_seq); | |
| 327 | |||
| 328 | ✗ | bprint_bytes(&invalid_seq, p0, p - p0); | |
| 329 | |||
| 330 | ✗ | av_log(tctx, AV_LOG_DEBUG, "Invalid UTF-8 sequence '%s' found in string '%s'\n", invalid_seq.str, src); | |
| 331 | ✗ | invalid = 1; | |
| 332 | } | ||
| 333 | |||
| 334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 225903 times.
|
225903 | if (invalid) { |
| 335 | ✗ | invalid_chars_nb++; | |
| 336 | |||
| 337 | ✗ | switch (tctx->string_validation) { | |
| 338 | ✗ | case AV_TEXTFORMAT_STRING_VALIDATION_FAIL: | |
| 339 | ✗ | av_log(tctx, AV_LOG_ERROR, "Invalid UTF-8 sequence found in string '%s'\n", src); | |
| 340 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 341 | ✗ | goto end; | |
| 342 | |||
| 343 | ✗ | case AV_TEXTFORMAT_STRING_VALIDATION_REPLACE: | |
| 344 | ✗ | av_bprintf(&dstbuf, "%s", tctx->string_validation_replacement); | |
| 345 | ✗ | break; | |
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 225903 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
225903 | if (!invalid || tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_IGNORE) |
| 350 | 225903 | av_bprint_append_data(&dstbuf, p0, p-p0); | |
| 351 | } | ||
| 352 | |||
| 353 |
1/4✓ Branch 0 taken 6656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6656 | if (invalid_chars_nb && tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_REPLACE) |
| 354 | ✗ | av_log(tctx, AV_LOG_WARNING, | |
| 355 | "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n", | ||
| 356 | invalid_chars_nb, src, tctx->string_validation_replacement); | ||
| 357 | |||
| 358 | 6656 | end: | |
| 359 | 6656 | av_bprint_finalize(&dstbuf, dstp); | |
| 360 | 6656 | av_bprint_finalize(&invalid_seq, NULL); | |
| 361 | 6656 | return ret; | |
| 362 | } | ||
| 363 | |||
| 364 | struct unit_value { | ||
| 365 | union { | ||
| 366 | double d; | ||
| 367 | int64_t i; | ||
| 368 | } val; | ||
| 369 | |||
| 370 | const char *unit; | ||
| 371 | }; | ||
| 372 | |||
| 373 | 39831 | static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_size, struct unit_value uv) | |
| 374 | { | ||
| 375 | double vald; | ||
| 376 | 39831 | int64_t vali = 0; | |
| 377 | 39831 | int show_float = 0; | |
| 378 | |||
| 379 |
2/2✓ Branch 0 taken 30379 times.
✓ Branch 1 taken 9452 times.
|
39831 | if (uv.unit == unit_second_str) { |
| 380 | 30379 | vald = uv.val.d; | |
| 381 | 30379 | show_float = 1; | |
| 382 | } else { | ||
| 383 | 9452 | vald = (double)uv.val.i; | |
| 384 | 9452 | vali = uv.val.i; | |
| 385 | } | ||
| 386 | |||
| 387 |
3/4✓ Branch 0 taken 30379 times.
✓ Branch 1 taken 9452 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30379 times.
|
39831 | if (uv.unit == unit_second_str && tctx->opts.use_value_sexagesimal_format) { |
| 388 | double secs; | ||
| 389 | int hours, mins; | ||
| 390 | ✗ | secs = vald; | |
| 391 | ✗ | mins = (int)secs / 60; | |
| 392 | ✗ | secs = secs - mins * 60; | |
| 393 | ✗ | hours = mins / 60; | |
| 394 | ✗ | mins %= 60; | |
| 395 | ✗ | snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs); | |
| 396 | } else { | ||
| 397 | 39831 | const char *prefix_string = ""; | |
| 398 | |||
| 399 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 39831 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
39831 | if (tctx->opts.use_value_prefix && vald > 1) { |
| 400 | int64_t index; | ||
| 401 | |||
| 402 | ✗ | if (uv.unit == unit_byte_str && tctx->opts.use_byte_value_binary_prefix) { | |
| 403 | ✗ | index = (int64_t)(log2(vald) / 10); | |
| 404 | ✗ | index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1); | |
| 405 | ✗ | vald /= si_prefixes[index].bin_val; | |
| 406 | ✗ | prefix_string = si_prefixes[index].bin_str; | |
| 407 | } else { | ||
| 408 | ✗ | index = (int64_t)(log10(vald) / 3); | |
| 409 | ✗ | index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1); | |
| 410 | ✗ | vald /= si_prefixes[index].dec_val; | |
| 411 | ✗ | prefix_string = si_prefixes[index].dec_str; | |
| 412 | } | ||
| 413 | ✗ | vali = (int64_t)vald; | |
| 414 | } | ||
| 415 | |||
| 416 |
3/6✓ Branch 0 taken 9452 times.
✓ Branch 1 taken 30379 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9452 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
39831 | if (show_float || (tctx->opts.use_value_prefix && vald != (int64_t)vald)) |
| 417 | 30379 | snprintf(buf, buf_size, "%f", vald); | |
| 418 | else | ||
| 419 | 9452 | snprintf(buf, buf_size, "%"PRId64, vali); | |
| 420 | |||
| 421 |
2/4✓ Branch 0 taken 39831 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39831 times.
|
39831 | av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->opts.show_value_unit ? " " : "", |
| 422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39831 times.
|
39831 | prefix_string, tctx->opts.show_value_unit ? uv.unit : ""); |
| 423 | } | ||
| 424 | |||
| 425 | 39831 | return buf; | |
| 426 | } | ||
| 427 | |||
| 428 | |||
| 429 | 9452 | void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, const char *unit) | |
| 430 | { | ||
| 431 | char val_str[128]; | ||
| 432 | struct unit_value uv; | ||
| 433 | 9452 | uv.val.i = val; | |
| 434 | 9452 | uv.unit = unit; | |
| 435 | 9452 | avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0); | |
| 436 | 9452 | } | |
| 437 | |||
| 438 | |||
| 439 | 91115 | int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags) | |
| 440 | { | ||
| 441 | const AVTextFormatSection *section; | ||
| 442 | 91115 | int ret = 0; | |
| 443 | |||
| 444 |
4/8✓ Branch 0 taken 91115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91115 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91115 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 91115 times.
|
91115 | av_assert0(key && val && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS); |
| 445 | |||
| 446 | 91115 | section = tctx->section[tctx->level]; | |
| 447 | |||
| 448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 91115 times.
|
91115 | if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER) |
| 449 | ✗ | return 0; | |
| 450 | |||
| 451 |
1/2✓ Branch 0 taken 91115 times.
✗ Branch 1 not taken.
|
91115 | if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO |
| 452 |
2/2✓ Branch 0 taken 7749 times.
✓ Branch 1 taken 83366 times.
|
91115 | && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL) |
| 453 |
2/2✓ Branch 0 taken 292 times.
✓ Branch 1 taken 7457 times.
|
7749 | && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS)) |
| 454 | 292 | return 0; | |
| 455 | |||
| 456 |
3/4✓ Branch 0 taken 90823 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 41795 times.
✓ Branch 4 taken 49028 times.
|
90823 | if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) { |
| 457 |
2/2✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 38467 times.
|
41795 | if (flags & AV_TEXTFORMAT_PRINT_STRING_VALIDATE) { |
| 458 | 3328 | char *key1 = NULL, *val1 = NULL; | |
| 459 | 3328 | ret = validate_string(tctx, &key1, key); | |
| 460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3328 times.
|
3328 | if (ret < 0) goto end; |
| 461 | 3328 | ret = validate_string(tctx, &val1, val); | |
| 462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3328 times.
|
3328 | if (ret < 0) goto end; |
| 463 | 3328 | tctx->formatter->print_string(tctx, key1, val1); | |
| 464 | 3328 | end: | |
| 465 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3328 times.
|
3328 | if (ret < 0) |
| 466 | ✗ | av_log(tctx, AV_LOG_ERROR, | |
| 467 | "Invalid key=value string combination %s=%s in section %s\n", | ||
| 468 | ✗ | key, val, section->unique_name); | |
| 469 | 3328 | av_free(key1); | |
| 470 | 3328 | av_free(val1); | |
| 471 | } else { | ||
| 472 | 38467 | tctx->formatter->print_string(tctx, key, val); | |
| 473 | } | ||
| 474 | |||
| 475 | 41795 | tctx->nb_item[tctx->level]++; | |
| 476 | } | ||
| 477 | |||
| 478 | 90823 | return ret; | |
| 479 | } | ||
| 480 | |||
| 481 | 2586 | void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep) | |
| 482 | { | ||
| 483 | char buf[44]; | ||
| 484 | 2586 | snprintf(buf, sizeof(buf), "%d%c%d", q.num, sep, q.den); | |
| 485 | 2586 | avtext_print_string(tctx, key, buf, 0); | |
| 486 | 2586 | } | |
| 487 | |||
| 488 | 31120 | void avtext_print_time(AVTextFormatContext *tctx, const char *key, | |
| 489 | int64_t ts, const AVRational *time_base, int is_duration) | ||
| 490 | { | ||
| 491 |
8/8✓ Branch 0 taken 22095 times.
✓ Branch 1 taken 9025 times.
✓ Branch 2 taken 21358 times.
✓ Branch 3 taken 737 times.
✓ Branch 4 taken 9025 times.
✓ Branch 5 taken 21358 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 9021 times.
|
31120 | if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) { |
| 492 | 741 | avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL); | |
| 493 | } else { | ||
| 494 | char buf[128]; | ||
| 495 | 30379 | double d = av_q2d(*time_base) * ts; | |
| 496 | struct unit_value uv; | ||
| 497 | 30379 | uv.val.d = d; | |
| 498 | 30379 | uv.unit = unit_second_str; | |
| 499 | 30379 | value_string(tctx, buf, sizeof(buf), uv); | |
| 500 | 30379 | avtext_print_string(tctx, key, buf, 0); | |
| 501 | } | ||
| 502 | 31120 | } | |
| 503 | |||
| 504 | 30992 | void avtext_print_ts(AVTextFormatContext *tctx, const char *key, int64_t ts, int is_duration) | |
| 505 | { | ||
| 506 |
8/8✓ Branch 0 taken 21967 times.
✓ Branch 1 taken 9025 times.
✓ Branch 2 taken 21232 times.
✓ Branch 3 taken 735 times.
✓ Branch 4 taken 9025 times.
✓ Branch 5 taken 21232 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 9021 times.
|
30992 | if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) |
| 507 | 739 | avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL); | |
| 508 | else | ||
| 509 | 30253 | avtext_print_integer(tctx, key, ts, 0); | |
| 510 | 30992 | } | |
| 511 | |||
| 512 | ✗ | static void print_data_xxd(AVBPrint *bp, const uint8_t *data, int size) | |
| 513 | { | ||
| 514 | ✗ | unsigned offset = 0; | |
| 515 | int i; | ||
| 516 | |||
| 517 | ✗ | av_bprintf(bp, "\n"); | |
| 518 | ✗ | while (size) { | |
| 519 | ✗ | av_bprintf(bp, "%08x: ", offset); | |
| 520 | ✗ | int l = FFMIN(size, 16); | |
| 521 | ✗ | for (i = 0; i < l; i++) { | |
| 522 | ✗ | av_bprintf(bp, "%02x", data[i]); | |
| 523 | ✗ | if (i & 1) | |
| 524 | ✗ | av_bprintf(bp, " "); | |
| 525 | } | ||
| 526 | ✗ | av_bprint_chars(bp, ' ', 41 - 2 * i - i / 2); | |
| 527 | ✗ | for (i = 0; i < l; i++) | |
| 528 | ✗ | av_bprint_chars(bp, data[i] - 32U < 95 ? data[i] : '.', 1); | |
| 529 | ✗ | av_bprintf(bp, "\n"); | |
| 530 | ✗ | offset += l; | |
| 531 | ✗ | data += l; | |
| 532 | ✗ | size -= l; | |
| 533 | } | ||
| 534 | ✗ | } | |
| 535 | |||
| 536 | ✗ | static void print_data_base64(AVBPrint *bp, const uint8_t *data, int size) | |
| 537 | { | ||
| 538 | char buf[AV_BASE64_SIZE(60)]; | ||
| 539 | |||
| 540 | ✗ | av_bprintf(bp, "\n"); | |
| 541 | ✗ | while (size) { | |
| 542 | ✗ | int l = FFMIN(size, 60); | |
| 543 | ✗ | av_base64_encode(buf, sizeof(buf), data, l); | |
| 544 | ✗ | av_bprintf(bp, "%s\n", buf); | |
| 545 | ✗ | data += l; | |
| 546 | ✗ | size -= l; | |
| 547 | } | ||
| 548 | ✗ | } | |
| 549 | ✗ | void avtext_print_data(AVTextFormatContext *tctx, const char *key, | |
| 550 | const uint8_t *data, int size) | ||
| 551 | { | ||
| 552 | AVBPrint bp; | ||
| 553 | ✗ | av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 554 | ✗ | switch (tctx->opts.data_dump_format) { | |
| 555 | ✗ | case AV_TEXTFORMAT_DATADUMP_XXD: | |
| 556 | ✗ | print_data_xxd(&bp, data, size); | |
| 557 | ✗ | break; | |
| 558 | ✗ | case AV_TEXTFORMAT_DATADUMP_BASE64: | |
| 559 | ✗ | print_data_base64(&bp, data, size); | |
| 560 | ✗ | break; | |
| 561 | ✗ | default: | |
| 562 | ✗ | av_unreachable("Invalid data dump type"); | |
| 563 | } | ||
| 564 | ✗ | avtext_print_string(tctx, key, bp.str, 0); | |
| 565 | ✗ | av_bprint_finalize(&bp, NULL); | |
| 566 | ✗ | } | |
| 567 | |||
| 568 | 6140 | void avtext_print_data_hash(AVTextFormatContext *tctx, const char *key, | |
| 569 | const uint8_t *data, int size) | ||
| 570 | { | ||
| 571 | 6140 | char buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 }; | |
| 572 | int len; | ||
| 573 | |||
| 574 |
2/2✓ Branch 0 taken 4109 times.
✓ Branch 1 taken 2031 times.
|
6140 | if (!tctx->hash) |
| 575 | 4109 | return; | |
| 576 | |||
| 577 | 2031 | av_hash_init(tctx->hash); | |
| 578 | 2031 | av_hash_update(tctx->hash, data, size); | |
| 579 | 2031 | len = snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(tctx->hash)); | |
| 580 | 2031 | av_hash_final_hex(tctx->hash, (uint8_t *)&buf[len], (int)sizeof(buf) - len); | |
| 581 | 2031 | avtext_print_string(tctx, key, buf, 0); | |
| 582 | } | ||
| 583 | |||
| 584 | ✗ | static const char *writercontext_get_writer_name(void *p) | |
| 585 | { | ||
| 586 | ✗ | AVTextWriterContext *wctx = p; | |
| 587 | ✗ | return wctx->writer->name; | |
| 588 | } | ||
| 589 | |||
| 590 | ✗ | static void *writercontext_child_next(void *obj, void *prev) | |
| 591 | { | ||
| 592 | ✗ | AVTextFormatContext *ctx = obj; | |
| 593 | ✗ | if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv) | |
| 594 | ✗ | return ctx->priv; | |
| 595 | ✗ | return NULL; | |
| 596 | } | ||
| 597 | |||
| 598 | static const AVClass textwriter_class = { | ||
| 599 | .class_name = "AVTextWriterContext", | ||
| 600 | .item_name = writercontext_get_writer_name, | ||
| 601 | .version = LIBAVUTIL_VERSION_INT, | ||
| 602 | .child_next = writercontext_child_next, | ||
| 603 | }; | ||
| 604 | |||
| 605 | |||
| 606 | 198 | int avtextwriter_context_close(AVTextWriterContext **pwctx) | |
| 607 | { | ||
| 608 | 198 | AVTextWriterContext *wctx = *pwctx; | |
| 609 | 198 | int ret = 0; | |
| 610 | |||
| 611 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (!wctx) |
| 612 | ✗ | return AVERROR(EINVAL); | |
| 613 | |||
| 614 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (wctx->writer) { |
| 615 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (wctx->writer->uninit) |
| 616 | ✗ | ret = wctx->writer->uninit(wctx); | |
| 617 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (wctx->writer->priv_class) |
| 618 | 198 | av_opt_free(wctx->priv); | |
| 619 | } | ||
| 620 | 198 | av_freep(&wctx->priv); | |
| 621 | 198 | av_freep(pwctx); | |
| 622 | 198 | return ret; | |
| 623 | } | ||
| 624 | |||
| 625 | |||
| 626 | 198 | int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer) | |
| 627 | { | ||
| 628 | AVTextWriterContext *wctx; | ||
| 629 | 198 | int ret = 0; | |
| 630 | |||
| 631 |
2/4✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 198 times.
|
198 | if (!pwctx || !writer) |
| 632 | ✗ | return AVERROR(EINVAL); | |
| 633 | |||
| 634 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
|
198 | if (!((wctx = av_mallocz(sizeof(AVTextWriterContext))))) { |
| 635 | ✗ | ret = AVERROR(ENOMEM); | |
| 636 | ✗ | goto fail; | |
| 637 | } | ||
| 638 | |||
| 639 |
2/4✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 198 times.
|
198 | if (writer->priv_size && !((wctx->priv = av_mallocz(writer->priv_size)))) { |
| 640 | ✗ | ret = AVERROR(ENOMEM); | |
| 641 | ✗ | goto fail; | |
| 642 | } | ||
| 643 | |||
| 644 |
1/2✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
|
198 | if (writer->priv_class) { |
| 645 | 198 | void *priv_ctx = wctx->priv; | |
| 646 | 198 | *(const AVClass **)priv_ctx = writer->priv_class; | |
| 647 | 198 | av_opt_set_defaults(priv_ctx); | |
| 648 | } | ||
| 649 | |||
| 650 | 198 | wctx->class = &textwriter_class; | |
| 651 | 198 | wctx->writer = writer; | |
| 652 | |||
| 653 | 198 | av_opt_set_defaults(wctx); | |
| 654 | |||
| 655 | |||
| 656 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (wctx->writer->init) |
| 657 | ✗ | ret = wctx->writer->init(wctx); | |
| 658 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
|
198 | if (ret < 0) |
| 659 | ✗ | goto fail; | |
| 660 | |||
| 661 | 198 | *pwctx = wctx; | |
| 662 | |||
| 663 | 198 | return 0; | |
| 664 | |||
| 665 | ✗ | fail: | |
| 666 | ✗ | avtextwriter_context_close(&wctx); | |
| 667 | ✗ | return ret; | |
| 668 | } | ||
| 669 | |||
| 670 | static const AVTextFormatter *const registered_formatters[] = | ||
| 671 | { | ||
| 672 | &avtextformatter_default, | ||
| 673 | &avtextformatter_compact, | ||
| 674 | &avtextformatter_csv, | ||
| 675 | &avtextformatter_flat, | ||
| 676 | &avtextformatter_ini, | ||
| 677 | &avtextformatter_json, | ||
| 678 | &avtextformatter_xml, | ||
| 679 | &avtextformatter_mermaid, | ||
| 680 | &avtextformatter_mermaidhtml, | ||
| 681 | NULL | ||
| 682 | }; | ||
| 683 | |||
| 684 | 198 | const AVTextFormatter *avtext_get_formatter_by_name(const char *name) | |
| 685 | { | ||
| 686 |
1/2✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
|
286 | for (int i = 0; registered_formatters[i]; i++) { |
| 687 | const char *end; | ||
| 688 |
2/2✓ Branch 1 taken 198 times.
✓ Branch 2 taken 88 times.
|
286 | if (av_strstart(name, registered_formatters[i]->name, &end) && |
| 689 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
198 | (*end == '\0' || *end == '=')) |
| 690 | 198 | return registered_formatters[i]; | |
| 691 | } | ||
| 692 | |||
| 693 | ✗ | return NULL; | |
| 694 | } | ||
| 695 |