win32_uuid.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008 Lemur Consulting Ltd
5  * Copyright (C) 2013,2015,2016,2017 Olly Betts
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <config.h>
23 
24 #include "win32_uuid.h"
25 
26 #include "xapian/error.h"
27 
28 #include <cstring>
29 #include "stringutils.h"
30 
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
37 
38 using namespace std;
39 
41 const size_t UUID_SIZE = 16;
42 
43 void
45 {
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");
54  }
55  uuid.Data1 = htonl(uuid.Data1);
56  uuid.Data2 = htons(uuid.Data2);
57  uuid.Data3 = htons(uuid.Data3);
58  memcpy(uu, &uuid, UUID_SIZE);
59 }
60 
61 int
62 uuid_parse(const char * in, uuid_t uu)
63 {
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;
67  }
68  return 0;
69 }
70 
71 void uuid_unparse_lower(const uuid_t uu, char * out)
72 {
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++ = '-';
79  }
80  *out = '\0';
81 }
82 
84 {
85  memset(uu, 0, UUID_SIZE);
86 }
87 
88 int uuid_is_null(const uuid_t uu)
89 {
90  unsigned i = 0;
91  while (i < UUID_SIZE) {
92  if (uu[i++])
93  return 0;
94  }
95  return 1;
96 }
int uuid_parse(const char *in, uuid_t uu)
Definition: win32_uuid.cc:62
void uuid_unparse_lower(const uuid_t uu, char *out)
Definition: win32_uuid.cc:71
unsigned char uuid_t[16]
Definition: proc_uuid.h:25
int hex_digit(char ch)
Definition: stringutils.h:188
STL namespace.
void uuid_clear(uuid_t uu)
Definition: win32_uuid.cc:83
#define rare(COND)
Definition: config.h:502
Hierarchy of classes which Xapian can throw as exceptions.
void uuid_generate(uuid_t uu)
Definition: win32_uuid.cc:44
DatabaseCreateError indicates a failure to create a database.
Definition: error.h:438
include <winsock2.h> but working around problems.
int uuid_is_null(const uuid_t uu)
Definition: win32_uuid.cc:88
Various handy helpers which std::string really should provide.
const size_t UUID_SIZE
The size of a UUID in bytes.
Definition: win32_uuid.cc:41

Documentation for Xapian (version 1.4.7).
Generated on Fri Jul 20 2018 by Doxygen 1.8.13.