2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-02 15:25:22 +00:00

util: Make hexits_value() support 64-bit integers too.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
This commit is contained in:
Ben Pfaff
2014-09-30 12:45:50 -07:00
parent c8058af786
commit 0429d9599c
4 changed files with 17 additions and 17 deletions

View File

@@ -746,13 +746,14 @@ static const char *
json_lex_4hex(const char *cp, const char *end, int *valuep) json_lex_4hex(const char *cp, const char *end, int *valuep)
{ {
unsigned int value; unsigned int value;
bool ok;
if (cp + 4 > end) { if (cp + 4 > end) {
return "quoted string ends within \\u escape"; return "quoted string ends within \\u escape";
} }
value = hexits_value(cp, 4, NULL); value = hexits_value(cp, 4, &ok);
if (value == UINT_MAX) { if (!ok) {
return "malformed \\u escape"; return "malformed \\u escape";
} }
if (!value) { if (!value) {

View File

@@ -1097,8 +1097,11 @@ parse_nxm_field_name(const char *name, int name_len)
/* Check whether it's a 32-bit field header value as hex. /* Check whether it's a 32-bit field header value as hex.
* (This isn't ordinarily useful except for testing error behavior.) */ * (This isn't ordinarily useful except for testing error behavior.) */
if (name_len == 8) { if (name_len == 8) {
uint32_t header = hexits_value(name, name_len, NULL); uint32_t header;
if (header != UINT_MAX) { bool ok;
header = hexits_value(name, name_len, &ok);
if (ok) {
return header; return header;
} }
} }

View File

@@ -701,29 +701,25 @@ hexit_value(int c)
} }
/* Returns the integer value of the 'n' hexadecimal digits starting at 's', or /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
* UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is * UINTMAX_MAX if one of those "digits" is not really a hex digit. Sets '*ok'
* nonnull, '*ok' is set to true if the conversion succeeds or to false if a * to true if the conversion succeeds or to false if a non-hex digit is
* non-hex digit is detected. */ * detected. */
unsigned int uintmax_t
hexits_value(const char *s, size_t n, bool *ok) hexits_value(const char *s, size_t n, bool *ok)
{ {
unsigned int value; uintmax_t value;
size_t i; size_t i;
value = 0; value = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
int hexit = hexit_value(s[i]); int hexit = hexit_value(s[i]);
if (hexit < 0) { if (hexit < 0) {
if (ok) {
*ok = false; *ok = false;
} return UINTMAX_MAX;
return UINT_MAX;
} }
value = (value << 4) + hexit; value = (value << 4) + hexit;
} }
if (ok) {
*ok = true; *ok = true;
}
return value; return value;
} }

View File

@@ -319,7 +319,7 @@ bool ovs_scan(const char *s, const char *format, ...) SCANF_FORMAT(2, 3);
bool str_to_double(const char *, double *); bool str_to_double(const char *, double *);
int hexit_value(int c); int hexit_value(int c);
unsigned int hexits_value(const char *s, size_t n, bool *ok); uintmax_t hexits_value(const char *s, size_t n, bool *ok);
const char *english_list_delimiter(size_t index, size_t total); const char *english_list_delimiter(size_t index, size_t total);