chiark / gitweb /
volume_id: LVM - add uuid
[elogind.git] / extras / volume_id / lib / util.c
index 09bfc6750029916e164c561d85855e2d6b616126..c094661cccd12a602d15a427f37924472d750842 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * volume_id - reads filesystem label and uuid
  *
- * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
+ * 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
 #include "libvolume_id.h"
 #include "util.h"
 
-void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
+/* count of characters used to encode one unicode char */
+static int utf8_encoded_expected_len(const char *str)
 {
-       unsigned int i, j;
+       unsigned char c = (unsigned char)str[0];
+
+       if (c < 0x80)
+               return 1;
+       if ((c & 0xe0) == 0xc0)
+               return 2;
+       if ((c & 0xf0) == 0xe0)
+               return 3;
+       if ((c & 0xf8) == 0xf0)
+               return 4;
+       if ((c & 0xfc) == 0xf8)
+               return 5;
+       if ((c & 0xfe) == 0xfc)
+               return 6;
+       return 0;
+}
+
+/* decode one unicode char */
+static int utf8_encoded_to_unichar(const char *str)
+{
+       int unichar;
+       int len;
+       int i;
+
+       len = utf8_encoded_expected_len(str);
+       switch (len) {
+       case 1:
+               return (int)str[0];
+       case 2:
+               unichar = str[0] & 0x1f;
+               break;
+       case 3:
+               unichar = (int)str[0] & 0x0f;
+               break;
+       case 4:
+               unichar = (int)str[0] & 0x07;
+               break;
+       case 5:
+               unichar = (int)str[0] & 0x03;
+               break;
+       case 6:
+               unichar = (int)str[0] & 0x01;
+               break;
+       default:
+               return -1;
+       }
+
+       for (i = 1; i < len; i++) {
+               if (((int)str[i] & 0xc0) != 0x80)
+                       return -1;
+               unichar <<= 6;
+               unichar |= (int)str[i] & 0x3f;
+       }
+
+       return unichar;
+}
+
+/* expected size used to encode one unicode char */
+static int utf8_unichar_to_encoded_len(int unichar)
+{
+       if (unichar < 0x80)
+               return 1;
+       if (unichar < 0x800)
+               return 2;
+       if (unichar < 0x10000)
+               return 3;
+       if (unichar < 0x200000)
+               return 4;
+       if (unichar < 0x4000000)
+               return 5;
+       return 6;
+}
+
+/* check if unicode char has a valid numeric range */
+static int utf8_unichar_valid_range(int unichar)
+{
+       if (unichar > 0x10ffff)
+               return 0;
+       if ((unichar & 0xfffff800) == 0xd800)
+               return 0;
+       if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
+               return 0;
+       if ((unichar & 0xffff) == 0xffff)
+               return 0;
+       return 1;
+}
+
+/* validate one encoded unicode char and return its length */
+int volume_id_utf8_encoded_valid_unichar(const char *str)
+{
+       int len;
+       int unichar;
+       int i;
+
+       len = utf8_encoded_expected_len(str);
+       if (len == 0)
+               return -1;
+
+       /* ascii is valid */
+       if (len == 1)
+               return 1;
+
+       /* check if expected encoded chars are available */
+       for (i = 0; i < len; i++)
+               if ((str[i] & 0x80) != 0x80)
+                       return -1;
+
+       unichar = utf8_encoded_to_unichar(str);
+
+       /* check if encoded length matches encoded value */
+       if (utf8_unichar_to_encoded_len(unichar) != len)
+               return -1;
+
+       /* check if value has valid range */
+       if (!utf8_unichar_valid_range(unichar))
+               return -1;
+
+       return len;
+}
+
+size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
+{
+       size_t i, j;
        uint16_t c;
 
        j = 0;
@@ -60,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)
@@ -91,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))
+               count = sizeof(id->label);
+
        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);
@@ -112,27 +243,42 @@ 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, enum uuid_format format)
+void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
 {
        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;
+               break;
+       case UUID_HEX_STRING:
+               count = len;
+               break;
        case UUID_DOS:
                count = 4;
                break;
-       case UUID_NTFS:
-       case UUID_HFS:
+       case UUID_64BIT_LE:
+       case UUID_64BIT_BE:
                count = 8;
                break;
        case UUID_DCE:
                count = 16;
                break;
-       case UUID_DCE_STRING:
-               count = 36;
+       case UUID_MD:
+               count = 35;
+               break;
+       case UUID_LVM:
+               count = 32;
                break;
        }
        memcpy(id->uuid_raw, buf, count);
@@ -150,12 +296,12 @@ set:
                sprintf(id->uuid, "%02X%02X-%02X%02X",
                        buf[3], buf[2], buf[1], buf[0]);
                break;
-       case UUID_NTFS:
+       case UUID_64BIT_LE:
                sprintf(id->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_HFS:
+       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]);
@@ -169,10 +315,37 @@ set:
                        buf[8], buf[9],
                        buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
                break;
-       case UUID_DCE_STRING:
+       case UUID_HEX_STRING:
+               /* translate A..F to a..f */
+               memcpy(id->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';
+               break;
+       case UUID_STRING:
                memcpy(id->uuid, buf, count);
                id->uuid[count] = '\0';
                break;
+       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],
+                       buf[4], buf[5], buf[6], buf[7],
+                       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;
        }
 }