[vlc-commits] strings: use vlc_memstream in vlc_xml_encode()

Rémi Denis-Courmont git at videolan.org
Fri Feb 24 21:01:51 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Feb 24 22:01:38 2017 +0200| [7641dfe3eabcd7c3f1c2ae56cac3ed98b099d892] | committer: Rémi Denis-Courmont

strings: use vlc_memstream in vlc_xml_encode()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=7641dfe3eabcd7c3f1c2ae56cac3ed98b099d892
---

 src/text/strings.c | 45 ++++++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/text/strings.c b/src/text/strings.c
index edc8834..45d72a1 100644
--- a/src/text/strings.c
+++ b/src/text/strings.c
@@ -284,38 +284,46 @@ void vlc_xml_decode( char *psz_value )
 
 char *vlc_xml_encode (const char *str)
 {
-    assert (str != NULL);
-
-    const size_t len = strlen (str);
-    char *const buf = malloc (6 * len + 1), *ptr = buf;
-    if (unlikely(buf == NULL))
-        return NULL;
-
+    struct vlc_memstream stream;
     size_t n;
     uint32_t cp;
 
+    assert(str != NULL);
+    vlc_memstream_open(&stream);
+
     while ((n = vlc_towc (str, &cp)) != 0)
     {
         if (unlikely(n == (size_t)-1))
         {
-            free (buf);
+            if (vlc_memstream_close(&stream))
+                free(stream.ptr);
             errno = EILSEQ;
             return NULL;
         }
 
         switch (cp)
         {
-            case '\"': strcpy (ptr, """); ptr += 6; break;
-            case '&':  strcpy (ptr, "&");  ptr += 5; break;
-            case '\'': strcpy (ptr, "'");  ptr += 5; break;
-            case '<':  strcpy (ptr, "<");   ptr += 4; break;
-            case '>':  strcpy (ptr, ">");   ptr += 4; break;
+            case '\"':
+                vlc_memstream_puts(&stream, """);
+                break;
+            case '&':
+                vlc_memstream_puts(&stream, "&");
+                break;
+            case '\'':
+                vlc_memstream_puts(&stream, "'");
+                break;
+            case '<':
+                vlc_memstream_puts(&stream, "<");
+                break;
+            case '>':
+                vlc_memstream_puts(&stream, ">");
+                break;
             default:
                 if (cp < 32) /* C0 code not allowed (except 9, 10 and 13) */
                     break;
                 if (cp >= 128 && cp < 160) /* C1 code encoded (except 133) */
                 {
-                    ptr += sprintf (ptr, "&#%"PRIu32";", cp);
+                    vlc_memstream_printf(&stream, "&#%"PRIu32";", cp);
                     break;
                 }
                 /* fall through */
@@ -323,16 +331,15 @@ char *vlc_xml_encode (const char *str)
             case 10:
             case 13:
             case 133:
-                memcpy (ptr, str, n);
-                ptr += n;
+                vlc_memstream_write(&stream, str, n);
                 break;
         }
         str += n;
     }
-    *(ptr++) = '\0';
 
-    ptr = realloc (buf, ptr - buf);
-    return likely(ptr != NULL) ? ptr : buf; /* cannot fail */
+    if (vlc_memstream_close(&stream))
+        return NULL;
+    return stream.ptr;
 }
 
 /* Base64 encoding */



More information about the vlc-commits mailing list