VirtualBox

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

Last change on this file since 27513 was 26344, checked in by vboxsync, 15 years ago

Runtime: white space cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: uuid-win.cpp 26344 2010-02-09 03:39:45Z vboxsync $ */
2/** @file
3 * IPRT - UUID, Windows implementation.
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
44RTDECL(int) RTUuidClear(PRTUUID pUuid)
45{
46 /* check params */
47 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
48
49 return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
50}
51
52
53RTDECL(bool) RTUuidIsNull(PCRTUUID pUuid)
54{
55 /* check params */
56 AssertPtrReturn(pUuid, true);
57
58 RPC_STATUS status;
59 return !!UuidIsNil((UUID *)pUuid, &status);
60}
61
62
63RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
64{
65 /*
66 * Special cases.
67 */
68 if (pUuid1 == pUuid2)
69 return 0;
70 if (!pUuid1)
71 return RTUuidIsNull(pUuid2) ? 0 : -1;
72 if (!pUuid2)
73 return RTUuidIsNull(pUuid1) ? 0 : 1;
74 AssertPtrReturn(pUuid1, -1);
75 AssertPtrReturn(pUuid2, 1);
76
77 /*
78 * Hand the rest to the Windows API.
79 */
80 RPC_STATUS status;
81 return UuidCompare((UUID *)pUuid1, (UUID *)pUuid2, &status);
82}
83
84
85RTDECL(int) RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString2)
86{
87 /* check params */
88 AssertPtrReturn(pUuid1, -1);
89 AssertPtrReturn(pszString2, 1);
90
91 /*
92 * Try convert the string to a UUID and then compare the two.
93 */
94 RTUUID Uuid2;
95 int rc = RTUuidFromStr(&Uuid2, pszString2);
96 AssertRCReturn(rc, 1);
97
98 return RTUuidCompare(pUuid1, &Uuid2);
99}
100
101
102RTDECL(int) RTUuidCompare2Strs(const char *pszString1, const char *pszString2)
103{
104 RTUUID Uuid1;
105 RTUUID Uuid2;
106 int rc;
107
108 /* check params */
109 AssertPtrReturn(pszString1, -1);
110 AssertPtrReturn(pszString2, 1);
111
112 /*
113 * Try convert the strings to UUIDs and then compare them.
114 */
115 rc = RTUuidFromStr(&Uuid1, pszString1);
116 AssertRCReturn(rc, -1);
117
118 rc = RTUuidFromStr(&Uuid2, pszString2);
119 AssertRCReturn(rc, 1);
120
121 return RTUuidCompare(&Uuid1, &Uuid2);
122}
123
124
125RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
126{
127 /* check params */
128 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
129 AssertPtrReturn(pszString, VERR_INVALID_POINTER);
130 AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
131
132 /*
133 * Try convert it.
134 *
135 * The API allocates a new string buffer for us, so we can do our own
136 * buffer overflow handling.
137 */
138 RPC_STATUS Status;
139 unsigned char *pszTmpStr = NULL;
140#ifdef RPC_UNICODE_SUPPORTED
141 /* always use ASCII version! */
142 Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
143#else
144 Status = UuidToString((UUID *)pUuid, &pszTmpStr);
145#endif
146 if (Status != RPC_S_OK)
147 return RTErrConvertFromWin32(Status);
148
149 /* copy it. */
150 int rc = VINF_SUCCESS;
151 size_t cchTmpStr = strlen((char *)pszTmpStr);
152 if (cchTmpStr < cchString)
153 memcpy(pszString, pszTmpStr, cchTmpStr + 1);
154 else
155 {
156 AssertFailed();
157 rc = ERROR_BUFFER_OVERFLOW;
158 }
159
160 /* free buffer */
161#ifdef RPC_UNICODE_SUPPORTED
162 /* always use ASCII version! */
163 RpcStringFreeA(&pszTmpStr);
164#else
165 RpcStringFree(&pszTmpStr);
166#endif
167
168 /* all done */
169 return rc;
170}
171
172
173RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
174{
175 /* check params */
176 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
177 AssertPtrReturn(pszString, VERR_INVALID_POINTER);
178
179 RPC_STATUS rc;
180#ifdef RPC_UNICODE_SUPPORTED
181 /* always use ASCII version! */
182 rc = UuidFromStringA((unsigned char *)pszString, (UUID *)pUuid);
183#else
184 rc = UuidFromString((unsigned char *)pszString, (UUID *)pUuid);
185#endif
186
187 return RTErrConvertFromWin32(rc);
188}
189
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette