Commit 2f3b028c authored by Stefano Sabatini's avatar Stefano Sabatini

ffprobe: redesign json_escape_str(), to make use of a global buffer

The new code avoids to call av_malloc/av_free when escaping is needed
(possibly faster), avoids an integer overflow in case of a huge
string, and provides feedback when a string cannot be escaped.

When a string cannot be escaped, a special string is printed instead.
parent 284aa079
...@@ -410,39 +410,85 @@ static Writer default_writer = { ...@@ -410,39 +410,85 @@ static Writer default_writer = {
typedef struct { typedef struct {
int multiple_entries; ///< tells if the given chapter requires multiple entries int multiple_entries; ///< tells if the given chapter requires multiple entries
char *buf;
size_t buf_size;
} JSONContext; } JSONContext;
static char *json_escape_str(const char *s) #define ESCAPE_INIT_BUF_SIZE 256
static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
{
JSONContext *json = wctx->priv;
json->buf_size = ESCAPE_INIT_BUF_SIZE;
if (!(json->buf = av_malloc(json->buf_size)))
return AVERROR(ENOMEM);
return 0;
}
static av_cold void json_uninit(WriterContext *wctx)
{
JSONContext *json = wctx->priv;
av_freep(&json->buf);
}
#define ESCAPE_CHECK_SIZE(src, size, max_size) \
if (size > max_size) { \
char buf[64]; \
snprintf(buf, sizeof(buf), "%s", src); \
av_log(log_ctx, AV_LOG_WARNING, \
"String '%s...' with is too big\n", buf); \
return "FFPROBE_TOO_BIG_STRING"; \
}
#define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
if (*dst_size_p < size) { \
char *q = av_realloc(*dst_p, size); \
if (!q) { \
char buf[64]; \
snprintf(buf, sizeof(buf), "%s", src); \
av_log(log_ctx, AV_LOG_WARNING, \
"String '%s...' could not be escaped\n", buf); \
return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
} \
*dst_size_p = size; \
*dst = q; \
}
static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
void *log_ctx)
{ {
static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0}; static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0}; static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
char *ret, *p; const char *p;
int i, len = 0; char *q;
size_t size = 1;
// compute the length of the escaped string // compute the length of the escaped string
for (i = 0; s[i]; i++) { for (p = src; *p; p++) {
if (strchr(json_escape, s[i])) len += 2; // simple escape ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
else if ((unsigned char)s[i] < 32) len += 6; // handle non-printable chars if (strchr(json_escape, *p)) size += 2; // simple escape
else len += 1; // char copy else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
else size += 1; // char copy
} }
ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
p = ret = av_malloc(len + 1);
if (!p) q = *dst;
return NULL; for (p = src; *p; p++) {
for (i = 0; s[i]; i++) { char *s = strchr(json_escape, *p);
char *q = strchr(json_escape, s[i]); if (s) {
if (q) { *q++ = '\\';
*p++ = '\\'; *q++ = json_subst[s - json_escape];
*p++ = json_subst[q - json_escape]; } else if ((unsigned char)*p < 32) {
} else if ((unsigned char)s[i] < 32) { snprintf(q, 7, "\\u00%02x", *p & 0xff);
snprintf(p, 7, "\\u00%02x", s[i] & 0xff); q += 6;
p += 6;
} else { } else {
*p++ = s[i]; *q++ = *p;
} }
} }
*p = 0; *q = 0;
return ret; return *dst;
} }
static void json_print_header(WriterContext *wctx) static void json_print_header(WriterContext *wctx)
...@@ -458,15 +504,12 @@ static void json_print_footer(WriterContext *wctx) ...@@ -458,15 +504,12 @@ static void json_print_footer(WriterContext *wctx)
static void json_print_chapter_header(WriterContext *wctx, const char *chapter) static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
{ {
JSONContext *json = wctx->priv; JSONContext *json = wctx->priv;
char *chapter_esc;
if (wctx->nb_chapter) if (wctx->nb_chapter)
printf(","); printf(",");
json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams"); json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
chapter_esc = json_escape_str(chapter); printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
printf("\n \"%s\":%s", chapter_esc ? chapter_esc : "",
json->multiple_entries ? " [" : " "); json->multiple_entries ? " [" : " ");
av_free(chapter_esc);
} }
static void json_print_chapter_footer(WriterContext *wctx, const char *chapter) static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
...@@ -492,14 +535,10 @@ static inline void json_print_item_str(WriterContext *wctx, ...@@ -492,14 +535,10 @@ static inline void json_print_item_str(WriterContext *wctx,
const char *key, const char *value, const char *key, const char *value,
const char *indent) const char *indent)
{ {
char *key_esc = json_escape_str(key); JSONContext *json = wctx->priv;
char *value_esc = json_escape_str(value);
printf("%s\"%s\": \"%s\"", indent, printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
key_esc ? key_esc : "", printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
value_esc ? value_esc : "");
av_free(key_esc);
av_free(value_esc);
} }
#define INDENT " " #define INDENT " "
...@@ -512,11 +551,11 @@ static void json_print_str(WriterContext *wctx, const char *key, const char *val ...@@ -512,11 +551,11 @@ static void json_print_str(WriterContext *wctx, const char *key, const char *val
static void json_print_int(WriterContext *wctx, const char *key, int value) static void json_print_int(WriterContext *wctx, const char *key, int value)
{ {
char *key_esc = json_escape_str(key); JSONContext *json = wctx->priv;
if (wctx->nb_item) printf(",\n"); if (wctx->nb_item) printf(",\n");
printf(INDENT "\"%s\": %d", key_esc ? key_esc : "", value); printf(INDENT "\"%s\": %d",
av_free(key_esc); json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
} }
static void json_show_tags(WriterContext *wctx, AVDictionary *dict) static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
...@@ -538,6 +577,8 @@ static Writer json_writer = { ...@@ -538,6 +577,8 @@ static Writer json_writer = {
.name = "json", .name = "json",
.priv_size = sizeof(JSONContext), .priv_size = sizeof(JSONContext),
.init = json_init,
.uninit = json_uninit,
.print_header = json_print_header, .print_header = json_print_header,
.print_footer = json_print_footer, .print_footer = json_print_footer,
.print_chapter_header = json_print_chapter_header, .print_chapter_header = json_print_chapter_header,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment