1*da6c28aaSamw /*
2*da6c28aaSamw  * CDDL HEADER START
3*da6c28aaSamw  *
4*da6c28aaSamw  * The contents of this file are subject to the terms of the
5*da6c28aaSamw  * Common Development and Distribution License (the "License").
6*da6c28aaSamw  * You may not use this file except in compliance with the License.
7*da6c28aaSamw  *
8*da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10*da6c28aaSamw  * See the License for the specific language governing permissions
11*da6c28aaSamw  * and limitations under the License.
12*da6c28aaSamw  *
13*da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14*da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16*da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17*da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*da6c28aaSamw  *
19*da6c28aaSamw  * CDDL HEADER END
20*da6c28aaSamw  */
21*da6c28aaSamw /*
22*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*da6c28aaSamw  * Use is subject to license terms.
24*da6c28aaSamw  */
25*da6c28aaSamw 
26*da6c28aaSamw /*
27*da6c28aaSamw  * Buffer manipulation routines. These routines can be used to format
28*da6c28aaSamw  * data within a data buffer without worrying about overrunning the
29*da6c28aaSamw  * buffer.
30*da6c28aaSamw  *
31*da6c28aaSamw  * A ctxbuf_t structure is used to track the current location within
32*da6c28aaSamw  * the buffer. The ctxbuf_init() must be called first to initialize the
33*da6c28aaSamw  * context structure. ctxbuf_printf() can then be called to fill the buffer.
34*da6c28aaSamw  * ctxbuf_printf will discard any data that would overrun the buffer and
35*da6c28aaSamw  * the buffer will always be null terminated.
36*da6c28aaSamw  */
37*da6c28aaSamw 
38*da6c28aaSamw #include <stdio.h>
39*da6c28aaSamw #include <stdarg.h>
40*da6c28aaSamw #include <smbsrv/libsmb.h>
41*da6c28aaSamw 
42*da6c28aaSamw /*
43*da6c28aaSamw  * smb_ctxbuf_init
44*da6c28aaSamw  *
45*da6c28aaSamw  * Initialize the buffer context structure.
46*da6c28aaSamw  * This must be called before any of the other
47*da6c28aaSamw  * buffer routines can be used.
48*da6c28aaSamw  *
49*da6c28aaSamw  * Returns -1 if invalid parameters, 0 otherwise
50*da6c28aaSamw  */
51*da6c28aaSamw int
smb_ctxbuf_init(smb_ctxbuf_t * ctx,unsigned char * buf,size_t buflen)52*da6c28aaSamw smb_ctxbuf_init(smb_ctxbuf_t *ctx, unsigned char *buf, size_t buflen)
53*da6c28aaSamw {
54*da6c28aaSamw 	if (ctx == 0 || buf == 0 || buflen == 0)
55*da6c28aaSamw 		return (-1);
56*da6c28aaSamw 
57*da6c28aaSamw 	buf[0] = '\0';
58*da6c28aaSamw 
59*da6c28aaSamw 	ctx->basep = buf;
60*da6c28aaSamw 	ctx->curp = buf;
61*da6c28aaSamw 	ctx->endp = &buf[buflen];
62*da6c28aaSamw 
63*da6c28aaSamw 	return (0);
64*da6c28aaSamw }
65*da6c28aaSamw 
66*da6c28aaSamw /*
67*da6c28aaSamw  * smb_ctxbuf_len
68*da6c28aaSamw  *
69*da6c28aaSamw  * Return the amount of data stored in the buffer,
70*da6c28aaSamw  * excluding the terminating null character. Similar
71*da6c28aaSamw  * to strlen()
72*da6c28aaSamw  *
73*da6c28aaSamw  * Returns 0 if the ctx is invalid.
74*da6c28aaSamw  */
75*da6c28aaSamw int
smb_ctxbuf_len(smb_ctxbuf_t * ctx)76*da6c28aaSamw smb_ctxbuf_len(smb_ctxbuf_t *ctx)
77*da6c28aaSamw {
78*da6c28aaSamw 	if (ctx == 0 || ctx->basep == 0 ||
79*da6c28aaSamw 	    ctx->curp == 0 || ctx->endp == 0)
80*da6c28aaSamw 		return (0);
81*da6c28aaSamw 	else
82*da6c28aaSamw 		/*LINTED E_PTRDIFF_OVERFLOW*/
83*da6c28aaSamw 		return (ctx->curp - ctx->basep);
84*da6c28aaSamw }
85*da6c28aaSamw 
86*da6c28aaSamw /*
87*da6c28aaSamw  * smb_ctxbuf_printf
88*da6c28aaSamw  *
89*da6c28aaSamw  * Move formatted output (based on fmt string) to the buffer
90*da6c28aaSamw  * identified in ctxbuf.  Any output characters beyond the buffer
91*da6c28aaSamw  * are discarded and a null character is written at the end of the
92*da6c28aaSamw  * characters actually written.
93*da6c28aaSamw  *
94*da6c28aaSamw  * Returns
95*da6c28aaSamw  * Always return the number of bytes actually written (excluding the
96*da6c28aaSamw  * terminating null).
97*da6c28aaSamw  */
98*da6c28aaSamw int
smb_ctxbuf_printf(smb_ctxbuf_t * ctx,const char * fmt,...)99*da6c28aaSamw smb_ctxbuf_printf(smb_ctxbuf_t   *ctx, const char *fmt, ...)
100*da6c28aaSamw {
101*da6c28aaSamw 	int n;
102*da6c28aaSamw 	va_list args;
103*da6c28aaSamw 
104*da6c28aaSamw 	if (ctx == 0 || ctx->basep == 0 ||
105*da6c28aaSamw 	    ctx->curp == 0 || ctx->endp == 0)
106*da6c28aaSamw 		return (-1);
107*da6c28aaSamw 
108*da6c28aaSamw 	va_start(args, fmt);
109*da6c28aaSamw 	/*LINTED E_PTRDIFF_OVERFLOW*/
110*da6c28aaSamw 	n = vsnprintf((char *)ctx->curp, ctx->endp-ctx->curp, fmt, args);
111*da6c28aaSamw 	ctx->curp += n;
112*da6c28aaSamw 	va_end(args);
113*da6c28aaSamw 
114*da6c28aaSamw 	/*
115*da6c28aaSamw 	 * return the number of bytes moved into the buffer.
116*da6c28aaSamw 	 */
117*da6c28aaSamw 	return (n);
118*da6c28aaSamw }
119