1 | /* $Id: uuid-win.cpp 9738 2008-06-16 22:38:49Z 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 | /** @todo split out this guy */
|
---|
45 | RTDECL(int) RTUuidCreate(PRTUUID pUuid)
|
---|
46 | {
|
---|
47 | /* check params */
|
---|
48 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
49 |
|
---|
50 | RPC_STATUS rc = UuidCreate((UUID *)pUuid);
|
---|
51 | if ( rc == RPC_S_OK
|
---|
52 | || rc == RPC_S_UUID_LOCAL_ONLY)
|
---|
53 | return VINF_SUCCESS;
|
---|
54 |
|
---|
55 | /* error exit */
|
---|
56 | return RTErrConvertFromWin32(rc);
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
61 | {
|
---|
62 | /* check params */
|
---|
63 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
64 |
|
---|
65 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
|
---|
70 | {
|
---|
71 | /* check params */
|
---|
72 | AssertPtrReturn(pUuid, true);
|
---|
73 |
|
---|
74 | RPC_STATUS status;
|
---|
75 | return !!UuidIsNil((UUID *)pUuid, &status);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
80 | {
|
---|
81 | /* check params */
|
---|
82 | AssertPtrReturn(pUuid1, -1);
|
---|
83 | AssertPtrReturn(pUuid1, 1);
|
---|
84 |
|
---|
85 | RPC_STATUS status;
|
---|
86 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString)
|
---|
91 | {
|
---|
92 | /* check params */
|
---|
93 | AssertPtrReturn(pUuid1, -1);
|
---|
94 | AssertPtrReturn(pszString, 1);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Try convert the string to a UUID and then compare the two.
|
---|
98 | */
|
---|
99 | RTUUID Uuid2;
|
---|
100 | int rc = RTUuidFromStr(&Uuid2, pszString);
|
---|
101 | AssertRCReturn(rc, 1);
|
---|
102 |
|
---|
103 | return RTUuidCompare(pUuid1, &Uuid2);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
|
---|
108 | {
|
---|
109 | /* check params */
|
---|
110 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
111 | AssertPtrReturn(pszString, VERR_INVALID_POINTER);
|
---|
112 | AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Try convert it.
|
---|
116 | *
|
---|
117 | * The API allocates a new string buffer for us, so we can do our own
|
---|
118 | * buffer overflow handling.
|
---|
119 | */
|
---|
120 | RPC_STATUS Status;
|
---|
121 | unsigned char *pszTmpStr = NULL;
|
---|
122 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
123 | /* always use ASCII version! */
|
---|
124 | Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
|
---|
125 | #else
|
---|
126 | Status = UuidToString((UUID *)pUuid, &pszTmpStr);
|
---|
127 | #endif
|
---|
128 | if (Status != RPC_S_OK)
|
---|
129 | return RTErrConvertFromWin32(Status);
|
---|
130 |
|
---|
131 | /* copy it. */
|
---|
132 | int rc = VINF_SUCCESS;
|
---|
133 | size_t cch = strlen((char *)pszTmpStr);
|
---|
134 | if (cch < cchString)
|
---|
135 | memcpy(pszString, pszTmpStr, cchTmpStr + 1);
|
---|
136 | else
|
---|
137 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
138 |
|
---|
139 | /* free buffer */
|
---|
140 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
141 | /* always use ASCII version! */
|
---|
142 | RpcStringFreeA(&pszTmpStr);
|
---|
143 | #else
|
---|
144 | RpcStringFree(&pszTmpStr);
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | /* all done */
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
153 | {
|
---|
154 | /* check params */
|
---|
155 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
156 | AssertPtrReturn(pszString, VERR_INVALID_POINTER);
|
---|
157 |
|
---|
158 | RPC_STATUS rc;
|
---|
159 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
160 | /* always use ASCII version! */
|
---|
161 | rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
|
---|
162 | #else
|
---|
163 | rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | return RTErrConvertFromWin32(rc);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|