2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/buffer-view.h>
9 #include <common/dynamic-buffer.h>
10 #include <common/error.h>
14 struct lttng_buffer_view
lttng_buffer_view_init(
15 const char *src
, size_t offset
, ptrdiff_t len
)
17 struct lttng_buffer_view view
= { .data
= src
+ offset
, .size
= len
};
22 struct lttng_buffer_view
lttng_buffer_view_from_view(
23 const struct lttng_buffer_view
*src
, size_t offset
,
26 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
30 if (offset
> src
->size
) {
31 ERR("Attempt to create buffer view with invalid offset");
35 if (len
!= -1 && len
> (src
->size
- offset
)) {
36 ERR("Attempt to create buffer view with invalid length");
40 view
.data
= src
->data
+ offset
;
41 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
47 struct lttng_buffer_view
lttng_buffer_view_from_dynamic_buffer(
48 const struct lttng_dynamic_buffer
*src
, size_t offset
,
51 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
55 if (offset
> src
->size
) {
56 ERR("Attempt to create buffer view with invalid offset");
60 if (len
!= -1 && len
> (src
->size
- offset
)) {
61 ERR("Attempt to create buffer view with invalid length");
65 view
.data
= src
->data
+ offset
;
66 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
72 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view
*buf
,
74 size_t len_with_null_terminator
)
76 const char *past_buf_end
;
77 size_t max_str_len_with_null_terminator
;
81 past_buf_end
= buf
->data
+ buf
->size
;
83 /* Is the start of the string in the buffer view? */
84 if (str
< buf
->data
|| str
>= past_buf_end
) {
90 * Max length the string could have to fit in the buffer, including
93 max_str_len_with_null_terminator
= past_buf_end
- str
;
95 /* Could the string even fit in the buffer? */
96 if (len_with_null_terminator
> max_str_len_with_null_terminator
) {
101 str_len
= lttng_strnlen(str
, max_str_len_with_null_terminator
);
102 if (str_len
!= (len_with_null_terminator
- 1)) {