]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-id128/id128-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "id128-util.h"
12 #include "stdio-util.h"
14 char *id128_to_uuid_string(sd_id128_t id
, char s
[static ID128_UUID_STRING_MAX
]) {
19 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
21 for (n
= 0; n
< 16; n
++) {
23 if (IN_SET(n
, 4, 6, 8, 10))
26 s
[k
++] = hexchar(id
.bytes
[n
] >> 4);
27 s
[k
++] = hexchar(id
.bytes
[n
] & 0xF);
37 bool id128_is_valid(const char *s
) {
45 /* Plain formatted 128bit hex string */
47 for (i
= 0; i
< l
; i
++) {
50 if (!(c
>= '0' && c
<= '9') &&
51 !(c
>= 'a' && c
<= 'z') &&
52 !(c
>= 'A' && c
<= 'Z'))
60 for (i
= 0; i
< l
; i
++) {
63 if (IN_SET(i
, 8, 13, 18, 23)) {
67 if (!(c
>= '0' && c
<= '9') &&
68 !(c
>= 'a' && c
<= 'z') &&
69 !(c
>= 'A' && c
<= 'Z'))
80 int id128_read_fd(int fd
, Id128Format f
, sd_id128_t
*ret
) {
85 assert(f
< _ID128_FORMAT_MAX
);
87 /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
88 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
89 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
92 l
= loop_read(fd
, buffer
, sizeof(buffer
), false); /* we expect a short read of either 32/33 or 36/37 chars */
95 if (l
== 0) /* empty? */
100 case 33: /* plain UUID with trailing newline */
101 if (buffer
[32] != '\n')
105 case 32: /* plain UUID without trailing newline */
112 case 37: /* RFC UUID with trailing newline */
113 if (buffer
[36] != '\n')
117 case 36: /* RFC UUID without trailing newline */
118 if (f
== ID128_PLAIN
)
128 return sd_id128_from_string(buffer
, ret
);
131 int id128_read(const char *p
, Id128Format f
, sd_id128_t
*ret
) {
132 _cleanup_close_
int fd
= -1;
134 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
138 return id128_read_fd(fd
, f
, ret
);
141 int id128_write_fd(int fd
, Id128Format f
, sd_id128_t id
, bool do_sync
) {
147 assert(f
< _ID128_FORMAT_MAX
);
149 if (f
!= ID128_UUID
) {
150 sd_id128_to_string(id
, buffer
);
154 id128_to_uuid_string(id
, buffer
);
159 r
= loop_write(fd
, buffer
, sz
, false);
167 r
= fsync_directory_of_file(fd
);
175 int id128_write(const char *p
, Id128Format f
, sd_id128_t id
, bool do_sync
) {
176 _cleanup_close_
int fd
= -1;
178 fd
= open(p
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_TRUNC
, 0444);
182 return id128_write_fd(fd
, f
, id
, do_sync
);
185 void id128_hash_func(const sd_id128_t
*p
, struct siphash
*state
) {
186 siphash24_compress(p
, sizeof(sd_id128_t
), state
);
189 int id128_compare_func(const sd_id128_t
*a
, const sd_id128_t
*b
) {
190 return memcmp(a
, b
, 16);
193 sd_id128_t
id128_make_v4_uuid(sd_id128_t id
) {
194 /* Stolen from generate_random_uuid() of drivers/char/random.c
195 * in the kernel sources */
197 /* Set UUID version to 4 --- truly random generation */
198 id
.bytes
[6] = (id
.bytes
[6] & 0x0F) | 0x40;
200 /* Set the UUID variant to DCE */
201 id
.bytes
[8] = (id
.bytes
[8] & 0x3F) | 0x80;
206 DEFINE_HASH_OPS(id128_hash_ops
, sd_id128_t
, id128_hash_func
, id128_compare_func
);