xref: /titanic_50/usr/src/cmd/geniconvtbl/itm_util.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <assert.h>
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate #include <libintl.h>
33*7c478bd9Sstevel@tonic-gate #include <strings.h>
34*7c478bd9Sstevel@tonic-gate #include "iconv_tm.h"
35*7c478bd9Sstevel@tonic-gate #include "itmcomp.h"
36*7c478bd9Sstevel@tonic-gate #include "itm_util.h"
37*7c478bd9Sstevel@tonic-gate #include "hash.h"
38*7c478bd9Sstevel@tonic-gate #include "maptype.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static size_t	map_table_resultlen(itmc_map_t *);
42*7c478bd9Sstevel@tonic-gate static int	data_pair_compare(itmc_data_pair_t **, itmc_data_pair_t **);
43*7c478bd9Sstevel@tonic-gate static long	data_to_long(itm_data_t *);
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t	*map_table_indexed_fixed(itmc_data_pair_t **,
46*7c478bd9Sstevel@tonic-gate 				itm_size_t, itm_data_t *, long, itm_num_t);
47*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t	*map_table_dense_encoding(itmc_data_pair_t **,
48*7c478bd9Sstevel@tonic-gate 				itm_size_t, itm_data_t *, unsigned long,
49*7c478bd9Sstevel@tonic-gate 				unsigned char *, unsigned char *, long,
50*7c478bd9Sstevel@tonic-gate 				itm_num_t);
51*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t	*map_table_lookup_fixed(itmc_data_pair_t **,
52*7c478bd9Sstevel@tonic-gate 				itm_size_t, itm_data_t *, long, itm_size_t);
53*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t	*map_table_hash(itmc_data_pair_t **, itm_size_t,
54*7c478bd9Sstevel@tonic-gate 				itm_data_t *, long, long, itm_size_t,
55*7c478bd9Sstevel@tonic-gate 				itm_num_t);
56*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t	*map_table_lookup_var();
57*7c478bd9Sstevel@tonic-gate static void		put_dense_encoding_default(char *, unsigned char *,
58*7c478bd9Sstevel@tonic-gate 			unsigned char *, unsigned char *, long, long, long);
59*7c478bd9Sstevel@tonic-gate static size_t		map_table_resultlen(itmc_map_t *);
60*7c478bd9Sstevel@tonic-gate static void		map_range_adjust_byte_seq(unsigned char *,
61*7c478bd9Sstevel@tonic-gate 				unsigned char *, long, itmc_data_pair_t *);
62*7c478bd9Sstevel@tonic-gate static void		map_range_make_result(char *, itm_size_t, itm_size_t,
63*7c478bd9Sstevel@tonic-gate 				char *, itm_size_t);
64*7c478bd9Sstevel@tonic-gate static size_t		map_table_num_range(itmc_data_pair_t *);
65*7c478bd9Sstevel@tonic-gate static itmc_map_type_t	check_map_type(itmc_map_attr_t *);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate static itmc_name_t	*name_lookup(itm_data_t *, itm_type_t);
69*7c478bd9Sstevel@tonic-gate static itmc_name_t	*name_refer(itm_data_t *, itm_type_t, itmc_ref_t *);
70*7c478bd9Sstevel@tonic-gate static itmc_name_t	*name_register(itm_data_t *, itm_type_t, itmc_ref_t *);
71*7c478bd9Sstevel@tonic-gate static void		op_hirarchy(itm_tbl_hdr_t *, itmc_obj_t *);
72*7c478bd9Sstevel@tonic-gate static obj_array_t	obj_list_to_array(itm_size_t, itmc_obj_t *, itm_size_t);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate void
76*7c478bd9Sstevel@tonic-gate itm_def_process(itm_data_t	*itm_name)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	itm_hdr_t	*itm_hdr;
79*7c478bd9Sstevel@tonic-gate 	long		len;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('y', ("itm_def_process\n"));
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	itm_hdr = malloc_vital(sizeof (itm_hdr_t));
85*7c478bd9Sstevel@tonic-gate 	(void) memset(itm_hdr, 0, sizeof (itm_hdr_t));
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if ((NULL != cmd_opt.interpreter) &&
88*7c478bd9Sstevel@tonic-gate 	    (0 < (len = strlen(cmd_opt.interpreter)))) {
89*7c478bd9Sstevel@tonic-gate 		itm_hdr->interpreter = *(str_to_data(len, cmd_opt.interpreter));
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	if ((sizeof (itm_place_t)) < itm_hdr->interpreter.size) {
92*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_STRING, NULL,
93*7c478bd9Sstevel@tonic-gate 				(void *)itm_hdr->interpreter.place.itm_ptr,
94*7c478bd9Sstevel@tonic-gate 				itm_hdr->interpreter.size,
95*7c478bd9Sstevel@tonic-gate 				&(itm_hdr->interpreter.place),
96*7c478bd9Sstevel@tonic-gate 				OBJ_REG_HEAD);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	itm_hdr->type_id = *itm_name;
100*7c478bd9Sstevel@tonic-gate 	if ((sizeof (itm_place_t)) < itm_hdr->type_id.size) {
101*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_STRING, NULL,
102*7c478bd9Sstevel@tonic-gate 				(void *)itm_hdr->type_id.place.itm_ptr,
103*7c478bd9Sstevel@tonic-gate 				itm_hdr->type_id.size,
104*7c478bd9Sstevel@tonic-gate 				&(itm_hdr->type_id.place),
105*7c478bd9Sstevel@tonic-gate 				OBJ_REG_HEAD);
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	(void) assemble(itm_hdr);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate itmc_obj_t *
114*7c478bd9Sstevel@tonic-gate direction_unit(
115*7c478bd9Sstevel@tonic-gate 	itmc_ref_t	*cond,
116*7c478bd9Sstevel@tonic-gate 	itm_data_t	*cond_name,
117*7c478bd9Sstevel@tonic-gate 	itmc_action_t	*act,
118*7c478bd9Sstevel@tonic-gate 	itm_data_t	*act_name)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*du;
121*7c478bd9Sstevel@tonic-gate 	itm_direc_t	*direc;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	du = malloc_vital(sizeof (itmc_obj_t));
124*7c478bd9Sstevel@tonic-gate 	du->type = ITMC_OBJ_DIREC;
125*7c478bd9Sstevel@tonic-gate 	du->name = NULL;
126*7c478bd9Sstevel@tonic-gate 	du->obj = direc = malloc_vital(sizeof (itm_direc_t));
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (NULL != cond) {
129*7c478bd9Sstevel@tonic-gate 		direc->condition.itm_ptr = 0;
130*7c478bd9Sstevel@tonic-gate 		cond->referencer = &(direc->condition);
131*7c478bd9Sstevel@tonic-gate 		du->ref[0] = cond;
132*7c478bd9Sstevel@tonic-gate 	} else if (NULL != cond_name) {
133*7c478bd9Sstevel@tonic-gate 		direc->condition.itm_ptr = (itm_place2_t)(cond_name);
134*7c478bd9Sstevel@tonic-gate 		du->ref[0] = obj_register(ITMC_OBJ_COND, cond_name, NULL, 0,
135*7c478bd9Sstevel@tonic-gate 					&(direc->condition), OBJ_REG_TAIL);
136*7c478bd9Sstevel@tonic-gate 	} else {
137*7c478bd9Sstevel@tonic-gate 		direc->condition.itm_ptr = 0;
138*7c478bd9Sstevel@tonic-gate 		du->ref[0] = NULL;
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (NULL != act_name) {
143*7c478bd9Sstevel@tonic-gate 		direc->action.itm_ptr = (itm_place2_t)(act_name);
144*7c478bd9Sstevel@tonic-gate 		du->ref[1] = obj_register(ITMC_OBJ_ACTION, act_name, NULL, 0,
145*7c478bd9Sstevel@tonic-gate 					&(direc->action), OBJ_REG_TAIL);
146*7c478bd9Sstevel@tonic-gate 	} else if (NULL != act && act->tbl_hdr != NULL) {
147*7c478bd9Sstevel@tonic-gate 		direc->action.itm_ptr = (itm_place2_t)(act->tbl_hdr);
148*7c478bd9Sstevel@tonic-gate 		du->ref[1] = obj_register(act->type,
149*7c478bd9Sstevel@tonic-gate 				(itm_data_t *)(act->tbl_hdr->name.itm_ptr),
150*7c478bd9Sstevel@tonic-gate 				act->tbl_hdr, act->tbl_hdr->size,
151*7c478bd9Sstevel@tonic-gate 				&(direc->action), OBJ_REG_TAIL);
152*7c478bd9Sstevel@tonic-gate 	} else {
153*7c478bd9Sstevel@tonic-gate 		return (NULL);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	du->ref[2] = NULL;
157*7c478bd9Sstevel@tonic-gate 	return	(du);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate itm_tbl_hdr_t *
163*7c478bd9Sstevel@tonic-gate obj_table(itm_type_t	tbl_type,
164*7c478bd9Sstevel@tonic-gate 	itm_data_t	*name,
165*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*obj_list,
166*7c478bd9Sstevel@tonic-gate 	itm_size_t	obj_size)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t	*tbl;
169*7c478bd9Sstevel@tonic-gate 	obj_array_t	obj_array;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	obj_array = obj_list_to_array(sizeof (itm_tbl_hdr_t),
172*7c478bd9Sstevel@tonic-gate 					obj_list, obj_size);
173*7c478bd9Sstevel@tonic-gate 	tbl = obj_array.obj;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	tbl->type = tbl_type;
176*7c478bd9Sstevel@tonic-gate 	if (name) {
177*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
178*7c478bd9Sstevel@tonic-gate 		tbl->name.itm_pad = 0;
179*7c478bd9Sstevel@tonic-gate #endif
180*7c478bd9Sstevel@tonic-gate 		tbl->name.itm_ptr = (itm_place2_t)name;
181*7c478bd9Sstevel@tonic-gate 	} else {
182*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
183*7c478bd9Sstevel@tonic-gate 		tbl->name.itm_pad = 0;
184*7c478bd9Sstevel@tonic-gate #endif
185*7c478bd9Sstevel@tonic-gate 		tbl->name.itm_ptr = NULL;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	tbl->size = (sizeof (itm_tbl_hdr_t)) + (obj_array.num	*obj_size);
188*7c478bd9Sstevel@tonic-gate 	tbl->number = obj_array.num;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if ((ITM_TBL_MASK&tbl->type) == ITM_TBL_OP) {
191*7c478bd9Sstevel@tonic-gate 		op_hirarchy(tbl, obj_list);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	return	(tbl);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate static obj_array_t
200*7c478bd9Sstevel@tonic-gate obj_list_to_array(itm_size_t hdr_size, itmc_obj_t	*obj_list,
201*7c478bd9Sstevel@tonic-gate 			itm_size_t size)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	obj_array_t	obj_array;
204*7c478bd9Sstevel@tonic-gate 	itm_size_t	offset;
205*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*ol;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	for (obj_array.num = 0, ol = obj_list;
208*7c478bd9Sstevel@tonic-gate 	    ol; obj_array.num += 1, ol = ol->next) {
209*7c478bd9Sstevel@tonic-gate 		/* NOP */;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	obj_array.obj = malloc_vital(hdr_size + (size * obj_array.num));
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	if (obj_array.num == 0)
215*7c478bd9Sstevel@tonic-gate 		return	(obj_array);
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	for (offset = hdr_size, ol = obj_list;
218*7c478bd9Sstevel@tonic-gate 	    ol; offset += size, ol = ol->next) {
219*7c478bd9Sstevel@tonic-gate 		(void) memcpy((char *)(obj_array.obj) + offset, ol->obj, size);
220*7c478bd9Sstevel@tonic-gate 		if (ol->ref[0]) {
221*7c478bd9Sstevel@tonic-gate 			ol->ref[0]->referencer =
222*7c478bd9Sstevel@tonic-gate 			(void *)((char *)(ol->ref[0]->referencer) +
223*7c478bd9Sstevel@tonic-gate 			((char *)(obj_array.obj) -
224*7c478bd9Sstevel@tonic-gate 			(char *)(ol->obj) + offset));
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 		if (ol->ref[1]) {
227*7c478bd9Sstevel@tonic-gate 			ol->ref[1]->referencer =
228*7c478bd9Sstevel@tonic-gate 			(void *)((char *)(ol->ref[1]->referencer) +
229*7c478bd9Sstevel@tonic-gate 			((char *)(obj_array.obj) -
230*7c478bd9Sstevel@tonic-gate 			(char *)(ol->obj) + offset));
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 		if (ol->ref[2]) {
233*7c478bd9Sstevel@tonic-gate 			ol->ref[2]->referencer =
234*7c478bd9Sstevel@tonic-gate 			(void *)((char *)(ol->ref[2]->referencer) +
235*7c478bd9Sstevel@tonic-gate 			((char *)(obj_array.obj) -
236*7c478bd9Sstevel@tonic-gate 			(char *)(ol->obj) + offset));
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	return	(obj_array);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate static void
244*7c478bd9Sstevel@tonic-gate op_hirarchy(itm_tbl_hdr_t	*optbl,
245*7c478bd9Sstevel@tonic-gate 	itmc_obj_t		*obj_list)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	itm_op_outer_t	*o;
248*7c478bd9Sstevel@tonic-gate 	itm_op_inner_t	*in;
249*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*ol;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('l', ("op_hirarchy (optbl=%x)\n", optbl));
252*7c478bd9Sstevel@tonic-gate 	o = malloc_vital(sizeof (itm_op_outer_t));
253*7c478bd9Sstevel@tonic-gate 	o->link = itm_op_outer;
254*7c478bd9Sstevel@tonic-gate 	itm_op_outer = o;
255*7c478bd9Sstevel@tonic-gate 	o->in = NULL;
256*7c478bd9Sstevel@tonic-gate 	o->optbl = optbl;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	for (ol = obj_list; ol != NULL; ol = ol->next) {
259*7c478bd9Sstevel@tonic-gate 		if ((ol->type == ITMC_OBJ_OP) &&
260*7c478bd9Sstevel@tonic-gate 			(((itm_op_t *)ol->obj)->type == ITM_OP_OPERATION)) {
261*7c478bd9Sstevel@tonic-gate 			in = malloc_vital(sizeof (itm_op_inner_t));
262*7c478bd9Sstevel@tonic-gate 			in->in = o->in;
263*7c478bd9Sstevel@tonic-gate 			o->in = in;
264*7c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('L', ("o->in(%x) in->in(%x)\n",
265*7c478bd9Sstevel@tonic-gate 				o->in, in->in));
266*7c478bd9Sstevel@tonic-gate 			in->ref = ol->ref[0];
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate #ifdef ENABLE_TRACE
271*7c478bd9Sstevel@tonic-gate 	for (in = o->in; in != NULL; in = in->in) {
272*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('L', ("o=%x in=%x in->in=%x\n",
273*7c478bd9Sstevel@tonic-gate 				o, in, in->in));
274*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('L', ("o(table)%x->in(ref)=%x\n",
275*7c478bd9Sstevel@tonic-gate 		o->optbl, in->ref));
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate #endif
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate itmc_obj_t *
282*7c478bd9Sstevel@tonic-gate obj_list_append(itmc_obj_t	*obj_list, itmc_obj_t	*obj)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	if (0 == obj) {
285*7c478bd9Sstevel@tonic-gate 		return	(obj_list);
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	obj->next = NULL;
289*7c478bd9Sstevel@tonic-gate 	obj->last = obj;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	if (obj_list) {
292*7c478bd9Sstevel@tonic-gate 		obj_list->last->next = obj;
293*7c478bd9Sstevel@tonic-gate 		obj_list->last = obj;
294*7c478bd9Sstevel@tonic-gate 		return	(obj_list);
295*7c478bd9Sstevel@tonic-gate 	} else {
296*7c478bd9Sstevel@tonic-gate 		return	(obj);
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate itmc_ref_t *
302*7c478bd9Sstevel@tonic-gate obj_register(itm_type_t type, itm_data_t	*name,
303*7c478bd9Sstevel@tonic-gate 		void	*obj, size_t size, itm_place_t	*ref,
304*7c478bd9Sstevel@tonic-gate 		itm_type_t reg_place)
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate 	itmc_ref_t	*refp;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('O', ("obj_register: %6ld %08p %08p %08ld %08p %ld\n",
309*7c478bd9Sstevel@tonic-gate 			type, name, obj, size, ref, reg_place));
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	refp = malloc_vital(sizeof (itmc_ref_t));
312*7c478bd9Sstevel@tonic-gate 	refp->name = NULL;
313*7c478bd9Sstevel@tonic-gate 	refp->referencee = obj;
314*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
315*7c478bd9Sstevel@tonic-gate 	refp->reloc.itm_pad = 0;
316*7c478bd9Sstevel@tonic-gate #endif
317*7c478bd9Sstevel@tonic-gate 	refp->reloc.itm_ptr = 0;
318*7c478bd9Sstevel@tonic-gate 	refp->size = size;
319*7c478bd9Sstevel@tonic-gate 	refp->referencer = ref;
320*7c478bd9Sstevel@tonic-gate 	refp->next = NULL;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	if (NULL == obj) { /* reference to named object */
323*7c478bd9Sstevel@tonic-gate 		if (NULL == name) {
324*7c478bd9Sstevel@tonic-gate 			if (0 == error_deferred) {
325*7c478bd9Sstevel@tonic-gate 				/* should never happen */
326*7c478bd9Sstevel@tonic-gate 				itm_error(
327*7c478bd9Sstevel@tonic-gate 				gettext("internal error: "
328*7c478bd9Sstevel@tonic-gate 					"obj_register: (NULL == obj) "
329*7c478bd9Sstevel@tonic-gate 					"&& (NULL == name)\n"));
330*7c478bd9Sstevel@tonic-gate 				exit(ITMC_STATUS_SYS2);
331*7c478bd9Sstevel@tonic-gate 			}
332*7c478bd9Sstevel@tonic-gate 			return (NULL);
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 		refp->name = name_refer(name, type, refp);
335*7c478bd9Sstevel@tonic-gate 		return	(refp);
336*7c478bd9Sstevel@tonic-gate 	} else if ((NULL != name) && (0 < name->size)) {
337*7c478bd9Sstevel@tonic-gate 		/* definition of named object */
338*7c478bd9Sstevel@tonic-gate 		refp->name = name_register(name, type, refp);
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if ((ITMC_OBJ_FIRST <= type) && (type <= ITMC_OBJ_LAST)) {
342*7c478bd9Sstevel@tonic-gate 		switch (reg_place) {
343*7c478bd9Sstevel@tonic-gate 		case OBJ_REG_HEAD:
344*7c478bd9Sstevel@tonic-gate 			refp->next = ref_first[type];
345*7c478bd9Sstevel@tonic-gate 			ref_first[type] = refp;
346*7c478bd9Sstevel@tonic-gate 			if (NULL == ref_last[type]) {
347*7c478bd9Sstevel@tonic-gate 				ref_last[type] = refp;
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 			break;
350*7c478bd9Sstevel@tonic-gate 		case OBJ_REG_TAIL:
351*7c478bd9Sstevel@tonic-gate 			if (ref_first[type]) {
352*7c478bd9Sstevel@tonic-gate 				ref_last[type]->next = refp;
353*7c478bd9Sstevel@tonic-gate 			} else {
354*7c478bd9Sstevel@tonic-gate 				ref_first[type] = refp;
355*7c478bd9Sstevel@tonic-gate 			}
356*7c478bd9Sstevel@tonic-gate 			ref_last[type] = refp;
357*7c478bd9Sstevel@tonic-gate 			break;
358*7c478bd9Sstevel@tonic-gate 		}
359*7c478bd9Sstevel@tonic-gate 	} else {
360*7c478bd9Sstevel@tonic-gate 		itm_error(gettext("obj_register: illegal object type\n"));
361*7c478bd9Sstevel@tonic-gate 		exit(ITMC_STATUS_SYS2);
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	return	(refp);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate itm_tbl_hdr_t *
369*7c478bd9Sstevel@tonic-gate range_table(itm_data_t		*name, itmc_obj_t	*obj_list)
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate 	itm_num_t		num;
372*7c478bd9Sstevel@tonic-gate 	itmc_obj_t		*ol;
373*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	*rp;
374*7c478bd9Sstevel@tonic-gate 	itm_range_hdr_t		*rh;
375*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*table;
376*7c478bd9Sstevel@tonic-gate 	itm_size_t		length = 0;
377*7c478bd9Sstevel@tonic-gate 	itm_num_t		i;
378*7c478bd9Sstevel@tonic-gate 	char			*p;
379*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	/* count range, determine length */
382*7c478bd9Sstevel@tonic-gate 	for (num = 0, ol = obj_list; ol; ol = ol->next, num++) {
383*7c478bd9Sstevel@tonic-gate 		rp = (itmc_data_pair_t *)(ol->obj);
384*7c478bd9Sstevel@tonic-gate 		if (length == 0) {
385*7c478bd9Sstevel@tonic-gate 			if (rp->data0.size == 0) {
386*7c478bd9Sstevel@tonic-gate 				itm_error(gettext("between has null range\n"));
387*7c478bd9Sstevel@tonic-gate 					error_deferred += 1;
388*7c478bd9Sstevel@tonic-gate 				return	(NULL);
389*7c478bd9Sstevel@tonic-gate 			}
390*7c478bd9Sstevel@tonic-gate 			length = rp->data0.size;
391*7c478bd9Sstevel@tonic-gate 		}
392*7c478bd9Sstevel@tonic-gate 		if ((rp->data0.size != length) ||
393*7c478bd9Sstevel@tonic-gate 		    (rp->data1.size != length)) {
394*7c478bd9Sstevel@tonic-gate 			itm_error(gettext(
395*7c478bd9Sstevel@tonic-gate 			"length of source sequences must be the same\n"));
396*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
397*7c478bd9Sstevel@tonic-gate 			return	(NULL);
398*7c478bd9Sstevel@tonic-gate 		}
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 	if (num == 0) {
401*7c478bd9Sstevel@tonic-gate 		itm_error(gettext("between has no ranges\n"));
402*7c478bd9Sstevel@tonic-gate 		error_deferred += 1;
403*7c478bd9Sstevel@tonic-gate 		return	(NULL);
404*7c478bd9Sstevel@tonic-gate 	}
405*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
406*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_range_hdr_t)) + (length * num) * 2);
407*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	table = malloc_vital(table_size);
410*7c478bd9Sstevel@tonic-gate 	table->type = ITM_TBL_RANGE;
411*7c478bd9Sstevel@tonic-gate 	if (NULL != name)
412*7c478bd9Sstevel@tonic-gate 		table->name.itm_ptr = (itm_place2_t)name;
413*7c478bd9Sstevel@tonic-gate 	table->size = table_size;
414*7c478bd9Sstevel@tonic-gate 	table->number = num;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	rh = (itm_range_hdr_t *)(table + 1);
417*7c478bd9Sstevel@tonic-gate 	rh->len = length;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	p = (char *)(rh + 1);
420*7c478bd9Sstevel@tonic-gate 	for (ol = obj_list, i = 0; ol; ol = ol->next, i++) {
421*7c478bd9Sstevel@tonic-gate 		rp = (itmc_data_pair_t *)(ol->obj);
422*7c478bd9Sstevel@tonic-gate 		(void) memcpy(p, (NSPTR(&(rp->data0))), length);
423*7c478bd9Sstevel@tonic-gate 		p += length;
424*7c478bd9Sstevel@tonic-gate 		(void) memcpy(p, (NSPTR(&(rp->data1))), length);
425*7c478bd9Sstevel@tonic-gate 		p += length;
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	return	(table);
429*7c478bd9Sstevel@tonic-gate }
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate /*
432*7c478bd9Sstevel@tonic-gate  *	escape sequence table for stateful code set sequence
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate itm_tbl_hdr_t *
435*7c478bd9Sstevel@tonic-gate escseq_table(itm_data_t		*name, itmc_obj_t	*obj_list)
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	itm_num_t		num;
438*7c478bd9Sstevel@tonic-gate 	itmc_obj_t		*ol;
439*7c478bd9Sstevel@tonic-gate 	itm_data_t		*ep;
440*7c478bd9Sstevel@tonic-gate 	itm_escapeseq_hdr_t	*eh;
441*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*table;
442*7c478bd9Sstevel@tonic-gate 	itm_size_t		len_max = 0;
443*7c478bd9Sstevel@tonic-gate 	itm_size_t		len_min;
444*7c478bd9Sstevel@tonic-gate 	itm_num_t		i;
445*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	ol = obj_list;
448*7c478bd9Sstevel@tonic-gate 	len_min = ((itm_data_t *)(ol->obj))->size;
449*7c478bd9Sstevel@tonic-gate 	for (num = 0; NULL != ol; ol = ol->next, num++) {
450*7c478bd9Sstevel@tonic-gate 		ep = (itm_data_t *)(ol->obj);
451*7c478bd9Sstevel@tonic-gate 		if (ep->size < len_min)	 len_min = ep->size;
452*7c478bd9Sstevel@tonic-gate 		if (ep->size > len_max)	 len_max = ep->size;
453*7c478bd9Sstevel@tonic-gate 	}
454*7c478bd9Sstevel@tonic-gate 	if (num == 0) {
455*7c478bd9Sstevel@tonic-gate 		itm_error(gettext
456*7c478bd9Sstevel@tonic-gate 			("escape sequence is defined without sequence\n"));
457*7c478bd9Sstevel@tonic-gate 		error_deferred += 1;
458*7c478bd9Sstevel@tonic-gate 		return	(NULL);
459*7c478bd9Sstevel@tonic-gate 	} else if (0 == len_min) {
460*7c478bd9Sstevel@tonic-gate 		itm_error(gettext("null sequence\n"));
461*7c478bd9Sstevel@tonic-gate 		error_deferred += 1;
462*7c478bd9Sstevel@tonic-gate 		return	(NULL);
463*7c478bd9Sstevel@tonic-gate 	}
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
466*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_escapeseq_hdr_t)) +
467*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_data_t) * num));
468*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
469*7c478bd9Sstevel@tonic-gate 	table = malloc_vital(table_size);
470*7c478bd9Sstevel@tonic-gate 	table->type = ITM_TBL_ESCAPESEQ;
471*7c478bd9Sstevel@tonic-gate 	if (NULL != name)
472*7c478bd9Sstevel@tonic-gate 		table->name.itm_ptr = (itm_place2_t)name;
473*7c478bd9Sstevel@tonic-gate 	table->size = table_size;
474*7c478bd9Sstevel@tonic-gate 	table->number = num;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	eh = (itm_escapeseq_hdr_t *)(table + 1);
477*7c478bd9Sstevel@tonic-gate 	eh->len_max = len_max;
478*7c478bd9Sstevel@tonic-gate 	eh->len_min = len_min;
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	for (ol = obj_list, ep = (itm_data_t *)(eh + 1);
481*7c478bd9Sstevel@tonic-gate 	    ol != NULL;
482*7c478bd9Sstevel@tonic-gate 	    ol = ol->next, ep++) {
483*7c478bd9Sstevel@tonic-gate 		*ep = *((itm_data_t *)(ol->obj));
484*7c478bd9Sstevel@tonic-gate 		if ((sizeof (itm_place_t)) < ep->size) {
485*7c478bd9Sstevel@tonic-gate 			(void) obj_register(ITMC_OBJ_DATA, NULL,
486*7c478bd9Sstevel@tonic-gate 					(void *)(ep->place.itm_ptr), ep->size,
487*7c478bd9Sstevel@tonic-gate 					&(ep->place), OBJ_REG_TAIL);
488*7c478bd9Sstevel@tonic-gate 		}
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 	(void) qsort((itm_data_t *)(eh + 1), num, sizeof (itm_data_t),
491*7c478bd9Sstevel@tonic-gate 		(int (*)(const void *, const void *))data_compare);
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	for (i = 0, ep = (itm_data_t *)(eh + 1);
494*7c478bd9Sstevel@tonic-gate 	    i < num - 1;
495*7c478bd9Sstevel@tonic-gate 	    i++, ep++) {
496*7c478bd9Sstevel@tonic-gate 		if (0 <= data_compare(ep, (ep + 1))) {
497*7c478bd9Sstevel@tonic-gate 			itm_error(
498*7c478bd9Sstevel@tonic-gate 			gettext(
499*7c478bd9Sstevel@tonic-gate 			"same escape sequences are defined: 0x%1$s 0x%2$s\n"),
500*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(ep),
501*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(ep + 1));
502*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
503*7c478bd9Sstevel@tonic-gate 			return	(NULL);
504*7c478bd9Sstevel@tonic-gate 		}
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 	return	(table);
507*7c478bd9Sstevel@tonic-gate }
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate itm_tbl_hdr_t *
513*7c478bd9Sstevel@tonic-gate map_table(itm_data_t	*name, itmc_map_t	*map_list,
514*7c478bd9Sstevel@tonic-gate 		itmc_map_attr_t *attr)
515*7c478bd9Sstevel@tonic-gate {
516*7c478bd9Sstevel@tonic-gate 	itm_size_t		num;
517*7c478bd9Sstevel@tonic-gate 	itm_size_t		num2;
518*7c478bd9Sstevel@tonic-gate 	itmc_map_t		*ml;
519*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**tpp;
520*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*table;
521*7c478bd9Sstevel@tonic-gate 	long			source_len = 0;
522*7c478bd9Sstevel@tonic-gate 	long			result_len = 0;
523*7c478bd9Sstevel@tonic-gate 	long			source_fixed_len = 1;
524*7c478bd9Sstevel@tonic-gate 	long			pass_through = 0;
525*7c478bd9Sstevel@tonic-gate 	long			default_count = 0;
526*7c478bd9Sstevel@tonic-gate 	itm_data_t		*default_data = NULL;
527*7c478bd9Sstevel@tonic-gate 	long			error_deferred_local = 0;
528*7c478bd9Sstevel@tonic-gate 	unsigned long		dense_encoded_map_ent;
529*7c478bd9Sstevel@tonic-gate 	unsigned long		simple_indexed_map_ent;
530*7c478bd9Sstevel@tonic-gate 	itm_size_t		source_start;
531*7c478bd9Sstevel@tonic-gate 	itm_size_t		source_end;
532*7c478bd9Sstevel@tonic-gate 	unsigned long		u;
533*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_min;
534*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_max;
535*7c478bd9Sstevel@tonic-gate 	unsigned char		*p;
536*7c478bd9Sstevel@tonic-gate 	long			i;
537*7c478bd9Sstevel@tonic-gate 	itmc_map_type_t		map_type = NULL;
538*7c478bd9Sstevel@tonic-gate 	itmc_map_name_type_t	*map_name_type;
539*7c478bd9Sstevel@tonic-gate 	long			hash_factor;
540*7c478bd9Sstevel@tonic-gate 	long			result_len_specfied = 0;
541*7c478bd9Sstevel@tonic-gate 	size_t			j;
542*7c478bd9Sstevel@tonic-gate 	long			n;
543*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**dp1;
544*7c478bd9Sstevel@tonic-gate 	itm_num_t		error_count = 0;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	if (attr != NULL) {
547*7c478bd9Sstevel@tonic-gate 		map_type = check_map_type(attr);
548*7c478bd9Sstevel@tonic-gate 	}
549*7c478bd9Sstevel@tonic-gate 	if (NULL == map_type) {
550*7c478bd9Sstevel@tonic-gate 		map_type = ITMC_MAP_AUTOMATIC;
551*7c478bd9Sstevel@tonic-gate 	}
552*7c478bd9Sstevel@tonic-gate 	hash_factor = ((NULL != attr) && (attr->hash_factor != 0)) ?
553*7c478bd9Sstevel@tonic-gate 							attr->hash_factor:
554*7c478bd9Sstevel@tonic-gate 							200;
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 	map_name_type = cmd_opt.map_name_type;
557*7c478bd9Sstevel@tonic-gate 	for (; map_name_type; map_name_type = map_name_type->next) {
558*7c478bd9Sstevel@tonic-gate 		if ('\0' == *(map_name_type->name)) {
559*7c478bd9Sstevel@tonic-gate 			map_type = map_name_type->type;
560*7c478bd9Sstevel@tonic-gate 			hash_factor = map_name_type->hash_factor;
561*7c478bd9Sstevel@tonic-gate 			break;
562*7c478bd9Sstevel@tonic-gate 		}
563*7c478bd9Sstevel@tonic-gate 	}
564*7c478bd9Sstevel@tonic-gate 	map_name_type = cmd_opt.map_name_type;
565*7c478bd9Sstevel@tonic-gate 	if ((NULL != name) && (NULL != cmd_opt.map_name_type)) {
566*7c478bd9Sstevel@tonic-gate 		p = NSPTR(name);
567*7c478bd9Sstevel@tonic-gate 		for (; map_name_type; map_name_type = map_name_type->next) {
568*7c478bd9Sstevel@tonic-gate 			if (0 == strcmp(map_name_type->name, (char *)p)) {
569*7c478bd9Sstevel@tonic-gate 				map_type = map_name_type->type;
570*7c478bd9Sstevel@tonic-gate 				hash_factor = map_name_type->hash_factor;
571*7c478bd9Sstevel@tonic-gate 				break;
572*7c478bd9Sstevel@tonic-gate 			}
573*7c478bd9Sstevel@tonic-gate 		}
574*7c478bd9Sstevel@tonic-gate 	}
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 	if (NULL != attr) {
577*7c478bd9Sstevel@tonic-gate 		if (MAXSEQUENCE < attr->resultlen) {
578*7c478bd9Sstevel@tonic-gate 			itm_error(
579*7c478bd9Sstevel@tonic-gate 			gettext("output_byte_length must be less than %1$d\n"),
580*7c478bd9Sstevel@tonic-gate 			MAXSEQUENCE);
581*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
582*7c478bd9Sstevel@tonic-gate 			return	(NULL);
583*7c478bd9Sstevel@tonic-gate 		}
584*7c478bd9Sstevel@tonic-gate 		result_len_specfied = attr->resultlen;
585*7c478bd9Sstevel@tonic-gate 	} else {
586*7c478bd9Sstevel@tonic-gate 		result_len_specfied = 0;
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 	for (num = 0, ml = map_list; ml; ml = ml->next, num++) {
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 		/* default */
592*7c478bd9Sstevel@tonic-gate 		if (0 == ml->data_pair.data0.size) {
593*7c478bd9Sstevel@tonic-gate 			if (0 == ml->data_pair.data1.size) {
594*7c478bd9Sstevel@tonic-gate 				pass_through += 1;
595*7c478bd9Sstevel@tonic-gate 				default_data = (itm_data_t *)(-1);
596*7c478bd9Sstevel@tonic-gate 			} else {
597*7c478bd9Sstevel@tonic-gate 				default_count += 1;
598*7c478bd9Sstevel@tonic-gate 				default_data = &(ml->data_pair.data1);
599*7c478bd9Sstevel@tonic-gate 			}
600*7c478bd9Sstevel@tonic-gate 			--num;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 		} else if (0 == ml->data_pair.data1.size) {
604*7c478bd9Sstevel@tonic-gate 			/* error source sequence */
605*7c478bd9Sstevel@tonic-gate 			continue;
606*7c478bd9Sstevel@tonic-gate 		}
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 		/* fixed length */
609*7c478bd9Sstevel@tonic-gate 		if ((0 < source_len) &&
610*7c478bd9Sstevel@tonic-gate 		    (0 < ml->data_pair.data0.size) &&
611*7c478bd9Sstevel@tonic-gate 		    (source_len != ml->data_pair.data0.size)) {
612*7c478bd9Sstevel@tonic-gate 			source_fixed_len = 0;
613*7c478bd9Sstevel@tonic-gate 		}
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 		/* maximum length */
616*7c478bd9Sstevel@tonic-gate 		if (source_len < ml->data_pair.data0.size) {
617*7c478bd9Sstevel@tonic-gate 			source_len = ml->data_pair.data0.size;
618*7c478bd9Sstevel@tonic-gate 		}
619*7c478bd9Sstevel@tonic-gate 		if (result_len < ml->data_pair.data1.size) {
620*7c478bd9Sstevel@tonic-gate 			result_len = ml->data_pair.data1.size;
621*7c478bd9Sstevel@tonic-gate 		}
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 		/* map source has range */
624*7c478bd9Sstevel@tonic-gate 		if (0 < ml->data_pair.range.size) {
625*7c478bd9Sstevel@tonic-gate 			if (ml->data_pair.range.size !=
626*7c478bd9Sstevel@tonic-gate 			    ml->data_pair.data0.size) {
627*7c478bd9Sstevel@tonic-gate 				itm_error(
628*7c478bd9Sstevel@tonic-gate 				gettext("length of source range must be "
629*7c478bd9Sstevel@tonic-gate 				"the same: 0x%1$s 0x%2$s\n"),
630*7c478bd9Sstevel@tonic-gate 				data_to_hexadecimal(&(ml->data_pair.data0)),
631*7c478bd9Sstevel@tonic-gate 				data_to_hexadecimal(&(ml->data_pair.range)));
632*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
633*7c478bd9Sstevel@tonic-gate 				return	(NULL);
634*7c478bd9Sstevel@tonic-gate 			}
635*7c478bd9Sstevel@tonic-gate 			if (0 <= data_compare(&(ml->data_pair.data0),
636*7c478bd9Sstevel@tonic-gate 			    &((ml->data_pair.range)))) {
637*7c478bd9Sstevel@tonic-gate 				itm_error(
638*7c478bd9Sstevel@tonic-gate 				gettext("source range error: 0x%1$s 0x%2$s\n"),
639*7c478bd9Sstevel@tonic-gate 				data_to_hexadecimal(&(ml->data_pair.data0)),
640*7c478bd9Sstevel@tonic-gate 				data_to_hexadecimal(&(ml->data_pair.range)));
641*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
642*7c478bd9Sstevel@tonic-gate 				return	(NULL);
643*7c478bd9Sstevel@tonic-gate 			}
644*7c478bd9Sstevel@tonic-gate 			j = map_table_resultlen(ml);
645*7c478bd9Sstevel@tonic-gate 			if (result_len < j) {
646*7c478bd9Sstevel@tonic-gate 				result_len = j;
647*7c478bd9Sstevel@tonic-gate 			}
648*7c478bd9Sstevel@tonic-gate 		}
649*7c478bd9Sstevel@tonic-gate 	}
650*7c478bd9Sstevel@tonic-gate 	if (num == 0) {
651*7c478bd9Sstevel@tonic-gate 		itm_error(
652*7c478bd9Sstevel@tonic-gate 			gettext("no mapping pair\n"));
653*7c478bd9Sstevel@tonic-gate 		error_deferred += 1;
654*7c478bd9Sstevel@tonic-gate 		return	(NULL);
655*7c478bd9Sstevel@tonic-gate 	}
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 	if (0 != result_len_specfied) {
658*7c478bd9Sstevel@tonic-gate 		if (result_len > result_len_specfied) {
659*7c478bd9Sstevel@tonic-gate 			itm_error(
660*7c478bd9Sstevel@tonic-gate 			gettext("result value length is "
661*7c478bd9Sstevel@tonic-gate 			"over specifed output_byte_length(%1$ld)\n"),
662*7c478bd9Sstevel@tonic-gate 			result_len_specfied);
663*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
664*7c478bd9Sstevel@tonic-gate 			return	(NULL);
665*7c478bd9Sstevel@tonic-gate 		}
666*7c478bd9Sstevel@tonic-gate 		result_len = result_len_specfied;
667*7c478bd9Sstevel@tonic-gate 	}
668*7c478bd9Sstevel@tonic-gate 	byte_seq_min = malloc_vital((sizeof (unsigned char)) * source_len);
669*7c478bd9Sstevel@tonic-gate 	byte_seq_max = malloc_vital((sizeof (unsigned char)) * source_len);
670*7c478bd9Sstevel@tonic-gate 	for (num = 0, ml = map_list; ml; ml = ml->next, num++) {
671*7c478bd9Sstevel@tonic-gate 		if (0 == ml->data_pair.data0.size) {
672*7c478bd9Sstevel@tonic-gate 			continue;
673*7c478bd9Sstevel@tonic-gate 		}
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)(NSPTR(&((ml->data_pair).data0)));
676*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < source_len; i++) {
677*7c478bd9Sstevel@tonic-gate 			*(byte_seq_min + i) = *(p + i);
678*7c478bd9Sstevel@tonic-gate 			*(byte_seq_max + i) = *(p + i);
679*7c478bd9Sstevel@tonic-gate 		}
680*7c478bd9Sstevel@tonic-gate 		break;
681*7c478bd9Sstevel@tonic-gate 	}
682*7c478bd9Sstevel@tonic-gate 	for (num = 0, ml = map_list; ml; ml = ml->next, num++) {
683*7c478bd9Sstevel@tonic-gate 		if (0 == ml->data_pair.data0.size) {
684*7c478bd9Sstevel@tonic-gate 			num--;
685*7c478bd9Sstevel@tonic-gate 			continue;
686*7c478bd9Sstevel@tonic-gate 		}
687*7c478bd9Sstevel@tonic-gate 		if (ml->data_pair.range.size > 0) {
688*7c478bd9Sstevel@tonic-gate 			map_range_adjust_byte_seq(byte_seq_min, byte_seq_max,
689*7c478bd9Sstevel@tonic-gate 					    source_len, &(ml->data_pair));
690*7c478bd9Sstevel@tonic-gate 		} else {
691*7c478bd9Sstevel@tonic-gate 			p = (unsigned char *)(NSPTR(&((ml->data_pair).data0)));
692*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < source_len; i++) {
693*7c478bd9Sstevel@tonic-gate 				if (*(p + i) < *(byte_seq_min + i)) {
694*7c478bd9Sstevel@tonic-gate 					*(byte_seq_min + i) = *(p + i);
695*7c478bd9Sstevel@tonic-gate 				}
696*7c478bd9Sstevel@tonic-gate 				if (*(byte_seq_max + i) < *(p + i)) {
697*7c478bd9Sstevel@tonic-gate 					*(byte_seq_max + i) = *(p + i);
698*7c478bd9Sstevel@tonic-gate 				}
699*7c478bd9Sstevel@tonic-gate 			}
700*7c478bd9Sstevel@tonic-gate 		}
701*7c478bd9Sstevel@tonic-gate 	}
702*7c478bd9Sstevel@tonic-gate 	for (dense_encoded_map_ent = 1, i = 0; i < source_len; i++) {
703*7c478bd9Sstevel@tonic-gate 		u = dense_encoded_map_ent;
704*7c478bd9Sstevel@tonic-gate 		dense_encoded_map_ent *=
705*7c478bd9Sstevel@tonic-gate 			(*(byte_seq_max + i) - *(byte_seq_min + i) + 1);
706*7c478bd9Sstevel@tonic-gate 		if (dense_encoded_map_ent < u) {
707*7c478bd9Sstevel@tonic-gate 			dense_encoded_map_ent = (ulong_t)(~0);
708*7c478bd9Sstevel@tonic-gate 			break;
709*7c478bd9Sstevel@tonic-gate 		}
710*7c478bd9Sstevel@tonic-gate 	}
711*7c478bd9Sstevel@tonic-gate #if defined(DEBUG)
712*7c478bd9Sstevel@tonic-gate 	if (TRACE('m')) {
713*7c478bd9Sstevel@tonic-gate 		int	i;
714*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('m', ("map_table: ent=%lu num=%lu	",
715*7c478bd9Sstevel@tonic-gate 				dense_encoded_map_ent, num));
716*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('m', ("byte_seq_min=0x"));
717*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < source_len; i++) {
718*7c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('m', ("%02x", *(byte_seq_min + i)));
719*7c478bd9Sstevel@tonic-gate 		}
720*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('m', ("  byte_seq_max=0x"));
721*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < source_len; i++) {
722*7c478bd9Sstevel@tonic-gate 			TRACE_MESSAGE('m', ("%02x", *(byte_seq_max + i)));
723*7c478bd9Sstevel@tonic-gate 		}
724*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('m', ("\n"));
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 	tpp = malloc_vital((sizeof (itmc_data_pair_t *)) * num);
729*7c478bd9Sstevel@tonic-gate 	for (num = 0, num2 = 0, ml = map_list; ml; ml = ml->next) {
730*7c478bd9Sstevel@tonic-gate 		if (0 < ml->data_pair.data0.size) {
731*7c478bd9Sstevel@tonic-gate 			itm_num_t range_num;
732*7c478bd9Sstevel@tonic-gate 			*(tpp + num) = &(ml->data_pair);
733*7c478bd9Sstevel@tonic-gate 			num++;
734*7c478bd9Sstevel@tonic-gate 			range_num = 1;
735*7c478bd9Sstevel@tonic-gate 			if (ml->data_pair.range.size > 0) {
736*7c478bd9Sstevel@tonic-gate 				range_num +=
737*7c478bd9Sstevel@tonic-gate 				map_table_num_range(&(ml->data_pair));
738*7c478bd9Sstevel@tonic-gate 			}
739*7c478bd9Sstevel@tonic-gate 			num2 += range_num;
740*7c478bd9Sstevel@tonic-gate 			if (0 == ml->data_pair.data1.size) {
741*7c478bd9Sstevel@tonic-gate 				/* specified error sequence */
742*7c478bd9Sstevel@tonic-gate 				error_count += range_num;
743*7c478bd9Sstevel@tonic-gate 			}
744*7c478bd9Sstevel@tonic-gate 		}
745*7c478bd9Sstevel@tonic-gate 	}
746*7c478bd9Sstevel@tonic-gate 	(void) qsort(tpp, num, sizeof (itmc_data_pair_t *),
747*7c478bd9Sstevel@tonic-gate 		(int (*)(const void *, const void *))data_pair_compare);
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	/* check if map_pair range and next map_pair are overrapped */
750*7c478bd9Sstevel@tonic-gate 	for (n = 0, dp1 = tpp; n < (num-1); n++, dp1++) {
751*7c478bd9Sstevel@tonic-gate 		if (((*(dp1+0))->range.size != 0) &&
752*7c478bd9Sstevel@tonic-gate 		    (0 <= data_compare(&((*(dp1+0))->range),
753*7c478bd9Sstevel@tonic-gate 		    &((*(dp1+1))->data0)))) {
754*7c478bd9Sstevel@tonic-gate 			itm_error(
755*7c478bd9Sstevel@tonic-gate 			gettext("ranges of source sequences "
756*7c478bd9Sstevel@tonic-gate 			"overrapped: %1$s %2$s\n"),
757*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&((*(dp1+0))->range)),
758*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&((*(dp1+1))->data0)));
759*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
760*7c478bd9Sstevel@tonic-gate 			return	(NULL);
761*7c478bd9Sstevel@tonic-gate 		}
762*7c478bd9Sstevel@tonic-gate 	}
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	if (1 < default_count) {
765*7c478bd9Sstevel@tonic-gate 		itm_error(
766*7c478bd9Sstevel@tonic-gate 			gettext("default is specified %1$d times in a map\n"),
767*7c478bd9Sstevel@tonic-gate 			default_count);
768*7c478bd9Sstevel@tonic-gate 		error_deferred_local += 1;
769*7c478bd9Sstevel@tonic-gate 	}
770*7c478bd9Sstevel@tonic-gate 	if ((1 == default_count) && (!source_fixed_len)) {
771*7c478bd9Sstevel@tonic-gate 		itm_error(
772*7c478bd9Sstevel@tonic-gate 			gettext("default is specified,"
773*7c478bd9Sstevel@tonic-gate 				" but length of source data is not fixed\n"));
774*7c478bd9Sstevel@tonic-gate 		error_deferred_local += 1;
775*7c478bd9Sstevel@tonic-gate 	}
776*7c478bd9Sstevel@tonic-gate 	if ((1 <= pass_through) && (source_len != result_len)) {
777*7c478bd9Sstevel@tonic-gate 		itm_error(
778*7c478bd9Sstevel@tonic-gate 			gettext("\"default no_change_copy\" is "
779*7c478bd9Sstevel@tonic-gate 			"specified, but size does not match\n"));
780*7c478bd9Sstevel@tonic-gate 		error_deferred_local += 1;
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	if (error_deferred_local) {
784*7c478bd9Sstevel@tonic-gate 		error_deferred += error_deferred_local;
785*7c478bd9Sstevel@tonic-gate 		return	(NULL);
786*7c478bd9Sstevel@tonic-gate 	}
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	if (source_fixed_len) {
789*7c478bd9Sstevel@tonic-gate 		source_start = data_to_long(&((*(tpp + 0))->data0));
790*7c478bd9Sstevel@tonic-gate 		source_end = data_to_long(&((*(tpp + num - 1))->data0));
791*7c478bd9Sstevel@tonic-gate 		if (0 < (*(tpp + num - 1))->range.size) {
792*7c478bd9Sstevel@tonic-gate 			source_end = data_to_long(&((*(tpp + num - 1))->range));
793*7c478bd9Sstevel@tonic-gate 		}
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 		simple_indexed_map_ent = source_end - source_start + 1;
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('m', ("map_table: simple_indexed_map_ent=%lu\n",
798*7c478bd9Sstevel@tonic-gate 				simple_indexed_map_ent));
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate 		switch (map_type) {
801*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_AUTOMATIC:
802*7c478bd9Sstevel@tonic-gate 			if ((source_len <= 2) &&
803*7c478bd9Sstevel@tonic-gate 			    (((ulong_t)(~0) == dense_encoded_map_ent) ||
804*7c478bd9Sstevel@tonic-gate 			    (simple_indexed_map_ent <
805*7c478bd9Sstevel@tonic-gate 			    (dense_encoded_map_ent * 2)))) {
806*7c478bd9Sstevel@tonic-gate 				/*
807*7c478bd9Sstevel@tonic-gate 				 * for small source sequence,
808*7c478bd9Sstevel@tonic-gate 				 * if dense table is not so large
809*7c478bd9Sstevel@tonic-gate 				 * compared with simple table,
810*7c478bd9Sstevel@tonic-gate 				 * use simple.
811*7c478bd9Sstevel@tonic-gate 				 */
812*7c478bd9Sstevel@tonic-gate 				map_type = ITMC_MAP_SIMPLE_INDEX;
813*7c478bd9Sstevel@tonic-gate 			} else if (cmd_opt.large_table) {
814*7c478bd9Sstevel@tonic-gate 				if ((sizeof (long)) < source_len) {
815*7c478bd9Sstevel@tonic-gate 					itm_error(
816*7c478bd9Sstevel@tonic-gate 					gettext("length of source is too long "
817*7c478bd9Sstevel@tonic-gate 						"for large table: %ld\n"),
818*7c478bd9Sstevel@tonic-gate 						source_len);
819*7c478bd9Sstevel@tonic-gate 					error_deferred += 1;
820*7c478bd9Sstevel@tonic-gate 					return	(NULL);
821*7c478bd9Sstevel@tonic-gate 				}
822*7c478bd9Sstevel@tonic-gate 				map_type = ITMC_MAP_SIMPLE_INDEX;
823*7c478bd9Sstevel@tonic-gate 			} else if (((ulong_t)(~0) == dense_encoded_map_ent) ||
824*7c478bd9Sstevel@tonic-gate 			    ((0xffff < dense_encoded_map_ent) &&
825*7c478bd9Sstevel@tonic-gate 			    ((num2 * 8) < dense_encoded_map_ent))) {
826*7c478bd9Sstevel@tonic-gate 				/*
827*7c478bd9Sstevel@tonic-gate 				 * if dense can be used and not too large
828*7c478bd9Sstevel@tonic-gate 				 * ( less than (hash table entry * 8),
829*7c478bd9Sstevel@tonic-gate 				 * use dense.
830*7c478bd9Sstevel@tonic-gate 				 */
831*7c478bd9Sstevel@tonic-gate 				map_type = ITMC_MAP_SIMPLE_HASH;
832*7c478bd9Sstevel@tonic-gate 			} else {
833*7c478bd9Sstevel@tonic-gate 				map_type = ITMC_MAP_DENSE_ENCODING;
834*7c478bd9Sstevel@tonic-gate 			}
835*7c478bd9Sstevel@tonic-gate 			break;
836*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_SIMPLE_INDEX:
837*7c478bd9Sstevel@tonic-gate 			if ((sizeof (long)) < source_len) {
838*7c478bd9Sstevel@tonic-gate 				itm_error(
839*7c478bd9Sstevel@tonic-gate 				gettext("length of source is too long "
840*7c478bd9Sstevel@tonic-gate 					"for index lookup: %ld\n"),
841*7c478bd9Sstevel@tonic-gate 					source_len);
842*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
843*7c478bd9Sstevel@tonic-gate 				return	(NULL);
844*7c478bd9Sstevel@tonic-gate 			}
845*7c478bd9Sstevel@tonic-gate 			break;
846*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_SIMPLE_HASH:
847*7c478bd9Sstevel@tonic-gate 			for (i = 2, u = 256; i < (sizeof (long)); i++) {
848*7c478bd9Sstevel@tonic-gate 				u *= 256;
849*7c478bd9Sstevel@tonic-gate 			}
850*7c478bd9Sstevel@tonic-gate 			if (u < num2) {
851*7c478bd9Sstevel@tonic-gate 				itm_error(
852*7c478bd9Sstevel@tonic-gate 				gettext("map is too large for hashing: %lu\n"),
853*7c478bd9Sstevel@tonic-gate 					num2);
854*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
855*7c478bd9Sstevel@tonic-gate 				return	(NULL);
856*7c478bd9Sstevel@tonic-gate 			}
857*7c478bd9Sstevel@tonic-gate 			break;
858*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_DENSE_ENCODING:
859*7c478bd9Sstevel@tonic-gate 			for (i = 2, u = 256; i < (sizeof (long)); i++) {
860*7c478bd9Sstevel@tonic-gate 				u *= 256;
861*7c478bd9Sstevel@tonic-gate 			}
862*7c478bd9Sstevel@tonic-gate 			if (u < dense_encoded_map_ent) {
863*7c478bd9Sstevel@tonic-gate 				itm_error(
864*7c478bd9Sstevel@tonic-gate 				gettext(
865*7c478bd9Sstevel@tonic-gate 				"map is too large for dense encoding: %lu\n"),
866*7c478bd9Sstevel@tonic-gate 				dense_encoded_map_ent);
867*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
868*7c478bd9Sstevel@tonic-gate 				return	(NULL);
869*7c478bd9Sstevel@tonic-gate 			}
870*7c478bd9Sstevel@tonic-gate 			break;
871*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_BINARY_SEARCH:
872*7c478bd9Sstevel@tonic-gate 			for (i = 2, u = 256; i < (sizeof (long)); i++) {
873*7c478bd9Sstevel@tonic-gate 				u *= 256;
874*7c478bd9Sstevel@tonic-gate 			}
875*7c478bd9Sstevel@tonic-gate 			if (u < num2) {
876*7c478bd9Sstevel@tonic-gate 				itm_error(
877*7c478bd9Sstevel@tonic-gate 				gettext("length of source is too long for "
878*7c478bd9Sstevel@tonic-gate 					"binary search: %ld\n"),
879*7c478bd9Sstevel@tonic-gate 				source_len);
880*7c478bd9Sstevel@tonic-gate 				error_deferred += 1;
881*7c478bd9Sstevel@tonic-gate 				return	(NULL);
882*7c478bd9Sstevel@tonic-gate 			}
883*7c478bd9Sstevel@tonic-gate 			break;
884*7c478bd9Sstevel@tonic-gate 		default:
885*7c478bd9Sstevel@tonic-gate 			break;
886*7c478bd9Sstevel@tonic-gate 		}
887*7c478bd9Sstevel@tonic-gate 		switch (map_type) {
888*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_SIMPLE_INDEX:
889*7c478bd9Sstevel@tonic-gate 			table = map_table_indexed_fixed(
890*7c478bd9Sstevel@tonic-gate 				tpp, num, default_data,
891*7c478bd9Sstevel@tonic-gate 				result_len, error_count);
892*7c478bd9Sstevel@tonic-gate 			break;
893*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_SIMPLE_HASH:
894*7c478bd9Sstevel@tonic-gate 			table = map_table_hash(tpp, num, default_data,
895*7c478bd9Sstevel@tonic-gate 					hash_factor, result_len, num2,
896*7c478bd9Sstevel@tonic-gate 					error_count);
897*7c478bd9Sstevel@tonic-gate 			break;
898*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_DENSE_ENCODING:
899*7c478bd9Sstevel@tonic-gate 			table = map_table_dense_encoding(tpp, num,
900*7c478bd9Sstevel@tonic-gate 					default_data,
901*7c478bd9Sstevel@tonic-gate 					dense_encoded_map_ent,
902*7c478bd9Sstevel@tonic-gate 					byte_seq_min, byte_seq_max,
903*7c478bd9Sstevel@tonic-gate 					result_len, error_count);
904*7c478bd9Sstevel@tonic-gate 			break;
905*7c478bd9Sstevel@tonic-gate 		case ITMC_MAP_BINARY_SEARCH:
906*7c478bd9Sstevel@tonic-gate 			table = map_table_lookup_fixed(tpp, num,
907*7c478bd9Sstevel@tonic-gate 					default_data,
908*7c478bd9Sstevel@tonic-gate 					result_len, num2);
909*7c478bd9Sstevel@tonic-gate 			break;
910*7c478bd9Sstevel@tonic-gate 		}
911*7c478bd9Sstevel@tonic-gate 	} else {
912*7c478bd9Sstevel@tonic-gate 		table = map_table_lookup_var();
913*7c478bd9Sstevel@tonic-gate 	}
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 	if ((NULL != name) && (NULL != table)) {
916*7c478bd9Sstevel@tonic-gate 		table->name.itm_ptr = (itm_place2_t)name;
917*7c478bd9Sstevel@tonic-gate 	}
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	return	(table);
920*7c478bd9Sstevel@tonic-gate }
921*7c478bd9Sstevel@tonic-gate 
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate static itmc_map_type_t
924*7c478bd9Sstevel@tonic-gate check_map_type(itmc_map_attr_t *attr)
925*7c478bd9Sstevel@tonic-gate {
926*7c478bd9Sstevel@tonic-gate 	int i;
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate 	if (NULL == attr->type) {
929*7c478bd9Sstevel@tonic-gate 		return (0);
930*7c478bd9Sstevel@tonic-gate 	}
931*7c478bd9Sstevel@tonic-gate 	for (i = 0; NULL != map_type_name[i].name; i++) {
932*7c478bd9Sstevel@tonic-gate 		if (0 == strncmp(((char *)&(attr->type->place)),
933*7c478bd9Sstevel@tonic-gate 				map_type_name[i].name, attr->type->size)) {
934*7c478bd9Sstevel@tonic-gate 			return (map_type_name[i].type);
935*7c478bd9Sstevel@tonic-gate 		}
936*7c478bd9Sstevel@tonic-gate 	}
937*7c478bd9Sstevel@tonic-gate 	return (0);
938*7c478bd9Sstevel@tonic-gate }
939*7c478bd9Sstevel@tonic-gate 
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t *
942*7c478bd9Sstevel@tonic-gate map_table_indexed_fixed(
943*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**tpp,
944*7c478bd9Sstevel@tonic-gate 	itm_size_t		num,
945*7c478bd9Sstevel@tonic-gate 	itm_data_t		*default_data,
946*7c478bd9Sstevel@tonic-gate 	long			resultlen,
947*7c478bd9Sstevel@tonic-gate 	itm_num_t		error_count)
948*7c478bd9Sstevel@tonic-gate {
949*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*header;
950*7c478bd9Sstevel@tonic-gate 	itm_map_idx_fix_hdr_t	*sub_hdr;
951*7c478bd9Sstevel@tonic-gate 	char			*table;
952*7c478bd9Sstevel@tonic-gate 	char			*error_table;
953*7c478bd9Sstevel@tonic-gate 	itm_size_t		source_start;
954*7c478bd9Sstevel@tonic-gate 	itm_size_t		source_end;
955*7c478bd9Sstevel@tonic-gate 	itm_size_t		entry_num;
956*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
957*7c478bd9Sstevel@tonic-gate 	itm_size_t		j;
958*7c478bd9Sstevel@tonic-gate 	itm_size_t		i;
959*7c478bd9Sstevel@tonic-gate 	itm_size_t		k;
960*7c478bd9Sstevel@tonic-gate 	char			*p;
961*7c478bd9Sstevel@tonic-gate 	itm_data_t		*source;
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('m', ("map_table_range : %ld\n", num));
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	source = &((*(tpp + 0))->data0);
966*7c478bd9Sstevel@tonic-gate 	assert((sizeof (itm_place_t)) >= source->size);
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	if ((1 == source->size) &&
969*7c478bd9Sstevel@tonic-gate 	    (1 == resultlen)) {
970*7c478bd9Sstevel@tonic-gate 		source_start = 0;
971*7c478bd9Sstevel@tonic-gate 		source_end = 255;
972*7c478bd9Sstevel@tonic-gate 	} else {
973*7c478bd9Sstevel@tonic-gate 		source_start = data_to_long(&((*(tpp + 0))->data0));
974*7c478bd9Sstevel@tonic-gate 		source_end = data_to_long(&((*(tpp + num - 1))->data0));
975*7c478bd9Sstevel@tonic-gate 		if (0 < (*(tpp + num - 1))->range.size)
976*7c478bd9Sstevel@tonic-gate 			source_end = data_to_long(&((*(tpp + num - 1))->range));
977*7c478bd9Sstevel@tonic-gate 	}
978*7c478bd9Sstevel@tonic-gate 
979*7c478bd9Sstevel@tonic-gate 	entry_num = source_end - source_start + 1;
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
982*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_map_idx_fix_hdr_t)) +
983*7c478bd9Sstevel@tonic-gate 			(resultlen * entry_num));
984*7c478bd9Sstevel@tonic-gate 	if (0 < error_count) {
985*7c478bd9Sstevel@tonic-gate 		table_size += entry_num;
986*7c478bd9Sstevel@tonic-gate 	}
987*7c478bd9Sstevel@tonic-gate 	if (NULL == default_data) {
988*7c478bd9Sstevel@tonic-gate 		if ((num < entry_num) ||
989*7c478bd9Sstevel@tonic-gate 		    (error_count <= 0)) {
990*7c478bd9Sstevel@tonic-gate 			table_size += entry_num;
991*7c478bd9Sstevel@tonic-gate 		}
992*7c478bd9Sstevel@tonic-gate 	} else if ((itm_data_t *)(-1) != default_data) {
993*7c478bd9Sstevel@tonic-gate 		table_size += resultlen;
994*7c478bd9Sstevel@tonic-gate 	}
995*7c478bd9Sstevel@tonic-gate 
996*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
997*7c478bd9Sstevel@tonic-gate 	header = malloc_vital(table_size);
998*7c478bd9Sstevel@tonic-gate 	sub_hdr = (itm_map_idx_fix_hdr_t *)(header + 1);
999*7c478bd9Sstevel@tonic-gate 	table = (char *)(sub_hdr + 1);
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate 	if ((1 == (*tpp)->data0.size) &&
1002*7c478bd9Sstevel@tonic-gate 	    (1 == (*tpp)->data1.size)) {
1003*7c478bd9Sstevel@tonic-gate 		header->type = ITM_TBL_MAP_INDEX_FIXED_1_1;
1004*7c478bd9Sstevel@tonic-gate 	} else {
1005*7c478bd9Sstevel@tonic-gate 		header->type = ITM_TBL_MAP_INDEX_FIXED;
1006*7c478bd9Sstevel@tonic-gate 	}
1007*7c478bd9Sstevel@tonic-gate 	header->name.itm_ptr = 0;
1008*7c478bd9Sstevel@tonic-gate 	header->size = table_size;
1009*7c478bd9Sstevel@tonic-gate 	header->number = entry_num;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 	sub_hdr->source_len = (*tpp)->data0.size;
1012*7c478bd9Sstevel@tonic-gate 	sub_hdr->result_len = resultlen;
1013*7c478bd9Sstevel@tonic-gate 	sub_hdr->start.itm_ptr = source_start;
1014*7c478bd9Sstevel@tonic-gate 	sub_hdr->end.itm_ptr = source_end;
1015*7c478bd9Sstevel@tonic-gate 	sub_hdr->error_num = error_count; /* > 0; so pad4 = 0 */
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 	if (NULL != default_data) {
1018*7c478bd9Sstevel@tonic-gate 		if ((itm_data_t *)(-1) == default_data) {
1019*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = -1;
1020*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
1021*7c478bd9Sstevel@tonic-gate 			sub_hdr->pad3_num = (pad_t)(~0);
1022*7c478bd9Sstevel@tonic-gate #endif
1023*7c478bd9Sstevel@tonic-gate 		} else {
1024*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 0;
1025*7c478bd9Sstevel@tonic-gate 		}
1026*7c478bd9Sstevel@tonic-gate 	} else {
1027*7c478bd9Sstevel@tonic-gate 		if (num < entry_num) {
1028*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 1;
1029*7c478bd9Sstevel@tonic-gate 		} else {
1030*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 2;
1031*7c478bd9Sstevel@tonic-gate 		}
1032*7c478bd9Sstevel@tonic-gate 	}
1033*7c478bd9Sstevel@tonic-gate 
1034*7c478bd9Sstevel@tonic-gate 	error_table = (table + (resultlen * entry_num));
1035*7c478bd9Sstevel@tonic-gate 	if (-1 == sub_hdr->default_error) {
1036*7c478bd9Sstevel@tonic-gate 		if (source->size != resultlen) {
1037*7c478bd9Sstevel@tonic-gate 			itm_error(
1038*7c478bd9Sstevel@tonic-gate 				gettext("\"default no_change_copy\" is "
1039*7c478bd9Sstevel@tonic-gate 				"specified, but size does not match\n"));
1040*7c478bd9Sstevel@tonic-gate 			exit(ITMC_STATUS_BT);
1041*7c478bd9Sstevel@tonic-gate 		}
1042*7c478bd9Sstevel@tonic-gate 
1043*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0;
1044*7c478bd9Sstevel@tonic-gate 		    i < (entry_num);
1045*7c478bd9Sstevel@tonic-gate 		    i++, j += resultlen) {
1046*7c478bd9Sstevel@tonic-gate 			for (k = 0; k < resultlen; k++) {
1047*7c478bd9Sstevel@tonic-gate 				*(table + j + k) =
1048*7c478bd9Sstevel@tonic-gate 					(((source_start + i) >>
1049*7c478bd9Sstevel@tonic-gate 					((resultlen - k - 1) * 8)) &
1050*7c478bd9Sstevel@tonic-gate 					0x00ff);
1051*7c478bd9Sstevel@tonic-gate 			}
1052*7c478bd9Sstevel@tonic-gate 		}
1053*7c478bd9Sstevel@tonic-gate 	} else if (0 == sub_hdr->default_error) {
1054*7c478bd9Sstevel@tonic-gate 		error_table += resultlen;
1055*7c478bd9Sstevel@tonic-gate 		if (default_data->size <= (sizeof (itm_place_t))) {
1056*7c478bd9Sstevel@tonic-gate 			for (i = 0, j = 0;
1057*7c478bd9Sstevel@tonic-gate 			    i < (entry_num + 1); /* last one is for default */
1058*7c478bd9Sstevel@tonic-gate 			    i++, j += resultlen) {
1059*7c478bd9Sstevel@tonic-gate 				(void) memcpy(table + j +
1060*7c478bd9Sstevel@tonic-gate 				(resultlen - default_data->size),
1061*7c478bd9Sstevel@tonic-gate 				(void *)(&(default_data->place.itm_64d)),
1062*7c478bd9Sstevel@tonic-gate 				default_data->size);
1063*7c478bd9Sstevel@tonic-gate 			}
1064*7c478bd9Sstevel@tonic-gate 		} else {
1065*7c478bd9Sstevel@tonic-gate 			for (i = 0, j = 0;
1066*7c478bd9Sstevel@tonic-gate 			    i < (entry_num + 1); /* last one is for default */
1067*7c478bd9Sstevel@tonic-gate 			    i++, j += resultlen) {
1068*7c478bd9Sstevel@tonic-gate 				(void) memcpy(table + j +
1069*7c478bd9Sstevel@tonic-gate 				(resultlen - default_data->size),
1070*7c478bd9Sstevel@tonic-gate 				(void *)(default_data->place.itm_ptr),
1071*7c478bd9Sstevel@tonic-gate 				default_data->size);
1072*7c478bd9Sstevel@tonic-gate 			}
1073*7c478bd9Sstevel@tonic-gate 		}
1074*7c478bd9Sstevel@tonic-gate 	}
1075*7c478bd9Sstevel@tonic-gate 	if (1 == sub_hdr->default_error) {
1076*7c478bd9Sstevel@tonic-gate 		(void) memset(error_table, 1, entry_num);
1077*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
1078*7c478bd9Sstevel@tonic-gate 			if (0 == (*(tpp + i))->data1.size) {
1079*7c478bd9Sstevel@tonic-gate 				continue; /* error sequence */
1080*7c478bd9Sstevel@tonic-gate 			}
1081*7c478bd9Sstevel@tonic-gate 			j = data_to_long(&((*(tpp + i))->data0)) -
1082*7c478bd9Sstevel@tonic-gate 				source_start;
1083*7c478bd9Sstevel@tonic-gate 			k = ((*(tpp + i))->range.size) == 0 ? j :
1084*7c478bd9Sstevel@tonic-gate 			    data_to_long(&((*(tpp + i))->range)) -
1085*7c478bd9Sstevel@tonic-gate 				source_start;
1086*7c478bd9Sstevel@tonic-gate 			for (; j <= k; j++) {
1087*7c478bd9Sstevel@tonic-gate 				*(error_table + j) = 0;
1088*7c478bd9Sstevel@tonic-gate 			}
1089*7c478bd9Sstevel@tonic-gate 		}
1090*7c478bd9Sstevel@tonic-gate 	} else if (0 < error_count) {
1091*7c478bd9Sstevel@tonic-gate 		(void) memset(error_table, 0, entry_num);
1092*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
1093*7c478bd9Sstevel@tonic-gate 			if (0 == (*(tpp + i))->data1.size) {
1094*7c478bd9Sstevel@tonic-gate 				/* error sequence */
1095*7c478bd9Sstevel@tonic-gate 				j = data_to_long(&((*(tpp + i))->data0)) -
1096*7c478bd9Sstevel@tonic-gate 					source_start;
1097*7c478bd9Sstevel@tonic-gate 				k = ((*(tpp + i))->range.size) == 0 ? j :
1098*7c478bd9Sstevel@tonic-gate 				data_to_long(&((*(tpp + i))->range)) -
1099*7c478bd9Sstevel@tonic-gate 				source_start;
1100*7c478bd9Sstevel@tonic-gate 				for (; j <= k; j++) {
1101*7c478bd9Sstevel@tonic-gate 					*(error_table + j) = 1;
1102*7c478bd9Sstevel@tonic-gate 				}
1103*7c478bd9Sstevel@tonic-gate 			}
1104*7c478bd9Sstevel@tonic-gate 		}
1105*7c478bd9Sstevel@tonic-gate 	}
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 	p = malloc_vital(sizeof (uchar_t *) * resultlen);
1108*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
1109*7c478bd9Sstevel@tonic-gate 		j = data_to_long(&((*(tpp + i))->data0)) - source_start;
1110*7c478bd9Sstevel@tonic-gate 		if (0 != (*(tpp + i))->range.size)
1111*7c478bd9Sstevel@tonic-gate 			k = data_to_long(&((*(tpp + i))->range)) -
1112*7c478bd9Sstevel@tonic-gate 			    source_start;
1113*7c478bd9Sstevel@tonic-gate 		else
1114*7c478bd9Sstevel@tonic-gate 			k = j;
1115*7c478bd9Sstevel@tonic-gate 		(void) memset(p, 0, sizeof (uchar_t *) * resultlen);
1116*7c478bd9Sstevel@tonic-gate 		(void) memcpy(p + (resultlen  - (*(tpp + i))->data1.size),
1117*7c478bd9Sstevel@tonic-gate 			((caddr_t)NSPTR(&((*(tpp + i))->data1))),
1118*7c478bd9Sstevel@tonic-gate 			(*(tpp + i))->data1.size);
1119*7c478bd9Sstevel@tonic-gate 		map_range_make_result(table, j, k, p, resultlen);
1120*7c478bd9Sstevel@tonic-gate 	}
1121*7c478bd9Sstevel@tonic-gate 	free(p);
1122*7c478bd9Sstevel@tonic-gate 
1123*7c478bd9Sstevel@tonic-gate 	return	(header);
1124*7c478bd9Sstevel@tonic-gate }
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t *
1130*7c478bd9Sstevel@tonic-gate map_table_lookup_fixed(
1131*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**tpp,
1132*7c478bd9Sstevel@tonic-gate 	itm_size_t		num,
1133*7c478bd9Sstevel@tonic-gate 	itm_data_t		*default_data,
1134*7c478bd9Sstevel@tonic-gate 	long			resultlen,
1135*7c478bd9Sstevel@tonic-gate 	itm_size_t		num2)
1136*7c478bd9Sstevel@tonic-gate {
1137*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*header;
1138*7c478bd9Sstevel@tonic-gate 	itm_map_lookup_hdr_t	*sub_hdr;
1139*7c478bd9Sstevel@tonic-gate 	char			*table;
1140*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
1141*7c478bd9Sstevel@tonic-gate 	itm_size_t		j;
1142*7c478bd9Sstevel@tonic-gate 	itm_size_t		i;
1143*7c478bd9Sstevel@tonic-gate 	itm_size_t		k;
1144*7c478bd9Sstevel@tonic-gate 	itm_size_t		h;
1145*7c478bd9Sstevel@tonic-gate 	itm_data_t		*source;
1146*7c478bd9Sstevel@tonic-gate 	uchar_t			*source_data;
1147*7c478bd9Sstevel@tonic-gate 	uchar_t			*result_data;
1148*7c478bd9Sstevel@tonic-gate 
1149*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('m', ("map_table_lookup_fixed : %ld(%ld) 0x%lx\n",
1150*7c478bd9Sstevel@tonic-gate 			num, num2, default_data));
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 	source = &((*(tpp + 0))->data0);
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
1155*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_map_idx_fix_hdr_t)) +
1156*7c478bd9Sstevel@tonic-gate 			((source->size + 1 + resultlen) * num2));
1157*7c478bd9Sstevel@tonic-gate 	if ((NULL != default_data) &&
1158*7c478bd9Sstevel@tonic-gate 	    (((itm_data_t *)(-1)) != default_data)) {
1159*7c478bd9Sstevel@tonic-gate 		table_size += (source->size + 1 + resultlen);
1160*7c478bd9Sstevel@tonic-gate 	}
1161*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
1162*7c478bd9Sstevel@tonic-gate 	header = malloc_vital(table_size);
1163*7c478bd9Sstevel@tonic-gate 	sub_hdr = (itm_map_lookup_hdr_t *)(header + 1);
1164*7c478bd9Sstevel@tonic-gate 	table = (char *)(sub_hdr + 1);
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate 	header->type = ITM_TBL_MAP_LOOKUP;
1167*7c478bd9Sstevel@tonic-gate 	header->name.itm_ptr = 0;
1168*7c478bd9Sstevel@tonic-gate 	header->size = table_size;
1169*7c478bd9Sstevel@tonic-gate 	header->number = num2;
1170*7c478bd9Sstevel@tonic-gate 	if (NULL != default_data) {
1171*7c478bd9Sstevel@tonic-gate 		if ((itm_data_t *)(-1) == default_data) {
1172*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
1173*7c478bd9Sstevel@tonic-gate 			sub_hdr->pad3_num = (pad_t)(~0);
1174*7c478bd9Sstevel@tonic-gate #endif
1175*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = -1;
1176*7c478bd9Sstevel@tonic-gate 		} else {
1177*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 0;
1178*7c478bd9Sstevel@tonic-gate 		}
1179*7c478bd9Sstevel@tonic-gate 	} else {
1180*7c478bd9Sstevel@tonic-gate 		sub_hdr->default_error = 2;
1181*7c478bd9Sstevel@tonic-gate 	}
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate 	sub_hdr->source_len = source->size;
1184*7c478bd9Sstevel@tonic-gate 	sub_hdr->result_len = resultlen;
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	/* specified map */
1187*7c478bd9Sstevel@tonic-gate 	source_data = malloc_vital(source->size);
1188*7c478bd9Sstevel@tonic-gate 	result_data = malloc_vital(resultlen);
1189*7c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < num; i++) {
1190*7c478bd9Sstevel@tonic-gate 		(void) memcpy(table + j,
1191*7c478bd9Sstevel@tonic-gate 			NSPTR(&((*(tpp + i))->data0)), source->size);
1192*7c478bd9Sstevel@tonic-gate 		j += source->size;
1193*7c478bd9Sstevel@tonic-gate 		if (0 == (*(tpp + i))->data1.size) {
1194*7c478bd9Sstevel@tonic-gate 			*(table + j) = 1; /* specified error */
1195*7c478bd9Sstevel@tonic-gate 			j += 1;
1196*7c478bd9Sstevel@tonic-gate 		} else {
1197*7c478bd9Sstevel@tonic-gate 			/* *(table + j) = 0; ** valid */
1198*7c478bd9Sstevel@tonic-gate 			j += 1;
1199*7c478bd9Sstevel@tonic-gate 			(void) memcpy(table + j +
1200*7c478bd9Sstevel@tonic-gate 				(resultlen  - (*(tpp + i))->data1.size),
1201*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data1)),
1202*7c478bd9Sstevel@tonic-gate 				(*(tpp + i))->data1.size);
1203*7c478bd9Sstevel@tonic-gate 		}
1204*7c478bd9Sstevel@tonic-gate 		j += resultlen;
1205*7c478bd9Sstevel@tonic-gate 
1206*7c478bd9Sstevel@tonic-gate 		if ((*(tpp + i))->range.size != 0) {
1207*7c478bd9Sstevel@tonic-gate 			(void) memcpy(source_data,
1208*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data0)),
1209*7c478bd9Sstevel@tonic-gate 				source->size);
1210*7c478bd9Sstevel@tonic-gate 			(void) memset(result_data, 0, resultlen);
1211*7c478bd9Sstevel@tonic-gate 			(void) memcpy(result_data +
1212*7c478bd9Sstevel@tonic-gate 				(resultlen  - (*(tpp + i))->data1.size),
1213*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data1)),
1214*7c478bd9Sstevel@tonic-gate 				(*(tpp + i))->data1.size);
1215*7c478bd9Sstevel@tonic-gate 			h = map_table_num_range((*(tpp + i)));
1216*7c478bd9Sstevel@tonic-gate 			for (k = 0; k < h; k++) {
1217*7c478bd9Sstevel@tonic-gate 				uchar_t		*dp;
1218*7c478bd9Sstevel@tonic-gate 				itm_size_t	m;
1219*7c478bd9Sstevel@tonic-gate 
1220*7c478bd9Sstevel@tonic-gate 				for (m = 0,
1221*7c478bd9Sstevel@tonic-gate 				    dp = (uchar_t *)
1222*7c478bd9Sstevel@tonic-gate 				    (source_data + source->size - 1);
1223*7c478bd9Sstevel@tonic-gate 				    m < source->size;
1224*7c478bd9Sstevel@tonic-gate 				    m++, dp--) {
1225*7c478bd9Sstevel@tonic-gate 					if (0xff != *dp) {
1226*7c478bd9Sstevel@tonic-gate 						(*dp) += (char)1;
1227*7c478bd9Sstevel@tonic-gate 						for (++dp; m > 0; m--, dp++) {
1228*7c478bd9Sstevel@tonic-gate 							(*dp) = 0x00;
1229*7c478bd9Sstevel@tonic-gate 						}
1230*7c478bd9Sstevel@tonic-gate 						break;
1231*7c478bd9Sstevel@tonic-gate 					}
1232*7c478bd9Sstevel@tonic-gate 				}
1233*7c478bd9Sstevel@tonic-gate 				(void) memcpy(table + j,
1234*7c478bd9Sstevel@tonic-gate 					source_data, source->size);
1235*7c478bd9Sstevel@tonic-gate 				j += source->size;
1236*7c478bd9Sstevel@tonic-gate 
1237*7c478bd9Sstevel@tonic-gate 				if (0 == (*(tpp + i))->data1.size) {
1238*7c478bd9Sstevel@tonic-gate 					*(table + j) = 1; /* specified error */
1239*7c478bd9Sstevel@tonic-gate 					j += 1;
1240*7c478bd9Sstevel@tonic-gate 				} else {
1241*7c478bd9Sstevel@tonic-gate 					/* *(table + j) = 0; ** valid */
1242*7c478bd9Sstevel@tonic-gate 					j += 1;
1243*7c478bd9Sstevel@tonic-gate 					for (m = 0, dp = (uchar_t *)
1244*7c478bd9Sstevel@tonic-gate 					    (result_data + resultlen - 1);
1245*7c478bd9Sstevel@tonic-gate 					    m < resultlen;
1246*7c478bd9Sstevel@tonic-gate 					    m++, dp--) {
1247*7c478bd9Sstevel@tonic-gate 						if (0xff != *dp) {
1248*7c478bd9Sstevel@tonic-gate 							(*dp) += 1;
1249*7c478bd9Sstevel@tonic-gate 							for (++dp;
1250*7c478bd9Sstevel@tonic-gate 							    m > 0;
1251*7c478bd9Sstevel@tonic-gate 							    m--, dp++) {
1252*7c478bd9Sstevel@tonic-gate 								(*dp) = 0x00;
1253*7c478bd9Sstevel@tonic-gate 							}
1254*7c478bd9Sstevel@tonic-gate 							break;
1255*7c478bd9Sstevel@tonic-gate 						}
1256*7c478bd9Sstevel@tonic-gate 					}
1257*7c478bd9Sstevel@tonic-gate 					(void) memcpy(table + j, result_data,
1258*7c478bd9Sstevel@tonic-gate 						resultlen);
1259*7c478bd9Sstevel@tonic-gate 				}
1260*7c478bd9Sstevel@tonic-gate 				j += resultlen;
1261*7c478bd9Sstevel@tonic-gate 			}
1262*7c478bd9Sstevel@tonic-gate 		}
1263*7c478bd9Sstevel@tonic-gate 	}
1264*7c478bd9Sstevel@tonic-gate 	free(source_data);
1265*7c478bd9Sstevel@tonic-gate 	free(result_data);
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate 	/* default */
1268*7c478bd9Sstevel@tonic-gate 	if ((NULL != default_data) &&
1269*7c478bd9Sstevel@tonic-gate 	    (((itm_data_t *)(-1)) != default_data)) {
1270*7c478bd9Sstevel@tonic-gate 		(void) memset(table + j, 0, source->size + 1 + resultlen);
1271*7c478bd9Sstevel@tonic-gate 		(void) memcpy(table + j + source->size + 1 +
1272*7c478bd9Sstevel@tonic-gate 		(resultlen  - default_data->size),
1273*7c478bd9Sstevel@tonic-gate 		NSPTR(default_data), default_data->size);
1274*7c478bd9Sstevel@tonic-gate 	}
1275*7c478bd9Sstevel@tonic-gate 	return	(header);
1276*7c478bd9Sstevel@tonic-gate }
1277*7c478bd9Sstevel@tonic-gate 
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate 
1280*7c478bd9Sstevel@tonic-gate 
1281*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t *
1282*7c478bd9Sstevel@tonic-gate map_table_hash(
1283*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**tpp,
1284*7c478bd9Sstevel@tonic-gate 	itm_size_t		num,
1285*7c478bd9Sstevel@tonic-gate 	itm_data_t		*default_data,
1286*7c478bd9Sstevel@tonic-gate 	long			hash_factor,
1287*7c478bd9Sstevel@tonic-gate 	long			resultlen,
1288*7c478bd9Sstevel@tonic-gate 	itm_size_t		num2,
1289*7c478bd9Sstevel@tonic-gate 	itm_num_t		error_count)
1290*7c478bd9Sstevel@tonic-gate {
1291*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*header;
1292*7c478bd9Sstevel@tonic-gate 	itm_map_hash_hdr_t	*sub_hdr;
1293*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
1294*7c478bd9Sstevel@tonic-gate 	char			*error_table;
1295*7c478bd9Sstevel@tonic-gate 	char			*hash_table;
1296*7c478bd9Sstevel@tonic-gate 	itm_size_t		hash_table_num;
1297*7c478bd9Sstevel@tonic-gate 	char			*of_table;
1298*7c478bd9Sstevel@tonic-gate 	itm_size_t		of_table_num;
1299*7c478bd9Sstevel@tonic-gate 	itm_size_t		pair_size;
1300*7c478bd9Sstevel@tonic-gate 	itm_size_t		i;
1301*7c478bd9Sstevel@tonic-gate 	itm_size_t		j;
1302*7c478bd9Sstevel@tonic-gate 	itm_size_t		k;
1303*7c478bd9Sstevel@tonic-gate 	char			*p;
1304*7c478bd9Sstevel@tonic-gate 	itm_data_t		*source;
1305*7c478bd9Sstevel@tonic-gate 	long			hash_value;
1306*7c478bd9Sstevel@tonic-gate #if defined(DEBUG)
1307*7c478bd9Sstevel@tonic-gate 	long			hash_none;
1308*7c478bd9Sstevel@tonic-gate 	long			hash_one;
1309*7c478bd9Sstevel@tonic-gate 	long			hash_conflict;
1310*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1311*7c478bd9Sstevel@tonic-gate 	uchar_t			*source_data;
1312*7c478bd9Sstevel@tonic-gate 	uchar_t			*result_data;
1313*7c478bd9Sstevel@tonic-gate 	uchar_t			*dp;
1314*7c478bd9Sstevel@tonic-gate 	itm_size_t		m;
1315*7c478bd9Sstevel@tonic-gate 	itm_size_t		n;
1316*7c478bd9Sstevel@tonic-gate 	itm_size_t		h;
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('m', ("map_table_hash : %ld(%ld) 0x%lx\n",
1319*7c478bd9Sstevel@tonic-gate 			num, num2, default_data));
1320*7c478bd9Sstevel@tonic-gate 	source = &((*(tpp + 0))->data0);
1321*7c478bd9Sstevel@tonic-gate 	pair_size = (source->size + 1 + resultlen);
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate 	if (100 <= hash_factor) {
1324*7c478bd9Sstevel@tonic-gate 		hash_table_num = (num2 * (hash_factor / 100.0));
1325*7c478bd9Sstevel@tonic-gate 	} else {
1326*7c478bd9Sstevel@tonic-gate 		hash_table_num = (num2 * 2);
1327*7c478bd9Sstevel@tonic-gate 	}
1328*7c478bd9Sstevel@tonic-gate 	if (hash_table_num < 256) {
1329*7c478bd9Sstevel@tonic-gate 		hash_table_num = 256;
1330*7c478bd9Sstevel@tonic-gate 	}
1331*7c478bd9Sstevel@tonic-gate 	source_data = malloc_vital(source->size);
1332*7c478bd9Sstevel@tonic-gate 	result_data = malloc_vital(resultlen);
1333*7c478bd9Sstevel@tonic-gate 
1334*7c478bd9Sstevel@tonic-gate 	hash_table = malloc_vital(hash_table_num);
1335*7c478bd9Sstevel@tonic-gate 	for (i = 0, of_table_num = 0; i < num; i++) {
1336*7c478bd9Sstevel@tonic-gate 		hash_value =
1337*7c478bd9Sstevel@tonic-gate 			hash(NSPTR(&((*(tpp + i))->data0)),
1338*7c478bd9Sstevel@tonic-gate 				(*(tpp + i))->data0.size,
1339*7c478bd9Sstevel@tonic-gate 				hash_table_num);
1340*7c478bd9Sstevel@tonic-gate 		if (0 == *(hash_table + hash_value)) {
1341*7c478bd9Sstevel@tonic-gate 			*(hash_table + hash_value) = 1;
1342*7c478bd9Sstevel@tonic-gate 		} else {
1343*7c478bd9Sstevel@tonic-gate 			*(hash_table + hash_value) = 2;
1344*7c478bd9Sstevel@tonic-gate 			of_table_num += 1;
1345*7c478bd9Sstevel@tonic-gate 		}
1346*7c478bd9Sstevel@tonic-gate 
1347*7c478bd9Sstevel@tonic-gate 		if ((*(tpp + i))->range.size != 0) {
1348*7c478bd9Sstevel@tonic-gate 			(void) memcpy(source_data,
1349*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data0)),
1350*7c478bd9Sstevel@tonic-gate 				source->size);
1351*7c478bd9Sstevel@tonic-gate 			h = map_table_num_range((*(tpp + i)));
1352*7c478bd9Sstevel@tonic-gate 			for (n = 0; n < h; n++) {
1353*7c478bd9Sstevel@tonic-gate 				for (m = 0,
1354*7c478bd9Sstevel@tonic-gate 				    dp = (uchar_t *)
1355*7c478bd9Sstevel@tonic-gate 				    (source_data + source->size - 1);
1356*7c478bd9Sstevel@tonic-gate 				    m < source->size;
1357*7c478bd9Sstevel@tonic-gate 				    m++, dp--) {
1358*7c478bd9Sstevel@tonic-gate 					if (0xff != *dp) {
1359*7c478bd9Sstevel@tonic-gate 						(*dp) += 1;
1360*7c478bd9Sstevel@tonic-gate 						for (++dp; m > 0; m--, dp++) {
1361*7c478bd9Sstevel@tonic-gate 							(*dp) = 0x00;
1362*7c478bd9Sstevel@tonic-gate 						}
1363*7c478bd9Sstevel@tonic-gate 						break;
1364*7c478bd9Sstevel@tonic-gate 					}
1365*7c478bd9Sstevel@tonic-gate 				}
1366*7c478bd9Sstevel@tonic-gate 				hash_value =
1367*7c478bd9Sstevel@tonic-gate 					hash((char *)source_data, source->size,
1368*7c478bd9Sstevel@tonic-gate 						hash_table_num);
1369*7c478bd9Sstevel@tonic-gate 
1370*7c478bd9Sstevel@tonic-gate 				if (0 == *(hash_table + hash_value)) {
1371*7c478bd9Sstevel@tonic-gate 					*(hash_table + hash_value) = 1;
1372*7c478bd9Sstevel@tonic-gate 				} else {
1373*7c478bd9Sstevel@tonic-gate 					*(hash_table + hash_value) = 2;
1374*7c478bd9Sstevel@tonic-gate 					of_table_num += 1;
1375*7c478bd9Sstevel@tonic-gate 				}
1376*7c478bd9Sstevel@tonic-gate 			}
1377*7c478bd9Sstevel@tonic-gate 		}
1378*7c478bd9Sstevel@tonic-gate 	}
1379*7c478bd9Sstevel@tonic-gate 
1380*7c478bd9Sstevel@tonic-gate #if defined(DEBUG)
1381*7c478bd9Sstevel@tonic-gate 	if (TRACE('s')) {
1382*7c478bd9Sstevel@tonic-gate 		hash_none = 0;
1383*7c478bd9Sstevel@tonic-gate 		hash_one = 0;
1384*7c478bd9Sstevel@tonic-gate 		hash_conflict = 0;
1385*7c478bd9Sstevel@tonic-gate 		j = 0;
1386*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < hash_table_num; i++) {
1387*7c478bd9Sstevel@tonic-gate 			if (2 == *(hash_table + i)) {
1388*7c478bd9Sstevel@tonic-gate 				(void) putchar('2');
1389*7c478bd9Sstevel@tonic-gate 				hash_conflict += 1;
1390*7c478bd9Sstevel@tonic-gate 			} else if (1 == *(hash_table + i)) {
1391*7c478bd9Sstevel@tonic-gate 				(void) putchar('1');
1392*7c478bd9Sstevel@tonic-gate 				hash_one += 1;
1393*7c478bd9Sstevel@tonic-gate 			} else if (0 == *(hash_table + i)) {
1394*7c478bd9Sstevel@tonic-gate 				(void) putchar('-');
1395*7c478bd9Sstevel@tonic-gate 				hash_none += 1;
1396*7c478bd9Sstevel@tonic-gate 			} else {
1397*7c478bd9Sstevel@tonic-gate 				(void) putchar('*');
1398*7c478bd9Sstevel@tonic-gate 			}
1399*7c478bd9Sstevel@tonic-gate 			if (63 <= j) {
1400*7c478bd9Sstevel@tonic-gate 				j = 0;
1401*7c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
1402*7c478bd9Sstevel@tonic-gate 			} else {
1403*7c478bd9Sstevel@tonic-gate 				j += 1;
1404*7c478bd9Sstevel@tonic-gate 			}
1405*7c478bd9Sstevel@tonic-gate 		}
1406*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
1407*7c478bd9Sstevel@tonic-gate 		(void) printf("null=%ld one=%ld conflict=%ld\n",
1408*7c478bd9Sstevel@tonic-gate 			hash_none, hash_one, hash_conflict);
1409*7c478bd9Sstevel@tonic-gate 	}
1410*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 	free(hash_table);
1413*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
1414*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_map_hash_hdr_t)) +
1415*7c478bd9Sstevel@tonic-gate 			(hash_table_num) +
1416*7c478bd9Sstevel@tonic-gate 			(pair_size * hash_table_num) +
1417*7c478bd9Sstevel@tonic-gate 			(pair_size * of_table_num));
1418*7c478bd9Sstevel@tonic-gate 	if ((NULL != default_data) &&
1419*7c478bd9Sstevel@tonic-gate 	    (((itm_data_t *)(-1)) != default_data)) {
1420*7c478bd9Sstevel@tonic-gate 		table_size += pair_size;
1421*7c478bd9Sstevel@tonic-gate 	}
1422*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
1423*7c478bd9Sstevel@tonic-gate 	header = malloc_vital(table_size);
1424*7c478bd9Sstevel@tonic-gate 	sub_hdr = (itm_map_hash_hdr_t *)(header + 1);
1425*7c478bd9Sstevel@tonic-gate 	error_table = (char *)(sub_hdr + 1);
1426*7c478bd9Sstevel@tonic-gate 	hash_table = error_table + hash_table_num;
1427*7c478bd9Sstevel@tonic-gate 	of_table = hash_table + (pair_size * hash_table_num);
1428*7c478bd9Sstevel@tonic-gate 
1429*7c478bd9Sstevel@tonic-gate 	header->type = ITM_TBL_MAP_HASH;
1430*7c478bd9Sstevel@tonic-gate 	header->name.itm_ptr = 0;
1431*7c478bd9Sstevel@tonic-gate 	header->size = table_size;
1432*7c478bd9Sstevel@tonic-gate 	header->number = num2;
1433*7c478bd9Sstevel@tonic-gate 	if (NULL != default_data) {
1434*7c478bd9Sstevel@tonic-gate 		if ((itm_data_t *)(-1) == default_data) {
1435*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = -1;
1436*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
1437*7c478bd9Sstevel@tonic-gate 			sub_hdr->pad7_num = (pad_t)(~0);
1438*7c478bd9Sstevel@tonic-gate #endif
1439*7c478bd9Sstevel@tonic-gate 		} else {
1440*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 0;
1441*7c478bd9Sstevel@tonic-gate 		}
1442*7c478bd9Sstevel@tonic-gate 	} else {
1443*7c478bd9Sstevel@tonic-gate 		sub_hdr->default_error = 2;
1444*7c478bd9Sstevel@tonic-gate 	}
1445*7c478bd9Sstevel@tonic-gate 
1446*7c478bd9Sstevel@tonic-gate 	sub_hdr->source_len = source->size;
1447*7c478bd9Sstevel@tonic-gate 	sub_hdr->result_len = resultlen;
1448*7c478bd9Sstevel@tonic-gate 	sub_hdr->hash_tbl_size = (pair_size * hash_table_num);
1449*7c478bd9Sstevel@tonic-gate 	sub_hdr->hash_tbl_num = hash_table_num;
1450*7c478bd9Sstevel@tonic-gate 	sub_hdr->hash_of_size =
1451*7c478bd9Sstevel@tonic-gate 		(pair_size * of_table_num);
1452*7c478bd9Sstevel@tonic-gate 	sub_hdr->hash_of_num = of_table_num;
1453*7c478bd9Sstevel@tonic-gate 	sub_hdr->error_num = error_count; /* > 0; so pad4 = 0 */
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate 	/* specified map */
1456*7c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0, k = 0; i < num; i++) {
1457*7c478bd9Sstevel@tonic-gate 		hash_value =
1458*7c478bd9Sstevel@tonic-gate 			hash(NSPTR(&((*(tpp + i))->data0)),
1459*7c478bd9Sstevel@tonic-gate 				(*(tpp + i))->data0.size,
1460*7c478bd9Sstevel@tonic-gate 				hash_table_num);
1461*7c478bd9Sstevel@tonic-gate 		p = error_table + hash_value;
1462*7c478bd9Sstevel@tonic-gate 		if (*p) {	/* conflict */
1463*7c478bd9Sstevel@tonic-gate 			if (*p < 63) {
1464*7c478bd9Sstevel@tonic-gate 				*p += 1;
1465*7c478bd9Sstevel@tonic-gate 			}
1466*7c478bd9Sstevel@tonic-gate 			p = of_table + k;
1467*7c478bd9Sstevel@tonic-gate 			k += pair_size;
1468*7c478bd9Sstevel@tonic-gate 		} else {
1469*7c478bd9Sstevel@tonic-gate 			*p = 1;
1470*7c478bd9Sstevel@tonic-gate 			p = hash_table + (pair_size * hash_value);
1471*7c478bd9Sstevel@tonic-gate 		}
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 		(void) memcpy(p, NSPTR(&((*(tpp + i))->data0)), source->size);
1474*7c478bd9Sstevel@tonic-gate 		p += source->size;
1475*7c478bd9Sstevel@tonic-gate 		if (0 == (*(tpp + i))->data1.size) {
1476*7c478bd9Sstevel@tonic-gate 			(*p) = 1; /* specified error */
1477*7c478bd9Sstevel@tonic-gate 			p++;
1478*7c478bd9Sstevel@tonic-gate 		} else {
1479*7c478bd9Sstevel@tonic-gate 			/* (*p) = 0; ** valid */
1480*7c478bd9Sstevel@tonic-gate 			p++;
1481*7c478bd9Sstevel@tonic-gate 			(void) memset(p, 0,
1482*7c478bd9Sstevel@tonic-gate 				(resultlen - (*(tpp + i))->data1.size));
1483*7c478bd9Sstevel@tonic-gate 			(void) memcpy(p +
1484*7c478bd9Sstevel@tonic-gate 				(resultlen - (*(tpp + i))->data1.size),
1485*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data1)),
1486*7c478bd9Sstevel@tonic-gate 			(*(tpp + i))->data1.size);
1487*7c478bd9Sstevel@tonic-gate 		}
1488*7c478bd9Sstevel@tonic-gate 
1489*7c478bd9Sstevel@tonic-gate 		if ((*(tpp + i))->range.size != 0) {
1490*7c478bd9Sstevel@tonic-gate 			(void) memcpy(source_data,
1491*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data0)),
1492*7c478bd9Sstevel@tonic-gate 				source->size);
1493*7c478bd9Sstevel@tonic-gate 			(void) memset(result_data, 0,
1494*7c478bd9Sstevel@tonic-gate 				(resultlen  - (*(tpp + i))->data1.size));
1495*7c478bd9Sstevel@tonic-gate 			(void) memcpy(result_data +
1496*7c478bd9Sstevel@tonic-gate 				(resultlen  - (*(tpp + i))->data1.size),
1497*7c478bd9Sstevel@tonic-gate 				NSPTR(&((*(tpp + i))->data1)),
1498*7c478bd9Sstevel@tonic-gate 				(*(tpp + i))->data1.size);
1499*7c478bd9Sstevel@tonic-gate 			h = map_table_num_range((*(tpp + i)));
1500*7c478bd9Sstevel@tonic-gate 			for (n = 0; n < h; n++) {
1501*7c478bd9Sstevel@tonic-gate 				for (m = 0,
1502*7c478bd9Sstevel@tonic-gate 				    dp = (uchar_t *)
1503*7c478bd9Sstevel@tonic-gate 				    (source_data + source->size - 1);
1504*7c478bd9Sstevel@tonic-gate 				    m < source->size;
1505*7c478bd9Sstevel@tonic-gate 				    m++, dp--) {
1506*7c478bd9Sstevel@tonic-gate 					if (0xff != *dp) {
1507*7c478bd9Sstevel@tonic-gate 						(*dp) += 1;
1508*7c478bd9Sstevel@tonic-gate 						for (++dp; m > 0; m--, dp++) {
1509*7c478bd9Sstevel@tonic-gate 							(*dp) = 0x00;
1510*7c478bd9Sstevel@tonic-gate 						}
1511*7c478bd9Sstevel@tonic-gate 						break;
1512*7c478bd9Sstevel@tonic-gate 					}
1513*7c478bd9Sstevel@tonic-gate 				}
1514*7c478bd9Sstevel@tonic-gate 
1515*7c478bd9Sstevel@tonic-gate 				hash_value = hash((char *)source_data,
1516*7c478bd9Sstevel@tonic-gate 						source->size,
1517*7c478bd9Sstevel@tonic-gate 						hash_table_num);
1518*7c478bd9Sstevel@tonic-gate 				p = error_table + hash_value;
1519*7c478bd9Sstevel@tonic-gate 				if (*p) {	/* conflict */
1520*7c478bd9Sstevel@tonic-gate 					if (*p < 63) {
1521*7c478bd9Sstevel@tonic-gate 						*p += 1;
1522*7c478bd9Sstevel@tonic-gate 					}
1523*7c478bd9Sstevel@tonic-gate 					p = of_table + k;
1524*7c478bd9Sstevel@tonic-gate 					k += pair_size;
1525*7c478bd9Sstevel@tonic-gate 				} else {
1526*7c478bd9Sstevel@tonic-gate 					*p = 1;
1527*7c478bd9Sstevel@tonic-gate 					p = hash_table +
1528*7c478bd9Sstevel@tonic-gate 						(pair_size * hash_value);
1529*7c478bd9Sstevel@tonic-gate 				}
1530*7c478bd9Sstevel@tonic-gate 				(void) memcpy(p, source_data, source->size);
1531*7c478bd9Sstevel@tonic-gate 				p += source->size;
1532*7c478bd9Sstevel@tonic-gate 
1533*7c478bd9Sstevel@tonic-gate 				if (0 == (*(tpp + i))->data1.size) {
1534*7c478bd9Sstevel@tonic-gate 					(*p) = 1; /* specified error */
1535*7c478bd9Sstevel@tonic-gate 					p += 1;
1536*7c478bd9Sstevel@tonic-gate 				} else {
1537*7c478bd9Sstevel@tonic-gate 					/* (*p) = 0; ** valid */
1538*7c478bd9Sstevel@tonic-gate 					p += 1;
1539*7c478bd9Sstevel@tonic-gate 					for (m = 0, dp = (uchar_t *)
1540*7c478bd9Sstevel@tonic-gate 					    (result_data + resultlen - 1);
1541*7c478bd9Sstevel@tonic-gate 					    m < resultlen;
1542*7c478bd9Sstevel@tonic-gate 					    m++, dp--) {
1543*7c478bd9Sstevel@tonic-gate 						if (0xff != *dp) {
1544*7c478bd9Sstevel@tonic-gate 							(*dp) += 1;
1545*7c478bd9Sstevel@tonic-gate 							for (++dp; m > 0;
1546*7c478bd9Sstevel@tonic-gate 							    m--, dp++) {
1547*7c478bd9Sstevel@tonic-gate 								(*dp) = 0x00;
1548*7c478bd9Sstevel@tonic-gate 							}
1549*7c478bd9Sstevel@tonic-gate 							break;
1550*7c478bd9Sstevel@tonic-gate 						}
1551*7c478bd9Sstevel@tonic-gate 					}
1552*7c478bd9Sstevel@tonic-gate 					(void) memcpy(p,
1553*7c478bd9Sstevel@tonic-gate 						result_data, resultlen);
1554*7c478bd9Sstevel@tonic-gate 				}
1555*7c478bd9Sstevel@tonic-gate 			}
1556*7c478bd9Sstevel@tonic-gate 		}
1557*7c478bd9Sstevel@tonic-gate 	}
1558*7c478bd9Sstevel@tonic-gate 	free(source_data);
1559*7c478bd9Sstevel@tonic-gate 	free(result_data);
1560*7c478bd9Sstevel@tonic-gate 
1561*7c478bd9Sstevel@tonic-gate 	/* default */
1562*7c478bd9Sstevel@tonic-gate 	if ((NULL != default_data) &&
1563*7c478bd9Sstevel@tonic-gate 	    (((itm_data_t *)(-1)) != default_data)) {
1564*7c478bd9Sstevel@tonic-gate 		j = ((pair_size * hash_table_num) +
1565*7c478bd9Sstevel@tonic-gate 			(pair_size * of_table_num));
1566*7c478bd9Sstevel@tonic-gate 		(void) memcpy(hash_table + j + (resultlen - default_data->size),
1567*7c478bd9Sstevel@tonic-gate 			NSPTR(default_data), default_data->size);
1568*7c478bd9Sstevel@tonic-gate 	}
1569*7c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
1570*7c478bd9Sstevel@tonic-gate 	for (i = 0, p = of_table; i < of_table_num; i++, p += 5) {
1571*7c478bd9Sstevel@tonic-gate 		(void) printf("0x%02x%02x%02x%02x	0x%02x\n",
1572*7c478bd9Sstevel@tonic-gate 			((unsigned char)(*(p + 0))),
1573*7c478bd9Sstevel@tonic-gate 			((unsigned char)(*(p + 1))),
1574*7c478bd9Sstevel@tonic-gate 			((unsigned char)(*(p + 2))),
1575*7c478bd9Sstevel@tonic-gate 			((unsigned char)(*(p + 3))),
1576*7c478bd9Sstevel@tonic-gate 			((unsigned char)(*(p + 4))));
1577*7c478bd9Sstevel@tonic-gate 	}
1578*7c478bd9Sstevel@tonic-gate #endif
1579*7c478bd9Sstevel@tonic-gate 	return	(header);
1580*7c478bd9Sstevel@tonic-gate }
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate 
1583*7c478bd9Sstevel@tonic-gate 
1584*7c478bd9Sstevel@tonic-gate 
1585*7c478bd9Sstevel@tonic-gate static itm_tbl_hdr_t *
1586*7c478bd9Sstevel@tonic-gate map_table_dense_encoding(
1587*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	**tpp,
1588*7c478bd9Sstevel@tonic-gate 	itm_size_t		num,
1589*7c478bd9Sstevel@tonic-gate 	itm_data_t		*default_data,
1590*7c478bd9Sstevel@tonic-gate 	unsigned long		entry_num,
1591*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_min,
1592*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_max,
1593*7c478bd9Sstevel@tonic-gate 	long			resultlen,
1594*7c478bd9Sstevel@tonic-gate 	itm_num_t		error_count)
1595*7c478bd9Sstevel@tonic-gate {
1596*7c478bd9Sstevel@tonic-gate 
1597*7c478bd9Sstevel@tonic-gate 	itm_tbl_hdr_t		*header;
1598*7c478bd9Sstevel@tonic-gate 	itm_map_dense_enc_hdr_t	*sub_hdr;
1599*7c478bd9Sstevel@tonic-gate 	char			*table;
1600*7c478bd9Sstevel@tonic-gate 	char			*error_table;
1601*7c478bd9Sstevel@tonic-gate 	itm_size_t		table_size;
1602*7c478bd9Sstevel@tonic-gate 	itm_size_t		j;
1603*7c478bd9Sstevel@tonic-gate 	itm_size_t		i;
1604*7c478bd9Sstevel@tonic-gate 	itm_size_t		k;
1605*7c478bd9Sstevel@tonic-gate 	char			*p;
1606*7c478bd9Sstevel@tonic-gate 	itm_data_t		*source;
1607*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_def;
1608*7c478bd9Sstevel@tonic-gate 
1609*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('m', ("map_table_dense_encoding : %ld\n", num));
1610*7c478bd9Sstevel@tonic-gate 
1611*7c478bd9Sstevel@tonic-gate 	source = &((*(tpp + 0))->data0);
1612*7c478bd9Sstevel@tonic-gate 
1613*7c478bd9Sstevel@tonic-gate 
1614*7c478bd9Sstevel@tonic-gate 	table_size = ((sizeof (itm_tbl_hdr_t)) +
1615*7c478bd9Sstevel@tonic-gate 			(sizeof (itm_map_dense_enc_hdr_t)) +
1616*7c478bd9Sstevel@tonic-gate 			(source->size + source->size) +
1617*7c478bd9Sstevel@tonic-gate 			(resultlen * entry_num));
1618*7c478bd9Sstevel@tonic-gate 	if (0 < error_count) {
1619*7c478bd9Sstevel@tonic-gate 		table_size += entry_num;
1620*7c478bd9Sstevel@tonic-gate 	}
1621*7c478bd9Sstevel@tonic-gate 	if (NULL == default_data) {
1622*7c478bd9Sstevel@tonic-gate 		if ((num < entry_num) ||
1623*7c478bd9Sstevel@tonic-gate 		    (error_count <= 0)) {
1624*7c478bd9Sstevel@tonic-gate 			table_size += entry_num;
1625*7c478bd9Sstevel@tonic-gate 		}
1626*7c478bd9Sstevel@tonic-gate 	} else if ((itm_data_t *)(-1) != default_data) {
1627*7c478bd9Sstevel@tonic-gate 		table_size += resultlen;
1628*7c478bd9Sstevel@tonic-gate 	}
1629*7c478bd9Sstevel@tonic-gate 
1630*7c478bd9Sstevel@tonic-gate 	table_size = ITMROUNDUP(table_size);
1631*7c478bd9Sstevel@tonic-gate 	header = malloc_vital(table_size);
1632*7c478bd9Sstevel@tonic-gate 	sub_hdr = (itm_map_dense_enc_hdr_t *)(header + 1);
1633*7c478bd9Sstevel@tonic-gate 	table = (char *)(sub_hdr + 1) + source->size + source->size;
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 	header->type = ITM_TBL_MAP_DENSE_ENC;
1636*7c478bd9Sstevel@tonic-gate 	header->name.itm_ptr = 0;
1637*7c478bd9Sstevel@tonic-gate 	header->size = table_size;
1638*7c478bd9Sstevel@tonic-gate 	header->number = entry_num;
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate 	sub_hdr->source_len = (*tpp)->data0.size;
1641*7c478bd9Sstevel@tonic-gate 	sub_hdr->result_len = resultlen;
1642*7c478bd9Sstevel@tonic-gate 	sub_hdr->error_num = error_count; /* > 0; so pad4 = 0 */
1643*7c478bd9Sstevel@tonic-gate 
1644*7c478bd9Sstevel@tonic-gate 	if (NULL != default_data) {
1645*7c478bd9Sstevel@tonic-gate 		if ((itm_data_t *)(-1) == default_data) {
1646*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = -1;
1647*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
1648*7c478bd9Sstevel@tonic-gate 			sub_hdr->pad3_num = (pad_t)(~0);
1649*7c478bd9Sstevel@tonic-gate #endif
1650*7c478bd9Sstevel@tonic-gate 
1651*7c478bd9Sstevel@tonic-gate 		} else {
1652*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 0;
1653*7c478bd9Sstevel@tonic-gate 		}
1654*7c478bd9Sstevel@tonic-gate 	} else {
1655*7c478bd9Sstevel@tonic-gate 		if (num < entry_num) {
1656*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 1;
1657*7c478bd9Sstevel@tonic-gate 		} else {
1658*7c478bd9Sstevel@tonic-gate 			sub_hdr->default_error = 2;
1659*7c478bd9Sstevel@tonic-gate 		}
1660*7c478bd9Sstevel@tonic-gate 	}
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)(sub_hdr + 1), byte_seq_min, source->size);
1663*7c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)(sub_hdr + 1) + source->size,
1664*7c478bd9Sstevel@tonic-gate 		byte_seq_max, source->size);
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate 	if (-1 == sub_hdr->default_error) {
1667*7c478bd9Sstevel@tonic-gate 		byte_seq_def = malloc_vital((sizeof (unsigned char *)) *
1668*7c478bd9Sstevel@tonic-gate 					    resultlen);
1669*7c478bd9Sstevel@tonic-gate 		if (source->size != resultlen) {
1670*7c478bd9Sstevel@tonic-gate 			itm_error(
1671*7c478bd9Sstevel@tonic-gate 				gettext("\"default no_change_copy\" is "
1672*7c478bd9Sstevel@tonic-gate 				"specified, but size does not match\n"));
1673*7c478bd9Sstevel@tonic-gate 			exit(ITMC_STATUS_BT);
1674*7c478bd9Sstevel@tonic-gate 		}
1675*7c478bd9Sstevel@tonic-gate 		put_dense_encoding_default(
1676*7c478bd9Sstevel@tonic-gate 			table, byte_seq_min, byte_seq_max, byte_seq_def,
1677*7c478bd9Sstevel@tonic-gate 			resultlen - 1, 0, 0);
1678*7c478bd9Sstevel@tonic-gate 		free(byte_seq_def);
1679*7c478bd9Sstevel@tonic-gate 	} else if (0 == sub_hdr->default_error) {
1680*7c478bd9Sstevel@tonic-gate 		if (default_data->size <= (sizeof (itm_place_t))) {
1681*7c478bd9Sstevel@tonic-gate 			for (i = 0, j = 0;
1682*7c478bd9Sstevel@tonic-gate 			    i < (entry_num + 1); /* 1:default data */
1683*7c478bd9Sstevel@tonic-gate 			    i++, j += resultlen) {
1684*7c478bd9Sstevel@tonic-gate 				(void) memcpy(table + j +
1685*7c478bd9Sstevel@tonic-gate 				(resultlen - default_data->size),
1686*7c478bd9Sstevel@tonic-gate 				(void *)(&(default_data->place.itm_64d)),
1687*7c478bd9Sstevel@tonic-gate 				default_data->size);
1688*7c478bd9Sstevel@tonic-gate 			}
1689*7c478bd9Sstevel@tonic-gate 		} else {
1690*7c478bd9Sstevel@tonic-gate 			for (i = 0, j = 0;
1691*7c478bd9Sstevel@tonic-gate 			    i < (entry_num + 1);  /* 1:default data */
1692*7c478bd9Sstevel@tonic-gate 			    i++, j += resultlen) {
1693*7c478bd9Sstevel@tonic-gate 				(void) memcpy(table + j +
1694*7c478bd9Sstevel@tonic-gate 				(resultlen - default_data->size),
1695*7c478bd9Sstevel@tonic-gate 				(void *)(default_data->place.itm_ptr),
1696*7c478bd9Sstevel@tonic-gate 				default_data->size);
1697*7c478bd9Sstevel@tonic-gate 			}
1698*7c478bd9Sstevel@tonic-gate 		}
1699*7c478bd9Sstevel@tonic-gate 	}
1700*7c478bd9Sstevel@tonic-gate 	if (1 == sub_hdr->default_error) {
1701*7c478bd9Sstevel@tonic-gate 		(void) memset(table + (resultlen * entry_num), 1, entry_num);
1702*7c478bd9Sstevel@tonic-gate 		error_table = (table + (resultlen * entry_num));
1703*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
1704*7c478bd9Sstevel@tonic-gate 			if (0 == (*(tpp + i))->data1.size) {
1705*7c478bd9Sstevel@tonic-gate 				continue; /* error sequence */
1706*7c478bd9Sstevel@tonic-gate 			}
1707*7c478bd9Sstevel@tonic-gate 			j = hash_dense_encoding(NSPTR(&((*(tpp + i))->data0)),
1708*7c478bd9Sstevel@tonic-gate 						(*(tpp + i))->data0.size,
1709*7c478bd9Sstevel@tonic-gate 						byte_seq_min, byte_seq_max);
1710*7c478bd9Sstevel@tonic-gate 			k = ((*(tpp + i))->range.size) == 0 ? j :
1711*7c478bd9Sstevel@tonic-gate 			    hash_dense_encoding(NSPTR(&((*(tpp + i))->range)),
1712*7c478bd9Sstevel@tonic-gate 						(*(tpp + i))->data0.size,
1713*7c478bd9Sstevel@tonic-gate 						byte_seq_min, byte_seq_max);
1714*7c478bd9Sstevel@tonic-gate 			for (; j <= k; j++) {
1715*7c478bd9Sstevel@tonic-gate 				*(error_table + j) = 0;
1716*7c478bd9Sstevel@tonic-gate 			}
1717*7c478bd9Sstevel@tonic-gate 		}
1718*7c478bd9Sstevel@tonic-gate 	} else if (0 < error_count) {
1719*7c478bd9Sstevel@tonic-gate 		error_table = (table + (resultlen * entry_num));
1720*7c478bd9Sstevel@tonic-gate 		if (0 == sub_hdr->default_error) {
1721*7c478bd9Sstevel@tonic-gate 			error_table += resultlen;
1722*7c478bd9Sstevel@tonic-gate 		}
1723*7c478bd9Sstevel@tonic-gate 		(void) memset(error_table, 0, entry_num);
1724*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
1725*7c478bd9Sstevel@tonic-gate 			if (0 == (*(tpp + i))->data1.size) {
1726*7c478bd9Sstevel@tonic-gate 				j = hash_dense_encoding(
1727*7c478bd9Sstevel@tonic-gate 					NSPTR(&((*(tpp + i))->data0)),
1728*7c478bd9Sstevel@tonic-gate 						(*(tpp + i))->data0.size,
1729*7c478bd9Sstevel@tonic-gate 						byte_seq_min, byte_seq_max);
1730*7c478bd9Sstevel@tonic-gate 				k = ((*(tpp + i))->range.size) == 0 ? j :
1731*7c478bd9Sstevel@tonic-gate 					hash_dense_encoding(
1732*7c478bd9Sstevel@tonic-gate 					NSPTR(&((*(tpp + i))->range)),
1733*7c478bd9Sstevel@tonic-gate 					(*(tpp + i))->data0.size,
1734*7c478bd9Sstevel@tonic-gate 					byte_seq_min, byte_seq_max);
1735*7c478bd9Sstevel@tonic-gate 				for (; j <= k; j++) {
1736*7c478bd9Sstevel@tonic-gate 					*(error_table + j) = 1; /* specified */
1737*7c478bd9Sstevel@tonic-gate 				}
1738*7c478bd9Sstevel@tonic-gate 			}
1739*7c478bd9Sstevel@tonic-gate 		}
1740*7c478bd9Sstevel@tonic-gate 	}
1741*7c478bd9Sstevel@tonic-gate 
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate 	p = malloc_vital(resultlen);
1744*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
1745*7c478bd9Sstevel@tonic-gate 		j = hash_dense_encoding(NSPTR(&((*(tpp + i))->data0)),
1746*7c478bd9Sstevel@tonic-gate 					(*(tpp + i))->data0.size,
1747*7c478bd9Sstevel@tonic-gate 					byte_seq_min, byte_seq_max);
1748*7c478bd9Sstevel@tonic-gate 
1749*7c478bd9Sstevel@tonic-gate 		if (0 != (*(tpp + i))->range.size)
1750*7c478bd9Sstevel@tonic-gate 			k = hash_dense_encoding(
1751*7c478bd9Sstevel@tonic-gate 						NSPTR(&((*(tpp + i))->range)),
1752*7c478bd9Sstevel@tonic-gate 						(*(tpp + i))->range.size,
1753*7c478bd9Sstevel@tonic-gate 						byte_seq_min, byte_seq_max);
1754*7c478bd9Sstevel@tonic-gate 		else
1755*7c478bd9Sstevel@tonic-gate 			k = j;
1756*7c478bd9Sstevel@tonic-gate 		(void) memset(p, 0, (resultlen	 - (*(tpp + i))->data1.size));
1757*7c478bd9Sstevel@tonic-gate 		(void) memcpy(p + (resultlen  - (*(tpp + i))->data1.size),
1758*7c478bd9Sstevel@tonic-gate 			((caddr_t)NSPTR(&((*(tpp + i))->data1))),
1759*7c478bd9Sstevel@tonic-gate 			(*(tpp + i))->data1.size);
1760*7c478bd9Sstevel@tonic-gate 		map_range_make_result(table, j, k, p, resultlen);
1761*7c478bd9Sstevel@tonic-gate 	}
1762*7c478bd9Sstevel@tonic-gate 	free(p);
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	return	(header);
1765*7c478bd9Sstevel@tonic-gate }
1766*7c478bd9Sstevel@tonic-gate 
1767*7c478bd9Sstevel@tonic-gate 
1768*7c478bd9Sstevel@tonic-gate static void
1769*7c478bd9Sstevel@tonic-gate put_dense_encoding_default(
1770*7c478bd9Sstevel@tonic-gate 	char	*table,
1771*7c478bd9Sstevel@tonic-gate 	unsigned char	*byte_seq_min,
1772*7c478bd9Sstevel@tonic-gate 	unsigned char	*byte_seq_max,
1773*7c478bd9Sstevel@tonic-gate 	unsigned char	*byte_seq_def,
1774*7c478bd9Sstevel@tonic-gate 	long		pos_max,
1775*7c478bd9Sstevel@tonic-gate 	long		position,
1776*7c478bd9Sstevel@tonic-gate 	long		dense_encoded_value)
1777*7c478bd9Sstevel@tonic-gate {
1778*7c478bd9Sstevel@tonic-gate 	uchar_t	i;
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 	if (position < pos_max) {
1781*7c478bd9Sstevel@tonic-gate 		for (i = *(byte_seq_min + position);
1782*7c478bd9Sstevel@tonic-gate 		    i <= *(byte_seq_max + position); i++) {
1783*7c478bd9Sstevel@tonic-gate 			*(byte_seq_def + position) = i;
1784*7c478bd9Sstevel@tonic-gate 			put_dense_encoding_default(
1785*7c478bd9Sstevel@tonic-gate 				table,
1786*7c478bd9Sstevel@tonic-gate 				byte_seq_min, byte_seq_max,
1787*7c478bd9Sstevel@tonic-gate 				byte_seq_def,
1788*7c478bd9Sstevel@tonic-gate 				pos_max, position + 1,
1789*7c478bd9Sstevel@tonic-gate 				((dense_encoded_value + i) *
1790*7c478bd9Sstevel@tonic-gate 				(*(byte_seq_max + position) -
1791*7c478bd9Sstevel@tonic-gate 				*(byte_seq_min + position) + 1)));
1792*7c478bd9Sstevel@tonic-gate 		}
1793*7c478bd9Sstevel@tonic-gate 		return;
1794*7c478bd9Sstevel@tonic-gate 	}
1795*7c478bd9Sstevel@tonic-gate 
1796*7c478bd9Sstevel@tonic-gate 	for (i = *(byte_seq_min + position);
1797*7c478bd9Sstevel@tonic-gate 	    i <= *(byte_seq_max + position); i++) {
1798*7c478bd9Sstevel@tonic-gate 		*(byte_seq_def + position) = i;
1799*7c478bd9Sstevel@tonic-gate 		(void) memcpy(table +
1800*7c478bd9Sstevel@tonic-gate 			((pos_max + 1) * (dense_encoded_value + i - 1)),
1801*7c478bd9Sstevel@tonic-gate 			byte_seq_def, pos_max + 1);
1802*7c478bd9Sstevel@tonic-gate 	}
1803*7c478bd9Sstevel@tonic-gate }
1804*7c478bd9Sstevel@tonic-gate 
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate char *
1807*7c478bd9Sstevel@tonic-gate dense_enc_index_to_byte_seq(
1808*7c478bd9Sstevel@tonic-gate 	long		value,
1809*7c478bd9Sstevel@tonic-gate 	long		length,
1810*7c478bd9Sstevel@tonic-gate 	unsigned char	*byte_seq_min,
1811*7c478bd9Sstevel@tonic-gate 	unsigned char	*byte_seq_max)
1812*7c478bd9Sstevel@tonic-gate {
1813*7c478bd9Sstevel@tonic-gate 	static char	*buf;
1814*7c478bd9Sstevel@tonic-gate 	static long	buf_len;
1815*7c478bd9Sstevel@tonic-gate 	char		*p;
1816*7c478bd9Sstevel@tonic-gate 	int		i;
1817*7c478bd9Sstevel@tonic-gate 	int		l;
1818*7c478bd9Sstevel@tonic-gate 	int		residue;
1819*7c478bd9Sstevel@tonic-gate 
1820*7c478bd9Sstevel@tonic-gate 	if (buf_len < (2 + (length * 2) + 1)) {
1821*7c478bd9Sstevel@tonic-gate 		free(buf);
1822*7c478bd9Sstevel@tonic-gate 		buf_len = (2 + (length * 2) + 1) + 16;
1823*7c478bd9Sstevel@tonic-gate 		buf = malloc_vital(buf_len);
1824*7c478bd9Sstevel@tonic-gate 	}
1825*7c478bd9Sstevel@tonic-gate 
1826*7c478bd9Sstevel@tonic-gate 	*(buf + (length * 2)) = '\0';
1827*7c478bd9Sstevel@tonic-gate 	*(buf + 0) = '0';
1828*7c478bd9Sstevel@tonic-gate 	*(buf + 1) = 'x';
1829*7c478bd9Sstevel@tonic-gate 	p = buf + 2;
1830*7c478bd9Sstevel@tonic-gate 	for (i = length - 1; 0 <= i; --i) {
1831*7c478bd9Sstevel@tonic-gate 		residue = value % (*(byte_seq_max + i) -
1832*7c478bd9Sstevel@tonic-gate 					*(byte_seq_min + i) + 1);
1833*7c478bd9Sstevel@tonic-gate 		value /= (*(byte_seq_max + i) -
1834*7c478bd9Sstevel@tonic-gate 				*(byte_seq_min + i) + 1);
1835*7c478bd9Sstevel@tonic-gate 
1836*7c478bd9Sstevel@tonic-gate 		residue += *(byte_seq_min + i);
1837*7c478bd9Sstevel@tonic-gate 		l = ((0xf0 & residue) >> 4);
1838*7c478bd9Sstevel@tonic-gate 		if (l < 10) {
1839*7c478bd9Sstevel@tonic-gate 			*(p + (i * 2)) = ('0' + l);
1840*7c478bd9Sstevel@tonic-gate 		} else {
1841*7c478bd9Sstevel@tonic-gate 			*(p + (i * 2)) = ('a' + l - 10);
1842*7c478bd9Sstevel@tonic-gate 		}
1843*7c478bd9Sstevel@tonic-gate 		l = (0x0f & residue);
1844*7c478bd9Sstevel@tonic-gate 		if (l < 10) {
1845*7c478bd9Sstevel@tonic-gate 			*(p + (i * 2) + 1) = ('0' + l);
1846*7c478bd9Sstevel@tonic-gate 		} else {
1847*7c478bd9Sstevel@tonic-gate 			*(p + (i * 2) + 1) = ('a' + l - 10);
1848*7c478bd9Sstevel@tonic-gate 		}
1849*7c478bd9Sstevel@tonic-gate 	}
1850*7c478bd9Sstevel@tonic-gate 	return	(buf);
1851*7c478bd9Sstevel@tonic-gate }
1852*7c478bd9Sstevel@tonic-gate 
1853*7c478bd9Sstevel@tonic-gate 
1854*7c478bd9Sstevel@tonic-gate itm_tbl_hdr_t *
1855*7c478bd9Sstevel@tonic-gate map_table_lookup_var()
1856*7c478bd9Sstevel@tonic-gate {
1857*7c478bd9Sstevel@tonic-gate 	itm_error(gettext(
1858*7c478bd9Sstevel@tonic-gate 		"lenghth of all source sequences must be the same\n"));
1859*7c478bd9Sstevel@tonic-gate 	error_deferred += 1;
1860*7c478bd9Sstevel@tonic-gate 	return	(NULL);
1861*7c478bd9Sstevel@tonic-gate }
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 
1864*7c478bd9Sstevel@tonic-gate 
1865*7c478bd9Sstevel@tonic-gate static void
1866*7c478bd9Sstevel@tonic-gate map_range_adjust_byte_seq(
1867*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_min,
1868*7c478bd9Sstevel@tonic-gate 	unsigned char		*byte_seq_max,
1869*7c478bd9Sstevel@tonic-gate 	long			source_len,
1870*7c478bd9Sstevel@tonic-gate 	itmc_data_pair_t	*pair)
1871*7c478bd9Sstevel@tonic-gate {
1872*7c478bd9Sstevel@tonic-gate 	unsigned char		*p, *p2;
1873*7c478bd9Sstevel@tonic-gate 	int			i;
1874*7c478bd9Sstevel@tonic-gate 	int			flag;
1875*7c478bd9Sstevel@tonic-gate 
1876*7c478bd9Sstevel@tonic-gate 	p  = (unsigned char *)(NSPTR(&((pair)->data0)));
1877*7c478bd9Sstevel@tonic-gate 	p2 = (unsigned char *)(NSPTR(&((pair)->range)));
1878*7c478bd9Sstevel@tonic-gate 	flag = 0;
1879*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < source_len; i++) {
1880*7c478bd9Sstevel@tonic-gate 		if (flag != 0) {
1881*7c478bd9Sstevel@tonic-gate 			break;
1882*7c478bd9Sstevel@tonic-gate 		}
1883*7c478bd9Sstevel@tonic-gate 		if (*(p + i) != *(p2 + i))
1884*7c478bd9Sstevel@tonic-gate 			flag = 1;
1885*7c478bd9Sstevel@tonic-gate 		if (*(p + i) < *(byte_seq_min + i)) {
1886*7c478bd9Sstevel@tonic-gate 			*(byte_seq_min + i) = *(p + i);
1887*7c478bd9Sstevel@tonic-gate 		}
1888*7c478bd9Sstevel@tonic-gate 		if (*(byte_seq_max + i) < *(p2 + i)) {
1889*7c478bd9Sstevel@tonic-gate 			*(byte_seq_max + i) = *(p2 + i);
1890*7c478bd9Sstevel@tonic-gate 		}
1891*7c478bd9Sstevel@tonic-gate 	}
1892*7c478bd9Sstevel@tonic-gate 	for (; i < source_len; i++) {
1893*7c478bd9Sstevel@tonic-gate 		*(byte_seq_min + i) = 0x00;
1894*7c478bd9Sstevel@tonic-gate 		*(byte_seq_max + i) = 0xff;
1895*7c478bd9Sstevel@tonic-gate 	}
1896*7c478bd9Sstevel@tonic-gate }
1897*7c478bd9Sstevel@tonic-gate 
1898*7c478bd9Sstevel@tonic-gate /*
1899*7c478bd9Sstevel@tonic-gate  *	result value + (source range value - source base value)
1900*7c478bd9Sstevel@tonic-gate  *	and just caluculate its length
1901*7c478bd9Sstevel@tonic-gate  */
1902*7c478bd9Sstevel@tonic-gate static size_t
1903*7c478bd9Sstevel@tonic-gate map_table_resultlen(itmc_map_t		*ml)
1904*7c478bd9Sstevel@tonic-gate {
1905*7c478bd9Sstevel@tonic-gate 	size_t	j;
1906*7c478bd9Sstevel@tonic-gate 	size_t	len;
1907*7c478bd9Sstevel@tonic-gate 	int	m;
1908*7c478bd9Sstevel@tonic-gate 	uchar_t *c1;
1909*7c478bd9Sstevel@tonic-gate 	uchar_t *c2;
1910*7c478bd9Sstevel@tonic-gate 	uchar_t *c3;
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate 	j = ml->data_pair.data0.size;
1913*7c478bd9Sstevel@tonic-gate 	if (j < ml->data_pair.data1.size) j = ml->data_pair.data1.size;
1914*7c478bd9Sstevel@tonic-gate 	if (j < ml->data_pair.range.size) j = ml->data_pair.range.size;
1915*7c478bd9Sstevel@tonic-gate 	c1 = (uchar_t *)(NSPTR(&((ml->data_pair).data0))) +
1916*7c478bd9Sstevel@tonic-gate 			ml->data_pair.data0.size - 1;
1917*7c478bd9Sstevel@tonic-gate 	c2 = (uchar_t *)(NSPTR(&((ml->data_pair).data1))) +
1918*7c478bd9Sstevel@tonic-gate 			ml->data_pair.data1.size - 1;
1919*7c478bd9Sstevel@tonic-gate 	c3 = (uchar_t *)(NSPTR(&((ml->data_pair.range)))) +
1920*7c478bd9Sstevel@tonic-gate 			ml->data_pair.range.size - 1;
1921*7c478bd9Sstevel@tonic-gate 	m = 0;
1922*7c478bd9Sstevel@tonic-gate 	for (len = 0; len < j; len++, c1--, c2--, c3--) {
1923*7c478bd9Sstevel@tonic-gate 		if (len < ml->data_pair.data0.size) m -= *c1;
1924*7c478bd9Sstevel@tonic-gate 		if (len < ml->data_pair.data1.size) m += *c2;
1925*7c478bd9Sstevel@tonic-gate 		if (len < ml->data_pair.range.size) m += *c3;
1926*7c478bd9Sstevel@tonic-gate 		m >>= 8;
1927*7c478bd9Sstevel@tonic-gate 	}
1928*7c478bd9Sstevel@tonic-gate 	if (m > 0) {
1929*7c478bd9Sstevel@tonic-gate 		len += 1;
1930*7c478bd9Sstevel@tonic-gate 	}
1931*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('g', ("map_table_resutlen: source(0x%s..0x%s), "
1932*7c478bd9Sstevel@tonic-gate 			"result(0x%s.... len= %ld)\n",
1933*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&(ml->data_pair.data0)),
1934*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&(ml->data_pair.range)),
1935*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&(ml->data_pair.data1)),
1936*7c478bd9Sstevel@tonic-gate 			len));
1937*7c478bd9Sstevel@tonic-gate 	return (len);
1938*7c478bd9Sstevel@tonic-gate }
1939*7c478bd9Sstevel@tonic-gate 
1940*7c478bd9Sstevel@tonic-gate /*
1941*7c478bd9Sstevel@tonic-gate  *
1942*7c478bd9Sstevel@tonic-gate  */
1943*7c478bd9Sstevel@tonic-gate static void
1944*7c478bd9Sstevel@tonic-gate map_range_make_result(
1945*7c478bd9Sstevel@tonic-gate 	char		*table,
1946*7c478bd9Sstevel@tonic-gate 	itm_size_t	range_start,
1947*7c478bd9Sstevel@tonic-gate 	itm_size_t	range_end,
1948*7c478bd9Sstevel@tonic-gate 	char		*result_data,
1949*7c478bd9Sstevel@tonic-gate 	itm_size_t	result_size)
1950*7c478bd9Sstevel@tonic-gate {
1951*7c478bd9Sstevel@tonic-gate 	itm_size_t	i;
1952*7c478bd9Sstevel@tonic-gate 	itm_size_t	j;
1953*7c478bd9Sstevel@tonic-gate 	itm_size_t	p;
1954*7c478bd9Sstevel@tonic-gate 	uchar_t		*dp; /* unsigned for ++ operation */
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate 	for (i = range_start, p = i * result_size;
1957*7c478bd9Sstevel@tonic-gate 	    i <= range_end; i++, p += result_size) {
1958*7c478bd9Sstevel@tonic-gate 		(void) memcpy(table + p, result_data, result_size);
1959*7c478bd9Sstevel@tonic-gate 		for (j = 0, dp = (uchar_t *)(result_data + result_size - 1);
1960*7c478bd9Sstevel@tonic-gate 		    j < result_size;
1961*7c478bd9Sstevel@tonic-gate 		    j++, dp--) {
1962*7c478bd9Sstevel@tonic-gate 			if (0xff != *dp) {
1963*7c478bd9Sstevel@tonic-gate 				(*dp) += 1;
1964*7c478bd9Sstevel@tonic-gate 				for (++dp; j > 0; j--, dp++) {
1965*7c478bd9Sstevel@tonic-gate 					(*dp) = 0x00;
1966*7c478bd9Sstevel@tonic-gate 				}
1967*7c478bd9Sstevel@tonic-gate 				break;
1968*7c478bd9Sstevel@tonic-gate 			}
1969*7c478bd9Sstevel@tonic-gate 		}
1970*7c478bd9Sstevel@tonic-gate 	}
1971*7c478bd9Sstevel@tonic-gate }
1972*7c478bd9Sstevel@tonic-gate 
1973*7c478bd9Sstevel@tonic-gate /*
1974*7c478bd9Sstevel@tonic-gate  *
1975*7c478bd9Sstevel@tonic-gate  */
1976*7c478bd9Sstevel@tonic-gate static size_t
1977*7c478bd9Sstevel@tonic-gate map_table_num_range(itmc_data_pair_t	*pair)
1978*7c478bd9Sstevel@tonic-gate {
1979*7c478bd9Sstevel@tonic-gate 	size_t		i, j;
1980*7c478bd9Sstevel@tonic-gate 	itm_num_t	num;
1981*7c478bd9Sstevel@tonic-gate 	itm_num_t	num2;
1982*7c478bd9Sstevel@tonic-gate 	uchar_t		*c1;
1983*7c478bd9Sstevel@tonic-gate 	uchar_t		*c2;
1984*7c478bd9Sstevel@tonic-gate 
1985*7c478bd9Sstevel@tonic-gate 	assert(0 < pair->range.size);
1986*7c478bd9Sstevel@tonic-gate 	j = pair->data0.size;
1987*7c478bd9Sstevel@tonic-gate 	if (j < pair->range.size)
1988*7c478bd9Sstevel@tonic-gate 		j = pair->range.size;
1989*7c478bd9Sstevel@tonic-gate 	c1 = ((uchar_t *)(NSPTR(&(pair->data0)))) + pair->data0.size - 1;
1990*7c478bd9Sstevel@tonic-gate 	c2 = ((uchar_t *)(NSPTR(&(pair->range)))) + pair->range.size - 1;
1991*7c478bd9Sstevel@tonic-gate 	num = 0;
1992*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < j; i++, c1--, c2--) {
1993*7c478bd9Sstevel@tonic-gate 		if (i < pair->range.size) num2 = *c2;
1994*7c478bd9Sstevel@tonic-gate 		if (i < pair->data0.size) num2 -= *c1;
1995*7c478bd9Sstevel@tonic-gate 		TRACE_MESSAGE('G', (" num += %d(=%d-%d)\n ",
1996*7c478bd9Sstevel@tonic-gate 				*c2 - *c1, *c2, *c1));
1997*7c478bd9Sstevel@tonic-gate 		num2 <<= (i*8);
1998*7c478bd9Sstevel@tonic-gate 		num += num2;
1999*7c478bd9Sstevel@tonic-gate 	}
2000*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('g', ("map_table_num_range: source(0x%s..0x%s), "
2001*7c478bd9Sstevel@tonic-gate 			"num= %ld\n",
2002*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&(pair->data0)),
2003*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(&(pair->range)),
2004*7c478bd9Sstevel@tonic-gate 			num));
2005*7c478bd9Sstevel@tonic-gate 	return (num);
2006*7c478bd9Sstevel@tonic-gate }
2007*7c478bd9Sstevel@tonic-gate 
2008*7c478bd9Sstevel@tonic-gate /*
2009*7c478bd9Sstevel@tonic-gate  *
2010*7c478bd9Sstevel@tonic-gate  */
2011*7c478bd9Sstevel@tonic-gate itmc_map_t *
2012*7c478bd9Sstevel@tonic-gate map_list_append(itmc_map_t	*map_list, itmc_map_t	*map_pair)
2013*7c478bd9Sstevel@tonic-gate {
2014*7c478bd9Sstevel@tonic-gate 	if (0 == map_pair) {
2015*7c478bd9Sstevel@tonic-gate 		return	(map_list);
2016*7c478bd9Sstevel@tonic-gate 	}
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 	map_pair->next = NULL;
2019*7c478bd9Sstevel@tonic-gate 	map_pair->last = map_pair;
2020*7c478bd9Sstevel@tonic-gate 
2021*7c478bd9Sstevel@tonic-gate 	if (map_list) {
2022*7c478bd9Sstevel@tonic-gate 		map_list->last->next = map_pair;
2023*7c478bd9Sstevel@tonic-gate 		map_list->last = map_pair;
2024*7c478bd9Sstevel@tonic-gate 		return	(map_list);
2025*7c478bd9Sstevel@tonic-gate 	} else {
2026*7c478bd9Sstevel@tonic-gate 		return	(map_pair);
2027*7c478bd9Sstevel@tonic-gate 	}
2028*7c478bd9Sstevel@tonic-gate }
2029*7c478bd9Sstevel@tonic-gate 
2030*7c478bd9Sstevel@tonic-gate 
2031*7c478bd9Sstevel@tonic-gate 
2032*7c478bd9Sstevel@tonic-gate itmc_obj_t *
2033*7c478bd9Sstevel@tonic-gate op_self(itm_op_type_t type)
2034*7c478bd9Sstevel@tonic-gate {
2035*7c478bd9Sstevel@tonic-gate 	return (op_unit(type, NULL, 0, NULL, 0, NULL, 0));
2036*7c478bd9Sstevel@tonic-gate }
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 
2039*7c478bd9Sstevel@tonic-gate itmc_obj_t *
2040*7c478bd9Sstevel@tonic-gate op_unary(itm_op_type_t type, void	*data, size_t data_size)
2041*7c478bd9Sstevel@tonic-gate {
2042*7c478bd9Sstevel@tonic-gate 	return (op_unit(type, data, data_size, NULL, 0, NULL, 0));
2043*7c478bd9Sstevel@tonic-gate }
2044*7c478bd9Sstevel@tonic-gate 
2045*7c478bd9Sstevel@tonic-gate itmc_obj_t *
2046*7c478bd9Sstevel@tonic-gate op_unit(itm_op_type_t	type,
2047*7c478bd9Sstevel@tonic-gate 	void	*data0, size_t data0_size,
2048*7c478bd9Sstevel@tonic-gate 	void	*data1, size_t data1_size,
2049*7c478bd9Sstevel@tonic-gate 	void	*data2, size_t data2_size)
2050*7c478bd9Sstevel@tonic-gate {
2051*7c478bd9Sstevel@tonic-gate 	itm_op_t	*op;
2052*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*obj;
2053*7c478bd9Sstevel@tonic-gate 
2054*7c478bd9Sstevel@tonic-gate 	op = malloc_vital(sizeof (itm_op_t));
2055*7c478bd9Sstevel@tonic-gate 	op->type = type;
2056*7c478bd9Sstevel@tonic-gate 	op->data.operand[0].itm_ptr = (itm_place2_t)(data0);
2057*7c478bd9Sstevel@tonic-gate 	op->data.operand[1].itm_ptr = (itm_place2_t)(data1);
2058*7c478bd9Sstevel@tonic-gate 	op->data.operand[2].itm_ptr = (itm_place2_t)(data2);
2059*7c478bd9Sstevel@tonic-gate 
2060*7c478bd9Sstevel@tonic-gate 	obj = malloc_vital(sizeof (itmc_obj_t));
2061*7c478bd9Sstevel@tonic-gate 	obj->type = ITMC_OBJ_OP;
2062*7c478bd9Sstevel@tonic-gate 	obj->name = NULL;
2063*7c478bd9Sstevel@tonic-gate 	obj->obj = op;
2064*7c478bd9Sstevel@tonic-gate 	obj->ref[0] = obj->ref[1] = obj->ref[2] = NULL;
2065*7c478bd9Sstevel@tonic-gate 	if (NULL != data0) {
2066*7c478bd9Sstevel@tonic-gate 		obj->ref[0] = obj_register(ITMC_OBJ_EXPR, NULL,
2067*7c478bd9Sstevel@tonic-gate 						data0, data0_size,
2068*7c478bd9Sstevel@tonic-gate 						&(op->data.operand[0]),
2069*7c478bd9Sstevel@tonic-gate 						OBJ_REG_TAIL);
2070*7c478bd9Sstevel@tonic-gate 	}
2071*7c478bd9Sstevel@tonic-gate 	if (NULL != data1) {
2072*7c478bd9Sstevel@tonic-gate 		obj->ref[1] = obj_register(ITMC_OBJ_EXPR, NULL,
2073*7c478bd9Sstevel@tonic-gate 						data1, data1_size,
2074*7c478bd9Sstevel@tonic-gate 						&(op->data.operand[1]),
2075*7c478bd9Sstevel@tonic-gate 						OBJ_REG_TAIL);
2076*7c478bd9Sstevel@tonic-gate 	}
2077*7c478bd9Sstevel@tonic-gate 	if (NULL != data2) {
2078*7c478bd9Sstevel@tonic-gate 		obj->ref[2] = obj_register(ITMC_OBJ_EXPR, NULL,
2079*7c478bd9Sstevel@tonic-gate 						data2, data2_size,
2080*7c478bd9Sstevel@tonic-gate 						&(op->data.operand[2]),
2081*7c478bd9Sstevel@tonic-gate 						OBJ_REG_TAIL);
2082*7c478bd9Sstevel@tonic-gate 	}
2083*7c478bd9Sstevel@tonic-gate 	obj->next = NULL;
2084*7c478bd9Sstevel@tonic-gate 	obj->last = NULL;
2085*7c478bd9Sstevel@tonic-gate 
2086*7c478bd9Sstevel@tonic-gate 	return	(obj);
2087*7c478bd9Sstevel@tonic-gate }
2088*7c478bd9Sstevel@tonic-gate 
2089*7c478bd9Sstevel@tonic-gate 
2090*7c478bd9Sstevel@tonic-gate itmc_obj_t *
2091*7c478bd9Sstevel@tonic-gate op_self_num(itm_op_type_t type, itm_num_t data)
2092*7c478bd9Sstevel@tonic-gate {
2093*7c478bd9Sstevel@tonic-gate 	itm_op_t	*op;
2094*7c478bd9Sstevel@tonic-gate 	itmc_obj_t	*obj;
2095*7c478bd9Sstevel@tonic-gate 
2096*7c478bd9Sstevel@tonic-gate 	op = malloc_vital(sizeof (itm_op_t));
2097*7c478bd9Sstevel@tonic-gate 	op->type = type;
2098*7c478bd9Sstevel@tonic-gate 	op->data.itm_opnum = data;
2099*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2100*7c478bd9Sstevel@tonic-gate 	op->data.itm_oppad = (data < 0) ? (pad_t)(~0) : 0;
2101*7c478bd9Sstevel@tonic-gate #endif
2102*7c478bd9Sstevel@tonic-gate 	obj = malloc_vital(sizeof (itmc_obj_t));
2103*7c478bd9Sstevel@tonic-gate 	obj->type = ITMC_OBJ_OP;
2104*7c478bd9Sstevel@tonic-gate 	obj->name = NULL;
2105*7c478bd9Sstevel@tonic-gate 	obj->obj = op;
2106*7c478bd9Sstevel@tonic-gate 	obj->ref[0] = obj->ref[1] = obj->ref[2] = NULL;
2107*7c478bd9Sstevel@tonic-gate 
2108*7c478bd9Sstevel@tonic-gate 	return	(obj);
2109*7c478bd9Sstevel@tonic-gate }
2110*7c478bd9Sstevel@tonic-gate 
2111*7c478bd9Sstevel@tonic-gate 
2112*7c478bd9Sstevel@tonic-gate itm_expr_t *
2113*7c478bd9Sstevel@tonic-gate expr_self_num(itm_expr_type_t type, itm_num_t data)
2114*7c478bd9Sstevel@tonic-gate {
2115*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2116*7c478bd9Sstevel@tonic-gate 
2117*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2118*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2119*7c478bd9Sstevel@tonic-gate 	expr->data.itm_exnum = data;
2120*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2121*7c478bd9Sstevel@tonic-gate 	expr->data.itm_expad = (data < 0) ? (pad_t)(~0) : 0;
2122*7c478bd9Sstevel@tonic-gate #endif
2123*7c478bd9Sstevel@tonic-gate 	return	(expr);
2124*7c478bd9Sstevel@tonic-gate }
2125*7c478bd9Sstevel@tonic-gate 
2126*7c478bd9Sstevel@tonic-gate 
2127*7c478bd9Sstevel@tonic-gate itm_expr_t *
2128*7c478bd9Sstevel@tonic-gate expr_self(itm_expr_type_t type, itm_data_t	*data)
2129*7c478bd9Sstevel@tonic-gate {
2130*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2131*7c478bd9Sstevel@tonic-gate 	itmc_name_t	*name;
2132*7c478bd9Sstevel@tonic-gate 
2133*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2134*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2135*7c478bd9Sstevel@tonic-gate 	if (NULL == data) {
2136*7c478bd9Sstevel@tonic-gate 		expr->data.value.size = 0;
2137*7c478bd9Sstevel@tonic-gate 		expr->data.value.place.itm_ptr = 0;
2138*7c478bd9Sstevel@tonic-gate 	} else {
2139*7c478bd9Sstevel@tonic-gate 		expr->data.value = *(data);
2140*7c478bd9Sstevel@tonic-gate 	}
2141*7c478bd9Sstevel@tonic-gate 
2142*7c478bd9Sstevel@tonic-gate 	switch (type) {
2143*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_NAME: /* register */
2144*7c478bd9Sstevel@tonic-gate 		name = name_lookup(data, ITMC_OBJ_REGISTER);
2145*7c478bd9Sstevel@tonic-gate 		if (&name_lookup_error == name) {
2146*7c478bd9Sstevel@tonic-gate 			return	(NULL);
2147*7c478bd9Sstevel@tonic-gate 		} else if (NULL == name) {
2148*7c478bd9Sstevel@tonic-gate 			if (reg_id >= MAXREGID) {
2149*7c478bd9Sstevel@tonic-gate 			itm_error(
2150*7c478bd9Sstevel@tonic-gate 				gettext("more than %d variables are used\n"),
2151*7c478bd9Sstevel@tonic-gate 				MAXREGID);
2152*7c478bd9Sstevel@tonic-gate 				exit(ITMC_STATUS_BT2);
2153*7c478bd9Sstevel@tonic-gate 			}
2154*7c478bd9Sstevel@tonic-gate 			name = name_register(data, ITMC_OBJ_REGISTER, NULL);
2155*7c478bd9Sstevel@tonic-gate 			name->reg_id = (reg_id++);
2156*7c478bd9Sstevel@tonic-gate 		}
2157*7c478bd9Sstevel@tonic-gate 		expr->type = ITM_EXPR_REG;
2158*7c478bd9Sstevel@tonic-gate 		expr->data.itm_exnum = name->reg_id;
2159*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2160*7c478bd9Sstevel@tonic-gate 		expr->data.itm_expad =
2161*7c478bd9Sstevel@tonic-gate 			(expr->data.itm_exnum < 0) ? (pad_t)(~0) : 0;
2162*7c478bd9Sstevel@tonic-gate #endif
2163*7c478bd9Sstevel@tonic-gate 		break;
2164*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_SEQ:
2165*7c478bd9Sstevel@tonic-gate 		if ((sizeof (itm_place_t)) < data->size) {
2166*7c478bd9Sstevel@tonic-gate 			(void) obj_register(ITMC_OBJ_DATA, NULL,
2167*7c478bd9Sstevel@tonic-gate 				(void *)(data->place.itm_ptr), data->size,
2168*7c478bd9Sstevel@tonic-gate 				&(expr->data.value.place), OBJ_REG_TAIL);
2169*7c478bd9Sstevel@tonic-gate 		}
2170*7c478bd9Sstevel@tonic-gate 		break;
2171*7c478bd9Sstevel@tonic-gate 	}
2172*7c478bd9Sstevel@tonic-gate 	return	(expr);
2173*7c478bd9Sstevel@tonic-gate }
2174*7c478bd9Sstevel@tonic-gate 
2175*7c478bd9Sstevel@tonic-gate 
2176*7c478bd9Sstevel@tonic-gate itm_expr_t *
2177*7c478bd9Sstevel@tonic-gate expr_unary(itm_expr_type_t type, itm_expr_t *data0)
2178*7c478bd9Sstevel@tonic-gate {
2179*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2180*7c478bd9Sstevel@tonic-gate 
2181*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2182*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2183*7c478bd9Sstevel@tonic-gate 	expr->data.operand[0].itm_ptr = (itm_place2_t)(data0);
2184*7c478bd9Sstevel@tonic-gate 	(void) obj_register(ITMC_OBJ_EXPR, NULL,
2185*7c478bd9Sstevel@tonic-gate 			data0, sizeof (itm_expr_t),
2186*7c478bd9Sstevel@tonic-gate 			&(expr->data.operand[0]), OBJ_REG_TAIL);
2187*7c478bd9Sstevel@tonic-gate 
2188*7c478bd9Sstevel@tonic-gate 	return	(expr);
2189*7c478bd9Sstevel@tonic-gate }
2190*7c478bd9Sstevel@tonic-gate 
2191*7c478bd9Sstevel@tonic-gate 
2192*7c478bd9Sstevel@tonic-gate itm_expr_t *
2193*7c478bd9Sstevel@tonic-gate expr_binary(itm_expr_type_t type,
2194*7c478bd9Sstevel@tonic-gate 	    itm_expr_t		*data0, itm_expr_t	*data1)
2195*7c478bd9Sstevel@tonic-gate {
2196*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2197*7c478bd9Sstevel@tonic-gate 	itm_num_t	num;
2198*7c478bd9Sstevel@tonic-gate 	unsigned char	*p;
2199*7c478bd9Sstevel@tonic-gate 	int		i;
2200*7c478bd9Sstevel@tonic-gate 
2201*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2202*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2203*7c478bd9Sstevel@tonic-gate 
2204*7c478bd9Sstevel@tonic-gate 	if (ITM_EXPR_SEQ == data0->type) {
2205*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)NSPTR(&(data0->data.value));
2206*7c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0; i < data0->data.value.size; i++, p++) {
2207*7c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
2208*7c478bd9Sstevel@tonic-gate 		}
2209*7c478bd9Sstevel@tonic-gate 		data0 = expr_self_num(ITM_EXPR_INT, num);
2210*7c478bd9Sstevel@tonic-gate 	}
2211*7c478bd9Sstevel@tonic-gate 	if (ITM_EXPR_SEQ == data1->type) {
2212*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)NSPTR(&(data1->data.value));
2213*7c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0; i < data1->data.value.size; i++, p++) {
2214*7c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
2215*7c478bd9Sstevel@tonic-gate 		}
2216*7c478bd9Sstevel@tonic-gate 		data1 = expr_self_num(ITM_EXPR_INT, num);
2217*7c478bd9Sstevel@tonic-gate 	}
2218*7c478bd9Sstevel@tonic-gate 
2219*7c478bd9Sstevel@tonic-gate 	expr->data.operand[0].itm_ptr = (itm_place2_t)(data0);
2220*7c478bd9Sstevel@tonic-gate 	expr->data.operand[1].itm_ptr = (itm_place2_t)(data1);
2221*7c478bd9Sstevel@tonic-gate 
2222*7c478bd9Sstevel@tonic-gate 	(void) obj_register(ITMC_OBJ_EXPR, NULL,
2223*7c478bd9Sstevel@tonic-gate 			data0, sizeof (itm_expr_t),
2224*7c478bd9Sstevel@tonic-gate 			&(expr->data.operand[0]), OBJ_REG_TAIL);
2225*7c478bd9Sstevel@tonic-gate 	(void) obj_register(ITMC_OBJ_EXPR, NULL,
2226*7c478bd9Sstevel@tonic-gate 			data1, sizeof (itm_expr_t),
2227*7c478bd9Sstevel@tonic-gate 			&(expr->data.operand[1]), OBJ_REG_TAIL);
2228*7c478bd9Sstevel@tonic-gate 
2229*7c478bd9Sstevel@tonic-gate 	return	(expr);
2230*7c478bd9Sstevel@tonic-gate }
2231*7c478bd9Sstevel@tonic-gate 
2232*7c478bd9Sstevel@tonic-gate 
2233*7c478bd9Sstevel@tonic-gate itm_expr_t *
2234*7c478bd9Sstevel@tonic-gate expr_binary2(itm_expr_type_t type,
2235*7c478bd9Sstevel@tonic-gate 		itm_expr_t *data0, itm_expr_t *data1)
2236*7c478bd9Sstevel@tonic-gate {
2237*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2238*7c478bd9Sstevel@tonic-gate 	itm_num_t	num;
2239*7c478bd9Sstevel@tonic-gate 	unsigned char	*p;
2240*7c478bd9Sstevel@tonic-gate 	int		i;
2241*7c478bd9Sstevel@tonic-gate 
2242*7c478bd9Sstevel@tonic-gate 	if ((NULL == data0) || (NULL == data1)) {
2243*7c478bd9Sstevel@tonic-gate 		return (NULL);
2244*7c478bd9Sstevel@tonic-gate 	}
2245*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2246*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2247*7c478bd9Sstevel@tonic-gate 
2248*7c478bd9Sstevel@tonic-gate 	switch (data0->type) {
2249*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_SEQ:
2250*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)NSPTR(&(data0->data.value));
2251*7c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0; i < data0->data.value.size; i++, p++) {
2252*7c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
2253*7c478bd9Sstevel@tonic-gate 		}
2254*7c478bd9Sstevel@tonic-gate 		data0 = expr_self_num(ITM_EXPR_INT, num);
2255*7c478bd9Sstevel@tonic-gate 		expr->data.operand[0].itm_ptr = (itm_place2_t)(data0);
2256*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_EXPR, NULL,
2257*7c478bd9Sstevel@tonic-gate 				data0, sizeof (itm_expr_t),
2258*7c478bd9Sstevel@tonic-gate 				&(expr->data.operand[0]), OBJ_REG_TAIL);
2259*7c478bd9Sstevel@tonic-gate 		break;
2260*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_INT:
2261*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_REG:
2262*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_IN_VECTOR_D:
2263*7c478bd9Sstevel@tonic-gate 		expr->data.operand[0] = data0->data.operand[0];
2264*7c478bd9Sstevel@tonic-gate 		break;
2265*7c478bd9Sstevel@tonic-gate 	default:
2266*7c478bd9Sstevel@tonic-gate 		expr->data.operand[0].itm_ptr = (itm_place2_t)(data0);
2267*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_EXPR, NULL,
2268*7c478bd9Sstevel@tonic-gate 				data0, sizeof (itm_expr_t),
2269*7c478bd9Sstevel@tonic-gate 				&(expr->data.operand[0]), OBJ_REG_TAIL);
2270*7c478bd9Sstevel@tonic-gate 		break;
2271*7c478bd9Sstevel@tonic-gate 	}
2272*7c478bd9Sstevel@tonic-gate 
2273*7c478bd9Sstevel@tonic-gate 	switch (data1->type) {
2274*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_SEQ:
2275*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)NSPTR(&(data1->data.value));
2276*7c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0; i < data1->data.value.size; i++, p++) {
2277*7c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
2278*7c478bd9Sstevel@tonic-gate 		}
2279*7c478bd9Sstevel@tonic-gate 		data1 = expr_self_num(ITM_EXPR_INT, num);
2280*7c478bd9Sstevel@tonic-gate 		expr->data.operand[1].itm_ptr = (itm_place2_t)(data1);
2281*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_EXPR, NULL,
2282*7c478bd9Sstevel@tonic-gate 				data1, sizeof (itm_expr_t),
2283*7c478bd9Sstevel@tonic-gate 				&(expr->data.operand[1]), OBJ_REG_TAIL);
2284*7c478bd9Sstevel@tonic-gate 		break;
2285*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_INT:
2286*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_REG:
2287*7c478bd9Sstevel@tonic-gate 	case ITM_EXPR_IN_VECTOR_D:
2288*7c478bd9Sstevel@tonic-gate 		expr->data.operand[1] = data1->data.operand[0];
2289*7c478bd9Sstevel@tonic-gate 		break;
2290*7c478bd9Sstevel@tonic-gate 	default:
2291*7c478bd9Sstevel@tonic-gate 		expr->data.operand[1].itm_ptr = (itm_place2_t)(data1);
2292*7c478bd9Sstevel@tonic-gate 		(void) obj_register(ITMC_OBJ_EXPR, NULL,
2293*7c478bd9Sstevel@tonic-gate 				data1, sizeof (itm_expr_t),
2294*7c478bd9Sstevel@tonic-gate 				&(expr->data.operand[1]), OBJ_REG_TAIL);
2295*7c478bd9Sstevel@tonic-gate 		break;
2296*7c478bd9Sstevel@tonic-gate 	}
2297*7c478bd9Sstevel@tonic-gate 	return	(expr);
2298*7c478bd9Sstevel@tonic-gate }
2299*7c478bd9Sstevel@tonic-gate 
2300*7c478bd9Sstevel@tonic-gate 
2301*7c478bd9Sstevel@tonic-gate itm_expr_t *
2302*7c478bd9Sstevel@tonic-gate expr_assign(itm_expr_type_t type,
2303*7c478bd9Sstevel@tonic-gate 	    itm_data_t		*data0, itm_expr_t	*data1)
2304*7c478bd9Sstevel@tonic-gate {
2305*7c478bd9Sstevel@tonic-gate 	itm_expr_t	*expr;
2306*7c478bd9Sstevel@tonic-gate 	itmc_name_t	*name;
2307*7c478bd9Sstevel@tonic-gate 
2308*7c478bd9Sstevel@tonic-gate 	expr = malloc_vital(sizeof (itm_expr_t));
2309*7c478bd9Sstevel@tonic-gate 	expr->type = type;
2310*7c478bd9Sstevel@tonic-gate 	expr->data.operand[1].itm_ptr = (itm_place2_t)(data1);
2311*7c478bd9Sstevel@tonic-gate 
2312*7c478bd9Sstevel@tonic-gate 	name = name_lookup(data0, ITMC_OBJ_REGISTER);
2313*7c478bd9Sstevel@tonic-gate 	if (&name_lookup_error == name) {
2314*7c478bd9Sstevel@tonic-gate 		free(expr);
2315*7c478bd9Sstevel@tonic-gate 		exit(ITMC_STATUS_BT);
2316*7c478bd9Sstevel@tonic-gate 	} else if (NULL == name) {
2317*7c478bd9Sstevel@tonic-gate 		name = name_register(data0, ITMC_OBJ_REGISTER, NULL);
2318*7c478bd9Sstevel@tonic-gate 		name->reg_id = (reg_id++);
2319*7c478bd9Sstevel@tonic-gate 	}
2320*7c478bd9Sstevel@tonic-gate 	expr->data.operand[0].itm_ptr = name->reg_id;
2321*7c478bd9Sstevel@tonic-gate 
2322*7c478bd9Sstevel@tonic-gate 	(void) obj_register(ITMC_OBJ_EXPR, NULL,
2323*7c478bd9Sstevel@tonic-gate 			data1, sizeof (itm_expr_t),
2324*7c478bd9Sstevel@tonic-gate 			&(expr->data.operand[1]), OBJ_REG_TAIL);
2325*7c478bd9Sstevel@tonic-gate 	return	(expr);
2326*7c478bd9Sstevel@tonic-gate }
2327*7c478bd9Sstevel@tonic-gate 
2328*7c478bd9Sstevel@tonic-gate 
2329*7c478bd9Sstevel@tonic-gate itm_expr_t *
2330*7c478bd9Sstevel@tonic-gate expr_seq_to_int(itm_expr_t	*expr)
2331*7c478bd9Sstevel@tonic-gate {
2332*7c478bd9Sstevel@tonic-gate 	itm_num_t	num;
2333*7c478bd9Sstevel@tonic-gate 	unsigned char	*p;
2334*7c478bd9Sstevel@tonic-gate 	int		i;
2335*7c478bd9Sstevel@tonic-gate 
2336*7c478bd9Sstevel@tonic-gate 	if (ITM_EXPR_SEQ == expr->type) {
2337*7c478bd9Sstevel@tonic-gate 		if ((sizeof (itm_place_t)) < expr->data.value.size) {
2338*7c478bd9Sstevel@tonic-gate 			p = (unsigned char *)(expr->data.value.place.itm_ptr);
2339*7c478bd9Sstevel@tonic-gate 		} else {
2340*7c478bd9Sstevel@tonic-gate 			p = (unsigned char *)&(expr->data.value.place.itm_64d);
2341*7c478bd9Sstevel@tonic-gate 		}
2342*7c478bd9Sstevel@tonic-gate 		for (i = 0, num = 0;
2343*7c478bd9Sstevel@tonic-gate 		    i < expr->data.value.size;
2344*7c478bd9Sstevel@tonic-gate 		    i++, p++) {
2345*7c478bd9Sstevel@tonic-gate 			num = ((num << 8) | *p);
2346*7c478bd9Sstevel@tonic-gate 		}
2347*7c478bd9Sstevel@tonic-gate 		free(expr);
2348*7c478bd9Sstevel@tonic-gate 		expr = expr_self_num(ITM_EXPR_INT, num);
2349*7c478bd9Sstevel@tonic-gate 	}
2350*7c478bd9Sstevel@tonic-gate 	return	(expr);
2351*7c478bd9Sstevel@tonic-gate }
2352*7c478bd9Sstevel@tonic-gate 
2353*7c478bd9Sstevel@tonic-gate 
2354*7c478bd9Sstevel@tonic-gate itmc_name_t *
2355*7c478bd9Sstevel@tonic-gate name_lookup(itm_data_t		*name, itm_type_t type)
2356*7c478bd9Sstevel@tonic-gate {
2357*7c478bd9Sstevel@tonic-gate 	itmc_name_t	*p;
2358*7c478bd9Sstevel@tonic-gate 
2359*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('N', ("name_lookup\t: \"%-16s\" %2ld %2ld %2ld\n",
2360*7c478bd9Sstevel@tonic-gate 			name_to_str(name), name->size, type, name_id));
2361*7c478bd9Sstevel@tonic-gate 
2362*7c478bd9Sstevel@tonic-gate 	if (0 == name->size)
2363*7c478bd9Sstevel@tonic-gate 		return	(NULL);
2364*7c478bd9Sstevel@tonic-gate 	for (p = name_first; p; p = p->next) {
2365*7c478bd9Sstevel@tonic-gate 		if ((name->size != p->name.size) ||
2366*7c478bd9Sstevel@tonic-gate 		    (memcmp(NSPTR(name), NSPTR(&(p->name)), name->size))) {
2367*7c478bd9Sstevel@tonic-gate 			continue;
2368*7c478bd9Sstevel@tonic-gate 		}
2369*7c478bd9Sstevel@tonic-gate 		if ((type != p->type) &&
2370*7c478bd9Sstevel@tonic-gate 		    (((ITMC_OBJ_ACTION	!= type) &&
2371*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_ACTION	!= p->type)) ||
2372*7c478bd9Sstevel@tonic-gate 		    ((ITMC_OBJ_ACTION	== type) &&
2373*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_DIREC	!= p->type) &&
2374*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_OP	!= p->type) &&
2375*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_MAP	!= p->type)) ||
2376*7c478bd9Sstevel@tonic-gate 		    ((ITMC_OBJ_ACTION	== p->type) &&
2377*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_DIREC	!= type) &&
2378*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_OP	!= type) &&
2379*7c478bd9Sstevel@tonic-gate 		    (ITMC_OBJ_MAP	!= type)))) {
2380*7c478bd9Sstevel@tonic-gate 			itm_error(
2381*7c478bd9Sstevel@tonic-gate 				gettext("name type conflict: \"%1$s\" "
2382*7c478bd9Sstevel@tonic-gate 				"%2$s %3$s\n"),
2383*7c478bd9Sstevel@tonic-gate 				name_to_str(name),
2384*7c478bd9Sstevel@tonic-gate 				itm_name_type_name[type],
2385*7c478bd9Sstevel@tonic-gate 				itm_name_type_name[p->type]);
2386*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
2387*7c478bd9Sstevel@tonic-gate 			return (&name_lookup_error);
2388*7c478bd9Sstevel@tonic-gate 		} else {
2389*7c478bd9Sstevel@tonic-gate 			return	(p);
2390*7c478bd9Sstevel@tonic-gate 		}
2391*7c478bd9Sstevel@tonic-gate 	}
2392*7c478bd9Sstevel@tonic-gate 	return	(NULL);
2393*7c478bd9Sstevel@tonic-gate }
2394*7c478bd9Sstevel@tonic-gate 
2395*7c478bd9Sstevel@tonic-gate 
2396*7c478bd9Sstevel@tonic-gate itmc_name_t *
2397*7c478bd9Sstevel@tonic-gate name_refer(itm_data_t	*name, itm_type_t type, itmc_ref_t	*refp)
2398*7c478bd9Sstevel@tonic-gate {
2399*7c478bd9Sstevel@tonic-gate 	itmc_name_t		*p;
2400*7c478bd9Sstevel@tonic-gate 	itmc_ref_link_t		*rl;
2401*7c478bd9Sstevel@tonic-gate 
2402*7c478bd9Sstevel@tonic-gate 	p = name_lookup(name, type);
2403*7c478bd9Sstevel@tonic-gate 
2404*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('N', ("name_refer\t: \"%-16s\" %2ld %2ld %08p %2d %08p\n",
2405*7c478bd9Sstevel@tonic-gate 			name_to_str(name), name->size, type, refp, name_id, p));
2406*7c478bd9Sstevel@tonic-gate 
2407*7c478bd9Sstevel@tonic-gate 	if (&name_lookup_error == p) {
2408*7c478bd9Sstevel@tonic-gate 		return	(NULL);
2409*7c478bd9Sstevel@tonic-gate 	}
2410*7c478bd9Sstevel@tonic-gate 
2411*7c478bd9Sstevel@tonic-gate 	rl = malloc_vital(sizeof (itmc_ref_link_t));
2412*7c478bd9Sstevel@tonic-gate 
2413*7c478bd9Sstevel@tonic-gate 	rl->ref = refp;
2414*7c478bd9Sstevel@tonic-gate 	rl->next = NULL;
2415*7c478bd9Sstevel@tonic-gate 
2416*7c478bd9Sstevel@tonic-gate 	if (NULL != p) {
2417*7c478bd9Sstevel@tonic-gate 		if (p->ref_last) {
2418*7c478bd9Sstevel@tonic-gate 			p->ref_last->next = rl;
2419*7c478bd9Sstevel@tonic-gate 		} else {
2420*7c478bd9Sstevel@tonic-gate 			p->ref_first = rl;
2421*7c478bd9Sstevel@tonic-gate 		}
2422*7c478bd9Sstevel@tonic-gate 		p->ref_last = rl;
2423*7c478bd9Sstevel@tonic-gate 	} else {
2424*7c478bd9Sstevel@tonic-gate 		p = malloc_vital(sizeof (itmc_name_t));
2425*7c478bd9Sstevel@tonic-gate 		p->id = (name_id++);
2426*7c478bd9Sstevel@tonic-gate 		p->reg_id = 0;
2427*7c478bd9Sstevel@tonic-gate 		p->name = *name;
2428*7c478bd9Sstevel@tonic-gate 		p->type = type;
2429*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2430*7c478bd9Sstevel@tonic-gate 		p->reloc.itm_pad = 0;
2431*7c478bd9Sstevel@tonic-gate #endif
2432*7c478bd9Sstevel@tonic-gate 		p->reloc.itm_ptr = 0;
2433*7c478bd9Sstevel@tonic-gate 		p->ref_first = rl;
2434*7c478bd9Sstevel@tonic-gate 		p->ref_last = rl;
2435*7c478bd9Sstevel@tonic-gate 		p->next = NULL;
2436*7c478bd9Sstevel@tonic-gate 
2437*7c478bd9Sstevel@tonic-gate 		if (name_last) {
2438*7c478bd9Sstevel@tonic-gate 			name_last->next = p;
2439*7c478bd9Sstevel@tonic-gate 		} else {
2440*7c478bd9Sstevel@tonic-gate 			name_first = p;
2441*7c478bd9Sstevel@tonic-gate 		}
2442*7c478bd9Sstevel@tonic-gate 		name_last = p;
2443*7c478bd9Sstevel@tonic-gate 	}
2444*7c478bd9Sstevel@tonic-gate 	return	(p);
2445*7c478bd9Sstevel@tonic-gate }
2446*7c478bd9Sstevel@tonic-gate 
2447*7c478bd9Sstevel@tonic-gate 
2448*7c478bd9Sstevel@tonic-gate itmc_name_t *
2449*7c478bd9Sstevel@tonic-gate name_register(itm_data_t	*name, itm_type_t type, itmc_ref_t	*refp)
2450*7c478bd9Sstevel@tonic-gate {
2451*7c478bd9Sstevel@tonic-gate 	itmc_name_t	*p;
2452*7c478bd9Sstevel@tonic-gate 
2453*7c478bd9Sstevel@tonic-gate 	TRACE_MESSAGE('N', ("name_register\t: \"%-16s\" %2ld %2ld %08p %2ld\n",
2454*7c478bd9Sstevel@tonic-gate 			name_to_str(name), name->size, type, refp, name_id));
2455*7c478bd9Sstevel@tonic-gate 
2456*7c478bd9Sstevel@tonic-gate 
2457*7c478bd9Sstevel@tonic-gate 	p = name_lookup(name, type);
2458*7c478bd9Sstevel@tonic-gate 	if (&name_lookup_error == p) {
2459*7c478bd9Sstevel@tonic-gate 		return	(NULL);
2460*7c478bd9Sstevel@tonic-gate 	}
2461*7c478bd9Sstevel@tonic-gate 	if (NULL != p) {
2462*7c478bd9Sstevel@tonic-gate 		if (NULL != p->object) {
2463*7c478bd9Sstevel@tonic-gate 			itm_error(gettext(
2464*7c478bd9Sstevel@tonic-gate 				"same names are specified: %1$s\n"),
2465*7c478bd9Sstevel@tonic-gate 				name_to_str(name));
2466*7c478bd9Sstevel@tonic-gate 			error_deferred += 1;
2467*7c478bd9Sstevel@tonic-gate 			return (NULL);
2468*7c478bd9Sstevel@tonic-gate 		}
2469*7c478bd9Sstevel@tonic-gate 		p->object = refp;
2470*7c478bd9Sstevel@tonic-gate 	} else {
2471*7c478bd9Sstevel@tonic-gate 		p = malloc_vital(sizeof (itmc_name_t));
2472*7c478bd9Sstevel@tonic-gate 		p->id = (name_id++);
2473*7c478bd9Sstevel@tonic-gate 		p->reg_id = 0;
2474*7c478bd9Sstevel@tonic-gate 		p->name = *name;
2475*7c478bd9Sstevel@tonic-gate 		p->type = type;
2476*7c478bd9Sstevel@tonic-gate 		p->object = refp;
2477*7c478bd9Sstevel@tonic-gate 		p->reloc.itm_ptr = 0;
2478*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2479*7c478bd9Sstevel@tonic-gate 		p->reloc.itm_pad = 0;
2480*7c478bd9Sstevel@tonic-gate #endif
2481*7c478bd9Sstevel@tonic-gate 		p->ref_first = NULL;
2482*7c478bd9Sstevel@tonic-gate 		p->ref_last = NULL;
2483*7c478bd9Sstevel@tonic-gate 		p->next = NULL;
2484*7c478bd9Sstevel@tonic-gate 
2485*7c478bd9Sstevel@tonic-gate 		if (name_last) {
2486*7c478bd9Sstevel@tonic-gate 			name_last->next = p;
2487*7c478bd9Sstevel@tonic-gate 		} else {
2488*7c478bd9Sstevel@tonic-gate 			name_first = p;
2489*7c478bd9Sstevel@tonic-gate 		}
2490*7c478bd9Sstevel@tonic-gate 		name_last = p;
2491*7c478bd9Sstevel@tonic-gate 	}
2492*7c478bd9Sstevel@tonic-gate 
2493*7c478bd9Sstevel@tonic-gate 	return	(p);
2494*7c478bd9Sstevel@tonic-gate }
2495*7c478bd9Sstevel@tonic-gate 
2496*7c478bd9Sstevel@tonic-gate 
2497*7c478bd9Sstevel@tonic-gate int
2498*7c478bd9Sstevel@tonic-gate data_compare(const itm_data_t	*d0, const itm_data_t	*d1)
2499*7c478bd9Sstevel@tonic-gate {
2500*7c478bd9Sstevel@tonic-gate 	if (d0->size < d1->size) {
2501*7c478bd9Sstevel@tonic-gate 		if (memcmp(NSPTR(d0), NSPTR(d1), d0->size) < 0) {
2502*7c478bd9Sstevel@tonic-gate 			return (-1);
2503*7c478bd9Sstevel@tonic-gate 		} else {
2504*7c478bd9Sstevel@tonic-gate 			return	(1);
2505*7c478bd9Sstevel@tonic-gate 		}
2506*7c478bd9Sstevel@tonic-gate 	} else if (d0->size == d1->size) {
2507*7c478bd9Sstevel@tonic-gate 		return (memcmp(NSPTR(d0), NSPTR(d1), d0->size));
2508*7c478bd9Sstevel@tonic-gate 	} else /* (d0->size > d1->size) */ {
2509*7c478bd9Sstevel@tonic-gate 		if (memcmp(NSPTR(d0), NSPTR(d1), d1->size) <= 0) {
2510*7c478bd9Sstevel@tonic-gate 			return (-1);
2511*7c478bd9Sstevel@tonic-gate 		} else {
2512*7c478bd9Sstevel@tonic-gate 			return	(1);
2513*7c478bd9Sstevel@tonic-gate 		}
2514*7c478bd9Sstevel@tonic-gate 	}
2515*7c478bd9Sstevel@tonic-gate }
2516*7c478bd9Sstevel@tonic-gate 
2517*7c478bd9Sstevel@tonic-gate int
2518*7c478bd9Sstevel@tonic-gate data_pair_compare(itmc_data_pair_t	**p0, itmc_data_pair_t	**p1)
2519*7c478bd9Sstevel@tonic-gate {
2520*7c478bd9Sstevel@tonic-gate 	int		r;
2521*7c478bd9Sstevel@tonic-gate 	itm_data_t	*d0;
2522*7c478bd9Sstevel@tonic-gate 	itm_data_t	*d1;
2523*7c478bd9Sstevel@tonic-gate 	uchar_t		*c0;
2524*7c478bd9Sstevel@tonic-gate 	uchar_t		*c1;
2525*7c478bd9Sstevel@tonic-gate 	size_t		s;
2526*7c478bd9Sstevel@tonic-gate 	int		i;
2527*7c478bd9Sstevel@tonic-gate 
2528*7c478bd9Sstevel@tonic-gate 	d0 = &((*p0)->data0);
2529*7c478bd9Sstevel@tonic-gate 	d1 = &((*p1)->data0);
2530*7c478bd9Sstevel@tonic-gate 	c0 = NSPTR(d0);
2531*7c478bd9Sstevel@tonic-gate 	c1 = NSPTR(d1);
2532*7c478bd9Sstevel@tonic-gate 	if (d0->size == d1->size) {
2533*7c478bd9Sstevel@tonic-gate 		s = d0->size;
2534*7c478bd9Sstevel@tonic-gate 	} else if (d0->size < d1->size) {
2535*7c478bd9Sstevel@tonic-gate 		s = d1->size - d0->size;
2536*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < s; i++, c1++) {
2537*7c478bd9Sstevel@tonic-gate 			if (0x00 != *c1) {
2538*7c478bd9Sstevel@tonic-gate 				return (-1);
2539*7c478bd9Sstevel@tonic-gate 			}
2540*7c478bd9Sstevel@tonic-gate 		}
2541*7c478bd9Sstevel@tonic-gate 		s = d0->size;
2542*7c478bd9Sstevel@tonic-gate 	} else {
2543*7c478bd9Sstevel@tonic-gate 		assert(d0->size > d1->size);
2544*7c478bd9Sstevel@tonic-gate 		s = d0->size - d1->size;
2545*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < s; i++, c0++) {
2546*7c478bd9Sstevel@tonic-gate 			if (0x00 != *c0) {
2547*7c478bd9Sstevel@tonic-gate 				return	(1);
2548*7c478bd9Sstevel@tonic-gate 			}
2549*7c478bd9Sstevel@tonic-gate 		}
2550*7c478bd9Sstevel@tonic-gate 		s = d1->size;
2551*7c478bd9Sstevel@tonic-gate 	}
2552*7c478bd9Sstevel@tonic-gate 	r = memcmp(c0, c1, s);
2553*7c478bd9Sstevel@tonic-gate 	if (0 == r) {
2554*7c478bd9Sstevel@tonic-gate 		itm_data_t	*d;
2555*7c478bd9Sstevel@tonic-gate 		if (c0 == NSPTR(d0)) {
2556*7c478bd9Sstevel@tonic-gate 			d = d0;
2557*7c478bd9Sstevel@tonic-gate 		} else {
2558*7c478bd9Sstevel@tonic-gate 			assert(c1 == NSPTR(d0));
2559*7c478bd9Sstevel@tonic-gate 			d = d1;
2560*7c478bd9Sstevel@tonic-gate 		}
2561*7c478bd9Sstevel@tonic-gate 		itm_error(gettext(
2562*7c478bd9Sstevel@tonic-gate 			"distinct source values are specified: 0x%1$s\n"),
2563*7c478bd9Sstevel@tonic-gate 			data_to_hexadecimal(d));
2564*7c478bd9Sstevel@tonic-gate 		error_deferred += 1;
2565*7c478bd9Sstevel@tonic-gate 	}
2566*7c478bd9Sstevel@tonic-gate 	return	(r);
2567*7c478bd9Sstevel@tonic-gate }
2568*7c478bd9Sstevel@tonic-gate 
2569*7c478bd9Sstevel@tonic-gate 
2570*7c478bd9Sstevel@tonic-gate static long
2571*7c478bd9Sstevel@tonic-gate data_to_long(itm_data_t		*data)
2572*7c478bd9Sstevel@tonic-gate {
2573*7c478bd9Sstevel@tonic-gate 	long		l;
2574*7c478bd9Sstevel@tonic-gate 	int		i;
2575*7c478bd9Sstevel@tonic-gate 	unsigned char	*p;
2576*7c478bd9Sstevel@tonic-gate 
2577*7c478bd9Sstevel@tonic-gate 	if ((sizeof (itm_place_t)) < data->size) {
2578*7c478bd9Sstevel@tonic-gate 		return (0);
2579*7c478bd9Sstevel@tonic-gate 	}
2580*7c478bd9Sstevel@tonic-gate 	for (l = 0, i = 0, p = (unsigned char *)&(data->place);
2581*7c478bd9Sstevel@tonic-gate 	    i < data->size;
2582*7c478bd9Sstevel@tonic-gate 	    i++, p++) {
2583*7c478bd9Sstevel@tonic-gate 		l <<= 8;
2584*7c478bd9Sstevel@tonic-gate 		l |= *p;
2585*7c478bd9Sstevel@tonic-gate 	}
2586*7c478bd9Sstevel@tonic-gate 	return	(l);
2587*7c478bd9Sstevel@tonic-gate }
2588