libwbclient: Make wbcGuidToString not use talloc
[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 "replace.h"
26 #include "libwbclient.h"
27
28 /* Convert a binary GUID to a character string */
29 wbcErr wbcGuidToString(const struct wbcGuid *guid,
30                        char **guid_string)
31 {
32         char *result;
33
34         result = (char *)wbcAllocateMemory(37, 1, NULL);
35         if (result == NULL) {
36                 return WBC_ERR_NO_MEMORY;
37         }
38         snprintf(result, 37,
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         *guid_string = result;
48
49         return WBC_ERR_SUCCESS;
50 }
51
52 /* @brief Convert a character string to a binary GUID */
53 wbcErr wbcStringToGuid(const char *str,
54                        struct wbcGuid *guid)
55 {
56         uint32_t time_low;
57         uint32_t time_mid, time_hi_and_version;
58         uint32_t clock_seq[2];
59         uint32_t node[6];
60         int i;
61         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
62
63         if (!guid) {
64                 wbc_status = WBC_ERR_INVALID_PARAM;
65                 BAIL_ON_WBC_ERROR(wbc_status);
66         }
67
68         if (!str) {
69                 wbc_status = WBC_ERR_INVALID_PARAM;
70                 BAIL_ON_WBC_ERROR(wbc_status);
71         }
72
73         if (11 == sscanf(str, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
74                          &time_low, &time_mid, &time_hi_and_version,
75                          &clock_seq[0], &clock_seq[1],
76                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
77                 wbc_status = WBC_ERR_SUCCESS;
78         } else if (11 == sscanf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
79                                 &time_low, &time_mid, &time_hi_and_version,
80                                 &clock_seq[0], &clock_seq[1],
81                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
82                 wbc_status = WBC_ERR_SUCCESS;
83         }
84
85         BAIL_ON_WBC_ERROR(wbc_status);
86
87         guid->time_low = time_low;
88         guid->time_mid = time_mid;
89         guid->time_hi_and_version = time_hi_and_version;
90         guid->clock_seq[0] = clock_seq[0];
91         guid->clock_seq[1] = clock_seq[1];
92
93         for (i=0;i<6;i++) {
94                 guid->node[i] = node[i];
95         }
96
97         wbc_status = WBC_ERR_SUCCESS;
98
99 done:
100         return wbc_status;
101 }