]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Deduplicate hex string creation code
authorattilamolnar <attilamolnar@hush.com>
Thu, 16 May 2013 13:34:45 +0000 (15:34 +0200)
committerattilamolnar <attilamolnar@hush.com>
Sat, 18 May 2013 19:07:42 +0000 (21:07 +0200)
include/hashcomp.h
include/inspstring.h
src/hashcomp.cpp
src/inspstring.cpp
src/modules/extra/m_ssl_gnutls.cpp
src/modules/extra/m_ssl_openssl.cpp

index c1e8a0ec51215cb5f1822472aa662d2b541f74d2..94c222e14f0b6f59766108f8098a83fb0e6ff6f8 100644 (file)
@@ -159,13 +159,6 @@ namespace irc
                static CoreExport const char* find(const char* s1, int  n, char c);
        };
 
-       /** Compose a hex string from raw data.
-        * @param raw The raw data to compose hex from
-        * @param rawsz The size of the raw data buffer
-        * @return The hex string.
-        */
-       CoreExport std::string hex(const unsigned char *raw, size_t rawsz);
-
        /** This typedef declares irc::string based upon irc_char_traits.
         */
        typedef std::basic_string<char, irc_char_traits, std::allocator<char> > string;
index 6864245b0219e719e5eda500e913c41c53f3a569..eb7b7218f7798a833fbcf7aee1aa370fb2ac64be 100644 (file)
 #include "config.h"
 #include <cstring>
 
-/** Binary to hexadecimal conversion */
-CoreExport std::string BinToHex(const std::string& data);
+/** Compose a hex string from raw data.
+ * @param raw The raw data to compose hex from (can be NULL if rawsize is 0)
+ * @param rawsize The size of the raw data buffer
+ * @return The hex string
+ */
+CoreExport std::string BinToHex(const void* raw, size_t rawsize);
+
 /** Base64 encode */
 CoreExport std::string BinToBase64(const std::string& data, const char* table = NULL, char pad = 0);
 /** Base64 decode */
 CoreExport std::string Base64ToBin(const std::string& data, const char* table = NULL);
+
+/** Compose a hex string from the data in a std::string.
+ * @param data The data to compose hex from
+ * @return The hex string.
+ */
+inline std::string BinToHex(const std::string& data)
+{
+       return BinToHex(data.data(), data.size());
+}
index 4ddf942eae028a8ce327d3a6e65952f7e9239c49..a2f99b70c95416bd0e05ecc57a32dac5d9fa4579 100644 (file)
@@ -365,27 +365,6 @@ irc::sepstream::~sepstream()
 {
 }
 
-std::string irc::hex(const unsigned char *raw, size_t rawsz)
-{
-       if (!rawsz)
-               return "";
-
-       /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
-
-       const char *hex = "0123456789abcdef";
-       static char hexbuf[MAXBUF];
-
-       size_t i, j;
-       for (i = 0, j = 0; j < rawsz; ++j)
-       {
-               hexbuf[i++] = hex[raw[j] / 16];
-               hexbuf[i++] = hex[raw[j] % 16];
-       }
-       hexbuf[i] = 0;
-
-       return hexbuf;
-}
-
 irc::modestacker::modestacker(bool add) : adding(add)
 {
        sequence.clear();
index 72d6c64c83f1b3acd33e4e22ec8a4bcdb405f0e2..7fa4762c5b1b873bfe734f4eec2d66dc060716a9 100644 (file)
 
 static const char hextable[] = "0123456789abcdef";
 
-std::string BinToHex(const std::string& data)
+std::string BinToHex(const void* raw, size_t l)
 {
-       int l = data.length();
+       const char* data = static_cast<const char*>(raw);
        std::string rv;
        rv.reserve(l * 2);
-       for(int i=0; i < l; i++)
+       for (size_t i = 0; i < l; i++)
        {
                unsigned char c = data[i];
-               rv.append(1, hextable[c >> 4]);
-               rv.append(1, hextable[c & 0xF]);
+               rv.push_back(hextable[c >> 4]);
+               rv.push_back(hextable[c & 0xF]);
        }
        return rv;
 }
index f85b886e1587038516d6fd1658b177be5a97163e..2ae16c619b71d6474e5323f217d46c1f417ae874 100644 (file)
@@ -846,7 +846,7 @@ class ModuleSSLGnuTLS : public Module
                }
                else
                {
-                       certinfo->fingerprint = irc::hex(digest, digest_size);
+                       certinfo->fingerprint = BinToHex(digest, digest_size);
                }
 
                /* Beware here we do not check for errors.
index b8f588b066e1ae11bad995a8a300fc212c9826f6..23e770c8b11bdc78b1dc9e1c56fa60f8736b5b3e 100644 (file)
@@ -635,7 +635,7 @@ class ModuleSSLOpenSSL : public Module
                }
                else
                {
-                       certinfo->fingerprint = irc::hex(md, n);
+                       certinfo->fingerprint = BinToHex(md, n);
                }
 
                if ((ASN1_UTCTIME_cmp_time_t(X509_get_notAfter(cert), ServerInstance->Time()) == -1) || (ASN1_UTCTIME_cmp_time_t(X509_get_notBefore(cert), ServerInstance->Time()) == 0))