diff --git a/lib/uuid.c b/lib/uuid.c
index 186a7e6..e3f31df 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
string[23] == '-');
}
+static inline int is_base_uuid128(const char *string)
+{
+ uint16_t uuid;
+ char dummy;
+
+ if (!is_uuid128(string))
+ return 0;
+
+ return sscanf(string,
+ "0000%04hx-0000-1000-8000-00805%1[fF]9%1[bB]34%1[fF]%1[bB]",
+ &uuid, &dummy, &dummy, &dummy, &dummy) == 5;
+}
+
static inline int is_uuid32(const char *string)
{
return (strlen(string) == 8 || strlen(string) == 10);
char *endptr = NULL;
u16 = strtol(string, &endptr, 16);
- if (endptr && *endptr == '\0') {
+ if (endptr && (*endptr == '\0' || *endptr == '-')) {
bt_uuid16_create(uuid, u16);
return 0;
}
int bt_string_to_uuid(bt_uuid_t *uuid, const char *string)
{
- if (is_uuid128(string))
+ if (is_base_uuid128(string))
+ return bt_string_to_uuid16(uuid, string + 4);
+ else if (is_uuid128(string))
return bt_string_to_uuid128(uuid, string);
else if (is_uuid32(string))
return bt_string_to_uuid32(uuid, string);
diff --git a/unit/test-uuid.c b/unit/test-uuid.c
index 49ea031..01f44f4 100644
--- a/unit/test-uuid.c
+++ b/unit/test-uuid.c
0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
static struct uuid_test_data uuid_base = {
- .str = "00000000-0000-1000-8000-00805f9b34fb",
- .binary = uuid_base_binary,
- .type = BT_UUID128,
+ .str = "0000",
+ .val16 = 0x0000,
+ .type = BT_UUID16,
.str128 = "00000000-0000-1000-8000-00805f9b34fb",
.binary128 = uuid_base_binary,
};
.binary128 = uuid_32_binary,
};
+static unsigned char uuid_128_binary[] = {
+ 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
+ 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
+
+static struct uuid_test_data uuid_128 = {
+ .str = "F0000000-0000-1000-8000-00805f9b34fb",
+ .binary = uuid_128_binary,
+ .type = BT_UUID128,
+ .str128 = "F0000000-0000-1000-8000-00805f9b34fb",
+ .binary128 = uuid_128_binary,
+};
+
static void test_uuid(gconstpointer data)
{
const struct uuid_test_data *test_data = data;
g_assert(bt_uuid_cmp(&uuid1, &uuid2) == 0);
}
+static const struct uuid_test_data compress[] = {
+ {
+ .str = "00001234-0000-1000-8000-00805f9b34fb",
+ .type = BT_UUID16,
+ .val16 = 0x1234,
+ }, {
+ .str = "0000FFFF-0000-1000-8000-00805f9b34fb",
+ .type = BT_UUID16,
+ .val16 = 0xFFFF,
+ }, {
+ .str = "0000FFFF-0000-1000-8000-00805F9B34FB",
+ .type = BT_UUID16,
+ .val16 = 0xFFFF,
+ }, {
+ .str = "F0000000-0000-1000-8000-00805f9b34fb",
+ .type = BT_UUID128,
+ .binary = uuid_128_binary,
+ },
+};
+
static const char *malformed[] = {
"0",
"01",
int main(int argc, char *argv[])
{
- int i;
+ size_t i;
g_test_init(&argc, &argv, NULL);
g_test_add_data_func("/uuid/thritytwo2/str", &uuid_32_2, test_str);
g_test_add_data_func("/uuid/thirtytwo2/cmp", &uuid_32_2, test_cmp);
+ g_test_add_data_func("/uuid/onetwentyeight", &uuid_128, test_uuid);
+ g_test_add_data_func("/uuid/onetwentyeight/str", &uuid_128, test_str);
+ g_test_add_data_func("/uuid/onetwentyeight/cmp", &uuid_128, test_cmp);
+
for (i = 0; malformed[i]; i++) {
char *testpath;
g_free(testpath);
}
+ for (i = 0; i < (sizeof(compress) / sizeof(compress[0])); i++) {
+ char *testpath;
+
+ testpath = g_strdup_printf("/uuid/compress/%s",
+ compress[i].str);
+ g_test_add_data_func(testpath, compress + i, test_uuid);
+ g_free(testpath);
+ }
+
return g_test_run();
}