8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / smbsrv / smb_msgbuf.c
blob54cb75e066a053e3dd489c0caa94e2e7b2b61dab
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
29 * Msgbuf buffer management implementation. The smb_msgbuf interface is
30 * typically used to encode or decode SMB data using sprintf/scanf
31 * style operations. It contains special handling for the SMB header.
32 * It can also be used for general purpose encoding and decoding.
35 #include <sys/types.h>
36 #include <sys/varargs.h>
37 #include <sys/byteorder.h>
38 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
39 #include <stdlib.h>
40 #include <syslog.h>
41 #include <string.h>
42 #include <strings.h>
43 #else
44 #include <sys/sunddi.h>
45 #include <sys/kmem.h>
46 #endif
47 #include <smbsrv/string.h>
48 #include <smbsrv/msgbuf.h>
49 #include <smbsrv/smb.h>
51 static int buf_decode(smb_msgbuf_t *, char *, va_list ap);
52 static int buf_encode(smb_msgbuf_t *, char *, va_list ap);
53 static void *smb_msgbuf_malloc(smb_msgbuf_t *, size_t);
54 static int smb_msgbuf_chkerc(char *text, int erc);
57 * Returns the offset or number of bytes used within the buffer.
59 size_t
60 smb_msgbuf_used(smb_msgbuf_t *mb)
62 /*LINTED E_PTRDIFF_OVERFLOW*/
63 return (mb->scan - mb->base);
67 * Returns the actual buffer size.
69 size_t
70 smb_msgbuf_size(smb_msgbuf_t *mb)
72 return (mb->max);
75 uint8_t *
76 smb_msgbuf_base(smb_msgbuf_t *mb)
78 return (mb->base);
82 * Ensure that the scan is aligned on a word (16-bit) boundary.
84 void
85 smb_msgbuf_word_align(smb_msgbuf_t *mb)
87 mb->scan = (uint8_t *)((uintptr_t)(mb->scan + 1) & ~1);
91 * Ensure that the scan is aligned on a dword (32-bit) boundary.
93 void
94 smb_msgbuf_dword_align(smb_msgbuf_t *mb)
96 mb->scan = (uint8_t *)((uintptr_t)(mb->scan + 3) & ~3);
100 * Checks whether or not the buffer has space for the amount of data
101 * specified. Returns 1 if there is space, otherwise returns 0.
104 smb_msgbuf_has_space(smb_msgbuf_t *mb, size_t size)
106 if (size > mb->max || (mb->scan + size) > mb->end)
107 return (0);
109 return (1);
113 * Set flags the smb_msgbuf.
115 void
116 smb_msgbuf_fset(smb_msgbuf_t *mb, uint32_t flags)
118 mb->flags |= flags;
122 * Clear flags the smb_msgbuf.
124 void
125 smb_msgbuf_fclear(smb_msgbuf_t *mb, uint32_t flags)
127 mb->flags &= ~flags;
131 * smb_msgbuf_init
133 * Initialize a smb_msgbuf_t structure based on the buffer and size
134 * specified. Both scan and base initially point to the beginning
135 * of the buffer and end points to the limit of the buffer. As
136 * data is added scan should be incremented to point to the next
137 * offset at which data will be written. Max and count are set
138 * to the actual buffer size.
140 void
141 smb_msgbuf_init(smb_msgbuf_t *mb, uint8_t *buf, size_t size, uint32_t flags)
143 mb->scan = mb->base = buf;
144 mb->max = mb->count = size;
145 mb->end = &buf[size];
146 mb->flags = flags;
147 mb->mlist.next = 0;
152 * smb_msgbuf_term
154 * Destruct a smb_msgbuf_t. Free any memory hanging off the mlist.
156 void
157 smb_msgbuf_term(smb_msgbuf_t *mb)
159 smb_msgbuf_mlist_t *item = mb->mlist.next;
160 smb_msgbuf_mlist_t *tmp;
162 while (item) {
163 tmp = item;
164 item = item->next;
165 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
166 free(tmp);
167 #else
168 kmem_free(tmp, tmp->size);
169 #endif
175 * smb_msgbuf_decode
177 * Decode a smb_msgbuf buffer as indicated by the format string into
178 * the variable arg list. This is similar to a scanf operation.
180 * On success, returns the number of bytes encoded. Otherwise
181 * returns a -ve error code.
184 smb_msgbuf_decode(smb_msgbuf_t *mb, char *fmt, ...)
186 int rc;
187 uint8_t *orig_scan;
188 va_list ap;
190 va_start(ap, fmt);
191 orig_scan = mb->scan;
192 rc = buf_decode(mb, fmt, ap);
193 va_end(ap);
195 if (rc != SMB_MSGBUF_SUCCESS) {
196 (void) smb_msgbuf_chkerc("smb_msgbuf_decode", rc);
197 mb->scan = orig_scan;
198 return (rc);
201 /*LINTED E_PTRDIFF_OVERFLOW*/
202 return (mb->scan - orig_scan);
207 * buf_decode
209 * Private decode function, where the real work of decoding the smb_msgbuf
210 * is done. This function should only be called via smb_msgbuf_decode to
211 * ensure correct behaviour and error handling.
213 static int
214 buf_decode(smb_msgbuf_t *mb, char *fmt, va_list ap)
216 uint32_t ival;
217 uint8_t c;
218 uint8_t *bvalp;
219 uint16_t *wvalp;
220 uint32_t *lvalp;
221 uint64_t *llvalp;
222 char *cvalp;
223 char **cvalpp;
224 smb_wchar_t wchar;
225 boolean_t repc_specified;
226 int repc;
227 int rc;
229 while ((c = *fmt++) != 0) {
230 repc_specified = B_FALSE;
231 repc = 1;
233 if (c == ' ' || c == '\t')
234 continue;
236 if (c == '(') {
237 while (((c = *fmt++) != 0) && c != ')')
240 if (!c)
241 return (SMB_MSGBUF_SUCCESS);
243 continue;
246 if ('0' <= c && c <= '9') {
247 repc = 0;
248 do {
249 repc = repc * 10 + c - '0';
250 c = *fmt++;
251 } while ('0' <= c && c <= '9');
252 repc_specified = B_TRUE;
253 } else if (c == '#') {
254 repc = va_arg(ap, int);
255 c = *fmt++;
256 repc_specified = B_TRUE;
259 switch (c) {
260 case '.':
261 if (smb_msgbuf_has_space(mb, repc) == 0)
262 return (SMB_MSGBUF_UNDERFLOW);
264 mb->scan += repc;
265 break;
267 case 'c': /* get char */
268 if (smb_msgbuf_has_space(mb, repc) == 0)
269 return (SMB_MSGBUF_UNDERFLOW);
271 bvalp = va_arg(ap, uint8_t *);
272 bcopy(mb->scan, bvalp, repc);
273 mb->scan += repc;
274 break;
276 case 'b': /* get byte */
277 if (smb_msgbuf_has_space(mb, repc) == 0)
278 return (SMB_MSGBUF_UNDERFLOW);
280 bvalp = va_arg(ap, uint8_t *);
281 while (repc-- > 0) {
282 *bvalp++ = *mb->scan++;
284 break;
286 case 'w': /* get word */
287 rc = smb_msgbuf_has_space(mb, repc * sizeof (uint16_t));
288 if (rc == 0)
289 return (SMB_MSGBUF_UNDERFLOW);
291 wvalp = va_arg(ap, uint16_t *);
292 while (repc-- > 0) {
293 *wvalp++ = LE_IN16(mb->scan);
294 mb->scan += sizeof (uint16_t);
296 break;
298 case 'l': /* get long */
299 rc = smb_msgbuf_has_space(mb, repc * sizeof (int32_t));
300 if (rc == 0)
301 return (SMB_MSGBUF_UNDERFLOW);
303 lvalp = va_arg(ap, uint32_t *);
304 while (repc-- > 0) {
305 *lvalp++ = LE_IN32(mb->scan);
306 mb->scan += sizeof (int32_t);
308 break;
310 case 'q': /* get quad */
311 rc = smb_msgbuf_has_space(mb, repc * sizeof (int64_t));
312 if (rc == 0)
313 return (SMB_MSGBUF_UNDERFLOW);
315 llvalp = va_arg(ap, uint64_t *);
316 while (repc-- > 0) {
317 *llvalp++ = LE_IN64(mb->scan);
318 mb->scan += sizeof (int64_t);
320 break;
322 case 'u': /* Convert from unicode if flags are set */
323 if (mb->flags & SMB_MSGBUF_UNICODE)
324 goto unicode_translation;
325 /*FALLTHROUGH*/
327 case 's': /* get string */
328 if (!repc_specified)
329 repc = strlen((const char *)mb->scan) + 1;
330 if (smb_msgbuf_has_space(mb, repc) == 0)
331 return (SMB_MSGBUF_UNDERFLOW);
332 if ((cvalp = smb_msgbuf_malloc(mb, repc * 2)) == 0)
333 return (SMB_MSGBUF_UNDERFLOW);
334 cvalpp = va_arg(ap, char **);
335 *cvalpp = cvalp;
336 /* Translate OEM to mbs */
337 while (repc > 0) {
338 wchar = *mb->scan++;
339 repc--;
340 if (wchar == 0)
341 break;
342 ival = smb_wctomb(cvalp, wchar);
343 cvalp += ival;
345 *cvalp = '\0';
346 if (repc > 0)
347 mb->scan += repc;
348 break;
350 case 'U': /* get unicode string */
351 unicode_translation:
353 * Unicode strings are always word aligned.
354 * The malloc'd area is larger than the
355 * original string because the UTF-8 chars
356 * may be longer than the wide-chars.
358 smb_msgbuf_word_align(mb);
359 if (!repc_specified) {
361 * Count bytes, including the null.
363 uint8_t *tmp_scan = mb->scan;
364 repc = 2; /* the null */
365 while ((wchar = LE_IN16(tmp_scan)) != 0) {
366 tmp_scan += 2;
367 repc += 2;
370 if (smb_msgbuf_has_space(mb, repc) == 0)
371 return (SMB_MSGBUF_UNDERFLOW);
373 * Get space for translated string
374 * Allocates worst-case size.
376 if ((cvalp = smb_msgbuf_malloc(mb, repc * 2)) == 0)
377 return (SMB_MSGBUF_UNDERFLOW);
378 cvalpp = va_arg(ap, char **);
379 *cvalpp = cvalp;
381 * Translate unicode to mbs, stopping after
382 * null or repc limit.
384 while (repc >= 2) {
385 wchar = LE_IN16(mb->scan);
386 mb->scan += 2;
387 repc -= 2;
388 if (wchar == 0)
389 break;
390 ival = smb_wctomb(cvalp, wchar);
391 cvalp += ival;
393 *cvalp = '\0';
394 if (repc > 0)
395 mb->scan += repc;
396 break;
398 case 'M':
399 if (smb_msgbuf_has_space(mb, 4) == 0)
400 return (SMB_MSGBUF_UNDERFLOW);
402 if (mb->scan[0] != 0xFF ||
403 mb->scan[1] != 'S' ||
404 mb->scan[2] != 'M' ||
405 mb->scan[3] != 'B') {
406 return (SMB_MSGBUF_INVALID_HEADER);
408 mb->scan += 4;
409 break;
411 default:
412 return (SMB_MSGBUF_INVALID_FORMAT);
416 return (SMB_MSGBUF_SUCCESS);
421 * smb_msgbuf_encode
423 * Encode a smb_msgbuf buffer as indicated by the format string using
424 * the variable arg list. This is similar to a sprintf operation.
426 * On success, returns the number of bytes encoded. Otherwise
427 * returns a -ve error code.
430 smb_msgbuf_encode(smb_msgbuf_t *mb, char *fmt, ...)
432 int rc;
433 uint8_t *orig_scan;
434 va_list ap;
436 va_start(ap, fmt);
437 orig_scan = mb->scan;
438 rc = buf_encode(mb, fmt, ap);
439 va_end(ap);
441 if (rc != SMB_MSGBUF_SUCCESS) {
442 (void) smb_msgbuf_chkerc("smb_msgbuf_encode", rc);
443 mb->scan = orig_scan;
444 return (rc);
447 /*LINTED E_PTRDIFF_OVERFLOW*/
448 return (mb->scan - orig_scan);
453 * buf_encode
455 * Private encode function, where the real work of encoding the smb_msgbuf
456 * is done. This function should only be called via smb_msgbuf_encode to
457 * ensure correct behaviour and error handling.
459 static int
460 buf_encode(smb_msgbuf_t *mb, char *fmt, va_list ap)
462 uint8_t cval;
463 uint16_t wval;
464 uint32_t lval;
465 uint64_t llval;
466 uint8_t *bvalp;
467 char *cvalp;
468 uint8_t c;
469 smb_wchar_t wchar;
470 int count;
471 boolean_t repc_specified;
472 int repc;
473 int rc;
475 while ((c = *fmt++) != 0) {
476 repc_specified = B_FALSE;
477 repc = 1;
479 if (c == ' ' || c == '\t')
480 continue;
482 if (c == '(') {
483 while (((c = *fmt++) != 0) && c != ')')
486 if (!c)
487 return (SMB_MSGBUF_SUCCESS);
489 continue;
492 if ('0' <= c && c <= '9') {
493 repc = 0;
494 do {
495 repc = repc * 10 + c - '0';
496 c = *fmt++;
497 } while ('0' <= c && c <= '9');
498 repc_specified = B_TRUE;
499 } else if (c == '#') {
500 repc = va_arg(ap, int);
501 c = *fmt++;
502 repc_specified = B_TRUE;
505 switch (c) {
506 case '.':
507 if (smb_msgbuf_has_space(mb, repc) == 0)
508 return (SMB_MSGBUF_OVERFLOW);
510 while (repc-- > 0)
511 *mb->scan++ = 0;
512 break;
514 case 'c': /* put char */
515 if (smb_msgbuf_has_space(mb, repc) == 0)
516 return (SMB_MSGBUF_OVERFLOW);
518 bvalp = va_arg(ap, uint8_t *);
519 bcopy(bvalp, mb->scan, repc);
520 mb->scan += repc;
521 break;
523 case 'b': /* put byte */
524 if (smb_msgbuf_has_space(mb, repc) == 0)
525 return (SMB_MSGBUF_OVERFLOW);
527 while (repc-- > 0) {
528 cval = va_arg(ap, int);
529 *mb->scan++ = cval;
531 break;
533 case 'w': /* put word */
534 rc = smb_msgbuf_has_space(mb, repc * sizeof (uint16_t));
535 if (rc == 0)
536 return (SMB_MSGBUF_OVERFLOW);
538 while (repc-- > 0) {
539 wval = va_arg(ap, int);
540 LE_OUT16(mb->scan, wval);
541 mb->scan += sizeof (uint16_t);
543 break;
545 case 'l': /* put long */
546 rc = smb_msgbuf_has_space(mb, repc * sizeof (int32_t));
547 if (rc == 0)
548 return (SMB_MSGBUF_OVERFLOW);
550 while (repc-- > 0) {
551 lval = va_arg(ap, uint32_t);
552 LE_OUT32(mb->scan, lval);
553 mb->scan += sizeof (int32_t);
555 break;
557 case 'q': /* put quad */
558 rc = smb_msgbuf_has_space(mb, repc * sizeof (int64_t));
559 if (rc == 0)
560 return (SMB_MSGBUF_OVERFLOW);
562 while (repc-- > 0) {
563 llval = va_arg(ap, uint64_t);
564 LE_OUT64(mb->scan, llval);
565 mb->scan += sizeof (uint64_t);
567 break;
569 case 'u': /* conditional unicode */
570 if (mb->flags & SMB_MSGBUF_UNICODE)
571 goto unicode_translation;
572 /* FALLTHROUGH */
574 case 's': /* put string */
575 cvalp = va_arg(ap, char *);
576 if (!repc_specified) {
577 repc = smb_sbequiv_strlen(cvalp);
578 if (repc == -1)
579 return (SMB_MSGBUF_OVERFLOW);
580 if (!(mb->flags & SMB_MSGBUF_NOTERM))
581 repc++;
583 if (smb_msgbuf_has_space(mb, repc) == 0)
584 return (SMB_MSGBUF_OVERFLOW);
585 while (repc > 0) {
586 count = smb_mbtowc(&wchar, cvalp,
587 MTS_MB_CHAR_MAX);
588 if (count < 0)
589 return (SMB_MSGBUF_DATA_ERROR);
590 cvalp += count;
591 if (wchar == 0)
592 break;
593 *mb->scan++ = (uint8_t)wchar;
594 repc--;
595 if (wchar & 0xff00) {
596 *mb->scan++ = wchar >> 8;
597 repc--;
600 if (*cvalp == '\0' && repc > 0 &&
601 (mb->flags & SMB_MSGBUF_NOTERM) == 0) {
602 *mb->scan++ = 0;
603 repc--;
605 while (repc > 0) {
606 *mb->scan++ = 0;
607 repc--;
609 break;
611 case 'U': /* put unicode string */
612 unicode_translation:
614 * Unicode strings are always word aligned.
616 smb_msgbuf_word_align(mb);
617 cvalp = va_arg(ap, char *);
618 if (!repc_specified) {
619 repc = smb_wcequiv_strlen(cvalp);
620 if (!(mb->flags & SMB_MSGBUF_NOTERM))
621 repc += 2;
623 if (!smb_msgbuf_has_space(mb, repc))
624 return (SMB_MSGBUF_OVERFLOW);
625 while (repc >= 2) {
626 count = smb_mbtowc(&wchar, cvalp,
627 MTS_MB_CHAR_MAX);
628 if (count < 0)
629 return (SMB_MSGBUF_DATA_ERROR);
630 cvalp += count;
631 if (wchar == 0)
632 break;
634 LE_OUT16(mb->scan, wchar);
635 mb->scan += 2;
636 repc -= 2;
638 if (*cvalp == '\0' && repc >= 2 &&
639 (mb->flags & SMB_MSGBUF_NOTERM) == 0) {
640 LE_OUT16(mb->scan, 0);
641 mb->scan += 2;
642 repc -= 2;
644 while (repc > 0) {
645 *mb->scan++ = 0;
646 repc--;
648 break;
650 case 'M':
651 if (smb_msgbuf_has_space(mb, 4) == 0)
652 return (SMB_MSGBUF_OVERFLOW);
654 *mb->scan++ = 0xFF;
655 *mb->scan++ = 'S';
656 *mb->scan++ = 'M';
657 *mb->scan++ = 'B';
658 break;
660 default:
661 return (SMB_MSGBUF_INVALID_FORMAT);
665 return (SMB_MSGBUF_SUCCESS);
670 * smb_msgbuf_malloc
672 * Allocate some memory for use with this smb_msgbuf. We increase the
673 * requested size to hold the list pointer and return a pointer
674 * to the area for use by the caller.
676 static void *
677 smb_msgbuf_malloc(smb_msgbuf_t *mb, size_t size)
679 smb_msgbuf_mlist_t *item;
681 size += sizeof (smb_msgbuf_mlist_t);
683 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
684 if ((item = malloc(size)) == NULL)
685 return (NULL);
686 #else
687 item = kmem_alloc(size, KM_SLEEP);
688 #endif
689 item->next = mb->mlist.next;
690 item->size = size;
691 mb->mlist.next = item;
694 * The caller gets a pointer to the address
695 * immediately after the smb_msgbuf_mlist_t.
697 return ((void *)(item + 1));
702 * smb_msgbuf_chkerc
704 * Diagnostic function to write an appropriate message to the system log.
706 static int
707 smb_msgbuf_chkerc(char *text, int erc)
709 static struct {
710 int erc;
711 char *name;
712 } etable[] = {
713 { SMB_MSGBUF_SUCCESS, "success" },
714 { SMB_MSGBUF_UNDERFLOW, "overflow/underflow" },
715 { SMB_MSGBUF_INVALID_FORMAT, "invalid format" },
716 { SMB_MSGBUF_INVALID_HEADER, "invalid header" },
717 { SMB_MSGBUF_DATA_ERROR, "data error" }
720 int i;
722 for (i = 0; i < sizeof (etable)/sizeof (etable[0]); ++i) {
723 if (etable[i].erc == erc) {
724 if (text == 0)
725 text = "smb_msgbuf_chkerc";
726 break;
729 return (erc);