mb/google/brya/var/orisa: Update Type C DisplayPort HPD Configuration
[coreboot2.git] / src / commonlib / iobuf.c
blob378793e223dbe6ff02d0197542f7c4cceaf19b50
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/endian.h>
4 #include <commonlib/iobuf.h>
5 #include <string.h>
6 #include <stdint.h>
8 static int ibuf_check_size(const struct ibuf *ib, size_t sz)
10 if (ibuf_remaining(ib) < sz)
11 return -1;
13 return 0;
16 void ibuf_init(struct ibuf *ib, const void *b, size_t sz)
18 ib->b = b;
19 ib->n_read = 0;
20 ib->capacity = sz;
23 void ibuf_from_obuf(struct ibuf *ib, const struct obuf *ob)
25 ibuf_init(ib, ob->b, ob->n_written);
28 int ibuf_splice(const struct ibuf *src, struct ibuf *dst, size_t off, size_t sz)
30 size_t end = off + sz;
31 size_t capacity = ibuf_capacity(src);
32 size_t nr_read = ibuf_nr_read(src);
34 if (end < off || end < sz || end > capacity)
35 return -1;
37 ibuf_init(dst, &src->b[off], sz);
39 /* Handle previously read data in src. */
40 if (off < nr_read)
41 dst->n_read = nr_read - off;
43 return 0;
46 int ibuf_splice_current(const struct ibuf *src, struct ibuf *dst, size_t sz)
48 return ibuf_splice(src, dst, ibuf_nr_read(src), sz);
51 int ibuf_split(const struct ibuf *src, struct ibuf *a, struct ibuf *b,
52 size_t boundary)
54 if (ibuf_splice(src, a, 0, boundary))
55 return -1;
57 return ibuf_splice(src, b, boundary, ibuf_capacity(src) - boundary);
60 const void *ibuf_oob_drain(struct ibuf *ib, size_t sz)
62 const void *b;
64 if (ibuf_check_size(ib, sz))
65 return NULL;
67 b = &ib->b[ib->n_read];
68 ib->n_read += sz;
70 return b;
73 int ibuf_read(struct ibuf *ib, void *data, size_t sz)
75 const void *b = ibuf_oob_drain(ib, sz);
77 if (b == NULL)
78 return -1;
80 memcpy(data, b, sz);
82 return 0;
85 int ibuf_read_be8(struct ibuf *ib, uint8_t *v)
87 size_t sz = sizeof(*v);
89 if (ibuf_check_size(ib, sz))
90 return -1;
92 *v = read_at_be8(ib->b, ib->n_read);
93 ib->n_read += sz;
95 return 0;
98 int ibuf_read_be16(struct ibuf *ib, uint16_t *v)
100 size_t sz = sizeof(*v);
102 if (ibuf_check_size(ib, sz))
103 return -1;
105 *v = read_at_be16(ib->b, ib->n_read);
106 ib->n_read += sz;
108 return 0;
111 int ibuf_read_be32(struct ibuf *ib, uint32_t *v)
113 size_t sz = sizeof(*v);
115 if (ibuf_check_size(ib, sz))
116 return -1;
118 *v = read_at_be32(ib->b, ib->n_read);
119 ib->n_read += sz;
121 return 0;
124 int ibuf_read_be64(struct ibuf *ib, uint64_t *v)
126 size_t sz = sizeof(*v);
128 if (ibuf_check_size(ib, sz))
129 return -1;
131 *v = read_at_be64(ib->b, ib->n_read);
132 ib->n_read += sz;
134 return 0;
137 int ibuf_read_le8(struct ibuf *ib, uint8_t *v)
139 size_t sz = sizeof(*v);
141 if (ibuf_check_size(ib, sz))
142 return -1;
144 *v = read_at_le8(ib->b, ib->n_read);
145 ib->n_read += sz;
147 return 0;
150 int ibuf_read_le16(struct ibuf *ib, uint16_t *v)
152 size_t sz = sizeof(*v);
154 if (ibuf_check_size(ib, sz))
155 return -1;
157 *v = read_at_le16(ib->b, ib->n_read);
158 ib->n_read += sz;
160 return 0;
163 int ibuf_read_le32(struct ibuf *ib, uint32_t *v)
165 size_t sz = sizeof(*v);
167 if (ibuf_check_size(ib, sz))
168 return -1;
170 *v = read_at_le32(ib->b, ib->n_read);
171 ib->n_read += sz;
173 return 0;
176 int ibuf_read_le64(struct ibuf *ib, uint64_t *v)
178 size_t sz = sizeof(*v);
180 if (ibuf_check_size(ib, sz))
181 return -1;
183 *v = read_at_le64(ib->b, ib->n_read);
184 ib->n_read += sz;
186 return 0;
189 int ibuf_read_n8(struct ibuf *ib, uint8_t *v)
191 return ibuf_read(ib, v, sizeof(*v));
194 int ibuf_read_n16(struct ibuf *ib, uint16_t *v)
196 return ibuf_read(ib, v, sizeof(*v));
199 int ibuf_read_n32(struct ibuf *ib, uint32_t *v)
201 return ibuf_read(ib, v, sizeof(*v));
204 int ibuf_read_n64(struct ibuf *ib, uint64_t *v)
206 return ibuf_read(ib, v, sizeof(*v));
209 static int obuf_check_size(const struct obuf *ob, size_t sz)
211 if (obuf_remaining(ob) < sz)
212 return -1;
214 return 0;
217 void obuf_init(struct obuf *ob, void *b, size_t sz)
219 ob->b = b;
220 ob->n_written = 0;
221 ob->capacity = sz;
224 int obuf_splice(const struct obuf *src, struct obuf *dst, size_t off, size_t sz)
226 size_t end = off + sz;
227 size_t capacity = obuf_capacity(src);
228 size_t nr_written = obuf_nr_written(src);
230 if (end < off || end < sz || end > capacity)
231 return -1;
233 obuf_init(dst, &src->b[off], sz);
235 /* Handle previously written data in src. */
236 if (off < nr_written)
237 dst->n_written = nr_written - off;
239 return 0;
242 int obuf_splice_current(const struct obuf *src, struct obuf *dst, size_t sz)
244 return obuf_splice(src, dst, obuf_nr_written(src), sz);
247 int obuf_split(const struct obuf *src, struct obuf *a, struct obuf *b,
248 size_t boundary)
250 if (obuf_splice(src, a, 0, boundary))
251 return -1;
253 return obuf_splice(src, b, boundary, obuf_capacity(src) - boundary);
256 void *obuf_oob_fill(struct obuf *ob, size_t sz)
258 void *b;
260 if (obuf_check_size(ob, sz))
261 return NULL;
263 b = &ob->b[ob->n_written];
264 ob->n_written += sz;
266 return b;
269 int obuf_write(struct obuf *ob, const void *data, size_t sz)
271 void *b;
273 b = obuf_oob_fill(ob, sz);
274 if (b == NULL)
275 return -1;
277 memcpy(b, data, sz);
279 return 0;
282 int obuf_write_be8(struct obuf *ob, uint8_t v)
284 size_t sz = sizeof(v);
286 if (obuf_check_size(ob, sz))
287 return -1;
289 write_at_be8(ob->b, v, ob->n_written);
290 ob->n_written += sz;
292 return 0;
295 int obuf_write_be16(struct obuf *ob, uint16_t v)
297 size_t sz = sizeof(v);
299 if (obuf_check_size(ob, sz))
300 return -1;
302 write_at_be16(ob->b, v, ob->n_written);
303 ob->n_written += sz;
305 return 0;
308 int obuf_write_be32(struct obuf *ob, uint32_t v)
310 size_t sz = sizeof(v);
312 if (obuf_check_size(ob, sz))
313 return -1;
315 write_at_be32(ob->b, v, ob->n_written);
316 ob->n_written += sz;
318 return 0;
321 int obuf_write_be64(struct obuf *ob, uint64_t v)
323 size_t sz = sizeof(v);
325 if (obuf_check_size(ob, sz))
326 return -1;
328 write_at_be64(ob->b, v, ob->n_written);
329 ob->n_written += sz;
331 return 0;
334 int obuf_write_le8(struct obuf *ob, uint8_t v)
336 size_t sz = sizeof(v);
338 if (obuf_check_size(ob, sz))
339 return -1;
341 write_at_le8(ob->b, v, ob->n_written);
342 ob->n_written += sz;
344 return 0;
347 int obuf_write_le16(struct obuf *ob, uint16_t v)
349 size_t sz = sizeof(v);
351 if (obuf_check_size(ob, sz))
352 return -1;
354 write_at_le16(ob->b, v, ob->n_written);
355 ob->n_written += sz;
357 return 0;
360 int obuf_write_le32(struct obuf *ob, uint32_t v)
362 size_t sz = sizeof(v);
364 if (obuf_check_size(ob, sz))
365 return -1;
367 write_at_le32(ob->b, v, ob->n_written);
368 ob->n_written += sz;
370 return 0;
373 int obuf_write_le64(struct obuf *ob, uint64_t v)
375 size_t sz = sizeof(v);
377 if (obuf_check_size(ob, sz))
378 return -1;
380 write_at_le64(ob->b, v, ob->n_written);
381 ob->n_written += sz;
383 return 0;
386 int obuf_write_n8(struct obuf *ob, uint8_t v)
388 return obuf_write(ob, &v, sizeof(v));
391 int obuf_write_n16(struct obuf *ob, uint16_t v)
393 return obuf_write(ob, &v, sizeof(v));
396 int obuf_write_n32(struct obuf *ob, uint32_t v)
398 return obuf_write(ob, &v, sizeof(v));
401 int obuf_write_n64(struct obuf *ob, uint64_t v)
403 return obuf_write(ob, &v, sizeof(v));
406 const void *obuf_contents(const struct obuf *ob, size_t *sz)
408 *sz = obuf_nr_written(ob);
409 return ob->b;