]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-id128/id128-util.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / libsystemd / sd-id128 / id128-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
910fd145 2/***
910fd145
LP
3***/
4
dccca82b 5#include <errno.h>
910fd145 6#include <fcntl.h>
15b1248a 7#include <unistd.h>
910fd145
LP
8
9#include "fd-util.h"
8ac2f74f 10#include "fs-util.h"
910fd145
LP
11#include "hexdecoct.h"
12#include "id128-util.h"
13#include "io-util.h"
14#include "stdio-util.h"
15
16char *id128_to_uuid_string(sd_id128_t id, char s[37]) {
17 unsigned n, k = 0;
18
19 assert(s);
20
21 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
22
23 for (n = 0; n < 16; n++) {
24
25 if (IN_SET(n, 4, 6, 8, 10))
26 s[k++] = '-';
27
28 s[k++] = hexchar(id.bytes[n] >> 4);
29 s[k++] = hexchar(id.bytes[n] & 0xF);
30 }
31
32 assert(k == 36);
33
34 s[k] = 0;
35
36 return s;
37}
38
39bool id128_is_valid(const char *s) {
40 size_t i, l;
41
42 assert(s);
43
44 l = strlen(s);
45 if (l == 32) {
46
47 /* Plain formatted 128bit hex string */
48
49 for (i = 0; i < l; i++) {
50 char c = s[i];
51
52 if (!(c >= '0' && c <= '9') &&
53 !(c >= 'a' && c <= 'z') &&
54 !(c >= 'A' && c <= 'Z'))
55 return false;
56 }
57
58 } else if (l == 36) {
59
60 /* Formatted UUID */
61
62 for (i = 0; i < l; i++) {
63 char c = s[i];
64
945c2931 65 if (IN_SET(i, 8, 13, 18, 23)) {
910fd145
LP
66 if (c != '-')
67 return false;
68 } else {
69 if (!(c >= '0' && c <= '9') &&
70 !(c >= 'a' && c <= 'z') &&
71 !(c >= 'A' && c <= 'Z'))
72 return false;
73 }
74 }
75
76 } else
77 return false;
78
79 return true;
80}
81
82int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
83 char buffer[36 + 2];
84 ssize_t l;
85
86 assert(fd >= 0);
87 assert(f < _ID128_FORMAT_MAX);
88
89 /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
65548c58
LP
90 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
91 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
92 * accept". */
910fd145 93
65548c58 94 l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
910fd145
LP
95 if (l < 0)
96 return (int) l;
97 if (l == 0) /* empty? */
98 return -ENOMEDIUM;
99
65548c58 100 switch (l) {
910fd145 101
65548c58 102 case 33: /* plain UUID with trailing newline */
910fd145
LP
103 if (buffer[32] != '\n')
104 return -EINVAL;
105
4831981d 106 _fallthrough_;
65548c58
LP
107 case 32: /* plain UUID without trailing newline */
108 if (f == ID128_UUID)
109 return -EINVAL;
110
910fd145 111 buffer[32] = 0;
65548c58 112 break;
910fd145 113
65548c58
LP
114 case 37: /* RFC UUID with trailing newline */
115 if (buffer[36] != '\n')
910fd145
LP
116 return -EINVAL;
117
4831981d 118 _fallthrough_;
65548c58
LP
119 case 36: /* RFC UUID without trailing newline */
120 if (f == ID128_PLAIN)
910fd145
LP
121 return -EINVAL;
122
123 buffer[36] = 0;
65548c58
LP
124 break;
125
126 default:
910fd145 127 return -EINVAL;
65548c58 128 }
910fd145
LP
129
130 return sd_id128_from_string(buffer, ret);
131}
132
133int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
134 _cleanup_close_ int fd = -1;
135
136 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
137 if (fd < 0)
138 return -errno;
139
140 return id128_read_fd(fd, f, ret);
141}
142
15b1248a 143int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
910fd145
LP
144 char buffer[36 + 2];
145 size_t sz;
15b1248a 146 int r;
910fd145
LP
147
148 assert(fd >= 0);
149 assert(f < _ID128_FORMAT_MAX);
150
151 if (f != ID128_UUID) {
152 sd_id128_to_string(id, buffer);
153 buffer[32] = '\n';
154 sz = 33;
155 } else {
156 id128_to_uuid_string(id, buffer);
157 buffer[36] = '\n';
158 sz = 37;
159 }
160
15b1248a
LP
161 r = loop_write(fd, buffer, sz, false);
162 if (r < 0)
163 return r;
164
165 if (do_sync) {
166 if (fsync(fd) < 0)
167 return -errno;
8ac2f74f
LP
168
169 r = fsync_directory_of_file(fd);
170 if (r < 0)
171 return r;
15b1248a
LP
172 }
173
8ac2f74f 174 return 0;
910fd145
LP
175}
176
15b1248a 177int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
910fd145
LP
178 _cleanup_close_ int fd = -1;
179
da2d1421 180 fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
910fd145
LP
181 if (fd < 0)
182 return -errno;
183
15b1248a 184 return id128_write_fd(fd, f, id, do_sync);
910fd145 185}
4b58153d
LP
186
187void id128_hash_func(const void *p, struct siphash *state) {
188 siphash24_compress(p, 16, state);
189}
190
191int id128_compare_func(const void *a, const void *b) {
192 return memcmp(a, b, 16);
193}
194
195const struct hash_ops id128_hash_ops = {
196 .hash = id128_hash_func,
197 .compare = id128_compare_func,
198};