CPlay/buf/buf.c
2025-08-08 05:13:20 +02:00

203 lines
5.9 KiB
C

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
/**
* @brief Write a 8 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 8 bit unsigned int to write
* @param index The index read/write pointer
*/
void buf_append_i8(uint8_t *buf, int8_t value, size_t *index) {
buf[(*index)++] = value;
}
/**
* @brief Write a 8 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 8 bit unsigned int to write
* @param index The index read/write pointer
*/
void buf_append_u8(uint8_t *buf, uint8_t value, size_t *index) {
buf[(*index)++] = value;
}
/**
* @brief Write a 16 bit signed int to the buffer
*
* @param buf The buffer to write to
* @param value The 16 bit signed int to write
* @param index The index read/write pointer
*/
void buf_append_i16(uint8_t *buf, int16_t value, size_t *index) {
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
/**
* @brief Write a 16 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 16 bit signed int to write
* @param index The index read/write pointer
*/
void buf_append_u16(uint8_t *buf, uint16_t value, size_t *index) {
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
/**
* @brief Write a 32 bit signed int to the buffer
*
* @param buf The buffer to write to
* @param value The 32 bit signed int to write
* @param index The index read/write pointer
*/
void buf_append_i32(uint8_t *buf, int32_t value, size_t *index) {
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
/**
* @brief Write a 32 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 32 bit signed int to write
* @param index The index read/write pointer
*/
void buf_append_u32(uint8_t *buf, uint32_t value, size_t *index) {
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
void buf_append_i64(uint8_t *buf, int64_t value, int32_t *index) {
buf[(*index)++] = (value >> 56) & 0xFF;
buf[(*index)++] = (value >> 48) & 0xFF;
buf[(*index)++] = (value >> 40) & 0xFF;
buf[(*index)++] = (value >> 32) & 0xFF;
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = (value) & 0xFF;
}
void buf_append_u64(uint8_t *buf, uint64_t value, int32_t *index) {
buf[(*index)++] = (value >> 56) & 0xFF;
buf[(*index)++] = (value >> 48) & 0xFF;
buf[(*index)++] = (value >> 40) & 0xFF;
buf[(*index)++] = (value >> 32) & 0xFF;
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = (value) & 0xFF;
}
/*
* READS
*/
/**
* @brief Read a 8 bit signed int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 8 bit signed int
*/
int8_t buf_read_i8(const uint8_t *buf, size_t *index) {
return buf[(*index)++];
}
/**
* @brief Read a 8 bit unsigned int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 8 bit unsigned int
*/
uint8_t buf_read_u8(const uint8_t *buf, size_t *index) {
return buf[(*index)++];
}
/**
* @brief Read a 16 bit int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 16 bit signed int
*/
int16_t buf_read_i16(const uint8_t *buf, size_t *index) {
int16_t val = 0;
val |= ((int16_t)buf[(*index)++]) << 8;
val |= ((int16_t)buf[(*index)++]);
return val;
}
/**
* @brief Read a 16 bit int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 16 bit unsigned int
*/
uint16_t buf_read_u16(const uint8_t *buf, size_t *index) {
int16_t val = 0;
val |= ((int16_t)buf[(*index)++]) << 8;
val |= ((int16_t)buf[(*index)++]);
return val;
}
/**
* @brief Read a 32 bit int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 32 bit signed int
*/
int32_t buf_read_i32(const uint8_t *buf, size_t *index) {
int32_t val = 0;
val |= ((int32_t)buf[(*index)++]) << 24;
val |= ((int32_t)buf[(*index)++]) << 16;
val |= ((int32_t)buf[(*index)++]) << 8;
val |= ((int32_t)buf[(*index)++]);
return val;
}
/**
* @brief Read a 32 bit uint from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 32 bit signed int
*/
uint32_t buf_read_u32(const uint8_t *buf, size_t *index) {
uint32_t val = 0;
val |= ((uint32_t)buf[(*index)++]) << 24;
val |= ((uint32_t)buf[(*index)++]) << 16;
val |= ((uint32_t)buf[(*index)++]) << 8;
val |= ((uint32_t)buf[(*index)++]);
return val;
}
int64_t buf_read_i64(const uint8_t *buf, int32_t *index) {
uint64_t res = ((uint64_t)buf[*index]) << 56 | ((uint64_t)buf[*index + 1]) << 48 |
((uint64_t)buf[*index + 2]) << 40 | ((uint64_t)buf[*index + 3]) << 32 |
((uint64_t)buf[*index + 4]) << 24 | ((uint64_t)buf[*index + 5]) << 16 |
((uint64_t)buf[*index + 6]) << 8 | ((uint64_t)buf[*index + 7]);
*index += 8;
return res;
}
uint64_t buf_read_u64(const uint8_t *buf, size_t *index) {
uint64_t res = ((uint64_t)buf[*index]) << 56 | ((uint64_t)buf[*index + 1]) << 48 |
((uint64_t)buf[*index + 2]) << 40 | ((uint64_t)buf[*index + 3]) << 32 |
((uint64_t)buf[*index + 4]) << 24 | ((uint64_t)buf[*index + 5]) << 16 |
((uint64_t)buf[*index + 6]) << 8 | ((uint64_t)buf[*index + 7]);
*index += 8;
return res;
}