coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
iobuf.h
Go to the documentation of this file.
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 {
32  size_t n_written;
33  size_t capacity;
34 };
35 
36 /* Helper functions. */
37 static inline size_t ibuf_capacity(const struct ibuf *ib)
38 {
39  return ib->capacity;
40 }
41 
42 static inline size_t ibuf_nr_read(const struct ibuf *ib)
43 {
44  return ib->n_read;
45 }
46 
47 static inline size_t ibuf_remaining(const struct ibuf *ib)
48 {
49  return ibuf_capacity(ib) - ibuf_nr_read(ib);
50 }
51 
52 static inline size_t obuf_capacity(const struct obuf *ob)
53 {
54  return ob->capacity;
55 }
56 
57 static inline size_t obuf_nr_written(const struct obuf *ob)
58 {
59  return ob->n_written;
60 }
61 
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
void obuf_init(struct obuf *ob, void *b, size_t sz)
Definition: iobuf.c:216
const void * obuf_contents(const struct obuf *ob, size_t *sz)
Definition: iobuf.c:405
int obuf_splice_current(const struct obuf *src, struct obuf *dst, size_t sz)
Definition: iobuf.c:241
int obuf_write_n8(struct obuf *ob, uint8_t v)
Definition: iobuf.c:385
int obuf_write_le32(struct obuf *ob, uint32_t v)
Definition: iobuf.c:359
int ibuf_read_n16(struct ibuf *ib, uint16_t *v)
Definition: iobuf.c:193
int obuf_write_le16(struct obuf *ob, uint16_t v)
Definition: iobuf.c:346
static size_t obuf_remaining(const struct obuf *ob)
Definition: iobuf.h:62
static size_t ibuf_remaining(const struct ibuf *ib)
Definition: iobuf.h:47
int ibuf_read_be16(struct ibuf *ib, uint16_t *v)
Definition: iobuf.c:97
int obuf_write_be64(struct obuf *ob, uint64_t v)
Definition: iobuf.c:320
int obuf_write_be32(struct obuf *ob, uint32_t v)
Definition: iobuf.c:307
void * obuf_oob_fill(struct obuf *ob, size_t sz)
Definition: iobuf.c:255
int ibuf_splice_current(const struct ibuf *src, struct ibuf *dst, size_t sz)
Definition: iobuf.c:45
static size_t ibuf_capacity(const struct ibuf *ib)
Definition: iobuf.h:37
int ibuf_split(const struct ibuf *src, struct ibuf *a, struct ibuf *b, size_t boundary)
Definition: iobuf.c:50
int obuf_split(const struct obuf *src, struct obuf *a, struct obuf *b, size_t boundary)
Definition: iobuf.c:246
int ibuf_read_n8(struct ibuf *ib, uint8_t *v)
Definition: iobuf.c:188
int obuf_write_be16(struct obuf *ob, uint16_t v)
Definition: iobuf.c:294
const void * ibuf_oob_drain(struct ibuf *ib, size_t sz)
Definition: iobuf.c:59
int obuf_write_n32(struct obuf *ob, uint32_t v)
Definition: iobuf.c:395
int ibuf_read(struct ibuf *ib, void *data, size_t sz)
Definition: iobuf.c:72
int ibuf_read_be64(struct ibuf *ib, uint64_t *v)
Definition: iobuf.c:123
int obuf_splice(const struct obuf *src, struct obuf *dst, size_t off, size_t sz)
Definition: iobuf.c:223
int obuf_write_be8(struct obuf *ob, uint8_t v)
Definition: iobuf.c:281
static size_t obuf_capacity(const struct obuf *ob)
Definition: iobuf.h:52
int ibuf_read_le8(struct ibuf *ib, uint8_t *v)
Definition: iobuf.c:136
int ibuf_read_n32(struct ibuf *ib, uint32_t *v)
Definition: iobuf.c:198
int obuf_write_le64(struct obuf *ob, uint64_t v)
Definition: iobuf.c:372
int ibuf_read_be8(struct ibuf *ib, uint8_t *v)
Definition: iobuf.c:84
int ibuf_read_le64(struct ibuf *ib, uint64_t *v)
Definition: iobuf.c:175
static size_t obuf_nr_written(const struct obuf *ob)
Definition: iobuf.h:57
int ibuf_read_le16(struct ibuf *ib, uint16_t *v)
Definition: iobuf.c:149
void ibuf_init(struct ibuf *ib, const void *b, size_t sz)
Definition: iobuf.c:15
int obuf_write_le8(struct obuf *ob, uint8_t v)
Definition: iobuf.c:333
int ibuf_splice(const struct ibuf *src, struct ibuf *dst, size_t off, size_t sz)
Definition: iobuf.c:27
int obuf_write_n16(struct obuf *ob, uint16_t v)
Definition: iobuf.c:390
int ibuf_read_le32(struct ibuf *ib, uint32_t *v)
Definition: iobuf.c:162
static size_t ibuf_nr_read(const struct ibuf *ib)
Definition: iobuf.h:42
int ibuf_read_n64(struct ibuf *ib, uint64_t *v)
Definition: iobuf.c:203
void ibuf_from_obuf(struct ibuf *ib, const struct obuf *ob)
Definition: iobuf.c:22
int ibuf_read_be32(struct ibuf *ib, uint32_t *v)
Definition: iobuf.c:110
int obuf_write(struct obuf *ob, const void *data, size_t sz)
Definition: iobuf.c:268
int obuf_write_n64(struct obuf *ob, uint64_t v)
Definition: iobuf.c:400
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
unsigned long long uint64_t
Definition: stdint.h:17
unsigned char uint8_t
Definition: stdint.h:8
Definition: iobuf.h:24
const uint8_t * b
Definition: iobuf.h:25
size_t capacity
Definition: iobuf.h:27
size_t n_read
Definition: iobuf.h:26
Definition: iobuf.h:30
size_t n_written
Definition: iobuf.h:32
uint8_t * b
Definition: iobuf.h:31
size_t capacity
Definition: iobuf.h:33