From 45fccb0203d8a7c366f09e88edd4b7940817c44c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Leroy?= <frederic.leroy@b-com.com>
Date: Tue, 26 Jul 2016 08:47:57 +0200
Subject: [PATCH] UE/API/USIM: move hex functions to common/utils.c

---
 cmake_targets/nas_sim_tools/CMakeLists.txt |  4 ++
 common/utils/utils.c                       | 51 ++++++++++++++++++++
 common/utils/utils.h                       |  6 +++
 openair3/NAS/UE/API/USIM/usim_api.c        | 56 +---------------------
 4 files changed, 63 insertions(+), 54 deletions(-)

diff --git a/cmake_targets/nas_sim_tools/CMakeLists.txt b/cmake_targets/nas_sim_tools/CMakeLists.txt
index 732ede3f940..897e4a17735 100644
--- a/cmake_targets/nas_sim_tools/CMakeLists.txt
+++ b/cmake_targets/nas_sim_tools/CMakeLists.txt
@@ -27,6 +27,7 @@ set(usim_SRC
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/nas_log.c
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/OctetString.c
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/TLVEncoder.c
+    ${OPENAIR_DIR}/common/utils/utils.c
 )
 set(usim_HDR
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/network.h
@@ -36,8 +37,11 @@ set(usim_HDR
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/nas_log.h
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/OctetString.h
     ${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/TLVEncoder.h
+    ${OPENAIR_DIR}/common/utils/utils.h
 )
 include_directories(
+    ${OPENAIR_DIR}/common/utils
+    ${OPENAIR_DIR}/openair3/NAS/UE
     ${OPENAIR_DIR}/openair3/NAS/COMMON
     ${OPENAIR_DIR}/openair3/NAS/UE/API/USIM
     ${OPENAIR_DIR}/openair3/NAS/UE/EMM/
diff --git a/common/utils/utils.c b/common/utils/utils.c
index 7d292cc84d0..fed85c42138 100644
--- a/common/utils/utils.c
+++ b/common/utils/utils.c
@@ -21,3 +21,54 @@ void *malloc_or_fail(size_t size) {
   }
   return ptr;
 }
+
+/****************************************************************************
+ **                                                                        **
+ ** Name:        hex_char_to_hex_value()                                   **
+ **                                                                        **
+ ** Description: Converts an hexadecimal ASCII coded digit into its value. **
+ **                                                                        **
+ ** Inputs:      c:             A char holding the ASCII coded value       **
+ **              Others:        None                                       **
+ **                                                                        **
+ ** Outputs:     None                                                      **
+ **              Return:        Converted value                            **
+ **              Others:        None                                       **
+ **                                                                        **
+ ***************************************************************************/
+uint8_t hex_char_to_hex_value (char c)
+{
+  if (c >= 'A') {
+    /* Remove case bit */
+    c &= ~('a' ^ 'A');
+
+    return (c - 'A' + 10);
+  } else {
+    return (c - '0');
+  }
+}
+
+/****************************************************************************
+ **                                                                        **
+ ** Name:        hex_string_to_hex_value()                                 **
+ **                                                                        **
+ ** Description: Converts an hexadecimal ASCII coded string into its value.**
+ **                                                                        **
+ ** Inputs:      hex_value:     A pointer to the location to store the     **
+ **                             conversion result                          **
+ **              size:          The size of hex_value in bytes             **
+ **              Others:        None                                       **
+ **                                                                        **
+ ** Outputs:     hex_value:     Converted value                            **
+ **              Return:        None                                       **
+ **              Others:        None                                       **
+ **                                                                        **
+ ***************************************************************************/
+void hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int size)
+{
+  int i;
+
+  for (i=0; i < size; i++) {
+    hex_value[i] = (hex_char_to_hex_value(hex_string[2 * i]) << 4) | hex_char_to_hex_value(hex_string[2 * i + 1]);
+  }
+}
diff --git a/common/utils/utils.h b/common/utils/utils.h
index db455485a2a..3114569b4e9 100644
--- a/common/utils/utils.h
+++ b/common/utils/utils.h
@@ -1,9 +1,15 @@
 #ifndef _UTILS_H
 #define _UTILS_H
 
+#include <stdint.h>
 #include <sys/types.h>
 
 void *calloc_or_fail(size_t size);
 void *malloc_or_fail(size_t size);
 
+// Converts an hexadecimal ASCII coded digit into its value. **
+uint8_t hex_char_to_hex_value (char c);
+// Converts an hexadecimal ASCII coded string into its value.**
+void hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int size);
+
 #endif
diff --git a/openair3/NAS/UE/API/USIM/usim_api.c b/openair3/NAS/UE/API/USIM/usim_api.c
index 73aeda709d8..00252d2c038 100644
--- a/openair3/NAS/UE/API/USIM/usim_api.c
+++ b/openair3/NAS/UE/API/USIM/usim_api.c
@@ -41,6 +41,7 @@ Description Implements the API used by the NAS layer to read/write
 
 #include "usim_api.h"
 #include "nas_log.h"
+#include "utils.h"
 #include "memory.h"
 #include <stdio.h>
 #include "aka_functions.h"
@@ -66,8 +67,6 @@ Description Implements the API used by the NAS layer to read/write
  */
 #define USIM_API_NVRAM_DIRNAME  "USIM_DIR"
 
-static uint8_t _usim_api_hex_char_to_hex_value (char c);
-static void _usim_api_hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int size);
 static int _usim_api_check_sqn(uint32_t seq, uint8_t ind);
 
 /****************************************************************************/
@@ -110,7 +109,7 @@ int usim_api_read(usim_data_t* data)
   }
 
   /* initialize the subscriber authentication security key */
-  _usim_api_hex_string_to_hex_value(data->keys.usim_api_k, USIM_API_K_VALUE, USIM_API_K_SIZE);
+  hex_string_to_hex_value(data->keys.usim_api_k, USIM_API_K_VALUE, USIM_API_K_SIZE);
 
   // initialize OP
   /* PFT OP used currently in HSS (OPENAIRHSS/auc/kdf.c) */
@@ -497,57 +496,6 @@ int usim_api_authenticate(usim_data_t *usim_data, const OctetString* rand_pP, co
 /*********************  L O C A L    F U N C T I O N S  *********************/
 /****************************************************************************/
 
-/****************************************************************************
- **                                                                        **
- ** Name:        _usim_api_hex_char_to_hex_value()                         **
- **                                                                        **
- ** Description: Converts an hexadecimal ASCII coded digit into its value. **
- **                                                                        **
- ** Inputs:      c:             A char holding the ASCII coded value       **
- **              Others:        None                                       **
- **                                                                        **
- ** Outputs:     None                                                      **
- **              Return:        Converted value                            **
- **              Others:        None                                       **
- **                                                                        **
- ***************************************************************************/
-static uint8_t _usim_api_hex_char_to_hex_value (char c)
-{
-  if (c >= 'A') {
-    /* Remove case bit */
-    c &= ~('a' ^ 'A');
-
-    return (c - 'A' + 10);
-  } else {
-    return (c - '0');
-  }
-}
-
-/****************************************************************************
- **                                                                        **
- ** Name:        _usim_api_hex_string_to_hex_value()                       **
- **                                                                        **
- ** Description: Converts an hexadecimal ASCII coded string into its value.**
- **                                                                        **
- ** Inputs:      hex_value:     A pointer to the location to store the     **
- **                             conversion result                          **
- **              size:          The size of hex_value in bytes             **
- **              Others:        None                                       **
- **                                                                        **
- ** Outputs:     hex_value:     Converted value                            **
- **              Return:        None                                       **
- **              Others:        None                                       **
- **                                                                        **
- ***************************************************************************/
-static void _usim_api_hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int size)
-{
-  int i;
-
-  for (i=0; i < size; i++) {
-    hex_value[i] = (_usim_api_hex_char_to_hex_value(hex_string[2 * i]) << 4) | _usim_api_hex_char_to_hex_value(hex_string[2 * i + 1]);
-  }
-}
-
 /****************************************************************************
  **                                                                        **
  ** Name:        _usim_api_check_sqn()                                     **
-- 
GitLab