fix (utils): fixed bitmap bounds check and added 'bitmap_T.size_bits'

This commit is contained in:
antifallobst 2023-03-03 17:23:52 +01:00
parent ede4bacff7
commit 91cd670c50
2 changed files with 9 additions and 6 deletions

View File

@ -28,6 +28,7 @@
typedef struct {
uint32_t size;
uint32_t size_bits;
uint8_t* buffer;
} bitmap_T;

View File

@ -26,15 +26,17 @@
bitmap_T bitmap_init_from_buffer(void* buffer, uint32_t size) {
bitmap_T bitmap;
bitmap.buffer = buffer;
bitmap.size = size;
bitmap.buffer = buffer;
bitmap.size = size;
bitmap.size_bits = size * 8;
return bitmap;
}
bitmap_T bitmap_init(uint32_t size) {
bitmap_T bitmap;
bitmap.size = size / 8 + 1;
bitmap.buffer = memory_allocate(bitmap.size);
bitmap.size = size / 8 + 1;
bitmap.size_bits = size;
bitmap.buffer = memory_allocate(bitmap.size);
memory_set(bitmap.buffer, 0, bitmap.size);
return bitmap;
}
@ -44,7 +46,7 @@ void bitmap_destruct(bitmap_T* bitmap) {
}
bool bitmap_set(bitmap_T* bitmap, uint32_t index, bool value) {
if (index >= bitmap->size) { return false; }
if (index >= bitmap->size_bits) { return false; }
uint32_t index_byte = index / 8;
uint8_t index_bit = index % 8;
@ -59,7 +61,7 @@ bool bitmap_set(bitmap_T* bitmap, uint32_t index, bool value) {
}
bool bitmap_get(bitmap_T* bitmap, uint32_t index) {
if (index >= bitmap->size) { return false; }
if (index >= bitmap->size_bits) { return false; }
uint32_t index_byte = index / 8;
uint8_t index_bit = index % 8;