1 | /* $Id: uuid-win32.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime UUID (unique identifiers) handling (win32 host).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_UUID
|
---|
27 | #include <Windows.h>
|
---|
28 |
|
---|
29 | #include <iprt/uuid.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Generates a new UUID value.
|
---|
37 | *
|
---|
38 | * @returns iprt status code.
|
---|
39 | * @param pUuid Where to store generated uuid.
|
---|
40 | */
|
---|
41 | RTDECL(int) RTUuidCreate(PRTUUID pUuid)
|
---|
42 | {
|
---|
43 | /* check params */
|
---|
44 | if (pUuid == NULL)
|
---|
45 | {
|
---|
46 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
47 | return VERR_INVALID_PARAMETER;
|
---|
48 | }
|
---|
49 |
|
---|
50 | RPC_STATUS rc = UuidCreate((UUID *)pUuid);
|
---|
51 | if ((rc == RPC_S_OK) || (rc == RPC_S_UUID_LOCAL_ONLY))
|
---|
52 | return VINF_SUCCESS;
|
---|
53 |
|
---|
54 | /* error exit */
|
---|
55 | return RTErrConvertFromWin32(rc);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Makes null UUID value.
|
---|
60 | *
|
---|
61 | * @returns iprt status code.
|
---|
62 | * @param pUuid Where to store generated null uuid.
|
---|
63 | */
|
---|
64 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
65 | {
|
---|
66 | /* check params */
|
---|
67 | if (pUuid == NULL)
|
---|
68 | {
|
---|
69 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
70 | return VERR_INVALID_PARAMETER;
|
---|
71 | }
|
---|
72 |
|
---|
73 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Checks if UUID is null.
|
---|
78 | *
|
---|
79 | * @returns true if UUID is null.
|
---|
80 | * @param pUuid uuid to check.
|
---|
81 | */
|
---|
82 | RTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
|
---|
83 | {
|
---|
84 | /* check params */
|
---|
85 | if (pUuid == NULL)
|
---|
86 | {
|
---|
87 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
88 | return TRUE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | RPC_STATUS status;
|
---|
92 | return UuidIsNil((UUID *)pUuid, &status);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Compares two UUID values.
|
---|
97 | *
|
---|
98 | * @returns 0 if eq, < 0 or > 0.
|
---|
99 | * @param pUuid1 First value to compare.
|
---|
100 | * @param pUuid2 Second value to compare.
|
---|
101 | */
|
---|
102 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
103 | {
|
---|
104 | /* check params */
|
---|
105 | if ((pUuid1 == NULL) || (pUuid2 == NULL))
|
---|
106 | {
|
---|
107 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | RPC_STATUS status;
|
---|
112 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Converts binary UUID to its string representation.
|
---|
117 | *
|
---|
118 | * @returns iprt status code.
|
---|
119 | * @param pUuid Uuid to convert.
|
---|
120 | * @param pszString Where to store result string.
|
---|
121 | * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
|
---|
122 | */
|
---|
123 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
|
---|
124 | {
|
---|
125 | /* check params */
|
---|
126 | if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
|
---|
127 | {
|
---|
128 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
129 | return VERR_INVALID_PARAMETER;
|
---|
130 | }
|
---|
131 |
|
---|
132 | RPC_STATUS rc;
|
---|
133 | char *pStr = NULL;
|
---|
134 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
135 | /* always use ASCII version! */
|
---|
136 | rc = UuidToStringA((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
137 | #else
|
---|
138 | rc = UuidToString((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
139 | #endif
|
---|
140 | if (rc != RPC_S_OK)
|
---|
141 | return RTErrConvertFromWin32(rc);
|
---|
142 |
|
---|
143 | if (strlen(pStr) >= cchString)
|
---|
144 | {
|
---|
145 | /* out of buffer space */
|
---|
146 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
147 | /* always use ASCII version! */
|
---|
148 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
149 | #else
|
---|
150 | RpcStringFree((unsigned char **)&pStr);
|
---|
151 | #endif
|
---|
152 | AssertMsgFailed(("Buffer overflow\n"));
|
---|
153 | return ERROR_BUFFER_OVERFLOW;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* copy str to user buffer */
|
---|
157 | pszString[0] = '\0';
|
---|
158 | strncat(pszString, pStr, cchString);
|
---|
159 |
|
---|
160 | /* free buffer */
|
---|
161 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
162 | /* always use ASCII version! */
|
---|
163 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
164 | #else
|
---|
165 | RpcStringFree((unsigned char **)&pStr);
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | /* all done */
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Converts UUID from its string representation to binary format.
|
---|
174 | *
|
---|
175 | * @returns iprt status code.
|
---|
176 | * @param pUuid Where to store result Uuid.
|
---|
177 | * @param pszString String with UUID text data.
|
---|
178 | */
|
---|
179 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
180 | {
|
---|
181 | /* check params */
|
---|
182 | if ((pUuid == NULL) || (pszString == NULL))
|
---|
183 | {
|
---|
184 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
185 | return VERR_INVALID_PARAMETER;
|
---|
186 | }
|
---|
187 |
|
---|
188 | RPC_STATUS rc;
|
---|
189 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
190 | /* always use ASCII version! */
|
---|
191 | rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
|
---|
192 | #else
|
---|
193 | rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
|
---|
194 | #endif
|
---|
195 |
|
---|
196 | return RTErrConvertFromWin32(rc);
|
---|
197 | }
|
---|
198 |
|
---|