diff options
author | Volker Lendecke <vl@samba.org> | 2021-01-24 09:15:29 +0100 |
---|---|---|
committer | Volker Lendecke <vl@samba.org> | 2021-01-28 16:58:35 +0000 |
commit | 2b9ba992b610ebc600f573d8b3f46568d5347cec (patch) | |
tree | dd59c52ad5a956cea36e207ce3c38f255eae6f29 /lib/util | |
parent | 522ef9e6a57ba46c6c097c088deb27fe12f3b300 (diff) | |
download | samba-2b9ba992b610ebc600f573d8b3f46568d5347cec.tar.gz |
lib: Simplify parse_guid_string() and ndr_syntax_id_from_string()
Return "bool" instead of NTSTATUS, use hex_byte() instead of
read_hex_bytes(). And parse directly into a struct GUID instead of the
components. 99 lines less code.
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Samuel Cabrero <scabrero@samba.org>
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/util_str_hex.c | 101 | ||||
-rw-r--r-- | lib/util/util_str_hex.h | 13 |
2 files changed, 39 insertions, 75 deletions
diff --git a/lib/util/util_str_hex.c b/lib/util/util_str_hex.c index 792b4e8420b..553fb30681e 100644 --- a/lib/util/util_str_hex.c +++ b/lib/util/util_str_hex.c @@ -1,48 +1,27 @@ #include "replace.h" -#include "libcli/util/ntstatus.h" #include "util_str_hex.h" +#include "lib/util/data_blob.h" +#include "librpc/gen_ndr/misc.h" -NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest) +static bool hex_uint16(const char *in, uint16_t *out) { - uint64_t x = 0; - uint i; - char c; - - if ((hexchars & 1) || hexchars > 16) { - return NT_STATUS_INVALID_PARAMETER; - } - - for (i = 0; i < hexchars; i++) { - x <<= 4; - c = s[i]; - if (c >= '0' && c <= '9') { - x += c - '0'; - } - else if (c >= 'a' && c <= 'f') { - x += c - 'a' + 10; - } - else if (c >= 'A' && c <= 'F') { - x += c - 'A' + 10; - } - else { - /* BAD character (including '\0') */ - return NT_STATUS_INVALID_PARAMETER; - } - } - *dest = x; - return NT_STATUS_OK; + uint8_t hi=0, lo=0; + bool ok = hex_byte(in, &hi) && hex_byte(in+2, &lo); + *out = (((uint16_t)hi)<<8) + lo; + return ok; } +bool hex_uint32(const char *in, uint32_t *out) +{ + uint16_t hi=0, lo=0; + bool ok = hex_uint16(in, &hi) && hex_uint16(in+4, &lo); + *out = (((uint32_t)hi)<<16) + lo; + return ok; +} -NTSTATUS parse_guid_string(const char *s, - uint32_t *time_low, - uint32_t *time_mid, - uint32_t *time_hi_and_version, - uint32_t clock_seq[2], - uint32_t node[6]) +bool parse_guid_string(const char *s, struct GUID *guid) { - uint64_t tmp; - NTSTATUS status; + bool ok; int i; /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd" | | | | | @@ -52,49 +31,39 @@ NTSTATUS parse_guid_string(const char *s, | \_______________ time_mid \_____________________ time_low */ - status = read_hex_bytes(s, 8, &tmp); - if (!NT_STATUS_IS_OK(status) || s[8] != '-') { - return NT_STATUS_INVALID_PARAMETER; + + ok = hex_uint32(s, &guid->time_low); + if (!ok || (s[8] != '-')) { + return false; } - *time_low = tmp; s += 9; - status = read_hex_bytes(s, 4, &tmp); - if (!NT_STATUS_IS_OK(status) || s[4] != '-') { - return NT_STATUS_INVALID_PARAMETER; + ok = hex_uint16(s, &guid->time_mid); + if (!ok || (s[4] != '-')) { + return false; } - *time_mid = tmp; s += 5; - status = read_hex_bytes(s, 4, &tmp); - if (!NT_STATUS_IS_OK(status) || s[4] != '-') { - return NT_STATUS_INVALID_PARAMETER; + ok = hex_uint16(s, &guid->time_hi_and_version); + if (!ok || (s[4] != '-')) { + return false; } - *time_hi_and_version = tmp; s += 5; - for (i = 0; i < 2; i++) { - status = read_hex_bytes(s, 2, &tmp); - if (!NT_STATUS_IS_OK(status)) { - return NT_STATUS_INVALID_PARAMETER; - } - clock_seq[i] = tmp; - s += 2; - } - if (s[0] != '-') { - return NT_STATUS_INVALID_PARAMETER; + ok = hex_byte(s, &guid->clock_seq[0]) && + hex_byte(s+2, &guid->clock_seq[1]); + if (!ok || (s[4] != '-')) { + return false; } - - s++; + s += 5; for (i = 0; i < 6; i++) { - status = read_hex_bytes(s, 2, &tmp); - if (!NT_STATUS_IS_OK(status)) { - return NT_STATUS_INVALID_PARAMETER; + ok = hex_byte(s, &guid->node[i]); + if (!ok) { + return false; } - node[i] = tmp; s += 2; } - return NT_STATUS_OK; + return true; } diff --git a/lib/util/util_str_hex.h b/lib/util/util_str_hex.h index d0d53e3ed05..7dd527fcdab 100644 --- a/lib/util/util_str_hex.h +++ b/lib/util/util_str_hex.h @@ -1,10 +1,5 @@ -#include "../libcli/util/ntstatus.h" +#include "replace.h" -NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest); - -NTSTATUS parse_guid_string(const char *s, - uint32_t *time_low, - uint32_t *time_mid, - uint32_t *time_hi_and_version, - uint32_t clock_seq[2], - uint32_t node[6]); +bool hex_uint32(const char *in, uint32_t *out); +struct GUID; +bool parse_guid_string(const char *s, struct GUID *guid); |