chiark / gitweb /
util: truncmsg: New way of manipulating buffers
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 21 Sep 2019 12:19:37 +0000 (13:19 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 24 Oct 2019 18:16:17 +0000 (19:16 +0100)
No callers yet.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
util.c
util.h

diff --git a/util.c b/util.c
index 6f4854f8a010207783b8d41ccb1be8e714b011fe..d0c3d966de4bcd2feb177dcb32300717e3f7b2b1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -361,6 +361,46 @@ void buf_append_string(struct buffer_if *buf, cstring_t s)
     BUF_ADD_BYTES(append,buf,s,len);
 }
 
+void truncmsg_add_string(struct buffer_if *buf, cstring_t s)
+{
+    int32_t l = MIN((int32_t)strlen(s), buf_remaining_space(buf));
+    BUF_ADD_BYTES(append, buf, s, l);
+}
+void truncmsg_add_packet_string(struct buffer_if *buf, int32_t l,
+                               const uint8_t *s)
+{
+    char c;
+    while (l-- > 0) {
+       c = *s++;
+       if (c >= ' ' && c <= 126 && c != '\\' && c != '"' && c != '\'') {
+           if (!buf_remaining_space(buf)) break;
+           buf->start[buf->size++] = c;
+           continue;
+       }
+       char quoted[5];
+       quoted[0] = '\\';
+       quoted[2] = 0;
+       switch (c) {
+       case '\n': quoted[1] = 'n'; break;
+       case '\r': quoted[1] = 'r'; break;
+       case '\t': quoted[1] = 't'; break;
+       case '\\': case '"': case '\'': quoted[1] = c; break;
+       default: sprintf(quoted, "\\x%02x", (unsigned)c);
+       }
+       truncmsg_add_string(buf, quoted);
+    }
+}
+const char *truncmsg_terminate(const struct buffer_if *buf)
+{
+    if (buf_remaining_space(buf)) {
+       buf->start[buf->size] = 0;
+    } else {
+       assert(buf->size >= 4);
+       strcpy(buf->start + buf->size - 4, "...");
+    }
+    return buf->start;
+}
+
 void buffer_new(struct buffer_if *buf, int32_t len)
 {
     buf->free=True;
diff --git a/util.h b/util.h
index 598e2b65c4d55a18d2ea3057bddd9732276fe08b..d783cd076b10c7a70ecd7b215ba2947d61913d08 100644 (file)
--- a/util.h
+++ b/util.h
@@ -49,6 +49,16 @@ extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
 
+/* These construct a message in a buffer, truncating if necessary.
+ * _string is only safe for trusted input and *not* UTF-8 (sorry).
+ * _packet_string is safe for any input, including untrusted.
+ * _terminate arranges for the buffer to be null-terminated (and
+ * maybe for a trailing `...' to indicate truncation), and returns
+ * a pointer to the null-terminated string. */
+void truncmsg_add_string(struct buffer_if *buf, cstring_t s);
+void truncmsg_add_packet_string(struct buffer_if*, int32_t, const uint8_t*);
+const char *truncmsg_terminate(const struct buffer_if *buf);
+
 /*
  * void BUF_ADD_BYTES(append,    struct buffer_if*, const void*, int32_t size);
  * void BUF_ADD_BYTES(prepend,   struct buffer_if*, const void*, int32_t size);