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