1 | /* $Id: uuid-win.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT UUID (unique identifiers) handling (win32 host).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_UUID
|
---|
36 | #include <Windows.h>
|
---|
37 |
|
---|
38 | #include <iprt/uuid.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Generates a new UUID value.
|
---|
46 | *
|
---|
47 | * @returns iprt status code.
|
---|
48 | * @param pUuid Where to store generated uuid.
|
---|
49 | */
|
---|
50 | RTDECL(int) RTUuidCreate(PRTUUID pUuid)
|
---|
51 | {
|
---|
52 | /* check params */
|
---|
53 | if (pUuid == NULL)
|
---|
54 | {
|
---|
55 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
56 | return VERR_INVALID_PARAMETER;
|
---|
57 | }
|
---|
58 |
|
---|
59 | RPC_STATUS rc = UuidCreate((UUID *)pUuid);
|
---|
60 | if ((rc == RPC_S_OK) || (rc == RPC_S_UUID_LOCAL_ONLY))
|
---|
61 | return VINF_SUCCESS;
|
---|
62 |
|
---|
63 | /* error exit */
|
---|
64 | return RTErrConvertFromWin32(rc);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Makes null UUID value.
|
---|
69 | *
|
---|
70 | * @returns iprt status code.
|
---|
71 | * @param pUuid Where to store generated null uuid.
|
---|
72 | */
|
---|
73 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
74 | {
|
---|
75 | /* check params */
|
---|
76 | if (pUuid == NULL)
|
---|
77 | {
|
---|
78 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
79 | return VERR_INVALID_PARAMETER;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Checks if UUID is null.
|
---|
87 | *
|
---|
88 | * @returns true if UUID is null.
|
---|
89 | * @param pUuid uuid to check.
|
---|
90 | */
|
---|
91 | RTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
|
---|
92 | {
|
---|
93 | /* check params */
|
---|
94 | if (pUuid == NULL)
|
---|
95 | {
|
---|
96 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
97 | return TRUE;
|
---|
98 | }
|
---|
99 |
|
---|
100 | RPC_STATUS status;
|
---|
101 | return UuidIsNil((UUID *)pUuid, &status);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Compares two UUID values.
|
---|
106 | *
|
---|
107 | * @returns 0 if eq, < 0 or > 0.
|
---|
108 | * @param pUuid1 First value to compare.
|
---|
109 | * @param pUuid2 Second value to compare.
|
---|
110 | */
|
---|
111 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
112 | {
|
---|
113 | /* check params */
|
---|
114 | if ((pUuid1 == NULL) || (pUuid2 == NULL))
|
---|
115 | {
|
---|
116 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | RPC_STATUS status;
|
---|
121 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Converts binary UUID to its string representation.
|
---|
126 | *
|
---|
127 | * @returns iprt status code.
|
---|
128 | * @param pUuid Uuid to convert.
|
---|
129 | * @param pszString Where to store result string.
|
---|
130 | * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
|
---|
131 | */
|
---|
132 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
|
---|
133 | {
|
---|
134 | /* check params */
|
---|
135 | if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
|
---|
136 | {
|
---|
137 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
138 | return VERR_INVALID_PARAMETER;
|
---|
139 | }
|
---|
140 |
|
---|
141 | RPC_STATUS rc;
|
---|
142 | char *pStr = NULL;
|
---|
143 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
144 | /* always use ASCII version! */
|
---|
145 | rc = UuidToStringA((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
146 | #else
|
---|
147 | rc = UuidToString((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
148 | #endif
|
---|
149 | if (rc != RPC_S_OK)
|
---|
150 | return RTErrConvertFromWin32(rc);
|
---|
151 |
|
---|
152 | if (strlen(pStr) >= cchString)
|
---|
153 | {
|
---|
154 | /* out of buffer space */
|
---|
155 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
156 | /* always use ASCII version! */
|
---|
157 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
158 | #else
|
---|
159 | RpcStringFree((unsigned char **)&pStr);
|
---|
160 | #endif
|
---|
161 | AssertMsgFailed(("Buffer overflow\n"));
|
---|
162 | return ERROR_BUFFER_OVERFLOW;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* copy str to user buffer */
|
---|
166 | pszString[0] = '\0';
|
---|
167 | strncat(pszString, pStr, cchString);
|
---|
168 |
|
---|
169 | /* free buffer */
|
---|
170 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
171 | /* always use ASCII version! */
|
---|
172 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
173 | #else
|
---|
174 | RpcStringFree((unsigned char **)&pStr);
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | /* all done */
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Converts UUID from its string representation to binary format.
|
---|
183 | *
|
---|
184 | * @returns iprt status code.
|
---|
185 | * @param pUuid Where to store result Uuid.
|
---|
186 | * @param pszString String with UUID text data.
|
---|
187 | */
|
---|
188 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
189 | {
|
---|
190 | /* check params */
|
---|
191 | if ((pUuid == NULL) || (pszString == NULL))
|
---|
192 | {
|
---|
193 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
194 | return VERR_INVALID_PARAMETER;
|
---|
195 | }
|
---|
196 |
|
---|
197 | RPC_STATUS rc;
|
---|
198 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
199 | /* always use ASCII version! */
|
---|
200 | rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
|
---|
201 | #else
|
---|
202 | rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | return RTErrConvertFromWin32(rc);
|
---|
206 | }
|
---|
207 |
|
---|