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