chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / libelogind / sd-id128 / id128-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 ***/
4
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "fd-util.h"
10 #include "fs-util.h"
11 #include "hexdecoct.h"
12 #include "id128-util.h"
13 #include "io-util.h"
14 #include "stdio-util.h"
15
16 char *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
39 bool 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
65                         if (IN_SET(i, 8, 13, 18, 23)) {
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
82 int 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
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". */
93
94         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)
96                 return (int) l;
97         if (l == 0) /* empty? */
98                 return -ENOMEDIUM;
99
100         switch (l) {
101
102         case 33: /* plain UUID with trailing newline */
103                 if (buffer[32] != '\n')
104                         return -EINVAL;
105
106                 _fallthrough_;
107         case 32: /* plain UUID without trailing newline */
108                 if (f == ID128_UUID)
109                         return -EINVAL;
110
111                 buffer[32] = 0;
112                 break;
113
114         case 37: /* RFC UUID with trailing newline */
115                 if (buffer[36] != '\n')
116                         return -EINVAL;
117
118                 _fallthrough_;
119         case 36: /* RFC UUID without trailing newline */
120                 if (f == ID128_PLAIN)
121                         return -EINVAL;
122
123                 buffer[36] = 0;
124                 break;
125
126         default:
127                 return -EINVAL;
128         }
129
130         return sd_id128_from_string(buffer, ret);
131 }
132
133 int 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
143 int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
144         char buffer[36 + 2];
145         size_t sz;
146         int r;
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
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;
168
169                 r = fsync_directory_of_file(fd);
170                 if (r < 0)
171                         return r;
172         }
173
174         return 0;
175 }
176
177 #if 0 /// UNNEEDED by elogind
178 int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
179         _cleanup_close_ int fd = -1;
180
181         fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
182         if (fd < 0)
183                 return -errno;
184
185         return id128_write_fd(fd, f, id, do_sync);
186 }
187
188 void id128_hash_func(const void *p, struct siphash *state) {
189         siphash24_compress(p, 16, state);
190 }
191
192 int id128_compare_func(const void *a, const void *b) {
193         return memcmp(a, b, 16);
194 }
195
196 const struct hash_ops id128_hash_ops = {
197         .hash = id128_hash_func,
198         .compare = id128_compare_func,
199 };
200 #endif // 0