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