]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/websocket/detail/utf8_checker.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / websocket / detail / utf8_checker.hpp
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/beast
8 //
9
10 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_UTF8_CHECKER_HPP
11 #define BOOST_BEAST_WEBSOCKET_DETAIL_UTF8_CHECKER_HPP
12
13 #include <boost/beast/core/buffers_range.hpp>
14 #include <boost/asio/buffer.hpp>
15
16 #include <cstdint>
17
18 namespace boost {
19 namespace beast {
20 namespace websocket {
21 namespace detail {
22
23 /** A UTF8 validator.
24
25 This validator can be used to check if a buffer containing UTF8 text is
26 valid. The write function may be called incrementally with segmented UTF8
27 sequences. The finish function determines if all processed text is valid.
28 */
29 class utf8_checker
30 {
31 std::size_t need_ = 0; // chars we need to finish the code point
32 std::uint8_t* p_ = cp_; // current position in temp buffer
33 std::uint8_t cp_[4]; // a temp buffer for the code point
34
35 public:
36 /** Prepare to process text as valid utf8
37 */
38 BOOST_BEAST_DECL
39 void
40 reset();
41
42 /** Check that all processed text is valid utf8
43 */
44 BOOST_BEAST_DECL
45 bool
46 finish();
47
48 /** Check if text is valid UTF8
49
50 @return `true` if the text is valid utf8 or false otherwise.
51 */
52 BOOST_BEAST_DECL
53 bool
54 write(std::uint8_t const* in, std::size_t size);
55
56 /** Check if text is valid UTF8
57
58 @return `true` if the text is valid utf8 or false otherwise.
59 */
60 template<class ConstBufferSequence>
61 bool
62 write(ConstBufferSequence const& bs);
63 };
64
65
66 template<class ConstBufferSequence>
67 bool
68 utf8_checker::
69 write(ConstBufferSequence const& buffers)
70 {
71 static_assert(
72 net::is_const_buffer_sequence<ConstBufferSequence>::value,
73 "ConstBufferSequence type requirements not met");
74 for(auto b : beast::buffers_range_ref(buffers))
75 if(! write(static_cast<
76 std::uint8_t const*>(b.data()),
77 b.size()))
78 return false;
79 return true;
80 }
81
82
83 BOOST_BEAST_DECL
84 bool
85 check_utf8(char const* p, std::size_t n);
86
87 } // detail
88 } // websocket
89 } // beast
90 } // boost
91
92 #if BOOST_BEAST_HEADER_ONLY
93 #include <boost/beast/websocket/detail/utf8_checker.ipp>
94 #endif
95
96 #endif