From a878735abc2b62b0eae9b4f1dcb398e1143d5494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Sat, 19 Aug 2017 20:31:43 +0200 Subject: [PATCH] updated signature for lwmqtt_varnum_length --- src/helpers.c | 17 +++++++++++------ src/helpers.h | 5 +++-- src/packet.c | 28 ++++++++++++++++------------ tests/helpers.cpp | 28 ++++++++++++++++++++-------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/helpers.c b/src/helpers.c index 5a89173..54695b8 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -142,17 +142,22 @@ lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, uint8_t *buf_end, uint8_t byte) { return LWMQTT_SUCCESS; } -int lwmqtt_varnum_length(uint32_t varnum) { +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len) { if (varnum < 128) { - return 1; + *len = 1; + return LWMQTT_SUCCESS; } else if (varnum < 16384) { - return 2; + *len = 2; + return LWMQTT_SUCCESS; } else if (varnum < 2097151) { - return 3; + *len = 3; + return LWMQTT_SUCCESS; } else if (varnum < 268435455) { - return 4; + *len = 4; + return LWMQTT_SUCCESS; } else { - return -1; + *len = 0; + return LWMQTT_VARNUM_OVERFLOW; } } diff --git a/src/helpers.h b/src/helpers.h index c96b61d..a87abc8 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -89,9 +89,10 @@ lwmqtt_err_t lwmqtt_write_byte(uint8_t **buf, uint8_t *buf_end, uint8_t byte); * Returns the amount of bytes required by the variable number. * * @param varnum - The number to check. - * @return The required length or -1 if overflowed. + * @param len - The required length; + * @return LWMQTT_SUCCESS or LWMQTT_VARNUM_OVERFLOW. */ -int lwmqtt_varnum_length(uint32_t varnum); +lwmqtt_err_t lwmqtt_varnum_length(uint32_t varnum, int *len); /** * Reads a variable number from the specified buffer. The pointer is incremented by the bytes read. diff --git a/src/packet.c b/src/packet.c index 2ce97e7..320781a 100644 --- a/src/packet.c +++ b/src/packet.c @@ -114,8 +114,9 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw } // calculate remaining length length - int rem_len_len = lwmqtt_varnum_length(rem_len); - if (rem_len_len < 0) { + int rem_len_len; + lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -129,7 +130,7 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw header.bits.type = LWMQTT_CONNECT_PACKET; // write header - lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); + err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); if (err != LWMQTT_SUCCESS) { return err; } @@ -454,8 +455,9 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo } // calculate remaining length length - int rem_len_len = lwmqtt_varnum_length(rem_len); - if (rem_len_len < 0) { + int rem_len_len; + lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -467,7 +469,7 @@ lwmqtt_err_t lwmqtt_encode_publish(uint8_t *buf, size_t buf_len, size_t *len, bo header.bits.retain = msg.retained ? 1 : 0; // write header - lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); + err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); if (err != LWMQTT_SUCCESS) { return err; } @@ -517,8 +519,9 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, } // calculate remaining length length - int rem_len_len = lwmqtt_varnum_length(rem_len); - if (rem_len_len < 0) { + int rem_len_len; + lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -528,7 +531,7 @@ lwmqtt_err_t lwmqtt_encode_subscribe(uint8_t *buf, size_t buf_len, size_t *len, header.bits.qos = 1; // write header - lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); + err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); if (err != LWMQTT_SUCCESS) { return err; } @@ -633,8 +636,9 @@ lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len } // calculate remaining length length - int rem_len_len = lwmqtt_varnum_length(rem_len); - if (rem_len_len < 0) { + int rem_len_len; + lwmqtt_err_t err = lwmqtt_varnum_length(rem_len, &rem_len_len); + if (err == LWMQTT_VARNUM_OVERFLOW) { return LWMQTT_REMAINING_LENGTH_OVERFLOW; } @@ -644,7 +648,7 @@ lwmqtt_err_t lwmqtt_encode_unsubscribe(uint8_t *buf, size_t buf_len, size_t *len header.bits.qos = 1; // write header - lwmqtt_err_t err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); + err = lwmqtt_write_byte(&buf_ptr, buf_end, header.byte); if (err != LWMQTT_SUCCESS) { return err; } diff --git a/tests/helpers.cpp b/tests/helpers.cpp index 2492738..8900838 100644 --- a/tests/helpers.cpp +++ b/tests/helpers.cpp @@ -9,10 +9,13 @@ TEST(VarNum1, Valid) { memset(buf, 0, 2); for (uint32_t i = 1; i < 128; i++) { - EXPECT_EQ(1, lwmqtt_varnum_length(i)); + int len; + lwmqtt_err_t err = lwmqtt_varnum_length(i, &len); + EXPECT_EQ(err, LWMQTT_SUCCESS); + EXPECT_EQ(len, 1); uint8_t *ptr1 = buf; - lwmqtt_err_t err = lwmqtt_write_varnum(&ptr1, buf + 1, i); + err = lwmqtt_write_varnum(&ptr1, buf + 1, i); EXPECT_EQ(err, LWMQTT_SUCCESS); uint8_t *ptr2 = buf; @@ -32,10 +35,13 @@ TEST(VarNum2, Valid) { memset(buf, 0, 3); for (uint32_t i = 1; i < 128; i++) { - EXPECT_EQ(lwmqtt_varnum_length(128 * i), 2); + int len; + lwmqtt_err_t err = lwmqtt_varnum_length(128 * i, &len); + EXPECT_EQ(err, LWMQTT_SUCCESS); + EXPECT_EQ(len, 2); uint8_t *ptr1 = buf; - lwmqtt_err_t err = lwmqtt_write_varnum(&ptr1, buf + 2, 128 * i); + err = lwmqtt_write_varnum(&ptr1, buf + 2, 128 * i); EXPECT_EQ(err, LWMQTT_SUCCESS); uint8_t *ptr2 = buf; @@ -55,10 +61,13 @@ TEST(VarNum3, Valid) { memset(buf, 0, 4); for (uint32_t i = 1; i < 128; i++) { - EXPECT_EQ(lwmqtt_varnum_length(128 * 128 * i), 3); + int len; + lwmqtt_err_t err = lwmqtt_varnum_length(128 * 128 * i, &len); + EXPECT_EQ(err, LWMQTT_SUCCESS); + EXPECT_EQ(len, 3); uint8_t *ptr1 = buf; - lwmqtt_err_t err = lwmqtt_write_varnum(&ptr1, buf + 3, 128 * 128 * i); + err = lwmqtt_write_varnum(&ptr1, buf + 3, 128 * 128 * i); EXPECT_EQ(err, LWMQTT_SUCCESS); uint8_t *ptr2 = buf; @@ -78,10 +87,13 @@ TEST(VarNum4, Valid) { memset(buf, 0, 5); for (uint32_t i = 1; i < 128; i++) { - EXPECT_EQ(lwmqtt_varnum_length(128 * 128 * 128 * i), 4); + int len; + lwmqtt_err_t err = lwmqtt_varnum_length(128 * 128 * 128 * i, &len); + EXPECT_EQ(err, LWMQTT_SUCCESS); + EXPECT_EQ(len, 4); uint8_t *ptr1 = buf; - lwmqtt_err_t err = lwmqtt_write_varnum(&ptr1, buf + 4, 128 * 128 * 128 * i); + err = lwmqtt_write_varnum(&ptr1, buf + 4, 128 * 128 * 128 * i); EXPECT_EQ(err, LWMQTT_SUCCESS); uint8_t *ptr2 = buf;