Commit | Line | Data |
---|---|---|
01dc0eed | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
01dc0eed | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
01dc0eed | 5 | * |
01dc0eed JG |
6 | */ |
7 | ||
8 | #include <common/buffer-view.h> | |
9 | #include <common/dynamic-buffer.h> | |
01dc0eed JG |
10 | #include <common/error.h> |
11 | #include <assert.h> | |
12 | ||
b35aac84 JG |
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 | ||
3e6e0df2 JG |
21 | LTTNG_HIDDEN |
22 | bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view) | |
23 | { | |
24 | return view && view->data && view->size > 0; | |
25 | } | |
26 | ||
ff28f865 | 27 | LTTNG_HIDDEN |
01dc0eed JG |
28 | struct lttng_buffer_view lttng_buffer_view_from_view( |
29 | const struct lttng_buffer_view *src, size_t offset, | |
30 | ptrdiff_t len) | |
31 | { | |
32 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; | |
33 | ||
34 | assert(src); | |
35 | ||
36 | if (offset > src->size) { | |
df62dbcd JG |
37 | 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 | src->size, offset, len); | |
01dc0eed JG |
39 | goto end; |
40 | } | |
41 | ||
42 | if (len != -1 && len > (src->size - offset)) { | |
df62dbcd JG |
43 | 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", |
44 | src->size, offset, len); | |
01dc0eed JG |
45 | goto end; |
46 | } | |
47 | ||
48 | view.data = src->data + offset; | |
49 | view.size = len == -1 ? (src->size - offset) : len; | |
50 | end: | |
51 | return view; | |
52 | } | |
53 | ||
ff28f865 | 54 | LTTNG_HIDDEN |
01dc0eed JG |
55 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
56 | const struct lttng_dynamic_buffer *src, size_t offset, | |
57 | ptrdiff_t len) | |
58 | { | |
59 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; | |
60 | ||
61 | assert(src); | |
62 | ||
63 | if (offset > src->size) { | |
df62dbcd JG |
64 | 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", |
65 | src->size, offset, len); | |
01dc0eed JG |
66 | goto end; |
67 | } | |
68 | ||
69 | if (len != -1 && len > (src->size - offset)) { | |
df62dbcd JG |
70 | 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", |
71 | src->size, offset, len); | |
01dc0eed JG |
72 | goto end; |
73 | } | |
74 | ||
75 | view.data = src->data + offset; | |
76 | view.size = len == -1 ? (src->size - offset) : len; | |
77 | end: | |
78 | return view; | |
79 | } | |
67d8e2ef SM |
80 | |
81 | LTTNG_HIDDEN | |
82 | bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf, | |
83 | const char *str, | |
84 | size_t len_with_null_terminator) | |
85 | { | |
86 | const char *past_buf_end; | |
87 | size_t max_str_len_with_null_terminator; | |
88 | size_t str_len; | |
89 | bool ret; | |
90 | ||
91 | past_buf_end = buf->data + buf->size; | |
92 | ||
93 | /* Is the start of the string in the buffer view? */ | |
94 | if (str < buf->data || str >= past_buf_end) { | |
95 | ret = false; | |
96 | goto end; | |
97 | } | |
98 | ||
99 | /* | |
100 | * Max length the string could have to fit in the buffer, including | |
101 | * NULL terminator. | |
102 | */ | |
103 | max_str_len_with_null_terminator = past_buf_end - str; | |
104 | ||
105 | /* Could the string even fit in the buffer? */ | |
106 | if (len_with_null_terminator > max_str_len_with_null_terminator) { | |
107 | ret = false; | |
108 | goto end; | |
109 | } | |
110 | ||
111 | str_len = lttng_strnlen(str, max_str_len_with_null_terminator); | |
112 | if (str_len != (len_with_null_terminator - 1)) { | |
113 | ret = false; | |
114 | goto end; | |
115 | } | |
116 | ||
117 | ret = true; | |
118 | ||
119 | end: | |
120 | return ret; | |
121 | } |