xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_mbuf_marshaling.c (revision 17a5fa85fe0c34b1146222e40a80b42f2aae8500)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
26  */
27 
28 /*
29  * SMB mbuf marshaling encode/decode.
30  */
31 
32 #include <smbsrv/smb_kproto.h>
33 
34 
35 #define	MALLOC_QUANTUM	80
36 
37 #define	DECODE_NO_ERROR		0
38 #define	DECODE_NO_MORE_DATA	1
39 #define	DECODE_ALLOCATION_ERROR	2
40 #define	DECODE_CONVERSION_ERROR	3
41 
42 static int mbc_marshal_make_room(mbuf_chain_t *, int32_t);
43 static void mbc_marshal_store_byte(mbuf_chain_t *, uint8_t);
44 static int mbc_marshal_put_char(mbuf_chain_t *mbc, uint8_t);
45 static int mbc_marshal_put_short(mbuf_chain_t *mbc, uint16_t);
46 static int mbc_marshal_put_long(mbuf_chain_t *mbc, uint32_t);
47 static int mbc_marshal_put_long_long(mbuf_chain_t *mbc, uint64_t);
48 static int mbc_marshal_put_oem_string(mbuf_chain_t *, char *, int);
49 static int mbc_marshal_put_unicode_string(mbuf_chain_t *, char *, int);
50 static int mbc_marshal_put_uio(mbuf_chain_t *, struct uio *);
51 static int mbc_marshal_put_mbufs(mbuf_chain_t *mbc, mbuf_t *m);
52 static int mbc_marshal_put_mbuf_chain(mbuf_chain_t *mbc, mbuf_chain_t *nmbc);
53 static uint8_t mbc_marshal_fetch_byte(mbuf_chain_t *mbc);
54 static int mbc_marshal_get_char(mbuf_chain_t *mbc, uint8_t *data);
55 static int mbc_marshal_get_short(mbuf_chain_t *mbc, uint16_t *data);
56 static int mbc_marshal_get_long(mbuf_chain_t *mbc, uint32_t *data);
57 static uint64_t qswap(uint64_t ll);
58 static int mbc_marshal_get_odd_long_long(mbuf_chain_t *mbc, uint64_t *data);
59 static int mbc_marshal_get_long_long(mbuf_chain_t *mbc, uint64_t *data);
60 static int mbc_marshal_get_oem_string(smb_request_t *, mbuf_chain_t *,
61     char **, int);
62 static int mbc_marshal_get_unicode_string(smb_request_t *, mbuf_chain_t *,
63     char **, int);
64 static int mbc_marshal_get_mbufs(mbuf_chain_t *, int32_t, mbuf_t **);
65 static int mbc_marshal_get_mbuf_chain(mbuf_chain_t *, int32_t, mbuf_chain_t *);
66 static int mbc_marshal_get_uio(mbuf_chain_t *, struct uio *);
67 static int mbc_marshal_get_skip(mbuf_chain_t *, uint_t);
68 
69 /*
70  * smb_mbc_vdecodef
71  *
72  * This function reads the contents of the mbc chain passed in under the list
73  * of arguments passed in.
74  *
75  * The format string provides a description of the parameters passed in as well
76  * as an action to be taken by smb_mbc_vdecodef().
77  *
78  *	%	Pointer to an SMB request structure (smb_request_t *). There
79  *		should be only one of these in the string.
80  *
81  *	C	Pointer to an mbuf chain. Copy to that mbuf chain the number of
82  *		bytes specified (number preceding C).
83  *
84  *	m	Pointer to an mbuf. Copy to that mbuf the number of bytes
85  *		specified (number preceding m).
86  *
87  *	M	Read the 32 bit value at the current location of the mbuf chain
88  *		and check if it matches the signature of an SMB1 request (SMBx).
89  *
90  *	N	Read the 32 bit value at the current location of the mbuf chain
91  *		and check if it matches the signature of an SMB2 request (SMBx).
92  *
93  *	b	Pointer to a buffer. Copy to that buffer the number of bytes
94  *		specified (number preceding b).
95  *
96  *	c	Same as 'b'.
97  *
98  *	w	Pointer to a word (16bit value). Copy the next 16bit value into
99  *		that location.
100  *
101  *	l	Pointer to a long (32bit value). Copy the next 32bit value into
102  *		that location.
103  *
104  *	q	Pointer to a quad (64bit value). Copy the next 64bit value into
105  *		that location.
106  *
107  *	Q	Same as above with a call to qswap().
108  *
109  *	B	Pointer to a vardata_block structure. That structure is used to
110  *		retrieve data from the mbuf chain (an iovec type structure is
111  *		embedded in a vardata_block).
112  *
113  *	D	Pointer to a vardata_block structure. That structure is used to
114  *		retrieve data from the mbuf chain, however, two fields of the
115  *		vardata_block structure (tag and len) are first initialized
116  *		using the mbuf chain itself.
117  *
118  *	V	Same as 'D'.
119  *
120  *	L
121  *
122  *	A
123  *
124  *	P	Same as 'A'
125  *
126  *	S	Same as 'A'
127  *
128  *	u	Pointer to a string pointer. Allocate memory and retrieve the
129  *		string at the current location in the mbuf chain. Store the
130  *		address to the buffer allocated at the address specified by
131  *		the pointer. In addition if an sr was passed and it indicates
132  *		that the string is an unicode string, convert it.
133  *
134  *	s	Same as 'u' without convertion.
135  *
136  *	U	Same as 'u'. The string to retrieve is unicode.
137  *
138  *	y	Pointer to a 32bit value. Read the dos time at the current mbuf
139  *		chain location, convert it to unix time and store it at the
140  *		location indicated by the pointer.
141  *
142  *	Y	Same as 'y' bt the dos time coded in the mbuf chain is inverted.
143  *
144  *	.	Skip the number of bytes indicated by the number preceding '.'.
145  *
146  *	,	Same as '.' but take in account it is an unicode string.
147  */
148 int
149 smb_mbc_vdecodef(mbuf_chain_t *mbc, const char *fmt, va_list ap)
150 {
151 	uint8_t		c;
152 	uint8_t		cval;
153 	uint8_t		*cvalp;
154 	char		**charpp;
155 	uint16_t	wval;
156 	uint16_t	*wvalp;
157 	uint32_t	*lvalp;
158 	uint64_t	*llvalp;
159 	smb_vdb_t	*vdp;
160 	smb_request_t	*sr = NULL;
161 	uint32_t	lval;
162 	int		unicode = 0;
163 	int		repc;
164 	boolean_t	repc_specified;
165 
166 	while ((c = *fmt++) != 0) {
167 		repc_specified = B_FALSE;
168 		repc = 1;
169 
170 		if ('0' <= c && c <= '9') {
171 			repc = 0;
172 			do {
173 				repc = repc * 10 + c - '0';
174 				c = *fmt++;
175 			} while ('0' <= c && c <= '9');
176 			repc_specified = B_TRUE;
177 		} else if (c == '#') {
178 			repc = va_arg(ap, int);
179 			c = *fmt++;
180 			repc_specified = B_TRUE;
181 		}
182 
183 		switch (c) {
184 		case '%':
185 			sr = va_arg(ap, struct smb_request *);
186 			if (sr->session->dialect >= SMB_VERS_2_BASE) {
187 				unicode = 1;
188 				break;
189 			}
190 			unicode = sr->smb_flg2 & SMB_FLAGS2_UNICODE;
191 			break;
192 
193 		case 'C':	/* Mbuf_chain */
194 			if (mbc_marshal_get_mbuf_chain(mbc, repc,
195 			    va_arg(ap, mbuf_chain_t *)) != 0)
196 				return (-1);
197 			break;
198 
199 		case 'm':	/* struct_mbuf */
200 			if (mbc_marshal_get_mbufs(mbc, repc,
201 			    va_arg(ap, mbuf_t **)) != 0)
202 				return (-1);
203 			break;
204 
205 		case 'M':
206 			if (mbc_marshal_get_long(mbc, &lval) != 0)
207 				return (-1);
208 			if (lval != 0x424D53FF) /* 0xFF S M B */
209 				return (-1);
210 			break;
211 
212 		case 'N':
213 			if (mbc_marshal_get_long(mbc, &lval) != 0)
214 				return (-1);
215 			if (lval != 0x424D53FE) /* 0xFE S M B */
216 				return (-1);
217 			break;
218 
219 		case 'b':
220 		case 'c':
221 			cvalp = va_arg(ap, uint8_t *);
222 			if (MBC_ROOM_FOR(mbc, repc) == 0)
223 				/* Data will never be available */
224 				return (-1);
225 
226 			while (repc-- > 0)
227 				*cvalp++ = mbc_marshal_fetch_byte(mbc);
228 			break;
229 
230 		case 'w':
231 			wvalp = va_arg(ap, uint16_t *);
232 			while (repc-- > 0)
233 				if (mbc_marshal_get_short(mbc, wvalp++) != 0)
234 					return (-1);
235 			break;
236 
237 		case 'l':
238 			lvalp = va_arg(ap, uint32_t *);
239 			while (repc-- > 0)
240 				if (mbc_marshal_get_long(mbc, lvalp++) != 0)
241 					return (-1);
242 			break;
243 
244 		case 'q':
245 			llvalp = va_arg(ap, uint64_t *);
246 			while (repc-- > 0)
247 				if (mbc_marshal_get_long_long(
248 				    mbc, llvalp++) != 0)
249 					return (-1);
250 			break;
251 
252 		case 'Q':
253 			llvalp = va_arg(ap, uint64_t *);
254 			while (repc-- > 0)
255 				if (mbc_marshal_get_odd_long_long(
256 				    mbc, llvalp++) != 0)
257 					return (-1);
258 			break;
259 
260 		case 'B':
261 			vdp = va_arg(ap, struct vardata_block *);
262 			vdp->vdb_tag = 0;
263 			vdp->vdb_len = repc;
264 			vdp->vdb_uio.uio_iov = &vdp->vdb_iovec[0];
265 			vdp->vdb_uio.uio_iovcnt = MAX_IOVEC;
266 			vdp->vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
267 			vdp->vdb_uio.uio_resid = repc;
268 			if (mbc_marshal_get_uio(mbc, &vdp->vdb_uio) != 0)
269 				return (-1);
270 			break;
271 
272 		case 'D':
273 		case 'V':
274 			vdp = va_arg(ap, struct vardata_block *);
275 			if (mbc_marshal_get_char(mbc, &vdp->vdb_tag) != 0)
276 				return (-1);
277 			if (mbc_marshal_get_short(mbc, &wval) != 0)
278 				return (-1);
279 			vdp->vdb_len = (uint32_t)wval;
280 			vdp->vdb_uio.uio_iov = &vdp->vdb_iovec[0];
281 			vdp->vdb_uio.uio_iovcnt = MAX_IOVEC;
282 			vdp->vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
283 			vdp->vdb_uio.uio_resid = vdp->vdb_len;
284 			if (vdp->vdb_len != 0) {
285 				if (mbc_marshal_get_uio(mbc,
286 				    &vdp->vdb_uio) != 0)
287 					return (-1);
288 			}
289 			break;
290 
291 		case 'L':
292 			if (mbc_marshal_get_char(mbc, &cval) != 0)
293 				return (-1);
294 			if (cval != 2)
295 				return (-1);
296 			goto oem_conversion;
297 
298 		case 'A':
299 		case 'S':
300 			if (mbc_marshal_get_char(mbc, &cval) != 0)
301 				return (-1);
302 			if (((c == 'A' || c == 'S') && cval != 4) ||
303 			    (c == 'L' && cval != 2))
304 				return (-1);
305 			/* FALLTHROUGH */
306 
307 		case 'u': /* Convert from unicode if flags are set */
308 			if (unicode)
309 				goto unicode_translation;
310 			/* FALLTHROUGH */
311 
312 		case 's':	/* get OEM string */
313 oem_conversion:
314 			ASSERT(sr != NULL);
315 			charpp = va_arg(ap, char **);
316 			if (!repc_specified)
317 				repc = 0;
318 			if (mbc_marshal_get_oem_string(sr,
319 			    mbc, charpp, repc) != 0)
320 				return (-1);
321 			break;
322 
323 		case 'U':	/* get UTF-16 string */
324 unicode_translation:
325 			ASSERT(sr != 0);
326 			charpp = va_arg(ap, char **);
327 			if (!repc_specified)
328 				repc = 0;
329 			if (mbc_marshal_get_unicode_string(sr,
330 			    mbc, charpp, repc) != 0)
331 				return (-1);
332 			break;
333 
334 		case 'Y': /* dos time to unix time tt/dd */
335 			lvalp = va_arg(ap, uint32_t *);
336 			while (repc-- > 0) {
337 				short	d, t;
338 
339 				if (mbc_marshal_get_short(mbc,
340 				    (uint16_t *)&t) != 0)
341 					return (-1);
342 				if (mbc_marshal_get_short(mbc,
343 				    (uint16_t *)&d) != 0)
344 					return (-1);
345 				*lvalp++ = smb_time_dos_to_unix(d, t);
346 			}
347 			break;
348 
349 		case 'y': /* dos time to unix time dd/tt */
350 			lvalp = va_arg(ap, uint32_t *);
351 			while (repc-- > 0) {
352 				short	d, t;
353 
354 				if (mbc_marshal_get_short(mbc,
355 				    (uint16_t *)&d) != 0)
356 					return (-1);
357 				if (mbc_marshal_get_short(mbc,
358 				    (uint16_t *)&t) != 0)
359 					return (-1);
360 				*lvalp++ = smb_time_dos_to_unix(d, t);
361 			}
362 			break;
363 
364 		case ',':
365 			if (unicode)
366 				repc *= 2;
367 			/* FALLTHROUGH */
368 
369 		case '.':
370 			if (mbc_marshal_get_skip(mbc, repc) != 0)
371 				return (-1);
372 			break;
373 
374 		default:
375 			ASSERT(0);
376 			return (-1);
377 		}
378 	}
379 	return (0);
380 }
381 
382 /*
383  * smb_mbc_decodef
384  *
385  * This function reads the contents of the mbc chain passed in under the
386  * control of the format fmt.
387  *
388  * (for a description of the format string see smb_mbc_vencodef()).
389  */
390 int
391 smb_mbc_decodef(mbuf_chain_t *mbc, const char *fmt, ...)
392 {
393 	int	xx;
394 	va_list	ap;
395 
396 	va_start(ap, fmt);
397 	xx = smb_mbc_vdecodef(mbc, fmt, ap);
398 	va_end(ap);
399 	return (xx);
400 }
401 
402 /*
403  * smb_mbc_peek
404  *
405  * This function reads the contents of the mbc passed in at the specified offset
406  * under the control of the format fmt. The offset of the chain passed in is not
407  * modified.
408  *
409  * (for a description of the format string see smb_mbc_vdecodef()).
410  */
411 int
412 smb_mbc_peek(mbuf_chain_t *mbc, int offset, const char *fmt, ...)
413 {
414 	mbuf_chain_t	tmp;
415 	va_list		ap;
416 	int		xx;
417 
418 	va_start(ap, fmt);
419 
420 	(void) MBC_SHADOW_CHAIN(&tmp, mbc, offset, mbc->max_bytes - offset);
421 	xx = smb_mbc_vdecodef(&tmp, fmt, ap);
422 	va_end(ap);
423 	return (xx);
424 }
425 
426 /*
427  * smb_mbc_vencodef
428  *
429  * This function builds a stream of bytes in the mbc chain passed in under the
430  * control of the list of arguments passed in.
431  *
432  * The format string provides a description of the parameters passed in as well
433  * as an action to be taken by smb_mbc_vencodef().
434  *
435  *	\b	Restore the mbuf chain offset to its initial value.
436  *
437  *	%	Pointer to an SMB request structure (smb_request_t *). There
438  *		should be only one of these in the string. If an sr in present
439  *		it will be used to determine if unicode conversion should be
440  *		applied to the strings.
441  *
442  *	C	Pointer to an mbuf chain. Copy that mbuf chain into the
443  *		destination mbuf chain.
444  *
445  *	D	Pointer to a vardata_block structure. Copy the data described
446  *		by that structure into the mbuf chain. The tag field is hard
447  *		coded to '1'.
448  *
449  *	M	Write the SMB1 request signature ('SMBX') into the mbuf chain.
450  *
451  *	N	Write the SMB2 request signature ('SMBX') into the mbuf chain.
452  *
453  *	T	Pointer to a timestruc_t. Convert the content of the structure
454  *		into NT time and store the result of the conversion in the
455  *		mbuf chain.
456  *
457  *	V	Same as 'D' but the tag field is hard coded to '5'.
458  *
459  *	b	Byte. Store the byte or the nymber of bytes specified into the
460  *		the mbuf chain. A format string like this "2b" would require 2
461  *		bytes to be passed in.
462  *
463  *	m	Pointer to an mbuf. Copy the contents of the mbuf into the mbuf
464  *		chain.
465  *
466  *	c	Pointer to a buffer. Copy the buffer into the mbuf chain. The
467  *		size of the buffer is indicated by the number preceding 'c'.
468  *
469  *	w	Word (16bit value). Store the word or the number of words
470  *              specified into the the mbuf chain. A format string like this
471  *		"2w" would require 2 words to be passed in.
472  *
473  *	l	Long (32bit value). Store the long or the number of longs
474  *		specified into the the mbuf chain. A format string like this
475  *		"2l" would require 2 longs to be passed in.
476  *
477  *	q	Quad (64bit value). Store the quad or the number of quads
478  *		specified into the the mbuf chain. A format string like this
479  *		"2q" would require 2 quads to be passed in.
480  *
481  *	L	Pointer to a string. Store the string passed in into the mbuf
482  *		chain preceded with a tag value of '2'.
483  *
484  *	S	Pointer to a string. Store the string passed in into the mbuf
485  *		chain preceded with a tag value of '4'. Applied a unicode
486  *		conversion is appropriate.
487  *
488  *	A	Same as 'S'
489  *
490  *	P	Pointer to a string. Store the string passed in into the mbuf
491  *		chain preceded with a tag value of '5'. Applied a unicode
492  *		conversion is appropriate.
493  *
494  *	u	Pointer to a string. Store the string passed in into the mbuf
495  *		chain. Applied a unicode conversion is appropriate.
496  *
497  *	s	Pointer to a string. Store the string passed in into the mbuf
498  *		chain.
499  *
500  *	Y	Date/Time.  Store the Date/Time or the number of Date/Time(s)
501  *		specified into the the mbuf chain. A format string like this
502  *		"2Y" would require 2 Date/Time values. The Date/Time is
503  *		converted to DOS before storing.
504  *
505  *	y	Same as 'Y'. The order of Date and Time is reversed.
506  *
507  *	,	Character. Store the character or number of character specified
508  *		into the mbuf chain.  A format string like this "2c" would
509  *		require 2 characters to be passed in. A unicode conversion is
510  *		applied if appropriate.
511  *
512  *	.	Same as '`' without unicode conversion.
513  *
514  *	U	Align the offset of the mbuf chain on a 16bit boundary.
515  */
516 int
517 smb_mbc_vencodef(mbuf_chain_t *mbc, const char *fmt, va_list ap)
518 {
519 	char		*charp;
520 	uint8_t		*cvalp;
521 	timestruc_t	*tvp;
522 	smb_vdb_t	*vdp;
523 	smb_request_t	*sr = NULL;
524 	uint64_t	llval;
525 	int64_t		nt_time;
526 	uint32_t	lval;
527 	uint_t		tag;
528 	int		unicode = 0;
529 	int		repc;
530 	boolean_t	repc_specified;
531 	uint16_t	wval;
532 	uint8_t		cval;
533 	uint8_t		c;
534 
535 	while ((c = *fmt++) != 0) {
536 		repc_specified = B_FALSE;
537 		repc = 1;
538 
539 		if ('0' <= c && c <= '9') {
540 			repc = 0;
541 			do {
542 				repc = repc * 10 + c - '0';
543 				c = *fmt++;
544 			} while ('0' <= c && c <= '9');
545 			repc_specified = B_TRUE;
546 		} else if (c == '#') {
547 			repc = va_arg(ap, int);
548 			c = *fmt++;
549 			repc_specified = B_TRUE;
550 		}
551 
552 		switch (c) {
553 		case '%':
554 			sr = va_arg(ap, struct smb_request *);
555 			if (sr->session->dialect >= SMB_VERS_2_BASE) {
556 				unicode = 1;
557 				break;
558 			}
559 			unicode = sr->smb_flg2 & SMB_FLAGS2_UNICODE;
560 			break;
561 
562 		case 'C':	/* Mbuf_chain */
563 			if (mbc_marshal_put_mbuf_chain(mbc,
564 			    va_arg(ap, mbuf_chain_t *)) != 0)
565 				return (DECODE_NO_MORE_DATA);
566 			break;
567 
568 		case 'D':
569 			vdp = va_arg(ap, struct vardata_block *);
570 
571 			if (mbc_marshal_put_char(mbc, 1) != 0)
572 				return (DECODE_NO_MORE_DATA);
573 			if (mbc_marshal_put_short(mbc, vdp->vdb_len) != 0)
574 				return (DECODE_NO_MORE_DATA);
575 			if (mbc_marshal_put_uio(mbc, &vdp->vdb_uio) != 0)
576 				return (DECODE_NO_MORE_DATA);
577 			break;
578 
579 		case 'M':
580 			/* 0xFF S M B */
581 			if (mbc_marshal_put_long(mbc, 0x424D53FF))
582 				return (DECODE_NO_MORE_DATA);
583 			break;
584 
585 		case 'N':
586 			/* 0xFE S M B */
587 			if (mbc_marshal_put_long(mbc, 0x424D53FE))
588 				return (DECODE_NO_MORE_DATA);
589 			break;
590 
591 		case 'T':
592 			tvp = va_arg(ap, timestruc_t *);
593 			nt_time = smb_time_unix_to_nt(tvp);
594 			if (mbc_marshal_put_long_long(mbc, nt_time) != 0)
595 				return (DECODE_NO_MORE_DATA);
596 			break;
597 
598 		case 'V':
599 			vdp = va_arg(ap, struct vardata_block *);
600 
601 			if (mbc_marshal_put_char(mbc, 5) != 0)
602 				return (DECODE_NO_MORE_DATA);
603 			if (mbc_marshal_put_short(mbc, vdp->vdb_len) != 0)
604 				return (DECODE_NO_MORE_DATA);
605 			if (mbc_marshal_put_uio(mbc, &vdp->vdb_uio) != 0)
606 				return (DECODE_NO_MORE_DATA);
607 			break;
608 
609 		case 'b':
610 			while (repc-- > 0) {
611 				cval = va_arg(ap, int);
612 				if (mbc_marshal_put_char(mbc, cval) != 0)
613 					return (DECODE_NO_MORE_DATA);
614 			}
615 			break;
616 
617 		case 'm':	/* struct_mbuf */
618 			if (mbc_marshal_put_mbufs(mbc,
619 			    va_arg(ap, mbuf_t *)) != 0)
620 				return (DECODE_NO_MORE_DATA);
621 			break;
622 
623 		case 'c':
624 			cvalp = va_arg(ap, uint8_t *);
625 			while (repc-- > 0) {
626 				if (mbc_marshal_put_char(mbc,
627 				    *cvalp++) != 0)
628 					return (DECODE_NO_MORE_DATA);
629 			}
630 			break;
631 
632 		case 'w':
633 			while (repc-- > 0) {
634 				wval = va_arg(ap, int);
635 				if (mbc_marshal_put_short(mbc, wval) != 0)
636 					return (DECODE_NO_MORE_DATA);
637 			}
638 			break;
639 
640 		case 'l':
641 			while (repc-- > 0) {
642 				lval = va_arg(ap, uint32_t);
643 				if (mbc_marshal_put_long(mbc, lval) != 0)
644 					return (DECODE_NO_MORE_DATA);
645 			}
646 			break;
647 
648 		case 'q':
649 			while (repc-- > 0) {
650 				llval = va_arg(ap, uint64_t);
651 				if (mbc_marshal_put_long_long(mbc, llval) != 0)
652 					return (DECODE_NO_MORE_DATA);
653 			}
654 			break;
655 
656 
657 		case 'L':
658 			tag = 2;
659 			goto oem_conversion;
660 
661 		case 'S':
662 		case 'A':
663 			tag = 4;
664 			goto tagged_str;
665 
666 		case 'P':
667 			tag = 3;
668 			goto tagged_str;
669 
670 		tagged_str:
671 			if (mbc_marshal_put_char(mbc, tag) != 0)
672 				return (DECODE_NO_MORE_DATA);
673 			/* FALLTHROUGH */
674 
675 		case 'u':	/* Convert from unicode if flags are set */
676 			if (unicode)
677 				goto unicode_translation;
678 			/* FALLTHROUGH */
679 
680 		case 's':	/* put OEM string */
681 oem_conversion:
682 			charp = va_arg(ap, char *);
683 			if (!repc_specified)
684 				repc = 0;
685 			if (mbc_marshal_put_oem_string(mbc,
686 			    charp, repc) != 0)
687 				return (DECODE_NO_MORE_DATA);
688 			break;
689 
690 		case 'U':	/* put UTF-16 string */
691 unicode_translation:
692 			charp = va_arg(ap, char *);
693 			if (!repc_specified)
694 				repc = 0;
695 			if (mbc_marshal_put_unicode_string(mbc,
696 			    charp, repc) != 0)
697 				return (DECODE_NO_MORE_DATA);
698 			break;
699 
700 		case 'Y':		/* int32_t, encode dos date/time */
701 			while (repc-- > 0) {
702 				uint16_t	d, t;
703 
704 				lval = va_arg(ap, uint32_t);
705 				smb_time_unix_to_dos(lval,
706 				    (short *)&d, (short *)&t);
707 				if (mbc_marshal_put_short(mbc, t) != 0)
708 					return (DECODE_NO_MORE_DATA);
709 				if (mbc_marshal_put_short(mbc, d) != 0)
710 					return (DECODE_NO_MORE_DATA);
711 			}
712 			break;
713 
714 		case 'y':		/* int32_t, encode dos date/time */
715 			while (repc-- > 0) {
716 				uint16_t	d, t;
717 
718 				lval = va_arg(ap, uint32_t);
719 				smb_time_unix_to_dos(lval,
720 				    (short *)&d, (short *)&t);
721 				if (mbc_marshal_put_short(mbc, d) != 0)
722 					return (DECODE_NO_MORE_DATA);
723 				if (mbc_marshal_put_short(mbc, t) != 0)
724 					return (DECODE_NO_MORE_DATA);
725 			}
726 			break;
727 
728 		case ',':
729 			if (unicode)
730 				repc *= 2;
731 			/* FALLTHROUGH */
732 
733 		case '.':
734 			while (repc-- > 0)
735 				if (mbc_marshal_put_char(mbc, 0) != 0)
736 					return (DECODE_NO_MORE_DATA);
737 			break;
738 
739 		default:
740 			ASSERT(0);
741 			return (-1);
742 		}
743 	}
744 	return (0);
745 }
746 
747 /*
748  * smb_mbc_encodef
749  *
750  * This function builds a stream of bytes in the mbc chain passed in under the
751  * control of the format fmt.
752  *
753  * (for a description of the format string see smb_mbc_vencodef()).
754  */
755 int
756 smb_mbc_encodef(mbuf_chain_t *mbc, const char *fmt, ...)
757 {
758 	int	rc;
759 	va_list	ap;
760 
761 	va_start(ap, fmt);
762 	rc = smb_mbc_vencodef(mbc, fmt, ap);
763 	va_end(ap);
764 	return (rc);
765 }
766 
767 /*
768  * smb_mbc_poke
769  *
770  * This function writes a stream of bytes in the mbc passed in at the specified
771  * offset under the control of the format fmt. The offset of the chain passed in
772  * is not modified.
773  *
774  * (for a description of the format string see smb_mbc_vencodef()).
775  */
776 int
777 smb_mbc_poke(mbuf_chain_t *mbc, int offset, const char *fmt, ...)
778 {
779 	int		len, rc;
780 	mbuf_chain_t	tmp;
781 	va_list		ap;
782 
783 	if ((len = mbc->max_bytes - offset) < 0)
784 		return (DECODE_NO_MORE_DATA);
785 	rc = MBC_SHADOW_CHAIN(&tmp, mbc, offset, len);
786 	if (rc)
787 		return (DECODE_NO_MORE_DATA);
788 
789 	va_start(ap, fmt);
790 	rc = smb_mbc_vencodef(&tmp, fmt, ap);
791 	va_end(ap);
792 
793 	return (rc);
794 }
795 
796 /*
797  * Copy data from the src mbuf chain to the dst mbuf chain,
798  * at the given offset in the src and current offset in dst,
799  * for copy_len bytes.  Does NOT update src->chain_offset.
800  */
801 int
802 smb_mbc_copy(mbuf_chain_t *dst_mbc, const mbuf_chain_t *src_mbc,
803 	int copy_offset, int copy_len)
804 {
805 	mbuf_t	*src_m;
806 	int offset, len;
807 	int rc;
808 
809 	if (copy_len <= 0)
810 		return (0);
811 	if (copy_offset < 0)
812 		return (EINVAL);
813 	if ((copy_offset + copy_len) > src_mbc->max_bytes)
814 		return (EMSGSIZE);
815 
816 	/*
817 	 * Advance to the src mbuf where we start copying.
818 	 */
819 	offset = copy_offset;
820 	src_m = src_mbc->chain;
821 	while (src_m && offset >= src_m->m_len) {
822 		offset -= src_m->m_len;
823 		src_m = src_m->m_next;
824 	}
825 	if (src_m == NULL)
826 		return (EFAULT);
827 
828 	/*
829 	 * Copy the first part, which may start somewhere past
830 	 * the beginning of the current mbuf.
831 	 */
832 	len = src_m->m_len - offset;
833 	if (len > copy_len)
834 		len = copy_len;
835 	rc = smb_mbc_put_mem(dst_mbc, src_m->m_data + offset, len);
836 	if (rc != 0)
837 		return (rc);
838 	copy_len -= len;
839 
840 	/*
841 	 * Copy remaining mbufs...
842 	 */
843 	while (copy_len > 0) {
844 		src_m = src_m->m_next;
845 		if (src_m == NULL)
846 			break;
847 		len = src_m->m_len;
848 		if (len > copy_len)
849 			len = copy_len;
850 		rc = smb_mbc_put_mem(dst_mbc, src_m->m_data, len);
851 		copy_len -= len;
852 	}
853 
854 	return (0);
855 }
856 
857 /*
858  * Copy data from the passed memory buffer into the mbuf chain
859  * at the current offset.
860  */
861 int
862 smb_mbc_put_mem(mbuf_chain_t *mbc, void *vmem, int mem_len)
863 {
864 	caddr_t mem = vmem;
865 	mbuf_t	*m;
866 	int32_t	offset, tlen;
867 	int rc;
868 
869 	if (mem_len <= 0)
870 		return (0);
871 
872 	if ((rc = mbc_marshal_make_room(mbc, mem_len)) != 0)
873 		return (rc);
874 
875 	/*
876 	 * Advance to the dst mbuf where we start copying.
877 	 * Allocations were done by _make_room().
878 	 */
879 	offset = mbc->chain_offset;
880 	m = mbc->chain;
881 	while (offset >= m->m_len) {
882 		ASSERT(m->m_len > 0);
883 		offset -= m->m_len;
884 		m = m->m_next;
885 	}
886 
887 	/*
888 	 * Copy the first part, which may start somewhere past
889 	 * the beginning of the current mbuf.
890 	 */
891 	tlen = m->m_len - offset;
892 	if (tlen > mem_len)
893 		tlen = mem_len;
894 	bcopy(mem, m->m_data + offset, tlen);
895 	mbc->chain_offset += tlen;
896 	mem += tlen;
897 	mem_len -= tlen;
898 
899 	/*
900 	 * Copy remaining mem into mbufs.  These all start
901 	 * at the beginning of each mbuf, and the last may
902 	 * end somewhere short of m_len.
903 	 */
904 	while (mem_len > 0) {
905 		m = m->m_next;
906 		tlen = m->m_len;
907 		if (tlen > mem_len)
908 			tlen = mem_len;
909 		bcopy(mem, m->m_data, tlen);
910 		mbc->chain_offset += tlen;
911 		mem += tlen;
912 		mem_len -= tlen;
913 	}
914 
915 	return (0);
916 }
917 
918 /*
919  * Put data into mbuf chain allocating as needed.
920  * Adds room to end of mbuf chain if needed.
921  */
922 static int
923 mbc_marshal_make_room(mbuf_chain_t *mbc, int32_t bytes_needed)
924 {
925 	mbuf_t	*m;
926 	mbuf_t	*l;
927 	int32_t	bytes_available;
928 
929 	bytes_needed += mbc->chain_offset;
930 	if (bytes_needed > mbc->max_bytes)
931 		return (EMSGSIZE);
932 
933 	if ((m = mbc->chain) == 0) {
934 		MGET(m, M_WAIT, MT_DATA);
935 		m->m_len = 0;
936 		MCLGET(m, M_WAIT);
937 		mbc->chain = m;
938 		/* xxxx */
939 		/* ^    */
940 	}
941 
942 	/* ---- ----- --xx ---xxx */
943 	/* ^			  */
944 
945 	l = 0;
946 	while ((m != 0) && (bytes_needed >= m->m_len)) {
947 		l = m;
948 		bytes_needed -= m->m_len;
949 		m = m->m_next;
950 	}
951 
952 	if ((bytes_needed == 0) || (m != 0)) {
953 		/* We have enough room already */
954 		return (0);
955 	}
956 
957 	/* ---- ----- --xx ---xxx */
958 	/*			 ^ */
959 	/* Back up to start of last mbuf */
960 	m = l;
961 	bytes_needed += m->m_len;
962 
963 	/* ---- ----- --xx ---xxx */
964 	/*		   ^	  */
965 
966 	bytes_available = (m->m_flags & M_EXT) ?
967 	    m->m_ext.ext_size : MLEN;
968 
969 	/* ---- ----- --xx ---xxx */
970 	/*		   ^	  */
971 	while ((bytes_needed != 0) && (bytes_needed > bytes_available)) {
972 		m->m_len = bytes_available;
973 		bytes_needed -= m->m_len;
974 		/* ---- ----- --xx ------ */
975 		/*		   ^	  */
976 
977 		MGET(m->m_next, M_WAIT, MT_DATA);
978 		m = m->m_next;
979 		m->m_len = 0;
980 		MCLGET(m, M_WAIT);
981 
982 		ASSERT((m->m_flags & M_EXT) != 0);
983 		bytes_available = m->m_ext.ext_size;
984 
985 		/* ---- ----- --xx ------ xxxx */
986 		/*			  ^    */
987 	}
988 
989 	/* ---- ----- --xx ------ xxxx */
990 	/*			  ^    */
991 	/* Expand last tail as needed */
992 	if (m->m_len <= bytes_needed) {
993 		m->m_len = bytes_needed;
994 		/* ---- ----- --xx ------ --xx */
995 		/*			   ^   */
996 	}
997 
998 	return (0);
999 }
1000 
1001 static void
1002 mbc_marshal_store_byte(mbuf_chain_t *mbc, uint8_t data)
1003 {
1004 	mbuf_t	*m = mbc->chain;
1005 	int32_t	cur_offset = mbc->chain_offset;
1006 
1007 	/*
1008 	 * Scan forward looking for the last data currently in chain.
1009 	 */
1010 	while (cur_offset >= m->m_len) {
1011 		cur_offset -= m->m_len;
1012 		m = m->m_next;
1013 	}
1014 	((char *)m->m_data)[cur_offset] = data;
1015 	mbc->chain_offset++;
1016 }
1017 
1018 static int
1019 mbc_marshal_put_char(mbuf_chain_t *mbc, uint8_t data)
1020 {
1021 	if (mbc_marshal_make_room(mbc, sizeof (char)) != 0)
1022 		return (DECODE_NO_MORE_DATA);
1023 	mbc_marshal_store_byte(mbc, data);
1024 	return (0);
1025 }
1026 
1027 static int
1028 mbc_marshal_put_short(mbuf_chain_t *mbc, uint16_t data)
1029 {
1030 	if (mbc_marshal_make_room(mbc, sizeof (short)))
1031 		return (DECODE_NO_MORE_DATA);
1032 	mbc_marshal_store_byte(mbc, data);
1033 	mbc_marshal_store_byte(mbc, data >> 8);
1034 	return (0);
1035 }
1036 
1037 static int
1038 mbc_marshal_put_long(mbuf_chain_t *mbc, uint32_t data)
1039 {
1040 	if (mbc_marshal_make_room(mbc, sizeof (int32_t)))
1041 		return (DECODE_NO_MORE_DATA);
1042 	mbc_marshal_store_byte(mbc, data);
1043 	mbc_marshal_store_byte(mbc, data >> 8);
1044 	mbc_marshal_store_byte(mbc, data >> 16);
1045 	mbc_marshal_store_byte(mbc, data >> 24);
1046 	return (0);
1047 }
1048 
1049 static int
1050 mbc_marshal_put_long_long(mbuf_chain_t *mbc, uint64_t data)
1051 {
1052 	if (mbc_marshal_make_room(mbc, sizeof (int64_t)))
1053 		return (DECODE_NO_MORE_DATA);
1054 
1055 	mbc_marshal_store_byte(mbc, data);
1056 	mbc_marshal_store_byte(mbc, data >> 8);
1057 	mbc_marshal_store_byte(mbc, data >> 16);
1058 	mbc_marshal_store_byte(mbc, data >> 24);
1059 	mbc_marshal_store_byte(mbc, data >> 32);
1060 	mbc_marshal_store_byte(mbc, data >> 40);
1061 	mbc_marshal_store_byte(mbc, data >> 48);
1062 	mbc_marshal_store_byte(mbc, data >> 56);
1063 	return (0);
1064 }
1065 
1066 /*
1067  * Marshal a UTF-8 string (str) into mbc, converting to OEM codeset.
1068  * Also write a null unless the repc count limits the length we put.
1069  * When (repc > 0) the length we marshal must be exactly repc, and
1070  * truncate or pad the mbc data as necessary.
1071  * See also: msgbuf_put_oem_string
1072  */
1073 static int
1074 mbc_marshal_put_oem_string(mbuf_chain_t *mbc, char *mbs, int repc)
1075 {
1076 	uint8_t		*oembuf = NULL;
1077 	uint8_t		*s;
1078 	int		oemlen;
1079 	int		rlen;
1080 	int		rc;
1081 
1082 	/*
1083 	 * Compute length of converted OEM string,
1084 	 * NOT including null terminator
1085 	 */
1086 	if ((oemlen = smb_sbequiv_strlen(mbs)) == -1)
1087 		return (DECODE_NO_MORE_DATA);
1088 
1089 	/*
1090 	 * If repc not specified, put whole string + NULL,
1091 	 * otherwise will truncate or pad as needed.
1092 	 */
1093 	if (repc <= 0)
1094 		repc = oemlen + 1;
1095 	if (mbc_marshal_make_room(mbc, repc))
1096 		return (DECODE_NO_MORE_DATA);
1097 
1098 	/*
1099 	 * Convert into a temporary buffer
1100 	 * Free oembuf before return.
1101 	 */
1102 	oembuf = smb_mem_zalloc(oemlen + 1);
1103 	ASSERT(oembuf != NULL);
1104 	rlen = smb_mbstooem(oembuf, mbs, oemlen);
1105 	if (rlen < 0) {
1106 		rc = DECODE_NO_MORE_DATA;
1107 		goto out;
1108 	}
1109 	if (rlen > oemlen)
1110 		rlen = oemlen;
1111 	oembuf[rlen] = '\0';
1112 
1113 	/*
1114 	 * Copy the converted string into the message,
1115 	 * truncated or paded as required.
1116 	 */
1117 	s = oembuf;
1118 	while (repc > 0) {
1119 		mbc_marshal_store_byte(mbc, *s);
1120 		if (*s != '\0')
1121 			s++;
1122 		repc--;
1123 	}
1124 	rc = 0;
1125 
1126 out:
1127 	if (oembuf != NULL)
1128 		smb_mem_free(oembuf);
1129 	return (rc);
1130 }
1131 
1132 /*
1133  * Marshal a UTF-8 string (str) into mbc, converting to UTF-16.
1134  * Also write a null unless the repc count limits the length.
1135  * When (repc > 0) the length we marshal must be exactly repc,
1136  * and truncate or pad the mbc data as necessary.
1137  * See also: msgbuf_put_unicode_string
1138  */
1139 static int
1140 mbc_marshal_put_unicode_string(mbuf_chain_t *mbc, char *mbs, int repc)
1141 {
1142 	smb_wchar_t	*wcsbuf = NULL;
1143 	smb_wchar_t	*wp;
1144 	size_t		wcslen, wcsbytes;
1145 	size_t		rlen;
1146 	int		rc;
1147 
1148 	/* align to word boundary */
1149 	if (mbc->chain_offset & 1) {
1150 		if (mbc_marshal_make_room(mbc, 1))
1151 			return (DECODE_NO_MORE_DATA);
1152 		mbc_marshal_store_byte(mbc, 0);
1153 	}
1154 
1155 	/*
1156 	 * Compute length of converted UTF-16 string,
1157 	 * NOT including null terminator (in bytes).
1158 	 */
1159 	wcsbytes = smb_wcequiv_strlen(mbs);
1160 	if (wcsbytes == (size_t)-1)
1161 		return (DECODE_NO_MORE_DATA);
1162 
1163 	/*
1164 	 * If repc not specified, put whole string + NULL,
1165 	 * otherwise will truncate or pad as needed.
1166 	 */
1167 	if (repc <= 0)
1168 		repc = wcsbytes + 2;
1169 	if (mbc_marshal_make_room(mbc, repc))
1170 		return (DECODE_NO_MORE_DATA);
1171 
1172 	/*
1173 	 * Convert into a temporary buffer
1174 	 * Free wcsbuf before return.
1175 	 */
1176 	wcslen = wcsbytes / 2;
1177 	wcsbuf = smb_mem_zalloc(wcsbytes + 2);
1178 	ASSERT(wcsbuf != NULL);
1179 	rlen = smb_mbstowcs(wcsbuf, mbs, wcslen);
1180 	if (rlen == (size_t)-1) {
1181 		rc = DECODE_NO_MORE_DATA;
1182 		goto out;
1183 	}
1184 	if (rlen > wcslen)
1185 		rlen = wcslen;
1186 	wcsbuf[rlen] = 0;
1187 
1188 	/*
1189 	 * Copy the converted string into the message,
1190 	 * truncated or paded as required.  Preserve
1191 	 * little-endian order while copying.
1192 	 */
1193 	wp = wcsbuf;
1194 	while (repc > 1) {
1195 		smb_wchar_t wchar = LE_IN16(wp);
1196 		mbc_marshal_store_byte(mbc, wchar);
1197 		mbc_marshal_store_byte(mbc, wchar >> 8);
1198 		if (wchar != 0)
1199 			wp++;
1200 		repc -= sizeof (smb_wchar_t);
1201 	}
1202 	if (repc > 0)
1203 		mbc_marshal_store_byte(mbc, 0);
1204 
1205 	rc = 0;
1206 out:
1207 	if (wcsbuf != NULL)
1208 		smb_mem_free(wcsbuf);
1209 	return (rc);
1210 }
1211 
1212 static int /*ARGSUSED*/
1213 uiorefnoop(caddr_t p, int size, int adj)
1214 {
1215 	return (0);
1216 }
1217 
1218 static int
1219 mbc_marshal_put_uio(mbuf_chain_t *mbc, struct uio *uio)
1220 {
1221 	mbuf_t		**t;
1222 	mbuf_t		*m = NULL;
1223 	struct iovec	*iov = uio->uio_iov;
1224 	int32_t		i, iov_cnt = uio->uio_iovcnt;
1225 
1226 	iov = uio->uio_iov;
1227 	t = &mbc->chain;
1228 	for (i = 0; i < iov_cnt; i++) {
1229 		MGET(m, M_WAIT, MT_DATA);
1230 		m->m_ext.ext_buf = iov->iov_base;
1231 		m->m_ext.ext_ref = uiorefnoop;
1232 		m->m_data = m->m_ext.ext_buf;
1233 		m->m_flags |= M_EXT;
1234 		m->m_len = m->m_ext.ext_size = iov->iov_len;
1235 		mbc->max_bytes += m->m_len;
1236 		m->m_next = 0;
1237 		*t = m;
1238 		t = &m->m_next;
1239 		iov++;
1240 	}
1241 	return (0);
1242 }
1243 
1244 static int
1245 mbc_marshal_put_mbufs(mbuf_chain_t *mbc, mbuf_t *m)
1246 {
1247 	mbuf_t	*mt;
1248 	mbuf_t	**t;
1249 	int	bytes;
1250 
1251 	if (m != NULL) {
1252 		mt = m;
1253 		bytes = mt->m_len;
1254 		while (mt->m_next != 0) {
1255 			mt = mt->m_next;
1256 			bytes += mt->m_len;
1257 		}
1258 		if (bytes != 0) {
1259 			t = &mbc->chain;
1260 			while (*t != 0) {
1261 				bytes += (*t)->m_len;
1262 				t = &(*t)->m_next;
1263 			}
1264 			*t = m;
1265 			mbc->chain_offset = bytes;
1266 		} else {
1267 			m_freem(m);
1268 		}
1269 	}
1270 	return (0);
1271 }
1272 
1273 static int
1274 mbc_marshal_put_mbuf_chain(mbuf_chain_t *mbc, mbuf_chain_t *nmbc)
1275 {
1276 	if (nmbc->chain != 0) {
1277 		if (mbc_marshal_put_mbufs(mbc, nmbc->chain))
1278 			return (DECODE_NO_MORE_DATA);
1279 		MBC_SETUP(nmbc, nmbc->max_bytes);
1280 	}
1281 	return (0);
1282 }
1283 
1284 static uint8_t
1285 mbc_marshal_fetch_byte(mbuf_chain_t *mbc)
1286 {
1287 	uint8_t	data;
1288 	mbuf_t	*m = mbc->chain;
1289 	int32_t	offset = mbc->chain_offset;
1290 
1291 	while (offset >= m->m_len) {
1292 		offset -= m->m_len;
1293 		m = m->m_next;
1294 	}
1295 	data = ((uint8_t *)m->m_data)[offset];
1296 	mbc->chain_offset++;
1297 	return (data);
1298 }
1299 
1300 static int
1301 mbc_marshal_get_char(mbuf_chain_t *mbc, uint8_t *data)
1302 {
1303 	if (MBC_ROOM_FOR(mbc, sizeof (char)) == 0) {
1304 		/* Data will never be available */
1305 		return (DECODE_NO_MORE_DATA);
1306 	}
1307 	*data = mbc_marshal_fetch_byte(mbc);
1308 	return (0);
1309 }
1310 
1311 static int
1312 mbc_marshal_get_short(mbuf_chain_t *mbc, uint16_t *data)
1313 {
1314 	uint16_t	tmp;
1315 	mbuf_t		*m = mbc->chain;
1316 	int32_t		offset = mbc->chain_offset;
1317 
1318 	if (MBC_ROOM_FOR(mbc, sizeof (short)) == 0) {
1319 		/* Data will never be available */
1320 		return (DECODE_NO_MORE_DATA);
1321 	}
1322 
1323 	while (offset >= m->m_len) {
1324 		offset -= m->m_len;
1325 		m = m->m_next;
1326 	}
1327 	if ((m->m_len - offset) >= sizeof (short)) {
1328 		*data = LE_IN16(m->m_data + offset);
1329 		mbc->chain_offset += sizeof (short);
1330 	} else {
1331 		tmp = (uint16_t)mbc_marshal_fetch_byte(mbc);
1332 		tmp |= ((uint16_t)mbc_marshal_fetch_byte(mbc)) << 8;
1333 		*data = tmp;
1334 	}
1335 	return (0);
1336 }
1337 
1338 static int
1339 mbc_marshal_get_long(mbuf_chain_t *mbc, uint32_t *data)
1340 {
1341 	uint32_t	tmp;
1342 	mbuf_t		*m = mbc->chain;
1343 	int32_t		offset = mbc->chain_offset;
1344 
1345 	if (MBC_ROOM_FOR(mbc, sizeof (int32_t)) == 0) {
1346 		/* Data will never be available */
1347 		return (DECODE_NO_MORE_DATA);
1348 	}
1349 	while (offset >= m->m_len) {
1350 		offset -= m->m_len;
1351 		m = m->m_next;
1352 	}
1353 	if ((m->m_len - offset) >= sizeof (int32_t)) {
1354 		*data = LE_IN32(m->m_data + offset);
1355 		mbc->chain_offset += sizeof (int32_t);
1356 	} else {
1357 		tmp = (uint32_t)mbc_marshal_fetch_byte(mbc);
1358 		tmp |= ((uint32_t)mbc_marshal_fetch_byte(mbc)) << 8;
1359 		tmp |= ((uint32_t)mbc_marshal_fetch_byte(mbc)) << 16;
1360 		tmp |= ((uint32_t)mbc_marshal_fetch_byte(mbc)) << 24;
1361 		*data = tmp;
1362 	}
1363 	return (0);
1364 }
1365 
1366 static uint64_t
1367 qswap(uint64_t ll)
1368 {
1369 	uint64_t v;
1370 
1371 	v = ll >> 32;
1372 	v |= ll << 32;
1373 
1374 	return (v);
1375 }
1376 
1377 static int
1378 mbc_marshal_get_odd_long_long(mbuf_chain_t *mbc, uint64_t *data)
1379 {
1380 	uint64_t	tmp;
1381 	mbuf_t		*m = mbc->chain;
1382 	int32_t		offset = mbc->chain_offset;
1383 
1384 	if (MBC_ROOM_FOR(mbc, sizeof (int64_t)) == 0) {
1385 		/* Data will never be available */
1386 		return (DECODE_NO_MORE_DATA);
1387 	}
1388 	while (offset >= m->m_len) {
1389 		offset -= m->m_len;
1390 		m = m->m_next;
1391 	}
1392 
1393 	if ((m->m_len - offset) >= sizeof (int64_t)) {
1394 		*data = qswap(LE_IN64(m->m_data + offset));
1395 		mbc->chain_offset += sizeof (int64_t);
1396 	} else {
1397 		tmp = (uint64_t)mbc_marshal_fetch_byte(mbc) << 32;
1398 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 40;
1399 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 48;
1400 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 56;
1401 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc);
1402 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 8;
1403 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 16;
1404 		tmp |= (uint64_t)mbc_marshal_fetch_byte(mbc) << 24;
1405 
1406 		*(uint64_t *)data = tmp;
1407 	}
1408 	return (0);
1409 }
1410 
1411 static int
1412 mbc_marshal_get_long_long(mbuf_chain_t *mbc, uint64_t *data)
1413 {
1414 	uint64_t	tmp;
1415 	mbuf_t		*m = mbc->chain;
1416 	int32_t		offset = mbc->chain_offset;
1417 
1418 	if (MBC_ROOM_FOR(mbc, sizeof (int64_t)) == 0) {
1419 		/* Data will never be available */
1420 		return (DECODE_NO_MORE_DATA);
1421 	}
1422 	while (offset >= m->m_len) {
1423 		offset -= m->m_len;
1424 		m = m->m_next;
1425 	}
1426 	if ((m->m_len - offset) >= sizeof (int64_t)) {
1427 		*data = LE_IN64(m->m_data + offset);
1428 		mbc->chain_offset += sizeof (int64_t);
1429 	} else {
1430 		tmp = (uint32_t)mbc_marshal_fetch_byte(mbc);
1431 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 8;
1432 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 16;
1433 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 24;
1434 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 32;
1435 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 40;
1436 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 48;
1437 		tmp |= ((uint64_t)mbc_marshal_fetch_byte(mbc)) << 56;
1438 		*(uint64_t *)data = tmp;
1439 	}
1440 	return (0);
1441 }
1442 
1443 /*
1444  * mbc_marshal_get_oem_string
1445  *
1446  * Decode an OEM string, returning its UTF-8 form in strpp,
1447  * allocated using smb_srm_zalloc (automatically freed).
1448  * If max_bytes != 0, consume at most max_bytes of the mbc.
1449  * See also: msgbuf_get_oem_string
1450  */
1451 static int
1452 mbc_marshal_get_oem_string(smb_request_t *sr,
1453     mbuf_chain_t *mbc, char **strpp, int max_bytes)
1454 {
1455 	char		*mbs;
1456 	uint8_t		*oembuf = NULL;
1457 	int		oemlen, oemmax;
1458 	int		mbsmax;
1459 	int		rlen;
1460 	int		rc;
1461 
1462 	if (max_bytes == 0)
1463 		max_bytes = 0xffff;
1464 
1465 	/*
1466 	 * Get the OtW data into a temporary buffer.
1467 	 * Free oembuf before return.
1468 	 */
1469 	oemlen = 0;
1470 	oemmax = MALLOC_QUANTUM;
1471 	oembuf = smb_mem_alloc(oemmax);
1472 	for (;;) {
1473 		uint8_t ch;
1474 
1475 		if (oemlen >= max_bytes)
1476 			break;
1477 		if ((oemlen + 2) >= oemmax) {
1478 			oemmax += MALLOC_QUANTUM;
1479 			oembuf = smb_mem_realloc(oembuf, oemmax);
1480 		}
1481 		if (mbc_marshal_get_char(mbc, &ch) != 0) {
1482 			rc = DECODE_NO_MORE_DATA;
1483 			goto out;
1484 		}
1485 		if (ch == 0)
1486 			break;
1487 		oembuf[oemlen++] = ch;
1488 	}
1489 	oembuf[oemlen] = '\0';
1490 
1491 	/*
1492 	 * Get the buffer we'll return and convert to UTF-8.
1493 	 * May take as much as double the space.
1494 	 */
1495 	mbsmax = oemlen * 2;
1496 	mbs = smb_srm_zalloc(sr, mbsmax + 1);
1497 	ASSERT(mbs != NULL);
1498 	rlen = smb_oemtombs(mbs, oembuf, mbsmax);
1499 	if (rlen < 0) {
1500 		rc = DECODE_NO_MORE_DATA;
1501 		goto out;
1502 	}
1503 	if (rlen > mbsmax)
1504 		rlen = mbsmax;
1505 	mbs[rlen] = '\0';
1506 	*strpp = mbs;
1507 	rc = 0;
1508 
1509 out:
1510 	if (oembuf != NULL)
1511 		smb_mem_free(oembuf);
1512 	return (rc);
1513 }
1514 
1515 /*
1516  * mbc_marshal_get_unicode_string
1517  *
1518  * Decode a UTF-16 string, returning its UTF-8 form in strpp,
1519  * allocated using smb_srm_zalloc (automatically freed).
1520  * If max_bytes != 0, consume at most max_bytes of the mbc.
1521  * See also: msgbuf_get_unicode_string
1522  */
1523 static int
1524 mbc_marshal_get_unicode_string(smb_request_t *sr,
1525     mbuf_chain_t *mbc, char **strpp, int max_bytes)
1526 {
1527 	char		*mbs;
1528 	uint16_t	*wcsbuf = NULL;
1529 	int		wcslen;		// wchar count
1530 	int		wcsmax;		// byte count
1531 	size_t		mbsmax;
1532 	size_t		rlen;
1533 	int		rc;
1534 
1535 	if (max_bytes == 0)
1536 		max_bytes = 0xffff;
1537 
1538 	/*
1539 	 * Unicode strings are always word aligned.
1540 	 */
1541 	if (mbc->chain_offset & 1) {
1542 		if (MBC_ROOM_FOR(mbc, sizeof (char)) == 0)
1543 			return (DECODE_NO_MORE_DATA);
1544 		mbc->chain_offset++;
1545 	}
1546 
1547 	/*
1548 	 * Get the OtW data into a temporary buffer.
1549 	 * Free wcsbuf before return.
1550 	 */
1551 	wcslen = 0;
1552 	wcsmax = MALLOC_QUANTUM;
1553 	wcsbuf = smb_mem_alloc(wcsmax);
1554 	for (;;) {
1555 		uint16_t	wchar;
1556 
1557 		if ((wcslen * 2) >= max_bytes)
1558 			break;
1559 		if (((wcslen * 2) + 4) >= wcsmax) {
1560 			wcsmax += MALLOC_QUANTUM;
1561 			wcsbuf = smb_mem_realloc(wcsbuf, wcsmax);
1562 		}
1563 		if (mbc_marshal_get_short(mbc, &wchar) != 0) {
1564 			rc = DECODE_NO_MORE_DATA;
1565 			goto out;
1566 		}
1567 		if (wchar == 0)
1568 			break;
1569 		/* Keep in little-endian form. */
1570 		LE_OUT16(wcsbuf + wcslen, wchar);
1571 		wcslen++;
1572 	}
1573 	wcsbuf[wcslen] = 0;
1574 
1575 	/*
1576 	 * Get the buffer we'll return and convert to UTF-8.
1577 	 * May take as much 4X number of wide chars.
1578 	 */
1579 	mbsmax = wcslen * MTS_MB_CUR_MAX;
1580 	mbs = smb_srm_zalloc(sr, mbsmax + 1);
1581 	ASSERT(mbs != NULL);
1582 	rlen = smb_wcstombs(mbs, wcsbuf, mbsmax);
1583 	if (rlen == (size_t)-1) {
1584 		rc = DECODE_NO_MORE_DATA;
1585 		goto out;
1586 	}
1587 	if (rlen > mbsmax)
1588 		rlen = mbsmax;
1589 	mbs[rlen] = '\0';
1590 	*strpp = mbs;
1591 	rc = 0;
1592 
1593 out:
1594 	if (wcsbuf != NULL)
1595 		smb_mem_free(wcsbuf);
1596 	return (rc);
1597 }
1598 
1599 static int /*ARGSUSED*/
1600 mbc_marshal_get_mbufs(mbuf_chain_t *mbc, int32_t bytes, mbuf_t **m)
1601 {
1602 	*m = NULL;
1603 	if (MBC_ROOM_FOR(mbc, bytes) == 0) {
1604 		/* Data will never be available */
1605 		return (DECODE_NO_MORE_DATA);
1606 	}
1607 	/* not yet implemented */
1608 	return (-1);
1609 }
1610 
1611 static int
1612 mbc_marshal_get_mbuf_chain(mbuf_chain_t *mbc, int32_t bytes, mbuf_chain_t *nmbc)
1613 {
1614 	int	rc;
1615 	mbuf_t	*m;
1616 
1617 	if (bytes == 0) {
1618 		/* Get all the rest */
1619 		bytes = mbc->max_bytes - mbc->chain_offset;
1620 	}
1621 
1622 	MBC_SETUP(nmbc, mbc->max_bytes);
1623 	if ((rc = mbc_marshal_get_mbufs(mbc, bytes, &m)) != 0) {
1624 		if (m)
1625 			m_freem(m);
1626 		return (rc);
1627 	}
1628 	nmbc->chain = m;
1629 	while (m != 0) {
1630 		bytes += m->m_len;
1631 		m = m->m_next;
1632 	}
1633 	nmbc->max_bytes = bytes;
1634 	return (0);
1635 }
1636 
1637 static int
1638 mbc_marshal_get_uio(mbuf_chain_t *mbc, struct uio *uio)
1639 {
1640 	int		i, offset;
1641 	int32_t		bytes = uio->uio_resid;
1642 	int32_t		remainder;
1643 	struct iovec	*iov;
1644 	mbuf_t		*m;
1645 
1646 	/*
1647 	 * The residual count is tested because in the case of write requests
1648 	 * with no data (smbtorture RAW-WRITE test will generate that type of
1649 	 * request) this function is called with a residual count of zero
1650 	 * bytes.
1651 	 */
1652 	if (bytes != 0) {
1653 		iov = uio->uio_iov;
1654 		uio->uio_segflg = UIO_SYSSPACE;
1655 		uio->uio_extflg = UIO_COPY_DEFAULT;
1656 
1657 		if (MBC_ROOM_FOR(mbc, bytes) == 0) {
1658 			/* Data will never be available */
1659 			return (DECODE_NO_MORE_DATA);
1660 		}
1661 
1662 		m = mbc->chain;
1663 		offset = mbc->chain_offset;
1664 		while (offset >= m->m_len) {
1665 			offset -= m->m_len;
1666 			m = m->m_next;
1667 			ASSERT((offset == 0) || (offset && m));
1668 		}
1669 
1670 		for (i = 0; (bytes > 0) && (i < uio->uio_iovcnt); i++) {
1671 			iov[i].iov_base = &m->m_data[offset];
1672 			remainder = m->m_len - offset;
1673 			if (remainder >= bytes) {
1674 				iov[i].iov_len = bytes;
1675 				mbc->chain_offset += bytes;
1676 				uio->uio_iovcnt = i + 1;
1677 				return (0);
1678 			}
1679 			iov[i].iov_len = remainder;
1680 			mbc->chain_offset += remainder;
1681 			bytes -= remainder;
1682 			m = m->m_next;
1683 			offset = 0;
1684 		}
1685 		return (DECODE_NO_MORE_DATA);
1686 	}
1687 	return (0);
1688 }
1689 
1690 static int
1691 mbc_marshal_get_skip(mbuf_chain_t *mbc, uint_t skip)
1692 {
1693 	if (MBC_ROOM_FOR(mbc, skip) == 0)
1694 		return (DECODE_NO_MORE_DATA);
1695 	mbc->chain_offset += skip;
1696 	return (0);
1697 }
1698