mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
uuid: add uuid_from_string()
This commit is contained in:
parent
695a94449a
commit
73e971fc56
@ -122,7 +122,7 @@ void uuid_v5(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len);
|
||||
*
|
||||
* @return Version number
|
||||
*/
|
||||
static inline unsigned uuid_version(uuid_t *uuid)
|
||||
static inline unsigned uuid_version(const uuid_t *uuid)
|
||||
{
|
||||
uint16_t time_hi_vers = byteorder_ntohs(uuid->time_hi);
|
||||
|
||||
@ -137,7 +137,7 @@ static inline unsigned uuid_version(uuid_t *uuid)
|
||||
*
|
||||
* @return True when equal
|
||||
*/
|
||||
static inline bool uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
|
||||
static inline bool uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
|
||||
{
|
||||
return (memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0);
|
||||
}
|
||||
@ -150,6 +150,16 @@ static inline bool uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
|
||||
*/
|
||||
void uuid_to_string(const uuid_t *uuid, char *str);
|
||||
|
||||
/**
|
||||
* @brief Populate an UUID structure from an UUID string
|
||||
*
|
||||
* @param[out] uuid out UUID
|
||||
* @param[in] str null-terminated input UUID string, must be UUID_STR_LEN bytes
|
||||
*
|
||||
* @return 0 on succes, < 0 if @p str is not valid
|
||||
*/
|
||||
int uuid_from_string(uuid_t *uuid, const char *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -128,3 +128,42 @@ void uuid_to_string(const uuid_t *uuid, char *str)
|
||||
*p = '\0';
|
||||
fmt_to_lower(str, str);
|
||||
}
|
||||
|
||||
int uuid_from_string(uuid_t *uuid, const char *str)
|
||||
{
|
||||
uint32_t tmp;
|
||||
if (fmt_strlen(str) < UUID_STR_LEN) {
|
||||
return -1;
|
||||
}
|
||||
tmp = scn_u32_hex(str, 8);
|
||||
uuid->time_low = byteorder_htonl(tmp);
|
||||
str += 8;
|
||||
if (*str++ != '-') {
|
||||
return -2;
|
||||
}
|
||||
tmp = scn_u32_hex(str, 4);
|
||||
uuid->time_mid = byteorder_htons(tmp);
|
||||
str += 4;
|
||||
if (*str++ != '-') {
|
||||
return -2;
|
||||
}
|
||||
tmp = scn_u32_hex(str, 4);
|
||||
uuid->time_hi = byteorder_htons(tmp);
|
||||
str += 4;
|
||||
if (*str++ != '-') {
|
||||
return -2;
|
||||
}
|
||||
uuid->clk_seq_hi_res = scn_u32_hex(str, 2);
|
||||
str += 2;
|
||||
uuid->clk_seq_low = scn_u32_hex(str, 2);
|
||||
str += 2;
|
||||
if (*str++ != '-') {
|
||||
return -2;
|
||||
}
|
||||
for (unsigned i = 0; i < UUID_NODE_LEN; i++) {
|
||||
uuid->node[i] = scn_u32_hex(str, 2);
|
||||
str += 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -113,6 +113,19 @@ void test_uuid_str(void)
|
||||
const char x500[] = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
|
||||
uuid_to_string(&uuid_namespace_x500, str);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(x500, str, sizeof(dns)));
|
||||
|
||||
uuid_t uuid;
|
||||
TEST_ASSERT_EQUAL_INT(0, uuid_from_string(&uuid, dns));
|
||||
TEST_ASSERT_EQUAL_INT(true, uuid_equal(&uuid, &uuid_namespace_dns));
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, uuid_from_string(&uuid, url));
|
||||
TEST_ASSERT_EQUAL_INT(true, uuid_equal(&uuid, &uuid_namespace_url));
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, uuid_from_string(&uuid, iso));
|
||||
TEST_ASSERT_EQUAL_INT(true, uuid_equal(&uuid, &uuid_namespace_iso));
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, uuid_from_string(&uuid, x500));
|
||||
TEST_ASSERT_EQUAL_INT(true, uuid_equal(&uuid, &uuid_namespace_x500));
|
||||
}
|
||||
|
||||
Test *tests_uuid_all(void)
|
||||
|
Loading…
Reference in New Issue
Block a user