1 | /* $Id: uuid-win.cpp 62592 2016-07-27 13:24:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UUID, Windows implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_UUID
|
---|
32 | #include <iprt/win/windows.h>
|
---|
33 |
|
---|
34 | #include <iprt/uuid.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
41 | {
|
---|
42 | /* check params */
|
---|
43 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
44 |
|
---|
45 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
|
---|
50 | {
|
---|
51 | /* check params */
|
---|
52 | AssertPtrReturn(pUuid, true);
|
---|
53 |
|
---|
54 | RPC_STATUS status;
|
---|
55 | return !!UuidIsNil((UUID *)pUuid, &status);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * Special cases.
|
---|
63 | */
|
---|
64 | if (pUuid1 == pUuid2)
|
---|
65 | return 0;
|
---|
66 | if (!pUuid1)
|
---|
67 | return RTUuidIsNull(pUuid2) ? 0 : -1;
|
---|
68 | if (!pUuid2)
|
---|
69 | return RTUuidIsNull(pUuid1) ? 0 : 1;
|
---|
70 | AssertPtrReturn(pUuid1, -1);
|
---|
71 | AssertPtrReturn(pUuid2, 1);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Hand the rest to the Windows API.
|
---|
75 | */
|
---|
76 | RPC_STATUS status;
|
---|
77 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString2)
|
---|
82 | {
|
---|
83 | /* check params */
|
---|
84 | AssertPtrReturn(pUuid1, -1);
|
---|
85 | AssertPtrReturn(pszString2, 1);
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Try convert the string to a UUID and then compare the two.
|
---|
89 | */
|
---|
90 | RTUUID Uuid2;
|
---|
91 | int rc = RTUuidFromStr(&Uuid2, pszString2);
|
---|
92 | AssertRCReturn(rc, 1);
|
---|
93 |
|
---|
94 | return RTUuidCompare(pUuid1, &Uuid2);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTUuidCompare2Strs(const char *pszString1, const char *pszString2)
|
---|
99 | {
|
---|
100 | RTUUID Uuid1;
|
---|
101 | RTUUID Uuid2;
|
---|
102 | int rc;
|
---|
103 |
|
---|
104 | /* check params */
|
---|
105 | AssertPtrReturn(pszString1, -1);
|
---|
106 | AssertPtrReturn(pszString2, 1);
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Try convert the strings to UUIDs and then compare them.
|
---|
110 | */
|
---|
111 | rc = RTUuidFromStr(&Uuid1, pszString1);
|
---|
112 | AssertRCReturn(rc, -1);
|
---|
113 |
|
---|
114 | rc = RTUuidFromStr(&Uuid2, pszString2);
|
---|
115 | AssertRCReturn(rc, 1);
|
---|
116 |
|
---|
117 | return RTUuidCompare(&Uuid1, &Uuid2);
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
|
---|
122 | {
|
---|
123 | /* check params */
|
---|
124 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
125 | AssertPtrReturn(pszString, VERR_INVALID_POINTER);
|
---|
126 | AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Try convert it.
|
---|
130 | *
|
---|
131 | * The API allocates a new string buffer for us, so we can do our own
|
---|
132 | * buffer overflow handling.
|
---|
133 | */
|
---|
134 | RPC_STATUS Status;
|
---|
135 | unsigned char *pszTmpStr = NULL;
|
---|
136 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
137 | /* always use ASCII version! */
|
---|
138 | Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
|
---|
139 | #else
|
---|
140 | Status = UuidToString((UUID *)pUuid, &pszTmpStr);
|
---|
141 | #endif
|
---|
142 | if (Status != RPC_S_OK)
|
---|
143 | return RTErrConvertFromWin32(Status);
|
---|
144 |
|
---|
145 | /* copy it. */
|
---|
146 | int rc = VINF_SUCCESS;
|
---|
147 | size_t cchTmpStr = strlen((char *)pszTmpStr);
|
---|
148 | if (cchTmpStr < cchString)
|
---|
149 | memcpy(pszString, pszTmpStr, cchTmpStr + 1);
|
---|
150 | else
|
---|
151 | {
|
---|
152 | AssertFailed();
|
---|
153 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* free buffer */
|
---|
157 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
158 | /* always use ASCII version! */
|
---|
159 | RpcStringFreeA(&pszTmpStr);
|
---|
160 | #else
|
---|
161 | RpcStringFree(&pszTmpStr);
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | /* all done */
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
170 | {
|
---|
171 | /* check params */
|
---|
172 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
173 | AssertPtrReturn(pszString, VERR_INVALID_POINTER);
|
---|
174 |
|
---|
175 | RPC_STATUS rc;
|
---|
176 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
177 | /* always use ASCII version! */
|
---|
178 | rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
|
---|
179 | #else
|
---|
180 | rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | return RTErrConvertFromWin32(rc);
|
---|
184 | }
|
---|
185 |
|
---|