Overpass API regression suite coverage (Nova Scotia) / overpass_api/frontend/decode_text.cc

overpass_api/frontend/decode_text.cc

14 of 113 instrumented lines run by the suite (12.4%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "decode_text.h"
22void decode_to_utf8(const std::string& input, std::string& result,
23 std::string::size_type& from, std::string::size_type& to,
24 Error_Output* error_output)
250{
260 std::string::size_type limit = from + 4;
270 if (result.size() < limit)
280 limit = result.size();
300 uint val = 0;
310 while (from < limit)
320 {
330 val *= 16;
340 if (input[from] <= '9' && input[from] >= '0')
350 val += (input[from] - '0');
360 else if (input[from] >= 'a' && input[from] <= 'f')
370 val += (input[from] - ('a' - 10));
380 else if (input[from] >= 'A' && input[from] <= 'F')
390 val += (input[from] - ('A' - 10));
400 else
410 break;
420 ++from;
430 }
440 if (val < 0x20)
450 {
460 if (error_output)
470 error_output->add_parse_error("Invalid UTF-8 character (value below 32) in escape sequence.", 0);
480 }
490 else if (val < 0x80)
500 result[to++] = val;
510 else if (val < 0x800)
520 {
530 result[to++] = 0xc0 | (val>>6);
540 result[to++] = 0x80 | (val & 0x3f);
550 }
560 else
570 {
580 result[to++] = 0xe0 | (val>>12);
590 result[to++] = 0x80 | ((val>>6) & 0x3f);
600 result[to++] = 0x80 | (val & 0x3f);
610 }
620}
65std::string decode_json(const std::string& input, Error_Output* error_output, uint frame_size)
665052324 q{
675052324 q if (frame_size > 0 && (input.empty() || (input[0] != '\"' && input[0] != '\'')))
682460232 q return input;
702592215 q std::string::size_type j = 0;
712592215 q std::string result(input.size(), '\x0');
7298836215 q for (std::string::size_type i = frame_size; i < input.size()-frame_size; ++i)
7396244213 q {
7496244213 q if (input[i] == '\\')
750 {
760 ++i;
770 if (input[i] == 'n')
780 result[j++] = '\n';
790 else if (input[i] == 't')
800 result[j++] = '\t';
810 else if (input[i] == 'u')
820 {
830 decode_to_utf8(input, result, ++i, j, error_output);
840 --i;
850 }
860 else
870 result[j++] = input[i];
880 }
8996244213 q else
9096244213 q result[j++] = input[i];
9196244213 q }
922592215 q result.resize(j);
942592215 q return result;
955052324 q}
98std::string decode_to_utf8(const std::string& token, std::string::size_type& pos, Error_Output* error_output)
990{
1000 uint val = 0;
1010 pos += 2;
1020 std::string::size_type max_pos = pos + 4;
1030 if (token.size() < max_pos)
1040 max_pos = token.size();
1050 while (pos < max_pos &&
1060 ((token[pos] >= '0' && token[pos] <= '9')
1070 || (token[pos] >= 'a' && token[pos] <= 'f')
1080 || (token[pos] >= 'A' && token[pos] <= 'F')))
1090 {
1100 val *= 16;
1110 if (token[pos] >= '0' && token[pos] <= '9')
1120 val += (token[pos] - 0x30);
1130 else if (token[pos] >= 'a' && token[pos] <= 'f')
1140 val += (token[pos] - 87);
1150 else if (token[pos] >= 'A' && token[pos] <= 'F')
1160 val += (token[pos] - 55);
1170 ++pos;
1180 }
1190 if (val < 0x20)
1200 {
1210 if (error_output)
1220 error_output->add_parse_error("Invalid UTF-8 character (value below 32) in escape sequence.", 0);
1230 }
1240 else if (val < 0x80)
1250 {
1260 std::string buf = " ";
1270 buf[0] = val;
1280 return buf;
1290 }
1300 else if (val < 0x800)
1310 {
1320 std::string buf = " ";
1330 buf[0] = (0xc0 | (val>>6));
1340 buf[1] = (0x80 | (val & 0x3f));
1350 return buf;
1360 }
1370 else
1380 {
1390 std::string buf = " ";
1400 buf[0] = (0xe0 | (val>>12));
1410 buf[1] = (0x80 | ((val>>6) & 0x3f));
1420 buf[2] = (0x80 | (val & 0x3f));
1430 return buf;
1440 }
1450 return "";
1460}