|
| 1 | +/* Copyright 2016 Samsung Electronics Co., Ltd. |
| 2 | + * Copyright 2016 University of Szeged. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "jrt.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * Read data from a specified buffer. |
| 21 | + * |
| 22 | + * Note: |
| 23 | + * Offset is in-out and is incremented if the read operation completes successfully. |
| 24 | + * |
| 25 | + * @return true, if read was successful, i.e. offset + data_size doesn't exceed buffer size, |
| 26 | + * false - otherwise. |
| 27 | + */ |
| 28 | +bool __attr_always_inline___ |
| 29 | +jrt_read_from_buffer_by_offset (const uint8_t *buffer_p, /**< buffer */ |
| 30 | + size_t buffer_size, /**< size of buffer */ |
| 31 | + size_t *in_out_buffer_offset_p, /**< in: offset to read from, |
| 32 | + * out: offset, incremented on sizeof (T) */ |
| 33 | + void *out_data_p, /**< out: data */ |
| 34 | + size_t out_data_size) /**< size of the readable data */ |
| 35 | +{ |
| 36 | + if (*in_out_buffer_offset_p + out_data_size > buffer_size) |
| 37 | + { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + memcpy (out_data_p, buffer_p + *in_out_buffer_offset_p, out_data_size); |
| 42 | + *in_out_buffer_offset_p += out_data_size; |
| 43 | + |
| 44 | + return true; |
| 45 | +} /* jrt_read_from_buffer_by_offset */ |
| 46 | + |
| 47 | +/** |
| 48 | + * Write data to a specified buffer. |
| 49 | + * |
| 50 | + * Note: |
| 51 | + * Offset is in-out and is incremented if the write operation completes successfully. |
| 52 | + * |
| 53 | + * @return true, if write was successful, i.e. offset + data_size doesn't exceed buffer size, |
| 54 | + * false - otherwise. |
| 55 | + */ |
| 56 | +bool __attr_always_inline___ |
| 57 | +jrt_write_to_buffer_by_offset (uint8_t *buffer_p, /**< buffer */ |
| 58 | + size_t buffer_size, /**< size of buffer */ |
| 59 | + size_t *in_out_buffer_offset_p, /**< in: offset to read from, |
| 60 | + * out: offset, incremented on sizeof (T) */ |
| 61 | + const void *data_p, /**< data */ |
| 62 | + size_t data_size) /**< size of the writable data */ |
| 63 | +{ |
| 64 | + if (*in_out_buffer_offset_p + data_size > buffer_size) |
| 65 | + { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + memcpy (buffer_p + *in_out_buffer_offset_p, data_p, data_size); |
| 70 | + *in_out_buffer_offset_p += data_size; |
| 71 | + |
| 72 | + return true; |
| 73 | +} /* jrt_write_to_buffer_by_offset */ |
0 commit comments