1 /** @file win32_uuid.cc
2 * @brief Provide UUID functions compatible with libuuid from util-linux-ng.
4 /* Copyright (C) 2008 Lemur Consulting Ltd
5 * Copyright (C) 2013,2015,2016,2017 Olly Betts
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "win32_uuid.h"
26 #include "xapian/error.h"
29 #include "stringutils.h"
32 # include "safewinsock2.h" // For htonl() and htons().
35 # include <arpa/inet.h> // For htonl() and htons().
40 /// The size of a UUID in bytes.
41 const size_t UUID_SIZE
= 16;
44 uuid_generate(uuid_t uu
)
47 if (rare(UuidCreate(&uuid
) != RPC_S_OK
)) {
48 // Throw a DatabaseCreateError, since we can't make a UUID. The
49 // windows API documentation is a bit unclear about the situations in
50 // which this can happen, but if this behaviour causes a problem, an
51 // alternative would be to create a UUID ourselves somehow in this
53 throw Xapian::DatabaseCreateError("Cannot create UUID");
55 uuid
.Data1
= htonl(uuid
.Data1
);
56 uuid
.Data2
= htons(uuid
.Data2
);
57 uuid
.Data3
= htons(uuid
.Data3
);
58 memcpy(uu
, &uuid
, UUID_SIZE
);
62 uuid_parse(const char * in
, uuid_t uu
)
64 for (unsigned i
= 0; i
!= UUID_SIZE
; ++i
) {
65 uu
[i
] = hex_digit(in
[0]) << 4 | hex_digit(in
[1]);
66 in
+= ((0x2a8 >> i
) & 1) | 2;
71 void uuid_unparse_lower(const uuid_t uu
, char * out
)
73 for (unsigned i
= 0; i
!= UUID_SIZE
; ++i
) {
74 unsigned char ch
= uu
[i
];
75 *out
++ = "0123456789abcdef"[ch
>> 4];
76 *out
++ = "0123456789abcdef"[ch
& 0x0f];
83 void uuid_clear(uuid_t uu
)
85 memset(uu
, 0, UUID_SIZE
);
88 int uuid_is_null(const uuid_t uu
)
91 while (i
< UUID_SIZE
) {