Bump version to 4.1-6
[LibreOffice.git] / sal / rtl / uuid.cxx
blobf7f3c0c08bd3bc4f51b1f585b1acd5bb5c6d778e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <string.h>
22 #include <stdlib.h>
24 #include <osl/mutex.hxx>
25 #include <rtl/random.h>
26 #include <rtl/uuid.h>
27 #include <rtl/digest.h>
29 #define SWAP_INT32_TO_NETWORK(x)\
30 { sal_uInt32 y = x;\
31 sal_uInt8 *p = (sal_uInt8 * )&(x); \
32 p[0] = (sal_uInt8) ( ( y >> 24 ) & 0xff );\
33 p[1] = (sal_uInt8) ( ( y >> 16 ) & 0xff );\
34 p[2] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
35 p[3] = (sal_uInt8) ( ( y ) & 0xff);\
37 #define SWAP_INT16_TO_NETWORK(x)\
38 { sal_uInt16 y = x;\
39 sal_uInt8 *p = (sal_uInt8 * )&(x); \
40 p[0] = (sal_uInt8) ( ( y >> 8 ) & 0xff );\
41 p[1] = (sal_uInt8) ( ( y ) & 0xff);\
44 #define SWAP_NETWORK_TO_INT16(x)\
45 { sal_uInt16 y = x;\
46 sal_uInt8 *p = (sal_uInt8 * )&(y);\
47 x = ( ( ((sal_uInt16)p[0]) & 0xff) << 8 ) |\
48 ( ( (sal_uInt16)p[1]) & 0xff);\
50 #define SWAP_NETWORK_TO_INT32(x)\
51 { sal_uInt32 y = x;\
52 sal_uInt8 *p = (sal_uInt8 * )&(y); \
53 x = ( ( ((sal_uInt32)p[0]) & 0xff) << 24 ) |\
54 ( ( ((sal_uInt32)p[1]) & 0xff) << 16 ) |\
55 ( ( ((sal_uInt32)p[2]) & 0xff) << 8 ) |\
56 ( ( (sal_uInt32)p[3]) & 0xff);\
59 struct UUID
61 sal_uInt32 time_low;
62 sal_uInt16 time_mid;
63 sal_uInt16 time_hi_and_version;
64 sal_uInt8 clock_seq_hi_and_reserved;
65 sal_uInt8 clock_seq_low;
66 sal_uInt8 node[6];
69 static void write_v3( sal_uInt8 *pUuid )
71 UUID uuid;
72 // copy to avoid alignment problems
73 memcpy( &uuid , pUuid , 16 );
75 SWAP_NETWORK_TO_INT32( uuid.time_low );
76 SWAP_NETWORK_TO_INT16( uuid.time_mid );
77 SWAP_NETWORK_TO_INT16( uuid.time_hi_and_version );
79 /* put in the variant and version bits */
80 uuid.time_hi_and_version &= 0x0FFF;
81 uuid.time_hi_and_version |= (3 << 12);
82 uuid.clock_seq_hi_and_reserved &= 0x3F;
83 uuid.clock_seq_hi_and_reserved |= 0x80;
85 SWAP_INT32_TO_NETWORK( uuid.time_low );
86 SWAP_INT16_TO_NETWORK( uuid.time_mid );
87 SWAP_INT16_TO_NETWORK( uuid.time_hi_and_version );
89 memcpy( pUuid , &uuid , 16 );
93 extern "C" void SAL_CALL rtl_createUuid( sal_uInt8 *pTargetUUID ,
94 SAL_UNUSED_PARAMETER const sal_uInt8 *,
95 SAL_UNUSED_PARAMETER sal_Bool )
98 osl::MutexGuard g(osl::Mutex::getGlobalMutex());
99 static rtlRandomPool pool = NULL;
100 if (pool == NULL) {
101 pool = rtl_random_createPool();
102 if (pool == NULL) {
103 abort();
104 // only possible way to signal failure here (rtl_createUuid
105 // being part of a fixed C API)
108 if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None) {
109 abort();
110 // only possible way to signal failure here (rtl_createUuid
111 // being part of a fixed C API)
114 // See ITU-T Recommendation X.667:
115 pTargetUUID[6] &= 0x0F;
116 pTargetUUID[6] |= 0x40;
117 pTargetUUID[8] &= 0x3F;
118 pTargetUUID[8] |= 0x80;
122 extern "C" void SAL_CALL rtl_createNamedUuid( sal_uInt8 *pTargetUUID,
123 const sal_uInt8 *pNameSpaceUUID,
124 const rtl_String *pName )
126 rtlDigest digest = rtl_digest_createMD5 ();
128 rtl_digest_updateMD5( digest, pNameSpaceUUID , 16 );
129 rtl_digest_updateMD5( digest, pName->buffer , pName->length );
131 rtl_digest_getMD5( digest, pTargetUUID , 16 );
132 rtl_digest_destroyMD5 (digest);
134 write_v3(pTargetUUID);
139 extern "C" sal_Int32 SAL_CALL rtl_compareUuid( const sal_uInt8 *pUUID1 , const sal_uInt8 *pUUID2 )
141 int i;
142 UUID u1;
143 UUID u2;
144 memcpy( &u1 , pUUID1 , 16 );
145 memcpy( &u2 , pUUID2 , 16 );
147 SWAP_NETWORK_TO_INT32( u1.time_low );
148 SWAP_NETWORK_TO_INT16( u1.time_mid );
149 SWAP_NETWORK_TO_INT16( u1.time_hi_and_version );
151 SWAP_NETWORK_TO_INT32( u2.time_low );
152 SWAP_NETWORK_TO_INT16( u2.time_mid );
153 SWAP_NETWORK_TO_INT16( u2.time_hi_and_version );
155 #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
156 CHECK(u1.time_low, u2.time_low);
157 CHECK(u1.time_mid, u2.time_mid);
158 CHECK(u1.time_hi_and_version, u2.time_hi_and_version);
159 CHECK(u1.clock_seq_hi_and_reserved, u2.clock_seq_hi_and_reserved);
160 CHECK(u1.clock_seq_low, u2.clock_seq_low);
161 for (i = 0; i < 6; i++)
163 if (u1.node[i] < u2.node[i])
164 return -1;
165 if (u1.node[i] > u2.node[i])
166 return 1;
168 return 0;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */