GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 95.2% 59 / 12 / 74
Functions: 100.0% 7 / 0 / 7
Branches: 95.0% 19 / 8 / 28

libs/url/src/detail/decode.cpp
Line Branch Exec Source
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/url
8 //
9
10
11 #include <boost/url/detail/config.hpp>
12 #include <boost/url/detail/decode.hpp>
13 #include <boost/url/grammar/charset.hpp>
14 #include <boost/url/grammar/hexdig_chars.hpp>
15 #include <memory>
16
17 namespace boost {
18 namespace urls {
19 namespace detail {
20
21 char
22 877 decode_one(
23 char const* const it) noexcept
24 {
25 877 auto d0 = grammar::hexdig_value(it[0]);
26 877 auto d1 = grammar::hexdig_value(it[1]);
27 return static_cast<char>(
28 877 ((static_cast<
29 877 unsigned char>(d0) << 4) +
30 877 (static_cast<
31 877 unsigned char>(d1))));
32 }
33
34 std::size_t
35 2216 decode_bytes_unsafe(
36 core::string_view s) noexcept
37 {
38 2216 auto p = s.begin();
39 2216 auto const end = s.end();
40 2216 std::size_t dn = 0;
41
2/2
✓ Branch 1 taken 1027 times.
✓ Branch 2 taken 1189 times.
2216 if(s.size() >= 3)
42 {
43 1027 auto const safe_end = end - 2;
44
2/2
✓ Branch 0 taken 7321 times.
✓ Branch 1 taken 1027 times.
8348 while(p < safe_end)
45 {
46
2/2
✓ Branch 0 taken 7055 times.
✓ Branch 1 taken 266 times.
7321 if(*p != '%')
47 7055 p += 1;
48 else
49 266 p += 3;
50 7321 ++dn;
51 }
52 }
53 2216 dn += end - p;
54 2216 return dn;
55 }
56
57 template <bool SpaceAsPlus>
58 std::size_t
59 decode_unsafe_is_plus_impl(char c);
60
61 template <>
62 std::size_t
63 7644 decode_unsafe_is_plus_impl<true>(char c)
64 {
65 7644 return c == '+';
66 }
67
68 template <>
69 std::size_t
70 7689 decode_unsafe_is_plus_impl<false>(char)
71 {
72 7689 return false;
73 }
74
75
76 template <bool SpaceAsPlus>
77 std::size_t
78 2830 decode_unsafe_impl(
79 char* const dest0,
80 char const* end,
81 core::string_view s) noexcept
82 {
83 2830 auto it = s.data();
84 2830 auto const last = it + s.size();
85 2830 auto dest = dest0;
86
87
4/4
unsigned long boost::urls::detail::decode_unsafe_impl<false>(char*, char const*, boost::core::basic_string_view<char>):
✓ Branch 0 taken 7690 times.
✓ Branch 1 taken 1576 times.
unsigned long boost::urls::detail::decode_unsafe_impl<true>(char*, char const*, boost::core::basic_string_view<char>):
✓ Branch 0 taken 7644 times.
✓ Branch 1 taken 1253 times.
18163 while(it != last)
88 {
89 // LCOV_EXCL_START
90 if(dest == end)
91 {
92 /*
93 * dest too small: unreachable
94 * public functions always pass
95 * a buffer of sufficient size
96 */
97 return dest - dest0;
98 }
99 // LCOV_EXCL_STOP
100
3/4
unsigned long boost::urls::detail::decode_unsafe_impl<false>(char*, char const*, boost::core::basic_string_view<char>):
✗ Branch 1 not taken.
✓ Branch 2 taken 7689 times.
unsigned long boost::urls::detail::decode_unsafe_impl<true>(char*, char const*, boost::core::basic_string_view<char>):
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 7601 times.
15333 if(decode_unsafe_is_plus_impl<SpaceAsPlus>(*it))
101 {
102 // plus to space
103 43 *dest++ = ' ';
104 43 ++it;
105 43 continue;
106 }
107
4/4
unsigned long boost::urls::detail::decode_unsafe_impl<false>(char*, char const*, boost::core::basic_string_view<char>):
✓ Branch 0 taken 605 times.
✓ Branch 1 taken 7084 times.
unsigned long boost::urls::detail::decode_unsafe_impl<true>(char*, char const*, boost::core::basic_string_view<char>):
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 7458 times.
15290 if(*it == '%')
108 {
109 // escaped
110 748 ++it;
111 // LCOV_EXCL_START
112 if(last - it < 2)
113 {
114 // `%` not followed by two hex digits
115 // invalid input: unreachable
116 // public functions always pass
117 // a valid string_view.
118 // initialize output
119 std::memset(dest,
120 0, end - dest);
121 return dest - dest0;
122 }
123 // LCOV_EXCL_STOP
124 748 *dest++ = decode_one(it);
125 748 it += 2;
126 748 continue;
127 }
128 // unescaped
129 14542 *dest++ = *it++;
130 }
131 2829 return dest - dest0;
132 }
133
134 std::size_t
135 2830 decode_unsafe(
136 char* const dest0,
137 char const* end,
138 core::string_view s,
139 encoding_opts opt) noexcept
140 {
141
2/2
✓ Branch 0 taken 1253 times.
✓ Branch 1 taken 1577 times.
2830 if(opt.space_as_plus)
142 {
143 1253 return decode_unsafe_impl<true>(
144 1253 dest0, end, s);
145 }
146 1577 return decode_unsafe_impl<false>(
147 1577 dest0, end, s);
148 }
149
150 } // detail
151 } // urls
152 } // boost
153