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 void volume_id_set_unicode16(char *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));
188 static char *usage_to_string(enum volume_id_usage usage_id)
191 case VOLUME_ID_FILESYSTEM:
193 case VOLUME_ID_OTHER:
197 case VOLUME_ID_DISKLABEL:
199 case VOLUME_ID_CRYPTO:
201 case VOLUME_ID_UNPROBED:
203 case VOLUME_ID_UNUSED:
209 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
211 id->usage_id = usage_id;
212 id->usage = usage_to_string(usage_id);
215 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
217 memcpy(id->label_raw, buf, count);
218 id->label_raw_len = count;
221 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
225 memcpy(id->label, buf, count);
227 /* remove trailing whitespace */
228 i = strnlen(id->label, count);
230 if (!isspace(id->label[i]))
233 id->label[i+1] = '\0';
236 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
238 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
241 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
244 unsigned int count = 0;
246 if (len > sizeof(id->uuid_raw))
247 len = sizeof(id->uuid_raw);
253 case UUID_HEX_STRING:
270 memcpy(id->uuid_raw, buf, count);
271 id->uuid_raw_len = count;
273 /* if set, create string in the same format, the native platform uses */
274 for (i = 0; i < count; i++)
282 sprintf(id->uuid, "%02X%02X-%02X%02X",
283 buf[3], buf[2], buf[1], buf[0]);
286 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
287 buf[7], buf[6], buf[5], buf[4],
288 buf[3], buf[2], buf[1], buf[0]);
291 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
292 buf[0], buf[1], buf[2], buf[3],
293 buf[4], buf[5], buf[6], buf[7]);
297 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
298 buf[0], buf[1], buf[2], buf[3],
302 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
304 case UUID_HEX_STRING:
305 /* translate A..F to a..f */
306 memcpy(id->uuid, buf, count);
307 for (i = 0; i < count; i++)
308 if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
309 id->uuid[i] = (id->uuid[i] - 'A') + 'a';
310 id->uuid[count] = '\0';
313 memcpy(id->uuid, buf, count);
314 id->uuid[count] = '\0';
318 "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
319 buf[0], buf[1], buf[2], buf[3],
320 buf[4], buf[5], buf[6], buf[7],
321 buf[8], buf[9], buf[10], buf[11],
322 buf[12], buf[13], buf[14],buf[15]);
327 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
331 info("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
332 /* check if requested area fits in superblock buffer */
333 if (off + len <= SB_BUFFER_SIZE) {
334 if (id->sbbuf == NULL) {
335 id->sbbuf = malloc(SB_BUFFER_SIZE);
336 if (id->sbbuf == NULL) {
342 /* check if we need to read */
343 if ((off + len) > id->sbbuf_len) {
344 info("read sbbuf len:0x%llx", (unsigned long long) (off + len));
345 if (lseek(id->fd, 0, SEEK_SET) < 0) {
346 dbg("lseek failed (%s)", strerror(errno));
349 buf_len = read(id->fd, id->sbbuf, off + len);
351 dbg("read failed (%s)", strerror(errno));
354 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
355 id->sbbuf_len = buf_len;
356 if ((size_t)buf_len < off + len) {
357 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
362 return &(id->sbbuf[off]);
364 if (len > SEEK_BUFFER_SIZE) {
365 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
369 /* get seek buffer */
370 if (id->seekbuf == NULL) {
371 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
372 if (id->seekbuf == NULL) {
378 /* check if we need to read */
379 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
380 info("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
381 if (lseek(id->fd, off, SEEK_SET) < 0) {
382 dbg("lseek failed (%s)", strerror(errno));
385 buf_len = read(id->fd, id->seekbuf, len);
387 dbg("read failed (%s)", strerror(errno));
390 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
391 id->seekbuf_off = off;
392 id->seekbuf_len = buf_len;
393 if ((size_t)buf_len < len) {
394 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
399 return &(id->seekbuf[off - id->seekbuf_off]);
403 void volume_id_free_buffer(struct volume_id *id)
405 if (id->sbbuf != NULL) {
410 if (id->seekbuf != NULL) {