[FFmpeg-devel] [PATCH v4 1/4] avutil/{avstring, bprint}: add XML escaping from ffprobe to avutil

Nicolas George george at nsup.org
Mon Jan 25 11:57:50 EET 2021


Jan Ekström (12021-01-22):
> From: Stefano Sabatini <stefasab at gmail.com>
> 
> ---
>  libavutil/avstring.h |  7 ++++---
>  libavutil/bprint.c   | 15 +++++++++++++++
>  tools/ffescape.c     |  7 ++++---
>  3 files changed, 23 insertions(+), 6 deletions(-)

Thanks for this new version.

I think this patch and the next would be better merged.

> 
> diff --git a/libavutil/avstring.h b/libavutil/avstring.h
> index ee225585b3..189b4726a5 100644
> --- a/libavutil/avstring.h
> +++ b/libavutil/avstring.h
> @@ -321,9 +321,10 @@ int av_match_name(const char *name, const char *names);
>  char *av_append_path_component(const char *path, const char *component);
>  
>  enum AVEscapeMode {
> -    AV_ESCAPE_MODE_AUTO,      ///< Use auto-selected escaping mode.
> -    AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
> -    AV_ESCAPE_MODE_QUOTE,     ///< Use single-quote escaping.
> +    AV_ESCAPE_MODE_AUTO,          ///< Use auto-selected escaping mode.
> +    AV_ESCAPE_MODE_BACKSLASH,     ///< Use backslash escaping.
> +    AV_ESCAPE_MODE_QUOTE,         ///< Use single-quote escaping.

> +    AV_ESCAPE_MODE_XML_CHAR_DATA, ///< Use XML non-markup character data escaping.

It could be shorter: XML_TEXT, XML_ATTR_QUOT and XML_ATTR_APOS are clear
enough IMHO, and more convenient.

>  };
>  
>  /**
> diff --git a/libavutil/bprint.c b/libavutil/bprint.c
> index 2f059c5ba6..7cdbb75095 100644
> --- a/libavutil/bprint.c
> +++ b/libavutil/bprint.c
> @@ -283,6 +283,21 @@ void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_cha
>          av_bprint_chars(dstbuf, '\'', 1);
>          break;
>  
> +    case AV_ESCAPE_MODE_XML_CHAR_DATA:
> +        /* escape XML non-markup character data as per 2.4 */
> +        /*  [^<&]* - ([^<&]* ']]>' [^<&]*) */
> +        for (; *src; src++) {
> +            switch (*src) {
> +            case '&' : av_bprintf(dstbuf, "%s", "&");  break;
> +            case '<' : av_bprintf(dstbuf, "%s", "<");   break;
> +            case '>' : av_bprintf(dstbuf, "%s", ">");   break;

> +            case '"' : av_bprintf(dstbuf, "%s", """); break;
> +            case '\'': av_bprintf(dstbuf, "%s", "'"); break;

Outside attributes, " and ' do not need to be quoted.

> +            default: av_bprint_chars(dstbuf, *src, 1);
> +            }
> +        }
> +        break;
> +
>      /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
>      default:
>          /* \-escape characters */
> diff --git a/tools/ffescape.c b/tools/ffescape.c
> index 0530d28c6d..e18f1edaf9 100644
> --- a/tools/ffescape.c
> +++ b/tools/ffescape.c
> @@ -101,9 +101,10 @@ int main(int argc, char **argv)
>              break;
>          }
>          case 'm':
> -            if      (!strcmp(optarg, "auto"))      escape_mode = AV_ESCAPE_MODE_AUTO;
> -            else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
> -            else if (!strcmp(optarg, "quote"))     escape_mode = AV_ESCAPE_MODE_QUOTE;
> +            if      (!strcmp(optarg, "auto"))          escape_mode = AV_ESCAPE_MODE_AUTO;
> +            else if (!strcmp(optarg, "backslash"))     escape_mode = AV_ESCAPE_MODE_BACKSLASH;
> +            else if (!strcmp(optarg, "quote"))         escape_mode = AV_ESCAPE_MODE_QUOTE;

> +            else if (!strcmp(optarg, "xml_char_data")) escape_mode = AV_ESCAPE_MODE_XML_CHAR_DATA;
>              else {
>                  av_log(NULL, AV_LOG_ERROR,
>                         "Invalid value '%s' for option -m, "

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20210125/c1d03bb5/attachment.sig>


More information about the ffmpeg-devel mailing list