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