2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
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.
28 #include "libvolume_id.h"
29 #include "libvolume_id-private.h"
32 /* count of characters used to encode one unicode char */
33 static int utf8_encoded_expected_len(const char *str)
35 unsigned char c = (unsigned char)str[0];
39 if ((c & 0xe0) == 0xc0)
41 if ((c & 0xf0) == 0xe0)
43 if ((c & 0xf8) == 0xf0)
45 if ((c & 0xfc) == 0xf8)
47 if ((c & 0xfe) == 0xfc)
52 /* decode one unicode char */
53 static int utf8_encoded_to_unichar(const char *str)
59 len = utf8_encoded_expected_len(str);
64 unichar = str[0] & 0x1f;
67 unichar = (int)str[0] & 0x0f;
70 unichar = (int)str[0] & 0x07;
73 unichar = (int)str[0] & 0x03;
76 unichar = (int)str[0] & 0x01;
82 for (i = 1; i < len; i++) {
83 if (((int)str[i] & 0xc0) != 0x80)
86 unichar |= (int)str[i] & 0x3f;
92 /* expected size used to encode one unicode char */
93 static int utf8_unichar_to_encoded_len(int unichar)
99 if (unichar < 0x10000)
101 if (unichar < 0x200000)
103 if (unichar < 0x4000000)
108 /* check if unicode char has a valid numeric range */
109 static int utf8_unichar_valid_range(int unichar)
111 if (unichar > 0x10ffff)
113 if ((unichar & 0xfffff800) == 0xd800)
115 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
117 if ((unichar & 0xffff) == 0xffff)
122 /* validate one encoded unicode char and return its length */
123 int volume_id_utf8_encoded_valid_unichar(const char *str)
129 len = utf8_encoded_expected_len(str);
137 /* check if expected encoded chars are available */
138 for (i = 0; i < len; i++)
139 if ((str[i] & 0x80) != 0x80)
142 unichar = utf8_encoded_to_unichar(str);
144 /* check if encoded length matches encoded value */
145 if (utf8_unichar_to_encoded_len(unichar) != len)
148 /* check if value has valid range */
149 if (!utf8_unichar_valid_range(unichar))
155 size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
161 for (i = 0; i + 2 <= count; i += 2) {
163 c = (buf[i+1] << 8) | buf[i];
165 c = (buf[i] << 8) | buf[i+1];
169 } else if (c < 0x80) {
172 str[j++] = (uint8_t) c;
173 } else if (c < 0x800) {
176 str[j++] = (uint8_t) (0xc0 | (c >> 6));
177 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
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));
190 static char *usage_to_string(enum volume_id_usage usage_id)
193 case VOLUME_ID_FILESYSTEM:
195 case VOLUME_ID_OTHER:
199 case VOLUME_ID_DISKLABEL:
201 case VOLUME_ID_CRYPTO:
203 case VOLUME_ID_UNPROBED:
205 case VOLUME_ID_UNUSED:
211 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
213 id->usage_id = usage_id;
214 id->usage = usage_to_string(usage_id);
217 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
219 if (count > sizeof(id->label_raw))
220 count = sizeof(id->label_raw);
222 memcpy(id->label_raw, buf, count);
223 id->label_raw_len = count;
226 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
230 if (count >= sizeof(id->label))
231 count = sizeof(id->label)-1;
233 memcpy(id->label, buf, count);
234 id->label[count] = '\0';
236 /* remove trailing whitespace */
237 i = strnlen(id->label, count);
239 if (!isspace(id->label[i]))
242 id->label[i+1] = '\0';
245 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
247 if (count >= sizeof(id->label))
248 count = sizeof(id->label)-1;
250 volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count);
253 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
256 unsigned int count = 0;
258 if (len > sizeof(id->uuid_raw))
259 len = sizeof(id->uuid_raw);
265 case UUID_HEX_STRING:
284 memcpy(id->uuid_raw, buf, count);
285 id->uuid_raw_len = count;
287 /* if set, create string in the same format, the native platform uses */
288 for (i = 0; i < count; i++)
296 sprintf(id->uuid, "%02X%02X-%02X%02X",
297 buf[3], buf[2], buf[1], buf[0]);
300 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
301 buf[7], buf[6], buf[5], buf[4],
302 buf[3], buf[2], buf[1], buf[0]);
306 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
307 buf[0], buf[1], buf[2], buf[3],
311 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
313 case UUID_HEX_STRING:
314 /* translate A..F to a..f */
315 memcpy(id->uuid, buf, count);
316 for (i = 0; i < count; i++)
317 if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
318 id->uuid[i] = (id->uuid[i] - 'A') + 'a';
319 id->uuid[count] = '\0';
322 memcpy(id->uuid, buf, count);
323 id->uuid[count] = '\0';
327 "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
328 buf[0], buf[1], buf[2], buf[3],
329 buf[4], buf[5], buf[6], buf[7],
330 buf[8], buf[9], buf[10], buf[11],
331 buf[12], buf[13], buf[14],buf[15]);
335 "%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",
336 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
337 buf[6], buf[7], buf[8], buf[9],
338 buf[10], buf[11], buf[12], buf[13],
339 buf[14], buf[15], buf[16], buf[17],
340 buf[18], buf[19], buf[20], buf[21],
341 buf[22], buf[23], buf[24], buf[25],
342 buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]);
347 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
351 info("get buffer off 0x%llx(%llu), len 0x%zx\n", (unsigned long long) off, (unsigned long long) off, len);
352 /* check if requested area fits in superblock buffer */
353 if (off + len <= SB_BUFFER_SIZE) {
354 if (id->sbbuf == NULL) {
355 id->sbbuf = malloc(SB_BUFFER_SIZE);
356 if (id->sbbuf == NULL) {
357 dbg("error malloc\n");
362 /* check if we need to read */
363 if ((off + len) > id->sbbuf_len) {
364 info("read sbbuf len:0x%llx\n", (unsigned long long) (off + len));
365 if (lseek(id->fd, 0, SEEK_SET) < 0) {
366 dbg("lseek failed (%s)\n", strerror(errno));
369 buf_len = read(id->fd, id->sbbuf, off + len);
371 dbg("read failed (%s)\n", strerror(errno));
374 dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
375 id->sbbuf_len = buf_len;
376 if ((size_t)buf_len < off + len) {
377 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
382 return &(id->sbbuf[off]);
384 if (len > SEEK_BUFFER_SIZE) {
385 dbg("seek buffer too small %d\n", SEEK_BUFFER_SIZE);
389 /* get seek buffer */
390 if (id->seekbuf == NULL) {
391 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
392 if (id->seekbuf == NULL) {
393 dbg("error malloc\n");
398 /* check if we need to read */
399 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
400 info("read seekbuf off:0x%llx len:0x%zx\n", (unsigned long long) off, len);
401 if (lseek(id->fd, off, SEEK_SET) < 0) {
402 dbg("lseek failed (%s)\n", strerror(errno));
405 buf_len = read(id->fd, id->seekbuf, len);
407 dbg("read failed (%s)\n", strerror(errno));
410 dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
411 id->seekbuf_off = off;
412 id->seekbuf_len = buf_len;
413 if ((size_t)buf_len < len) {
414 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
419 return &(id->seekbuf[off - id->seekbuf_off]);
423 void volume_id_free_buffer(struct volume_id *id)
425 if (id->sbbuf != NULL) {
430 if (id->seekbuf != NULL) {