Fix 'occurred' typo
[bpdyndnsd] / src / util.cpp
index 96cd7e7..8aa5bd9 100644 (file)
@@ -7,9 +7,10 @@
  * @license GPLv2
 */
 
-#include "util.h"
+#include "util.hpp"
 
 #include <sstream>
+#include <iomanip>
 #include <openssl/evp.h>
 #include <boost/algorithm/string.hpp>
 
@@ -20,7 +21,7 @@ namespace Util
  * @param data The string to compute the md5 for
  * @return The computed md5 in hex
  */
-std::string compute_md5_digest(std::string data) throw (std::invalid_argument)
+std::string compute_md5_digest(std::string data)
 {
     // compute an MD5 digest.
 
@@ -43,25 +44,42 @@ std::string compute_md5_digest(std::string data) throw (std::invalid_argument)
     EVP_MD_CTX_init(&mdctx);
 
     // Now we can call init_ex.
-    EVP_DigestInit_ex(&mdctx, md, NULL);
+    if ( EVP_DigestInit_ex(&mdctx, md, NULL) == 0 )
+    {
+        EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
+        throw std::invalid_argument("Could not set up digest context correctly");
+    }
 
     // Test if data is empty.
     if ( data.empty() )
+    {
+        EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
         throw std::invalid_argument("Passed data is empty");
+    }
 
     // Hash the data. At this point data is not empty and &mdctx is initialized.
-    EVP_DigestUpdate(&mdctx, data.c_str(), data.size());
+    if ( EVP_DigestUpdate(&mdctx, data.c_str(), data.size()) == 0 )
+    {
+        EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
+        throw std::invalid_argument("Could not hash data into digest context");
+    }
 
     // Retrieve the digest value from &mdctx and place it in md_value.
-    EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
+    if ( EVP_DigestFinal_ex(&mdctx, md_value, &md_len) == 0 )
+    {
+        EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
+        throw std::invalid_argument("Could not retrieve digest value");
+    }
 
     // Test if md_value is filled correctly and md_len is not zero.
-    if ( (md_len == 0) || (md_value == NULL) || (EVP_MD_CTX_size(&mdctx) == 0) )
+    if ( (md_len == 0) || (EVP_MD_CTX_size(&mdctx) == 0) )
+    {
+        EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
         throw std::invalid_argument("Retrieved invalid digest value");
+    }
 
     // Internal cleanup of the digest content.
-    EVP_MD_CTX_cleanup(&mdctx);
-    EVP_cleanup();
+    EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */
 
     // Convert md5 digest C string to hex.
     std::ostringstream oss_digest_md5_hex;
@@ -69,7 +87,8 @@ std::string compute_md5_digest(std::string data) throw (std::invalid_argument)
     {
         // We have to do a static cast to an decimal representation, cause otherwise ostringstream would interpret
         // the stream as a character and output the character representation of the hex value.
-        oss_digest_md5_hex << std::hex << static_cast<unsigned short>(md_value[i]);
+        oss_digest_md5_hex << std::nouppercase << std::setw(2) << std::setfill('0')
+                           << std::hex << static_cast<int>(static_cast<unsigned char>(md_value[i]));
     }
 
     return oss_digest_md5_hex.str();
@@ -78,13 +97,17 @@ std::string compute_md5_digest(std::string data) throw (std::invalid_argument)
 
 /**
  * Get the status code from the given data.
- * @param data The data containing the status code at front, limited by " ".
- * @return The parsed status code.
+ * @param data The data containing the status code at front, limited by delimiter.
+ * @param delimiter The delimiter to use.
+ * @return The parsed status code
  */
-std::string parse_status_code(std::string data)
+std::string parse_status_code(std::string data, std::string delimiter)
 {
     std::list<std::string> tokens;
-    boost::algorithm::split(tokens,data,boost::is_any_of(" "));
+    boost::algorithm::split(tokens,data,boost::is_any_of(delimiter));
+    if ( tokens.empty() )
+        return "";
     return tokens.front();
 }
-};
+
+}