da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or http://www.opensolaris.org/os/licensing.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Buffer manipulation routines. These routines can be used to format
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * data within a data buffer without worrying about overrunning the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * buffer.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * A ctxbuf_t structure is used to track the current location within
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the buffer. The ctxbuf_init() must be called first to initialize the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * context structure. ctxbuf_printf() can then be called to fill the buffer.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ctxbuf_printf will discard any data that would overrun the buffer and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the buffer will always be null terminated.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#pragma ident "%Z%%M% %I% %E% SMI"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdio.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdarg.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/libsmb.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ctxbuf_init
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Initialize the buffer context structure.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This must be called before any of the other
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * buffer routines can be used.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns -1 if invalid parameters, 0 otherwise
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ctxbuf_init(smb_ctxbuf_t *ctx, unsigned char *buf, size_t buflen)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ctx == 0 || buf == 0 || buflen == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw buf[0] = '\0';
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->basep = buf;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->curp = buf;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->endp = &buf[buflen];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ctxbuf_len
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Return the amount of data stored in the buffer,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * excluding the terminating null character. Similar
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * to strlen()
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns 0 if the ctx is invalid.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ctxbuf_len(smb_ctxbuf_t *ctx)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ctx == 0 || ctx->basep == 0 ||
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->curp == 0 || ctx->endp == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (ctx->curp - ctx->basep);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ctxbuf_printf
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Move formatted output (based on fmt string) to the buffer
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * identified in ctxbuf. Any output characters beyond the buffer
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * are discarded and a null character is written at the end of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * characters actually written.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Always return the number of bytes actually written (excluding the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * terminating null).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ctxbuf_printf(smb_ctxbuf_t *ctx, const char *fmt, ...)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int n;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_list args;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ctx == 0 || ctx->basep == 0 ||
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->curp == 0 || ctx->endp == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_start(args, fmt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw n = vsnprintf((char *)ctx->curp, ctx->endp-ctx->curp, fmt, args);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ctx->curp += n;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw va_end(args);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * return the number of bytes moved into the buffer.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (n);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}