1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uuid.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
37 #include <osl/mutex.hxx>
38 #include <rtl/random.h>
40 #include <rtl/digest.h>
42 #define SWAP_INT32_TO_NETWORK(x)\
44 sal_uInt8 *p = (sal_uInt8 * )&(x); \
45 p[0] = (sal_uInt8) ( ( y >> 24 ) & 0xff );\
46 p[1] = (sal_uInt8) ( ( y >> 16 ) & 0xff );\
47 p[2] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
48 p[3] = (sal_uInt8) ( ( y ) & 0xff);\
50 #define SWAP_INT16_TO_NETWORK(x)\
52 sal_uInt8 *p = (sal_uInt8 * )&(x); \
53 p[0] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
54 p[1] = (sal_uInt8) ( ( y ) & 0xff);\
57 #define SWAP_NETWORK_TO_INT16(x)\
59 sal_uInt8 *p = (sal_uInt8 * )&(y);\
60 x = ( ( ((sal_uInt16)p[0]) & 0xff) << 8 ) |\
61 ( ( (sal_uInt16)p[1]) & 0xff);\
63 #define SWAP_NETWORK_TO_INT32(x)\
65 sal_uInt8 *p = (sal_uInt8 * )&(y); \
66 x = ( ( ((sal_uInt32)p[0]) & 0xff) << 24 ) |\
67 ( ( ((sal_uInt32)p[1]) & 0xff) << 16 ) |\
68 ( ( ((sal_uInt32)p[2]) & 0xff) << 8 ) |\
69 ( ( (sal_uInt32)p[3]) & 0xff);\
76 sal_uInt16 time_hi_and_version
;
77 sal_uInt8 clock_seq_hi_and_reserved
;
78 sal_uInt8 clock_seq_low
;
82 static void write_v3( sal_uInt8
*pUuid
)
85 // copy to avoid alignment problems
86 memcpy( &uuid
, pUuid
, 16 );
88 SWAP_NETWORK_TO_INT32( uuid
.time_low
);
89 SWAP_NETWORK_TO_INT16( uuid
.time_mid
);
90 SWAP_NETWORK_TO_INT16( uuid
.time_hi_and_version
);
92 /* put in the variant and version bits */
93 uuid
.time_hi_and_version
&= 0x0FFF;
94 uuid
.time_hi_and_version
|= (3 << 12);
95 uuid
.clock_seq_hi_and_reserved
&= 0x3F;
96 uuid
.clock_seq_hi_and_reserved
|= 0x80;
98 SWAP_INT32_TO_NETWORK( uuid
.time_low
);
99 SWAP_INT16_TO_NETWORK( uuid
.time_mid
);
100 SWAP_INT16_TO_NETWORK( uuid
.time_hi_and_version
);
102 memcpy( pUuid
, &uuid
, 16 );
106 extern "C" void SAL_CALL
rtl_createUuid( sal_uInt8
*pTargetUUID
,
107 const sal_uInt8
*, sal_Bool
)
110 osl::MutexGuard
g(osl::Mutex::getGlobalMutex());
111 static rtlRandomPool pool
= NULL
;
113 pool
= rtl_random_createPool();
116 // only possible way to signal failure here (rtl_createUuid
117 // being part of a fixed C API)
120 if (rtl_random_getBytes(pool
, pTargetUUID
, 16) != rtl_Random_E_None
) {
122 // only possible way to signal failure here (rtl_createUuid
123 // being part of a fixed C API)
126 // See ITU-T Recommendation X.667:
127 pTargetUUID
[6] &= 0x0F;
128 pTargetUUID
[6] |= 0x40;
129 pTargetUUID
[8] &= 0x3F;
130 pTargetUUID
[8] |= 0x80;
134 extern "C" void SAL_CALL
rtl_createNamedUuid( sal_uInt8
*pTargetUUID
,
135 const sal_uInt8
*pNameSpaceUUID
,
136 const rtl_String
*pName
)
138 rtlDigest digest
= rtl_digest_createMD5 ();
140 rtl_digest_updateMD5( digest
, pNameSpaceUUID
, 16 );
141 rtl_digest_updateMD5( digest
, pName
->buffer
, pName
->length
);
143 rtl_digest_getMD5( digest
, pTargetUUID
, 16 );
144 rtl_digest_destroyMD5 (digest
);
146 write_v3(pTargetUUID
);
151 extern "C" sal_Int32 SAL_CALL
rtl_compareUuid( const sal_uInt8
*pUUID1
, const sal_uInt8
*pUUID2
)
156 memcpy( &u1
, pUUID1
, 16 );
157 memcpy( &u2
, pUUID2
, 16 );
159 SWAP_NETWORK_TO_INT32( u1
.time_low
);
160 SWAP_NETWORK_TO_INT16( u1
.time_mid
);
161 SWAP_NETWORK_TO_INT16( u1
.time_hi_and_version
);
163 SWAP_NETWORK_TO_INT32( u2
.time_low
);
164 SWAP_NETWORK_TO_INT16( u2
.time_mid
);
165 SWAP_NETWORK_TO_INT16( u2
.time_hi_and_version
);
167 #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
168 CHECK(u1
.time_low
, u2
.time_low
);
169 CHECK(u1
.time_mid
, u2
.time_mid
);
170 CHECK(u1
.time_hi_and_version
, u2
.time_hi_and_version
);
171 CHECK(u1
.clock_seq_hi_and_reserved
, u2
.clock_seq_hi_and_reserved
);
172 CHECK(u1
.clock_seq_low
, u2
.clock_seq_low
);
173 for (i
= 0; i
< 6; i
++)
175 if (u1
.node
[i
] < u2
.node
[i
])
177 if (u1
.node
[i
] > u2
.node
[i
])