/* Computes and encodes length for the MQTT fixed header.  */
static uint8_t packet_length_encode(uint32_t length, struct buf_ctx *buf)
/* Computes and encodes length for the MQTT fixed header.  */
static uint8_t packet_length_encode(uint32_t length, struct buf_ctx *buf)
{
	uint8_t encoded_bytes = 0U;
	NET_DBG(">> length:0x%08x cur:%p, end:%p", length,
		 (buf == NULL) ? 0 : (void *)buf->cur, (buf == NULL) ? 0 : (void *)buf->end);
	do {
		encoded_bytes++;
		if (buf != NULL) {
			*(buf->cur) = length & MQTT_LENGTH_VALUE_MASK;
		}
		length >>= MQTT_LENGTH_SHIFT;
		if (buf != NULL) {
			if (length > 0) {
				*(buf->cur) |= MQTT_LENGTH_CONTINUATION_BIT;
			}
			buf->cur++;
		}
	} while (length > 0);
	return encoded_bytes;
}