From bee5999a447880dda271926bbbd7d49dc5f4fbe9 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Sun, 14 Jul 2024 12:31:53 +0200 Subject: [PATCH] binarybuffer: str_to_buf(): rename buf_len as buf_bitsize The name 'buf_len' is misleading, as it usually refers to the byte length of a buffer. Here we use it for the length in bits. Rename it as 'buf_bitsize'. While there, fix checkpatch error by changing the index type to 'unsigned int'. Change-Id: I78855ed79a346d996d9c0100d94d14c64a36b228 Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/8395 Tested-by: jenkins Reviewed-by: Jan Matyas --- src/helper/binarybuffer.c | 14 +++++++------- src/helper/binarybuffer.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index da6e10bab..dd1449276 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -249,7 +249,7 @@ static const char *str_strip_number_prefix(const char *str, unsigned int radix) } } -int str_to_buf(const char *str, void *_buf, unsigned int buf_len) +int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize) { assert(str); @@ -314,18 +314,18 @@ int str_to_buf(const char *str, void *_buf, unsigned int buf_len) assert(tmp == 0); } - /* The result must not contain more bits than buf_len. */ + /* The result must not contain more bits than buf_bitsize. */ /* Check the whole bytes: */ - for (unsigned int j = DIV_ROUND_UP(buf_len, 8); j < b256_len; j++) { + for (unsigned int j = DIV_ROUND_UP(buf_bitsize, 8); j < b256_len; j++) { if (b256_buf[j] != 0x0) { free(b256_buf); return ERROR_NUMBER_EXCEEDS_BUFFER; } } /* Check the partial byte: */ - if (buf_len % 8) { - const uint8_t mask = 0xFFu << (buf_len % 8); - if ((b256_buf[(buf_len / 8)] & mask) != 0x0) { + if (buf_bitsize % 8) { + const uint8_t mask = 0xFFu << (buf_bitsize % 8); + if ((b256_buf[(buf_bitsize / 8)] & mask) != 0x0) { free(b256_buf); return ERROR_NUMBER_EXCEEDS_BUFFER; } @@ -333,7 +333,7 @@ int str_to_buf(const char *str, void *_buf, unsigned int buf_len) /* Copy the digits to the output buffer */ uint8_t *buf = _buf; - for (unsigned j = 0; j < DIV_ROUND_UP(buf_len, 8); j++) { + for (unsigned int j = 0; j < DIV_ROUND_UP(buf_bitsize, 8); j++) { if (j < b256_len) buf[j] = b256_buf[j]; else diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 6cff86bd9..103a48c5c 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -198,10 +198,10 @@ void *buf_set_buf(const void *src, unsigned src_start, * number is detected between decimal, hexadecimal and octal. * @param str Input number, zero-terminated string * @param _buf Output buffer, allocated by the caller - * @param buf_len Output buffer size in bits + * @param buf_bitsize Output buffer size in bits * @returns Error on invalid or overflowing number */ -int str_to_buf(const char *str, void *_buf, unsigned int buf_len); +int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize); char *buf_to_hex_str(const void *buf, unsigned size);