VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/uuid-win.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.2 KB
Line 
1/* $Id: uuid-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - UUID, Windows implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_UUID
42#include <iprt/win/windows.h>
43
44#include <iprt/uuid.h>
45#include <iprt/assert.h>
46#include <iprt/string.h>
47#include <iprt/errcore.h>
48
49
50RTDECL(int) RTUuidClear(PRTUUID pUuid)
51{
52 /* check params */
53 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
54
55 return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
56}
57
58
59RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
60{
61 /* check params */
62 AssertPtrReturn(pUuid, true);
63
64 RPC_STATUS status;
65 return !!UuidIsNil((UUID *)pUuid, &status);
66}
67
68
69RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
70{
71 /*
72 * Special cases.
73 */
74 if (pUuid1 == pUuid2)
75 return 0;
76 if (!pUuid1)
77 return RTUuidIsNull(pUuid2) ? 0 : -1;
78 if (!pUuid2)
79 return RTUuidIsNull(pUuid1) ? 0 : 1;
80 AssertPtrReturn(pUuid1, -1);
81 AssertPtrReturn(pUuid2, 1);
82
83 /*
84 * Hand the rest to the Windows API.
85 */
86 RPC_STATUS status;
87 return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
88}
89
90
91RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString2)
92{
93 /* check params */
94 AssertPtrReturn(pUuid1, -1);
95 AssertPtrReturn(pszString2, 1);
96
97 /*
98 * Try convert the string to a UUID and then compare the two.
99 */
100 RTUUID Uuid2;
101 int rc = RTUuidFromStr(&Uuid2, pszString2);
102 AssertRCReturn(rc, 1);
103
104 return RTUuidCompare(pUuid1, &Uuid2);
105}
106
107
108RTDECL(int) RTUuidCompare2Strs(const char *pszString1, const char *pszString2)
109{
110 RTUUID Uuid1;
111 RTUUID Uuid2;
112 int rc;
113
114 /* check params */
115 AssertPtrReturn(pszString1, -1);
116 AssertPtrReturn(pszString2, 1);
117
118 /*
119 * Try convert the strings to UUIDs and then compare them.
120 */
121 rc = RTUuidFromStr(&Uuid1, pszString1);
122 AssertRCReturn(rc, -1);
123
124 rc = RTUuidFromStr(&Uuid2, pszString2);
125 AssertRCReturn(rc, 1);
126
127 return RTUuidCompare(&Uuid1, &Uuid2);
128}
129
130
131RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
132{
133 /* check params */
134 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
135 AssertPtrReturn(pszString, VERR_INVALID_POINTER);
136 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
137
138 /*
139 * Try convert it.
140 *
141 * The API allocates a new string buffer for us, so we can do our own
142 * buffer overflow handling.
143 */
144 RPC_STATUS Status;
145 unsigned char *pszTmpStr = NULL;
146#ifdef RPC_UNICODE_SUPPORTED
147 /* always use ASCII version! */
148 Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
149#else
150 Status = UuidToString((UUID *)pUuid, &pszTmpStr);
151#endif
152 if (Status != RPC_S_OK)
153 return RTErrConvertFromWin32(Status);
154
155 /* copy it. */
156 int rc = VINF_SUCCESS;
157 size_t cchTmpStr = strlen((char *)pszTmpStr);
158 if (cchTmpStr < cchString)
159 memcpy(pszString, pszTmpStr, cchTmpStr + 1);
160 else
161 {
162 AssertFailed();
163 rc = ERROR_BUFFER_OVERFLOW;
164 }
165
166 /* free buffer */
167#ifdef RPC_UNICODE_SUPPORTED
168 /* always use ASCII version! */
169 RpcStringFreeA(&pszTmpStr);
170#else
171 RpcStringFree(&pszTmpStr);
172#endif
173
174 /* all done */
175 return rc;
176}
177
178
179RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
180{
181 /* check params */
182 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
183 AssertPtrReturn(pszString, VERR_INVALID_POINTER);
184
185 RPC_STATUS rc;
186#ifdef RPC_UNICODE_SUPPORTED
187 /* always use ASCII version! */
188 rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
189#else
190 rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
191#endif
192
193 return RTErrConvertFromWin32(rc);
194}
195
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use