smm_variable: remove duplicate string size calc.

std::basic_string can not return the number of bytes needed to store
the string. The size function returns the number of character values,
which for multi byte strings is not the size but the length.
As a solution the code needs to calculate the "real" size, which was
done at multiple places with repeated code. Introduce a template
function called string_get_length_in_bytes() to remove code duplication.

Change-Id: I5d557558f9f9bb14b8905bd3afddee1faef8142a
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
diff --git a/components/service/uefi/smm_variable/client/cpp/smm_variable_client.cpp b/components/service/uefi/smm_variable/client/cpp/smm_variable_client.cpp
index 84f60ed..fbbd475 100644
--- a/components/service/uefi/smm_variable/client/cpp/smm_variable_client.cpp
+++ b/components/service/uefi/smm_variable/client/cpp/smm_variable_client.cpp
@@ -9,6 +9,11 @@
 #include <cstring>
 #include <protocols/rpc/common/packed-c/status.h>
 
+static size_t string_get_size_in_bytes(const std::u16string &string)
+{
+	return string.size() * sizeof(typename std::u16string::value_type);
+}
+
 smm_variable_client::smm_variable_client()
 	: session(NULL)
 	, m_err_rpc_status(RPC_SUCCESS)
@@ -77,7 +82,7 @@
 					       size_t override_name_size, size_t override_data_size)
 {
 	efi_status_t efi_status = EFI_NOT_READY;
-	size_t name_size = name.size() * sizeof(char16_t);
+	size_t name_size = string_get_size_in_bytes(name);
 	size_t data_size = data.size();
 	size_t req_len = SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE_SIZE(name_size, data_size);
 
@@ -150,7 +155,7 @@
 {
 	efi_status_t efi_status = EFI_NOT_READY;
 
-	size_t name_size = name.size() * sizeof(char16_t);
+	size_t name_size = string_get_size_in_bytes(name);
 	size_t req_len = SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE_SIZE(name_size, 0);
 	size_t resp_len = SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE_SIZE(name_size, 0);
 
@@ -294,7 +299,7 @@
 {
 	efi_status_t efi_status = EFI_NOT_READY;
 
-	size_t name_size = name.size() * sizeof(char16_t);
+	size_t name_size = string_get_size_in_bytes(name);
 	size_t req_len = SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME_SIZE(name_size);
 	size_t resp_len = SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME_SIZE(name_size);
 
@@ -416,7 +421,7 @@
 {
 	efi_status_t efi_status = EFI_NOT_READY;
 
-	size_t name_size = name.size() * sizeof(char16_t);
+	size_t name_size = string_get_size_in_bytes(name);
 	size_t req_len = SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY_SIZE(name_size);
 	size_t resp_len = SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY_SIZE(name_size);
 
@@ -487,7 +492,7 @@
 {
 	efi_status_t efi_status = EFI_NOT_READY;
 
-	size_t name_size = name.size() * sizeof(char16_t);
+	size_t name_size = string_get_size_in_bytes(name);
 	size_t req_len = SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY_SIZE(name_size);
 	size_t resp_len = SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY_SIZE(name_size);