2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "volume_id_internal.h"
23 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
29 for (i = 0; i + 2 <= count; i += 2) {
31 c = (buf[i+1] << 8) | buf[i];
33 c = (buf[i] << 8) | buf[i+1];
37 } else if (c < 0x80) {
40 str[j++] = (uint8_t) c;
41 } else if (c < 0x800) {
44 str[j++] = (uint8_t) (0xc0 | (c >> 6));
45 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
49 str[j++] = (uint8_t) (0xe0 | (c >> 12));
50 str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
51 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
58 static const char *usage_to_string(enum volume_id_usage usage_id)
61 case VOLUME_ID_FILESYSTEM:
63 case VOLUME_ID_PARTITIONTABLE:
64 return "partitiontable";
69 case VOLUME_ID_DISKLABEL:
71 case VOLUME_ID_CRYPTO:
73 case VOLUME_ID_UNPROBED:
75 case VOLUME_ID_UNUSED:
81 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
83 part->usage_id = usage_id;
84 part->usage = usage_to_string(usage_id);
87 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
89 id->usage_id = usage_id;
90 id->usage = usage_to_string(usage_id);
93 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
95 memcpy(id->label_raw, buf, count);
96 id->label_raw_len = count;
101 static size_t strnlen(const char *s, size_t maxlen)
104 if (!maxlen) return 0;
106 for (i = 0; *s && i < maxlen; ++s) ++i;
111 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
115 memcpy(id->label, buf, count);
117 /* remove trailing whitespace */
118 i = strnlen(id->label, count);
120 if (!isspace(id->label[i]))
123 id->label[i+1] = '\0';
126 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
128 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
131 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
147 case UUID_DCE_STRING:
148 /* 36 is ok, id->uuid has one extra byte for NUL */
149 count = VOLUME_ID_UUID_SIZE;
152 // memcpy(id->uuid_raw, buf, count);
153 // id->uuid_raw_len = count;
155 /* if set, create string in the same format, the native platform uses */
156 for (i = 0; i < count; i++)
159 return; /* all bytes are zero, leave it empty ("") */
164 sprintf(id->uuid, "%02X%02X-%02X%02X",
165 buf[3], buf[2], buf[1], buf[0]);
168 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
169 buf[7], buf[6], buf[5], buf[4],
170 buf[3], buf[2], buf[1], buf[0]);
173 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
174 buf[0], buf[1], buf[2], buf[3],
175 buf[4], buf[5], buf[6], buf[7]);
179 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
180 buf[0], buf[1], buf[2], buf[3],
184 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
186 case UUID_DCE_STRING:
187 memcpy(id->uuid, buf, count);
188 id->uuid[count] = '\0';
193 void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
197 dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
198 /* check if requested area fits in superblock buffer */
199 if (off + len <= SB_BUFFER_SIZE) {
200 if (id->sbbuf == NULL) {
201 id->sbbuf = xmalloc(SB_BUFFER_SIZE);
204 /* check if we need to read */
205 if ((off + len) > id->sbbuf_len) {
206 dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
207 xlseek(id->fd, 0, SEEK_SET);
208 buf_len = full_read(id->fd, id->sbbuf, off + len);
210 dbg("read failed (%s)", strerror(errno));
213 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
214 id->sbbuf_len = buf_len;
215 if ((uint64_t)buf_len < off + len) {
216 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
221 return &(id->sbbuf[off]);
224 if (len > SEEK_BUFFER_SIZE) {
225 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
229 /* get seek buffer */
230 if (id->seekbuf == NULL) {
231 id->seekbuf = xmalloc(SEEK_BUFFER_SIZE);
234 /* check if we need to read */
235 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
236 dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
237 xlseek(id->fd, off, SEEK_SET);
238 buf_len = full_read(id->fd, id->seekbuf, len);
240 dbg("read failed (%s)", strerror(errno));
243 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
244 id->seekbuf_off = off;
245 id->seekbuf_len = buf_len;
246 if ((size_t)buf_len < len) {
247 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
252 return &(id->seekbuf[off - id->seekbuf_off]);
255 void volume_id_free_buffer(struct volume_id *id)