Debian Codegraph @

crt/aws-c-mqtt/source/v5/mqtt5_decoder.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/mqtt/private/v5/mqtt5_decoder.h>
7
8 #include <aws/mqtt/private/v5/mqtt5_topic_alias.h>
9 #include <aws/mqtt/private/v5/mqtt5_utils.h>
10
11 #define AWS_MQTT5_DECODER_BUFFER_START_SIZE 2048
12 #define PUBLISH_PACKET_FIXED_HEADER_DUPLICATE_FLAG 8
13 #define PUBLISH_PACKET_FIXED_HEADER_RETAIN_FLAG 1
14 #define PUBLISH_PACKET_FIXED_HEADER_QOS_FLAG 3
15
16 static void s_reset_decoder_for_new_packet(struct aws_mqtt5_decoder *decoder) {
17 aws_byte_buf_reset(&decoder->scratch_space, false);
18
19 decoder->packet_first_byte = 0;
20 decoder->remaining_length = 0;
21 AWS_ZERO_STRUCT(decoder->packet_cursor);
22 }
23
24 static void s_enter_state(struct aws_mqtt5_decoder *decoder, enum aws_mqtt5_decoder_state state) {
25 decoder->state = state;
26
27 if (state == AWS_MQTT5_DS_READ_PACKET_TYPE) {
28 s_reset_decoder_for_new_packet(decoder);
29 } else {
30 aws_byte_buf_reset(&decoder->scratch_space, false);
31 }
32 }
33
34 static bool s_is_decodable_packet_type(struct aws_mqtt5_decoder *decoder, enum aws_mqtt5_packet_type type) {
35 return (uint32_t)type < AWS_ARRAY_SIZE(decoder->options.decoder_table->decoders_by_packet_type) &&
36 decoder->options.decoder_table->decoders_by_packet_type[type] != NULL;
37 }
38
39 /*
40 * Every mqtt packet has a first byte that, amongst other things, determines the packet type
41 */
42 static int s_aws_mqtt5_decoder_read_packet_type_on_data(
43 struct aws_mqtt5_decoder *decoder,
44 struct aws_byte_cursor *data) {
45
46 if (data->len == 0) {
47 return AWS_MQTT5_DRT_MORE_DATA;
48 }
49
50 uint8_t byte = *data->ptr;
51 aws_byte_cursor_advance(data, 1);
52 aws_byte_buf_append_byte_dynamic(&decoder->scratch_space, byte);
53
54 enum aws_mqtt5_packet_type packet_type = (byte >> 4);
55
56 if (!s_is_decodable_packet_type(decoder, packet_type)) {
57 AWS_LOGF_ERROR(
58 AWS_LS_MQTT5_CLIENT,
59 "id=%p: unsupported or illegal packet type value: %d",
60 decoder->options.callback_user_data,
61 (int)packet_type);
62 return AWS_MQTT5_DRT_ERROR;
63 }
64
65 decoder->packet_first_byte = byte;
66
67 s_enter_state(decoder, AWS_MQTT5_DS_READ_REMAINING_LENGTH);
68
69 return AWS_MQTT5_DRT_SUCCESS;
70 }
71
72 /*
73 * non-streaming variable length integer decode. cursor is updated only if the value was successfully read
74 */
75 enum aws_mqtt5_decode_result_type aws_mqtt5_decode_vli(struct aws_byte_cursor *cursor, uint32_t *dest) {
76 uint32_t value = 0;
77 bool more_data = false;
78 size_t bytes_used = 0;
79
80 uint32_t shift = 0;
81
82 struct aws_byte_cursor cursor_copy = *cursor;
83 for (; bytes_used < 4; ++bytes_used) {
84 uint8_t byte = 0;
85 if (!aws_byte_cursor_read_u8(&cursor_copy, &byte)) {
86 return AWS_MQTT5_DRT_MORE_DATA;
87 }
88
89 value |= ((byte & 0x7F) << shift);
90 shift += 7;
91
92 more_data = (byte & 0x80) != 0;
93 if (!more_data) {
94 break;
95 }
96 }
97
98 if (more_data) {
99 /* A variable length integer with the 4th byte high bit set is not valid */
100 AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "(static) aws_mqtt5_decoder - illegal variable length integer encoding");
101 return AWS_MQTT5_DRT_ERROR;
102 }
103
104 aws_byte_cursor_advance(cursor, bytes_used + 1);
105 *dest = value;
106
107 return AWS_MQTT5_DRT_SUCCESS;
108 }
109
110 /* "streaming" variable length integer decode */
111 static enum aws_mqtt5_decode_result_type s_aws_mqtt5_decoder_read_vli_on_data(
112 struct aws_mqtt5_decoder *decoder,
113 uint32_t *vli_dest,
114 struct aws_byte_cursor *data) {
115
116 enum aws_mqtt5_decode_result_type decode_vli_result = AWS_MQTT5_DRT_MORE_DATA;
117
118 /* try to decode the vli integer one byte at a time */
119 while (data->len > 0 && decode_vli_result == AWS_MQTT5_DRT_MORE_DATA) {
120 /* append a single byte to the scratch buffer */
121 struct aws_byte_cursor byte_cursor = aws_byte_cursor_advance(data, 1);
122 aws_byte_buf_append_dynamic(&decoder->scratch_space, &byte_cursor);
123
124 /* now try and decode a vli integer based on the range implied by the offset into the buffer */
125 struct aws_byte_cursor vli_cursor = {
126 .ptr = decoder->scratch_space.buffer,
127 .len = decoder->scratch_space.len,
128 };
129
130 decode_vli_result = aws_mqtt5_decode_vli(&vli_cursor, vli_dest);
131 }
132
133 return decode_vli_result;
134 }
135
136 /* attempts to read the variable length integer that is always the second piece of data in an mqtt packet */
137 static enum aws_mqtt5_decode_result_type s_aws_mqtt5_decoder_read_remaining_length_on_data(
138 struct aws_mqtt5_decoder *decoder,
139 struct aws_byte_cursor *data) {
140
141 enum aws_mqtt5_decode_result_type result =
142 s_aws_mqtt5_decoder_read_vli_on_data(decoder, &decoder->remaining_length, data);
143 if (result != AWS_MQTT5_DRT_SUCCESS) {
144 return result;
145 }
146
147 s_enter_state(decoder, AWS_MQTT5_DS_READ_PACKET);
148
149 return AWS_MQTT5_DRT_SUCCESS;
150 }
151
152 /* non-streaming decode of a user property; failure implies connection termination */
153 int aws_mqtt5_decode_user_property(
154 struct aws_byte_cursor *packet_cursor,
155 struct aws_mqtt5_user_property_set *properties) {
156 struct aws_mqtt5_user_property property;
157
158 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(packet_cursor, &property.name, error);
159 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(packet_cursor, &property.value, error);
160
161 if (aws_array_list_push_back(&properties->properties, &property)) {
162 return AWS_OP_ERR;
163 }
164
165 return AWS_OP_SUCCESS;
166
167 error:
168
169 return AWS_OP_ERR;
170 }
171
172 /* decode function for all CONNACK properties */
173 static int s_read_connack_property(
174 struct aws_mqtt5_packet_connack_storage *storage,
175 struct aws_byte_cursor *packet_cursor) {
176 int result = AWS_OP_ERR;
177
178 uint8_t property_type = 0;
179 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
180
181 struct aws_mqtt5_packet_connack_view *storage_view = &storage->storage_view;
182
183 switch (property_type) {
184 case AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL:
185 AWS_MQTT5_DECODE_U32_OPTIONAL(
186 packet_cursor, &storage->session_expiry_interval, &storage_view->session_expiry_interval, done);
187 break;
188
189 case AWS_MQTT5_PROPERTY_TYPE_RECEIVE_MAXIMUM:
190 AWS_MQTT5_DECODE_U16_OPTIONAL(
191 packet_cursor, &storage->receive_maximum, &storage_view->receive_maximum, done);
192 break;
193
194 case AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_QOS:
195 AWS_MQTT5_DECODE_U8_OPTIONAL(packet_cursor, &storage->maximum_qos, &storage_view->maximum_qos, done);
196 break;
197
198 case AWS_MQTT5_PROPERTY_TYPE_RETAIN_AVAILABLE:
199 AWS_MQTT5_DECODE_U8_OPTIONAL(
200 packet_cursor, &storage->retain_available, &storage_view->retain_available, done);
201 break;
202
203 case AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_PACKET_SIZE:
204 AWS_MQTT5_DECODE_U32_OPTIONAL(
205 packet_cursor, &storage->maximum_packet_size, &storage_view->maximum_packet_size, done);
206 break;
207
208 case AWS_MQTT5_PROPERTY_TYPE_ASSIGNED_CLIENT_IDENTIFIER:
209 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
210 packet_cursor, &storage->assigned_client_identifier, &storage_view->assigned_client_identifier, done);
211 break;
212
213 case AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS_MAXIMUM:
214 AWS_MQTT5_DECODE_U16_OPTIONAL(
215 packet_cursor, &storage->topic_alias_maximum, &storage_view->topic_alias_maximum, done);
216 break;
217
218 case AWS_MQTT5_PROPERTY_TYPE_REASON_STRING:
219 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
220 packet_cursor, &storage->reason_string, &storage_view->reason_string, done);
221 break;
222
223 case AWS_MQTT5_PROPERTY_TYPE_WILDCARD_SUBSCRIPTIONS_AVAILABLE:
224 AWS_MQTT5_DECODE_U8_OPTIONAL(
225 packet_cursor,
226 &storage->wildcard_subscriptions_available,
227 &storage_view->wildcard_subscriptions_available,
228 done);
229 break;
230
231 case AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIERS_AVAILABLE:
232 AWS_MQTT5_DECODE_U8_OPTIONAL(
233 packet_cursor,
234 &storage->subscription_identifiers_available,
235 &storage_view->subscription_identifiers_available,
236 done);
237 break;
238
239 case AWS_MQTT5_PROPERTY_TYPE_SHARED_SUBSCRIPTIONS_AVAILABLE:
240 AWS_MQTT5_DECODE_U8_OPTIONAL(
241 packet_cursor,
242 &storage->shared_subscriptions_available,
243 &storage_view->shared_subscriptions_available,
244 done);
245 break;
246
247 case AWS_MQTT5_PROPERTY_TYPE_SERVER_KEEP_ALIVE:
248 AWS_MQTT5_DECODE_U16_OPTIONAL(
249 packet_cursor, &storage->server_keep_alive, &storage_view->server_keep_alive, done);
250 break;
251
252 case AWS_MQTT5_PROPERTY_TYPE_RESPONSE_INFORMATION:
253 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
254 packet_cursor, &storage->response_information, &storage_view->response_information, done);
255 break;
256
257 case AWS_MQTT5_PROPERTY_TYPE_SERVER_REFERENCE:
258 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
259 packet_cursor, &storage->server_reference, &storage_view->server_reference, done);
260 break;
261
262 case AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_METHOD:
263 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
264 packet_cursor, &storage->authentication_method, &storage_view->authentication_method, done);
265 break;
266
267 case AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_DATA:
268 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
269 packet_cursor, &storage->authentication_data, &storage_view->authentication_data, done);
270 break;
271
272 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
273 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
274 goto done;
275 }
276 break;
277
278 default:
279 goto done;
280 }
281
282 result = AWS_OP_SUCCESS;
283
284 done:
285
286 if (result != AWS_OP_SUCCESS) {
287 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read CONNACK property decode failure");
288 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
289 }
290
291 return result;
292 }
293
294 /* decodes a CONNACK packet whose data must be in the scratch buffer */
295 static int s_aws_mqtt5_decoder_decode_connack(struct aws_mqtt5_decoder *decoder) {
296 struct aws_mqtt5_packet_connack_storage storage;
297 if (aws_mqtt5_packet_connack_storage_init_from_external_storage(&storage, decoder->allocator)) {
298 return AWS_OP_ERR;
299 }
300
301 int result = AWS_OP_ERR;
302
303 uint8_t first_byte = decoder->packet_first_byte;
304 /* CONNACK flags must be zero by protocol */
305 if ((first_byte & 0x0F) != 0) {
306 goto done;
307 }
308
309 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
310 uint32_t remaining_length = decoder->remaining_length;
311 if (remaining_length != (uint32_t)packet_cursor.len) {
312 goto done;
313 }
314
315 uint8_t connect_flags = 0;
316 AWS_MQTT5_DECODE_U8(&packet_cursor, &connect_flags, done);
317
318 /* everything but the 0-bit must be 0 */
319 if ((connect_flags & 0xFE) != 0) {
320 goto done;
321 }
322
323 struct aws_mqtt5_packet_connack_view *storage_view = &storage.storage_view;
324
325 storage_view->session_present = (connect_flags & 0x01) != 0;
326
327 uint8_t reason_code = 0;
328 AWS_MQTT5_DECODE_U8(&packet_cursor, &reason_code, done);
329 storage_view->reason_code = reason_code;
330
331 uint32_t property_length = 0;
332 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
333 if (property_length != (uint32_t)packet_cursor.len) {
334 goto done;
335 }
336
337 while (packet_cursor.len > 0) {
338 if (s_read_connack_property(&storage, &packet_cursor)) {
339 goto done;
340 }
341 }
342
343 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
344 storage_view->user_properties = storage.user_properties.properties.data;
345
346 result = AWS_OP_SUCCESS;
347
348 done:
349
350 if (result == AWS_OP_SUCCESS) {
351 if (decoder->options.on_packet_received != NULL) {
352 result = (*decoder->options.on_packet_received)(
353 AWS_MQTT5_PT_CONNACK, &storage.storage_view, decoder->options.callback_user_data);
354 }
355 } else {
356 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "id=%p: CONNACK decode failure", decoder->options.callback_user_data);
357 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
358 }
359
360 aws_mqtt5_packet_connack_storage_clean_up(&storage);
361
362 return result;
363 }
364
365 /* decode function for all PUBLISH properties */
366 static int s_read_publish_property(
367 struct aws_mqtt5_packet_publish_storage *storage,
368 struct aws_byte_cursor *packet_cursor) {
369 int result = AWS_OP_ERR;
370
371 uint8_t property_type = 0;
372 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
373
374 struct aws_mqtt5_packet_publish_view *storage_view = &storage->storage_view;
375
376 switch (property_type) {
377 case AWS_MQTT5_PROPERTY_TYPE_PAYLOAD_FORMAT_INDICATOR:
378 AWS_MQTT5_DECODE_U8_OPTIONAL(packet_cursor, &storage->payload_format, &storage_view->payload_format, done);
379 break;
380
381 case AWS_MQTT5_PROPERTY_TYPE_MESSAGE_EXPIRY_INTERVAL:
382 AWS_MQTT5_DECODE_U32_OPTIONAL(
383 packet_cursor,
384 &storage->message_expiry_interval_seconds,
385 &storage_view->message_expiry_interval_seconds,
386 done);
387 break;
388
389 case AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS:
390 AWS_MQTT5_DECODE_U16_OPTIONAL(packet_cursor, &storage->topic_alias, &storage_view->topic_alias, done);
391 break;
392
393 case AWS_MQTT5_PROPERTY_TYPE_RESPONSE_TOPIC:
394 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
395 packet_cursor, &storage->response_topic, &storage_view->response_topic, done);
396 break;
397
398 case AWS_MQTT5_PROPERTY_TYPE_CORRELATION_DATA:
399 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
400 packet_cursor, &storage->correlation_data, &storage_view->correlation_data, done);
401 break;
402
403 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
404 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
405 goto done;
406 }
407 break;
408
409 case AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIER: {
410 uint32_t subscription_identifier = 0;
411 AWS_MQTT5_DECODE_VLI(packet_cursor, &subscription_identifier, done);
412 aws_array_list_push_back(&storage->subscription_identifiers, &subscription_identifier);
413 break;
414 }
415
416 case AWS_MQTT5_PROPERTY_TYPE_CONTENT_TYPE:
417 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
418 packet_cursor, &storage->content_type, &storage_view->content_type, done);
419 break;
420
421 default:
422 goto done;
423 }
424
425 result = AWS_OP_SUCCESS;
426
427 done:
428
429 if (result != AWS_OP_SUCCESS) {
430 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read PUBLISH property decode failure");
431 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
432 }
433
434 return result;
435 }
436
437 /* decodes a PUBLISH packet whose data must be in the scratch buffer */
438 static int s_aws_mqtt5_decoder_decode_publish(struct aws_mqtt5_decoder *decoder) {
439 struct aws_mqtt5_packet_publish_storage storage;
440 if (aws_mqtt5_packet_publish_storage_init_from_external_storage(&storage, decoder->allocator)) {
441 return AWS_OP_ERR;
442 }
443
444 int result = AWS_OP_ERR;
445 struct aws_mqtt5_packet_publish_view *storage_view = &storage.storage_view;
446
447 /*
448 * Fixed Header
449 * byte 1:
450 * bits 4-7: MQTT Control Packet Type
451 * bit 3: DUP flag
452 * bit 1-2: QoS level
453 * bit 0: RETAIN
454 * byte 2-x: Remaining Length as Variable Byte Integer (1-4 bytes)
455 */
456
457 uint8_t first_byte = decoder->packet_first_byte;
458 if ((first_byte & PUBLISH_PACKET_FIXED_HEADER_DUPLICATE_FLAG) != 0) {
459 storage_view->duplicate = true;
460 }
461 if ((first_byte & PUBLISH_PACKET_FIXED_HEADER_RETAIN_FLAG) != 0) {
462 storage_view->retain = true;
463 }
464 storage_view->qos = (enum aws_mqtt5_qos)((first_byte >> 1) & PUBLISH_PACKET_FIXED_HEADER_QOS_FLAG);
465
466 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
467 uint32_t remaining_length = decoder->remaining_length;
468 if (remaining_length != (uint32_t)packet_cursor.len) {
469 goto done;
470 }
471
472 /*
473 * Topic Name
474 * Packet Identifier (only present for > QoS 0)
475 * Properties
476 * - Property Length
477 * - Properties
478 * Payload
479 */
480 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(&packet_cursor, &storage_view->topic, done);
481
482 if (storage_view->qos > 0) {
483 AWS_MQTT5_DECODE_U16(&packet_cursor, &storage_view->packet_id, done);
484 }
485
486 uint32_t property_length = 0;
487 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
488 if (property_length > (uint32_t)packet_cursor.len) {
489 goto done;
490 }
491 struct aws_byte_cursor properties_cursor = aws_byte_cursor_advance(&packet_cursor, property_length);
492
493 while (properties_cursor.len > 0) {
494 if (s_read_publish_property(&storage, &properties_cursor)) {
495 goto done;
496 }
497 }
498
499 storage_view->subscription_identifier_count = aws_array_list_length(&storage.subscription_identifiers);
500 storage_view->subscription_identifiers = storage.subscription_identifiers.data;
501
502 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
503 storage_view->user_properties = storage.user_properties.properties.data;
504 storage_view->payload = packet_cursor;
505
506 if (storage_view->topic_alias != NULL) {
507 if (decoder->topic_alias_resolver == NULL) {
508 AWS_LOGF_ERROR(
509 AWS_LS_MQTT5_CLIENT,
510 "id=%p: PUBLISH packet contained topic alias when not allowed",
511 decoder->options.callback_user_data);
512 goto done;
513 }
514
515 uint16_t topic_alias_id = *storage_view->topic_alias;
516 if (topic_alias_id == 0) {
517 AWS_LOGF_ERROR(
518 AWS_LS_MQTT5_CLIENT,
519 "id=%p: PUBLISH packet contained illegal topic alias",
520 decoder->options.callback_user_data);
521 goto done;
522 }
523
524 if (storage_view->topic.len > 0) {
525 if (aws_mqtt5_inbound_topic_alias_resolver_register_alias(
526 decoder->topic_alias_resolver, topic_alias_id, storage_view->topic)) {
527 AWS_LOGF_ERROR(
528 AWS_LS_MQTT5_CLIENT, "id=%p: unable to register topic alias", decoder->options.callback_user_data);
529 goto done;
530 }
531 } else {
532 if (aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(
533 decoder->topic_alias_resolver, topic_alias_id, &storage_view->topic)) {
534 AWS_LOGF_ERROR(
535 AWS_LS_MQTT5_CLIENT,
536 "id=%p: PUBLISH packet contained unknown topic alias",
537 decoder->options.callback_user_data);
538 goto done;
539 }
540 }
541 }
542
543 result = AWS_OP_SUCCESS;
544
545 done:
546
547 if (result == AWS_OP_SUCCESS) {
548 if (decoder->options.on_packet_received != NULL) {
549 result = (*decoder->options.on_packet_received)(
550 AWS_MQTT5_PT_PUBLISH, &storage.storage_view, decoder->options.callback_user_data);
551 }
552 } else {
553 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "id=%p: PUBLISH decode failure", decoder->options.callback_user_data);
554 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
555 }
556
557 aws_mqtt5_packet_publish_storage_clean_up(&storage);
558
559 return result;
560 }
561
562 /* decode function for all PUBACK properties */
563 static int s_read_puback_property(
564 struct aws_mqtt5_packet_puback_storage *storage,
565 struct aws_byte_cursor *packet_cursor) {
566 int result = AWS_OP_ERR;
567
568 uint8_t property_type = 0;
569 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
570
571 struct aws_mqtt5_packet_puback_view *storage_view = &storage->storage_view;
572
573 switch (property_type) {
574 case AWS_MQTT5_PROPERTY_TYPE_REASON_STRING:
575 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
576 packet_cursor, &storage->reason_string, &storage_view->reason_string, done);
577 break;
578
579 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
580 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
581 goto done;
582 }
583 break;
584
585 default:
586 goto done;
587 }
588
589 result = AWS_OP_SUCCESS;
590
591 done:
592
593 if (result != AWS_OP_SUCCESS) {
594 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read PUBACK property decode failure");
595 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
596 }
597
598 return result;
599 }
600
601 /* decodes a PUBACK packet whose data must be in the scratch buffer */
602 static int s_aws_mqtt5_decoder_decode_puback(struct aws_mqtt5_decoder *decoder) {
603 struct aws_mqtt5_packet_puback_storage storage;
604 if (aws_mqtt5_packet_puback_storage_init_from_external_storage(&storage, decoder->allocator)) {
605 return AWS_OP_ERR;
606 }
607 int result = AWS_OP_ERR;
608
609 uint8_t first_byte = decoder->packet_first_byte;
610 /* PUBACK flags must be zero by protocol */
611 if ((first_byte & 0x0F) != 0) {
612 goto done;
613 }
614
615 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
616 uint32_t remaining_length = decoder->remaining_length;
617 if (remaining_length != (uint32_t)packet_cursor.len) {
618 goto done;
619 }
620
621 struct aws_mqtt5_packet_puback_view *storage_view = &storage.storage_view;
622
623 AWS_MQTT5_DECODE_U16(&packet_cursor, &storage_view->packet_id, done);
624
625 /* Packet can end immediately following packet id with default success reason code */
626 uint8_t reason_code = 0;
627 if (packet_cursor.len > 0) {
628 AWS_MQTT5_DECODE_U8(&packet_cursor, &reason_code, done);
629
630 /* Packet can end immediately following reason code */
631 if (packet_cursor.len > 0) {
632 uint32_t property_length = 0;
633 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
634 if (property_length != (uint32_t)packet_cursor.len) {
635 goto done;
636 }
637 while (packet_cursor.len > 0) {
638 if (s_read_puback_property(&storage, &packet_cursor)) {
639 goto done;
640 }
641 }
642 }
643 }
644
645 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
646 storage_view->user_properties = storage.user_properties.properties.data;
647 storage_view->reason_code = reason_code;
648
649 result = AWS_OP_SUCCESS;
650
651 done:
652
653 if (result == AWS_OP_SUCCESS) {
654 if (decoder->options.on_packet_received != NULL) {
655 result = (*decoder->options.on_packet_received)(
656 AWS_MQTT5_PT_PUBACK, &storage.storage_view, decoder->options.callback_user_data);
657 }
658 } else {
659 AWS_LOGF_ERROR(
660 AWS_LS_MQTT5_GENERAL,
661 "(%p) aws_mqtt5_decoder - PUBACK decode failure",
662 decoder->options.callback_user_data);
663 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
664 }
665
666 aws_mqtt5_packet_puback_storage_clean_up(&storage);
667
668 return result;
669 }
670
671 /* decode function for all SUBACK properties */
672 static int s_read_suback_property(
673 struct aws_mqtt5_packet_suback_storage *storage,
674 struct aws_byte_cursor *packet_cursor) {
675 int result = AWS_OP_ERR;
676
677 uint8_t property_type = 0;
678 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
679
680 struct aws_mqtt5_packet_suback_view *storage_view = &storage->storage_view;
681
682 switch (property_type) {
683 case AWS_MQTT5_PROPERTY_TYPE_REASON_STRING:
684 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
685 packet_cursor, &storage->reason_string, &storage_view->reason_string, done);
686 break;
687
688 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
689 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
690 goto done;
691 }
692 break;
693
694 default:
695 goto done;
696 }
697
698 result = AWS_OP_SUCCESS;
699
700 done:
701
702 if (result != AWS_OP_SUCCESS) {
703 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read SUBACK property decode failure");
704 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
705 }
706
707 return result;
708 }
709
710 /* decodes a SUBACK packet whose data must be in the scratch buffer */
711 static int s_aws_mqtt5_decoder_decode_suback(struct aws_mqtt5_decoder *decoder) {
712 struct aws_mqtt5_packet_suback_storage storage;
713 if (aws_mqtt5_packet_suback_storage_init_from_external_storage(&storage, decoder->allocator)) {
714 return AWS_OP_ERR;
715 }
716 int result = AWS_OP_ERR;
717
718 struct aws_mqtt5_packet_suback_view *storage_view = &storage.storage_view;
719
720 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
721
722 AWS_MQTT5_DECODE_U16(&packet_cursor, &storage_view->packet_id, done);
723
724 uint32_t property_length = 0;
725 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
726 struct aws_byte_cursor properties_cursor = aws_byte_cursor_advance(&packet_cursor, property_length);
727 while (properties_cursor.len > 0) {
728 if (s_read_suback_property(&storage, &properties_cursor)) {
729 goto done;
730 }
731 }
732
733 aws_array_list_init_dynamic(
734 &storage.reason_codes, decoder->allocator, packet_cursor.len, sizeof(enum aws_mqtt5_suback_reason_code));
735
736 while (packet_cursor.len > 0) {
737 uint8_t reason_code;
738 AWS_MQTT5_DECODE_U8(&packet_cursor, &reason_code, done);
739 enum aws_mqtt5_suback_reason_code reason_code_enum = reason_code;
740 aws_array_list_push_back(&storage.reason_codes, &reason_code_enum);
741 }
742
743 storage_view->reason_code_count = aws_array_list_length(&storage.reason_codes);
744 storage_view->reason_codes = storage.reason_codes.data;
745 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
746 storage_view->user_properties = storage.user_properties.properties.data;
747
748 result = AWS_OP_SUCCESS;
749
750 done:
751
752 if (result == AWS_OP_SUCCESS) {
753 if (decoder->options.on_packet_received != NULL) {
754 result = (*decoder->options.on_packet_received)(
755 AWS_MQTT5_PT_SUBACK, &storage.storage_view, decoder->options.callback_user_data);
756 }
757 } else {
758 AWS_LOGF_ERROR(
759 AWS_LS_MQTT5_GENERAL,
760 "(%p) aws_mqtt5_decoder - SUBACK decode failure",
761 decoder->options.callback_user_data);
762 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
763 }
764
765 aws_mqtt5_packet_suback_storage_clean_up(&storage);
766
767 return result;
768 }
769
770 /* decode function for all UNSUBACK properties */
771 static int s_read_unsuback_property(
772 struct aws_mqtt5_packet_unsuback_storage *storage,
773 struct aws_byte_cursor *packet_cursor) {
774 int result = AWS_OP_ERR;
775
776 uint8_t property_type = 0;
777 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
778
779 struct aws_mqtt5_packet_unsuback_view *storage_view = &storage->storage_view;
780
781 switch (property_type) {
782 case AWS_MQTT5_PROPERTY_TYPE_REASON_STRING:
783 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
784 packet_cursor, &storage->reason_string, &storage_view->reason_string, done);
785 break;
786
787 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
788 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
789 goto done;
790 }
791 break;
792
793 default:
794 goto done;
795 }
796
797 result = AWS_OP_SUCCESS;
798
799 done:
800
801 if (result != AWS_OP_SUCCESS) {
802 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read UNSUBACK property decode failure");
803 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
804 }
805
806 return result;
807 }
808
809 /* decodes an UNSUBACK packet whose data must be in the scratch buffer */
810 static int s_aws_mqtt5_decoder_decode_unsuback(struct aws_mqtt5_decoder *decoder) {
811 struct aws_mqtt5_packet_unsuback_storage storage;
812
813 /*
814 * Fixed Header
815 * byte 1: MQTT5 Control Packet - Reserved 0
816 * byte 2 - x: VLI Remaining Length
817 *
818 * Variable Header
819 * byte 1-2: Packet Identifier
820 * Byte 3 - x: VLI Property Length
821 *
822 * Properties
823 * byte 1: Idenfier
824 * bytes 2 - x: Property content
825 *
826 * Payload
827 * 1 byte per reason code in order of unsub requests
828 */
829
830 if (aws_mqtt5_packet_unsuback_storage_init_from_external_storage(&storage, decoder->allocator)) {
831 return AWS_OP_ERR;
832 }
833 int result = AWS_OP_ERR;
834
835 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
836
837 struct aws_mqtt5_packet_unsuback_view *storage_view = &storage.storage_view;
838
839 AWS_MQTT5_DECODE_U16(&packet_cursor, &storage_view->packet_id, done);
840
841 uint32_t property_length = 0;
842 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
843 struct aws_byte_cursor properties_cursor = aws_byte_cursor_advance(&packet_cursor, property_length);
844 while (properties_cursor.len > 0) {
845 if (s_read_unsuback_property(&storage, &properties_cursor)) {
846 goto done;
847 }
848 }
849
850 aws_array_list_init_dynamic(
851 &storage.reason_codes, decoder->allocator, packet_cursor.len, sizeof(enum aws_mqtt5_unsuback_reason_code));
852
853 while (packet_cursor.len > 0) {
854 uint8_t reason_code;
855 AWS_MQTT5_DECODE_U8(&packet_cursor, &reason_code, done);
856 enum aws_mqtt5_unsuback_reason_code reason_code_enum = reason_code;
857 aws_array_list_push_back(&storage.reason_codes, &reason_code_enum);
858 }
859
860 storage_view->reason_code_count = aws_array_list_length(&storage.reason_codes);
861 storage_view->reason_codes = storage.reason_codes.data;
862 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
863 storage_view->user_properties = storage.user_properties.properties.data;
864
865 result = AWS_OP_SUCCESS;
866
867 done:
868
869 if (result == AWS_OP_SUCCESS) {
870 if (decoder->options.on_packet_received != NULL) {
871 result = (*decoder->options.on_packet_received)(
872 AWS_MQTT5_PT_UNSUBACK, &storage.storage_view, decoder->options.callback_user_data);
873 }
874 } else {
875 AWS_LOGF_ERROR(
876 AWS_LS_MQTT5_GENERAL,
877 "(%p) aws_mqtt5_decoder - UNSUBACK decode failure",
878 decoder->options.callback_user_data);
879 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
880 }
881
882 aws_mqtt5_packet_unsuback_storage_clean_up(&storage);
883
884 return result;
885 }
886
887 /* decode function for all DISCONNECT properties */
888 static int s_read_disconnect_property(
889 struct aws_mqtt5_packet_disconnect_storage *storage,
890 struct aws_byte_cursor *packet_cursor) {
891 int result = AWS_OP_ERR;
892
893 uint8_t property_type = 0;
894 AWS_MQTT5_DECODE_U8(packet_cursor, &property_type, done);
895
896 struct aws_mqtt5_packet_disconnect_view *storage_view = &storage->storage_view;
897
898 switch (property_type) {
899 case AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL:
900 AWS_MQTT5_DECODE_U32_OPTIONAL(
901 packet_cursor,
902 &storage->session_expiry_interval_seconds,
903 &storage_view->session_expiry_interval_seconds,
904 done);
905 break;
906
907 case AWS_MQTT5_PROPERTY_TYPE_SERVER_REFERENCE:
908 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
909 packet_cursor, &storage->server_reference, &storage_view->server_reference, done);
910 break;
911
912 case AWS_MQTT5_PROPERTY_TYPE_REASON_STRING:
913 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(
914 packet_cursor, &storage->reason_string, &storage_view->reason_string, done);
915 break;
916
917 case AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY:
918 if (aws_mqtt5_decode_user_property(packet_cursor, &storage->user_properties)) {
919 goto done;
920 }
921 break;
922
923 default:
924 goto done;
925 }
926
927 result = AWS_OP_SUCCESS;
928
929 done:
930
931 if (result == AWS_OP_ERR) {
932 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Read DISCONNECT property decode failure");
933 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
934 }
935
936 return result;
937 }
938
939 /* decodes a DISCONNECT packet whose data must be in the scratch buffer */
940 static int s_aws_mqtt5_decoder_decode_disconnect(struct aws_mqtt5_decoder *decoder) {
941 struct aws_mqtt5_packet_disconnect_storage storage;
942 if (aws_mqtt5_packet_disconnect_storage_init_from_external_storage(&storage, decoder->allocator)) {
943 return AWS_OP_ERR;
944 }
945
946 int result = AWS_OP_ERR;
947
948 uint8_t first_byte = decoder->packet_first_byte;
949 /* DISCONNECT flags must be zero by protocol */
950 if ((first_byte & 0x0F) != 0) {
951 goto done;
952 }
953
954 struct aws_byte_cursor packet_cursor = decoder->packet_cursor;
955 uint32_t remaining_length = decoder->remaining_length;
956 if (remaining_length != (uint32_t)packet_cursor.len) {
957 goto done;
958 }
959
960 struct aws_mqtt5_packet_disconnect_view *storage_view = &storage.storage_view;
961 if (remaining_length > 0) {
962 uint8_t reason_code = 0;
963 AWS_MQTT5_DECODE_U8(&packet_cursor, &reason_code, done);
964 storage_view->reason_code = reason_code;
965 if (packet_cursor.len == 0) {
966 result = AWS_OP_SUCCESS;
967 goto done;
968 }
969
970 uint32_t property_length = 0;
971 AWS_MQTT5_DECODE_VLI(&packet_cursor, &property_length, done);
972 if (property_length != (uint32_t)packet_cursor.len) {
973 goto done;
974 }
975
976 while (packet_cursor.len > 0) {
977 if (s_read_disconnect_property(&storage, &packet_cursor)) {
978 goto done;
979 }
980 }
981 }
982
983 storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage.user_properties);
984 storage_view->user_properties = storage.user_properties.properties.data;
985
986 result = AWS_OP_SUCCESS;
987
988 done:
989
990 if (result == AWS_OP_SUCCESS) {
991 if (decoder->options.on_packet_received != NULL) {
992 result = (*decoder->options.on_packet_received)(
993 AWS_MQTT5_PT_DISCONNECT, &storage.storage_view, decoder->options.callback_user_data);
994 }
995 } else {
996 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "id=%p: DISCONNECT decode failure", decoder->options.callback_user_data);
997 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
998 }
999
1000 aws_mqtt5_packet_disconnect_storage_clean_up(&storage);
1001
1002 return result;
1003 }
1004
1005 static int s_aws_mqtt5_decoder_decode_pingresp(struct aws_mqtt5_decoder *decoder) {
1006 if (decoder->packet_cursor.len != 0) {
1007 goto error;
1008 }
1009
1010 uint8_t expected_first_byte = aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PINGRESP, 0);
1011 if (decoder->packet_first_byte != expected_first_byte || decoder->remaining_length != 0) {
1012 goto error;
1013 }
1014
1015 int result = AWS_OP_SUCCESS;
1016 if (decoder->options.on_packet_received != NULL) {
1017 result =
1018 (*decoder->options.on_packet_received)(AWS_MQTT5_PT_PINGRESP, NULL, decoder->options.callback_user_data);
1019 }
1020
1021 return result;
1022
1023 error:
1024
1025 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "id=%p: PINGRESP decode failure", decoder->options.callback_user_data);
1026 return aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
1027 }
1028
1029 static int s_aws_mqtt5_decoder_decode_packet(struct aws_mqtt5_decoder *decoder) {
1030 enum aws_mqtt5_packet_type packet_type = (enum aws_mqtt5_packet_type)(decoder->packet_first_byte >> 4);
1031 aws_mqtt5_decoding_fn *decoder_fn = decoder->options.decoder_table->decoders_by_packet_type[packet_type];
1032 if (decoder_fn == NULL) {
1033 AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "Decoder decode packet function missing for enum: %d", packet_type);
1034 return aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
1035 }
1036
1037 return (*decoder_fn)(decoder);
1038 }
1039
1040 /*
1041 * (Streaming) Given a packet type and a variable length integer specifying the packet length, this state either
1042 * (1) decodes directly from the cursor if possible
1043 * (2) reads the packet into the scratch buffer and then decodes it once it is completely present
1044 *
1045 */
1046 static enum aws_mqtt5_decode_result_type s_aws_mqtt5_decoder_read_packet_on_data(
1047 struct aws_mqtt5_decoder *decoder,
1048 struct aws_byte_cursor *data) {
1049
1050 /* Are we able to decode directly from the channel message data buffer? */
1051 if (decoder->scratch_space.len == 0 && decoder->remaining_length <= data->len) {
1052 /* The cursor contains the entire packet, so decode directly from the backing io message buffer */
1053 decoder->packet_cursor = aws_byte_cursor_advance(data, decoder->remaining_length);
1054 } else {
1055 /* If the packet is fragmented across multiple io messages, then we buffer it internally */
1056 size_t unread_length = decoder->remaining_length - decoder->scratch_space.len;
1057 size_t copy_length = aws_min_size(unread_length, data->len);
1058
1059 struct aws_byte_cursor copy_cursor = aws_byte_cursor_advance(data, copy_length);
1060 if (aws_byte_buf_append_dynamic(&decoder->scratch_space, &copy_cursor)) {
1061 return AWS_MQTT5_DRT_ERROR;
1062 }
1063
1064 if (copy_length < unread_length) {
1065 return AWS_MQTT5_DRT_MORE_DATA;
1066 }
1067
1068 decoder->packet_cursor = aws_byte_cursor_from_buf(&decoder->scratch_space);
1069 }
1070
1071 if (s_aws_mqtt5_decoder_decode_packet(decoder)) {
1072 return AWS_MQTT5_DRT_ERROR;
1073 }
1074
1075 s_enter_state(decoder, AWS_MQTT5_DS_READ_PACKET_TYPE);
1076
1077 return AWS_MQTT5_DRT_SUCCESS;
1078 }
1079
1080 /* top-level entry function for all new data received from the remote mqtt endpoint */
1081 int aws_mqtt5_decoder_on_data_received(struct aws_mqtt5_decoder *decoder, struct aws_byte_cursor data) {
1082 enum aws_mqtt5_decode_result_type result = AWS_MQTT5_DRT_SUCCESS;
1083 while (result == AWS_MQTT5_DRT_SUCCESS) {
1084 switch (decoder->state) {
1085 case AWS_MQTT5_DS_READ_PACKET_TYPE:
1086 result = s_aws_mqtt5_decoder_read_packet_type_on_data(decoder, &data);
1087 break;
1088
1089 case AWS_MQTT5_DS_READ_REMAINING_LENGTH:
1090 result = s_aws_mqtt5_decoder_read_remaining_length_on_data(decoder, &data);
1091 break;
1092
1093 case AWS_MQTT5_DS_READ_PACKET:
1094 result = s_aws_mqtt5_decoder_read_packet_on_data(decoder, &data);
1095 break;
1096
1097 default:
1098 result = AWS_MQTT5_DRT_ERROR;
1099 break;
1100 }
1101 }
1102
1103 if (result == AWS_MQTT5_DRT_ERROR) {
1104 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);
1105 decoder->state = AWS_MQTT5_DS_FATAL_ERROR;
1106 return AWS_OP_ERR;
1107 }
1108
1109 return AWS_OP_SUCCESS;
1110 }
1111
1112 static struct aws_mqtt5_decoder_function_table s_aws_mqtt5_decoder_default_function_table = {
1113 .decoders_by_packet_type =
1114 {
1115 NULL, /* RESERVED = 0 */
1116 NULL, /* CONNECT */
1117 &s_aws_mqtt5_decoder_decode_connack, /* CONNACK */
1118 &s_aws_mqtt5_decoder_decode_publish, /* PUBLISH */
1119 &s_aws_mqtt5_decoder_decode_puback, /* PUBACK */
1120 NULL, /* PUBREC */
1121 NULL, /* PUBREL */
1122 NULL, /* PUBCOMP */
1123 NULL, /* SUBSCRIBE */
1124 &s_aws_mqtt5_decoder_decode_suback, /* SUBACK */
1125 NULL, /* UNSUBSCRIBE */
1126 &s_aws_mqtt5_decoder_decode_unsuback, /* UNSUBACK */
1127 NULL, /* PINGREQ */
1128 &s_aws_mqtt5_decoder_decode_pingresp, /* PINGRESP */
1129 &s_aws_mqtt5_decoder_decode_disconnect, /* DISCONNECT */
1130 NULL /* AUTH */
1131 },
1132 };
1133
1134 const struct aws_mqtt5_decoder_function_table *g_aws_mqtt5_default_decoder_table =
1135 &s_aws_mqtt5_decoder_default_function_table;
1136
1137 int aws_mqtt5_decoder_init(
1138 struct aws_mqtt5_decoder *decoder,
1139 struct aws_allocator *allocator,
1140 struct aws_mqtt5_decoder_options *options) {
1141 AWS_ZERO_STRUCT(*decoder);
1142
1143 decoder->options = *options;
1144
1145 if (decoder->options.decoder_table == NULL) {
1146 decoder->options.decoder_table = g_aws_mqtt5_default_decoder_table;
1147 }
1148
1149 decoder->allocator = allocator;
1150
1151 decoder->state = AWS_MQTT5_DS_READ_PACKET_TYPE;
1152
1153 if (aws_byte_buf_init(&decoder->scratch_space, allocator, AWS_MQTT5_DECODER_BUFFER_START_SIZE)) {
1154 return AWS_OP_ERR;
1155 }
1156
1157 return AWS_OP_SUCCESS;
1158 }
1159
1160 void aws_mqtt5_decoder_reset(struct aws_mqtt5_decoder *decoder) {
1161 s_reset_decoder_for_new_packet(decoder);
1162
1163 decoder->state = AWS_MQTT5_DS_READ_PACKET_TYPE;
1164 }
1165
1166 void aws_mqtt5_decoder_clean_up(struct aws_mqtt5_decoder *decoder) {
1167 aws_byte_buf_clean_up(&decoder->scratch_space);
1168 }
1169
1170 void aws_mqtt5_decoder_set_inbound_topic_alias_resolver(
1171 struct aws_mqtt5_decoder *decoder,
1172 struct aws_mqtt5_inbound_topic_alias_resolver *resolver) {
1173 decoder->topic_alias_resolver = resolver;
1174 }