1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_region.h,v 1.7 2008/02/09 14:56:20 junyoung Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2003 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor * 29219019Sgabor */ 30219019Sgabor 31219019Sgabor#ifndef _CITRUS_REGION_H_ 32219019Sgabor#define _CITRUS_REGION_H_ 33219019Sgabor 34219019Sgabor#include <sys/types.h> 35219019Sgabor 36219019Sgaborstruct _citrus_region { 37219019Sgabor void *r_head; 38219019Sgabor size_t r_size; 39219019Sgabor}; 40219019Sgabor 41219019Sgaborstatic __inline void 42219019Sgabor_citrus_region_init(struct _citrus_region *r, void *h, size_t sz) 43219019Sgabor{ 44219019Sgabor 45219019Sgabor r->r_head = h; 46219019Sgabor r->r_size = sz; 47219019Sgabor} 48219019Sgabor 49219019Sgaborstatic __inline void * 50219019Sgabor_citrus_region_head(const struct _citrus_region *r) 51219019Sgabor{ 52219019Sgabor 53219019Sgabor return (r->r_head); 54219019Sgabor} 55219019Sgabor 56219019Sgaborstatic __inline size_t 57219019Sgabor_citrus_region_size(const struct _citrus_region *r) 58219019Sgabor{ 59219019Sgabor 60219019Sgabor return (r->r_size); 61219019Sgabor} 62219019Sgabor 63219019Sgaborstatic __inline int 64219019Sgabor_citrus_region_check(const struct _citrus_region *r, size_t ofs, size_t sz) 65219019Sgabor{ 66219019Sgabor 67219019Sgabor return (r->r_size >= ofs + sz ? 0 : -1); 68219019Sgabor} 69219019Sgabor 70219019Sgaborstatic __inline void * 71219019Sgabor_citrus_region_offset(const struct _citrus_region *r, size_t pos) 72219019Sgabor{ 73219019Sgabor 74219019Sgabor return ((void *)((uint8_t *)r->r_head + pos)); 75219019Sgabor} 76219019Sgabor 77219019Sgaborstatic __inline uint8_t 78219019Sgabor_citrus_region_peek8(const struct _citrus_region *r, size_t pos) 79219019Sgabor{ 80219019Sgabor 81219019Sgabor return (*(uint8_t *)_citrus_region_offset(r, pos)); 82219019Sgabor} 83219019Sgabor 84219019Sgaborstatic __inline uint16_t 85219019Sgabor_citrus_region_peek16(const struct _citrus_region *r, size_t pos) 86219019Sgabor{ 87219019Sgabor uint16_t val; 88219019Sgabor 89219019Sgabor memcpy(&val, _citrus_region_offset(r, pos), (size_t)2); 90219019Sgabor return (val); 91219019Sgabor} 92219019Sgabor 93219019Sgaborstatic __inline uint32_t 94219019Sgabor_citrus_region_peek32(const struct _citrus_region *r, size_t pos) 95219019Sgabor{ 96219019Sgabor uint32_t val; 97219019Sgabor 98219019Sgabor memcpy(&val, _citrus_region_offset(r, pos), (size_t)4); 99219019Sgabor return (val); 100219019Sgabor} 101219019Sgabor 102219019Sgaborstatic __inline int 103219019Sgabor_citrus_region_get_subregion(struct _citrus_region *subr, 104219019Sgabor const struct _citrus_region *r, size_t ofs, size_t sz) 105219019Sgabor{ 106219019Sgabor 107219019Sgabor if (_citrus_region_check(r, ofs, sz)) 108219019Sgabor return (-1); 109219019Sgabor _citrus_region_init(subr, _citrus_region_offset(r, ofs), sz); 110219019Sgabor return (0); 111219019Sgabor} 112219019Sgabor 113219019Sgabor#endif 114