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