summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
commitcca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch)
tree146f39ded1c938019e1ed42d30923c2ac9e86789 /src/util
parentInitial commit. (diff)
downloadinkscape-upstream.tar.xz
inkscape-upstream.zip
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/CMakeLists.txt34
-rw-r--r--src/util/README9
-rw-r--r--src/util/const_char_ptr.h51
-rw-r--r--src/util/enums.h134
-rw-r--r--src/util/expression-evaluator.cpp396
-rw-r--r--src/util/expression-evaluator.h196
-rw-r--r--src/util/fixed_point.h111
-rw-r--r--src/util/format.h57
-rw-r--r--src/util/forward-pointer-iterator.h122
-rw-r--r--src/util/longest-common-suffix.h102
-rw-r--r--src/util/pages-skeleton.h154
-rw-r--r--src/util/paper.cpp145
-rw-r--r--src/util/paper.h65
-rw-r--r--src/util/preview.cpp112
-rw-r--r--src/util/preview.h43
-rw-r--r--src/util/reference.h50
-rw-r--r--src/util/share.cpp45
-rw-r--r--src/util/share.h112
-rw-r--r--src/util/signal-blocker.h71
-rw-r--r--src/util/trim.h60
-rw-r--r--src/util/units.cpp574
-rw-r--r--src/util/units.h231
-rw-r--r--src/util/ziptool.cpp3043
-rw-r--r--src/util/ziptool.h575
24 files changed, 6492 insertions, 0 deletions
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
new file mode 100644
index 0000000..d5f4433
--- /dev/null
+++ b/src/util/CMakeLists.txt
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+set(util_SRC
+ expression-evaluator.cpp
+ share.cpp
+ paper.cpp
+ preview.cpp
+ units.cpp
+ ziptool.cpp
+
+
+ # -------
+ # Headers
+ const_char_ptr.h
+ enums.h
+ expression-evaluator.h
+ fixed_point.h
+ format.h
+ forward-pointer-iterator.h
+ longest-common-suffix.h
+ pages-skeleton.h
+ paper.h
+ preview.h
+ reference.h
+ share.h
+ signal-blocker.h
+ trim.h
+ units.h
+ ziptool.h
+)
+
+add_inkscape_lib(util_LIB "${util_SRC}")
+target_link_libraries(util_LIB PUBLIC 2Geom::2geom)
+# add_inkscape_source("${util_SRC}")
diff --git a/src/util/README b/src/util/README
new file mode 100644
index 0000000..254e3fa
--- /dev/null
+++ b/src/util/README
@@ -0,0 +1,9 @@
+
+
+This directory contains a variety of utility code.
+
+To do:
+
+* Merge with 'helper' into this directory.
+* Move individual files to more appropriate directories.
+* Split into three sub-directories: numeric, color, svg.
diff --git a/src/util/const_char_ptr.h b/src/util/const_char_ptr.h
new file mode 100644
index 0000000..44e6906
--- /dev/null
+++ b/src/util/const_char_ptr.h
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/** @file
+ * Provides `const_char_ptr`
+ */
+/*
+ * Authors:
+ * Sergei Izmailov <sergei.a.izmailov@gmail.com>
+ *
+ * Copyright (C) 2020 Sergei Izmailov
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#ifndef SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+#define SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+#include <glibmm/ustring.h>
+#include <string>
+#include "share.h"
+
+namespace Inkscape {
+namespace Util {
+
+/**
+ * Non-owning reference to 'const char*'
+ * Main-purpose: avoid overloads of type `f(char*, str&)`, `f(str&, char*)`, `f(char*, char*)`, ...
+ */
+class const_char_ptr{
+public:
+ const_char_ptr() noexcept: m_data(nullptr){};
+ const_char_ptr(std::nullptr_t): const_char_ptr() {};
+ const_char_ptr(const char* const data) noexcept: m_data(data) {};
+ const_char_ptr(const Glib::ustring& str) noexcept: const_char_ptr(str.c_str()) {};
+ const_char_ptr(const std::string& str) noexcept: const_char_ptr(str.c_str()) {};
+ const_char_ptr(const ptr_shared& shared) : const_char_ptr(static_cast<const char*>(shared)) {};
+
+ const char * data() const noexcept { return m_data; }
+private:
+ const char * const m_data = nullptr;
+};
+}
+}
+#endif // SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace .0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: \ No newline at end of file
diff --git a/src/util/enums.h b/src/util/enums.h
new file mode 100644
index 0000000..46c4d5e
--- /dev/null
+++ b/src/util/enums.h
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Authors:
+ * Nicholas Bishop <nicholasbishop@gmail.com>
+ * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
+ *
+ * Copyright (C) 2007 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#ifndef INKSCAPE_UTIL_ENUMS_H
+#define INKSCAPE_UTIL_ENUMS_H
+
+#include <glibmm/ustring.h>
+
+namespace Inkscape {
+namespace Util {
+
+/**
+ * Simplified management of enumerations of svg items with UI labels.
+ * IMPORTANT:
+ * When initializing the EnumData struct, you cannot use _(...) to translate strings.
+ * Instead, one must use N_(...) and do the translation every time the string is retrieved.
+ */
+template<typename E>
+struct EnumData
+{
+ E id;
+ const Glib::ustring label;
+ const Glib::ustring key;
+};
+
+const Glib::ustring empty_string("");
+
+/**
+ * Simplified management of enumerations of svg items with UI labels.
+ *
+ * @note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string.
+ * @note that get_label and get_key return an empty string when the requested id is not in the list.
+ */
+template<typename E> class EnumDataConverter
+{
+public:
+ typedef EnumData<E> Data;
+
+ EnumDataConverter(const EnumData<E>* cd, const unsigned int length)
+ : _length(length), _data(cd)
+ {}
+
+ E get_id_from_label(const Glib::ustring& label) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].label == label)
+ return _data[i].id;
+ }
+
+ return (E)0;
+ }
+
+ E get_id_from_key(const Glib::ustring& key) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].key == key)
+ return _data[i].id;
+ }
+
+ return (E)0;
+ }
+
+ bool is_valid_key(const Glib::ustring& key) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].key == key)
+ return true;
+ }
+
+ return false;
+ }
+
+ bool is_valid_id(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return true;
+ }
+ return false;
+ }
+
+ const Glib::ustring& get_label(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return _data[i].label;
+ }
+
+ return empty_string;
+ }
+
+ const Glib::ustring& get_key(const E id) const
+ {
+ for(unsigned int i = 0; i < _length; ++i) {
+ if(_data[i].id == id)
+ return _data[i].key;
+ }
+
+ return empty_string;
+ }
+
+ const EnumData<E>& data(const unsigned int i) const
+ {
+ return _data[i];
+ }
+
+ const unsigned int _length;
+private:
+ const EnumData<E>* _data;
+};
+
+
+}
+}
+
+#endif
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/expression-evaluator.cpp b/src/util/expression-evaluator.cpp
new file mode 100644
index 0000000..24a56b7
--- /dev/null
+++ b/src/util/expression-evaluator.cpp
@@ -0,0 +1,396 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+/** @file
+ * TODO: insert short description here
+ */
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * Original file from libgimpwidgets: gimpeevl.c
+ * Copyright (C) 2008 Fredrik Alstromer <roe@excu.se>
+ * Copyright (C) 2008 Martin Nordholts <martinn@svn.gnome.org>
+ * Modified for Inkscape by Johan Engelen
+ * Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "util/expression-evaluator.h"
+#include "util/units.h"
+
+#include <glib/gconvert.h>
+
+#include <cmath>
+#include <cstring>
+
+using Inkscape::Util::unit_table;
+
+namespace Inkscape {
+namespace Util {
+
+EvaluatorQuantity::EvaluatorQuantity(double value, unsigned int dimension) :
+ value(value),
+ dimension(dimension)
+{
+}
+
+EvaluatorToken::EvaluatorToken()
+{
+ type = 0;
+ value.fl = 0;
+}
+
+ExpressionEvaluator::ExpressionEvaluator(const char *string, Unit const *unit) :
+ string(g_locale_to_utf8(string,-1,nullptr,nullptr,nullptr)),
+ unit(unit)
+{
+ current_token.type = TOKEN_END;
+
+ // Preload symbol
+ parseNextToken();
+}
+
+/**
+ * Evaluates the given arithmetic expression, along with an optional dimension
+ * analysis, and basic unit conversions.
+ *
+ * All units conversions factors are relative to some implicit
+ * base-unit. This is also the unit of the returned value.
+ *
+ * Returns: An EvaluatorQuantity with a value given in the base unit along with
+ * the order of the dimension (e.g. if the base unit is inches, a dimension
+ * order of two means in^2).
+ *
+ * @return Result of evaluation.
+ * @throws Inkscape::Util::EvaluatorException There was a parse error.
+ **/
+EvaluatorQuantity ExpressionEvaluator::evaluate()
+{
+ if (!g_utf8_validate(string, -1, nullptr)) {
+ throw EvaluatorException("Invalid UTF8 string", nullptr);
+ }
+
+ EvaluatorQuantity result = EvaluatorQuantity();
+ EvaluatorQuantity default_unit_factor;
+
+ // Empty expression evaluates to 0
+ if (acceptToken(TOKEN_END, nullptr)) {
+ return result;
+ }
+
+ result = evaluateExpression();
+
+ // There should be nothing left to parse by now
+ isExpected(TOKEN_END, nullptr);
+
+ resolveUnit(nullptr, &default_unit_factor, unit);
+
+ // Entire expression is dimensionless, apply default unit if applicable
+ if ( result.dimension == 0 && default_unit_factor.dimension != 0 ) {
+ result.value /= default_unit_factor.value;
+ result.dimension = default_unit_factor.dimension;
+ }
+ return result;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateExpression()
+{
+ bool subtract;
+ EvaluatorQuantity evaluated_terms;
+
+ evaluated_terms = evaluateTerm();
+
+ // Continue evaluating terms, chained with + or -.
+ for (subtract = FALSE;
+ acceptToken('+', nullptr) || (subtract = acceptToken('-', nullptr));
+ subtract = FALSE)
+ {
+ EvaluatorQuantity new_term = evaluateTerm();
+
+ // If dimensions mismatch, attempt default unit assignment
+ if ( new_term.dimension != evaluated_terms.dimension ) {
+ EvaluatorQuantity default_unit_factor;
+
+ resolveUnit(nullptr, &default_unit_factor, unit);
+
+ if ( new_term.dimension == 0
+ && evaluated_terms.dimension == default_unit_factor.dimension )
+ {
+ new_term.value /= default_unit_factor.value;
+ new_term.dimension = default_unit_factor.dimension;
+ } else if ( evaluated_terms.dimension == 0
+ && new_term.dimension == default_unit_factor.dimension )
+ {
+ evaluated_terms.value /= default_unit_factor.value;
+ evaluated_terms.dimension = default_unit_factor.dimension;
+ } else {
+ throwError("Dimension mismatch during addition");
+ }
+ }
+
+ evaluated_terms.value += (subtract ? -new_term.value : new_term.value);
+ }
+
+ return evaluated_terms;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateTerm()
+{
+ bool division;
+ EvaluatorQuantity evaluated_exp_terms = evaluateExpTerm();
+
+ for ( division = false;
+ acceptToken('*', nullptr) || (division = acceptToken('/', nullptr));
+ division = false )
+ {
+ EvaluatorQuantity new_exp_term = evaluateExpTerm();
+
+ if (division) {
+ evaluated_exp_terms.value /= new_exp_term.value;
+ evaluated_exp_terms.dimension -= new_exp_term.dimension;
+ } else {
+ evaluated_exp_terms.value *= new_exp_term.value;
+ evaluated_exp_terms.dimension += new_exp_term.dimension;
+ }
+ }
+
+ return evaluated_exp_terms;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateExpTerm()
+{
+ EvaluatorQuantity evaluated_signed_factors = evaluateSignedFactor();
+
+ while(acceptToken('^', nullptr)) {
+ EvaluatorQuantity new_signed_factor = evaluateSignedFactor();
+
+ if (new_signed_factor.dimension == 0) {
+ evaluated_signed_factors.value = pow(evaluated_signed_factors.value,
+ new_signed_factor.value);
+ evaluated_signed_factors.dimension *= new_signed_factor.value;
+ } else {
+ throwError("Unit in exponent");
+ }
+ }
+
+ return evaluated_signed_factors;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateSignedFactor()
+{
+ EvaluatorQuantity result;
+ bool negate = FALSE;
+
+ if (!acceptToken('+', nullptr)) {
+ negate = acceptToken ('-', nullptr);
+ }
+
+ result = evaluateFactor();
+
+ if (negate) {
+ result.value = -result.value;
+ }
+
+ return result;
+}
+
+EvaluatorQuantity ExpressionEvaluator::evaluateFactor()
+{
+ EvaluatorQuantity evaluated_factor = EvaluatorQuantity();
+ EvaluatorToken consumed_token = EvaluatorToken();
+
+ if (acceptToken(TOKEN_END, &consumed_token)) {
+ return evaluated_factor;
+ }
+ else if (acceptToken(TOKEN_NUM, &consumed_token)) {
+ evaluated_factor.value = consumed_token.value.fl;
+ } else if (acceptToken('(', nullptr)) {
+ evaluated_factor = evaluateExpression();
+ isExpected(')', nullptr);
+ } else {
+ throwError("Expected number or '('");
+ }
+
+ if ( current_token.type == TOKEN_IDENTIFIER ) {
+ char *identifier;
+ EvaluatorQuantity result;
+
+ acceptToken(TOKEN_ANY, &consumed_token);
+
+ identifier = g_newa(char, consumed_token.value.size + 1);
+
+ strncpy(identifier, consumed_token.value.c, consumed_token.value.size);
+ identifier[consumed_token.value.size] = '\0';
+
+ if (resolveUnit(identifier, &result, unit)) {
+ evaluated_factor.value /= result.value;
+ evaluated_factor.dimension += result.dimension;
+ } else {
+ throwError("Unit was not resolved");
+ }
+ }
+
+ return evaluated_factor;
+}
+
+bool ExpressionEvaluator::acceptToken(TokenType token_type,
+ EvaluatorToken *consumed_token)
+{
+ bool existed = FALSE;
+
+ if ( token_type == current_token.type || token_type == TOKEN_ANY ) {
+ existed = TRUE;
+
+ if (consumed_token) {
+ *consumed_token = current_token;
+ }
+
+ // Parse next token
+ parseNextToken();
+ }
+
+ return existed;
+}
+
+void ExpressionEvaluator::parseNextToken()
+{
+ const char *s;
+
+ movePastWhiteSpace();
+ s = string;
+ start_of_current_token = s;
+
+ if ( !s || s[0] == '\0' ) {
+ // We're all done
+ current_token.type = TOKEN_END;
+ } else if ( s[0] == '+' || s[0] == '-' ) {
+ // Snatch these before the g_strtod() does, otherwise they might
+ // be used in a numeric conversion.
+ acceptTokenCount(1, s[0]);
+ } else {
+ // Attempt to parse a numeric value
+ char *endptr = nullptr;
+ gdouble value = g_strtod(s, &endptr);
+
+ if ( endptr && endptr != s ) {
+ // A numeric could be parsed, use it
+ current_token.value.fl = value;
+
+ current_token.type = TOKEN_NUM;
+ string = endptr;
+ } else if (isUnitIdentifierStart(s[0])) {
+ // Unit identifier
+ current_token.value.c = s;
+ current_token.value.size = getIdentifierSize(s, 0);
+
+ acceptTokenCount(current_token.value.size, TOKEN_IDENTIFIER);
+ } else {
+ // Everything else is a single character token
+ acceptTokenCount(1, s[0]);
+ }
+ }
+}
+
+void ExpressionEvaluator::acceptTokenCount (int count, TokenType token_type)
+{
+ current_token.type = token_type;
+ string += count;
+}
+
+void ExpressionEvaluator::isExpected(TokenType token_type,
+ EvaluatorToken *value)
+{
+ if (!acceptToken(token_type, value)) {
+ throwError("Unexpected token");
+ }
+}
+
+void ExpressionEvaluator::movePastWhiteSpace()
+{
+ if (!string) {
+ return;
+ }
+
+ while (g_ascii_isspace(*string)) {
+ string++;
+ }
+}
+
+bool ExpressionEvaluator::isUnitIdentifierStart(gunichar c)
+{
+ return (g_unichar_isalpha (c)
+ || c == (gunichar) '%'
+ || c == (gunichar) '\'');
+}
+
+/**
+ * getIdentifierSize:
+ * @s:
+ * @start:
+ *
+ * Returns: Size of identifier in bytes (not including NULL
+ * terminator).
+ **/
+int ExpressionEvaluator::getIdentifierSize(const char *string, int start_offset)
+{
+ const char *start = g_utf8_offset_to_pointer(string, start_offset);
+ const char *s = start;
+ gunichar c = g_utf8_get_char(s);
+ int length = 0;
+
+ if (isUnitIdentifierStart(c)) {
+ s = g_utf8_next_char (s);
+ c = g_utf8_get_char (s);
+ length++;
+
+ while ( isUnitIdentifierStart (c) || g_unichar_isdigit (c) ) {
+ s = g_utf8_next_char(s);
+ c = g_utf8_get_char(s);
+ length++;
+ }
+ }
+
+ return g_utf8_offset_to_pointer(start, length) - start;
+}
+
+bool ExpressionEvaluator::resolveUnit (const char* identifier,
+ EvaluatorQuantity *result,
+ Unit const* unit)
+{
+ if (!unit) {
+ result->value = 1;
+ result->dimension = 1;
+ return true;
+ }else if (!identifier) {
+ result->value = 1;
+ result->dimension = unit->isAbsolute() ? 1 : 0;
+ return true;
+ } else if (unit_table.hasUnit(identifier)) {
+ Unit const *identifier_unit = unit_table.getUnit(identifier);
+ result->value = Quantity::convert(1, unit, identifier_unit);
+ result->dimension = identifier_unit->isAbsolute() ? 1 : 0;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void ExpressionEvaluator::throwError(const char *msg)
+{
+ throw EvaluatorException(msg, start_of_current_token);
+}
+
+} // namespace Util
+} // namespace Inkscape
diff --git a/src/util/expression-evaluator.h b/src/util/expression-evaluator.h
new file mode 100644
index 0000000..a45ad5a
--- /dev/null
+++ b/src/util/expression-evaluator.h
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+/** @file
+ * TODO: insert short description here
+ */
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * Original file from libgimpwidgets: gimpeevl.h
+ * Copyright (C) 2008-2009 Fredrik Alstromer <roe@excu.se>
+ * Copyright (C) 2008-2009 Martin Nordholts <martinn@svn.gnome.org>
+ * Modified for Inkscape by Johan Engelen
+ * Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
+#define INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
+
+#include "util/units.h"
+
+#include <exception>
+#include <sstream>
+#include <string>
+
+/**
+ * @file
+ * Expression evaluator: A straightforward recursive
+ * descent parser, no fuss, no new dependencies. The lexer is hand
+ * coded, tedious, not extremely fast but works. It evaluates the
+ * expression as it goes along, and does not create a parse tree or
+ * anything, and will not optimize anything. It uses doubles for
+ * precision, with the given use case, that's enough to combat any
+ * rounding errors (as opposed to optimizing the evaluation).
+ *
+ * It relies on external unit resolving through a callback and does
+ * elementary dimensionality constraint check (e.g. "2 mm + 3 px * 4
+ * in" is an error, as L + L^2 is a mismatch). It uses g_strtod() for numeric
+ * conversions and it's non-destructive in terms of the parameters, and
+ * it's reentrant.
+ *
+ * EBNF:
+ *
+ * expression ::= term { ('+' | '-') term }* |
+ * <empty string> ;
+ *
+ * term ::= exponent { ( '*' | '/' ) exponent }* ;
+ *
+ * exponent ::= signed factor { '^' signed factor }* ;
+ *
+ * signed factor ::= ( '+' | '-' )? factor ;
+ *
+ * unit factor ::= factor unit? ;
+ *
+ * factor ::= number | '(' expression ')' ;
+ *
+ * number ::= ? what g_strtod() consumes ? ;
+ *
+ * unit ::= ? what not g_strtod() consumes and not whitespace ? ;
+ *
+ * The code should match the EBNF rather closely (except for the
+ * non-terminal unit factor, which is inlined into factor) for
+ * maintainability reasons.
+ *
+ * It will allow 1++1 and 1+-1 (resulting in 2 and 0, respectively),
+ * but I figured one might want that, and I don't think it's going to
+ * throw anyone off.
+ */
+
+namespace Inkscape {
+namespace Util {
+
+class Unit;
+
+/**
+ * EvaluatorQuantity:
+ * @param value In reference units.
+ * @param dimension mm has a dimension of 1, mm^2 has a dimension of 2, etc.
+ */
+class EvaluatorQuantity
+{
+public:
+ EvaluatorQuantity(double value = 0, unsigned int dimension = 0);
+
+ double value;
+ unsigned int dimension;
+};
+
+/**
+ * TokenType
+ */
+enum {
+ TOKEN_NUM = 30000,
+ TOKEN_IDENTIFIER = 30001,
+ TOKEN_ANY = 40000,
+ TOKEN_END = 50000
+};
+typedef int TokenType;
+
+/**
+ * EvaluatorToken
+ */
+class EvaluatorToken
+{
+public:
+ EvaluatorToken();
+
+ TokenType type;
+
+ union {
+ double fl;
+ struct {
+ const char *c;
+ int size;
+ };
+ } value;
+};
+
+/**
+ * ExpressionEvaluator
+ * @param string NULL terminated input string to evaluate
+ * @param unit Unit output should be in
+ */
+class ExpressionEvaluator
+{
+public:
+ ExpressionEvaluator(const char *string, Unit const *unit = nullptr);
+
+ EvaluatorQuantity evaluate();
+
+private:
+ const char *string;
+ Unit const *unit;
+
+ EvaluatorToken current_token;
+ const char *start_of_current_token;
+
+ EvaluatorQuantity evaluateExpression();
+ EvaluatorQuantity evaluateTerm();
+ EvaluatorQuantity evaluateExpTerm();
+ EvaluatorQuantity evaluateSignedFactor();
+ EvaluatorQuantity evaluateFactor();
+
+ bool acceptToken(TokenType token_type, EvaluatorToken *consumed_token);
+ void parseNextToken();
+ void acceptTokenCount(int count, TokenType token_type);
+ void isExpected(TokenType token_type, EvaluatorToken *value);
+
+ void movePastWhiteSpace();
+
+ static bool isUnitIdentifierStart(gunichar c);
+ static int getIdentifierSize(const char *s, int start);
+
+ static bool resolveUnit(const char *identifier, EvaluatorQuantity *result, Unit const *unit);
+
+ void throwError(const char *msg);
+};
+
+/**
+ * Special exception class for the expression evaluator.
+ */
+class EvaluatorException : public std::exception {
+public:
+ EvaluatorException(const char *message, const char *at_position) {
+ std::ostringstream os;
+ const char *token = at_position ? at_position : "<End of input>";
+ os << "Expression evaluator error: " << message << " at '" << token << "'";
+ msgstr = os.str();
+ }
+
+ ~EvaluatorException() noexcept override = default; // necessary to destroy the string object!!!
+
+ const char *what() const noexcept override {
+ return msgstr.c_str();
+ }
+protected:
+ std::string msgstr;
+};
+
+}
+}
+
+#endif // INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
diff --git a/src/util/fixed_point.h b/src/util/fixed_point.h
new file mode 100644
index 0000000..44174d0
--- /dev/null
+++ b/src/util/fixed_point.h
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::FixedPoint - fixed point type
+ *
+ * Authors:
+ * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
+ *
+ * Copyright (C) 2006 Jasper van de Gronde
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
+#define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
+
+#include <cmath>
+#include <algorithm>
+#include <limits>
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename T, unsigned int precision>
+class FixedPoint {
+public:
+ FixedPoint() = default;
+ FixedPoint(const FixedPoint& value) : v(value.v) {}
+ FixedPoint(char value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned char value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(short value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned short value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(int value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(unsigned int value) : v(static_cast<T>(value)<<precision) {}
+ FixedPoint(double value) : v(static_cast<T>(floor(value*(1<<precision)))) {}
+
+ FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
+ FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
+ FixedPoint& operator*=(FixedPoint val) {
+ const unsigned int half_size = 8*sizeof(T)/2;
+ const T al = v&((1<<half_size)-1), bl = val.v&((1<<half_size)-1);
+ const T ah = v>>half_size, bh = val.v>>half_size;
+ v = static_cast<unsigned int>(al*bl)>>precision;
+ if ( half_size >= precision ) {
+ v += ((al*bh)+(ah*bl)+((ah*bh)<<half_size))<<(half_size-precision);
+ } else {
+ v += ((al*bh)+(ah*bl))>>(precision-half_size);
+ v += (ah*bh)<<(2*half_size-precision);
+ }
+ return *this;
+ }
+
+ FixedPoint& operator*=(char val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned char val) { v *= val; return *this; }
+ FixedPoint& operator*=(short val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned short val) { v *= val; return *this; }
+ FixedPoint& operator*=(int val) { v *= val; return *this; }
+ FixedPoint& operator*=(unsigned int val) { v *= val; return *this; }
+
+ FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; }
+ FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; }
+ FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; }
+
+ FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; }
+ FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; }
+
+ float operator*(float val) const { return static_cast<float>(*this)*val; }
+ double operator*(double val) const { return static_cast<double>(*this)*val; }
+
+ operator char() const { return v>>precision; }
+ operator unsigned char() const { return v>>precision; }
+ operator short() const { return v>>precision; }
+ operator unsigned short() const { return v>>precision; }
+ operator int() const { return v>>precision; }
+ operator unsigned int() const { return v>>precision; }
+
+ operator float() const { return ldexpf(v,-precision); }
+ operator double() const { return ldexp(v,-precision); }
+private:
+ T v;
+};
+
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
+template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
+
+template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
+template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/format.h b/src/util/format.h
new file mode 100644
index 0000000..b38f3a6
--- /dev/null
+++ b/src/util/format.h
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::format - g_strdup_printf wrapper producing shared strings
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FORMAT_H
+#define SEEN_INKSCAPE_UTIL_FORMAT_H
+
+#include <cstdarg>
+#include <glib.h>
+#include "util/share.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+inline ptr_shared vformat(char const *format, va_list args) {
+ char *temp=g_strdup_vprintf(format, args);
+ ptr_shared result=share_string(temp);
+ g_free(temp);
+ return result;
+}
+
+ // needed since G_GNUC_PRINTF can only be used on a declaration
+ ptr_shared format(char const *format, ...) G_GNUC_PRINTF(1,2);
+inline ptr_shared format(char const *format, ...) {
+ va_list args;
+
+ va_start(args, format);
+ ptr_shared result=vformat(format, args);
+ va_end(args);
+
+ return result;
+}
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/forward-pointer-iterator.h b/src/util/forward-pointer-iterator.h
new file mode 100644
index 0000000..9fe3bb8
--- /dev/null
+++ b/src/util/forward-pointer-iterator.h
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ForwardPointerIterator - wraps a simple pointer
+ * with various strategies
+ * to determine sequence
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
+#define SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
+
+#include <iterator>
+#include <cstddef>
+#include "util/reference.h"
+
+namespace Inkscape {
+
+namespace Util {
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator;
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator<BaseType const, Strategy> {
+public:
+ typedef std::forward_iterator_tag iterator_category;
+ typedef typename Traits::Reference<BaseType const>::LValue value_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef typename Traits::Reference<BaseType const>::LValue reference;
+ typedef typename Traits::Reference<BaseType const>::RValue const_reference;
+ typedef typename Traits::Reference<BaseType const>::Pointer pointer;
+
+ typedef ForwardPointerIterator<BaseType const, Strategy> Self;
+
+ ForwardPointerIterator() = default;
+ ForwardPointerIterator(pointer p) : _p(p) {}
+
+ operator pointer() const { return _p; }
+ reference operator*() const { return *_p; }
+ pointer operator->() const { return _p; }
+
+ bool operator==(Self const &other) const {
+ return _p == other._p;
+ }
+ bool operator!=(Self const &other) const {
+ return _p != other._p;
+ }
+
+ Self &operator++() {
+ _p = Strategy::next(_p);
+ return *this;
+ }
+ Self operator++(int) {
+ Self old(*this);
+ operator++();
+ return old;
+ }
+
+ operator bool() const { return _p != nullptr; }
+
+private:
+ pointer _p;
+};
+
+template <typename BaseType, typename Strategy>
+class ForwardPointerIterator
+: public ForwardPointerIterator<BaseType const, Strategy>
+{
+public:
+ typedef typename Traits::Reference<BaseType>::LValue value_type;
+ typedef typename Traits::Reference<BaseType>::LValue reference;
+ typedef typename Traits::Reference<BaseType>::RValue const_reference;
+ typedef typename Traits::Reference<BaseType>::Pointer pointer;
+
+ typedef ForwardPointerIterator<BaseType const, Strategy> Ancestor;
+ typedef ForwardPointerIterator<BaseType, Strategy> Self;
+
+ ForwardPointerIterator() : Ancestor() {}
+ ForwardPointerIterator(pointer p) : Ancestor(p) {}
+
+ operator pointer() const {
+ return const_cast<pointer>(Ancestor::operator->());
+ }
+ reference operator*() const {
+ return const_cast<reference>(Ancestor::operator*());
+ }
+ pointer operator->() const {
+ return const_cast<pointer>(Ancestor::operator->());
+ }
+
+ Self &operator++() {
+ Ancestor::operator++();
+ return *this;
+ }
+ Self operator++(int) {
+ Self old(*this);
+ operator++();
+ return old;
+ }
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/longest-common-suffix.h b/src/util/longest-common-suffix.h
new file mode 100644
index 0000000..1f7b228
--- /dev/null
+++ b/src/util/longest-common-suffix.h
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Algorithms::nearest_common_ancestor
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H
+#define SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H
+
+#include <iterator>
+#include <functional>
+
+namespace Inkscape {
+
+namespace Algorithms {
+
+/**
+ * Time costs:
+ *
+ * The case of sharing a common successor is handled in O(1) time.
+ *
+ * If \a a is the nearest common ancestor, then runs in O(len(rest of b)) time.
+ *
+ * Otherwise, runs in O(len(a) + len(b)) time.
+ */
+
+template <typename ForwardIterator>
+ForwardIterator nearest_common_ancestor(ForwardIterator a, ForwardIterator b, ForwardIterator end)
+{
+ if ( a == end || b == end ) {
+ return end;
+ }
+
+ /* Handle in O(1) time the common cases of identical lists or tails. */
+ {
+ /* identical lists? */
+ if ( a == b ) {
+ return a;
+ }
+
+ /* identical tails? */
+ ForwardIterator tail_a(a);
+ ForwardIterator tail_b(b);
+ if ( ++tail_a == ++tail_b ) {
+ return tail_a;
+ }
+ }
+
+ /* Build parallel lists of suffixes, ordered by increasing length. */
+
+ ForwardIterator lists[2] = { a, b };
+ std::vector<ForwardIterator> suffixes[2];
+
+ for ( int i=0 ; i < 2 ; i++ ) {
+ for ( ForwardIterator iter(lists[i]) ; iter != end ; ++iter ) {
+ if ( iter == lists[1-i] ) {
+ // the other list is a suffix of this one
+ return lists[1-i];
+ }
+ suffixes[i].push_back(iter);
+ }
+ }
+
+ /* Iterate in parallel through the lists of suffix lists from shortest to
+ * longest, stopping before the first pair of suffixes that differs
+ */
+
+ ForwardIterator longest_common(end);
+
+ while ( !suffixes[0].empty() && !suffixes[1].empty() &&
+ suffixes[0].back() == suffixes[1].back() )
+ {
+ longest_common = suffixes[0].back();
+ suffixes[0].pop_back();
+ suffixes[1].pop_back();
+ }
+
+ return longest_common;
+}
+
+}
+
+}
+
+#endif /* !SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H */
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
diff --git a/src/util/pages-skeleton.h b/src/util/pages-skeleton.h
new file mode 100644
index 0000000..ac984e4
--- /dev/null
+++ b/src/util/pages-skeleton.h
@@ -0,0 +1,154 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/**
+ * @file
+ * List of paper sizes
+ */
+/*
+ * Authors:
+ * bulia byak <buliabyak@users.sf.net>
+ * Lauris Kaplinski <lauris@kaplinski.com>
+ * Jon Phillips <jon@rejon.org>
+ * Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
+ * Bob Jamison <ishmal@users.sf.net>
+ * Abhishek Sharma
+ * + see git history
+ *
+ * Copyright (C) 2000 - 2018 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_PAGES_SKELETON_H
+#define SEEN_PAGES_SKELETON_H
+
+
+ /** \note
+ * The ISO page sizes in the table below differ from ghostscript's idea of page sizes (by
+ * less than 1pt). Being off by <1pt should be OK for most purposes, but may cause fuzziness
+ * (antialiasing) problems when printing to 72dpi or 144dpi printers or bitmap files due to
+ * postscript's different coordinate system (y=0 meaning bottom of page in postscript and top
+ * of page in SVG). I haven't looked into whether this does in fact cause fuzziness, I merely
+ * note the possibility. Rounding done by extension/internal/ps.cpp (e.g. floor/ceil calls)
+ * will also affect whether fuzziness occurs.
+ *
+ * The remainder of this comment discusses the origin of the numbers used for ISO page sizes in
+ * this table and in ghostscript.
+ *
+ * The versions here, in mm, are the official sizes according to
+ * <a href="http://en.wikipedia.org/wiki/Paper_sizes">http://en.wikipedia.org/wiki/Paper_sizes</a>
+ * at 2005-01-25. (The ISO entries in the below table
+ * were produced mechanically from the table on that page.)
+ *
+ * (The rule seems to be that A0, B0, ..., D0. sizes are rounded to the nearest number of mm
+ * from the "theoretical size" (i.e. 1000 * sqrt(2) or pow(2.0, .25) or the like), whereas
+ * going from e.g. A0 to A1 always take the floor of halving -- which by chance coincides
+ * exactly with flooring the "theoretical size" for n != 0 instead of the rounding to nearest
+ * done for n==0.)
+ *
+ * Ghostscript paper sizes are given in gs_statd.ps according to gs(1). gs_statd.ps always
+ * uses an integer number ofpt: sometimes gs_statd.ps rounds to nearest (e.g. a1), sometimes
+ * floors (e.g. a10), sometimes ceils (e.g. a8).
+ *
+ * I'm not sure how ghostscript's gs_statd.ps was calculated: it isn't just rounding the
+ * "theoretical size" of each page topt (see a0), nor is it rounding the a0 size times an
+ * appropriate power of two (see a1). Possibly it was prepared manually, with a human applying
+ * inconsistent rounding rules when converting from mm to pt.
+ */
+ /** \todo
+ * Should we include the JIS B series (used in Japan)
+ * (JIS B0 is sometimes called JB0, and similarly for JB1 etc)?
+ * Should we exclude B7--B10 and A7--10 to make the list smaller ?
+ * Should we include any of the ISO C, D and E series (see below) ?
+ */
+
+
+
+ /* See http://www.hbp.com/content/PCR_envelopes.cfm for a much larger list of US envelope
+ sizes. */
+ /* Note that `Folio' (used in QPrinter/KPrinter) is deliberately absent from this list, as it
+ means different sizes to different people: different people may expect the width to be
+ either 8, 8.25 or 8.5 inches, and the height to be either 13 or 13.5 inches, even
+ restricting our interpretation to foolscap folio. If you wish to introduce a folio-like
+ page size to the list, then please consider using a name more specific than just `Folio' or
+ `Foolscap Folio'. */
+
+static char const pages_skeleton[] = R"(#Inkscape page sizes
+#NAME, WIDTH, HEIGHT, UNIT
+A4, 210, 297, mm
+US Letter, 8.5, 11, in
+US Legal, 8.5, 14, in
+US Executive, 7.25, 10.5, in
+US Ledger/Tabloid, 11, 17, in
+A0, 841, 1189, mm
+A1, 594, 841, mm
+A2, 420, 594, mm
+A3, 297, 420, mm
+A5, 148, 210, mm
+A6, 105, 148, mm
+A7, 74, 105, mm
+A8, 52, 74, mm
+A9, 37, 52, mm
+A10, 26, 37, mm
+B0, 1000, 1414, mm
+B1, 707, 1000, mm
+B2, 500, 707, mm
+B3, 353, 500, mm
+B4, 250, 353, mm
+B5, 176, 250, mm
+B6, 125, 176, mm
+B7, 88, 125, mm
+B8, 62, 88, mm
+B9, 44, 62, mm
+B10, 31, 44, mm
+C0, 917, 1297, mm
+C1, 648, 917, mm
+C2, 458, 648, mm
+C3, 324, 458, mm
+C4, 229, 324, mm
+C5, 162, 229, mm
+C6, 114, 162, mm
+C7, 81, 114, mm
+C8, 57, 81, mm
+C9, 40, 57, mm
+C10, 28, 40, mm
+D1, 545, 771, mm
+D2, 385, 545, mm
+D3, 272, 385, mm
+D4, 192, 272, mm
+D5, 136, 192, mm
+D6, 96, 136, mm
+D7, 68, 96, mm
+E3, 400, 560, mm
+E4, 280, 400, mm
+E5, 200, 280, mm
+E6, 140, 200, mm
+CSE, 462, 649, pt
+US #10 Envelope, 9.5, 4.125, in
+DL Envelope, 220, 110, mm
+Banner 468x60, 468, 60, px
+Icon 16x16, 16, 16, px
+Icon 32x32, 32, 32, px
+Icon 48x48, 48, 48, px
+ID Card (ISO 7810), 85.60, 53.98, mm
+Business Card (US), 3.5, 2, in
+Business Card (Europe), 85, 55, mm
+Business Card (AU/NZ), 90, 55, mm
+Arch A, 9, 12, in
+Arch B, 12, 18, in
+Arch C, 18, 24, in
+Arch D, 24, 36, in
+Arch E, 36, 48, in
+Arch E1, 30, 42, in
+Video SD / PAL, 768, 576, px
+Video SD-Widescreen / PAL, 1024, 576, px
+Video SD / NTSC, 544, 480, px
+Video SD-Widescreen / NTSC, 872, 486, px
+Video HD 720p, 1280, 720, px
+Video HD 1080p, 1920, 1080, px
+Video DCI 2k (Full Frame), 2048, 1080, px
+Video UHD 4k, 3840, 2160, px
+Video DCI 4k (Full Frame), 4096, 2160, px
+Video UHD 8k, 7680, 4320, px
+)";
+
+#endif
diff --git a/src/util/paper.cpp b/src/util/paper.cpp
new file mode 100644
index 0000000..3a3efe2
--- /dev/null
+++ b/src/util/paper.cpp
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape PaperSize
+ *
+ * Authors:
+ * Bob Jamison (2006)
+ * Martin Owens (2021)
+ *
+ * Copyright (C) 2021 AUTHORS
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include <glibmm/i18n.h>
+
+#include "paper.h"
+#include "pages-skeleton.h"
+
+#include "io/resource.h"
+
+namespace Inkscape {
+
+/**
+ * Returns a list of page sizes.
+ */
+const std::vector<PaperSize>& PaperSize::getPageSizes()
+{
+ // Static makes us only load pages once.
+ static std::vector<PaperSize> ret;
+ if (!ret.empty())
+ return ret;
+
+ char *path = Inkscape::IO::Resource::profile_path("pages.csv");
+ if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
+ if (!g_file_set_contents(path, pages_skeleton, -1, nullptr)) {
+ g_warning("%s", _("Failed to create the page file."));
+ }
+ }
+ gchar *content = nullptr;
+ if (g_file_get_contents(path, &content, nullptr, nullptr)) {
+ gchar **lines = g_strsplit_set(content, "\n", 0);
+
+ for (int i = 0; lines && lines[i]; ++i) {
+ gchar **line = g_strsplit_set(lines[i], ",", 5);
+ if (!line[0] || !line[1] || !line[2] || !line[3] || line[0][0]=='#')
+ continue;
+
+ //name, width, height, unit
+ double width = g_ascii_strtod(line[1], nullptr);
+ double height = g_ascii_strtod(line[2], nullptr);
+ g_strstrip(line[0]);
+ g_strstrip(line[3]);
+ Glib::ustring name = line[0];
+ ret.push_back(PaperSize(name, width, height, Inkscape::Util::unit_table.getUnit(line[3])));
+ }
+ g_strfreev(lines);
+ g_free(content);
+ }
+ g_free(path);
+ return ret;
+}
+
+
+PaperSize::PaperSize()
+ : name("")
+ , width(0.0)
+ , height(0.0)
+{
+ unit = Inkscape::Util::unit_table.getUnit("px");
+}
+
+
+PaperSize::PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit)
+ : name(std::move(name))
+ , width(width)
+ , height(height)
+ , unit(unit)
+{}
+
+std::string PaperSize::getDescription(bool landscape) const {
+ return toDescription(name, size[landscape], size[!landscape], unit);
+}
+
+std::string PaperSize::toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit)
+{
+ return name + " (" + formatNumber(x) + " x " + formatNumber(y) + " " + unit->abbr + ")";
+}
+
+std::string PaperSize::formatNumber(double val)
+{
+ char buf[20];
+ snprintf(buf, 19, "%0.1f", val);
+ auto ret = std::string(buf);
+ // C++ doesn't provide a good number formatting control, so hack off trailing zeros.
+ if ((ret.length() > 2) && (ret.back() == '0')) {
+ ret = ret.substr(0, ret.length() - 2);
+ }
+ return ret;
+}
+
+void PaperSize::assign(const PaperSize &other)
+{
+ name = other.name;
+ width = other.width;
+ height = other.height;
+ auto [smaller, larger] = std::minmax(width, height);
+ size = Geom::Point(smaller, larger);
+ unit = other.unit;
+}
+
+/**
+ * Returns a matching paper size, if possible.
+ */
+const PaperSize *PaperSize::findPaperSize(double width, double height, Inkscape::Util::Unit const *unit)
+{
+ auto [smaller, larger] = std::minmax(width, height);
+ auto size = Geom::Point(smaller, larger);
+ auto px = Inkscape::Util::unit_table.getUnit("px");
+
+ for (auto&& page_size : Inkscape::PaperSize::getPageSizes()) {
+ auto cmp = Geom::Point(
+ unit->convert(size[0], page_size.unit),
+ unit->convert(size[1], page_size.unit)
+ );
+ // We want a half a pixel tollerance to catch floating point errors
+ auto tollerance = px->convert(0.5, page_size.unit);
+ if (Geom::are_near(page_size.size, cmp, tollerance)) {
+ return &page_size;
+ }
+ }
+ return nullptr;
+}
+
+} // namespace Inkscape
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/paper.h b/src/util/paper.h
new file mode 100644
index 0000000..225e806
--- /dev/null
+++ b/src/util/paper.h
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape Paper Sizes
+ *
+ * Authors:
+ *
+ * Copyright (C) 2013 AUTHORS
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include <string>
+#include <vector>
+#include <2geom/point.h>
+
+#include "units.h"
+
+#ifndef INKSCAPE_UTIL_PAPER_H
+#define INKSCAPE_UTIL_PAPER_H
+
+namespace Inkscape {
+
+/**
+ * Data class used to store common paper dimensions from pages.csv
+ */
+class PaperSize
+{
+public:
+ PaperSize();
+ PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit);
+ PaperSize(const PaperSize &other) { assign(other); }
+ PaperSize &operator=(const PaperSize &other) { assign(other); return *this; }
+
+ ~PaperSize() = default;
+
+ std::string name;
+ Geom::Point size;
+ double width;
+ double height;
+ Inkscape::Util::Unit const *unit; /// pointer to object in UnitTable, do not delete
+
+ std::string getDescription(bool landscape) const;
+
+ static const std::vector<PaperSize>& getPageSizes();
+ static const PaperSize *findPaperSize(double width, double height, Inkscape::Util::Unit const *unit);
+
+ static std::string toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit);
+private:
+ void assign(const PaperSize &other);
+ static std::string formatNumber(double val);
+};
+
+} // namespace Inkscape
+
+#endif // define INKSCAPE_UTIL_UNITS_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/preview.cpp b/src/util/preview.cpp
new file mode 100644
index 0000000..4dd3283
--- /dev/null
+++ b/src/util/preview.cpp
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/**
+ * @file
+ * Utility functions for generating export previews.
+ */
+/* Authors:
+ * Anshudhar Kumar Singh <anshudhar2001@gmail.com>
+ * Martin Owens <doctormo@gmail.com>
+ *
+ * Copyright (C) 2021 Anshudhar Kumar Singh
+ * 2021 Martin Owens
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include "preview.h"
+
+#include "desktop.h"
+#include "document.h"
+#include "display/cairo-utils.h"
+#include "display/drawing-context.h"
+#include "object/sp-namedview.h"
+#include "object/sp-root.h"
+#include "page-manager.h"
+
+namespace Inkscape {
+namespace UI {
+namespace PREVIEW {
+
+GdkPixbuf *render_preview(SPDocument *doc, Inkscape::Drawing &drawing, SPItem *item,
+ unsigned width_in, unsigned height_in, Geom::OptRect *dboxIn)
+{
+ if (auto name = (item ? item->getId() : nullptr)) {
+ // Get item even if it's in another document.
+ if (item->document != doc) {
+ item = dynamic_cast<SPItem *>(doc->getObjectById(name));
+ }
+ }
+
+ Geom::OptRect dbox;
+ if (dboxIn) {
+ dbox = *dboxIn;
+ } else if (item) {
+ if (item->parent) {
+ dbox = item->documentVisualBounds();
+ } else {
+ dbox = doc->preferredBounds();
+ }
+ } else if (doc->getRoot()) {
+ // If we still dont have a dbox we will use document coordinates.
+ dbox = doc->getRoot()->documentVisualBounds();
+ }
+
+ // If we still dont have anything to render then return
+ if (!dbox) return nullptr;
+
+ // Calculate a scaling factor for the requested bounding box.
+ double sf = 1.0;
+ Geom::IntRect ibox = dbox->roundOutwards();
+ if (ibox.width() != width_in || ibox.height() != height_in) {
+ sf = std::min((double)width_in / dbox->width(),
+ (double)height_in / dbox->height());
+ auto scaled_box = *dbox * Geom::Scale(sf);
+ ibox = scaled_box.roundOutwards();
+ }
+
+ // Resize the contents to the available space with a scale factor
+ drawing.root()->setTransform(Geom::Scale(sf));
+ drawing.update();
+
+ Geom::IntPoint pdim(width_in, height_in);
+ // The unsigned width/height can wrap around when negative.
+ int dx = ((int)width_in - ibox.width()) / 2;
+ int dy = ((int)height_in - ibox.height()) / 2;
+ Geom::IntRect area = Geom::IntRect::from_xywh(ibox.min() - Geom::IntPoint(dx, dy), pdim);
+
+ /* Actual renderable area */
+ Geom::IntRect ua = *Geom::intersect(ibox, area);
+
+ /* Render */
+ cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, ua.width(), ua.height());
+ Inkscape::DrawingContext dc(s, ua.min());
+ cairo_t *cr = cairo_create(s);
+ cairo_rectangle(cr, 0, 0, ua.width(), ua.height());
+
+ guint32 bg = doc->getPageManager().background_color;
+
+ // We always use checkerboard to indicate transparency.
+ if (SP_RGBA32_A_F(bg) < 1.0) {
+ auto pattern = ink_cairo_pattern_create_checkerboard(bg, false);
+ auto background = Cairo::RefPtr<Cairo::Pattern>(new Cairo::Pattern(pattern));
+ cairo_set_source(cr, background->cobj());
+ cairo_fill(cr);
+ }
+ // We always draw the background on top to indicate partial backgrounds
+ auto background = Cairo::SolidPattern::create_rgba(
+ SP_RGBA32_R_F(bg), SP_RGBA32_G_F(bg),
+ SP_RGBA32_B_F(bg), SP_RGBA32_A_F(bg));
+ cairo_set_source(cr, background->cobj());
+ cairo_fill(cr);
+
+ cairo_save(cr);
+ cairo_destroy(cr);
+
+ drawing.render(dc, ua);
+ cairo_surface_flush(s);
+ return ink_pixbuf_create_from_cairo_surface(s);
+}
+
+} // namespace PREVIEW
+} // namespace UI
+} // namespace Inkscape
diff --git a/src/util/preview.h b/src/util/preview.h
new file mode 100644
index 0000000..36de1fc
--- /dev/null
+++ b/src/util/preview.h
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/**
+ * @file
+ * Utility functions for previewing icon representation.
+ */
+/* Authors:
+ * Jon A. Cruz
+ * Bob Jamison
+ * Other dudes from The Inkscape Organization
+ * Abhishek Sharma
+ * Anshudhar Kumar Singh <anshudhar2001@gmail.com>
+ *
+ * Copyright (C) 2004 Bob Jamison
+ * Copyright (C) 2005,2010 Jon A. Cruz
+ * Copyright (C) 2021 Anshudhar Kumar Singh
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SP_PREVIEW_UTIL_H
+#define SP_PREVIEW_UTIL_H
+
+#include <glibmm/main.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "display/drawing.h"
+
+class SPDocument;
+class SPItem;
+
+namespace Inkscape {
+namespace UI {
+namespace PREVIEW {
+
+// takes doc, drawing, icon, and icon name to produce pixels
+GdkPixbuf *render_preview(SPDocument *doc, Inkscape::Drawing &drawing, SPItem *item,
+ unsigned width_in, unsigned height_in, Geom::OptRect *dboxIn = nullptr);
+
+} // namespace PREVIEW
+} // namespace UI
+} // namespace Inkscape
+
+#endif
diff --git a/src/util/reference.h b/src/util/reference.h
new file mode 100644
index 0000000..b4a123b
--- /dev/null
+++ b/src/util/reference.h
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Traits::Reference - traits class for dealing with reference types
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2004 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_TRAITS_REFERENCE_H
+#define SEEN_INKSCAPE_TRAITS_REFERENCE_H
+
+namespace Inkscape {
+
+namespace Traits {
+
+template <typename T>
+struct Reference {
+ typedef T const &RValue;
+ typedef T &LValue;
+ typedef T *Pointer;
+ typedef T const *ConstPointer;
+};
+
+template <typename T>
+struct Reference<T &> {
+ typedef T &RValue;
+ typedef T &LValue;
+ typedef T *Pointer;
+ typedef T const *ConstPointer;
+};
+
+}
+
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/share.cpp b/src/util/share.cpp
new file mode 100644
index 0000000..6c436dc
--- /dev/null
+++ b/src/util/share.cpp
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ptr_shared<T> - like T const *, but stronger.
+ * Used to hold c-style strings for objects that are managed by the gc.
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include "util/share.h"
+#include <glib.h>
+
+namespace Inkscape {
+namespace Util {
+
+ptr_shared share_string(char const *string) {
+ g_return_val_if_fail(string != nullptr, share_unsafe(nullptr));
+ return share_string(string, std::strlen(string));
+}
+
+ptr_shared share_string(char const *string, std::size_t length) {
+ g_return_val_if_fail(string != nullptr, share_unsafe(nullptr));
+ char *new_string=new (GC::ATOMIC) char[length+1];
+ std::memcpy(new_string, string, length);
+ new_string[length] = 0;
+ return share_unsafe(new_string);
+}
+
+}
+}
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/share.h b/src/util/share.h
new file mode 100644
index 0000000..e8b9fb1
--- /dev/null
+++ b/src/util/share.h
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape::Util::ptr_shared<T> - like T const *, but stronger.
+ * Used to hold c-style strings for objects that are managed by the gc.
+ *
+ * Authors:
+ * MenTaLguY <mental@rydia.net>
+ *
+ * Copyright (C) 2006 MenTaLguY
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_SHARE_H
+#define SEEN_INKSCAPE_UTIL_SHARE_H
+
+#include "inkgc/gc-core.h"
+#include <cstring>
+#include <cstddef>
+
+namespace Inkscape {
+namespace Util {
+
+class ptr_shared {
+public:
+
+ ptr_shared() : _string(nullptr) {}
+ ptr_shared(ptr_shared const &other) = default;
+
+ operator char const *() const { return _string; }
+ operator bool() const { return _string; }
+
+ char const *pointer() const { return _string; }
+ char const &operator[](int i) const { return _string[i]; }
+
+ ptr_shared operator+(int i) const {
+ return share_unsafe(_string+i);
+ }
+ ptr_shared operator-(int i) const {
+ return share_unsafe(_string-i);
+ }
+ //WARNING: No bounds checking in += and -= functions. Moving the pointer
+ //past the end of the string and then back could probably cause the garbage
+ //collector to deallocate the string inbetween, as there's temporary no
+ //valid reference pointing into the allocated space.
+ ptr_shared &operator+=(int i) {
+ _string += i;
+ return *this;
+ }
+ ptr_shared &operator-=(int i) {
+ _string -= i;
+ return *this;
+ }
+ std::ptrdiff_t operator-(ptr_shared const &other) {
+ return _string - other._string;
+ }
+
+ ptr_shared &operator=(ptr_shared const &other) = default;
+
+ bool operator==(ptr_shared const &other) const {
+ return _string == other._string;
+ }
+ bool operator!=(ptr_shared const &other) const {
+ return _string != other._string;
+ }
+ bool operator>(ptr_shared const &other) const {
+ return _string > other._string;
+ }
+ bool operator<(ptr_shared const &other) const {
+ return _string < other._string;
+ }
+
+ friend ptr_shared share_unsafe(char const *string);
+
+private:
+ ptr_shared(char const *string) : _string(string) {}
+ static ptr_shared share_unsafe(char const *string) {
+ return ptr_shared(string);
+ }
+
+ //This class (and code using it) assumes that it never has to free this
+ //pointer, and that the memory it points to will not be freed as long as a
+ //ptr_shared pointing to it exists.
+ char const *_string;
+};
+
+ptr_shared share_string(char const *string);
+ptr_shared share_string(char const *string, std::size_t length);
+
+inline ptr_shared share_unsafe(char const *string) {
+ return ptr_shared::share_unsafe(string);
+}
+
+//TODO: Do we need this function?
+inline ptr_shared share_static_string(char const *string) {
+ return share_unsafe(string);
+}
+
+}
+}
+
+#endif
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/signal-blocker.h b/src/util/signal-blocker.h
new file mode 100644
index 0000000..8fb6256
--- /dev/null
+++ b/src/util/signal-blocker.h
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Base RAII blocker for sgic++ signals.
+ *
+ * Authors:
+ * Jon A. Cruz <jon@joncruz.org>
+ *
+ * Copyright (C) 2014 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+#define SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+
+#include <string>
+#include <sigc++/connection.h>
+
+/**
+ * Base RAII blocker for sgic++ signals.
+ */
+class SignalBlocker
+{
+public:
+ /**
+ * Creates a new instance that if the signal is currently unblocked will block
+ * it until this instance is destructed and then will unblock it.
+ */
+ SignalBlocker( sigc::connection *connection ) :
+ _connection(connection),
+ _wasBlocked(_connection->blocked())
+ {
+ if (!_wasBlocked)
+ {
+ _connection->block();
+ }
+ }
+
+ /**
+ * Destructor that will unblock the signal if it was blocked initially by this
+ * instance.
+ */
+ ~SignalBlocker()
+ {
+ if (!_wasBlocked)
+ {
+ _connection->block(false);
+ }
+ }
+
+private:
+ // noncopyable, nonassignable
+ SignalBlocker(SignalBlocker const &other) = delete;
+ SignalBlocker& operator=(SignalBlocker const &other) = delete;
+
+ sigc::connection *_connection;
+ bool _wasBlocked;
+};
+
+#endif // SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/trim.h b/src/util/trim.h
new file mode 100644
index 0000000..c8c9fd5
--- /dev/null
+++ b/src/util/trim.h
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/**
+ * @file
+ * Inkscape::Util::trim - trim whitespace and other characters
+ *
+ * Authors:
+ * Rafael Siejakowski <rs@rs-math.net>
+ *
+ * Copyright (C) 2022 the Authors.
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef SEEN_TRIM_H
+#define SEEN_TRIM_H
+
+#include <glibmm/ustring.h>
+#include <glibmm/regex.h>
+
+namespace Inkscape {
+namespace Util {
+
+/**
+ * @brief
+ * Modifies a string in place, removing leading and trailing whitespace characters.
+ * Optionally, it can remove other characters or ranges in addition to whitespace.
+ *
+ * @param input - a reference to a Glib::ustring which will be modified in place.
+ * @param also_remove - optional range of characters to remove in addition to whitespace.
+ * NOTE: these characters are inserted into a regex range (square brackets) and
+ * therefore may need to be regex-escaped. It is the responsibility of
+ * the user to pass a string that will work correctly in a regex range.
+ */
+inline void trim(Glib::ustring &input, Glib::ustring const &also_remove = "")
+{
+ auto const regex = Glib::Regex::create(Glib::ustring("^[\\s") + also_remove + "]*(.+?)[\\s"
+ + also_remove + "]*$");
+ Glib::MatchInfo match_info;
+ regex->match(input, match_info);
+ if (!match_info.matches()) {
+ input.clear();
+ return;
+ }
+ input = match_info.fetch(1);
+}
+
+} // namespace Util
+} // namespace Inkscape
+
+#endif // SEEN_TRIM_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/units.cpp b/src/util/units.cpp
new file mode 100644
index 0000000..478e9dc
--- /dev/null
+++ b/src/util/units.cpp
@@ -0,0 +1,574 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape Units
+ *
+ * Authors:
+ * Matthew Petroff <matthew@mpetroff.net>
+ *
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include <cmath>
+#include <cerrno>
+#include <iomanip>
+#include <iostream>
+#include <utility>
+#include <unordered_map>
+#include <glib.h>
+#include <glibmm/regex.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/markup.h>
+
+#include <2geom/coord.h>
+
+#include "io/resource.h"
+#include "util/units.h"
+#include "path-prefix.h"
+#include "streq.h"
+
+using Inkscape::Util::UNIT_TYPE_DIMENSIONLESS;
+using Inkscape::Util::UNIT_TYPE_LINEAR;
+using Inkscape::Util::UNIT_TYPE_RADIAL;
+using Inkscape::Util::UNIT_TYPE_FONT_HEIGHT;
+
+namespace
+{
+
+#define MAKE_UNIT_CODE(a, b) \
+ ((((unsigned)(a) & 0xdf) << 8) | ((unsigned)(b) & 0xdf))
+
+enum UnitCode {
+ UNIT_CODE_PX = MAKE_UNIT_CODE('p','x'),
+ UNIT_CODE_PT = MAKE_UNIT_CODE('p','t'),
+ UNIT_CODE_PC = MAKE_UNIT_CODE('p','c'),
+ UNIT_CODE_MM = MAKE_UNIT_CODE('m','m'),
+ UNIT_CODE_CM = MAKE_UNIT_CODE('c','m'),
+ UNIT_CODE_IN = MAKE_UNIT_CODE('i','n'),
+ UNIT_CODE_EM = MAKE_UNIT_CODE('e','m'),
+ UNIT_CODE_EX = MAKE_UNIT_CODE('e','x'),
+ UNIT_CODE_PERCENT = MAKE_UNIT_CODE('%',0)
+};
+
+// TODO: convert to constexpr in C++11, so that the above constants can be eliminated
+inline unsigned make_unit_code(char const *str) {
+ if (!str || str[0] == 0) return 0;
+ return MAKE_UNIT_CODE(str[0], str[1]);
+}
+
+
+// This must match SVGLength::Unit
+unsigned const svg_length_lookup[] = {
+ 0,
+ UNIT_CODE_PX,
+ UNIT_CODE_PT,
+ UNIT_CODE_PC,
+ UNIT_CODE_MM,
+ UNIT_CODE_CM,
+ UNIT_CODE_IN,
+ UNIT_CODE_EM,
+ UNIT_CODE_EX,
+ UNIT_CODE_PERCENT
+};
+
+
+
+// maps unit codes obtained from their abbreviations to their SVGLength unit indexes
+typedef std::unordered_map<unsigned, SVGLength::Unit> UnitCodeLookup;
+
+UnitCodeLookup make_unit_code_lookup()
+{
+ UnitCodeLookup umap;
+ for (unsigned i = 1; i < G_N_ELEMENTS(svg_length_lookup); ++i) {
+ umap[svg_length_lookup[i]] = static_cast<SVGLength::Unit>(i);
+ }
+ return umap;
+}
+
+UnitCodeLookup const unit_code_lookup = make_unit_code_lookup();
+
+
+
+typedef std::unordered_map<Glib::ustring, Inkscape::Util::UnitType> TypeMap;
+
+/** A std::map that gives the data type value for the string version.
+ * @todo consider hiding map behind hasFoo() and getFoo() type functions. */
+TypeMap make_type_map()
+{
+ TypeMap tmap;
+ tmap["DIMENSIONLESS"] = UNIT_TYPE_DIMENSIONLESS;
+ tmap["LINEAR"] = UNIT_TYPE_LINEAR;
+ tmap["RADIAL"] = UNIT_TYPE_RADIAL;
+ tmap["FONT_HEIGHT"] = UNIT_TYPE_FONT_HEIGHT;
+ // Note that code was not yet handling LINEAR_SCALED, TIME, QTY and NONE
+
+ return tmap;
+}
+
+TypeMap const type_map = make_type_map();
+
+} // namespace
+
+namespace Inkscape {
+namespace Util {
+
+class UnitParser : public Glib::Markup::Parser
+{
+public:
+ typedef Glib::Markup::Parser::AttributeMap AttrMap;
+ typedef Glib::Markup::ParseContext Ctx;
+
+ UnitParser(UnitTable *table);
+ ~UnitParser() override = default;
+
+protected:
+ void on_start_element(Ctx &ctx, Glib::ustring const &name, AttrMap const &attrs) override;
+ void on_end_element(Ctx &ctx, Glib::ustring const &name) override;
+ void on_text(Ctx &ctx, Glib::ustring const &text) override;
+
+public:
+ UnitTable *tbl;
+ bool primary;
+ bool skip;
+ Unit unit;
+};
+
+UnitParser::UnitParser(UnitTable *table) :
+ tbl(table),
+ primary(false),
+ skip(false)
+{
+}
+
+#define BUFSIZE (255)
+
+Unit::Unit() :
+ type(UNIT_TYPE_DIMENSIONLESS), // should this or NONE be the default?
+ factor(1.0),
+ name(),
+ name_plural(),
+ abbr(),
+ description()
+{
+}
+
+Unit::Unit(UnitType type,
+ double factor,
+ Glib::ustring name,
+ Glib::ustring name_plural,
+ Glib::ustring abbr,
+ Glib::ustring description)
+ : type(type)
+ , factor(factor)
+ , name(std::move(name))
+ , name_plural(std::move(name_plural))
+ , abbr(std::move(abbr))
+ , description(std::move(description))
+{
+ g_return_if_fail(factor <= 0);
+}
+
+void Unit::clear()
+{
+ *this = Unit();
+}
+
+int Unit::defaultDigits() const
+{
+ int factor_digits = int(log10(factor));
+ if (factor_digits < 0) {
+ g_warning("factor = %f, factor_digits = %d", factor, factor_digits);
+ g_warning("factor_digits < 0 - returning 0");
+ factor_digits = 0;
+ }
+ return factor_digits;
+}
+
+bool Unit::compatibleWith(Unit const *u) const
+{
+ // Percentages
+ if (type == UNIT_TYPE_DIMENSIONLESS || u->type == UNIT_TYPE_DIMENSIONLESS) {
+ return true;
+ }
+
+ // Other units with same type
+ if (type == u->type) {
+ return true;
+ }
+
+ // Different, incompatible types
+ return false;
+}
+bool Unit::compatibleWith(Glib::ustring const &u) const
+{
+ return compatibleWith(unit_table.getUnit(u));
+}
+
+bool Unit::operator==(Unit const &other) const
+{
+ return (type == other.type && name.compare(other.name) == 0);
+}
+
+int Unit::svgUnit() const
+{
+ char const *astr = abbr.c_str();
+ unsigned code = make_unit_code(astr);
+
+ UnitCodeLookup::const_iterator u = unit_code_lookup.find(code);
+ if (u != unit_code_lookup.end()) {
+ return u->second;
+ }
+ return 0;
+}
+
+double Unit::convert(double from_dist, Unit const *to) const
+{
+ // Percentage
+ if (to->type == UNIT_TYPE_DIMENSIONLESS) {
+ return from_dist * to->factor;
+ }
+
+ // Incompatible units
+ if (type != to->type) {
+ return -1;
+ }
+
+ // Compatible units
+ return from_dist * factor / to->factor;
+}
+double Unit::convert(double from_dist, Glib::ustring const &to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
+double Unit::convert(double from_dist, char const *to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
+
+
+
+Unit UnitTable::_empty_unit;
+
+UnitTable::UnitTable()
+{
+ using namespace Inkscape::IO::Resource;
+ auto filename = get_path_string(SYSTEM, UIS, "units.xml");
+ load(filename);
+}
+
+UnitTable::~UnitTable()
+{
+ for (auto & iter : _unit_map)
+ {
+ delete iter.second;
+ }
+}
+
+void UnitTable::addUnit(Unit const &u, bool primary)
+{
+ _unit_map[make_unit_code(u.abbr.c_str())] = new Unit(u);
+ if (primary) {
+ _primary_unit[u.type] = u.abbr;
+ }
+}
+
+Unit const *UnitTable::getUnit(char const *abbr) const
+{
+ UnitCodeMap::const_iterator f = _unit_map.find(make_unit_code(abbr));
+ if (f != _unit_map.end()) {
+ return &(*f->second);
+ }
+ return &_empty_unit;
+}
+
+Unit const *UnitTable::getUnit(Glib::ustring const &unit_abbr) const
+{
+ return getUnit(unit_abbr.c_str());
+}
+Unit const *UnitTable::getUnit(SVGLength::Unit u) const
+{
+ if (u == 0 || u > SVGLength::LAST_UNIT) {
+ return &_empty_unit;
+ }
+
+ UnitCodeMap::const_iterator f = _unit_map.find(svg_length_lookup[u]);
+ if (f != _unit_map.end()) {
+ return &(*f->second);
+ }
+ return &_empty_unit;
+}
+
+Unit const *UnitTable::findUnit(double factor, UnitType type) const
+{
+ const double eps = factor * 0.01; // allow for 1% deviation
+
+ UnitCodeMap::const_iterator cit = _unit_map.begin();
+ while (cit != _unit_map.end()) {
+ if (cit->second->type == type) {
+ if (Geom::are_near(cit->second->factor, factor, eps)) {
+ // unit found!
+ break;
+ }
+ }
+ ++cit;
+ }
+
+ if (cit != _unit_map.end()) {
+ return cit->second;
+ } else {
+ return getUnit(_primary_unit[type]);
+ }
+}
+
+Quantity UnitTable::parseQuantity(Glib::ustring const &q) const
+{
+ Glib::MatchInfo match_info;
+
+ // Extract value
+ double value = 0;
+ Glib::RefPtr<Glib::Regex> value_regex = Glib::Regex::create("[-+]*[\\d+]*[\\.,]*[\\d+]*[eE]*[-+]*\\d+");
+ if (value_regex->match(q, match_info)) {
+ std::istringstream tmp_v(match_info.fetch(0).raw());
+ tmp_v >> value;
+ }
+ int start_pos, end_pos;
+ match_info.fetch_pos(0, end_pos, start_pos);
+ end_pos = q.size() - start_pos;
+ Glib::ustring u = q.substr(start_pos, end_pos);
+
+ // Extract unit abbreviation
+ Glib::ustring abbr;
+ Glib::RefPtr<Glib::Regex> unit_regex = Glib::Regex::create("[A-z%]+");
+ if (unit_regex->match(u, match_info)) {
+ abbr = match_info.fetch(0);
+ }
+
+ Quantity qty(value, abbr);
+ return qty;
+}
+
+/* UNSAFE while passing around pointers to the Unit objects in this table
+bool UnitTable::deleteUnit(Unit const &u)
+{
+ bool deleted = false;
+ // Cannot delete the primary unit type since it's
+ // used for conversions
+ if (u.abbr != _primary_unit[u.type]) {
+ UnitCodeMap::iterator iter = _unit_map.find(make_unit_code(u.abbr.c_str()));
+ if (iter != _unit_map.end()) {
+ delete (*iter).second;
+ _unit_map.erase(iter);
+ deleted = true;
+ }
+ }
+ return deleted;
+}
+*/
+
+bool UnitTable::hasUnit(Glib::ustring const &unit) const
+{
+ UnitCodeMap::const_iterator iter = _unit_map.find(make_unit_code(unit.c_str()));
+ return (iter != _unit_map.end());
+}
+
+UnitTable::UnitMap UnitTable::units(UnitType type) const
+{
+ UnitMap submap;
+ for (auto iter : _unit_map) {
+ if (iter.second->type == type) {
+ submap.insert(UnitMap::value_type(iter.second->abbr, *iter.second));
+ }
+ }
+
+ return submap;
+}
+
+Glib::ustring UnitTable::primary(UnitType type) const
+{
+ return _primary_unit[type];
+}
+
+bool UnitTable::load(std::string const &filename) {
+ UnitParser uparser(this);
+ Glib::Markup::ParseContext ctx(uparser);
+
+ try {
+ Glib::ustring unitfile = Glib::file_get_contents(filename);
+ ctx.parse(unitfile);
+ ctx.end_parse();
+ } catch (Glib::FileError const &e) {
+ g_warning("Units file %s is missing: %s\n", filename.c_str(), e.what().c_str());
+ return false;
+ } catch (Glib::MarkupError const &e) {
+ g_warning("Problem loading units file '%s': %s\n", filename.c_str(), e.what().c_str());
+ return false;
+ }
+ return true;
+}
+
+/*
+bool UnitTable::save(std::string const &filename) {
+ g_warning("UnitTable::save(): not implemented");
+
+ return false;
+}
+*/
+
+Inkscape::Util::UnitTable unit_table;
+
+void UnitParser::on_start_element(Ctx &/*ctx*/, Glib::ustring const &name, AttrMap const &attrs)
+{
+ if (name == "unit") {
+ // reset for next use
+ unit.clear();
+ primary = false;
+ skip = false;
+
+ AttrMap::const_iterator f;
+ if ((f = attrs.find("type")) != attrs.end()) {
+ Glib::ustring type = f->second;
+ TypeMap::const_iterator tf = type_map.find(type);
+ if (tf != type_map.end()) {
+ unit.type = tf->second;
+ } else {
+ g_warning("Skipping unknown unit type '%s'.\n", type.c_str());
+ skip = true;
+ }
+ }
+ if ((f = attrs.find("pri")) != attrs.end()) {
+ primary = (f->second[0] == 'y' || f->second[0] == 'Y');
+ }
+ }
+}
+
+void UnitParser::on_text(Ctx &ctx, Glib::ustring const &text)
+{
+ Glib::ustring element = ctx.get_element();
+ if (element == "name") {
+ unit.name = text;
+ } else if (element == "plural") {
+ unit.name_plural = text;
+ } else if (element == "abbr") {
+ unit.abbr = text;
+ } else if (element == "factor") {
+ // TODO make sure we use the right conversion
+ unit.factor = g_ascii_strtod(text.c_str(), nullptr);
+ } else if (element == "description") {
+ unit.description = text;
+ }
+}
+
+void UnitParser::on_end_element(Ctx &/*ctx*/, Glib::ustring const &name)
+{
+ if (name == "unit" && !skip) {
+ tbl->addUnit(unit, primary);
+ }
+}
+
+Quantity::Quantity(double q, Unit const *u)
+ : unit(u)
+ , quantity(q)
+{
+}
+Quantity::Quantity(double q, Glib::ustring const &u)
+ : unit(unit_table.getUnit(u.c_str()))
+ , quantity(q)
+{
+}
+Quantity::Quantity(double q, char const *u)
+ : unit(unit_table.getUnit(u))
+ , quantity(q)
+{
+}
+
+bool Quantity::compatibleWith(Unit const *u) const
+{
+ return unit->compatibleWith(u);
+}
+bool Quantity::compatibleWith(Glib::ustring const &u) const
+{
+ return compatibleWith(u.c_str());
+}
+bool Quantity::compatibleWith(char const *u) const
+{
+ return compatibleWith(unit_table.getUnit(u));
+}
+
+double Quantity::value(Unit const *u) const
+{
+ return convert(quantity, unit, u);
+}
+double Quantity::value(Glib::ustring const &u) const
+{
+ return value(u.c_str());
+}
+double Quantity::value(char const *u) const
+{
+ return value(unit_table.getUnit(u));
+}
+
+Glib::ustring Quantity::string(Unit const *u) const {
+ return Glib::ustring::format(std::fixed, std::setprecision(2), value(u)) + " " + u->abbr;
+}
+Glib::ustring Quantity::string(Glib::ustring const &u) const {
+ return string(unit_table.getUnit(u.c_str()));
+}
+Glib::ustring Quantity::string() const {
+ return string(unit);
+}
+
+double Quantity::convert(double from_dist, Unit const *from, Unit const *to)
+{
+ return from->convert(from_dist, to);
+}
+double Quantity::convert(double from_dist, Glib::ustring const &from, Unit const *to)
+{
+ return convert(from_dist, unit_table.getUnit(from.c_str()), to);
+}
+double Quantity::convert(double from_dist, Unit const *from, Glib::ustring const &to)
+{
+ return convert(from_dist, from, unit_table.getUnit(to.c_str()));
+}
+double Quantity::convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to)
+{
+ return convert(from_dist, unit_table.getUnit(from.c_str()), unit_table.getUnit(to.c_str()));
+}
+double Quantity::convert(double from_dist, char const *from, char const *to)
+{
+ return convert(from_dist, unit_table.getUnit(from), unit_table.getUnit(to));
+}
+
+bool Quantity::operator<(Quantity const &rhs) const
+{
+ if (unit->type != rhs.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return quantity < rhs.value(unit);
+}
+bool Quantity::operator==(Quantity const &other) const
+{
+ /** \fixme This is overly strict. I think we should change this to:
+ if (unit->type != other.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return are_near(quantity, other.value(unit));
+ */
+ return (*unit == *other.unit) && (quantity == other.quantity);
+}
+
+} // namespace Util
+} // namespace Inkscape
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/units.h b/src/util/units.h
new file mode 100644
index 0000000..c271ec5
--- /dev/null
+++ b/src/util/units.h
@@ -0,0 +1,231 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Inkscape Units
+ * These classes are used for defining different unit systems.
+ *
+ * Authors:
+ * Matthew Petroff <matthew@mpetroff.net>
+ *
+ * Copyright (C) 2013 Matthew Petroff
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#ifndef INKSCAPE_UTIL_UNITS_H
+#define INKSCAPE_UTIL_UNITS_H
+
+#include <unordered_map>
+#include <boost/operators.hpp>
+#include <glibmm/ustring.h>
+#include <2geom/coord.h>
+#include "svg/svg-length.h"
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+#define DEFAULT_UNIT_NAME "mm"
+
+namespace std {
+template <>
+struct hash<Glib::ustring> : public std::unary_function<Glib::ustring, std::size_t> {
+ std::size_t operator()(Glib::ustring const &s) const {
+ return hash<std::string>()(s.raw());
+ }
+};
+} // namespace std
+
+#endif
+
+namespace Inkscape {
+namespace Util {
+
+enum UnitType {
+ UNIT_TYPE_DIMENSIONLESS, /* Percentage */
+ UNIT_TYPE_LINEAR,
+ UNIT_TYPE_LINEAR_SCALED,
+ UNIT_TYPE_RADIAL,
+ UNIT_TYPE_TIME,
+ UNIT_TYPE_FONT_HEIGHT,
+ UNIT_TYPE_QTY,
+ UNIT_TYPE_NONE = -1
+};
+
+const char DEG[] = "°";
+
+class Unit
+ : boost::equality_comparable<Unit>
+{
+public:
+ Unit();
+ Unit(UnitType type,
+ double factor,
+ Glib::ustring name,
+ Glib::ustring name_plural,
+ Glib::ustring abbr,
+ Glib::ustring description);
+
+ void clear();
+
+ bool isAbsolute() const { return type != UNIT_TYPE_DIMENSIONLESS; }
+
+ /**
+ * Returns the suggested precision to use for displaying numbers
+ * of this unit.
+ */
+ int defaultDigits() const;
+
+ /** Checks if a unit is compatible with the specified unit. */
+ bool compatibleWith(Unit const *u) const;
+ bool compatibleWith(Glib::ustring const &) const;
+ bool compatibleWith(char const *) const;
+
+ UnitType type;
+ double factor;
+ Glib::ustring name;
+ Glib::ustring name_plural;
+ Glib::ustring abbr;
+ Glib::ustring description;
+
+ /** Check if units are equal. */
+ bool operator==(Unit const &other) const;
+
+ /** Get SVG unit code. */
+ int svgUnit() const;
+
+ /** Convert value from this unit **/
+ double convert(double from_dist, Unit const *to) const;
+ double convert(double from_dist, Glib::ustring const &to) const;
+ double convert(double from_dist, char const *to) const;
+};
+
+class Quantity
+ : boost::totally_ordered<Quantity>
+{
+public:
+ Unit const *unit;
+ double quantity;
+
+ /** Initialize a quantity. */
+ Quantity(double q, Unit const *u);
+ Quantity(double q, Glib::ustring const &u);
+ Quantity(double q, char const *u);
+
+ /** Checks if a quantity is compatible with the specified unit. */
+ bool compatibleWith(Unit const *u) const;
+ bool compatibleWith(Glib::ustring const &u) const;
+ bool compatibleWith(char const *u) const;
+
+ /** Return the quantity's value in the specified unit. */
+ double value(Unit const *u) const;
+ double value(Glib::ustring const &u) const;
+ double value(char const *u) const;
+
+ /** Return a printable string of the value in the specified unit. */
+ Glib::ustring string(Unit const *u) const;
+ Glib::ustring string(Glib::ustring const &u) const;
+ Glib::ustring string() const;
+
+ /** Convert distances.
+ no NULL check is performed on the passed pointers to Unit objects! */
+ static double convert(double from_dist, Unit const *from, Unit const *to);
+ static double convert(double from_dist, Glib::ustring const &from, Unit const *to);
+ static double convert(double from_dist, Unit const *from, Glib::ustring const &to);
+ static double convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to);
+ static double convert(double from_dist, char const *from, char const *to);
+
+ /** Comparison operators. */
+ bool operator<(Quantity const &rhs) const;
+ bool operator==(Quantity const &other) const;
+};
+
+inline bool are_near(Quantity const &a, Quantity const &b, double eps=Geom::EPSILON)
+{
+ return Geom::are_near(a.quantity, b.value(a.unit), eps);
+}
+
+class UnitTable {
+public:
+ /**
+ * Initializes the unit tables and identifies the primary unit types.
+ *
+ * The primary unit's conversion factor is required to be 1.00
+ */
+ UnitTable();
+ virtual ~UnitTable();
+
+ typedef std::unordered_map<Glib::ustring, Unit> UnitMap;
+ typedef std::unordered_map<unsigned, Unit*> UnitCodeMap;
+
+ /** Add a new unit to the table */
+ void addUnit(Unit const &u, bool primary);
+
+ /** Retrieve a given unit based on its string identifier */
+ Unit const *getUnit(Glib::ustring const &name) const;
+ Unit const *getUnit(char const *name) const;
+
+ /** Try to find a unit based on its conversion factor to the primary */
+ Unit const *findUnit(double factor, UnitType type) const;
+
+ /** Retrieve a given unit based on its SVGLength unit */
+ Unit const *getUnit(SVGLength::Unit u) const;
+
+ /** Retrieve a quantity based on its string identifier */
+ Quantity parseQuantity(Glib::ustring const &q) const;
+
+ /** Remove a unit definition from the given unit type table * /
+ * DISABLED, unsafe with the current passing around pointers to Unit objects in this table */
+ //bool deleteUnit(Unit const &u);
+
+ /** Returns true if the given string 'name' is a valid unit in the table */
+ bool hasUnit(Glib::ustring const &name) const;
+
+ /** Provides an iterable list of items in the given unit table */
+ UnitMap units(UnitType type) const;
+
+ /** Returns the default unit abbr for the given type */
+ Glib::ustring primary(UnitType type) const;
+
+ double getScale() const;
+
+ void setScale();
+
+ /** Load units from an XML file.
+ *
+ * Loads and merges the contents of the given file into the UnitTable,
+ * possibly overwriting existing unit definitions.
+ *
+ * @param filename file to be loaded
+ */
+ bool load(std::string const &filename);
+
+ /* * Saves the current UnitTable to the given file. */
+ //bool save(std::string const &filename);
+
+protected:
+ UnitCodeMap _unit_map;
+ Glib::ustring _primary_unit[UNIT_TYPE_QTY];
+
+ double _linear_scale;
+ static Unit _empty_unit;
+
+private:
+ UnitTable(UnitTable const &t);
+ UnitTable operator=(UnitTable const &t);
+
+};
+
+extern UnitTable unit_table;
+
+} // namespace Util
+} // namespace Inkscape
+
+#endif // define INKSCAPE_UTIL_UNITS_H
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/util/ziptool.cpp b/src/util/ziptool.cpp
new file mode 100644
index 0000000..dc87396
--- /dev/null
+++ b/src/util/ziptool.cpp
@@ -0,0 +1,3043 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors:
+ * see git history
+ * Bob Jamison
+ *
+ * Copyright (C) 2018 Authors
+ * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information.
+ */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+/*
+ * This is intended to be a standalone, reduced capability
+ * implementation of Gzip and Zip functionality. Its
+ * targeted use case is for archiving and retrieving single files
+ * which use these encoding types. Being memory based and
+ * non-optimized, it is not useful in cases where very large
+ * archives are needed or where high performance is desired.
+ * However, it should hopefully work very well for smaller,
+ * one-at-a-time tasks. What you get in return is the ability
+ * to drop these files into your project and remove the dependencies
+ * on ZLib and Info-Zip. Enjoy.
+ */
+
+
+#include <cstdio>
+#include <cstdarg>
+#include <ctime>
+
+#include <string>
+#include <utility>
+
+#include "ziptool.h"
+
+
+
+
+
+
+//########################################################################
+//# A D L E R 3 2
+//########################################################################
+
+/**
+ * Constructor
+ */
+Adler32::Adler32()
+{
+ reset();
+}
+
+/**
+ * Destructor
+ */
+Adler32::~Adler32()
+= default;
+
+/**
+ * Reset Adler-32 checksum to initial value.
+ */
+void Adler32::reset()
+{
+ value = 1;
+}
+
+// ADLER32_BASE is the largest prime number smaller than 65536
+#define ADLER32_BASE 65521
+
+void Adler32::update(unsigned char b)
+{
+ unsigned long s1 = value & 0xffff;
+ unsigned long s2 = (value >> 16) & 0xffff;
+ s1 += b & 0xff;
+ s2 += s1;
+ value = ((s2 % ADLER32_BASE) << 16) | (s1 % ADLER32_BASE);
+}
+
+void Adler32::update(char *str)
+{
+ if (str)
+ while (*str)
+ update((unsigned char)*str++);
+}
+
+
+/**
+ * Returns current checksum value.
+ */
+unsigned long Adler32::getValue()
+{
+ return value & 0xffffffffL;
+}
+
+
+
+//########################################################################
+//# C R C 3 2
+//########################################################################
+
+/**
+ * Constructor
+ */
+Crc32::Crc32()
+{
+ reset();
+}
+
+/**
+ * Destructor
+ */
+Crc32::~Crc32()
+= default;
+
+static bool crc_table_ready = false;
+static unsigned long crc_table[256];
+
+/**
+ * make the table for a fast CRC.
+ */
+static void makeCrcTable()
+{
+ if (crc_table_ready)
+ return;
+ for (int n = 0; n < 256; n++)
+ {
+ unsigned long c = n;
+ for (int k = 8; --k >= 0; )
+ {
+ if ((c & 1) != 0)
+ c = 0xedb88320 ^ (c >> 1);
+ else
+ c >>= 1;
+ }
+ crc_table[n] = c;
+ }
+ crc_table_ready = true;
+}
+
+
+/**
+ * Reset CRC-32 checksum to initial value.
+ */
+void Crc32::reset()
+{
+ value = 0;
+ makeCrcTable();
+}
+
+void Crc32::update(unsigned char b)
+{
+ unsigned long c = ~value;
+
+ c &= 0xffffffff;
+ c = crc_table[(c ^ b) & 0xff] ^ (c >> 8);
+ value = ~c;
+}
+
+
+void Crc32::update(char *str)
+{
+ if (str)
+ while (*str)
+ update((unsigned char)*str++);
+}
+
+void Crc32::update(const std::vector<unsigned char> &buf)
+{
+ std::vector<unsigned char>::const_iterator iter;
+ for (iter=buf.begin() ; iter!=buf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ update(ch);
+ }
+}
+
+
+/**
+ * Returns current checksum value.
+ */
+unsigned long Crc32::getValue()
+{
+ return value & 0xffffffffL;
+}
+
+//########################################################################
+//# I N F L A T E R
+//########################################################################
+
+
+/**
+ *
+ */
+struct Huffman
+{
+ int *count; // number of symbols of each length
+ int *symbol; // canonically ordered symbols
+};
+
+/**
+ *
+ */
+class Inflater
+{
+public:
+
+ Inflater();
+
+ virtual ~Inflater();
+
+ static const int MAXBITS = 15; // max bits in a code
+ static const int MAXLCODES = 286; // max number of literal/length codes
+ static const int MAXDCODES = 30; // max number of distance codes
+ static const int MAXCODES = 316; // max codes lengths to read
+ static const int FIXLCODES = 288; // number of fixed literal/length codes
+
+ /**
+ *
+ */
+ bool inflate(std::vector<unsigned char> &destination,
+ std::vector<unsigned char> &source);
+
+private:
+
+ /**
+ *
+ */
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ /**
+ *
+ */
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ /**
+ *
+ */
+ void dump();
+
+ /**
+ *
+ */
+ int buildHuffman(Huffman *h, int *length, int n);
+
+ /**
+ *
+ */
+ bool getBits(int need, int *oval);
+
+ /**
+ *
+ */
+ int doDecode(Huffman *h);
+
+ /**
+ *
+ */
+ bool doCodes(Huffman *lencode, Huffman *distcode);
+
+ /**
+ *
+ */
+ bool doStored();
+
+ /**
+ *
+ */
+ bool doFixed();
+
+ /**
+ *
+ */
+ bool doDynamic();
+
+
+ std::vector<unsigned char>dest;
+
+ std::vector<unsigned char>src;
+ unsigned long srcPos; //current read position
+ int bitBuf;
+ int bitCnt;
+
+};
+
+
+/**
+ *
+ */
+Inflater::Inflater() :
+ dest(),
+ src(),
+ srcPos(0),
+ bitBuf(0),
+ bitCnt(0)
+{
+}
+
+/**
+ *
+ */
+Inflater::~Inflater()
+= default;
+
+/**
+ *
+ */
+void Inflater::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Inflater error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ *
+ */
+void Inflater::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Inflater:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+
+/**
+ *
+ */
+void Inflater::dump()
+{
+ for (unsigned char i : dest)
+ {
+ fputc(i, stdout);
+ }
+}
+
+/**
+ *
+ */
+int Inflater::buildHuffman(Huffman *h, int *length, int n)
+{
+ // count number of codes of each length
+ for (int len = 0; len <= MAXBITS; len++)
+ h->count[len] = 0;
+ for (int symbol = 0; symbol < n; symbol++)
+ (h->count[length[symbol]])++; // assumes lengths are within bounds
+ if (h->count[0] == n) // no codes!
+ {
+ error("huffman tree will result in failed decode");
+ return -1;
+ }
+
+ // check for an over-subscribed or incomplete set of lengths
+ int left = 1; // number of possible codes left of current length
+ for (int len = 1; len <= MAXBITS; len++)
+ {
+ left <<= 1; // one more bit, double codes left
+ left -= h->count[len]; // deduct count from possible codes
+ if (left < 0)
+ {
+ error("huffman over subscribed");
+ return -1;
+ }
+ }
+
+ // generate offsets into symbol table for each length for sorting
+ int offs[MAXBITS+1]; //offsets in symbol table for each length
+ offs[1] = 0;
+ for (int len = 1; len < MAXBITS; len++)
+ offs[len + 1] = offs[len] + h->count[len];
+
+ /*
+ * put symbols in table sorted by length, by symbol order within each
+ * length
+ */
+ for (int symbol = 0; symbol < n; symbol++)
+ if (length[symbol] != 0)
+ h->symbol[offs[length[symbol]]++] = symbol;
+
+ // return zero for complete set, positive for incomplete set
+ return left;
+}
+
+
+/**
+ *
+ */
+bool Inflater::getBits(int requiredBits, int *oval)
+{
+ long val = bitBuf;
+
+ //add more bytes if needed
+ while (bitCnt < requiredBits)
+ {
+ if (srcPos >= src.size())
+ {
+ error("premature end of input");
+ return false;
+ }
+ val |= ((long)(src[srcPos++])) << bitCnt;
+ bitCnt += 8;
+ }
+
+ //update the buffer and return the data
+ bitBuf = (int)(val >> requiredBits);
+ bitCnt -= requiredBits;
+ *oval = (int)(val & ((1L << requiredBits) - 1));
+
+ return true;
+}
+
+
+/**
+ *
+ */
+int Inflater::doDecode(Huffman *h)
+{
+ int bitTmp = bitBuf;
+ int left = bitCnt;
+ int code = 0;
+ int first = 0;
+ int index = 0;
+ int len = 1;
+ int *next = h->count + 1;
+ while (true)
+ {
+ while (left--)
+ {
+ code |= bitTmp & 1;
+ bitTmp >>= 1;
+ int count = *next++;
+ if (code < first + count)
+ { /* if length len, return symbol */
+ bitBuf = bitTmp;
+ bitCnt = (bitCnt - len) & 7;
+ return h->symbol[index + (code - first)];
+ }
+ index += count;
+ first += count;
+ first <<= 1;
+ code <<= 1;
+ len++;
+ }
+ left = (MAXBITS+1) - len;
+ if (left == 0)
+ break;
+ if (srcPos >= src.size())
+ {
+ error("premature end of input");
+ dump();
+ return -1;
+ }
+ bitTmp = src[srcPos++];
+ if (left > 8)
+ left = 8;
+ }
+
+ error("no end of block found");
+ return -1;
+}
+
+/**
+ *
+ */
+bool Inflater::doCodes(Huffman *lencode, Huffman *distcode)
+{
+ static const int lens[29] = { // Size base for length codes 257..285
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
+ static const int lext[29] = { // Extra bits for length codes 257..285
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
+ 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
+ static const int dists[30] = { // Offset base for distance codes 0..29
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577};
+ static const int dext[30] = { // Extra bits for distance codes 0..29
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
+ 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
+ 12, 12, 13, 13};
+
+ //decode literals and length/distance pairs
+ while (true)
+ {
+ int symbol = doDecode(lencode);
+ if (symbol == 256)
+ break;
+ if (symbol < 0)
+ {
+ return false;
+ }
+ if (symbol < 256) //literal
+ {
+ dest.push_back(symbol);
+ }
+ else if (symbol > 256)//length
+ {
+ symbol -= 257;
+ if (symbol >= 29)
+ {
+ error("invalid fixed code");
+ return false;
+ }
+ int ret;
+ if (!getBits(lext[symbol], &ret))
+ return false;
+ int len = lens[symbol] + ret;
+
+ symbol = doDecode(distcode);//distance
+ if (symbol < 0)
+ {
+ return false;
+ }
+
+ if (!getBits(dext[symbol], &ret))
+ return false;
+ unsigned int dist = dists[symbol] + ret;
+ if (dist > dest.size())
+ {
+ error("distance too far back %d/%d", dist, dest.size());
+ dump();
+ //printf("pos:%d\n", srcPos);
+ return false;
+ }
+
+ // copy length bytes from distance bytes back
+ //dest.push_back('{');
+ while (len--)
+ {
+ dest.push_back(dest[dest.size() - dist]);
+ }
+ //dest.push_back('}');
+
+ }
+ }
+
+ return true;
+}
+
+/**
+ */
+bool Inflater::doStored()
+{
+ //trace("### stored ###");
+
+ // clear bits from current byte
+ bitBuf = 0;
+ bitCnt = 0;
+
+ // length
+ if (srcPos + 4 > src.size())
+ {
+ error("not enough input");
+ return false;
+ }
+
+ int len = src[srcPos++];
+ len |= src[srcPos++] << 8;
+ //trace("### len:%d", len);
+ // check complement
+ if (src[srcPos++] != (~len & 0xff) ||
+ src[srcPos++] != ((~len >> 8) & 0xff))
+ {
+ error("twos complement for storage size do not match");
+ return false;
+ }
+
+ // copy data
+ if (srcPos + len > src.size())
+ {
+ error("Not enough input for stored block");
+ return false;
+ }
+ while (len--)
+ dest.push_back(src[srcPos++]);
+
+ return true;
+}
+
+/**
+ */
+bool Inflater::doFixed()
+{
+ //trace("### fixed ###");
+
+ static bool firstTime = true;
+ static int lencnt[MAXBITS+1], lensym[FIXLCODES];
+ static int distcnt[MAXBITS+1], distsym[MAXDCODES];
+ static Huffman lencode = {lencnt, lensym};
+ static Huffman distcode = {distcnt, distsym};
+
+ if (firstTime)
+ {
+ firstTime = false;
+
+ int lengths[FIXLCODES];
+
+ // literal/length table
+ int symbol = 0;
+ for ( ; symbol < 144; symbol++)
+ lengths[symbol] = 8;
+ for ( ; symbol < 256; symbol++)
+ lengths[symbol] = 9;
+ for ( ; symbol < 280; symbol++)
+ lengths[symbol] = 7;
+ for ( ; symbol < FIXLCODES; symbol++)
+ lengths[symbol] = 8;
+ buildHuffman(&lencode, lengths, FIXLCODES);
+
+ // distance table
+ for (int symbol = 0; symbol < MAXDCODES; symbol++)
+ lengths[symbol] = 5;
+ buildHuffman(&distcode, lengths, MAXDCODES);
+ }
+
+ // decode data until end-of-block code
+ bool ret = doCodes(&lencode, &distcode);
+ return ret;
+}
+
+/**
+ */
+bool Inflater::doDynamic()
+{
+ //trace("### dynamic ###");
+ int lengths[MAXCODES]; // descriptor code lengths
+ int lencnt[MAXBITS+1], lensym[MAXLCODES]; // lencode memory
+ int distcnt[MAXBITS+1], distsym[MAXDCODES]; // distcode memory
+ Huffman lencode = {lencnt, lensym}; // length code
+ Huffman distcode = {distcnt, distsym}; // distance code
+ static const int order[19] = // permutation of code length codes
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ // get number of lengths in each table, check lengths
+ int ret;
+ if (!getBits(5, &ret))
+ return false;
+ int nlen = ret + 257;
+ if (!getBits(5, &ret))
+ return false;
+ int ndist = ret + 1;
+ if (!getBits(4, &ret))
+ return false;
+ int ncode = ret + 4;
+ if (nlen > MAXLCODES || ndist > MAXDCODES)
+ {
+ error("Bad codes");
+ return false;
+ }
+
+ // get code length code lengths
+ int index = 0;
+ for ( ; index < ncode; index++)
+ {
+ if (!getBits(3, &ret))
+ return false;
+ lengths[order[index]] = ret;
+ }
+ for ( ; index < 19; index++)
+ lengths[order[index]] = 0;
+
+ // build huffman table for code lengths codes
+ if (buildHuffman(&lencode, lengths, 19) != 0)
+ return false;
+
+ // read length/literal and distance code length tables
+ index = 0;
+ while (index < nlen + ndist)
+ {
+ int symbol = doDecode(&lencode);
+ if (symbol < 16) // length in 0..15
+ lengths[index++] = symbol;
+ else
+ { // repeat instruction
+ int len = 0; // assume repeating zeros
+ if (symbol == 16)
+ { // repeat last length 3..6 times
+ if (index == 0)
+ {
+ error("no last length");
+ return false;
+ }
+ len = lengths[index - 1];// last length
+ if (!getBits(2, &ret))
+ return false;
+ symbol = 3 + ret;
+ }
+ else if (symbol == 17) // repeat zero 3..10 times
+ {
+ if (!getBits(3, &ret))
+ return false;
+ symbol = 3 + ret;
+ }
+ else // == 18, repeat zero 11..138 times
+ {
+ if (!getBits(7, &ret))
+ return false;
+ symbol = 11 + ret;
+ }
+ if (index + symbol > nlen + ndist)
+ {
+ error("too many lengths");
+ return false;
+ }
+ while (symbol--) // repeat last or zero symbol times
+ lengths[index++] = len;
+ }
+ }
+
+ // build huffman table for literal/length codes
+ int err = buildHuffman(&lencode, lengths, nlen);
+ if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
+ {
+ error("incomplete length codes");
+ //return false;
+ }
+ // build huffman table for distance codes
+ err = buildHuffman(&distcode, lengths + nlen, ndist);
+ if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
+ {
+ error("incomplete dist codes");
+ return false;
+ }
+
+ // decode data until end-of-block code
+ bool retn = doCodes(&lencode, &distcode);
+ return retn;
+}
+
+/**
+ */
+bool Inflater::inflate(std::vector<unsigned char> &destination,
+ std::vector<unsigned char> &source)
+{
+ dest.clear();
+ src = source;
+ srcPos = 0;
+ bitBuf = 0;
+ bitCnt = 0;
+
+ while (true)
+ {
+ int last; // one if last block
+ if (!getBits(1, &last))
+ return false;
+ int type; // block type 0..3
+ if (!getBits(2, &type))
+ return false;
+ switch (type)
+ {
+ case 0:
+ if (!doStored())
+ return false;
+ break;
+ case 1:
+ if (!doFixed())
+ return false;
+ break;
+ case 2:
+ if (!doDynamic())
+ return false;
+ break;
+ default:
+ error("Unknown block type %d", type);
+ return false;
+ }
+ if (last)
+ break;
+ }
+
+ destination = dest;
+
+ return true;
+}
+
+
+
+
+
+
+//########################################################################
+//# D E F L A T E R
+//########################################################################
+
+
+#define DEFLATER_BUF_SIZE 32768
+class Deflater
+{
+public:
+
+ /**
+ *
+ */
+ Deflater();
+
+ /**
+ *
+ */
+ virtual ~Deflater();
+
+ /**
+ *
+ */
+ virtual void reset();
+
+ /**
+ *
+ */
+ virtual bool update(int ch);
+
+ /**
+ *
+ */
+ virtual bool finish();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getCompressed();
+
+ /**
+ *
+ */
+ bool deflate(std::vector<unsigned char> &dest,
+ const std::vector<unsigned char> &src);
+
+ void encodeDistStatic(unsigned int len, unsigned int dist);
+
+private:
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ bool compressWindow();
+
+ bool compress();
+
+ std::vector<unsigned char> compressed;
+
+ std::vector<unsigned char> uncompressed;
+
+ std::vector<unsigned char> window;
+
+ unsigned int windowPos;
+
+ //#### Output
+ unsigned int outputBitBuf;
+ unsigned int outputNrBits;
+
+ void put(int ch);
+
+ void putWord(int ch);
+
+ void putFlush();
+
+ void putBits(unsigned int ch, unsigned int bitsWanted);
+
+ void putBitsR(unsigned int ch, unsigned int bitsWanted);
+
+ //#### Huffman Encode
+ void encodeLiteralStatic(unsigned int ch);
+
+ unsigned char windowBuf[DEFLATER_BUF_SIZE];
+ //assume 32-bit ints
+ unsigned int windowHashBuf[DEFLATER_BUF_SIZE];
+};
+
+
+//########################################################################
+//# A P I
+//########################################################################
+
+
+/**
+ *
+ */
+Deflater::Deflater()
+{
+ reset();
+}
+
+/**
+ *
+ */
+Deflater::~Deflater()
+= default;
+
+/**
+ *
+ */
+void Deflater::reset()
+{
+ compressed.clear();
+ uncompressed.clear();
+ window.clear();
+ windowPos = 0;
+ outputBitBuf = 0;
+ outputNrBits = 0;
+ for (int k=0; k<DEFLATER_BUF_SIZE; k++)
+ {
+ windowBuf[k]=0;
+ windowHashBuf[k]=0;
+ }
+}
+
+/**
+ *
+ */
+bool Deflater::update(int ch)
+{
+ uncompressed.push_back((unsigned char)(ch & 0xff));
+ return true;
+}
+
+/**
+ *
+ */
+bool Deflater::finish()
+{
+ return compress();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &Deflater::getCompressed()
+{
+ return compressed;
+}
+
+
+/**
+ *
+ */
+bool Deflater::deflate(std::vector<unsigned char> &dest,
+ const std::vector<unsigned char> &src)
+{
+ reset();
+ uncompressed = src;
+ if (!compress())
+ return false;
+ dest = compressed;
+ return true;
+}
+
+
+
+
+
+
+
+//########################################################################
+//# W O R K I N G C O D E
+//########################################################################
+
+
+//#############################
+//# M E S S A G E S
+//#############################
+
+/**
+ * Print error messages
+ */
+void Deflater::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Deflater error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ * Print trace messages
+ */
+void Deflater::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "Deflater:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+
+
+
+//#############################
+//# O U T P U T
+//#############################
+
+/**
+ *
+ */
+void Deflater::put(int ch)
+{
+ compressed.push_back(ch);
+ outputBitBuf = 0;
+ outputNrBits = 0;
+}
+
+/**
+ *
+ */
+void Deflater::putWord(int ch)
+{
+ int lo = (ch ) & 0xff;
+ int hi = (ch>>8) & 0xff;
+ put(lo);
+ put(hi);
+}
+
+/**
+ *
+ */
+void Deflater::putFlush()
+{
+ if (outputNrBits > 0)
+ {
+ put(outputBitBuf & 0xff);
+ }
+ outputBitBuf = 0;
+ outputNrBits = 0;
+}
+
+/**
+ *
+ */
+void Deflater::putBits(unsigned int ch, unsigned int bitsWanted)
+{
+ //trace("n:%4u, %d\n", ch, bitsWanted);
+
+ while (bitsWanted--)
+ {
+ //add bits to position 7. shift right
+ outputBitBuf = (outputBitBuf>>1) + (ch<<7 & 0x80);
+ ch >>= 1;
+ outputNrBits++;
+ if (outputNrBits >= 8)
+ {
+ unsigned char b = outputBitBuf & 0xff;
+ //printf("b:%02x\n", b);
+ put(b);
+ }
+ }
+}
+
+static unsigned int bitReverse(unsigned int code, unsigned int nrBits)
+{
+ unsigned int outb = 0;
+ while (nrBits--)
+ {
+ outb = (outb << 1) | (code & 0x01);
+ code >>= 1;
+ }
+ return outb;
+}
+
+
+/**
+ *
+ */
+void Deflater::putBitsR(unsigned int ch, unsigned int bitsWanted)
+{
+ //trace("r:%4u, %d", ch, bitsWanted);
+
+ unsigned int rcode = bitReverse(ch, bitsWanted);
+
+ putBits(rcode, bitsWanted);
+
+}
+
+
+//#############################
+//# E N C O D E
+//#############################
+
+
+
+void Deflater::encodeLiteralStatic(unsigned int ch)
+{
+ //trace("c: %d", ch);
+
+ if (ch < 144)
+ {
+ putBitsR(ch + 0x0030 , 8); // 00110000
+ }
+ else if (ch < 256)
+ {
+ putBitsR(ch - 144 + 0x0190 , 9); // 110010000
+ }
+ else if (ch < 280)
+ {
+ putBitsR(ch - 256 + 0x0000 , 7); // 0000000
+ }
+ else if (ch < 288)
+ {
+ putBitsR(ch - 280 + 0x00c0 , 8); // 11000000
+ }
+ else //out of range
+ {
+ error("Literal out of range: %d", ch);
+ }
+
+}
+
+
+struct LenBase
+{
+ unsigned int base;
+ unsigned int range;
+ unsigned int bits;
+};
+
+LenBase lenBases[] =
+{
+ // clang-format off
+ { 3, 1, 0 },
+ { 4, 1, 0 },
+ { 5, 1, 0 },
+ { 6, 1, 0 },
+ { 7, 1, 0 },
+ { 8, 1, 0 },
+ { 9, 1, 0 },
+ { 10, 1, 0 },
+ { 11, 2, 1 },
+ { 13, 2, 1 },
+ { 15, 2, 1 },
+ { 17, 2, 1 },
+ { 19, 4, 2 },
+ { 23, 4, 2 },
+ { 27, 4, 2 },
+ { 31, 4, 2 },
+ { 35, 8, 3 },
+ { 43, 8, 3 },
+ { 51, 8, 3 },
+ { 59, 8, 3 },
+ { 67, 16, 4 },
+ { 83, 16, 4 },
+ { 99, 16, 4 },
+ { 115, 16, 4 },
+ { 131, 32, 5 },
+ { 163, 32, 5 },
+ { 195, 32, 5 },
+ { 227, 32, 5 },
+ { 258, 1, 0 }
+ // clang-format on
+};
+
+struct DistBase
+{
+ unsigned int base;
+ unsigned int range;
+ unsigned int bits;
+};
+
+DistBase distBases[] =
+{
+ // clang-format off
+ { 1, 1, 0 },
+ { 2, 1, 0 },
+ { 3, 1, 0 },
+ { 4, 1, 0 },
+ { 5, 2, 1 },
+ { 7, 2, 1 },
+ { 9, 4, 2 },
+ { 13, 4, 2 },
+ { 17, 8, 3 },
+ { 25, 8, 3 },
+ { 33, 16, 4 },
+ { 49, 16, 4 },
+ { 65, 32, 5 },
+ { 97, 32, 5 },
+ { 129, 64, 6 },
+ { 193, 64, 6 },
+ { 257, 128, 7 },
+ { 385, 128, 7 },
+ { 513, 256, 8 },
+ { 769, 256, 8 },
+ { 1025, 512, 9 },
+ { 1537, 512, 9 },
+ { 2049, 1024, 10 },
+ { 3073, 1024, 10 },
+ { 4097, 2048, 11 },
+ { 6145, 2048, 11 },
+ { 8193, 4096, 12 },
+ { 12289, 4096, 12 },
+ { 16385, 8192, 13 },
+ { 24577, 8192, 13 }
+ // clang-format on
+};
+
+void Deflater::encodeDistStatic(unsigned int len, unsigned int dist)
+{
+
+ //## Output length
+
+ if (len < 3 || len > 258)
+ {
+ error("Length out of range:%d", len);
+ return;
+ }
+
+ bool found = false;
+ for (int i=0 ; i<29 ; i++)
+ {
+ unsigned int base = lenBases[i].base;
+ unsigned int range = lenBases[i].range;
+ if (base + range > len)
+ {
+ unsigned int lenCode = 257 + i;
+ unsigned int length = len - base;
+ //trace("--- %d %d %d %d", len, base, range, length);
+ encodeLiteralStatic(lenCode);
+ putBits(length, lenBases[i].bits);
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ error("Length not found in table:%d", len);
+ return;
+ }
+
+ //## Output distance
+
+ if (dist < 4 || dist > 32768)
+ {
+ error("Distance out of range:%d", dist);
+ return;
+ }
+
+ found = false;
+ for (int i=0 ; i<30 ; i++)
+ {
+ unsigned int base = distBases[i].base;
+ unsigned int range = distBases[i].range;
+ if (base + range > dist)
+ {
+ unsigned int distCode = i;
+ unsigned int distance = dist - base;
+ //error("--- %d %d %d %d", dist, base, range, distance);
+ putBitsR(distCode, 5);
+ putBits(distance, distBases[i].bits);
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ error("Distance not found in table:%d", dist);
+ return;
+ }
+}
+
+
+//#############################
+//# C O M P R E S S
+//#############################
+
+
+/**
+ * This method does the dirty work of dictionary
+ * compression. Basically it looks for redundant
+ * strings and has the current duplicate refer back
+ * to the previous one.
+ */
+bool Deflater::compressWindow()
+{
+ windowPos = 0;
+ unsigned int windowSize = window.size();
+ //### Compress as much of the window as possible
+
+ unsigned int hash = 0;
+ //Have each value be a long with the byte at this position,
+ //plus the 3 bytes after it in the window
+ for (int i=windowSize-1 ; i>=0 ; i--)
+ {
+ unsigned char ch = window[i];
+ windowBuf[i] = ch;
+ hash = ((hash<<8) & 0xffffff00) | ch;
+ windowHashBuf[i] = hash;
+ }
+
+ while (windowPos < windowSize - 3)
+ {
+ //### Find best match, if any
+ unsigned int bestMatchLen = 0;
+ unsigned int bestMatchDist = 0;
+ if (windowPos >= 4)
+ {
+ for (unsigned int lookBack=0 ; lookBack<windowPos-4 ; lookBack++)
+ {
+ //Check 4-char hashes first, before continuing with string
+ if (windowHashBuf[lookBack] == windowHashBuf[windowPos])
+ {
+ unsigned int lookAhead=4;
+ unsigned int lookAheadMax = windowSize - 4 - windowPos;
+ if (lookBack + lookAheadMax >= windowPos -4 )
+ lookAheadMax = windowPos - 4 - lookBack;
+ if (lookAheadMax > 258)
+ lookAheadMax = 258;
+ unsigned char *wp = &(windowBuf[windowPos+4]);
+ unsigned char *lb = &(windowBuf[lookBack+4]);
+ while (lookAhead<lookAheadMax)
+ {
+ if (*lb++ != *wp++)
+ break;
+ lookAhead++;
+ }
+ if (lookAhead > bestMatchLen)
+ {
+ bestMatchLen = lookAhead;
+ bestMatchDist = windowPos - lookBack;
+ }
+ }
+ }
+ }
+ if (bestMatchLen > 3)
+ {
+ //Distance encode
+ //trace("### distance");
+ /*
+ printf("### 1 '");
+ for (int i=0 ; i < bestMatchLen ; i++)
+ fputc(window[windowPos+i], stdout);
+ printf("'\n### 2 '");
+ for (int i=0 ; i < bestMatchLen ; i++)
+ fputc(window[windowPos-bestMatchDist+i], stdout);
+ printf("'\n");
+ */
+ encodeDistStatic(bestMatchLen, bestMatchDist);
+ windowPos += bestMatchLen;
+ }
+ else
+ {
+ //Literal encode
+ //trace("### literal");
+ encodeLiteralStatic(windowBuf[windowPos]);
+ windowPos++;
+ }
+ }
+
+ while (windowPos < windowSize)
+ encodeLiteralStatic(windowBuf[windowPos++]);
+
+ encodeLiteralStatic(256);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool Deflater::compress()
+{
+ //trace("compress");
+ unsigned long total = 0L;
+ windowPos = 0;
+ std::vector<unsigned char>::iterator iter;
+ for (iter = uncompressed.begin(); iter != uncompressed.end() ; )
+ {
+ total += windowPos;
+ trace("total:%ld", total);
+ if (windowPos > window.size())
+ windowPos = window.size();
+ window.erase(window.begin() , window.begin()+windowPos);
+ while (window.size() < 32768 && iter != uncompressed.end())
+ {
+ window.push_back(*iter);
+ ++iter;
+ }
+ if (window.size() >= 32768)
+ putBits(0x00, 1); //0 -- more blocks
+ else
+ putBits(0x01, 1); //1 -- last block
+ putBits(0x01, 2); //01 -- static trees
+ if (!compressWindow())
+ return false;
+ }
+ putFlush();
+ return true;
+}
+
+
+
+
+
+//########################################################################
+//# G Z I P F I L E
+//########################################################################
+
+/**
+ * Constructor
+ */
+GzipFile::GzipFile() :
+ data(),
+ fileName(),
+ fileBuf(),
+ fileBufPos(0),
+ compressionMethod(0)
+{
+}
+
+/**
+ * Destructor
+ */
+GzipFile::~GzipFile()
+= default;
+
+/**
+ * Print error messages
+ */
+void GzipFile::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "GzipFile error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ * Print trace messages
+ */
+void GzipFile::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "GzipFile:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+/**
+ *
+ */
+void GzipFile::put(unsigned char ch)
+{
+ data.push_back(ch);
+}
+
+/**
+ *
+ */
+void GzipFile::setData(const std::vector<unsigned char> &str)
+{
+ data = str;
+}
+
+/**
+ *
+ */
+void GzipFile::clearData()
+{
+ data.clear();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &GzipFile::getData()
+{
+ return data;
+}
+
+/**
+ *
+ */
+std::string &GzipFile::getFileName()
+{
+ return fileName;
+}
+
+/**
+ *
+ */
+void GzipFile::setFileName(const std::string &val)
+{
+ fileName = val;
+}
+
+
+
+//#####################################
+//# U T I L I T Y
+//#####################################
+
+/**
+ * Loads a new file into an existing GzipFile
+ */
+bool GzipFile::loadFile(const std::string &fName)
+{
+ FILE *f = fopen(fName.c_str() , "rb");
+ if (!f)
+ {
+ error("Cannot open file %s", fName.c_str());
+ return false;
+ }
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ data.push_back(ch);
+ }
+ fclose(f);
+ setFileName(fName);
+ return true;
+}
+
+
+
+//#####################################
+//# W R I T E
+//#####################################
+
+/**
+ *
+ */
+bool GzipFile::putByte(unsigned char ch)
+{
+ fileBuf.push_back(ch);
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::putLong(unsigned long val)
+{
+ fileBuf.push_back( (unsigned char)((val ) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>> 8) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>>16) & 0xff));
+ fileBuf.push_back( (unsigned char)((val>>24) & 0xff));
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::write()
+{
+ fileBuf.clear();
+
+ putByte(0x1f); //magic
+ putByte(0x8b); //magic
+ putByte( 8); //compression method
+ putByte(0x08); //flags. say we have a crc and file name
+
+ unsigned long ltime = (unsigned long) time(nullptr);
+ putLong(ltime);
+
+ //xfl
+ putByte(0);
+ //OS
+ putByte(0);
+
+ //file name
+ for (char i : fileName)
+ putByte(i);
+ putByte(0);
+
+
+ //compress
+ std::vector<unsigned char> compBuf;
+ Deflater deflater;
+ if (!deflater.deflate(compBuf, data))
+ {
+ return false;
+ }
+
+ std::vector<unsigned char>::iterator iter;
+ for (iter=compBuf.begin() ; iter!=compBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ putByte(ch);
+ }
+
+ Crc32 crcEngine;
+ crcEngine.update(data);
+ unsigned long crc = crcEngine.getValue();
+ putLong(crc);
+
+ putLong(data.size());
+
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::writeBuffer(std::vector<unsigned char> &outBuf)
+{
+ if (!write())
+ return false;
+ outBuf.clear();
+ outBuf = fileBuf;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::writeFile(const std::string &fileName)
+{
+ if (!write())
+ return false;
+ FILE *f = fopen(fileName.c_str(), "wb");
+ if (!f)
+ return false;
+ std::vector<unsigned char>::iterator iter;
+ for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ fputc(ch, f);
+ }
+ fclose(f);
+ return true;
+}
+
+
+//#####################################
+//# R E A D
+//#####################################
+
+bool GzipFile::getByte(unsigned char *ch)
+{
+ if (fileBufPos >= fileBuf.size())
+ {
+ error("unexpected end of data");
+ return false;
+ }
+ *ch = fileBuf[fileBufPos++];
+ return true;
+}
+
+/**
+ *
+ */
+bool GzipFile::getLong(unsigned long *val)
+{
+ if (fileBuf.size() - fileBufPos < 4)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ int ch3 = fileBuf[fileBufPos++];
+ int ch4 = fileBuf[fileBufPos++];
+ *val = ((ch4<<24) & 0xff000000L) |
+ ((ch3<<16) & 0x00ff0000L) |
+ ((ch2<< 8) & 0x0000ff00L) |
+ ((ch1 ) & 0x000000ffL);
+ return true;
+}
+
+bool GzipFile::read()
+{
+ fileBufPos = 0;
+
+ unsigned char ch;
+
+ //magic cookie
+ if (!getByte(&ch))
+ return false;
+ if (ch != 0x1f)
+ {
+ error("bad gzip header");
+ return false;
+ }
+ if (!getByte(&ch))
+ return false;
+ if (ch != 0x8b)
+ {
+ error("bad gzip header");
+ return false;
+ }
+
+ //## compression method
+ if (!getByte(&ch))
+ return false;
+ compressionMethod = ch & 0xff;
+
+ //## flags
+ if (!getByte(&ch))
+ return false;
+ //bool ftext = ch & 0x01;
+ bool fhcrc = ch & 0x02;
+ bool fextra = ch & 0x04;
+ bool fname = ch & 0x08;
+ bool fcomment = ch & 0x10;
+
+ //trace("cm:%d ftext:%d fhcrc:%d fextra:%d fname:%d fcomment:%d",
+ // cm, ftext, fhcrc, fextra, fname, fcomment);
+
+ //## file time
+ unsigned long ltime;
+ if (!getLong(&ltime))
+ return false;
+ //time_t mtime = (time_t)ltime;
+
+ //## XFL
+ if (!getByte(&ch))
+ return false;
+ //int xfl = ch;
+
+ //## OS
+ if (!getByte(&ch))
+ return false;
+ //int os = ch;
+
+ //std::string timestr = ctime(&mtime);
+ //trace("xfl:%d os:%d mtime:%s", xfl, os, timestr.c_str());
+
+ if (fextra)
+ {
+ if (!getByte(&ch))
+ return false;
+ long xlen = ch;
+ if (!getByte(&ch))
+ return false;
+ xlen = (xlen << 8) + ch;
+ for (long l=0 ; l<xlen ; l++)
+ {
+ if (!getByte(&ch))
+ return false;
+ }
+ }
+
+ if (fname)
+ {
+ fileName = "";
+ while (true)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (ch==0)
+ break;
+ fileName.push_back(ch);
+ }
+ }
+
+ if (fcomment)
+ {
+ while (true)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (ch==0)
+ break;
+ }
+ }
+
+ if (fhcrc)
+ {
+ if (!getByte(&ch))
+ return false;
+ if (!getByte(&ch))
+ return false;
+ }
+
+ //read remainder of stream
+ //compressed data runs up until 8 bytes before end of buffer
+ std::vector<unsigned char> compBuf;
+ while (fileBufPos < fileBuf.size() - 8)
+ {
+ if (!getByte(&ch))
+ return false;
+ compBuf.push_back(ch);
+ }
+ //uncompress
+ data.clear();
+ Inflater inflater;
+ if (!inflater.inflate(data, compBuf))
+ {
+ return false;
+ }
+
+ //Get the CRC and compare
+ Crc32 crcEngine;
+ crcEngine.update(data);
+ unsigned long calcCrc = crcEngine.getValue();
+ unsigned long givenCrc;
+ if (!getLong(&givenCrc))
+ return false;
+ if (givenCrc != calcCrc)
+ {
+ error("Specified crc, %ud, not what received: %ud",
+ givenCrc, calcCrc);
+ return false;
+ }
+
+ //Get the file size and compare
+ unsigned long givenFileSize;
+ if (!getLong(&givenFileSize))
+ return false;
+ if (givenFileSize != data.size())
+ {
+ error("Specified data size, %ld, not what received: %ld",
+ givenFileSize, data.size());
+ return false;
+ }
+
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool GzipFile::readBuffer(const std::vector<unsigned char> &inbuf)
+{
+ fileBuf = inbuf;
+ if (!read())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool GzipFile::readFile(const std::string &fileName)
+{
+ fileBuf.clear();
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ return false;
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ fileBuf.push_back(ch);
+ }
+ fclose(f);
+ if (!read())
+ return false;
+ return true;
+}
+
+
+
+
+
+
+
+
+//########################################################################
+//# Z I P F I L E
+//########################################################################
+
+/**
+ * Constructor
+ */
+ZipEntry::ZipEntry() :
+ crc (0L),
+ fileName (),
+ comment (),
+ compressionMethod (8),
+ compressedData (),
+ uncompressedData (),
+ position (0)
+{
+}
+
+/**
+ *
+ */
+ZipEntry::ZipEntry(std::string fileNameArg,
+ std::string commentArg) :
+ crc (0L),
+ fileName (std::move(fileNameArg)),
+ comment (std::move(commentArg)),
+ compressionMethod (8),
+ compressedData (),
+ uncompressedData (),
+ position (0)
+{
+}
+
+/**
+ * Destructor
+ */
+ZipEntry::~ZipEntry()
+= default;
+
+
+/**
+ *
+ */
+std::string ZipEntry::getFileName()
+{
+ return fileName;
+}
+
+/**
+ *
+ */
+void ZipEntry::setFileName(const std::string &val)
+{
+ fileName = val;
+}
+
+/**
+ *
+ */
+std::string ZipEntry::getComment()
+{
+ return comment;
+}
+
+/**
+ *
+ */
+void ZipEntry::setComment(const std::string &val)
+{
+ comment = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getCompressedSize()
+{
+ return (unsigned long)compressedData.size();
+}
+
+/**
+ *
+ */
+int ZipEntry::getCompressionMethod()
+{
+ return compressionMethod;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCompressionMethod(int val)
+{
+ compressionMethod = val;
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &ZipEntry::getCompressedData()
+{
+ return compressedData;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCompressedData(const std::vector<unsigned char> &val)
+{
+ compressedData = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getUncompressedSize()
+{
+ return (unsigned long)uncompressedData.size();
+}
+
+/**
+ *
+ */
+std::vector<unsigned char> &ZipEntry::getUncompressedData()
+{
+ return uncompressedData;
+}
+
+/**
+ *
+ */
+void ZipEntry::setUncompressedData(const std::vector<unsigned char> &val)
+{
+ uncompressedData = val;
+}
+
+void ZipEntry::setUncompressedData(const std::string &s)
+{
+ uncompressedData.clear();
+ uncompressedData.reserve(s.size());
+ uncompressedData.insert(uncompressedData.begin(), s.begin(), s.end());
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getCrc()
+{
+ return crc;
+}
+
+/**
+ *
+ */
+void ZipEntry::setCrc(unsigned long val)
+{
+ crc = val;
+}
+
+/**
+ *
+ */
+void ZipEntry::write(unsigned char ch)
+{
+ uncompressedData.push_back(ch);
+}
+
+/**
+ *
+ */
+void ZipEntry::finish()
+{
+ Crc32 c32;
+ std::vector<unsigned char>::iterator iter;
+ for (iter = uncompressedData.begin() ;
+ iter!= uncompressedData.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ c32.update(ch);
+ }
+ crc = c32.getValue();
+ switch (compressionMethod)
+ {
+ case 0: //none
+ {
+ for (iter = uncompressedData.begin() ;
+ iter!= uncompressedData.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ compressedData.push_back(ch);
+ }
+ break;
+ }
+ case 8: //deflate
+ {
+ Deflater deflater;
+ if (!deflater.deflate(compressedData, uncompressedData))
+ {
+ //some error
+ }
+ break;
+ }
+ default:
+ {
+ printf("error: unknown compression method %d\n",
+ compressionMethod);
+ }
+ }
+}
+
+
+
+
+/**
+ *
+ */
+bool ZipEntry::readFile(const std::string &fileNameArg,
+ const std::string &commentArg)
+{
+ crc = 0L;
+ uncompressedData.clear();
+ fileName = fileNameArg;
+ comment = commentArg;
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ {
+ return false;
+ }
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ uncompressedData.push_back((unsigned char)ch);
+ }
+ fclose(f);
+ finish();
+ return true;
+}
+
+
+/**
+ *
+ */
+void ZipEntry::setPosition(unsigned long val)
+{
+ position = val;
+}
+
+/**
+ *
+ */
+unsigned long ZipEntry::getPosition()
+{
+ return position;
+}
+
+
+
+
+
+
+
+/**
+ * Constructor
+ */
+ZipFile::ZipFile() :
+ entries(),
+ fileBuf(),
+ fileBufPos(0),
+ comment()
+{
+}
+
+/**
+ * Destructor
+ */
+ZipFile::~ZipFile()
+{
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter=entries.begin() ; iter!=entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ delete entry;
+ }
+ entries.clear();
+}
+
+/**
+ *
+ */
+void ZipFile::setComment(const std::string &val)
+{
+ comment = val;
+}
+
+/**
+ *
+ */
+std::string ZipFile::getComment()
+{
+ return comment;
+}
+
+
+/**
+ *
+ */
+std::vector<ZipEntry *> &ZipFile::getEntries()
+{
+ return entries;
+}
+
+
+
+//#####################################
+//# M E S S A G E S
+//#####################################
+
+void ZipFile::error(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "ZipFile error:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+void ZipFile::trace(char const *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stdout, "ZipFile:");
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+ va_end(args);
+}
+
+//#####################################
+//# U T I L I T Y
+//#####################################
+
+/**
+ *
+ */
+ZipEntry *ZipFile::addFile(const std::string &fileName,
+ const std::string &comment)
+{
+ ZipEntry *ze = new ZipEntry();
+ if (!ze->readFile(fileName, comment))
+ {
+ delete ze;
+ return nullptr;
+ }
+ entries.push_back(ze);
+ return ze;
+}
+
+
+/**
+ *
+ */
+ZipEntry *ZipFile::newEntry(const std::string &fileName,
+ const std::string &comment)
+{
+ ZipEntry *ze = new ZipEntry(fileName, comment);
+ entries.push_back(ze);
+ return ze;
+}
+
+
+//#####################################
+//# W R I T E
+//#####################################
+
+/**
+ *
+ */
+bool ZipFile::putLong(unsigned long val)
+{
+ fileBuf.push_back( ((int)(val )) & 0xff);
+ fileBuf.push_back( ((int)(val>> 8)) & 0xff);
+ fileBuf.push_back( ((int)(val>>16)) & 0xff);
+ fileBuf.push_back( ((int)(val>>24)) & 0xff);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::putInt(unsigned int val)
+{
+ fileBuf.push_back( (val ) & 0xff);
+ fileBuf.push_back( (val>> 8) & 0xff);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::putByte(unsigned char val)
+{
+ fileBuf.push_back(val);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::writeFileData()
+{
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter = entries.begin() ; iter != entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ entry->setPosition(fileBuf.size());
+ //##### HEADER
+ std::string fname = entry->getFileName();
+ putLong(0x04034b50L);
+ putInt(20); //versionNeeded
+ putInt(0); //gpBitFlag
+ //putInt(0); //compression method
+ putInt(entry->getCompressionMethod()); //compression method
+ putInt(0); //mod time
+ putInt(0); //mod date
+ putLong(entry->getCrc()); //crc32
+ putLong(entry->getCompressedSize());
+ putLong(entry->getUncompressedSize());
+ putInt(fname.size());//fileName length
+ putInt(8);//extra field length
+ //file name
+ for (char i : fname)
+ putByte((unsigned char)i);
+ //extra field
+ putInt(0x7855);
+ putInt(4);
+ putInt(100);
+ putInt(100);
+
+ //##### DATA
+ std::vector<unsigned char> &buf = entry->getCompressedData();
+ std::vector<unsigned char>::iterator iter;
+ for (iter = buf.begin() ; iter != buf.end() ; ++iter)
+ {
+ unsigned char ch = (unsigned char) *iter;
+ putByte(ch);
+ }
+ }
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::writeCentralDirectory()
+{
+ unsigned long cdPosition = fileBuf.size();
+ std::vector<ZipEntry *>::iterator iter;
+ for (iter = entries.begin() ; iter != entries.end() ; ++iter)
+ {
+ ZipEntry *entry = *iter;
+ std::string fname = entry->getFileName();
+ std::string ecomment = entry->getComment();
+ putLong(0x02014b50L); //magic cookie
+ putInt(2386); //versionMadeBy
+ putInt(20); //versionNeeded
+ putInt(0); //gpBitFlag
+ putInt(entry->getCompressionMethod()); //compression method
+ putInt(0); //mod time
+ putInt(0); //mod date
+ putLong(entry->getCrc()); //crc32
+ putLong(entry->getCompressedSize());
+ putLong(entry->getUncompressedSize());
+ putInt(fname.size());//fileName length
+ putInt(4);//extra field length
+ putInt(ecomment.size());//comment length
+ putInt(0); //disk number start
+ putInt(0); //internal attributes
+ putLong(0); //external attributes
+ putLong(entry->getPosition());
+
+ //file name
+ for (char i : fname)
+ putByte((unsigned char)i);
+ //extra field
+ putInt(0x7855);
+ putInt(0);
+ //comment
+ for (char i : ecomment)
+ putByte((unsigned char)i);
+ }
+ unsigned long cdSize = fileBuf.size() - cdPosition;
+
+ putLong(0x06054b50L);
+ putInt(0);//number of this disk
+ putInt(0);//nr of disk with central dir
+ putInt(entries.size()); //number of entries on this disk
+ putInt(entries.size()); //number of entries total
+ putLong(cdSize); //size of central dir
+ putLong(cdPosition); //position of central dir
+ putInt(comment.size());//comment size
+ for (char i : comment)
+ putByte(i);
+ return true;
+}
+
+
+
+/**
+ *
+ */
+bool ZipFile::write()
+{
+ fileBuf.clear();
+ if (!writeFileData())
+ return false;
+ if (!writeCentralDirectory())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::writeBuffer(std::vector<unsigned char> &outBuf)
+{
+ if (!write())
+ return false;
+ outBuf.clear();
+ outBuf = fileBuf;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::writeFile(const std::string &fileName)
+{
+ if (!write())
+ return false;
+ FILE *f = fopen(fileName.c_str(), "wb");
+ if (!f)
+ return false;
+ std::vector<unsigned char>::iterator iter;
+ for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter)
+ {
+ unsigned char ch = *iter;
+ fputc(ch, f);
+ }
+ fclose(f);
+ return true;
+}
+
+//#####################################
+//# R E A D
+//#####################################
+
+/**
+ *
+ */
+bool ZipFile::getLong(unsigned long *val)
+{
+ if (fileBuf.size() - fileBufPos < 4)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ int ch3 = fileBuf[fileBufPos++];
+ int ch4 = fileBuf[fileBufPos++];
+ *val = ((ch4<<24) & 0xff000000L) |
+ ((ch3<<16) & 0x00ff0000L) |
+ ((ch2<< 8) & 0x0000ff00L) |
+ ((ch1 ) & 0x000000ffL);
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::getInt(unsigned int *val)
+{
+ if (fileBuf.size() - fileBufPos < 2)
+ return false;
+ int ch1 = fileBuf[fileBufPos++];
+ int ch2 = fileBuf[fileBufPos++];
+ *val = ((ch2<< 8) & 0xff00) |
+ ((ch1 ) & 0x00ff);
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::getByte(unsigned char *val)
+{
+ if (fileBuf.size() <= fileBufPos)
+ return false;
+ *val = fileBuf[fileBufPos++];
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readFileData()
+{
+ //printf("#################################################\n");
+ //printf("###D A T A\n");
+ //printf("#################################################\n");
+ while (true)
+ {
+ unsigned long magicCookie;
+ if (!getLong(&magicCookie))
+ {
+ error("magic cookie not found");
+ break;
+ }
+ trace("###Cookie:%lx", magicCookie);
+ if (magicCookie == 0x02014b50L) //central directory
+ break;
+ if (magicCookie != 0x04034b50L)
+ {
+ error("file header not found");
+ return false;
+ }
+ unsigned int versionNeeded;
+ if (!getInt(&versionNeeded))
+ {
+ error("bad version needed found");
+ return false;
+ }
+ unsigned int gpBitFlag;
+ if (!getInt(&gpBitFlag))
+ {
+ error("bad bit flag found");
+ return false;
+ }
+ unsigned int compressionMethod;
+ if (!getInt(&compressionMethod))
+ {
+ error("bad compressionMethod found");
+ return false;
+ }
+ unsigned int modTime;
+ if (!getInt(&modTime))
+ {
+ error("bad modTime found");
+ return false;
+ }
+ unsigned int modDate;
+ if (!getInt(&modDate))
+ {
+ error("bad modDate found");
+ return false;
+ }
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ unsigned int fileNameLength;
+ if (!getInt(&fileNameLength))
+ {
+ error("bad fileNameLength found");
+ return false;
+ }
+ unsigned int extraFieldLength;
+ if (!getInt(&extraFieldLength))
+ {
+ error("bad extraFieldLength found");
+ return false;
+ }
+ std::string fileName;
+ for (unsigned int i=0 ; i<fileNameLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileName.push_back(ch);
+ }
+ std::string extraField;
+ for (unsigned int i=0 ; i<extraFieldLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ extraField.push_back(ch);
+ }
+ trace("######################### DATA");
+ trace("FileName :%d:%s" , fileName.size(), fileName.c_str());
+ trace("Extra field :%d:%s" , extraField.size(), extraField.c_str());
+ trace("Version needed :%d" , versionNeeded);
+ trace("Bitflag :%d" , gpBitFlag);
+ trace("Compression Method :%d" , compressionMethod);
+ trace("Mod time :%d" , modTime);
+ trace("Mod date :%d" , modDate);
+ trace("CRC :%lx", crc32);
+ trace("Compressed size :%ld", compressedSize);
+ trace("Uncompressed size :%ld", uncompressedSize);
+
+ //#### Uncompress the data
+ std::vector<unsigned char> compBuf;
+ if (gpBitFlag & 0x8)//bit 3 was set. means we don't know compressed size
+ {
+ unsigned char c1, c2, c3, c4;
+ c2 = c3 = c4 = 0;
+ while (true)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ {
+ error("premature end of data");
+ break;
+ }
+ compBuf.push_back(ch);
+ c1 = c2; c2 = c3; c3 = c4; c4 = ch;
+ if (c1 == 0x50 && c2 == 0x4b && c3 == 0x07 && c4 == 0x08)
+ {
+ trace("found end of compressed data");
+ //remove the cookie
+ compBuf.erase(compBuf.end() -4, compBuf.end());
+ break;
+ }
+ }
+ }
+ else
+ {
+ for (unsigned long bnr = 0 ; bnr < compressedSize ; bnr++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ {
+ error("premature end of data");
+ break;
+ }
+ compBuf.push_back(ch);
+ }
+ }
+
+ printf("### data: ");
+ for (int i=0 ; i<10 ; i++)
+ printf("%02x ", compBuf[i] & 0xff);
+ printf("\n");
+
+ if (gpBitFlag & 0x8)//only if bit 3 set
+ {
+ /* this cookie was read in the loop above
+ unsigned long dataDescriptorSignature ;
+ if (!getLong(&dataDescriptorSignature))
+ break;
+ if (dataDescriptorSignature != 0x08074b50L)
+ {
+ error("bad dataDescriptorSignature found");
+ return false;
+ }
+ */
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ }//bit 3 was set
+ //break;
+
+ std::vector<unsigned char> uncompBuf;
+ switch (compressionMethod)
+ {
+ case 8: //deflate
+ {
+ Inflater inflater;
+ if (!inflater.inflate(uncompBuf, compBuf))
+ {
+ return false;
+ }
+ break;
+ }
+ default:
+ {
+ error("Unimplemented compression method %d", compressionMethod);
+ return false;
+ }
+ }
+
+ if (uncompressedSize != uncompBuf.size())
+ {
+ error("Size mismatch. Expected %ld, received %ld",
+ uncompressedSize, uncompBuf.size());
+ return false;
+ }
+
+ Crc32 crcEngine;
+ crcEngine.update(uncompBuf);
+ unsigned long crc = crcEngine.getValue();
+ if (crc != crc32)
+ {
+ error("Crc mismatch. Calculated %08ux, received %08ux", crc, crc32);
+ return false;
+ }
+
+ ZipEntry *ze = new ZipEntry(fileName, comment);
+ ze->setCompressionMethod(compressionMethod);
+ ze->setCompressedData(compBuf);
+ ze->setUncompressedData(uncompBuf);
+ ze->setCrc(crc);
+ entries.push_back(ze);
+
+
+ }
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readCentralDirectory()
+{
+ //printf("#################################################\n");
+ //printf("###D I R E C T O R Y\n");
+ //printf("#################################################\n");
+ while (true)
+ {
+ //We start with a central directory cookie already
+ //Check at the bottom of the loop.
+ unsigned int version;
+ if (!getInt(&version))
+ {
+ error("bad version found");
+ return false;
+ }
+ unsigned int versionNeeded;
+ if (!getInt(&versionNeeded))
+ {
+ error("bad version found");
+ return false;
+ }
+ unsigned int gpBitFlag;
+ if (!getInt(&gpBitFlag))
+ {
+ error("bad bit flag found");
+ return false;
+ }
+ unsigned int compressionMethod;
+ if (!getInt(&compressionMethod))
+ {
+ error("bad compressionMethod found");
+ return false;
+ }
+ unsigned int modTime;
+ if (!getInt(&modTime))
+ {
+ error("bad modTime found");
+ return false;
+ }
+ unsigned int modDate;
+ if (!getInt(&modDate))
+ {
+ error("bad modDate found");
+ return false;
+ }
+ unsigned long crc32;
+ if (!getLong(&crc32))
+ {
+ error("bad crc32 found");
+ return false;
+ }
+ unsigned long compressedSize;
+ if (!getLong(&compressedSize))
+ {
+ error("bad compressedSize found");
+ return false;
+ }
+ unsigned long uncompressedSize;
+ if (!getLong(&uncompressedSize))
+ {
+ error("bad uncompressedSize found");
+ return false;
+ }
+ unsigned int fileNameLength;
+ if (!getInt(&fileNameLength))
+ {
+ error("bad fileNameLength found");
+ return false;
+ }
+ unsigned int extraFieldLength;
+ if (!getInt(&extraFieldLength))
+ {
+ error("bad extraFieldLength found");
+ return false;
+ }
+ unsigned int fileCommentLength;
+ if (!getInt(&fileCommentLength))
+ {
+ error("bad fileCommentLength found");
+ return false;
+ }
+ unsigned int diskNumberStart;
+ if (!getInt(&diskNumberStart))
+ {
+ error("bad diskNumberStart found");
+ return false;
+ }
+ unsigned int internalFileAttributes;
+ if (!getInt(&internalFileAttributes))
+ {
+ error("bad internalFileAttributes found");
+ return false;
+ }
+ unsigned long externalFileAttributes;
+ if (!getLong(&externalFileAttributes))
+ {
+ error("bad externalFileAttributes found");
+ return false;
+ }
+ unsigned long localHeaderOffset;
+ if (!getLong(&localHeaderOffset))
+ {
+ error("bad localHeaderOffset found");
+ return false;
+ }
+ std::string fileName;
+ for (unsigned int i=0 ; i<fileNameLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileName.push_back(ch);
+ }
+ std::string extraField;
+ for (unsigned int i=0 ; i<extraFieldLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ extraField.push_back(ch);
+ }
+ std::string fileComment;
+ for (unsigned int i=0 ; i<fileCommentLength ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ fileComment.push_back(ch);
+ }
+ trace("######################### ENTRY");
+ trace("FileName :%s" , fileName.c_str());
+ trace("Extra field :%s" , extraField.c_str());
+ trace("File comment :%s" , fileComment.c_str());
+ trace("Version :%d" , version);
+ trace("Version needed :%d" , versionNeeded);
+ trace("Bitflag :%d" , gpBitFlag);
+ trace("Compression Method :%d" , compressionMethod);
+ trace("Mod time :%d" , modTime);
+ trace("Mod date :%d" , modDate);
+ trace("CRC :%lx", crc32);
+ trace("Compressed size :%ld", compressedSize);
+ trace("Uncompressed size :%ld", uncompressedSize);
+ trace("Disk nr start :%ld", diskNumberStart);
+ trace("Header offset :%ld", localHeaderOffset);
+
+
+ unsigned long magicCookie;
+ if (!getLong(&magicCookie))
+ {
+ error("magic cookie not found");
+ return false;
+ }
+ trace("###Cookie:%lx", magicCookie);
+ if (magicCookie == 0x06054b50L) //end of central directory
+ break;
+ else if (magicCookie == 0x05054b50L) //signature
+ {
+ //## Digital Signature
+ unsigned int signatureSize;
+ if (!getInt(&signatureSize))
+ {
+ error("bad signatureSize found");
+ return false;
+ }
+ std::string digitalSignature;
+ for (unsigned int i=0 ; i<signatureSize ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ digitalSignature.push_back(ch);
+ }
+ trace("######## SIGNATURE :'%s'" , digitalSignature.c_str());
+ }
+ else if (magicCookie != 0x02014b50L) //central directory
+ {
+ error("directory file header not found");
+ return false;
+ }
+ }
+
+ unsigned int diskNr;
+ if (!getInt(&diskNr))
+ {
+ error("bad diskNr found");
+ return false;
+ }
+ unsigned int diskWithCd;
+ if (!getInt(&diskWithCd))
+ {
+ error("bad diskWithCd found");
+ return false;
+ }
+ unsigned int nrEntriesDisk;
+ if (!getInt(&nrEntriesDisk))
+ {
+ error("bad nrEntriesDisk found");
+ return false;
+ }
+ unsigned int nrEntriesTotal;
+ if (!getInt(&nrEntriesTotal))
+ {
+ error("bad nrEntriesTotal found");
+ return false;
+ }
+ unsigned long cdSize;
+ if (!getLong(&cdSize))
+ {
+ error("bad cdSize found");
+ return false;
+ }
+ unsigned long cdPos;
+ if (!getLong(&cdPos))
+ {
+ error("bad cdPos found");
+ return false;
+ }
+ unsigned int commentSize;
+ if (!getInt(&commentSize))
+ {
+ error("bad commentSize found");
+ return false;
+ }
+ comment = "";
+ for (unsigned int i=0 ; i<commentSize ; i++)
+ {
+ unsigned char ch;
+ if (!getByte(&ch))
+ break;
+ comment.push_back(ch);
+ }
+ trace("######## Zip Comment :'%s'" , comment.c_str());
+
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::read()
+{
+ fileBufPos = 0;
+ if (!readFileData())
+ {
+ return false;
+ }
+ if (!readCentralDirectory())
+ {
+ return false;
+ }
+ return true;
+}
+
+/**
+ *
+ */
+bool ZipFile::readBuffer(const std::vector<unsigned char> &inbuf)
+{
+ fileBuf = inbuf;
+ if (!read())
+ return false;
+ return true;
+}
+
+
+/**
+ *
+ */
+bool ZipFile::readFile(const std::string &fileName)
+{
+ fileBuf.clear();
+ FILE *f = fopen(fileName.c_str(), "rb");
+ if (!f)
+ return false;
+ while (true)
+ {
+ int ch = fgetc(f);
+ if (ch < 0)
+ break;
+ fileBuf.push_back(ch);
+ }
+ fclose(f);
+ if (!read())
+ return false;
+ return true;
+}
+
+
+
+
+
+
+
+
+
+//########################################################################
+//# E N D O F F I L E
+//########################################################################
+
+
diff --git a/src/util/ziptool.h b/src/util/ziptool.h
new file mode 100644
index 0000000..26aaac8
--- /dev/null
+++ b/src/util/ziptool.h
@@ -0,0 +1,575 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/** @file
+ * TODO: insert short description here
+ *//*
+ * Authors:
+ * see git history
+ * Bob Jamison
+ *
+ * Copyright (C) 2018 Authors
+ * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information.
+ */
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#ifndef SEEN_ZIPTOOL_H
+#define SEEN_ZIPTOOL_H
+/**
+ * This is intended to be a standalone, reduced capability
+ * implementation of Gzip and Zip functionality. Its
+ * targeted use case is for archiving and retrieving single files
+ * which use these encoding types. Being memory based and
+ * non-optimized, it is not useful in cases where very large
+ * archives are needed or where high performance is desired.
+ * However, it should hopefully work well for smaller,
+ * one-at-a-time tasks. What you get in return is the ability
+ * to drop these files into your project and remove the dependencies
+ * on ZLib and Info-Zip. Enjoy.
+ */
+
+
+
+#include <vector>
+#include <string>
+
+
+//########################################################################
+//# A D L E R 3 2
+//########################################################################
+
+class Adler32
+{
+public:
+
+ Adler32();
+
+ virtual ~Adler32();
+
+ void reset();
+
+ void update(unsigned char b);
+
+ void update(char *str);
+
+ unsigned long getValue();
+
+private:
+
+ unsigned long value;
+
+};
+
+
+//########################################################################
+//# C R C 3 2
+//########################################################################
+
+class Crc32
+{
+public:
+
+ Crc32();
+
+ virtual ~Crc32();
+
+ void reset();
+
+ void update(unsigned char b);
+
+ void update(char *str);
+
+ void update(const std::vector<unsigned char> &buf);
+
+ unsigned long getValue();
+
+private:
+
+ unsigned long value;
+
+};
+
+
+
+
+
+
+//########################################################################
+//# G Z I P S T R E A M S
+//########################################################################
+
+class GzipFile
+{
+public:
+
+ /**
+ *
+ */
+ GzipFile();
+
+ /**
+ *
+ */
+ virtual ~GzipFile();
+
+ /**
+ *
+ */
+ virtual void put(unsigned char ch);
+
+ /**
+ *
+ */
+ virtual void setData(const std::vector<unsigned char> &str);
+
+ /**
+ *
+ */
+ virtual void clearData();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getData();
+
+ /**
+ *
+ */
+ virtual std::string &getFileName();
+
+ /**
+ *
+ */
+ virtual void setFileName(const std::string &val);
+
+
+ //######################
+ //# U T I L I T Y
+ //######################
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fName);
+
+ //######################
+ //# W R I T E
+ //######################
+
+ /**
+ *
+ */
+ virtual bool write();
+
+ /**
+ *
+ */
+ virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
+
+ /**
+ *
+ */
+ virtual bool writeFile(const std::string &fileName);
+
+
+ //######################
+ //# R E A D
+ //######################
+
+
+ /**
+ *
+ */
+ virtual bool read();
+
+ /**
+ *
+ */
+ virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
+
+ /**
+ *
+ */
+ virtual bool loadFile(const std::string &fileName);
+
+
+
+private:
+
+ std::vector<unsigned char> data;
+ std::string fileName;
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ std::vector<unsigned char> fileBuf;
+ unsigned long fileBufPos;
+
+ bool getByte(unsigned char *ch);
+ bool getLong(unsigned long *val);
+
+ bool putByte(unsigned char ch);
+ bool putLong(unsigned long val);
+
+ int compressionMethod;
+};
+
+
+
+
+//########################################################################
+//# Z I P F I L E
+//########################################################################
+
+
+/**
+ *
+ */
+class ZipEntry
+{
+public:
+
+ /**
+ *
+ */
+ ZipEntry();
+
+ /**
+ *
+ */
+ ZipEntry(std::string fileName,
+ std::string comment);
+
+ /**
+ *
+ */
+ virtual ~ZipEntry();
+
+ /**
+ *
+ */
+ virtual std::string getFileName();
+
+ /**
+ *
+ */
+ virtual void setFileName(const std::string &val);
+
+ /**
+ *
+ */
+ virtual std::string getComment();
+
+ /**
+ *
+ */
+ virtual void setComment(const std::string &val);
+
+ /**
+ *
+ */
+ virtual unsigned long getCompressedSize();
+
+ /**
+ *
+ */
+ virtual int getCompressionMethod();
+
+ /**
+ *
+ */
+ virtual void setCompressionMethod(int val);
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getCompressedData();
+
+ /**
+ *
+ */
+ virtual void setCompressedData(const std::vector<unsigned char> &val);
+
+ /**
+ *
+ */
+ virtual unsigned long getUncompressedSize();
+
+ /**
+ *
+ */
+ virtual std::vector<unsigned char> &getUncompressedData();
+
+ /**
+ *
+ */
+ virtual void setUncompressedData(const std::vector<unsigned char> &val);
+ virtual void setUncompressedData(const std::string &val);
+
+ /**
+ *
+ */
+ virtual void write(unsigned char ch);
+
+ /**
+ *
+ */
+ virtual void finish();
+
+ /**
+ *
+ */
+ virtual unsigned long getCrc();
+
+ /**
+ *
+ */
+ virtual void setCrc(unsigned long crc);
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ /**
+ *
+ */
+ virtual void setPosition(unsigned long val);
+
+ /**
+ *
+ */
+ virtual unsigned long getPosition();
+
+private:
+
+ unsigned long crc;
+
+ std::string fileName;
+ std::string comment;
+
+ int compressionMethod;
+
+ std::vector<unsigned char> compressedData;
+ std::vector<unsigned char> uncompressedData;
+
+ unsigned long position;
+};
+
+
+
+
+
+
+
+
+
+/**
+ * This class sits over the zlib and gzip code to
+ * implement a PKWare or Info-Zip .zip file reader and
+ * writer
+ */
+class ZipFile
+{
+public:
+
+ /**
+ *
+ */
+ ZipFile();
+
+ /**
+ *
+ */
+ virtual ~ZipFile();
+
+ //######################
+ //# V A R I A B L E S
+ //######################
+
+ /**
+ *
+ */
+ virtual void setComment(const std::string &val);
+
+ /**
+ *
+ */
+ virtual std::string getComment();
+
+ /**
+ * Return the list of entries currently in this file
+ */
+ std::vector<ZipEntry *> &getEntries();
+
+
+ //######################
+ //# U T I L I T Y
+ //######################
+
+ /**
+ *
+ */
+ virtual ZipEntry *addFile(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ /**
+ *
+ */
+ virtual ZipEntry *newEntry(const std::string &fileNameArg,
+ const std::string &commentArg);
+
+ //######################
+ //# W R I T E
+ //######################
+
+ /**
+ *
+ */
+ virtual bool write();
+
+ /**
+ *
+ */
+ virtual bool writeBuffer(std::vector<unsigned char> &outbuf);
+
+ /**
+ *
+ */
+ virtual bool writeFile(const std::string &fileName);
+
+
+ //######################
+ //# R E A D
+ //######################
+
+
+ /**
+ *
+ */
+ virtual bool read();
+
+ /**
+ *
+ */
+ virtual bool readBuffer(const std::vector<unsigned char> &inbuf);
+
+ /**
+ *
+ */
+ virtual bool readFile(const std::string &fileName);
+
+
+private:
+
+ //debug messages
+ void error(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+ void trace(char const *fmt, ...)
+ #ifdef G_GNUC_PRINTF
+ G_GNUC_PRINTF(2, 3)
+ #endif
+ ;
+
+ //# Private writing methods
+
+ /**
+ *
+ */
+ bool putLong(unsigned long val);
+
+ /**
+ *
+ */
+ bool putInt(unsigned int val);
+
+
+ /**
+ *
+ */
+ bool putByte(unsigned char val);
+
+ /**
+ *
+ */
+ bool writeFileData();
+
+ /**
+ *
+ */
+ bool writeCentralDirectory();
+
+
+ //# Private reading methods
+
+ /**
+ *
+ */
+ bool getLong(unsigned long *val);
+
+ /**
+ *
+ */
+ bool getInt(unsigned int *val);
+
+ /**
+ *
+ */
+ bool getByte(unsigned char *val);
+
+ /**
+ *
+ */
+ bool readFileData();
+
+ /**
+ *
+ */
+ bool readCentralDirectory();
+
+
+ std::vector<ZipEntry *> entries;
+
+ std::vector<unsigned char> fileBuf;
+ unsigned long fileBufPos;
+
+ std::string comment;
+};
+
+
+
+
+
+
+#endif // SEEN_ZIPTOOL_H
+
+
+//########################################################################
+//# E N D O F F I L E
+//########################################################################
+