Skip to content

Commit

Permalink
updated signature for lwmqtt_varnum_length
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Aug 19, 2017
1 parent 4f039e4 commit a878735
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
17 changes: 11 additions & 6 deletions src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 16 additions & 12 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
28 changes: 20 additions & 8 deletions tests/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit a878735

Please sign in to comment.