1 /* $OpenBSD: bitmap.c,v 1.9 2017/10/20 01:56:39 djm Exp $ */
3 * Copyright (c) 2015 Damien Miller <djm@mindrot.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
26 #define BITMAP_WTYPE u_int
27 #define BITMAP_MAX (1<<24)
28 #define BITMAP_BYTES (sizeof(BITMAP_WTYPE))
29 #define BITMAP_BITS (sizeof(BITMAP_WTYPE) * 8)
30 #define BITMAP_WMASK ((BITMAP_WTYPE)BITMAP_BITS - 1)
33 size_t len; /* number of words allocated */
34 size_t top; /* index of top word allocated */
42 if ((ret = calloc(1, sizeof(*ret))) == NULL)
44 if ((ret->d = calloc(1, BITMAP_BYTES)) == NULL) {
54 bitmap_free(struct bitmap *b)
56 if (b != NULL && b->d != NULL) {
65 bitmap_zero(struct bitmap *b)
67 memset(b->d, 0, b->len * BITMAP_BYTES);
72 bitmap_test_bit(struct bitmap *b, u_int n)
75 return 0; /* invalid */
76 if (b->len == 0 || (n / BITMAP_BITS) > b->top)
78 return (b->d[n / BITMAP_BITS] >> (n & BITMAP_WMASK)) & 1;
82 reserve(struct bitmap *b, u_int n)
87 if (b->top >= b->len || n > BITMAP_MAX)
88 return -1; /* invalid */
89 nlen = (n / BITMAP_BITS) + 1;
91 if ((tmp = recallocarray(b->d, b->len,
92 nlen, BITMAP_BYTES)) == NULL)
101 bitmap_set_bit(struct bitmap *b, u_int n)
106 if ((r = reserve(b, n)) != 0)
108 offset = n / BITMAP_BITS;
111 b->d[offset] |= (BITMAP_WTYPE)1 << (n & BITMAP_WMASK);
115 /* Resets b->top to point to the most significant bit set in b->d */
117 retop(struct bitmap *b)
119 if (b->top >= b->len)
121 while (b->top > 0 && b->d[b->top] == 0)
126 bitmap_clear_bit(struct bitmap *b, u_int n)
130 if (b->top >= b->len || n > BITMAP_MAX)
131 return; /* invalid */
132 offset = n / BITMAP_BITS;
135 b->d[offset] &= ~((BITMAP_WTYPE)1 << (n & BITMAP_WMASK));
136 /* The top may have changed as a result of the clear */
141 bitmap_nbits(struct bitmap *b)
147 if (b->top >= b->len)
148 return 0; /* invalid */
149 if (b->len == 0 || (b->top == 0 && b->d[0] == 0))
153 bits = (b->top + 1) * BITMAP_BITS;
154 while (!(w & ((BITMAP_WTYPE)1 << (BITMAP_BITS - 1)))) {
162 bitmap_nbytes(struct bitmap *b)
164 return (bitmap_nbits(b) + 7) / 8;
168 bitmap_to_string(struct bitmap *b, void *p, size_t l)
170 u_char *s = (u_char *)p;
171 size_t i, j, k, need = bitmap_nbytes(b);
173 if (l < need || b->top >= b->len)
177 /* Put the bytes from LSB backwards */
178 for (i = k = 0; i < b->top + 1; i++) {
179 for (j = 0; j < BITMAP_BYTES; j++) {
182 s[need - 1 - k++] = (b->d[i] >> (j * 8)) & 0xff;
189 bitmap_from_string(struct bitmap *b, const void *p, size_t l)
192 size_t i, offset, shift;
193 const u_char *s = (const u_char *)p;
195 if (l > BITMAP_MAX / 8)
197 if ((r = reserve(b, l * 8)) != 0)
202 b->top = offset = ((l + (BITMAP_BYTES - 1)) / BITMAP_BYTES) - 1;
203 shift = ((l + (BITMAP_BYTES - 1)) % BITMAP_BYTES) * 8;
204 for (i = 0; i < l; i++) {
205 b->d[offset] |= (BITMAP_WTYPE)s[i] << shift;
208 shift = BITMAP_BITS - 8;