| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/buffer-view.h> |
| 9 | #include <common/dynamic-buffer.h> |
| 10 | #include <common/error.h> |
| 11 | #include <assert.h> |
| 12 | |
| 13 | LTTNG_HIDDEN |
| 14 | struct lttng_buffer_view lttng_buffer_view_init( |
| 15 | const char *src, size_t offset, ptrdiff_t len) |
| 16 | { |
| 17 | struct lttng_buffer_view view = { .data = src + offset, .size = len }; |
| 18 | return view; |
| 19 | } |
| 20 | |
| 21 | LTTNG_HIDDEN |
| 22 | struct lttng_buffer_view lttng_buffer_view_from_view( |
| 23 | const struct lttng_buffer_view *src, size_t offset, |
| 24 | ptrdiff_t len) |
| 25 | { |
| 26 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 27 | |
| 28 | assert(src); |
| 29 | |
| 30 | if (offset > src->size) { |
| 31 | ERR("Attempt to create buffer view with invalid offset"); |
| 32 | goto end; |
| 33 | } |
| 34 | |
| 35 | if (len != -1 && len > (src->size - offset)) { |
| 36 | ERR("Attempt to create buffer view with invalid length"); |
| 37 | goto end; |
| 38 | } |
| 39 | |
| 40 | view.data = src->data + offset; |
| 41 | view.size = len == -1 ? (src->size - offset) : len; |
| 42 | end: |
| 43 | return view; |
| 44 | } |
| 45 | |
| 46 | LTTNG_HIDDEN |
| 47 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
| 48 | const struct lttng_dynamic_buffer *src, size_t offset, |
| 49 | ptrdiff_t len) |
| 50 | { |
| 51 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 52 | |
| 53 | assert(src); |
| 54 | |
| 55 | if (offset > src->size) { |
| 56 | ERR("Attempt to create buffer view with invalid offset"); |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | if (len != -1 && len > (src->size - offset)) { |
| 61 | ERR("Attempt to create buffer view with invalid length"); |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | view.data = src->data + offset; |
| 66 | view.size = len == -1 ? (src->size - offset) : len; |
| 67 | end: |
| 68 | return view; |
| 69 | } |