| 1 | // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer in the |
| 10 | // documentation and/or other materials provided with the distribution. |
| 11 | // * Neither the name of the Andrey N. Sabelnikov nor the |
| 12 | // names of its contributors may be used to endorse or promote products |
| 13 | // derived from this software without specific prior written permission. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY |
| 19 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | |
| 27 | #include "storages/parserse_base_utils.h" |
| 28 | |
| 29 | #include "misc_log_ex.h" |
| 30 | #include <boost/utility/string_ref.hpp> |
| 31 | #include <algorithm> |
| 32 | |
| 33 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 34 | #define MONERO_DEFAULT_LOG_CATEGORY "serialization" |
| 35 | |
| 36 | namespace epee |
| 37 | { |
| 38 | namespace misc_utils |
| 39 | { |
| 40 | namespace parse |
| 41 | { |
| 42 | std::string transform_to_escape_sequence(const std::string& src) |
| 43 | { |
| 44 | static const char escaped[] = "\b\f\n\r\t\v\"\\/"; |
| 45 | std::string::const_iterator it = std::find_first_of(src.begin(), src.end(), escaped, escaped + sizeof(escaped)); |
| 46 | if (it == src.end()) |
| 47 | return src; |
| 48 | |
| 49 | std::string res; |
| 50 | res.reserve(2 * src.size()); |
| 51 | res.assign(src.begin(), it); |
| 52 | for(; it!=src.end(); ++it) |
| 53 | { |
| 54 | switch(*it) |
| 55 | { |
| 56 | case '\b': //Backspace (ascii code 08) |
| 57 | res+="\\b"; break; |
| 58 | case '\f': //Form feed (ascii code 0C) |
| 59 | res+="\\f"; break; |
| 60 | case '\n': //New line |
| 61 | res+="\\n"; break; |
| 62 | case '\r': //Carriage return |
| 63 | res+="\\r"; break; |
| 64 | case '\t': //Tab |
| 65 | res+="\\t"; break; |
| 66 | case '\v': //Vertical tab |
| 67 | res+="\\v"; break; |
| 68 | //case '\'': //Apostrophe or single quote |
| 69 | // res+="\\'"; break; |
| 70 | case '"': //Double quote |
| 71 | res+="\\\""; break; |
| 72 | case '\\': //Backslash caracter |
| 73 | res+="\\\\"; break; |
| 74 | case '/': //Backslash caracter |
| 75 | res+="\\/"; break; |
| 76 | default: |
| 77 | res.push_back(*it); |
| 78 | } |
| 79 | } |
| 80 | return res; |
| 81 | } |
| 82 | /* |
| 83 | |
| 84 | \b Backspace (ascii code 08) |
| 85 | \f Form feed (ascii code 0C) |
| 86 | \n New line |
| 87 | \r Carriage return |
| 88 | \t Tab |
| 89 | \v Vertical tab |
| 90 | \' Apostrophe or single quote |
| 91 | \" Double quote |
| 92 | \\ Backslash character |
| 93 | |
| 94 | */ |
| 95 | void match_string2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val) |
| 96 | { |
| 97 | bool escape_mode = false; |
| 98 | std::string::const_iterator it = star_end_string; |
| 99 | ++it; |
| 100 | std::string::const_iterator fi = it; |
| 101 | while (fi != buf_end && ((lut[(uint8_t)*fi] & 32)) == 0) |
| 102 | ++fi; |
| 103 | val.assign(it, fi); |
| 104 | it = fi; |
| 105 | for(;it != buf_end;++it) |
| 106 | { |
| 107 | if(escape_mode/*prev_ch == '\\'*/) |
| 108 | { |
| 109 | switch(*it) |
| 110 | { |
| 111 | case 'b': //Backspace (ascii code 08) |
| 112 | val.push_back(0x08);break; |
| 113 | case 'f': //Form feed (ascii code 0C) |
| 114 | val.push_back(0x0C);break; |
| 115 | case 'n': //New line |
| 116 | val.push_back('\n');break; |
| 117 | case 'r': //Carriage return |
| 118 | val.push_back('\r');break; |
| 119 | case 't': //Tab |
| 120 | val.push_back('\t');break; |
| 121 | case 'v': //Vertical tab |
| 122 | val.push_back('\v');break; |
| 123 | case '\'': //Apostrophe or single quote |
| 124 | val.push_back('\'');break; |
| 125 | case '"': //Double quote |
| 126 | val.push_back('"');break; |
| 127 | case '\\': //Backslash character |
| 128 | val.push_back('\\');break; |
| 129 | case '/': //Slash character |
| 130 | val.push_back('/');break; |
| 131 | case 'u': //Unicode code point |
| 132 | if (buf_end - it < 4) |
| 133 | { |
| 134 | ASSERT_MES_AND_THROW("Invalid Unicode escape sequence"); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | uint32_t dst = 0; |
| 139 | for (int i = 0; i < 4; ++i) |
| 140 | { |
| 141 | const unsigned char tmp = isx[(unsigned char)*++it]; |
| 142 | CHECK_AND_ASSERT_THROW_MES(tmp != 0xff, "Bad Unicode encoding"); |
| 143 | dst = dst << 4 | tmp; |
| 144 | } |
| 145 | // encode as UTF-8 |
| 146 | if (dst <= 0x7f) |
| 147 | { |
| 148 | val.push_back(dst); |
| 149 | } |
| 150 | else if (dst <= 0x7ff) |
| 151 | { |
| 152 | val.push_back(0xc0 | (dst >> 6)); |
| 153 | val.push_back(0x80 | (dst & 0x3f)); |
| 154 | } |
| 155 | else if (dst <= 0xffff) |
| 156 | { |
| 157 | val.push_back(0xe0 | (dst >> 12)); |
| 158 | val.push_back(0x80 | ((dst >> 6) & 0x3f)); |
| 159 | val.push_back(0x80 | (dst & 0x3f)); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | ASSERT_MES_AND_THROW("Unicode code point is out or range"); |
| 164 | } |
| 165 | } |
| 166 | break; |
| 167 | default: |
| 168 | val.push_back(*it); |
| 169 | LOG_PRINT_L0("Unknown escape sequence :\"\\" << *it << "\""); |
| 170 | } |
| 171 | escape_mode = false; |
| 172 | }else if(*it == '"') |
| 173 | { |
| 174 | star_end_string = it; |
| 175 | return; |
| 176 | }else if(*it == '\\') |
| 177 | { |
| 178 | escape_mode = true; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | val.push_back(*it); |
| 183 | } |
| 184 | } |
| 185 | ASSERT_MES_AND_THROW("Failed to match string in json entry: " << std::string(star_end_string, buf_end)); |
| 186 | } |
| 187 | void match_number2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val, bool& is_float_val, bool& is_signed_val) |
| 188 | { |
| 189 | val.clear(); |
| 190 | uint8_t float_flag = 0; |
| 191 | is_signed_val = false; |
| 192 | size_t chars = 0; |
| 193 | std::string::const_iterator it = star_end_string; |
| 194 | if (it != buf_end && *it == '-') |
| 195 | { |
| 196 | is_signed_val = true; |
| 197 | ++chars; |
| 198 | ++it; |
| 199 | } |
| 200 | for(;it != buf_end;++it) |
| 201 | { |
| 202 | const uint8_t flags = lut[(uint8_t)*it]; |
| 203 | if (flags & 16) |
| 204 | { |
| 205 | float_flag |= flags; |
| 206 | ++chars; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | val = boost::string_ref(&*star_end_string, chars); |
| 211 | if(val.size()) |
| 212 | { |
| 213 | star_end_string = --it; |
| 214 | is_float_val = !!(float_flag & 2); |
| 215 | return; |
| 216 | } |
| 217 | else |
| 218 | ASSERT_MES_AND_THROW("wrong number in json entry: " << std::string(star_end_string, buf_end)); |
| 219 | } |
| 220 | } |
| 221 | ASSERT_MES_AND_THROW("wrong number in json entry: " << std::string(star_end_string, buf_end)); |
| 222 | } |
| 223 | void match_word2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val) |
| 224 | { |
| 225 | val.clear(); |
| 226 | |
| 227 | for(std::string::const_iterator it = star_end_string;it != buf_end;++it) |
| 228 | { |
| 229 | if (!(lut[(uint8_t)*it] & 4)) |
| 230 | { |
| 231 | val = boost::string_ref(&*star_end_string, std::distance(star_end_string, it)); |
| 232 | if(val.size()) |
| 233 | { |
| 234 | star_end_string = --it; |
| 235 | return; |
| 236 | }else |
| 237 | ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end)); |
| 238 | } |
| 239 | } |
| 240 | ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end)); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |