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