diff options
Diffstat (limited to 'src/helper')
-rw-r--r-- | src/helper/binarybuffer.c | 8 | ||||
-rw-r--r-- | src/helper/binarybuffer.h | 2 | ||||
-rw-r--r-- | src/helper/types.h | 9 |
3 files changed, 13 insertions, 6 deletions
diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 53ad4d39..081cc037 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -54,7 +54,7 @@ void* buf_cpy(const void *from, void *_to, unsigned size) return NULL; // copy entire buffer - memcpy(_to, from, CEIL(size, 8)); + memcpy(_to, from, DIV_ROUND_UP(size, 8)); /* mask out bits that don't belong to the buffer */ unsigned trailing_bits = size % 8; @@ -191,11 +191,11 @@ char* buf_to_str(const void *_buf, unsigned buf_len, unsigned radix) return NULL; } - unsigned str_len = ceil_f_to_u32(CEIL(buf_len, 8) * factor); + unsigned str_len = ceil_f_to_u32(DIV_ROUND_UP(buf_len, 8) * factor); char *str = calloc(str_len + 1, 1); const uint8_t *buf = _buf; - int b256_len = CEIL(buf_len, 8); + int b256_len = DIV_ROUND_UP(buf_len, 8); for (int i = b256_len - 1; i >= 0; i--) { uint32_t tmp = buf[i]; @@ -300,7 +300,7 @@ int str_to_buf(const char *str, unsigned str_len, } uint8_t *buf = _buf; - for (unsigned j = 0; j < CEIL(buf_len, 8); j++) + for (unsigned j = 0; j < DIV_ROUND_UP(buf_len, 8); j++) { if (j < b256_len) buf[j] = b256_buf[j]; diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 9e0cc9b7..460d0178 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -124,8 +124,6 @@ int str_to_buf(const char *str, unsigned len, void *bin_buf, unsigned buf_size, unsigned radix); char* buf_to_str(const void *buf, unsigned size, unsigned radix); -#define CEIL(m, n) (((m) + (n) - 1) / (n)) - /* read a uint32_t from a buffer in target memory endianness */ static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le) { diff --git a/src/helper/types.h b/src/helper/types.h index a8753c5f..8f6283b5 100644 --- a/src/helper/types.h +++ b/src/helper/types.h @@ -73,6 +73,15 @@ typedef bool _Bool; (type *)( (char *)__mptr - offsetof(type,member) );}) +/** + * Rounds @c m up to the nearest multiple of @c n using division. + * @params m The value to round up to @c n. + * @params n Round @c m up to a multiple of this number. + * @returns The rounded integer value. + */ +#define DIV_ROUND_UP(m, n) (((m) + (n) - 1) / (n)) + + /* DANGER!!!! here be dragons! * * Leave these fn's as byte accesses because it is safe |