aboutsummaryrefslogtreecommitdiff
path: root/src/iobuf.h
blob: 9446274b160d374524ede64eea8ab8d55ec3b198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/** \file
 * \brief The IO buffer implementation.
 * \note The IO buffer is a FIFO byte array object with some extra convenience
 *	functions. The IO buffer is similar to the C++ counterpart,
 *	\c std::vector<uint8_t>
 */
/*
* Copyright (c) 2019-2021 David Timber <mieabby@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#include <sys/types.h>


/* Alias declarations */
typedef struct prne_iobuf prne_iobuf_t;

// The IO buffer object.
struct prne_iobuf {
	uint8_t *m; // The buffer
	size_t size; // The size of buffer
	size_t avail; // The length of the buffer available (size - len)
	size_t len; // The length of the contents
	/* The ownership status of the buffer.
	 * True if the object is responsible for freeing the allocated memory for
	 * the buffer. False otherwise.
	 */
	bool ownership;
};


/**
 * \brief Initialise the IO buffer object.
 * \param ib The pointer to the IO buffer object.
 * \note \p ib can be freed using \c prne_free_iobuf() once initialised.
 * \see \c prne_free_iobuf()
 */
void prne_init_iobuf (prne_iobuf_t *ib);
/**
 * \brief Free resources allocated for the IO buffer object.
 * \param ib The pointer to the IO buffer object.
 * \see \c prne_init_iobuf()
 */
void prne_free_iobuf (prne_iobuf_t *ib);
/**
 * \brief Allocate memory to set the size of the buffer.
 * \param ib The pointer to the IO buffer object.
 * \param ny_size The new byte size of the buffer.
 * \retval true if allocation has been successful.
 * \retval false otherwise with \c errno set.
 */
bool prne_alloc_iobuf (prne_iobuf_t *ib, const size_t ny_size);
/**
 * \brief Try allocating memory for the buffer using the sizes specified in the
 *	array.
 * \param ib The pointer to the IO buffer object.
 * \param ny_size The pointer to the array of new sizes of the buffer.
 * \retval true if the size of the buffer has been successfully set to one of
 *	the sizes in \p ny_size.
 * \retval false otherwise with \c errno set.
 * \note The sizes are tried from the first element of \p ny_size. Usually,
 *	you'd want to set the elements of the array in the descending order so the
 *	largest size is tried first which is optimal in most cases.
 */
bool prne_try_alloc_iobuf (prne_iobuf_t *ib, const size_t *ny_size);
/**
 * \brief Set up the IO buffer object to use the external buffer, relieving the
 *	IO buffer object's responsibility of freeing the buffer.
 * \param ib The pointer to the IO buffer object.
 * \param m The pointer to the external buffer.
 * \param size The size of the external buffer.
 * \param len The initial length of the contents in the external buffer.
 *	This is usually zero unless there are contents in the external buffer to be
 *	used.
 * \note The function is useful when the use of static type of memory such as
 *	.bss or stack is desired. Any dynamic resource previously allocated is
*	freed.
 */
void prne_iobuf_setextbuf (
	prne_iobuf_t *ib,
	uint8_t *m,
	const size_t size,
	const size_t len);
/**
 * \brief Reset the buffer state - Set \c len to zero and \c avail to the size
 *	of the buffer.
 * \param ib The pointer to the io buffer object.
 * \note Use this function to discard the contents of the buffer. The contents
 *	of the buffer will remain untouched. You may want to use
 *	\c prne_iobuf_zero() to scrub the data off memory.
 * \see \c prne_iobuf_zero()
 */
void prne_iobuf_reset (prne_iobuf_t *ib);
/**
 * \brief Zero-fill the entire buffer - \c memset() convenience function.
 * \param ib The pointer to the IO buffer object.
 * \note This is the equivalent of calling \c memset() and
 *	\c prne_iobuf_reset().
 */
void prne_iobuf_zero (prne_iobuf_t *ib);
/**
 * \brief Shift the contents of the buffer - \c memmove() convenience function.
 * \param ib The pointer to the IO buffer object.
 * \param amount The number of bytes to shift. A positive value simply increases
 *	\c len and decreases \c avail. A negative value causes the function to call
 *	\c memmove() to discard the amount of data specified, increasing \c avail
 *	and decreasing \c len
 * \warning When shifting the contents of the buffer to the left, depending on
 *	the behaviour of \c memmove(), the contents of the buffer on the right may
 *	remain intact on memory. Do not use IO buffer to store sensitive data.
 */
void prne_iobuf_shift (prne_iobuf_t *ib, const ssize_t amount);