1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <stdlib.h>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include <osl/mutex.hxx>
31cdf0e10cSrcweir #include <rtl/random.h>
32cdf0e10cSrcweir #include <rtl/uuid.h>
33cdf0e10cSrcweir #include <rtl/digest.h>
34cdf0e10cSrcweir
35cdf0e10cSrcweir #define SWAP_INT32_TO_NETWORK(x)\
36cdf0e10cSrcweir { sal_uInt32 y = x;\
37cdf0e10cSrcweir sal_uInt8 *p = (sal_uInt8 * )&(x); \
38cdf0e10cSrcweir p[0] = (sal_uInt8) ( ( y >> 24 ) & 0xff );\
39cdf0e10cSrcweir p[1] = (sal_uInt8) ( ( y >> 16 ) & 0xff );\
40cdf0e10cSrcweir p[2] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
41cdf0e10cSrcweir p[3] = (sal_uInt8) ( ( y ) & 0xff);\
42cdf0e10cSrcweir }
43cdf0e10cSrcweir #define SWAP_INT16_TO_NETWORK(x)\
44cdf0e10cSrcweir { sal_uInt16 y = x;\
45cdf0e10cSrcweir sal_uInt8 *p = (sal_uInt8 * )&(x); \
46cdf0e10cSrcweir p[0] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
47cdf0e10cSrcweir p[1] = (sal_uInt8) ( ( y ) & 0xff);\
48cdf0e10cSrcweir }
49cdf0e10cSrcweir
50cdf0e10cSrcweir #define SWAP_NETWORK_TO_INT16(x)\
51cdf0e10cSrcweir { sal_uInt16 y = x;\
52cdf0e10cSrcweir sal_uInt8 *p = (sal_uInt8 * )&(y);\
53cdf0e10cSrcweir x = ( ( ((sal_uInt16)p[0]) & 0xff) << 8 ) |\
54cdf0e10cSrcweir ( ( (sal_uInt16)p[1]) & 0xff);\
55cdf0e10cSrcweir }
56cdf0e10cSrcweir #define SWAP_NETWORK_TO_INT32(x)\
57cdf0e10cSrcweir { sal_uInt32 y = x;\
58cdf0e10cSrcweir sal_uInt8 *p = (sal_uInt8 * )&(y); \
59cdf0e10cSrcweir x = ( ( ((sal_uInt32)p[0]) & 0xff) << 24 ) |\
60cdf0e10cSrcweir ( ( ((sal_uInt32)p[1]) & 0xff) << 16 ) |\
61cdf0e10cSrcweir ( ( ((sal_uInt32)p[2]) & 0xff) << 8 ) |\
62cdf0e10cSrcweir ( ( (sal_uInt32)p[3]) & 0xff);\
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
65cdf0e10cSrcweir typedef struct _UUID
66cdf0e10cSrcweir {
67cdf0e10cSrcweir sal_uInt32 time_low;
68cdf0e10cSrcweir sal_uInt16 time_mid;
69cdf0e10cSrcweir sal_uInt16 time_hi_and_version;
70cdf0e10cSrcweir sal_uInt8 clock_seq_hi_and_reserved;
71cdf0e10cSrcweir sal_uInt8 clock_seq_low;
72cdf0e10cSrcweir sal_uInt8 node[6];
73cdf0e10cSrcweir } UUID;
74cdf0e10cSrcweir
write_v3(sal_uInt8 * pUuid)75cdf0e10cSrcweir static void write_v3( sal_uInt8 *pUuid )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir UUID uuid;
78cdf0e10cSrcweir // copy to avoid alignment problems
79cdf0e10cSrcweir memcpy( &uuid , pUuid , 16 );
80cdf0e10cSrcweir
81cdf0e10cSrcweir SWAP_NETWORK_TO_INT32( uuid.time_low );
82cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( uuid.time_mid );
83cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( uuid.time_hi_and_version );
84cdf0e10cSrcweir
85cdf0e10cSrcweir /* put in the variant and version bits */
86cdf0e10cSrcweir uuid.time_hi_and_version &= 0x0FFF;
87cdf0e10cSrcweir uuid.time_hi_and_version |= (3 << 12);
88cdf0e10cSrcweir uuid.clock_seq_hi_and_reserved &= 0x3F;
89cdf0e10cSrcweir uuid.clock_seq_hi_and_reserved |= 0x80;
90cdf0e10cSrcweir
91cdf0e10cSrcweir SWAP_INT32_TO_NETWORK( uuid.time_low );
92cdf0e10cSrcweir SWAP_INT16_TO_NETWORK( uuid.time_mid );
93cdf0e10cSrcweir SWAP_INT16_TO_NETWORK( uuid.time_hi_and_version );
94cdf0e10cSrcweir
95cdf0e10cSrcweir memcpy( pUuid , &uuid , 16 );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
98cdf0e10cSrcweir
rtl_createUuid(sal_uInt8 * pTargetUUID,const sal_uInt8 *,sal_Bool)99cdf0e10cSrcweir extern "C" void SAL_CALL rtl_createUuid( sal_uInt8 *pTargetUUID ,
100cdf0e10cSrcweir const sal_uInt8 *, sal_Bool )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir {
103cdf0e10cSrcweir osl::MutexGuard g(osl::Mutex::getGlobalMutex());
104cdf0e10cSrcweir static rtlRandomPool pool = NULL;
105cdf0e10cSrcweir if (pool == NULL) {
106cdf0e10cSrcweir pool = rtl_random_createPool();
107cdf0e10cSrcweir if (pool == NULL) {
108cdf0e10cSrcweir abort();
109cdf0e10cSrcweir // only possible way to signal failure here (rtl_createUuid
110cdf0e10cSrcweir // being part of a fixed C API)
111cdf0e10cSrcweir }
112cdf0e10cSrcweir }
113cdf0e10cSrcweir if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None) {
114cdf0e10cSrcweir abort();
115cdf0e10cSrcweir // only possible way to signal failure here (rtl_createUuid
116cdf0e10cSrcweir // being part of a fixed C API)
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir // See ITU-T Recommendation X.667:
120cdf0e10cSrcweir pTargetUUID[6] &= 0x0F;
121cdf0e10cSrcweir pTargetUUID[6] |= 0x40;
122cdf0e10cSrcweir pTargetUUID[8] &= 0x3F;
123cdf0e10cSrcweir pTargetUUID[8] |= 0x80;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
126cdf0e10cSrcweir
rtl_createNamedUuid(sal_uInt8 * pTargetUUID,const sal_uInt8 * pNameSpaceUUID,const rtl_String * pName)127cdf0e10cSrcweir extern "C" void SAL_CALL rtl_createNamedUuid( sal_uInt8 *pTargetUUID,
128cdf0e10cSrcweir const sal_uInt8 *pNameSpaceUUID,
129cdf0e10cSrcweir const rtl_String *pName )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir rtlDigest digest = rtl_digest_createMD5 ();
132cdf0e10cSrcweir
133cdf0e10cSrcweir rtl_digest_updateMD5( digest, pNameSpaceUUID , 16 );
134cdf0e10cSrcweir rtl_digest_updateMD5( digest, pName->buffer , pName->length );
135cdf0e10cSrcweir
136cdf0e10cSrcweir rtl_digest_getMD5( digest, pTargetUUID , 16 );
137cdf0e10cSrcweir rtl_digest_destroyMD5 (digest);
138cdf0e10cSrcweir
139cdf0e10cSrcweir write_v3(pTargetUUID);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir
142cdf0e10cSrcweir
143cdf0e10cSrcweir
rtl_compareUuid(const sal_uInt8 * pUUID1,const sal_uInt8 * pUUID2)144cdf0e10cSrcweir extern "C" sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const sal_uInt8 *pUUID2 )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir int i;
147cdf0e10cSrcweir UUID u1;
148cdf0e10cSrcweir UUID u2;
149cdf0e10cSrcweir memcpy( &u1 , pUUID1 , 16 );
150cdf0e10cSrcweir memcpy( &u2 , pUUID2 , 16 );
151cdf0e10cSrcweir
152cdf0e10cSrcweir SWAP_NETWORK_TO_INT32( u1.time_low );
153cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( u1.time_mid );
154cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( u1.time_hi_and_version );
155cdf0e10cSrcweir
156cdf0e10cSrcweir SWAP_NETWORK_TO_INT32( u2.time_low );
157cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( u2.time_mid );
158cdf0e10cSrcweir SWAP_NETWORK_TO_INT16( u2.time_hi_and_version );
159cdf0e10cSrcweir
160cdf0e10cSrcweir #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
161cdf0e10cSrcweir CHECK(u1.time_low, u2.time_low);
162cdf0e10cSrcweir CHECK(u1.time_mid, u2.time_mid);
163cdf0e10cSrcweir CHECK(u1.time_hi_and_version, u2.time_hi_and_version);
164cdf0e10cSrcweir CHECK(u1.clock_seq_hi_and_reserved, u2.clock_seq_hi_and_reserved);
165cdf0e10cSrcweir CHECK(u1.clock_seq_low, u2.clock_seq_low);
166cdf0e10cSrcweir for (i = 0; i < 6; i++)
167cdf0e10cSrcweir {
168cdf0e10cSrcweir if (u1.node[i] < u2.node[i])
169cdf0e10cSrcweir return -1;
170cdf0e10cSrcweir if (u1.node[i] > u2.node[i])
171cdf0e10cSrcweir return 1;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir return 0;
174cdf0e10cSrcweir
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177