#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "common.h"
#include "scaffold.h"
#include "bincode.h"
uint8_t
bincode_getu8(struct bincode *s)
{
uint8_t ret;
ret = s->buf[0];
s->buf += sizeof(uint8_t);
s->avail -= sizeof(uint8_t);
return ret;
}
uint16_t
bincode_getu16(struct bincode *s)
{
uint16_t ret;
uint8_t *b;
b = s->buf;
ret = (uint16_t)b[1] << 8 | (uint16_t)b[0];
s->buf += sizeof(uint16_t);
s->avail -= sizeof(uint16_t);
return ret;
}
uint32_t
bincode_getu32(struct bincode *s)
{
uint32_t ret;
uint8_t *b;
b = s->buf;
ret = (uint32_t)b[3] << 24 | (uint32_t)b[2] << 16 |
(uint32_t)b[1] << 8 | (uint32_t)b[0];
s->buf += sizeof(uint32_t);
s->avail -= sizeof(uint32_t);
return ret;
}
uint64_t
bincode_getu64(struct bincode *s)
{
uint64_t ret;
uint8_t *b;
b = s->buf;
ret = (uint64_t)b[7] << 56 | (uint64_t)b[6] << 48 |
(uint64_t)b[5] << 40 | (uint64_t)b[4] << 32 |
(uint64_t)b[3] << 24 | (uint64_t)b[2] << 16 |
(uint64_t)b[1] << 8 | (uint64_t)b[0];
s->buf += sizeof(uint64_t);
s->avail -= sizeof(uint64_t);
return ret;
}
void
bincode_getstr(struct bincode *s, char *str, size_t len)
{
memcpy(str, s->buf, len);
s->buf += len;
s->avail -= len;
str[len] = '\0';
}
void
bincode_getbytes(struct bincode *s, uint8_t *dest, size_t len)
{
memcpy(dest, s->buf, len);
s->buf += len;
s->avail -= len;
}