Debian Codegraph @

crt/aws-c-mqtt/source/fixed_header.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/fixed_header.h>
7
8 /**
9 * Implements encoding & decoding of the remaining_length field across 1-4 bytes [MQTT-2.2.3].
10 *
11 * Any number less than or equal to 127 (7 bit max) can be written into a single byte, where any number larger than 128
12 * may be written into multiple bytes, using the most significant bit (128) as a continuation flag.
13 */
14 static int s_encode_remaining_length(struct aws_byte_buf *buf, size_t remaining_length) {
15
16 AWS_PRECONDITION(buf);
17 AWS_PRECONDITION(remaining_length < UINT32_MAX);
18
19 do {
20 uint8_t encoded_byte = remaining_length % 128;
21 remaining_length /= 128;
22 if (remaining_length) {
23 encoded_byte |= 128;
24 }
25 if (!aws_byte_buf_write_u8(buf, encoded_byte)) {
26 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
27 }
28 } while (remaining_length);
29
30 return AWS_OP_SUCCESS;
31 }
32
33 int aws_mqtt311_decode_remaining_length(struct aws_byte_cursor *cur, size_t *remaining_length_out) {
34
35 AWS_PRECONDITION(cur);
36
37 /* Read remaining_length */
38 size_t multiplier = 1;
39 size_t remaining_length = 0;
40 while (true) {
41 uint8_t encoded_byte;
42 if (!aws_byte_cursor_read_u8(cur, &encoded_byte)) {
43 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
44 }
45
46 remaining_length += (encoded_byte & 127) * multiplier;
47 multiplier *= 128;
48
49 if (!(encoded_byte & 128)) {
50 break;
51 }
52 if (multiplier > 128 * 128 * 128) {
53 /* If high order bit is set on last byte, value is malformed */
54 return aws_raise_error(AWS_ERROR_MQTT_INVALID_REMAINING_LENGTH);
55 }
56 }
57
58 *remaining_length_out = remaining_length;
59 return AWS_OP_SUCCESS;
60 }
61
62 enum aws_mqtt_packet_type aws_mqtt_get_packet_type(const uint8_t *buffer) {
63 return *buffer >> 4;
64 }
65
66 bool aws_mqtt_packet_has_flags(const struct aws_mqtt_fixed_header *header) {
67
68 /* Parse attributes based on packet type */
69 switch (header->packet_type) {
70 case AWS_MQTT_PACKET_SUBSCRIBE:
71 case AWS_MQTT_PACKET_UNSUBSCRIBE:
72 case AWS_MQTT_PACKET_PUBLISH:
73 case AWS_MQTT_PACKET_PUBREL:
74 return true;
75 break;
76
77 case AWS_MQTT_PACKET_CONNECT:
78 case AWS_MQTT_PACKET_CONNACK:
79 case AWS_MQTT_PACKET_PUBACK:
80 case AWS_MQTT_PACKET_PUBREC:
81 case AWS_MQTT_PACKET_PUBCOMP:
82 case AWS_MQTT_PACKET_SUBACK:
83 case AWS_MQTT_PACKET_UNSUBACK:
84 case AWS_MQTT_PACKET_PINGREQ:
85 case AWS_MQTT_PACKET_PINGRESP:
86 case AWS_MQTT_PACKET_DISCONNECT:
87 return false;
88
89 default:
90 return false;
91 }
92 }
93
94 int aws_mqtt_fixed_header_encode(struct aws_byte_buf *buf, const struct aws_mqtt_fixed_header *header) {
95
96 AWS_PRECONDITION(buf);
97 AWS_PRECONDITION(header);
98
99 /* Check that flags are 0 if they must not be present */
100 if (!aws_mqtt_packet_has_flags(header) && header->flags != 0) {
101 return aws_raise_error(AWS_ERROR_MQTT_INVALID_RESERVED_BITS);
102 }
103
104 /* Write packet type and flags */
105 uint8_t byte_1 = (uint8_t)((header->packet_type << 4) | (header->flags & 0xF));
106 if (!aws_byte_buf_write_u8(buf, byte_1)) {
107 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
108 }
109
110 /* Write remaining length */
111 if (s_encode_remaining_length(buf, header->remaining_length)) {
112 return AWS_OP_ERR;
113 }
114
115 return AWS_OP_SUCCESS;
116 }
117
118 int aws_mqtt_fixed_header_decode(struct aws_byte_cursor *cur, struct aws_mqtt_fixed_header *header) {
119
120 AWS_PRECONDITION(cur);
121 AWS_PRECONDITION(header);
122
123 /* Read packet type and flags */
124 uint8_t byte_1 = 0;
125 if (!aws_byte_cursor_read_u8(cur, &byte_1)) {
126 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
127 }
128 header->packet_type = aws_mqtt_get_packet_type(&byte_1);
129 header->flags = byte_1 & 0xF;
130
131 /* Read remaining length */
132 if (aws_mqtt311_decode_remaining_length(cur, &header->remaining_length)) {
133 return AWS_OP_ERR;
134 }
135 if (cur->len < header->remaining_length) {
136 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
137 }
138
139 /* Check that flags are 0 if they must not be present */
140 if (!aws_mqtt_packet_has_flags(header) && header->flags != 0) {
141 return aws_raise_error(AWS_ERROR_MQTT_INVALID_RESERVED_BITS);
142 }
143
144 return AWS_OP_SUCCESS;
145 }