-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_parser.c
78 lines (68 loc) · 2.67 KB
/
mqtt_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (C) 2022 Mikhail Burakov. This file is part of mqttfs.
*
* mqttfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mqttfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mqttfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mqtt_parser.h"
#include <setjmp.h>
#include <stdint.h>
#include "str.h"
static uint8_t ReadByte(const uint8_t** buffer, size_t* size, jmp_buf jmpbuf) {
if (!*size) longjmp(jmpbuf, kMqttParseStatusReadMore);
uint8_t result = **buffer;
(*buffer)++;
(*size)--;
return result;
}
static size_t ReadRemainingLength(const uint8_t** buffer, size_t* size,
jmp_buf jmpbuf) {
size_t result = 0;
for (size_t counter = 0; counter < 4; counter++) {
uint8_t byte = ReadByte(buffer, size, jmpbuf);
result |= (byte & 0x7full) << (7ull * counter);
if (~byte & 0x80) return result;
}
longjmp(jmpbuf, kMqttParseStatusError);
}
enum MqttParseStatus MqttParseMessage(const void** buffer, size_t* size,
struct Str* topic_view,
const void** payload,
size_t* payload_len) {
jmp_buf jmpbuf;
int result = setjmp(jmpbuf);
if (result) return (enum MqttParseStatus)result;
const uint8_t* buffer_copy = *buffer;
size_t size_copy = *size;
uint8_t packet_type = ReadByte(&buffer_copy, &size_copy, jmpbuf);
size_t remaining_length =
ReadRemainingLength(&buffer_copy, &size_copy, jmpbuf);
if (remaining_length > size_copy) return kMqttParseStatusReadMore;
if ((packet_type & 0xf0) != 0x30) {
*buffer = buffer_copy + remaining_length;
*size = size_copy - remaining_length;
return kMqttParseStatusSkipped;
}
uint16_t topic_len = (buffer_copy[0] << 8 | buffer_copy[1]) & 0xffff;
if (topic_len > remaining_length) return kMqttParseStatusError;
*buffer = buffer_copy + remaining_length;
*size = size_copy - remaining_length;
struct Str topic = {
.size = topic_len,
.data = (const char*)buffer_copy + sizeof(uint16_t),
};
*topic_view = topic;
*payload = buffer_copy + sizeof(uint16_t) + topic_len;
*payload_len = remaining_length - sizeof(uint16_t) - topic_len;
return kMqttParseStatusSuccess;
}