debug: factor out a function that opens and closes the new and old logfile
[samba.git] / lib / util / util_str_hex.c
1 #include "replace.h"
2 #include "libcli/util/ntstatus.h"
3 #include "util_str_hex.h"
4
5 NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest)
6 {
7         uint64_t x = 0;
8         uint i;
9         char c;
10
11         if ((hexchars & 1) || hexchars > 16) {
12                 return NT_STATUS_INVALID_PARAMETER;
13         }
14
15         for (i = 0; i < hexchars; i++) {
16                 x <<= 4;
17                 c = s[i];
18                 if (c >= '0' && c <= '9') {
19                         x += c - '0';
20                 }
21                 else if (c >= 'a' && c <= 'f') {
22                         x += c - 'a' + 10;
23                 }
24                 else if (c >= 'A' && c <= 'F') {
25                         x += c - 'A' + 10;
26                 }
27                 else {
28                         /* BAD character (including '\0') */
29                         return NT_STATUS_INVALID_PARAMETER;
30                 }
31         }
32         *dest = x;
33         return NT_STATUS_OK;
34 }
35
36
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])
43 {
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
54         */
55         status = read_hex_bytes(s, 8, &tmp);
56         if (!NT_STATUS_IS_OK(status) || s[8] != '-') {
57                 return NT_STATUS_INVALID_PARAMETER;
58         }
59         *time_low = tmp;
60         s += 9;
61
62         status = read_hex_bytes(s, 4, &tmp);
63         if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
64                 return NT_STATUS_INVALID_PARAMETER;
65         }
66         *time_mid = tmp;
67         s += 5;
68
69         status = read_hex_bytes(s, 4, &tmp);
70         if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
71                 return NT_STATUS_INVALID_PARAMETER;
72         }
73         *time_hi_and_version = tmp;
74         s += 5;
75
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;
80                 }
81                 clock_seq[i] = tmp;
82                 s += 2;
83         }
84         if (s[0] != '-') {
85                 return NT_STATUS_INVALID_PARAMETER;
86         }
87
88         s++;
89
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;
94                 }
95                 node[i] = tmp;
96                 s += 2;
97         }
98
99         return NT_STATUS_OK;
100 }