chiark / gitweb /
volume_id: support sub-uuid's and plug in btrfs device uuid
[elogind.git] / extras / volume_id / lib / util.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
35
36 /* count of characters used to encode one unicode char */
37 static int utf8_encoded_expected_len(const char *str)
38 {
39         unsigned char c = (unsigned char)str[0];
40
41         if (c < 0x80)
42                 return 1;
43         if ((c & 0xe0) == 0xc0)
44                 return 2;
45         if ((c & 0xf0) == 0xe0)
46                 return 3;
47         if ((c & 0xf8) == 0xf0)
48                 return 4;
49         if ((c & 0xfc) == 0xf8)
50                 return 5;
51         if ((c & 0xfe) == 0xfc)
52                 return 6;
53         return 0;
54 }
55
56 /* decode one unicode char */
57 static int utf8_encoded_to_unichar(const char *str)
58 {
59         int unichar;
60         int len;
61         int i;
62
63         len = utf8_encoded_expected_len(str);
64         switch (len) {
65         case 1:
66                 return (int)str[0];
67         case 2:
68                 unichar = str[0] & 0x1f;
69                 break;
70         case 3:
71                 unichar = (int)str[0] & 0x0f;
72                 break;
73         case 4:
74                 unichar = (int)str[0] & 0x07;
75                 break;
76         case 5:
77                 unichar = (int)str[0] & 0x03;
78                 break;
79         case 6:
80                 unichar = (int)str[0] & 0x01;
81                 break;
82         default:
83                 return -1;
84         }
85
86         for (i = 1; i < len; i++) {
87                 if (((int)str[i] & 0xc0) != 0x80)
88                         return -1;
89                 unichar <<= 6;
90                 unichar |= (int)str[i] & 0x3f;
91         }
92
93         return unichar;
94 }
95
96 /* expected size used to encode one unicode char */
97 static int utf8_unichar_to_encoded_len(int unichar)
98 {
99         if (unichar < 0x80)
100                 return 1;
101         if (unichar < 0x800)
102                 return 2;
103         if (unichar < 0x10000)
104                 return 3;
105         if (unichar < 0x200000)
106                 return 4;
107         if (unichar < 0x4000000)
108                 return 5;
109         return 6;
110 }
111
112 /* check if unicode char has a valid numeric range */
113 static int utf8_unichar_valid_range(int unichar)
114 {
115         if (unichar > 0x10ffff)
116                 return 0;
117         if ((unichar & 0xfffff800) == 0xd800)
118                 return 0;
119         if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
120                 return 0;
121         if ((unichar & 0xffff) == 0xffff)
122                 return 0;
123         return 1;
124 }
125
126 /* validate one encoded unicode char and return its length */
127 int volume_id_utf8_encoded_valid_unichar(const char *str)
128 {
129         int len;
130         int unichar;
131         int i;
132
133         len = utf8_encoded_expected_len(str);
134         if (len == 0)
135                 return -1;
136
137         /* ascii is valid */
138         if (len == 1)
139                 return 1;
140
141         /* check if expected encoded chars are available */
142         for (i = 0; i < len; i++)
143                 if ((str[i] & 0x80) != 0x80)
144                         return -1;
145
146         unichar = utf8_encoded_to_unichar(str);
147
148         /* check if encoded length matches encoded value */
149         if (utf8_unichar_to_encoded_len(unichar) != len)
150                 return -1;
151
152         /* check if value has valid range */
153         if (!utf8_unichar_valid_range(unichar))
154                 return -1;
155
156         return len;
157 }
158
159 size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
160 {
161         size_t i, j;
162         uint16_t c;
163
164         j = 0;
165         for (i = 0; i + 2 <= count; i += 2) {
166                 if (endianess == LE)
167                         c = (buf[i+1] << 8) | buf[i];
168                 else
169                         c = (buf[i] << 8) | buf[i+1];
170                 if (c == 0) {
171                         str[j] = '\0';
172                         break;
173                 } else if (c < 0x80) {
174                         if (j+1 >= len)
175                                 break;
176                         str[j++] = (uint8_t) c;
177                 } else if (c < 0x800) {
178                         if (j+2 >= len)
179                                 break;
180                         str[j++] = (uint8_t) (0xc0 | (c >> 6));
181                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
182                 } else {
183                         if (j+3 >= len)
184                                 break;
185                         str[j++] = (uint8_t) (0xe0 | (c >> 12));
186                         str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
187                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
188                 }
189         }
190         str[j] = '\0';
191         return j;
192 }
193
194 static char *usage_to_string(enum volume_id_usage usage_id)
195 {
196         switch (usage_id) {
197         case VOLUME_ID_FILESYSTEM:
198                 return "filesystem";
199         case VOLUME_ID_OTHER:
200                 return "other";
201         case VOLUME_ID_RAID:
202                 return "raid";
203         case VOLUME_ID_CRYPTO:
204                 return "crypto";
205         case VOLUME_ID_UNUSED:
206                 return "unused";
207         }
208         return NULL;
209 }
210
211 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
212 {
213         id->usage_id = usage_id;
214         id->usage = usage_to_string(usage_id);
215 }
216
217 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
218 {
219         if (count > sizeof(id->label_raw))
220                 count = sizeof(id->label_raw);
221
222         memcpy(id->label_raw, buf, count);
223         id->label_raw_len = count;
224 }
225
226 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
227 {
228         size_t i;
229
230         if (count >= sizeof(id->label))
231                 count = sizeof(id->label)-1;
232
233         memcpy(id->label, buf, count);
234         id->label[count] = '\0';
235
236         /* remove trailing whitespace */
237         i = strnlen(id->label, count);
238         while (i--) {
239                 if (!isspace(id->label[i]))
240                         break;
241         }
242         id->label[i+1] = '\0';
243 }
244
245 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
246 {
247         if (count >= sizeof(id->label))
248                 count = sizeof(id->label)-1;
249
250          volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count);
251 }
252
253 static void set_uuid(const uint8_t *buf, size_t len, enum uuid_format format,
254                      char *uuid, uint8_t *uuid_raw, size_t *uuid_raw_len)
255 {
256         unsigned int i;
257         unsigned int count = 0;
258
259         switch(format) {
260         case UUID_STRING:
261                 count = len;
262                 break;
263         case UUID_HEX_STRING:
264                 count = len;
265                 break;
266         case UUID_DOS:
267                 count = 4;
268                 break;
269         case UUID_64BIT_LE:
270                 count = 8;
271                 break;
272         case UUID_DCE:
273                 count = 16;
274                 break;
275         case UUID_MD:
276                 count = 35;
277                 break;
278         case UUID_LVM:
279                 count = 32;
280                 break;
281         }
282         memcpy(uuid_raw, buf, count);
283         *uuid_raw_len = count;
284
285         /* if set, create string in the same format, the native platform uses */
286         for (i = 0; i < count; i++)
287                 if (buf[i] != 0)
288                         goto set;
289         return;
290
291 set:
292         switch(format) {
293         case UUID_DOS:
294                 sprintf(uuid, "%02X%02X-%02X%02X",
295                         buf[3], buf[2], buf[1], buf[0]);
296                 break;
297         case UUID_64BIT_LE:
298                 sprintf(uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
299                         buf[7], buf[6], buf[5], buf[4],
300                         buf[3], buf[2], buf[1], buf[0]);
301                 break;
302         case UUID_DCE:
303                 sprintf(uuid,
304                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
305                         buf[0], buf[1], buf[2], buf[3],
306                         buf[4], buf[5],
307                         buf[6], buf[7],
308                         buf[8], buf[9],
309                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
310                 break;
311         case UUID_HEX_STRING:
312                 /* translate A..F to a..f */
313                 memcpy(uuid, buf, count);
314                 for (i = 0; i < count; i++)
315                         if (uuid[i] >= 'A' && uuid[i] <= 'F')
316                                 uuid[i] = (uuid[i] - 'A') + 'a';
317                 uuid[count] = '\0';
318                 break;
319         case UUID_STRING:
320                 memcpy(uuid, buf, count);
321                 uuid[count] = '\0';
322                 break;
323         case UUID_MD:
324                 sprintf(uuid,
325                         "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
326                         buf[0], buf[1], buf[2], buf[3],
327                         buf[4], buf[5], buf[6], buf[7],
328                         buf[8], buf[9], buf[10], buf[11],
329                         buf[12], buf[13], buf[14],buf[15]);
330                 break;
331         case UUID_LVM:
332                 sprintf(uuid,
333                         "%c%c%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c%c%c",
334                         buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
335                         buf[6], buf[7], buf[8], buf[9],
336                         buf[10], buf[11], buf[12], buf[13],
337                         buf[14], buf[15], buf[16], buf[17],
338                         buf[18], buf[19], buf[20], buf[21],
339                         buf[22], buf[23], buf[24], buf[25],
340                         buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]);
341                 break;
342         }
343 }
344
345 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
346 {
347         if (len > sizeof(id->uuid_raw))
348                 len = sizeof(id->uuid_raw);
349
350         set_uuid(buf, len, format, id->uuid, id->uuid_raw, &id->uuid_raw_len);
351 }
352
353 void volume_id_set_uuid_sub(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
354 {
355         if (len > sizeof(id->uuid_sub_raw))
356                 len = sizeof(id->uuid_sub_raw);
357
358         set_uuid(buf, len, format, id->uuid_sub, id->uuid_sub_raw, &id->uuid_sub_raw_len);
359 }
360
361 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
362 {
363         ssize_t buf_len;
364
365         info("get buffer off 0x%" PRIx64 "(%" PRIu64 "), len 0x%zx\n", off, off, len);
366         /* check if requested area fits in superblock buffer */
367         if (off + len <= SB_BUFFER_SIZE) {
368                 if (id->sbbuf == NULL) {
369                         id->sbbuf = malloc(SB_BUFFER_SIZE);
370                         if (id->sbbuf == NULL) {
371                                 dbg("error malloc\n");
372                                 return NULL;
373                         }
374                 }
375
376                 /* check if we need to read */
377                 if ((off + len) > id->sbbuf_len) {
378                         info("read sbbuf len:0x%" PRIx64 "\n", (off + len));
379                         if (lseek(id->fd, 0, SEEK_SET) < 0) {
380                                 dbg("lseek failed (%s)\n", strerror(errno));
381                                 return NULL;
382                         }
383                         buf_len = read(id->fd, id->sbbuf, off + len);
384                         if (buf_len < 0) {
385                                 dbg("read failed (%s)\n", strerror(errno));
386                                 return NULL;
387                         }
388                         dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
389                         id->sbbuf_len = buf_len;
390                         if ((size_t)buf_len < off + len) {
391                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
392                                 return NULL;
393                         }
394                 }
395
396                 return &(id->sbbuf[off]);
397         } else {
398                 if (len > SEEK_BUFFER_SIZE) {
399                         dbg("seek buffer too small %d\n", SEEK_BUFFER_SIZE);
400                         return NULL;
401                 }
402
403                 /* get seek buffer */
404                 if (id->seekbuf == NULL) {
405                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
406                         if (id->seekbuf == NULL) {
407                                 dbg("error malloc\n");
408                                 return NULL;
409                         }
410                 }
411
412                 /* check if we need to read */
413                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
414                         info("read seekbuf off:0x%" PRIx64 " len:0x%zx\n", off, len);
415                         if (lseek(id->fd, off, SEEK_SET) < 0) {
416                                 dbg("lseek failed (%s)\n", strerror(errno));
417                                 return NULL;
418                         }
419                         buf_len = read(id->fd, id->seekbuf, len);
420                         if (buf_len < 0) {
421                                 dbg("read failed (%s)\n", strerror(errno));
422                                 return NULL;
423                         }
424                         dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
425                         id->seekbuf_off = off;
426                         id->seekbuf_len = buf_len;
427                         if ((size_t)buf_len < len) {
428                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
429                                 return NULL;
430                         }
431                 }
432
433                 return &(id->seekbuf[off - id->seekbuf_off]);
434         }
435 }
436
437 void volume_id_free_buffer(struct volume_id *id)
438 {
439         if (id->sbbuf != NULL) {
440                 free(id->sbbuf);
441                 id->sbbuf = NULL;
442                 id->sbbuf_len = 0;
443         }
444         if (id->seekbuf != NULL) {
445                 free(id->seekbuf);
446                 id->seekbuf = NULL;
447                 id->seekbuf_len = 0;
448         }
449 }