Support: quest -f cjk_ngram
[xapian.git] / xapian-core / common / win32_uuid.cc
blob35151dc802b40532fe6db405c79277bc18904304
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 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>
30 #ifdef __WIN32__
31 # include "safewinsock2.h" // For htonl() and htons().
32 #else
33 // Cygwin:
34 # include <arpa/inet.h> // For htonl() and htons().
35 #endif
37 using namespace std;
39 /// The size of a UUID in bytes.
40 const size_t UUID_SIZE = 16;
42 /// The size of a UUID string in bytes (not including trailing '\0').
43 const size_t UUID_STRING_SIZE = 36;
45 void
46 uuid_generate(uuid_t uu)
48 UUID uuid;
49 if (rare(UuidCreate(&uuid) != RPC_S_OK)) {
50 // Throw a DatabaseCreateError, since we can't make a UUID. The
51 // windows API documentation is a bit unclear about the situations in
52 // which this can happen, but if this behaviour causes a problem, an
53 // alternative would be to create a UUID ourselves somehow in this
54 // situation.
55 throw Xapian::DatabaseCreateError("Cannot create UUID");
57 uuid.Data1 = htonl(uuid.Data1);
58 uuid.Data2 = htons(uuid.Data2);
59 uuid.Data3 = htons(uuid.Data3);
60 memcpy(uu, &uuid, UUID_SIZE);
63 int
64 uuid_parse(const char * in, uuid_t uu)
66 UUID uuid;
67 // UuidFromString() requires a non-const unsigned char * pointer, though it
68 // doesn't modify the passed string.
69 unsigned char * in_ = (unsigned char *)const_cast<char *>(in);
70 if (UuidFromString(in_, &uuid) != RPC_S_OK)
71 return -1;
72 uuid.Data1 = htonl(uuid.Data1);
73 uuid.Data2 = htons(uuid.Data2);
74 uuid.Data3 = htons(uuid.Data3);
75 memcpy(uu, &uuid, UUID_SIZE);
76 return 0;
79 void uuid_unparse_lower(const uuid_t uu, char * out)
81 UUID uuid;
82 char *uuidstr;
83 memcpy(&uuid, uu, UUID_SIZE);
84 uuid.Data1 = htonl(uuid.Data1);
85 uuid.Data2 = htons(uuid.Data2);
86 uuid.Data3 = htons(uuid.Data3);
87 if (rare(UuidToString(&uuid, (unsigned char **)(&uuidstr)) != RPC_S_OK)) {
88 // The only documented (or really conceivable) error code is
89 // RPC_S_OUT_OF_MEMORY.
90 throw std::bad_alloc();
92 memcpy(out, strlwr(uuidstr), UUID_STRING_SIZE);
93 out[UUID_STRING_SIZE] = '\0';
94 RpcStringFree((unsigned char**)(&uuidstr));
97 void uuid_clear(uuid_t uu)
99 memset(uu, 0, UUID_SIZE);
102 int uuid_is_null(const uuid_t uu)
104 unsigned i = 0;
105 while (i < UUID_SIZE) {
106 if (uu[i++])
107 return 0;
109 return 1;