Commit 6211de87 authored by Pisit Sawangvonganan's avatar Pisit Sawangvonganan Committed by Carles Cufi
Browse files

net: mqtt: improve decoder buffer handling



Improve buffer handling logic to use local variables extensively.

This change reduces the number of pointer dereferences, which leads
to more efficient runtime and helps reduce the code size.

Signed-off-by: default avatarPisit Sawangvonganan <pisit@ndrsolution.com>
parent 45282a41
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -29,13 +29,17 @@ LOG_MODULE_REGISTER(net_mqtt_dec, CONFIG_MQTT_LOG_LEVEL);
 */
static int unpack_uint8(struct buf_ctx *buf, uint8_t *val)
{
	NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end);
	uint8_t *cur = buf->cur;
	uint8_t *end = buf->end;

	NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end);

	if ((buf->end - buf->cur) < sizeof(uint8_t)) {
	if ((end - cur) < sizeof(uint8_t)) {
		return -EINVAL;
	}

	*val = *(buf->cur++);
	*val = cur[0];
	buf->cur = (cur + sizeof(uint8_t));

	NET_DBG("<< val:%02x", *val);

@@ -55,14 +59,17 @@ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val)
 */
static int unpack_uint16(struct buf_ctx *buf, uint16_t *val)
{
	NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end);
	uint8_t *cur = buf->cur;
	uint8_t *end = buf->end;

	NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end);

	if ((buf->end - buf->cur) < sizeof(uint16_t)) {
	if ((end - cur) < sizeof(uint16_t)) {
		return -EINVAL;
	}

	*val = *(buf->cur++) << 8; /* MSB */
	*val |= *(buf->cur++); /* LSB */
	*val = sys_get_be16(cur);
	buf->cur = (cur + sizeof(uint16_t));

	NET_DBG("<< val:%04x", *val);