xref: /titanic_41/usr/src/cmd/geniconvtbl/geniconvtbl.c (revision 85bb5f1d642e430b889478fb1200b511338085d7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*85bb5f1dSis  * Common Development and Distribution License (the "License").
6*85bb5f1dSis  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*85bb5f1dSis  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #if defined(DEBUG)
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #endif /* !DEBUG */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "iconv_tm.h"
447c478bd9Sstevel@tonic-gate #include "hash.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Debug
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #if defined(DEBUG)
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void	trace_init(void);
537c478bd9Sstevel@tonic-gate static void	trace_message(char *, ...);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static char	trace_option[128];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	TRACE(c)		(*(trace_option + (c & 0x007f)))
587c478bd9Sstevel@tonic-gate #define	TRACE_MESSAGE(c, args)	((TRACE(c))? trace_message args: (void)0)
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #else /* !DEBUG */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	trace_init()
637c478bd9Sstevel@tonic-gate #define	TRACE()
647c478bd9Sstevel@tonic-gate #define	TRACE_MESSAGE(c, args)
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #endif /* !DEBUG */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * ITM reference information
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate typedef struct _itm_ref {
737c478bd9Sstevel@tonic-gate 	char		*name;		/* ITM file name */
747c478bd9Sstevel@tonic-gate 	itm_hdr_t	*hdr;		/* address of ITM */
757c478bd9Sstevel@tonic-gate 	size_t		len;		/* length of ITM */
767c478bd9Sstevel@tonic-gate } itm_ref_t;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * struct _icv_state; to keep status
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate typedef struct _icv_state {
837c478bd9Sstevel@tonic-gate 	struct _itm_ref	*itm;		/* reference to ITM */
847c478bd9Sstevel@tonic-gate 	itm_hdr_t	*itm_hdr;	/* address of ITM */
857c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t	*direc;		/* current direction */
867c478bd9Sstevel@tonic-gate 	itm_place_t	default_action;	/* default action */
877c478bd9Sstevel@tonic-gate 	itm_num_t	*regs;		/* register */
887c478bd9Sstevel@tonic-gate 	itm_num_t	reg_num;	/* number of register */
897c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
907c478bd9Sstevel@tonic-gate 	int		op_depth;	/* depth of operation */
917c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
927c478bd9Sstevel@tonic-gate } icv_state_t;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * function prototype
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate void *	_icv_open(const char *);
997c478bd9Sstevel@tonic-gate void	_icv_close(icv_state_t *);
1007c478bd9Sstevel@tonic-gate size_t	_icv_iconv(icv_state_t *, const unsigned char **,
1017c478bd9Sstevel@tonic-gate 		    size_t *, unsigned char **, size_t *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static size_t	map_i_f(itm_tbl_hdr_t *,
1047c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1057c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *, long);
1067c478bd9Sstevel@tonic-gate static size_t	map_l_f(itm_tbl_hdr_t *,
1077c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1087c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *, long);
1097c478bd9Sstevel@tonic-gate static size_t	map_h_l(itm_tbl_hdr_t *,
1107c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1117c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *, long);
1127c478bd9Sstevel@tonic-gate static size_t	map_d_e_l(itm_tbl_hdr_t *,
1137c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1147c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *, long);
1157c478bd9Sstevel@tonic-gate static size_t	eval_cond_tbl(icv_state_t *, itm_place_t,
1167c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1177c478bd9Sstevel@tonic-gate 			size_t, itm_direc_t *);
1187c478bd9Sstevel@tonic-gate static size_t	eval_op_tbl(icv_state_t *, itm_place_t,
1197c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1207c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *);
1217c478bd9Sstevel@tonic-gate static size_t	eval_op(icv_state_t *, itm_place2_t,
1227c478bd9Sstevel@tonic-gate 			const unsigned char **, size_t *,
1237c478bd9Sstevel@tonic-gate 			unsigned char **, size_t *);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static itm_num_t	eval_expr(icv_state_t *, itm_place_t,
1267c478bd9Sstevel@tonic-gate 				size_t, const unsigned char *, size_t);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static void		itm_ref_free(int, void *, void *, void *, size_t);
1297c478bd9Sstevel@tonic-gate static itm_ref_t	*itm_ref_inc(const char *);
1307c478bd9Sstevel@tonic-gate static void		itm_ref_dec(itm_ref_t *);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void		op_init_default(icv_state_t *);
1337c478bd9Sstevel@tonic-gate static void		op_reset_default(icv_state_t *);
1347c478bd9Sstevel@tonic-gate static void		regs_init(icv_state_t *);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * macro definition
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #define	ADDR(place)	((void *)(((char *)(ist->itm_hdr)) +\
1427c478bd9Sstevel@tonic-gate 				((itm_place2_t)(place.itm_ptr))))
1437c478bd9Sstevel@tonic-gate #define	ADDR2(place2)	((void *)(((char *)(ist->itm_hdr)) +\
1447c478bd9Sstevel@tonic-gate 				((itm_place2_t)(place2))))
1457c478bd9Sstevel@tonic-gate #define	DADDR(n)	(((n)->size <= (sizeof ((n)->place.itm_64d))) ?	\
1467c478bd9Sstevel@tonic-gate 				((unsigned char *)(&((n)->place.itm_64d))) :\
1477c478bd9Sstevel@tonic-gate 				((unsigned char *)(ADDR((n)->place))))
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define	REG(n)		(*(ist->regs + (n)))
1507c478bd9Sstevel@tonic-gate #define	DISCARD(c)	(((*inbuf) = (void *)((*inbuf) + (c))),\
1517c478bd9Sstevel@tonic-gate 			((*inbytesleft) -= (c)))
1527c478bd9Sstevel@tonic-gate #define	GET(c)		((c) = **inbuf, (*inbuf)++, (*inbytesleft)--)
1537c478bd9Sstevel@tonic-gate #define	PUT(c)		(**outbuf = (c), (*outbuf)++, (*outbytesleft)--)
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #define	RETVALERR	((size_t)(-1))
1567c478bd9Sstevel@tonic-gate #define	RETVALDIR	((size_t)(-2))
1577c478bd9Sstevel@tonic-gate #define	RETVALBRK	((size_t)(-3))
1587c478bd9Sstevel@tonic-gate #define	RETVALRET	((size_t)(-4))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	UPDATE_ARGS()	(*inbuf = ip, \
1617c478bd9Sstevel@tonic-gate 			 *inbytesleft = ileft, \
1627c478bd9Sstevel@tonic-gate 			 *outbuf = op, \
1637c478bd9Sstevel@tonic-gate 			 *outbytesleft = oleft)
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Open; called from iconv_open()
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate void *
_icv_open(const char * itm)1697c478bd9Sstevel@tonic-gate _icv_open(const char	*itm)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	icv_state_t	*ist;
1727c478bd9Sstevel@tonic-gate 	itm_hdr_t	*hdr;
1737c478bd9Sstevel@tonic-gate 	itm_ref_t	*itm_ref;
1747c478bd9Sstevel@tonic-gate 	int		r;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * for debug
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	trace_init();
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * _icv_open() primaty task
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	itm_ref = itm_ref_inc(itm);
1857c478bd9Sstevel@tonic-gate 	if (NULL == itm_ref) {
1867c478bd9Sstevel@tonic-gate 		return ((void *)(-1));
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (NULL == (ist = malloc(sizeof (icv_state_t)))) {
1907c478bd9Sstevel@tonic-gate 		r = errno;
1917c478bd9Sstevel@tonic-gate 		itm_ref_dec(itm_ref);
1927c478bd9Sstevel@tonic-gate 		errno = r;
1937c478bd9Sstevel@tonic-gate 		return	(NULL);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	ist->itm = itm_ref;
1977c478bd9Sstevel@tonic-gate 	ist->itm_hdr = ist->itm->hdr;
1987c478bd9Sstevel@tonic-gate 	ist->reg_num = ist->itm->hdr->reg_num;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	hdr =  ist->itm->hdr;
2017c478bd9Sstevel@tonic-gate 	ist->direc = ADDR(hdr->direc_init_tbl);
2027c478bd9Sstevel@tonic-gate 	ist->default_action.itm_64d = 0;
2037c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
2047c478bd9Sstevel@tonic-gate 	ist->op_depth = 0;
2057c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * brief sanity check
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (hdr->itm_size.itm_ptr <= hdr->direc_init_tbl.itm_ptr) {
2127c478bd9Sstevel@tonic-gate 		_icv_close(ist);
2137c478bd9Sstevel@tonic-gate 		errno = ELIBBAD;
2147c478bd9Sstevel@tonic-gate 		return ((void *)(-1));
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* allocate register region */
2197c478bd9Sstevel@tonic-gate 	if (hdr->reg_num <= 0) {
2207c478bd9Sstevel@tonic-gate 		ist->regs = NULL;
2217c478bd9Sstevel@tonic-gate 	} else {
2227c478bd9Sstevel@tonic-gate 		ist->regs = malloc((sizeof (itm_num_t)) * hdr->reg_num);
2237c478bd9Sstevel@tonic-gate 		if (NULL == ist->regs) {
2247c478bd9Sstevel@tonic-gate 			r = errno;
2257c478bd9Sstevel@tonic-gate 			_icv_close(ist);
2267c478bd9Sstevel@tonic-gate 			errno = r;
2277c478bd9Sstevel@tonic-gate 			return ((void *)(-1));
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		(void) memset(ist->regs, 0,
2307c478bd9Sstevel@tonic-gate 		    (sizeof (itm_num_t)) * hdr->reg_num);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* evaluate init operation */
2357c478bd9Sstevel@tonic-gate 	if (0 != ist->itm_hdr->op_init_tbl.itm_ptr) {
2367c478bd9Sstevel@tonic-gate 		const unsigned char	*ip = NULL;
2377c478bd9Sstevel@tonic-gate 		size_t			ileft = 0;
2387c478bd9Sstevel@tonic-gate 		unsigned char		*op = NULL;
2397c478bd9Sstevel@tonic-gate 		size_t			oleft = 0;
240*85bb5f1dSis 		(void) eval_op_tbl(ist, ist->itm_hdr->op_init_tbl, &ip,
241*85bb5f1dSis 		    &ileft, &op, &oleft);
2427c478bd9Sstevel@tonic-gate 	} else {
2437c478bd9Sstevel@tonic-gate 		op_init_default(ist);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return	(ist);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * Close; called from iconv_close
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate void
_icv_close(icv_state_t * ist)2547c478bd9Sstevel@tonic-gate _icv_close(icv_state_t		*ist)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	if (NULL == ist) {
2577c478bd9Sstevel@tonic-gate 		errno = EBADF;
2587c478bd9Sstevel@tonic-gate 		return;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	itm_ref_dec(ist->itm);
2617c478bd9Sstevel@tonic-gate 	free(ist->regs);
2627c478bd9Sstevel@tonic-gate 	free(ist);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Actual conversion; called from iconv()
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate size_t
_icv_iconv(icv_state_t * ist,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft)2707c478bd9Sstevel@tonic-gate _icv_iconv(
2717c478bd9Sstevel@tonic-gate 	icv_state_t		*ist,
2727c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
2737c478bd9Sstevel@tonic-gate 	size_t			*inbytesleft,
2747c478bd9Sstevel@tonic-gate 	unsigned char		**outbuf,
2757c478bd9Sstevel@tonic-gate 	size_t			*outbytesleft)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	size_t			retval;
2787c478bd9Sstevel@tonic-gate 	itm_hdr_t		*hdr;
2797c478bd9Sstevel@tonic-gate 	itm_type_t		type;
2807c478bd9Sstevel@tonic-gate 	const unsigned char	*ip;
2817c478bd9Sstevel@tonic-gate 	size_t			ileft;
2827c478bd9Sstevel@tonic-gate 	itm_place_t		action;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (NULL == ist) {
2857c478bd9Sstevel@tonic-gate 		errno = EBADF;
2867c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('e', ("_icv_iconv: error=%d\n", errno));
2877c478bd9Sstevel@tonic-gate 		return ((size_t)(-1));
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	if (NULL == inbuf) {
2907c478bd9Sstevel@tonic-gate 		ip = NULL;
2917c478bd9Sstevel@tonic-gate 		inbuf = &ip;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	if (NULL == inbytesleft) {
2947c478bd9Sstevel@tonic-gate 		ileft = 0;
2957c478bd9Sstevel@tonic-gate 		inbytesleft = &ileft;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	hdr = ist->itm_hdr;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	retval = 0;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('i', ("_icv_iconv(inbuf=%p inbytesleft=%ld "
303*85bb5f1dSis 	    "outbuf=%p outbytesleft=%ld)\n", (NULL == inbuf) ? 0 : *inbuf,
3047c478bd9Sstevel@tonic-gate 	    (NULL == inbytesleft) ? 0 : *inbytesleft,
3057c478bd9Sstevel@tonic-gate 	    (NULL == outbuf) ? 0 : *outbuf,
3067c478bd9Sstevel@tonic-gate 	    (NULL == outbytesleft) ? 0 : *outbytesleft));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * If (NULL == inbuf || NULL == *inbuf) then this conversion is
3107c478bd9Sstevel@tonic-gate 	 * placed into initial state.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if ((NULL == inbuf) || (NULL == *inbuf)) {
3137c478bd9Sstevel@tonic-gate 		if (0 != hdr->op_reset_tbl.itm_ptr) {
3147c478bd9Sstevel@tonic-gate 			ist->direc = ADDR(hdr->direc_init_tbl);
315*85bb5f1dSis 			retval = eval_op_tbl(ist, hdr->op_reset_tbl, inbuf,
316*85bb5f1dSis 			    inbytesleft, outbuf, outbytesleft);
3177c478bd9Sstevel@tonic-gate 			if ((size_t)(-1) == retval) {
3187c478bd9Sstevel@tonic-gate 				return	(retval);
3197c478bd9Sstevel@tonic-gate 			}
3207c478bd9Sstevel@tonic-gate 		} else {
3217c478bd9Sstevel@tonic-gate 			op_reset_default(ist);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		return ((size_t)(0));
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (ITM_TBL_MAP_INDEX_FIXED_1_1 == ist->direc->type) {
3277c478bd9Sstevel@tonic-gate 		itm_map_idx_fix_hdr_t	*map_hdr;
3287c478bd9Sstevel@tonic-gate 		char			*map;
3297c478bd9Sstevel@tonic-gate 		const unsigned char	*ip;
3307c478bd9Sstevel@tonic-gate 		size_t			ileft;
3317c478bd9Sstevel@tonic-gate 		unsigned char		*op;
3327c478bd9Sstevel@tonic-gate 		size_t			oleft;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		map_hdr = (itm_map_idx_fix_hdr_t *)(ist->direc + 1);
3357c478bd9Sstevel@tonic-gate 		map = (char *)(map_hdr + 1);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		if (1 == map_hdr->default_error) {
338*85bb5f1dSis 			retval = map_i_f(ist->direc, inbuf, inbytesleft,
3397c478bd9Sstevel@tonic-gate 			    outbuf, outbytesleft, 0);
3407c478bd9Sstevel@tonic-gate 			return	(retval);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		ip = *inbuf;
3447c478bd9Sstevel@tonic-gate 		ileft = *inbytesleft;
3457c478bd9Sstevel@tonic-gate 		op = *outbuf;
3467c478bd9Sstevel@tonic-gate 		oleft = *outbytesleft;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		while (1 <= ileft) {
3497c478bd9Sstevel@tonic-gate 			if (oleft < 1) {
3507c478bd9Sstevel@tonic-gate 				UPDATE_ARGS();
3517c478bd9Sstevel@tonic-gate 				errno = E2BIG;
3527c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e', ("_icv_iconv: error=%d\n",
3537c478bd9Sstevel@tonic-gate 				    errno));
3547c478bd9Sstevel@tonic-gate 				return ((size_t)-1);
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 			*(op++) = *(map + *(ip++));
3577c478bd9Sstevel@tonic-gate 			ileft--;
3587c478bd9Sstevel@tonic-gate 			oleft--;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		UPDATE_ARGS();
3627c478bd9Sstevel@tonic-gate 		return (0);
3637c478bd9Sstevel@tonic-gate 	} else if (ITM_TBL_MAP_INDEX_FIXED == ist->direc->type) {
364*85bb5f1dSis 		retval = map_i_f(ist->direc, inbuf, inbytesleft,
3657c478bd9Sstevel@tonic-gate 		    outbuf, outbytesleft, 0);
3667c478bd9Sstevel@tonic-gate 		return	(retval);
3677c478bd9Sstevel@tonic-gate 	} else if (ITM_TBL_MAP_HASH == ist->direc->type) {
368*85bb5f1dSis 		retval = map_h_l(ist->direc, inbuf, inbytesleft,
3697c478bd9Sstevel@tonic-gate 		    outbuf, outbytesleft, 0);
3707c478bd9Sstevel@tonic-gate 		return	(retval);
3717c478bd9Sstevel@tonic-gate 	} else if (ITM_TBL_MAP_DENSE_ENC == ist->direc->type) {
372*85bb5f1dSis 		retval = map_d_e_l(ist->direc, inbuf, inbytesleft,
3737c478bd9Sstevel@tonic-gate 		    outbuf, outbytesleft, 0);
3747c478bd9Sstevel@tonic-gate 		return	(retval);
3757c478bd9Sstevel@tonic-gate 	} else if (ITM_TBL_MAP_LOOKUP == ist->direc->type) {
376*85bb5f1dSis 		retval = map_l_f(ist->direc, inbuf, inbytesleft,
3777c478bd9Sstevel@tonic-gate 		    outbuf, outbytesleft, 0);
3787c478bd9Sstevel@tonic-gate 		return	(retval);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
3827c478bd9Sstevel@tonic-gate 	ist->op_depth = 0;
3837c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * Main loop; basically 1 loop per 1 output character
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate retry_cond_eval:
3907c478bd9Sstevel@tonic-gate 	while (0 < *inbytesleft) {
3917c478bd9Sstevel@tonic-gate 		itm_tbl_hdr_t	*direc_hdr;
3927c478bd9Sstevel@tonic-gate 		itm_direc_t	*direc;
3937c478bd9Sstevel@tonic-gate 		long		i;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		direc_hdr = ist->direc;
3967c478bd9Sstevel@tonic-gate 		direc = (itm_direc_t *)(ist->direc + 1);
3977c478bd9Sstevel@tonic-gate 		for (i = 0; /* NULL */; i++, direc++) {
3987c478bd9Sstevel@tonic-gate 			if (i >= direc_hdr->number) {
3997c478bd9Sstevel@tonic-gate 				if (0 == ist->default_action.itm_ptr) {
4007c478bd9Sstevel@tonic-gate 					errno = EILSEQ;
4017c478bd9Sstevel@tonic-gate 					TRACE_MESSAGE('e',
402*85bb5f1dSis 					    ("_icv_iconv:error=%d\n", errno));
4037c478bd9Sstevel@tonic-gate 					return ((size_t)(-1));
4047c478bd9Sstevel@tonic-gate 				}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 				action = ist->default_action;
4097c478bd9Sstevel@tonic-gate 				type = ((itm_tbl_hdr_t *)(ADDR(action)))->type;
4107c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('E',
411*85bb5f1dSis 				    ("escape seq (default action=%6p, "
412*85bb5f1dSis 				    "type=%ld) executing\n",
413*85bb5f1dSis 				    action.itm_ptr, type));
4147c478bd9Sstevel@tonic-gate 			} else if (0 != direc->condition.itm_ptr) {
4157c478bd9Sstevel@tonic-gate 				retval = eval_cond_tbl(ist, direc->condition,
416*85bb5f1dSis 				    inbuf, inbytesleft, *outbytesleft, direc);
4177c478bd9Sstevel@tonic-gate 				if ((size_t)(0) == retval) {
4187c478bd9Sstevel@tonic-gate 					continue;
4197c478bd9Sstevel@tonic-gate 				} else if ((size_t)(-1) == retval) {
4207c478bd9Sstevel@tonic-gate 					return	(retval);
4217c478bd9Sstevel@tonic-gate 				} else if ((size_t)(2) == retval) {
4227c478bd9Sstevel@tonic-gate 					goto retry_cond_eval;
4237c478bd9Sstevel@tonic-gate 				}
4247c478bd9Sstevel@tonic-gate 				action = direc->action;
4257c478bd9Sstevel@tonic-gate 				type = ((itm_tbl_hdr_t *)(ADDR(action)))->type;
4267c478bd9Sstevel@tonic-gate 			} else {
4277c478bd9Sstevel@tonic-gate 				action = direc->action;
4287c478bd9Sstevel@tonic-gate 				type = ((itm_tbl_hdr_t *)(ADDR(action)))->type;
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('a',
4327c478bd9Sstevel@tonic-gate 			    ("inbytesleft=%ld; type=%ld:action=%p\n",
4337c478bd9Sstevel@tonic-gate 			    *inbytesleft, type, action.itm_ptr));
4347c478bd9Sstevel@tonic-gate 			switch (ITM_TBL_MASK & type) {
4357c478bd9Sstevel@tonic-gate 			case ITM_TBL_OP:
4367c478bd9Sstevel@tonic-gate 				retval = eval_op_tbl(ist, action,
437*85bb5f1dSis 				    inbuf, inbytesleft, outbuf, outbytesleft);
4387c478bd9Sstevel@tonic-gate 				if ((size_t)(-1) == retval) {
4397c478bd9Sstevel@tonic-gate 					return	(retval);
4407c478bd9Sstevel@tonic-gate 				}
4417c478bd9Sstevel@tonic-gate 				break;
4427c478bd9Sstevel@tonic-gate 			case ITM_TBL_DIREC:
4437c478bd9Sstevel@tonic-gate 				ist->direc = ADDR(action);
4447c478bd9Sstevel@tonic-gate 				break;
4457c478bd9Sstevel@tonic-gate 			case ITM_TBL_MAP:
4467c478bd9Sstevel@tonic-gate 				switch (type) {
4477c478bd9Sstevel@tonic-gate 				case ITM_TBL_MAP_INDEX_FIXED_1_1:
4487c478bd9Sstevel@tonic-gate 				case ITM_TBL_MAP_INDEX_FIXED:
449*85bb5f1dSis 					retval = map_i_f(ADDR(action),
4507c478bd9Sstevel@tonic-gate 					    inbuf, inbytesleft,
451*85bb5f1dSis 					    outbuf, outbytesleft, 1);
4527c478bd9Sstevel@tonic-gate 					break;
4537c478bd9Sstevel@tonic-gate 				case ITM_TBL_MAP_HASH:
4547c478bd9Sstevel@tonic-gate 					retval = map_h_l(ADDR(action),
4557c478bd9Sstevel@tonic-gate 					    inbuf, inbytesleft,
4567c478bd9Sstevel@tonic-gate 					    outbuf, outbytesleft, 1);
4577c478bd9Sstevel@tonic-gate 					break;
4587c478bd9Sstevel@tonic-gate 				case ITM_TBL_MAP_DENSE_ENC:
4597c478bd9Sstevel@tonic-gate 					retval = map_d_e_l(ADDR(action),
4607c478bd9Sstevel@tonic-gate 					    inbuf, inbytesleft,
4617c478bd9Sstevel@tonic-gate 					    outbuf, outbytesleft, 1);
4627c478bd9Sstevel@tonic-gate 					break;
4637c478bd9Sstevel@tonic-gate 				case ITM_TBL_MAP_LOOKUP:
464*85bb5f1dSis 					retval = map_l_f(ADDR(action),
4657c478bd9Sstevel@tonic-gate 					    inbuf, inbytesleft,
466*85bb5f1dSis 					    outbuf, outbytesleft, 1);
4677c478bd9Sstevel@tonic-gate 					break;
4687c478bd9Sstevel@tonic-gate 				default:
4697c478bd9Sstevel@tonic-gate 					errno = ELIBBAD;
4707c478bd9Sstevel@tonic-gate 					TRACE_MESSAGE('e',
4717c478bd9Sstevel@tonic-gate 					    ("_icv_iconv:error=%d\n", errno));
4727c478bd9Sstevel@tonic-gate 					return ((size_t)(-1));
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 				}
4757c478bd9Sstevel@tonic-gate 				if ((size_t)(-1) == retval) {
4767c478bd9Sstevel@tonic-gate 					return	(retval);
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 				break;
4797c478bd9Sstevel@tonic-gate 			default:	/* never */
4807c478bd9Sstevel@tonic-gate 				errno = ELIBBAD;
4817c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e',
4827c478bd9Sstevel@tonic-gate 				    ("_icv_iconv:error=%d\n", errno));
4837c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 			break;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	return	(retval);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * map-indexed-fixed
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate static size_t
map_i_f(itm_tbl_hdr_t * tbl_hdr,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft,long once)4977c478bd9Sstevel@tonic-gate map_i_f(
4987c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*tbl_hdr,
4997c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
5007c478bd9Sstevel@tonic-gate 	size_t			*inbytesleft,
5017c478bd9Sstevel@tonic-gate 	unsigned char		**outbuf,
5027c478bd9Sstevel@tonic-gate 	size_t			*outbytesleft,
5037c478bd9Sstevel@tonic-gate 	long			once)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	itm_map_idx_fix_hdr_t	*map_hdr;
5067c478bd9Sstevel@tonic-gate 	long			i;
5077c478bd9Sstevel@tonic-gate 	unsigned char		c;
5087c478bd9Sstevel@tonic-gate 	unsigned long		j;
5097c478bd9Sstevel@tonic-gate 	const unsigned char	*p;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('i', ("map_i_f\n"));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	map_hdr = (itm_map_idx_fix_hdr_t *)(tbl_hdr + 1);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	do {
5167c478bd9Sstevel@tonic-gate 		if (*inbytesleft < map_hdr->source_len) {
5177c478bd9Sstevel@tonic-gate 			errno = EINVAL;
5187c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_i_f:error=%d\n", errno));
5197c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		j = 0;
5237c478bd9Sstevel@tonic-gate 		for (i = 0; i < map_hdr->source_len; i++) {
5247c478bd9Sstevel@tonic-gate 			GET(c);
5257c478bd9Sstevel@tonic-gate 			j = ((j << 8) | c);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		if (((j < map_hdr->start.itm_ptr) ||
5297c478bd9Sstevel@tonic-gate 		    (map_hdr->end.itm_ptr < j)) &&
5307c478bd9Sstevel@tonic-gate 		    (0 < map_hdr->default_error)) {
5317c478bd9Sstevel@tonic-gate 			errno = EILSEQ;
5327c478bd9Sstevel@tonic-gate 			(*inbuf) = (void*) ((*inbuf) - map_hdr->source_len);
5337c478bd9Sstevel@tonic-gate 			(*inbytesleft) += map_hdr->source_len;
5347c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_i_f:error=%d\n", errno));
5357c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if (*outbytesleft < map_hdr->result_len) {
5397c478bd9Sstevel@tonic-gate 			errno = E2BIG;
5407c478bd9Sstevel@tonic-gate 			(*inbuf) = (void *)((*inbuf) - map_hdr->source_len);
5417c478bd9Sstevel@tonic-gate 			(*inbytesleft) += map_hdr->source_len;
5427c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_i_f:error=%d\n", errno));
5437c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if ((j < map_hdr->start.itm_ptr) ||
5477c478bd9Sstevel@tonic-gate 		    (map_hdr->end.itm_ptr < j)) {
5487c478bd9Sstevel@tonic-gate 			if (0 == map_hdr->default_error) {
5497c478bd9Sstevel@tonic-gate 				p = (((unsigned char *)(map_hdr + 1)) +
550*85bb5f1dSis 				    (map_hdr->result_len * (tbl_hdr->number)));
5517c478bd9Sstevel@tonic-gate 				for (i = 0; i < map_hdr->result_len; i++) {
5527c478bd9Sstevel@tonic-gate 					PUT(*(p + i));
5537c478bd9Sstevel@tonic-gate 				}
5547c478bd9Sstevel@tonic-gate 			} else {
5557c478bd9Sstevel@tonic-gate 				p = ((*inbuf) - map_hdr->source_len);
5567c478bd9Sstevel@tonic-gate 				for (i = 0; i < map_hdr->source_len; i++) {
5577c478bd9Sstevel@tonic-gate 					PUT(*(p + i));
5587c478bd9Sstevel@tonic-gate 				}
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 		} else {
5617c478bd9Sstevel@tonic-gate 			char	*map_error;
5627c478bd9Sstevel@tonic-gate 			map_error = (((char *)(map_hdr + 1)) +
563*85bb5f1dSis 			    (map_hdr->result_len * (tbl_hdr->number)) +
5647c478bd9Sstevel@tonic-gate 			    (j - map_hdr->start.itm_ptr));
5657c478bd9Sstevel@tonic-gate 			if (0 == map_hdr->default_error) {
5667c478bd9Sstevel@tonic-gate 				map_error = (void *)
5677c478bd9Sstevel@tonic-gate 				    (map_error + map_hdr->result_len);
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 			if (((1 == map_hdr->default_error) ||
5707c478bd9Sstevel@tonic-gate 			    (0 < map_hdr->error_num)) &&
5717c478bd9Sstevel@tonic-gate 			    (0 != *(map_error))) {
5727c478bd9Sstevel@tonic-gate 				errno = EILSEQ;
5737c478bd9Sstevel@tonic-gate 				(*inbuf) = (void *)
5747c478bd9Sstevel@tonic-gate 				    ((*inbuf) - map_hdr->source_len);
5757c478bd9Sstevel@tonic-gate 				(*inbytesleft) += map_hdr->source_len;
5767c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e',
5777c478bd9Sstevel@tonic-gate 				    ("map_i_f:error=%d\n", errno));
5787c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			p = (((unsigned char *)(map_hdr + 1)) +
5817c478bd9Sstevel@tonic-gate 			    (map_hdr->result_len *
5827c478bd9Sstevel@tonic-gate 			    (j - map_hdr->start.itm_ptr)));
5837c478bd9Sstevel@tonic-gate 			for (i = 0; i < map_hdr->result_len; i++) {
5847c478bd9Sstevel@tonic-gate 				PUT(*(p + i));
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	} while ((0 < *inbytesleft) && (0 == once));
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	return (size_t)(0);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * map-lookup-fixed
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate static size_t
map_l_f(itm_tbl_hdr_t * tbl_hdr,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft,long once)5977c478bd9Sstevel@tonic-gate map_l_f(
5987c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t	*tbl_hdr,
5997c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
6007c478bd9Sstevel@tonic-gate 	size_t		*inbytesleft,
6017c478bd9Sstevel@tonic-gate 	unsigned char	**outbuf,
6027c478bd9Sstevel@tonic-gate 	size_t		*outbytesleft,
6037c478bd9Sstevel@tonic-gate 	long		once)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	itm_map_lookup_hdr_t	*map_hdr;
6067c478bd9Sstevel@tonic-gate 	long			i;
6077c478bd9Sstevel@tonic-gate 	unsigned char		*map;
6087c478bd9Sstevel@tonic-gate 	const unsigned char	*p;
6097c478bd9Sstevel@tonic-gate 	long			high;
6107c478bd9Sstevel@tonic-gate 	long			mid;
6117c478bd9Sstevel@tonic-gate 	long			low;
6127c478bd9Sstevel@tonic-gate 	long			result;
6137c478bd9Sstevel@tonic-gate 	itm_size_t		pair_size;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('i', ("map_l_f\n"));
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	map_hdr = (itm_map_lookup_hdr_t *)(tbl_hdr + 1);
6187c478bd9Sstevel@tonic-gate 	map = (unsigned char *)(map_hdr + 1);
6197c478bd9Sstevel@tonic-gate 	pair_size = map_hdr->source_len + 1 + map_hdr->result_len;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	do {
6227c478bd9Sstevel@tonic-gate 		if (*inbytesleft < map_hdr->source_len) {
6237c478bd9Sstevel@tonic-gate 			errno = EINVAL;
6247c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_l_f:error=%d\n", errno));
6257c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		for (low = 0, high = tbl_hdr->number; low < high; ) {
6297c478bd9Sstevel@tonic-gate 			mid = (low + high) / 2;
6307c478bd9Sstevel@tonic-gate 			p = map + (pair_size * mid);
6317c478bd9Sstevel@tonic-gate 			for (i = 0, result = 0; i < map_hdr->source_len;
6327c478bd9Sstevel@tonic-gate 			    i++, p++) {
6337c478bd9Sstevel@tonic-gate 				if (*(unsigned char *)(*inbuf + i) < *p) {
6347c478bd9Sstevel@tonic-gate 					result = -1;
6357c478bd9Sstevel@tonic-gate 					break;
6367c478bd9Sstevel@tonic-gate 				}
6377c478bd9Sstevel@tonic-gate 				if (*p < *(unsigned char *)(*inbuf + i)) {
6387c478bd9Sstevel@tonic-gate 					result = 1;
6397c478bd9Sstevel@tonic-gate 					break;
6407c478bd9Sstevel@tonic-gate 				}
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 			if (result < 0) {
6437c478bd9Sstevel@tonic-gate 				high = mid;
6447c478bd9Sstevel@tonic-gate 			} else if (0 < result) {
6457c478bd9Sstevel@tonic-gate 				low = mid + 1;
6467c478bd9Sstevel@tonic-gate 			} else { /* 0 == result */
6477c478bd9Sstevel@tonic-gate 				break;
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		if (0 != result) {
6527c478bd9Sstevel@tonic-gate 			if (map_hdr->default_error < 0) {
6537c478bd9Sstevel@tonic-gate 				p = *inbuf;
6547c478bd9Sstevel@tonic-gate 			} else if (0 == map_hdr->default_error) {
6557c478bd9Sstevel@tonic-gate 				p = map + (pair_size * tbl_hdr->number) +
6567c478bd9Sstevel@tonic-gate 				    map_hdr->source_len + 1;
6577c478bd9Sstevel@tonic-gate 			} else if (0 < map_hdr->default_error) {
6587c478bd9Sstevel@tonic-gate 				errno = EILSEQ;
6597c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e', ("map_l_f:error=%d\n",
6607c478bd9Sstevel@tonic-gate 				    errno));
6617c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
6627c478bd9Sstevel@tonic-gate 			}
6637c478bd9Sstevel@tonic-gate 		} else {
6647c478bd9Sstevel@tonic-gate 			if (0 != (*p)) {
6657c478bd9Sstevel@tonic-gate 				errno = EILSEQ;
6667c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e', ("map_l_f:error=%d\n",
6677c478bd9Sstevel@tonic-gate 				    errno));
6687c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			p++;
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (*outbytesleft < map_hdr->result_len) {
6747c478bd9Sstevel@tonic-gate 			errno = E2BIG;
6757c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_l_f:error=%d\n", errno));
6767c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		DISCARD(map_hdr->source_len);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		for (i = 0; i < map_hdr->result_len; i++) {
6817c478bd9Sstevel@tonic-gate 			PUT(*(p + i));
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 	} while ((0 < *inbytesleft) && (0 == once));
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return ((size_t)(0));
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate /*
6907c478bd9Sstevel@tonic-gate  * map-hash-lookup
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate static size_t
map_h_l(itm_tbl_hdr_t * tbl_hdr,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft,long once)6937c478bd9Sstevel@tonic-gate map_h_l(
6947c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t	*tbl_hdr,
6957c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
6967c478bd9Sstevel@tonic-gate 	size_t		*inbytesleft,
6977c478bd9Sstevel@tonic-gate 	unsigned char	**outbuf,
6987c478bd9Sstevel@tonic-gate 	size_t		*outbytesleft,
6997c478bd9Sstevel@tonic-gate 	long		once)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	itm_map_hash_hdr_t	*map_hdr;
7027c478bd9Sstevel@tonic-gate 	long			i;
7037c478bd9Sstevel@tonic-gate 	unsigned char	*map_error;
7047c478bd9Sstevel@tonic-gate 	unsigned char	*map_hash;
7057c478bd9Sstevel@tonic-gate 	unsigned char	*map_of;
7067c478bd9Sstevel@tonic-gate 	const unsigned char	*p;
7077c478bd9Sstevel@tonic-gate 	const unsigned char	*q;
7087c478bd9Sstevel@tonic-gate 	long			high;
7097c478bd9Sstevel@tonic-gate 	long			mid;
7107c478bd9Sstevel@tonic-gate 	long			low;
7117c478bd9Sstevel@tonic-gate 	long			result;
7127c478bd9Sstevel@tonic-gate 	itm_size_t		pair_size;
7137c478bd9Sstevel@tonic-gate 	itm_size_t		hash_value;
7147c478bd9Sstevel@tonic-gate 	itm_size_t		source_len;
7157c478bd9Sstevel@tonic-gate 	itm_size_t		result_len;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('i', ("map_hash\n"));
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	map_hdr = (itm_map_hash_hdr_t *)(tbl_hdr + 1);
7207c478bd9Sstevel@tonic-gate 	map_error = (unsigned char *)(map_hdr + 1);
7217c478bd9Sstevel@tonic-gate 	map_hash = (map_error + map_hdr->hash_tbl_num);
7227c478bd9Sstevel@tonic-gate 	map_of = map_hash + map_hdr->hash_tbl_size;
7237c478bd9Sstevel@tonic-gate 	pair_size = map_hdr->source_len + 1 + map_hdr->result_len;
7247c478bd9Sstevel@tonic-gate 	source_len = map_hdr->source_len;
7257c478bd9Sstevel@tonic-gate 	result_len = map_hdr->result_len;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	do {
7287c478bd9Sstevel@tonic-gate 		if (*inbytesleft < source_len) {
7297c478bd9Sstevel@tonic-gate 			errno = EINVAL;
7307c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_h_l:error=%d\n", errno));
7317c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		result = 1;
7357c478bd9Sstevel@tonic-gate 		q = *inbuf;
736*85bb5f1dSis 		hash_value = hash((const char *)(q), source_len,
737*85bb5f1dSis 		    map_hdr->hash_tbl_num);
7387c478bd9Sstevel@tonic-gate 		p = map_hash + (pair_size * hash_value);
7397c478bd9Sstevel@tonic-gate 		if (1 == *(map_error + hash_value)) {
7407c478bd9Sstevel@tonic-gate 			for (i = 0, result = 0; i < source_len; i++) {
7417c478bd9Sstevel@tonic-gate 				if (*(q + i) != *(p++)) {
7427c478bd9Sstevel@tonic-gate 					result = -2;
7437c478bd9Sstevel@tonic-gate 					break;
7447c478bd9Sstevel@tonic-gate 				}
7457c478bd9Sstevel@tonic-gate 			}
7467c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('G',
7477c478bd9Sstevel@tonic-gate 			    ("(h=%d): find pair without conflict\n",
7487c478bd9Sstevel@tonic-gate 			    hash_value));
7497c478bd9Sstevel@tonic-gate 		} else if (0 == *(map_error + hash_value)) {
750*85bb5f1dSis 			TRACE_MESSAGE('G', ("(h=%d): No Pair\n", hash_value));
7517c478bd9Sstevel@tonic-gate 			result = -3;
7527c478bd9Sstevel@tonic-gate 		} else /* if (0 < *(map_error + hash_value)) */ {
7537c478bd9Sstevel@tonic-gate 			for (i = 0, result = 0; i < source_len; i++) {
7547c478bd9Sstevel@tonic-gate 				if (*(q + i) != *(p++)) {
7557c478bd9Sstevel@tonic-gate 					result = 1;
7567c478bd9Sstevel@tonic-gate 					break;
7577c478bd9Sstevel@tonic-gate 				}
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 			if (0 < result) {
7607c478bd9Sstevel@tonic-gate 				for (low = 0, high = map_hdr->hash_of_num;
7617c478bd9Sstevel@tonic-gate 				    low < high; /* NOP */) {
7627c478bd9Sstevel@tonic-gate 					mid = (low + high) / 2;
7637c478bd9Sstevel@tonic-gate 					p = map_of + (pair_size * mid);
7647c478bd9Sstevel@tonic-gate 					for (i = 0, result = 0;
7657c478bd9Sstevel@tonic-gate 					    i < source_len;
7667c478bd9Sstevel@tonic-gate 					    i++, p++) {
7677c478bd9Sstevel@tonic-gate 						if (*(q + i) < *p) {
7687c478bd9Sstevel@tonic-gate 							result = -1;
7697c478bd9Sstevel@tonic-gate 							break;
7707c478bd9Sstevel@tonic-gate 						}
7717c478bd9Sstevel@tonic-gate 						if (*p < *(q + i)) {
7727c478bd9Sstevel@tonic-gate 							result = 1;
7737c478bd9Sstevel@tonic-gate 							break;
7747c478bd9Sstevel@tonic-gate 						}
7757c478bd9Sstevel@tonic-gate 					}
7767c478bd9Sstevel@tonic-gate 					if (result < 0) {
7777c478bd9Sstevel@tonic-gate 						high = mid;
7787c478bd9Sstevel@tonic-gate 					} else if (0 < result) {
7797c478bd9Sstevel@tonic-gate 						low = mid + 1;
7807c478bd9Sstevel@tonic-gate 					} else { /* 0 == result */
781*85bb5f1dSis 						TRACE_MESSAGE('G', ("(h=%d): "
782*85bb5f1dSis 						    "find data on out of "
7837c478bd9Sstevel@tonic-gate 						    "hashtable with CONFLICT\n",
7847c478bd9Sstevel@tonic-gate 						    hash_value));
7857c478bd9Sstevel@tonic-gate 						break;
7867c478bd9Sstevel@tonic-gate 					}
7877c478bd9Sstevel@tonic-gate 				}
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 		if (0 != result) {
7917c478bd9Sstevel@tonic-gate 			if (map_hdr->default_error < 0) {
7927c478bd9Sstevel@tonic-gate 				p = q;
7937c478bd9Sstevel@tonic-gate 			} else if (0 == map_hdr->default_error) {
7947c478bd9Sstevel@tonic-gate 				p = map_of + map_hdr->hash_of_size;
7957c478bd9Sstevel@tonic-gate 			} else if (0 < map_hdr->default_error) {
796*85bb5f1dSis 				TRACE_MESSAGE('G', ("(h=%d): NO PAIR\n",
7977c478bd9Sstevel@tonic-gate 				    hash_value));
7987c478bd9Sstevel@tonic-gate 				errno = EILSEQ;
7997c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('e',
8007c478bd9Sstevel@tonic-gate 				    ("map_h_l:error=%d\n", errno));
8017c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
8027c478bd9Sstevel@tonic-gate 			}
8037c478bd9Sstevel@tonic-gate 		} else {
8047c478bd9Sstevel@tonic-gate 			if (0 != (*p)) {
8057c478bd9Sstevel@tonic-gate 				errno = EILSEQ;
806*85bb5f1dSis 				TRACE_MESSAGE('G', ("	      : error pair\n"));
807*85bb5f1dSis 				TRACE_MESSAGE('e', ("map_l_f:error\n", errno));
8087c478bd9Sstevel@tonic-gate 				return ((size_t)(-1));
8097c478bd9Sstevel@tonic-gate 			}
8107c478bd9Sstevel@tonic-gate 			p++;
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		if (*outbytesleft < result_len) {
8147c478bd9Sstevel@tonic-gate 			errno = E2BIG;
8157c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_h_l:error=%d\n", errno));
8167c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		DISCARD(source_len);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		for (i = 0; i < result_len; i++) {
8217c478bd9Sstevel@tonic-gate 			PUT(*(p + i));
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	} while ((0 < *inbytesleft) && (0 == once));
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	return ((size_t)(0));
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * map-dense_encoding-lookup
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate static size_t
map_d_e_l(itm_tbl_hdr_t * tbl_hdr,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft,long once)8337c478bd9Sstevel@tonic-gate map_d_e_l(
8347c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*tbl_hdr,
8357c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
8367c478bd9Sstevel@tonic-gate 	size_t			*inbytesleft,
8377c478bd9Sstevel@tonic-gate 	unsigned char		**outbuf,
8387c478bd9Sstevel@tonic-gate 	size_t			*outbytesleft,
8397c478bd9Sstevel@tonic-gate 	long			once)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	itm_map_dense_enc_hdr_t	*map_hdr;
8427c478bd9Sstevel@tonic-gate 	long			i;
8437c478bd9Sstevel@tonic-gate 	itm_num_t		j;
8447c478bd9Sstevel@tonic-gate 	const unsigned char	*p;
8457c478bd9Sstevel@tonic-gate 	unsigned char		*map_ptr;
8467c478bd9Sstevel@tonic-gate 	unsigned char		*map_error;
8477c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_min;
8487c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_max;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('i', ("map_d_e_l\n"));
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	map_hdr = (itm_map_dense_enc_hdr_t *)(tbl_hdr + 1);
853*85bb5f1dSis 	map_ptr = ((unsigned char *)(map_hdr + 1) + map_hdr->source_len +
854*85bb5f1dSis 	    map_hdr->source_len);
8557c478bd9Sstevel@tonic-gate 	map_error = (map_ptr + (tbl_hdr->number * map_hdr->result_len));
8567c478bd9Sstevel@tonic-gate 	if (0 == map_hdr->default_error) {
8577c478bd9Sstevel@tonic-gate 		map_error = (void *)(map_error + map_hdr->result_len);
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	byte_seq_min = (unsigned char *)(map_hdr + 1);
8607c478bd9Sstevel@tonic-gate 	byte_seq_max = byte_seq_min + map_hdr->source_len;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	do {
8637c478bd9Sstevel@tonic-gate 		if (*inbytesleft < map_hdr->source_len) {
8647c478bd9Sstevel@tonic-gate 			errno = EINVAL;
8657c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_d_e_l:error=%d\n", errno));
8667c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		j = hash_dense_encoding(*inbuf, map_hdr->source_len,
8707c478bd9Sstevel@tonic-gate 		    byte_seq_min, byte_seq_max);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		if (((j < 0) || (tbl_hdr->number < j)) &&
8737c478bd9Sstevel@tonic-gate 		    (0 < map_hdr->default_error)) {
8747c478bd9Sstevel@tonic-gate 			errno = EILSEQ;
8757c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_d_e_l:error=%d\n", errno));
8767c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		if (*outbytesleft < map_hdr->result_len) {
8807c478bd9Sstevel@tonic-gate 			errno = E2BIG;
8817c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("map_d_e_l:error=%d\n", errno));
8827c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if ((j < 0) || (tbl_hdr->number < j)) {
8867c478bd9Sstevel@tonic-gate 			if (0 == map_hdr->default_error) {
887*85bb5f1dSis 				p = (map_ptr + (tbl_hdr->number *
888*85bb5f1dSis 				    map_hdr->result_len));
8897c478bd9Sstevel@tonic-gate 				for (i = 0; i < map_hdr->result_len; i++) {
8907c478bd9Sstevel@tonic-gate 					PUT(*(p + i));
8917c478bd9Sstevel@tonic-gate 				}
8927c478bd9Sstevel@tonic-gate 			} else {
8937c478bd9Sstevel@tonic-gate 				p = *inbuf;
8947c478bd9Sstevel@tonic-gate 				for (i = 0; i < map_hdr->source_len; i++) {
8957c478bd9Sstevel@tonic-gate 					PUT(*(p + i));
8967c478bd9Sstevel@tonic-gate 				}
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 		} else {
8997c478bd9Sstevel@tonic-gate 			if ((1 == map_hdr->default_error) ||
9007c478bd9Sstevel@tonic-gate 			    (0 < map_hdr->error_num)) {
9017c478bd9Sstevel@tonic-gate 				if (0 != *(map_error + j)) {
9027c478bd9Sstevel@tonic-gate 					errno = EILSEQ;
9037c478bd9Sstevel@tonic-gate 					TRACE_MESSAGE('e',
9047c478bd9Sstevel@tonic-gate 					    ("map_d_e_l:error=%d\n", errno));
9057c478bd9Sstevel@tonic-gate 					return ((size_t)(-1));
9067c478bd9Sstevel@tonic-gate 				}
9077c478bd9Sstevel@tonic-gate 			}
9087c478bd9Sstevel@tonic-gate 			p = (map_ptr + (map_hdr->result_len * j));
9097c478bd9Sstevel@tonic-gate 			for (i = 0; i < map_hdr->result_len; i++) {
9107c478bd9Sstevel@tonic-gate 				PUT(*(p + i));
9117c478bd9Sstevel@tonic-gate 			}
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 		DISCARD(map_hdr->source_len);
9147c478bd9Sstevel@tonic-gate 	} while ((0 < *inbytesleft) && (0 == once));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	return ((size_t)(0));
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * Evaluate condition table
9237c478bd9Sstevel@tonic-gate  *
9247c478bd9Sstevel@tonic-gate  */
9257c478bd9Sstevel@tonic-gate static size_t
eval_cond_tbl(icv_state_t * ist,itm_place_t cond_place,const unsigned char ** inbuf,size_t * inbytesleft,size_t outbytesleft,itm_direc_t * direc)9267c478bd9Sstevel@tonic-gate eval_cond_tbl(
9277c478bd9Sstevel@tonic-gate 	icv_state_t		*ist,
9287c478bd9Sstevel@tonic-gate 	itm_place_t		cond_place,
9297c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
9307c478bd9Sstevel@tonic-gate 	size_t			*inbytesleft,
9317c478bd9Sstevel@tonic-gate 	size_t			outbytesleft,
9327c478bd9Sstevel@tonic-gate 	itm_direc_t		*direc
9337c478bd9Sstevel@tonic-gate )
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*cond_hdr;
9367c478bd9Sstevel@tonic-gate 	itm_cond_t		*cond;
9377c478bd9Sstevel@tonic-gate 	long			i;
9387c478bd9Sstevel@tonic-gate 	long			j;
9397c478bd9Sstevel@tonic-gate 	long			k;
9407c478bd9Sstevel@tonic-gate 	size_t			retval;
9417c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*rth;
9427c478bd9Sstevel@tonic-gate 	itm_range_hdr_t		*rtsh;
9437c478bd9Sstevel@tonic-gate 	unsigned char		*p;
9447c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*eth;
9457c478bd9Sstevel@tonic-gate 	itm_escapeseq_hdr_t	*eh;
9467c478bd9Sstevel@tonic-gate 	itm_data_t		*d;
9477c478bd9Sstevel@tonic-gate 	const unsigned char	*ip;
9487c478bd9Sstevel@tonic-gate 	size_t			ileft;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	retval = 0;
9517c478bd9Sstevel@tonic-gate 	ip =	*inbuf;
9527c478bd9Sstevel@tonic-gate 	ileft = *inbytesleft;
9537c478bd9Sstevel@tonic-gate 	cond_hdr = ADDR(cond_place);
9547c478bd9Sstevel@tonic-gate 	cond = (itm_cond_t *)(cond_hdr + 1);
9557c478bd9Sstevel@tonic-gate 	for (i = 0; i < cond_hdr->number; i++, cond++) {
9567c478bd9Sstevel@tonic-gate 		switch (cond->type) {
9577c478bd9Sstevel@tonic-gate 		case ITM_COND_BETWEEN:
9587c478bd9Sstevel@tonic-gate 			rth = ADDR(cond->operand.place);
9597c478bd9Sstevel@tonic-gate 			rtsh = (itm_range_hdr_t *)(rth + 1);
9607c478bd9Sstevel@tonic-gate 			if (ileft < rtsh->len) {
9617c478bd9Sstevel@tonic-gate 				errno = EINVAL;
962*85bb5f1dSis 				TRACE_MESSAGE('e', ("eval_cond_tbl:error=%d\n",
963*85bb5f1dSis 				    errno));
9647c478bd9Sstevel@tonic-gate 				retval = ((size_t)(-1));
9657c478bd9Sstevel@tonic-gate 				goto eval_cond_return;
9667c478bd9Sstevel@tonic-gate 			}
9677c478bd9Sstevel@tonic-gate 			p = (unsigned char *)(rtsh + 1);
9687c478bd9Sstevel@tonic-gate 			retval = 0;
9697c478bd9Sstevel@tonic-gate 			for (j = 0; j < rth->number;
9707c478bd9Sstevel@tonic-gate 			    j++,  p = (void *)(p + (2 * rtsh->len))) {
9717c478bd9Sstevel@tonic-gate 				retval = 1;
9727c478bd9Sstevel@tonic-gate 				for (k = 0; k < rtsh->len; k++) {
9737c478bd9Sstevel@tonic-gate 					if ((*(ip + k) < *(p + k)) ||
9747c478bd9Sstevel@tonic-gate 					    (*(p + rtsh->len + k) <
9757c478bd9Sstevel@tonic-gate 					    *(ip + k))) {
9767c478bd9Sstevel@tonic-gate 						retval = 0;
9777c478bd9Sstevel@tonic-gate 						break;
9787c478bd9Sstevel@tonic-gate 					}
9797c478bd9Sstevel@tonic-gate 				}
9807c478bd9Sstevel@tonic-gate 				if (1 == retval) {
9817c478bd9Sstevel@tonic-gate 					break;
9827c478bd9Sstevel@tonic-gate 				}
9837c478bd9Sstevel@tonic-gate 			}
9847c478bd9Sstevel@tonic-gate 			if (0 == retval) {
9857c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('b',
9867c478bd9Sstevel@tonic-gate 				    ("out of between (%p) len= rtsh=%ld\n",
9877c478bd9Sstevel@tonic-gate 				    *ip, rtsh->len));
9887c478bd9Sstevel@tonic-gate 				goto eval_cond_return;
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 			break; /* continue */
9917c478bd9Sstevel@tonic-gate 		case ITM_COND_ESCAPESEQ:
9927c478bd9Sstevel@tonic-gate 			/*
9937c478bd9Sstevel@tonic-gate 			 * if escape sequence occur,
9947c478bd9Sstevel@tonic-gate 			 * change ist->default_action and return 2.
9957c478bd9Sstevel@tonic-gate 			 * never return 1.
9967c478bd9Sstevel@tonic-gate 			 */
9977c478bd9Sstevel@tonic-gate 			retval = 0;
9987c478bd9Sstevel@tonic-gate 			eth = ADDR(cond->operand.place);
9997c478bd9Sstevel@tonic-gate 			eh = (itm_escapeseq_hdr_t *)(eth + 1);
10007c478bd9Sstevel@tonic-gate 			if (NULL == ist->default_action.itm_ptr) {
10017c478bd9Sstevel@tonic-gate 				ist->default_action = direc->action;
10027c478bd9Sstevel@tonic-gate 				TRACE_MESSAGE('E',
10037c478bd9Sstevel@tonic-gate 				    ("escape seq (default action=%6p, "
10047c478bd9Sstevel@tonic-gate 				    "type=%ld) set\n",
1005*85bb5f1dSis 				    direc->action.itm_ptr, ((itm_tbl_hdr_t *)
1006*85bb5f1dSis 				    (ADDR(direc->action)))->type));
10077c478bd9Sstevel@tonic-gate 			}
10087c478bd9Sstevel@tonic-gate 			retval = 0;
10097c478bd9Sstevel@tonic-gate 			if (*inbytesleft < eh->len_min) {
10107c478bd9Sstevel@tonic-gate 				break;
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 			for (j = 0, d = (itm_data_t *)(eh + 1);
10137c478bd9Sstevel@tonic-gate 			    j < eth->number;
10147c478bd9Sstevel@tonic-gate 			    j++, d++) {
10157c478bd9Sstevel@tonic-gate 				if (*inbytesleft < d->size) {
10167c478bd9Sstevel@tonic-gate 					continue;
10177c478bd9Sstevel@tonic-gate 				}
10187c478bd9Sstevel@tonic-gate 				if (0 == memcmp(*inbuf, DADDR(d), d->size)) {
10197c478bd9Sstevel@tonic-gate 					TRACE_MESSAGE('E',
10207c478bd9Sstevel@tonic-gate 					    ("escape seq: discard=%ld chars\n",
10217c478bd9Sstevel@tonic-gate 					    d->size));
1022*85bb5f1dSis 					TRACE_MESSAGE('E',
1023*85bb5f1dSis 					    ("escape seq (default "
1024*85bb5f1dSis 					    "action=%6p, type=%ld) set\n",
10257c478bd9Sstevel@tonic-gate 					    direc->action.itm_ptr,
10267c478bd9Sstevel@tonic-gate 					    ((itm_tbl_hdr_t *)
10277c478bd9Sstevel@tonic-gate 					    (ADDR(direc->action)))->type));
10287c478bd9Sstevel@tonic-gate 					ist->default_action = direc->action;
10297c478bd9Sstevel@tonic-gate 					DISCARD(d->size);
10307c478bd9Sstevel@tonic-gate 					retval = 2;
10317c478bd9Sstevel@tonic-gate 					goto eval_cond_return;
10327c478bd9Sstevel@tonic-gate 				}
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 			if (0 == retval) {
10357c478bd9Sstevel@tonic-gate 				goto eval_cond_return;
10367c478bd9Sstevel@tonic-gate 			}
10377c478bd9Sstevel@tonic-gate 			break; /* continue */
10387c478bd9Sstevel@tonic-gate 		case ITM_COND_EXPR:
10397c478bd9Sstevel@tonic-gate 			retval = eval_expr(ist, cond->operand.place,
10407c478bd9Sstevel@tonic-gate 			    *inbytesleft, ip, outbytesleft);
10417c478bd9Sstevel@tonic-gate 			if (0 == retval) {
10427c478bd9Sstevel@tonic-gate 				goto eval_cond_return;
10437c478bd9Sstevel@tonic-gate 			} else {
10447c478bd9Sstevel@tonic-gate 				retval = 1;
10457c478bd9Sstevel@tonic-gate 			}
10467c478bd9Sstevel@tonic-gate 			break; /* continue */
10477c478bd9Sstevel@tonic-gate 		default:
10487c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_cond_tbl:illegal cond=%d\n",
10497c478bd9Sstevel@tonic-gate 			    cond->type));
10507c478bd9Sstevel@tonic-gate 			retval = (size_t)-1;
10517c478bd9Sstevel@tonic-gate 			goto eval_cond_return;
10527c478bd9Sstevel@tonic-gate 		}
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate eval_cond_return:
10567c478bd9Sstevel@tonic-gate 	return (retval);
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * Evaluate operation table
10617c478bd9Sstevel@tonic-gate  *
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate static size_t
eval_op_tbl(icv_state_t * ist,itm_place_t op_tbl_place,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft)10647c478bd9Sstevel@tonic-gate eval_op_tbl(
10657c478bd9Sstevel@tonic-gate 	icv_state_t	*ist,
10667c478bd9Sstevel@tonic-gate 	itm_place_t	op_tbl_place,
10677c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
10687c478bd9Sstevel@tonic-gate 	size_t		*inbytesleft,
10697c478bd9Sstevel@tonic-gate 	unsigned char	**outbuf,
10707c478bd9Sstevel@tonic-gate 	size_t		*outbytesleft)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t	*op_hdr;
10737c478bd9Sstevel@tonic-gate 	itm_op_t	*operation;
10747c478bd9Sstevel@tonic-gate 	itm_place2_t	op_place;
10757c478bd9Sstevel@tonic-gate 	size_t		retval;
10767c478bd9Sstevel@tonic-gate 	long		i;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	retval = 0;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
10817c478bd9Sstevel@tonic-gate 	if (OP_DEPTH_MAX <= ist->op_depth) {
10827c478bd9Sstevel@tonic-gate 		errno = ELIBBAD;
10837c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('e', ("eval_op_tbl:error=%d\n", errno));
10847c478bd9Sstevel@tonic-gate 		return	(RETVALERR);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 	ist->op_depth += 1;
10877c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	op_hdr = ADDR(op_tbl_place);
10907c478bd9Sstevel@tonic-gate 	operation = (itm_op_t *)(op_hdr + 1);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	op_place = op_tbl_place.itm_ptr + (sizeof (itm_tbl_hdr_t));
10937c478bd9Sstevel@tonic-gate 	for (i = 0; i < op_hdr->number; i++, operation++,
10947c478bd9Sstevel@tonic-gate 	    op_place += (sizeof (itm_op_t))) {
10957c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('O', ("eval_op_tbl: %ld %p\n", i, op_place));
1096*85bb5f1dSis 		retval = eval_op(ist, op_place, inbuf, inbytesleft,
10977c478bd9Sstevel@tonic-gate 		    outbuf, outbytesleft);
10987c478bd9Sstevel@tonic-gate 		if (((long)(retval)) < 0) {
10997c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
11007c478bd9Sstevel@tonic-gate 			ist->op_depth -= 1;
11017c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
11027c478bd9Sstevel@tonic-gate 			switch (retval) {
11037c478bd9Sstevel@tonic-gate 			case RETVALERR:
11047c478bd9Sstevel@tonic-gate 				return	(retval);
11057c478bd9Sstevel@tonic-gate 			case RETVALRET:
11067c478bd9Sstevel@tonic-gate 				if (0 == op_hdr->name.itm_ptr) {
11077c478bd9Sstevel@tonic-gate 					return	(RETVALRET);
11087c478bd9Sstevel@tonic-gate 				} else {
11097c478bd9Sstevel@tonic-gate 					return (0);
11107c478bd9Sstevel@tonic-gate 				}
11117c478bd9Sstevel@tonic-gate 			}
11127c478bd9Sstevel@tonic-gate 		}
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate #if defined(OP_DEPTH_MAX)
11157c478bd9Sstevel@tonic-gate 	ist->op_depth -= 1;
11167c478bd9Sstevel@tonic-gate #endif /* OP_DEPTH_MAX */
11177c478bd9Sstevel@tonic-gate 	return	(retval);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * Evaluate single operation
11237c478bd9Sstevel@tonic-gate  *
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate static size_t
eval_op(icv_state_t * ist,itm_place2_t op_place,const unsigned char ** inbuf,size_t * inbytesleft,unsigned char ** outbuf,size_t * outbytesleft)11267c478bd9Sstevel@tonic-gate eval_op(
11277c478bd9Sstevel@tonic-gate 	icv_state_t		*ist,
11287c478bd9Sstevel@tonic-gate 	itm_place2_t		op_place,
11297c478bd9Sstevel@tonic-gate 	const unsigned char	**inbuf,
11307c478bd9Sstevel@tonic-gate 	size_t			*inbytesleft,
11317c478bd9Sstevel@tonic-gate 	unsigned char		**outbuf,
11327c478bd9Sstevel@tonic-gate 	size_t			*outbytesleft)
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate 	size_t			retval;
11357c478bd9Sstevel@tonic-gate 	itm_num_t		num;
11367c478bd9Sstevel@tonic-gate 	itm_op_t		*operation;
11377c478bd9Sstevel@tonic-gate 	itm_expr_t		*expr;
11387c478bd9Sstevel@tonic-gate 	itm_num_t		c;
11397c478bd9Sstevel@tonic-gate 	itm_num_t		i;
11407c478bd9Sstevel@tonic-gate 	itm_size_t		z;
11417c478bd9Sstevel@tonic-gate 	unsigned char		*p;
11427c478bd9Sstevel@tonic-gate 	itm_expr_t		*expr0;
1143*85bb5f1dSis 	itm_tbl_hdr_t		*h;
1144*85bb5f1dSis 	itm_type_t		t;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate #define	EVAL_EXPR(n)							\
11477c478bd9Sstevel@tonic-gate 	(expr0 = ADDR(operation->data.operand[(n)]),			\
11487c478bd9Sstevel@tonic-gate 		(itm_num_t)((expr0->type == ITM_EXPR_INT) ?		\
11497c478bd9Sstevel@tonic-gate 		expr0->data.itm_exnum :					\
11507c478bd9Sstevel@tonic-gate 		((expr0->type == ITM_EXPR_REG) ?			\
11517c478bd9Sstevel@tonic-gate 		REG(expr0->data.itm_exnum) :				\
11527c478bd9Sstevel@tonic-gate 		((expr0->type == ITM_EXPR_IN_VECTOR_D) ?		\
11537c478bd9Sstevel@tonic-gate 		((expr0->data.itm_exnum < 0) ?				\
11547c478bd9Sstevel@tonic-gate 		(((-1) == expr0->data.itm_exnum) ? *inbytesleft : 0) :	\
11557c478bd9Sstevel@tonic-gate 		((expr0->data.itm_exnum < *inbytesleft) ?		\
11567c478bd9Sstevel@tonic-gate 		(*(uchar_t *)(*inbuf + expr0->data.itm_exnum)) : 0)):	\
11577c478bd9Sstevel@tonic-gate 		eval_expr(ist, operation->data.operand[(n)],		\
11587c478bd9Sstevel@tonic-gate 		*inbytesleft, *inbuf, *outbytesleft)))))
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	retval = 0;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	operation = (itm_op_t *)ADDR2(op_place);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	switch (operation->type) {
11657c478bd9Sstevel@tonic-gate 	case ITM_OP_EXPR:
11667c478bd9Sstevel@tonic-gate 		num = eval_expr(ist, operation->data.operand[0],
11677c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
11687c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_EXPR: %ld\n", retval));
11697c478bd9Sstevel@tonic-gate 		break;
11707c478bd9Sstevel@tonic-gate 	case ITM_OP_ERROR:
11717c478bd9Sstevel@tonic-gate 		num = eval_expr(ist, operation->data.operand[0],
11727c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
11737c478bd9Sstevel@tonic-gate 		errno = (int)num;
11747c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_ERROR: %ld\n", num));
11757c478bd9Sstevel@tonic-gate 		retval = (size_t)(-1);
11767c478bd9Sstevel@tonic-gate 		break;
11777c478bd9Sstevel@tonic-gate 	case ITM_OP_ERROR_D:
11787c478bd9Sstevel@tonic-gate 		errno = (int)operation->data.itm_opnum;
11797c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_ERROR_D: %d\n", errno));
11807c478bd9Sstevel@tonic-gate 		retval = (size_t)(-1);
11817c478bd9Sstevel@tonic-gate 		break;
11827c478bd9Sstevel@tonic-gate 	case ITM_OP_OUT:
11837c478bd9Sstevel@tonic-gate 		expr = ADDR(operation->data.operand[0]);
11847c478bd9Sstevel@tonic-gate 		if ((*outbytesleft) == 0) {
11857c478bd9Sstevel@tonic-gate 			errno = E2BIG;
11867c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
11877c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 		c = eval_expr(ist, operation->data.operand[0],
11907c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
11917c478bd9Sstevel@tonic-gate 		PUT((uchar_t)c);
11927c478bd9Sstevel@tonic-gate 		retval = *inbytesleft;
11937c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_OUT: %ld %ld\n", c, *inbytesleft));
11947c478bd9Sstevel@tonic-gate 		break;
11957c478bd9Sstevel@tonic-gate 	case ITM_OP_OUT_D:
11967c478bd9Sstevel@tonic-gate 		expr = ADDR(operation->data.operand[0]);
11977c478bd9Sstevel@tonic-gate 		if ((*outbytesleft) == 0) {
11987c478bd9Sstevel@tonic-gate 			errno = E2BIG;
11997c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
12007c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		PUT(0xff & (expr->data.itm_exnum));
12037c478bd9Sstevel@tonic-gate 		break;
12047c478bd9Sstevel@tonic-gate 	case ITM_OP_OUT_S:
12057c478bd9Sstevel@tonic-gate 		expr = ADDR(operation->data.operand[0]);
12067c478bd9Sstevel@tonic-gate 		if ((*outbytesleft) == 0) {
12077c478bd9Sstevel@tonic-gate 			errno = E2BIG;
12087c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
12097c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 		z = expr->data.value.size;
12127c478bd9Sstevel@tonic-gate 		if (*outbytesleft < z) {
12137c478bd9Sstevel@tonic-gate 			errno = E2BIG;
12147c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
12157c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 		p = DADDR(&(expr->data.value));
12187c478bd9Sstevel@tonic-gate 		for (; 0 < z; --z, p++) {
12197c478bd9Sstevel@tonic-gate 			PUT(*p);
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 	case ITM_OP_OUT_R:
12237c478bd9Sstevel@tonic-gate 		expr = ADDR(operation->data.operand[0]);
12247c478bd9Sstevel@tonic-gate 		if ((*outbytesleft) == 0) {
12257c478bd9Sstevel@tonic-gate 			errno = E2BIG;
12267c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
12277c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		c = REG(expr->data.itm_exnum);
12307c478bd9Sstevel@tonic-gate 		PUT((uchar_t)c);
12317c478bd9Sstevel@tonic-gate 		break;
12327c478bd9Sstevel@tonic-gate 	case ITM_OP_OUT_INVD:
12337c478bd9Sstevel@tonic-gate 		expr = ADDR(operation->data.operand[0]);
12347c478bd9Sstevel@tonic-gate 		if ((*outbytesleft) == 0) {
12357c478bd9Sstevel@tonic-gate 			errno = E2BIG;
12367c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
12377c478bd9Sstevel@tonic-gate 			return ((size_t)(-1));
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		z = (((0 <= expr->data.itm_exnum) &&
12407c478bd9Sstevel@tonic-gate 		    (expr->data.itm_exnum < *inbytesleft)) ?
12417c478bd9Sstevel@tonic-gate 		    (*((unsigned char *)(*inbuf + expr->data.itm_exnum))) :
12427c478bd9Sstevel@tonic-gate 		    (((-1) == expr->data.itm_exnum) ? *inbytesleft : 0));
12437c478bd9Sstevel@tonic-gate 		PUT((uchar_t)z);
12447c478bd9Sstevel@tonic-gate 		break;
12457c478bd9Sstevel@tonic-gate 	case ITM_OP_DISCARD:
12467c478bd9Sstevel@tonic-gate #if defined(EVAL_EXPR)
12477c478bd9Sstevel@tonic-gate 		num = EVAL_EXPR(0);
12487c478bd9Sstevel@tonic-gate #else /* !defined(EVAL_EXPR) */
12497c478bd9Sstevel@tonic-gate 		num = eval_expr(ist, operation->data.operand[0],
12507c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
12517c478bd9Sstevel@tonic-gate #endif /* defined(EVAL_EXPR) */
12527c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_DISCARD: %ld\n", num));
12537c478bd9Sstevel@tonic-gate #if defined(DISCARD)
1254*85bb5f1dSis 		DISCARD((num <= *inbytesleft) ? ((ulong_t)num) : *inbytesleft);
12557c478bd9Sstevel@tonic-gate #else /* defined(DISCARD) */
12567c478bd9Sstevel@tonic-gate 		for (num = ((num <= *inbytesleft) ? num : *inbytesleft);
12577c478bd9Sstevel@tonic-gate 		    0 < num; --num) {
12587c478bd9Sstevel@tonic-gate 			GET(c);
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate #endif /* defined(DISCARD) */
12617c478bd9Sstevel@tonic-gate 		break;
12627c478bd9Sstevel@tonic-gate 	case ITM_OP_DISCARD_D:
12637c478bd9Sstevel@tonic-gate 		num = operation->data.itm_opnum;
12647c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_DISCARD_D: %ld\n", num));
12657c478bd9Sstevel@tonic-gate #if defined(DISCARD)
12667c478bd9Sstevel@tonic-gate 		DISCARD((num <= *inbytesleft) ? num : *inbytesleft);
12677c478bd9Sstevel@tonic-gate #else /* defined(DISCARD) */
12687c478bd9Sstevel@tonic-gate 		for (num = ((num <= *inbytesleft) ? num : *inbytesleft);
12697c478bd9Sstevel@tonic-gate 		    0 < num; --num) {
12707c478bd9Sstevel@tonic-gate 			GET(c);
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate #endif /* defined(DISCARD) */
12737c478bd9Sstevel@tonic-gate 		break;
12747c478bd9Sstevel@tonic-gate 	case ITM_OP_IF:
12757c478bd9Sstevel@tonic-gate 		c = eval_expr(ist, operation->data.operand[0],
12767c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
12777c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_IF: %ld\n", c));
12787c478bd9Sstevel@tonic-gate 		if (c) {
1279*85bb5f1dSis 			retval = eval_op_tbl(ist, operation->data.operand[1],
1280*85bb5f1dSis 			    inbuf, inbytesleft, outbuf, outbytesleft);
12817c478bd9Sstevel@tonic-gate 		}
12827c478bd9Sstevel@tonic-gate 		break;
12837c478bd9Sstevel@tonic-gate 	case ITM_OP_IF_ELSE:
12847c478bd9Sstevel@tonic-gate 		c = eval_expr(ist, operation->data.operand[0],
12857c478bd9Sstevel@tonic-gate 		    *inbytesleft, *inbuf, *outbytesleft);
12867c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_IF_ELSE: %ld\n", c));
12877c478bd9Sstevel@tonic-gate 		if (c) {
1288*85bb5f1dSis 			retval = eval_op_tbl(ist, operation->data.operand[1],
1289*85bb5f1dSis 			    inbuf, inbytesleft, outbuf, outbytesleft);
12907c478bd9Sstevel@tonic-gate 		} else {
1291*85bb5f1dSis 			retval = eval_op_tbl(ist, operation->data.operand[2],
1292*85bb5f1dSis 			    inbuf, inbytesleft, outbuf, outbytesleft);
12937c478bd9Sstevel@tonic-gate 		}
12947c478bd9Sstevel@tonic-gate 		break;
12957c478bd9Sstevel@tonic-gate 	case ITM_OP_DIRECTION:
12967c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_DIRECTION: %p\n",
12977c478bd9Sstevel@tonic-gate 		    operation->data.operand[0].itm_ptr));
12987c478bd9Sstevel@tonic-gate 		ist->direc = ADDR(operation->data.operand[0]);
12997c478bd9Sstevel@tonic-gate 		return ((size_t)(-2));
13007c478bd9Sstevel@tonic-gate 	case ITM_OP_MAP:
13017c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_MAP: %p\n",
13027c478bd9Sstevel@tonic-gate 		    operation->data.operand[0].itm_ptr));
13037c478bd9Sstevel@tonic-gate 		i = 0;
13047c478bd9Sstevel@tonic-gate 		if (0 != operation->data.operand[1].itm_ptr) {
13057c478bd9Sstevel@tonic-gate #if defined(EVAL_EXPR)
13067c478bd9Sstevel@tonic-gate 			i = EVAL_EXPR(1);
13077c478bd9Sstevel@tonic-gate #else /* !defined(EVAL_EXPR) */
13087c478bd9Sstevel@tonic-gate 			i = eval_expr(ist, operation->data.operand[1],
13097c478bd9Sstevel@tonic-gate 			    *inbytesleft, *inbuf, *outbytesleft);
13107c478bd9Sstevel@tonic-gate #endif /* defined(EVAL_EXPR) */
13117c478bd9Sstevel@tonic-gate 			(*inbytesleft) -= i;
13127c478bd9Sstevel@tonic-gate 			(*inbuf) += i;
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 
1315*85bb5f1dSis 		/*
1316*85bb5f1dSis 		 * Based on what is the maptype, we call the corresponding
1317*85bb5f1dSis 		 * mapping function.
1318*85bb5f1dSis 		 */
1319*85bb5f1dSis 		h = ADDR(operation->data.operand[0]);
1320*85bb5f1dSis 		t = h->type;
1321*85bb5f1dSis 		switch (t) {
1322*85bb5f1dSis 		case ITM_TBL_MAP_INDEX_FIXED:
1323*85bb5f1dSis 		case ITM_TBL_MAP_INDEX_FIXED_1_1:
1324*85bb5f1dSis 			retval = map_i_f(h, inbuf, inbytesleft,
13257c478bd9Sstevel@tonic-gate 			    outbuf, outbytesleft, 1);
1326*85bb5f1dSis 			break;
1327*85bb5f1dSis 		case ITM_TBL_MAP_HASH:
1328*85bb5f1dSis 			retval = map_h_l(h, inbuf, inbytesleft,
1329*85bb5f1dSis 			    outbuf, outbytesleft, 1);
1330*85bb5f1dSis 			break;
1331*85bb5f1dSis 		case ITM_TBL_MAP_DENSE_ENC:
1332*85bb5f1dSis 			retval = map_d_e_l(h, inbuf, inbytesleft,
1333*85bb5f1dSis 			    outbuf, outbytesleft, 1);
1334*85bb5f1dSis 			break;
1335*85bb5f1dSis 		case ITM_TBL_MAP_LOOKUP:
1336*85bb5f1dSis 			retval = map_l_f(h, inbuf, inbytesleft,
1337*85bb5f1dSis 			    outbuf, outbytesleft, 1);
1338*85bb5f1dSis 			break;
1339*85bb5f1dSis 		default:
1340*85bb5f1dSis 			/*
1341*85bb5f1dSis 			 * This should not be possible, but in case we
1342*85bb5f1dSis 			 * have an incorrect maptype, don't fall back to
1343*85bb5f1dSis 			 * map_i_f(). Instead, because it is an error, return
1344*85bb5f1dSis 			 * an error. See CR 6622765.
1345*85bb5f1dSis 			 */
1346*85bb5f1dSis 			errno = EBADF;
1347*85bb5f1dSis 			TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
1348*85bb5f1dSis 			retval = (size_t)-1;
1349*85bb5f1dSis 			break;
1350*85bb5f1dSis 		}
1351*85bb5f1dSis 
13527c478bd9Sstevel@tonic-gate 		if ((size_t)(-1) == retval) {
13537c478bd9Sstevel@tonic-gate 			(*outbytesleft) += i;
13547c478bd9Sstevel@tonic-gate 			(*outbuf) -= i;
13557c478bd9Sstevel@tonic-gate 		}
13567c478bd9Sstevel@tonic-gate 		break;
13577c478bd9Sstevel@tonic-gate 	case ITM_OP_OPERATION:
13587c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_OPERATION: %p\n",
13597c478bd9Sstevel@tonic-gate 		    operation->data.operand[0].itm_ptr));
1360*85bb5f1dSis 		retval = eval_op_tbl(ist, operation->data.operand[0],
1361*85bb5f1dSis 		    inbuf, inbytesleft, outbuf, outbytesleft);
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		break;
13647c478bd9Sstevel@tonic-gate 	case ITM_OP_INIT:
13657c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_INIT: %p\n",
13667c478bd9Sstevel@tonic-gate 		    ist->itm_hdr->op_init_tbl));
13677c478bd9Sstevel@tonic-gate 		if (0 != ist->itm_hdr->op_init_tbl.itm_ptr) {
1368*85bb5f1dSis 			retval = eval_op_tbl(ist, ist->itm_hdr->op_init_tbl,
1369*85bb5f1dSis 			    inbuf, inbytesleft, outbuf, outbytesleft);
13707c478bd9Sstevel@tonic-gate 		} else {
13717c478bd9Sstevel@tonic-gate 			op_init_default(ist);
13727c478bd9Sstevel@tonic-gate 			retval = (size_t)-2;
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 		break;
13757c478bd9Sstevel@tonic-gate 	case ITM_OP_RESET:
13767c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_RESET: %p\n",
13777c478bd9Sstevel@tonic-gate 		    ist->itm_hdr->op_reset_tbl));
13787c478bd9Sstevel@tonic-gate 		if (0 != ist->itm_hdr->op_reset_tbl.itm_ptr) {
1379*85bb5f1dSis 			retval = eval_op_tbl(ist, ist->itm_hdr->op_reset_tbl,
1380*85bb5f1dSis 			    inbuf, inbytesleft, outbuf, outbytesleft);
13817c478bd9Sstevel@tonic-gate 		} else {
13827c478bd9Sstevel@tonic-gate 			op_reset_default(ist);
13837c478bd9Sstevel@tonic-gate 			retval = (size_t)-2;
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 		break;
13867c478bd9Sstevel@tonic-gate 	case ITM_OP_BREAK:
13877c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_BREAK\n"));
13887c478bd9Sstevel@tonic-gate 		return	(RETVALBRK);
13897c478bd9Sstevel@tonic-gate 	case ITM_OP_RETURN:
13907c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('o', ("ITM_OP_RETURN\n"));
13917c478bd9Sstevel@tonic-gate 		return	(RETVALRET);
13927c478bd9Sstevel@tonic-gate 	case ITM_OP_PRINTCHR:
1393*85bb5f1dSis 		c = eval_expr(ist, operation->data.operand[0], *inbytesleft,
1394*85bb5f1dSis 		    *inbuf, *outbytesleft);
13957c478bd9Sstevel@tonic-gate 		(void) fputc((uchar_t)c, stderr);
1396*85bb5f1dSis 		TRACE_MESSAGE('o', ("ITM_OP_PRINTCHR: %ld %ld\n",
1397*85bb5f1dSis 		    c, *inbytesleft));
13987c478bd9Sstevel@tonic-gate 		break;
13997c478bd9Sstevel@tonic-gate 	case ITM_OP_PRINTHD:
1400*85bb5f1dSis 		c = eval_expr(ist, operation->data.operand[0], *inbytesleft,
1401*85bb5f1dSis 		    *inbuf, *outbytesleft);
14027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%lx", c);
1403*85bb5f1dSis 		TRACE_MESSAGE('o', ("ITM_OP_PRINTHD: %ld %ld\n",
1404*85bb5f1dSis 		    c, *inbytesleft));
14057c478bd9Sstevel@tonic-gate 		break;
14067c478bd9Sstevel@tonic-gate 	case ITM_OP_PRINTINT:
1407*85bb5f1dSis 		c = eval_expr(ist, operation->data.operand[0], *inbytesleft,
1408*85bb5f1dSis 		    *inbuf, *outbytesleft);
14097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%ld", c);
1410*85bb5f1dSis 		TRACE_MESSAGE('o', ("ITM_OP_PRINTINT: %ld %ld\n",
1411*85bb5f1dSis 		    c, *inbytesleft));
14127c478bd9Sstevel@tonic-gate 		break;
14137c478bd9Sstevel@tonic-gate 	default: /* never */
14147c478bd9Sstevel@tonic-gate 		errno = ELIBBAD;
14157c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('e', ("eval_op:error=%d\n", errno));
14167c478bd9Sstevel@tonic-gate 		return (size_t)(-1);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 	return	(retval);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate #undef EVAL_EXPR
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*
14257c478bd9Sstevel@tonic-gate  * Evaluate expression
14267c478bd9Sstevel@tonic-gate  */
14277c478bd9Sstevel@tonic-gate static itm_num_t
eval_expr(icv_state_t * ist,itm_place_t expr_place,size_t inbytesleft,const unsigned char * inbuf,size_t outbytesleft)14287c478bd9Sstevel@tonic-gate eval_expr(
14297c478bd9Sstevel@tonic-gate 	icv_state_t		*ist,
14307c478bd9Sstevel@tonic-gate 	itm_place_t		expr_place,
14317c478bd9Sstevel@tonic-gate 	size_t			inbytesleft,
14327c478bd9Sstevel@tonic-gate 	const unsigned char	*inbuf,
14337c478bd9Sstevel@tonic-gate 	size_t			outbytesleft)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	itm_expr_t		*expr;
14367c478bd9Sstevel@tonic-gate 	itm_expr_t		*expr_op;
14377c478bd9Sstevel@tonic-gate 	itm_num_t		num;
14387c478bd9Sstevel@tonic-gate 	unsigned char		*p;
14397c478bd9Sstevel@tonic-gate 	long			i;
14407c478bd9Sstevel@tonic-gate 	itm_expr_t		*expr0;
14417c478bd9Sstevel@tonic-gate 	itm_num_t		num00;
14427c478bd9Sstevel@tonic-gate 	itm_num_t		num01;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate #define	EVAL_EXPR_E(n) (eval_expr(ist, expr->data.operand[(n)],		\
14457c478bd9Sstevel@tonic-gate 	inbytesleft, inbuf, outbytesleft))
14467c478bd9Sstevel@tonic-gate #define	EVAL_EXPR_D(n)	((itm_num_t)(expr->data.operand[(n)].itm_ptr))
14477c478bd9Sstevel@tonic-gate #define	EVAL_EXPR_R(n)	(REG((itm_num_t)(expr->data.operand[(n)].itm_ptr)))
14487c478bd9Sstevel@tonic-gate #define	EVAL_EXPR_INVD(n)						\
14497c478bd9Sstevel@tonic-gate 	((num0 ## n) = ((itm_num_t)(expr->data.operand[(n)].itm_ptr)),	\
14507c478bd9Sstevel@tonic-gate 		((num0 ## n) < 0) ?					\
14517c478bd9Sstevel@tonic-gate 		(((-1) == (num0 ## n)) ? inbytesleft : 0) :		\
14527c478bd9Sstevel@tonic-gate 		(((num0 ## n) < inbytesleft) ?				\
14537c478bd9Sstevel@tonic-gate 		(*(unsigned char *)(inbuf + (num0 ## n))) : 0))
14547c478bd9Sstevel@tonic-gate #define	EVAL_EXPR(n)							\
14557c478bd9Sstevel@tonic-gate 	(expr0 = ADDR(expr->data.operand[(n)]),				\
14567c478bd9Sstevel@tonic-gate 		(itm_num_t)((expr0->type == ITM_EXPR_INT) ?		\
14577c478bd9Sstevel@tonic-gate 		expr0->data.itm_exnum :					\
14587c478bd9Sstevel@tonic-gate 		((expr0->type == ITM_EXPR_REG) ?			\
14597c478bd9Sstevel@tonic-gate 		REG(expr0->data.itm_exnum) :				\
14607c478bd9Sstevel@tonic-gate 		((expr0->type == ITM_EXPR_IN_VECTOR_D) ?		\
14617c478bd9Sstevel@tonic-gate 		((expr0->data.itm_exnum < 0) ?				\
14627c478bd9Sstevel@tonic-gate 		(((-1) == expr0->data.itm_exnum) ? inbytesleft : 0) :	\
14637c478bd9Sstevel@tonic-gate 		((expr0->data.itm_exnum < inbytesleft) ?		\
14647c478bd9Sstevel@tonic-gate 		(*(uchar_t *)(inbuf + expr0->data.itm_exnum)) : 0)) :	\
14657c478bd9Sstevel@tonic-gate 		eval_expr(ist, expr->data.operand[(n)],			\
14667c478bd9Sstevel@tonic-gate 		inbytesleft, inbuf, outbytesleft)))))
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTO(op, name, name0, name1)			\
14697c478bd9Sstevel@tonic-gate 	case ITM_EXPR_##name##_##name0##_##name1:			\
14707c478bd9Sstevel@tonic-gate 		return (EVAL_EXPR_##name0(0) op EVAL_EXPR_##name1(1));
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN1(op, name)					\
14737c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, E, E)		\
14747c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, E, D)		\
14757c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, E, R)		\
14767c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, E, INVD)
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN2(op, name)					\
14797c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, D, E)		\
14807c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, D, D)		\
14817c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, D, R)		\
14827c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, D, INVD)
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN3(op, name)					\
14857c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, R, E)		\
14867c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, R, D)		\
14877c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, R, R)		\
14887c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, R, INVD)
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN4(op, name)					\
14917c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, INVD, E)		\
14927c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, INVD, D)		\
14937c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, INVD, R)		\
14947c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTO(op, name, INVD, INVD)
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTECT_PROTO(op, name, name0, name1)	\
14977c478bd9Sstevel@tonic-gate 	case ITM_EXPR_##name##_##name0##_##name1:		\
14987c478bd9Sstevel@tonic-gate 		num = EVAL_EXPR_##name1(1);			\
14997c478bd9Sstevel@tonic-gate 		if (0 != num) {					\
15007c478bd9Sstevel@tonic-gate 			return (EVAL_EXPR_##name0(0) op num);	\
15017c478bd9Sstevel@tonic-gate 		} else {					\
15027c478bd9Sstevel@tonic-gate 			return (0);				\
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTECT1(op, name)				\
15067c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, E, E)	\
15077c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, E, D)	\
15087c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, E, R)	\
15097c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, E, INVD)
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTECT2(op, name)				\
15127c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, D, E)	\
15137c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, D, D)	\
15147c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, D, R)	\
15157c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, D, INVD)
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTECT3(op, name)				\
15187c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, R, E)	\
15197c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, R, D)	\
15207c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, R, R)	\
15217c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, R, INVD)
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate #define	EVAL_OP_BIN_PROTECT4(op, name)				\
15247c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, INVD, E)	\
15257c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, INVD, D)	\
15267c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, INVD, R)	\
15277c478bd9Sstevel@tonic-gate 		EVAL_OP_BIN_PROTECT_PROTO(op, name, INVD, INVD)
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	expr = ADDR(expr_place);
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	switch (expr->type) {
15327c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NONE:		/* not used */
15337c478bd9Sstevel@tonic-gate 		return (0);
15347c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NOP:		/* not used */
15357c478bd9Sstevel@tonic-gate 		return (0);
15367c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NAME:		/* not used */
15377c478bd9Sstevel@tonic-gate 		return (0);
15387c478bd9Sstevel@tonic-gate 	case ITM_EXPR_INT:		/* integer */
15397c478bd9Sstevel@tonic-gate 		return (expr->data.itm_exnum);
15407c478bd9Sstevel@tonic-gate 	case ITM_EXPR_SEQ:		/* byte sequence */
15417c478bd9Sstevel@tonic-gate 		if ((sizeof (itm_place_t)) < expr->data.value.size) {
15427c478bd9Sstevel@tonic-gate 			p = (unsigned char *)ADDR(expr->data.value.place);
15437c478bd9Sstevel@tonic-gate 		} else {
15447c478bd9Sstevel@tonic-gate 			p = (unsigned char *)&(expr->data.value.place);
15457c478bd9Sstevel@tonic-gate 		}
15467c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0; i < expr->data.value.size; i++, p++) {
15477c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
15487c478bd9Sstevel@tonic-gate 		}
15497c478bd9Sstevel@tonic-gate 		return	(num);
15507c478bd9Sstevel@tonic-gate 	case ITM_EXPR_REG:		/* register */
15517c478bd9Sstevel@tonic-gate 		return (REG(expr->data.itm_exnum));
15527c478bd9Sstevel@tonic-gate 	case ITM_EXPR_IN_VECTOR:	/* in[expr] */
15537c478bd9Sstevel@tonic-gate 		num = EVAL_EXPR(0);
15547c478bd9Sstevel@tonic-gate 		if ((0 <= num) && (num < inbytesleft)) {
15557c478bd9Sstevel@tonic-gate 			return (*((unsigned char *)(inbuf + num)));
15567c478bd9Sstevel@tonic-gate 		} else if ((-1) == num) {
15577c478bd9Sstevel@tonic-gate 			return	(inbytesleft);
15587c478bd9Sstevel@tonic-gate 		} else {
15597c478bd9Sstevel@tonic-gate 			return (0);
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 	case ITM_EXPR_IN_VECTOR_D:	/* in[DECIMAL] */
15627c478bd9Sstevel@tonic-gate 		num = expr->data.itm_exnum;
15637c478bd9Sstevel@tonic-gate 		if ((0 <= num) && (num < inbytesleft)) {
15647c478bd9Sstevel@tonic-gate 			return (*((unsigned char *)(inbuf + num)));
15657c478bd9Sstevel@tonic-gate 		} else if ((-1) == num) {
15667c478bd9Sstevel@tonic-gate 			return	(inbytesleft);
15677c478bd9Sstevel@tonic-gate 		} else {
15687c478bd9Sstevel@tonic-gate 			return (0);
15697c478bd9Sstevel@tonic-gate 		}
15707c478bd9Sstevel@tonic-gate 	case ITM_EXPR_OUT:		/* out */
15717c478bd9Sstevel@tonic-gate 		return	(outbytesleft);
15727c478bd9Sstevel@tonic-gate 	case ITM_EXPR_TRUE:		/* true */
15737c478bd9Sstevel@tonic-gate 		return (1);
15747c478bd9Sstevel@tonic-gate 	case ITM_EXPR_FALSE:		/* false */
15757c478bd9Sstevel@tonic-gate 		return (0);
15767c478bd9Sstevel@tonic-gate 	case ITM_EXPR_UMINUS:		/* unary minus */
15777c478bd9Sstevel@tonic-gate 		return ((-1) * EVAL_EXPR(0));
15787c478bd9Sstevel@tonic-gate #define	PLUS_FOR_CSTYLE_CLEAN +
15797c478bd9Sstevel@tonic-gate #define	MINUS_FOR_CSTYLE_CLEAN -
15807c478bd9Sstevel@tonic-gate #define	MUL_FOR_CSTYLE_CLEAN *
15817c478bd9Sstevel@tonic-gate #define	DIV_FOR_CSTYLE_CLEAN /
15827c478bd9Sstevel@tonic-gate #define	MOD_FOR_CSTYLE_CLEAN %
15837c478bd9Sstevel@tonic-gate #define	SHIFT_L_FOR_CSTYLE_CLEAN <<
15847c478bd9Sstevel@tonic-gate #define	SHIFT_R_FOR_CSTYLE_CLEAN >>
15857c478bd9Sstevel@tonic-gate #define	OR_FOR_CSTYLE_CLEAN |
15867c478bd9Sstevel@tonic-gate #define	XOR_FOR_CSTYLE_CLEAN ^
15877c478bd9Sstevel@tonic-gate #define	AND_FOR_CSTYLE_CLEAN &
15887c478bd9Sstevel@tonic-gate #define	EQ_FOR_CSTYLE_CLEAN ==
15897c478bd9Sstevel@tonic-gate #define	NE_FOR_CSTYLE_CLEAN !=
15907c478bd9Sstevel@tonic-gate #define	GT_FOR_CSTYLE_CLEAN >
15917c478bd9Sstevel@tonic-gate #define	GE_FOR_CSTYLE_CLEAN >=
15927c478bd9Sstevel@tonic-gate #define	LT_FOR_CSTYLE_CLEAN <
15937c478bd9Sstevel@tonic-gate #define	LE_FOR_CSTYLE_CLEAN <=
15947c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(PLUS_FOR_CSTYLE_CLEAN, PLUS)	/* A + B */
15957c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(PLUS_FOR_CSTYLE_CLEAN, PLUS)	/* A + B */
15967c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(PLUS_FOR_CSTYLE_CLEAN, PLUS)	/* A + B */
15977c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(PLUS_FOR_CSTYLE_CLEAN, PLUS)	/* A + B */
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(MINUS_FOR_CSTYLE_CLEAN, MINUS)	/* A - B */
16007c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(MINUS_FOR_CSTYLE_CLEAN, MINUS)	/* A - B */
16017c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(MINUS_FOR_CSTYLE_CLEAN, MINUS)	/* A - B */
16027c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(MINUS_FOR_CSTYLE_CLEAN, MINUS)	/* A - B */
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(MUL_FOR_CSTYLE_CLEAN, MUL)		/* A * B */
16057c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(MUL_FOR_CSTYLE_CLEAN, MUL)		/* A * B */
16067c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(MUL_FOR_CSTYLE_CLEAN, MUL)		/* A * B */
16077c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(MUL_FOR_CSTYLE_CLEAN, MUL)		/* A * B */
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT1(DIV_FOR_CSTYLE_CLEAN, DIV)	/* A / B */
16107c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT2(DIV_FOR_CSTYLE_CLEAN, DIV)	/* A / B */
16117c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT3(DIV_FOR_CSTYLE_CLEAN, DIV)	/* A / B */
16127c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT4(DIV_FOR_CSTYLE_CLEAN, DIV)	/* A / B */
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT1(MOD_FOR_CSTYLE_CLEAN, MOD)	/* A % B */
16157c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT2(MOD_FOR_CSTYLE_CLEAN, MOD)	/* A % B */
16167c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT3(MOD_FOR_CSTYLE_CLEAN, MOD)	/* A % B */
16177c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN_PROTECT4(MOD_FOR_CSTYLE_CLEAN, MOD)	/* A % B */
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(SHIFT_L_FOR_CSTYLE_CLEAN, SHIFT_L)	/* A << B */
16207c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(SHIFT_L_FOR_CSTYLE_CLEAN, SHIFT_L)	/* A << B */
16217c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(SHIFT_L_FOR_CSTYLE_CLEAN, SHIFT_L)	/* A << B */
16227c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(SHIFT_L_FOR_CSTYLE_CLEAN, SHIFT_L)	/* A << B */
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(SHIFT_R_FOR_CSTYLE_CLEAN, SHIFT_R)	/* A >> B */
16257c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(SHIFT_R_FOR_CSTYLE_CLEAN, SHIFT_R)	/* A >> B */
16267c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(SHIFT_R_FOR_CSTYLE_CLEAN, SHIFT_R)	/* A >> B */
16277c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(SHIFT_R_FOR_CSTYLE_CLEAN, SHIFT_R)	/* A >> B */
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(OR_FOR_CSTYLE_CLEAN, OR)		/* A |	B */
16307c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(OR_FOR_CSTYLE_CLEAN, OR)		/* A |	B */
16317c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(OR_FOR_CSTYLE_CLEAN, OR)		/* A |	B */
16327c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(OR_FOR_CSTYLE_CLEAN, OR)		/* A |	B */
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(XOR_FOR_CSTYLE_CLEAN, XOR)		/* A ^	B */
16357c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(XOR_FOR_CSTYLE_CLEAN, XOR)		/* A ^	B */
16367c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(XOR_FOR_CSTYLE_CLEAN, XOR)		/* A ^	B */
16377c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(XOR_FOR_CSTYLE_CLEAN, XOR)		/* A ^	B */
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(AND_FOR_CSTYLE_CLEAN, AND)		/* A &	B */
16407c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(AND_FOR_CSTYLE_CLEAN, AND)		/* A &	B */
16417c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(AND_FOR_CSTYLE_CLEAN, AND)		/* A &	B */
16427c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(AND_FOR_CSTYLE_CLEAN, AND)		/* A &	B */
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(EQ_FOR_CSTYLE_CLEAN, EQ)		/* A == B */
16457c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(EQ_FOR_CSTYLE_CLEAN, EQ)		/* A == B */
16467c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(EQ_FOR_CSTYLE_CLEAN, EQ)		/* A == B */
16477c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(EQ_FOR_CSTYLE_CLEAN, EQ)		/* A == B */
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(NE_FOR_CSTYLE_CLEAN, NE)		/* A != B */
16507c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(NE_FOR_CSTYLE_CLEAN, NE)		/* A != B */
16517c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(NE_FOR_CSTYLE_CLEAN, NE)		/* A != B */
16527c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(NE_FOR_CSTYLE_CLEAN, NE)		/* A != B */
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(GT_FOR_CSTYLE_CLEAN, GT)		/* A >	B */
16557c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(GT_FOR_CSTYLE_CLEAN, GT)		/* A >	B */
16567c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(GT_FOR_CSTYLE_CLEAN, GT)		/* A >	B */
16577c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(GT_FOR_CSTYLE_CLEAN, GT)		/* A >	B */
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(GE_FOR_CSTYLE_CLEAN, GE)		/* A >= B */
16607c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(GE_FOR_CSTYLE_CLEAN, GE)		/* A >= B */
16617c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(GE_FOR_CSTYLE_CLEAN, GE)		/* A >= B */
16627c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(GE_FOR_CSTYLE_CLEAN, GE)		/* A >= B */
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(LT_FOR_CSTYLE_CLEAN, LT)		/* A <	B */
16657c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(LT_FOR_CSTYLE_CLEAN, LT)		/* A <	B */
16667c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(LT_FOR_CSTYLE_CLEAN, LT)		/* A <	B */
16677c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(LT_FOR_CSTYLE_CLEAN, LT)		/* A <	B */
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN1(LE_FOR_CSTYLE_CLEAN, LE)		/* A <= B */
16707c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN2(LE_FOR_CSTYLE_CLEAN, LE)		/* A <= B */
16717c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN3(LE_FOR_CSTYLE_CLEAN, LE)		/* A <= B */
16727c478bd9Sstevel@tonic-gate 	EVAL_OP_BIN4(LE_FOR_CSTYLE_CLEAN, LE)		/* A <= B */
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NOT:		/*   !A	  */
16757c478bd9Sstevel@tonic-gate 		return (!(EVAL_EXPR(0)));
16767c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NEG:		/*   ~A	  */
16777c478bd9Sstevel@tonic-gate 		return (~(EVAL_EXPR(0)));
16787c478bd9Sstevel@tonic-gate 	case ITM_EXPR_LOR:		/* A || B */
16797c478bd9Sstevel@tonic-gate 		if (0 != (num = EVAL_EXPR(0)))
16807c478bd9Sstevel@tonic-gate 			return	(num);
16817c478bd9Sstevel@tonic-gate 		if (0 != (num = EVAL_EXPR(1)))
16827c478bd9Sstevel@tonic-gate 			return	(num);
16837c478bd9Sstevel@tonic-gate 		return (0);
16847c478bd9Sstevel@tonic-gate 	case ITM_EXPR_LAND:		/* A && B */
16857c478bd9Sstevel@tonic-gate 		if (0 == EVAL_EXPR(0))
16867c478bd9Sstevel@tonic-gate 			return (0);
16877c478bd9Sstevel@tonic-gate 		if (0 == (num = EVAL_EXPR(1)))
16887c478bd9Sstevel@tonic-gate 			return (0);
16897c478bd9Sstevel@tonic-gate 		return	(num);
16907c478bd9Sstevel@tonic-gate 	case ITM_EXPR_ASSIGN:		/* A  = B */
16917c478bd9Sstevel@tonic-gate 		num = EVAL_EXPR(1);
16927c478bd9Sstevel@tonic-gate 		if (expr->data.operand[0].itm_ptr < ist->itm_hdr->reg_num) {
16937c478bd9Sstevel@tonic-gate 			return (*(ist->regs + expr->data.operand[0].itm_ptr)
16947c478bd9Sstevel@tonic-gate 			    = num);
16957c478bd9Sstevel@tonic-gate 		} else {
16967c478bd9Sstevel@tonic-gate 			return (0);
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 	case ITM_EXPR_IN_EQ:		/* in == A */
16997c478bd9Sstevel@tonic-gate 		expr_op = ADDR(expr->data.operand[0]);
17007c478bd9Sstevel@tonic-gate 		switch (expr_op->type) {
17017c478bd9Sstevel@tonic-gate 		case ITM_EXPR_SEQ:
17027c478bd9Sstevel@tonic-gate 			if (inbytesleft < expr_op->data.value.size) {
17037c478bd9Sstevel@tonic-gate 				return (0);
17047c478bd9Sstevel@tonic-gate 			}
17057c478bd9Sstevel@tonic-gate 			p = DADDR(&(expr_op->data.value));
17067c478bd9Sstevel@tonic-gate 			for (i = 0; i < expr_op->data.value.size; i++, p++) {
17077c478bd9Sstevel@tonic-gate 				if (*p != *(inbuf + i)) {
17087c478bd9Sstevel@tonic-gate 					return (0);
17097c478bd9Sstevel@tonic-gate 				}
17107c478bd9Sstevel@tonic-gate 			}
17117c478bd9Sstevel@tonic-gate 			return (1);
17127c478bd9Sstevel@tonic-gate 		default:
17137c478bd9Sstevel@tonic-gate 			num = EVAL_EXPR(0);
17147c478bd9Sstevel@tonic-gate 			return (num == *((unsigned char *)inbuf));
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 	default:
17177c478bd9Sstevel@tonic-gate 		break;
17187c478bd9Sstevel@tonic-gate 	}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	return (0);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate #undef EVAL_EXPR_E
17237c478bd9Sstevel@tonic-gate #undef EVAL_EXPR_D
17247c478bd9Sstevel@tonic-gate #undef EVAL_EXPR_R
17257c478bd9Sstevel@tonic-gate #undef EVAL_EXPR_INVD
17267c478bd9Sstevel@tonic-gate #undef EVAL_EXPR
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate /*
17317c478bd9Sstevel@tonic-gate  * maintain ITM reference information
17327c478bd9Sstevel@tonic-gate  */
17337c478bd9Sstevel@tonic-gate static void
itm_ref_free(int fd,void * ptr0,void * ptr1,void * ptr2,size_t len)17347c478bd9Sstevel@tonic-gate itm_ref_free(int fd, void *ptr0, void *ptr1, void *ptr2, size_t len)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	int	r;
17377c478bd9Sstevel@tonic-gate 	r = errno;
17387c478bd9Sstevel@tonic-gate 	if (0 <= fd) {
17397c478bd9Sstevel@tonic-gate 		(void) close(fd);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	free(ptr0);
17427c478bd9Sstevel@tonic-gate 	free(ptr1);
17437c478bd9Sstevel@tonic-gate 	if (0 < len) {
17447c478bd9Sstevel@tonic-gate 		(void) munmap(ptr2, len);
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 	errno = r;
17477c478bd9Sstevel@tonic-gate }
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate static itm_ref_t *
itm_ref_inc(const char * itm)17507c478bd9Sstevel@tonic-gate itm_ref_inc(const char		*itm)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	itm_ref_t	*ref;
17537c478bd9Sstevel@tonic-gate 	itm_hdr_t	*hdr;
17547c478bd9Sstevel@tonic-gate 	struct stat	st;
17557c478bd9Sstevel@tonic-gate 	int		fd;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	fd = open(itm, O_RDONLY, 0);
17587c478bd9Sstevel@tonic-gate 	if (fd == -1) {
17597c478bd9Sstevel@tonic-gate 		itm_ref_free(-1, NULL, NULL, NULL, 0);
17607c478bd9Sstevel@tonic-gate 		return	(NULL);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) == -1) {
17647c478bd9Sstevel@tonic-gate 		itm_ref_free(fd, NULL, NULL, NULL, 0);
17657c478bd9Sstevel@tonic-gate 		return	(NULL);
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 	hdr = (void *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
17687c478bd9Sstevel@tonic-gate 	if (MAP_FAILED == hdr) {
17697c478bd9Sstevel@tonic-gate 		itm_ref_free(fd, NULL, NULL, NULL, 0);
17707c478bd9Sstevel@tonic-gate 		return	(NULL);
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	(void) close(fd);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	ref = malloc(sizeof (itm_ref_t));
17767c478bd9Sstevel@tonic-gate 	if (NULL == ref) {
17777c478bd9Sstevel@tonic-gate 		itm_ref_free(-1, NULL, NULL, hdr, st.st_size);
17787c478bd9Sstevel@tonic-gate 		return	(NULL);
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 	ref->name = malloc(strlen(itm) + 1);
17817c478bd9Sstevel@tonic-gate 	if (NULL == ref->name) {
17827c478bd9Sstevel@tonic-gate 		itm_ref_free(-1, ref, NULL, hdr, st.st_size);
17837c478bd9Sstevel@tonic-gate 		return	(NULL);
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 	(void) strcpy(ref->name, itm);
17867c478bd9Sstevel@tonic-gate 	ref->hdr = hdr;
17877c478bd9Sstevel@tonic-gate 	ref->len = st.st_size;
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if ((hdr->ident[0] != ITM_IDENT_0) ||
17907c478bd9Sstevel@tonic-gate 	    (hdr->ident[1] != ITM_IDENT_1) ||
17917c478bd9Sstevel@tonic-gate 	    (hdr->ident[2] != ITM_IDENT_2) ||
17927c478bd9Sstevel@tonic-gate 	    (hdr->ident[3] != ITM_IDENT_3) ||
17937c478bd9Sstevel@tonic-gate 	    (hdr->spec[0] != ITM_SPEC_0) ||
17947c478bd9Sstevel@tonic-gate 	    (hdr->spec[1] != ITM_SPEC_1) ||
17957c478bd9Sstevel@tonic-gate 	    (hdr->spec[2] != ITM_SPEC_2) ||
17967c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
17977c478bd9Sstevel@tonic-gate #if defined(_LP64)
17987c478bd9Sstevel@tonic-gate 	    ((hdr->spec[3] != ITM_SPEC_3_32_LITTLE_ENDIAN) &&
17997c478bd9Sstevel@tonic-gate 	    (hdr->spec[3] != ITM_SPEC_3_64_LITTLE_ENDIAN)) ||
18007c478bd9Sstevel@tonic-gate #else
18017c478bd9Sstevel@tonic-gate 	    (hdr->spec[3] != ITM_SPEC_3_32_LITTLE_ENDIAN) ||
18027c478bd9Sstevel@tonic-gate #endif
18037c478bd9Sstevel@tonic-gate #else
18047c478bd9Sstevel@tonic-gate #if defined(_LP64)
18057c478bd9Sstevel@tonic-gate 	    ((hdr->spec[3] != ITM_SPEC_3_32_BIG_ENDIAN) &&
18067c478bd9Sstevel@tonic-gate 	    (hdr->spec[3] != ITM_SPEC_3_64_BIG_ENDIAN)) ||
18077c478bd9Sstevel@tonic-gate #else
18087c478bd9Sstevel@tonic-gate 	    (hdr->spec[3] != ITM_SPEC_3_32_BIG_ENDIAN) ||
18097c478bd9Sstevel@tonic-gate #endif
18107c478bd9Sstevel@tonic-gate #endif
18117c478bd9Sstevel@tonic-gate 	    (hdr->version[0] != ITM_VER_0) ||
18127c478bd9Sstevel@tonic-gate 	    (hdr->version[1] != ITM_VER_1) ||
18137c478bd9Sstevel@tonic-gate 	    (hdr->version[2] != ITM_VER_2) ||
18147c478bd9Sstevel@tonic-gate 	    (hdr->version[3] != ITM_VER_3) ||
18157c478bd9Sstevel@tonic-gate 	    (((size_t)(hdr->itm_size.itm_ptr)) != st.st_size)) {
1816*85bb5f1dSis 		itm_ref_free(-1, ref, ref->name, ref->hdr, ref->len);
18177c478bd9Sstevel@tonic-gate 		errno = ELIBBAD;
18187c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('e', ("itm_ref_inc:error=%d\n", errno));
18197c478bd9Sstevel@tonic-gate 		return	(NULL);
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	return	(ref);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate static void
itm_ref_dec(itm_ref_t * ref)18277c478bd9Sstevel@tonic-gate itm_ref_dec(itm_ref_t	*ref)
18287c478bd9Sstevel@tonic-gate {
18297c478bd9Sstevel@tonic-gate 	(void) munmap((char *)(ref->hdr), ref->len);
18307c478bd9Sstevel@tonic-gate 	free(ref->name);
18317c478bd9Sstevel@tonic-gate 	free(ref);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate static void
op_init_default(icv_state_t * ist)18367c478bd9Sstevel@tonic-gate op_init_default(icv_state_t	*ist)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	ist->direc = ADDR(ist->itm_hdr->direc_init_tbl);
18397c478bd9Sstevel@tonic-gate 	regs_init(ist);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate static void
op_reset_default(icv_state_t * ist)18447c478bd9Sstevel@tonic-gate op_reset_default(icv_state_t	*ist)
18457c478bd9Sstevel@tonic-gate {
18467c478bd9Sstevel@tonic-gate 	ist->direc = ADDR(ist->itm_hdr->direc_init_tbl);
18477c478bd9Sstevel@tonic-gate 	regs_init(ist);
18487c478bd9Sstevel@tonic-gate }
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate static void
regs_init(icv_state_t * ist)18527c478bd9Sstevel@tonic-gate regs_init(icv_state_t	*ist)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	if (0 < ist->itm_hdr->reg_num) {
18557c478bd9Sstevel@tonic-gate 		(void) memset(ist->regs, 0,
18567c478bd9Sstevel@tonic-gate 		    (sizeof (itm_num_t)) * ist->itm_hdr->reg_num);
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate #if defined(DEBUG)
18627c478bd9Sstevel@tonic-gate static void
trace_init()18637c478bd9Sstevel@tonic-gate trace_init()
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	char	*env_val;
18667c478bd9Sstevel@tonic-gate 	char	*p;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	env_val = getenv("ITM_INT_TRACE");
18697c478bd9Sstevel@tonic-gate 	if (NULL == env_val)
18707c478bd9Sstevel@tonic-gate 		return;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	for (p = env_val; *p; p++) {
18737c478bd9Sstevel@tonic-gate 		trace_option[(*p) & 0x007f] = 1;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate static void
trace_message(char * format,...)18787c478bd9Sstevel@tonic-gate trace_message(char	*format, ...)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate 	va_list	ap;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	va_start(ap, format);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	va_end(ap);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1889