1172302Spjd/*- 2172302Spjd * Copyright (c) 2005 Ivan Voras <ivoras@gmail.com> 3172302Spjd * All rights reserved. 4172302Spjd * 5172302Spjd * Redistribution and use in source and binary forms, with or without 6172302Spjd * modification, are permitted provided that the following conditions 7172302Spjd * are met: 8172302Spjd * 1. Redistributions of source code must retain the above copyright 9172302Spjd * notice, this list of conditions and the following disclaimer. 10172302Spjd * 2. Redistributions in binary form must reproduce the above copyright 11172302Spjd * notice, this list of conditions and the following disclaimer in the 12172302Spjd * documentation and/or other materials provided with the distribution. 13172302Spjd * 14172302Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15172302Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16172302Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17172302Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18172302Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19172302Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20172302Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21172302Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22172302Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23172302Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24172302Spjd * SUCH DAMAGE. 25172302Spjd * 26172302Spjd * $FreeBSD: releng/10.3/sys/geom/virstor/binstream.h 172302 2007-09-23 07:34:23Z pjd $ 27172302Spjd */ 28172302Spjd 29172302Spjd// $Id: binstream.h,v 1.1 2006/07/05 10:47:54 ivoras Exp $ 30172302Spjd 31172302Spjd 32172302Spjd#ifndef _BIN_STREAM_ 33172302Spjd#define _BIN_STREAM_ 34172302Spjd 35172302Spjd#ifndef uint8_t 36172302Spjd#define uint8_t unsigned char 37172302Spjd#endif 38172302Spjd 39172302Spjdtypedef struct { 40172302Spjd unsigned char *data; 41172302Spjd int pos; 42172302Spjd} bin_stream_t; 43172302Spjd 44172302Spjd 45172302Spjd/* "Open" a binary stream for reading */ 46172302Spjdvoid bs_open (bin_stream_t * bs, void *data); 47172302Spjd 48172302Spjd/* "Reset" position in binary stream to zero */ 49172302Spjdvoid bs_reset (bin_stream_t * bs); 50172302Spjd 51172302Spjd 52172302Spjd/* Write a zero-terminated string; return next position */ 53172302Spjdunsigned bs_write_str(bin_stream_t * bs, char *data); 54172302Spjd 55172302Spjd/* Write an arbitrary buffer; return next position */ 56172302Spjdunsigned bs_write_buf(bin_stream_t * bs, char *data, unsigned data_size); 57172302Spjd 58172302Spjd/* Write a 8bit uint; return next position. */ 59172302Spjdunsigned bs_write_u8(bin_stream_t * bs, uint8_t data); 60172302Spjd 61172302Spjd/* Write a 16bit uint; return next position. */ 62172302Spjdunsigned bs_write_u16(bin_stream_t * bs, uint16_t data); 63172302Spjd 64172302Spjd/* Write a 32bit uint; return next position. */ 65172302Spjdunsigned bs_write_u32(bin_stream_t * bs, uint32_t data); 66172302Spjd 67172302Spjd/* Write a 64bit uint; return next position. */ 68172302Spjdunsigned bs_write_u64(bin_stream_t * bs, uint64_t data); 69172302Spjd 70172302Spjd 71172302Spjd/* 72172302Spjd * Read a null-terminated string from stream into a buffer; buf_size is size 73172302Spjd * of the buffer, including the final \0. Returns buf pointer or NULL if 74172302Spjd * garbage input. 75172302Spjd */ 76172302Spjdchar *bs_read_str(bin_stream_t * bs, char *buf, unsigned buf_size); 77172302Spjd 78172302Spjd/* Read an arbitrary buffer. */ 79172302Spjdvoid bs_read_buf(bin_stream_t * bs, char *buf, unsigned buf_size); 80172302Spjd 81172302Spjd/* Read a 8bit uint * return it */ 82172302Spjduint8_t bs_read_u8(bin_stream_t * bs); 83172302Spjd 84172302Spjd/* Read a 16bit uint * return it */ 85172302Spjduint16_t bs_read_u16(bin_stream_t * bs); 86172302Spjd 87172302Spjd/* Read a 8bit uint * return it */ 88172302Spjduint32_t bs_read_u32(bin_stream_t * bs); 89172302Spjd 90172302Spjd/* Read a 8bit uint * return it */ 91172302Spjduint64_t bs_read_u64(bin_stream_t * bs); 92172302Spjd 93172302Spjd#endif 94