@@ -96,6 +96,22 @@ static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) {
96
96
return p + len;
97
97
}
98
98
99
+ // packetAdditionalLen is a helper function used to figure out
100
+ // how bigger the payload needs to be in order to account for
101
+ // its variable length field. As per
102
+ // http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
103
+ // See also readFullPacket
104
+ static uint16_t packetAdditionalLen (uint32_t currLen) {
105
+ /* Increase length field based on current length */
106
+ if (currLen < 128 ) // 7-bits
107
+ return 0 ;
108
+ if (currLen < 16384 ) // 14-bits
109
+ return 1 ;
110
+ if (currLen < 2097152 ) // 21-bits
111
+ return 2 ;
112
+ return 3 ;
113
+ }
114
+
99
115
// Adafruit_MQTT Definition ////////////////////////////////////////////////////
100
116
101
117
Adafruit_MQTT::Adafruit_MQTT (const char *server, uint16_t port, const char *cid,
@@ -267,7 +283,8 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
267
283
DEBUG_PRINT (F (" Packet Length:\t " ));
268
284
DEBUG_PRINTLN (value);
269
285
270
- if (value > (maxsize - (pbuff - buffer) - 1 )) {
286
+ // maxsize is limited to 65536 by 16-bit unsigned
287
+ if (value > uint32_t (maxsize - (pbuff - buffer) - 1 )) {
271
288
DEBUG_PRINTLN (F (" Packet too big for buffer" ));
272
289
rlen = readPacket (pbuff, (maxsize - (pbuff - buffer) - 1 ), timeout);
273
290
} else {
@@ -474,22 +491,6 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
474
491
return handleSubscriptionPacket (len);
475
492
}
476
493
477
- namespace {
478
-
479
- uint16_t topicOffsetFromLength (uint16_t const len)
480
- {
481
- if (len < 128 ) { // 7 bits (+1 continuation bit)
482
- return 0 ;
483
- } else if (len < 16384 ) { // 14 bits (+2 continuation bits)
484
- return 1 ;
485
- } else if (len < 2097152 ) { // 21 bits
486
- return 2 ;
487
- }
488
- return 3 ;
489
- }
490
-
491
- } // anon
492
-
493
494
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket (uint16_t len) {
494
495
uint16_t i, topiclen, datalen;
495
496
@@ -508,9 +509,9 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
508
509
}
509
510
510
511
// Parse out length of packet.
511
- uint16_t const topicoffset = topicOffsetFromLength (len);
512
+ uint16_t const topicoffset = packetAdditionalLen (len);
512
513
uint16_t const topicstart = topicoffset + 4 ;
513
- topiclen = buffer[3 + topicoffset];
514
+ topiclen = buffer[3 + topicoffset];
514
515
DEBUG_PRINT (F (" Looking for subscription len " ));
515
516
DEBUG_PRINTLN (topiclen);
516
517
@@ -523,8 +524,8 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
523
524
continue ;
524
525
// Stop if the subscription topic matches the received topic. Be careful
525
526
// to make comparison case insensitive.
526
- if (strncasecmp ((char *)buffer + topicstart, subscriptions[i]->topic , topiclen) ==
527
- 0 ) {
527
+ if (strncasecmp ((char *)buffer + topicstart, subscriptions[i]->topic ,
528
+ topiclen) == 0 ) {
528
529
DEBUG_PRINT (F (" Found sub #" ));
529
530
DEBUG_PRINTLN (i);
530
531
break ;
@@ -552,8 +553,8 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
552
553
datalen = SUBSCRIPTIONDATALEN - 1 ; // cut it off
553
554
}
554
555
// extract out just the data, into the subscription object itself
555
- memmove (subscriptions[i]->lastread , buffer + topicstart + topiclen + packet_id_len,
556
- datalen);
556
+ memmove (subscriptions[i]->lastread ,
557
+ buffer + topicstart + topiclen + packet_id_len, datalen);
557
558
subscriptions[i]->datalen = datalen;
558
559
DEBUG_PRINT (F (" Data len: " ));
559
560
DEBUG_PRINTLN (datalen);
@@ -687,21 +688,6 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
687
688
return len;
688
689
}
689
690
690
- // packetAdditionalLen is a helper function used to figure out
691
- // how bigger the payload needs to be in order to account for
692
- // its variable length field. As per
693
- // http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
694
- static uint16_t packetAdditionalLen (uint16_t currLen) {
695
- /* Increase length field based on current length */
696
- if (currLen < 128 )
697
- return 0 ;
698
- if (currLen < 16384 )
699
- return 1 ;
700
- if (currLen < 2097151 )
701
- return 2 ;
702
- return 3 ;
703
- }
704
-
705
691
// as per
706
692
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
707
693
uint16_t Adafruit_MQTT::publishPacket (uint8_t *packet, const char *topic,
0 commit comments