ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
type_hash.c
1 // Copyright 2023 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include <yaml.h>
20 
21 #include "rcl/allocator.h"
22 #include "rcl/error_handling.h"
23 #include "rcl/type_hash.h"
24 #include "rcl/types.h"
25 
26 #include "rcutils/sha256.h"
27 #include "rcutils/types/char_array.h"
28 #include "rcutils/types/rcutils_ret.h"
29 #include "type_description_interfaces/msg/type_description.h"
30 
31 #include "./common.h"
32 
33 static int yaml_write_handler(void * ext, uint8_t * buffer, size_t size)
34 {
35  rcutils_char_array_t * repr = (rcutils_char_array_t *)ext;
36  rcutils_ret_t res = rcutils_char_array_strncat(repr, (char *)buffer, size);
37  return res == RCL_RET_OK ? 1 : 0;
38 }
39 
40 static inline int start_sequence(yaml_emitter_t * emitter)
41 {
42  yaml_event_t event;
43  return
44  yaml_sequence_start_event_initialize(&event, NULL, NULL, 1, YAML_FLOW_SEQUENCE_STYLE) &&
45  yaml_emitter_emit(emitter, &event);
46 }
47 
48 static inline int end_sequence(yaml_emitter_t * emitter)
49 {
50  yaml_event_t event;
51  return
52  yaml_sequence_end_event_initialize(&event) &&
53  yaml_emitter_emit(emitter, &event);
54 }
55 
56 static inline int start_mapping(yaml_emitter_t * emitter)
57 {
58  yaml_event_t event;
59  return
60  yaml_mapping_start_event_initialize(&event, NULL, NULL, 1, YAML_FLOW_MAPPING_STYLE) &&
61  yaml_emitter_emit(emitter, &event);
62 }
63 
64 static inline int end_mapping(yaml_emitter_t * emitter)
65 {
66  yaml_event_t event;
67  return
68  yaml_mapping_end_event_initialize(&event) &&
69  yaml_emitter_emit(emitter, &event);
70 }
71 
72 static int emit_key(yaml_emitter_t * emitter, const char * key)
73 {
74  yaml_event_t event;
75  return
76  yaml_scalar_event_initialize(
77  &event, NULL, NULL,
78  (yaml_char_t *)key, (int)strlen(key),
79  0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE) &&
80  yaml_emitter_emit(emitter, &event);
81 }
82 
83 static int emit_int(yaml_emitter_t * emitter, size_t val, const char * fmt)
84 {
85  // longest uint64 is 20 decimal digits, plus one byte for trailing \0
86  char decimal_buf[21];
87  yaml_event_t event;
88  int ret = snprintf(decimal_buf, sizeof(decimal_buf), fmt, val);
89  if (ret < 0) {
90  emitter->problem = "Failed expanding integer";
91  return 0;
92  }
93  if ((size_t)ret >= sizeof(decimal_buf)) {
94  emitter->problem = "Decimal buffer overflow";
95  return 0;
96  }
97  return
98  yaml_scalar_event_initialize(
99  &event, NULL, NULL,
100  (yaml_char_t *)decimal_buf, (int)strlen(decimal_buf),
101  1, 0, YAML_PLAIN_SCALAR_STYLE) &&
102  yaml_emitter_emit(emitter, &event);
103 }
104 
105 static int emit_str(yaml_emitter_t * emitter, const rosidl_runtime_c__String * val)
106 {
107  yaml_event_t event;
108  return
109  yaml_scalar_event_initialize(
110  &event, NULL, NULL,
111  (yaml_char_t *)val->data, (int)val->size,
112  0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE) &&
113  yaml_emitter_emit(emitter, &event);
114 }
115 
116 static int emit_field_type(
117  yaml_emitter_t * emitter,
118  const type_description_interfaces__msg__FieldType * field_type)
119 {
120  return
121  start_mapping(emitter) &&
122 
123  emit_key(emitter, "type_id") &&
124  emit_int(emitter, field_type->type_id, "%d") &&
125 
126  emit_key(emitter, "capacity") &&
127  emit_int(emitter, field_type->capacity, "%zu") &&
128 
129  emit_key(emitter, "string_capacity") &&
130  emit_int(emitter, field_type->string_capacity, "%zu") &&
131 
132  emit_key(emitter, "nested_type_name") &&
133  emit_str(emitter, &field_type->nested_type_name) &&
134 
135  end_mapping(emitter);
136 }
137 
138 static int emit_field(
139  yaml_emitter_t * emitter,
140  const type_description_interfaces__msg__Field * field)
141 {
142  return
143  start_mapping(emitter) &&
144 
145  emit_key(emitter, "name") &&
146  emit_str(emitter, &field->name) &&
147 
148  emit_key(emitter, "type") &&
149  emit_field_type(emitter, &field->type) &&
150 
151  end_mapping(emitter);
152 }
153 
154 static int emit_individual_type_description(
155  yaml_emitter_t * emitter,
156  const type_description_interfaces__msg__IndividualTypeDescription * individual_type_description)
157 {
158  if (!(
159  start_mapping(emitter) &&
160 
161  emit_key(emitter, "type_name") &&
162  emit_str(emitter, &individual_type_description->type_name) &&
163 
164  emit_key(emitter, "fields") &&
165  start_sequence(emitter)))
166  {
167  return 0;
168  }
169  for (size_t i = 0; i < individual_type_description->fields.size; i++) {
170  if (!emit_field(emitter, &individual_type_description->fields.data[i])) {
171  return 0;
172  }
173  }
174  return end_sequence(emitter) && end_mapping(emitter);
175 }
176 
177 static int emit_type_description(
178  yaml_emitter_t * emitter,
179  const type_description_interfaces__msg__TypeDescription * type_description)
180 {
181  if (!(
182  start_mapping(emitter) &&
183 
184  emit_key(emitter, "type_description") &&
185  emit_individual_type_description(emitter, &type_description->type_description) &&
186 
187  emit_key(emitter, "referenced_type_descriptions") &&
188  start_sequence(emitter)))
189  {
190  return 0;
191  }
192  for (size_t i = 0; i < type_description->referenced_type_descriptions.size; i++) {
193  if (!emit_individual_type_description(
194  emitter, &type_description->referenced_type_descriptions.data[i]))
195  {
196  return 0;
197  }
198  }
199  return end_sequence(emitter) && end_mapping(emitter);
200 }
201 
202 rcl_ret_t
203 rcl_type_description_to_hashable_json(
204  const type_description_interfaces__msg__TypeDescription * type_description,
205  rcutils_char_array_t * output_repr)
206 {
207  RCL_CHECK_ARGUMENT_FOR_NULL(type_description, RCL_RET_INVALID_ARGUMENT);
208  RCL_CHECK_ARGUMENT_FOR_NULL(output_repr, RCL_RET_INVALID_ARGUMENT);
209 
210  yaml_emitter_t emitter;
211  yaml_event_t event;
212 
213  if (!yaml_emitter_initialize(&emitter)) {
214  goto error;
215  }
216 
217  // Disable line breaks based on line length
218  yaml_emitter_set_width(&emitter, -1);
219  // Circumvent EOF line break by providing invalid break style
220  yaml_emitter_set_break(&emitter, -1);
221  yaml_emitter_set_output(&emitter, yaml_write_handler, output_repr);
222 
223  if (!(
224  yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING) &&
225  yaml_emitter_emit(&emitter, &event) &&
226 
227  yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1) &&
228  yaml_emitter_emit(&emitter, &event) &&
229 
230  emit_type_description(&emitter, type_description) &&
231 
232  yaml_document_end_event_initialize(&event, 1) &&
233  yaml_emitter_emit(&emitter, &event) &&
234 
235  yaml_stream_end_event_initialize(&event) &&
236  yaml_emitter_emit(&emitter, &event)))
237  {
238  goto error;
239  }
240 
241  yaml_emitter_delete(&emitter);
242  return RCL_RET_OK;
243 
244 error:
245  rcl_set_error_state(emitter.problem, __FILE__, __LINE__);
246  yaml_emitter_delete(&emitter);
247  return RCL_RET_ERROR;
248 }
249 
250 rcl_ret_t
251 rcl_calculate_type_hash(
252  const type_description_interfaces__msg__TypeDescription * type_description,
253  rosidl_type_hash_t * output_hash)
254 {
255  RCL_CHECK_ARGUMENT_FOR_NULL(type_description, RCL_RET_INVALID_ARGUMENT);
256  RCL_CHECK_ARGUMENT_FOR_NULL(output_hash, RCL_RET_INVALID_ARGUMENT);
257 
258  rcl_ret_t result = RCL_RET_OK;
260  rcutils_char_array_t msg_repr = rcutils_get_zero_initialized_char_array();
261  rcutils_ret_t rcutils_result = rcutils_char_array_init(&msg_repr, 0, &allocator);
262  if (rcutils_result != RCL_RET_OK) {
263  // rcutils_char_array_init already set the error
264  return rcl_convert_rcutils_ret_to_rcl_ret(rcutils_result);
265  }
266 
267  output_hash->version = 1;
268  result = rcl_type_description_to_hashable_json(type_description, &msg_repr);
269  if (result == RCL_RET_OK) {
270  rcutils_sha256_ctx_t sha_ctx;
271  rcutils_sha256_init(&sha_ctx);
272  // Last item in char_array is null terminator, which should not be hashed.
273  rcutils_sha256_update(&sha_ctx, (const uint8_t *)msg_repr.buffer, msg_repr.buffer_length - 1);
274  rcutils_sha256_final(&sha_ctx, output_hash->value);
275  }
276  result = rcutils_char_array_fini(&msg_repr);
277  return result;
278 }
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Definition: allocator.h:37
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
#define RCL_RET_OK
Success return code.
Definition: types.h:28
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:36
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:30
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:25