]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // |
92f5a8d4 | 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
b32b8144 FG |
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 | ||
92f5a8d4 | 13 | #include <boost/beast/core/buffers_range.hpp> |
b32b8144 | 14 | #include <boost/asio/buffer.hpp> |
92f5a8d4 | 15 | |
b32b8144 FG |
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 | */ | |
92f5a8d4 | 29 | class utf8_checker |
b32b8144 FG |
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 | */ | |
92f5a8d4 | 38 | BOOST_BEAST_DECL |
b32b8144 FG |
39 | void |
40 | reset(); | |
41 | ||
42 | /** Check that all processed text is valid utf8 | |
43 | */ | |
92f5a8d4 | 44 | BOOST_BEAST_DECL |
b32b8144 FG |
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 | */ | |
92f5a8d4 | 52 | BOOST_BEAST_DECL |
b32b8144 FG |
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 | ||
b32b8144 | 65 | |
b32b8144 FG |
66 | template<class ConstBufferSequence> |
67 | bool | |
92f5a8d4 TL |
68 | utf8_checker:: |
69 | write(ConstBufferSequence const& buffers) | |
b32b8144 | 70 | { |
92f5a8d4 TL |
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< | |
b32b8144 FG |
76 | std::uint8_t const*>(b.data()), |
77 | b.size())) | |
78 | return false; | |
79 | return true; | |
80 | } | |
81 | ||
b32b8144 | 82 | |
92f5a8d4 | 83 | BOOST_BEAST_DECL |
b32b8144 | 84 | bool |
92f5a8d4 | 85 | check_utf8(char const* p, std::size_t n); |
b32b8144 FG |
86 | |
87 | } // detail | |
88 | } // websocket | |
89 | } // beast | |
90 | } // boost | |
91 | ||
92f5a8d4 TL |
92 | #if BOOST_BEAST_HEADER_ONLY |
93 | #include <boost/beast/websocket/detail/utf8_checker.ipp> | |
94 | #endif | |
95 | ||
b32b8144 | 96 | #endif |