Buffer integer finished with tests
This commit is contained in:
parent
3d45b8b472
commit
713d96d770
3 changed files with 258 additions and 30 deletions
124
buf/buf.c
124
buf/buf.c
|
|
@ -1,6 +1,18 @@
|
|||
#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
|
||||
*
|
||||
|
|
@ -24,6 +36,18 @@ void buf_append_i16(uint8_t *buf, int16_t value, size_t *index) {
|
|||
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
|
||||
*
|
||||
|
|
@ -39,16 +63,54 @@ void buf_append_i32(uint8_t *buf, int32_t value, size_t *index) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Write a 32 bit float to the buffer
|
||||
* @brief Write a 32 bit unsigned int to the buffer
|
||||
*
|
||||
* @param buf The buffer to write to
|
||||
* @param value The 32 bit float to write
|
||||
* @param value The 32 bit signed int to write
|
||||
* @param index The index read/write pointer
|
||||
*/
|
||||
void buf_append_f32(uint8_t *buf, float value, size_t *index) {
|
||||
uint32_t u;
|
||||
memcpy(&u, &value, sizeof(float)); // Safe conversion
|
||||
buf_append_i32(buf, (int32_t)u, index);
|
||||
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)++];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -69,7 +131,21 @@ uint8_t buf_read_u8(const uint8_t *buf, size_t *index) {
|
|||
* @param index The index read/write pointer
|
||||
* @return 16 bit signed int
|
||||
*/
|
||||
uint8_t buf_read_i16(const uint8_t *buf, size_t *index) {
|
||||
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)++]);
|
||||
|
|
@ -93,15 +169,35 @@ int32_t buf_read_i32(const uint8_t *buf, size_t *index) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Read a 32 bit float from the buffer
|
||||
* @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 float
|
||||
* @return 32 bit signed int
|
||||
*/
|
||||
float buf_read_f32(const uint8_t *buf, size_t *index) {
|
||||
uint32_t u = (uint32_t)buf_read_i32(buf, index);
|
||||
float f;
|
||||
memcpy(&f, &u, sizeof(float));
|
||||
return f;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue