From ddb8af8009f151c7107daf0c2127f0ba2d882649 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Fri, 24 Jan 2014 20:05:26 -0500 Subject: Move absolute path generator to URI and use std::strings (bzr r12977) --- src/uri.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index de6a454ec..8d0e49139 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -10,7 +10,9 @@ #include #include "uri.h" +#include #include +#include namespace Inkscape { @@ -134,6 +136,17 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) return filename; } +const std::string URI::getFullPath(std::string const base) const { + std::string path = std::string(_impl->getPath()); + // Calculate the absolute path from an available base + if(!path.empty() && !base.empty() && path.compare(0, 1, "/") != 0) { + path = Glib::build_filename(base, path); + } + // TODO: Check existance of file here + return path; +} + + /* TODO !!! proper error handling */ gchar *URI::toNativeFilename() const throw(BadURIException) { gchar *uriString = toString(); -- cgit v1.2.3 From 7455f1a259ce28ee56866b5cde06e79be4cfaf97 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 26 Jan 2014 12:19:47 -0500 Subject: Check file existance and clean up memory issues thanks to KK and Johan (bzr r12979) --- src/uri.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index 8d0e49139..89f6f33e4 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -135,14 +135,25 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) filename = tmp.toNativeFilename(); return filename; } - +/* + * 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 + * an empty string is returned. + * + * Does not check if the returned path is the local document's path (local) + * and thus redundent. Caller is expected to check against the document's path. + */ const std::string URI::getFullPath(std::string const base) const { std::string path = std::string(_impl->getPath()); // Calculate the absolute path from an available base - if(!path.empty() && !base.empty() && path.compare(0, 1, "/") != 0) { + if(!base.empty() && !path.empty() && path[0] != '/') { path = Glib::build_filename(base, path); } - // TODO: Check existance of file here + // 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) ) { + path.clear(); + } return path; } -- cgit v1.2.3 From 048e27577b22844eaaab36d1884a511092e6ddf7 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 20 Feb 2014 11:37:22 -0500 Subject: Add test for URI and help with data uris. (bzr r13045) --- src/uri.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index 89f6f33e4..a48aa8274 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -11,6 +11,8 @@ #include #include "uri.h" #include +#include +#include // XXX #include #include @@ -26,6 +28,10 @@ URI::URI(gchar const *preformed) throw(BadURIException) { if (!preformed) { throw MalformedURIException(); } + if( strncmp(preformed, "data:", 5)==0 ) { + parseDataUri( preformed + 5 ); + preformed = ""; // g_free? + } uri = xmlParseURI(preformed); if (!uri) { throw MalformedURIException(); @@ -135,6 +141,38 @@ 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 char *uri) { + + int len = (strchr(uri, ',') - uri) * sizeof(char); + + const char *data; + std::string head; + if (len > 0) { + head = std::string(uri, len); + data = uri + len; + } else { + head = "text/plain"; + data = uri; + } + + //bool base64 = false; + std::cout << "Header: " << head << "\n"; + std::cout << "Data: " << data << "\n"; + /*while(uri < data) { + if (strncmp(uri,"base64",6) == 0) + base64 = true; + if (strncmp(uri,"image/", + data = strchr(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,12 +187,16 @@ const std::string URI::getFullPath(std::string const base) const { if(!base.empty() && !path.empty() && path[0] != '/') { path = Glib::build_filename(base, path); } - // 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) ) { + // Normalise path, this only works if all parts of the path exist + char *output = realpath(path.c_str(), NULL); + if(output == NULL) { + path.clear(); + } else // Re-check the existance of the file (make damn sure) + if(! g_file_test(output, G_FILE_TEST_EXISTS) + || g_file_test(output, G_FILE_TEST_IS_DIR) ) { path.clear(); } - return path; + return std::string( output ); } -- cgit v1.2.3 From bf6ba105405a7eac70f1d36490d51962a4041ac0 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 20 Feb 2014 18:58:24 +0100 Subject: Reverting r13045. 1. It contains a memleak realpath allocates new memory if the second argument is NULL. In the added code, this memory is never freed. 2. Breaks build on Windows (realpath(...) not found) 3. Please don't add functions that use C-strings unnecessarily. Simply rewrite parseDataUri to parseDataUri(const std::string &uri), and use C++'s string methods. ("#include " is a huge red flag upon code review) 4. Please read this about "realpath" http://insanecoding.blogspot.ch/2007/11/pathmax-simply-isnt.html (bzr r13046) --- src/uri.cpp | 50 ++++---------------------------------------------- 1 file changed, 4 insertions(+), 46 deletions(-) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index a48aa8274..89f6f33e4 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -11,8 +11,6 @@ #include #include "uri.h" #include -#include -#include // XXX #include #include @@ -28,10 +26,6 @@ URI::URI(gchar const *preformed) throw(BadURIException) { if (!preformed) { throw MalformedURIException(); } - if( strncmp(preformed, "data:", 5)==0 ) { - parseDataUri( preformed + 5 ); - preformed = ""; // g_free? - } uri = xmlParseURI(preformed); if (!uri) { throw MalformedURIException(); @@ -141,38 +135,6 @@ 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 char *uri) { - - int len = (strchr(uri, ',') - uri) * sizeof(char); - - const char *data; - std::string head; - if (len > 0) { - head = std::string(uri, len); - data = uri + len; - } else { - head = "text/plain"; - data = uri; - } - - //bool base64 = false; - std::cout << "Header: " << head << "\n"; - std::cout << "Data: " << data << "\n"; - /*while(uri < data) { - if (strncmp(uri,"base64",6) == 0) - base64 = true; - if (strncmp(uri,"image/", - data = strchr(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 @@ -187,16 +149,12 @@ const std::string URI::getFullPath(std::string const base) const { if(!base.empty() && !path.empty() && path[0] != '/') { path = Glib::build_filename(base, path); } - // Normalise path, this only works if all parts of the path exist - char *output = realpath(path.c_str(), NULL); - if(output == NULL) { - path.clear(); - } else // Re-check the existance of the file (make damn sure) - if(! g_file_test(output, G_FILE_TEST_EXISTS) - || g_file_test(output, G_FILE_TEST_IS_DIR) ) { + // 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) ) { path.clear(); } - return std::string( output ); + return path; } -- cgit v1.2.3 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 From bb21b145819e999294e711058b26d97e0527a646 Mon Sep 17 00:00:00 2001 From: Nicolas Dufour Date: Tue, 18 Mar 2014 19:30:41 +0100 Subject: Fix for Bug #1291546 (Linking color profile from Document properties dialog crashes Inkscape). Fixed bugs: - https://launchpad.net/bugs/1291546 (bzr r13165) --- src/uri.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/uri.cpp') diff --git a/src/uri.cpp b/src/uri.cpp index e81d108c9..2eaf4ecc1 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -149,6 +149,9 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) * and thus redundent. Caller is expected to check against the document's path. */ const std::string URI::getFullPath(std::string const base) const { + if (!_impl->getPath()) { + return ""; + } std::string path = std::string(_impl->getPath()); // Calculate the absolute path from an available base if(!base.empty() && !path.empty() && path[0] != '/') { -- cgit v1.2.3