From f64f2824ac11e9fb885bce8754fa6327831f3d74 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 20 Feb 2014 15:12:35 -0500 Subject: Add data uri checking back into the code (bzr r13047.1.1) --- src/uri.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index 89f6f33e4..219bc1c96 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -26,6 +26,15 @@ URI::URI(gchar const *preformed) throw(BadURIException) { if (!preformed) { throw MalformedURIException(); } + // One day Inkscape::URI should use std::string by default + std::string path = std::string(preformed); + // Check for a data URI and parse seperately because + // libxml can't handle this; although svgs need to. + if( path.compare(0, 5, "data:") == 0 ) { + parseDataUri( path ); + // Empty the uri for libxml + preformed = ""; + } uri = xmlParseURI(preformed); if (!uri) { throw MalformedURIException(); @@ -135,6 +144,17 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) filename = tmp.toNativeFilename(); return filename; } + +/* + * Parse data:... uris so we can access their data transparently. + * otherwise data: is considered a malformed protocol like http: + * and two // as appended and the data is stored in the path of the uri. + */ +bool URI::parseDataUri(const std::string &uri) { + + return true; +} + /* * Returns the absolute path to an existing file referenced in this URI, * if the uri is data, the path is empty or the file doesn't exist, then @@ -149,6 +169,8 @@ const std::string URI::getFullPath(std::string const base) const { if(!base.empty() && !path.empty() && path[0] != '/') { path = Glib::build_filename(base, path); } + // Path normalisation should go here TODO + // Check the existance of the file if(! g_file_test(path.c_str(), G_FILE_TEST_EXISTS) || g_file_test(path.c_str(), G_FILE_TEST_IS_DIR) ) { -- cgit v1.2.3 From 7f9907d4fa5836d20555343dfe5e13649cad1f30 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 20 Feb 2014 15:48:07 -0500 Subject: Not finished by improved data uri support (bzr r13047.1.2) --- src/uri.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index 219bc1c96..33a2fd13d 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -14,6 +14,8 @@ #include #include +#include + namespace Inkscape { URI::URI(const URI &uri) { @@ -151,6 +153,22 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) * and two // as appended and the data is stored in the path of the uri. */ bool URI::parseDataUri(const std::string &uri) { + unsigned int track = 5; // Ignore start of uri 'data:' + + unsigned int head_end = uri.find(",", track); + if (head_end < uri.length()) { + std::string head = uri.substr(track, head_end); + // Head split by ';' for mime-type, charset and base64 bool + track = head_end + 1; + } else { + head = "No Head"; + } + data = uri.substr(track, uri.length()-track); + if(data_base64) { + // Parse data here + } else { + // Fill data here + } return true; } -- cgit v1.2.3 From 6f844ef457690c841b0be91d70b1e54b61c04812 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Wed, 26 Feb 2014 10:31:28 -0500 Subject: Remove DOM directory and reduce size of inkscape. Use Inkscape::URI and save ziptool to utils. (bzr r13047.1.5) --- src/uri.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index 33a2fd13d..383bdba15 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -18,6 +18,11 @@ namespace Inkscape { +URI::URI() { + const gchar *in = ""; + _impl = Impl::create(xmlParseURI(in)); +} + URI::URI(const URI &uri) { uri._impl->reference(); _impl = uri._impl; @@ -153,25 +158,51 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) * and two // as appended and the data is stored in the path of the uri. */ bool URI::parseDataUri(const std::string &uri) { - unsigned int track = 5; // Ignore start of uri 'data:' - unsigned int head_end = uri.find(",", track); + data_base64 = false; + + unsigned int head_end = uri.find(",", 5); if (head_end < uri.length()) { - std::string head = uri.substr(track, head_end); - // Head split by ';' for mime-type, charset and base64 bool - track = head_end + 1; - } else { - head = "No Head"; - } - data = uri.substr(track, uri.length()-track); - if(data_base64) { - // Parse data here - } else { - // Fill data here + unsigned int last_sep = 5; + while (last_sep < head_end) { + unsigned int next_sep = uri.find(";", 5); + if (next_sep > head_end) next_sep = head_end; + + if( uri.compare(last_sep, next_sep, "base64") == 0 ) { + data_base64 = true; + } else if ( uri.compare(last_sep, last_sep+8, "charset=") && data_charset.empty() ) { + data_charset = uri.substr(last_sep+8, next_sep); + } else if ( data_mimetype.empty() ) { + data_mimetype = uri.substr(last_sep, next_sep); + } else { + throw BadURIException(); + } + + last_sep = next_sep; + } } + data = uri.substr(head_end, uri.length() - head_end); return true; } +/* + * Returns the data as a decoded byte vector. + */ +std::vector URI::getDataBytes() const { + std::vector result; + gsize len; + + if(data_base64) { + guchar *decoded = g_base64_decode(data.data(), &len); + for (gsize i = 0; i < len; ++i) { + result.push_back(decoded[i]); + } + g_free(decoded); + } else { + // XXX Unimlemented Octet decoding! + } + return result; +} /* * Returns the absolute path to an existing file referenced in this URI, -- cgit v1.2.3