1 | /* $Id: uuid-linux.cpp 204 2007-01-21 09:57:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - UUID, LINUX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung 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 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/uuid.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 |
|
---|
31 | #include <uuid/uuid.h>
|
---|
32 | #include <sys/types.h> /* for BYTE_ORDER */
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Converts the byte order of the first 32 bit UUID component and next two 16
|
---|
37 | * bit components from BIG_ENDIAN to LITTLE_ENDIAN and vice versa.
|
---|
38 | *
|
---|
39 | * @returns iprt status code.
|
---|
40 | * @param pUuid Uuid to convert.
|
---|
41 | */
|
---|
42 |
|
---|
43 | static void rtuuid_convert_byteorder(PRTUUID pUuid)
|
---|
44 | {
|
---|
45 | uint8_t *pu8 = &pUuid->au8[0];
|
---|
46 | uint8_t u8;
|
---|
47 | // 32 bit component
|
---|
48 | u8 = pu8[0];
|
---|
49 | pu8[0] = pu8[3];
|
---|
50 | pu8[3] = u8;
|
---|
51 | u8 = pu8[1];
|
---|
52 | pu8[1] = pu8[2];
|
---|
53 | pu8[2] = u8;
|
---|
54 | // two 16 bit components
|
---|
55 | u8 = pu8[4];
|
---|
56 | pu8[4] = pu8[5];
|
---|
57 | pu8[5] = u8;
|
---|
58 | u8 = pu8[6];
|
---|
59 | pu8[6] = pu8[7];
|
---|
60 | pu8[7] = u8;
|
---|
61 | }
|
---|
62 |
|
---|
63 | #if BYTE_ORDER == LITTLE_ENDIAN
|
---|
64 | #define RTUUID_CONVERT_BYTEORDER(pUuid) rtuuid_convert_byteorder(pUuid)
|
---|
65 | #else
|
---|
66 | #define RTUUID_CONVERT_BYTEORDER(pUuid) do {} while (0)
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Generates a new UUID value.
|
---|
71 | *
|
---|
72 | * @returns iprt status code.
|
---|
73 | * @param pUuid Where to store generated uuid.
|
---|
74 | */
|
---|
75 | RTDECL(int) RTUuidCreate(PRTUUID pUuid)
|
---|
76 | {
|
---|
77 | /* check params */
|
---|
78 | if (pUuid == NULL)
|
---|
79 | {
|
---|
80 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
81 | return VERR_INVALID_PARAMETER;
|
---|
82 | }
|
---|
83 |
|
---|
84 | uuid_generate(&pUuid->au8[0]);
|
---|
85 | RTUUID_CONVERT_BYTEORDER(pUuid);
|
---|
86 |
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Makes a null UUID value.
|
---|
92 | *
|
---|
93 | * @returns iprt status code.
|
---|
94 | * @param pUuid Where to store generated null uuid.
|
---|
95 | */
|
---|
96 | RTDECL(int) RTUuidClear(PRTUUID pUuid)
|
---|
97 | {
|
---|
98 | /* check params */
|
---|
99 | if (pUuid == NULL)
|
---|
100 | {
|
---|
101 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
102 | return VERR_INVALID_PARAMETER;
|
---|
103 | }
|
---|
104 |
|
---|
105 | uuid_clear(&pUuid->au8[0]);
|
---|
106 |
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Checks if UUID is null.
|
---|
112 | *
|
---|
113 | * @returns true if UUID is null.
|
---|
114 | * @param pUuid uuid to check.
|
---|
115 | */
|
---|
116 | RTDECL(int) RTUuidIsNull(PCRTUUID pUuid)
|
---|
117 | {
|
---|
118 | /* check params */
|
---|
119 | if (pUuid == NULL)
|
---|
120 | {
|
---|
121 | AssertMsgFailed(("pUuid=NULL\n"));
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return uuid_is_null(&pUuid->au8[0]);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Compares two UUID values.
|
---|
130 | *
|
---|
131 | * @returns 0 if eq, < 0 or > 0.
|
---|
132 | * @param pUuid1 First value to compare.
|
---|
133 | * @param pUuid2 Second value to compare.
|
---|
134 | */
|
---|
135 | RTDECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
|
---|
136 | {
|
---|
137 | /* check params */
|
---|
138 | if ((pUuid1 == NULL) || (pUuid2 == NULL))
|
---|
139 | {
|
---|
140 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | return uuid_compare(pUuid1->aUuid, pUuid2->aUuid);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Converts binary UUID to its string representation.
|
---|
149 | *
|
---|
150 | * @returns iprt status code.
|
---|
151 | * @param pUuid Uuid to convert.
|
---|
152 | * @param pszString Where to store result string.
|
---|
153 | * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
|
---|
154 | */
|
---|
155 | RTDECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
|
---|
156 | {
|
---|
157 | /* check params */
|
---|
158 | if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
|
---|
159 | {
|
---|
160 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
161 | return VERR_INVALID_PARAMETER;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static RTUUID uuid;
|
---|
165 | memcpy(&uuid, pUuid, sizeof(RTUUID));
|
---|
166 | RTUUID_CONVERT_BYTEORDER(&uuid);
|
---|
167 | uuid_unparse(uuid.aUuid, pszString);
|
---|
168 |
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Converts UUID from its string representation to binary format.
|
---|
174 | *
|
---|
175 | * @returns iprt status code.
|
---|
176 | * @param pUuid Where to store result Uuid.
|
---|
177 | * @param pszString String with UUID text data.
|
---|
178 | */
|
---|
179 | RTDECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
|
---|
180 | {
|
---|
181 | /* check params */
|
---|
182 | if ((pUuid == NULL) || (pszString == NULL))
|
---|
183 | {
|
---|
184 | AssertMsgFailed(("Invalid parameters\n"));
|
---|
185 | return VERR_INVALID_PARAMETER;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (uuid_parse(pszString, &pUuid->au8[0]) == 0)
|
---|
189 | {
|
---|
190 | RTUUID_CONVERT_BYTEORDER(pUuid);
|
---|
191 | return VINF_SUCCESS;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return VERR_INVALID_UUID_FORMAT;
|
---|
195 | }
|
---|