]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-id128/id128-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "id128-util.h"
12 #include "stdio-util.h"
13 #include "string-util.h"
15 char *id128_to_uuid_string(sd_id128_t id
, char s
[static ID128_UUID_STRING_MAX
]) {
20 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
22 for (n
= 0; n
< 16; n
++) {
24 if (IN_SET(n
, 4, 6, 8, 10))
27 s
[k
++] = hexchar(id
.bytes
[n
] >> 4);
28 s
[k
++] = hexchar(id
.bytes
[n
] & 0xF);
38 bool id128_is_valid(const char *s
) {
46 /* Plain formatted 128bit hex string */
48 for (i
= 0; i
< l
; i
++) {
51 if (!(c
>= '0' && c
<= '9') &&
52 !(c
>= 'a' && c
<= 'z') &&
53 !(c
>= 'A' && c
<= 'Z'))
61 for (i
= 0; i
< l
; i
++) {
64 if (IN_SET(i
, 8, 13, 18, 23)) {
68 if (!(c
>= '0' && c
<= '9') &&
69 !(c
>= 'a' && c
<= 'z') &&
70 !(c
>= 'A' && c
<= 'Z'))
81 int id128_read_fd(int fd
, Id128Format f
, sd_id128_t
*ret
) {
86 assert(f
< _ID128_FORMAT_MAX
);
88 /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
89 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
90 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
93 l
= loop_read(fd
, buffer
, sizeof(buffer
), false); /* we expect a short read of either 32/33 or 36/37 chars */
96 if (l
== 0) /* empty? */
103 /* Treat an "uninitialized" id file like an empty one */
104 return f
== ID128_PLAIN_OR_UNINIT
&& strneq(buffer
, "uninitialized\n", l
) ? -ENOMEDIUM
: -EINVAL
;
106 case 33: /* plain UUID with trailing newline */
107 if (buffer
[32] != '\n')
111 case 32: /* plain UUID without trailing newline */
118 case 37: /* RFC UUID with trailing newline */
119 if (buffer
[36] != '\n')
123 case 36: /* RFC UUID without trailing newline */
124 if (IN_SET(f
, ID128_PLAIN
, ID128_PLAIN_OR_UNINIT
))
134 return sd_id128_from_string(buffer
, ret
);
137 int id128_read(const char *p
, Id128Format f
, sd_id128_t
*ret
) {
138 _cleanup_close_
int fd
= -1;
140 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
144 return id128_read_fd(fd
, f
, ret
);
147 int id128_write_fd(int fd
, Id128Format f
, sd_id128_t id
, bool do_sync
) {
153 assert(f
< _ID128_FORMAT_MAX
);
155 if (f
!= ID128_UUID
) {
156 sd_id128_to_string(id
, buffer
);
160 id128_to_uuid_string(id
, buffer
);
165 r
= loop_write(fd
, buffer
, sz
, false);
173 r
= fsync_directory_of_file(fd
);
181 int id128_write(const char *p
, Id128Format f
, sd_id128_t id
, bool do_sync
) {
182 _cleanup_close_
int fd
= -1;
184 fd
= open(p
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_TRUNC
, 0444);
188 return id128_write_fd(fd
, f
, id
, do_sync
);
191 void id128_hash_func(const sd_id128_t
*p
, struct siphash
*state
) {
192 siphash24_compress(p
, sizeof(sd_id128_t
), state
);
195 int id128_compare_func(const sd_id128_t
*a
, const sd_id128_t
*b
) {
196 return memcmp(a
, b
, 16);
199 sd_id128_t
id128_make_v4_uuid(sd_id128_t id
) {
200 /* Stolen from generate_random_uuid() of drivers/char/random.c
201 * in the kernel sources */
203 /* Set UUID version to 4 --- truly random generation */
204 id
.bytes
[6] = (id
.bytes
[6] & 0x0F) | 0x40;
206 /* Set the UUID variant to DCE */
207 id
.bytes
[8] = (id
.bytes
[8] & 0x3F) | 0x80;
212 DEFINE_HASH_OPS(id128_hash_ops
, sd_id128_t
, id128_hash_func
, id128_compare_func
);