0x3E8
  5   5ˆl–Ãa¾”êh¥2 ¸&®6â˜´o�þDÛÎ,Ü¤’¸Æ ùÑƒ}À9Ð¿Ó %†    5//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_UTF8_CHECKER_IPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_UTF8_CHECKER_IPP

#include <boost/beast/websocket/detail/utf8_checker.hpp>

#include <boost/assert.hpp>

namespace boost {
namespace beast {
namespace websocket {
namespace detail {

void
utf8_checker::
reset()
{
    need_ = 0;
    p_ = cp_;
}

bool
utf8_checker::
finish()
{
    auto const success = need_ == 0;
    reset();
    return success;
}

bool
utf8_checker::
write(std::uint8_t const* in, std::size_t size)
{
    auto const valid =
        [](std::uint8_t const*& p)
        {
            if(p[0] < 128)
            {
                ++p;
                return true;
            }
            if((p[0] & 0xe0) == 0xc0)
            {
                if( (p[1] & 0xc0) != 0x80 ||
                    (p[0] & 0x1e) == 0)  // overlong
                    return false;
                p += 2;
                return true;
            }
            if((p[0] & 0xf0) == 0xe0)
            {
                if(    (p[1] & 0xc0) != 0x80
                    || (p[2] & 0xc0) != 0x80
                    || (p[0] == 0xe0 && (p[1] & 0x20) == 0) // overlong
                    || (p[0] == 0xed && (p[1] & 0x20) == 0x20) // surrogate
                    //|| (p[0] == 0xef && p[1] == 0xbf && (p[2] & 0xfe) == 0xbe) // U+FFFE or U+FFFF
                    )
                    return false;
                p += 3;
                return true;
            }
            if((p[0] & 0xf8) == 0xf0)
            {
                if(    (p[0] & 0x07) >= 0x05 // invalid F5...FF characters
                    || (p[1] & 0xc0) != 0x80
                    || (p[2] & 0xc0) != 0x80
                    || (p[3] & 0xc0) != 0x80
                    || (p[0] == 0xf0 && (p[1] & 0x30) == 0) // overlong
                    || (p[0] == 0xf4 && p[1] > 0x8f) || p[0] > 0xf4 // > U+10FFFF
                    )
                    return false;
                p += 4;
                return true;
            }
            return false;
        };
    auto const fail_fast =
        [&]()
        {
            if(cp_[0] < 128)
            {
                return false;
            }

            const auto& p = cp_; // alias, only to keep this code similar to valid() above
            const auto known_only = p_ - cp_;
            if (known_only == 1)
            {
                if((p[0] & 0xe0) == 0xc0)
                {
                    return ((p[0] & 0x1e) == 0);  // overlong
                }
                if((p[0] & 0xf0) == 0xe0)
                {
                    return false;
                }
                if((p[0] & 0xf8) == 0xf0)
                {
                    return ((p[0] & 0x07) >= 0x05);  // invalid F5...FF characters
                }
            }
            else if (known_only == 2)
            {
                if((p[0] & 0xe0) == 0xc0)
                {
                    return ((p[1] & 0xc0) != 0x80 ||
                            (p[0] & 0x1e) == 0);  // overlong
                }
                if((p[0] & 0xf0) == 0xe0)
                {
                    return (  (p[1] & 0xc0) != 0x80
                           || (p[0] == 0xe0 && (p[1] & 0x20) == 0) // overlong
                           || (p[0] == 0xed && (p[1] & 0x20) == 0x20)); // surrogate
                }
                if((p[0] & 0xf8) == 0xf0)
                {
                    return (  (p[0] & 0x07) >= 0x05 // invalid F5...FF characters
                           || (p[1] & 0xc0) != 0x80
                           || (p[0] == 0xf0 && (p[1] & 0x30) == 0) // overlong
                           || (p[0] == 0xf4 && p[1] > 0x8f) || p[0] > 0xf4); // > U+10FFFF
                }
            }
          