Coverage Report

Created: 2025-07-01 06:55

/src/libtorrent/src/sha1_hash.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2016, Pavel Pimenov
4
Copyright (c) 2016-2017, Alden Torres
5
Copyright (c) 2016-2020, Arvid Norberg
6
Copyright (c) 2017, Steven Siloti
7
All rights reserved.
8
9
Redistribution and use in source and binary forms, with or without
10
modification, are permitted provided that the following conditions
11
are met:
12
13
    * Redistributions of source code must retain the above copyright
14
      notice, this list of conditions and the following disclaimer.
15
    * Redistributions in binary form must reproduce the above copyright
16
      notice, this list of conditions and the following disclaimer in
17
      the documentation and/or other materials provided with the distribution.
18
    * Neither the name of the author nor the names of its
19
      contributors may be used to endorse or promote products derived
20
      from this software without specific prior written permission.
21
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33
34
*/
35
36
#include "libtorrent/sha1_hash.hpp"
37
#include "libtorrent/hex.hpp" // to_hex, from_hex
38
39
#if TORRENT_USE_IOSTREAM
40
#include <iostream>
41
#include <iomanip>
42
#endif // TORRENT_USE_IOSTREAM
43
44
namespace libtorrent {
45
46
#if TORRENT_USE_IOSTREAM
47
48
  // print a sha1_hash object to an ostream as 40 hexadecimal digits
49
  template <std::ptrdiff_t N>
50
  void digest32<N>::stream_out(std::ostream& os) const
51
0
  {
52
0
    os << aux::to_hex(*this);
53
0
  }
Unexecuted instantiation: libtorrent::digest32<160l>::stream_out(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const
Unexecuted instantiation: libtorrent::digest32<256l>::stream_out(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const
54
55
  // read hexadecimal digits from an istream into a digest32
56
  template <std::ptrdiff_t N>
57
  void digest32<N>::stream_in(std::istream& is)
58
0
  {
59
0
    char hex[size() * 2];
60
0
    is.read(hex, size() * 2);
61
0
    if (!aux::from_hex(hex, data()))
62
0
      is.setstate(std::ios_base::failbit);
63
0
  }
Unexecuted instantiation: libtorrent::digest32<160l>::stream_in(std::__1::basic_istream<char, std::__1::char_traits<char> >&)
Unexecuted instantiation: libtorrent::digest32<256l>::stream_in(std::__1::basic_istream<char, std::__1::char_traits<char> >&)
64
65
  // explicitly instantiate these template functions for sha1 and sha256
66
  template TORRENT_EXPORT void digest32<160>::stream_out(std::ostream&) const;
67
  template TORRENT_EXPORT void digest32<256>::stream_out(std::ostream&) const;
68
69
  template TORRENT_EXPORT void digest32<160>::stream_in(std::istream&);
70
  template TORRENT_EXPORT void digest32<256>::stream_in(std::istream&);
71
72
#endif // TORRENT_USE_IOSTREAM
73
74
namespace {
75
76
  void bits_shift_left(span<std::uint32_t> const number, int n) noexcept
77
0
  {
78
0
    TORRENT_ASSERT(n >= 0);
79
0
    int const num_words = n / 32;
80
0
    int const number_size = int(number.size());
81
0
    if (num_words >= number_size)
82
0
    {
83
0
      std::memset(number.data(), 0, std::size_t(number.size() * 4));
84
0
      return;
85
0
    }
86
87
0
    if (num_words > 0)
88
0
    {
89
0
      std::memmove(number.data(), number.data() + num_words
90
0
        , std::size_t(number_size - num_words) * sizeof(std::uint32_t));
91
0
      std::memset(number.data() + (number_size - num_words)
92
0
        , 0, std::size_t(num_words) * sizeof(std::uint32_t));
93
0
      n -= num_words * 32;
94
0
    }
95
0
    if (n > 0)
96
0
    {
97
      // keep in mind that the uint32_t are stored in network
98
      // byte order, so they have to be byteswapped before
99
      // applying the shift operations, and then byteswapped
100
      // back again.
101
0
      number[0] = aux::network_to_host(number[0]);
102
0
      for (int i = 0; i < number_size - 1; ++i)
103
0
      {
104
0
        number[i] <<= n;
105
0
        number[i + 1] = aux::network_to_host(number[i + 1]);
106
0
        number[i] |= number[i + 1] >> (32 - n);
107
0
        number[i] = aux::host_to_network(number[i]);
108
0
      }
109
0
      number[number_size - 1] <<= n;
110
0
      number[number_size - 1] = aux::host_to_network(number[number_size - 1]);
111
0
    }
112
0
  }
113
114
  void bits_shift_right(span<std::uint32_t> const number, int n) noexcept
115
0
  {
116
0
    TORRENT_ASSERT(n >= 0);
117
0
    int const num_words = n / 32;
118
0
    int const number_size = int(number.size());
119
0
    if (num_words >= number_size)
120
0
    {
121
0
      std::memset(number.data(), 0, std::size_t(number.size() * 4));
122
0
      return;
123
0
    }
124
0
    if (num_words > 0)
125
0
    {
126
0
      std::memmove(number.data() + num_words
127
0
        , number.data(), std::size_t(number_size - num_words) * sizeof(std::uint32_t));
128
0
      std::memset(number.data(), 0, std::size_t(num_words) * sizeof(std::uint32_t));
129
0
      n -= num_words * 32;
130
0
    }
131
0
    if (n > 0)
132
0
    {
133
      // keep in mind that the uint32_t are stored in network
134
      // byte order, so they have to be byteswapped before
135
      // applying the shift operations, and then byteswapped
136
      // back again.
137
0
      number[number_size - 1] = aux::network_to_host(number[number_size - 1]);
138
139
0
      for (int i = number_size - 1; i > 0; --i)
140
0
      {
141
0
        number[i] >>= n;
142
0
        number[i - 1] = aux::network_to_host(number[i - 1]);
143
0
        number[i] |= (number[i - 1] << (32 - n)) & 0xffffffff;
144
0
        number[i] = aux::host_to_network(number[i]);
145
0
      }
146
0
      number[0] >>= n;
147
0
      number[0] = aux::host_to_network(number[0]);
148
0
    }
149
0
  }
150
}
151
152
    // shift left ``n`` bits.
153
  template <std::ptrdiff_t N>
154
  digest32<N>& digest32<N>::operator<<=(int const n) & noexcept
155
0
  {
156
0
    bits_shift_left(m_number, n);
157
0
    return *this;
158
0
  }
Unexecuted instantiation: libtorrent::digest32<160l>::operator<<=(int) &
Unexecuted instantiation: libtorrent::digest32<256l>::operator<<=(int) &
159
160
  // shift right ``n`` bits.
161
  template <std::ptrdiff_t N>
162
  digest32<N>& digest32<N>::operator>>=(int const n) & noexcept
163
0
  {
164
0
    bits_shift_right(m_number, n);
165
0
    return *this;
166
0
  }
Unexecuted instantiation: libtorrent::digest32<160l>::operator>>=(int) &
Unexecuted instantiation: libtorrent::digest32<256l>::operator>>=(int) &
167
168
  template TORRENT_EXPORT digest32<160>& digest32<160>::operator<<=(int) & noexcept;
169
  template TORRENT_EXPORT digest32<256>& digest32<256>::operator<<=(int) & noexcept;
170
  template TORRENT_EXPORT digest32<160>& digest32<160>::operator>>=(int) & noexcept;
171
  template TORRENT_EXPORT digest32<256>& digest32<256>::operator>>=(int) & noexcept;
172
173
  static_assert(std::is_nothrow_move_constructible<sha1_hash>::value
174
    , "should be nothrow move constructible");
175
  static_assert(std::is_nothrow_move_assignable<sha1_hash>::value
176
    , "should be nothrow move assignable");
177
  static_assert(std::is_nothrow_default_constructible<sha1_hash>::value
178
    , "should be nothrow default constructible");
179
}