6 #include "bytesource.h"
8 #define MARKER_CHAR 0xff
9 #define STUFF_MARKER 0x00
11 void init_byte_source(struct byte_source* source, raw_input_func_t* input_func, void* userdata)
13 // TODO: should this size be a different constant?
14 memset(source, 0, sizeof(*source));
15 source->bytes = (uint8_t*)malloc(BYTESOURCE_CHUNK_SIZE);
16 source->input_func = input_func;
17 source->userdata = userdata;
20 uint8_t byte_source_read_marker(struct byte_source* src)
22 // Refill until we have at least two bytes or EOF.
23 while (src->bytes_available < 2) {
24 const unsigned bytes_to_read = BYTESOURCE_CHUNK_SIZE - src->bytes_available;
25 const ssize_t bytes_read =
26 (*src->input_func)(src->userdata,
27 src->bytes + src->bytes_available,
29 assert(bytes_read >= -1);
30 assert(bytes_read <= (ssize_t)bytes_to_read);
32 if (bytes_read == -1 || bytes_read == 0) {
36 src->bytes_available += bytes_read;
39 assert(src->bytes_available >= 2);
40 if (src->bytes[0] != MARKER_CHAR || src->bytes[1] == STUFF_MARKER) {
44 uint8_t ret = src->bytes[1];
45 memmove(src->bytes, src->bytes + 2, src->bytes_available - 2);
46 src->bytes_available -= 2;
51 ssize_t byte_source_input_func(void* source, uint8_t* buf, size_t len)
53 struct byte_source* src = (struct byte_source*)source;
55 // If there's no data in the buffer (or only a partial marker), we have
56 // to read in more from our upstream src.
57 while (src->bytes_available == 0 ||
58 (src->bytes_available == 1 && src->bytes[0] == MARKER_CHAR)) {
59 const unsigned space_left = BYTESOURCE_CHUNK_SIZE - src->bytes_available;
60 const size_t bytes_to_read = (len > space_left ? space_left : len);
61 assert(bytes_to_read <= BYTESOURCE_CHUNK_SIZE);
62 const ssize_t bytes_read =
63 (*src->input_func)(src->userdata,
64 src->bytes + src->bytes_available,
66 assert(bytes_read >= -1);
67 assert(bytes_read <= (ssize_t)bytes_to_read);
69 if (bytes_read == -1) {
71 } else if (bytes_read == 0) {
72 if (src->bytes_available == 1) {
73 // EOF in the middle of a marker => read error
76 assert(src->bytes_available == 0);
81 src->bytes_available += bytes_read;
84 // Now unstuff as much as we can. First of all, if there's a 0xFF at the
85 // end of the buffer, we don't include it this time; the unstuff function
86 // will only give us an error since it can't decide if it's a marker or
88 unsigned bytes_to_unstuff = src->bytes_available;
89 bool end_marker = false;
90 assert(bytes_to_unstuff > 0);
91 if (src->bytes[bytes_to_unstuff - 1] == 0xff) {
96 int unstuffed_bytes = (*unstuff_choice)(buf, src->bytes, bytes_to_unstuff);
97 assert(unstuffed_bytes != 0);
98 if (unstuffed_bytes > 0) {
99 // Fast path: No markers in the data. We can basically just
102 src->bytes_available = 1;
103 src->bytes[0] = 0xff;
105 src->bytes_available = 0;
108 return unstuffed_bytes;
111 // Slow path: There was a marker in the data. Unstuff manually until
112 // we hit the marker, then return that.
113 assert(unstuffed_bytes == -1);
115 unsigned bytes_written = 0;
116 for (bytes_read = 0; bytes_read < src->bytes_available; ++bytes_read) {
117 buf[bytes_written++] = src->bytes[bytes_read];
118 if (src->bytes[bytes_read] != MARKER_CHAR) {
122 assert(bytes_read < src->bytes_available);
123 if (src->bytes[bytes_read + 1] == STUFF_MARKER) {
124 // Skip the stuff byte.
128 // OK, this is our marker.
133 memmove(src->bytes, src->bytes + bytes_read, src->bytes_available - bytes_read);
134 src->bytes_available -= bytes_read;
135 assert(bytes_written >= 1);
136 return bytes_written - 1;