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  
// Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
11  
#ifndef BOOST_URL_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
12  
#define BOOST_URL_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
12  
#define BOOST_URL_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
13  

13  

14  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/detail/config.hpp>
15  
#include <boost/url/grammar/charset.hpp>
15  
#include <boost/url/grammar/charset.hpp>
16  
#include <boost/url/grammar/digit_chars.hpp>
16  
#include <boost/url/grammar/digit_chars.hpp>
17  
#include <boost/url/grammar/error.hpp>
17  
#include <boost/url/grammar/error.hpp>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  
namespace grammar {
21  
namespace grammar {
22  
namespace implementation_defined {
22  
namespace implementation_defined {
23  

23  

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

91  

92  
} // implementation_defined
92  
} // implementation_defined
93  
} // grammar
93  
} // grammar
94  
} // urls
94  
} // urls
95  
} // boost
95  
} // boost
96  

96  

97  
#endif
97  
#endif