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 | ||
ff28f865 | 21 | LTTNG_HIDDEN |
01dc0eed JG |
22 | struct lttng_buffer_view lttng_buffer_view_from_view( |
23 | const struct lttng_buffer_view *src, size_t offset, | |
24 | ptrdiff_t len) | |
25 | { | |
26 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; | |
27 | ||
28 | assert(src); | |
29 | ||
30 | if (offset > src->size) { | |
31 | ERR("Attempt to create buffer view with invalid offset"); | |
32 | goto end; | |
33 | } | |
34 | ||
35 | if (len != -1 && len > (src->size - offset)) { | |
36 | ERR("Attempt to create buffer view with invalid length"); | |
37 | goto end; | |
38 | } | |
39 | ||
40 | view.data = src->data + offset; | |
41 | view.size = len == -1 ? (src->size - offset) : len; | |
42 | end: | |
43 | return view; | |
44 | } | |
45 | ||
ff28f865 | 46 | LTTNG_HIDDEN |
01dc0eed JG |
47 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
48 | const struct lttng_dynamic_buffer *src, size_t offset, | |
49 | ptrdiff_t len) | |
50 | { | |
51 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; | |
52 | ||
53 | assert(src); | |
54 | ||
55 | if (offset > src->size) { | |
56 | ERR("Attempt to create buffer view with invalid offset"); | |
57 | goto end; | |
58 | } | |
59 | ||
60 | if (len != -1 && len > (src->size - offset)) { | |
61 | ERR("Attempt to create buffer view with invalid length"); | |
62 | goto end; | |
63 | } | |
64 | ||
65 | view.data = src->data + offset; | |
66 | view.size = len == -1 ? (src->size - offset) : len; | |
67 | end: | |
68 | return view; | |
69 | } | |
67d8e2ef SM |
70 | |
71 | LTTNG_HIDDEN | |
72 | bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf, | |
73 | const char *str, | |
74 | size_t len_with_null_terminator) | |
75 | { | |
76 | const char *past_buf_end; | |
77 | size_t max_str_len_with_null_terminator; | |
78 | size_t str_len; | |
79 | bool ret; | |
80 | ||
81 | past_buf_end = buf->data + buf->size; | |
82 | ||
83 | /* Is the start of the string in the buffer view? */ | |
84 | if (str < buf->data || str >= past_buf_end) { | |
85 | ret = false; | |
86 | goto end; | |
87 | } | |
88 | ||
89 | /* | |
90 | * Max length the string could have to fit in the buffer, including | |
91 | * NULL terminator. | |
92 | */ | |
93 | max_str_len_with_null_terminator = past_buf_end - str; | |
94 | ||
95 | /* Could the string even fit in the buffer? */ | |
96 | if (len_with_null_terminator > max_str_len_with_null_terminator) { | |
97 | ret = false; | |
98 | goto end; | |
99 | } | |
100 | ||
101 | str_len = lttng_strnlen(str, max_str_len_with_null_terminator); | |
102 | if (str_len != (len_with_null_terminator - 1)) { | |
103 | ret = false; | |
104 | goto end; | |
105 | } | |
106 | ||
107 | ret = true; | |
108 | ||
109 | end: | |
110 | return ret; | |
111 | } |