fixed_header.c 4.4 KB

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