From 51b73bdef340e25521b9836ba4b694e3a4c0d2b0 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Tue, 25 Dec 2018 10:43:18 +0100 Subject: [PATCH 1/5] Code cleanup --- CMakeLists.txt | 12 ------- src/control/Control.cpp | 1 - src/control/jobs/PdfExportJob.cpp | 3 -- src/model/Document.cpp | 2 +- src/util/GzHelper.cpp | 42 ------------------------ src/util/GzHelper.h | 29 ---------------- src/util/XournalTypeList.h | 1 + src/view/background/BackgroundConfig.cpp | 7 ++-- 8 files changed, 4 insertions(+), 93 deletions(-) delete mode 100644 src/util/GzHelper.cpp delete mode 100644 src/util/GzHelper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 563dd1b3..98ee6822 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,19 +121,7 @@ add_includes_ldflags ("${GThread_LDFLAGS}" "${GThread_INCLUDE_DIRS}") pkg_check_modules (Libxml REQUIRED "libxml-2.0 >= 2.0.0") add_includes_ldflags ("${Libxml_LDFLAGS}" "${Libxml_INCLUDE_DIRS}") -find_program(LSB_RELEASE_EXEC lsb_release) -execute_process(COMMAND ${LSB_RELEASE_EXEC} -is - OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT - OUTPUT_STRIP_TRAILING_WHITESPACE -) -find_program(LSB_RELEASE_EXEC lsb_release) -execute_process(COMMAND ${LSB_RELEASE_EXEC} -rs - OUTPUT_VARIABLE LSB_RELEASE_NUMBER_SHORT - OUTPUT_STRIP_TRAILING_WHITESPACE -) - # Poppler - pkg_check_modules (PopplerGlib REQUIRED "poppler-glib >= 0.41.0") add_includes_ldflags ("${PopplerGlib_LDFLAGS}" "${PopplerGlib_INCLUDE_DIRS}") set(POPPLER_INCLUDE_DIR, "${PopplerGlib_INCLUDE_DIRS}") diff --git a/src/control/Control.cpp b/src/control/Control.cpp index 1b176c5a..bbd8cd40 100644 --- a/src/control/Control.cpp +++ b/src/control/Control.cpp @@ -1985,7 +1985,6 @@ bool Control::newFile(string pageTemplate) return true; } - /** * Check if this is an autosave file, return false in this case and display a user instruction */ diff --git a/src/control/jobs/PdfExportJob.cpp b/src/control/jobs/PdfExportJob.cpp index e89dbda4..1cd243d3 100644 --- a/src/control/jobs/PdfExportJob.cpp +++ b/src/control/jobs/PdfExportJob.cpp @@ -5,9 +5,6 @@ #include -#include - - PdfExportJob::PdfExportJob(Control* control) : BaseExportJob(control, _("PDF Export")) { diff --git a/src/model/Document.cpp b/src/model/Document.cpp index 1558a101..ebeb0786 100644 --- a/src/model/Document.cpp +++ b/src/model/Document.cpp @@ -187,7 +187,7 @@ path Document::createSaveFilename(DocumentType type, string defaultSaveName) { if (!filename.empty()) { - //This can be any extension + // This can be any extension return filename.stem(); } else if (!pdfFilename.empty()) diff --git a/src/util/GzHelper.cpp b/src/util/GzHelper.cpp deleted file mode 100644 index 8b20d17b..00000000 --- a/src/util/GzHelper.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "GzHelper.h" - -#include "Stacktrace.h" - -#include -#include -namespace bio = boost::iostreams; - -#include -using std::stringstream; - -GzHelper::GzHelper() { } - -GzHelper::~GzHelper() { } - -string GzHelper::gzcompress(const string& str, const bio::zlib_params& params) -{ - stringstream decompressed; - decompressed << str; - - bio::filtering_istreambuf out; - out.push(bio::zlib_compressor(params)); - out.push(decompressed); - - stringstream compressed; - bio::copy(out, compressed); - return compressed.str(); -} - -string GzHelper::gzuncompress(const string& str) -{ - stringstream compressed; - compressed << str; - - bio::filtering_istreambuf in; - in.push(bio::zlib_decompressor()); - in.push(compressed); - - stringstream decompressed; - bio::copy(in, decompressed); - return decompressed.str(); -} diff --git a/src/util/GzHelper.h b/src/util/GzHelper.h deleted file mode 100644 index 620ec5ae..00000000 --- a/src/util/GzHelper.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Xournal++ - * - * Helper functions for ZLib GZip Compression - * - * @author Xournal++ Team - * https://github.com/xournalpp/xournalpp - * - * @license GNU GPLv2 or later - */ - -#pragma once - -#include -namespace bio = boost::iostreams; - -#include -using std::string; - -class GzHelper -{ -private: - GzHelper(); - virtual ~GzHelper(); - -public: - static string gzcompress(const string& str, const bio::zlib_params& = bio::zlib::default_compression); - static string gzuncompress(const string& str); -}; diff --git a/src/util/XournalTypeList.h b/src/util/XournalTypeList.h index ef35e7f7..f25f6a70 100644 --- a/src/util/XournalTypeList.h +++ b/src/util/XournalTypeList.h @@ -250,4 +250,5 @@ XOJ_DECLARE_TYPE(InputSequence, 239); XOJ_DECLARE_TYPE(ZoomGesture, 240); XOJ_DECLARE_TYPE(GroupUndoAction, 241); XOJ_DECLARE_TYPE(FillTransparencyDialog, 242); +XOJ_DECLARE_TYPE(Path, 243); diff --git a/src/view/background/BackgroundConfig.cpp b/src/view/background/BackgroundConfig.cpp index bf4034e2..b9c5c08f 100644 --- a/src/view/background/BackgroundConfig.cpp +++ b/src/view/background/BackgroundConfig.cpp @@ -1,15 +1,12 @@ #include "BackgroundConfig.h" -#include +#include BackgroundConfig::BackgroundConfig(string config) { XOJ_INIT_TYPE(BackgroundConfig); - vector parts; - boost::split(parts, config, boost::is_any_of(",")); - - for (string s : parts) + for (string s : StringUtils::split(config, ',')) { size_t dotPos = s.find_last_of("="); if (dotPos != string::npos) From 00bf7b93fb2f67d3f200af369a648bdbe74e9acd Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Tue, 25 Dec 2018 12:14:43 +0100 Subject: [PATCH 2/5] Use same gzip lib for all file reading --- .travis.yml | 1 - readme/LinuxBuild.md | 7 +- src/util/XojPreviewExtractor.cpp | 172 ++++++++---------- src/util/XojPreviewExtractor.h | 10 +- src/xoj-preview-extractor/CMakeLists.txt | 12 +- .../xournal-thumbnailer.cpp | 4 +- 6 files changed, 94 insertions(+), 112 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d3f1694..11b8251c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,6 @@ addons: - cmake - libboost-filesystem-dev - - libboost-iostreams-dev - libcppunit-dev diff --git a/readme/LinuxBuild.md b/readme/LinuxBuild.md index 701464b3..9cc517b1 100644 --- a/readme/LinuxBuild.md +++ b/readme/LinuxBuild.md @@ -21,15 +21,14 @@ dnf install cmake texlive-scheme-basic texlive-dvipng poppler-glib-devel ### For Ubuntu/Debian: ````bash sudo apt-get install cmake libgtk-3-dev libpoppler-glib-dev \ -libboost-filesystem-dev libboost-locale-dev libboost-iostreams-dev \ -libcppunit-dev dvipng texlive +libboost-filesystem-dev libcppunit-dev dvipng texlive ```` ### For OpenSuse: ```bash sudo zypper install cmake gtk3-devel cppunit-devel \ -libboost_filesystem-devel libboost_locale-devel libboost_iostreams-devel \ -texlive-dvipng texlive libxml2-devel libpoppler-glib-devel +libboost_filesystem-devel texlive-dvipng texlive libxml2-devel \ +libpoppler-glib-devel ``` ### For Fedora: diff --git a/src/util/XojPreviewExtractor.cpp b/src/util/XojPreviewExtractor.cpp index 923429dc..def7505f 100644 --- a/src/util/XojPreviewExtractor.cpp +++ b/src/util/XojPreviewExtractor.cpp @@ -1,22 +1,15 @@ #include "XojPreviewExtractor.h" #include -#include - -#include -#include -#include -namespace bio = boost::iostreams; - -#include -using std::istream; -#include -using std::ifstream; -#include -using std::unique_ptr; - -#define TAG_PREVIEW_NAME "preview" -#define TAG_PAGE_NAME "page" +#include +#include + +const char* TAG_PREVIEW_NAME = "preview"; +const int TAG_PREVIEW_NAME_LEN = strlen(TAG_PREVIEW_NAME); +const char* TAG_PAGE_NAME = "page"; +const int TAG_PAGE_NAME_LEN = strlen(TAG_PAGE_NAME); +const char* TAG_PREVIEW_END_NAME = "/preview"; +const int TAG_PREVIEW_END_NAME_LEN = strlen(TAG_PREVIEW_END_NAME); #define BUF_SIZE 8192 XojPreviewExtractor::XojPreviewExtractor() @@ -32,7 +25,6 @@ XojPreviewExtractor::~XojPreviewExtractor() dataLen = 0; } - /** * @return The preview data, should be a binary PNG */ @@ -42,13 +34,67 @@ unsigned char* XojPreviewExtractor::getData(gsize& dataLen) return this->data; } -#define CLOSE \ - if (gzip) \ - { \ - delete in; \ - } \ - ifile.close(); \ - return +/** + * Try to read the preview from byte buffer + * @param buffer Buffer + * @param len Buffer len + * @return If an image was read, or the error + */ +PreviewExtractResult XojPreviewExtractor::readPreview(char* buffer, int len) +{ + bool inTag = false; + int startTag = 0; + int startPreview = -1; + int endPreview = -1; + int pageStart = -1; + for (int i = 0; i < len; i++) + { + if (inTag) + { + if (buffer[i] == '>') + { + inTag = false; + int tagLen = i - startTag; + if (tagLen == TAG_PREVIEW_NAME_LEN && strncmp(TAG_PREVIEW_NAME, buffer + startTag, TAG_PREVIEW_NAME_LEN) == 0) + { + startPreview = i + 1; + } + if (tagLen == TAG_PREVIEW_END_NAME_LEN && strncmp(TAG_PREVIEW_END_NAME, buffer + startTag, TAG_PREVIEW_END_NAME_LEN) == 0) + { + endPreview = i - TAG_PREVIEW_END_NAME_LEN - 1; + break; + } + if (tagLen >= TAG_PAGE_NAME_LEN && strncmp(TAG_PAGE_NAME, buffer + startTag, TAG_PAGE_NAME_LEN) == 0) + { + pageStart = i; + break; + } + } + continue; + } + + if (buffer[i] == '<') + { + inTag = true; + startTag = i + 1; + continue; + } + } + + if (startPreview != -1 && endPreview != -1) + { + buffer[endPreview] = 0; + this->data = g_base64_decode(buffer + startPreview, &dataLen); + return PREVIEW_RESULT_IMAGE_READ; + } + + if (pageStart != -1) + { + return PREVIEW_RESULT_NO_PREVIEW; + } + + return PREVIEW_RESULT_ERROR_READING_PREVIEW; +} /** * Try to read the preview from file @@ -63,7 +109,6 @@ PreviewExtractResult XojPreviewExtractor::readFile(string file) if (dotPos != string::npos) { ext = file.substr(dotPos); - boost::algorithm::to_lower(ext); } if (!(ext == ".xoj" || ext == ".xopp")) @@ -71,79 +116,22 @@ PreviewExtractResult XojPreviewExtractor::readFile(string file) return PREVIEW_RESULT_BAD_FILE_EXTENSION; } - // open input file - ifstream ifile(file, ifstream::in | ifstream::binary); - if (!ifile.is_open()) + gzFile fp = gzopen(file.c_str(), "r"); + if (!fp) { return PREVIEW_RESULT_COULD_NOT_OPEN_FILE; } - istream* in; - bio::filtering_istreambuf inbuf; - bool gzip; - - // check for gzip magic header - if (ifile.get() == 0x1F && ifile.get() == 0x8B) - { - gzip = true; - ifile.seekg(0); //seek back to beginning - - inbuf.push(bio::gzip_decompressor()); - inbuf.push(ifile); + // The Tag is within the first 179 Bytes + // The Preview should end within the first 8k - in = new istream(&inbuf); - } - else - { - gzip = false; - in = &ifile; - } - - char buf[BUF_SIZE]; - bool inPreview = false; - bool inTag = false; - string preview; - while (!in->eof()) - { - if (in->peek() == '<') - { - in->ignore(); - inTag = true; - continue; - } - - in->get(buf, BUF_SIZE, '<'); - if (!inPreview) - { - if (inTag) - { - if (strncmp(TAG_PREVIEW_NAME, buf, sizeof(TAG_PREVIEW_NAME) - 1) == 0) - { - inPreview = true; - preview += (buf + sizeof(TAG_PREVIEW_NAME)); - } - else if (strncmp(TAG_PAGE_NAME, buf, sizeof(TAG_PAGE_NAME) - 1) == 0) - { - CLOSE PREVIEW_RESULT_NO_PREVIEW; - } - inTag = false; - } - } - else - { - if (inTag) - { - this->data = g_base64_decode(preview.c_str(), &this->dataLen); - CLOSE PREVIEW_RESULT_IMAGE_READ; - } - else - { - preview += buf; - } - } - } + char buffer[BUF_SIZE]; + int readLen = gzread(fp, buffer, BUF_SIZE); + + PreviewExtractResult result = readPreview(buffer, readLen); - CLOSE PREVIEW_RESULT_ERROR_READING_PREVIEW; + gzclose(fp); + return result; } diff --git a/src/util/XojPreviewExtractor.h b/src/util/XojPreviewExtractor.h index f35fd6b6..41929d84 100644 --- a/src/util/XojPreviewExtractor.h +++ b/src/util/XojPreviewExtractor.h @@ -4,7 +4,7 @@ * Extracts a preview of an .xoj file, used by xournal-thumbnailer and xournalpp * Because of this xournal type checks cannot be used * - * @author Andreas Butti + * @author Xournal++ Team * https://github.com/xournalpp/xournalpp * * @license GNU GPLv2 or later @@ -60,6 +60,14 @@ public: */ PreviewExtractResult readFile(string file); + /** + * Try to read the preview from byte buffer + * @param buffer Buffer + * @param len Buffer len + * @return If an image was read, or the error + */ + PreviewExtractResult readPreview(char* buffer, int len); + /** * @return The preview data, should be a binary PNG */ diff --git a/src/xoj-preview-extractor/CMakeLists.txt b/src/xoj-preview-extractor/CMakeLists.txt index c56f0ab5..3f223c01 100644 --- a/src/xoj-preview-extractor/CMakeLists.txt +++ b/src/xoj-preview-extractor/CMakeLists.txt @@ -1,6 +1,7 @@ ## xournal-thumbnailer executable ## if (WIN32) + # Windows does not support the thumbniler like this return () endif () @@ -11,22 +12,11 @@ add_executable (xournal-thumbnailer ) target_link_libraries (xournal-thumbnailer - ${Boost_LIBRARIES} - ${Glibmm_LDFLAGS} ${ZLIB_LIBRARIES} ${Glib_LDFLAGS} - ${GThread_LDFLAGS} - ${Libxml_LDFLAGS} ) -if (BUILD_BOOST) - add_dependencies (xournal-thumbnailer boost) -endif (BUILD_BOOST) - set (THUMBNAILER_BIN "xournal-thumbnailer") -if (WIN32) - set (THUMBNAILER_BIN "${THUMBNAILER_BIN}.exe") -endif (WIN32) add_custom_command (TARGET xournal-thumbnailer POST_BUILD COMMAND ${CMAKE_STRIP} ${THUMBNAILER_BIN} diff --git a/src/xoj-preview-extractor/xournal-thumbnailer.cpp b/src/xoj-preview-extractor/xournal-thumbnailer.cpp index 4a44c995..afd558be 100644 --- a/src/xoj-preview-extractor/xournal-thumbnailer.cpp +++ b/src/xoj-preview-extractor/xournal-thumbnailer.cpp @@ -3,7 +3,7 @@ * * This small program extracts a preview out of a xoj file * - * @author MarPiRK + * @author Xournal++ Team * https://github.com/xournalpp/xournalpp * * @license GPL @@ -23,8 +23,6 @@ using std::cerr; using std::cout; using std::endl; -#include -using std::string; void initLocalisation() { From f4a8704bcf8f518c4fac08948f30f30e1653a0d9 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Tue, 25 Dec 2018 12:20:12 +0100 Subject: [PATCH 3/5] Fixed UnitTests --- test/files/load/test.xoi | 0 test/util/XojPreviewExtractorTest.cpp | 6 +----- 2 files changed, 1 insertion(+), 5 deletions(-) create mode 100644 test/files/load/test.xoi diff --git a/test/files/load/test.xoi b/test/files/load/test.xoi new file mode 100644 index 00000000..e69de29b diff --git a/test/util/XojPreviewExtractorTest.cpp b/test/util/XojPreviewExtractorTest.cpp index 844f0031..c42bd100 100644 --- a/test/util/XojPreviewExtractorTest.cpp +++ b/test/util/XojPreviewExtractorTest.cpp @@ -52,13 +52,9 @@ public: void testExtensionCheck() { XojPreviewExtractor extractor; - PreviewExtractResult result = extractor.readFile(GET_TESTFILE("THIS FILE DOES NOT EXIST.xoi")); + PreviewExtractResult result = extractor.readFile(GET_TESTFILE("test.xoi")); CPPUNIT_ASSERT_EQUAL(PREVIEW_RESULT_BAD_FILE_EXTENSION, result); - - result = extractor.readFile(GET_TESTFILE("THIS FILE DOES NOT EXIST.xOj")); - - CPPUNIT_ASSERT_EQUAL(PREVIEW_RESULT_COULD_NOT_OPEN_FILE, result); } void testLoadGzipped() From 72f3239279ae0f5948db8877734e148e10905d5a Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Tue, 25 Dec 2018 12:32:39 +0100 Subject: [PATCH 4/5] Cleanup --- CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98ee6822..b3034f15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,7 @@ if (BUILD_BOOST) BUILD_COMMAND cd "${BOOST_PREFIX}/src/boost" && ./b2 ${BOOST_CXXFLAGS} - --with-filesystem --with-iostreams + --with-filesystem "--prefix=${BOOST_PREFIX}" "--build-dir=${BOOST_PREFIX}/src/boost-build" "--stagedir=${BOOST_PREFIX}" @@ -89,7 +89,6 @@ if (BUILD_BOOST) link_directories ("${BOOST_PREFIX}/lib/") set (Boost_LIBRARIES boost_filesystem - boost_iostreams ${ICU_LIBRARIES} ) set (Boost_INCLUDE_DIRS "${BOOST_PREFIX}/src/boost") @@ -97,7 +96,7 @@ if (BUILD_BOOST) else (BUILD_BOOST) set (Boost_USE_MULTITHREADED ON) - find_package (Boost 1.54 COMPONENTS filesystem iostreams) + find_package (Boost 1.54 COMPONENTS filesystem) if (Boost_VERSION VERSION_LESS 1.54) message (FATAL_ERROR "Boost 1.54 or newer not found – you should enable BUILD_BOOST CMake flag") endif (Boost_VERSION VERSION_LESS 1.54) From bfd6a6397194f45e145405b5b7cf32403d1d6c17 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Tue, 25 Dec 2018 12:32:48 +0100 Subject: [PATCH 5/5] Cleanup --- .travis.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 11b8251c..cd285b04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,11 +35,6 @@ addons: - libgtk-3-dev - libpoppler-glib-dev -# Do not use PPAs, use standard Ubuntu Sources -# sources: -# - boost-latest -# - kalakris-cmake - coverity_scan: project: name: "xournalpp/xournalpp" @@ -55,7 +50,7 @@ before_script: script: - "cmake .. -DCMAKE_DEBUG_INCLUDES_LDFLAGS=ON -DENABLE_MATHTEX=ON -DENABLE_CPPUNIT=ON -DUNSTABLE_LAYERS_SIDEBAR=ON -DDEBUG_COMPILE=ON" - - "make -j`grep processor /proc/cpuinfo | tail -c 2`" + - "make -j `grep processor /proc/cpuinfo | tail -c 2`" - "ctest" notifications: