[ci] Enable IRC notifications from travis
[xapian.git] / xapian-core / common / win32_uuid.cc
blob6b56c0b41c1a62a2c2edbfd04f9f226e73d12506
1 /** @file win32_uuid.cc
2 * @brief Provide UUID functions compatible with libuuid from util-linux-ng.
3 */
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
22 #include <config.h>
24 #include "win32_uuid.h"
26 #include "xapian/error.h"
28 #include <cstring>
29 #include "stringutils.h"
31 #ifdef __WIN32__
32 # include "safewinsock2.h" // For htonl() and htons().
33 #else
34 // Cygwin:
35 # include <arpa/inet.h> // For htonl() and htons().
36 #endif
38 using namespace std;
40 /// The size of a UUID in bytes.
41 const size_t UUID_SIZE = 16;
43 void
44 uuid_generate(uuid_t uu)
46 UUID uuid;
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
52 // situation.
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);
61 int
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;
68 return 0;
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];
77 if ((0x2a8 >> i) & 1)
78 *out++ = '-';
80 *out = '\0';
83 void uuid_clear(uuid_t uu)
85 memset(uu, 0, UUID_SIZE);
88 int uuid_is_null(const uuid_t uu)
90 unsigned i = 0;
91 while (i < UUID_SIZE) {
92 if (uu[i++])
93 return 0;
95 return 1;