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.hpp>
9 #include <common/dynamic-buffer.hpp>
10 #include <common/error.hpp>
12 struct lttng_buffer_view
lttng_buffer_view_init(const char *src
, size_t offset
, ptrdiff_t len
)
14 struct lttng_buffer_view view
= { .data
= src
+ offset
, .size
= (size_t) len
};
18 bool lttng_buffer_view_is_valid(const struct lttng_buffer_view
*view
)
20 return view
&& view
->data
&& view
->size
> 0;
23 struct lttng_buffer_view
24 lttng_buffer_view_from_view(const struct lttng_buffer_view
*src
, size_t offset
, ptrdiff_t len
)
26 struct lttng_buffer_view view
= { .data
= nullptr, .size
= 0 };
30 if (offset
> src
->size
) {
31 ERR("Attempt to create buffer view from another view with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
38 if (len
!= -1 && len
> (src
->size
- offset
)) {
39 ERR("Attempt to create buffer view from another view with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
46 view
.data
= src
->data
+ offset
;
47 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
52 struct lttng_buffer_view
lttng_buffer_view_from_dynamic_buffer(
53 const struct lttng_dynamic_buffer
*src
, size_t offset
, ptrdiff_t len
)
55 struct lttng_buffer_view view
= { .data
= nullptr, .size
= 0 };
59 if (offset
> src
->size
) {
60 ERR("Attempt to create buffer view from a dynamic buffer with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
67 if (len
!= -1 && len
> (src
->size
- offset
)) {
68 ERR("Attempt to create buffer view from a dynamic buffer with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
75 view
.data
= src
->data
+ offset
;
76 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
81 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view
*buf
,
83 size_t len_with_null_terminator
)
85 const char *past_buf_end
;
86 size_t max_str_len_with_null_terminator
;
90 past_buf_end
= buf
->data
+ buf
->size
;
92 /* Is the start of the string in the buffer view? */
93 if (str
< buf
->data
|| str
>= past_buf_end
) {
99 * Max length the string could have to fit in the buffer, including
102 max_str_len_with_null_terminator
= past_buf_end
- str
;
104 /* Could the string even fit in the buffer? */
105 if (len_with_null_terminator
> max_str_len_with_null_terminator
) {
110 str_len
= lttng_strnlen(str
, max_str_len_with_null_terminator
);
111 if (str_len
!= (len_with_null_terminator
- 1)) {