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

          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_COMMON_BLOB_HPP
       7             : #define IROHA_COMMON_BLOB_HPP
       8             : 
       9             : #include <algorithm>
      10             : #include <array>
      11             : #include <cstdint>
      12             : #include <stdexcept>
      13             : #include <string>
      14             : 
      15             : namespace iroha {
      16             :   using BadFormatException = std::invalid_argument;
      17             :   using byte_t = uint8_t;
      18             : 
      19             :   namespace {
      20             :     static const std::string code = {'0',
      21             :                                      '1',
      22             :                                      '2',
      23             :                                      '3',
      24             :                                      '4',
      25             :                                      '5',
      26             :                                      '6',
      27             :                                      '7',
      28             :                                      '8',
      29             :                                      '9',
      30             :                                      'a',
      31             :                                      'b',
      32             :                                      'c',
      33             :                                      'd',
      34             :                                      'e',
      35             :                                      'f'};
      36             :   }
      37             : 
      38             :   /**
      39             :    * Base type which represents blob of fixed size.
      40             :    *
      41             :    * std::string is convenient to use but it is not safe.
      42             :    * We can not specify the fixed length for string.
      43             :    *
      44             :    * For std::array it is possible, so we prefer it over std::string.
      45             :    */
      46             :   template <size_t size_>
      47             :   class blob_t : public std::array<byte_t, size_> {
      48             :    public:
      49             :     /**
      50             :      * Initialize blob value
      51             :      */
      52             :     blob_t() {
      53       76394 :       this->fill(0);
      54       76396 :     }
      55             : 
      56             :     /**
      57             :      * In compile-time returns size of current blob.
      58             :      */
      59             :     constexpr static size_t size() {
      60        2796 :       return size_;
      61             :     }
      62             : 
      63             :     /**
      64             :      * Converts current blob to std::string
      65             :      */
      66             :     std::string to_string() const noexcept {
      67       55161 :       return std::string{this->begin(), this->end()};
      68             :     }
      69             : 
      70             :     /**
      71             :      * Converts current blob to hex string.
      72             :      */
      73             :     std::string to_hexstring() const noexcept {
      74         246 :       std::string res(size_ * 2, 0);
      75         246 :       auto ptr = this->data();
      76       14646 :       for (uint32_t i = 0, k = 0; i < size_; i++) {
      77       14400 :         const auto front = (uint8_t)(ptr[i] & 0xF0) >> 4;
      78       14400 :         const auto back = (uint8_t)(ptr[i] & 0xF);
      79       14400 :         res[k++] = code[front];
      80       14400 :         res[k++] = code[back];
      81       14400 :       }
      82         246 :       return res;
      83         246 :     }
      84             : 
      85             :     static blob_t<size_> from_string(const std::string &data) {
      86       22466 :       if (data.size() != size_) {
      87           2 :         std::string value = "blob_t: input string has incorrect length. Found: "
      88           2 :             + std::to_string(data.size())
      89           2 :             + +", required: " + std::to_string(size_);
      90           2 :         throw BadFormatException(value.c_str());
      91           2 :       }
      92             : 
      93       22463 :       blob_t<size_> b;
      94       22463 :       std::copy(data.begin(), data.end(), b.begin());
      95             : 
      96       22463 :       return b;
      97           2 :     }
      98             :   };
      99             : }  // namespace iroha
     100             : 
     101             : #endif  // IROHA_COMMON_BLOB_HPP

Generated by: LCOV version 1.13