1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
24 #include <rtl/random.h>
26 #include <rtl/digest.h>
28 #define SWAP_INT16_TO_NETWORK(x)\
30 sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(x)); \
31 p[0] = static_cast<sal_uInt8>( ( y >> 8 ) & 0xff );\
32 p[1] = static_cast<sal_uInt8>( ( y ) & 0xff);\
35 #define SWAP_NETWORK_TO_INT16(x)\
37 sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(y));\
38 x = ( ( (static_cast<sal_uInt16>(p[0])) & 0xff) << 8 ) |\
39 ( ( static_cast<sal_uInt16>(p[1])) & 0xff);\
41 #define SWAP_NETWORK_TO_INT32(x)\
43 sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(y)); \
44 x = ( ( (static_cast<sal_uInt32>(p[0])) & 0xff) << 24 ) |\
45 ( ( (static_cast<sal_uInt32>(p[1])) & 0xff) << 16 ) |\
46 ( ( (static_cast<sal_uInt32>(p[2])) & 0xff) << 8 ) |\
47 ( ( static_cast<sal_uInt32>(p[3])) & 0xff);\
56 sal_uInt16 time_hi_and_version
;
57 sal_uInt8 clock_seq_hi_and_reserved
;
58 sal_uInt8 clock_seq_low
;
64 static void write_v3( sal_uInt8
*pUuid
)
67 // copy to avoid alignment problems
68 memcpy(&uuid
, pUuid
, 16);
70 SWAP_NETWORK_TO_INT16(uuid
.time_hi_and_version
);
72 /* put in the variant and version bits */
73 uuid
.time_hi_and_version
&= 0x0FFF;
74 uuid
.time_hi_and_version
|= (3 << 12);
75 uuid
.clock_seq_hi_and_reserved
&= 0x3F;
76 uuid
.clock_seq_hi_and_reserved
|= 0x80;
78 SWAP_INT16_TO_NETWORK(uuid
.time_hi_and_version
);
80 memcpy(pUuid
, &uuid
, 16);
83 extern "C" void SAL_CALL
rtl_createUuid(sal_uInt8
*pTargetUUID
,
84 SAL_UNUSED_PARAMETER
const sal_uInt8
*,
85 SAL_UNUSED_PARAMETER sal_Bool
)
88 static rtlRandomPool pool
= []() {
89 rtlRandomPool aPool
= rtl_random_createPool();
93 // only possible way to signal failure here (rtl_createUuid
94 // being part of a fixed C API)
99 static std::mutex aMutex
;
101 std::scoped_lock
g(aMutex
);
102 if (rtl_random_getBytes(pool
, pTargetUUID
, 16) != rtl_Random_E_None
)
105 // only possible way to signal failure here (rtl_createUuid
106 // being part of a fixed C API)
109 // See ITU-T Recommendation X.667:
110 pTargetUUID
[6] &= 0x0F;
111 pTargetUUID
[6] |= 0x40;
112 pTargetUUID
[8] &= 0x3F;
113 pTargetUUID
[8] |= 0x80;
116 extern "C" void SAL_CALL
rtl_createNamedUuid(sal_uInt8
*pTargetUUID
,
117 const sal_uInt8
*pNameSpaceUUID
,
118 const rtl_String
*pName
)
120 rtlDigest digest
= rtl_digest_createMD5();
122 rtl_digest_updateMD5(digest
, pNameSpaceUUID
, 16);
123 rtl_digest_updateMD5(digest
, pName
->buffer
, pName
->length
);
125 rtl_digest_getMD5(digest
, pTargetUUID
, 16);
126 rtl_digest_destroyMD5(digest
);
128 write_v3(pTargetUUID
);
131 extern "C" sal_Int32 SAL_CALL
rtl_compareUuid(const sal_uInt8
*pUUID1
, const sal_uInt8
*pUUID2
)
136 memcpy(&u1
, pUUID1
, 16 );
137 memcpy(&u2
, pUUID2
, 16 );
139 SWAP_NETWORK_TO_INT32(u1
.time_low
);
140 SWAP_NETWORK_TO_INT16(u1
.time_mid
);
141 SWAP_NETWORK_TO_INT16(u1
.time_hi_and_version
);
143 SWAP_NETWORK_TO_INT32(u2
.time_low
);
144 SWAP_NETWORK_TO_INT16(u2
.time_mid
);
145 SWAP_NETWORK_TO_INT16(u2
.time_hi_and_version
);
147 #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
148 CHECK(u1
.time_low
, u2
.time_low
);
149 CHECK(u1
.time_mid
, u2
.time_mid
);
150 CHECK(u1
.time_hi_and_version
, u2
.time_hi_and_version
);
151 CHECK(u1
.clock_seq_hi_and_reserved
, u2
.clock_seq_hi_and_reserved
);
152 CHECK(u1
.clock_seq_low
, u2
.clock_seq_low
);
153 for (i
= 0; i
< 6; i
++)
155 if (u1
.node
[i
] < u2
.node
[i
])
157 if (u1
.node
[i
] > u2
.node
[i
])
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */