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"
31 /* count of characters used to encode one unicode char */
32 static int utf8_encoded_expected_len(const char *str)
34 unsigned char c = (unsigned char)str[0];
38 if ((c & 0xe0) == 0xc0)
40 if ((c & 0xf0) == 0xe0)
42 if ((c & 0xf8) == 0xf0)
44 if ((c & 0xfc) == 0xf8)
46 if ((c & 0xfe) == 0xfc)
51 /* decode one unicode char */
52 static int utf8_encoded_to_unichar(const char *str)
58 len = utf8_encoded_expected_len(str);
63 unichar = str[0] & 0x1f;
66 unichar = (int)str[0] & 0x0f;
69 unichar = (int)str[0] & 0x07;
72 unichar = (int)str[0] & 0x03;
75 unichar = (int)str[0] & 0x01;
81 for (i = 1; i < len; i++) {
82 if (((int)str[i] & 0xc0) != 0x80)
85 unichar |= (int)str[i] & 0x3f;
91 /* expected size used to encode one unicode char */
92 static int utf8_unichar_to_encoded_len(int unichar)
98 if (unichar < 0x10000)
100 if (unichar < 0x200000)
102 if (unichar < 0x4000000)
107 /* check if unicode char has a valid numeric range */
108 static int utf8_unichar_valid_range(int unichar)
110 if (unichar > 0x10ffff)
112 if ((unichar & 0xfffff800) == 0xd800)
114 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
116 if ((unichar & 0xffff) == 0xffff)
121 /* validate one encoded unicode char and return its length */
122 int volume_id_utf8_encoded_valid_unichar(const char *str)
128 len = utf8_encoded_expected_len(str);
136 /* check if expected encoded chars are available */
137 for (i = 0; i < len; i++)
138 if ((str[i] & 0x80) != 0x80)
141 unichar = utf8_encoded_to_unichar(str);
143 /* check if encoded length matches encoded value */
144 if (utf8_unichar_to_encoded_len(unichar) != len)
147 /* check if value has valid range */
148 if (!utf8_unichar_valid_range(unichar))
154 size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
160 for (i = 0; i + 2 <= count; i += 2) {
162 c = (buf[i+1] << 8) | buf[i];
164 c = (buf[i] << 8) | buf[i+1];
168 } else if (c < 0x80) {
171 str[j++] = (uint8_t) c;
172 } else if (c < 0x800) {
175 str[j++] = (uint8_t) (0xc0 | (c >> 6));
176 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
180 str[j++] = (uint8_t) (0xe0 | (c >> 12));
181 str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
182 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
189 static char *usage_to_string(enum volume_id_usage usage_id)
192 case VOLUME_ID_FILESYSTEM:
194 case VOLUME_ID_OTHER:
198 case VOLUME_ID_DISKLABEL:
200 case VOLUME_ID_CRYPTO:
202 case VOLUME_ID_UNPROBED:
204 case VOLUME_ID_UNUSED:
210 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
212 id->usage_id = usage_id;
213 id->usage = usage_to_string(usage_id);
216 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
218 if (count > sizeof(id->label))
219 count = sizeof(id->label);
221 memcpy(id->label_raw, buf, count);
222 id->label_raw_len = count;
225 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
229 if (count >= sizeof(id->label))
230 count = sizeof(id->label)-1;
232 memcpy(id->label, buf, count);
233 id->label[count] = '\0';
235 /* remove trailing whitespace */
236 i = strnlen(id->label, count);
238 if (!isspace(id->label[i]))
241 id->label[i+1] = '\0';
244 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
246 if (count >= sizeof(id->label))
247 count = sizeof(id->label)-1;
249 volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count);
252 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
255 unsigned int count = 0;
257 if (len > sizeof(id->uuid_raw))
258 len = sizeof(id->uuid_raw);
264 case UUID_HEX_STRING:
281 memcpy(id->uuid_raw, buf, count);
282 id->uuid_raw_len = count;
284 /* if set, create string in the same format, the native platform uses */
285 for (i = 0; i < count; i++)
293 sprintf(id->uuid, "%02X%02X-%02X%02X",
294 buf[3], buf[2], buf[1], buf[0]);
297 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
298 buf[7], buf[6], buf[5], buf[4],
299 buf[3], buf[2], buf[1], buf[0]);
302 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
303 buf[0], buf[1], buf[2], buf[3],
304 buf[4], buf[5], buf[6], buf[7]);
308 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
309 buf[0], buf[1], buf[2], buf[3],
313 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
315 case UUID_HEX_STRING:
316 /* translate A..F to a..f */
317 memcpy(id->uuid, buf, count);
318 for (i = 0; i < count; i++)
319 if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
320 id->uuid[i] = (id->uuid[i] - 'A') + 'a';
321 id->uuid[count] = '\0';
324 memcpy(id->uuid, buf, count);
325 id->uuid[count] = '\0';
329 "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
330 buf[0], buf[1], buf[2], buf[3],
331 buf[4], buf[5], buf[6], buf[7],
332 buf[8], buf[9], buf[10], buf[11],
333 buf[12], buf[13], buf[14],buf[15]);
338 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
342 info("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
343 /* check if requested area fits in superblock buffer */
344 if (off + len <= SB_BUFFER_SIZE) {
345 if (id->sbbuf == NULL) {
346 id->sbbuf = malloc(SB_BUFFER_SIZE);
347 if (id->sbbuf == NULL) {
353 /* check if we need to read */
354 if ((off + len) > id->sbbuf_len) {
355 info("read sbbuf len:0x%llx", (unsigned long long) (off + len));
356 if (lseek(id->fd, 0, SEEK_SET) < 0) {
357 dbg("lseek failed (%s)", strerror(errno));
360 buf_len = read(id->fd, id->sbbuf, off + len);
362 dbg("read failed (%s)", strerror(errno));
365 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
366 id->sbbuf_len = buf_len;
367 if ((size_t)buf_len < off + len) {
368 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
373 return &(id->sbbuf[off]);
375 if (len > SEEK_BUFFER_SIZE) {
376 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
380 /* get seek buffer */
381 if (id->seekbuf == NULL) {
382 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
383 if (id->seekbuf == NULL) {
389 /* check if we need to read */
390 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
391 info("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
392 if (lseek(id->fd, off, SEEK_SET) < 0) {
393 dbg("lseek failed (%s)", strerror(errno));
396 buf_len = read(id->fd, id->seekbuf, len);
398 dbg("read failed (%s)", strerror(errno));
401 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
402 id->seekbuf_off = off;
403 id->seekbuf_len = buf_len;
404 if ((size_t)buf_len < len) {
405 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
410 return &(id->seekbuf[off - id->seekbuf_off]);
414 void volume_id_free_buffer(struct volume_id *id)
416 if (id->sbbuf != NULL) {
421 if (id->seekbuf != NULL) {