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>
12 struct lttng_buffer_view
lttng_buffer_view_init(
13 const char *src
, size_t offset
, ptrdiff_t len
)
15 struct lttng_buffer_view view
= { .data
= src
+ offset
, .size
= (size_t) len
};
19 bool lttng_buffer_view_is_valid(const struct lttng_buffer_view
*view
)
21 return view
&& view
->data
&& view
->size
> 0;
24 struct lttng_buffer_view
lttng_buffer_view_from_view(
25 const struct lttng_buffer_view
*src
, size_t offset
,
28 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
32 if (offset
> src
->size
) {
33 ERR("Attempt to create buffer view from another view with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
34 src
->size
, offset
, len
);
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",
40 src
->size
, offset
, len
);
44 view
.data
= src
->data
+ offset
;
45 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
50 struct lttng_buffer_view
lttng_buffer_view_from_dynamic_buffer(
51 const struct lttng_dynamic_buffer
*src
, size_t offset
,
54 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
58 if (offset
> src
->size
) {
59 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",
60 src
->size
, offset
, len
);
64 if (len
!= -1 && len
> (src
->size
- offset
)) {
65 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",
66 src
->size
, offset
, len
);
70 view
.data
= src
->data
+ offset
;
71 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
76 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view
*buf
,
78 size_t len_with_null_terminator
)
80 const char *past_buf_end
;
81 size_t max_str_len_with_null_terminator
;
85 past_buf_end
= buf
->data
+ buf
->size
;
87 /* Is the start of the string in the buffer view? */
88 if (str
< buf
->data
|| str
>= past_buf_end
) {
94 * Max length the string could have to fit in the buffer, including
97 max_str_len_with_null_terminator
= past_buf_end
- str
;
99 /* Could the string even fit in the buffer? */
100 if (len_with_null_terminator
> max_str_len_with_null_terminator
) {
105 str_len
= lttng_strnlen(str
, max_str_len_with_null_terminator
);
106 if (str_len
!= (len_with_null_terminator
- 1)) {