File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / vars.c
Revision 1.10.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Jan 24 17:21:15 2022 UTC (2 years, 4 months ago) by misho
Branches: elwix5_2
Diff to: branchpoint 1.10: preferred, unified
remove float like autodetection type from ait_array2vars

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: vars.c,v 1.10.2.1 2022/01/24 17:21:15 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2022
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: static inline int
   50: vars2buffer(u_char * __restrict buf, int buflen, int be, array_t * __restrict vars)
   51: {
   52: 	int Limit = 0;
   53: 	register int i;
   54: 	ait_val_t *v, *val;
   55: 	u_char *dat;
   56: 
   57: 	assert(buf);
   58: 	assert(vars);
   59: 	if (!buf || !vars)
   60: 		return -1;
   61: 	if (!buflen || !array_Size(vars))
   62: 		return 0;
   63: 	be = !!be;
   64: 
   65: 	Limit = sizeof(ait_val_t) * array_Size(vars);
   66: 	if (Limit > buflen) {
   67: 		elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", 
   68: 				buflen, Limit);
   69: 		return -1;
   70: 	} else {
   71: 		memset(buf, 0, buflen);
   72: 
   73: 		v = (ait_val_t*) buf;
   74: 		dat = buf + Limit;
   75: 	}
   76: 
   77: 	/* marshaling */
   78: 	for (i = 0; i < array_Size(vars); i++) {
   79: 		val = array(vars, i, ait_val_t*);
   80: 
   81: 		v[i].val_type = val->val_type;
   82: 		AIT_IN(&v[i]) = 1;
   83: 		AIT_BE(&v[i]) = be;
   84: 		AIT_LE(&v[i]) = !be;
   85: 		if (AIT_BE(&v[i])) {
   86: 			AIT_KEY(&v[i]) = htobe16(AIT_KEY(val));
   87: 			AIT_LEN(&v[i]) = htobe32(AIT_LEN(val));
   88: 		}
   89: 		if (AIT_LE(&v[i])) {
   90: 			AIT_KEY(&v[i]) = htole16(AIT_KEY(val));
   91: 			AIT_LEN(&v[i]) = htole32(AIT_LEN(val));
   92: 		}
   93: 
   94: 		switch (AIT_TYPE(val)) {
   95: 			case blob:
   96: 			case f32:
   97: 			case f64:
   98: 			case i8:
   99: 			case i16:
  100: 			case i32:
  101: 			case i64:
  102: 			case u8:
  103: 			case u16:
  104: 			case u32:
  105: 			case u64:
  106: 				if (AIT_BE(&v[i]))
  107: 					v[i].val.net = htobe64(val->val.net);
  108: 				if (AIT_LE(&v[i]))
  109: 					v[i].val.net = htole64(val->val.net);
  110: 				break;
  111: 			case data:
  112: 				if (AIT_LEN(val) > buflen - Limit) {
  113: 					elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d "
  114: 							"needed min %d", buflen, Limit + AIT_LEN(val));
  115: 					return -1;
  116: 				} else
  117: 					Limit += AIT_LEN(val);
  118: 
  119: 				memcpy(dat, val->val_data, AIT_LEN(val));
  120: 				/* Debug:: data offset in packet, not matter for anything! */
  121: 				v[i].val.net = dat - buf;
  122: 				dat += AIT_LEN(val);
  123: 				break;
  124: 			case buffer:
  125: 			case string:
  126: 			case ptr:
  127: 				if (AIT_LEN(val) > buflen - Limit) {
  128: 					elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d "
  129: 							"needed min %d", buflen, Limit + AIT_LEN(val));
  130: 					return -1;
  131: 				} else
  132: 					Limit += AIT_LEN(val);
  133: 
  134: 				memcpy(dat, val->val.buffer, AIT_LEN(val));
  135: 				/* Debug:: data offset in packet, not matter for anything! */
  136: 				v[i].val.net = dat - buf;
  137: 				dat += AIT_LEN(val);
  138: 				break;
  139: 			default:
  140: 				elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", 
  141: 						AIT_TYPE(val), i);
  142: 				return -1;
  143: 		}
  144: 	}
  145: 
  146: 	return Limit;
  147: }
  148: 
  149: static inline array_t *
  150: buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy)
  151: {
  152: 	array_t *vars;
  153: 	int Limit = 0;
  154: 	register int i;
  155: 	ait_val_t *v, *val;
  156: 	u_char *dat;
  157: 
  158: 	assert(buf);
  159: 	if (!buf || !buflen || !vnum)
  160: 		return NULL;
  161: 
  162: 	Limit = sizeof(ait_val_t) * vnum;
  163: 	if (Limit > buflen) {
  164: 		elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", 
  165: 				buflen, Limit);
  166: 		return NULL;
  167: 	} else {
  168: 		if (!(vars = array_Init(vnum)))
  169: 			return NULL;
  170: 
  171: 		v = (ait_val_t*) buf;
  172: 		dat = buf + Limit;
  173: 	}
  174: 
  175: 	/* de-marshaling */
  176: 	for (i = 0; i < array_Size(vars); i++) {
  177: 		if (!zcpy) {
  178: 			val = e_malloc(sizeof(ait_val_t));
  179: 			if (!val) {
  180: 				if (!zcpy)
  181: 					array_Free(vars);
  182: 				array_Destroy(&vars);
  183: 				return NULL;
  184: 			}
  185: 			AIT_IN(val) = 0;
  186: 		} else {
  187: 			val = v + i;
  188: 			AIT_IN(val) = 1;
  189: 		}
  190: 		array_Set(vars, i, val);
  191: 
  192: 		val->val_type = v[i].val_type;
  193: 		AIT_BE(val) = AIT_BE(&v[i]);
  194: 		AIT_LE(val) = AIT_LE(&v[i]);
  195: 		if (AIT_BE(val)) {
  196: 			AIT_LEN(val) = be32toh(AIT_LEN(&v[i]));
  197: 			AIT_KEY(val) = be16toh(AIT_KEY(&v[i]));
  198: 		}
  199: 		if (AIT_LE(val)) {
  200: 			AIT_LEN(val) = le32toh(AIT_LEN(&v[i]));
  201: 			AIT_KEY(val) = le16toh(AIT_KEY(&v[i]));
  202: 		}
  203: 
  204: 		switch (AIT_TYPE(val)) {
  205: 			case blob:
  206: 			case f32:
  207: 			case f64:
  208: 			case i8:
  209: 			case i16:
  210: 			case i32:
  211: 			case i64:
  212: 			case u8:
  213: 			case u16:
  214: 			case u32:
  215: 			case u64:
  216: 				if (AIT_BE(val))
  217: 					val->val.net = be64toh(v[i].val.net);
  218: 				if (AIT_LE(val))
  219: 					val->val.net = le64toh(v[i].val.net);
  220: 				break;
  221: 			case data:
  222: 			case ptr:
  223: 				/* WARNING:: remap data and ptr type to buffer! */
  224: 				val->val_type = buffer;
  225: 			case buffer:
  226: 			case string:
  227: 				if (!zcpy) {
  228: 					val->val.buffer = e_malloc(AIT_LEN(val));
  229: 					if (!val->val.buffer) {
  230: 						array_Free(vars);
  231: 						array_Destroy(&vars);
  232: 						return NULL;
  233: 					} else
  234: 						memcpy(val->val.buffer, dat, AIT_LEN(val));
  235: 				} else
  236: 					val->val.buffer = dat;
  237: 				dat += AIT_LEN(val);
  238: 				break;
  239: 			default:
  240: 				elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", 
  241: 						AIT_TYPE(val), i);
  242: 				if (!zcpy)
  243: 					array_Free(vars);
  244: 				array_Destroy(&vars);
  245: 				return NULL;
  246: 		}
  247: 	}
  248: 
  249: 	return vars;
  250: }
  251: 
  252: /*
  253:  * ait_vars2tlv() - Marshaling data from array with variables to TLV buffer
  254:  *
  255:  * @buf = Buffer, If =NULL then we return only needed buffer size
  256:  * @buflen = Size of buffer
  257:  * @vars = Variable array
  258:  * return: -1 error, 0 nothing done or >0 size of marshaled data
  259:  */
  260: int
  261: ait_vars2tlv(u_char * __restrict buf, int buflen, array_t * __restrict vars)
  262: {
  263: 	int Limit = 0;
  264: 	register int i;
  265: 	ait_val_t *val;
  266: 	u_char *dat;
  267: 
  268: 	assert(vars);
  269: 	if (!vars)
  270: 		return -1;
  271: 	if (!array_Size(vars))
  272: 		return 0;
  273: 
  274: 	/* calculate amount of data into buffer */
  275: 	for (i = 0, Limit = 0; i < array_Size(vars); 
  276: 			Limit += 5 + AIT_LEN(array(vars, i, ait_val_t*)), i++);
  277: 	/* check only needed buffer size */
  278: 	if (!buf)
  279: 		return Limit;
  280: 
  281: 	if (Limit > buflen) {
  282: 		elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", 
  283: 				buflen, Limit);
  284: 		return -1;
  285: 	} else
  286: 		memset(buf, 0, buflen);
  287: 
  288: 	/* marshaling */
  289: 	for (i = 0, dat = buf; i < array_Size(vars) && dat < (buf + Limit); i++) {
  290: 		val = array(vars, i, ait_val_t*);
  291: 
  292: 		*dat++ = AIT_TYPE(val); 
  293: 
  294: 		*((uint32_t*) dat) = htonl(AIT_LEN(val));
  295: 		dat += sizeof(uint32_t);
  296: 
  297: 		switch (AIT_TYPE(val)) {
  298: 			case i8:
  299: 				*((int8_t*) dat) = AIT_GET_I8(val);
  300: 				break;
  301: 			case u8:
  302: 				*((uint8_t*) dat) = AIT_GET_U8(val);
  303: 				break;
  304: 			case i16:
  305: 				*((int16_t*) dat) = AIT_GET_I16(val);
  306: 				break;
  307: 			case u16:
  308: 				*((uint16_t*) dat) = AIT_GET_U16(val);
  309: 				break;
  310: 			case i32:
  311: 				*((int32_t*) dat) = AIT_GET_I32(val);
  312: 				break;
  313: 			case blob:
  314: 			case u32:
  315: 				*((uint32_t*) dat) = AIT_GET_U32(val);
  316: 				break;
  317: 			case i64:
  318: 				*((int64_t*) dat) = AIT_GET_I64(val);
  319: 				break;
  320: 			case u64:
  321: 				*((uint64_t*) dat) = AIT_GET_U64(val);
  322: 				break;
  323: 			case f32:
  324: 				*((float*) dat) = AIT_GET_F32(val);
  325: 				break;
  326: 			case f64:
  327: 				*((double*) dat) = AIT_GET_F64(val);
  328: 				break;
  329: 			case data:
  330: 				memcpy(dat, AIT_GET_DATA(val), AIT_LEN(val));
  331: 				break;
  332: 			case buffer:
  333: 				memcpy(dat, AIT_GET_BUF(val), AIT_LEN(val));
  334: 				break;
  335: 			case string:
  336: 				memcpy(dat, AIT_GET_STR(val), AIT_LEN(val));
  337: 				break;
  338: 			case ptr:
  339: 				memcpy(dat, AIT_GET_PTR(val), AIT_LEN(val));
  340: 				break;
  341: 			default:
  342: 				elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", 
  343: 						AIT_TYPE(val), i);
  344: 				return -1;
  345: 		}
  346: 		dat += AIT_LEN(val);
  347: 	}
  348: 
  349: 	return Limit;
  350: }
  351: 
  352: /*
  353:  * ait_tlv2vars() - De-marshaling data from TLV buffer to array with variables
  354:  *
  355:  * @buf = Buffer
  356:  * @buflen = Size of buffer
  357:  * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVars()
  358:  */
  359: array_t *
  360: ait_tlv2vars(u_char * __restrict buf, int buflen)
  361: {
  362: 	array_t *vars;
  363: 	register int i;
  364: 	ait_val_t *val;
  365: 	u_char *dat;
  366: 
  367: 	assert(buf);
  368: 	if (!buf || !buflen)
  369: 		return NULL;
  370: 
  371: 	if (!(vars = ait_allocVars(1)))
  372: 		return NULL;
  373: 
  374: 	/* de-marshaling */
  375: 	for (i = 0, dat = buf; *dat != empty && dat < (buf + buflen); i++) {
  376: 		val = ait_getVars(&vars, i);
  377: 		if (!val) {
  378: 			ait_freeVars(&vars);
  379: 			return NULL;
  380: 		}
  381: 
  382: 		val->val_type = *dat++;
  383: 		AIT_LEN(val) = ntohl(*((uint32_t*) dat));
  384: 		dat += sizeof(uint32_t);
  385: 		
  386: 		switch (AIT_TYPE(val)) {
  387: 			case f32:
  388: 				AIT_SET_F32(val, *((float*) dat));
  389: 				break;
  390: 			case f64:
  391: 				AIT_SET_F64(val, *((double*) dat));
  392: 				break;
  393: 			case i8:
  394: 				AIT_SET_I8(val, *((int8_t*) dat));
  395: 				break;
  396: 			case i16:
  397: 				AIT_SET_I16(val, *((int16_t*) dat));
  398: 				break;
  399: 			case i32:
  400: 				AIT_SET_I32(val, *((int32_t*) dat));
  401: 				break;
  402: 			case i64:
  403: 				AIT_SET_I64(val, *((int64_t*) dat));
  404: 				break;
  405: 			case u8:
  406: 				AIT_SET_U8(val, *((uint8_t*) dat));
  407: 				break;
  408: 			case u16:
  409: 				AIT_SET_U16(val, *((uint16_t*) dat));
  410: 				break;
  411: 			case blob:
  412: 			case u32:
  413: 				AIT_SET_U32(val, *((uint32_t*) dat));
  414: 				break;
  415: 			case u64:
  416: 				AIT_SET_U64(val, *((uint64_t*) dat));
  417: 				break;
  418: 			case data:
  419: 				AIT_SET_DATA(val, dat, AIT_LEN(val));
  420: 				break;
  421: 			case ptr:
  422: 			case buffer:
  423: 				AIT_SET_BUF(val, dat, AIT_LEN(val));
  424: 				break;
  425: 			case string:
  426: 				AIT_SET_STR(val, (char*) dat);
  427: 				break;
  428: 			default:
  429: 				elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", 
  430: 						AIT_TYPE(val), i);
  431: 				ait_freeVars(&vars);
  432: 				return NULL;
  433: 		}
  434: 		dat += AIT_LEN(val);
  435: 	}
  436: 
  437: 	return vars;
  438: }
  439: 
  440: /* buffer marshaling with swapping bytes to network order */
  441: 
  442: /*
  443:  * ait_vars2buffer() - Marshaling data from array with variables to buffer
  444:  *
  445:  * @buf = Buffer
  446:  * @buflen = Size of buffer
  447:  * @vars = Variable array
  448:  * return: -1 error, 0 nothing done or >0 size of marshaled data
  449:  */
  450: int
  451: ait_vars2buffer(u_char * __restrict buf, int buflen, array_t * __restrict vars)
  452: {
  453: 	return vars2buffer(buf, buflen, 42, vars);
  454: }
  455: 
  456: /*
  457:  * ait_buffer2vars() - De-marshaling data from buffer to array with variables
  458:  *
  459:  * @buf = Buffer
  460:  * @buflen = Size of buffer
  461:  * @vnum = Number of variables into buffer
  462:  * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and 
  463:  		*DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
  464:  * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
  465:  */
  466: array_t *
  467: ait_buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy)
  468: {
  469: 	return buffer2vars(buf, buflen, vnum, zcpy);
  470: }
  471: 
  472: /* buffer marshaling without swapping bytes to network order */
  473: 
  474: /*
  475:  * ait_vars2map() - Marshaling data from array with variables to memory map
  476:  *
  477:  * @buf = Buffer
  478:  * @buflen = Size of buffer
  479:  * @vars = Variable array
  480:  * return: -1 error, 0 nothing done or >0 size of marshaled data
  481:  */
  482: int
  483: ait_vars2map(u_char *buf, int buflen, array_t *vars)
  484: {
  485: 	return vars2buffer(buf, buflen, 0, vars);
  486: }
  487: 
  488: /*
  489:  * ait_map2vars() - De-marshaling data from memory map to array with variables
  490:  *
  491:  * @buf = Buffer
  492:  * @buflen = Size of buffer
  493:  * @vnum = Number of variables into buffer
  494:  * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and 
  495:  		*DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
  496:  * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
  497:  */
  498: array_t *
  499: ait_map2vars(u_char *buf, int buflen, int vnum, int zcpy)
  500: {
  501: 	return buffer2vars(buf, buflen, vnum, zcpy);
  502: }
  503: 
  504: /*
  505:  * ait_array2vars() - Build array with variables from Null Terminated String Array
  506:  *
  507:  * @args = Null-terminated array with strings
  508:  * @dn = Convert numbers from strings to numbers into variables
  509:  * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVars()
  510:  */
  511: array_t *
  512: ait_array2vars(const char **args, int dn)
  513: {
  514: 	array_t *vars;
  515: 	ait_val_t *val;
  516: 	register int i;
  517: 	long n;
  518: 	double d;
  519: 	char *str;
  520: 
  521: 	if (!args)
  522: 		return NULL;
  523: 
  524: 	vars = ait_allocVars(0);
  525: 	if (!vars)
  526: 		return NULL;
  527: 
  528: 	for (i = 0; *args; i++, args++) {
  529: 		val = ait_getVars(&vars, i);
  530: 		if (!val) {
  531: 			ait_freeVars(&vars);
  532: 			return NULL;
  533: 		}
  534: 
  535: 		if (dn) {
  536: 			n = strtol(*args, &str, 0);
  537: 			if (!str || !*str) {
  538: 				AIT_SET_I64(val, (int64_t) n);
  539: 				continue;
  540: 			}
  541: 			d = strtod(*args, &str);
  542: 			if (!str || !*str) {
  543: 				AIT_SET_F64(val, d);
  544: 				continue;
  545: 			}
  546: 			AIT_SET_STR(val, *args);
  547: 		} else
  548: 			AIT_SET_STR(val, *args);
  549: 	}
  550: 
  551: 	return vars;
  552: }
  553: 
  554: /* variables array */
  555: 
  556: /*
  557:  * ait_allocVars() - Allocate ait_val_t array
  558:  *
  559:  * @varnum = Number of variables
  560:  * return: =NULL error or !=NULL allocated array
  561:  */
  562: array_t *
  563: ait_allocVars(int varnum)
  564: {
  565: 	array_t *arr;
  566: 	register int i;
  567: 	ait_val_t *v;
  568: 
  569: 	if (!(arr = array_Init(varnum)))
  570: 		return NULL;
  571: 
  572: 	for (i = 0; i < array_Size(arr); i++) {
  573: 		if (!(v = ait_allocVar())) {
  574: 			ait_freeVars(&arr);
  575: 			return NULL;
  576: 		} else
  577: 			array_Set(arr, i, v);
  578: 	}
  579: 
  580: 	return arr;
  581: }
  582: 
  583: /*
  584:  * ait_getVars() - Get ait_val_t element from array and if not exists allocate it
  585:  *
  586:  * @vars = Variable array
  587:  * @n = index of variable into array
  588:  * return: NULL error or !=NULL ait_val_t element
  589:  */
  590: ait_val_t *
  591: ait_getVars(array_t ** __restrict vars, int n)
  592: {
  593: 	register int i;
  594: 	ait_val_t *v;
  595: 
  596: 	if (!vars)
  597: 		return NULL;
  598: 
  599: 	if (!*vars) {
  600: 		if (!(*vars = ait_allocVars(n + 1)))
  601: 			return NULL;
  602: 	} else if (n >= (i = array_Size(*vars))) {
  603: 		if (array_Grow(*vars, n + 1, 0))
  604: 			return NULL;
  605: 		for (; i < array_Size(*vars); i++)
  606: 			if (!array_Get(*vars, i)) {
  607: 				if (!(v = ait_allocVar()))
  608: 					return NULL;
  609: 				else
  610: 					array_Set(*vars, i, v);
  611: 			}
  612: 	}
  613: 
  614: 	return array(*vars, n, ait_val_t*);
  615: }
  616: 
  617: /*
  618:  * ait_clrVars() - Clear ait_val_t elements from array
  619:  *
  620:  * @vars = Variable array
  621:  * return: -1 error or size of array
  622:  */
  623: int
  624: ait_clrVars(array_t * __restrict vars)
  625: {
  626: 	register int i;
  627: 	ait_val_t *v;
  628: 
  629: 	if (!vars)
  630: 		return -1;
  631: 
  632: 	for (i = 0; i < array_Size(vars); i++)
  633: 		if ((v = array(vars, i, ait_val_t*)))
  634: 			AIT_FREE_VAL(v);
  635: 
  636: 	return array_Size(vars);
  637: }
  638: 
  639: /*
  640:  * ait_freeVars() - Free ait_val_t array
  641:  *
  642:  * @vars = Variable array
  643:  * return: none
  644:  */
  645: void
  646: ait_freeVars(array_t ** __restrict vars)
  647: {
  648: 	register int i;
  649: 	ait_val_t *v;
  650: 
  651: 	if (!vars || !*vars)
  652: 		return;
  653: 
  654: 	for (i = 0; i < array_Size(*vars); i++)
  655: 		if ((v = array(*vars, i, ait_val_t*))) {
  656: 			/* free memory if isn't zero copy */
  657: 			if (!AIT_IN(v)) {
  658: 				AIT_FREE_VAL(v);
  659: 				if ((*vars)->arr_data[i])
  660: 					e_free((*vars)->arr_data[i]);
  661: 			} else
  662: 				AIT_FREE_VAL(v);
  663: 			(*vars)->arr_data[i] = NULL;
  664: 		}
  665: 	(*vars)->arr_last = -1;
  666: 
  667: 	array_Destroy(vars);
  668: }
  669: 
  670: /*
  671:  * ait_resideVars() - Calculate footprint of resided variables into array
  672:  *
  673:  * @vars = Variable array
  674:  * return: bytes for whole array
  675:  */
  676: size_t
  677: ait_resideVars(array_t * __restrict vars)
  678: {
  679: 	size_t ret = 0;
  680: 	register int i;
  681: 
  682: 	if (vars) {
  683: 		ret = array_Size(vars) * sizeof(ait_val_t);
  684: 		for (i = 0; i < array_Size(vars); i++)
  685: 			switch (AIT_TYPE(array(vars, i, ait_val_t*))) {
  686: 				case buffer:
  687: 				case string:
  688: 				case data:
  689: 				case ptr:
  690: 					ret += AIT_LEN(array(vars, i, ait_val_t*));
  691: 					break;
  692: 				default:
  693: 					break;
  694: 			}
  695: 	}
  696: 
  697: 	return ret;
  698: }
  699: 
  700: 
  701: /*
  702:  * ait_allocVar() - Allocate memory for variable
  703:  *
  704:  * return: NULL error or new variable, after use free variable with ait_freeVar()
  705:  */
  706: ait_val_t *
  707: ait_allocVar(void)
  708: {
  709: 	ait_val_t *v = NULL;
  710: 
  711: 	v = e_malloc(sizeof(ait_val_t));
  712: 	if (!v)
  713: 		return NULL;
  714: 	else
  715: 		memset(v, 0, sizeof(ait_val_t));
  716: 	v->val_type = empty;
  717: 
  718: 	return v;
  719: }
  720: 
  721: /*
  722:  * ait_freeVar() - Free allocated memory for variable
  723:  *
  724:  * @val = Variable
  725:  * return: none
  726:  */
  727: void
  728: ait_freeVar(ait_val_t ** __restrict val)
  729: {
  730: 	if (val && *val) {
  731: 		AIT_FREE_VAL(*val);
  732: 		e_free(*val);
  733: 		*val = NULL;
  734: 	}
  735: }
  736: 
  737: /*
  738:  * ait_makeVar() - Allocate memory and fill variable
  739:  *
  740:  * @type = type of variable
  741:  * @... = arg1 is value of variable
  742:  * @... = arg2 is length of variabla. Not required for numbers and strings!
  743:  * return: NULL error or new variable, after use free variable with io_freeVar()
  744:  */
  745: ait_val_t *
  746: ait_makeVar(ait_type_t type, ...)
  747: {
  748: 	ait_val_t *v = NULL;
  749: 	va_list lst;
  750: 	void *p = NULL;
  751: 	uint32_t len = 0;
  752: 	uint64_t n = 0LL;
  753: 
  754: 	v = ait_allocVar();
  755: 	if (!v)
  756: 		return NULL;
  757: 
  758: 	va_start(lst, type);
  759: 	switch (type) {
  760: 		case empty:
  761: 			v->val_type = (uint8_t) empty;
  762: 			break;
  763: 		case ptr:
  764: 			p = va_arg(lst, void*);
  765: 			len = va_arg(lst, uint32_t);
  766: 			AIT_SET_PTR(v, p, len);
  767: 			break;
  768: 		case data:
  769: 			p = va_arg(lst, void*);
  770: 			len = va_arg(lst, uint32_t);
  771: 			AIT_SET_DATA(v, p, len);
  772: 			break;
  773: 		case buffer:
  774: 			p = va_arg(lst, void*);
  775: 			len = va_arg(lst, uint32_t);
  776: 			AIT_SET_BUF(v, p, len);
  777: 			break;
  778: 		case string:
  779: 			p = va_arg(lst, char*);
  780: 			AIT_SET_STR(v, (char*) p);
  781: 			break;
  782: 		case blob:
  783: 			n = va_arg(lst, uint32_t);
  784: 			len = va_arg(lst, uint32_t);
  785: 			AIT_SET_BLOB(v, n, len);
  786: 			break;
  787: 		case f32:
  788: 			AIT_SET_F32(v, (float) va_arg(lst, double));
  789: 			break;
  790: 		case f64:
  791: 			AIT_SET_F64(v, va_arg(lst, double));
  792: 			break;
  793: 		case u8:
  794: 			AIT_SET_U8(v, (uint8_t) va_arg(lst, int));
  795: 			break;
  796: 		case u16:
  797: 			AIT_SET_U16(v, (uint16_t) va_arg(lst, int));
  798: 			break;
  799: 		case u32:
  800: 			AIT_SET_U32(v, va_arg(lst, uint32_t));
  801: 			break;
  802: 		case u64:
  803: 			AIT_SET_U64(v, va_arg(lst, uint64_t));
  804: 			break;
  805: 		case i8:
  806: 			AIT_SET_I8(v, (int8_t) va_arg(lst, int));
  807: 			break;
  808: 		case i16:
  809: 			AIT_SET_I16(v, (int16_t) va_arg(lst, int));
  810: 			break;
  811: 		case i32:
  812: 			AIT_SET_I32(v, va_arg(lst, int32_t));
  813: 			break;
  814: 		case i64:
  815: 			AIT_SET_I64(v, va_arg(lst, int64_t));
  816: 			break;
  817: 	}
  818: 	va_end(lst);
  819: 
  820: 	return v;
  821: }
  822: 
  823: static int
  824: _cmp_arr_key_asc(const void *a, const void *b)
  825: {
  826: 	return AIT_KEY(*(ait_val_t**) a) - AIT_KEY(*(ait_val_t**) b);
  827: }
  828: 
  829: static int
  830: _cmp_arr_key_desc(const void *a, const void *b)
  831: {
  832: 	return AIT_KEY(*(ait_val_t**) b) - AIT_KEY(*(ait_val_t**) a);
  833: }
  834: 
  835: static int
  836: _cmp_arr_val_asc(const void *a, const void *b)
  837: {
  838: 	return AIT_RAW(*(ait_val_t**) a) - AIT_RAW(*(ait_val_t**) b);
  839: }
  840: 
  841: static int
  842: _cmp_arr_val_desc(const void *a, const void *b)
  843: {
  844: 	return AIT_RAW(*(ait_val_t**) b) - AIT_RAW(*(ait_val_t**) a);
  845: }
  846: 
  847: /*
  848:  * ait_sortVarsByVal() - Sorting array with variables by value
  849:  *
  850:  * @vars = Variable array
  851:  * @order = Sort order. If =0 ascend or !=0 descend
  852:  * @cmp = Custom compare function for sorting. If =NULL compare by value
  853:  * return: none
  854:  */
  855: void
  856: ait_sortVarsByVal(array_t * __restrict vars, int order,  int (*cmp)(const void*, const void*))
  857: {
  858: 	if (!vars)
  859: 		return;
  860: 
  861: 	if (cmp)
  862: 		qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), cmp);
  863: 	else if (order)
  864: 		qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_desc);
  865: 	else
  866: 		qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_asc);
  867: }
  868: 
  869: /*
  870:  * ait_sortVarsByKey() - Sorting array with variables by key
  871:  *
  872:  * @vars = Variable array
  873:  * @order = Sort order. If =0 ascend or !=0 descend
  874:  * return: none
  875:  */
  876: void
  877: ait_sortVarsByKey(array_t * __restrict vars, int order)
  878: {
  879: 	if (!vars)
  880: 		return;
  881: 
  882: 	if (order)
  883: 		qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_desc);
  884: 	else
  885: 		qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
  886: }
  887: 
  888: /*
  889:  * ait_findKeyVars() - Find variable by key from array
  890:  *
  891:  * @vars = Variables
  892:  * @key = Search key
  893:  * return: NULL error or not found, !=NULL valid element
  894:  */
  895: ait_val_t *
  896: ait_findKeyVars(array_t * __restrict vars, u_short key)
  897: {
  898: 	array_t *tmp;
  899: 	ait_val_t **vv, *v = NULL;
  900: 	register int i;
  901: 	const u_char *p;
  902: 
  903: 	if (!vars)
  904: 		return NULL;
  905: 
  906: 	if (array_Copy(&tmp, vars) == -1)
  907: 		return NULL;
  908: 	else
  909: 		qsort(tmp->arr_data, tmp->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
  910: 
  911: 	/* binary search */
  912: 	for (p = (const u_char*) tmp->arr_data, i = array_Size(tmp); i; i >>= 1) {
  913: 		vv = (ait_val_t**) (p + (i >> 1) * sizeof(uintptr_t));
  914: 		if (!(key - AIT_KEY(*vv))) {	/* found! */
  915: 			v = *vv;
  916: 			break;
  917: 		}
  918: 		if ((key - AIT_KEY(*vv)) > 0) {	/* move right key > current */
  919: 			p = (const u_char*) vv + sizeof(uintptr_t);
  920: 			i--;
  921: 		}				/* else move left */
  922: 	}
  923: 
  924: 	array_Destroy(&tmp);
  925: 	return v;
  926: }
  927: 
  928: /*
  929:  * ait_hashVar() - Generate hash key for variable from string or value
  930:  *
  931:  * @v = variable
  932:  * @key = key string for hash, if =NULL hash will built from variable
  933:  * return: hash key
  934:  */
  935: u_short
  936: ait_hashVar(ait_val_t * __restrict v, const char * __restrict key)
  937: {
  938: 	void *p;
  939: 	u_short cksum;
  940: 	int l;
  941: 
  942: 	if (!v)
  943: 		return 0;
  944: 
  945: 	if (key) {
  946: 		p = (void*) key;
  947: 		l = (strlen(key) + 1) / 2;
  948: 	} else {
  949: 		switch (AIT_TYPE(v)) {
  950: 			case empty:
  951: 				AIT_KEY(v) = 0;
  952: 				return 0;
  953: 			case string:
  954: 			case buffer:
  955: 				p = AIT_ADDR(v);
  956: 				l = AIT_LEN(v) / 2;
  957: 				break;
  958: 			case data:
  959: 				p = v->val_data;
  960: 				l = AIT_LEN(v) / 2;
  961: 				break;
  962: 			default:
  963: 				p = &AIT_RAW(v);
  964: 				l = sizeof AIT_RAW(v) / 2;
  965: 				break;
  966: 		}
  967: 	}
  968: 
  969: 	cksum = crcFletcher16((u_short*) p, l);
  970: 
  971: 	if (AIT_BE(v))
  972: 		AIT_KEY(v) = htobe16(cksum);
  973: 	else if (AIT_LE(v))
  974: 		AIT_KEY(v) = htole16(cksum);
  975: 	else
  976: 		AIT_KEY(v) = cksum;
  977: 
  978: 	return AIT_KEY(v);
  979: }
  980: 
  981: /*
  982:  * ait_hashKeyVars() - Generate hash keys for variables
  983:  *
  984:  * @vars = Variables
  985:  * return -1 error or 0 ok
  986:  */
  987: int
  988: ait_hashKeyVars(array_t * __restrict vars)
  989: {
  990: 	register int i;
  991: 
  992: 	if (!vars)
  993: 		return -1;
  994: 
  995: 	for (i = 0; i < array_Size(vars); i++)
  996: 		ait_hashVar(array(vars, i, ait_val_t*), NULL);
  997: 
  998: 	return 0;
  999: }
 1000: 
 1001: /*
 1002:  * ait_findKeyHash() - Find variable by hash string from array
 1003:  *
 1004:  * @vars = Variables
 1005:  * @key = Search string
 1006:  * return: NULL error or not found, !=NULL valid element
 1007:  */
 1008: ait_val_t *
 1009: ait_findKeyHash(array_t * __restrict vars, const char * __restrict key)
 1010: {
 1011: 	u_short k = 0;
 1012: 
 1013: 	if (!vars || !key)
 1014: 		return NULL;
 1015: 
 1016: 	k = crcFletcher16((u_short*) key, (strlen(key) + 1) / 2);
 1017: 	return ait_findKeyVars(vars, k);
 1018: }
 1019: 
 1020: /*
 1021:  * ait_sprintfVar() - Builtin string variable from formatted input
 1022:  *
 1023:  * @v = variable
 1024:  * @fmt = format string
 1025:  * @... = argument(s)
 1026:  * return: -1 error or >0 copied bytes to variable
 1027:  */
 1028: int
 1029: ait_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...)
 1030: {
 1031: 	int ret = 0;
 1032: 	va_list lst;
 1033: 	char str[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
 1034: 
 1035: 	if (!v || !fmt)
 1036: 		return -1;
 1037: 
 1038: 	va_start(lst, fmt);
 1039: 	ret = vsnprintf(str, sizeof str - 1, fmt, lst);
 1040: 	va_end(lst);
 1041: 
 1042: 	if (ret > -1) {
 1043: 		AIT_FREE_VAL(v);
 1044: 		AIT_SET_STR(v, str);
 1045: 	} else
 1046: 		LOGERR;
 1047: 
 1048: 	return ret;
 1049: }
 1050: 
 1051: /*
 1052:  * ait_setlikeVar() - Set variable like ...
 1053:  *
 1054:  * @v = variable
 1055:  * @t = type of data
 1056:  * @l = length of data
 1057:  * @... = data
 1058:  * return: -1 error or 0 ok
 1059:  */
 1060: int
 1061: ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, u_int l, ...)
 1062: {
 1063: 	va_list lst;
 1064: 
 1065: 	if (!v)
 1066: 		return -1;
 1067: 
 1068: 	AIT_FREE_VAL(v);
 1069: 	AIT_INIT_VAL2(v, t);
 1070: 	AIT_LEN(v) = l;
 1071: 	AIT_IN(v) = 1;
 1072: 
 1073: 	va_start(lst, l);
 1074: 	switch (AIT_TYPE(v)) {
 1075: 		case ptr:
 1076: 		case buffer:
 1077: 		case string:
 1078: 			AIT_ADDR(v) = va_arg(lst, void*);
 1079: 			break;
 1080: 		default:
 1081: 			AIT_RAW(v) = va_arg(lst, uint64_t);
 1082: 			break;
 1083: 	}
 1084: 	va_end(lst);
 1085: 
 1086: 	return 0;
 1087: }
 1088: 
 1089: /*
 1090:  * ait_getlikeVar() - Get variable like ...
 1091:  *
 1092:  * @v = variable
 1093:  * return: return raw data
 1094:  */
 1095: uint64_t
 1096: ait_getlikeVar(ait_val_t * __restrict v)
 1097: {
 1098: 	if (!v)
 1099: 		return (uintptr_t) -1;
 1100: 
 1101: 	return AIT_RAW(v);
 1102: }
 1103: 
 1104: /*
 1105:  * ait_cmpVar() - Compare two variables
 1106:  *
 1107:  * @a = 1st variable
 1108:  * @b = 2nd variable
 1109:  * return: 0 is equal or !=0 is different
 1110:  */
 1111: int
 1112: ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b)
 1113: {
 1114: 	intptr_t ret;
 1115: 
 1116: 	if (!(ret = (a - b)))
 1117: 		return ret;
 1118: 	if ((ret = AIT_TYPE(a) - AIT_TYPE(b)))
 1119: 		return ret;
 1120: 	if ((ret = AIT_LEN(a) - AIT_LEN(b)))
 1121: 		return ret;
 1122: 
 1123: 	switch (AIT_TYPE(a)) {
 1124: 		case buffer:
 1125: 			ret = memcmp(AIT_GET_BUF(a), AIT_GET_BUF(b), AIT_LEN(a));
 1126: 			break;
 1127: 		case string:
 1128: 			ret = strncmp(AIT_GET_STR(a), AIT_GET_STR(b), AIT_LEN(a));
 1129: 			break;
 1130: 		case data:
 1131: 			ret = memcmp(AIT_GET_DATA(a), AIT_GET_DATA(b), AIT_LEN(a));
 1132: 			break;
 1133: 		case ptr:
 1134: 			ret = AIT_ADDR(a) - AIT_ADDR(b);
 1135: 			break;
 1136: 		default:
 1137: 			ret = AIT_RAW(a) - AIT_RAW(b);
 1138: 			break;
 1139: 	}
 1140: 
 1141: 	return (int) ret;
 1142: }

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