2019-01-25 23:13:07 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Foundries.io
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_INCLUDE_BUF_UTIL_H_
|
|
|
|
#define ZEPHYR_INCLUDE_BUF_UTIL_H_
|
|
|
|
|
|
|
|
#include <zephyr/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* append */
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_append(uint8_t *dst, uint16_t *dst_len, uint16_t dst_size,
|
2022-05-20 13:27:15 +02:00
|
|
|
const uint8_t *src, uint16_t src_len)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
if (!dst || !src) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*dst_len + src_len > dst_size) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst + *dst_len, src, src_len);
|
|
|
|
*dst_len += src_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert */
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_insert(uint8_t *dst, uint16_t *dst_len, uint16_t dst_size,
|
|
|
|
uint16_t offset, uint8_t *src, uint16_t src_len)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
if (!dst || !src) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*dst_len + src_len > dst_size) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shift everything in fbuf after offset by len */
|
|
|
|
memmove(dst + offset + src_len, dst + offset, *dst_len - offset);
|
|
|
|
|
|
|
|
/* copy src into fbuf at offset */
|
|
|
|
memcpy(dst + offset, src, src_len);
|
|
|
|
*dst_len += src_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read */
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read(uint8_t *dst, uint16_t len, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
if (!src) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*offset + len > src_len) {
|
2021-12-17 16:43:39 +01:00
|
|
|
return -ENODATA;
|
2019-01-25 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dst) {
|
|
|
|
/* copy data at offset into dst */
|
|
|
|
memcpy(dst, src + *offset, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_skip(uint16_t len, uint8_t *src, uint16_t src_len, uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
return buf_read(NULL, len, src, src_len, offset);
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read_u8(uint8_t *value, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
return buf_read(value, sizeof(uint8_t), src, src_len, offset);
|
2019-01-25 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read_u16(uint16_t *value, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
return buf_read((uint8_t *)value, sizeof(uint16_t), src, src_len, offset);
|
2019-01-25 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read_be16(uint16_t *value, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t v16[2];
|
2019-01-25 23:13:07 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
ret = buf_read(v16, sizeof(uint16_t), src, src_len, offset);
|
2019-01-25 23:13:07 +01:00
|
|
|
*value = v16[0] << 8 | v16[1];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read_u32(uint32_t *value, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
return buf_read((uint8_t *)value, sizeof(uint32_t), src, src_len, offset);
|
2019-01-25 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int buf_read_be32(uint32_t *value, uint8_t *src, uint16_t src_len,
|
|
|
|
uint16_t *offset)
|
2019-01-25 23:13:07 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t v32[4];
|
2019-01-25 23:13:07 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
ret = buf_read(v32, sizeof(uint32_t), src, src_len, offset);
|
2019-01-25 23:13:07 +01:00
|
|
|
*value = v32[0] << 24 | v32[1] << 16 | v32[2] << 8 | v32[3];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ZEPHYR_INCLUDE_BUF_UTIL_H_ */
|