diff mbox series

[FFmpeg-devel,v4,2/4] avutil/{avstring, bprint}: add XML attribute value escape modes

Message ID 20210122112058.49068-3-jeebjp@gmail.com
State New
Headers show
Series Initial implementation of TTML encoding/muxing
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Jan Ekström Jan. 22, 2021, 11:20 a.m. UTC
From: Jan Ekström <jan.ekstrom@24i.com>

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
---
 libavutil/avstring.h | 10 ++++++----
 libavutil/bprint.c   | 26 ++++++++++++++++++++++++++
 tools/ffescape.c     | 10 ++++++----
 3 files changed, 38 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/avstring.h b/libavutil/avstring.h
index 189b4726a5..c83aa9cf57 100644
--- a/libavutil/avstring.h
+++ b/libavutil/avstring.h
@@ -321,10 +321,12 @@  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_XML_CHAR_DATA, ///< Use XML non-markup character data 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.
+    AV_ESCAPE_MODE_XML_ATT_VALUE_SINGLE_QUOTED, ///< Use XML single quoted attribute value escaping.
+    AV_ESCAPE_MODE_XML_ATT_VALUE_DOUBLE_QUOTED, ///< Use XML double quoted attribute value escaping.
 };
 
 /**
diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index 7cdbb75095..c42ccdeee5 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -298,6 +298,32 @@  void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_cha
         }
         break;
 
+    case AV_ESCAPE_MODE_XML_ATT_VALUE_SINGLE_QUOTED:
+        /* escape XML single quoted attribute values as per 2.3 */
+        /* "'" ([^<&'] | Reference)* "'" */
+        for (; *src; src++) {
+            switch (*src) {
+            case '&' : av_bprintf(dstbuf, "%s", "&amp;");  break;
+            case '<' : av_bprintf(dstbuf, "%s", "&lt;");   break;
+            case '\'': av_bprintf(dstbuf, "%s", "&apos;"); break;
+            default: av_bprint_chars(dstbuf, *src, 1);
+            }
+        }
+        break;
+
+    case AV_ESCAPE_MODE_XML_ATT_VALUE_DOUBLE_QUOTED:
+        /* escape XML double quoted attribute values as per 2.3 */
+        /* '"' ([^<&"] | Reference)* '"' */
+        for (; *src; src++) {
+            switch (*src) {
+            case '&' : av_bprintf(dstbuf, "%s", "&amp;");  break;
+            case '<' : av_bprintf(dstbuf, "%s", "&lt;");   break;
+            case '"' : av_bprintf(dstbuf, "%s", "&quot;"); break;
+            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 e18f1edaf9..c827e0b301 100644
--- a/tools/ffescape.c
+++ b/tools/ffescape.c
@@ -101,10 +101,12 @@  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;
-            else if (!strcmp(optarg, "xml_char_data")) escape_mode = AV_ESCAPE_MODE_XML_CHAR_DATA;
+            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 if (!strcmp(optarg, "xml_att_value_single_quoted")) escape_mode = AV_ESCAPE_MODE_XML_ATT_VALUE_SINGLE_QUOTED;
+            else if (!strcmp(optarg, "xml_att_value_double_quoted")) escape_mode = AV_ESCAPE_MODE_XML_ATT_VALUE_DOUBLE_QUOTED;
             else {
                 av_log(NULL, AV_LOG_ERROR,
                        "Invalid value '%s' for option -m, "