]> git.sesse.net Git - fjl/blob - bytesource.c
Move some common input stuff around.
[fjl] / bytesource.c
1 #include <stdbool.h>
2 #include <string.h>
3 #include <assert.h>
4
5 #include "choice.h"
6 #include "bytesource.h"
7
8 #define MARKER_CHAR 0xff
9 #define STUFF_MARKER 0x00
10
11 void init_byte_source(struct byte_source* source, raw_input_func_t* input_func, void* userdata)
12 {
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;
18 }
19
20 uint8_t byte_source_read_marker(struct byte_source* source)
21 {
22         assert(source->bytes_available >= 2);
23         assert(source->bytes[0] == MARKER_CHAR);
24         assert(source->bytes[1] != STUFF_MARKER);
25
26         uint8_t ret = source->bytes[1];
27
28         memmove(source->bytes, source->bytes + 2, source->bytes_available - 2);
29         source->bytes_available -= 2;
30
31         return ret;
32 }
33
34 ssize_t byte_source_input_func(void* source, uint8_t* buf, size_t len)
35 {
36         struct byte_source* src = (struct byte_source*)source;
37
38         // If there's no data in the buffer (or only a partial marker), we have
39         // to read in more from our upstream src.
40         while (src->bytes_available == 0 ||
41                (src->bytes_available == 1 && src->bytes[0] == MARKER_CHAR)) {
42                 const unsigned space_left = BYTESOURCE_CHUNK_SIZE - src->bytes_available;
43                 const size_t bytes_to_read = (len > space_left ? space_left : len);
44                 assert(bytes_to_read <= BYTESOURCE_CHUNK_SIZE);
45                 const ssize_t bytes_read =
46                         (*src->input_func)(src->userdata,
47                                               src->bytes + src->bytes_available,
48                                               bytes_to_read);
49                 assert(bytes_read >= -1);
50                 assert(bytes_read <= bytes_to_read);
51                 
52                 if (bytes_read == -1) {
53                         return -1;
54                 } else if (bytes_read == 0) {
55                         if (src->bytes_available == 1) {
56                                 // EOF in the middle of a marker => read error
57                                 return -1;
58                         } else {
59                                 assert(src->bytes_available == 0);
60                                 return 0;
61                         }
62                 }
63
64                 src->bytes_available += bytes_read;
65         }
66         
67         // Now unstuff as much as we can. First of all, if there's a 0xFF at the
68         // end of the buffer, we don't include it this time; the unstuff function
69         // will only give us an error since it can't decide if it's a marker or
70         // a stuffed 0xFF.
71         unsigned bytes_to_unstuff = src->bytes_available;
72         bool end_marker = false;
73         assert(bytes_to_unstuff > 0);
74         if (src->bytes[bytes_to_unstuff - 1] == 0xff) {
75                 --bytes_to_unstuff;
76                 end_marker = true;
77         }
78
79         int unstuffed_bytes = (*unstuff_choice)(buf, src->bytes, bytes_to_unstuff);
80         assert(unstuffed_bytes != 0);
81         if (unstuffed_bytes > 0) {
82                 // Fast path: No markers in the data. We can basically just
83                 // return it.
84                 if (end_marker) {
85                         src->bytes_available = 1;
86                         src->bytes[0] = 0xff;
87                 } else {
88                         src->bytes_available = 0;
89                         src->bytes[0] = 0;
90                 }
91                 return unstuffed_bytes;
92         }
93
94         // Slow path: There was a marker in the data. Unstuff manually until
95         // we hit the marker, then return that.
96         assert(unstuffed_bytes == -1);
97         unsigned bytes_read;
98         unsigned bytes_written = 0;
99         for (bytes_read = 0; bytes_read < src->bytes_available; ++bytes_read) {
100                 buf[bytes_written++] = src->bytes[bytes_read];
101                 if (src->bytes[bytes_read] != MARKER_CHAR) {
102                         continue;
103                 }
104
105                 assert(bytes_read < src->bytes_available);
106                 if (src->bytes[bytes_read + 1] == STUFF_MARKER) {
107                         // Skip the stuff byte.
108                         ++bytes_read;
109                         continue;
110                 } else {
111                         // OK, this is our marker.
112                         break;
113                 }       
114         }
115
116         memmove(src->bytes, src->bytes + bytes_read, src->bytes_available - bytes_read);
117         src->bytes_available -= bytes_read;
118         assert(bytes_written >= 1);
119         return bytes_written - 1;
120 }