This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
sys-uuid.cc
105 lines (89 loc) · 2.22 KB
/
sys-uuid.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Copyright © 2015-2019 Jakub Wilk <[email protected]>
*
* This file is part of pdf2djvu.
*
* pdf2djvu is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* pdf2djvu is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "sys-uuid.hh"
#include "autoconf.hh"
#include "system.hh"
#if WIN32
#include <cassert>
#include <cerrno>
#include <cstring>
#include <rpc.h>
static void throw_uuid_error(const char *context, RPC_STATUS rc)
{
switch (rc) {
case RPC_S_UUID_LOCAL_ONLY:
errno = EIO;
break;
case RPC_S_UUID_NO_ADDRESS:
errno = ENXIO;
break;
case RPC_S_OUT_OF_MEMORY:
errno = ENOMEM;
break;
default:
errno = EINVAL;
break;
}
throw_posix_error(context);
}
void uuid_generate_random(uuid_t &uu)
{
RPC_STATUS rc;
rc = UuidCreate(&uu);
if (rc != RPC_S_OK)
throw_uuid_error("UuidCreate()", rc);
}
void uuid_unparse_lower(uuid_t &uu, char *out)
{
unsigned char *us;
RPC_STATUS rc;
rc = UuidToString(&uu, &us);
if (rc != RPC_S_OK)
throw_uuid_error("UuidToString()", rc);
const char *s = reinterpret_cast<const char *>(us);
assert(strlen(s) == 36U);
strcpy(out, s);
RpcStringFree(&us);
}
#elif HAVE_DCE_UUID
#include <cassert>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <uuid.h>
static void throw_uuid_error(const char *context, uint32_t rc)
{
errno = (rc == uuid_s_no_memory) ? ENOMEM : EINVAL;
throw_posix_error(context);
}
void uuid_generate_random(uuid_t &uu)
{
uint32_t rc;
uuid_create(&uu, &rc);
if (rc != uuid_s_ok)
throw_uuid_error("uuid_create()", rc);
}
void uuid_unparse_lower(uuid_t &uu, char *out)
{
uint32_t rc;
char *s;
uuid_to_string(&uu, &s, &rc);
if (rc != uuid_s_ok)
throw_uuid_error("uuid_to_string()", rc);
assert(strlen(s) == 36U);
strcpy(out, s);
free(s);
}
#endif
// vim:ts=4 sts=4 sw=4 et