xref: /aosp_15_r20/external/coreboot/src/commonlib/include/commonlib/iobuf.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef COMMONLIB_IOBUF_H
4 #define COMMONLIB_IOBUF_H
5 
6 #include <stdint.h>
7 #include <sys/types.h>
8 
9 /*
10  * Two types are provided to aid in dealing with automatic buffer management
11  * for code that deals with serializing and deserializing data structures.
12  * The ibuf (input buffer) is read from while the obuf (output buffer) is
13  * written to. Both keep track of capacity of the buffer as well as current
14  * read or write cursor.
15  *
16  * When splicing or splitting ibufs of obufs the source object doesn't track
17  * reads or writes through the newly created objects back to the source object.
18  *
19  * Any function returning an int encodes the return values as < 0 on error
20  * and 0 on success. Any function returning a pointer returns NULL on error
21  * and non-NULL on success.
22  */
23 
24 struct ibuf {
25 	const uint8_t *b;
26 	size_t n_read;
27 	size_t capacity;
28 };
29 
30 struct obuf {
31 	uint8_t *b;
32 	size_t n_written;
33 	size_t capacity;
34 };
35 
36 /* Helper functions. */
ibuf_capacity(const struct ibuf * ib)37 static inline size_t ibuf_capacity(const struct ibuf *ib)
38 {
39 	return ib->capacity;
40 }
41 
ibuf_nr_read(const struct ibuf * ib)42 static inline size_t ibuf_nr_read(const struct ibuf *ib)
43 {
44 	return ib->n_read;
45 }
46 
ibuf_remaining(const struct ibuf * ib)47 static inline size_t ibuf_remaining(const struct ibuf *ib)
48 {
49 	return ibuf_capacity(ib) - ibuf_nr_read(ib);
50 }
51 
obuf_capacity(const struct obuf * ob)52 static inline size_t obuf_capacity(const struct obuf *ob)
53 {
54 	return ob->capacity;
55 }
56 
obuf_nr_written(const struct obuf * ob)57 static inline size_t obuf_nr_written(const struct obuf *ob)
58 {
59 	return ob->n_written;
60 }
61 
obuf_remaining(const struct obuf * ob)62 static inline size_t obuf_remaining(const struct obuf *ob)
63 {
64 	return obuf_capacity(ob) - obuf_nr_written(ob);
65 }
66 
67 /* Initialize an ibuf with buffer and size of data. */
68 void ibuf_init(struct ibuf *ib, const void *b, size_t sz);
69 
70 /* Create a new ibuf based on a subregion of the src ibuf. */
71 int ibuf_splice(const struct ibuf *src, struct ibuf *dst, size_t off,
72 		size_t sz);
73 
74 /* Same as ibuf_splice(), but start from last read byte offset. */
75 int ibuf_splice_current(const struct ibuf *src, struct ibuf *dst, size_t sz);
76 
77 /* Split an ibuf into 2 new ibufs at provided boundary. */
78 int ibuf_split(const struct ibuf *src, struct ibuf *a, struct ibuf *b,
79 		size_t boundary);
80 
81 /* Out-of-band drain of ibuf by returning pointer to data of specified size. */
82 const void *ibuf_oob_drain(struct ibuf *ib, size_t sz);
83 
84 /* Read arbitrary data from input buffer. */
85 int ibuf_read(struct ibuf *ib, void *data, size_t sz);
86 
87 /* Read big endian fixed size values. */
88 int ibuf_read_be8(struct ibuf *ib, uint8_t *v);
89 int ibuf_read_be16(struct ibuf *ib, uint16_t *v);
90 int ibuf_read_be32(struct ibuf *ib, uint32_t *v);
91 int ibuf_read_be64(struct ibuf *ib, uint64_t *v);
92 
93 /* Read little endian fixed size values. */
94 int ibuf_read_le8(struct ibuf *ib, uint8_t *v);
95 int ibuf_read_le16(struct ibuf *ib, uint16_t *v);
96 int ibuf_read_le32(struct ibuf *ib, uint32_t *v);
97 int ibuf_read_le64(struct ibuf *ib, uint64_t *v);
98 
99 /* Read native endian fixed size values. */
100 int ibuf_read_n8(struct ibuf *ib, uint8_t *v);
101 int ibuf_read_n16(struct ibuf *ib, uint16_t *v);
102 int ibuf_read_n32(struct ibuf *ib, uint32_t *v);
103 int ibuf_read_n64(struct ibuf *ib, uint64_t *v);
104 
105 /* Helper to create an ibuf from an obuf after an entity has written data. */
106 void ibuf_from_obuf(struct ibuf *ib, const struct obuf *ob);
107 
108 /* Initialize an obuf with buffer and maximum capacity. */
109 void obuf_init(struct obuf *ob, void *b, size_t sz);
110 
111 /* Provide the buffer and size of the written contents. */
112 const void *obuf_contents(const struct obuf *ob, size_t *sz);
113 
114 /* Create a new obuf based on a subregion of the src obuf. */
115 int obuf_splice(const struct obuf *src, struct obuf *dst, size_t off,
116 		size_t sz);
117 
118 /* Same as obuf_splice(), but start from last written byte offset. */
119 int obuf_splice_current(const struct obuf *src, struct obuf *dst, size_t sz);
120 
121 /* Split an obuf into 2 new obufs at provided boundary. */
122 int obuf_split(const struct obuf *src, struct obuf *a, struct obuf *b,
123 		size_t boundary);
124 
125 /* Fill the buffer out-of-band. The size is accounted for. */
126 void *obuf_oob_fill(struct obuf *ob, size_t sz);
127 
128 /* Write arbitrary data to output buffer. */
129 int obuf_write(struct obuf *ob, const void *data, size_t sz);
130 
131 /* Write big endian fixed size values. */
132 int obuf_write_be8(struct obuf *ob, uint8_t v);
133 int obuf_write_be16(struct obuf *ob, uint16_t v);
134 int obuf_write_be32(struct obuf *ob, uint32_t v);
135 int obuf_write_be64(struct obuf *ob, uint64_t v);
136 
137 /* Write little endian fixed size values. */
138 int obuf_write_le8(struct obuf *ob, uint8_t v);
139 int obuf_write_le16(struct obuf *ob, uint16_t v);
140 int obuf_write_le32(struct obuf *ob, uint32_t v);
141 int obuf_write_le64(struct obuf *ob, uint64_t v);
142 
143 /* Write native endian fixed size values. */
144 int obuf_write_n8(struct obuf *ob, uint8_t v);
145 int obuf_write_n16(struct obuf *ob, uint16_t v);
146 int obuf_write_n32(struct obuf *ob, uint32_t v);
147 int obuf_write_n64(struct obuf *ob, uint64_t v);
148 
149 #endif
150