Blame SOURCES/0020-UTIL-Add-type-specific-getsetters-to-sss_iobuf.patch

ecf709
From cef2ade5294bd9dc06f7eca26287c2e90e2c2ee1 Mon Sep 17 00:00:00 2001
ecf709
From: Jakub Hrozek <jhrozek@redhat.com>
ecf709
Date: Thu, 23 Feb 2017 21:57:13 +0100
ecf709
Subject: [PATCH 20/36] UTIL: Add type-specific getsetters to sss_iobuf
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
The KCM responder receives its input as unstructured data. To make the
ecf709
parsing easier, this commit adds several type-specific getsetters to the
ecf709
iobuf module.
ecf709
ecf709
Reviewed-by: Michal Židek <mzidek@redhat.com>
ecf709
Reviewed-by: Simo Sorce <simo@redhat.com>
ecf709
---
ecf709
 src/util/sss_iobuf.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++
ecf709
 src/util/sss_iobuf.h |  33 ++++++++++++++++
ecf709
 2 files changed, 141 insertions(+)
ecf709
ecf709
diff --git a/src/util/sss_iobuf.c b/src/util/sss_iobuf.c
ecf709
index 900418b750a3455ebc2c3bb1893db726692260b8..fc288d2df2bfaaba393dd490d4da8976de804cb5 100644
ecf709
--- a/src/util/sss_iobuf.c
ecf709
+++ b/src/util/sss_iobuf.c
ecf709
@@ -184,6 +184,25 @@ errno_t sss_iobuf_read(struct sss_iobuf *iobuf,
ecf709
     return EOK;
ecf709
 }
ecf709
 
ecf709
+errno_t sss_iobuf_read_len(struct sss_iobuf *iobuf,
ecf709
+                           size_t len,
ecf709
+                           uint8_t *_buf)
ecf709
+{
ecf709
+    size_t read;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    ret = sss_iobuf_read(iobuf, len, _buf, &read;;
ecf709
+    if (ret != EOK) {
ecf709
+        return ret;
ecf709
+    }
ecf709
+
ecf709
+    if (read != len) {
ecf709
+        return ENOBUFS;
ecf709
+    }
ecf709
+
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
 errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
ecf709
                             uint8_t *buf,
ecf709
                             size_t len)
ecf709
@@ -203,3 +222,92 @@ errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
ecf709
 
ecf709
     return EOK;
ecf709
 }
ecf709
+
ecf709
+errno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf,
ecf709
+                              uint32_t *_val)
ecf709
+{
ecf709
+    SAFEALIGN_COPY_UINT32_CHECK(_val, iobuf_ptr(iobuf),
ecf709
+                                iobuf->capacity, &iobuf->dp);
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+errno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf,
ecf709
+                             int32_t *_val)
ecf709
+{
ecf709
+    SAFEALIGN_COPY_INT32_CHECK(_val, iobuf_ptr(iobuf),
ecf709
+                               iobuf->capacity, &iobuf->dp);
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+errno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf,
ecf709
+                               uint32_t val)
ecf709
+{
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    ret = ensure_bytes(iobuf, sizeof(uint32_t));
ecf709
+    if (ret != EOK) {
ecf709
+        return ret;
ecf709
+    }
ecf709
+
ecf709
+    SAFEALIGN_SETMEM_UINT32(iobuf_ptr(iobuf), val, &iobuf->dp);
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+errno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf,
ecf709
+                              int32_t val)
ecf709
+{
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    ret = ensure_bytes(iobuf, sizeof(int32_t));
ecf709
+    if (ret != EOK) {
ecf709
+        return ret;
ecf709
+    }
ecf709
+
ecf709
+    SAFEALIGN_SETMEM_INT32(iobuf_ptr(iobuf), val, &iobuf->dp);
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+errno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf,
ecf709
+                               const char **_out)
ecf709
+{
ecf709
+    uint8_t *end;
ecf709
+    size_t len;
ecf709
+
ecf709
+    if (iobuf == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    if (_out == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    *_out = NULL;
ecf709
+
ecf709
+    end = memchr(iobuf_ptr(iobuf), '\0', sss_iobuf_get_size(iobuf));
ecf709
+    if (end == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    len = end + 1 - iobuf_ptr(iobuf);
ecf709
+    if (sss_iobuf_get_size(iobuf) < len) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    *_out = (const char *) iobuf_ptr(iobuf);
ecf709
+    iobuf->dp += len;
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+errno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf,
ecf709
+                                const char *str)
ecf709
+{
ecf709
+    if (iobuf == NULL || str == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    SAFEALIGN_MEMCPY_CHECK(iobuf_ptr(iobuf),
ecf709
+                           str, strlen(str)+1,
ecf709
+                           sss_iobuf_get_size(iobuf),
ecf709
+                           &iobuf->dp);
ecf709
+    return EOK;
ecf709
+}
ecf709
diff --git a/src/util/sss_iobuf.h b/src/util/sss_iobuf.h
ecf709
index 900faaa212230f72f52e344c085167e80ae2b465..cc3dfd1e98eeb49b979ac321bd0253bffa8a6dff 100644
ecf709
--- a/src/util/sss_iobuf.h
ecf709
+++ b/src/util/sss_iobuf.h
ecf709
@@ -96,6 +96,22 @@ errno_t sss_iobuf_read(struct sss_iobuf *iobuf,
ecf709
                        size_t *_read);
ecf709
 
ecf709
 /*
ecf709
+ * @brief Read an exact number of bytes from an IO buffer
ecf709
+ *
ecf709
+ * Read exactly len bytes from an IO buffer. If the buffer contains fewer
ecf709
+ * bytes than len, ENOBUFS is returned.
ecf709
+ *
ecf709
+ * @param[in]  iobuf        The IO buffer to read from
ecf709
+ * @param[in]  len          The maximum number of bytes to read
ecf709
+ * @param[out] _buf         The buffer to read data into from iobuf
ecf709
+ *
ecf709
+ * @return EOK on success, errno otherwise
ecf709
+ */
ecf709
+errno_t sss_iobuf_read_len(struct sss_iobuf *iobuf,
ecf709
+                           size_t len,
ecf709
+                           uint8_t *_buf);
ecf709
+
ecf709
+/*
ecf709
  * @brief Write into an IO buffer
ecf709
  *
ecf709
  * Attempts to write len bytes into the iobuf. If the capacity is exceeded,
ecf709
@@ -115,4 +131,21 @@ errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
ecf709
                             uint8_t *buf,
ecf709
                             size_t len);
ecf709
 
ecf709
+errno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf,
ecf709
+                              uint32_t *_val);
ecf709
+
ecf709
+errno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf,
ecf709
+                               uint32_t val);
ecf709
+
ecf709
+errno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf,
ecf709
+                             int32_t *_val);
ecf709
+
ecf709
+errno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf,
ecf709
+                              int32_t val);
ecf709
+
ecf709
+errno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf,
ecf709
+                               const char **_out);
ecf709
+
ecf709
+errno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf,
ecf709
+                                const char *str);
ecf709
 #endif /* __SSS_IOBUF_H_ */
ecf709
-- 
ecf709
2.9.3
ecf709