/src/libreoffice/sal/rtl/uuid.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <string.h> |
21 | | #include <stdlib.h> |
22 | | |
23 | | #include <rtl/random.h> |
24 | | #include <rtl/uuid.h> |
25 | | #include <rtl/digest.h> |
26 | | |
27 | | #define SWAP_INT16_TO_NETWORK(x)\ |
28 | 0 | { sal_uInt16 y = x;\ |
29 | 0 | sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(x)); \ |
30 | 0 | p[0] = static_cast<sal_uInt8>( ( y >> 8 ) & 0xff );\ |
31 | 0 | p[1] = static_cast<sal_uInt8>( ( y ) & 0xff);\ |
32 | 0 | } |
33 | | |
34 | | #define SWAP_NETWORK_TO_INT16(x)\ |
35 | 0 | { sal_uInt16 y = x;\ |
36 | 0 | sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(y));\ |
37 | 0 | x = ( ( (static_cast<sal_uInt16>(p[0])) & 0xff) << 8 ) |\ |
38 | 0 | ( ( static_cast<sal_uInt16>(p[1])) & 0xff);\ |
39 | 0 | } |
40 | | #define SWAP_NETWORK_TO_INT32(x)\ |
41 | 0 | { sal_uInt32 y = x;\ |
42 | 0 | sal_uInt8 *p = reinterpret_cast<sal_uInt8 *>(&(y)); \ |
43 | 0 | x = ( ( (static_cast<sal_uInt32>(p[0])) & 0xff) << 24 ) |\ |
44 | 0 | ( ( (static_cast<sal_uInt32>(p[1])) & 0xff) << 16 ) |\ |
45 | 0 | ( ( (static_cast<sal_uInt32>(p[2])) & 0xff) << 8 ) |\ |
46 | 0 | ( ( static_cast<sal_uInt32>(p[3])) & 0xff);\ |
47 | 0 | } |
48 | | |
49 | | namespace { |
50 | | |
51 | | struct UUID |
52 | | { |
53 | | sal_uInt32 time_low; |
54 | | sal_uInt16 time_mid; |
55 | | sal_uInt16 time_hi_and_version; |
56 | | sal_uInt8 clock_seq_hi_and_reserved; |
57 | | sal_uInt8 clock_seq_low; |
58 | | sal_uInt8 node[6]; |
59 | | }; |
60 | | |
61 | | } |
62 | | |
63 | | static void write_v3( sal_uInt8 *pUuid ) |
64 | 0 | { |
65 | 0 | UUID uuid; |
66 | | // copy to avoid alignment problems |
67 | 0 | memcpy(&uuid, pUuid, 16); |
68 | |
|
69 | 0 | SWAP_NETWORK_TO_INT16(uuid.time_hi_and_version); |
70 | | |
71 | | /* put in the variant and version bits */ |
72 | 0 | uuid.time_hi_and_version &= 0x0FFF; |
73 | 0 | uuid.time_hi_and_version |= (3 << 12); |
74 | 0 | uuid.clock_seq_hi_and_reserved &= 0x3F; |
75 | 0 | uuid.clock_seq_hi_and_reserved |= 0x80; |
76 | |
|
77 | 0 | SWAP_INT16_TO_NETWORK(uuid.time_hi_and_version); |
78 | |
|
79 | 0 | memcpy(pUuid, &uuid, 16); |
80 | 0 | } |
81 | | |
82 | | extern "C" void SAL_CALL rtl_createUuid(sal_uInt8 *pTargetUUID , |
83 | | SAL_UNUSED_PARAMETER const sal_uInt8 *, |
84 | | SAL_UNUSED_PARAMETER sal_Bool) |
85 | 155k | { |
86 | 155k | if (rtl_random_getBytes(nullptr, pTargetUUID, 16) != rtl_Random_E_None) |
87 | 0 | { |
88 | 0 | abort(); |
89 | | // only possible way to signal failure here (rtl_createUuid |
90 | | // being part of a fixed C API) |
91 | 0 | } |
92 | | |
93 | | // See ITU-T Recommendation X.667: |
94 | 155k | pTargetUUID[6] &= 0x0F; |
95 | 155k | pTargetUUID[6] |= 0x40; |
96 | 155k | pTargetUUID[8] &= 0x3F; |
97 | 155k | pTargetUUID[8] |= 0x80; |
98 | 155k | } |
99 | | |
100 | | extern "C" void SAL_CALL rtl_createNamedUuid(sal_uInt8 *pTargetUUID, |
101 | | const sal_uInt8 *pNameSpaceUUID, |
102 | | const rtl_String *pName ) |
103 | 0 | { |
104 | 0 | rtlDigest digest = rtl_digest_createMD5(); |
105 | |
|
106 | 0 | rtl_digest_updateMD5(digest, pNameSpaceUUID, 16); |
107 | 0 | rtl_digest_updateMD5(digest, pName->buffer, pName->length); |
108 | |
|
109 | 0 | rtl_digest_getMD5(digest, pTargetUUID, 16); |
110 | 0 | rtl_digest_destroyMD5(digest); |
111 | |
|
112 | 0 | write_v3(pTargetUUID); |
113 | 0 | } |
114 | | |
115 | | extern "C" sal_Int32 SAL_CALL rtl_compareUuid(const sal_uInt8 *pUUID1, const sal_uInt8 *pUUID2) |
116 | 0 | { |
117 | 0 | int i; |
118 | 0 | UUID u1; |
119 | 0 | UUID u2; |
120 | 0 | memcpy(&u1, pUUID1, 16 ); |
121 | 0 | memcpy(&u2, pUUID2, 16 ); |
122 | |
|
123 | 0 | SWAP_NETWORK_TO_INT32(u1.time_low); |
124 | 0 | SWAP_NETWORK_TO_INT16(u1.time_mid); |
125 | 0 | SWAP_NETWORK_TO_INT16(u1.time_hi_and_version); |
126 | |
|
127 | 0 | SWAP_NETWORK_TO_INT32(u2.time_low); |
128 | 0 | SWAP_NETWORK_TO_INT16(u2.time_mid); |
129 | 0 | SWAP_NETWORK_TO_INT16(u2.time_hi_and_version); |
130 | |
|
131 | 0 | #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1; |
132 | 0 | CHECK(u1.time_low, u2.time_low); |
133 | 0 | CHECK(u1.time_mid, u2.time_mid); |
134 | 0 | CHECK(u1.time_hi_and_version, u2.time_hi_and_version); |
135 | 0 | CHECK(u1.clock_seq_hi_and_reserved, u2.clock_seq_hi_and_reserved); |
136 | 0 | CHECK(u1.clock_seq_low, u2.clock_seq_low); |
137 | 0 | for (i = 0; i < 6; i++) |
138 | 0 | { |
139 | 0 | if (u1.node[i] < u2.node[i]) |
140 | 0 | return -1; |
141 | 0 | if (u1.node[i] > u2.node[i]) |
142 | 0 | return 1; |
143 | 0 | } |
144 | 0 | return 0; |
145 | 0 | } |
146 | | |
147 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |