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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Buffer manipulation routines. These routines can be used to format
28 * data within a data buffer without worrying about overrunning the
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.
38 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include <smbsrv/libsmb.h>
47 * Initialize the buffer context structure.
48 * This must be called before any of the other
49 * buffer routines can be used.
51 * Returns -1 if invalid parameters, 0 otherwise
54 smb_ctxbuf_init(smb_ctxbuf_t
*ctx
, unsigned char *buf
, size_t buflen
)
56 if (ctx
== 0 || buf
== 0 || buflen
== 0)
63 ctx
->endp
= &buf
[buflen
];
71 * Return the amount of data stored in the buffer,
72 * excluding the terminating null character. Similar
75 * Returns 0 if the ctx is invalid.
78 smb_ctxbuf_len(smb_ctxbuf_t
*ctx
)
80 if (ctx
== 0 || ctx
->basep
== 0 ||
81 ctx
->curp
== 0 || ctx
->endp
== 0)
84 /*LINTED E_PTRDIFF_OVERFLOW*/
85 return (ctx
->curp
- ctx
->basep
);
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.
97 * Always return the number of bytes actually written (excluding the
101 smb_ctxbuf_printf(smb_ctxbuf_t
*ctx
, const char *fmt
, ...)
106 if (ctx
== 0 || ctx
->basep
== 0 ||
107 ctx
->curp
== 0 || ctx
->endp
== 0)
111 /*LINTED E_PTRDIFF_OVERFLOW*/
112 n
= vsnprintf((char *)ctx
->curp
, ctx
->endp
-ctx
->curp
, fmt
, args
);
117 * return the number of bytes moved into the buffer.