Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <string.h>
31 : : #include <stdlib.h>
32 : :
33 : : #include <osl/mutex.hxx>
34 : : #include <rtl/random.h>
35 : : #include <rtl/uuid.h>
36 : : #include <rtl/digest.h>
37 : :
38 : : #define SWAP_INT32_TO_NETWORK(x)\
39 : : { sal_uInt32 y = x;\
40 : : sal_uInt8 *p = (sal_uInt8 * )&(x); \
41 : : p[0] = (sal_uInt8) ( ( y >> 24 ) & 0xff );\
42 : : p[1] = (sal_uInt8) ( ( y >> 16 ) & 0xff );\
43 : : p[2] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
44 : : p[3] = (sal_uInt8) ( ( y ) & 0xff);\
45 : : }
46 : : #define SWAP_INT16_TO_NETWORK(x)\
47 : : { sal_uInt16 y = x;\
48 : : sal_uInt8 *p = (sal_uInt8 * )&(x); \
49 : : p[0] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
50 : : p[1] = (sal_uInt8) ( ( y ) & 0xff);\
51 : : }
52 : :
53 : : #define SWAP_NETWORK_TO_INT16(x)\
54 : : { sal_uInt16 y = x;\
55 : : sal_uInt8 *p = (sal_uInt8 * )&(y);\
56 : : x = ( ( ((sal_uInt16)p[0]) & 0xff) << 8 ) |\
57 : : ( ( (sal_uInt16)p[1]) & 0xff);\
58 : : }
59 : : #define SWAP_NETWORK_TO_INT32(x)\
60 : : { sal_uInt32 y = x;\
61 : : sal_uInt8 *p = (sal_uInt8 * )&(y); \
62 : : x = ( ( ((sal_uInt32)p[0]) & 0xff) << 24 ) |\
63 : : ( ( ((sal_uInt32)p[1]) & 0xff) << 16 ) |\
64 : : ( ( ((sal_uInt32)p[2]) & 0xff) << 8 ) |\
65 : : ( ( (sal_uInt32)p[3]) & 0xff);\
66 : : }
67 : :
68 : : struct UUID
69 : : {
70 : : sal_uInt32 time_low;
71 : : sal_uInt16 time_mid;
72 : : sal_uInt16 time_hi_and_version;
73 : : sal_uInt8 clock_seq_hi_and_reserved;
74 : : sal_uInt8 clock_seq_low;
75 : : sal_uInt8 node[6];
76 : : };
77 : :
78 : 0 : static void write_v3( sal_uInt8 *pUuid )
79 : : {
80 : : UUID uuid;
81 : : // copy to avoid alignment problems
82 : 0 : memcpy( &uuid , pUuid , 16 );
83 : :
84 : 0 : SWAP_NETWORK_TO_INT32( uuid.time_low );
85 : 0 : SWAP_NETWORK_TO_INT16( uuid.time_mid );
86 : 0 : SWAP_NETWORK_TO_INT16( uuid.time_hi_and_version );
87 : :
88 : : /* put in the variant and version bits */
89 : 0 : uuid.time_hi_and_version &= 0x0FFF;
90 : 0 : uuid.time_hi_and_version |= (3 << 12);
91 : 0 : uuid.clock_seq_hi_and_reserved &= 0x3F;
92 : 0 : uuid.clock_seq_hi_and_reserved |= 0x80;
93 : :
94 : 0 : SWAP_INT32_TO_NETWORK( uuid.time_low );
95 : 0 : SWAP_INT16_TO_NETWORK( uuid.time_mid );
96 : 0 : SWAP_INT16_TO_NETWORK( uuid.time_hi_and_version );
97 : :
98 : 0 : memcpy( pUuid , &uuid , 16 );
99 : 0 : }
100 : :
101 : :
102 : 4943 : extern "C" void SAL_CALL rtl_createUuid( sal_uInt8 *pTargetUUID ,
103 : : SAL_UNUSED_PARAMETER const sal_uInt8 *,
104 : : SAL_UNUSED_PARAMETER sal_Bool )
105 : : {
106 : : {
107 [ + - ][ + - ]: 4943 : osl::MutexGuard g(osl::Mutex::getGlobalMutex());
108 : : static rtlRandomPool pool = NULL;
109 [ + + ]: 4943 : if (pool == NULL) {
110 : 271 : pool = rtl_random_createPool();
111 [ - + ]: 271 : if (pool == NULL) {
112 : 0 : abort();
113 : : // only possible way to signal failure here (rtl_createUuid
114 : : // being part of a fixed C API)
115 : : }
116 : : }
117 [ - + ]: 4943 : if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None) {
118 : 0 : abort();
119 : : // only possible way to signal failure here (rtl_createUuid
120 : : // being part of a fixed C API)
121 [ + - ]: 4943 : }
122 : : }
123 : : // See ITU-T Recommendation X.667:
124 : 4943 : pTargetUUID[6] &= 0x0F;
125 : 4943 : pTargetUUID[6] |= 0x40;
126 : 4943 : pTargetUUID[8] &= 0x3F;
127 : 4943 : pTargetUUID[8] |= 0x80;
128 : 4943 : }
129 : :
130 : :
131 : 0 : extern "C" void SAL_CALL rtl_createNamedUuid( sal_uInt8 *pTargetUUID,
132 : : const sal_uInt8 *pNameSpaceUUID,
133 : : const rtl_String *pName )
134 : : {
135 : 0 : rtlDigest digest = rtl_digest_createMD5 ();
136 : :
137 : 0 : rtl_digest_updateMD5( digest, pNameSpaceUUID , 16 );
138 : 0 : rtl_digest_updateMD5( digest, pName->buffer , pName->length );
139 : :
140 : 0 : rtl_digest_getMD5( digest, pTargetUUID , 16 );
141 : 0 : rtl_digest_destroyMD5 (digest);
142 : :
143 : 0 : write_v3(pTargetUUID);
144 : 0 : }
145 : :
146 : :
147 : :
148 : 0 : extern "C" sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const sal_uInt8 *pUUID2 )
149 : : {
150 : : int i;
151 : : UUID u1;
152 : : UUID u2;
153 : 0 : memcpy( &u1 , pUUID1 , 16 );
154 : 0 : memcpy( &u2 , pUUID2 , 16 );
155 : :
156 : 0 : SWAP_NETWORK_TO_INT32( u1.time_low );
157 : 0 : SWAP_NETWORK_TO_INT16( u1.time_mid );
158 : 0 : SWAP_NETWORK_TO_INT16( u1.time_hi_and_version );
159 : :
160 : 0 : SWAP_NETWORK_TO_INT32( u2.time_low );
161 : 0 : SWAP_NETWORK_TO_INT16( u2.time_mid );
162 : 0 : SWAP_NETWORK_TO_INT16( u2.time_hi_and_version );
163 : :
164 : : #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
165 [ # # ][ # # ]: 0 : CHECK(u1.time_low, u2.time_low);
166 [ # # ][ # # ]: 0 : CHECK(u1.time_mid, u2.time_mid);
167 [ # # ][ # # ]: 0 : CHECK(u1.time_hi_and_version, u2.time_hi_and_version);
168 [ # # ][ # # ]: 0 : CHECK(u1.clock_seq_hi_and_reserved, u2.clock_seq_hi_and_reserved);
169 [ # # ][ # # ]: 0 : CHECK(u1.clock_seq_low, u2.clock_seq_low);
170 [ # # ]: 0 : for (i = 0; i < 6; i++)
171 : : {
172 [ # # ]: 0 : if (u1.node[i] < u2.node[i])
173 : 0 : return -1;
174 [ # # ]: 0 : if (u1.node[i] > u2.node[i])
175 : 0 : return 1;
176 : : }
177 : 0 : return 0;
178 : :
179 : : }
180 : :
181 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|