1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  

10  

11  
#include <boost/url/detail/config.hpp>
11  
#include <boost/url/detail/config.hpp>
12  
#include <boost/url/ipv6_address.hpp>
12  
#include <boost/url/ipv6_address.hpp>
13  
#include <boost/url/ipv4_address.hpp>
13  
#include <boost/url/ipv4_address.hpp>
14  
#include <boost/url/rfc/ipv6_address_rule.hpp>
14  
#include <boost/url/rfc/ipv6_address_rule.hpp>
15  
#include <boost/url/detail/except.hpp>
15  
#include <boost/url/detail/except.hpp>
16  
#include <boost/url/grammar/parse.hpp>
16  
#include <boost/url/grammar/parse.hpp>
17  
#include <cstring>
17  
#include <cstring>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  

21  

22  
ipv6_address::
22  
ipv6_address::
23 -
    bytes_type const& bytes) noexcept
 
24 -
{
 
25 -
    std::memcpy(&addr_,
 
26 -
        bytes.data(), 16);
 
27 -
}
 
28 -

 
29 -
ipv6_address::
 
30 -
ipv6_address(
 
31  
ipv6_address(
23  
ipv6_address(
32  
    ipv4_address const& addr) noexcept
24  
    ipv4_address const& addr) noexcept
33  
{
25  
{
34  
    auto const v = addr.to_bytes();
26  
    auto const v = addr.to_bytes();
35  
    ipv6_address::bytes_type bytes = {
27  
    ipv6_address::bytes_type bytes = {
36  
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28  
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37  
      0xff, 0xff, v[0], v[1], v[2], v[3] } };
29  
      0xff, 0xff, v[0], v[1], v[2], v[3] } };
38  
    std::memcpy(&addr_, bytes.data(), 16);
30  
    std::memcpy(&addr_, bytes.data(), 16);
39  
}
31  
}
40  

32  

41  
ipv6_address::
33  
ipv6_address::
42  
ipv6_address(
34  
ipv6_address(
43  
    core::string_view s)
35  
    core::string_view s)
44  
    : ipv6_address(
36  
    : ipv6_address(
45  
        parse_ipv6_address(s
37  
        parse_ipv6_address(s
46  
            ).value(BOOST_URL_POS))
38  
            ).value(BOOST_URL_POS))
47  
{
39  
{
48  
}
40  
}
49  

41  

50  
core::string_view
42  
core::string_view
51  
ipv6_address::
43  
ipv6_address::
52  
to_buffer(
44  
to_buffer(
53  
    char* dest,
45  
    char* dest,
54  
    std::size_t dest_size) const
46  
    std::size_t dest_size) const
55  
{
47  
{
56  
    if(dest_size < max_str_len)
48  
    if(dest_size < max_str_len)
57  
        detail::throw_length_error();
49  
        detail::throw_length_error();
58  
    auto n = print_impl(dest);
50  
    auto n = print_impl(dest);
59  
    return core::string_view(dest, n);
51  
    return core::string_view(dest, n);
60  
}
52  
}
61  

53  

62  
bool
54  
bool
63  
ipv6_address::
55  
ipv6_address::
64  
is_loopback() const noexcept
56  
is_loopback() const noexcept
65  
{
57  
{
66  
    return *this == loopback();
58  
    return *this == loopback();
67  
}
59  
}
68  

60  

69  
bool
61  
bool
70  
ipv6_address::
62  
ipv6_address::
71  
is_unspecified() const noexcept
63  
is_unspecified() const noexcept
72  
{
64  
{
73  
    return *this == ipv6_address();
65  
    return *this == ipv6_address();
74  
}
66  
}
75  

67  

76  
bool
68  
bool
77  
ipv6_address::
69  
ipv6_address::
78  
is_v4_mapped() const noexcept
70  
is_v4_mapped() const noexcept
79  
{
71  
{
80  
    return
72  
    return
81  
        addr_[ 0] == 0 && addr_[ 1] == 0 &&
73  
        addr_[ 0] == 0 && addr_[ 1] == 0 &&
82  
        addr_[ 2] == 0 && addr_[ 3] == 0 &&
74  
        addr_[ 2] == 0 && addr_[ 3] == 0 &&
83  
        addr_[ 4] == 0 && addr_[ 5] == 0 &&
75  
        addr_[ 4] == 0 && addr_[ 5] == 0 &&
84  
        addr_[ 6] == 0 && addr_[ 7] == 0 &&
76  
        addr_[ 6] == 0 && addr_[ 7] == 0 &&
85  
        addr_[ 8] == 0 && addr_[ 9] == 0 &&
77  
        addr_[ 8] == 0 && addr_[ 9] == 0 &&
86  
        addr_[10] == 0xff &&
78  
        addr_[10] == 0xff &&
87  
        addr_[11] == 0xff;
79  
        addr_[11] == 0xff;
88  
}
80  
}
89  

81  

90  
ipv6_address
82  
ipv6_address
91  
ipv6_address::
83  
ipv6_address::
92  
loopback() noexcept
84  
loopback() noexcept
93  
{
85  
{
94  
    ipv6_address a;
86  
    ipv6_address a;
95  
    a.addr_[15] = 1;
87  
    a.addr_[15] = 1;
96  
    return a;
88  
    return a;
97  
}
89  
}
98  

90  

99  
void
91  
void
100  
ipv6_address::
92  
ipv6_address::
101  
write_ostream(
93  
write_ostream(
102  
    std::ostream& os) const
94  
    std::ostream& os) const
103  
{
95  
{
104  
    char buf[ipv6_address::max_str_len];
96  
    char buf[ipv6_address::max_str_len];
105  
    auto const s = to_buffer(buf, sizeof(buf));
97  
    auto const s = to_buffer(buf, sizeof(buf));
106  
    os << s;
98  
    os << s;
107  
}
99  
}
108  

100  

109  
std::size_t
101  
std::size_t
110  
ipv6_address::
102  
ipv6_address::
111  
print_impl(
103  
print_impl(
112  
    char* dest) const noexcept
104  
    char* dest) const noexcept
113  
{
105  
{
114  
    auto const count_zeroes =
106  
    auto const count_zeroes =
115  
    []( unsigned char const* first,
107  
    []( unsigned char const* first,
116  
        unsigned char const* const last)
108  
        unsigned char const* const last)
117  
    {
109  
    {
118  
        std::size_t n = 0;
110  
        std::size_t n = 0;
119  
        while(first != last)
111  
        while(first != last)
120  
        {
112  
        {
121  
            if( first[0] != 0 ||
113  
            if( first[0] != 0 ||
122  
                first[1] != 0)
114  
                first[1] != 0)
123  
                break;
115  
                break;
124  
            n += 2;
116  
            n += 2;
125  
            first += 2;
117  
            first += 2;
126  
        }
118  
        }
127  
        return n;
119  
        return n;
128  
    };
120  
    };
129  
    auto const print_hex =
121  
    auto const print_hex =
130  
    []( char* dest,
122  
    []( char* dest,
131  
        unsigned short v)
123  
        unsigned short v)
132  
    {
124  
    {
133  
        char const* const dig =
125  
        char const* const dig =
134  
            "0123456789abcdef";
126  
            "0123456789abcdef";
135  
        if(v >= 0x1000)
127  
        if(v >= 0x1000)
136  
        {
128  
        {
137  
            *dest++ = dig[v>>12];
129  
            *dest++ = dig[v>>12];
138  
            v &= 0x0fff;
130  
            v &= 0x0fff;
139  
            *dest++ = dig[v>>8];
131  
            *dest++ = dig[v>>8];
140  
            v &= 0x0ff;
132  
            v &= 0x0ff;
141  
            *dest++ = dig[v>>4];
133  
            *dest++ = dig[v>>4];
142  
            v &= 0x0f;
134  
            v &= 0x0f;
143  
            *dest++ = dig[v];
135  
            *dest++ = dig[v];
144  
        }
136  
        }
145  
        else if(v >= 0x100)
137  
        else if(v >= 0x100)
146  
        {
138  
        {
147  
            *dest++ = dig[v>>8];
139  
            *dest++ = dig[v>>8];
148  
            v &= 0x0ff;
140  
            v &= 0x0ff;
149  
            *dest++ = dig[v>>4];
141  
            *dest++ = dig[v>>4];
150  
            v &= 0x0f;
142  
            v &= 0x0f;
151  
            *dest++ = dig[v];
143  
            *dest++ = dig[v];
152  
        }
144  
        }
153  
        else if(v >= 0x10)
145  
        else if(v >= 0x10)
154  
        {
146  
        {
155  
            *dest++ = dig[v>>4];
147  
            *dest++ = dig[v>>4];
156  
            v &= 0x0f;
148  
            v &= 0x0f;
157  
            *dest++ = dig[v];
149  
            *dest++ = dig[v];
158  
        }
150  
        }
159  
        else
151  
        else
160  
        {
152  
        {
161  
            *dest++ = dig[v];
153  
            *dest++ = dig[v];
162  
        }
154  
        }
163  
        return dest;
155  
        return dest;
164  
    };
156  
    };
165  
    auto const dest0 = dest;
157  
    auto const dest0 = dest;
166  
    // find longest run of zeroes
158  
    // find longest run of zeroes
167  
    std::size_t best_len = 0;
159  
    std::size_t best_len = 0;
168  
    int best_pos = -1;
160  
    int best_pos = -1;
169  
    auto it = addr_.data();
161  
    auto it = addr_.data();
170  
    auto const v4 =
162  
    auto const v4 =
171  
        is_v4_mapped();
163  
        is_v4_mapped();
172  
    auto const end = v4 ?
164  
    auto const end = v4 ?
173  
        (it + addr_.size() - 4)
165  
        (it + addr_.size() - 4)
174  
        : it + addr_.size();
166  
        : it + addr_.size();
175  
    while(it != end)
167  
    while(it != end)
176  
    {
168  
    {
177  
        auto n = count_zeroes(
169  
        auto n = count_zeroes(
178  
            it, end);
170  
            it, end);
179  
        if(n == 0)
171  
        if(n == 0)
180  
        {
172  
        {
181  
            it += 2;
173  
            it += 2;
182  
            continue;
174  
            continue;
183  
        }
175  
        }
184  
        if(n > best_len)
176  
        if(n > best_len)
185  
        {
177  
        {
186  
            best_pos = static_cast<
178  
            best_pos = static_cast<
187  
                int>(it - addr_.data());
179  
                int>(it - addr_.data());
188  
            best_len = n;
180  
            best_len = n;
189  
        }
181  
        }
190  
        it += n;
182  
        it += n;
191  
    }
183  
    }
192  
    it = addr_.data();
184  
    it = addr_.data();
193  
    if(best_pos != 0)
185  
    if(best_pos != 0)
194  
    {
186  
    {
195  
        unsigned short v =
187  
        unsigned short v =
196  
            (it[0] * 256U) + it[1];
188  
            (it[0] * 256U) + it[1];
197  
        dest = print_hex(dest, v);
189  
        dest = print_hex(dest, v);
198  
        it += 2;
190  
        it += 2;
199  
    }
191  
    }
200  
    else
192  
    else
201  
    {
193  
    {
202  
        *dest++ = ':';
194  
        *dest++ = ':';
203  
        it += best_len;
195  
        it += best_len;
204  
        if(it == end)
196  
        if(it == end)
205  
            *dest++ = ':';
197  
            *dest++ = ':';
206  
    }
198  
    }
207  
    while(it != end)
199  
    while(it != end)
208  
    {
200  
    {
209  
        *dest++ = ':';
201  
        *dest++ = ':';
210  
        if(it - addr_.data() ==
202  
        if(it - addr_.data() ==
211  
            best_pos)
203  
            best_pos)
212  
        {
204  
        {
213  
            it += best_len;
205  
            it += best_len;
214  
            if(it == end)
206  
            if(it == end)
215  
                *dest++ = ':';
207  
                *dest++ = ':';
216  
            continue;
208  
            continue;
217  
        }
209  
        }
218  
        unsigned short v =
210  
        unsigned short v =
219  
            (it[0] * 256U) + it[1];
211  
            (it[0] * 256U) + it[1];
220  
        dest = print_hex(dest, v);
212  
        dest = print_hex(dest, v);
221  
        it += 2;
213  
        it += 2;
222  
    }
214  
    }
223  
    if(v4)
215  
    if(v4)
224  
    {
216  
    {
225  
        ipv4_address::bytes_type bytes;
217  
        ipv4_address::bytes_type bytes;
226  
        bytes[0] = it[0];
218  
        bytes[0] = it[0];
227  
        bytes[1] = it[1];
219  
        bytes[1] = it[1];
228  
        bytes[2] = it[2];
220  
        bytes[2] = it[2];
229  
        bytes[3] = it[3];
221  
        bytes[3] = it[3];
230  
        ipv4_address a(bytes);
222  
        ipv4_address a(bytes);
231  
        *dest++ = ':';
223  
        *dest++ = ':';
232  
        dest += a.print_impl(dest);
224  
        dest += a.print_impl(dest);
233  
    }
225  
    }
234  
    return dest - dest0;
226  
    return dest - dest0;
235  
}
227  
}
236  

228  

237  
void
229  
void
238  
ipv6_address::
230  
ipv6_address::
239  
to_string_impl(
231  
to_string_impl(
240  
    string_token::arg& t) const
232  
    string_token::arg& t) const
241  
{
233  
{
242  
    char buf[max_str_len];
234  
    char buf[max_str_len];
243  
    auto const n = print_impl(buf);
235  
    auto const n = print_impl(buf);
244  
    char* dest = t.prepare(n);
236  
    char* dest = t.prepare(n);
245  
    std::memcpy(dest, buf, n);
237  
    std::memcpy(dest, buf, n);
246  
}
238  
}
247  

239  

248  
//------------------------------------------------
240  
//------------------------------------------------
249  

241  

250  
auto
242  
auto
251  
parse_ipv6_address(
243  
parse_ipv6_address(
252  
    core::string_view s) noexcept ->
244  
    core::string_view s) noexcept ->
253  
        system::result<ipv6_address>
245  
        system::result<ipv6_address>
254  
{
246  
{
255  
    return grammar::parse(
247  
    return grammar::parse(
256  
        s, ipv6_address_rule);
248  
        s, ipv6_address_rule);
257  
}
249  
}
258  

250  

259  
} // urls
251  
} // urls
260  
} // boost
252  
} // boost
261  

253