3030#include "pack.h"
3131#include "mqtt.h"
3232
33- typedef int mqtt_unpack_handler (unsigned char * , struct mqtt_packet * , size_t );
33+ typedef int mqtt_unpack_handler (u8 * , struct mqtt_packet * , usize );
3434
35- typedef size_t mqtt_pack_handler (const struct mqtt_packet * , unsigned char * );
35+ typedef usize mqtt_pack_handler (const struct mqtt_packet * , u8 * );
3636
37- static int unpack_mqtt_connect (unsigned char * , struct mqtt_packet * , size_t );
37+ static int unpack_mqtt_connect (u8 * , struct mqtt_packet * , usize );
3838
39- static int unpack_mqtt_publish (unsigned char * , struct mqtt_packet * , size_t );
39+ static int unpack_mqtt_publish (u8 * , struct mqtt_packet * , usize );
4040
41- static int unpack_mqtt_subscribe (unsigned char * ,
42- struct mqtt_packet * , size_t );
41+ static int unpack_mqtt_subscribe (u8 * , struct mqtt_packet * , usize );
4342
44- static int unpack_mqtt_unsubscribe (unsigned char * ,
45- struct mqtt_packet * , size_t );
43+ static int unpack_mqtt_unsubscribe (u8 * , struct mqtt_packet * , usize );
4644
47- static int unpack_mqtt_ack (unsigned char * , struct mqtt_packet * , size_t );
45+ static int unpack_mqtt_ack (u8 * , struct mqtt_packet * , usize );
4846
49- static size_t pack_mqtt_header (const union mqtt_header * , unsigned char * );
47+ static usize pack_mqtt_header (const union mqtt_header * , u8 * );
5048
51- static size_t pack_mqtt_ack (const struct mqtt_packet * , unsigned char * );
49+ static usize pack_mqtt_ack (const struct mqtt_packet * , u8 * );
5250
53- static size_t pack_mqtt_connack (const struct mqtt_packet * , unsigned char * );
51+ static usize pack_mqtt_connack (const struct mqtt_packet * , u8 * );
5452
55- static size_t pack_mqtt_suback (const struct mqtt_packet * , unsigned char * );
53+ static usize pack_mqtt_suback (const struct mqtt_packet * , u8 * );
5654
57- static size_t pack_mqtt_publish (const struct mqtt_packet * , unsigned char * );
55+ static usize pack_mqtt_publish (const struct mqtt_packet * , u8 * );
5856
5957/* MQTT v3.1.1 standard */
6058static const int MAX_LEN_BYTES = 4 ;
@@ -99,10 +97,10 @@ static mqtt_pack_handler *pack_handlers[13] = {
9997 * required to store itself. Refer to MQTT v3.1.1 algorithm for the
10098 * implementation.
10199 */
102- int mqtt_encode_length (unsigned char * buf , size_t len ) {
100+ int mqtt_encode_length (u8 * buf , usize len ) {
103101
104102 int bytes = 0 ;
105- short encoded = 0 ;
103+ u16 encoded = 0 ;
106104
107105 do {
108106
@@ -130,11 +128,11 @@ int mqtt_encode_length(unsigned char *buf, size_t len) {
130128 *
131129 * TODO Handle case where multiplier > 128 * 128 * 128
132130 */
133- size_t mqtt_decode_length (unsigned char * buf , unsigned * pos ) {
131+ usize mqtt_decode_length (u8 * buf , unsigned * pos ) {
134132
135- char c ;
136- unsigned long long multiplier = 1LL ;
137- unsigned long long value = 0LL ;
133+ u8 c ;
134+ u64 multiplier = 1LL ;
135+ u64 value = 0LL ;
138136 * pos = 0 ;
139137
140138 do {
@@ -212,16 +210,15 @@ size_t mqtt_decode_length(unsigned char *buf, unsigned *pos) {
212210 * unpack from the Variable Header position to the end of the packet as stated
213211 * by the total length expected.
214212 */
215- static int unpack_mqtt_connect (unsigned char * buf ,
216- struct mqtt_packet * pkt , size_t len ) {
213+ static int unpack_mqtt_connect (u8 * buf , struct mqtt_packet * pkt , usize len ) {
217214
218215 /*
219216 * For now we ignore checks on protocol name and reserved bits, just skip
220217 * to the 8th byte
221218 */
222219 buf += 7 ;
223220
224- unsigned int cid_len = 0 ;
221+ u16 cid_len = 0 ;
225222
226223 /*
227224 * Read variable header byte flags, followed by keepalive MSB and LSB
@@ -240,23 +237,23 @@ static int unpack_mqtt_connect(unsigned char *buf,
240237 /* Read the will topic and message if will is set on flags */
241238 if (pkt -> connect .bits .will == 1 ) {
242239
243- uint16_t will_topic_len = unpack_integer (& buf , 'H' );
240+ u16 will_topic_len = unpack_integer (& buf , 'H' );
244241 pkt -> connect .payload .will_topic = unpack_bytes (& buf , will_topic_len );
245242
246- uint16_t will_message_len = unpack_integer (& buf , 'H' );
247- pkt -> connect .payload .will_message = unpack_bytes (& buf , will_message_len );
243+ u16 will_msg_len = unpack_integer (& buf , 'H' );
244+ pkt -> connect .payload .will_message = unpack_bytes (& buf , will_msg_len );
248245
249246 }
250247
251248 /* Read the username if username flag is set */
252249 if (pkt -> connect .bits .username == 1 ) {
253- uint16_t username_len = unpack_integer (& buf , 'H' );
250+ u16 username_len = unpack_integer (& buf , 'H' );
254251 pkt -> connect .payload .username = unpack_bytes (& buf , username_len );
255252 }
256253
257254 /* Read the password if password flag is set */
258255 if (pkt -> connect .bits .password == 1 ) {
259- uint16_t password_len = unpack_integer (& buf , 'H' );
256+ u16 password_len = unpack_integer (& buf , 'H' );
260257 pkt -> connect .payload .password = unpack_bytes (& buf , password_len );
261258 }
262259
@@ -303,29 +300,28 @@ static int unpack_mqtt_connect(unsigned char *buf,
303300 * unpack from the Variable Header position to the end of the packet as stated
304301 * by the total length expected.
305302 */
306- static int unpack_mqtt_publish (unsigned char * buf ,
307- struct mqtt_packet * pkt , size_t len ) {
303+ static int unpack_mqtt_publish (u8 * buf , struct mqtt_packet * pkt , usize len ) {
308304 /* Read topic length and topic of the soon-to-be-published message */
309- uint16_t topic_len = unpack_integer (& buf , 'H' );
305+ u16 topic_len = unpack_integer (& buf , 'H' );
310306 pkt -> publish .topiclen = topic_len ;
311307 pkt -> publish .topic = unpack_bytes (& buf , topic_len );
312308
313309 if (!pkt -> publish .topic )
314310 return - MQTT_ERR ;
315311
316- uint64_t message_len = len ;
312+ u64 message_len = len ;
317313
318314 /* Read packet id */
319315 if (pkt -> header .bits .qos > AT_MOST_ONCE ) {
320316 pkt -> publish .pkt_id = unpack_integer (& buf , 'H' );
321- message_len -= sizeof (uint16_t );
317+ message_len -= sizeof (u16 );
322318 }
323319
324320 /*
325321 * Message len is calculated subtracting the length of the variable header
326322 * from the Remaining Length field that is in the Fixed Header
327323 */
328- message_len -= (sizeof (uint16_t ) + topic_len );
324+ message_len -= (sizeof (u16 ) + topic_len );
329325 pkt -> publish .payloadlen = message_len ;
330326 pkt -> publish .payload = unpack_bytes (& buf , message_len );
331327
@@ -335,15 +331,14 @@ static int unpack_mqtt_publish(unsigned char *buf,
335331 return MQTT_OK ;
336332}
337333
338- static int unpack_mqtt_subscribe (unsigned char * buf ,
339- struct mqtt_packet * pkt , size_t len ) {
334+ static int unpack_mqtt_subscribe (u8 * buf , struct mqtt_packet * pkt , usize len ) {
340335
341336 struct mqtt_subscribe subscribe ;
342337 subscribe .tuples = NULL ;
343338
344339 /* Read packet id */
345340 subscribe .pkt_id = unpack_integer (& buf , 'H' );
346- len -= sizeof (uint16_t );
341+ len -= sizeof (u16 );
347342
348343 /*
349344 * Read in a loop all remaining bytes specified by len of the Fixed Header.
@@ -352,11 +347,11 @@ static int unpack_mqtt_subscribe(unsigned char *buf,
352347 * - topic filter (string)
353348 * - qos
354349 */
355- int i = 0 ;
350+ usize i = 0 ;
356351 for (; len > 0 ; ++ i ) {
357352 /* Read length bytes of the first topic filter */
358- unsigned int topic_len = unpack_integer (& buf , 'H' );
359- len -= sizeof (uint16_t );
353+ u16 topic_len = unpack_integer (& buf , 'H' );
354+ len -= sizeof (u16 );
360355
361356 /* We have to make room for additional incoming tuples */
362357 subscribe .tuples = xrealloc (subscribe .tuples ,
@@ -369,7 +364,7 @@ static int unpack_mqtt_subscribe(unsigned char *buf,
369364
370365 len -= topic_len ;
371366 subscribe .tuples [i ].qos = unpack_integer (& buf , 'B' );
372- len -= sizeof (uint8_t );
367+ len -= sizeof (u8 );
373368 }
374369
375370 subscribe .tuples_len = i ;
@@ -381,28 +376,28 @@ static int unpack_mqtt_subscribe(unsigned char *buf,
381376 return - MQTT_ERR ;
382377}
383378
384- static int unpack_mqtt_unsubscribe (unsigned char * buf ,
385- struct mqtt_packet * pkt , size_t len ) {
379+ static int unpack_mqtt_unsubscribe (u8 * buf ,
380+ struct mqtt_packet * pkt , usize len ) {
386381
387382 struct mqtt_unsubscribe unsubscribe ;
388383 unsubscribe .tuples = NULL ;
389384
390385 /* Read packet id */
391386 unsubscribe .pkt_id = unpack_integer (& buf , 'H' );
392- len -= sizeof (uint16_t );
387+ len -= sizeof (u16 );
393388
394389 /*
395390 * Read in a loop all remaining bytes specified by len of the Fixed Header.
396391 * From now on the payload consists of 2-tuples formed by:
397392 * - topic length
398393 * - topic filter (string)
399394 */
400- int i = 0 ;
395+ usize i = 0 ;
401396 for (; len > 0 ; ++ i ) {
402397
403398 /* Read length bytes of the first topic filter */
404- uint16_t topic_len = unpack_integer (& buf , 'H' );
405- len -= sizeof (uint16_t );
399+ u16 topic_len = unpack_integer (& buf , 'H' );
400+ len -= sizeof (u16 );
406401
407402 /* We have to make room for additional incoming tuples */
408403 unsubscribe .tuples = xrealloc (unsubscribe .tuples ,
@@ -425,8 +420,7 @@ static int unpack_mqtt_unsubscribe(unsigned char *buf,
425420 return - MQTT_ERR ;
426421}
427422
428- static int unpack_mqtt_ack (unsigned char * buf ,
429- struct mqtt_packet * pkt , size_t len ) {
423+ static int unpack_mqtt_ack (u8 * buf , struct mqtt_packet * pkt , usize len ) {
430424 pkt -> ack = (struct mqtt_ack ) { .pkt_id = unpacku16 (buf ) };
431425 return MQTT_OK ;
432426}
@@ -435,11 +429,10 @@ static int unpack_mqtt_ack(unsigned char *buf,
435429 * Main unpacking function entry point. Call the correct unpacking function
436430 * through a dispatch table
437431 */
438- int mqtt_unpack (unsigned char * buf , struct mqtt_packet * pkt ,
439- unsigned char byte , size_t len ) {
432+ int mqtt_unpack (u8 * buf , struct mqtt_packet * pkt , u8 byte , usize len ) {
440433
441434 int rc = MQTT_OK ;
442- unsigned type = byte >> 4 ;
435+ u8 type = byte >> 4 ;
443436
444437 pkt -> header = (union mqtt_header ) { .byte = byte };
445438
@@ -458,8 +451,7 @@ int mqtt_unpack(unsigned char *buf, struct mqtt_packet *pkt,
458451 * Meant to be called through a dispatch table, with command opcode as index
459452 */
460453
461- static size_t pack_mqtt_header (const union mqtt_header * hdr ,
462- unsigned char * buf ) {
454+ static usize pack_mqtt_header (const union mqtt_header * hdr , u8 * buf ) {
463455 pack (buf ++ , "B" , hdr -> byte );
464456
465457 /* Encode 0 length bytes, message like this have only a fixed header */
@@ -468,15 +460,14 @@ static size_t pack_mqtt_header(const union mqtt_header *hdr,
468460 return MQTT_HEADER_LEN ;
469461}
470462
471- static size_t pack_mqtt_ack (const struct mqtt_packet * pkt , unsigned char * buf ) {
463+ static usize pack_mqtt_ack (const struct mqtt_packet * pkt , u8 * buf ) {
472464
473465 pack (buf , "BBH" , pkt -> header .byte , MQTT_HEADER_LEN , pkt -> ack .pkt_id );
474466
475467 return MQTT_ACK_LEN ;
476468}
477469
478- static size_t pack_mqtt_connack (const struct mqtt_packet * pkt ,
479- unsigned char * buf ) {
470+ static usize pack_mqtt_connack (const struct mqtt_packet * pkt , u8 * buf ) {
480471
481472 pack (buf ++ , "B" , pkt -> header .byte );
482473 buf += mqtt_encode_length (buf , MQTT_HEADER_LEN );
@@ -486,11 +477,10 @@ static size_t pack_mqtt_connack(const struct mqtt_packet *pkt,
486477 return MQTT_ACK_LEN ;
487478}
488479
489- static size_t pack_mqtt_suback (const struct mqtt_packet * pkt ,
490- unsigned char * buf ) {
480+ static usize pack_mqtt_suback (const struct mqtt_packet * pkt , u8 * buf ) {
491481
492- size_t len = 0 ;
493- size_t pktlen = mqtt_size (pkt , & len );
482+ usize len = 0 ;
483+ usize pktlen = mqtt_size (pkt , & len );
494484
495485 pack (buf ++ , "B" , pkt -> header .byte );
496486 buf += mqtt_encode_length (buf , len );
@@ -502,17 +492,16 @@ static size_t pack_mqtt_suback(const struct mqtt_packet *pkt,
502492 return pktlen ;
503493}
504494
505- static size_t pack_mqtt_publish (const struct mqtt_packet * pkt ,
506- unsigned char * buf ) {
495+ static usize pack_mqtt_publish (const struct mqtt_packet * pkt , u8 * buf ) {
507496
508497 /*
509498 * We must calculate the total length of the packet including header and
510499 * length field of the fixed header part
511500 */
512501
513502 // Total len of the packet excluding fixed header len
514- size_t len = 0L ;
515- size_t pktlen = mqtt_size (pkt , & len );
503+ usize len = 0L ;
504+ usize pktlen = mqtt_size (pkt , & len );
516505
517506 pack (buf ++ , "B" , pkt -> header .byte );
518507
@@ -541,8 +530,8 @@ static size_t pack_mqtt_publish(const struct mqtt_packet *pkt,
541530 * Main packing function entry point. Call the correct packing function through
542531 * a dispatch table
543532 */
544- size_t mqtt_pack (const struct mqtt_packet * pkt , unsigned char * buf ) {
545- unsigned type = pkt -> header .bits .type ;
533+ usize mqtt_pack (const struct mqtt_packet * pkt , u8 * buf ) {
534+ u8 type = pkt -> header .bits .type ;
546535 if (type == PINGREQ || type == PINGRESP )
547536 return pack_mqtt_header (& pkt -> header , buf );
548537 return pack_handlers [type ](pkt , buf );
@@ -552,20 +541,18 @@ size_t mqtt_pack(const struct mqtt_packet *pkt, unsigned char *buf) {
552541 * MQTT packets building functions
553542 */
554543
555- void mqtt_ack (struct mqtt_packet * pkt , unsigned short pkt_id ) {
544+ void mqtt_ack (struct mqtt_packet * pkt , u16 pkt_id ) {
556545 pkt -> ack = (struct mqtt_ack ) { .pkt_id = pkt_id };
557546}
558547
559- void mqtt_connack (struct mqtt_packet * pkt ,
560- unsigned char cflags , unsigned char rc ) {
548+ void mqtt_connack (struct mqtt_packet * pkt , u8 cflags , u8 rc ) {
561549 pkt -> connack = (struct mqtt_connack ) {
562550 .byte = cflags ,
563551 .rc = rc
564552 };
565553}
566554
567- void mqtt_suback (struct mqtt_packet * pkt , unsigned short pkt_id ,
568- unsigned char * rcs , unsigned short rcslen ) {
555+ void mqtt_suback (struct mqtt_packet * pkt , u16 pkt_id , u8 * rcs , u16 rcslen ) {
569556 pkt -> suback = (struct mqtt_suback ) {
570557 .pkt_id = pkt_id ,
571558 .rcslen = rcslen ,
@@ -574,9 +561,8 @@ void mqtt_suback(struct mqtt_packet *pkt, unsigned short pkt_id,
574561 memcpy (pkt -> suback .rcs , rcs , rcslen );
575562}
576563
577- void mqtt_packet_publish (struct mqtt_packet * pkt , unsigned short pkt_id ,
578- size_t topiclen , unsigned char * topic ,
579- size_t payloadlen , unsigned char * payload ) {
564+ void mqtt_packet_publish (struct mqtt_packet * pkt , u16 pkt_id , usize topiclen ,
565+ u8 * topic , usize payloadlen , u8 * payload ) {
580566 pkt -> publish = (struct mqtt_publish ) {
581567 .pkt_id = pkt_id ,
582568 .topiclen = topiclen ,
@@ -626,8 +612,8 @@ void mqtt_set_dup(struct mqtt_packet *pkt) {
626612 * of length 4, 1 byte for the fixed header, 1 for the encoded length of the
627613 * packet and 2 for the packet identifier value, which is a 16 bit integer
628614 */
629- int mqtt_pack_mono (unsigned char * buf , unsigned char op , unsigned short id ) {
630- unsigned byte = 0 ;
615+ int mqtt_pack_mono (u8 * buf , u8 op , u16 id ) {
616+ u8 byte = 0 ;
631617 switch (op ) {
632618 case PUBACK :
633619 byte = PUBACK_B ;
@@ -658,8 +644,8 @@ int mqtt_pack_mono(unsigned char *buf, unsigned char op, unsigned short id) {
658644 * excluding the fixed header (1 byte) and the bytes needed to store the
659645 * value itself (1 to 3 bytes).
660646 */
661- size_t mqtt_size (const struct mqtt_packet * pkt , size_t * len ) {
662- size_t size = 0LL ;
647+ usize mqtt_size (const struct mqtt_packet * pkt , usize * len ) {
648+ usize size = 0LL ;
663649 switch (pkt -> header .bits .type ) {
664650 case PUBLISH :
665651 size = MQTT_HEADER_LEN + sizeof (uint16_t ) +
@@ -700,7 +686,7 @@ static void mqtt_packet_free(const struct ref *refcount) {
700686}
701687
702688/* Just a packet allocing with the reference counter set */
703- struct mqtt_packet * mqtt_packet_alloc (unsigned char byte ) {
689+ struct mqtt_packet * mqtt_packet_alloc (u8 byte ) {
704690 struct mqtt_packet * packet = xmalloc (sizeof (* packet ));
705691 packet -> header = (union mqtt_header ) { .byte = byte };
706692 packet -> refcount = (struct ref ) { mqtt_packet_free , 0 };
0 commit comments