CryptoPkg/TlsLib: use binary search in the TlsGetCipherMapping() function
authorLaszlo Ersek <lersek@redhat.com>
Sat, 31 Mar 2018 15:06:39 +0000 (17:06 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 13 Apr 2018 12:06:16 +0000 (14:06 +0200)
Improve the performance of the TlsGetCipherMapping() function by adopting
the binary search from DhcpFindOptionFormat()
[MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Option.c].

Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Qin Long <qin.long@intel.com>
Cc: Siyuan Fu <siyuan.fu@intel.com>
Cc: Ting Ye <ting.ye@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=915
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Long Qin <qin.long@intel.com>
Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>
CryptoPkg/Library/TlsLib/TlsConfig.c

index 5074893..c1d91a5 100644 (file)
@@ -30,6 +30,8 @@ typedef struct {
 // The mapping table between IANA/IETF Cipher Suite definitions and\r
 // OpenSSL-used Cipher Suite name.\r
 //\r
+// Keep the table uniquely sorted by the IanaCipher field, in increasing order.\r
+//\r
 STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable[] = {\r
   { 0x0001, "NULL-MD5" },                 /// TLS_RSA_WITH_NULL_MD5\r
   { 0x0002, "NULL-SHA" },                 /// TLS_RSA_WITH_NULL_SHA\r
@@ -71,22 +73,30 @@ TlsGetCipherMapping (
   IN     UINT16                   CipherId\r
   )\r
 {\r
-  CONST TLS_CIPHER_MAPPING  *CipherEntry;\r
-  UINTN                     TableSize;\r
-  UINTN                     Index;\r
-\r
-  CipherEntry = TlsCipherMappingTable;\r
-  TableSize = sizeof (TlsCipherMappingTable) / sizeof (TLS_CIPHER_MAPPING);\r
+  INTN                      Left;\r
+  INTN                      Right;\r
+  INTN                      Middle;\r
 \r
   //\r
-  // Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
+  // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
   //\r
-  for (Index = 0; Index < TableSize; Index++, CipherEntry++) {\r
-    //\r
-    // Translate IANA cipher suite name to OpenSSL name.\r
-    //\r
-    if (CipherEntry->IanaCipher == CipherId) {\r
-      return CipherEntry;\r
+  Left  = 0;\r
+  Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;\r
+\r
+  while (Right >= Left) {\r
+    Middle = (Left + Right) / 2;\r
+\r
+    if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {\r
+      //\r
+      // Translate IANA cipher suite ID to OpenSSL name.\r
+      //\r
+      return &TlsCipherMappingTable[Middle];\r
+    }\r
+\r
+    if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {\r
+      Right = Middle - 1;\r
+    } else {\r
+      Left  = Middle + 1;\r
     }\r
   }\r
 \r