1/* $NetBSD: bitstring.c,v 1.1.1.1 2009/12/13 16:54:14 kardel Exp $ */ 2 3/* 4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1999-2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20/* Id: bitstring.c,v 1.17 2007/06/19 23:47:17 tbox Exp */ 21 22/*! \file */ 23 24#include <config.h> 25 26#include <stddef.h> 27 28#include <isc/magic.h> 29#include <isc/bitstring.h> 30#include <isc/util.h> 31 32#define DIV8(x) ((x) >> 3) 33#define MOD8(x) ((x) & 0x00000007U) 34#define OCTETS(n) (((n) + 7) >> 3) 35#define PADDED(n) ((((n) + 7) >> 3) << 3) 36#define BITSET(bs, n) (((bs)->data[DIV8(n)] & \ 37 (1 << (7 - MOD8(n)))) != 0) 38#define SETBIT(bs, n) (bs)->data[DIV8(n)] |= (1 << (7 - MOD8(n))) 39#define CLEARBIT(bs, n) (bs)->data[DIV8(n)] &= ~(1 << (7 - MOD8(n))) 40 41#define BITSTRING_MAGIC ISC_MAGIC('B', 'S', 't', 'r') 42#define VALID_BITSTRING(b) ISC_MAGIC_VALID(b, BITSTRING_MAGIC) 43 44void 45isc_bitstring_init(isc_bitstring_t *bitstring, unsigned char *data, 46 unsigned int length, unsigned int size, isc_boolean_t lsb0) 47{ 48 /* 49 * Make 'bitstring' refer to the bitstring of 'size' bits starting 50 * at 'data'. 'length' bits of the bitstring are valid. If 'lsb0' 51 * is set then, bit 0 refers to the least significant bit of the 52 * bitstring. Otherwise bit 0 is the most significant bit. 53 */ 54 55 REQUIRE(bitstring != NULL); 56 REQUIRE(data != NULL); 57 REQUIRE(length <= size); 58 59 bitstring->magic = BITSTRING_MAGIC; 60 bitstring->data = data; 61 bitstring->length = length; 62 bitstring->size = size; 63 bitstring->lsb0 = lsb0; 64} 65 66void 67isc_bitstring_invalidate(isc_bitstring_t *bitstring) { 68 69 /* 70 * Invalidate 'bitstring'. 71 */ 72 73 REQUIRE(VALID_BITSTRING(bitstring)); 74 75 bitstring->magic = 0; 76 bitstring->data = NULL; 77 bitstring->length = 0; 78 bitstring->size = 0; 79 bitstring->lsb0 = ISC_FALSE; 80} 81 82void 83isc_bitstring_copy(isc_bitstring_t *source, unsigned int sbitpos, 84 isc_bitstring_t *target, unsigned int tbitpos, 85 unsigned int n) 86{ 87 unsigned int tlast; 88 89 /* 90 * Starting at bit 'sbitpos', copy 'n' bits from 'source' to 91 * the 'n' bits of 'target' starting at 'tbitpos'. 92 */ 93 94 REQUIRE(VALID_BITSTRING(source)); 95 REQUIRE(VALID_BITSTRING(target)); 96 REQUIRE(source->lsb0 == target->lsb0); 97 if (source->lsb0) { 98 REQUIRE(sbitpos <= source->length); 99 sbitpos = PADDED(source->size) - sbitpos; 100 REQUIRE(sbitpos >= n); 101 sbitpos -= n; 102 } else 103 REQUIRE(sbitpos + n <= source->length); 104 tlast = tbitpos + n; 105 if (target->lsb0) { 106 REQUIRE(tbitpos <= target->length); 107 tbitpos = PADDED(target->size) - tbitpos; 108 REQUIRE(tbitpos >= n); 109 tbitpos -= n; 110 } else 111 REQUIRE(tlast <= target->size); 112 113 if (tlast > target->length) 114 target->length = tlast; 115 116 /* 117 * This is far from optimal... 118 */ 119 120 while (n > 0) { 121 if (BITSET(source, sbitpos)) 122 SETBIT(target, tbitpos); 123 else 124 CLEARBIT(target, tbitpos); 125 sbitpos++; 126 tbitpos++; 127 n--; 128 } 129} 130