1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
14 #include "hexdecoct.h"
15 #include "id128-util.h"
17 #include "stdio-util.h"
19 char *id128_to_uuid_string(sd_id128_t id, char s[37]) {
24 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
26 for (n = 0; n < 16; n++) {
28 if (IN_SET(n, 4, 6, 8, 10))
31 s[k++] = hexchar(id.bytes[n] >> 4);
32 s[k++] = hexchar(id.bytes[n] & 0xF);
42 bool id128_is_valid(const char *s) {
50 /* Plain formatted 128bit hex string */
52 for (i = 0; i < l; i++) {
55 if (!(c >= '0' && c <= '9') &&
56 !(c >= 'a' && c <= 'z') &&
57 !(c >= 'A' && c <= 'Z'))
65 for (i = 0; i < l; i++) {
68 if (IN_SET(i, 8, 13, 18, 23)) {
72 if (!(c >= '0' && c <= '9') &&
73 !(c >= 'a' && c <= 'z') &&
74 !(c >= 'A' && c <= 'Z'))
85 int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
90 assert(f < _ID128_FORMAT_MAX);
92 /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
93 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
94 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
97 l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
100 if (l == 0) /* empty? */
105 case 33: /* plain UUID with trailing newline */
106 if (buffer[32] != '\n')
110 case 32: /* plain UUID without trailing newline */
117 case 37: /* RFC UUID with trailing newline */
118 if (buffer[36] != '\n')
122 case 36: /* RFC UUID without trailing newline */
123 if (f == ID128_PLAIN)
133 return sd_id128_from_string(buffer, ret);
136 int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
137 _cleanup_close_ int fd = -1;
139 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
143 return id128_read_fd(fd, f, ret);
146 int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
152 assert(f < _ID128_FORMAT_MAX);
154 if (f != ID128_UUID) {
155 sd_id128_to_string(id, buffer);
159 id128_to_uuid_string(id, buffer);
164 r = loop_write(fd, buffer, sz, false);
172 r = fsync_directory_of_file(fd);
180 #if 0 /// UNNEEDED by elogind
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 void *p, struct siphash *state) {
192 siphash24_compress(p, 16, state);
195 int id128_compare_func(const void *a, const void *b) {
196 return memcmp(a, b, 16);
199 const struct hash_ops id128_hash_ops = {
200 .hash = id128_hash_func,
201 .compare = id128_compare_func,