xref: /titanic_41/usr/src/lib/smbsrv/libsmb/common/smb_ctxbuf.c (revision 0a47c91c895e274dd0990009919e30e984364a8b)
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 /*
27  * Buffer manipulation routines. These routines can be used to format
28  * data within a data buffer without worrying about overrunning the
29  * buffer.
30  *
31  * A ctxbuf_t structure is used to track the current location within
32  * the buffer. The ctxbuf_init() must be called first to initialize the
33  * context structure. ctxbuf_printf() can then be called to fill the buffer.
34  * ctxbuf_printf will discard any data that would overrun the buffer and
35  * the buffer will always be null terminated.
36  */
37 
38 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <smbsrv/libsmb.h>
43 
44 /*
45  * smb_ctxbuf_init
46  *
47  * Initialize the buffer context structure.
48  * This must be called before any of the other
49  * buffer routines can be used.
50  *
51  * Returns -1 if invalid parameters, 0 otherwise
52  */
53 int
54 smb_ctxbuf_init(smb_ctxbuf_t *ctx, unsigned char *buf, size_t buflen)
55 {
56 	if (ctx == 0 || buf == 0 || buflen == 0)
57 		return (-1);
58 
59 	buf[0] = '\0';
60 
61 	ctx->basep = buf;
62 	ctx->curp = buf;
63 	ctx->endp = &buf[buflen];
64 
65 	return (0);
66 }
67 
68 /*
69  * smb_ctxbuf_len
70  *
71  * Return the amount of data stored in the buffer,
72  * excluding the terminating null character. Similar
73  * to strlen()
74  *
75  * Returns 0 if the ctx is invalid.
76  */
77 int
78 smb_ctxbuf_len(smb_ctxbuf_t *ctx)
79 {
80 	if (ctx == 0 || ctx->basep == 0 ||
81 	    ctx->curp == 0 || ctx->endp == 0)
82 		return (0);
83 	else
84 		/*LINTED E_PTRDIFF_OVERFLOW*/
85 		return (ctx->curp - ctx->basep);
86 }
87 
88 /*
89  * smb_ctxbuf_printf
90  *
91  * Move formatted output (based on fmt string) to the buffer
92  * identified in ctxbuf.  Any output characters beyond the buffer
93  * are discarded and a null character is written at the end of the
94  * characters actually written.
95  *
96  * Returns
97  * Always return the number of bytes actually written (excluding the
98  * terminating null).
99  */
100 int
101 smb_ctxbuf_printf(smb_ctxbuf_t   *ctx, const char *fmt, ...)
102 {
103 	int n;
104 	va_list args;
105 
106 	if (ctx == 0 || ctx->basep == 0 ||
107 	    ctx->curp == 0 || ctx->endp == 0)
108 		return (-1);
109 
110 	va_start(args, fmt);
111 	/*LINTED E_PTRDIFF_OVERFLOW*/
112 	n = vsnprintf((char *)ctx->curp, ctx->endp-ctx->curp, fmt, args);
113 	ctx->curp += n;
114 	va_end(args);
115 
116 	/*
117 	 * return the number of bytes moved into the buffer.
118 	 */
119 	return (n);
120 }
121