[1] | 1 | /* $Id: uuid-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[9741] | 3 | * IPRT - UUID, Windows implementation.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_UUID
|
---|
[62592] | 42 | #include <iprt/win/windows.h>
|
---|
[1] | 43 |
|
---|
| 44 | #include <iprt/uuid.h>
|
---|
| 45 | #include <iprt/assert.h>
|
---|
| 46 | #include <iprt/string.h>
|
---|
[76452] | 47 | #include <iprt/errcore.h>
|
---|
[1] | 48 |
|
---|
| 49 |
|
---|
[204] | 50 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
[1] | 51 | {
|
---|
| 52 | /* check params */
|
---|
[9738] | 53 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
[1] | 54 |
|
---|
| 55 | return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[9738] | 58 |
|
---|
| 59 | RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
|
---|
[1] | 60 | {
|
---|
| 61 | /* check params */
|
---|
[9738] | 62 | AssertPtrReturn(pUuid, true);
|
---|
[1] | 63 |
|
---|
| 64 | RPC_STATUS status;
|
---|
[9738] | 65 | return !!UuidIsNil((UUID *)pUuid, &status);
|
---|
[1] | 66 | }
|
---|
| 67 |
|
---|
[9738] | 68 |
|
---|
[204] | 69 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
[1] | 70 | {
|
---|
[9743] | 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;
|
---|
[9738] | 80 | AssertPtrReturn(pUuid1, -1);
|
---|
[9743] | 81 | AssertPtrReturn(pUuid2, 1);
|
---|
[1] | 82 |
|
---|
[9743] | 83 | /*
|
---|
| 84 | * Hand the rest to the Windows API.
|
---|
| 85 | */
|
---|
[1] | 86 | RPC_STATUS status;
|
---|
| 87 | return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[9738] | 90 |
|
---|
[25961] | 91 | RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString2)
|
---|
[1] | 92 | {
|
---|
| 93 | /* check params */
|
---|
[9738] | 94 | AssertPtrReturn(pUuid1, -1);
|
---|
[25961] | 95 | AssertPtrReturn(pszString2, 1);
|
---|
[1] | 96 |
|
---|
[9738] | 97 | /*
|
---|
| 98 | * Try convert the string to a UUID and then compare the two.
|
---|
| 99 | */
|
---|
| 100 | RTUUID Uuid2;
|
---|
[25961] | 101 | int rc = RTUuidFromStr(&Uuid2, pszString2);
|
---|
[9738] | 102 | AssertRCReturn(rc, 1);
|
---|
| 103 |
|
---|
| 104 | return RTUuidCompare(pUuid1, &Uuid2);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[25961] | 108 | RTDECL(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 |
|
---|
[9738] | 131 | RTDECL(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;
|
---|
[1] | 146 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
| 147 | /* always use ASCII version! */
|
---|
[9738] | 148 | Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
|
---|
[1] | 149 | #else
|
---|
[9738] | 150 | Status = UuidToString((UUID *)pUuid, &pszTmpStr);
|
---|
[1] | 151 | #endif
|
---|
[9738] | 152 | if (Status != RPC_S_OK)
|
---|
| 153 | return RTErrConvertFromWin32(Status);
|
---|
[1] | 154 |
|
---|
[9738] | 155 | /* copy it. */
|
---|
| 156 | int rc = VINF_SUCCESS;
|
---|
[9739] | 157 | size_t cchTmpStr = strlen((char *)pszTmpStr);
|
---|
| 158 | if (cchTmpStr < cchString)
|
---|
[9738] | 159 | memcpy(pszString, pszTmpStr, cchTmpStr + 1);
|
---|
| 160 | else
|
---|
[9740] | 161 | {
|
---|
| 162 | AssertFailed();
|
---|
[9738] | 163 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
[9740] | 164 | }
|
---|
[1] | 165 |
|
---|
| 166 | /* free buffer */
|
---|
| 167 | #ifdef RPC_UNICODE_SUPPORTED
|
---|
| 168 | /* always use ASCII version! */
|
---|
[9738] | 169 | RpcStringFreeA(&pszTmpStr);
|
---|
[1] | 170 | #else
|
---|
[9738] | 171 | RpcStringFree(&pszTmpStr);
|
---|
[1] | 172 | #endif
|
---|
| 173 |
|
---|
| 174 | /* all done */
|
---|
[9738] | 175 | return rc;
|
---|
[1] | 176 | }
|
---|
| 177 |
|
---|
[9738] | 178 |
|
---|
[204] | 179 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
[1] | 180 | {
|
---|
| 181 | /* check params */
|
---|
[9738] | 182 | AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
|
---|
| 183 | AssertPtrReturn(pszString, VERR_INVALID_POINTER);
|
---|
[1] | 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 |
|
---|