1*5331Samw /*
2*5331Samw  * CDDL HEADER START
3*5331Samw  *
4*5331Samw  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7*5331Samw  *
8*5331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5331Samw  * or http://www.opensolaris.org/os/licensing.
10*5331Samw  * See the License for the specific language governing permissions
11*5331Samw  * and limitations under the License.
12*5331Samw  *
13*5331Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*5331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5331Samw  * If applicable, add the following below this CDDL HEADER, with the
16*5331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*5331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5331Samw  *
19*5331Samw  * CDDL HEADER END
20*5331Samw  */
21*5331Samw /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*5331Samw  * Use is subject to license terms.
24*5331Samw  */
25*5331Samw 
26*5331Samw #ifndef _SMBSRV_MSGBUF_H
27*5331Samw #define	_SMBSRV_MSGBUF_H
28*5331Samw 
29*5331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*5331Samw 
31*5331Samw /*
32*5331Samw  * Definition and interface for smb_msgbuf buffer management.  The
33*5331Samw  * smb_msgbuf interface is typically used to encode or decode SMB
34*5331Samw  * data using sprintf/scanf style operations.  It can also be used
35*5331Samw  * for general purpose encoding and decoding.
36*5331Samw  */
37*5331Samw 
38*5331Samw #include <sys/types.h>
39*5331Samw #include <smbsrv/smb_i18n.h>
40*5331Samw 
41*5331Samw #ifdef __cplusplus
42*5331Samw extern "C" {
43*5331Samw #endif
44*5331Samw 
45*5331Samw /*
46*5331Samw  * When unicode strings are decoded, the resultant UTF-8 strings are
47*5331Samw  * stored in dynamically allocated areas, which are held on a linked
48*5331Samw  * list anchored at smb_msgbuf.mlist.  The list is deallocated by
49*5331Samw  * smb_msgbuf_term.
50*5331Samw  */
51*5331Samw typedef struct smb_msgbuf_mlist {
52*5331Samw     struct smb_msgbuf_mlist *next;
53*5331Samw     size_t size;
54*5331Samw } smb_msgbuf_mlist_t;
55*5331Samw 
56*5331Samw /*
57*5331Samw  * smb_smgbuf flags
58*5331Samw  *
59*5331Samw  * SMB_MSGBUF_UNICODE	When there is a choice between unicode or ascii
60*5331Samw  *			formatting, select unicode processing.
61*5331Samw  * SMB_MSGBUF_NOTERM	Do not null terminate strings.
62*5331Samw  */
63*5331Samw #define	SMB_MSGBUF_UNICODE		0x00000001
64*5331Samw #define	SMB_MSGBUF_NOTERM		0x00000002
65*5331Samw 
66*5331Samw /*
67*5331Samw  * base:   points to the beginning of the buffer
68*5331Samw  * end:    points to the limit of the buffer.
69*5331Samw  * scan:   points to the current offset.
70*5331Samw  * max:    holds the number of bytes in the buffer.
71*5331Samw  * count:  unused.
72*5331Samw  * mlist:  anchors the dynamically allocated memory list.
73*5331Samw  * flags:  see SMB_SMGBUF flags.
74*5331Samw  */
75*5331Samw typedef struct smb_msgbuf {
76*5331Samw 	uint8_t *base;
77*5331Samw 	uint8_t *end;
78*5331Samw 	uint8_t *scan;
79*5331Samw 	size_t count;
80*5331Samw 	size_t max;
81*5331Samw 	smb_msgbuf_mlist_t mlist;
82*5331Samw 	uint32_t flags;
83*5331Samw } smb_msgbuf_t;
84*5331Samw 
85*5331Samw /*
86*5331Samw  * List of smb_msgbuf_decode and smb_msgbuf_encode return values.
87*5331Samw  */
88*5331Samw #define	SMB_MSGBUF_SUCCESS		0
89*5331Samw #define	SMB_MSGBUF_UNDERFLOW		-1
90*5331Samw #define	SMB_MSGBUF_OVERFLOW		SMB_MSGBUF_UNDERFLOW
91*5331Samw #define	SMB_MSGBUF_INVALID_FORMAT	-2
92*5331Samw #define	SMB_MSGBUF_INVALID_HEADER	-3
93*5331Samw #define	SMB_MSGBUF_DATA_ERROR		-4
94*5331Samw 
95*5331Samw /*
96*5331Samw  * smb_msgbuf_init must be called to associate the smb_msgbuf_t with
97*5331Samw  * a buffer before any encode or decode operations may be performed.
98*5331Samw  *
99*5331Samw  * smb_msgbuf_term must be called to free any dynamically allocated memory
100*5331Samw  * that was acquired during encode or decode operations. At this time
101*5331Samw  * the only operation that allocates memory is a unicode string decode.
102*5331Samw  *
103*5331Samw  * If there are no errors, smb_msgbuf_decode and smb_msgbuf_encode return
104*5331Samw  * the number of bytes decoded or encoded.  If there is a problem they
105*5331Samw  * return -ve error codes.
106*5331Samw  */
107*5331Samw extern void smb_msgbuf_init(smb_msgbuf_t *, uint8_t *, size_t, uint32_t);
108*5331Samw extern void smb_msgbuf_term(smb_msgbuf_t *);
109*5331Samw extern int smb_msgbuf_decode(smb_msgbuf_t *, char *, ...);
110*5331Samw extern int smb_msgbuf_encode(smb_msgbuf_t *, char *, ...);
111*5331Samw extern size_t smb_msgbuf_used(smb_msgbuf_t *);
112*5331Samw extern size_t smb_msgbuf_size(smb_msgbuf_t *);
113*5331Samw extern uint8_t *smb_msgbuf_base(smb_msgbuf_t *);
114*5331Samw extern void smb_msgbuf_word_align(smb_msgbuf_t *);
115*5331Samw extern void smb_msgbuf_dword_align(smb_msgbuf_t *);
116*5331Samw extern int smb_msgbuf_has_space(smb_msgbuf_t *, size_t);
117*5331Samw extern void smb_msgbuf_fset(smb_msgbuf_t *, uint32_t);
118*5331Samw extern void smb_msgbuf_fclear(smb_msgbuf_t *, uint32_t);
119*5331Samw 
120*5331Samw #ifdef __cplusplus
121*5331Samw }
122*5331Samw #endif
123*5331Samw 
124*5331Samw #endif /* _SMBSRV_MSGBUF_H */
125