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
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.
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.
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/>.
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
36 /* count of characters used to encode one unicode char */
37 static int utf8_encoded_expected_len(const char *str)
39 unsigned char c = (unsigned char)str[0];
43 if ((c & 0xe0) == 0xc0)
45 if ((c & 0xf0) == 0xe0)
47 if ((c & 0xf8) == 0xf0)
49 if ((c & 0xfc) == 0xf8)
51 if ((c & 0xfe) == 0xfc)
56 /* decode one unicode char */
57 static int utf8_encoded_to_unichar(const char *str)
63 len = utf8_encoded_expected_len(str);
68 unichar = str[0] & 0x1f;
71 unichar = (int)str[0] & 0x0f;
74 unichar = (int)str[0] & 0x07;
77 unichar = (int)str[0] & 0x03;
80 unichar = (int)str[0] & 0x01;
86 for (i = 1; i < len; i++) {
87 if (((int)str[i] & 0xc0) != 0x80)
90 unichar |= (int)str[i] & 0x3f;
96 /* expected size used to encode one unicode char */
97 static int utf8_unichar_to_encoded_len(int unichar)
103 if (unichar < 0x10000)
105 if (unichar < 0x200000)
107 if (unichar < 0x4000000)
112 /* check if unicode char has a valid numeric range */
113 static int utf8_unichar_valid_range(int unichar)
115 if (unichar > 0x10ffff)
117 if ((unichar & 0xfffff800) == 0xd800)
119 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
121 if ((unichar & 0xffff) == 0xffff)
126 /* validate one encoded unicode char and return its length */
127 int volume_id_utf8_encoded_valid_unichar(const char *str)
133 len = utf8_encoded_expected_len(str);
141 /* check if expected encoded chars are available */
142 for (i = 0; i < len; i++)
143 if ((str[i] & 0x80) != 0x80)
146 unichar = utf8_encoded_to_unichar(str);
148 /* check if encoded length matches encoded value */
149 if (utf8_unichar_to_encoded_len(unichar) != len)
152 /* check if value has valid range */
153 if (!utf8_unichar_valid_range(unichar))
159 size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
165 for (i = 0; i + 2 <= count; i += 2) {
167 c = (buf[i+1] << 8) | buf[i];
169 c = (buf[i] << 8) | buf[i+1];
173 } else if (c < 0x80) {
176 str[j++] = (uint8_t) c;
177 } else if (c < 0x800) {
180 str[j++] = (uint8_t) (0xc0 | (c >> 6));
181 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
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));
194 static char *usage_to_string(enum volume_id_usage usage_id)
197 case VOLUME_ID_FILESYSTEM:
199 case VOLUME_ID_OTHER:
203 case VOLUME_ID_DISKLABEL:
205 case VOLUME_ID_CRYPTO:
207 case VOLUME_ID_UNPROBED:
209 case VOLUME_ID_UNUSED:
215 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
217 id->usage_id = usage_id;
218 id->usage = usage_to_string(usage_id);
221 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
223 if (count > sizeof(id->label_raw))
224 count = sizeof(id->label_raw);
226 memcpy(id->label_raw, buf, count);
227 id->label_raw_len = count;
230 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
234 if (count >= sizeof(id->label))
235 count = sizeof(id->label)-1;
237 memcpy(id->label, buf, count);
238 id->label[count] = '\0';
240 /* remove trailing whitespace */
241 i = strnlen(id->label, count);
243 if (!isspace(id->label[i]))
246 id->label[i+1] = '\0';
249 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
251 if (count >= sizeof(id->label))
252 count = sizeof(id->label)-1;
254 volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count);
257 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
260 unsigned int count = 0;
262 if (len > sizeof(id->uuid_raw))
263 len = sizeof(id->uuid_raw);
269 case UUID_HEX_STRING:
288 memcpy(id->uuid_raw, buf, count);
289 id->uuid_raw_len = count;
291 /* if set, create string in the same format, the native platform uses */
292 for (i = 0; i < count; i++)
300 sprintf(id->uuid, "%02X%02X-%02X%02X",
301 buf[3], buf[2], buf[1], buf[0]);
304 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
305 buf[7], buf[6], buf[5], buf[4],
306 buf[3], buf[2], buf[1], buf[0]);
310 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
311 buf[0], buf[1], buf[2], buf[3],
315 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
317 case UUID_HEX_STRING:
318 /* translate A..F to a..f */
319 memcpy(id->uuid, buf, count);
320 for (i = 0; i < count; i++)
321 if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
322 id->uuid[i] = (id->uuid[i] - 'A') + 'a';
323 id->uuid[count] = '\0';
326 memcpy(id->uuid, buf, count);
327 id->uuid[count] = '\0';
331 "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
332 buf[0], buf[1], buf[2], buf[3],
333 buf[4], buf[5], buf[6], buf[7],
334 buf[8], buf[9], buf[10], buf[11],
335 buf[12], buf[13], buf[14],buf[15]);
339 "%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",
340 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
341 buf[6], buf[7], buf[8], buf[9],
342 buf[10], buf[11], buf[12], buf[13],
343 buf[14], buf[15], buf[16], buf[17],
344 buf[18], buf[19], buf[20], buf[21],
345 buf[22], buf[23], buf[24], buf[25],
346 buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]);
351 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
355 info("get buffer off 0x%llx(%llu), len 0x%zx\n", (unsigned long long) off, (unsigned long long) off, len);
356 /* check if requested area fits in superblock buffer */
357 if (off + len <= SB_BUFFER_SIZE) {
358 if (id->sbbuf == NULL) {
359 id->sbbuf = malloc(SB_BUFFER_SIZE);
360 if (id->sbbuf == NULL) {
361 dbg("error malloc\n");
366 /* check if we need to read */
367 if ((off + len) > id->sbbuf_len) {
368 info("read sbbuf len:0x%llx\n", (unsigned long long) (off + len));
369 if (lseek(id->fd, 0, SEEK_SET) < 0) {
370 dbg("lseek failed (%s)\n", strerror(errno));
373 buf_len = read(id->fd, id->sbbuf, off + len);
375 dbg("read failed (%s)\n", strerror(errno));
378 dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
379 id->sbbuf_len = buf_len;
380 if ((size_t)buf_len < off + len) {
381 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
386 return &(id->sbbuf[off]);
388 if (len > SEEK_BUFFER_SIZE) {
389 dbg("seek buffer too small %d\n", SEEK_BUFFER_SIZE);
393 /* get seek buffer */
394 if (id->seekbuf == NULL) {
395 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
396 if (id->seekbuf == NULL) {
397 dbg("error malloc\n");
402 /* check if we need to read */
403 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
404 info("read seekbuf off:0x%llx len:0x%zx\n", (unsigned long long) off, len);
405 if (lseek(id->fd, off, SEEK_SET) < 0) {
406 dbg("lseek failed (%s)\n", strerror(errno));
409 buf_len = read(id->fd, id->seekbuf, len);
411 dbg("read failed (%s)\n", strerror(errno));
414 dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
415 id->seekbuf_off = off;
416 id->seekbuf_len = buf_len;
417 if ((size_t)buf_len < len) {
418 dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
423 return &(id->seekbuf[off - id->seekbuf_off]);
427 void volume_id_free_buffer(struct volume_id *id)
429 if (id->sbbuf != NULL) {
434 if (id->seekbuf != NULL) {