From 39e183da3efd87950a980f8770133d3b631298b4 Mon Sep 17 00:00:00 2001 From: Xinyu Hou Date: Thu, 2 Apr 2015 14:01:50 +0100 Subject: [PATCH] Refactored string operations Conflicts: src/lib/base/String.cpp --- src/lib/base/String.cpp | 27 +++++++++++++++++++++++++++ src/lib/base/String.h | 19 +++++++++++++++++++ src/lib/plugin/ns/SecureSocket.cpp | 15 ++++----------- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/lib/base/String.cpp b/src/lib/base/String.cpp index 4ce38899..32531df8 100644 --- a/src/lib/base/String.cpp +++ b/src/lib/base/String.cpp @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include namespace synergy { namespace string { @@ -180,6 +183,30 @@ removeFileExt(String filename) return filename.substr(0, dot); } +void +toHex(CString& subject, int width, const char fill) +{ + std::stringstream ss; + ss << std::hex; + for (unsigned int i = 0; i < subject.length(); i++) { + ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i]; + } + + subject = ss.str(); +} + +void +uppercase(CString& subject) +{ + std::transform(subject.begin(), subject.end(), subject.begin(), ::toupper); +} + +void +removeChar(CString& subject, const char c) +{ + subject.erase(std::remove(subject.begin(), subject.end(), c), subject.end()); +} + // // CaselessCmp // diff --git a/src/lib/base/String.h b/src/lib/base/String.h index 9d137143..57f3c69c 100644 --- a/src/lib/base/String.h +++ b/src/lib/base/String.h @@ -70,6 +70,25 @@ Finds the last dot and remove all characters from the dot to the end */ String removeFileExt(String filename); +//! Convert into hexdecimal +/*! +Convert each character in \c subject into hexdecimal form with \c width +*/ +void toHex(CString& subject, int width, const char fill = '0'); + +//! Convert to all uppercase +/*! +Convert each character in \c subject to uppercase +*/ +void uppercase(CString& subject); + +//! Remove all specific char in suject +/*! +Remove all specific \c char in \c suject +*/ +void removeChar(CString& subject, const char c); + + //! Case-insensitive comparisons /*! This class provides case-insensitve comparison functions. diff --git a/src/lib/plugin/ns/SecureSocket.cpp b/src/lib/plugin/ns/SecureSocket.cpp index 56900461..8821a3e6 100644 --- a/src/lib/plugin/ns/SecureSocket.cpp +++ b/src/lib/plugin/ns/SecureSocket.cpp @@ -28,10 +28,7 @@ #include #include #include -#include -#include #include -#include // // SecureSocket @@ -419,15 +416,11 @@ CSecureSocket::verifyCertFingerprint() } // convert fingerprint into hexdecimal format - std::stringstream ss; - ss << std::hex; - for (unsigned int i = 0; i < tempFingerprintLen; i++) { - ss << std::setw(2) << std::setfill('0') << (int)tempFingerprint[i]; - } + CString fingerprint(reinterpret_cast(tempFingerprint), tempFingerprintLen); + synergy::string::toHex(fingerprint, 2); // all uppercase - CString fingerprint = ss.str(); - std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(), ::toupper); + synergy::string::uppercase(fingerprint); // check if this fingerprint exist CString fileLine; @@ -445,7 +438,7 @@ CSecureSocket::verifyCertFingerprint() if (!certificateFingerprint.empty()) { // remove colons - certificateFingerprint.erase(std::remove(certificateFingerprint.begin(), certificateFingerprint.end(), ':'), certificateFingerprint.end()); + synergy::string::removeChar(certificateFingerprint, ':'); if(certificateFingerprint.compare(fingerprint) == 0) { file.close();