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 bool lttng_buffer_view_is_valid(const struct lttng_buffer_view
*view
)
24 return view
&& view
->data
&& view
->size
> 0;
28 struct lttng_buffer_view
lttng_buffer_view_from_view(
29 const struct lttng_buffer_view
*src
, size_t offset
,
32 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
36 if (offset
> src
->size
) {
37 ERR("Attempt to create buffer view with invalid offset");
41 if (len
!= -1 && len
> (src
->size
- offset
)) {
42 ERR("Attempt to create buffer view with invalid length");
46 view
.data
= src
->data
+ offset
;
47 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
53 struct lttng_buffer_view
lttng_buffer_view_from_dynamic_buffer(
54 const struct lttng_dynamic_buffer
*src
, size_t offset
,
57 struct lttng_buffer_view view
= { .data
= NULL
, .size
= 0 };
61 if (offset
> src
->size
) {
62 ERR("Attempt to create buffer view with invalid offset");
66 if (len
!= -1 && len
> (src
->size
- offset
)) {
67 ERR("Attempt to create buffer view with invalid length");
71 view
.data
= src
->data
+ offset
;
72 view
.size
= len
== -1 ? (src
->size
- offset
) : len
;
78 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view
*buf
,
80 size_t len_with_null_terminator
)
82 const char *past_buf_end
;
83 size_t max_str_len_with_null_terminator
;
87 past_buf_end
= buf
->data
+ buf
->size
;
89 /* Is the start of the string in the buffer view? */
90 if (str
< buf
->data
|| str
>= past_buf_end
) {
96 * Max length the string could have to fit in the buffer, including
99 max_str_len_with_null_terminator
= past_buf_end
- str
;
101 /* Could the string even fit in the buffer? */
102 if (len_with_null_terminator
> max_str_len_with_null_terminator
) {
107 str_len
= lttng_strnlen(str
, max_str_len_with_null_terminator
);
108 if (str_len
!= (len_with_null_terminator
- 1)) {