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 <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8395
Tested-by: jenkins
Reviewed-by: Jan Matyas <jan.matyas@codasip.com>
This commit is contained in:
Antonio Borneo 2024-07-14 12:31:53 +02:00
parent 8a3efbf21f
commit bee5999a44
2 changed files with 9 additions and 9 deletions

View File

@ -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); assert(str);
@ -314,18 +314,18 @@ int str_to_buf(const char *str, void *_buf, unsigned int buf_len)
assert(tmp == 0); 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: */ /* 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) { if (b256_buf[j] != 0x0) {
free(b256_buf); free(b256_buf);
return ERROR_NUMBER_EXCEEDS_BUFFER; return ERROR_NUMBER_EXCEEDS_BUFFER;
} }
} }
/* Check the partial byte: */ /* Check the partial byte: */
if (buf_len % 8) { if (buf_bitsize % 8) {
const uint8_t mask = 0xFFu << (buf_len % 8); const uint8_t mask = 0xFFu << (buf_bitsize % 8);
if ((b256_buf[(buf_len / 8)] & mask) != 0x0) { if ((b256_buf[(buf_bitsize / 8)] & mask) != 0x0) {
free(b256_buf); free(b256_buf);
return ERROR_NUMBER_EXCEEDS_BUFFER; 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 */ /* Copy the digits to the output buffer */
uint8_t *buf = _buf; 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) if (j < b256_len)
buf[j] = b256_buf[j]; buf[j] = b256_buf[j];
else else

View File

@ -198,10 +198,10 @@ void *buf_set_buf(const void *src, unsigned src_start,
* number is detected between decimal, hexadecimal and octal. * number is detected between decimal, hexadecimal and octal.
* @param str Input number, zero-terminated string * @param str Input number, zero-terminated string
* @param _buf Output buffer, allocated by the caller * @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 * @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); char *buf_to_hex_str(const void *buf, unsigned size);