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>
// The mapping table between IANA/IETF Cipher Suite definitions and\r
// OpenSSL-used Cipher Suite name.\r
//\r
// 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
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
IN UINT16 CipherId\r
)\r
{\r
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
- // Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
+ // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\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