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 : 1872627 : explicit JSONUTF8StringFilter(std::string& s)
19 : 1872627 : : str(s)
20 : : {
21 : 1872627 : }
22 : : // Write single 8-bit char (may be part of UTF-8 sequence)
23 : 5091180691 : void push_back(unsigned char ch)
24 : : {
25 [ + + ]: 5091180691 : if (state == 0) {
26 [ + + ]: 5091180340 : if (ch < 0x80) // 7-bit ASCII, fast direct pass-through
27 : 5091180196 : str.push_back(ch);
28 [ + + ]: 144 : else if (ch < 0xc0) // Mid-sequence character, invalid in this state
29 : 3 : is_valid = false;
30 [ + + ]: 141 : else if (ch < 0xe0) { // Start of 2-byte sequence
31 : 1 : codepoint = (ch & 0x1f) << 6;
32 : 1 : state = 6;
33 [ + + ]: 140 : } else if (ch < 0xf0) { // Start of 3-byte sequence
34 : 69 : codepoint = (ch & 0x0f) << 12;
35 : 69 : state = 12;
36 [ + - ]: 71 : } else if (ch < 0xf8) { // Start of 4-byte sequence
37 : 71 : codepoint = (ch & 0x07) << 18;
38 : 71 : state = 18;
39 : : } else // Reserved, invalid
40 : 0 : is_valid = false;
41 : : } else {
42 [ - + ]: 351 : if ((ch & 0xc0) != 0x80) // Not a continuation, invalid
43 : 0 : is_valid = false;
44 : 351 : state -= 6;
45 : 351 : codepoint |= (ch & 0x3f) << state;
46 [ + + ]: 351 : if (state == 0)
47 : 140 : push_back_u(codepoint);
48 : : }
49 : 5091180691 : }
50 : : // Write codepoint directly, possibly collating surrogate pairs
51 : 852 : void push_back_u(unsigned int codepoint_)
52 : : {
53 [ - + ]: 852 : if (state) // Only accept full codepoints in open state
54 : 0 : is_valid = false;
55 [ + + ]: 852 : if (codepoint_ >= 0xD800 && codepoint_ < 0xDC00) { // First half of surrogate pair
56 [ - + ]: 192 : if (surpair) // Two subsequent surrogate pair openers - fail
57 : 0 : is_valid = false;
58 : : else
59 : 192 : surpair = codepoint_;
60 [ + + ]: 660 : } else if (codepoint_ >= 0xDC00 && codepoint_ < 0xE000) { // Second half of surrogate pair
61 [ + + ]: 192 : if (surpair) { // Open surrogate pair, expect second half
62 : : // Compute code point from UTF-16 surrogate pair
63 : 191 : append_codepoint(0x10000 | ((surpair - 0xD800)<<10) | (codepoint_ - 0xDC00));
64 : 191 : surpair = 0;
65 : : } else // Second half doesn't follow a first half - fail
66 : 1 : is_valid = false;
67 : : } else {
68 [ - + ]: 468 : if (surpair) // First half of surrogate pair not followed by second - fail
69 : 0 : is_valid = false;
70 : : else
71 : 468 : append_codepoint(codepoint_);
72 : : }
73 : 852 : }
74 : : // Check that we're in a state where the string can be ended
75 : : // No open sequences, no open surrogate pairs, etc
76 : 1872619 : bool finalize()
77 : : {
78 [ + + + + ]: 1872619 : if (state || surpair)
79 : 2 : is_valid = false;
80 [ + + ]: 1872619 : 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 : 659 : void append_codepoint(unsigned int codepoint_)
102 : : {
103 [ + + ]: 659 : if (codepoint_ <= 0x7f)
104 : 98 : str.push_back((char)codepoint_);
105 [ + + ]: 561 : else if (codepoint_ <= 0x7FF) {
106 : 11 : str.push_back((char)(0xC0 | (codepoint_ >> 6)));
107 : 11 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
108 [ + + ]: 550 : } else if (codepoint_ <= 0xFFFF) {
109 : 289 : str.push_back((char)(0xE0 | (codepoint_ >> 12)));
110 : 289 : str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
111 : 289 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
112 [ + - ]: 261 : } else if (codepoint_ <= 0x1FFFFF) {
113 : 261 : str.push_back((char)(0xF0 | (codepoint_ >> 18)));
114 : 261 : str.push_back((char)(0x80 | ((codepoint_ >> 12) & 0x3F)));
115 : 261 : str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
116 : 261 : str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
117 : : }
118 : 659 : }
119 : : };
120 : :
121 : : #endif // BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_UTFFILTER_H
|