]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/ipc/TypedMsgHdr.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / TypedMsgHdr.cc
index 622e142dbbbde20cbc6a4a4673f334ea553b36f2..68e011064a96a45259942ae61646098626b9480d 100644 (file)
@@ -5,13 +5,13 @@
  *
  */
 
-
-#include "config.h"
-#include <string.h>
+#include "squid.h"
 #include "protos.h"
 #include "base/TextException.h"
 #include "ipc/TypedMsgHdr.h"
 
+#include <string.h>
+
 Ipc::TypedMsgHdr::TypedMsgHdr()
 {
     xmemset(this, 0, sizeof(*this));
@@ -20,14 +20,14 @@ Ipc::TypedMsgHdr::TypedMsgHdr()
 
 Ipc::TypedMsgHdr::TypedMsgHdr(const TypedMsgHdr &tmh)
 {
-    xmemcpy(this, &tmh, sizeof(*this));
+    memcpy(this, &tmh, sizeof(*this));
     sync();
 }
 
 Ipc::TypedMsgHdr &Ipc::TypedMsgHdr::operator =(const TypedMsgHdr &tmh)
 {
     if (this != &tmh) { // skip assignment to self
-        xmemcpy(this, &tmh, sizeof(*this));
+        memcpy(this, &tmh, sizeof(*this));
         sync();
     }
     return *this;
@@ -57,10 +57,9 @@ void Ipc::TypedMsgHdr::sync()
     } else {
         Must(!msg_controllen && !msg_control);
     }
+    offset = 0;
 }
 
-
-
 int
 Ipc::TypedMsgHdr::type() const
 {
@@ -78,21 +77,96 @@ Ipc::TypedMsgHdr::address(const struct sockaddr_un& addr)
 }
 
 void
-Ipc::TypedMsgHdr::getData(int destType, void *raw, size_t size) const
+Ipc::TypedMsgHdr::checkType(int destType) const
 {
     Must(type() == destType);
-    Must(size == data.size);
-    xmemcpy(raw, data.raw, size);
 }
 
 void
-Ipc::TypedMsgHdr::putData(int aType, const void *raw, size_t size)
+Ipc::TypedMsgHdr::setType(int aType)
 {
-    Must(size <= sizeof(data.raw));
-    allocData();
-    data.type_ = aType;
-    data.size = size;
-    xmemcpy(data.raw, raw, size);
+    if (data.type_) {
+        Must(data.type_ == aType);
+    } else {
+        allocData();
+        data.type_ = aType;
+    }
+}
+
+int
+Ipc::TypedMsgHdr::getInt() const
+{
+    int n = 0;
+    getPod(n);
+    return n;
+}
+
+void
+Ipc::TypedMsgHdr::putInt(const int n)
+{
+    putPod(n);
+}
+
+void
+Ipc::TypedMsgHdr::getString(String &s) const
+{
+    const int length = getInt();
+    Must(length >= 0);
+    // String uses memcpy uncoditionally; TODO: SBuf eliminates this check
+    if (!length) {
+        s.clean();
+        return;
+    }
+
+    Must(length <= maxSize);
+    // TODO: use SBuf.reserve() instead of a temporary buffer
+    char buf[maxSize];
+    getRaw(&buf, length);
+    s.limitInit(buf, length);
+}
+
+void
+Ipc::TypedMsgHdr::putString(const String &s)
+{
+    Must(s.psize() <= maxSize);
+    putInt(s.psize());
+    putRaw(s.rawBuf(), s.psize());
+}
+
+void
+Ipc::TypedMsgHdr::getFixed(void *raw, size_t size) const
+{
+    // no need to load size because it is constant
+    getRaw(raw, size);
+}
+
+void
+Ipc::TypedMsgHdr::putFixed(const void *raw, size_t size)
+{
+    // no need to store size because it is constant
+    putRaw(raw, size);
+}
+
+/// low-level loading of exactly size bytes of raw data
+void
+Ipc::TypedMsgHdr::getRaw(void *raw, size_t size) const
+{
+    if (size > 0) {
+        Must(size <= data.size - offset);
+        memcpy(raw, data.raw + offset, size);
+        offset += size;
+    }
+}
+
+/// low-level storage of exactly size bytes of raw data
+void
+Ipc::TypedMsgHdr::putRaw(const void *raw, size_t size)
+{
+    if (size > 0) {
+        Must(size <= sizeof(data.raw) - data.size);
+        memcpy(data.raw + data.size, raw, size);
+        data.size += size;
+    }
 }
 
 void
@@ -109,7 +183,7 @@ Ipc::TypedMsgHdr::putFd(int fd)
     cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fdCount);
 
     int *fdStore = reinterpret_cast<int*>(CMSG_DATA(cmsg));
-    xmemcpy(fdStore, &fd, fdCount * sizeof(int));
+    memcpy(fdStore, &fd, fdCount * sizeof(int));
     msg_controllen = cmsg->cmsg_len;
 }
 
@@ -125,7 +199,7 @@ Ipc::TypedMsgHdr::getFd() const
     const int fdCount = 1;
     const int *fdStore = reinterpret_cast<const int*>(CMSG_DATA(cmsg));
     int fd = -1;
-    xmemcpy(&fd, fdStore, fdCount * sizeof(int));
+    memcpy(&fd, fdStore, fdCount * sizeof(int));
     return fd;
 }