| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "fcml_stream.h" |
| 21 |
|
| 22 |
#include "fcml_env_int.h" |
| 23 |
|
| 24 |
void fcml_fn_stream_seek(fcml_st_memory_stream *stream, fcml_int32_t offset, |
| 25 |
fcml_en_stream_seek_type type) { |
| 26 |
switch (type) { |
| 27 |
case FCML_EN_ST_START: |
| 28 |
stream->offset = offset; |
| 29 |
break; |
| 30 |
case FCML_EN_ST_CURRENT: |
| 31 |
stream->offset += offset; |
| 32 |
break; |
| 33 |
case FCML_EN_ST_END: |
| 34 |
stream->offset = (stream->size + offset); |
| 35 |
break; |
| 36 |
} |
| 37 |
if (stream->offset < 0) { |
| 38 |
stream->offset = 0; |
| 39 |
} |
| 40 |
if (stream->offset > stream->size) { |
| 41 |
stream->offset = stream->size; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
void fcml_fn_stream_clean(fcml_st_memory_stream *stream) { |
| 46 |
fcml_fn_env_memory_clear(stream->base_address, stream->size); |
| 47 |
stream->offset = 0; |
| 48 |
} |
| 49 |
|
| 50 |
fcml_uint8_t fcml_fn_stream_read(fcml_st_memory_stream *stream, |
| 51 |
fcml_bool *result) { |
| 52 |
fcml_uint8_t *base_address = (fcml_uint8_t*) stream->base_address; |
| 53 |
*result = (stream->offset == stream->size) ? FCML_FALSE : FCML_TRUE; |
| 54 |
if (*result) { |
| 55 |
return base_address[stream->offset++]; |
| 56 |
} |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
fcml_bool fcml_fn_stream_write(fcml_st_memory_stream *stream, fcml_uint8_t data) { |
| 61 |
fcml_bool result = FCML_FALSE; |
| 62 |
if (stream->offset < stream->size) { |
| 63 |
fcml_uint8_t *base_address = (fcml_uint8_t*) stream->base_address; |
| 64 |
base_address[stream->offset++] = data; |
| 65 |
result = FCML_TRUE; |
| 66 |
} |
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
fcml_uint8_t fcml_fn_stream_peek(fcml_st_memory_stream *stream, |
| 71 |
fcml_bool *result) { |
| 72 |
fcml_uint8_t *base_address = (fcml_uint8_t*) stream->base_address; |
| 73 |
*result = (stream->offset == stream->size) ? FCML_FALSE : FCML_TRUE; |
| 74 |
if (*result) |
| 75 |
return base_address[stream->offset]; |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
fcml_int32_t fcml_fn_stream_size(const fcml_st_memory_stream *stream) { |
| 80 |
return stream->size - stream->offset; |
| 81 |
} |
| 82 |
|
| 83 |
int fcml_fn_stream_read_bytes(fcml_st_memory_stream *stream, fcml_ptr buffer, |
| 84 |
fcml_usize size) { |
| 85 |
if (size == 0) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
fcml_uint8_t *destination_buffer = (fcml_uint8_t*) buffer; |
| 89 |
fcml_uint8_t *source_buffer = (fcml_uint8_t*) stream->base_address; |
| 90 |
fcml_usize i = 0; |
| 91 |
while (stream->offset < stream->size && i < size) { |
| 92 |
destination_buffer[i++] = source_buffer[stream->offset++]; |
| 93 |
} |
| 94 |
return i; |
| 95 |
} |
| 96 |
|
| 97 |
int fcml_fn_stream_write_bytes(fcml_st_memory_stream *stream, fcml_ptr buffer, |
| 98 |
fcml_usize size) { |
| 99 |
if (size == 0) { |
| 100 |
return 0; |
| 101 |
} |
| 102 |
fcml_uint8_t *source_buffer = (fcml_uint8_t*) buffer; |
| 103 |
fcml_uint8_t *destination_buffer = (fcml_uint8_t*) stream->base_address; |
| 104 |
fcml_usize i = 0; |
| 105 |
while (stream->offset < stream->size && i < size) { |
| 106 |
destination_buffer[stream->offset++] = source_buffer[i++]; |
| 107 |
} |
| 108 |
return i; |
| 109 |
} |
| 110 |
|
| 111 |
fcml_uint16_t fcml_fn_stream_read_word(fcml_st_memory_stream *stream, |
| 112 |
fcml_bool *result) { |
| 113 |
fcml_uint16_t value = 0; |
| 114 |
if (stream->size - stream->offset >= sizeof(fcml_uint16_t)) { |
| 115 |
int offset = stream->offset; |
| 116 |
value |= ((fcml_uint8_t*) stream->base_address)[offset]; |
| 117 |
value |= ((fcml_uint8_t*) stream->base_address)[offset + 1] << 8; |
| 118 |
stream->offset += sizeof(fcml_uint16_t); |
| 119 |
*result = FCML_TRUE; |
| 120 |
} else { |
| 121 |
*result = FCML_FALSE; |
| 122 |
} |
| 123 |
return value; |
| 124 |
} |
| 125 |
|
| 126 |
fcml_uint32_t fcml_fn_stream_read_dword(fcml_st_memory_stream *stream, |
| 127 |
fcml_bool *result) { |
| 128 |
fcml_uint32_t value = 0; |
| 129 |
if (stream->size - stream->offset >= sizeof(fcml_uint32_t)) { |
| 130 |
int offset = stream->offset; |
| 131 |
value |= ((fcml_uint8_t*) stream->base_address)[offset]; |
| 132 |
value |= ((fcml_uint8_t*) stream->base_address)[offset + 1] << 8; |
| 133 |
value |= ((fcml_uint8_t*) stream->base_address)[offset + 2] << 16; |
| 134 |
value |= ((fcml_uint8_t*) stream->base_address)[offset + 3] << 24; |
| 135 |
stream->offset += sizeof(fcml_uint32_t); |
| 136 |
*result = FCML_TRUE; |
| 137 |
} else { |
| 138 |
*result = FCML_FALSE; |
| 139 |
} |
| 140 |
return value; |
| 141 |
} |
| 142 |
|
| 143 |
fcml_uint64_t fcml_fn_stream_read_qword(fcml_st_memory_stream *stream, |
| 144 |
fcml_bool *result) { |
| 145 |
fcml_uint64_t value = 0; |
| 146 |
if (stream->size - stream->offset >= sizeof(fcml_uint64_t)) { |
| 147 |
int i; |
| 148 |
int offset = stream->offset; |
| 149 |
for (i = 0; i < sizeof(fcml_uint64_t); i++) { |
| 150 |
value |= |
| 151 |
((fcml_uint64_t) ((fcml_uint8_t*) stream->base_address)[offset |
| 152 |
+ i]) << (i << 3); |
| 153 |
} |
| 154 |
stream->offset += sizeof(fcml_uint64_t); |
| 155 |
*result = FCML_TRUE; |
| 156 |
} else { |
| 157 |
*result = FCML_FALSE; |
| 158 |
} |
| 159 |
return value; |
| 160 |
} |
| 161 |
|
| 162 |
fcml_bool fcml_fn_stream_write_word(fcml_st_memory_stream *stream, |
| 163 |
fcml_uint16_t data) { |
| 164 |
fcml_bool result = FCML_FALSE; |
| 165 |
if (stream->size - stream->offset >= sizeof(fcml_uint16_t)) { |
| 166 |
int offset = stream->offset; |
| 167 |
fcml_uint8_t *buffer = (fcml_uint8_t*) stream->base_address; |
| 168 |
buffer[offset] = data & 0x00FF; |
| 169 |
buffer[offset + 1] = data >> 8; |
| 170 |
stream->offset += sizeof(fcml_uint16_t); |
| 171 |
result = FCML_TRUE; |
| 172 |
} |
| 173 |
return result; |
| 174 |
} |
| 175 |
|
| 176 |
fcml_bool fcml_fn_stream_write_dword(fcml_st_memory_stream *stream, |
| 177 |
fcml_uint32_t data) { |
| 178 |
fcml_bool result = FCML_FALSE; |
| 179 |
if (stream->size - stream->offset >= sizeof(fcml_uint32_t)) { |
| 180 |
int offset = stream->offset; |
| 181 |
((fcml_uint8_t*) stream->base_address)[offset] = data; |
| 182 |
((fcml_uint8_t*) stream->base_address)[offset + 1] = data >> 8; |
| 183 |
((fcml_uint8_t*) stream->base_address)[offset + 2] = data >> 16; |
| 184 |
((fcml_uint8_t*) stream->base_address)[offset + 3] = data >> 24; |
| 185 |
stream->offset += sizeof(fcml_uint32_t); |
| 186 |
result = FCML_TRUE; |
| 187 |
} |
| 188 |
return result; |
| 189 |
} |
| 190 |
|
| 191 |
fcml_bool fcml_fn_stream_write_qword(fcml_st_memory_stream *stream, |
| 192 |
fcml_uint64_t data) { |
| 193 |
fcml_bool result = FCML_FALSE; |
| 194 |
if (stream->size - stream->offset >= sizeof(fcml_uint64_t)) { |
| 195 |
int i; |
| 196 |
int offset = stream->offset; |
| 197 |
fcml_uint8_t *buffer = (fcml_uint8_t*) stream->base_address; |
| 198 |
for (i = 0; i < sizeof(fcml_uint64_t); i++) { |
| 199 |
buffer[offset + i] = (fcml_uint8_t) (data >> (i << 3)); |
| 200 |
} |
| 201 |
stream->offset += sizeof(fcml_uint64_t); |
| 202 |
result = FCML_TRUE; |
| 203 |
} |
| 204 |
return result; |
| 205 |
} |
| 206 |
|
| 207 |
fcml_stream_pointer fcml_fn_stream_save_point( |
| 208 |
const fcml_st_memory_stream *stream) { |
| 209 |
return stream->offset; |
| 210 |
} |
| 211 |
|
| 212 |
void fcml_fn_stream_restore_point(fcml_st_memory_stream *stream, |
| 213 |
const fcml_stream_pointer stream_pointer) { |
| 214 |
stream->offset = stream_pointer; |
| 215 |
} |
| 216 |
|
| 217 |
fcml_st_memory_stream fcml_fn_stream_wrap( fcml_string buffer, |
| 218 |
fcml_usize length) { |
| 219 |
fcml_st_memory_stream stream = { 0 }; |
| 220 |
stream.base_address = buffer; |
| 221 |
stream.offset = 0; |
| 222 |
stream.size = length; |
| 223 |
return stream; |
| 224 |
} |