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

Jan Ekström jeebjp at gmail.com
Fri Jan 22 13:20:56 EET 2021


From: Jan Ekström <jan.ekstrom at 24i.com>

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

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", "&");  break;
+            case '<' : av_bprintf(dstbuf, "%s", "<");   break;
+            case '\'': av_bprintf(dstbuf, "%s", "'"); 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", "&");  break;
+            case '<' : av_bprintf(dstbuf, "%s", "<");   break;
+            case '"' : av_bprintf(dstbuf, "%s", """); 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, "
-- 
2.29.2



More information about the ffmpeg-devel mailing list