Merge branch 'master' of ssh://git.samba.org/data/git/samba
[samba.git] / nsswitch / libwbclient / wbc_guid.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind client API
5
6    Copyright (C) Gerald (Jerry) Carter 2007
7
8
9    This library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public
11    License as published by the Free Software Foundation; either
12    version 3 of the License, or (at your option) any later version.
13
14    This library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Library General Public License for more details.
18
19    You should have received a copy of the GNU Lesser General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /* Required Headers */
24
25 #include "libwbclient.h"
26
27 /* Convert a binary GUID to a character string */
28 wbcErr wbcGuidToString(const struct wbcGuid *guid,
29                        char **guid_string)
30 {
31         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
32
33         if (!guid) {
34                 wbc_status = WBC_ERR_INVALID_PARAM;
35                 BAIL_ON_WBC_ERROR(wbc_status);
36         }
37
38         *guid_string = talloc_asprintf(NULL,
39                                        "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
40                                        guid->time_low, guid->time_mid,
41                                        guid->time_hi_and_version,
42                                        guid->clock_seq[0],
43                                        guid->clock_seq[1],
44                                        guid->node[0], guid->node[1],
45                                        guid->node[2], guid->node[3],
46                                        guid->node[4], guid->node[5]);
47         BAIL_ON_PTR_ERROR((*guid_string), wbc_status);
48
49         wbc_status = WBC_ERR_SUCCESS;
50
51 done:
52         return wbc_status;
53 }
54
55 /* @brief Convert a character string to a binary GUID */
56 wbcErr wbcStringToGuid(const char *str,
57                        struct wbcGuid *guid)
58 {
59         uint32_t time_low;
60         uint32_t time_mid, time_hi_and_version;
61         uint32_t clock_seq[2];
62         uint32_t node[6];
63         int i;
64         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
65
66         if (!guid) {
67                 wbc_status = WBC_ERR_INVALID_PARAM;
68                 BAIL_ON_WBC_ERROR(wbc_status);
69         }
70
71         if (!str) {
72                 wbc_status = WBC_ERR_INVALID_PARAM;
73                 BAIL_ON_WBC_ERROR(wbc_status);
74         }
75
76         if (11 == sscanf(str, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
77                          &time_low, &time_mid, &time_hi_and_version,
78                          &clock_seq[0], &clock_seq[1],
79                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
80                 wbc_status = WBC_ERR_SUCCESS;
81         } else if (11 == sscanf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
82                                 &time_low, &time_mid, &time_hi_and_version,
83                                 &clock_seq[0], &clock_seq[1],
84                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
85                 wbc_status = WBC_ERR_SUCCESS;
86         }
87
88         BAIL_ON_WBC_ERROR(wbc_status);
89
90         guid->time_low = time_low;
91         guid->time_mid = time_mid;
92         guid->time_hi_and_version = time_hi_and_version;
93         guid->clock_seq[0] = clock_seq[0];
94         guid->clock_seq[1] = clock_seq[1];
95
96         for (i=0;i<6;i++) {
97                 guid->node[i] = node[i];
98         }
99
100         wbc_status = WBC_ERR_SUCCESS;
101
102 done:
103         return wbc_status;
104 }