xref: /titanic_41/usr/src/lib/smbsrv/libmlrpc/common/ndr_process.c (revision e5816e352629470f540696fb7aa56c52d6719e67)
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 2009 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
1190 			 * (multibyte) string, including the null.
1191 			 */
1192 			size_is = (mts_wcequiv_strlen(valp) /
1193 			    sizeof (mts_wchar_t)) + 1;
1194 
1195 			if (size_is > NDR_STRING_MAX) {
1196 				NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
1197 				return (0);
1198 			}
1199 		} else {
1200 			valp = outer_ref->datum;
1201 			n_zeroes = 0;
1202 			for (ix = 0; ix < 1024; ix++) {
1203 				if (valp[ix] == 0) {
1204 					n_zeroes++;
1205 					if (n_zeroes >= is_varlen &&
1206 					    ix % is_varlen == 0) {
1207 						break;
1208 					}
1209 				} else {
1210 					n_zeroes = 0;
1211 				}
1212 			}
1213 			if (ix >= 1024) {
1214 				NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
1215 				return (0);
1216 			}
1217 			size_is = ix+1;
1218 		}
1219 
1220 		first_is = 0;
1221 
1222 		if (nds->flags & NDS_F_NOTERM)
1223 			length_is = size_is - 1;
1224 		else
1225 			length_is = size_is;
1226 
1227 		if (!ndr_outer_poke_sizing(outer_ref, 0, &size_is) ||
1228 		    !ndr_outer_poke_sizing(outer_ref, 4, &first_is) ||
1229 		    !ndr_outer_poke_sizing(outer_ref, 8, &length_is))
1230 			return (0);		/* error already set */
1231 		break;
1232 
1233 	case NDR_M_OP_UNMARSHALL:
1234 		if (!ndr_outer_peek_sizing(outer_ref, 0, &size_is) ||
1235 		    !ndr_outer_peek_sizing(outer_ref, 4, &first_is) ||
1236 		    !ndr_outer_peek_sizing(outer_ref, 8, &length_is))
1237 			return (0);		/* error already set */
1238 
1239 		/*
1240 		 * In addition to the first_is check, we used to check that
1241 		 * size_is or size_is-1 was equal to length_is but Windows95
1242 		 * doesn't conform to this "rule" (see variable part below).
1243 		 * The srvmgr tool for Windows95 sent the following values
1244 		 * for a path string:
1245 		 *
1246 		 *	size_is   = 261 (0x105)
1247 		 *	first_is  = 0
1248 		 *	length_is = 53  (0x35)
1249 		 *
1250 		 * The length_is was correct (for the given path) but the
1251 		 * size_is was the maximum path length rather than being
1252 		 * related to length_is.
1253 		 */
1254 		if (first_is != 0) {
1255 			NDR_SET_ERROR(outer_ref, NDR_ERR_STRING_SIZING);
1256 			return (0);
1257 		}
1258 
1259 		if (ti == &ndt_s_wchar) {
1260 			/*
1261 			 * Decoding Unicode to UTF-8; we need to allow
1262 			 * for the maximum possible char size. It would
1263 			 * be nice to use mbequiv_strlen but the string
1264 			 * may not be null terminated.
1265 			 */
1266 			n_alloc = (size_is + 1) * MTS_MB_CHAR_MAX;
1267 		} else {
1268 			n_alloc = (size_is + 1) * is_varlen;
1269 		}
1270 
1271 		valp = NDS_MALLOC(nds, n_alloc, outer_ref);
1272 		if (!valp) {
1273 			NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
1274 			return (0);
1275 		}
1276 
1277 		bzero(valp, (size_is+1) * is_varlen);
1278 
1279 		if (outer_ref->backptr)
1280 			*outer_ref->backptr = valp;
1281 		outer_ref->datum = valp;
1282 		break;
1283 
1284 	default:
1285 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1286 		return (0);
1287 	}
1288 
1289 	/*
1290 	 * Variable part - exactly length_is of these.
1291 	 *
1292 	 * Usually, length_is is same as size_is and includes nul.
1293 	 * Some protocols use length_is = size_is-1, and length_is does
1294 	 * not include the nul (which is more consistent with DCE spec).
1295 	 * If the length_is is 0, there is no data following the
1296 	 * sizing header, regardless of size_is.
1297 	 */
1298 	n_variable = length_is * is_varlen;
1299 
1300 	/* sum them up to determine the PDU space required */
1301 	n_pdu_total = n_hdr + n_fixed + n_variable;
1302 
1303 	/* similar sum to determine how much local memory is required */
1304 	n_alloc = n_fixed + n_variable;
1305 
1306 	rc = ndr_outer_grow(outer_ref, n_pdu_total);
1307 	if (!rc)
1308 		return (rc);		/* error already set */
1309 
1310 	if (length_is > 0) {
1311 		bzero(&myref, sizeof (myref));
1312 		myref.stream = nds;
1313 		myref.enclosing = outer_ref;
1314 		myref.ti = outer_ref->ti;
1315 		myref.datum = outer_ref->datum;
1316 		myref.name = "OUTER-STRING";
1317 		myref.outer_flags = NDR_F_IS_STRING;
1318 		myref.inner_flags = NDR_F_NONE;
1319 
1320 		/*
1321 		 * Set up size_is and strlen_is for ndr_s_wchar.
1322 		 */
1323 		myref.size_is = size_is;
1324 		myref.strlen_is = length_is;
1325 	}
1326 
1327 	myref.pdu_offset = outer_ref->pdu_offset + 12;
1328 
1329 	/*
1330 	 * Don't try to decode empty strings.
1331 	 */
1332 	if ((size_is == 0) && (first_is == 0) && (length_is == 0)) {
1333 		nds->pdu_scan_offset = outer_ref->pdu_end_offset;
1334 		return (1);
1335 	}
1336 
1337 	if ((size_is != 0) && (length_is != 0)) {
1338 		rc = ndr_inner(&myref);
1339 		if (!rc)
1340 			return (rc);		/* error already set */
1341 	}
1342 
1343 	nds->pdu_scan_offset = outer_ref->pdu_end_offset;
1344 	return (1);
1345 }
1346 
1347 int
1348 ndr_outer_peek_sizing(ndr_ref_t *outer_ref, unsigned offset,
1349     unsigned long *sizing_p)
1350 {
1351 	ndr_stream_t 	*nds = outer_ref->stream;
1352 	unsigned long	pdu_offset;
1353 	int		rc;
1354 
1355 	pdu_offset = outer_ref->pdu_offset + offset;
1356 
1357 	if (pdu_offset < nds->outer_current->pdu_offset ||
1358 	    pdu_offset > nds->outer_current->pdu_end_offset ||
1359 	    pdu_offset+4 > nds->outer_current->pdu_end_offset) {
1360 		NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
1361 		return (0);
1362 	}
1363 
1364 	switch (nds->m_op) {
1365 	case NDR_M_OP_MARSHALL:
1366 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
1367 		return (0);
1368 
1369 	case NDR_M_OP_UNMARSHALL:
1370 		rc = NDS_GET_PDU(nds, pdu_offset, 4, (char *)sizing_p,
1371 		    nds->swap, outer_ref);
1372 		break;
1373 
1374 	default:
1375 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1376 		return (0);
1377 	}
1378 
1379 	return (rc);
1380 }
1381 
1382 int
1383 ndr_outer_poke_sizing(ndr_ref_t *outer_ref, unsigned offset,
1384     unsigned long *sizing_p)
1385 {
1386 	ndr_stream_t 	*nds = outer_ref->stream;
1387 	unsigned long	pdu_offset;
1388 	int		rc;
1389 
1390 	pdu_offset = outer_ref->pdu_offset + offset;
1391 
1392 	if (pdu_offset < nds->outer_current->pdu_offset ||
1393 	    pdu_offset > nds->outer_current->pdu_end_offset ||
1394 	    pdu_offset+4 > nds->outer_current->pdu_end_offset) {
1395 		NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
1396 		return (0);
1397 	}
1398 
1399 	switch (nds->m_op) {
1400 	case NDR_M_OP_MARSHALL:
1401 		rc = NDS_PUT_PDU(nds, pdu_offset, 4, (char *)sizing_p,
1402 		    nds->swap, outer_ref);
1403 		break;
1404 
1405 	case NDR_M_OP_UNMARSHALL:
1406 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
1407 		return (0);
1408 
1409 	default:
1410 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1411 		return (0);
1412 	}
1413 
1414 	return (rc);
1415 }
1416 
1417 /*
1418  * All OUTER constructs begin on a mod4 (dword) boundary - except
1419  * for the ones that don't: some MSRPC calls appear to use word or
1420  * packed alignment.  Strings appear to be dword aligned.
1421  */
1422 int
1423 ndr_outer_align(ndr_ref_t *outer_ref)
1424 {
1425 	ndr_stream_t 	*nds = outer_ref->stream;
1426 	int		rc;
1427 	unsigned	n_pad;
1428 	unsigned	align;
1429 
1430 	if (outer_ref->packed_alignment && outer_ref->ti != &ndt_s_wchar) {
1431 		align = outer_ref->ti->alignment;
1432 		n_pad = ((align + 1) - nds->pdu_scan_offset) & align;
1433 	} else {
1434 		n_pad = NDR_ALIGN4(nds->pdu_scan_offset);
1435 	}
1436 
1437 	if (n_pad == 0)
1438 		return (1);	/* already aligned, often the case */
1439 
1440 	if (!ndr_outer_grow(outer_ref, n_pad))
1441 		return (0);	/* error already set */
1442 
1443 	switch (nds->m_op) {
1444 	case NDR_M_OP_MARSHALL:
1445 		rc = NDS_PAD_PDU(nds, nds->pdu_scan_offset, n_pad, outer_ref);
1446 		if (!rc) {
1447 			NDR_SET_ERROR(outer_ref, NDR_ERR_PAD_FAILED);
1448 			return (0);
1449 		}
1450 		break;
1451 
1452 	case NDR_M_OP_UNMARSHALL:
1453 		break;
1454 
1455 	default:
1456 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1457 		return (0);
1458 	}
1459 
1460 	nds->pdu_scan_offset += n_pad;
1461 	return (1);
1462 }
1463 
1464 int
1465 ndr_outer_grow(ndr_ref_t *outer_ref, unsigned n_total)
1466 {
1467 	ndr_stream_t 	*nds = outer_ref->stream;
1468 	unsigned long	pdu_want_size;
1469 	int		rc, is_ok = 0;
1470 
1471 	pdu_want_size = nds->pdu_scan_offset + n_total;
1472 
1473 	if (pdu_want_size <= nds->pdu_max_size) {
1474 		is_ok = 1;
1475 	}
1476 
1477 	switch (nds->m_op) {
1478 	case NDR_M_OP_MARSHALL:
1479 		if (is_ok)
1480 			break;
1481 		rc = NDS_GROW_PDU(nds, pdu_want_size, outer_ref);
1482 		if (!rc) {
1483 			NDR_SET_ERROR(outer_ref, NDR_ERR_GROW_FAILED);
1484 			return (0);
1485 		}
1486 		break;
1487 
1488 	case NDR_M_OP_UNMARSHALL:
1489 		if (is_ok)
1490 			break;
1491 		NDR_SET_ERROR(outer_ref, NDR_ERR_UNDERFLOW);
1492 		return (0);
1493 
1494 	default:
1495 		NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
1496 		return (0);
1497 	}
1498 
1499 	if (nds->pdu_size < pdu_want_size)
1500 		nds->pdu_size = pdu_want_size;
1501 
1502 	outer_ref->pdu_end_offset = pdu_want_size;
1503 	return (1);
1504 }
1505 
1506 /*
1507  * INNER ELEMENTS
1508  *
1509  * The local datum (arg_ref->datum) already exists, there is no need to
1510  * malloc() it.  The datum should point at a member of a structure.
1511  *
1512  * For the most part, ndr_inner() and its helpers are just a sanity
1513  * check.  The underlying ti->ndr_func() could be called immediately
1514  * for non-pointer elements.  For the sake of robustness, we detect
1515  * run-time errors here.  Most of the situations this protects against
1516  * have already been checked by the IDL compiler.  This is also a
1517  * common point for processing of all data, and so is a convenient
1518  * place to work from for debugging.
1519  */
1520 int
1521 ndr_inner(ndr_ref_t *arg_ref)
1522 {
1523 	ndr_typeinfo_t 	*ti = arg_ref->ti;
1524 	int	is_varlen = ti->pdu_size_variable_part;
1525 	int	is_union = NDR_IS_UNION(ti);
1526 	int	error = NDR_ERR_INNER_PARAMS_BAD;
1527 	int	params;
1528 
1529 	params = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1530 
1531 	switch (params) {
1532 	case NDR_F_NONE:
1533 		if (is_union) {
1534 			error = NDR_ERR_SWITCH_VALUE_MISSING;
1535 			break;
1536 		}
1537 		return (*ti->ndr_func)(arg_ref);
1538 		break;
1539 
1540 	case NDR_F_SIZE_IS:
1541 	case NDR_F_DIMENSION_IS:
1542 	case NDR_F_IS_POINTER+NDR_F_SIZE_IS:   /* pointer to something */
1543 	case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: /* pointer to something */
1544 		if (is_varlen) {
1545 			error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
1546 			break;
1547 		}
1548 		if (is_union) {
1549 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1550 			break;
1551 		}
1552 		if (params & NDR_F_IS_POINTER)
1553 			return (ndr_inner_pointer(arg_ref));
1554 		else if (params & NDR_F_IS_REFERENCE)
1555 			return (ndr_inner_reference(arg_ref));
1556 		else
1557 			return (ndr_inner_array(arg_ref));
1558 		break;
1559 
1560 	case NDR_F_IS_POINTER:	/* type is pointer to one something */
1561 		if (is_union) {
1562 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1563 			break;
1564 		}
1565 		return (ndr_inner_pointer(arg_ref));
1566 		break;
1567 
1568 	case NDR_F_IS_REFERENCE:	/* type is pointer to one something */
1569 		if (is_union) {
1570 			error = NDR_ERR_ARRAY_UNION_ILLEGAL;
1571 			break;
1572 		}
1573 		return (ndr_inner_reference(arg_ref));
1574 		break;
1575 
1576 	case NDR_F_SWITCH_IS:
1577 		if (!is_union) {
1578 			error = NDR_ERR_SWITCH_VALUE_ILLEGAL;
1579 			break;
1580 		}
1581 		return (*ti->ndr_func)(arg_ref);
1582 		break;
1583 
1584 	default:
1585 		error = NDR_ERR_INNER_PARAMS_BAD;
1586 		break;
1587 	}
1588 
1589 	/*
1590 	 * If we get here, something is wrong. Most likely,
1591 	 * the params flags do not match
1592 	 */
1593 	NDR_SET_ERROR(arg_ref, error);
1594 	return (0);
1595 }
1596 
1597 int
1598 ndr_inner_pointer(ndr_ref_t *arg_ref)
1599 {
1600 	ndr_stream_t	*nds = arg_ref->stream;
1601 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
1602 	char 		**valpp = (char **)arg_ref->datum;
1603 	ndr_ref_t 	*outer_ref;
1604 
1605 	if (!ndr__ulong(arg_ref))
1606 		return (0);	/* error */
1607 	if (!*valpp)
1608 		return (1);	/* NULL pointer */
1609 
1610 	outer_ref = ndr_enter_outer_queue(arg_ref);
1611 	if (!outer_ref)
1612 		return (0);	/* error already set */
1613 
1614 	/* move advice in inner_flags to outer_flags sans pointer */
1615 	outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1616 	outer_ref->outer_flags &= ~NDR_F_IS_POINTER;
1617 #ifdef NDR_INNER_NOT_YET
1618 	outer_ref->outer_flags |= NDR_F_BACKPTR;
1619 	if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
1620 		outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
1621 	}
1622 #endif /* NDR_INNER_NOT_YET */
1623 
1624 	outer_ref->backptr = valpp;
1625 
1626 	switch (nds->m_op) {
1627 	case NDR_M_OP_MARSHALL:
1628 		outer_ref->datum = *valpp;
1629 		break;
1630 
1631 	case NDR_M_OP_UNMARSHALL:
1632 		/*
1633 		 * This is probably wrong if the application allocated
1634 		 * memory in advance.  Indicate no value for now.
1635 		 * ONC RPC handles this case.
1636 		 */
1637 		*valpp = 0;
1638 		outer_ref->datum = 0;
1639 		break;
1640 	}
1641 
1642 	return (1);		/* pointer dereference scheduled */
1643 }
1644 
1645 int
1646 ndr_inner_reference(ndr_ref_t *arg_ref)
1647 {
1648 	ndr_stream_t	*nds = arg_ref->stream;
1649 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
1650 	char		**valpp = (char **)arg_ref->datum;
1651 	ndr_ref_t	*outer_ref;
1652 
1653 	outer_ref = ndr_enter_outer_queue(arg_ref);
1654 	if (!outer_ref)
1655 		return (0);	/* error already set */
1656 
1657 	/* move advice in inner_flags to outer_flags sans pointer */
1658 	outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
1659 	outer_ref->outer_flags &= ~NDR_F_IS_REFERENCE;
1660 #ifdef NDR_INNER_REF_NOT_YET
1661 	outer_ref->outer_flags |= NDR_F_BACKPTR;
1662 	if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
1663 		outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
1664 	}
1665 #endif /* NDR_INNER_REF_NOT_YET */
1666 
1667 	outer_ref->backptr = valpp;
1668 
1669 	switch (nds->m_op) {
1670 	case NDR_M_OP_MARSHALL:
1671 		outer_ref->datum = *valpp;
1672 		break;
1673 
1674 	case NDR_M_OP_UNMARSHALL:
1675 		/*
1676 		 * This is probably wrong if the application allocated
1677 		 * memory in advance.  Indicate no value for now.
1678 		 * ONC RPC handles this case.
1679 		 */
1680 		*valpp = 0;
1681 		outer_ref->datum = 0;
1682 		break;
1683 	}
1684 
1685 	return (1);		/* pointer dereference scheduled */
1686 }
1687 
1688 int
1689 ndr_inner_array(ndr_ref_t *encl_ref)
1690 {
1691 	ndr_typeinfo_t		*ti = encl_ref->ti;
1692 	ndr_ref_t		myref;
1693 	unsigned long		pdu_offset = encl_ref->pdu_offset;
1694 	unsigned long		n_elem;
1695 	unsigned long		i;
1696 	char			name[30];
1697 
1698 	if (encl_ref->inner_flags & NDR_F_SIZE_IS) {
1699 		/* now is the time to check/set size */
1700 		if (!ndr_size_is(encl_ref))
1701 			return (0);	/* error already set */
1702 		n_elem = encl_ref->size_is;
1703 	} else {
1704 		assert(encl_ref->inner_flags & NDR_F_DIMENSION_IS);
1705 		n_elem = encl_ref->dimension_is;
1706 	}
1707 
1708 	bzero(&myref, sizeof (myref));
1709 	myref.enclosing = encl_ref;
1710 	myref.stream = encl_ref->stream;
1711 	myref.packed_alignment = 0;
1712 	myref.ti = ti;
1713 	myref.inner_flags = NDR_F_NONE;
1714 
1715 	for (i = 0; i < n_elem; i++) {
1716 		(void) sprintf(name, "[%lu]", i);
1717 		myref.name = name;
1718 		myref.pdu_offset = pdu_offset + i * ti->pdu_size_fixed_part;
1719 		myref.datum = encl_ref->datum + i * ti->c_size_fixed_part;
1720 
1721 		if (!ndr_inner(&myref))
1722 			return (0);
1723 	}
1724 
1725 	return (1);
1726 }
1727 
1728 
1729 /*
1730  * BASIC TYPES
1731  */
1732 #define	MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
1733     extern int ndr_##TYPE(struct ndr_reference *encl_ref); \
1734     ndr_typeinfo_t ndt_##TYPE = { \
1735 	1,		/* NDR version */ \
1736 	(SIZE)-1,	/* alignment */ \
1737 	NDR_F_NONE,	/* flags */ \
1738 	ndr_##TYPE,	/* ndr_func */ \
1739 	SIZE,		/* pdu_size_fixed_part */ \
1740 	0,		/* pdu_size_variable_part */ \
1741 	SIZE,		/* c_size_fixed_part */ \
1742 	0,		/* c_size_variable_part */ \
1743 	}; \
1744     int ndr_##TYPE(struct ndr_reference *ref) { \
1745 	return (ndr_basic_integer(ref, SIZE)); \
1746 }
1747 
1748 #define	MAKE_BASIC_TYPE_STRING(TYPE, SIZE) \
1749     extern int ndr_s##TYPE(struct ndr_reference *encl_ref); \
1750     ndr_typeinfo_t ndt_s##TYPE = { \
1751 	1,		/* NDR version */ \
1752 	(SIZE)-1,	/* alignment */ \
1753 	NDR_F_STRING,	/* flags */ \
1754 	ndr_s##TYPE,	/* ndr_func */ \
1755 	0,		/* pdu_size_fixed_part */ \
1756 	SIZE,		/* pdu_size_variable_part */ \
1757 	0,		/* c_size_fixed_part */ \
1758 	SIZE,		/* c_size_variable_part */ \
1759 	}; \
1760     int ndr_s##TYPE(struct ndr_reference *ref) { \
1761 	return (ndr_string_basic_integer(ref, &ndt_##TYPE)); \
1762 }
1763 
1764 #define	MAKE_BASIC_TYPE(TYPE, SIZE) \
1765 	MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
1766 	MAKE_BASIC_TYPE_STRING(TYPE, SIZE)
1767 
1768 int ndr_basic_integer(ndr_ref_t *, unsigned);
1769 int ndr_string_basic_integer(ndr_ref_t *, ndr_typeinfo_t *);
1770 
1771 
1772 MAKE_BASIC_TYPE(_char, 1)
1773 MAKE_BASIC_TYPE(_uchar, 1)
1774 MAKE_BASIC_TYPE(_short, 2)
1775 MAKE_BASIC_TYPE(_ushort, 2)
1776 MAKE_BASIC_TYPE(_long, 4)
1777 MAKE_BASIC_TYPE(_ulong, 4)
1778 
1779 MAKE_BASIC_TYPE_BASE(_wchar, 2)
1780 
1781 int
1782 ndr_basic_integer(ndr_ref_t *ref, unsigned size)
1783 {
1784 	ndr_stream_t 	*nds = ref->stream;
1785 	char 		*valp = (char *)ref->datum;
1786 	int		rc;
1787 
1788 	switch (nds->m_op) {
1789 	case NDR_M_OP_MARSHALL:
1790 		rc = NDS_PUT_PDU(nds, ref->pdu_offset, size,
1791 		    valp, nds->swap, ref);
1792 		break;
1793 
1794 	case NDR_M_OP_UNMARSHALL:
1795 		rc = NDS_GET_PDU(nds, ref->pdu_offset, size,
1796 		    valp, nds->swap, ref);
1797 		break;
1798 
1799 	default:
1800 		NDR_SET_ERROR(ref, NDR_ERR_M_OP_INVALID);
1801 		return (0);
1802 	}
1803 
1804 	return (rc);
1805 }
1806 
1807 int
1808 ndr_string_basic_integer(ndr_ref_t *encl_ref, ndr_typeinfo_t *type_under)
1809 {
1810 	unsigned long		pdu_offset = encl_ref->pdu_offset;
1811 	unsigned		size = type_under->pdu_size_fixed_part;
1812 	char			*valp;
1813 	ndr_ref_t		myref;
1814 	unsigned long		i;
1815 	long			sense = 0;
1816 	char			name[30];
1817 
1818 	assert(size != 0);
1819 
1820 	bzero(&myref, sizeof (myref));
1821 	myref.enclosing = encl_ref;
1822 	myref.stream = encl_ref->stream;
1823 	myref.packed_alignment = 0;
1824 	myref.ti = type_under;
1825 	myref.inner_flags = NDR_F_NONE;
1826 	myref.name = name;
1827 
1828 	for (i = 0; i < NDR_STRING_MAX; i++) {
1829 		(void) sprintf(name, "[%lu]", i);
1830 		myref.pdu_offset = pdu_offset + i * size;
1831 		valp = encl_ref->datum + i * size;
1832 		myref.datum = valp;
1833 
1834 		if (!ndr_inner(&myref))
1835 			return (0);
1836 
1837 		switch (size) {
1838 		case 1:		sense = *valp; break;
1839 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1840 		case 2:		sense = *(short *)valp; break;
1841 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1842 		case 4:		sense = *(long *)valp; break;
1843 		}
1844 
1845 		if (!sense)
1846 			break;
1847 	}
1848 
1849 	return (1);
1850 }
1851 
1852 
1853 extern int ndr_s_wchar(ndr_ref_t *encl_ref);
1854 ndr_typeinfo_t ndt_s_wchar = {
1855 	1,		/* NDR version */
1856 	2-1,		/* alignment */
1857 	NDR_F_STRING,	/* flags */
1858 	ndr_s_wchar,	/* ndr_func */
1859 	0,		/* pdu_size_fixed_part */
1860 	2,		/* pdu_size_variable_part */
1861 	0,		/* c_size_fixed_part */
1862 	1,		/* c_size_variable_part */
1863 };
1864 
1865 
1866 /*
1867  * Hand coded wchar function because all strings are transported
1868  * as wide characters. During NDR_M_OP_MARSHALL, we convert from
1869  * multi-byte to wide characters. During NDR_M_OP_UNMARSHALL, we
1870  * convert from wide characters to multi-byte.
1871  *
1872  * It appeared that NT would sometimes leave a spurious character
1873  * in the data stream before the null wide_char, which would get
1874  * included in the string decode because we processed until the
1875  * null character. It now looks like NT does not always terminate
1876  * RPC Unicode strings and the terminating null is a side effect
1877  * of field alignment. So now we rely on the strlen_is (set up in
1878  * ndr_outer_string) of the enclosing reference. This may or may
1879  * not include the null but it doesn't matter, the algorithm will
1880  * get it right.
1881  */
1882 int
1883 ndr_s_wchar(ndr_ref_t *encl_ref)
1884 {
1885 	ndr_stream_t		*nds = encl_ref->stream;
1886 	unsigned short		wide_char;
1887 	char 			*valp;
1888 	ndr_ref_t		myref;
1889 	unsigned long		i;
1890 	char			name[30];
1891 	int			count;
1892 	int			char_count = 0;
1893 
1894 	if (nds->m_op == NDR_M_OP_UNMARSHALL) {
1895 		/*
1896 		 * To avoid problems with zero length strings
1897 		 * we can just null terminate here and be done.
1898 		 */
1899 		if (encl_ref->strlen_is == 0) {
1900 			encl_ref->datum[0] = '\0';
1901 			return (1);
1902 		}
1903 	}
1904 
1905 	bzero(&myref, sizeof (myref));
1906 	myref.enclosing = encl_ref;
1907 	myref.stream = encl_ref->stream;
1908 	myref.packed_alignment = 0;
1909 	myref.ti = &ndt__wchar;
1910 	myref.inner_flags = NDR_F_NONE;
1911 	myref.datum = (char *)&wide_char;
1912 	myref.name = name;
1913 	myref.pdu_offset = encl_ref->pdu_offset;
1914 
1915 	valp = encl_ref->datum;
1916 	count = 0;
1917 
1918 	for (i = 0; i < NDR_STRING_MAX; i++) {
1919 		(void) sprintf(name, "[%lu]", i);
1920 
1921 		if (nds->m_op == NDR_M_OP_MARSHALL) {
1922 			count = mts_mbtowc((mts_wchar_t *)&wide_char, valp,
1923 			    MTS_MB_CHAR_MAX);
1924 			if (count < 0) {
1925 				return (0);
1926 			} else if (count == 0) {
1927 				if (encl_ref->strlen_is != encl_ref->size_is)
1928 					break;
1929 
1930 				/*
1931 				 * If the input char is 0, mbtowc
1932 				 * returns 0 without setting wide_char.
1933 				 * Set wide_char to 0 and a count of 1.
1934 				 */
1935 				wide_char = *valp;
1936 				count = 1;
1937 			}
1938 		}
1939 
1940 		if (!ndr_inner(&myref))
1941 			return (0);
1942 
1943 		if (nds->m_op == NDR_M_OP_UNMARSHALL) {
1944 			count = mts_wctomb(valp, wide_char);
1945 
1946 			if ((++char_count) == encl_ref->strlen_is) {
1947 				valp += count;
1948 				*valp = '\0';
1949 				break;
1950 			}
1951 		}
1952 
1953 		if (!wide_char)
1954 			break;
1955 
1956 		myref.pdu_offset += sizeof (wide_char);
1957 		valp += count;
1958 	}
1959 
1960 	return (1);
1961 }
1962 
1963 /*
1964  * Converts a multibyte character string to a little-endian, wide-char
1965  * string.  No more than nwchars wide characters are stored.
1966  * A terminating null wide character is appended if there is room.
1967  *
1968  * Returns the number of wide characters converted, not counting
1969  * any terminating null wide character.  Returns -1 if an invalid
1970  * multibyte character is encountered.
1971  */
1972 size_t
1973 ndr_mbstowcs(ndr_stream_t *nds, mts_wchar_t *wcs, const char *mbs,
1974     size_t nwchars)
1975 {
1976 	mts_wchar_t *start = wcs;
1977 	int nbytes;
1978 
1979 	while (nwchars--) {
1980 		nbytes = ndr_mbtowc(nds, wcs, mbs, MTS_MB_CHAR_MAX);
1981 		if (nbytes < 0) {
1982 			*wcs = 0;
1983 			return ((size_t)-1);
1984 		}
1985 
1986 		if (*mbs == 0)
1987 			break;
1988 
1989 		++wcs;
1990 		mbs += nbytes;
1991 	}
1992 
1993 	return (wcs - start);
1994 }
1995 
1996 /*
1997  * Converts a multibyte character to a little-endian, wide-char, which
1998  * is stored in wcharp.  Up to nbytes bytes are examined.
1999  *
2000  * If mbchar is valid, returns the number of bytes processed in mbchar.
2001  * If mbchar is invalid, returns -1.  See also mts_mbtowc().
2002  */
2003 /*ARGSUSED*/
2004 int
2005 ndr_mbtowc(ndr_stream_t *nds, mts_wchar_t *wcharp, const char *mbchar,
2006     size_t nbytes)
2007 {
2008 	int rc;
2009 
2010 	if ((rc = mts_mbtowc(wcharp, mbchar, nbytes)) < 0)
2011 		return (rc);
2012 
2013 #ifdef _BIG_ENDIAN
2014 	if (nds == NULL || NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND))
2015 		*wcharp = BSWAP_16(*wcharp);
2016 #endif
2017 
2018 	return (rc);
2019 }
2020