1  
//
1  
//
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/grammar/charset.hpp>
12  
#include <boost/url/grammar/charset.hpp>
13  
#include <boost/url/grammar/dec_octet_rule.hpp>
13  
#include <boost/url/grammar/dec_octet_rule.hpp>
14  
#include <boost/url/grammar/digit_chars.hpp>
14  
#include <boost/url/grammar/digit_chars.hpp>
15  
#include <boost/url/grammar/error.hpp>
15  
#include <boost/url/grammar/error.hpp>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace urls {
18  
namespace urls {
19  
namespace grammar {
19  
namespace grammar {
20  
namespace implementation_defined {
20  
namespace implementation_defined {
21  
auto
21  
auto
22  
dec_octet_rule_t::
22  
dec_octet_rule_t::
23  
parse(
23  
parse(
24  
    char const*& it,
24  
    char const*& it,
25  
    char const* const end
25  
    char const* const end
26  
        ) const noexcept ->
26  
        ) const noexcept ->
27  
    system::result<value_type>
27  
    system::result<value_type>
28  
{
28  
{
29  
    if(it == end)
29  
    if(it == end)
30  
    {
30  
    {
31  
        // end
31  
        // end
32  
        BOOST_URL_RETURN_EC(
32  
        BOOST_URL_RETURN_EC(
33  
            error::mismatch);
33  
            error::mismatch);
34  
    }
34  
    }
35  
    if(! digit_chars(*it))
35  
    if(! digit_chars(*it))
36  
    {
36  
    {
37  
        // expected DIGIT
37  
        // expected DIGIT
38  
        BOOST_URL_RETURN_EC(
38  
        BOOST_URL_RETURN_EC(
39  
            error::mismatch);
39  
            error::mismatch);
40  
    }
40  
    }
41  
    unsigned v = *it - '0';
41  
    unsigned v = *it - '0';
42  
    ++it;
42  
    ++it;
43  
    if( it == end ||
43  
    if( it == end ||
44  
        ! digit_chars(*it))
44  
        ! digit_chars(*it))
45  
    {
45  
    {
46  
        return static_cast<
46  
        return static_cast<
47  
            value_type>(v);
47  
            value_type>(v);
48  
    }
48  
    }
49  
    if(v == 0)
49  
    if(v == 0)
50  
    {
50  
    {
51  
        // leading '0'
51  
        // leading '0'
52  
        BOOST_URL_RETURN_EC(
52  
        BOOST_URL_RETURN_EC(
53  
            error::invalid);
53  
            error::invalid);
54  
    }
54  
    }
55  
    v = (10 * v) + *it - '0';
55  
    v = (10 * v) + *it - '0';
56  
    ++it;
56  
    ++it;
57  
    if( it == end ||
57  
    if( it == end ||
58  
        ! digit_chars(*it))
58  
        ! digit_chars(*it))
59  
    {
59  
    {
60  
        return static_cast<
60  
        return static_cast<
61  
            value_type>(v);
61  
            value_type>(v);
62  
    }
62  
    }
63  
    if(v > 25)
63  
    if(v > 25)
64  
    {
64  
    {
65  
        // integer overflow
65  
        // integer overflow
66  
        BOOST_URL_RETURN_EC(
66  
        BOOST_URL_RETURN_EC(
67  
            error::invalid);
67  
            error::invalid);
68  
    }
68  
    }
69  
    v = (10 * v) + *it - '0';
69  
    v = (10 * v) + *it - '0';
70  
    if(v > 255)
70  
    if(v > 255)
71  
    {
71  
    {
72  
        // integer overflow
72  
        // integer overflow
73  
        BOOST_URL_RETURN_EC(
73  
        BOOST_URL_RETURN_EC(
74  
            error::invalid);
74  
            error::invalid);
75  
    }
75  
    }
76  
    ++it;
76  
    ++it;
77  
    if( it != end &&
77  
    if( it != end &&
78  
        digit_chars(*it))
78  
        digit_chars(*it))
79  
    {
79  
    {
80  
        // integer overflow
80  
        // integer overflow
81  
        BOOST_URL_RETURN_EC(
81  
        BOOST_URL_RETURN_EC(
82  
            error::invalid);
82  
            error::invalid);
83  
    }
83  
    }
84  
    return static_cast<
84  
    return static_cast<
85  
        value_type>(v);
85  
        value_type>(v);
86  
}
86  
}
87  
} // implementation_defined
87  
} // implementation_defined
88  
} // grammar
88  
} // grammar
89  
} // urls
89  
} // urls
90  
} // boost
90  
} // boost
91  

91