File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / vars.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Aug 31 12:29:32 2011 UTC (12 years, 10 months ago) by misho
Branches: io2_1
added many changes and features
- added macros for array
- reworked assert when destroy arrays
- added feature for free before shrink into io_arrayGrow
- add general feature for marshaling

    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.1.2.2 2011/08/31 12:29:32 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   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: /*
   50:  * io_vals2buffer() Marshaling data from array with variables to buffer
   51:  * @buf = Buffer
   52:  * @buflen = Size of buffer
   53:  * @vars = Variable array
   54:  * return: -1 error, 0 nothing done or >0 size of marshaled data
   55:  */
   56: int
   57: io_vals2buffer(u_char **buf, int buflen, array_t *vars)
   58: {
   59: 	int Limit = 0;
   60: 	register int i;
   61: 	ait_val_t *v, *val;
   62: 	u_char *data;
   63: 
   64: 	assert(buf);
   65: 	assert(vars);
   66: 	if (!buf || !vars || !*buf)
   67: 		return -1;
   68: 	if (!buflen || !io_arraySize(vars))
   69: 		return 0;
   70: 
   71: 	Limit = sizeof(ait_val_t) * io_arraySize(vars);
   72: 	if (Limit > buflen) {
   73: 		io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", 
   74: 				buflen, Limit);
   75: 		return -1;
   76: 	} else {
   77: 		memset(*buf, 0, buflen);
   78: 
   79: 		v = (ait_val_t*) *buf;
   80: 		data = *buf + Limit;
   81: 	}
   82: 
   83: 	/* marshaling */
   84: 	for (i = 0; i < io_arraySize(vars); i++) {
   85: 		val = io_array(vars, i, ait_val_t*);
   86: 
   87: 		v[i].val_type = val->val_type;
   88: 		AIT_LEN(&v[i]) = htobe32(AIT_LEN(val));
   89: 
   90: 		switch (AIT_TYPE(val)) {
   91: 			case blob:
   92: 			case f32:
   93: 			case f64:
   94: 			case i8:
   95: 			case i16:
   96: 			case i32:
   97: 			case i64:
   98: 			case u8:
   99: 			case u16:
  100: 			case u32:
  101: 			case u64:
  102: 				v[i].val.net = htobe64(val->val.net);
  103: 				break;
  104: 			case buffer:
  105: 			case string:
  106: 				if (AIT_LEN(val) > buflen - Limit) {
  107: 					io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d "
  108: 							"needed min %d ...\n", buflen, Limit + AIT_LEN(val));
  109: 					return -1;
  110: 				} else
  111: 					Limit += AIT_LEN(val);
  112: 
  113: 				memcpy(data, val->val.buffer, AIT_LEN(val));
  114: 				/* Debug:: data offset in packet, not matter for anything! */
  115: 				v[i].val.net = data - *buf;
  116: 				data += AIT_LEN(val);
  117: 				break;
  118: 			default:
  119: 				io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", 
  120: 						AIT_TYPE(val), i);
  121: 				return -1;
  122: 		}
  123: 	}
  124: 
  125: 	return Limit;
  126: }
  127: 
  128: /*
  129:  * io_buffer2vals() De-marshaling data from buffer to array with variables
  130:  * @buf = Buffer
  131:  * @buflen = Size of buffer
  132:  * @vnum = Number of variables into buffer
  133:  * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and 
  134:  		*DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
  135:  * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
  136:  */
  137: array_t *
  138: io_buffer2vals(u_char *buf, int buflen, int vnum, int zcpy)
  139: {
  140: 	array_t *vars;
  141: 	int Limit = 0;
  142: 	register int i;
  143: 	ait_val_t *v, *val;
  144: 	u_char *data;
  145: 
  146: 	assert(buf);
  147: 	if (!buf || !buflen || !vnum)
  148: 		return NULL;
  149: 
  150: 	Limit = sizeof(ait_val_t) * vnum;
  151: 	if (Limit > buflen) {
  152: 		io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d needed min %d ...\n", 
  153: 				buflen, Limit);
  154: 		return NULL;
  155: 	} else {
  156: 		if (!(vars = io_arrayInit(vnum)))
  157: 			return NULL;
  158: 
  159: 		v = (ait_val_t*) buf;
  160: 		data = buf + Limit;
  161: 	}
  162: 
  163: 	/* de-marshaling */
  164: 	for (i = 0; i < io_arraySize(vars); i++) {
  165: 		if (!zcpy) {
  166: 			val = malloc(sizeof(ait_val_t));
  167: 			if (!val) {
  168: 				LOGERR;
  169: 				io_arrayFree(vars);
  170: 				io_arrayDestroy(&vars);
  171: 				return NULL;
  172: 			}
  173: 		} else
  174: 			val = v + i;
  175: 		io_arraySet(vars, i, val);
  176: 
  177: 		val->val_type = v[i].val_type;
  178: 		AIT_LEN(val) = betoh32(AIT_LEN(&v[i]));
  179: 
  180: 		switch (AIT_TYPE(val)) {
  181: 			case blob:
  182: 			case f32:
  183: 			case f64:
  184: 			case i8:
  185: 			case i16:
  186: 			case i32:
  187: 			case i64:
  188: 			case u8:
  189: 			case u16:
  190: 			case u32:
  191: 			case u64:
  192: 				val->val.net = betoh64(v[i].val.net);
  193: 				break;
  194: 			case buffer:
  195: 			case string:
  196: 				if (AIT_LEN(val) > buflen - Limit) {
  197: 					io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d "
  198: 							"needed min %d ...\n", buflen, Limit + AIT_LEN(val));
  199: 					if (!zcpy)
  200: 						io_arrayFree(vars);
  201: 					io_arrayDestroy(&vars);
  202: 					return NULL;
  203: 				} else
  204: 					Limit += AIT_LEN(val);
  205: 
  206: 				if (!zcpy)
  207: 					memcpy(val->val.buffer, data, AIT_LEN(val));
  208: 				else
  209: 					val->val.buffer = data;
  210: 				data += AIT_LEN(val);
  211: 				break;
  212: 			default:
  213: 				io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", 
  214: 						AIT_TYPE(val), i);
  215: 				if (!zcpy)
  216: 					io_arrayFree(vars);
  217: 				io_arrayDestroy(&vars);
  218: 				return NULL;
  219: 		}
  220: 	}
  221: 
  222: 	return vars;
  223: }

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