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