9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#ifndef __SSS_IOBUF_H_
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#define __SSS_IOBUF_H_
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include <talloc.h>
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include <stdint.h>
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include <errno.h>
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#include "util/util_errors.h"
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekstruct sss_iobuf;
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Allocate an empty IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] mem_ctx The talloc context that owns the iobuf
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * When this buffer is written into, but the capacity is exceeded, the write
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * function will return an error.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] mem_ctx The talloc context that owns the iobuf
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] size The size of the data buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] capacity The maximum capacity the buffer can grow into.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * Use 0 for an 'unlimited' buffer that will grow
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * until SIZE_MAX/2.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return The newly created buffer on success or NULL on an error.
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/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Allocate an IO buffer with a fixed size
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * This function is useful for parsing an input buffer from an existing
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * buffer pointed to by data.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * The iobuf does not assume ownership of the data buffer in talloc terms,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * but copies the data instead.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] mem_ctx The talloc context that owns the iobuf
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] data The data to initialize the IO buffer with. This
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * data is copied into the iobuf-owned buffer.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] size The size of the data buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return The newly created buffer on success or NULL on an error.
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/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Returns the number of bytes currently stored in the iobuf
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return The number of bytes (the data pointer offset)
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_len(struct sss_iobuf *iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Returns the capacity of the IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return The capacity of the IO buffer. Returns zero
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * for an unlimited buffer.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_capacity(struct sss_iobuf *iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Returns the current size of the IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozeksize_t sss_iobuf_get_size(struct sss_iobuf *iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Returns the data pointer of the IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekuint8_t *sss_iobuf_get_data(struct sss_iobuf *iobuf);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Read from an IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * Read up to len bytes from an IO buffer. It is not an error to request
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * more bytes than the buffer actually has - the function will succeed, but
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * return the actual number of bytes read. Reading from an empty buffer just
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * returns zero bytes read.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] iobuf The IO buffer to read from
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] len The maximum number of bytes to read
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[out] _buf The buffer to read data into from iobuf
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[out] _read The actual number of bytes read from IO buffer.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return EOK on success, errno otherwise
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
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek/*
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * @brief Read an exact number of bytes from an IO buffer
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek *
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * Read exactly len bytes from an IO buffer. If the buffer contains fewer
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * bytes than len, ENOBUFS is returned.
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek *
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * @param[in] iobuf The IO buffer to read from
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * @param[in] len The maximum number of bytes to read
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * @param[out] _buf The buffer to read data into from iobuf
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek *
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek * @return EOK on success, errno otherwise
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek */
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_len(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek size_t len,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint8_t *_buf);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek/*
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @brief Write into an IO buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * Attempts to write len bytes into the iobuf. If the capacity is exceeded,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * the iobuf module tries to extend the buffer up to the maximum capacity.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * If reallocating the internal buffer fails, the data pointers are not
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * touched.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] iobuf The IO buffer to write to
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] buf The data to write into the buffer
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @param[in] len The number of bytes to write
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek *
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * @return EOK on success, errno otherwise. Notably returns ENOBUFS if
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek * the buffer capacity is exceeded.
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek */
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozekerrno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek uint8_t *buf,
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek size_t len);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint32_t *_val);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek uint32_t val);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek int32_t *_val);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek int32_t val);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek const char **_out);
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozekerrno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf,
5f7f45a64bdb9353f15b945db4ad2564b4b28ab2Jakub Hrozek const char *str);
9a9b5e115b079751422be22fd252c0b283611c62Jakub Hrozek#endif /* __SSS_IOBUF_H_ */