Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 69 additions & 18 deletions src/expat/escape_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,82 @@
* along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
*/

#include <ostream>
#include <string>

#include "escape_json.h"


std::string escape_cstr(const std::string& s)
namespace
{
std::string result;
result.reserve(s.length()*2);
for (std::string::size_type i(0); i < s.size(); ++i)

struct Replacements
{
Replacements()
{
for (int c = 0; c < 256; ++c)
replacement[c] = c < 32 ? "?" : 0;
replacement[(unsigned char)'\"'] = "\\\"";
replacement[(unsigned char)'\\'] = "\\\\";
replacement[(unsigned char)'\n'] = "\\n";
replacement[(unsigned char)'\t'] = "\\t";
replacement[(unsigned char)'\r'] = "\\r";
}

// What each byte is written as, or 0 if it is written as it is.
const char* replacement[256];
};


const Replacements& replacements()
{
static const Replacements result;
return result;
}


struct Stream_Sink
{
std::ostream& out;
void append(const char* s, std::string::size_type n) { out.write(s, n); }
void append(const char* s) { out<<s; }
};


template< typename Sink >
void write_escaped(const std::string& s, Sink& sink)
{
const Replacements& table = replacements();
const char* run = s.data();
const char* end = s.data() + s.size();
for (const char* pos = run; pos != end; ++pos)
{
if (s[i] == '\"')
result += "\\\"";
else if (s[i] == '\\')
result += "\\\\";
else if (s[i] == '\n')
result += "\\n";
else if (s[i] == '\t')
result += "\\t";
else if (s[i] == '\r')
result += "\\r";
else if ((unsigned char)s[i] < 32)
result += '?';
else
result += s[i];
const char* replacement = table.replacement[(unsigned char)*pos];
if (replacement)
{
sink.append(run, pos - run);
sink.append(replacement);
run = pos + 1;
}
}
sink.append(run, end - run);
}

}


std::string escape_cstr(const std::string& s)
{
std::string result;
result.reserve(s.size());
write_escaped(s, result);
return result;
}


std::ostream& operator<<(std::ostream& out, const Json_Escaped& e)
{
Stream_Sink sink{out};
write_escaped(e.s, sink);
return out;
}
11 changes: 11 additions & 0 deletions src/expat/escape_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@
#ifndef DE__OSM3S___EXPAT__ESCAPE_JSON_H
#define DE__OSM3S___EXPAT__ESCAPE_JSON_H

#include <ostream>
#include <string>


std::string escape_cstr(const std::string& s);


// Writes escape_cstr(s) to a stream without building the escaped copy. s must outlive the expression.
struct Json_Escaped
{
explicit Json_Escaped(const std::string& s_) : s(s_) {}
const std::string& s;
};

std::ostream& operator<<(std::ostream& out, const Json_Escaped& e);


#endif
88 changes: 68 additions & 20 deletions src/expat/escape_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,84 @@
* along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
*/

#include <ostream>
#include <string>

#include "escape_xml.h"


std::string escape_xml(const std::string& s)
namespace
{
const char* digit = "0123456789abcdef";

std::string result;
result.reserve(s.length()*2);
for (std::string::size_type i(0); i < s.size(); ++i)
struct Replacements
{
Replacements()
{
if (s[i] == '&')
result += "&amp;";
else if (s[i] == '\"')
result += "&quot;";
else if (s[i] == '<')
result += "&lt;";
else if (s[i] == '>')
result += "&gt;";
else if ((unsigned char)s[i] < 32)
for (int c = 0; c < 256; ++c)
replacement[c] = c < 32 ? "?" : 0;
replacement[(unsigned char)'&'] = "&amp;";
replacement[(unsigned char)'\"'] = "&quot;";
replacement[(unsigned char)'<'] = "&lt;";
replacement[(unsigned char)'>'] = "&gt;";
replacement[(unsigned char)'\t'] = "&#x09;";
replacement[(unsigned char)'\n'] = "&#x0a;";
replacement[(unsigned char)'\r'] = "&#x0d;";
}

// What each byte is written as, or 0 if it is written as it is.
const char* replacement[256];
};


const Replacements& replacements()
{
static const Replacements result;
return result;
}


struct Stream_Sink
{
std::ostream& out;
void append(const char* s, std::string::size_type n) { out.write(s, n); }
void append(const char* s) { out<<s; }
};


template< typename Sink >
void write_escaped(const std::string& s, Sink& sink)
{
const Replacements& table = replacements();
const char* run = s.data();
const char* end = s.data() + s.size();
for (const char* pos = run; pos != end; ++pos)
{
const char* replacement = table.replacement[(unsigned char)*pos];
if (replacement)
{
if ((s[i] == '\n') || (s[i] == '\t') || (s[i] == '\r'))
result += std::string("&#x") + digit[s[i] / 16] + digit[s[i] % 16] + ';';
else
result += '?';
sink.append(run, pos - run);
sink.append(replacement);
run = pos + 1;
}
else
result += s[i];
}
sink.append(run, end - run);
}

}


std::string escape_xml(const std::string& s)
{
std::string result;
result.reserve(s.size());
write_escaped(s, result);
return result;
}


std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e)
{
Stream_Sink sink{out};
write_escaped(e.s, sink);
return out;
}
11 changes: 11 additions & 0 deletions src/expat/escape_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@
#ifndef DE__OSM3S___EXPAT__ESCAPE_XML_H
#define DE__OSM3S___EXPAT__ESCAPE_XML_H

#include <ostream>
#include <string>


std::string escape_xml(const std::string& s);


// Writes escape_xml(s) to a stream without building the escaped copy. s must outlive the expression.
struct Xml_Escaped
{
explicit Xml_Escaped(const std::string& s_) : s(s_) {}
const std::string& s;
};

std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e);


#endif
2 changes: 1 addition & 1 deletion src/overpass_api/output_formats/output_custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ std::string process_members(const std::string& raw_template, uint32 id, const Re
else if (raw_template.substr(new_pos, 10) == "{{{role}}}")
{
std::map< uint32, std::string >::const_iterator rit = roles.find(entry.role);
result<<escape_xml(rit != roles.end() ? rit->second : "???");
result<<Xml_Escaped(rit != roles.end() ? rit->second : "???");
}
else if (raw_template.substr(new_pos, 8) == "{{first:")
;
Expand Down
8 changes: 4 additions & 4 deletions src/overpass_api/output_formats/output_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void print_meta_json(const OSM_Element_Metadata_Skeleton< Id_Type >& meta,
",\n \"changeset\": "<<meta.changeset;
std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id);
if (it != users.end())
std::cout<<",\n \"user\": \""<<escape_cstr(it->second)<<"\"";
std::cout<<",\n \"user\": \""<<Json_Escaped(it->second)<<"\"";
std::cout<<",\n \"uid\": "<<meta.user_id;
}

Expand All @@ -97,9 +97,9 @@ void print_tags(const std::vector< std::pair< std::string, std::string > >* tags
{
std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin();
std::cout<<",\n \"tags\": {"
"\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
"\n \""<<Json_Escaped(it->first)<<"\": \""<<Json_Escaped(it->second)<<"\"";
for (++it; it != tags->end(); ++it)
std::cout<<",\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
std::cout<<",\n \""<<Json_Escaped(it->first)<<"\": \""<<Json_Escaped(it->second)<<"\"";
std::cout<<"\n }";
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ void Output_JSON::print_item(const Relation_Skeleton& skel,
std::cout <<"\n {"
"\n \"type\": \""<<member_type_name(skel.members[i].type)<<
"\",\n \"ref\": "<<skel.members[i].ref.val()<<
",\n \"role\": \""<<escape_cstr(rit != roles->end() ? rit->second : "???") << "\"";
",\n \"role\": \""<<Json_Escaped(rit != roles->end() ? rit->second : "???") << "\"";

if (skel.members[i].type == Relation_Entry::NODE &&
geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i))
Expand Down
6 changes: 3 additions & 3 deletions src/overpass_api/output_formats/output_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void print_meta_xml(const OSM_Element_Metadata_Skeleton< Id_Type >& meta,
<<"\" changeset=\""<<meta.changeset<<"\" uid=\""<<meta.user_id<<"\"";
std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id);
if (it != users.end())
std::cout<<" user=\""<<escape_xml(it->second)<<"\"";
std::cout<<" user=\""<<Xml_Escaped(it->second)<<"\"";
}


Expand Down Expand Up @@ -152,7 +152,7 @@ void print_tags(const std::vector< std::pair< std::string, std::string > >* tags
}
for (std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin();
it != tags->end(); ++it)
std::cout<<" <tag k=\""<<escape_xml(it->first)<<"\" v=\""<<escape_xml(it->second)<<"\"/>\n";
std::cout<<" <tag k=\""<<Xml_Escaped(it->first)<<"\" v=\""<<Xml_Escaped(it->second)<<"\"/>\n";
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ void print_members(const Relation_Skeleton& skel, const Opaque_Geometry& geometr
std::map< uint32, std::string >::const_iterator it = roles.find(skel.members[i].role);
std::cout<<" <member type=\""<<member_type_name(skel.members[i].type)
<<"\" ref=\""<<skel.members[i].ref.val()
<<"\" role=\""<<escape_xml(it != roles.end() ? it->second : "???")<<"\"";
<<"\" role=\""<<Xml_Escaped(it != roles.end() ? it->second : "???")<<"\"";

if (skel.members[i].type == Relation_Entry::NODE)
{
Expand Down