chiark / gitweb /
volume_id: remove deprecated functions and bump major version
[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 it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "libvolume_id-private.h"
30 #include "util.h"
31
32 /* count of characters used to encode one unicode char */
33 static int utf8_encoded_expected_len(const char *str)
34 {
35         unsigned char c = (unsigned char)str[0];
36
37         if (c < 0x80)
38                 return 1;
39         if ((c & 0xe0) == 0xc0)
40                 return 2;
41         if ((c & 0xf0) == 0xe0)
42                 return 3;
43         if ((c & 0xf8) == 0xf0)
44                 return 4;
45         if ((c & 0xfc) == 0xf8)
46                 return 5;
47         if ((c & 0xfe) == 0xfc)
48                 return 6;
49         return 0;
50 }
51
52 /* decode one unicode char */
53 static int utf8_encoded_to_unichar(const char *str)
54 {
55         int unichar;
56         int len;
57         int i;
58
59         len = utf8_encoded_expected_len(str);
60         switch (len) {
61         case 1:
62                 return (int)str[0];
63         case 2:
64                 unichar = str[0] & 0x1f;
65                 break;
66         case 3:
67                 unichar = (int)str[0] & 0x0f;
68                 break;
69         case 4:
70                 unichar = (int)str[0] & 0x07;
71                 break;
72         case 5:
73                 unichar = (int)str[0] & 0x03;
74                 break;
75         case 6:
76                 unichar = (int)str[0] & 0x01;
77                 break;
78         default:
79                 return -1;
80         }
81
82         for (i = 1; i < len; i++) {
83                 if (((int)str[i] & 0xc0) != 0x80)
84                         return -1;
85                 unichar <<= 6;
86                 unichar |= (int)str[i] & 0x3f;
87         }
88
89         return unichar;
90 }
91
92 /* expected size used to encode one unicode char */
93 static int utf8_unichar_to_encoded_len(int unichar)
94 {
95         if (unichar < 0x80)
96                 return 1;
97         if (unichar < 0x800)
98                 return 2;
99         if (unichar < 0x10000)
100                 return 3;
101         if (unichar < 0x200000)
102                 return 4;
103         if (unichar < 0x4000000)
104                 return 5;
105         return 6;
106 }
107
108 /* check if unicode char has a valid numeric range */
109 static int utf8_unichar_valid_range(int unichar)
110 {
111         if (unichar > 0x10ffff)
112                 return 0;
113         if ((unichar & 0xfffff800) == 0xd800)
114                 return 0;
115         if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
116                 return 0;
117         if ((unichar & 0xffff) == 0xffff)
118                 return 0;
119         return 1;
120 }
121
122 /* validate one encoded unicode char and return its length */
123 int volume_id_utf8_encoded_valid_unichar(const char *str)
124 {
125         int len;
126         int unichar;
127         int i;
128
129         len = utf8_encoded_expected_len(str);
130         if (len == 0)
131                 return -1;
132
133         /* ascii is valid */
134         if (len == 1)
135                 return 1;
136
137         /* check if expected encoded chars are available */
138         for (i = 0; i < len; i++)
139                 if ((str[i] & 0x80) != 0x80)
140                         return -1;
141
142         unichar = utf8_encoded_to_unichar(str);
143
144         /* check if encoded length matches encoded value */
145         if (utf8_unichar_to_encoded_len(unichar) != len)
146                 return -1;
147
148         /* check if value has valid range */
149         if (!utf8_unichar_valid_range(unichar))
150                 return -1;
151
152         return len;
153 }
154
155 size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
156 {
157         size_t i, j;
158         uint16_t c;
159
160         j = 0;
161         for (i = 0; i + 2 <= count; i += 2) {
162                 if (endianess == LE)
163                         c = (buf[i+1] << 8) | buf[i];
164                 else
165                         c = (buf[i] << 8) | buf[i+1];
166                 if (c == 0) {
167                         str[j] = '\0';
168                         break;
169                 } else if (c < 0x80) {
170                         if (j+1 >= len)
171                                 break;
172                         str[j++] = (uint8_t) c;
173                 } else if (c < 0x800) {
174                         if (j+2 >= len)
175                                 break;
176                         str[j++] = (uint8_t) (0xc0 | (c >> 6));
177                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
178                 } else {
179                         if (j+3 >= len)
180                                 break;
181                         str[j++] = (uint8_t) (0xe0 | (c >> 12));
182                         str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
183                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
184                 }
185         }
186         str[j] = '\0';
187         return j;
188 }
189
190 static char *usage_to_string(enum volume_id_usage usage_id)
191 {
192         switch (usage_id) {
193         case VOLUME_ID_FILESYSTEM:
194                 return "filesystem";
195         case VOLUME_ID_OTHER:
196                 return "other";
197         case VOLUME_ID_RAID:
198                 return "raid";
199         case VOLUME_ID_DISKLABEL:
200                 return "disklabel";
201         case VOLUME_ID_CRYPTO:
202                 return "crypto";
203         case VOLUME_ID_UNPROBED:
204                 return "unprobed";
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 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
254 {
255         unsigned int i;
256         unsigned int count = 0;
257
258         if (len > sizeof(id->uuid_raw))
259                 len = sizeof(id->uuid_raw);
260
261         switch(format) {
262         case UUID_STRING:
263                 count = len;
264                 break;
265         case UUID_HEX_STRING:
266                 count = len;
267                 break;
268         case UUID_DOS:
269                 count = 4;
270                 break;
271         case UUID_64BIT_LE:
272         case UUID_64BIT_BE:
273                 count = 8;
274                 break;
275         case UUID_DCE:
276                 count = 16;
277                 break;
278         case UUID_MD:
279                 count = 35;
280                 break;
281         case UUID_LVM:
282                 count = 32;
283                 break;
284         }
285         memcpy(id->uuid_raw, buf, count);
286         id->uuid_raw_len = count;
287
288         /* if set, create string in the same format, the native platform uses */
289         for (i = 0; i < count; i++)
290                 if (buf[i] != 0)
291                         goto set;
292         return;
293
294 set:
295         switch(format) {
296         case UUID_DOS:
297                 sprintf(id->uuid, "%02X%02X-%02X%02X",
298                         buf[3], buf[2], buf[1], buf[0]);
299                 break;
300         case UUID_64BIT_LE:
301                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
302                         buf[7], buf[6], buf[5], buf[4],
303                         buf[3], buf[2], buf[1], buf[0]);
304                 break;
305         case UUID_64BIT_BE:
306                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
307                         buf[0], buf[1], buf[2], buf[3],
308                         buf[4], buf[5], buf[6], buf[7]);
309                 break;
310         case UUID_DCE:
311                 sprintf(id->uuid,
312                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
313                         buf[0], buf[1], buf[2], buf[3],
314                         buf[4], buf[5],
315                         buf[6], buf[7],
316                         buf[8], buf[9],
317                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
318                 break;
319         case UUID_HEX_STRING:
320                 /* translate A..F to a..f */
321                 memcpy(id->uuid, buf, count);
322                 for (i = 0; i < count; i++)
323                         if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
324                                 id->uuid[i] = (id->uuid[i] - 'A') + 'a';
325                 id->uuid[count] = '\0';
326                 break;
327         case UUID_STRING:
328                 memcpy(id->uuid, buf, count);
329                 id->uuid[count] = '\0';
330                 break;
331         case UUID_MD:
332                 sprintf(id->uuid,
333                         "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
334                         buf[0], buf[1], buf[2], buf[3],
335                         buf[4], buf[5], buf[6], buf[7],
336                         buf[8], buf[9], buf[10], buf[11],
337                         buf[12], buf[13], buf[14],buf[15]);
338                 break;
339         case UUID_LVM:
340                 sprintf(id->uuid,
341                         "%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",
342                         buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
343                         buf[6], buf[7], buf[8], buf[9],
344                         buf[10], buf[11], buf[12], buf[13],
345                         buf[14], buf[15], buf[16], buf[17],
346                         buf[18], buf[19], buf[20], buf[21],
347                         buf[22], buf[23], buf[24], buf[25],
348                         buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]);
349                 break;
350         }
351 }
352
353 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
354 {
355         ssize_t buf_len;
356
357         info("get buffer off 0x%llx(%llu), len 0x%zx\n", (unsigned long long) off, (unsigned long long) off, len);
358         /* check if requested area fits in superblock buffer */
359         if (off + len <= SB_BUFFER_SIZE) {
360                 if (id->sbbuf == NULL) {
361                         id->sbbuf = malloc(SB_BUFFER_SIZE);
362                         if (id->sbbuf == NULL) {
363                                 dbg("error malloc\n");
364                                 return NULL;
365                         }
366                 }
367
368                 /* check if we need to read */
369                 if ((off + len) > id->sbbuf_len) {
370                         info("read sbbuf len:0x%llx\n", (unsigned long long) (off + len));
371                         if (lseek(id->fd, 0, SEEK_SET) < 0) {
372                                 dbg("lseek failed (%s)\n", strerror(errno));
373                                 return NULL;
374                         }
375                         buf_len = read(id->fd, id->sbbuf, off + len);
376                         if (buf_len < 0) {
377                                 dbg("read failed (%s)\n", strerror(errno));
378                                 return NULL;
379                         }
380                         dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
381                         id->sbbuf_len = buf_len;
382                         if ((size_t)buf_len < off + len) {
383                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
384                                 return NULL;
385                         }
386                 }
387
388                 return &(id->sbbuf[off]);
389         } else {
390                 if (len > SEEK_BUFFER_SIZE) {
391                         dbg("seek buffer too small %d\n", SEEK_BUFFER_SIZE);
392                         return NULL;
393                 }
394
395                 /* get seek buffer */
396                 if (id->seekbuf == NULL) {
397                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
398                         if (id->seekbuf == NULL) {
399                                 dbg("error malloc\n");
400                                 return NULL;
401                         }
402                 }
403
404                 /* check if we need to read */
405                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
406                         info("read seekbuf off:0x%llx len:0x%zx\n", (unsigned long long) off, len);
407                         if (lseek(id->fd, off, SEEK_SET) < 0) {
408                                 dbg("lseek failed (%s)\n", strerror(errno));
409                                 return NULL;
410                         }
411                         buf_len = read(id->fd, id->seekbuf, len);
412                         if (buf_len < 0) {
413                                 dbg("read failed (%s)\n", strerror(errno));
414                                 return NULL;
415                         }
416                         dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
417                         id->seekbuf_off = off;
418                         id->seekbuf_len = buf_len;
419                         if ((size_t)buf_len < len) {
420                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
421                                 return NULL;
422                         }
423                 }
424
425                 return &(id->seekbuf[off - id->seekbuf_off]);
426         }
427 }
428
429 void volume_id_free_buffer(struct volume_id *id)
430 {
431         if (id->sbbuf != NULL) {
432                 free(id->sbbuf);
433                 id->sbbuf = NULL;
434                 id->sbbuf_len = 0;
435         }
436         if (id->seekbuf != NULL) {
437                 free(id->seekbuf);
438                 id->seekbuf = NULL;
439                 id->seekbuf_len = 0;
440         }
441 }