version 1.1, 2011/11/07 08:47:16
|
version 1.1.1.1.2.5, 2011/11/18 17:23:59
|
Line 1
|
Line 1
|
#include "global.h" |
#include "global.h" |
|
|
|
|
static int mqtt_Errno; | #pragma GCC visibility push(hidden) |
static char mqtt_Error[STRSIZ]; | |
|
|
|
int mqtt_Errno; |
|
char mqtt_Error[STRSIZ]; |
|
|
|
#pragma GCC visibility pop |
|
|
// |
// |
// Error maintenance functions ... |
// Error maintenance functions ... |
// |
// |
Line 38 mqtt_SetErr(int eno, char *estr, ...)
|
Line 41 mqtt_SetErr(int eno, char *estr, ...)
|
|
|
// ---------------------------------------------------------- |
// ---------------------------------------------------------- |
|
|
|
/* |
|
* mqtt_msgFree() Free MQTT message |
|
* @msg = Message buffer |
|
* @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc() |
|
* return: none |
|
*/ |
|
inline void |
|
mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) |
|
{ |
|
if (msg && *msg) { |
|
if ((*msg)->msg_base) { |
|
free((*msg)->msg_base); |
|
(*msg)->msg_base = NULL; |
|
} |
|
if (all) { |
|
free(*msg); |
|
*msg = NULL; |
|
} else |
|
(*msg)->msg_len ^= (*msg)->msg_len; |
|
} |
|
} |
|
|
|
/* |
|
* mqtt_msgAlloc() Allocate memory for MQTT Message |
|
* @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 *m = NULL; |
|
|
|
m = malloc(sizeof(mqtt_msg_t)); |
|
if (!m) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
memset(m, 0, sizeof(mqtt_msg_t)); |
|
|
|
if (len) { |
|
m->msg_len = len; |
|
m->msg_base = malloc(m->msg_len); |
|
if (!m->msg_base) { |
|
LOGERR; |
|
free(m); |
|
return NULL; |
|
} else |
|
memset(m->msg_base, 0, m->msg_len); |
|
} |
|
|
|
return m; |
|
} |
|
|
|
/* |
|
* mqtt_msgRealloc() Reallocate MQTT message buffer |
|
* @msg = MQTT message |
|
* @len = new length |
|
* return: -1 error or >-1 old buffer length |
|
*/ |
|
inline int |
|
mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len) |
|
{ |
|
void *p = NULL; |
|
int ret = 0; |
|
|
|
if (!msg) |
|
return -1; |
|
|
|
if (len == msg->msg_len) |
|
return len; |
|
|
|
p = realloc(msg->msg_base, len); |
|
if (!p) { |
|
LOGERR; |
|
return -1; |
|
} |
|
|
|
ret = msg->msg_len; |
|
msg->msg_len = len; |
|
msg->msg_base = p; |
|
|
|
return ret; |
|
} |
|
|
|
/* |
|
* mqtt_encodeLen() Encode number to MQTT length field |
|
* @num = number for encode |
|
* return: -1 error or >-1 length |
|
*/ |
|
inline u_int |
|
mqtt_encodeLen(u_int num) |
|
{ |
|
register u_int dig, i; |
|
u_int ret = 0; |
|
|
|
if (num > 268435455) |
|
return (u_int) -1; |
|
|
|
for (i = 0; i < sizeof ret && num > 0; i++) { |
|
dig = num % 0x80; |
|
num /= 0x80; |
|
if (num > 0) |
|
dig |= 0x80; |
|
|
|
*((u_char*) &ret + i) = (u_char) dig; |
|
} |
|
|
|
return ret; |
|
} |
|
|
|
/* |
|
* mqtt_decodeLen() Decode length from MQTT packet |
|
* @len = length |
|
* @n = sizeof bytes, if !=NULL |
|
* return: -1 error, >-1 length of message |
|
*/ |
|
inline u_int |
|
mqtt_decodeLen(u_int len, char *n) |
|
{ |
|
register u_int i, dig, mul; |
|
u_int ret = 0; |
|
u_char *p = (u_char*) &len; |
|
|
|
if (len > 0xffffff7f) |
|
return (u_int) -1; |
|
|
|
for (mul = 1, i = 0; i < sizeof ret; i++, mul *= 0x80) { |
|
dig = p[i]; |
|
ret += (dig & 0x7f) * mul; |
|
|
|
if (!(dig & 0x80)) |
|
break; |
|
} |
|
|
|
if (n) |
|
*n = (char) (i & 0x7f) + 1; |
|
return ret; |
|
} |
|
|
|
/* |
|
* mqtt_sizeLen Return sizeof len field |
|
* @len = length |
|
* return: -1 error, >-1 sizeof len in bytes |
|
*/ |
|
inline char |
|
mqtt_sizeLen(u_int len) |
|
{ |
|
register char i; |
|
u_char *p = (u_char*) &len; |
|
|
|
if (len > 0xffffff7f) |
|
return -1; |
|
|
|
for (i = 0; i < sizeof len; i++) |
|
if (!(*(p + i) & 0x80)) |
|
break; |
|
|
|
return ++i; |
|
} |
|
|
|
/* |
|
* mqtt_str2var Create MQTT variable from string |
|
* @csStr = string |
|
* @strLen = string length |
|
* return: NULL error or != ok variable, must be free after use! |
|
*/ |
|
inline mqtthdr_var_t * |
|
mqtt_str2var(const u_char *csStr, u_short strLen) |
|
{ |
|
mqtthdr_var_t *v; |
|
|
|
if (!csStr) |
|
return NULL; |
|
if (!(v = malloc(strLen + sizeof(mqtthdr_var_t)))) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
memset(v, 0, strLen + sizeof(mqtthdr_var_t)); |
|
|
|
memcpy(v->var_data, csStr, strLen); |
|
v->var_sb.val = htons(strLen); |
|
|
|
return v; |
|
} |