Debian Codegraph @

crt/aws-c-io/source/stream.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/stream.h>
7
8 #include <aws/common/file.h>
9 #include <aws/io/file_utils.h>
10 #include <aws/io/private/tracing.h>
11
12 #include <errno.h>
13
14 int aws_input_stream_seek(struct aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis) {
15 AWS_ASSERT(stream && stream->vtable && stream->vtable->seek);
16
17 return stream->vtable->seek(stream, offset, basis);
18 }
19
20 int aws_input_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
21 AWS_ASSERT(stream && stream->vtable && stream->vtable->read);
22 AWS_ASSERT(dest);
23 AWS_ASSERT(dest->len <= dest->capacity);
24
25 /* Deal with this edge case here, instead of relying on every implementation to do it right. */
26 if (dest->capacity == dest->len) {
27 return AWS_OP_SUCCESS;
28 }
29
30 /* Prevent implementations from accidentally overwriting existing data in the buffer.
31 * Hand them a "safe" buffer that starts where the existing data ends. */
32 const void *safe_buf_start = dest->buffer + dest->len;
33 const size_t safe_buf_capacity = dest->capacity - dest->len;
34 struct aws_byte_buf safe_buf = aws_byte_buf_from_empty_array(safe_buf_start, safe_buf_capacity);
35
36 __itt_task_begin(io_tracing_domain, __itt_null, __itt_null, tracing_input_stream_read);
37 int read_result = stream->vtable->read(stream, &safe_buf);
38 __itt_task_end(io_tracing_domain);
39
40 /* Ensure the implementation did not commit forbidden acts upon the buffer */
41 AWS_FATAL_ASSERT(
42 (safe_buf.buffer == safe_buf_start) && (safe_buf.capacity == safe_buf_capacity) &&
43 (safe_buf.len <= safe_buf_capacity));
44
45 if (read_result == AWS_OP_SUCCESS) {
46 /* Update the actual buffer */
47 dest->len += safe_buf.len;
48 }
49
50 return read_result;
51 }
52
53 int aws_input_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
54 AWS_ASSERT(stream && stream->vtable && stream->vtable->get_status);
55
56 return stream->vtable->get_status(stream, status);
57 }
58
59 int aws_input_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) {
60 AWS_ASSERT(stream && stream->vtable && stream->vtable->get_length);
61
62 return stream->vtable->get_length(stream, out_length);
63 }
64
65 /*
66 * cursor stream implementation
67 */
68
69 struct aws_input_stream_byte_cursor_impl {
70 struct aws_input_stream base;
71 struct aws_allocator *allocator;
72 struct aws_byte_cursor original_cursor;
73 struct aws_byte_cursor current_cursor;
74 };
75
76 /*
77 * This is an ugly function that, in the absence of better guidance, is designed to handle all possible combinations of
78 * size_t (uint32_t, uint64_t). If size_t ever exceeds 64 bits this function will fail badly.
79 *
80 * Safety and invariant assumptions are sprinkled via comments. The overall strategy is to cast up to 64 bits and
81 * perform all arithmetic there, being careful with signed vs. unsigned to prevent bad operations.
82 *
83 * Assumption #1: size_t resolves to an unsigned integer 64 bits or smaller
84 */
85
86 AWS_STATIC_ASSERT(sizeof(size_t) <= 8);
87
88 static int s_aws_input_stream_byte_cursor_seek(
89 struct aws_input_stream *stream,
90 int64_t offset,
91 enum aws_stream_seek_basis basis) {
92 struct aws_input_stream_byte_cursor_impl *impl =
93 AWS_CONTAINER_OF(stream, struct aws_input_stream_byte_cursor_impl, base);
94
95 uint64_t final_offset = 0;
96
97 switch (basis) {
98 case AWS_SSB_BEGIN:
99 /*
100 * (uint64_t)offset -- safe by virtue of the earlier is-negative check
101 * (uint64_t)impl->original_cursor.len -- safe via assumption 1
102 */
103 if (offset < 0 || (uint64_t)offset > (uint64_t)impl->original_cursor.len) {
104 return aws_raise_error(AWS_IO_STREAM_INVALID_SEEK_POSITION);
105 }
106
107 /* safe because negative offsets were turned into an error */
108 final_offset = (uint64_t)offset;
109 break;
110
111 case AWS_SSB_END:
112 /*
113 * -offset -- safe as long offset is not INT64_MIN which was previously checked
114 * (uint64_t)(-offset) -- safe because (-offset) is positive (and < INT64_MAX < UINT64_MAX)
115 */
116 if (offset > 0 || offset == INT64_MIN || (uint64_t)(-offset) > (uint64_t)impl->original_cursor.len) {
117 return aws_raise_error(AWS_IO_STREAM_INVALID_SEEK_POSITION);
118 }
119
120 /* cases that would make this unsafe became errors with previous conditional */
121 final_offset = (uint64_t)impl->original_cursor.len - (uint64_t)(-offset);
122 break;
123
124 default:
125 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
126 }
127
128 /* true because we already validated against (impl->original_cursor.len) which is <= SIZE_MAX */
129 AWS_ASSERT(final_offset <= SIZE_MAX);
130
131 /* safe via previous assert */
132 size_t final_offset_sz = (size_t)final_offset;
133
134 /* sanity */
135 AWS_ASSERT(final_offset_sz <= impl->original_cursor.len);
136
137 /* reset current_cursor to new position */
138 impl->current_cursor = impl->original_cursor;
139 impl->current_cursor.ptr += final_offset_sz;
140 impl->current_cursor.len -= final_offset_sz;
141
142 return AWS_OP_SUCCESS;
143 }
144
145 static int s_aws_input_stream_byte_cursor_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
146 struct aws_input_stream_byte_cursor_impl *impl =
147 AWS_CONTAINER_OF(stream, struct aws_input_stream_byte_cursor_impl, base);
148
149 size_t actually_read = dest->capacity - dest->len;
150 if (actually_read > impl->current_cursor.len) {
151 actually_read = impl->current_cursor.len;
152 }
153
154 if (!aws_byte_buf_write(dest, impl->current_cursor.ptr, actually_read)) {
155 return aws_raise_error(AWS_IO_STREAM_READ_FAILED);
156 }
157
158 aws_byte_cursor_advance(&impl->current_cursor, actually_read);
159
160 return AWS_OP_SUCCESS;
161 }
162
163 static int s_aws_input_stream_byte_cursor_get_status(
164 struct aws_input_stream *stream,
165 struct aws_stream_status *status) {
166 struct aws_input_stream_byte_cursor_impl *impl =
167 AWS_CONTAINER_OF(stream, struct aws_input_stream_byte_cursor_impl, base);
168
169 status->is_end_of_stream = impl->current_cursor.len == 0;
170 status->is_valid = true;
171
172 return AWS_OP_SUCCESS;
173 }
174
175 static int s_aws_input_stream_byte_cursor_get_length(struct aws_input_stream *stream, int64_t *out_length) {
176 struct aws_input_stream_byte_cursor_impl *impl =
177 AWS_CONTAINER_OF(stream, struct aws_input_stream_byte_cursor_impl, base);
178
179 #if SIZE_MAX > INT64_MAX
180 size_t length = impl->original_cursor.len;
181 if (length > INT64_MAX) {
182 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
183 }
184 #endif
185
186 *out_length = (int64_t)impl->original_cursor.len;
187
188 return AWS_OP_SUCCESS;
189 }
190
191 static void s_aws_input_stream_byte_cursor_destroy(void *user_data) {
192 struct aws_input_stream_byte_cursor_impl *impl = user_data;
193 aws_mem_release(impl->allocator, impl);
194 }
195
196 static struct aws_input_stream_vtable s_aws_input_stream_byte_cursor_vtable = {
197 .seek = s_aws_input_stream_byte_cursor_seek,
198 .read = s_aws_input_stream_byte_cursor_read,
199 .get_status = s_aws_input_stream_byte_cursor_get_status,
200 .get_length = s_aws_input_stream_byte_cursor_get_length,
201 };
202
203 struct aws_input_stream *aws_input_stream_new_from_cursor(
204 struct aws_allocator *allocator,
205 const struct aws_byte_cursor *cursor) {
206
207 struct aws_input_stream_byte_cursor_impl *impl =
208 aws_mem_calloc(allocator, 1, sizeof(struct aws_input_stream_byte_cursor_impl));
209
210 impl->allocator = allocator;
211 impl->original_cursor = *cursor;
212 impl->current_cursor = *cursor;
213 impl->base.vtable = &s_aws_input_stream_byte_cursor_vtable;
214 aws_ref_count_init(
215 &impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_stream_byte_cursor_destroy);
216
217 return &impl->base;
218 }
219
220 /*
221 * file-based input stream
222 */
223 struct aws_input_stream_file_impl {
224 struct aws_input_stream base;
225 struct aws_allocator *allocator;
226 FILE *file;
227 bool close_on_clean_up;
228 };
229
230 static int s_aws_input_stream_file_seek(
231 struct aws_input_stream *stream,
232 int64_t offset,
233 enum aws_stream_seek_basis basis) {
234 struct aws_input_stream_file_impl *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_file_impl, base);
235
236 int whence = (basis == AWS_SSB_BEGIN) ? SEEK_SET : SEEK_END;
237 if (aws_fseek(impl->file, offset, whence)) {
238 return AWS_OP_ERR;
239 }
240
241 return AWS_OP_SUCCESS;
242 }
243
244 static int s_aws_input_stream_file_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
245 struct aws_input_stream_file_impl *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_file_impl, base);
246
247 size_t max_read = dest->capacity - dest->len;
248 size_t actually_read = fread(dest->buffer + dest->len, 1, max_read, impl->file);
249 if (actually_read == 0) {
250 if (ferror(impl->file)) {
251 return aws_raise_error(AWS_IO_STREAM_READ_FAILED);
252 }
253 }
254
255 dest->len += actually_read;
256
257 return AWS_OP_SUCCESS;
258 }
259
260 static int s_aws_input_stream_file_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
261 struct aws_input_stream_file_impl *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_file_impl, base);
262
263 status->is_end_of_stream = feof(impl->file) != 0;
264 status->is_valid = ferror(impl->file) == 0;
265
266 return AWS_OP_SUCCESS;
267 }
268
269 static int s_aws_input_stream_file_get_length(struct aws_input_stream *stream, int64_t *length) {
270 struct aws_input_stream_file_impl *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_file_impl, base);
271
272 return aws_file_get_length(impl->file, length);
273 }
274
275 static void s_aws_input_stream_file_destroy(void *user_data) {
276 struct aws_input_stream_file_impl *impl = user_data;
277
278 if (impl->close_on_clean_up && impl->file) {
279 fclose(impl->file);
280 }
281 aws_mem_release(impl->allocator, impl);
282 }
283
284 static struct aws_input_stream_vtable s_aws_input_stream_file_vtable = {
285 .seek = s_aws_input_stream_file_seek,
286 .read = s_aws_input_stream_file_read,
287 .get_status = s_aws_input_stream_file_get_status,
288 .get_length = s_aws_input_stream_file_get_length,
289 };
290
291 struct aws_input_stream *aws_input_stream_new_from_file(struct aws_allocator *allocator, const char *file_name) {
292
293 struct aws_input_stream_file_impl *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_input_stream_file_impl));
294
295 impl->file = aws_fopen(file_name, "rb");
296 if (impl->file == NULL) {
297 goto on_error;
298 }
299
300 impl->close_on_clean_up = true;
301 impl->allocator = allocator;
302 impl->base.vtable = &s_aws_input_stream_file_vtable;
303 aws_ref_count_init(&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_stream_file_destroy);
304
305 return &impl->base;
306
307 on_error:
308 aws_mem_release(allocator, impl);
309 return NULL;
310 }
311
312 struct aws_input_stream *aws_input_stream_new_from_open_file(struct aws_allocator *allocator, FILE *file) {
313 struct aws_input_stream_file_impl *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_input_stream_file_impl));
314
315 impl->file = file;
316 impl->close_on_clean_up = false;
317 impl->allocator = allocator;
318
319 impl->base.vtable = &s_aws_input_stream_file_vtable;
320 aws_ref_count_init(&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_stream_file_destroy);
321 return &impl->base;
322 }
323
324 struct aws_input_stream *aws_input_stream_acquire(struct aws_input_stream *stream) {
325 if (stream != NULL) {
326 if (stream->vtable->acquire) {
327 stream->vtable->acquire(stream);
328 } else {
329 aws_ref_count_acquire(&stream->ref_count);
330 }
331 }
332 return stream;
333 }
334
335 struct aws_input_stream *aws_input_stream_release(struct aws_input_stream *stream) {
336 if (stream != NULL) {
337 if (stream->vtable->release) {
338 stream->vtable->release(stream);
339 } else {
340 aws_ref_count_release(&stream->ref_count);
341 }
342 }
343 return NULL;
344 }
345
346 void aws_input_stream_destroy(struct aws_input_stream *stream) {
347 aws_input_stream_release(stream);
348 }