s3/selftest: Move samba3.raw.read to ad_dc_smb1
[Samba.git] / lib / util / util_str_hex.c
blob792b4e8420bd9ecbf8776139c287d064f42ac198
1 #include "replace.h"
2 #include "libcli/util/ntstatus.h"
3 #include "util_str_hex.h"
5 NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest)
7 uint64_t x = 0;
8 uint i;
9 char c;
11 if ((hexchars & 1) || hexchars > 16) {
12 return NT_STATUS_INVALID_PARAMETER;
15 for (i = 0; i < hexchars; i++) {
16 x <<= 4;
17 c = s[i];
18 if (c >= '0' && c <= '9') {
19 x += c - '0';
21 else if (c >= 'a' && c <= 'f') {
22 x += c - 'a' + 10;
24 else if (c >= 'A' && c <= 'F') {
25 x += c - 'A' + 10;
27 else {
28 /* BAD character (including '\0') */
29 return NT_STATUS_INVALID_PARAMETER;
32 *dest = x;
33 return NT_STATUS_OK;
37 NTSTATUS parse_guid_string(const char *s,
38 uint32_t *time_low,
39 uint32_t *time_mid,
40 uint32_t *time_hi_and_version,
41 uint32_t clock_seq[2],
42 uint32_t node[6])
44 uint64_t tmp;
45 NTSTATUS status;
46 int i;
47 /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd"
48 | | | | |
49 | | | | \ node[6]
50 | | | \_____ clock_seq[2]
51 | | \__________ time_hi_and_version
52 | \_______________ time_mid
53 \_____________________ time_low
55 status = read_hex_bytes(s, 8, &tmp);
56 if (!NT_STATUS_IS_OK(status) || s[8] != '-') {
57 return NT_STATUS_INVALID_PARAMETER;
59 *time_low = tmp;
60 s += 9;
62 status = read_hex_bytes(s, 4, &tmp);
63 if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
64 return NT_STATUS_INVALID_PARAMETER;
66 *time_mid = tmp;
67 s += 5;
69 status = read_hex_bytes(s, 4, &tmp);
70 if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
71 return NT_STATUS_INVALID_PARAMETER;
73 *time_hi_and_version = tmp;
74 s += 5;
76 for (i = 0; i < 2; i++) {
77 status = read_hex_bytes(s, 2, &tmp);
78 if (!NT_STATUS_IS_OK(status)) {
79 return NT_STATUS_INVALID_PARAMETER;
81 clock_seq[i] = tmp;
82 s += 2;
84 if (s[0] != '-') {
85 return NT_STATUS_INVALID_PARAMETER;
88 s++;
90 for (i = 0; i < 6; i++) {
91 status = read_hex_bytes(s, 2, &tmp);
92 if (!NT_STATUS_IS_OK(status)) {
93 return NT_STATUS_INVALID_PARAMETER;
95 node[i] = tmp;
96 s += 2;
99 return NT_STATUS_OK;