| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <common/buffer-view.h> |
| 19 | #include <common/dynamic-buffer.h> |
| 20 | #include <common/error.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | LTTNG_HIDDEN |
| 24 | struct lttng_buffer_view lttng_buffer_view_init( |
| 25 | const char *src, size_t offset, ptrdiff_t len) |
| 26 | { |
| 27 | struct lttng_buffer_view view = { .data = src + offset, .size = len }; |
| 28 | return view; |
| 29 | } |
| 30 | |
| 31 | LTTNG_HIDDEN |
| 32 | struct lttng_buffer_view lttng_buffer_view_from_view( |
| 33 | const struct lttng_buffer_view *src, size_t offset, |
| 34 | ptrdiff_t len) |
| 35 | { |
| 36 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 37 | |
| 38 | assert(src); |
| 39 | |
| 40 | if (offset > src->size) { |
| 41 | ERR("Attempt to create buffer view with invalid offset"); |
| 42 | goto end; |
| 43 | } |
| 44 | |
| 45 | if (len != -1 && len > (src->size - offset)) { |
| 46 | ERR("Attempt to create buffer view with invalid length"); |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | view.data = src->data + offset; |
| 51 | view.size = len == -1 ? (src->size - offset) : len; |
| 52 | end: |
| 53 | return view; |
| 54 | } |
| 55 | |
| 56 | LTTNG_HIDDEN |
| 57 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
| 58 | const struct lttng_dynamic_buffer *src, size_t offset, |
| 59 | ptrdiff_t len) |
| 60 | { |
| 61 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 62 | |
| 63 | assert(src); |
| 64 | |
| 65 | if (offset > src->size) { |
| 66 | ERR("Attempt to create buffer view with invalid offset"); |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | if (len != -1 && len > (src->size - offset)) { |
| 71 | ERR("Attempt to create buffer view with invalid length"); |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | view.data = src->data + offset; |
| 76 | view.size = len == -1 ? (src->size - offset) : len; |
| 77 | end: |
| 78 | return view; |
| 79 | } |