diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-04-02 23:14:10 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-04-04 14:10:40 +0200 |
commit | 12f1eb15ca3fed6310402847559a7c99d3c77847 (patch) | |
tree | 8935675b623c6f3b4914b8b44f7fa5f2816a9241 /libqpdf/QUtil.cc | |
parent | f20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff) | |
download | qpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst |
Programmatically apply new formatting to code
Run this:
for i in **/*.cc **/*.c **/*.h **/*.hh; do
clang-format < $i >| $i.new && mv $i.new $i
done
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r-- | libqpdf/QUtil.cc | 1735 |
1 files changed, 751 insertions, 984 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index d56dd14f..11ced38d 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -3,253 +3,253 @@ #include <qpdf/QUtil.hh> -#include <qpdf/PointerHolder.hh> #include <qpdf/CryptoRandomDataProvider.hh> +#include <qpdf/Pipeline.hh> +#include <qpdf/PointerHolder.hh> +#include <qpdf/QIntC.hh> #include <qpdf/QPDFSystemError.hh> #include <qpdf/QTC.hh> -#include <qpdf/QIntC.hh> -#include <qpdf/Pipeline.hh> #include <cmath> +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <fstream> #include <iomanip> +#include <locale> +#include <memory> +#include <regex> +#include <set> #include <sstream> -#include <fstream> #include <stdexcept> -#include <set> #include <stdio.h> -#include <errno.h> -#include <ctype.h> #include <stdlib.h> #include <string.h> -#include <fcntl.h> -#include <memory> -#include <locale> -#include <regex> #ifndef QPDF_NO_WCHAR_T # include <cwchar> #endif #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN -# include <windows.h> # include <direct.h> # include <io.h> +# include <windows.h> #else -# include <unistd.h> # include <sys/stat.h> +# include <unistd.h> #endif // First element is 24 static unsigned short pdf_doc_low_to_unicode[] = { - 0x02d8, // 0x18 BREVE - 0x02c7, // 0x19 CARON - 0x02c6, // 0x1a MODIFIER LETTER CIRCUMFLEX ACCENT - 0x02d9, // 0x1b DOT ABOVE - 0x02dd, // 0x1c DOUBLE ACUTE ACCENT - 0x02db, // 0x1d OGONEK - 0x02da, // 0x1e RING ABOVE - 0x02dc, // 0x1f SMALL TILDE + 0x02d8, // 0x18 BREVE + 0x02c7, // 0x19 CARON + 0x02c6, // 0x1a MODIFIER LETTER CIRCUMFLEX ACCENT + 0x02d9, // 0x1b DOT ABOVE + 0x02dd, // 0x1c DOUBLE ACUTE ACCENT + 0x02db, // 0x1d OGONEK + 0x02da, // 0x1e RING ABOVE + 0x02dc, // 0x1f SMALL TILDE }; // First element is 127 static unsigned short pdf_doc_to_unicode[] = { - 0xfffd, // 0x7f UNDEFINED - 0x2022, // 0x80 BULLET - 0x2020, // 0x81 DAGGER - 0x2021, // 0x82 DOUBLE DAGGER - 0x2026, // 0x83 HORIZONTAL ELLIPSIS - 0x2014, // 0x84 EM DASH - 0x2013, // 0x85 EN DASH - 0x0192, // 0x86 SMALL LETTER F WITH HOOK - 0x2044, // 0x87 FRACTION SLASH (solidus) - 0x2039, // 0x88 SINGLE LEFT-POINTING ANGLE QUOTATION MARK - 0x203a, // 0x89 SINGLE RIGHT-POINTING ANGLE QUOTATION MARK - 0x2212, // 0x8a MINUS SIGN - 0x2030, // 0x8b PER MILLE SIGN - 0x201e, // 0x8c DOUBLE LOW-9 QUOTATION MARK (quotedblbase) - 0x201c, // 0x8d LEFT DOUBLE QUOTATION MARK (double quote left) - 0x201d, // 0x8e RIGHT DOUBLE QUOTATION MARK (quotedblright) - 0x2018, // 0x8f LEFT SINGLE QUOTATION MARK (quoteleft) - 0x2019, // 0x90 RIGHT SINGLE QUOTATION MARK (quoteright) - 0x201a, // 0x91 SINGLE LOW-9 QUOTATION MARK (quotesinglbase) - 0x2122, // 0x92 TRADE MARK SIGN - 0xfb01, // 0x93 LATIN SMALL LIGATURE FI - 0xfb02, // 0x94 LATIN SMALL LIGATURE FL - 0x0141, // 0x95 LATIN CAPITAL LETTER L WITH STROKE - 0x0152, // 0x96 LATIN CAPITAL LIGATURE OE - 0x0160, // 0x97 LATIN CAPITAL LETTER S WITH CARON - 0x0178, // 0x98 LATIN CAPITAL LETTER Y WITH DIAERESIS - 0x017d, // 0x99 LATIN CAPITAL LETTER Z WITH CARON - 0x0131, // 0x9a LATIN SMALL LETTER DOTLESS I - 0x0142, // 0x9b LATIN SMALL LETTER L WITH STROKE - 0x0153, // 0x9c LATIN SMALL LIGATURE OE - 0x0161, // 0x9d LATIN SMALL LETTER S WITH CARON - 0x017e, // 0x9e LATIN SMALL LETTER Z WITH CARON - 0xfffd, // 0x9f UNDEFINED - 0x20ac, // 0xa0 EURO SIGN + 0xfffd, // 0x7f UNDEFINED + 0x2022, // 0x80 BULLET + 0x2020, // 0x81 DAGGER + 0x2021, // 0x82 DOUBLE DAGGER + 0x2026, // 0x83 HORIZONTAL ELLIPSIS + 0x2014, // 0x84 EM DASH + 0x2013, // 0x85 EN DASH + 0x0192, // 0x86 SMALL LETTER F WITH HOOK + 0x2044, // 0x87 FRACTION SLASH (solidus) + 0x2039, // 0x88 SINGLE LEFT-POINTING ANGLE QUOTATION MARK + 0x203a, // 0x89 SINGLE RIGHT-POINTING ANGLE QUOTATION MARK + 0x2212, // 0x8a MINUS SIGN + 0x2030, // 0x8b PER MILLE SIGN + 0x201e, // 0x8c DOUBLE LOW-9 QUOTATION MARK (quotedblbase) + 0x201c, // 0x8d LEFT DOUBLE QUOTATION MARK (double quote left) + 0x201d, // 0x8e RIGHT DOUBLE QUOTATION MARK (quotedblright) + 0x2018, // 0x8f LEFT SINGLE QUOTATION MARK (quoteleft) + 0x2019, // 0x90 RIGHT SINGLE QUOTATION MARK (quoteright) + 0x201a, // 0x91 SINGLE LOW-9 QUOTATION MARK (quotesinglbase) + 0x2122, // 0x92 TRADE MARK SIGN + 0xfb01, // 0x93 LATIN SMALL LIGATURE FI + 0xfb02, // 0x94 LATIN SMALL LIGATURE FL + 0x0141, // 0x95 LATIN CAPITAL LETTER L WITH STROKE + 0x0152, // 0x96 LATIN CAPITAL LIGATURE OE + 0x0160, // 0x97 LATIN CAPITAL LETTER S WITH CARON + 0x0178, // 0x98 LATIN CAPITAL LETTER Y WITH DIAERESIS + 0x017d, // 0x99 LATIN CAPITAL LETTER Z WITH CARON + 0x0131, // 0x9a LATIN SMALL LETTER DOTLESS I + 0x0142, // 0x9b LATIN SMALL LETTER L WITH STROKE + 0x0153, // 0x9c LATIN SMALL LIGATURE OE + 0x0161, // 0x9d LATIN SMALL LETTER S WITH CARON + 0x017e, // 0x9e LATIN SMALL LETTER Z WITH CARON + 0xfffd, // 0x9f UNDEFINED + 0x20ac, // 0xa0 EURO SIGN }; static unsigned short win_ansi_to_unicode[] = { - 0x20ac, // 0x80 - 0xfffd, // 0x81 - 0x201a, // 0x82 - 0x0192, // 0x83 - 0x201e, // 0x84 - 0x2026, // 0x85 - 0x2020, // 0x86 - 0x2021, // 0x87 - 0x02c6, // 0x88 - 0x2030, // 0x89 - 0x0160, // 0x8a - 0x2039, // 0x8b - 0x0152, // 0x8c - 0xfffd, // 0x8d - 0x017d, // 0x8e - 0xfffd, // 0x8f - 0xfffd, // 0x90 - 0x2018, // 0x91 - 0x2019, // 0x92 - 0x201c, // 0x93 - 0x201d, // 0x94 - 0x2022, // 0x95 - 0x2013, // 0x96 - 0x2014, // 0x97 - 0x0303, // 0x98 - 0x2122, // 0x99 - 0x0161, // 0x9a - 0x203a, // 0x9b - 0x0153, // 0x9c - 0xfffd, // 0x9d - 0x017e, // 0x9e - 0x0178, // 0x9f - 0x00a0, // 0xa0 + 0x20ac, // 0x80 + 0xfffd, // 0x81 + 0x201a, // 0x82 + 0x0192, // 0x83 + 0x201e, // 0x84 + 0x2026, // 0x85 + 0x2020, // 0x86 + 0x2021, // 0x87 + 0x02c6, // 0x88 + 0x2030, // 0x89 + 0x0160, // 0x8a + 0x2039, // 0x8b + 0x0152, // 0x8c + 0xfffd, // 0x8d + 0x017d, // 0x8e + 0xfffd, // 0x8f + 0xfffd, // 0x90 + 0x2018, // 0x91 + 0x2019, // 0x92 + 0x201c, // 0x93 + 0x201d, // 0x94 + 0x2022, // 0x95 + 0x2013, // 0x96 + 0x2014, // 0x97 + 0x0303, // 0x98 + 0x2122, // 0x99 + 0x0161, // 0x9a + 0x203a, // 0x9b + 0x0153, // 0x9c + 0xfffd, // 0x9d + 0x017e, // 0x9e + 0x0178, // 0x9f + 0x00a0, // 0xa0 }; static unsigned short mac_roman_to_unicode[] = { - 0x00c4, // 0x80 - 0x00c5, // 0x81 - 0x00c7, // 0x82 - 0x00c9, // 0x83 - 0x00d1, // 0x84 - 0x00d6, // 0x85 - 0x00dc, // 0x86 - 0x00e1, // 0x87 - 0x00e0, // 0x88 - 0x00e2, // 0x89 - 0x00e4, // 0x8a - 0x00e3, // 0x8b - 0x00e5, // 0x8c - 0x00e7, // 0x8d - 0x00e9, // 0x8e - 0x00e8, // 0x8f - 0x00ea, // 0x90 - 0x00eb, // 0x91 - 0x00ed, // 0x92 - 0x00ec, // 0x93 - 0x00ee, // 0x94 - 0x00ef, // 0x95 - 0x00f1, // 0x96 - 0x00f3, // 0x97 - 0x00f2, // 0x98 - 0x00f4, // 0x99 - 0x00f6, // 0x9a - 0x00f5, // 0x9b - 0x00fa, // 0x9c - 0x00f9, // 0x9d - 0x00fb, // 0x9e - 0x00fc, // 0x9f - 0x2020, // 0xa0 - 0x00b0, // 0xa1 - 0x00a2, // 0xa2 - 0x00a3, // 0xa3 - 0x00a7, // 0xa4 - 0x2022, // 0xa5 - 0x00b6, // 0xa6 - 0x00df, // 0xa7 - 0x00ae, // 0xa8 - 0x00a9, // 0xa9 - 0x2122, // 0xaa - 0x0301, // 0xab - 0x0308, // 0xac - 0xfffd, // 0xad - 0x00c6, // 0xae - 0x00d8, // 0xaf - 0xfffd, // 0xb0 - 0x00b1, // 0xb1 - 0xfffd, // 0xb2 - 0xfffd, // 0xb3 - 0x00a5, // 0xb4 - 0x03bc, // 0xb5 - 0xfffd, // 0xb6 - 0xfffd, // 0xb7 - 0xfffd, // 0xb8 - 0xfffd, // 0xb9 - 0xfffd, // 0xba - 0x1d43, // 0xbb - 0x1d52, // 0xbc - 0xfffd, // 0xbd - 0x00e6, // 0xbe - 0x00f8, // 0xbf - 0x00bf, // 0xc0 - 0x00a1, // 0xc1 - 0x00ac, // 0xc2 - 0xfffd, // 0xc3 - 0x0192, // 0xc4 - 0xfffd, // 0xc5 - 0xfffd, // 0xc6 - 0x00ab, // 0xc7 - 0x00bb, // 0xc8 - 0x2026, // 0xc9 - 0xfffd, // 0xca - 0x00c0, // 0xcb - 0x00c3, // 0xcc - 0x00d5, // 0xcd - 0x0152, // 0xce - 0x0153, // 0xcf - 0x2013, // 0xd0 - 0x2014, // 0xd1 - 0x201c, // 0xd2 - 0x201d, // 0xd3 - 0x2018, // 0xd4 - 0x2019, // 0xd5 - 0x00f7, // 0xd6 - 0xfffd, // 0xd7 - 0x00ff, // 0xd8 - 0x0178, // 0xd9 - 0x2044, // 0xda - 0x00a4, // 0xdb - 0x2039, // 0xdc - 0x203a, // 0xdd - 0xfb01, // 0xde - 0xfb02, // 0xdf - 0x2021, // 0xe0 - 0x00b7, // 0xe1 - 0x201a, // 0xe2 - 0x201e, // 0xe3 - 0x2030, // 0xe4 - 0x00c2, // 0xe5 - 0x00ca, // 0xe6 - 0x00c1, // 0xe7 - 0x00cb, // 0xe8 - 0x00c8, // 0xe9 - 0x00cd, // 0xea - 0x00ce, // 0xeb - 0x00cf, // 0xec - 0x00cc, // 0xed - 0x00d3, // 0xee - 0x00d4, // 0xef - 0xfffd, // 0xf0 - 0x00d2, // 0xf1 - 0x00da, // 0xf2 - 0x00db, // 0xf3 - 0x00d9, // 0xf4 - 0x0131, // 0xf5 - 0x02c6, // 0xf6 - 0x0303, // 0xf7 - 0x0304, // 0xf8 - 0x0306, // 0xf9 - 0x0307, // 0xfa - 0x030a, // 0xfb - 0x0327, // 0xfc - 0x030b, // 0xfd - 0x0328, // 0xfe - 0x02c7, // 0xff + 0x00c4, // 0x80 + 0x00c5, // 0x81 + 0x00c7, // 0x82 + 0x00c9, // 0x83 + 0x00d1, // 0x84 + 0x00d6, // 0x85 + 0x00dc, // 0x86 + 0x00e1, // 0x87 + 0x00e0, // 0x88 + 0x00e2, // 0x89 + 0x00e4, // 0x8a + 0x00e3, // 0x8b + 0x00e5, // 0x8c + 0x00e7, // 0x8d + 0x00e9, // 0x8e + 0x00e8, // 0x8f + 0x00ea, // 0x90 + 0x00eb, // 0x91 + 0x00ed, // 0x92 + 0x00ec, // 0x93 + 0x00ee, // 0x94 + 0x00ef, // 0x95 + 0x00f1, // 0x96 + 0x00f3, // 0x97 + 0x00f2, // 0x98 + 0x00f4, // 0x99 + 0x00f6, // 0x9a + 0x00f5, // 0x9b + 0x00fa, // 0x9c + 0x00f9, // 0x9d + 0x00fb, // 0x9e + 0x00fc, // 0x9f + 0x2020, // 0xa0 + 0x00b0, // 0xa1 + 0x00a2, // 0xa2 + 0x00a3, // 0xa3 + 0x00a7, // 0xa4 + 0x2022, // 0xa5 + 0x00b6, // 0xa6 + 0x00df, // 0xa7 + 0x00ae, // 0xa8 + 0x00a9, // 0xa9 + 0x2122, // 0xaa + 0x0301, // 0xab + 0x0308, // 0xac + 0xfffd, // 0xad + 0x00c6, // 0xae + 0x00d8, // 0xaf + 0xfffd, // 0xb0 + 0x00b1, // 0xb1 + 0xfffd, // 0xb2 + 0xfffd, // 0xb3 + 0x00a5, // 0xb4 + 0x03bc, // 0xb5 + 0xfffd, // 0xb6 + 0xfffd, // 0xb7 + 0xfffd, // 0xb8 + 0xfffd, // 0xb9 + 0xfffd, // 0xba + 0x1d43, // 0xbb + 0x1d52, // 0xbc + 0xfffd, // 0xbd + 0x00e6, // 0xbe + 0x00f8, // 0xbf + 0x00bf, // 0xc0 + 0x00a1, // 0xc1 + 0x00ac, // 0xc2 + 0xfffd, // 0xc3 + 0x0192, // 0xc4 + 0xfffd, // 0xc5 + 0xfffd, // 0xc6 + 0x00ab, // 0xc7 + 0x00bb, // 0xc8 + 0x2026, // 0xc9 + 0xfffd, // 0xca + 0x00c0, // 0xcb + 0x00c3, // 0xcc + 0x00d5, // 0xcd + 0x0152, // 0xce + 0x0153, // 0xcf + 0x2013, // 0xd0 + 0x2014, // 0xd1 + 0x201c, // 0xd2 + 0x201d, // 0xd3 + 0x2018, // 0xd4 + 0x2019, // 0xd5 + 0x00f7, // 0xd6 + 0xfffd, // 0xd7 + 0x00ff, // 0xd8 + 0x0178, // 0xd9 + 0x2044, // 0xda + 0x00a4, // 0xdb + 0x2039, // 0xdc + 0x203a, // 0xdd + 0xfb01, // 0xde + 0xfb02, // 0xdf + 0x2021, // 0xe0 + 0x00b7, // 0xe1 + 0x201a, // 0xe2 + 0x201e, // 0xe3 + 0x2030, // 0xe4 + 0x00c2, // 0xe5 + 0x00ca, // 0xe6 + 0x00c1, // 0xe7 + 0x00cb, // 0xe8 + 0x00c8, // 0xe9 + 0x00cd, // 0xea + 0x00ce, // 0xeb + 0x00cf, // 0xec + 0x00cc, // 0xed + 0x00d3, // 0xee + 0x00d4, // 0xef + 0xfffd, // 0xf0 + 0x00d2, // 0xf1 + 0x00da, // 0xf2 + 0x00db, // 0xf3 + 0x00d9, // 0xf4 + 0x0131, // 0xf5 + 0x02c6, // 0xf6 + 0x0303, // 0xf7 + 0x0304, // 0xf8 + 0x0306, // 0xf9 + 0x0307, // 0xfa + 0x030a, // 0xfb + 0x0327, // 0xfc + 0x030b, // 0xfd + 0x0328, // 0xfe + 0x02c7, // 0xff }; class FileCloser @@ -270,27 +270,22 @@ class FileCloser }; template <typename T> -static -std::string +static std::string int_to_string_base_internal(T num, int base, int length) { // Backward compatibility -- int_to_string, which calls this // function, used to use sprintf with %0*d, so we interpret length // such that a negative value appends spaces and a positive value // prepends zeroes. - if (! ((base == 8) || (base == 10) || (base == 16))) - { + if (!((base == 8) || (base == 10) || (base == 16))) { throw std::logic_error( "int_to_string_base called with unsupported base"); } std::string cvt; - if (base == 10) - { + if (base == 10) { // Use the more efficient std::to_string when possible cvt = std::to_string(num); - } - else - { + } else { std::ostringstream buf; buf.imbue(std::locale::classic()); buf << std::setbase(base) << std::nouppercase << num; @@ -298,13 +293,11 @@ int_to_string_base_internal(T num, int base, int length) } std::string result; int str_length = QIntC::to_int(cvt.length()); - if ((length > 0) && (str_length < length)) - { + if ((length > 0) && (str_length < length)) { result.append(QIntC::to_size(length - str_length), '0'); } result += cvt; - if ((length < 0) && (str_length < -length)) - { + if ((length < 0) && (str_length < -length)) { result.append(QIntC::to_size(-length - str_length), ' '); } return result; @@ -341,29 +334,25 @@ QUtil::double_to_string(double num, int decimal_places) } std::string -QUtil::double_to_string(double num, int decimal_places, - bool trim_trailing_zeroes) +QUtil::double_to_string( + double num, int decimal_places, bool trim_trailing_zeroes) { // Backward compatibility -- this code used to use sprintf and // treated decimal_places <= 0 to mean to use the default, which // was six decimal places. Starting in 10.2, we trim trailing // zeroes by default. - if (decimal_places <= 0) - { + if (decimal_places <= 0) { decimal_places = 6; } std::ostringstream buf; buf.imbue(std::locale::classic()); buf << std::setprecision(decimal_places) << std::fixed << num; std::string result = buf.str(); - if (trim_trailing_zeroes) - { - while ((result.length() > 1) && (result.back() == '0')) - { + if (trim_trailing_zeroes) { + while ((result.length() > 1) && (result.back() == '0')) { result.pop_back(); } - if ((result.length() > 1) && (result.back() == '.')) - { + if ((result.length() > 1) && (result.back() == '.')) { result.pop_back(); } } @@ -379,11 +368,10 @@ QUtil::string_to_ll(char const* str) #else long long result = strtoll(str, 0, 10); #endif - if (errno == ERANGE) - { + if (errno == ERANGE) { throw std::range_error( - std::string("overflow/underflow converting ") + str - + " to 64-bit integer"); + std::string("overflow/underflow converting ") + str + + " to 64-bit integer"); } return result; } @@ -399,15 +387,13 @@ unsigned long long QUtil::string_to_ull(char const* str) { char const* p = str; - while (*p && is_space(*p)) - { + while (*p && is_space(*p)) { ++p; } - if (*p == '-') - { + if (*p == '-') { throw std::runtime_error( - std::string("underflow converting ") + str - + " to 64-bit unsigned integer"); + std::string("underflow converting ") + str + + " to 64-bit unsigned integer"); } errno = 0; @@ -416,11 +402,10 @@ QUtil::string_to_ull(char const* str) #else unsigned long long result = strtoull(str, 0, 10); #endif - if (errno == ERANGE) - { + if (errno == ERANGE) { throw std::runtime_error( - std::string("overflow converting ") + str - + " to 64-bit unsigned integer"); + std::string("overflow converting ") + str + + " to 64-bit unsigned integer"); } return result; } @@ -453,8 +438,7 @@ QUtil::throw_system_error(std::string const& description) int QUtil::os_wrapper(std::string const& description, int status) { - if (status == -1) - { + if (status == -1) { throw_system_error(description); } return status; @@ -473,12 +457,10 @@ win_convert_filename(char const* filename) PointerHolder<wchar_t> wfilenamep(true, new wchar_t[wlen + 1]); wchar_t* wfilename = wfilenamep.get(); wfilename[wlen] = 0; - for (unsigned int i = 2; i < len; i += 2) - { - wfilename[(i/2) - 1] = - static_cast<wchar_t>( - (static_cast<unsigned char>(u16.at(i)) << 8) + - static_cast<unsigned char>(u16.at(i+1))); + for (unsigned int i = 2; i < len; i += 2) { + wfilename[(i / 2) - 1] = static_cast<wchar_t>( + (static_cast<unsigned char>(u16.at(i)) << 8) + + static_cast<unsigned char>(u16.at(i + 1))); } return wfilenamep; } @@ -494,22 +476,19 @@ QUtil::safe_fopen(char const* filename, char const* mode) PointerHolder<wchar_t> wmodep(true, new wchar_t[strlen(mode) + 1]); wchar_t* wmode = wmodep.get(); wmode[strlen(mode)] = 0; - for (size_t i = 0; i < strlen(mode); ++i) - { + for (size_t i = 0; i < strlen(mode); ++i) { wmode[i] = static_cast<wchar_t>(mode[i]); } -#ifdef _MSC_VER +# ifdef _MSC_VER errno_t err = _wfopen_s(&f, wfilename, wmode); - if (err != 0) - { + if (err != 0) { errno = err; } -#else +# else f = _wfopen(wfilename, wmode); -#endif - if (f == 0) - { +# endif + if (f == 0) { throw_system_error(std::string("open ") + filename); } #else @@ -521,8 +500,7 @@ QUtil::safe_fopen(char const* filename, char const* mode) FILE* QUtil::fopen_wrapper(std::string const& description, FILE* f) { - if (f == 0) - { + if (f == 0) { throw_system_error(description); } return f; @@ -531,13 +509,10 @@ QUtil::fopen_wrapper(std::string const& description, FILE* f) bool QUtil::file_can_be_opened(char const* filename) { - try - { + try { fclose(safe_fopen(filename, "rb")); return true; - } - catch (std::runtime_error&) - { + } catch (std::runtime_error&) { // can't open the file } return false; @@ -547,9 +522,10 @@ int QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence) { #if HAVE_FSEEKO - return fseeko(stream, - QIntC::IntConverter<qpdf_offset_t, off_t>::convert(offset), - whence); + return fseeko( + stream, + QIntC::IntConverter<qpdf_offset_t, off_t>::convert(offset), + whence); #elif HAVE_FSEEKO64 return fseeko64(stream, offset, whence); #else @@ -580,36 +556,43 @@ QUtil::tell(FILE* stream) bool QUtil::same_file(char const* name1, char const* name2) { - if ((name1 == 0) || (strlen(name1) == 0) || - (name2 == 0) || (strlen(name2) == 0)) - { + if ((name1 == 0) || (strlen(name1) == 0) || (name2 == 0) || + (strlen(name2) == 0)) { return false; } #ifdef _WIN32 bool same = false; # ifndef AVOID_WINDOWS_HANDLE - HANDLE fh1 = CreateFile(name1, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - HANDLE fh2 = CreateFile(name2, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE fh1 = CreateFile( + name1, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + HANDLE fh2 = CreateFile( + name2, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); BY_HANDLE_FILE_INFORMATION fi1; BY_HANDLE_FILE_INFORMATION fi2; - if ((fh1 != INVALID_HANDLE_VALUE) && - (fh2 != INVALID_HANDLE_VALUE) && + if ((fh1 != INVALID_HANDLE_VALUE) && (fh2 != INVALID_HANDLE_VALUE) && GetFileInformationByHandle(fh1, &fi1) && GetFileInformationByHandle(fh2, &fi2) && (fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber) && (fi1.nFileIndexLow == fi2.nFileIndexLow) && - (fi1.nFileIndexHigh == fi2.nFileIndexHigh)) - { + (fi1.nFileIndexHigh == fi2.nFileIndexHigh)) { same = true; } - if (fh1 != INVALID_HANDLE_VALUE) - { + if (fh1 != INVALID_HANDLE_VALUE) { CloseHandle(fh1); } - if (fh2 != INVALID_HANDLE_VALUE) - { + if (fh2 != INVALID_HANDLE_VALUE) { CloseHandle(fh2); } # endif @@ -617,18 +600,14 @@ QUtil::same_file(char const* name1, char const* name2) #else struct stat st1; struct stat st2; - if ((stat(name1, &st1) == 0) && - (stat(name2, &st2) == 0) && - (st1.st_ino == st2.st_ino) && - (st1.st_dev == st2.st_dev)) - { + if ((stat(name1, &st1) == 0) && (stat(name2, &st2) == 0) && + (st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) { return true; } #endif return false; } - void QUtil::remove_file(char const* path) { @@ -644,21 +623,20 @@ void QUtil::rename_file(char const* oldname, char const* newname) { #ifdef _WIN32 - try - { + try { remove_file(newname); - } - catch (QPDFSystemError&) - { + } catch (QPDFSystemError&) { // ignore } PointerHolder<wchar_t> wold = win_convert_filename(oldname); PointerHolder<wchar_t> wnew = win_convert_filename(newname); - os_wrapper(std::string("rename ") + oldname + " " + newname, - _wrename(wold.get(), wnew.get())); + os_wrapper( + std::string("rename ") + oldname + " " + newname, + _wrename(wold.get(), wnew.get())); #else - os_wrapper(std::string("rename ") + oldname + " " + newname, - rename(oldname, newname)); + os_wrapper( + std::string("rename ") + oldname + " " + newname, + rename(oldname, newname)); #endif } @@ -671,13 +649,11 @@ QUtil::pipe_file(char const* filename, Pipeline* p) size_t len = 0; int constexpr size = 8192; unsigned char buf[size]; - while ((len = fread(buf, 1, size, f)) > 0) - { + while ((len = fread(buf, 1, size, f)) > 0) { p->write(buf, len); } p->finish(); - if (ferror(f)) - { + if (ferror(f)) { throw std::runtime_error( std::string("failure reading file ") + filename); } @@ -686,9 +662,7 @@ QUtil::pipe_file(char const* filename, Pipeline* p) std::function<void(Pipeline*)> QUtil::file_provider(std::string const& filename) { - return [filename](Pipeline* p) { - pipe_file(filename.c_str(), p); - }; + return [filename](Pipeline* p) { pipe_file(filename.c_str(), p); }; } std::string @@ -701,20 +675,14 @@ QUtil::path_basename(std::string const& filename) #endif std::string last = filename; auto len = last.length(); - while (len > 1) - { + while (len > 1) { auto pos = last.find_last_of(pathsep); - if (pos == len - 1) - { + if (pos == len - 1) { last.pop_back(); --len; - } - else if (pos == std::string::npos) - { + } else if (pos == std::string::npos) { break; - } - else - { + } else { last = last.substr(pos + 1); break; } @@ -756,8 +724,7 @@ std::string QUtil::hex_encode(std::string const& input) { std::string result; - for (unsigned int i = 0; i < input.length(); ++i) - { + for (unsigned int i = 0; i < input.length(); ++i) { result += QUtil::int_to_string_base( QIntC::to_int(static_cast<unsigned char>(input.at(i))), 16, 2); } @@ -769,36 +736,24 @@ QUtil::hex_decode(std::string const& input) { std::string result; size_t pos = 0; - for (std::string::const_iterator p = input.begin(); p != input.end(); ++p) - { + for (std::string::const_iterator p = input.begin(); p != input.end(); ++p) { char ch = *p; bool skip = false; - if ((*p >= 'A') && (*p <= 'F')) - { + if ((*p >= 'A') && (*p <= 'F')) { ch = QIntC::to_char(ch - 'A' + 10); - } - else if ((*p >= 'a') && (*p <= 'f')) - { + } else if ((*p >= 'a') && (*p <= 'f')) { ch = QIntC::to_char(ch - 'a' + 10); - } - else if ((*p >= '0') && (*p <= '9')) - { + } else if ((*p >= '0') && (*p <= '9')) { ch = QIntC::to_char(ch - '0'); - } - else - { + } else { skip = true; } - if (! skip) - { - if (pos == 0) - { + if (!skip) { + if (pos == 0) { result.push_back(static_cast<char>(ch << 4)); pos = 1; - } - else - { - result[result.length()-1] |= ch; + } else { + result[result.length() - 1] |= ch; pos = 0; } } @@ -810,7 +765,7 @@ void QUtil::binary_stdout() { #if defined(_WIN32) && defined(__BORLANDC__) - setmode(_fileno(stdout), _O_BINARY); + setmode(_fileno(stdout), _O_BINARY); #elif defined(_WIN32) _setmode(_fileno(stdout), _O_BINARY); #endif @@ -820,7 +775,7 @@ void QUtil::binary_stdin() { #if defined(_WIN32) && defined(__BORLANDC__) - setmode(_fileno(stdin), _O_BINARY); + setmode(_fileno(stdin), _O_BINARY); #elif defined(_WIN32) _setmode(_fileno(stdin), _O_BINARY); #endif @@ -830,7 +785,7 @@ void QUtil::setLineBuf(FILE* f) { #ifndef _WIN32 - setvbuf(f, reinterpret_cast<char *>(0), _IOLBF, 0); + setvbuf(f, reinterpret_cast<char*>(0), _IOLBF, 0); #endif } @@ -839,18 +794,14 @@ QUtil::getWhoami(char* argv0) { char* whoami = 0; if (((whoami = strrchr(argv0, '/')) == NULL) && - ((whoami = strrchr(argv0, '\\')) == NULL)) - { + ((whoami = strrchr(argv0, '\\')) == NULL)) { whoami = argv0; - } - else - { + } else { ++whoami; } if ((strlen(whoami) > 4) && - (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0)) - { + (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0)) { whoami[strlen(whoami) - 4] = '\0'; } @@ -867,14 +818,12 @@ QUtil::get_env(std::string const& var, std::string* value) # else // first get the size of the buffer DWORD len = ::GetEnvironmentVariable(var.c_str(), NULL, 0); - if (len == 0) - { + if (len == 0) { // this means that there is no such variable return false; } - if (value) - { + if (value) { PointerHolder<char> t = PointerHolder<char>(true, new char[len + 1]); ::GetEnvironmentVariable(var.c_str(), t.get(), len); *value = t.get(); @@ -884,12 +833,10 @@ QUtil::get_env(std::string const& var, std::string* value) # endif #else char* p = getenv(var.c_str()); - if (p == 0) - { + if (p == 0) { return false; } - if (value) - { + if (value) { *value = p; } @@ -931,40 +878,42 @@ QUtil::get_current_qpdf_time() GetLocalTime(<ime); TIME_ZONE_INFORMATION tzinfo; GetTimeZoneInformation(&tzinfo); - return QPDFTime(static_cast<int>(ltime.wYear), - static_cast<int>(ltime.wMonth), - static_cast<int>(ltime.wDay), - static_cast<int>(ltime.wHour), - static_cast<int>(ltime.wMinute), - static_cast<int>(ltime.wSecond), - // tzinfo.Bias is minutes before UTC - static_cast<int>(tzinfo.Bias)); + return QPDFTime( + static_cast<int>(ltime.wYear), + static_cast<int>(ltime.wMonth), + static_cast<int>(ltime.wDay), + static_cast<int>(ltime.wHour), + static_cast<int>(ltime.wMinute), + static_cast<int>(ltime.wSecond), + // tzinfo.Bias is minutes before UTC + static_cast<int>(tzinfo.Bias)); #else struct tm ltime; time_t now = time(0); tzset(); -#ifdef HAVE_LOCALTIME_R +# ifdef HAVE_LOCALTIME_R localtime_r(&now, <ime); -#else +# else ltime = *localtime(&now); -#endif -#if HAVE_TM_GMTOFF +# endif +# if HAVE_TM_GMTOFF // tm_gmtoff is seconds after UTC int tzoff = -static_cast<int>(ltime.tm_gmtoff / 60); -#elif HAVE_EXTERN_LONG_TIMEZONE +# elif HAVE_EXTERN_LONG_TIMEZONE // timezone is seconds before UTC, not adjusted for daylight saving time int tzoff = static_cast<int>(timezone / 60); -#else +# else // Don't know how to get timezone on this platform int tzoff = 0; -#endif - return QPDFTime(static_cast<int>(ltime.tm_year + 1900), - static_cast<int>(ltime.tm_mon + 1), - static_cast<int>(ltime.tm_mday), - static_cast<int>(ltime.tm_hour), - static_cast<int>(ltime.tm_min), - static_cast<int>(ltime.tm_sec), - tzoff); +# endif + return QPDFTime( + static_cast<int>(ltime.tm_year + 1900), + static_cast<int>(ltime.tm_mon + 1), + static_cast<int>(ltime.tm_mday), + static_cast<int>(ltime.tm_hour), + static_cast<int>(ltime.tm_min), + static_cast<int>(ltime.tm_sec), + tzoff); #endif } @@ -973,33 +922,24 @@ QUtil::qpdf_time_to_pdf_time(QPDFTime const& qtm) { std::string tz_offset; int t = qtm.tz_delta; - if (t == 0) - { + if (t == 0) { tz_offset = "Z"; - } - else - { - if (t < 0) - { + } else { + if (t < 0) { t = -t; tz_offset += "+"; - } - else - { + } else { tz_offset += "-"; } - tz_offset += - QUtil::int_to_string(t / 60, 2) + "'" + + tz_offset += QUtil::int_to_string(t / 60, 2) + "'" + QUtil::int_to_string(t % 60, 2) + "'"; } - return ("D:" + - QUtil::int_to_string(qtm.year, 4) + - QUtil::int_to_string(qtm.month, 2) + - QUtil::int_to_string(qtm.day, 2) + - QUtil::int_to_string(qtm.hour, 2) + - QUtil::int_to_string(qtm.minute, 2) + - QUtil::int_to_string(qtm.second, 2) + - tz_offset); + return ( + "D:" + QUtil::int_to_string(qtm.year, 4) + + QUtil::int_to_string(qtm.month, 2) + QUtil::int_to_string(qtm.day, 2) + + QUtil::int_to_string(qtm.hour, 2) + + QUtil::int_to_string(qtm.minute, 2) + + QUtil::int_to_string(qtm.second, 2) + tz_offset); } bool @@ -1009,8 +949,7 @@ QUtil::pdf_time_to_qpdf_time(std::string const& str, QPDFTime* qtm) "([0-9]{2})([0-9]{2})([0-9]{2})" "(?:(Z?)|([\\+\\-])([0-9]{2})'([0-9]{2})')$"); std::smatch m; - if (! std::regex_match(str, m, pdf_date)) - { + if (!std::regex_match(str, m, pdf_date)) { return false; } int tz_delta = 0; @@ -1018,24 +957,21 @@ QUtil::pdf_time_to_qpdf_time(std::string const& str, QPDFTime* qtm) return QUtil::string_to_int(s.c_str()); }; - if (m[8] != "") - { - tz_delta = ((to_i(m[9]) * 60) + - to_i(m[10])); - if (m[8] == "+") - { + if (m[8] != "") { + tz_delta = ((to_i(m[9]) * 60) + to_i(m[10])); + if (m[8] == "+") { tz_delta = -tz_delta; } } - if (qtm) - { - *qtm = QPDFTime(to_i(m[1]), - to_i(m[2]), - to_i(m[3]), - to_i(m[4]), - to_i(m[5]), - to_i(m[6]), - tz_delta); + if (qtm) { + *qtm = QPDFTime( + to_i(m[1]), + to_i(m[2]), + to_i(m[3]), + to_i(m[4]), + to_i(m[5]), + to_i(m[6]), + tz_delta); } return true; } @@ -1055,16 +991,11 @@ QUtil::toUTF8(unsigned long uval) // 110zzzzz, the second is 10zzzzzz, and the z's represent the // remaining bits. - if (uval > 0x7fffffff) - { + if (uval > 0x7fffffff) { throw std::runtime_error("bounds error in QUtil::toUTF8"); - } - else if (uval < 128) - { + } else if (uval < 128) { result += static_cast<char>(uval); - } - else - { + } else { unsigned char bytes[7]; bytes[6] = '\0'; unsigned char* cur_byte = &bytes[5]; @@ -1072,8 +1003,7 @@ QUtil::toUTF8(unsigned long uval) // maximum value that will fit in the current number of bytes unsigned char maxval = 0x3f; // six bits - while (uval > QIntC::to_ulong(maxval)) - { + while (uval > QIntC::to_ulong(maxval)) { // Assign low six bits plus 10000000 to lowest unused // byte position, then shift *cur_byte = static_cast<unsigned char>(0x80 + (uval & 0x3f)); @@ -1081,8 +1011,7 @@ QUtil::toUTF8(unsigned long uval) // Maximum that will fit in high byte now shrinks by one bit maxval = static_cast<unsigned char>(maxval >> 1); // Slide to the left one byte - if (cur_byte <= bytes) - { + if (cur_byte <= bytes) { throw std::logic_error("QUtil::toUTF8: overflow error"); } --cur_byte; @@ -1102,19 +1031,14 @@ std::string QUtil::toUTF16(unsigned long uval) { std::string result; - if ((uval >= 0xd800) && (uval <= 0xdfff)) - { + if ((uval >= 0xd800) && (uval <= 0xdfff)) { result = "\xff\xfd"; - } - else if (uval <= 0xffff) - { + } else if (uval <= 0xffff) { char out[2]; out[0] = static_cast<char>((uval & 0xff00) >> 8); out[1] = static_cast<char>(uval & 0xff); result = std::string(out, 2); - } - else if (uval <= 0x10ffff) - { + } else if (uval <= 0x10ffff) { char out[4]; uval -= 0x10000; unsigned short high = @@ -1126,9 +1050,7 @@ QUtil::toUTF16(unsigned long uval) out[2] = static_cast<char>((low & 0xff00) >> 8); out[3] = static_cast<char>(low & 0xff); result = std::string(out, 4); - } - else - { + } else { result = "\xff\xfd"; } @@ -1199,8 +1121,7 @@ QUtil::random() { long result = 0L; initializeWithRandomBytes( - reinterpret_cast<unsigned char*>(&result), - sizeof(result)); + reinterpret_cast<unsigned char*>(&result), sizeof(result)); return result; } @@ -1226,33 +1147,24 @@ bool QUtil::is_number(char const* p) { // ^[\+\-]?(\.\d*|\d+(\.\d*)?)$ - if (! *p) - { + if (!*p) { return false; } - if ((*p == '-') || (*p == '+')) - { + if ((*p == '-') || (*p == '+')) { ++p; } bool found_dot = false; bool found_digit = false; - for (; *p; ++p) - { - if (*p == '.') - { - if (found_dot) - { + for (; *p; ++p) { + if (*p == '.') { + if (found_dot) { // only one dot return false; } found_dot = true; - } - else if (QUtil::is_digit(*p)) - { + } else if (QUtil::is_digit(*p)) { found_digit = true; - } - else - { + } else { return false; } } @@ -1261,8 +1173,7 @@ QUtil::is_number(char const* p) void QUtil::read_file_into_memory( - char const* filename, - PointerHolder<char>& file_buf, size_t& size) + char const* filename, PointerHolder<char>& file_buf, size_t& size) { FILE* f = safe_fopen(filename, "rb"); FileCloser fc(f); @@ -1273,38 +1184,30 @@ QUtil::read_file_into_memory( char* buf_p = file_buf.get(); size_t bytes_read = 0; size_t len = 0; - while ((len = fread(buf_p + bytes_read, 1, size - bytes_read, f)) > 0) - { + while ((len = fread(buf_p + bytes_read, 1, size - bytes_read, f)) > 0) { bytes_read += len; } - if (bytes_read != size) - { - if (ferror(f)) - { + if (bytes_read != size) { + if (ferror(f)) { throw std::runtime_error( std::string("failure reading file ") + filename + - " into memory: read " + - uint_to_string(bytes_read) + "; wanted " + - uint_to_string(size)); - } - else - { + " into memory: read " + uint_to_string(bytes_read) + + "; wanted " + uint_to_string(size)); + } else { throw std::runtime_error( std::string("premature eof reading file ") + filename + - " into memory: read " + - uint_to_string(bytes_read) + "; wanted " + - uint_to_string(size)); + " into memory: read " + uint_to_string(bytes_read) + + "; wanted " + uint_to_string(size)); } } } -static bool read_char_from_FILE(char& ch, FILE* f) +static bool +read_char_from_FILE(char& ch, FILE* f) { auto len = fread(&ch, 1, 1, f); - if (len == 0) - { - if (ferror(f)) - { + if (len == 0) { + if (ferror(f)) { throw std::runtime_error("failure reading character from file"); } return false; @@ -1327,7 +1230,7 @@ std::list<std::string> QUtil::read_lines_from_file(std::istream& in, bool preserve_eol) { std::list<std::string> lines; - auto next_char = [&in](char& ch) { return (in.get(ch)) ? true: false; }; + auto next_char = [&in](char& ch) { return (in.get(ch)) ? true : false; }; read_lines_from_file(next_char, lines, preserve_eol); return lines; } @@ -1342,51 +1245,42 @@ QUtil::read_lines_from_file(FILE* f, bool preserve_eol) } void -QUtil::read_lines_from_file(std::function<bool(char&)> next_char, - std::list<std::string>& lines, - bool preserve_eol) +QUtil::read_lines_from_file( + std::function<bool(char&)> next_char, + std::list<std::string>& lines, + bool preserve_eol) { std::string* buf = 0; char c; - while (next_char(c)) - { - if (buf == 0) - { + while (next_char(c)) { + if (buf == 0) { lines.push_back(""); buf = &(lines.back()); buf->reserve(80); } - if (buf->capacity() == buf->size()) - { + if (buf->capacity() == buf->size()) { buf->reserve(buf->capacity() * 2); } - if (c == '\n') - { - if (preserve_eol) - { + if (c == '\n') { + if (preserve_eol) { buf->append(1, c); - } - else - { + } else { // Remove any carriage return that preceded the // newline and discard the newline - if ((! buf->empty()) && ((*(buf->rbegin())) == '\r')) - { + if ((!buf->empty()) && ((*(buf->rbegin())) == '\r')) { buf->erase(buf->length() - 1); } } buf = 0; - } - else - { + } else { buf->append(1, c); } } } int -QUtil::str_compare_nocase(char const *s1, char const *s2) +QUtil::str_compare_nocase(char const* s1, char const* s2) { #if defined(_WIN32) && defined(__BORLANDC__) return stricmp(s1, s2); @@ -1397,16 +1291,13 @@ QUtil::str_compare_nocase(char const *s1, char const *s2) #endif } -static int maybe_from_end(int num, bool from_end, int max) +static int +maybe_from_end(int num, bool from_end, int max) { - if (from_end) - { - if (num > max) - { + if (from_end) { + if (num > max) { num = 0; - } - else - { + } else { num = max + 1 - num; } } @@ -1418,189 +1309,134 @@ QUtil::parse_numrange(char const* range, int max) { std::vector<int> result; char const* p = range; - try - { + try { std::vector<int> work; static int const comma = -1; static int const dash = -2; size_t start_idx = 0; size_t skip = 1; - enum { st_top, - st_in_number, - st_after_number } state = st_top; + enum { st_top, st_in_number, st_after_number } state = st_top; bool last_separator_was_dash = false; int cur_number = 0; bool from_end = false; - while (*p) - { + while (*p) { char ch = *p; - if (isdigit(ch)) - { - if (! ((state == st_top) || (state == st_in_number))) - { + if (isdigit(ch)) { + if (!((state == st_top) || (state == st_in_number))) { throw std::runtime_error("digit not expected"); } state = st_in_number; cur_number *= 10; cur_number += (ch - '0'); - } - else if (ch == 'z') - { + } else if (ch == 'z') { // z represents max - if (! (state == st_top)) - { + if (!(state == st_top)) { throw std::runtime_error("z not expected"); } state = st_after_number; cur_number = max; - } - else if (ch == 'r') - { - if (! (state == st_top)) - { + } else if (ch == 'r') { + if (!(state == st_top)) { throw std::runtime_error("r not expected"); } state = st_in_number; from_end = true; - } - else if ((ch == ',') || (ch == '-')) - { - if (! ((state == st_in_number) || (state == st_after_number))) - { + } else if ((ch == ',') || (ch == '-')) { + if (!((state == st_in_number) || (state == st_after_number))) { throw std::runtime_error("unexpected separator"); } cur_number = maybe_from_end(cur_number, from_end, max); work.push_back(cur_number); cur_number = 0; from_end = false; - if (ch == ',') - { + if (ch == ',') { state = st_top; last_separator_was_dash = false; work.push_back(comma); - } - else if (ch == '-') - { - if (last_separator_was_dash) - { + } else if (ch == '-') { + if (last_separator_was_dash) { throw std::runtime_error("unexpected dash"); } state = st_top; last_separator_was_dash = true; work.push_back(dash); } - } - else if (ch == ':') - { - if (! ((state == st_in_number) || (state == st_after_number))) - { + } else if (ch == ':') { + if (!((state == st_in_number) || (state == st_after_number))) { throw std::runtime_error("unexpected colon"); } break; - } - else - { + } else { throw std::runtime_error("unexpected character"); } ++p; } - if ((state == st_in_number) || (state == st_after_number)) - { + if ((state == st_in_number) || (state == st_after_number)) { cur_number = maybe_from_end(cur_number, from_end, max); work.push_back(cur_number); - } - else - { + } else { throw std::runtime_error("number expected"); } - if (*p == ':') - { - if (strcmp(p, ":odd") == 0) - { + if (*p == ':') { + if (strcmp(p, ":odd") == 0) { skip = 2; - } - else if (strcmp(p, ":even") == 0) - { + } else if (strcmp(p, ":even") == 0) { skip = 2; start_idx = 1; - } - else - { + } else { throw std::runtime_error("unexpected even/odd modifier"); } } p = 0; - for (size_t i = 0; i < work.size(); i += 2) - { + for (size_t i = 0; i < work.size(); i += 2) { int num = work.at(i); // max == 0 means we don't know the max and are just // testing for valid syntax. - if ((max > 0) && ((num < 1) || (num > max))) - { + if ((max > 0) && ((num < 1) || (num > max))) { throw std::runtime_error( "number " + QUtil::int_to_string(num) + " out of range"); } - if (i == 0) - { + if (i == 0) { result.push_back(work.at(i)); - } - else - { - int separator = work.at(i-1); - if (separator == comma) - { + } else { + int separator = work.at(i - 1); + if (separator == comma) { result.push_back(num); - } - else if (separator == dash) - { + } else if (separator == dash) { int lastnum = result.back(); - if (num > lastnum) - { - for (int j = lastnum + 1; j <= num; ++j) - { + if (num > lastnum) { + for (int j = lastnum + 1; j <= num; ++j) { result.push_back(j); } - } - else - { - for (int j = lastnum - 1; j >= num; --j) - { + } else { + for (int j = lastnum - 1; j >= num; --j) { result.push_back(j); } } - } - else - { + } else { throw std::logic_error( "INTERNAL ERROR parsing numeric range"); } } } - if ((start_idx > 0) || (skip != 1)) - { + if ((start_idx > 0) || (skip != 1)) { auto t = result; result.clear(); - for (size_t i = start_idx; i < t.size(); i += skip) - { + for (size_t i = start_idx; i < t.size(); i += skip) { result.push_back(t.at(i)); } } - } - catch (std::runtime_error const& e) - { + } catch (std::runtime_error const& e) { std::string message; - if (p) - { + if (p) { message = "error at * in numeric range " + - std::string(range, QIntC::to_size(p - range)) + - "*" + p + ": " + e.what(); - } - else - { - message = "error in numeric range " + - std::string(range) + ": " + e.what(); + std::string(range, QIntC::to_size(p - range)) + "*" + p + ": " + + e.what(); + } else { + message = "error in numeric range " + std::string(range) + ": " + + e.what(); } throw std::runtime_error(message); } @@ -1615,93 +1451,92 @@ encode_winansi(unsigned long codepoint) // Use this ugly switch statement to avoid a static, which is not // thread-safe. unsigned char ch = '\0'; - switch (codepoint) - { - case 0x20ac: + switch (codepoint) { + case 0x20ac: ch = 0x80; break; - case 0x201a: + case 0x201a: ch = 0x82; break; - case 0x192: + case 0x192: ch = 0x83; break; - case 0x201e: + case 0x201e: ch = 0x84; break; - case 0x2026: + case 0x2026: ch = 0x85; break; - case 0x2020: + case 0x2020: ch = 0x86; break; - case 0x2021: + case 0x2021: ch = 0x87; break; - case 0x2c6: + case 0x2c6: ch = 0x88; break; - case 0x2030: + case 0x2030: ch = 0x89; break; - case 0x160: + case 0x160: ch = 0x8a; break; - case 0x2039: + case 0x2039: ch = 0x8b; break; - case 0x152: + case 0x152: ch = 0x8c; break; - case 0x17d: + case 0x17d: ch = 0x8e; break; - case 0x2018: + case 0x2018: ch = 0x91; break; - case 0x2019: + case 0x2019: ch = 0x92; break; - case 0x201c: + case 0x201c: ch = 0x93; break; - case 0x201d: + case 0x201d: ch = 0x94; break; - case 0x2022: + case 0x2022: ch = 0x95; break; - case 0x2013: + case 0x2013: ch = 0x96; break; - case 0x2014: + case 0x2014: ch = 0x97; break; - case 0x303: + case 0x303: ch = 0x98; break; - case 0x2122: + case 0x2122: ch = 0x99; break; - case 0x161: + case 0x161: ch = 0x9a; break; - case 0x203a: + case 0x203a: ch = 0x9b; break; - case 0x153: + case 0x153: ch = 0x9c; break; - case 0x17e: + case 0x17e: ch = 0x9e; break; - case 0x178: + case 0x178: ch = 0x9f; break; - case 0xa0: + case 0xa0: ch = 0xa0; break; - default: + default: break; } return ch; @@ -1713,345 +1548,344 @@ encode_macroman(unsigned long codepoint) // Use this ugly switch statement to avoid a static, which is not // thread-safe. unsigned char ch = '\0'; - switch (codepoint) - { - case 0xc4: + switch (codepoint) { + case 0xc4: ch = 0x80; break; - case 0xc5: + case 0xc5: ch = 0x81; break; - case 0xc7: + case 0xc7: ch = 0x82; break; - case 0xc9: + case 0xc9: ch = 0x83; break; - case 0xd1: + case 0xd1: ch = 0x84; break; - case 0xd6: + case 0xd6: ch = 0x85; break; - case 0xdc: + case 0xdc: ch = 0x86; break; - case 0xe1: + case 0xe1: ch = 0x87; break; - case 0xe0: + case 0xe0: ch = 0x88; break; - case 0xe2: + case 0xe2: ch = 0x89; break; - case 0xe4: + case 0xe4: ch = 0x8a; break; - case 0xe3: + case 0xe3: ch = 0x8b; break; - case 0xe5: + case 0xe5: ch = 0x8c; break; - case 0xe7: + case 0xe7: ch = 0x8d; break; - case 0xe9: + case 0xe9: ch = 0x8e; break; - case 0xe8: + case 0xe8: ch = 0x8f; break; - case 0xea: + case 0xea: ch = 0x90; break; - case 0xeb: + case 0xeb: ch = 0x91; break; - case 0xed: + case 0xed: ch = 0x92; break; - case 0xec: + case 0xec: ch = 0x93; break; - case 0xee: + case 0xee: ch = 0x94; break; - case 0xef: + case 0xef: ch = 0x95; break; - case 0xf1: + case 0xf1: ch = 0x96; break; - case 0xf3: + case 0xf3: ch = 0x97; break; - case 0xf2: + case 0xf2: ch = 0x98; break; - case 0xf4: + case 0xf4: ch = 0x99; break; - case 0xf6: + case 0xf6: ch = 0x9a; break; - case 0xf5: + case 0xf5: ch = 0x9b; break; - case 0xfa: + case 0xfa: ch = 0x9c; break; - case 0xf9: + case 0xf9: ch = 0x9d; break; - case 0xfb: + case 0xfb: ch = 0x9e; break; - case 0xfc: + case 0xfc: ch = 0x9f; break; - case 0x2020: + case 0x2020: ch = 0xa0; break; - case 0xb0: + case 0xb0: ch = 0xa1; break; - case 0xa2: + case 0xa2: ch = 0xa2; break; - case 0xa3: + case 0xa3: ch = 0xa3; break; - case 0xa7: + case 0xa7: ch = 0xa4; break; - case 0x2022: + case 0x2022: ch = 0xa5; break; - case 0xb6: + case 0xb6: ch = 0xa6; break; - case 0xdf: + case 0xdf: ch = 0xa7; break; - case 0xae: + case 0xae: ch = 0xa8; break; - case 0xa9: + case 0xa9: ch = 0xa9; break; - case 0x2122: + case 0x2122: ch = 0xaa; break; - case 0x301: + case 0x301: ch = 0xab; break; - case 0x308: + case 0x308: ch = 0xac; break; - case 0xc6: + case 0xc6: ch = 0xae; break; - case 0xd8: + case 0xd8: ch = 0xaf; break; - case 0xb1: + case 0xb1: ch = 0xb1; break; - case 0xa5: + case 0xa5: ch = 0xb4; break; - case 0x3bc: + case 0x3bc: ch = 0xb5; break; - case 0x1d43: + case 0x1d43: ch = 0xbb; break; - case 0x1d52: + case 0x1d52: ch = 0xbc; break; - case 0xe6: + case 0xe6: ch = 0xbe; break; - case 0xf8: + case 0xf8: ch = 0xbf; break; - case 0xbf: + case 0xbf: ch = 0xc0; break; - case 0xa1: + case 0xa1: ch = 0xc1; break; - case 0xac: + case 0xac: ch = 0xc2; break; - case 0x192: + case 0x192: ch = 0xc4; break; - case 0xab: + case 0xab: ch = 0xc7; break; - case 0xbb: + case 0xbb: ch = 0xc8; break; - case 0x2026: + case 0x2026: ch = 0xc9; break; - case 0xc0: + case 0xc0: ch = 0xcb; break; - case 0xc3: + case 0xc3: ch = 0xcc; break; - case 0xd5: + case 0xd5: ch = 0xcd; break; - case 0x152: + case 0x152: ch = 0xce; break; - case 0x153: + case 0x153: ch = 0xcf; break; - case 0x2013: + case 0x2013: ch = 0xd0; break; - case 0x2014: + case 0x2014: ch = 0xd1; break; - case 0x201c: + case 0x201c: ch = 0xd2; break; - case 0x201d: + case 0x201d: ch = 0xd3; break; - case 0x2018: + case 0x2018: ch = 0xd4; break; - case 0x2019: + case 0x2019: ch = 0xd5; break; - case 0xf7: + case 0xf7: ch = 0xd6; break; - case 0xff: + case 0xff: ch = 0xd8; break; - case 0x178: + case 0x178: ch = 0xd9; break; - case 0x2044: + case 0x2044: ch = 0xda; break; - case 0xa4: + case 0xa4: ch = 0xdb; break; - case 0x2039: + case 0x2039: ch = 0xdc; break; - case 0x203a: + case 0x203a: ch = 0xdd; break; - case 0xfb01: + case 0xfb01: ch = 0xde; break; - case 0xfb02: + case 0xfb02: ch = 0xdf; break; - case 0x2021: + case 0x2021: ch = 0xe0; break; - case 0xb7: + case 0xb7: ch = 0xe1; break; - case 0x201a: + case 0x201a: ch = 0xe2; break; - case 0x201e: + case 0x201e: ch = 0xe3; break; - case 0x2030: + case 0x2030: ch = 0xe4; break; - case 0xc2: + case 0xc2: ch = 0xe5; break; - case 0xca: + case 0xca: ch = 0xe6; break; - case 0xc1: + case 0xc1: ch = 0xe7; break; - case 0xcb: + case 0xcb: ch = 0xe8; break; - case 0xc8: + case 0xc8: ch = 0xe9; break; - case 0xcd: + case 0xcd: ch = 0xea; break; - case 0xce: + case 0xce: ch = 0xeb; break; - case 0xcf: + case 0xcf: ch = 0xec; break; - case 0xcc: + case 0xcc: ch = 0xed; break; - case 0xd3: + case 0xd3: ch = 0xee; break; - case 0xd4: + case 0xd4: ch = 0xef; break; - case 0xd2: + case 0xd2: ch = 0xf1; break; - case 0xda: + case 0xda: ch = 0xf2; break; - case 0xdb: + case 0xdb: ch = 0xf3; break; - case 0xd9: + case 0xd9: ch = 0xf4; break; - case 0x131: + case 0x131: ch = 0xf5; break; - case 0x2c6: + case 0x2c6: ch = 0xf6; break; - case 0x303: + case 0x303: ch = 0xf7; break; - case 0x304: + case 0x304: ch = 0xf8; break; - case 0x306: + case 0x306: ch = 0xf9; break; - case 0x307: + case 0x307: ch = 0xfa; break; - case 0x30a: + case 0x30a: ch = 0xfb; break; - case 0x327: + case 0x327: ch = 0xfc; break; - case 0x30b: + case 0x30b: ch = 0xfd; break; - case 0x328: + case 0x328: ch = 0xfe; break; - case 0x2c7: + case 0x2c7: ch = 0xff; break; - default: + default: break; } return ch; @@ -2063,171 +1897,165 @@ encode_pdfdoc(unsigned long codepoint) // Use this ugly switch statement to avoid a static, which is not // thread-safe. unsigned char ch = '\0'; - switch (codepoint) - { - case 0x02d8: + switch (codepoint) { + case 0x02d8: ch = 0x18; break; - case 0x02c7: + case 0x02c7: ch = 0x19; break; - case 0x02c6: + case 0x02c6: ch = 0x1a; break; - case 0x02d9: + case 0x02d9: ch = 0x1b; break; - case 0x02dd: + case 0x02dd: ch = 0x1c; break; - case 0x02db: + case 0x02db: ch = 0x1d; break; - case 0x02da: + case 0x02da: ch = 0x1e; break; - case 0x02dc: + case 0x02dc: ch = 0x1f; break; - case 0x2022: + case 0x2022: ch = 0x80; break; - case 0x2020: + case 0x2020: ch = 0x81; break; - case 0x2021: + case 0x2021: ch = 0x82; break; - case 0x2026: + case 0x2026: ch = 0x83; break; - case 0x2014: + case 0x2014: ch = 0x84; break; - case 0x2013: + case 0x2013: ch = 0x85; break; - case 0x0192: + case 0x0192: ch = 0x86; break; - case 0x2044: + case 0x2044: ch = 0x87; break; - case 0x2039: + case 0x2039: ch = 0x88; break; - case 0x203a: + case 0x203a: ch = 0x89; break; - case 0x2212: + case 0x2212: ch = 0x8a; break; - case 0x2030: + case 0x2030: ch = 0x8b; break; - case 0x201e: + case 0x201e: ch = 0x8c; break; - case 0x201c: + case 0x201c: ch = 0x8d; break; - case 0x201d: + case 0x201d: ch = 0x8e; break; - case 0x2018: + case 0x2018: ch = 0x8f; break; - case 0x2019: + case 0x2019: ch = 0x90; break; - case 0x201a: + case 0x201a: ch = 0x91; break; - case 0x2122: + case 0x2122: ch = 0x92; break; - case 0xfb01: + case 0xfb01: ch = 0x93; break; - case 0xfb02: + case 0xfb02: ch = 0x94; break; - case 0x0141: + case 0x0141: ch = 0x95; break; - case 0x0152: + case 0x0152: ch = 0x96; break; - case 0x0160: + case 0x0160: ch = 0x97; break; - case 0x0178: + case 0x0178: ch = 0x98; break; - case 0x017d: + case 0x017d: ch = 0x99; break; - case 0x0131: + case 0x0131: ch = 0x9a; break; - case 0x0142: + case 0x0142: ch = 0x9b; break; - case 0x0153: + case 0x0153: ch = 0x9c; break; - case 0x0161: + case 0x0161: ch = 0x9d; break; - case 0x017e: + case 0x017e: ch = 0x9e; break; - case 0xfffd: + case 0xfffd: ch = 0x9f; break; - case 0x20ac: + case 0x20ac: ch = 0xa0; break; - default: + default: break; } return ch; } -unsigned long get_next_utf8_codepoint( - std::string const& utf8_val, size_t& pos, bool& error) +unsigned long +get_next_utf8_codepoint(std::string const& utf8_val, size_t& pos, bool& error) { size_t len = utf8_val.length(); unsigned char ch = static_cast<unsigned char>(utf8_val.at(pos)); error = false; - if (ch < 128) - { + if (ch < 128) { return static_cast<unsigned long>(ch); } size_t bytes_needed = 0; unsigned bit_check = 0x40; unsigned char to_clear = 0x80; - while (ch & bit_check) - { + while (ch & bit_check) { ++bytes_needed; to_clear = static_cast<unsigned char>(to_clear | bit_check); bit_check >>= 1; } if (((bytes_needed > 5) || (bytes_needed < 1)) || - ((pos + bytes_needed) >= len)) - { + ((pos + bytes_needed) >= len)) { error = true; return 0xfffd; } unsigned long codepoint = static_cast<unsigned long>(ch & ~to_clear); - while (bytes_needed > 0) - { + while (bytes_needed > 0) { --bytes_needed; ch = static_cast<unsigned char>(utf8_val.at(++pos)); - if ((ch & 0xc0) != 0x80) - { + if ((ch & 0xc0) != 0x80) { --pos; codepoint = 0xfffd; break; @@ -2239,86 +2067,64 @@ unsigned long get_next_utf8_codepoint( } static bool -transcode_utf8(std::string const& utf8_val, std::string& result, - encoding_e encoding, char unknown) +transcode_utf8( + std::string const& utf8_val, + std::string& result, + encoding_e encoding, + char unknown) { bool okay = true; result.clear(); - if (encoding == e_utf16) - { + if (encoding == e_utf16) { result += "\xfe\xff"; } size_t len = utf8_val.length(); - for (size_t i = 0; i < len; ++i) - { + for (size_t i = 0; i < len; ++i) { bool error = false; unsigned long codepoint = get_next_utf8_codepoint(utf8_val, i, error); - if (error) - { + if (error) { okay = false; - if (encoding == e_utf16) - { + if (encoding == e_utf16) { result += "\xff\xfd"; - } - else - { + } else { result.append(1, unknown); } - } - else if (codepoint < 128) - { + } else if (codepoint < 128) { char ch = static_cast<char>(codepoint); - if (encoding == e_utf16) - { + if (encoding == e_utf16) { result += QUtil::toUTF16(QIntC::to_ulong(ch)); - } - else if ((encoding == e_pdfdoc) && - (((ch >= 0x18) && (ch <= 0x1f)) || (ch == 127))) - { + } else if ( + (encoding == e_pdfdoc) && + (((ch >= 0x18) && (ch <= 0x1f)) || (ch == 127))) { // PDFDocEncoding maps some low characters to Unicode, // so if we encounter those invalid UTF-8 code points, // map them to unknown so reversing the mapping // doesn't change them into other characters. okay = false; result.append(1, unknown); - } - else - { + } else { result.append(1, ch); } - } - else if (encoding == e_utf16) - { + } else if (encoding == e_utf16) { result += QUtil::toUTF16(codepoint); - } - else if ((codepoint == 0xad) && (encoding == e_pdfdoc)) - { + } else if ((codepoint == 0xad) && (encoding == e_pdfdoc)) { // PDFDocEncoding omits 0x00ad (soft hyphen). okay = false; result.append(1, unknown); - } - else if ((codepoint > 160) && (codepoint < 256) && - ((encoding == e_winansi) || (encoding == e_pdfdoc))) - { + } else if ( + (codepoint > 160) && (codepoint < 256) && + ((encoding == e_winansi) || (encoding == e_pdfdoc))) { result.append(1, static_cast<char>(codepoint & 0xff)); - } - else - { + } else { unsigned char ch = '\0'; - if (encoding == e_winansi) - { + if (encoding == e_winansi) { ch = encode_winansi(codepoint); - } - else if (encoding == e_macroman) - { + } else if (encoding == e_macroman) { ch = encode_macroman(codepoint); - } - else if (encoding == e_pdfdoc) - { + } else if (encoding == e_pdfdoc) { ch = encode_pdfdoc(codepoint); } - if (ch == '\0') - { + if (ch == '\0') { okay = false; ch = static_cast<unsigned char>(unknown); } @@ -2329,8 +2135,7 @@ transcode_utf8(std::string const& utf8_val, std::string& result, } static std::string -transcode_utf8(std::string const& utf8_val, encoding_e encoding, - char unknown) +transcode_utf8(std::string const& utf8_val, encoding_e encoding, char unknown) { std::string result; transcode_utf8(utf8_val, result, encoding, unknown); @@ -2368,29 +2173,29 @@ QUtil::utf8_to_pdf_doc(std::string const& utf8, char unknown_char) } bool -QUtil::utf8_to_ascii(std::string const& utf8, std::string& ascii, - char unknown_char) +QUtil::utf8_to_ascii( + std::string const& utf8, std::string& ascii, char unknown_char) { return transcode_utf8(utf8, ascii, e_ascii, unknown_char); } bool -QUtil::utf8_to_win_ansi(std::string const& utf8, std::string& win, - char unknown_char) +QUtil::utf8_to_win_ansi( + std::string const& utf8, std::string& win, char unknown_char) { return transcode_utf8(utf8, win, e_winansi, unknown_char); } bool -QUtil::utf8_to_mac_roman(std::string const& utf8, std::string& mac, - char unknown_char) +QUtil::utf8_to_mac_roman( + std::string const& utf8, std::string& mac, char unknown_char) { return transcode_utf8(utf8, mac, e_macroman, unknown_char); } bool -QUtil::utf8_to_pdf_doc(std::string const& utf8, std::string& pdfdoc, - char unknown_char) +QUtil::utf8_to_pdf_doc( + std::string const& utf8, std::string& pdfdoc, char unknown_char) { return transcode_utf8(utf8, pdfdoc, e_pdfdoc, unknown_char); } @@ -2398,9 +2203,10 @@ QUtil::utf8_to_pdf_doc(std::string const& utf8, std::string& pdfdoc, bool QUtil::is_utf16(std::string const& val) { - return ((val.length() >= 2) && - (((val.at(0) == '\xfe') && (val.at(1) == '\xff')) || - ((val.at(0) == '\xff') && (val.at(1) == '\xfe')))); + return ( + (val.length() >= 2) && + (((val.at(0) == '\xfe') && (val.at(1) == '\xff')) || + ((val.at(0) == '\xff') && (val.at(1) == '\xfe')))); } std::string @@ -2415,45 +2221,35 @@ QUtil::utf16_to_utf8(std::string const& val) size_t len = val.length(); size_t start = 0; bool is_le = false; - if (is_utf16(val)) - { - if (static_cast<unsigned char>(val.at(0)) == 0xff) - { + if (is_utf16(val)) { + if (static_cast<unsigned char>(val.at(0)) == 0xff) { is_le = true; } start += 2; } // If the string has an odd number of bytes, the last byte is // ignored. - for (size_t i = start; i + 1 < len; i += 2) - { + for (size_t i = start; i + 1 < len; i += 2) { // Convert from UTF16-BE. If we get a malformed // codepoint, this code will generate incorrect output // without giving a warning. Specifically, a high // codepoint not followed by a low codepoint will be // discarded, and a low codepoint not preceded by a high // codepoint will just get its low 10 bits output. - auto msb = is_le ? i+1 : i; - auto lsb = is_le ? i : i+1; - unsigned short bits = - QIntC::to_ushort( - (static_cast<unsigned char>(val.at(msb)) << 8) + - static_cast<unsigned char>(val.at(lsb))); - if ((bits & 0xFC00) == 0xD800) - { + auto msb = is_le ? i + 1 : i; + auto lsb = is_le ? i : i + 1; + unsigned short bits = QIntC::to_ushort( + (static_cast<unsigned char>(val.at(msb)) << 8) + + static_cast<unsigned char>(val.at(lsb))); + if ((bits & 0xFC00) == 0xD800) { codepoint = 0x10000U + ((bits & 0x3FFU) << 10U); continue; - } - else if ((bits & 0xFC00) == 0xDC00) - { - if (codepoint != 0) - { + } else if ((bits & 0xFC00) == 0xDC00) { + if (codepoint != 0) { QTC::TC("qpdf", "QUtil non-trivial UTF-16"); } codepoint += bits & 0x3FF; - } - else - { + } else { codepoint = bits; } @@ -2468,12 +2264,10 @@ QUtil::win_ansi_to_utf8(std::string const& val) { std::string result; size_t len = val.length(); - for (unsigned int i = 0; i < len; ++i) - { + for (unsigned int i = 0; i < len; ++i) { unsigned char ch = static_cast<unsigned char>(val.at(i)); unsigned short ch_short = ch; - if ((ch >= 128) && (ch <= 160)) - { + if ((ch >= 128) && (ch <= 160)) { ch_short = win_ansi_to_unicode[ch - 128]; } result += QUtil::toUTF8(ch_short); @@ -2486,12 +2280,10 @@ QUtil::mac_roman_to_utf8(std::string const& val) { std::string result; size_t len = val.length(); - for (unsigned int i = 0; i < len; ++i) - { + for (unsigned int i = 0; i < len; ++i) { unsigned char ch = static_cast<unsigned char>(val.at(i)); unsigned short ch_short = ch; - if (ch >= 128) - { + if (ch >= 128) { ch_short = mac_roman_to_unicode[ch - 128]; } result += QUtil::toUTF8(ch_short); @@ -2504,20 +2296,14 @@ QUtil::pdf_doc_to_utf8(std::string const& val) { std::string result; size_t len = val.length(); - for (unsigned int i = 0; i < len; ++i) - { + for (unsigned int i = 0; i < len; ++i) { unsigned char ch = static_cast<unsigned char>(val.at(i)); unsigned short ch_short = ch; - if ((ch >= 127) && (ch <= 160)) - { + if ((ch >= 127) && (ch <= 160)) { ch_short = pdf_doc_to_unicode[ch - 127]; - } - else if ((ch >= 24) && (ch <= 31)) - { + } else if ((ch >= 24) && (ch <= 31)) { ch_short = pdf_doc_low_to_unicode[ch - 24]; - } - else if (ch == 173) - { + } else if (ch == 173) { ch_short = 0xfffd; } result += QUtil::toUTF8(ch_short); @@ -2526,35 +2312,31 @@ QUtil::pdf_doc_to_utf8(std::string const& val) } void -QUtil::analyze_encoding(std::string const& val, - bool& has_8bit_chars, - bool& is_valid_utf8, - bool& is_utf16) +QUtil::analyze_encoding( + std::string const& val, + bool& has_8bit_chars, + bool& is_valid_utf8, + bool& is_utf16) { has_8bit_chars = is_utf16 = is_valid_utf8 = false; - if (QUtil::is_utf16(val)) - { + if (QUtil::is_utf16(val)) { has_8bit_chars = true; is_utf16 = true; return; } size_t len = val.length(); bool any_errors = false; - for (size_t i = 0; i < len; ++i) - { + for (size_t i = 0; i < len; ++i) { bool error = false; unsigned long codepoint = get_next_utf8_codepoint(val, i, error); - if (error) - { + if (error) { any_errors = true; } - if (codepoint >= 128) - { + if (codepoint >= 128) { has_8bit_chars = true; } } - if (has_8bit_chars && (! any_errors)) - { + if (has_8bit_chars && (!any_errors)) { is_valid_utf8 = true; } } @@ -2569,37 +2351,29 @@ QUtil::possible_repaired_encodings(std::string supplied) bool is_valid_utf8 = false; bool is_utf16 = false; analyze_encoding(supplied, has_8bit_chars, is_valid_utf8, is_utf16); - if (! has_8bit_chars) - { + if (!has_8bit_chars) { return result; } - if (is_utf16) - { + if (is_utf16) { // Convert to UTF-8 and pretend we got a UTF-8 string. is_utf16 = false; is_valid_utf8 = true; supplied = utf16_to_utf8(supplied); } std::string output; - if (is_valid_utf8) - { + if (is_valid_utf8) { // Maybe we were given UTF-8 but wanted one of the single-byte // encodings. - if (utf8_to_pdf_doc(supplied, output)) - { + if (utf8_to_pdf_doc(supplied, output)) { result.push_back(output); } - if (utf8_to_win_ansi(supplied, output)) - { + if (utf8_to_win_ansi(supplied, output)) { result.push_back(output); } - if (utf8_to_mac_roman(supplied, output)) - { + if (utf8_to_mac_roman(supplied, output)) { result.push_back(output); } - } - else - { + } else { // Maybe we were given one of the single-byte encodings but // wanted UTF-8. std::string from_pdf_doc(pdf_doc_to_utf8(supplied)); @@ -2611,28 +2385,22 @@ QUtil::possible_repaired_encodings(std::string supplied) // Maybe we were given one of the other single-byte encodings // but wanted one of the other ones. - if (utf8_to_win_ansi(from_pdf_doc, output)) - { + if (utf8_to_win_ansi(from_pdf_doc, output)) { result.push_back(output); } - if (utf8_to_mac_roman(from_pdf_doc, output)) - { + if (utf8_to_mac_roman(from_pdf_doc, output)) { result.push_back(output); } - if (utf8_to_pdf_doc(from_win_ansi, output)) - { + if (utf8_to_pdf_doc(from_win_ansi, output)) { result.push_back(output); } - if (utf8_to_mac_roman(from_win_ansi, output)) - { + if (utf8_to_mac_roman(from_win_ansi, output)) { result.push_back(output); } - if (utf8_to_pdf_doc(from_mac_roman, output)) - { + if (utf8_to_pdf_doc(from_mac_roman, output)) { result.push_back(output); } - if (utf8_to_win_ansi(from_mac_roman, output)) - { + if (utf8_to_win_ansi(from_mac_roman, output)) { result.push_back(output); } } @@ -2640,10 +2408,9 @@ QUtil::possible_repaired_encodings(std::string supplied) std::vector<std::string> t; std::set<std::string> seen; for (std::vector<std::string>::iterator iter = result.begin(); - iter != result.end(); ++iter) - { - if (! seen.count(*iter)) - { + iter != result.end(); + ++iter) { + if (!seen.count(*iter)) { seen.insert(*iter); t.push_back(*iter); } @@ -2653,8 +2420,11 @@ QUtil::possible_repaired_encodings(std::string supplied) #ifndef QPDF_NO_WCHAR_T static int -call_main_from_wmain(bool, int argc, wchar_t const* const argv[], - std::function<int(int, char*[])> realmain) +call_main_from_wmain( + bool, + int argc, + wchar_t const* const argv[], + std::function<int(int, char*[])> realmain) { // argv contains UTF-16-encoded strings with a 16-bit wchar_t. // Convert this to UTF-8-encoded strings for compatibility with @@ -2662,24 +2432,20 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[], // arguments are UTF-8. std::vector<std::unique_ptr<char[]>> utf8_argv; - for (int i = 0; i < argc; ++i) - { + for (int i = 0; i < argc; ++i) { std::string utf16; - for (size_t j = 0; j < std::wcslen(argv[i]); ++j) - { + for (size_t j = 0; j < std::wcslen(argv[i]); ++j) { unsigned short codepoint = static_cast<unsigned short>(argv[i][j]); - utf16.append(1, static_cast<char>( - QIntC::to_uchar(codepoint >> 8))); - utf16.append(1, static_cast<char>( - QIntC::to_uchar(codepoint & 0xff))); + utf16.append(1, static_cast<char>(QIntC::to_uchar(codepoint >> 8))); + utf16.append( + 1, static_cast<char>(QIntC::to_uchar(codepoint & 0xff))); } std::string utf8 = QUtil::utf16_to_utf8(utf16); utf8_argv.push_back(QUtil::make_unique_cstr(utf8)); } - auto utf8_argv_sp = std::make_unique<char*[]>(1+utf8_argv.size()); + auto utf8_argv_sp = std::make_unique<char*[]>(1 + utf8_argv.size()); char** new_argv = utf8_argv_sp.get(); - for (size_t i = 0; i < utf8_argv.size(); ++i) - { + for (size_t i = 0; i < utf8_argv.size(); ++i) { new_argv[i] = utf8_argv.at(i).get(); } argc = QIntC::to_int(utf8_argv.size()); @@ -2688,15 +2454,16 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[], } int -QUtil::call_main_from_wmain(int argc, wchar_t* argv[], - std::function<int(int, char*[])> realmain) +QUtil::call_main_from_wmain( + int argc, wchar_t* argv[], std::function<int(int, char*[])> realmain) { return ::call_main_from_wmain(true, argc, argv, realmain); } int QUtil::call_main_from_wmain( - int argc, wchar_t const* const argv[], + int argc, + wchar_t const* const argv[], std::function<int(int, char const* const[])> realmain) { return ::call_main_from_wmain( |