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 #include <stdio.h>
39 #include <stdarg.h>
40 #include <smbsrv/libsmb.h>
41
42 /*
43 * smb_ctxbuf_init
44 *
45 * Initialize the buffer context structure.
46 * This must be called before any of the other
47 * buffer routines can be used.
48 *
49 * Returns -1 if invalid parameters, 0 otherwise
50 */
51 int
smb_ctxbuf_init(smb_ctxbuf_t * ctx,unsigned char * buf,size_t buflen)52 smb_ctxbuf_init(smb_ctxbuf_t *ctx, unsigned char *buf, size_t buflen)
53 {
54 if (ctx == 0 || buf == 0 || buflen == 0)
55 return (-1);
56
57 buf[0] = '\0';
58
59 ctx->basep = buf;
60 ctx->curp = buf;
61 ctx->endp = &buf[buflen];
62
63 return (0);
64 }
65
66 /*
67 * smb_ctxbuf_len
68 *
69 * Return the amount of data stored in the buffer,
70 * excluding the terminating null character. Similar
71 * to strlen()
72 *
73 * Returns 0 if the ctx is invalid.
74 */
75 int
smb_ctxbuf_len(smb_ctxbuf_t * ctx)76 smb_ctxbuf_len(smb_ctxbuf_t *ctx)
77 {
78 if (ctx == 0 || ctx->basep == 0 ||
79 ctx->curp == 0 || ctx->endp == 0)
80 return (0);
81 else
82 /*LINTED E_PTRDIFF_OVERFLOW*/
83 return (ctx->curp - ctx->basep);
84 }
85
86 /*
87 * smb_ctxbuf_printf
88 *
89 * Move formatted output (based on fmt string) to the buffer
90 * identified in ctxbuf. Any output characters beyond the buffer
91 * are discarded and a null character is written at the end of the
92 * characters actually written.
93 *
94 * Returns
95 * Always return the number of bytes actually written (excluding the
96 * terminating null).
97 */
98 int
smb_ctxbuf_printf(smb_ctxbuf_t * ctx,const char * fmt,...)99 smb_ctxbuf_printf(smb_ctxbuf_t *ctx, const char *fmt, ...)
100 {
101 int n;
102 va_list args;
103
104 if (ctx == 0 || ctx->basep == 0 ||
105 ctx->curp == 0 || ctx->endp == 0)
106 return (-1);
107
108 va_start(args, fmt);
109 /*LINTED E_PTRDIFF_OVERFLOW*/
110 n = vsnprintf((char *)ctx->curp, ctx->endp-ctx->curp, fmt, args);
111 ctx->curp += n;
112 va_end(args);
113
114 /*
115 * return the number of bytes moved into the buffer.
116 */
117 return (n);
118 }
119