Rizin
unix-like reverse engineering framework and cli tools
bitmap.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2020 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2017-2020 crowell <crowell@bu.edu>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_util.h>
6 
7 #define BITMAP_TEST 0
8 
9 #define BITWORD_BITS (sizeof(RBitword) * 8)
10 #define BITWORD_BITS_MASK (BITWORD_BITS - 1)
11 #define BITWORD_MULT(bit) (((bit) + (BITWORD_BITS_MASK)) & ~(BITWORD_BITS_MASK))
12 #define BITWORD_TEST(x, y) (((x) >> (y)) & 1)
13 
14 #define BITMAP_WORD_COUNT(bit) (BITWORD_MULT(bit) >> BITWORD_BITS_SHIFT)
15 
18  if (!b) {
19  return NULL;
20  }
21  b->length = len;
22  b->bitmap = calloc(BITMAP_WORD_COUNT(len), sizeof(RBitword));
23  return b;
24 }
25 
27  if (b->length < len) {
28  len = b->length;
29  }
30  memcpy(b->bitmap, buf, len);
31 }
32 
34  free(b->bitmap);
35  free(b);
36 }
37 
38 RZ_API void rz_bitmap_set(RzBitmap *b, size_t bit) {
39  if (bit < b->length) {
40  b->bitmap[(bit >> BITWORD_BITS_SHIFT)] |=
41  ((RBitword)1 << (bit & BITWORD_BITS_MASK));
42  }
43 }
44 
46  if (bit < b->length) {
47  b->bitmap[(bit >> BITWORD_BITS_SHIFT)] &=
48  ~((RBitword)1 << (bit & BITWORD_BITS_MASK));
49  }
50 }
51 
53  if (bit < b->length) {
54  RBitword bword = b->bitmap[(bit >> BITWORD_BITS_SHIFT)];
55  return BITWORD_TEST(bword, (bit & BITWORD_BITS_MASK));
56  }
57  return -1;
58 }
size_t len
Definition: 6502dis.c:15
#define BITWORD_BITS_MASK
Definition: bitmap.c:10
RZ_API RzBitmap * rz_bitmap_new(size_t len)
Definition: bitmap.c:16
RZ_API void rz_bitmap_set_bytes(RzBitmap *b, const ut8 *buf, int len)
Definition: bitmap.c:26
RZ_API void rz_bitmap_set(RzBitmap *b, size_t bit)
Definition: bitmap.c:38
#define BITWORD_TEST(x, y)
Definition: bitmap.c:12
RZ_API int rz_bitmap_test(RzBitmap *b, size_t bit)
Definition: bitmap.c:52
RZ_API void rz_bitmap_free(RzBitmap *b)
Definition: bitmap.c:33
RZ_API void rz_bitmap_unset(RzBitmap *b, size_t bit)
Definition: bitmap.c:45
#define BITMAP_WORD_COUNT(bit)
Definition: bitmap.c:14
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RzCryptoSelector bit
Definition: crypto.c:16
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define RBitword
Definition: rz_bitmap.h:11
#define BITWORD_BITS_SHIFT
Definition: rz_bitmap.h:10
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define b(i)
Definition: sha256.c:42