xref: /titanic_50/usr/src/lib/smbsrv/libmlrpc/common/ndr_process.c (revision af79d7d9cd4d7b7b3b52261fc6f4be7426335485)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Network Data Representation (NDR) is a compatible subset of the DCE RPC
28  * and MSRPC NDR.  NDR is used to move parameters consisting of
29  * complicated trees of data constructs between an RPC client and server.
30  */
31 
32 #include <sys/byteorder.h>
33 #include <strings.h>
34 #include <assert.h>
35 #include <string.h>
36 #include <stdlib.h>
37 
38 #include <smbsrv/libsmb.h>
39 #include <smbsrv/string.h>
40 #include <smbsrv/libmlrpc.h>
41 
42 #define	NDR_STRING_MAX		256
43 
44 #define	NDR_IS_UNION(T)	\
45 	(((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_UNION)
46 #define	NDR_IS_STRING(T)	\
47 	(((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_STRING)
48 
49 extern ndr_typeinfo_t ndt_s_wchar;
50 
51 /*
52  * The following synopsis describes the terms TOP-MOST, OUTER and INNER.
53  *
54  * Each parameter (call arguments and return values) is a TOP-MOST item.
55  * A TOP-MOST item consists of one or more OUTER items.  An OUTER item
56  * consists of one or more INNER items.  There are important differences
57  * between each kind, which, primarily, have to do with the allocation
58  * of memory to contain data structures and the order of processing.
59  *
60  * This is most easily demonstrated with a short example.
61  * Consider these structures:
62  *
63  *	struct top_param {
64  *		long		level;
65  *		struct list *	head;
66  *		long		count;
67  *	};
68  *
69  *	struct list {
70  *		struct list *	next;
71  *		char *		str; // a string
72  *	};
73  *
74  * Now, consider an instance tree like this:
75  *
76  *	+---------+       +-------+       +-------+
77  *	|top_param|  +--->|list #1|  +--->|list #2|
78  *	+---------+  |    +-------+  |    +-------+
79  *	| level   |  |    | next ----+    | next --->(NULL)
80  *	| head   ----+    | str  -->"foo" | str  -->"bar"
81  *	| count   |       | flag  |       | flag  |
82  *	+---------+       +-------+       +-------+
83  *
84  * The DCE(MS)/RPC Stub Data encoding for the tree is the following.
85  * The vertical bars (|) indicate OUTER construct boundaries.
86  *
87  *   +-----+----------------------+----------------------+-----+-----+-----+
88  *   |level|#1.next #1.str #1.flag|#2.next #2.str #2.flag|"bar"|"foo"|count|
89  *   +-----+----------------------+----------------------+-----+-----+-----+
90  *   level |<----------------------- head -------------------------->|count
91  *   TOP    TOP                                                       TOP
92  *
93  * Here's what to notice:
94  *
95  * - The members of the TOP-MOST construct are scattered through the Stub
96  *   Data in the order they occur.  This example shows a TOP-MOST construct
97  *   consisting of atomic types (pointers and integers).  A construct
98  *   (struct) within the TOP-MOST construct would be contiguous and not
99  *   scattered.
100  *
101  * - The members of OUTER constructs are contiguous, which allows for
102  *   non-copied relocated (fixed-up) data structures at the packet's
103  *   destination.  We don't do fix-ups here.  The pointers within the
104  *   OUTER constructs are processed depth-first in the order that they
105  *   occur.  If they were processed breadth first, the sequence would
106  *   be #1,"foo",#2,"bar".  This is tricky because OUTER constructs may
107  *   be variable length, and pointers are often encountered before the
108  *   size(s) is known.
109  *
110  * - The INNER constructs are simply the members of an OUTER construct.
111  *
112  * For comparison, consider how ONC RPC would handle the same tree of
113  * data.  ONC requires very little buffering, while DCE requires enough
114  * buffer space for the entire message.  ONC does atom-by-atom depth-first
115  * (de)serialization and copy, while DCE allows for constructs to be
116  * "fixed-up" (relocated) in place at the destination.  The packet data
117  * for the same tree processed by ONC RPC would look like this:
118  *
119  *   +---------------------------------------------------------------------+
120  *   |level #1.next #2.next #2.str "bar" #2.flag #1.str "foo" #1.flag count|
121  *   +---------------------------------------------------------------------+
122  *   TOP    #1      #2      #2     bar   #2      #1     foo   #1      TOP
123  *
124  * More details about each TOP-MOST, OUTER, and INNER constructs appear
125  * throughout this source file near where such constructs are processed.
126  *
127  * NDR_REFERENCE
128  *
129  * The primary object for NDR is the ndr_ref_t.
130  *
131  * An ndr reference indicates the local datum (i.e. native "C" data
132  * format), and the element within the Stub Data (contained within the
133  * RPC PDU (protocol data unit).  An ndr reference also indicates,
134  * largely as a debugging aid, something about the type of the
135  * element/datum, and the enclosing construct for the element. The
136  * ndr reference's are typically allocated on the stack as locals,
137  * and the chain of ndr-reference.enclosing references is in reverse
138  * order of the call graph.
139  *
140  * The ndr-reference.datum is a pointer to the local memory that
141  * contains/receives the value. The ndr-reference.pdu_offset indicates
142  * where in the Stub Data the value is to be stored/retrieved.
143  *
144  * The ndr-reference also contains various parameters to the NDR
145  * process, such as ndr-reference.size_is, which indicates the size
146  * of variable length data, or ndr-reference.switch_is, which
147  * indicates the arm of a union to use.
148  *
149  * QUEUE OF OUTER REFERENCES
150  *
151  * Some OUTER constructs are variable size.  Sometimes (often) we don't
152  * know the size of the OUTER construct until after pointers have been
153  * encountered. Hence, we can not begin processing the referent of the
154  * pointer until after the referring OUTER construct is completely
155  * processed, i.e. we don't know where to find/put the referent in the
156  * Stub Data until we know the size of all its predecessors.
157  *
158  * This is managed using the queue of OUTER references.  The queue is
159  * anchored in ndr_stream.outer_queue_head.  At any time,
160  * ndr_stream.outer_queue_tailp indicates where to put the
161  * ndr-reference for the next encountered pointer.
162  *
163  * Refer to the example above as we illustrate the queue here.  In these
164  * illustrations, the queue entries are not the data structures themselves.
165  * Rather, they are ndr-reference entries which **refer** to the data
166  * structures in both the PDU and local memory.
167  *
168  * During some point in the processing, the queue looks like this:
169  *
170  *   outer_current -------v
171  *   outer_queue_head --> list#1 --0
172  *   outer_queue_tailp ---------&
173  *
174  * When the pointer #1.next is encountered, and entry is added to the
175  * queue,
176  *
177  *   outer_current -------v
178  *   outer_queue_head --> list#1 --> list#2 --0
179  *   outer_queue_tailp --------------------&
180  *
181  * and the members of #1 continue to be processed, which encounters
182  * #1.str:
183  *
184  *   outer_current -------v
185  *   outer_queue_head --> list#1 --> list#2 --> "foo" --0
186  *   outer_queue_tailp ------------------------------&
187  *
188  * Upon the completion of list#1, the processing continues by moving to
189  * ndr_stream.outer_current->next, and the tail is set to this outer member:
190  *
191  *   outer_current ------------------v
192  *   outer_queue_head --> list#1 --> list#2 --> "foo" --0
193  *   outer_queue_tailp --------------------&
194  *
195  * Space for list#2 is allocated, either in the Stub Data or of local
196  * memory.  When #2.next is encountered, it is found to be the null
197  * pointer and no reference is added to the queue.  When #2.str is
198  * encountered, it is found to be valid, and a reference is added:
199  *
200  *   outer_current ------------------v
201  *   outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0
202  *   outer_queue_tailp ------------------------------&
203  *
204  * Processing continues in a similar fashion with the string "bar",
205  * which is variable-length.  At this point, memory for "bar" may be
206  * malloc()ed during NDR_M_OP_UNMARSHALL:
207  *
208  *   outer_current -----------------------------v
209  *   outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0
210  *   outer_queue_tailp ------------------------------&
211  *
212  * And finishes on string "foo".  Notice that because "bar" is a
213  * variable length string, and we don't know the PDU offset for "foo"
214  * until we reach this point.
215  *
216  * When the queue is drained (current->next==0), processing continues
217  * with the next TOP-MOST member.
218  *
219  * The queue of OUTER constructs manages the variable-length semantics
220  * of OUTER constructs and satisfies the depth-first requirement.
221  * We allow the queue to linger until the entire TOP-MOST structure is
222  * processed as an aid to debugging.
223  */
224 
225 static ndr_ref_t *ndr_enter_outer_queue(ndr_ref_t *);
226 extern int ndr__ulong(ndr_ref_t *);
227 
228 /*
229  * TOP-MOST ELEMENTS
230  *
231  * This is fundamentally the first OUTER construct of the parameter,
232  * possibly followed by more OUTER constructs due to pointers.  The
233  * datum (local memory) for TOP-MOST constructs (structs) is allocated
234  * by the caller of NDR.
235  *
236  * After the element is transferred, the outer_queue is drained.
237  *
238  * All we have to do is add an entry to the outer_queue for this
239  * top-most member, and commence the outer_queue processing.
240  */
241 int
242 ndo_process(ndr_stream_t *nds, ndr_typeinfo_t *ti, char *datum)
243 {
244 	ndr_ref_t	myref;
245 
246 	bzero(&myref, sizeof (myref));
247 	myref.stream = nds;
248 	myref.datum = datum;
249 	myref.name = "PROCESS";
250 	myref.ti = ti;
251 
252 	return (ndr_topmost(&myref));
253 }
254 
255 int
256 ndo_operation(ndr_stream_t *nds, ndr_typeinfo_t *ti, int opnum, char *datum)
257 {
258 	ndr_ref_t	myref;
259 
260 	bzero(&myref, sizeof (myref));
261 	myref.stream = nds;
262 	myref.datum = datum;
263 	myref.name = "OPERATION";
264 	myref.ti = ti;
265 	myref.inner_flags = NDR_F_SWITCH_IS;
266 	myref.switch_is = opnum;
267 
268 	if (ti->type_flags != NDR_F_INTERFACE) {
269 		NDR_SET_ERROR(&myref, NDR_ERR_NOT_AN_INTERFACE);
270 		return (0);
271 	}
272 
273 	return ((*ti->ndr_func)(&myref));
274 }
275 
276 int
277 ndr_params(ndr_ref_t *params_ref)
278 {
279 	ndr_typeinfo_t *ti = params_ref->ti;
280 
281 	if (ti->type_flags == NDR_F_OPERATION)
282 		return (*ti->ndr_func) (params_ref);
283 	else
284 		return (ndr_topmost(params_ref));
285 }
286 
287 int
288 ndr_topmost(ndr_ref_t *top_ref)
289 {
290 	ndr_stream_t *nds;
291 	ndr_typeinfo_t *ti;
292 	ndr_ref_t *outer_ref = 0;
293 	int	is_varlen;
294 	int	is_string;
295 	int	error;
296 	int	rc;
297 	unsigned n_fixed;
298 	int	params;
299 
300 	assert(top_ref);
301 	assert(top_ref->stream);
302 	assert(top_ref->ti);
303 
304 	nds = top_ref->stream;
305 	ti = top_ref->ti;
306 
307 	is_varlen = ti->pdu_size_variable_part;
308 	is_string = NDR_IS_STRING(ti);
309 
310 	assert(nds->outer_queue_tailp && !*nds->outer_queue_tailp);
311 	assert(!nds->outer_current);
312 
313 	params = top_ref->inner_flags & NDR_F_PARAMS_MASK;
314 
315 	switch (params) {
316 	case NDR_F_NONE:
317 	case NDR_F_SWITCH_IS:
318 		if (is_string || is_varlen) {
319 			error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL;
320 			NDR_SET_ERROR(outer_ref, error);
321 			return (0);
322 		}
323 		n_fixed = ti->pdu_size_fixed_part;
324 		break;
325 
326 	case NDR_F_SIZE_IS:
327 		error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL;
328 		NDR_SET_ERROR(outer_ref, error);
329 		return (0);
330 
331 	case NDR_F_DIMENSION_IS:
332 		if (is_varlen) {
333 			error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
334 			NDR_SET_ERROR(outer_ref, error);
335 			return (0);
336 		}
337 		n_fixed = ti->pdu_size_fixed_part * top_ref->dimension_is;
338 		break;
339 
340 	case NDR_F_IS_POINTER:
341 	case NDR_F_IS_POINTER+NDR_F_SIZE_IS:
342 		n_fixed = 4;
343 		break;
344 
345 	case NDR_F_IS_REFERENCE:
346 	case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS:
347 		n_fixed = 0;
348 		break;
349 
350 	default:
351 		error = NDR_ERR_OUTER_PARAMS_BAD;
352 		NDR_SET_ERROR(outer_ref, error);
353 		return (0);
354 	}
355 
356 	outer_ref = ndr_enter_outer_queue(top_ref);
357 	if (!outer_ref)
358 		return (0);	/* error already set */
359 
360 	/*
361 	 * Hand-craft the first OUTER construct and directly call
362 	 * ndr_inner(). Then, run the outer_queue. We do this
363 	 * because ndr_outer() wants to malloc() memory for
364 	 * the construct, and we already have the memory.
365 	 */
366 
367 	/* move the flags, etc, around again, undoes enter_outer_queue() */
368 	outer_ref->inner_flags = top_ref->inner_flags;
369 	outer_ref->outer_flags = 0;
370 	outer_ref->datum = top_ref->datum;
371 
372 	/* All outer constructs start on a mod4 (longword) boundary */
373 	if (!ndr_outer_align(outer_ref))
374 		return (0);		/* error already set */
375 
376 	/* Regardless of what it is, this is where it starts */
377 	outer_ref->pdu_offset = nds->pdu_scan_offset;
378 
379 	rc = ndr_outer_grow(outer_ref, n_fixed);
380 	if (!rc)
381 		return (0);		/* error already set */
382 
383 	outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_fixed;
384 
385 	/* set-up outer_current, as though run_outer_queue() was doing it */
386 	nds->outer_current = outer_ref;
387 	nds->outer_queue_tailp = &nds->outer_current->next;
388 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
389 
390 	/* do the topmost member */
391 	rc = ndr_inner(outer_ref);
392 	if (!rc)
393 		return (0);		/* error already set */
394 
395 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
396 
397 	/* advance, as though run_outer_queue() was doing it */
398 	nds->outer_current = nds->outer_current->next;
399 	return (ndr_run_outer_queue(nds));
400 }
401 
402 static ndr_ref_t *
403 ndr_enter_outer_queue(ndr_ref_t *arg_ref)
404 {
405 	ndr_stream_t	*nds = arg_ref->stream;
406 	ndr_ref_t	*outer_ref;
407 
408 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
409 	outer_ref = (ndr_ref_t *)NDS_MALLOC(nds, sizeof (*outer_ref), arg_ref);
410 	if (!outer_ref) {
411 		NDR_SET_ERROR(arg_ref, NDR_ERR_MALLOC_FAILED);
412 		return (0);
413 	}
414 
415 	*outer_ref = *arg_ref;
416 
417 	/* move advice in inner_flags to outer_flags */
418 	outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
419 	outer_ref->inner_flags = 0;
420 	outer_ref->enclosing = nds->outer_current;
421 	outer_ref->backptr = 0;
422 	outer_ref->datum = 0;
423 
424 	assert(nds->outer_queue_tailp);
425 
426 	outer_ref->next = *nds->outer_queue_tailp;
427 	*nds->outer_queue_tailp = outer_ref;
428 	nds->outer_queue_tailp = &outer_ref->next;
429 	return (outer_ref);
430 }
431 
432 int
433 ndr_run_outer_queue(ndr_stream_t *nds)
434 {
435 	while (nds->outer_current) {
436 		nds->outer_queue_tailp = &nds->outer_current->next;
437 
438 		if (!ndr_outer(nds->outer_current))
439 			return (0);
440 
441 		nds->outer_current = nds->outer_current->next;
442 	}
443 
444 	return (1);
445 }
446 
447 /*
448  * OUTER CONSTRUCTS
449  *
450  * OUTER constructs are where the real work is, which stems from the
451  * variable-length potential.
452  *
453  * DCE(MS)/RPC VARIABLE LENGTH -- CONFORMANT, VARYING, VARYING/CONFORMANT
454  *
455  * DCE(MS)/RPC provides for three forms of variable length: CONFORMANT,
456  * VARYING, and VARYING/CONFORMANT.
457  *
458  * What makes this so tough is that the variable-length array may be well
459  * encapsulated within the outer construct.  Further, because DCE(MS)/RPC
460  * tries to keep the constructs contiguous in the data stream, the sizing
461  * information precedes the entire OUTER construct.  The sizing information
462  * must be used at the appropriate time, which can be after many, many,
463  * many fixed-length elements.  During IDL type analysis, we know in
464  * advance constructs that encapsulate variable-length constructs.  So,
465  * we know when we have a sizing header and when we don't.  The actual
466  * semantics of the header are largely deferred.
467  *
468  * Currently, VARYING constructs are not implemented but they are described
469  * here in case they have to be implemented in the future.  Similarly,
470  * DCE(MS)/RPC provides for multi-dimensional arrays, which are currently
471  * not implemented.  Only one-dimensional, variable-length arrays are
472  * supported.
473  *
474  * CONFORMANT CONSTRUCTS -- VARIABLE LENGTH ARRAYS START THE SHOW
475  *
476  * All variable-length values are arrays.  These arrays may be embedded
477  * well within another construct.  However, a variable-length construct
478  * may ONLY appear as the last member of an enclosing construct.  Example:
479  *
480  *	struct credentials {
481  *		ulong	uid, gid;
482  *		ulong	n_gids;
483  *	    [size_is(n_gids)]
484  *		ulong	gids[*];    // variable-length.
485  *	};
486  *
487  * CONFORMANT constructs have a dynamic size in local memory and in the
488  * PDU.  The CONFORMANT quality is indicated by the [size_is()] advice.
489  * CONFORMANT constructs have the following header:
490  *
491  *	struct conformant_header {
492  *		ulong		size_is;
493  *	};
494  *
495  * (Multi-dimensional CONFORMANT arrays have a similar header for each
496  * dimension - not implemented).
497  *
498  * Example CONFORMANT construct:
499  *
500  *	struct user {
501  *		char *			name;
502  *		struct credentials	cred;	// see above
503  *	};
504  *
505  * Consider the data tree:
506  *
507  *    +--------+
508  *    |  user  |
509  *    +--------+
510  *    | name  ----> "fred" (the string is a different OUTER)
511  *    | uid    |
512  *    | gid    |
513  *    | n_gids |    for example, 3
514  *    | gids[0]|
515  *    | gids[1]|
516  *    | gids[2]|
517  *    +--------+
518  *
519  * The OUTER construct in the Stub Data would be:
520  *
521  *    +---+---------+---------------------------------------------+
522  *    |pad|size_is=3 name uid gid n_gids=3 gids[0] gids[1] gids[2]|
523  *    +---+---------+---------------------------------------------+
524  *         szing hdr|user |<-------------- user.cred ------------>|
525  *                  |<--- fixed-size ---->|<----- conformant ---->|
526  *
527  * The ndr_typeinfo for struct user will have:
528  *	pdu_fixed_size_part = 16	four long words (name uid gid n_gids)
529  *	pdu_variable_size_part = 4	per element, sizeof gids[0]
530  *
531  * VARYING CONSTRUCTS -- NOT IMPLEMENTED
532  *
533  * VARYING constructs have the following header:
534  *
535  *	struct varying_header {
536  *		ulong		first_is;
537  *		ulong		length_is;
538  *	};
539  *
540  * This indicates which interval of an array is significant.
541  * Non-intersecting elements of the array are undefined and usually
542  * zero-filled.  The first_is parameter for C arrays is always 0 for
543  * the first element.
544  *
545  * N.B. Constructs may contain one CONFORMANT element, which is always
546  * last, but may contain many VARYING elements, which can be anywhere.
547  *
548  * VARYING CONFORMANT constructs have the sizing headers arranged like
549  * this:
550  *
551  *	struct conformant_header	all_conformant[N_CONFORMANT_DIM];
552  *	struct varying_header		all_varying[N_VARYING_ELEMS_AND_DIMS];
553  *
554  * The sizing header is immediately followed by the values for the
555  * construct.  Again, we don't support more than one dimension and
556  * we don't support VARYING constructs at this time.
557  *
558  * A good example of a VARYING/CONFORMANT data structure is the UNIX
559  * directory entry:
560  *
561  *	struct dirent {
562  *		ushort		reclen;
563  *		ushort		namlen;
564  *		ulong		inum;
565  *	    [size_is(reclen-8) length_is(namlen+1)] // -(2+2+4), +1 for NUL
566  *		uchar		name[*];
567  *	};
568  *
569  *
570  * STRINGS ARE A SPECIAL CASE
571  *
572  * Strings are handled specially.  MS/RPC uses VARYING/CONFORMANT structures
573  * for strings.  This is a simple one-dimensional variable-length array,
574  * typically with its last element all zeroes.  We handle strings with the
575  * header:
576  *
577  *	struct string_header {
578  *		ulong		size_is;
579  *		ulong		first_is;	// always 0
580  *		ulong		length_is;	// always same as size_is
581  *	};
582  *
583  * If general support for VARYING and VARYING/CONFORMANT mechanisms is
584  * implemented, we probably won't need the strings special case.
585  */
586 int
587 ndr_outer(ndr_ref_t *outer_ref)
588 {
589 	ndr_stream_t 	*nds = outer_ref->stream;
590 	ndr_typeinfo_t	*ti = outer_ref->ti;
591 	int	is_varlen = ti->pdu_size_variable_part;
592 	int	is_union = NDR_IS_UNION(ti);
593 	int	is_string = NDR_IS_STRING(ti);
594 	int	error = NDR_ERR_OUTER_PARAMS_BAD;
595 	int	params;
596 
597 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
598 
599 	NDR_TATTLE(outer_ref, "--OUTER--");
600 
601 	/* All outer constructs start on a mod4 (longword) boundary */
602 	if (!ndr_outer_align(outer_ref))
603 		return (0);		/* error already set */
604 
605 	/* Regardless of what it is, this is where it starts */
606 	outer_ref->pdu_offset = nds->pdu_scan_offset;
607 
608 	if (is_union) {
609 		error = NDR_ERR_OUTER_UNION_ILLEGAL;
610 		NDR_SET_ERROR(outer_ref, error);
611 		return (0);
612 	}
613 
614 	switch (params) {
615 	case NDR_F_NONE:
616 		if (is_string)
617 			return (ndr_outer_string(outer_ref));
618 		if (is_varlen)
619 			return (ndr_outer_conformant_construct(outer_ref));
620 
621 		return (ndr_outer_fixed(outer_ref));
622 		break;
623 
624 	case NDR_F_SIZE_IS:
625 	case NDR_F_DIMENSION_IS:
626 		if (is_varlen) {
627 			error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
628 			break;
629 		}
630 
631 		if (params == NDR_F_SIZE_IS)
632 			return (ndr_outer_conformant_array(outer_ref));
633 		else
634 			return (ndr_outer_fixed_array(outer_ref));
635 		break;
636 
637 	default:
638 		error = NDR_ERR_OUTER_PARAMS_BAD;
639 		break;
640 	}
641 
642 	/*
643 	 * If we get here, something is wrong. Most likely,
644 	 * the params flags do not match.
645 	 */
646 	NDR_SET_ERROR(outer_ref, error);
647 	return (0);
648 }
649 
650 int
651 ndr_outer_fixed(ndr_ref_t *outer_ref)
652 {
653 	ndr_stream_t	*nds = outer_ref->stream;
654 	ndr_typeinfo_t	*ti = outer_ref->ti;
655 	ndr_ref_t	myref;
656 	char 		*valp = NULL;
657 	int		is_varlen = ti->pdu_size_variable_part;
658 	int		is_union = NDR_IS_UNION(ti);
659 	int		is_string = NDR_IS_STRING(ti);
660 	int		rc;
661 	unsigned	n_hdr;
662 	unsigned	n_fixed;
663 	unsigned	n_variable;
664 	unsigned	n_alloc;
665 	unsigned	n_pdu_total;
666 	int		params;
667 
668 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
669 
670 	assert(!is_varlen && !is_string && !is_union);
671 	assert(params == NDR_F_NONE);
672 
673 	/* no header for this */
674 	n_hdr = 0;
675 
676 	/* fixed part -- exactly one of these */
677 	n_fixed = ti->pdu_size_fixed_part;
678 	assert(n_fixed > 0);
679 
680 	/* variable part -- exactly none of these */
681 	n_variable = 0;
682 
683 	/* sum them up to determine the PDU space required */
684 	n_pdu_total = n_hdr + n_fixed + n_variable;
685 
686 	/* similar sum to determine how much local memory is required */
687 	n_alloc = n_fixed + n_variable;
688 
689 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
690 	if (!rc)
691 		return (rc);		/* error already set */
692 
693 	switch (nds->m_op) {
694 	case NDR_M_OP_MARSHALL:
695 		valp = outer_ref->datum;
696 		assert(valp);
697 		if (outer_ref->backptr) {
698 			assert(valp == *outer_ref->backptr);
699 		}
700 		break;
701 
702 	case NDR_M_OP_UNMARSHALL:
703 		valp = NDS_MALLOC(nds, n_alloc, outer_ref);
704 		if (!valp) {
705 			NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
706 			return (0);
707 		}
708 		if (outer_ref->backptr)
709 			*outer_ref->backptr = valp;
710 		outer_ref->datum = valp;
711 		break;
712 
713 	default:
714 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
715 		return (0);
716 	}
717 
718 	bzero(&myref, sizeof (myref));
719 	myref.stream = nds;
720 	myref.enclosing = outer_ref;
721 	myref.ti = outer_ref->ti;
722 	myref.datum = outer_ref->datum;
723 	myref.name = "FIXED-VALUE";
724 	myref.outer_flags = NDR_F_NONE;
725 	myref.inner_flags = NDR_F_NONE;
726 
727 	myref.pdu_offset = outer_ref->pdu_offset;
728 	outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
729 
730 	rc = ndr_inner(&myref);
731 	if (!rc)
732 		return (rc);		/* error already set */
733 
734 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
735 	return (1);
736 }
737 
738 int
739 ndr_outer_fixed_array(ndr_ref_t *outer_ref)
740 {
741 	ndr_stream_t	*nds = outer_ref->stream;
742 	ndr_typeinfo_t	*ti = outer_ref->ti;
743 	ndr_ref_t	myref;
744 	char 		*valp = NULL;
745 	int		is_varlen = ti->pdu_size_variable_part;
746 	int		is_union = NDR_IS_UNION(ti);
747 	int		is_string = NDR_IS_STRING(ti);
748 	int		rc;
749 	unsigned	n_hdr;
750 	unsigned	n_fixed;
751 	unsigned	n_variable;
752 	unsigned	n_alloc;
753 	unsigned	n_pdu_total;
754 	int		params;
755 
756 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
757 
758 	assert(!is_varlen && !is_string && !is_union);
759 	assert(params == NDR_F_DIMENSION_IS);
760 
761 	/* no header for this */
762 	n_hdr = 0;
763 
764 	/* fixed part -- exactly dimension_is of these */
765 	n_fixed = ti->pdu_size_fixed_part * outer_ref->dimension_is;
766 	assert(n_fixed > 0);
767 
768 	/* variable part -- exactly none of these */
769 	n_variable = 0;
770 
771 	/* sum them up to determine the PDU space required */
772 	n_pdu_total = n_hdr + n_fixed + n_variable;
773 
774 	/* similar sum to determine how much local memory is required */
775 	n_alloc = n_fixed + n_variable;
776 
777 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
778 	if (!rc)
779 		return (rc);		/* error already set */
780 
781 	switch (nds->m_op) {
782 	case NDR_M_OP_MARSHALL:
783 		valp = outer_ref->datum;
784 		assert(valp);
785 		if (outer_ref->backptr) {
786 			assert(valp == *outer_ref->backptr);
787 		}
788 		break;
789 
790 	case NDR_M_OP_UNMARSHALL:
791 		valp = NDS_MALLOC(nds, n_alloc, outer_ref);
792 		if (!valp) {
793 			NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
794 			return (0);
795 		}
796 		if (outer_ref->backptr)
797 			*outer_ref->backptr = valp;
798 		outer_ref->datum = valp;
799 		break;
800 
801 	default:
802 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
803 		return (0);
804 	}
805 
806 	bzero(&myref, sizeof (myref));
807 	myref.stream = nds;
808 	myref.enclosing = outer_ref;
809 	myref.ti = outer_ref->ti;
810 	myref.datum = outer_ref->datum;
811 	myref.name = "FIXED-ARRAY";
812 	myref.outer_flags = NDR_F_NONE;
813 	myref.inner_flags = NDR_F_DIMENSION_IS;
814 	myref.dimension_is = outer_ref->dimension_is;
815 
816 	myref.pdu_offset = outer_ref->pdu_offset;
817 	outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
818 
819 	rc = ndr_inner(&myref);
820 	if (!rc)
821 		return (rc);		/* error already set */
822 
823 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
824 	return (1);
825 }
826 
827 int
828 ndr_outer_conformant_array(ndr_ref_t *outer_ref)
829 {
830 	ndr_stream_t	*nds = outer_ref->stream;
831 	ndr_typeinfo_t	*ti = outer_ref->ti;
832 	ndr_ref_t	myref;
833 	char 		*valp = NULL;
834 	int		is_varlen = ti->pdu_size_variable_part;
835 	int		is_union = NDR_IS_UNION(ti);
836 	int		is_string = NDR_IS_STRING(ti);
837 	unsigned long	size_is;
838 	int		rc;
839 	unsigned	n_hdr;
840 	unsigned	n_fixed;
841 	unsigned	n_variable;
842 	unsigned	n_alloc;
843 	unsigned	n_pdu_total;
844 	int		params;
845 
846 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
847 
848 	assert(!is_varlen && !is_string && !is_union);
849 	assert(params == NDR_F_SIZE_IS);
850 
851 	/* conformant header for this */
852 	n_hdr = 4;
853 
854 	/* fixed part -- exactly none of these */
855 	n_fixed = 0;
856 
857 	/* variable part -- exactly size_of of these */
858 	/* notice that it is the **fixed** size of the ti */
859 	n_variable = ti->pdu_size_fixed_part * outer_ref->size_is;
860 
861 	/* sum them up to determine the PDU space required */
862 	n_pdu_total = n_hdr + n_fixed + n_variable;
863 
864 	/* similar sum to determine how much local memory is required */
865 	n_alloc = n_fixed + n_variable;
866 
867 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
868 	if (!rc)
869 		return (rc);		/* error already set */
870 
871 	switch (nds->m_op) {
872 	case NDR_M_OP_MARSHALL:
873 		size_is = outer_ref->size_is;
874 		rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
875 		if (!rc)
876 			return (0);	/* error already set */
877 
878 		valp = outer_ref->datum;
879 		assert(valp);
880 		if (outer_ref->backptr) {
881 			assert(valp == *outer_ref->backptr);
882 		}
883 		break;
884 
885 	case NDR_M_OP_UNMARSHALL:
886 		rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is);
887 		if (!rc)
888 			return (0);	/* error already set */
889 
890 		if (size_is != outer_ref->size_is) {
891 			NDR_SET_ERROR(outer_ref,
892 			    NDR_ERR_SIZE_IS_MISMATCH_PDU);
893 			return (0);
894 		}
895 
896 		if (size_is > 0) {
897 			valp = NDS_MALLOC(nds, n_alloc, outer_ref);
898 			if (!valp) {
899 				NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
900 				return (0);
901 			}
902 		}
903 
904 		if (outer_ref->backptr)
905 			*outer_ref->backptr = valp;
906 		outer_ref->datum = valp;
907 		break;
908 
909 	default:
910 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
911 		return (0);
912 	}
913 
914 	outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
915 	outer_ref->type_flags = NDR_F_NONE;
916 	outer_ref->inner_flags = NDR_F_NONE;
917 
918 	if (size_is > 0) {
919 		bzero(&myref, sizeof (myref));
920 		myref.stream = nds;
921 		myref.enclosing = outer_ref;
922 		myref.ti = outer_ref->ti;
923 		myref.datum = outer_ref->datum;
924 		myref.name = "CONFORMANT-ARRAY";
925 		myref.outer_flags = NDR_F_NONE;
926 		myref.inner_flags = NDR_F_SIZE_IS;
927 		myref.size_is = outer_ref->size_is;
928 
929 		myref.inner_flags = NDR_F_DIMENSION_IS;		/* convenient */
930 		myref.dimension_is = outer_ref->size_is;	/* convenient */
931 
932 		myref.pdu_offset = outer_ref->pdu_offset + 4;
933 
934 		rc = ndr_inner(&myref);
935 		if (!rc)
936 			return (rc);		/* error already set */
937 	}
938 
939 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
940 	return (1);
941 }
942 
943 int
944 ndr_outer_conformant_construct(ndr_ref_t *outer_ref)
945 {
946 	ndr_stream_t	*nds = outer_ref->stream;
947 	ndr_typeinfo_t	*ti = outer_ref->ti;
948 	ndr_ref_t	myref;
949 	char 		*valp = NULL;
950 	int		is_varlen = ti->pdu_size_variable_part;
951 	int		is_union = NDR_IS_UNION(ti);
952 	int		is_string = NDR_IS_STRING(ti);
953 	unsigned long	size_is;
954 	int		rc;
955 	unsigned	n_hdr;
956 	unsigned	n_fixed;
957 	unsigned	n_variable;
958 	unsigned	n_alloc;
959 	unsigned	n_pdu_total;
960 	int		params;
961 
962 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
963 
964 	assert(is_varlen && !is_string && !is_union);
965 	assert(params == NDR_F_NONE);
966 
967 	/* conformant header for this */
968 	n_hdr = 4;
969 
970 	/* fixed part -- exactly one of these */
971 	n_fixed = ti->pdu_size_fixed_part;
972 
973 	/* variable part -- exactly size_of of these */
974 	n_variable = 0;		/* 0 for the moment */
975 
976 	/* sum them up to determine the PDU space required */
977 	n_pdu_total = n_hdr + n_fixed + n_variable;
978 
979 	/* similar sum to determine how much local memory is required */
980 	n_alloc = n_fixed + n_variable;
981 
982 	/* For the moment, grow enough for the fixed-size part */
983 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
984 	if (!rc)
985 		return (rc);		/* error already set */
986 
987 	switch (nds->m_op) {
988 	case NDR_M_OP_MARSHALL:
989 		/*
990 		 * We don't know the size yet. We have to wait for
991 		 * it. Proceed with the fixed-size part, and await
992 		 * the call to ndr_size_is().
993 		 */
994 		size_is = 0;
995 		rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
996 		if (!rc)
997 			return (0);	/* error already set */
998 
999 		valp = outer_ref->datum;
1000 		assert(valp);
1001 		if (outer_ref->backptr) {
1002 			assert(valp == *outer_ref->backptr);
1003 		}
1004 		break;
1005 
1006 	case NDR_M_OP_UNMARSHALL:
1007 		/*
1008 		 * We know the size of the variable part because
1009 		 * of the CONFORMANT header. We will verify
1010 		 * the header against the [size_is(X)] advice
1011 		 * later when ndr_size_is() is called.
1012 		 */
1013 		rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is);
1014 		if (!rc)
1015 			return (0);	/* error already set */
1016 
1017 		/* recalculate metrics */
1018 		n_variable = size_is * ti->pdu_size_variable_part;
1019 		n_pdu_total = n_hdr + n_fixed + n_variable;
1020 		n_alloc = n_fixed + n_variable;
1021 
1022 		rc = ndr_outer_grow(outer_ref, n_pdu_total);
1023 		if (!rc)
1024 			return (rc);		/* error already set */
1025 
1026 		outer_ref->size_is = size_is; /* verified later */
1027 
1028 		valp = NDS_MALLOC(nds, n_alloc, outer_ref);
1029 		if (!valp) {
1030 			NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
1031 			return (0);
1032 		}
1033 		if (outer_ref->backptr)
1034 			*outer_ref->backptr = valp;
1035 		outer_ref->datum = valp;
1036 		break;
1037 
1038 	default:
1039 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1040 		return (0);
1041 	}
1042 
1043 	outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
1044 	outer_ref->type_flags = NDR_F_SIZE_IS; /* indicate pending */
1045 	outer_ref->inner_flags = NDR_F_NONE;   /* indicate pending */
1046 
1047 	bzero(&myref, sizeof (myref));
1048 	myref.stream = nds;
1049 	myref.enclosing = outer_ref;
1050 	myref.ti = outer_ref->ti;
1051 	myref.datum = outer_ref->datum;
1052 	myref.name = "CONFORMANT-CONSTRUCT";
1053 	myref.outer_flags = NDR_F_NONE;
1054 	myref.inner_flags = NDR_F_NONE;
1055 	myref.size_is = outer_ref->size_is;
1056 
1057 	myref.pdu_offset = outer_ref->pdu_offset + 4;
1058 
1059 	rc = ndr_inner(&myref);
1060 	if (!rc)
1061 		return (rc);		/* error already set */
1062 
1063 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
1064 
1065 	if (outer_ref->inner_flags != NDR_F_SIZE_IS) {
1066 		NDR_SET_ERROR(&myref, NDR_ERR_SIZE_IS_MISMATCH_AFTER);
1067 		return (0);
1068 	}
1069 
1070 	return (1);
1071 }
1072 
1073 int
1074 ndr_size_is(ndr_ref_t *ref)
1075 {
1076 	ndr_stream_t 		*nds = ref->stream;
1077 	ndr_ref_t 		*outer_ref = nds->outer_current;
1078 	ndr_typeinfo_t		*ti = outer_ref->ti;
1079 	unsigned long		size_is;
1080 	int			rc;
1081 	unsigned		n_hdr;
1082 	unsigned		n_fixed;
1083 	unsigned		n_variable;
1084 	unsigned		n_pdu_total;
1085 
1086 	assert(ref->inner_flags & NDR_F_SIZE_IS);
1087 	size_is = ref->size_is;
1088 
1089 	if (outer_ref->type_flags != NDR_F_SIZE_IS) {
1090 		NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_UNEXPECTED);
1091 		return (0);
1092 	}
1093 
1094 	if (outer_ref->inner_flags & NDR_F_SIZE_IS) {
1095 		NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_DUPLICATED);
1096 		return (0);
1097 	}
1098 
1099 	/* repeat metrics, see ndr_conformant_construct() above */
1100 	n_hdr = 4;
1101 	n_fixed = ti->pdu_size_fixed_part;
1102 	n_variable = size_is * ti->pdu_size_variable_part;
1103 	n_pdu_total = n_hdr + n_fixed + n_variable;
1104 
1105 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
1106 	if (!rc)
1107 		return (rc);		/* error already set */
1108 
1109 	switch (nds->m_op) {
1110 	case NDR_M_OP_MARSHALL:
1111 		/*
1112 		 * We have to set the sizing header and extend
1113 		 * the size of the PDU (already done).
1114 		 */
1115 		rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
1116 		if (!rc)
1117 			return (0);	/* error already set */
1118 		break;
1119 
1120 	case NDR_M_OP_UNMARSHALL:
1121 		/*
1122 		 * Allocation done during ndr_conformant_construct().
1123 		 * All we are doing here is verifying that the
1124 		 * intended size (ref->size_is) matches the sizing header.
1125 		 */
1126 		if (size_is != outer_ref->size_is) {
1127 			NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_MISMATCH_PDU);
1128 			return (0);
1129 		}
1130 		break;
1131 
1132 	default:
1133 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1134 		return (0);
1135 	}
1136 
1137 	outer_ref->inner_flags |= NDR_F_SIZE_IS;
1138 	outer_ref->size_is = ref->size_is;
1139 	return (1);
1140 }
1141 
1142 int
1143 ndr_outer_string(ndr_ref_t *outer_ref)
1144 {
1145 	ndr_stream_t	*nds = outer_ref->stream;
1146 	ndr_typeinfo_t 	*ti = outer_ref->ti;
1147 	ndr_ref_t	myref;
1148 	char 		*valp = NULL;
1149 	unsigned	is_varlen = ti->pdu_size_variable_part;
1150 	int		is_union = NDR_IS_UNION(ti);
1151 	int		is_string = NDR_IS_STRING(ti);
1152 	int		rc;
1153 	unsigned	n_zeroes;
1154 	unsigned	ix;
1155 	unsigned long	size_is;
1156 	unsigned long	first_is;
1157 	unsigned long	length_is;
1158 	unsigned	n_hdr;
1159 	unsigned	n_fixed;
1160 	unsigned	n_variable;
1161 	unsigned	n_alloc;
1162 	unsigned	n_pdu_total;
1163 	int		params;
1164 
1165 	params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
1166 
1167 	assert(is_varlen && is_string && !is_union);
1168 	assert(params == NDR_F_NONE);
1169 
1170 	/* string header for this: size_is first_is length_is */
1171 	n_hdr = 12;
1172 
1173 	/* fixed part -- exactly none of these */
1174 	n_fixed = 0;
1175 
1176 	if (!ndr_outer_grow(outer_ref, n_hdr))
1177 		return (0);		/* error already set */
1178 
1179 	switch (nds->m_op) {
1180 	case NDR_M_OP_MARSHALL:
1181 		valp = outer_ref->datum;
1182 		assert(valp);
1183 
1184 		if (outer_ref->backptr)
1185 			assert(valp == *outer_ref->backptr);
1186 
1187 		if (ti == &ndt_s_wchar) {
1188 			/*
1189 			 * size_is is the number of characters in the string,
1190 			 * including the null. We assume valp is UTF-8 encoded.
1191 			 * We can use mts_wcequiv_strlen for ASCII, extended
1192 			 * ASCII or Unicode (UCS-2).
1193 			 */
1194 			size_is = (mts_wcequiv_strlen(valp) /
1195 			    sizeof (mts_wchar_t)) + 1;
1196 
1197 			if (size_is > NDR_STRING_MAX) {
1198 				NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
1199 				return (0);
1200 			}
1201 		} else {
1202 			valp = outer_ref->datum;
1203 			n_zeroes = 0;
1204 			for (ix = 0; ix < 1024; ix++) {
1205 				if (valp[ix] == 0) {
1206 					n_zeroes++;
1207 					if (n_zeroes >= is_varlen &&
1208 					    ix % is_varlen == 0) {
1209 						break;
1210 					}
1211 				} else {
1212 					n_zeroes = 0;
1213 				}
1214 			}
1215 			if (ix >= 1024) {
1216 				NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
1217 				return (0);
1218 			}
1219 			size_is = ix+1;
1220 		}
1221 
1222 		first_is = 0;
1223 
1224 		if (nds->flags & NDS_F_NOTERM)
1225 			length_is = size_is - 1;
1226 		else
1227 			length_is = size_is;
1228 
1229 		if (!ndr_outer_poke_sizing(outer_ref, 0, &size_is) ||
1230 		    !ndr_outer_poke_sizing(outer_ref, 4, &first_is) ||
1231 		    !ndr_outer_poke_sizing(outer_ref, 8, &length_is))
1232 			return (0);		/* error already set */
1233 		break;
1234 
1235 	case NDR_M_OP_UNMARSHALL:
1236 		if (!ndr_outer_peek_sizing(outer_ref, 0, &size_is) ||
1237 		    !ndr_outer_peek_sizing(outer_ref, 4, &first_is) ||
1238 		    !ndr_outer_peek_sizing(outer_ref, 8, &length_is))
1239 			return (0);		/* error already set */
1240 
1241 		/*
1242 		 * In addition to the first_is check, we used to check that
1243 		 * size_is or size_is-1 was equal to length_is but Windows95
1244 		 * doesn't conform to this "rule" (see variable part below).
1245 		 * The srvmgr tool for Windows95 sent the following values
1246 		 * for a path string:
1247 		 *
1248 		 *	size_is   = 261 (0x105)
1249 		 *	first_is  = 0
1250 		 *	length_is = 53  (0x35)
1251 		 *
1252 		 * The length_is was correct (for the given path) but the
1253 		 * size_is was the maximum path length rather than being
1254 		 * related to length_is.
1255 		 */
1256 		if (first_is != 0) {
1257 			NDR_SET_ERROR(outer_ref, NDR_ERR_STRING_SIZING);
1258 			return (0);
1259 		}
1260 
1261 		if (ti == &ndt_s_wchar) {
1262 			/*
1263 			 * Decoding Unicode to UTF-8; we need to allow
1264 			 * for the maximum possible char size. It would
1265 			 * be nice to use mbequiv_strlen but the string
1266 			 * may not be null terminated.
1267 			 */
1268 			n_alloc = (size_is + 1) * MTS_MB_CHAR_MAX;
1269 		} else {
1270 			n_alloc = (size_is + 1) * is_varlen;
1271 		}
1272 
1273 		valp = NDS_MALLOC(nds, n_alloc, outer_ref);
1274 		if (!valp) {
1275 			NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
1276 			return (0);
1277 		}
1278 
1279 		bzero(valp, (size_is+1) * is_varlen);
1280 
1281 		if (outer_ref->backptr)
1282 			*outer_ref->backptr = valp;
1283 		outer_ref->datum = valp;
1284 		break;
1285 
1286 	default:
1287 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1288 		return (0);
1289 	}
1290 
1291 	/*
1292 	 * Variable part - exactly length_is of these.
1293 	 *
1294 	 * Usually, length_is is same as size_is and includes nul.
1295 	 * Some protocols use length_is = size_is-1, and length_is does
1296 	 * not include the nul (which is more consistent with DCE spec).
1297 	 * If the length_is is 0, there is no data following the
1298 	 * sizing header, regardless of size_is.
1299 	 */
1300 	n_variable = length_is * is_varlen;
1301 
1302 	/* sum them up to determine the PDU space required */
1303 	n_pdu_total = n_hdr + n_fixed + n_variable;
1304 
1305 	/* similar sum to determine how much local memory is required */
1306 	n_alloc = n_fixed + n_variable;
1307 
1308 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
1309 	if (!rc)
1310 		return (rc);		/* error already set */
1311 
1312 	if (length_is > 0) {
1313 		bzero(&myref, sizeof (myref));
1314 		myref.stream = nds;
1315 		myref.enclosing = outer_ref;
1316 		myref.ti = outer_ref->ti;
1317 		myref.datum = outer_ref->datum;
1318 		myref.name = "OUTER-STRING";
1319 		myref.outer_flags = NDR_F_IS_STRING;
1320 		myref.inner_flags = NDR_F_NONE;
1321 
1322 		/*
1323 		 * Set up size_is and strlen_is for ndr_s_wchar.
1324 		 */
1325 		myref.size_is = size_is;
1326 		myref.strlen_is = length_is;
1327 	}
1328 
1329 	myref.pdu_offset = outer_ref->pdu_offset + 12;
1330 
1331 	/*
1332 	 * Don't try to decode empty strings.
1333 	 */
1334 	if ((size_is == 0) && (first_is == 0) && (length_is == 0)) {
1335 		nds->pdu_scan_offset = outer_ref->pdu_end_offset;
1336 		return (1);
1337 	}
1338 
1339 	if ((size_is != 0) && (length_is != 0)) {
1340 		rc = ndr_inner(&myref);
1341 		if (!rc)
1342 			return (rc);		/* error already set */
1343 	}
1344 
1345 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
1346 	return (1);
1347 }
1348 
1349 int
1350 ndr_outer_peek_sizing(ndr_ref_t *outer_ref, unsigned offset,
1351     unsigned long *sizing_p)
1352 {
1353 	ndr_stream_t 	*nds = outer_ref->stream;
1354 	unsigned long	pdu_offset;
1355 	int		rc;
1356 
1357 	pdu_offset = outer_ref->pdu_offset + offset;
1358 
1359 	if (pdu_offset < nds->outer_current->pdu_offset ||
1360 	    pdu_offset > nds->outer_current->pdu_end_offset ||
1361 	    pdu_offset+4 > nds->outer_current->pdu_end_offset) {
1362 		NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
1363 		return (0);
1364 	}
1365 
1366 	switch (nds->m_op) {
1367 	case NDR_M_OP_MARSHALL:
1368 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
1369 		return (0);
1370 
1371 	case NDR_M_OP_UNMARSHALL:
1372 		rc = NDS_GET_PDU(nds, pdu_offset, 4, (char *)sizing_p,
1373 		    nds->swap, outer_ref);
1374 		break;
1375 
1376 	default:
1377 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1378 		return (0);
1379 	}
1380 
1381 	return (rc);
1382 }
1383 
1384 int
1385 ndr_outer_poke_sizing(ndr_ref_t *outer_ref, unsigned offset,
1386     unsigned long *sizing_p)
1387 {
1388 	ndr_stream_t 	*nds = outer_ref->stream;
1389 	unsigned long	pdu_offset;
1390 	int		rc;
1391 
1392 	pdu_offset = outer_ref->pdu_offset + offset;
1393 
1394 	if (pdu_offset < nds->outer_current->pdu_offset ||
1395 	    pdu_offset > nds->outer_current->pdu_end_offset ||
1396 	    pdu_offset+4 > nds->outer_current->pdu_end_offset) {
1397 		NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
1398 		return (0);
1399 	}
1400 
1401 	switch (nds->m_op) {
1402 	case NDR_M_OP_MARSHALL:
1403 		rc = NDS_PUT_PDU(nds, pdu_offset, 4, (char *)sizing_p,
1404 		    nds->swap, outer_ref);
1405 		break;
1406 
1407 	case NDR_M_OP_UNMARSHALL:
1408 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
1409 		return (0);
1410 
1411 	default:
1412 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1413 		return (0);
1414 	}
1415 
1416 	return (rc);
1417 }
1418 
1419 /*
1420  * All OUTER constructs begin on a mod4 (dword) boundary - except
1421  * for the ones that don't: some MSRPC calls appear to use word or
1422  * packed alignment.  Strings appear to be dword aligned.
1423  */
1424 int
1425 ndr_outer_align(ndr_ref_t *outer_ref)
1426 {
1427 	ndr_stream_t 	*nds = outer_ref->stream;
1428 	int		rc;
1429 	unsigned	n_pad;
1430 	unsigned	align;
1431 
1432 	if (outer_ref->packed_alignment && outer_ref->ti != &ndt_s_wchar) {
1433 		align = outer_ref->ti->alignment;
1434 		n_pad = ((align + 1) - nds->pdu_scan_offset) & align;
1435 	} else {
1436 		n_pad = NDR_ALIGN4(nds->pdu_scan_offset);
1437 	}
1438 
1439 	if (n_pad == 0)
1440 		return (1);	/* already aligned, often the case */
1441 
1442 	if (!ndr_outer_grow(outer_ref, n_pad))
1443 		return (0);	/* error already set */
1444 
1445 	switch (nds->m_op) {
1446 	case NDR_M_OP_MARSHALL:
1447 		rc = NDS_PAD_PDU(nds, nds->pdu_scan_offset, n_pad, outer_ref);
1448 		if (!rc) {
1449 			NDR_SET_ERROR(outer_ref, NDR_ERR_PAD_FAILED);
1450 			return (0);
1451 		}
1452 		break;
1453 
1454 	case NDR_M_OP_UNMARSHALL:
1455 		break;
1456 
1457 	default:
1458 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1459 		return (0);
1460 	}
1461 
1462 	nds->pdu_scan_offset += n_pad;
1463 	return (1);
1464 }
1465 
1466 int
1467 ndr_outer_grow(ndr_ref_t *outer_ref, unsigned n_total)
1468 {
1469 	ndr_stream_t 	*nds = outer_ref->stream;
1470 	unsigned long	pdu_want_size;
1471 	int		rc, is_ok = 0;
1472 
1473 	pdu_want_size = nds->pdu_scan_offset + n_total;
1474 
1475 	if (pdu_want_size <= nds->pdu_max_size) {
1476 		is_ok = 1;
1477 	}
1478 
1479 	switch (nds->m_op) {
1480 	case NDR_M_OP_MARSHALL:
1481 		if (is_ok)
1482 			break;
1483 		rc = NDS_GROW_PDU(nds, pdu_want_size, outer_ref);
1484 		if (!rc) {
1485 			NDR_SET_ERROR(outer_ref, NDR_ERR_GROW_FAILED);
1486 			return (0);
1487 		}
1488 		break;
1489 
1490 	case NDR_M_OP_UNMARSHALL:
1491 		if (is_ok)
1492 			break;
1493 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNDERFLOW);
1494 		return (0);
1495 
1496 	default:
1497 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1498 		return (0);
1499 	}
1500 
1501 	if (nds->pdu_size < pdu_want_size)
1502 		nds->pdu_size = pdu_want_size;
1503 
1504 	outer_ref->pdu_end_offset = pdu_want_size;
1505 	return (1);
1506 }
1507 
1508 /*
1509  * INNER ELEMENTS
1510  *
1511  * The local datum (arg_ref->datum) already exists, there is no need to
1512  * malloc() it.  The datum should point at a member of a structure.
1513  *
1514  * For the most part, ndr_inner() and its helpers are just a sanity
1515  * check.  The underlying ti->ndr_func() could be called immediately
1516  * for non-pointer elements.  For the sake of robustness, we detect
1517  * run-time errors here.  Most of the situations this protects against
1518  * have already been checked by the IDL compiler.  This is also a
1519  * common point for processing of all data, and so is a convenient
1520  * place to work from for debugging.
1521  */
1522 int
1523 ndr_inner(ndr_ref_t *arg_ref)
1524 {
1525 	ndr_typeinfo_t 	*ti = arg_ref->ti;
1526 	int	is_varlen = ti->pdu_size_variable_part;
1527 	int	is_union = NDR_IS_UNION(ti);
1528 	int	error = NDR_ERR_INNER_PARAMS_BAD;
1529 	int	params;
1530 
1531 	params = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1532 
1533 	switch (params) {
1534 	case NDR_F_NONE:
1535 		if (is_union) {
1536 			error = NDR_ERR_SWITCH_VALUE_MISSING;
1537 			break;
1538 		}
1539 		return (*ti->ndr_func)(arg_ref);
1540 		break;
1541 
1542 	case NDR_F_SIZE_IS:
1543 	case NDR_F_DIMENSION_IS:
1544 	case NDR_F_IS_POINTER+NDR_F_SIZE_IS:   /* pointer to something */
1545 	case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: /* pointer to something */
1546 		if (is_varlen) {
1547 			error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
1548 			break;
1549 		}
1550 		if (is_union) {
1551 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1552 			break;
1553 		}
1554 		if (params & NDR_F_IS_POINTER)
1555 			return (ndr_inner_pointer(arg_ref));
1556 		else if (params & NDR_F_IS_REFERENCE)
1557 			return (ndr_inner_reference(arg_ref));
1558 		else
1559 			return (ndr_inner_array(arg_ref));
1560 		break;
1561 
1562 	case NDR_F_IS_POINTER:	/* type is pointer to one something */
1563 		if (is_union) {
1564 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1565 			break;
1566 		}
1567 		return (ndr_inner_pointer(arg_ref));
1568 		break;
1569 
1570 	case NDR_F_IS_REFERENCE:	/* type is pointer to one something */
1571 		if (is_union) {
1572 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1573 			break;
1574 		}
1575 		return (ndr_inner_reference(arg_ref));
1576 		break;
1577 
1578 	case NDR_F_SWITCH_IS:
1579 		if (!is_union) {
1580 			error = NDR_ERR_SWITCH_VALUE_ILLEGAL;
1581 			break;
1582 		}
1583 		return (*ti->ndr_func)(arg_ref);
1584 		break;
1585 
1586 	default:
1587 		error = NDR_ERR_INNER_PARAMS_BAD;
1588 		break;
1589 	}
1590 
1591 	/*
1592 	 * If we get here, something is wrong. Most likely,
1593 	 * the params flags do not match
1594 	 */
1595 	NDR_SET_ERROR(arg_ref, error);
1596 	return (0);
1597 }
1598 
1599 int
1600 ndr_inner_pointer(ndr_ref_t *arg_ref)
1601 {
1602 	ndr_stream_t	*nds = arg_ref->stream;
1603 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
1604 	char 		**valpp = (char **)arg_ref->datum;
1605 	ndr_ref_t 	*outer_ref;
1606 
1607 	if (!ndr__ulong(arg_ref))
1608 		return (0);	/* error */
1609 	if (!*valpp)
1610 		return (1);	/* NULL pointer */
1611 
1612 	outer_ref = ndr_enter_outer_queue(arg_ref);
1613 	if (!outer_ref)
1614 		return (0);	/* error already set */
1615 
1616 	/* move advice in inner_flags to outer_flags sans pointer */
1617 	outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1618 	outer_ref->outer_flags &= ~NDR_F_IS_POINTER;
1619 #ifdef NDR_INNER_NOT_YET
1620 	outer_ref->outer_flags |= NDR_F_BACKPTR;
1621 	if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
1622 		outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
1623 	}
1624 #endif /* NDR_INNER_NOT_YET */
1625 
1626 	outer_ref->backptr = valpp;
1627 
1628 	switch (nds->m_op) {
1629 	case NDR_M_OP_MARSHALL:
1630 		outer_ref->datum = *valpp;
1631 		break;
1632 
1633 	case NDR_M_OP_UNMARSHALL:
1634 		/*
1635 		 * This is probably wrong if the application allocated
1636 		 * memory in advance.  Indicate no value for now.
1637 		 * ONC RPC handles this case.
1638 		 */
1639 		*valpp = 0;
1640 		outer_ref->datum = 0;
1641 		break;
1642 	}
1643 
1644 	return (1);		/* pointer dereference scheduled */
1645 }
1646 
1647 int
1648 ndr_inner_reference(ndr_ref_t *arg_ref)
1649 {
1650 	ndr_stream_t	*nds = arg_ref->stream;
1651 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
1652 	char		**valpp = (char **)arg_ref->datum;
1653 	ndr_ref_t	*outer_ref;
1654 
1655 	outer_ref = ndr_enter_outer_queue(arg_ref);
1656 	if (!outer_ref)
1657 		return (0);	/* error already set */
1658 
1659 	/* move advice in inner_flags to outer_flags sans pointer */
1660 	outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1661 	outer_ref->outer_flags &= ~NDR_F_IS_REFERENCE;
1662 #ifdef NDR_INNER_REF_NOT_YET
1663 	outer_ref->outer_flags |= NDR_F_BACKPTR;
1664 	if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
1665 		outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
1666 	}
1667 #endif /* NDR_INNER_REF_NOT_YET */
1668 
1669 	outer_ref->backptr = valpp;
1670 
1671 	switch (nds->m_op) {
1672 	case NDR_M_OP_MARSHALL:
1673 		outer_ref->datum = *valpp;
1674 		break;
1675 
1676 	case NDR_M_OP_UNMARSHALL:
1677 		/*
1678 		 * This is probably wrong if the application allocated
1679 		 * memory in advance.  Indicate no value for now.
1680 		 * ONC RPC handles this case.
1681 		 */
1682 		*valpp = 0;
1683 		outer_ref->datum = 0;
1684 		break;
1685 	}
1686 
1687 	return (1);		/* pointer dereference scheduled */
1688 }
1689 
1690 int
1691 ndr_inner_array(ndr_ref_t *encl_ref)
1692 {
1693 	ndr_typeinfo_t		*ti = encl_ref->ti;
1694 	ndr_ref_t		myref;
1695 	unsigned long		pdu_offset = encl_ref->pdu_offset;
1696 	unsigned long		n_elem;
1697 	unsigned long		i;
1698 	char			name[30];
1699 
1700 	if (encl_ref->inner_flags & NDR_F_SIZE_IS) {
1701 		/* now is the time to check/set size */
1702 		if (!ndr_size_is(encl_ref))
1703 			return (0);	/* error already set */
1704 		n_elem = encl_ref->size_is;
1705 	} else {
1706 		assert(encl_ref->inner_flags & NDR_F_DIMENSION_IS);
1707 		n_elem = encl_ref->dimension_is;
1708 	}
1709 
1710 	bzero(&myref, sizeof (myref));
1711 	myref.enclosing = encl_ref;
1712 	myref.stream = encl_ref->stream;
1713 	myref.packed_alignment = 0;
1714 	myref.ti = ti;
1715 	myref.inner_flags = NDR_F_NONE;
1716 
1717 	for (i = 0; i < n_elem; i++) {
1718 		(void) sprintf(name, "[%lu]", i);
1719 		myref.name = name;
1720 		myref.pdu_offset = pdu_offset + i * ti->pdu_size_fixed_part;
1721 		myref.datum = encl_ref->datum + i * ti->c_size_fixed_part;
1722 
1723 		if (!ndr_inner(&myref))
1724 			return (0);
1725 	}
1726 
1727 	return (1);
1728 }
1729 
1730 
1731 /*
1732  * BASIC TYPES
1733  */
1734 #define	MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
1735     extern int ndr_##TYPE(struct ndr_reference *encl_ref); \
1736     ndr_typeinfo_t ndt_##TYPE = { \
1737 	1,		/* NDR version */ \
1738 	(SIZE)-1,	/* alignment */ \
1739 	NDR_F_NONE,	/* flags */ \
1740 	ndr_##TYPE,	/* ndr_func */ \
1741 	SIZE,		/* pdu_size_fixed_part */ \
1742 	0,		/* pdu_size_variable_part */ \
1743 	SIZE,		/* c_size_fixed_part */ \
1744 	0,		/* c_size_variable_part */ \
1745 	}; \
1746     int ndr_##TYPE(struct ndr_reference *ref) { \
1747 	return (ndr_basic_integer(ref, SIZE)); \
1748 }
1749 
1750 #define	MAKE_BASIC_TYPE_STRING(TYPE, SIZE) \
1751     extern int ndr_s##TYPE(struct ndr_reference *encl_ref); \
1752     ndr_typeinfo_t ndt_s##TYPE = { \
1753 	1,		/* NDR version */ \
1754 	(SIZE)-1,	/* alignment */ \
1755 	NDR_F_STRING,	/* flags */ \
1756 	ndr_s##TYPE,	/* ndr_func */ \
1757 	0,		/* pdu_size_fixed_part */ \
1758 	SIZE,		/* pdu_size_variable_part */ \
1759 	0,		/* c_size_fixed_part */ \
1760 	SIZE,		/* c_size_variable_part */ \
1761 	}; \
1762     int ndr_s##TYPE(struct ndr_reference *ref) { \
1763 	return (ndr_string_basic_integer(ref, &ndt_##TYPE)); \
1764 }
1765 
1766 #define	MAKE_BASIC_TYPE(TYPE, SIZE) \
1767 	MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
1768 	MAKE_BASIC_TYPE_STRING(TYPE, SIZE)
1769 
1770 int ndr_basic_integer(ndr_ref_t *, unsigned);
1771 int ndr_string_basic_integer(ndr_ref_t *, ndr_typeinfo_t *);
1772 
1773 
1774 MAKE_BASIC_TYPE(_char, 1)
1775 MAKE_BASIC_TYPE(_uchar, 1)
1776 MAKE_BASIC_TYPE(_short, 2)
1777 MAKE_BASIC_TYPE(_ushort, 2)
1778 MAKE_BASIC_TYPE(_long, 4)
1779 MAKE_BASIC_TYPE(_ulong, 4)
1780 
1781 MAKE_BASIC_TYPE_BASE(_wchar, 2)
1782 
1783 int
1784 ndr_basic_integer(ndr_ref_t *ref, unsigned size)
1785 {
1786 	ndr_stream_t 	*nds = ref->stream;
1787 	char 		*valp = (char *)ref->datum;
1788 	int		rc;
1789 
1790 	switch (nds->m_op) {
1791 	case NDR_M_OP_MARSHALL:
1792 		rc = NDS_PUT_PDU(nds, ref->pdu_offset, size,
1793 		    valp, nds->swap, ref);
1794 		break;
1795 
1796 	case NDR_M_OP_UNMARSHALL:
1797 		rc = NDS_GET_PDU(nds, ref->pdu_offset, size,
1798 		    valp, nds->swap, ref);
1799 		break;
1800 
1801 	default:
1802 		NDR_SET_ERROR(ref, NDR_ERR_M_OP_INVALID);
1803 		return (0);
1804 	}
1805 
1806 	return (rc);
1807 }
1808 
1809 int
1810 ndr_string_basic_integer(ndr_ref_t *encl_ref, ndr_typeinfo_t *type_under)
1811 {
1812 	unsigned long		pdu_offset = encl_ref->pdu_offset;
1813 	unsigned		size = type_under->pdu_size_fixed_part;
1814 	char			*valp;
1815 	ndr_ref_t		myref;
1816 	unsigned long		i;
1817 	long			sense = 0;
1818 	char			name[30];
1819 
1820 	assert(size != 0);
1821 
1822 	bzero(&myref, sizeof (myref));
1823 	myref.enclosing = encl_ref;
1824 	myref.stream = encl_ref->stream;
1825 	myref.packed_alignment = 0;
1826 	myref.ti = type_under;
1827 	myref.inner_flags = NDR_F_NONE;
1828 	myref.name = name;
1829 
1830 	for (i = 0; i < NDR_STRING_MAX; i++) {
1831 		(void) sprintf(name, "[%lu]", i);
1832 		myref.pdu_offset = pdu_offset + i * size;
1833 		valp = encl_ref->datum + i * size;
1834 		myref.datum = valp;
1835 
1836 		if (!ndr_inner(&myref))
1837 			return (0);
1838 
1839 		switch (size) {
1840 		case 1:		sense = *valp; break;
1841 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1842 		case 2:		sense = *(short *)valp; break;
1843 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1844 		case 4:		sense = *(long *)valp; break;
1845 		}
1846 
1847 		if (!sense)
1848 			break;
1849 	}
1850 
1851 	return (1);
1852 }
1853 
1854 
1855 extern int ndr_s_wchar(ndr_ref_t *encl_ref);
1856 ndr_typeinfo_t ndt_s_wchar = {
1857 	1,		/* NDR version */
1858 	2-1,		/* alignment */
1859 	NDR_F_STRING,	/* flags */
1860 	ndr_s_wchar,	/* ndr_func */
1861 	0,		/* pdu_size_fixed_part */
1862 	2,		/* pdu_size_variable_part */
1863 	0,		/* c_size_fixed_part */
1864 	1,		/* c_size_variable_part */
1865 };
1866 
1867 
1868 /*
1869  * Hand coded wchar function because all strings are transported
1870  * as wide characters. During NDR_M_OP_MARSHALL, we convert from
1871  * multi-byte to wide characters. During NDR_M_OP_UNMARSHALL, we
1872  * convert from wide characters to multi-byte.
1873  *
1874  * It appeared that NT would sometimes leave a spurious character
1875  * in the data stream before the null wide_char, which would get
1876  * included in the string decode because we processed until the
1877  * null character. It now looks like NT does not always terminate
1878  * RPC Unicode strings and the terminating null is a side effect
1879  * of field alignment. So now we rely on the strlen_is (set up in
1880  * ndr_outer_string) of the enclosing reference. This may or may
1881  * not include the null but it doesn't matter, the algorithm will
1882  * get it right.
1883  */
1884 int
1885 ndr_s_wchar(ndr_ref_t *encl_ref)
1886 {
1887 	ndr_stream_t		*nds = encl_ref->stream;
1888 	unsigned short		wide_char;
1889 	char 			*valp;
1890 	ndr_ref_t		myref;
1891 	unsigned long		i;
1892 	char			name[30];
1893 	int			count;
1894 	int			char_count = 0;
1895 
1896 	if (nds->m_op == NDR_M_OP_UNMARSHALL) {
1897 		/*
1898 		 * To avoid problems with zero length strings
1899 		 * we can just null terminate here and be done.
1900 		 */
1901 		if (encl_ref->strlen_is == 0) {
1902 			encl_ref->datum[0] = '\0';
1903 			return (1);
1904 		}
1905 	}
1906 
1907 	bzero(&myref, sizeof (myref));
1908 	myref.enclosing = encl_ref;
1909 	myref.stream = encl_ref->stream;
1910 	myref.packed_alignment = 0;
1911 	myref.ti = &ndt__wchar;
1912 	myref.inner_flags = NDR_F_NONE;
1913 	myref.datum = (char *)&wide_char;
1914 	myref.name = name;
1915 	myref.pdu_offset = encl_ref->pdu_offset;
1916 
1917 	valp = encl_ref->datum;
1918 	count = 0;
1919 
1920 	for (i = 0; i < NDR_STRING_MAX; i++) {
1921 		(void) sprintf(name, "[%lu]", i);
1922 
1923 		if (nds->m_op == NDR_M_OP_MARSHALL) {
1924 			count = mts_mbtowc((mts_wchar_t *)&wide_char, valp,
1925 			    MTS_MB_CHAR_MAX);
1926 			if (count < 0) {
1927 				return (0);
1928 			} else if (count == 0) {
1929 				if (encl_ref->strlen_is != encl_ref->size_is)
1930 					break;
1931 
1932 				/*
1933 				 * If the input char is 0, mbtowc
1934 				 * returns 0 without setting wide_char.
1935 				 * Set wide_char to 0 and a count of 1.
1936 				 */
1937 				wide_char = *valp;
1938 				count = 1;
1939 			}
1940 		}
1941 
1942 		if (!ndr_inner(&myref))
1943 			return (0);
1944 
1945 		if (nds->m_op == NDR_M_OP_UNMARSHALL) {
1946 			count = mts_wctomb(valp, wide_char);
1947 
1948 			if ((++char_count) == encl_ref->strlen_is) {
1949 				valp += count;
1950 				*valp = '\0';
1951 				break;
1952 			}
1953 		}
1954 
1955 		if (!wide_char)
1956 			break;
1957 
1958 		myref.pdu_offset += sizeof (wide_char);
1959 		valp += count;
1960 	}
1961 
1962 	return (1);
1963 }
1964 
1965 /*
1966  * Converts a multibyte character string to a little-endian, wide-char
1967  * string.  No more than nwchars wide characters are stored.
1968  * A terminating null wide character is appended if there is room.
1969  *
1970  * Returns the number of wide characters converted, not counting
1971  * any terminating null wide character.  Returns -1 if an invalid
1972  * multibyte character is encountered.
1973  */
1974 size_t
1975 ndr_mbstowcs(ndr_stream_t *nds, mts_wchar_t *wcs, const char *mbs,
1976     size_t nwchars)
1977 {
1978 	mts_wchar_t *start = wcs;
1979 	int nbytes;
1980 
1981 	while (nwchars--) {
1982 		nbytes = ndr_mbtowc(nds, wcs, mbs, MTS_MB_CHAR_MAX);
1983 		if (nbytes < 0) {
1984 			*wcs = 0;
1985 			return ((size_t)-1);
1986 		}
1987 
1988 		if (*mbs == 0)
1989 			break;
1990 
1991 		++wcs;
1992 		mbs += nbytes;
1993 	}
1994 
1995 	return (wcs - start);
1996 }
1997 
1998 /*
1999  * Converts a multibyte character to a little-endian, wide-char, which
2000  * is stored in wcharp.  Up to nbytes bytes are examined.
2001  *
2002  * If mbchar is valid, returns the number of bytes processed in mbchar.
2003  * If mbchar is invalid, returns -1.  See also mts_mbtowc().
2004  */
2005 /*ARGSUSED*/
2006 int
2007 ndr_mbtowc(ndr_stream_t *nds, mts_wchar_t *wcharp, const char *mbchar,
2008     size_t nbytes)
2009 {
2010 	int rc;
2011 
2012 	if ((rc = mts_mbtowc(wcharp, mbchar, nbytes)) < 0)
2013 		return (rc);
2014 
2015 #ifdef _BIG_ENDIAN
2016 	if (nds == NULL || NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND))
2017 		*wcharp = BSWAP_16(*wcharp);
2018 #endif
2019 
2020 	return (rc);
2021 }
2022