1 | /* $Id: uuid-win.cpp 5999 2007-12-07 15:05:06Z 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 (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 <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 | /**
|
---|
41 | * Generates a new UUID value.
|
---|
42 | *
|
---|
43 | * @returns iprt status code.
|
---|
44 | * @param pUuid Where to store generated uuid.
|
---|
45 | */
|
---|
46 | RTDECL(int) RTUuidCreate(PRTUUID pUuid)
|
---|
47 | {
|
---|
48 | /* check params */
|
---|
49 | if (pUuid == NULL)
|
---|
50 | {
|
---|
51 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
52 | return VERR_INVALID_PARAMETER;
|
---|
53 | }
|
---|
54 |
|
---|
55 | RPC_STATUS rc = UuidCreate((UUID *)pUuid);
|
---|
56 | if ((rc == RPC_S_OK) || (rc == RPC_S_UUID_LOCAL_ONLY))
|
---|
57 | return VINF_SUCCESS;
|
---|
58 |
|
---|
59 | /* error exit */
|
---|
60 | return RTErrConvertFromWin32(rc);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Makes null UUID value.
|
---|
65 | *
|
---|
66 | * @returns iprt status code.
|
---|
67 | * @param pUuid Where to store generated null uuid.
|
---|
68 | */
|
---|
69 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
70 | {
|
---|
71 | /* check params */
|
---|
72 | if (pUuid == NULL)
|
---|
73 | {
|
---|
74 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
75 | return VERR_INVALID_PARAMETER;
|
---|
76 | }
|
---|
77 |
|
---|
78 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Checks if UUID is null.
|
---|
83 | *
|
---|
84 | * @returns true if UUID is null.
|
---|
85 | * @param pUuid uuid to check.
|
---|
86 | */
|
---|
87 | RTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
|
---|
88 | {
|
---|
89 | /* check params */
|
---|
90 | if (pUuid == NULL)
|
---|
91 | {
|
---|
92 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
93 | return TRUE;
|
---|
94 | }
|
---|
95 |
|
---|
96 | RPC_STATUS status;
|
---|
97 | return UuidIsNil((UUID *)pUuid, &status);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Compares two UUID values.
|
---|
102 | *
|
---|
103 | * @returns 0 if eq, < 0 or > 0.
|
---|
104 | * @param pUuid1 First value to compare.
|
---|
105 | * @param pUuid2 Second value to compare.
|
---|
106 | */
|
---|
107 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
108 | {
|
---|
109 | /* check params */
|
---|
110 | if ((pUuid1 == NULL) || (pUuid2 == NULL))
|
---|
111 | {
|
---|
112 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | RPC_STATUS status;
|
---|
117 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Converts binary UUID to its string representation.
|
---|
122 | *
|
---|
123 | * @returns iprt status code.
|
---|
124 | * @param pUuid Uuid to convert.
|
---|
125 | * @param pszString Where to store result string.
|
---|
126 | * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
|
---|
127 | */
|
---|
128 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
|
---|
129 | {
|
---|
130 | /* check params */
|
---|
131 | if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
|
---|
132 | {
|
---|
133 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
134 | return VERR_INVALID_PARAMETER;
|
---|
135 | }
|
---|
136 |
|
---|
137 | RPC_STATUS rc;
|
---|
138 | char *pStr = NULL;
|
---|
139 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
140 | /* always use ASCII version! */
|
---|
141 | rc = UuidToStringA((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
142 | #else
|
---|
143 | rc = UuidToString((UUID *)pUuid, (unsigned char **)&pStr);
|
---|
144 | #endif
|
---|
145 | if (rc != RPC_S_OK)
|
---|
146 | return RTErrConvertFromWin32(rc);
|
---|
147 |
|
---|
148 | if (strlen(pStr) >= cchString)
|
---|
149 | {
|
---|
150 | /* out of buffer space */
|
---|
151 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
152 | /* always use ASCII version! */
|
---|
153 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
154 | #else
|
---|
155 | RpcStringFree((unsigned char **)&pStr);
|
---|
156 | #endif
|
---|
157 | AssertMsgFailed(("Buffer overflow\n"));
|
---|
158 | return ERROR_BUFFER_OVERFLOW;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* copy str to user buffer */
|
---|
162 | pszString[0] = '\0';
|
---|
163 | strncat(pszString, pStr, cchString);
|
---|
164 |
|
---|
165 | /* free buffer */
|
---|
166 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
167 | /* always use ASCII version! */
|
---|
168 | RpcStringFreeA((unsigned char **)&pStr);
|
---|
169 | #else
|
---|
170 | RpcStringFree((unsigned char **)&pStr);
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | /* all done */
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Converts UUID from its string representation to binary format.
|
---|
179 | *
|
---|
180 | * @returns iprt status code.
|
---|
181 | * @param pUuid Where to store result Uuid.
|
---|
182 | * @param pszString String with UUID text data.
|
---|
183 | */
|
---|
184 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
185 | {
|
---|
186 | /* check params */
|
---|
187 | if ((pUuid == NULL) || (pszString == NULL))
|
---|
188 | {
|
---|
189 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
190 | return VERR_INVALID_PARAMETER;
|
---|
191 | }
|
---|
192 |
|
---|
193 | RPC_STATUS rc;
|
---|
194 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
195 | /* always use ASCII version! */
|
---|
196 | rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
|
---|
197 | #else
|
---|
198 | rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | return RTErrConvertFromWin32(rc);
|
---|
202 | }
|
---|
203 |
|
---|