X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=extras%2Fvolume_id%2Fvolume_id%2Futil.c;h=d1271a86be6519bbe156156d77971438473c9b29;hp=6159a5992800edcf9415555eb8f9e0391b79c1e2;hb=a3f129d054395019d4b2385dede233eb12425c78;hpb=e4d4a557e53c3fbcf14bd1bb29686fd884905b53 diff --git a/extras/volume_id/volume_id/util.c b/extras/volume_id/volume_id/util.c index 6159a5992..d1271a86b 100644 --- a/extras/volume_id/volume_id/util.c +++ b/extras/volume_id/volume_id/util.c @@ -34,12 +34,45 @@ #include #include #include -#include #include "volume_id.h" #include "logging.h" #include "util.h" +void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count) +{ + unsigned int i, j; + uint16_t c; + + j = 0; + for (i = 0; i + 2 <= count; i += 2) { + if (endianess == LE) + c = (buf[i+1] << 8) | buf[i]; + else + c = (buf[i] << 8) | buf[i+1]; + if (c == 0) { + str[j] = '\0'; + break; + } else if (c < 0x80) { + if (j+1 >= len) + break; + str[j++] = (uint8_t) c; + } else if (c < 0x800) { + if (j+2 >= len) + break; + str[j++] = (uint8_t) (0xc0 | (c >> 6)); + str[j++] = (uint8_t) (0x80 | (c & 0x3f)); + } else { + if (j+3 >= len) + break; + str[j++] = (uint8_t) (0xe0 | (c >> 12)); + str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); + str[j++] = (uint8_t) (0x80 | (c & 0x3f)); + } + } + str[j] = '\0'; +} + static char *usage_to_string(enum volume_id_usage usage_id) { switch (usage_id) { @@ -75,13 +108,13 @@ void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id) id->usage = usage_to_string(usage_id); } -void volume_id_set_label_raw(struct volume_id *id, const __u8 *buf, unsigned int count) +void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count) { memcpy(id->label_raw, buf, count); id->label_raw_len = count; } -void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned int count) +void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count) { unsigned int i; @@ -96,34 +129,12 @@ void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned id->label[i+1] = '\0'; } -void volume_id_set_label_unicode16(struct volume_id *id, const __u8 *buf, enum endian endianess, unsigned int count) +void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count) { - unsigned int i, j; - __u16 c; - - j = 0; - for (i = 0; i + 2 <= count; i += 2) { - if (endianess == LE) - c = (buf[i+1] << 8) | buf[i]; - else - c = (buf[i] << 8) | buf[i+1]; - if (c == 0) { - id->label[j] = '\0'; - break; - } else if (c < 0x80) { - id->label[j++] = (__u8) c; - } else if (c < 0x800) { - id->label[j++] = (__u8) (0xc0 | (c >> 6)); - id->label[j++] = (__u8) (0x80 | (c & 0x3f)); - } else { - id->label[j++] = (__u8) (0xe0 | (c >> 12)); - id->label[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f)); - id->label[j++] = (__u8) (0x80 | (c & 0x3f)); - } - } + volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count); } -void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format) +void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format) { unsigned int i; unsigned int count = 0; @@ -138,8 +149,13 @@ void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format break; case UUID_DCE: count = 16; + break; + case UUID_DCE_STRING: + count = 36; + break; } memcpy(id->uuid_raw, buf, count); + id->uuid_raw_len = count; /* if set, create string in the same format, the native platform uses */ for (i = 0; i < count; i++) @@ -172,31 +188,46 @@ set: buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]); break; + case UUID_DCE_STRING: + memcpy(id->uuid, buf, count); + id->uuid[count] = '\0'; + break; } } -__u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len) +uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len) { - unsigned int buf_len; + ssize_t buf_len; - dbg("get buffer off 0x%llx(%llu), len 0x%x", off, off, len); + dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) 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) + if (id->sbbuf == NULL) { + dbg("error malloc"); return NULL; + } } /* check if we need to read */ if ((off + len) > id->sbbuf_len) { - dbg("read sbbuf len:0x%llx", off + len); - lseek(id->fd, 0, SEEK_SET); + dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len)); + if (lseek(id->fd, 0, SEEK_SET) < 0) { + dbg("lseek failed (%s)", strerror(errno)); + return NULL; + } buf_len = read(id->fd, id->sbbuf, off + len); - dbg("got 0x%x (%i) bytes", buf_len, buf_len); + if (buf_len < 0) { + dbg("read failed (%s)", strerror(errno)); + return NULL; + } + dbg("got 0x%zx (%zi) bytes", buf_len, buf_len); id->sbbuf_len = buf_len; - if (buf_len < off + len) + if ((size_t)buf_len < off + len) { + dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len); return NULL; + } } return &(id->sbbuf[off]); @@ -209,21 +240,29 @@ __u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len) /* get seek buffer */ if (id->seekbuf == NULL) { id->seekbuf = malloc(SEEK_BUFFER_SIZE); - if (id->seekbuf == NULL) + if (id->seekbuf == NULL) { + dbg("error malloc"); return NULL; + } } /* check if we need to read */ if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) { - dbg("read seekbuf off:0x%llx len:0x%x", off, len); - if (lseek(id->fd, off, SEEK_SET) == -1) + dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len); + if (lseek(id->fd, off, SEEK_SET) < 0) { + dbg("lseek failed (%s)", strerror(errno)); return NULL; + } buf_len = read(id->fd, id->seekbuf, len); - dbg("got 0x%x (%i) bytes", buf_len, buf_len); + if (buf_len < 0) { + dbg("read failed (%s)", strerror(errno)); + return NULL; + } + dbg("got 0x%zx (%zi) bytes", buf_len, buf_len); id->seekbuf_off = off; id->seekbuf_len = buf_len; - if (buf_len < len) { - dbg("requested 0x%x bytes, got only 0x%x bytes", len, buf_len); + if ((size_t)buf_len < len) { + dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len); return NULL; } }