about summary refs log tree commit diff
path: root/src/libutil/serialise.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil/serialise.hh')
-rw-r--r--src/libutil/serialise.hh27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index fe3492235e97..c18e82463b5a 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -66,6 +66,33 @@ struct FdSource : Source
 };
 
 
+/* A sink that writes data to a string. */
+struct StringSink : Sink
+{
+    string s;
+    virtual void operator () (const unsigned char * data, unsigned int len)
+    {
+        s.append((const char *) data, len);
+    }
+};
+
+
+/* A source that reads data from a string. */
+struct StringSource : Source
+{
+    string & s;
+    unsigned int pos;
+    StringSource(string & _s) : s(_s), pos(0) { }
+    virtual void operator () (unsigned char * data, unsigned int len)
+    {
+        s.copy((char *) data, len, pos);
+        pos += len;
+        if (pos > s.size())
+            throw Error("end of string reached");
+    }
+};
+
+
 void writePadding(unsigned int len, Sink & sink);
 void writeInt(unsigned int n, Sink & sink);
 void writeString(const string & s, Sink & sink);