--- libaitmqtt/src/aitmqtt.c 2012/06/20 15:02:24 1.2 +++ libaitmqtt/src/aitmqtt.c 2022/09/13 20:16:43 1.3.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.c,v 1.2 2012/06/20 15:02:24 misho Exp $ +* $Id: aitmqtt.c,v 1.3.4.2 2022/09/13 20:16:43 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 +Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -54,21 +54,21 @@ char mqtt_Error[STRSIZ]; #pragma GCC visibility pop // mqtt_GetErrno() Get error code of last operation -inline int +int mqtt_GetErrno() { return mqtt_Errno; } // mqtt_GetError() Get error text of last operation -inline const char * +const char * mqtt_GetError() { return mqtt_Error; } // mqtt_SetErr() Set error to variables for internal use!!! -inline void +void mqtt_SetErr(int eno, char *estr, ...) { va_list lst; @@ -82,7 +82,7 @@ mqtt_SetErr(int eno, char *estr, ...) #pragma GCC visibility push(hidden) /* _mqtt_readHEADER() read fixed header from MQTT message */ -inline struct mqtthdr * +struct mqtthdr * _mqtt_readHEADER(mqtt_msg_t * __restrict buf, u_char cmd, int *bytes, int *len) { struct mqtthdr *hdr; @@ -110,7 +110,7 @@ _mqtt_readHEADER(mqtt_msg_t * __restrict buf, u_char c * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc() * return: none */ -inline void +void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) { if (msg && *msg) { @@ -132,8 +132,8 @@ mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) * @len = >0 Allocate buffer with length * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0 */ -inline mqtt_msg_t * -mqtt_msgAlloc(u_short len) +mqtt_msg_t * +mqtt_msgAlloc(u_int len) { mqtt_msg_t *m = NULL; @@ -165,8 +165,8 @@ mqtt_msgAlloc(u_short len) * @len = new length * return: -1 error or >-1 old buffer length */ -inline int -mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len) +int +mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_int len) { void *p = NULL; int ret = 0; @@ -196,7 +196,7 @@ mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short l * @msg = Message * return: NULL error or !=NULL duplicated message, after use must call mqtt_msgFree() with all!=0 */ -inline mqtt_msg_t * +mqtt_msg_t * mqtt_msgDup(mqtt_msg_t * __restrict msg) { mqtt_msg_t *m = NULL; @@ -228,13 +228,13 @@ mqtt_msgDup(mqtt_msg_t * __restrict msg) * @num = number for encode * return: -1 error or >-1 length */ -inline u_int +u_int 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++) { @@ -256,7 +256,7 @@ mqtt_encodeLen(u_int num) * @n = sizeof bytes, if !=NULL * return: -1 error, >-1 length of message */ -inline u_int +u_int mqtt_decodeLen(void * __restrict len, int * __restrict n) { register u_int i, dig, mul; @@ -276,6 +276,7 @@ mqtt_decodeLen(void * __restrict len, int * __restrict if (n) *n = (char) (i & 0x7f) + 1; + return ret; } @@ -285,7 +286,7 @@ mqtt_decodeLen(void * __restrict len, int * __restrict * @len = length * return: -1 error, >-1 sizeof len in bytes */ -inline char +char mqtt_sizeLen(u_int len) { register char i; @@ -307,7 +308,7 @@ mqtt_sizeLen(u_int len) * @hdr = MQTT packet header * return: packet length */ -inline u_int +u_int mqtt_pktLen(struct mqtthdr * __restrict hdr) { int siz, n = 0; @@ -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() */ -inline mqtt_subscr_t * -mqtt_str2subs(const char **csStr, u_short strnum, u_char *qoses) +mqtt_subscr_t * +mqtt_strs2subs(const char **csStr, u_short strnum, u_char *qoses) { mqtt_subscr_t *v; register int i, items; @@ -366,7 +367,7 @@ mqtt_str2subs(const char **csStr, u_short strnum, u_ch * @subs = Subscribe variables * return: none */ -inline void +void mqtt_subFree(mqtt_subscr_t ** __restrict subs) { mqtt_subscr_t *v; @@ -396,7 +397,7 @@ mqtt_subFree(mqtt_subscr_t ** __restrict subs) * @num = Number of elements * return: NULL error or subscribe array, after use must call mqtt_subFree() */ -inline mqtt_subscr_t * +mqtt_subscr_t * mqtt_subAlloc(u_short num) { mqtt_subscr_t *s = NULL; @@ -418,14 +419,21 @@ mqtt_subAlloc(u_short num) * @num = Number of elements * return: NULL error or subscribe array, after use must call mqtt_subFree() */ -inline mqtt_subscr_t * +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; @@ -445,7 +453,7 @@ mqtt_subRealloc(mqtt_subscr_t ** __restrict subs, u_sh * @src = source subscription * return: =NULL error or !=NULL successful copied a structure */ -inline mqtt_subscr_t * +mqtt_subscr_t * mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subscr_t * __restrict src) { if (!dst || !src) @@ -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; }