xref: /illumos-gate/usr/src/uts/common/smbsrv/msgbuf.h (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
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 #ifndef _SMBSRV_MSGBUF_H
27 #define	_SMBSRV_MSGBUF_H
28 
29 /*
30  * Definition and interface for smb_msgbuf buffer management.  The
31  * smb_msgbuf interface is typically used to encode or decode SMB
32  * data using sprintf/scanf style operations.  It can also be used
33  * for general purpose encoding and decoding.
34  */
35 
36 #include <sys/types.h>
37 #include <smbsrv/string.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * When unicode strings are decoded, the resultant UTF-8 strings are
45  * stored in dynamically allocated areas, which are held on a linked
46  * list anchored at smb_msgbuf.mlist.  The list is deallocated by
47  * smb_msgbuf_term.
48  */
49 typedef struct smb_msgbuf_mlist {
50     struct smb_msgbuf_mlist *next;
51     size_t size;
52 } smb_msgbuf_mlist_t;
53 
54 /*
55  * smb_smgbuf flags
56  *
57  * SMB_MSGBUF_UNICODE	When there is a choice between unicode or ascii
58  *			formatting, select unicode processing.
59  * SMB_MSGBUF_NOTERM	Do not null terminate strings.
60  */
61 #define	SMB_MSGBUF_UNICODE		0x00000001
62 #define	SMB_MSGBUF_NOTERM		0x00000002
63 
64 /*
65  * base:   points to the beginning of the buffer
66  * end:    points to the limit of the buffer.
67  * scan:   points to the current offset.
68  * max:    holds the number of bytes in the buffer.
69  * count:  unused.
70  * mlist:  anchors the dynamically allocated memory list.
71  * flags:  see SMB_SMGBUF flags.
72  */
73 typedef struct smb_msgbuf {
74 	uint8_t *base;
75 	uint8_t *end;
76 	uint8_t *scan;
77 	size_t count;
78 	size_t max;
79 	smb_msgbuf_mlist_t mlist;
80 	uint32_t flags;
81 } smb_msgbuf_t;
82 
83 /*
84  * List of smb_msgbuf_decode and smb_msgbuf_encode return values.
85  */
86 #define	SMB_MSGBUF_SUCCESS		0
87 #define	SMB_MSGBUF_UNDERFLOW		-1
88 #define	SMB_MSGBUF_OVERFLOW		SMB_MSGBUF_UNDERFLOW
89 #define	SMB_MSGBUF_INVALID_FORMAT	-2
90 #define	SMB_MSGBUF_INVALID_HEADER	-3
91 #define	SMB_MSGBUF_DATA_ERROR		-4
92 
93 /*
94  * smb_msgbuf_init must be called to associate the smb_msgbuf_t with
95  * a buffer before any encode or decode operations may be performed.
96  *
97  * smb_msgbuf_term must be called to free any dynamically allocated memory
98  * that was acquired during encode or decode operations. At this time
99  * the only operation that allocates memory is a unicode string decode.
100  *
101  * If there are no errors, smb_msgbuf_decode and smb_msgbuf_encode return
102  * the number of bytes decoded or encoded.  If there is a problem they
103  * return -ve error codes.
104  */
105 extern void smb_msgbuf_init(smb_msgbuf_t *, uint8_t *, size_t, uint32_t);
106 extern void smb_msgbuf_term(smb_msgbuf_t *);
107 extern int smb_msgbuf_decode(smb_msgbuf_t *, char *, ...);
108 extern int smb_msgbuf_encode(smb_msgbuf_t *, char *, ...);
109 extern size_t smb_msgbuf_used(smb_msgbuf_t *);
110 extern size_t smb_msgbuf_size(smb_msgbuf_t *);
111 extern uint8_t *smb_msgbuf_base(smb_msgbuf_t *);
112 extern void smb_msgbuf_word_align(smb_msgbuf_t *);
113 extern void smb_msgbuf_dword_align(smb_msgbuf_t *);
114 extern int smb_msgbuf_has_space(smb_msgbuf_t *, size_t);
115 extern void smb_msgbuf_fset(smb_msgbuf_t *, uint32_t);
116 extern void smb_msgbuf_fclear(smb_msgbuf_t *, uint32_t);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* _SMBSRV_MSGBUF_H */
123