1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// Copyright 2021 Nick Brassel (@tzarc)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "qp_stream.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Stream API
uint32_t qp_stream_read_impl(void *output_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream) {
uint8_t *output_ptr = (uint8_t *)output_buf;
uint32_t i;
for (i = 0; i < (num_members * member_size); ++i) {
int16_t c = qp_stream_get(stream);
if (c < 0) {
break;
}
output_ptr[i] = (uint8_t)(c & 0xFF);
}
return i / member_size;
}
uint32_t qp_stream_write_impl(const void *input_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream) {
uint8_t *input_ptr = (uint8_t *)input_buf;
uint32_t i;
for (i = 0; i < (num_members * member_size); ++i) {
if (!qp_stream_put(stream, input_ptr[i])) {
break;
}
}
return i / member_size;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Memory streams
int16_t mem_get(qp_stream_t *stream) {
qp_memory_stream_t *s = (qp_memory_stream_t *)stream;
if (s->position >= s->length) {
s->is_eof = true;
return STREAM_EOF;
}
return s->buffer[s->position++];
}
bool mem_put(qp_stream_t *stream, uint8_t c) {
qp_memory_stream_t *s = (qp_memory_stream_t *)stream;
if (s->position >= s->length) {
s->is_eof = true;
return false;
}
s->buffer[s->position++] = c;
return true;
}
int mem_seek(qp_stream_t *stream, int32_t offset, int origin) {
qp_memory_stream_t *s = (qp_memory_stream_t *)stream;
// Handle as per fseek
int32_t position = s->position;
switch (origin) {
case SEEK_SET:
position = offset;
break;
case SEEK_CUR:
position += offset;
break;
case SEEK_END:
position = s->length + offset;
break;
default:
return -1;
}
// If we're before the start, ignore it.
if (position < 0) {
return -1;
}
// If we're at the end it's okay, we only care if we're after the end for failure purposes -- as per lseek()
if (position > s->length) {
return -1;
}
// Update the offset
s->position = position;
// Successful invocation of fseek() results in clearing of the EOF flag by default, mirror the same functionality
s->is_eof = false;
return 0;
}
int32_t mem_tell(qp_stream_t *stream) {
qp_memory_stream_t *s = (qp_memory_stream_t *)stream;
return s->position;
}
bool mem_is_eof(qp_stream_t *stream) {
qp_memory_stream_t *s = (qp_memory_stream_t *)stream;
return s->is_eof;
}
qp_memory_stream_t qp_make_memory_stream(void *buffer, int32_t length) {
qp_memory_stream_t stream = {
.base =
{
.get = mem_get,
.put = mem_put,
.seek = mem_seek,
.tell = mem_tell,
.is_eof = mem_is_eof,
},
.buffer = (uint8_t *)buffer,
.length = length,
.position = 0,
};
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FILE streams
#ifdef QP_STREAM_HAS_FILE_IO
int16_t file_get(qp_stream_t *stream) {
qp_file_stream_t *s = (qp_file_stream_t *)stream;
int c = fgetc(s->file);
if (c < 0 || feof(s->file)) return STREAM_EOF;
return (uint16_t)c;
}
bool file_put(qp_stream_t *stream, uint8_t c) {
qp_file_stream_t *s = (qp_file_stream_t *)stream;
return fputc(c, s->file) == c;
}
int file_seek(qp_stream_t *stream, int32_t offset, int origin) {
qp_file_stream_t *s = (qp_file_stream_t *)stream;
return fseek(s->file, offset, origin);
}
int32_t file_tell(qp_stream_t *stream) {
qp_file_stream_t *s = (qp_file_stream_t *)stream;
return (int32_t)ftell(s->file);
}
bool file_is_eof(qp_stream_t *stream) {
qp_file_stream_t *s = (qp_file_stream_t *)stream;
return (bool)feof(s->file);
}
qp_file_stream_t qp_make_file_stream(FILE *f) {
qp_file_stream_t stream = {
.base =
{
.get = file_get,
.put = file_put,
.seek = file_seek,
.tell = file_tell,
.is_eof = file_is_eof,
},
.file = f,
};
return stream;
}
#endif // QP_STREAM_HAS_FILE_IO
|