MOS Source Code
Loading...
Searching...
No Matches
flakystream.c
Go to the documentation of this file.
1#include "flakystream.h"
2#include <string.h>
3
4bool flakystream_callback(pb_istream_t *stream, pb_byte_t *buf, size_t count)
5{
6 flakystream_t *state = stream->state;
7
8 if (state->position + count > state->msglen)
9 {
10 stream->bytes_left = 0;
11 return false;
12 }
13 else if (state->position + count > state->fail_after)
14 {
15 PB_RETURN_ERROR(stream, "flaky error");
16 }
17
18 memcpy(buf, state->buffer + state->position, count);
19 state->position += count;
20 return true;
21}
22
23void flakystream_init(flakystream_t *stream, const uint8_t *buffer, size_t msglen, size_t fail_after)
24{
25 memset(stream, 0, sizeof(*stream));
26 stream->stream.callback = flakystream_callback;
27 stream->stream.bytes_left = SIZE_MAX;
28 stream->stream.state = stream;
29 stream->buffer = buffer;
30 stream->position = 0;
31 stream->msglen = msglen;
32 stream->fail_after = fail_after;
33}
bool flakystream_callback(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition flakystream.c:4
void flakystream_init(flakystream_t *stream, const uint8_t *buffer, size_t msglen, size_t fail_after)
Definition flakystream.c:23
uint_least8_t pb_byte_t
Definition pb.h:227
#define PB_RETURN_ERROR(stream, msg)
Definition pb.h:894
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
unsigned char uint8_t
Definition pb_syshdr.h:20
size_t fail_after
Definition flakystream.h:14
size_t position
Definition flakystream.h:12
pb_istream_t stream
Definition flakystream.h:10
const uint8_t * buffer
Definition flakystream.h:11
static char buffer[2048]
Definition test_printf.c:7