From: Hyuk Lee Date: Thu, 27 Apr 2017 11:55:48 +0000 (+0900) Subject: Change sscanf to strtol function X-Git-Tag: accepted/tizen/unified/20170428.033210^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9e3f6d767af4e920052e4b4720466899ca3c1397;p=platform%2Fcore%2Fapi%2Fbluetooth.git Change sscanf to strtol function Change-Id: I539194a1b8338ec66b4f7704d6145d241092125a Signed-off-by: Hyuk Lee --- diff --git a/src/bluetooth-adapter.c b/src/bluetooth-adapter.c index 606172d..6154e03 100644 --- a/src/bluetooth-adapter.c +++ b/src/bluetooth-adapter.c @@ -1839,7 +1839,12 @@ static int __bt_convert_string_to_uuid(const char *string, return BT_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */ } - int ret; + char str_ptr[36] = { 0 }; + char *ptr[7]; + char *next_ptr; + char *stop; + int idx = 0; + unsigned int val0, val4; unsigned short val1, val2, val3, val5; data = g_malloc0(sizeof(char) *16); @@ -1847,13 +1852,29 @@ static int __bt_convert_string_to_uuid(const char *string, if (data == NULL) return BT_ERROR_OUT_OF_MEMORY; - ret = sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx", - &val0, &val1, &val2, &val3, &val4, &val5); - if (ret != 6) { - g_free(data); - return BT_ERROR_OPERATION_FAILED; + /* UUID format : %08x-%04hx-%04hx-%04hx-%08x%04hx */ + strncpy(str_ptr, string, 36); + + ptr[idx++] = strtok_r(str_ptr, "-", &next_ptr); + while ((ptr[idx++] = strtok_r(NULL, "-", &next_ptr)) != NULL) { + BT_INFO("ptr : %s", ptr[idx - 1]); + if (idx == 5) + break; } + /* ptr[4] contain "08x" and "04hx" */ + ptr[5] = g_malloc0(sizeof(char) * 8); + ptr[6] = g_malloc0(sizeof(char) * 4); + strncpy(ptr[5], ptr[4], 8); + strncpy(ptr[6], ptr[4] + 8, 4); + + val0 = strtol(ptr[0], &stop, 16); + val1 = strtol(ptr[1], &stop, 16); + val2 = strtol(ptr[2], &stop, 16); + val3 = strtol(ptr[3], &stop, 16); + val4 = strtol(ptr[5], &stop, 16); + val5 = strtol(ptr[6], &stop, 16); + val0 = htonl(val0); val1 = htons(val1); val2 = htons(val2); @@ -1861,6 +1882,8 @@ static int __bt_convert_string_to_uuid(const char *string, val4 = htonl(val4); val5 = htons(val5); + BT_INFO("%x, %x, %x, %x, %x, %x", val0, val1, val2, val3, val4, val5); + memcpy(data, &val0, 4); memcpy(data + 4, &val1, 2); memcpy(data + 6, &val2, 2); @@ -1870,6 +1893,9 @@ static int __bt_convert_string_to_uuid(const char *string, *uuid = data; *bit = 128; + + g_free(ptr[5]); + g_free(ptr[6]); } else { BT_ERR("Invalid UUID"); /* LCOV_EXCL_LINE */ return BT_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */ diff --git a/src/bluetooth-common.c b/src/bluetooth-common.c index 08e4a0f..3a76f72 100644 --- a/src/bluetooth-common.c +++ b/src/bluetooth-common.c @@ -555,18 +555,17 @@ int _bt_convert_address_to_string(char **addr_str, bluetooth_device_address_t *a void _bt_convert_address_to_hex(bluetooth_device_address_t *addr_hex, const char *addr_str) { - int i = 0; - unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, }; + char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5; if (addr_str == NULL || addr_str[0] == '\0') - return; /* LCOV_EXCL_LINE */ - - i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]); - if (i != BLUETOOTH_ADDRESS_LENGTH) - BT_ERR("Invalid format string - [%s]", addr_str); /* LCOV_EXCL_LINE */ + return; - for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++) - addr_hex->addr[i] = (unsigned char)addr[i]; + addr_hex->addr[0] = strtol(addr_str, &ptr5, 16); + addr_hex->addr[1] = strtol(ptr5 + 1, &ptr4, 16); + addr_hex->addr[2] = strtol(ptr4 + 1, &ptr3, 16); + addr_hex->addr[3] = strtol(ptr3 + 1, &ptr2, 16); + addr_hex->addr[4] = strtol(ptr2 + 1, &ptr1, 16); + addr_hex->addr[5] = strtol(ptr1 + 1, NULL, 16); } char *_bt_convert_error_to_string(int error)