1da6c28aaSamw /* 2da6c28aaSamw * CDDL HEADER START 3da6c28aaSamw * 4da6c28aaSamw * The contents of this file are subject to the terms of the 5da6c28aaSamw * Common Development and Distribution License (the "License"). 6da6c28aaSamw * You may not use this file except in compliance with the License. 7da6c28aaSamw * 8da6c28aaSamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9da6c28aaSamw * or http://www.opensolaris.org/os/licensing. 10da6c28aaSamw * See the License for the specific language governing permissions 11da6c28aaSamw * and limitations under the License. 12da6c28aaSamw * 13da6c28aaSamw * When distributing Covered Code, include this CDDL HEADER in each 14da6c28aaSamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15da6c28aaSamw * If applicable, add the following below this CDDL HEADER, with the 16da6c28aaSamw * fields enclosed by brackets "[]" replaced with your own identifying 17da6c28aaSamw * information: Portions Copyright [yyyy] [name of copyright owner] 18da6c28aaSamw * 19da6c28aaSamw * CDDL HEADER END 20da6c28aaSamw */ 21da6c28aaSamw /* 22*bbf6f00cSJordan Brown * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23da6c28aaSamw * Use is subject to license terms. 24da6c28aaSamw */ 25da6c28aaSamw 26da6c28aaSamw #ifndef _SMBSRV_MSGBUF_H 27da6c28aaSamw #define _SMBSRV_MSGBUF_H 28da6c28aaSamw 29da6c28aaSamw /* 30da6c28aaSamw * Definition and interface for smb_msgbuf buffer management. The 31da6c28aaSamw * smb_msgbuf interface is typically used to encode or decode SMB 32da6c28aaSamw * data using sprintf/scanf style operations. It can also be used 33da6c28aaSamw * for general purpose encoding and decoding. 34da6c28aaSamw */ 35da6c28aaSamw 36da6c28aaSamw #include <sys/types.h> 37*bbf6f00cSJordan Brown #include <smbsrv/string.h> 38da6c28aaSamw 39da6c28aaSamw #ifdef __cplusplus 40da6c28aaSamw extern "C" { 41da6c28aaSamw #endif 42da6c28aaSamw 43da6c28aaSamw /* 44da6c28aaSamw * When unicode strings are decoded, the resultant UTF-8 strings are 45da6c28aaSamw * stored in dynamically allocated areas, which are held on a linked 46da6c28aaSamw * list anchored at smb_msgbuf.mlist. The list is deallocated by 47da6c28aaSamw * smb_msgbuf_term. 48da6c28aaSamw */ 49da6c28aaSamw typedef struct smb_msgbuf_mlist { 50da6c28aaSamw struct smb_msgbuf_mlist *next; 51da6c28aaSamw size_t size; 52da6c28aaSamw } smb_msgbuf_mlist_t; 53da6c28aaSamw 54da6c28aaSamw /* 55da6c28aaSamw * smb_smgbuf flags 56da6c28aaSamw * 57da6c28aaSamw * SMB_MSGBUF_UNICODE When there is a choice between unicode or ascii 58da6c28aaSamw * formatting, select unicode processing. 59da6c28aaSamw * SMB_MSGBUF_NOTERM Do not null terminate strings. 60da6c28aaSamw */ 61da6c28aaSamw #define SMB_MSGBUF_UNICODE 0x00000001 62da6c28aaSamw #define SMB_MSGBUF_NOTERM 0x00000002 63da6c28aaSamw 64da6c28aaSamw /* 65da6c28aaSamw * base: points to the beginning of the buffer 66da6c28aaSamw * end: points to the limit of the buffer. 67da6c28aaSamw * scan: points to the current offset. 68da6c28aaSamw * max: holds the number of bytes in the buffer. 69da6c28aaSamw * count: unused. 70da6c28aaSamw * mlist: anchors the dynamically allocated memory list. 71da6c28aaSamw * flags: see SMB_SMGBUF flags. 72da6c28aaSamw */ 73da6c28aaSamw typedef struct smb_msgbuf { 74da6c28aaSamw uint8_t *base; 75da6c28aaSamw uint8_t *end; 76da6c28aaSamw uint8_t *scan; 77da6c28aaSamw size_t count; 78da6c28aaSamw size_t max; 79da6c28aaSamw smb_msgbuf_mlist_t mlist; 80da6c28aaSamw uint32_t flags; 81da6c28aaSamw } smb_msgbuf_t; 82da6c28aaSamw 83da6c28aaSamw /* 84da6c28aaSamw * List of smb_msgbuf_decode and smb_msgbuf_encode return values. 85da6c28aaSamw */ 86da6c28aaSamw #define SMB_MSGBUF_SUCCESS 0 87da6c28aaSamw #define SMB_MSGBUF_UNDERFLOW -1 88da6c28aaSamw #define SMB_MSGBUF_OVERFLOW SMB_MSGBUF_UNDERFLOW 89da6c28aaSamw #define SMB_MSGBUF_INVALID_FORMAT -2 90da6c28aaSamw #define SMB_MSGBUF_INVALID_HEADER -3 91da6c28aaSamw #define SMB_MSGBUF_DATA_ERROR -4 92da6c28aaSamw 93da6c28aaSamw /* 94da6c28aaSamw * smb_msgbuf_init must be called to associate the smb_msgbuf_t with 95da6c28aaSamw * a buffer before any encode or decode operations may be performed. 96da6c28aaSamw * 97da6c28aaSamw * smb_msgbuf_term must be called to free any dynamically allocated memory 98da6c28aaSamw * that was acquired during encode or decode operations. At this time 99da6c28aaSamw * the only operation that allocates memory is a unicode string decode. 100da6c28aaSamw * 101da6c28aaSamw * If there are no errors, smb_msgbuf_decode and smb_msgbuf_encode return 102da6c28aaSamw * the number of bytes decoded or encoded. If there is a problem they 103da6c28aaSamw * return -ve error codes. 104da6c28aaSamw */ 105da6c28aaSamw extern void smb_msgbuf_init(smb_msgbuf_t *, uint8_t *, size_t, uint32_t); 106da6c28aaSamw extern void smb_msgbuf_term(smb_msgbuf_t *); 107da6c28aaSamw extern int smb_msgbuf_decode(smb_msgbuf_t *, char *, ...); 108da6c28aaSamw extern int smb_msgbuf_encode(smb_msgbuf_t *, char *, ...); 109da6c28aaSamw extern size_t smb_msgbuf_used(smb_msgbuf_t *); 110da6c28aaSamw extern size_t smb_msgbuf_size(smb_msgbuf_t *); 111da6c28aaSamw extern uint8_t *smb_msgbuf_base(smb_msgbuf_t *); 112da6c28aaSamw extern void smb_msgbuf_word_align(smb_msgbuf_t *); 113da6c28aaSamw extern void smb_msgbuf_dword_align(smb_msgbuf_t *); 114da6c28aaSamw extern int smb_msgbuf_has_space(smb_msgbuf_t *, size_t); 115da6c28aaSamw extern void smb_msgbuf_fset(smb_msgbuf_t *, uint32_t); 116da6c28aaSamw extern void smb_msgbuf_fclear(smb_msgbuf_t *, uint32_t); 117da6c28aaSamw 118da6c28aaSamw #ifdef __cplusplus 119da6c28aaSamw } 120da6c28aaSamw #endif 121da6c28aaSamw 122da6c28aaSamw #endif /* _SMBSRV_MSGBUF_H */ 123