File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / Attic / aitmqtt.c
Revision 1.1.1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Nov 7 13:34:06 2011 UTC (13 years, 2 months ago) by misho
Branches: mqtt1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
commit

#include "global.h"


static int mqtt_Errno;
static char mqtt_Error[STRSIZ];


//
// Error maintenance functions ...
//

// mqtt_GetErrno() Get error code of last operation
inline int
mqtt_GetErrno()
{
	return mqtt_Errno;
}

// mqtt_GetError() Get error text of last operation
inline const char *
mqtt_GetError()
{
	return mqtt_Error;
}

// mqtt_SetErr() Set error to variables for internal use!!!
inline void
mqtt_SetErr(int eno, char *estr, ...)
{
	va_list lst;

	mqtt_Errno = eno;
	memset(mqtt_Error, 0, sizeof mqtt_Error);
	va_start(lst, estr);
	vsnprintf(mqtt_Error, sizeof mqtt_Error, estr, lst);
	va_end(lst);
}

// ----------------------------------------------------------

/*
 * 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
 * return: -1 error, >-1 length of message
 */
inline u_int
mqtt_decodeLen(u_int len)
{
	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;
	}

	return ret;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>