chiark / gitweb /
delete all Makefiles and move udev source to udev/
[elogind.git] / extras / volume_id / lib / util.c
index eaaececadbced2ac20a0bf36dfa4df178a60e7cb..2a63efe8f7e2aa9793c3de8b361056306ef6fbee 100644 (file)
@@ -151,9 +151,9 @@ int volume_id_utf8_encoded_valid_unichar(const char *str)
        return len;
 }
 
-void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
+size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
 {
-       unsigned int i, j;
+       size_t i, j;
        uint16_t c;
 
        j = 0;
@@ -183,6 +183,7 @@ void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum end
                }
        }
        str[j] = '\0';
+       return j;
 }
 
 static char *usage_to_string(enum volume_id_usage usage_id)
@@ -214,15 +215,22 @@ void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
 
 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
 {
+       if (count > sizeof(id->label_raw))
+               count = sizeof(id->label_raw);
+
        memcpy(id->label_raw, buf, count);
        id->label_raw_len = count;
 }
 
 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
 {
-       unsigned int i;
+       size_t i;
+
+       if (count >= sizeof(id->label))
+               count = sizeof(id->label)-1;
 
        memcpy(id->label, buf, count);
+       id->label[count] = '\0';
 
        /* remove trailing whitespace */
        i = strnlen(id->label, count);
@@ -235,7 +243,10 @@ void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t
 
 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
 {
-        volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
+       if (count >= sizeof(id->label))
+               count = sizeof(id->label)-1;
+
+        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)
@@ -263,9 +274,12 @@ void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, en
        case UUID_DCE:
                count = 16;
                break;
-       case UUID_FOURINT:
+       case UUID_MD:
                count = 35;
                break;
+       case UUID_LVM:
+               count = 32;
+               break;
        }
        memcpy(id->uuid_raw, buf, count);
        id->uuid_raw_len = count;
@@ -313,7 +327,7 @@ set:
                memcpy(id->uuid, buf, count);
                id->uuid[count] = '\0';
                break;
-       case UUID_FOURINT:
+       case UUID_MD:
                sprintf(id->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],
@@ -321,6 +335,17 @@ set:
                        buf[8], buf[9], buf[10], buf[11],
                        buf[12], buf[13], buf[14],buf[15]);
                break;
+       case UUID_LVM:
+               sprintf(id->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],
+                       buf[10], buf[11], buf[12], buf[13],
+                       buf[14], buf[15], buf[16], buf[17],
+                       buf[18], buf[19], buf[20], buf[21],
+                       buf[22], buf[23], buf[24], buf[25],
+                       buf[26], buf[27], buf[28], buf[29], buf[30], buf[31]);
+               break;
        }
 }
 
@@ -328,33 +353,33 @@ 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%llx(%llu), len 0x%zx\n", (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) {
-                               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%llx\n", (unsigned long long) (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;
                        }
                }
@@ -362,7 +387,7 @@ uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
                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;
                }
 
@@ -370,28 +395,28 @@ uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
                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%llx len:0x%zx\n", (unsigned long long) 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;
                        }
                }