add bitstring
This commit is contained in:
@@ -37,17 +37,17 @@ NLSOBJS = nls/msgcat.@O@
|
||||
PTHREADOBJS = pthreads/condition.@O@
|
||||
|
||||
OBJS = @ISC_EXTRA_OBJS@ \
|
||||
assertions.@O@ base64.@O@ buffer.@O@ bufferlist.@O@ error.@O@ \
|
||||
heap.@O@ lex.@O@ lib.@O@ mem.@O@ result.@O@ \
|
||||
rwlock.@O@ serial.@O@ sockaddr.@O@ symtab.@O@ str.@O@ event.@O@ \
|
||||
task.@O@ timer.@O@ version.@O@ \
|
||||
assertions.@O@ base64.@O@ bitstring.@O@ buffer.@O@ \
|
||||
bufferlist.@O@ error.@O@ event.@O@ heap.@O@ lex.@O@ lib.@O@ \
|
||||
mem.@O@ result.@O@ rwlock.@O@ serial.@O@ sockaddr.@O@ \
|
||||
symtab.@O@ str.@O@ task.@O@ timer.@O@ version.@O@ \
|
||||
${UNIXOBJS} ${NLSOBJS} ${PTHREADOBJS}
|
||||
|
||||
SRCS = @ISC_EXTRA_SRCS@ \
|
||||
assertions.c base64.c buffer.c bufferlist.c error.c \
|
||||
heap.c lex.c lib.c mem.c result.c \
|
||||
rwlock.c sockaddr.c symtab.c str.c event.c \
|
||||
task.c timer.c version.c
|
||||
assertions.c base64.c bitstring.c buffer.c \
|
||||
bufferlist.c error.c event.c heap.c lex.c lib.c \
|
||||
mem.c result.c rwlock.c serial.c sockaddr.c \
|
||||
symtab.c str.c task.c timer.c version.c
|
||||
|
||||
LIBS = @LIBS@
|
||||
|
||||
|
||||
123
lib/isc/bitstring.c
Normal file
123
lib/isc/bitstring.c
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright (C) 1999 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <isc/types.h>
|
||||
#include <isc/assertions.h>
|
||||
#include <isc/bitstring.h>
|
||||
|
||||
#define DIV8(x) ((x) >> 3)
|
||||
#define MOD8(x) ((x) & 0x00000007U)
|
||||
#define OCTETS(n) ((n + 7) >> 3)
|
||||
#define PADDED(n) (((n + 7) >> 3) << 3)
|
||||
#define BITSET(bs, n) (((bs)->data[DIV8(n)] & \
|
||||
(1 << (7 - MOD8(n)))) != 0)
|
||||
#define SETBIT(bs, n) (bs)->data[DIV8(n)] |= (1 << (7 - MOD8(n)))
|
||||
#define CLEARBIT(bs, n) (bs)->data[DIV8(n)] &= ~(1 << (7 - MOD8(n)))
|
||||
|
||||
#define BITSTRING_MAGIC 0x42537472U /* BStr. */
|
||||
#define VALID_BITSTRING(b) ((b) != NULL && \
|
||||
(b)->magic == BITSTRING_MAGIC)
|
||||
|
||||
void
|
||||
isc_bitstring_init(isc_bitstring_t *bitstring, unsigned char *data,
|
||||
unsigned int length, unsigned int size, isc_boolean_t lsb0)
|
||||
{
|
||||
/*
|
||||
* Make 'bitstring' refer to the bitstring of 'size' bits starting
|
||||
* at 'data'. 'length' bits of the bitstring are valid. If 'lsb0'
|
||||
* is set then, bit 0 refers to the least significant bit of the
|
||||
* bitstring. Otherwise bit 0 is the most significant bit.
|
||||
*/
|
||||
|
||||
REQUIRE(bitstring != NULL);
|
||||
REQUIRE(data != NULL);
|
||||
REQUIRE(length <= size);
|
||||
|
||||
bitstring->magic = BITSTRING_MAGIC;
|
||||
bitstring->data = data;
|
||||
bitstring->length = length;
|
||||
bitstring->size = size;
|
||||
bitstring->lsb0 = lsb0;
|
||||
}
|
||||
|
||||
void
|
||||
isc_bitstring_invalidate(isc_bitstring_t *bitstring) {
|
||||
|
||||
/*
|
||||
* Invalidate 'bitstring'.
|
||||
*/
|
||||
|
||||
REQUIRE(VALID_BITSTRING(bitstring));
|
||||
|
||||
bitstring->magic = 0;
|
||||
bitstring->data = NULL;
|
||||
bitstring->length = 0;
|
||||
bitstring->size = 0;
|
||||
bitstring->lsb0 = ISC_FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
isc_bitstring_copy(isc_bitstring_t *source, unsigned int sbitpos,
|
||||
isc_bitstring_t *target, unsigned int tbitpos,
|
||||
unsigned int n)
|
||||
{
|
||||
unsigned int tlast;
|
||||
|
||||
/*
|
||||
* Starting at bit 'sbitpos', copy 'n' bits from 'source' to
|
||||
* the 'n' bits of 'target' starting at 'tbitpos'.
|
||||
*/
|
||||
|
||||
REQUIRE(VALID_BITSTRING(source));
|
||||
REQUIRE(VALID_BITSTRING(target));
|
||||
if (source->lsb0) {
|
||||
REQUIRE(sbitpos < source->length);
|
||||
sbitpos = PADDED(source->size) - sbitpos - 1;
|
||||
REQUIRE(sbitpos >= n - 1);
|
||||
sbitpos -= n - 1;
|
||||
} else
|
||||
REQUIRE(sbitpos + n <= source->length);
|
||||
tlast = tbitpos + n;
|
||||
if (target->lsb0) {
|
||||
REQUIRE(tbitpos <= target->length);
|
||||
tbitpos = PADDED(target->size) - tbitpos - 1;
|
||||
REQUIRE(tbitpos >= n - 1);
|
||||
tbitpos -= n - 1;
|
||||
} else
|
||||
REQUIRE(tlast<= target->size);
|
||||
|
||||
if (tlast > target->length)
|
||||
target->length = tlast;
|
||||
|
||||
/*
|
||||
* This is far from optimal...
|
||||
*/
|
||||
|
||||
while (n > 0) {
|
||||
if (BITSET(source, sbitpos))
|
||||
SETBIT(target, tbitpos);
|
||||
else
|
||||
CLEARBIT(target, tbitpos);
|
||||
sbitpos++;
|
||||
tbitpos++;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
111
lib/isc/include/isc/bitstring.h
Normal file
111
lib/isc/include/isc/bitstring.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 1999 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ISC_BITSTRING_H
|
||||
#define ISC_BITSTRING_H 1
|
||||
|
||||
/*****
|
||||
***** Module Info
|
||||
*****/
|
||||
|
||||
/***
|
||||
*** Imports
|
||||
***/
|
||||
|
||||
#include <isc/lang.h>
|
||||
#include <isc/types.h>
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
/***
|
||||
*** Types
|
||||
***/
|
||||
|
||||
struct isc_bitstring {
|
||||
unsigned int magic;
|
||||
unsigned char * data;
|
||||
unsigned int length;
|
||||
unsigned int size;
|
||||
isc_boolean_t lsb0;
|
||||
};
|
||||
|
||||
/***
|
||||
*** Functions
|
||||
***/
|
||||
|
||||
void
|
||||
isc_bitstring_init(isc_bitstring_t *bitstring, unsigned char *data,
|
||||
unsigned int length, unsigned int size, isc_boolean_t lsb0);
|
||||
/*
|
||||
* Make 'bitstring' refer to the bitstring of 'size' bits starting
|
||||
* at 'data'. 'length' bits of the bitstring are valid. If 'lsb0'
|
||||
* is set then, bit 0 refers to the least significant bit of the
|
||||
* bitstring. Otherwise bit 0 is the most significant bit.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'bitstring' points to a isc_bitstring_t.
|
||||
*
|
||||
* 'data' points to an array of unsigned char large enough to hold
|
||||
* 'size' bits.
|
||||
*
|
||||
* 'length' <= 'size'.
|
||||
*
|
||||
* Ensures:
|
||||
*
|
||||
* 'bitstring' is a valid bitstring.
|
||||
*/
|
||||
|
||||
void
|
||||
isc_bitstring_invalidate(isc_bitstring_t *bitstring);
|
||||
/*
|
||||
* Invalidate 'bitstring'.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'bitstring' is a valid bitstring.
|
||||
*
|
||||
* Ensures:
|
||||
*
|
||||
* 'bitstring' is not a valid bitstring.
|
||||
*/
|
||||
|
||||
void
|
||||
isc_bitstring_copy(isc_bitstring_t *source, unsigned int sbitpos,
|
||||
isc_bitstring_t *target, unsigned int tbitpos,
|
||||
unsigned int n);
|
||||
/*
|
||||
* Starting at bit 'sbitpos', copy 'n' bits from 'source' to
|
||||
* the 'n' bits of 'target' starting at 'tbitpos'.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'source' and target are valid bitstring.
|
||||
*
|
||||
* 'sbitpos' + 'n' is less than or equal to the length of 'source'.
|
||||
*
|
||||
* 'tbitpos' + 'n' is less than or equal to the size of 'target'.
|
||||
*
|
||||
* Ensures:
|
||||
*
|
||||
* The specified bits have been copied, and the length of 'target'
|
||||
* adjusted (if required).
|
||||
*/
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* ISC_BITSTRING_H */
|
||||
@@ -38,6 +38,7 @@ typedef struct isc_taskmgr isc_taskmgr_t;
|
||||
typedef struct isc_timer isc_timer_t;
|
||||
typedef struct isc_timermgr isc_timermgr_t;
|
||||
typedef struct isc_rwlock isc_rwlock_t;
|
||||
typedef struct isc_bitstring isc_bitstring_t;
|
||||
|
||||
typedef void (*isc_taskaction_t)(isc_task_t *, isc_event_t *);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user