Files
imp/lib/memory_stream.c

35 lines
696 B
C

#include "memory_stream.h"
#include <stdbool.h>
static stream_status_t proc_byte(stream_t *s, uint8_t *out, bool consume)
{
memory_stream_t *ss = (memory_stream_t *)s;
if (ss->src < ss->end) {
*out = *ss->src;
if (consume)
++ss->src;
return STREAM_STATUS_OK;
} else {
return STREAM_STATUS_END;
}
}
static stream_status_t get_byte(stream_t *s, uint8_t *out)
{
return proc_byte(s, out, true);
}
static stream_status_t peek_byte(stream_t *s, uint8_t *out)
{
return proc_byte(s, out, false);
}
void memory_stream_init(memory_stream_t *s, const uint8_t *mem, size_t size)
{
s->stream.get_byte = get_byte;
s->stream.peek_byte = peek_byte;
s->src = mem;
s->end = mem + size;
}