LCOV - code coverage report
Current view: top level - libs/common - hexutils.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 23 24 95.8 %
Date: 2019-03-07 14:46:43 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : #ifndef IROHA_HEXUTILS_HPP
       6             : #define IROHA_HEXUTILS_HPP
       7             : 
       8             : #include <ciso646>
       9             : #include <iomanip>
      10             : #include <sstream>
      11             : #include <string>
      12             : 
      13             : #include <boost/optional.hpp>
      14             : 
      15             : namespace iroha {
      16             : 
      17             :   /**
      18             :    * Convert string of raw bytes to printable hex string
      19             :    * @param str - raw bytes string to convert
      20             :    * @return - converted hex string
      21             :    */
      22             :   inline std::string bytestringToHexstring(const std::string &str) {
      23      647030 :     std::stringstream ss;
      24      729852 :     ss << std::hex << std::setfill('0');
      25    41605652 :     for (const auto &c : str) {
      26    40679240 :       ss << std::setw(2) << (static_cast<int>(c) & 0xff);
      27             :     }
      28      641888 :     return ss.str();
      29      731965 :   }
      30             : 
      31             :   /**
      32             :    * Convert printable hex string to string of raw bytes
      33             :    * @param str - hex string to convert
      34             :    * @return - raw bytes converted string or boost::noneif provided string
      35             :    * was not a correct hex string
      36             :    */
      37             :   inline boost::optional<std::string> hexstringToBytestring(
      38             :       const std::string &str) {
      39      218853 :     if (str.empty() or str.size() % 2 != 0) {
      40        1142 :       return boost::none;
      41             :     }
      42      217842 :     std::string result(str.size() / 2, 0);
      43     9405023 :     for (size_t i = 0; i < result.length(); ++i) {
      44     9119184 :       std::string byte = str.substr(i * 2, 2);
      45     9066080 :       size_t pos = 0;  // processed characters count
      46             :       try {
      47     9210361 :         result.at(i) =
      48     9191128 :             static_cast<std::string::value_type>(std::stoul(byte, &pos, 16));
      49     9210361 :       } catch (const std::invalid_argument &) {
      50           1 :         return boost::none;
      51           1 :       } catch (const std::out_of_range &) {
      52           0 :         return boost::none;
      53           1 :       }
      54     9200119 :       if (pos != byte.size()) {
      55           1 :         return boost::none;
      56             :       }
      57     9201278 :     }
      58      217977 :     return result;
      59      219123 :   }
      60             : 
      61             : }  // namespace iroha
      62             : 
      63             : #endif  // IROHA_HEXUTILS_HPP

Generated by: LCOV version 1.13