Line data Source code
1 : #include "flakystream.h" 2 : #include <string.h> 3 : 4 22773858 : bool flakystream_callback(pb_istream_t *stream, pb_byte_t *buf, size_t count) 5 : { 6 22773858 : flakystream_t *state = stream->state; 7 : 8 22773858 : if (state->position + count > state->msglen) 9 : { 10 8990 : stream->bytes_left = 0; 11 8990 : return false; 12 : } 13 22764868 : else if (state->position + count > state->fail_after) 14 : { 15 5288 : PB_RETURN_ERROR(stream, "flaky error"); 16 : } 17 : 18 22759580 : memcpy(buf, state->buffer + state->position, count); 19 22759580 : state->position += count; 20 22759580 : return true; 21 : } 22 : 23 16047 : void flakystream_init(flakystream_t *stream, const uint8_t *buffer, size_t msglen, size_t fail_after) 24 : { 25 16047 : memset(stream, 0, sizeof(*stream)); 26 16047 : stream->stream.callback = flakystream_callback; 27 16047 : stream->stream.bytes_left = SIZE_MAX; 28 16047 : stream->stream.state = stream; 29 16047 : stream->buffer = buffer; 30 16047 : stream->position = 0; 31 16047 : stream->msglen = msglen; 32 16047 : stream->fail_after = fail_after; 33 16047 : }