From: openocd-gerrit <ope...@us...> - 2024-09-07 11:37:50
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Main OpenOCD repository". The branch, master has been updated via 4680d6ebdf142f9dd1acdc439d4e146ed36a290b (commit) from e09bb72da50c9a9878565af23ee8d5465c11526d (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 4680d6ebdf142f9dd1acdc439d4e146ed36a290b Author: Antonio Borneo <bor...@gm...> Date: Sun Aug 25 15:38:07 2024 +0200 binarybuffer: str_to_buf(): align prefix to TCL syntax Integer values are interpreted by TCL as decimal, binary, octal or hexadecimal if prepended with '0d', '0b', '0o' or '0x' respectively. The case of '0' prefix has been interpreted as octal till TCL 8.6 but is interpreted as part of a decimal number by JimTCL and from TCL 9. Align str_to_buf() to latest TCL syntax by: - addding support for '0d', '0b' and '0o' prefix; - dropping support for '0' prefix. Change-Id: I708ef72146d75b7bf429df329a0269cf48700a44 Signed-off-by: Antonio Borneo <bor...@gm...> Reviewed-on: https://review.openocd.org/c/openocd/+/8465 Tested-by: jenkins Reviewed-by: Jan Matyas <jan...@co...> diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 509f24f0b..423739a9d 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -192,50 +192,12 @@ char *buf_to_hex_str(const void *_buf, unsigned buf_len) return str; } -static bool str_has_hex_prefix(const char *s) -{ - /* Starts with "0x" or "0X" */ - return (s[0] == '0') && (s[1] == 'x' || s[1] == 'X'); -} - -static bool str_has_octal_prefix(const char *s) -{ - /* - starts with '0', - * - has at least two characters, and - * - the second character is not 'x' or 'X' */ - return (s[0] == '0') && (s[1] != '\0') && (s[1] != 'x') && (s[1] != 'X'); -} - -/** - * Try to identify the radix of the number by looking at its prefix. - * No further validation of the number is preformed. +/* + * TCL standard prefix is '0b', '0o', '0d' or '0x' respectively for binary, + * octal, decimal or hexadecimal. + * The prefix '0' is interpreted by TCL <= 8.6 as octal, but is ignored and + * interpreted as part of a decimal number by JimTCL and by TCL >= 9. */ -static unsigned int str_radix_guess(const char *str) -{ - if (str_has_hex_prefix(str)) - return 16; - - if (str_has_octal_prefix(str)) - return 8; - - /* Otherwise assume a decimal number. */ - return 10; -} - -/** Strip leading "0x" or "0X" from hex numbers or "0" from octal numbers. */ -static const char *str_strip_number_prefix(const char *str, unsigned int radix) -{ - switch (radix) { - case 16: - return str + 2; - case 8: - return str + 1; - case 10: - default: - return str; - } -} - int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize) { assert(str); @@ -243,9 +205,34 @@ int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize) assert(buf_bitsize > 0); uint8_t *buf = _buf; - unsigned int radix = str_radix_guess(str); - - str = str_strip_number_prefix(str, radix); + unsigned int radix = 10; /* default when no prefix */ + + if (str[0] == '0') { + switch (str[1]) { + case 'b': + case 'B': + radix = 2; + str += 2; + break; + case 'o': + case 'O': + radix = 8; + str += 2; + break; + case 'd': + case 'D': + radix = 10; + str += 2; + break; + case 'x': + case 'X': + radix = 16; + str += 2; + break; + default: + break; + } + } const size_t str_len = strlen(str); if (str_len == 0) ----------------------------------------------------------------------- Summary of changes: src/helper/binarybuffer.c | 79 ++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 46 deletions(-) hooks/post-receive -- Main OpenOCD repository |