--- libaitmqtt/src/aitmqtt.c 2013/05/30 09:18:33 1.3 +++ libaitmqtt/src/aitmqtt.c 2022/09/13 22:20:59 1.3.4.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.c,v 1.3 2013/05/30 09:18:33 misho Exp $ +* $Id: aitmqtt.c,v 1.3.4.3 2022/09/13 22:20:59 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -107,18 +107,18 @@ _mqtt_readHEADER(mqtt_msg_t * __restrict buf, u_char c * mqtt_msgFree() Free MQTT message * * @msg = Message buffer - * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc() + * @keepmsg = !=0 just free message content * return: none */ void -mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) +mqtt_msgFree(mqtt_msg_t ** __restrict msg, int keepmsg) { if (msg && *msg) { if ((*msg)->msg_base) { free((*msg)->msg_base); (*msg)->msg_base = NULL; } - if (all) { + if (!keepmsg) { free(*msg); *msg = NULL; } else @@ -133,7 +133,7 @@ mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0 */ mqtt_msg_t * -mqtt_msgAlloc(u_short len) +mqtt_msgAlloc(u_int len) { mqtt_msg_t *m = NULL; @@ -166,7 +166,7 @@ mqtt_msgAlloc(u_short len) * return: -1 error or >-1 old buffer length */ int -mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len) +mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_int len) { void *p = NULL; int ret = 0; @@ -234,7 +234,7 @@ mqtt_encodeLen(u_int num) register u_int dig, i; u_int ret = 0; - if (num > 268435455) + if (num > MQTT_DATA_MAX) return (u_int) -1; for (i = 0; i < sizeof ret && num > 0; i++) { @@ -276,6 +276,7 @@ mqtt_decodeLen(void * __restrict len, int * __restrict if (n) *n = (char) (i & 0x7f) + 1; + return ret; } @@ -325,13 +326,13 @@ mqtt_pktLen(struct mqtthdr * __restrict hdr) * mqtt_str2subs Create MQTT subscribe variable from string(s) * * @csStr = null terminated string array - * @strnum = copy at most number of strings elements + * @strnum = copy at most number of strings elements, ==0 till NULL element * @qoses = QoS elements applied to subscribe variable, * count of elements must be equal with csStr elements * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub() */ mqtt_subscr_t * -mqtt_str2subs(const char **csStr, u_short strnum, u_char *qoses) +mqtt_strs2subs(const char **csStr, u_short strnum, u_char *qoses) { mqtt_subscr_t *v; register int i, items; @@ -421,11 +422,18 @@ mqtt_subAlloc(u_short num) mqtt_subscr_t * mqtt_subRealloc(mqtt_subscr_t ** __restrict subs, u_short num) { - mqtt_subscr_t *s = NULL; + mqtt_subscr_t *ss, *s = NULL; + register int i; if (!subs) return NULL; + for (i = 0, ss = *subs; ss; i++, ss++); + if (i < num) + return NULL; + if (i == num) + return *subs; + s = realloc(*subs, (num + 1) * sizeof(mqtt_subscr_t)); if (!s) { LOGERR; @@ -464,13 +472,10 @@ mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subs dst->sub_topic.msg_len); } } else { - /* - if (dst->sub_topic.msg_base) - free(dst->sub_topic.msg_base); - */ dst->sub_topic.msg_base = NULL; dst->sub_topic.msg_len = 0; } + if (src->sub_value.msg_base) { dst->sub_value.msg_base = malloc(src->sub_value.msg_len + 1); if (!dst->sub_value.msg_base) { @@ -486,10 +491,6 @@ mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subs dst->sub_value.msg_len); } } else { - /* - if (dst->sub_value.msg_base) - free(dst->sub_value.msg_base); - */ dst->sub_value.msg_base = NULL; dst->sub_value.msg_len = 0; }