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

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