[libvirt] [PATCH 1/3] util: Introduce virBufferEscapeSizedBuf

John Ferlan jferlan at redhat.com
Thu May 12 11:49:30 UTC 2016


Soon we will need to escape a buffer string that cannot use strlen, so
introduce this API to allow printing/escaping of the entire buffer.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virbuffer.c     | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virbuffer.h     |  3 +++
 3 files changed, 58 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a980a32..a0112cd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1215,6 +1215,7 @@ virBufferError;
 virBufferEscape;
 virBufferEscapeSexpr;
 virBufferEscapeShell;
+virBufferEscapeSizedString;
 virBufferEscapeString;
 virBufferFreeAndReset;
 virBufferGetIndent;
diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index d582e7d..6985832 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -588,6 +588,60 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
     VIR_FREE(escaped);
 }
 
+
+/**
+ * virBufferEscapeSizedString:
+ * @buf: the buffer to append to
+ * @escape: the escape character to inject
+ * @toescape: NUL-terminated list of characters to escape
+ * @format: a printf like format string but with only one %s parameter
+ * @bufstr: buffer of bytes which needs to be escaped
+ * @bufstrlen: the length of bufstr
+ *
+ * Do a formatted print with a size to a buffer.  Any characters
+ * in the provided list that are contained in @bufstr are escaped with the
+ * given escape.  Escaping is not applied to characters specified in @format.
+ * Auto indentation may be applied.
+ */
+void
+virBufferEscapeSizedString(virBufferPtr buf,
+                           char escape,
+                           const char *toescape,
+                           const char *format,
+                           const uint8_t *bufstr,
+                           size_t bufstrlen)
+{
+    char *escaped, *out;
+    const uint8_t *cur;
+
+    if ((format == NULL) || (buf == NULL) || (bufstr == NULL))
+        return;
+
+    if (buf->error)
+        return;
+
+    if (xalloc_oversized(2, bufstrlen) ||
+        VIR_ALLOC_N_QUIET(escaped, 2 * bufstrlen + 1) < 0) {
+        virBufferSetError(buf, errno);
+        return;
+    }
+
+    cur = bufstr;
+    out = escaped;
+    while (bufstrlen) {
+        if (strchr(toescape, *cur))
+            *out++ = escape;
+        *out++ = *cur;
+        cur++;
+        bufstrlen--;
+    }
+    *out = 0;
+
+    virBufferAsprintf(buf, format, escaped);
+    VIR_FREE(escaped);
+}
+
+
 /**
  * virBufferURIEncodeString:
  * @buf: the buffer to append to
diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
index 144a1ba..6513d3e 100644
--- a/src/util/virbuffer.h
+++ b/src/util/virbuffer.h
@@ -87,6 +87,9 @@ void virBufferEscapeString(virBufferPtr buf, const char *format,
 void virBufferEscapeSexpr(virBufferPtr buf, const char *format,
                           const char *str);
 void virBufferEscapeShell(virBufferPtr buf, const char *str);
+void virBufferEscapeSizedString(virBufferPtr buf, char escape,
+                                const char *toescape, const char *format,
+                                const uint8_t *bufstr, size_t bufstrlen);
 void virBufferURIEncodeString(virBufferPtr buf, const char *str);
 
 # define virBufferAddLit(buf_, literal_string_) \
-- 
2.5.5




More information about the libvir-list mailing list