9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include <talloc.h>
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include "util/util.h"
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include "util/sss_iobuf.h"
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/**
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief The iobuf structure that holds the data, its capacity and
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * a pointer to the data.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @see sss_iobuf_init_empty()
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @see sss_iobuf_init_readonly()
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstruct sss_iobuf {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *data; /* Start of the data buffer */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t dp; /* Data pointer */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t size; /* Current data buffer size */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t capacity; /* Maximum capacity */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek};
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstruct sss_iobuf *sss_iobuf_init_empty(TALLOC_CTX *mem_ctx,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t size,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t capacity)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek struct sss_iobuf *iobuf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *buf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf = talloc_zero(mem_ctx, struct sss_iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return NULL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek buf = talloc_zero_array(iobuf, uint8_t, size);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (buf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek talloc_free(iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return NULL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (capacity == 0) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek capacity = SIZE_MAX / 2;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->data = buf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->size = size;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->capacity = capacity;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->dp = 0;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstruct sss_iobuf *sss_iobuf_init_readonly(TALLOC_CTX *mem_ctx,
3a4a88259ba90d3dc45c1adbbfd39bd7c0204a12Jakub Hrozek const uint8_t *data,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t size)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek struct sss_iobuf *iobuf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf = sss_iobuf_init_empty(mem_ctx, size, size);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return NULL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (data != NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek memcpy(iobuf->data, data, size);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_len(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return 0;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf->dp;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_capacity(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return 0;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf->capacity;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_size(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return 0;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf->size;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekuint8_t *sss_iobuf_get_data(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return NULL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf->data;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstatic size_t iobuf_get_len(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return 0;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return (iobuf->size - iobuf->dp);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstatic errno_t ensure_bytes(struct sss_iobuf *iobuf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t nbytes)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t wantsize;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t newsize;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *newdata;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EINVAL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek wantsize = iobuf->dp + nbytes;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (wantsize <= iobuf->size) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek /* Enough space already */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EOK;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek /* Else, try to extend the iobuf */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (wantsize > iobuf->capacity) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek /* We will never grow past capacity */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return ENOBUFS;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek /* Double the size until we add at least nbytes, but stop if we double past capacity */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek for (newsize = iobuf->size;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek (newsize < wantsize) && (newsize < iobuf->capacity);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek newsize *= 2)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek ;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (newsize > iobuf->capacity) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek newsize = iobuf->capacity;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek newdata = talloc_realloc(iobuf, iobuf->data, uint8_t, newsize);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (newdata == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return ENOMEM;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->data = newdata;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek iobuf->size = newsize;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EOK;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstatic inline uint8_t *iobuf_ptr(struct sss_iobuf *iobuf)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return iobuf->data + iobuf->dp;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekerrno_t sss_iobuf_read(struct sss_iobuf *iobuf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t len,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *_buf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t *_read)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t remaining;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL || _buf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EINVAL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek remaining = iobuf_get_len(iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (len > remaining) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek len = remaining;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek safealign_memcpy(_buf, iobuf_ptr(iobuf), len, &iobuf->dp);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (_read != NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *_read = len;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EOK;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_len(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek size_t len,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint8_t *_buf)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
18e4fe9d836e8f7bee52724374ffc0011172329fPavel Březina size_t read_bytes;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek errno_t ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
18e4fe9d836e8f7bee52724374ffc0011172329fPavel Březina ret = sss_iobuf_read(iobuf, len, _buf, &read_bytes);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (ret != EOK) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
18e4fe9d836e8f7bee52724374ffc0011172329fPavel Březina if (read_bytes != len) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return ENOBUFS;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekerrno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *buf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t len)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek{
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek errno_t ret;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (iobuf == NULL || buf == NULL) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EINVAL;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek ret = ensure_bytes(iobuf, len);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek if (ret != EOK) {
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return ret;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek }
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek safealign_memcpy(iobuf_ptr(iobuf), buf, len, &iobuf->dp);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek return EOK;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint32_t *_val)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek SAFEALIGN_COPY_UINT32_CHECK(_val, iobuf_ptr(iobuf),
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek iobuf->capacity, &iobuf->dp);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek int32_t *_val)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek SAFEALIGN_COPY_INT32_CHECK(_val, iobuf_ptr(iobuf),
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek iobuf->capacity, &iobuf->dp);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint32_t val)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek errno_t ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek ret = ensure_bytes(iobuf, sizeof(uint32_t));
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (ret != EOK) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek SAFEALIGN_SETMEM_UINT32(iobuf_ptr(iobuf), val, &iobuf->dp);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek int32_t val)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek errno_t ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek ret = ensure_bytes(iobuf, sizeof(int32_t));
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (ret != EOK) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return ret;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek SAFEALIGN_SETMEM_INT32(iobuf_ptr(iobuf), val, &iobuf->dp);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek const char **_out)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint8_t *end;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek size_t len;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (iobuf == NULL) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EINVAL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (_out == NULL) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EINVAL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek *_out = NULL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek end = memchr(iobuf_ptr(iobuf), '\0', sss_iobuf_get_size(iobuf));
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (end == NULL) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EINVAL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek len = end + 1 - iobuf_ptr(iobuf);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (sss_iobuf_get_size(iobuf) < len) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EINVAL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek *_out = (const char *) iobuf_ptr(iobuf);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek iobuf->dp += len;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek const char *str)
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek{
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek if (iobuf == NULL || str == NULL) {
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EINVAL;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek }
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek SAFEALIGN_MEMCPY_CHECK(iobuf_ptr(iobuf),
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek str, strlen(str)+1,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek sss_iobuf_get_size(iobuf),
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek &iobuf->dp);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek return EOK;
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek}