*
* Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation version 2 of the License.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "libvolume_id.h"
-#include "util.h"
+#include "libvolume_id-private.h"
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str)
return "other";
case VOLUME_ID_RAID:
return "raid";
- case VOLUME_ID_DISKLABEL:
- return "disklabel";
case VOLUME_ID_CRYPTO:
return "crypto";
- case VOLUME_ID_UNPROBED:
- return "unprobed";
case VOLUME_ID_UNUSED:
return "unused";
}
volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count);
}
-void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
+static void set_uuid(const uint8_t *buf, size_t len, enum uuid_format format,
+ char *uuid, uint8_t *uuid_raw, size_t *uuid_raw_len)
{
unsigned int i;
unsigned int count = 0;
- if (len > sizeof(id->uuid_raw))
- len = sizeof(id->uuid_raw);
-
switch(format) {
case UUID_STRING:
count = len;
count = 4;
break;
case UUID_64BIT_LE:
- case UUID_64BIT_BE:
count = 8;
break;
case UUID_DCE:
count = 32;
break;
}
- memcpy(id->uuid_raw, buf, count);
- id->uuid_raw_len = count;
+ memcpy(uuid_raw, buf, count);
+ *uuid_raw_len = count;
/* if set, create string in the same format, the native platform uses */
for (i = 0; i < count; i++)
set:
switch(format) {
case UUID_DOS:
- sprintf(id->uuid, "%02X%02X-%02X%02X",
+ sprintf(uuid, "%02X%02X-%02X%02X",
buf[3], buf[2], buf[1], buf[0]);
break;
case UUID_64BIT_LE:
- sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
+ sprintf(uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
buf[7], buf[6], buf[5], buf[4],
buf[3], buf[2], buf[1], buf[0]);
break;
- case UUID_64BIT_BE:
- sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
- buf[0], buf[1], buf[2], buf[3],
- buf[4], buf[5], buf[6], buf[7]);
- break;
case UUID_DCE:
- sprintf(id->uuid,
+ sprintf(uuid,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5],
break;
case UUID_HEX_STRING:
/* translate A..F to a..f */
- memcpy(id->uuid, buf, count);
+ memcpy(uuid, buf, count);
for (i = 0; i < count; i++)
- if (id->uuid[i] >= 'A' && id->uuid[i] <= 'F')
- id->uuid[i] = (id->uuid[i] - 'A') + 'a';
- id->uuid[count] = '\0';
+ if (uuid[i] >= 'A' && uuid[i] <= 'F')
+ uuid[i] = (uuid[i] - 'A') + 'a';
+ uuid[count] = '\0';
break;
case UUID_STRING:
- memcpy(id->uuid, buf, count);
- id->uuid[count] = '\0';
+ memcpy(uuid, buf, count);
+ uuid[count] = '\0';
break;
case UUID_MD:
- sprintf(id->uuid,
+ sprintf(uuid,
"%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7],
buf[12], buf[13], buf[14],buf[15]);
break;
case UUID_LVM:
- sprintf(id->uuid,
+ sprintf(uuid,
"%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",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
buf[6], buf[7], buf[8], buf[9],
}
}
+void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
+{
+ if (len > sizeof(id->uuid_raw))
+ len = sizeof(id->uuid_raw);
+
+ set_uuid(buf, len, format, id->uuid, id->uuid_raw, &id->uuid_raw_len);
+}
+
+void volume_id_set_uuid_sub(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
+{
+ if (len > sizeof(id->uuid_sub_raw))
+ len = sizeof(id->uuid_sub_raw);
+
+ set_uuid(buf, len, format, id->uuid_sub, id->uuid_sub_raw, &id->uuid_sub_raw_len);
+}
+
uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
{
ssize_t buf_len;
- info("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
+ info("get buffer off 0x%" PRIx64 "(%" PRIu64 "), len 0x%zx\n", off, off, len);
/* check if requested area fits in superblock buffer */
if (off + len <= SB_BUFFER_SIZE) {
if (id->sbbuf == NULL) {
id->sbbuf = malloc(SB_BUFFER_SIZE);
if (id->sbbuf == NULL) {
- dbg("error malloc");
+ dbg("error malloc\n");
return NULL;
}
}
/* check if we need to read */
if ((off + len) > id->sbbuf_len) {
- info("read sbbuf len:0x%llx", (unsigned long long) (off + len));
+ info("read sbbuf len:0x%" PRIx64 "\n", (off + len));
if (lseek(id->fd, 0, SEEK_SET) < 0) {
- dbg("lseek failed (%s)", strerror(errno));
+ dbg("lseek failed (%s)\n", strerror(errno));
return NULL;
}
buf_len = read(id->fd, id->sbbuf, off + len);
if (buf_len < 0) {
- dbg("read failed (%s)", strerror(errno));
+ dbg("read failed (%s)\n", strerror(errno));
return NULL;
}
- dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
+ dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
id->sbbuf_len = buf_len;
if ((size_t)buf_len < off + len) {
- dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
+ dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
return NULL;
}
}
return &(id->sbbuf[off]);
} else {
if (len > SEEK_BUFFER_SIZE) {
- dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
+ dbg("seek buffer too small %d\n", SEEK_BUFFER_SIZE);
return NULL;
}
if (id->seekbuf == NULL) {
id->seekbuf = malloc(SEEK_BUFFER_SIZE);
if (id->seekbuf == NULL) {
- dbg("error malloc");
+ dbg("error malloc\n");
return NULL;
}
}
/* check if we need to read */
if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
- info("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
+ info("read seekbuf off:0x%" PRIx64 " len:0x%zx\n", off, len);
if (lseek(id->fd, off, SEEK_SET) < 0) {
- dbg("lseek failed (%s)", strerror(errno));
+ dbg("lseek failed (%s)\n", strerror(errno));
return NULL;
}
buf_len = read(id->fd, id->seekbuf, len);
if (buf_len < 0) {
- dbg("read failed (%s)", strerror(errno));
+ dbg("read failed (%s)\n", strerror(errno));
return NULL;
}
- dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
+ dbg("got 0x%zx (%zi) bytes\n", buf_len, buf_len);
id->seekbuf_off = off;
id->seekbuf_len = buf_len;
if ((size_t)buf_len < len) {
- dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
+ dbg("requested 0x%zx bytes, got only 0x%zx bytes\n", len, buf_len);
return NULL;
}
}