]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_utilities/lib/memstreams.h
Security-59306.61.1.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / memstreams.h
index 0c7ac4968c04b03c4b7514d50cf5b18f27e75f39..f3287805cd67c239dd20349341bc9e11b9205e23 100644 (file)
@@ -110,7 +110,7 @@ public:
        size_t operator () (const char *s) { align(); return totalSoFar += strlen(s) + 1; }
        
        void countedData(const void *, size_t length)
-       { insert(sizeof(length)); insert(length); }
+       { insert(sizeof(uint32_t)); insert(length); }
        
        template <class Data>
        void countedData(const Data &data)
@@ -121,6 +121,28 @@ private:
 };
 
 
+// A variant of Writer which returns offsets from the start of the buffer from the operator() functions.
+class OffsetWriter : Writer {
+public:
+    OffsetWriter() { }
+    OffsetWriter(void *base) : Writer(base), basePos(base) { }
+    void operator = (void *base) { basePos = base; Writer::operator = (base); }
+
+    template <class T>
+    T *operator () (const T &obj)
+    { return Writer::operator() (obj) - basePos; }
+
+    void *operator () (const void *addr, size_t size)
+    { return reinterpret_cast<void *>(uintptr_t(Writer::operator() (addr, size)) - uintptr_t(basePos)); }
+
+    char *operator () (const char *s)
+    { return (char *)(*this)(s, strlen(s) + 1); }
+
+private:
+    void *basePos;          // base address
+};
+
+
 //
 // The Reader counter-part for a Writer.
 // Again, Reader and Writer share alignment and representation rules, so what was