diff options
author | Jakub Hrozek <jhrozek@redhat.com> | 2017-02-23 21:57:13 +0100 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2017-03-27 09:56:03 +0200 |
commit | 5f7f45a64bdb9353f15b945db4ad2564b4b28ab2 (patch) | |
tree | 28acd798a1095e0c12b3f88cfc9cdb269cd5a957 /src | |
parent | 4f511a4c5f0084e22ce4c7613f1b279533c68cc5 (diff) | |
download | sssd-5f7f45a64bdb9353f15b945db4ad2564b4b28ab2.tar.gz sssd-5f7f45a64bdb9353f15b945db4ad2564b4b28ab2.tar.xz sssd-5f7f45a64bdb9353f15b945db4ad2564b4b28ab2.zip |
UTIL: Add type-specific getsetters to sss_iobuf
The KCM responder receives its input as unstructured data. To make the
parsing easier, this commit adds several type-specific getsetters to the
iobuf module.
Reviewed-by: Michal Židek <mzidek@redhat.com>
Reviewed-by: Simo Sorce <simo@redhat.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/util/sss_iobuf.c | 108 | ||||
-rw-r--r-- | src/util/sss_iobuf.h | 33 |
2 files changed, 141 insertions, 0 deletions
diff --git a/src/util/sss_iobuf.c b/src/util/sss_iobuf.c index 900418b75..fc288d2df 100644 --- a/src/util/sss_iobuf.c +++ b/src/util/sss_iobuf.c @@ -184,6 +184,25 @@ errno_t sss_iobuf_read(struct sss_iobuf *iobuf, return EOK; } +errno_t sss_iobuf_read_len(struct sss_iobuf *iobuf, + size_t len, + uint8_t *_buf) +{ + size_t read; + errno_t ret; + + ret = sss_iobuf_read(iobuf, len, _buf, &read); + if (ret != EOK) { + return ret; + } + + if (read != len) { + return ENOBUFS; + } + + return EOK; +} + errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf, uint8_t *buf, size_t len) @@ -203,3 +222,92 @@ errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf, return EOK; } + +errno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf, + uint32_t *_val) +{ + SAFEALIGN_COPY_UINT32_CHECK(_val, iobuf_ptr(iobuf), + iobuf->capacity, &iobuf->dp); + return EOK; +} + +errno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf, + int32_t *_val) +{ + SAFEALIGN_COPY_INT32_CHECK(_val, iobuf_ptr(iobuf), + iobuf->capacity, &iobuf->dp); + return EOK; +} + +errno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf, + uint32_t val) +{ + errno_t ret; + + ret = ensure_bytes(iobuf, sizeof(uint32_t)); + if (ret != EOK) { + return ret; + } + + SAFEALIGN_SETMEM_UINT32(iobuf_ptr(iobuf), val, &iobuf->dp); + return EOK; +} + +errno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf, + int32_t val) +{ + errno_t ret; + + ret = ensure_bytes(iobuf, sizeof(int32_t)); + if (ret != EOK) { + return ret; + } + + SAFEALIGN_SETMEM_INT32(iobuf_ptr(iobuf), val, &iobuf->dp); + return EOK; +} + +errno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf, + const char **_out) +{ + uint8_t *end; + size_t len; + + if (iobuf == NULL) { + return EINVAL; + } + + if (_out == NULL) { + return EINVAL; + } + + *_out = NULL; + + end = memchr(iobuf_ptr(iobuf), '\0', sss_iobuf_get_size(iobuf)); + if (end == NULL) { + return EINVAL; + } + + len = end + 1 - iobuf_ptr(iobuf); + if (sss_iobuf_get_size(iobuf) < len) { + return EINVAL; + } + + *_out = (const char *) iobuf_ptr(iobuf); + iobuf->dp += len; + return EOK; +} + +errno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf, + const char *str) +{ + if (iobuf == NULL || str == NULL) { + return EINVAL; + } + + SAFEALIGN_MEMCPY_CHECK(iobuf_ptr(iobuf), + str, strlen(str)+1, + sss_iobuf_get_size(iobuf), + &iobuf->dp); + return EOK; +} diff --git a/src/util/sss_iobuf.h b/src/util/sss_iobuf.h index 900faaa21..cc3dfd1e9 100644 --- a/src/util/sss_iobuf.h +++ b/src/util/sss_iobuf.h @@ -96,6 +96,22 @@ errno_t sss_iobuf_read(struct sss_iobuf *iobuf, size_t *_read); /* + * @brief Read an exact number of bytes from an IO buffer + * + * Read exactly len bytes from an IO buffer. If the buffer contains fewer + * bytes than len, ENOBUFS is returned. + * + * @param[in] iobuf The IO buffer to read from + * @param[in] len The maximum number of bytes to read + * @param[out] _buf The buffer to read data into from iobuf + * + * @return EOK on success, errno otherwise + */ +errno_t sss_iobuf_read_len(struct sss_iobuf *iobuf, + size_t len, + uint8_t *_buf); + +/* * @brief Write into an IO buffer * * Attempts to write len bytes into the iobuf. If the capacity is exceeded, @@ -115,4 +131,21 @@ errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf, uint8_t *buf, size_t len); +errno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf, + uint32_t *_val); + +errno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf, + uint32_t val); + +errno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf, + int32_t *_val); + +errno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf, + int32_t val); + +errno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf, + const char **_out); + +errno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf, + const char *str); #endif /* __SSS_IOBUF_H_ */ |