LCOV - code coverage report
Current view: top level - libs/common - byteutils.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 28 31 90.3 %
Date: 2018-12-05 17:11:35 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_BYTEUTILS_H
       7             : #define IROHA_BYTEUTILS_H
       8             : 
       9             : #include <algorithm>
      10             : #include <iomanip>
      11             : #include <sstream>
      12             : #include <string>
      13             : #include <vector>
      14             : 
      15             : #include <boost/optional.hpp>
      16             : 
      17             : #include "common/bind.hpp"
      18             : #include "common/blob.hpp"
      19             : 
      20             : namespace iroha {
      21             :   /**
      22             :    * Convert string to blob vector
      23             :    * @param source - string for conversion
      24             :    * @return vector<blob>
      25             :    */
      26             :   inline std::vector<uint8_t> stringToBytes(const std::string &source) {
      27        1287 :     return std::vector<uint8_t>(source.begin(), source.end());
      28             :   }
      29             : 
      30             :   /**
      31             :    * blob vector to string
      32             :    * @param source - vector for conversion
      33             :    * @return result string
      34             :    */
      35             :   inline std::string bytesToString(const std::vector<uint8_t> &source) {
      36        1345 :     return std::string(source.begin(), source.end());
      37             :   }
      38             : 
      39             :   /**
      40             :    * Create blob_t from string of specified size
      41             :    * @tparam size - size of blob_t, expected size of string
      42             :    * @param s - string to convert
      43             :    * @return blob, if conversion was successful, otherwise nullopt
      44             :    */
      45             :   template <size_t size>
      46             :   boost::optional<blob_t<size>> stringToBlob(const std::string &string) {
      47          39 :     if (size != string.size()) {
      48           0 :       return boost::none;
      49             :     }
      50          39 :     blob_t<size> array;
      51          39 :     std::copy(string.begin(), string.end(), array.begin());
      52          39 :     return array;
      53          39 :   }
      54             : 
      55             :   /**
      56             :    * Convert string of raw bytes to printable hex string
      57             :    * @param str - raw bytes string to convert
      58             :    * @return - converted hex string
      59             :    */
      60             :   inline std::string bytestringToHexstring(const std::string &str) {
      61      198268 :     std::stringstream ss;
      62      209935 :     ss << std::hex << std::setfill('0');
      63    19612543 :     for (const auto &c : str) {
      64    19384797 :       ss << std::setw(2) << (static_cast<int>(c) & 0xff);
      65             :     }
      66      194273 :     return ss.str();
      67      210221 :   }
      68             : 
      69             :   /**
      70             :    * Convert printable hex string to string of raw bytes
      71             :    * @param str - hex string to convert
      72             :    * @return - raw bytes converted string or boost::noneif provided string
      73             :    * was not a correct hex string
      74             :    */
      75             :   inline boost::optional<std::string> hexstringToBytestring(
      76             :       const std::string &str) {
      77        4818 :     if (str.empty() or str.size() % 2 != 0) {
      78           3 :       return boost::none;
      79             :     }
      80        4815 :     std::string result(str.size() / 2, 0);
      81      167923 :     for (size_t i = 0; i < result.length(); ++i) {
      82      163054 :       std::string byte = str.substr(i * 2, 2);
      83             :       try {
      84      163113 :         result.at(i) =
      85      163108 :             static_cast<std::string::value_type>(std::stoul(byte, nullptr, 16));
      86      163113 :       } catch (const std::invalid_argument &) {
      87           2 :         return boost::none;
      88           2 :       } catch (const std::out_of_range &) {
      89           0 :         return boost::none;
      90           2 :       }
      91      163112 :     }
      92        4815 :     return result;
      93        4820 :   }
      94             : 
      95             :   /**
      96             :    * Convert hexstring to array of given size
      97             :    * @tparam size - output array size
      98             :    * @param string - input string for transform
      99             :    * @return array of given size if size matches, nullopt otherwise
     100             :    */
     101             :   template <size_t size>
     102             :   boost::optional<blob_t<size>> hexstringToArray(const std::string &string) {
     103          40 :     return hexstringToBytestring(string) | stringToBlob<size>;
     104           0 :   }
     105             : 
     106             : }  // namespace iroha
     107             : 
     108             : #endif  // IROHA_BYTEUTILS_H

Generated by: LCOV version 1.13