Selaa lähdekoodia

Merging SetKexList and SetCipherList implementation using template method

Source commit: af9f11135d3eeca586a8df2cf06df8d6e7e7b47d
Martin Prikryl 9 vuotta sitten
vanhempi
sitoutus
ab1ec3fb34
2 muutettua tiedostoa jossa 34 lisäystä ja 40 poistoa
  1. 30 38
      source/core/SessionData.cpp
  2. 4 2
      source/core/SessionData.h

+ 30 - 38
source/core/SessionData.cpp

@@ -16,14 +16,15 @@
 #include <StrUtils.hpp>
 #include <XMLDoc.hpp>
 #include <StrUtils.hpp>
+#include <algorithm>
 //---------------------------------------------------------------------------
 #pragma package(smart_init)
 //---------------------------------------------------------------------------
 const wchar_t * PingTypeNames = L"Off;Null;Dummy";
 const wchar_t * ProxyMethodNames = L"None;SOCKS4;SOCKS5;HTTP;Telnet;Cmd";
 const wchar_t * DefaultName = L"Default Settings";
-const wchar_t CipherNames[CIPHER_COUNT][10] = {L"WARN", L"3des", L"blowfish", L"aes", L"des", L"arcfour", L"chacha20"};
-const wchar_t KexNames[KEX_COUNT][20] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa", L"ecdh"};
+const UnicodeString CipherNames[CIPHER_COUNT] = {L"WARN", L"3des", L"blowfish", L"aes", L"des", L"arcfour", L"chacha20"};
+const UnicodeString KexNames[KEX_COUNT] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa", L"ecdh"};
 const wchar_t SshProtList[][10] = {L"1 only", L"1", L"2", L"2 only"};
 const TCipher DefaultCipherList[CIPHER_COUNT] =
   { cipChaCha20, cipAES, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
@@ -2033,34 +2034,48 @@ TCipher __fastcall TSessionData::GetCipher(int Index) const
   return FCiphers[Index];
 }
 //---------------------------------------------------------------------
-void __fastcall TSessionData::SetCipherList(UnicodeString value)
+template<class AlgoT>
+void __fastcall TSessionData::SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names, int Count, UnicodeString value)
 {
-  bool Used[CIPHER_COUNT];
-  for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
+  std::vector<bool> Used(Count); // initialized to false
+  std::vector<AlgoT> NewList(Count);
 
-  UnicodeString CipherStr;
   int Index = 0;
-  while (!value.IsEmpty() && (Index < CIPHER_COUNT))
+  while (!value.IsEmpty() && (Index < Count))
   {
-    CipherStr = CutToChar(value, L',', true);
-    for (int C = 0; C < CIPHER_COUNT; C++)
+    UnicodeString AlgoStr = CutToChar(value, L',', true);
+    for (int Algo = 0; Algo < Count; Algo++)
     {
-      if (!CipherStr.CompareIC(CipherNames[C]))
+      if (!AlgoStr.CompareIC(Names[Algo]))
       {
-        Cipher[Index] = (TCipher)C;
-        Used[C] = true;
+        NewList[Index] = (AlgoT)Algo;
+        Used[Algo] = true;
         Index++;
         break;
       }
     }
   }
 
-  for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
+  for (int Algo = 0; (Algo < Count) && (Index < Count); Algo++)
+  {
+    if (!Used[DefaultList[Algo]])
+    {
+      NewList[Index++] = DefaultList[Algo];
+    }
+  }
+
+  if (!std::equal(NewList.begin(), NewList.end(), List))
   {
-    if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
+    std::copy(NewList.begin(), NewList.end(), List);
+    Modify();
   }
 }
 //---------------------------------------------------------------------
+void __fastcall TSessionData::SetCipherList(UnicodeString value)
+{
+  SetAlgoList(FCiphers, DefaultCipherList, CipherNames, CIPHER_COUNT, value);
+}
+//---------------------------------------------------------------------
 UnicodeString __fastcall TSessionData::GetCipherList() const
 {
   UnicodeString Result;
@@ -2085,30 +2100,7 @@ TKex __fastcall TSessionData::GetKex(int Index) const
 //---------------------------------------------------------------------
 void __fastcall TSessionData::SetKexList(UnicodeString value)
 {
-  bool Used[KEX_COUNT];
-  for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
-
-  UnicodeString KexStr;
-  int Index = 0;
-  while (!value.IsEmpty() && (Index < KEX_COUNT))
-  {
-    KexStr = CutToChar(value, L',', true);
-    for (int K = 0; K < KEX_COUNT; K++)
-    {
-      if (!KexStr.CompareIC(KexNames[K]))
-      {
-        Kex[Index] = (TKex)K;
-        Used[K] = true;
-        Index++;
-        break;
-      }
-    }
-  }
-
-  for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
-  {
-    if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
-  }
+  SetAlgoList(FKex, DefaultKexList, KexNames, KEX_COUNT, value);
 }
 //---------------------------------------------------------------------
 UnicodeString __fastcall TSessionData::GetKexList() const

+ 4 - 2
source/core/SessionData.h

@@ -45,8 +45,8 @@ enum TSessionUrlFlags
   sufOpen = sufUserName | sufPassword
 };
 //---------------------------------------------------------------------------
-extern const wchar_t CipherNames[CIPHER_COUNT][10];
-extern const wchar_t KexNames[KEX_COUNT][20];
+extern const UnicodeString CipherNames[CIPHER_COUNT];
+extern const UnicodeString KexNames[KEX_COUNT];
 extern const wchar_t SshProtList[][10];
 extern const TCipher DefaultCipherList[CIPHER_COUNT];
 extern const TKex DefaultKexList[KEX_COUNT];
@@ -394,6 +394,8 @@ private:
     UnicodeString & Result, TAssemblyLanguage Language,
     const UnicodeString & Name, bool Value);
   TStrings * __fastcall SaveToOptions(const TSessionData * Default);
+  template<class AlgoT>
+  void __fastcall SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names, int Count, UnicodeString value);
 
   __property UnicodeString InternalStorageKey = { read = GetInternalStorageKey };