Line data Source code
1 : /* File: universal_memory_input_stream.c; Copyright and License: see below */ 2 : 3 : #include "u8stream/universal_memory_input_stream.h" 4 : #include "u8stream/universal_input_stream_if.h" 5 : #include "u8/u8_trace.h" 6 : #include "u8/u8_log.h" 7 : #include <string.h> 8 : #include <assert.h> 9 : 10 : /* the vmt implementing the interface */ 11 : static const universal_input_stream_if_t universal_memory_input_stream_private_if 12 : = { 13 : .read = (u8_error_t (*)(universal_input_stream_impl_t*, void*, size_t, size_t*)) &universal_memory_input_stream_read, 14 : .reset = (u8_error_t (*)(universal_input_stream_impl_t*)) &universal_memory_input_stream_reset 15 : }; 16 : 17 76 : void universal_memory_input_stream_init ( universal_memory_input_stream_t *this_, 18 : const void* mem_buf_start, 19 : size_t mem_buf_size ) 20 : { 21 76 : U8_TRACE_BEGIN(); 22 76 : assert( mem_buf_start != NULL ); 23 : 24 76 : (*this_).mem_buf_start = mem_buf_start; 25 76 : (*this_).mem_buf_size = mem_buf_size; 26 76 : (*this_).mem_buf_pos = 0; 27 76 : universal_input_stream_private_init( &((*this_).input_stream), &universal_memory_input_stream_private_if, this_ ); 28 : 29 76 : U8_TRACE_END(); 30 76 : } 31 : 32 49 : void universal_memory_input_stream_reinit ( universal_memory_input_stream_t *this_, 33 : const void* mem_buf_start, 34 : size_t mem_buf_size ) 35 : { 36 49 : U8_TRACE_BEGIN(); 37 : 38 49 : universal_memory_input_stream_destroy( this_ ); 39 49 : universal_memory_input_stream_init( this_, mem_buf_start, mem_buf_size ); 40 : 41 49 : U8_TRACE_END(); 42 49 : } 43 : 44 76 : void universal_memory_input_stream_destroy( universal_memory_input_stream_t *this_ ) 45 : { 46 76 : U8_TRACE_BEGIN(); 47 : 48 76 : (*this_).mem_buf_start = NULL; 49 76 : (*this_).mem_buf_size = 0; 50 76 : (*this_).mem_buf_pos = 0; 51 76 : universal_input_stream_private_destroy( &((*this_).input_stream) ); 52 : 53 76 : U8_TRACE_END(); 54 76 : } 55 : 56 4 : u8_error_t universal_memory_input_stream_reset ( universal_memory_input_stream_t *this_ ) 57 : { 58 4 : U8_TRACE_BEGIN(); 59 4 : assert( (*this_).mem_buf_start != NULL ); 60 4 : const u8_error_t err = U8_ERROR_NONE; 61 : 62 4 : (*this_).mem_buf_pos = 0; 63 : 64 4 : U8_TRACE_END_ERR(err); 65 4 : return err; 66 : } 67 : 68 147 : u8_error_t universal_memory_input_stream_read ( universal_memory_input_stream_t *this_, void *out_buffer, size_t max_size, size_t *out_length ) 69 : { 70 : /*U8_TRACE_BEGIN();*/ 71 147 : assert( out_buffer != NULL ); 72 147 : assert( max_size != 0 ); 73 147 : assert( out_length != NULL ); 74 147 : assert( (*this_).mem_buf_start != NULL ); 75 147 : u8_error_t err = U8_ERROR_NONE; 76 : 77 147 : const size_t stream_bytes_left = ( (*this_).mem_buf_size - (*this_).mem_buf_pos ); 78 147 : if ( stream_bytes_left != 0 ) 79 : { 80 119 : const size_t bytes_to_copy = (max_size <= stream_bytes_left) ? max_size : stream_bytes_left; 81 119 : char *const buf_first_read = &( (*( (char(*)[])(*this_).mem_buf_start ))[(*this_).mem_buf_pos] ); 82 : 83 119 : memcpy( out_buffer, buf_first_read, bytes_to_copy ); 84 119 : (*this_).mem_buf_pos += bytes_to_copy; 85 119 : *out_length = bytes_to_copy; 86 : } 87 : else 88 : { 89 28 : err = U8_ERROR_END_OF_STREAM; /* finished, no more bytes to read */ 90 28 : *out_length = 0; 91 : } 92 : 93 : /*U8_TRACE_END_ERR(err);*/ 94 147 : return err; 95 : } 96 : 97 76 : universal_input_stream_t* universal_memory_input_stream_get_input_stream( universal_memory_input_stream_t *this_ ) 98 : { 99 76 : U8_TRACE_BEGIN(); 100 : 101 76 : universal_input_stream_t* result = &((*this_).input_stream); 102 : 103 76 : U8_TRACE_END(); 104 76 : return result; 105 : } 106 : 107 : 108 : /* 109 : Copyright 2021-2024 Andreas Warnke 110 : 111 : Licensed under the Apache License, Version 2.0 (the "License"); 112 : you may not use this file except in compliance with the License. 113 : You may obtain a copy of the License at 114 : 115 : http://www.apache.org/licenses/LICENSE-2.0 116 : 117 : Unless required by applicable law or agreed to in writing, software 118 : distributed under the License is distributed on an "AS IS" BASIS, 119 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120 : See the License for the specific language governing permissions and 121 : limitations under the License. 122 : */