Branch data Line data Source code
1 : : // Copyright 2016 Wladimir J. van der Laan
2 : : // Copyright (c) The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or https://opensource.org/license/mit.
5 : :
6 : : #ifndef BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H
7 : : #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H
8 : :
9 : : #include <string>
10 : :
11 : : /**
12 : : * Filter that generates and validates UTF-8, as well as collates UTF-16
13 : : * surrogate pairs as specified in RFC4627.
14 : : */
15 : : class JSONUTF8StringFilter
16 : : {
17 : : public:
18 : 568315 : explicit JSONUTF8StringFilter(std::string& s)
19 : 568315 : : str(s)
20 : : {
21 : 568315 : }
22 : : // Write single 8-bit char (may be part of UTF-8 sequence)
23 : 25706705 : void push_back(unsigned char ch)
24 : : {
25 [ + + ]: 25706705 : if (state == 0) {
26 [ + + ]: 24748674 : if (ch < 0x80) // 7-bit ASCII, fast direct pass-through
27 : 24234368 : str.push_back(ch);
28 [ + + ]: 514306 : else if (ch < 0xc0) // Mid-sequence character, invalid in this state
29 : 60350 : is_valid = false;
30 [ + + ]: 453956 : else if (ch < 0xe0) { // Start of 2-byte sequence
31 : 149631 : codepoint = (ch & 0x1f) << 6;
32 : 149631 : state = 6;
33 [ + + ]: 304325 : } else if (ch < 0xf0) { // Start of 3-byte sequence
34 : 57495 : codepoint = (ch & 0x0f) << 12;
35 : 57495 : state = 12;
36 [ + + ]: 246830 : } else if (ch < 0xf8) { // Start of 4-byte sequence
37 : 231456 : codepoint = (ch & 0x07) << 18;
38 : 231456 : state = 18;
39 : : } else // Reserved, invalid
40 : 15374 : is_valid = false;
41 : : } else {
42 [ + + ]: 958031 : if ((ch & 0xc0) != 0x80) // Not a continuation, invalid
43 : 446177 : is_valid = false;
44 : 958031 : state -= 6;
45 : 958031 : codepoint |= (ch & 0x3f) << state;
46 [ + + ]: 958031 : if (state == 0)
47 : 437992 : push_back_u(codepoint);
48 : : }
49 : 25706705 : }
50 : : // Write codepoint directly, possibly collating surrogate pairs
51 : 493685 : void push_back_u(unsigned int codepoint_)
52 : : {
53 [ + + ]: 493685 : if (state) // Only accept full codepoints in open state
54 : 3395 : is_valid = false;
55 [ + + ]: 493685 : if (codepoint_ >= 0xD800 && codepoint_ < 0xDC00) { // First half of surrogate pair
56 [ + + ]: 10964 : if (surpair) // Two subsequent surrogate pair openers - fail
57 : 4610 : is_valid = false;
58 : : else
59 : 6354 : surpair = codepoint_;
60 [ + + ]: 482721 : } else if (codepoint_ >= 0xDC00 && codepoint_ < 0xE000) { // Second half of surrogate pair
61 [ + + ]: 10408 : if (surpair) { // Open surrogate pair, expect second half
62 : : // Compute code point from UTF-16 surrogate pair
63 : 5763 : append_codepoint(0x10000 | ((surpair - 0xD800)<<10) | (codepoint_ - 0xDC00));
64 : 5763 : surpair = 0;
65 : : } else // Second half doesn't follow a first half - fail
66 : 4645 : is_valid = false;
67 : : } else {
68 [ + + ]: 472313 : if (surpair) // First half of surrogate pair not followed by second - fail
69 : 6026 : is_valid = false;
70 : : else
71 : 466287 : append_codepoint(codepoint_);
72 : : }
73 : 493685 : }
74 : : // Check that we're in a state where the string can be ended
75 : : // No open sequences, no open surrogate pairs, etc
76 : 564916 : bool finalize()
77 : : {
78 [ + + + + ]: 564916 : if (state || surpair)
79 : 398 : is_valid = false;
80 [ + + ]: 564916 : return is_valid;
81 : : }
82 : : private:
83 : : std::string &str;
84 : : bool is_valid{true};
85 : : // Current UTF-8 decoding state
86 : : unsigned int codepoint{0};
87 : : int state{0}; // Top bit to be filled in for next UTF-8 byte, or 0
88 : :
89 : : // Keep track of the following state to handle the following section of
90 : : // RFC4627:
91 : : //
92 : : // To escape an extended character that is not in the Basic Multilingual
93 : : // Plane, the character is represented as a twelve-character sequence,
94 : : // encoding the UTF-16 surrogate pair. So, for example, a string
95 : : // containing only the G clef character (U+1D11E) may be represented as
96 : : // "\uD834\uDD1E".
97 : : //
98 : : // Two subsequent \u.... may have to be replaced with one actual codepoint.
99 : : unsigned int surpair{0}; // First half of open UTF-16 surrogate pair, or 0
100 : :
101 : 472050 : void append_codepoint(unsigned int codepoint_)
102 : : {
103 [ + + ]: 472050 : if (codepoint_ <= 0x7f)
104 : 43831 : str.push_back((char)codepoint_);
105 [ + + ]: 428219 : else if (codepoint_ <= 0x7FF) {
106 : 131342 : str.push_back((char)(0xC0 | (codepoint_ >> 6)));
107 : 131342 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
108 [ + + ]: 296877 : } else if (codepoint_ <= 0xFFFF) {
109 : 61152 : str.push_back((char)(0xE0 | (codepoint_ >> 12)));
110 : 61152 : str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
111 : 61152 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
112 [ + - ]: 235725 : } else if (codepoint_ <= 0x1FFFFF) {
113 : 235725 : str.push_back((char)(0xF0 | (codepoint_ >> 18)));
114 : 235725 : str.push_back((char)(0x80 | ((codepoint_ >> 12) & 0x3F)));
115 : 235725 : str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
116 : 235725 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
117 : : }
118 : 472050 : }
119 : : };
120 : :
121 : : #endif // BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H
|