some progress

This commit is contained in:
veclavtlica
2026-10-07 11:21:21 +03:00
parent 14062e9d00
commit 8d502173a2
26 changed files with 705 additions and 212 deletions
+64
View File
@@ -0,0 +1,64 @@
#include <stddef.h>
#include <stdint.h>
#include "bin.h"
#include "bits.h"
#include "buf.h"
#include "def.h"
#include "std.h"
bool com_bin_read_i16(com_bin *b, int16_t *value) {
int16_t buf;
if (!b->interface.readn(b->ctx, (char *)&buf, 2))
return false;
if (COM_DEF_CPU_LITEND && (b->flags & COM_BIN_BIG_ENDIAN)) {
*value = __builtin_bswap16(buf);
} else
*value = buf;
return true;
}
/* Same thing, really. */
bool com_bin_read_u16(com_bin *b, uint16_t *value) {
return (com_bin_read_i16(b, (int16_t *)value));
}
bool com_bin_read_bufb(com_bin *b, struct com_buf *buf, uint32_t bytes) {
com_std_assert(buf->typesz == 1);
if (!com_buf_make_room(buf, bytes))
return false;
bool s = b->interface.readn(b->ctx, (char *)&buf->data[buf->len], bytes);
if (s)
buf->len += bytes;
return s;
}
bool com_bin_read_bufbi(com_bin *b, struct com_buf *buf, uint32_t *index,
uint32_t bytes) {
com_std_assert(buf->typesz == 1);
uint32_t const alignextra =
com_bits_nalign(buf->len, COM_DEF_ALIGNMENT) - buf->len;
if (!com_buf_make_room(buf, alignextra + sizeof(struct com_buf) + bytes))
return false;
buf->len += alignextra;
struct com_buf *in = (struct com_buf *)&buf->data[buf->len];
buf->len += sizeof(struct com_buf);
bool s = b->interface.readn(b->ctx, (char *)&buf->data[buf->len], bytes);
if (s) {
buf->len += bytes;
*in = (struct com_buf){.cap = bytes,
.len = bytes,
.typesz = 1,
.flags = (buf->flags & COM_BUF_STACK) | COM_BUF_SUB};
*index = ((ptrdiff_t)in - (ptrdiff_t)buf->data) / sizeof(struct com_buf);
}
return s;
}
bool com_bin_skip(com_bin *b, uint32_t bytes) {
return b->interface.skipn(b->ctx, bytes);
}