1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  */
7 
8 #include <zephyr/llext/buf_loader.h>
9 #include <zephyr/sys/util.h>
10 #include <string.h>
11 
llext_buf_read(struct llext_loader * l,void * buf,size_t len)12 int llext_buf_read(struct llext_loader *l, void *buf, size_t len)
13 {
14 	struct llext_buf_loader *buf_l = CONTAINER_OF(l, struct llext_buf_loader, loader);
15 	size_t end = MIN(buf_l->pos + len, buf_l->len);
16 	size_t read_len = end - buf_l->pos;
17 
18 	memcpy(buf, buf_l->buf + buf_l->pos, read_len);
19 	buf_l->pos = end;
20 
21 	return 0;
22 }
23 
llext_buf_seek(struct llext_loader * l,size_t pos)24 int llext_buf_seek(struct llext_loader *l, size_t pos)
25 {
26 	struct llext_buf_loader *buf_l = CONTAINER_OF(l, struct llext_buf_loader, loader);
27 
28 	buf_l->pos = MIN(pos, buf_l->len);
29 
30 	return 0;
31 }
32 
llext_buf_peek(struct llext_loader * l,size_t pos)33 void *llext_buf_peek(struct llext_loader *l, size_t pos)
34 {
35 	struct llext_buf_loader *buf_l = CONTAINER_OF(l, struct llext_buf_loader, loader);
36 
37 	return (void *)(buf_l->buf + pos);
38 }
39