From 9cbad234b91dcee87431a4e0c906c6870f7a912c Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sun, 15 Dec 2024 14:28:07 +0100 Subject: [PATCH] Use a stricter clang-tidy config, retire clang-analyze --- .clang-tidy | 3 + .github/workflows/ccpp.yml | 2 - Makefile | 3 - bytestream | 2 +- lib/baselinify.cpp | 7 +- lib/curlrequester.cpp | 14 ++-- lib/curlrequester.h | 10 +-- lib/ippattr.cpp | 18 +++--- lib/ippattr.h | 24 +++---- lib/ippdiscovery.cpp | 77 +++++++++++----------- lib/ippdiscovery.h | 17 +++-- lib/ippmsg.cpp | 26 ++++---- lib/ippmsg.h | 7 +- lib/ippprinter.cpp | 17 +++-- lib/ippprinter.h | 10 +-- lib/ippprintjob.cpp | 8 +-- lib/ippprintjob.h | 6 +- lib/minimime.cpp | 12 ++-- lib/minimime.h | 12 ++-- lib/pdf2printable.cpp | 37 ++++++----- lib/pdf2printable.h | 4 +- lib/ppm2pwg.cpp | 8 +-- lib/ppm2pwg.h | 2 +- lib/printparameters.cpp | 16 ++--- lib/printparameters.h | 6 +- lib/pwg2ppm.cpp | 11 +++- lib/pwg2ppm.h | 2 +- lib/stringutils.h | 12 ++-- lib/url.h | 6 +- tests/test.cpp | 122 +++++++++++++++++------------------ utils/bsplit.cpp | 2 +- utils/ippclient.cpp | 22 +++---- utils/ippdiscover.cpp | 4 +- utils/pdf2printable_main.cpp | 4 +- utils/ppm2pwg_main.cpp | 9 ++- 35 files changed, 284 insertions(+), 258 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..953e21d --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,3 @@ +Checks: 'clang-analyzer-*,bugprone-*,performance-*,readability-*,modernize-*,-bugprone-easily-swappable-parameters,-bugprone-assignment-in-if-condition,-bugprone-narrowing-conversions,-bugprone-implicit-widening-of-multiplication-result,-bugprone-branch-clone,-bugprone-string-literal-with-embedded-nul,-bugprone-macro-parentheses,-bugprone-exception-escape,-performance-avoid-endl,-readability-identifier-length,-readability-named-parameter,-readability-implicit-bool-conversion,-readability-magic-numbers,-readability-else-after-return,-readability-function-cognitive-complexity,-readability-avoid-nested-conditional-operator,-readability-convert-member-functions-to-static,-readability-container-size-empty,-readability-use-anyofallof,-modernize-use-trailing-return-type,-modernize-use-auto,-modernize-return-braced-init-list' +WarningsAsErrors: '*' +FormatStyle: 'file' diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 91738f1..8ec494d 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -23,7 +23,5 @@ jobs: - name: test run: ./test working-directory: ./tests - - name: analyze - run: make analyze - name: tidy run: make tidy diff --git a/Makefile b/Makefile index 0ec33bb..027d384 100644 --- a/Makefile +++ b/Makefile @@ -67,9 +67,6 @@ ippdiscover: ippdiscover.o ippdiscovery.o bytestream.o clean: rm -f *.o $(OFFICIAL) $(EXTRAS) -analyze: - $(CLANGXX) --analyze $(CXXFLAGS) $(SILLY_CLANG_FLAGS) lib/*.cpp utils/*.cpp - tidy: $(CLANG_TIDY) lib/*.cpp utils/*.cpp -- $(CXXFLAGS) diff --git a/bytestream b/bytestream index 95f275d..1d584bc 160000 --- a/bytestream +++ b/bytestream @@ -1 +1 @@ -Subproject commit 95f275d2d3109c482a307749cd291db8c7c28b97 +Subproject commit 1d584bccb6ab6f90594c27858906206a5a934f5c diff --git a/lib/baselinify.cpp b/lib/baselinify.cpp index 2d3751d..7d35e8c 100644 --- a/lib/baselinify.cpp +++ b/lib/baselinify.cpp @@ -20,7 +20,7 @@ struct bts_source_mgr: jpeg_source_mgr jpeg_source_mgr::skip_input_data = skip_input_data; jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; jpeg_source_mgr::term_source = term_source; - jpeg_source_mgr::bytes_in_buffer = (size_t)bts.size(); + jpeg_source_mgr::bytes_in_buffer = bts.size(); jpeg_source_mgr::next_input_byte = (const JOCTET *)bts.raw(); } @@ -73,7 +73,7 @@ struct bts_destination_mgr: jpeg_destination_mgr dest->bts.putBytes(dest->buffer, size); } - JOCTET buffer[BS_REASONABLE_FILE_SIZE]; + Array buffer = Array(BS_REASONABLE_FILE_SIZE); Bytestream& bts; }; @@ -81,7 +81,8 @@ void baselinify(Bytestream& inBts, Bytestream& outBts) { struct jpeg_decompress_struct srcInfo; struct jpeg_compress_struct dstInfo; - struct jpeg_error_mgr jSrcErr, jDstErr; + struct jpeg_error_mgr jSrcErr; + struct jpeg_error_mgr jDstErr; jvirt_barray_ptr* coefArrays; #if MADNESS diff --git a/lib/curlrequester.cpp b/lib/curlrequester.cpp index 5623e17..5939463 100644 --- a/lib/curlrequester.cpp +++ b/lib/curlrequester.cpp @@ -6,7 +6,7 @@ #include #include -CurlRequester::CurlRequester(std::string addr, bool ignoreSslErrors) +CurlRequester::CurlRequester(const std::string& addr, bool ignoreSslErrors) : _curl(curl_easy_init()) { curl_easy_setopt(_curl, CURLOPT_URL, addr.c_str()); @@ -21,7 +21,7 @@ CurlRequester::CurlRequester(std::string addr, bool ignoreSslErrors) curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYSTATUS, 0L); } - _opts = NULL; + _opts = nullptr; #ifdef USER_AGENT _opts = curl_slist_append(_opts, "User-Agent: " USER_AGENT); #endif @@ -156,7 +156,7 @@ void CurlIppPosterBase::setCompression(Compression compression) deflateInit2(&_zstrm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, level, 7, Z_DEFAULT_STRATEGY); } -std::string http_url(std::string str) +std::string http_url(const std::string& str) { Url url(str); if(url.getScheme() == "ipp") @@ -178,7 +178,7 @@ std::string http_url(std::string str) return url.toStr(); } -CurlIppPosterBase::CurlIppPosterBase(std::string addr, bool ignoreSslErrors) +CurlIppPosterBase::CurlIppPosterBase(const std::string& addr, bool ignoreSslErrors) : CurlRequester(http_url(addr), ignoreSslErrors) { _canWrite.unlock(); @@ -207,7 +207,7 @@ CURLcode CurlIppPosterBase::await(Bytestream* data) return CurlRequester::await(data); } -CurlIppPoster::CurlIppPoster(std::string addr, Bytestream&& data, bool ignoreSslErrors) +CurlIppPoster::CurlIppPoster(const std::string& addr, Bytestream&& data, bool ignoreSslErrors) : CurlIppPosterBase(addr, ignoreSslErrors) { curl_easy_setopt(_curl, CURLOPT_POSTFIELDSIZE, data.size()); @@ -215,14 +215,14 @@ CurlIppPoster::CurlIppPoster(std::string addr, Bytestream&& data, bool ignoreSsl doRun(); } -CurlIppStreamer::CurlIppStreamer(std::string addr, bool ignoreSslErrors) +CurlIppStreamer::CurlIppStreamer(const std::string& addr, bool ignoreSslErrors) : CurlIppPosterBase(addr, ignoreSslErrors) { _opts = curl_slist_append(_opts, "Transfer-Encoding: chunked"); doRun(); } -CurlHttpGetter::CurlHttpGetter(std::string addr, bool ignoreSslErrors) +CurlHttpGetter::CurlHttpGetter(const std::string& addr, bool ignoreSslErrors) : CurlRequester(addr, ignoreSslErrors) { doRun(); diff --git a/lib/curlrequester.h b/lib/curlrequester.h index 462a703..a45a839 100644 --- a/lib/curlrequester.h +++ b/lib/curlrequester.h @@ -33,7 +33,7 @@ class CurlRequester protected: - CurlRequester(std::string addr, bool ignoreSslErrors); + CurlRequester(const std::string& addr, bool ignoreSslErrors); void doRun(); @@ -94,7 +94,7 @@ class CurlIppPosterBase : public CurlRequester } protected: - CurlIppPosterBase(std::string addr, bool ignoreSslErrors); + CurlIppPosterBase(const std::string& addr, bool ignoreSslErrors); private: std::mutex _canWrite; @@ -106,19 +106,19 @@ class CurlIppPosterBase : public CurlRequester class CurlIppPoster : public CurlIppPosterBase { public: - CurlIppPoster(std::string addr, Bytestream&& data, bool ignoreSslErrors = false); + CurlIppPoster(const std::string& addr, Bytestream&& data, bool ignoreSslErrors = false); }; class CurlIppStreamer : public CurlIppPosterBase { public: - CurlIppStreamer(std::string addr, bool ignoreSslErrors = false); + CurlIppStreamer(const std::string& addr, bool ignoreSslErrors = false); }; class CurlHttpGetter : public CurlRequester { public: - CurlHttpGetter(std::string addr, bool ignoreSslErrors = false); + CurlHttpGetter(const std::string& addr, bool ignoreSslErrors = false); }; #endif // CURLREQUESTER_H diff --git a/lib/ippattr.cpp b/lib/ippattr.cpp index 82c30bf..e289306 100644 --- a/lib/ippattr.cpp +++ b/lib/ippattr.cpp @@ -19,7 +19,7 @@ std::string IppIntRange::toStr() const return ss.str(); } -IppIntRange IppIntRange::fromJSON(const Json::object json) +IppIntRange IppIntRange::fromJSON(const Json::object& json) { return IppIntRange {json.at("low").int_value(), json.at("high").int_value()}; } @@ -41,7 +41,7 @@ std::string IppResolution::toStr() const return ss.str(); } -IppResolution IppResolution::fromJSON(const Json::object json) +IppResolution IppResolution::fromJSON(const Json::object& json) { return IppResolution {(uint32_t)json.at("x").int_value(), (uint32_t)json.at("y").int_value(), @@ -80,7 +80,7 @@ std::string IppDateTime::toStr() const return ss.str(); } -IppDateTime IppDateTime::fromJSON(const Json::object) +IppDateTime IppDateTime::fromJSON(const Json::object&) { // TODO return IppDateTime(); @@ -103,7 +103,7 @@ IppOneSetOf IppAttr::asList() const } } -IppAttr IppAttr::fromString(std::string string, IppTag tag) +IppAttr IppAttr::fromString(const std::string& string, IppTag tag) { switch(tag) { @@ -157,7 +157,7 @@ IppAttr IppAttr::fromString(std::string string, IppTag tag) } } -IppAttr IppAttr::fromJSON(Json::object json) +IppAttr IppAttr::fromJSON(const Json::object& json) { IppTag tag = (IppTag)json.at("tag").int_value(); return IppAttr(tag, valuefromJSON(tag, json.at("value"))); @@ -218,7 +218,7 @@ IppValue IppAttr::valuefromJSON(IppTag tag, const Json& json) } } -Json IppAttr::valueToJSON(IppValue value) +Json IppAttr::valueToJSON(const IppValue& value) { Json j; if(value.is()) @@ -317,17 +317,17 @@ std::ostream& operator<<(std::ostream& os, const IppValue& iv) return os; } -bool IppCollection::has(std::string key) const +bool IppCollection::has(const std::string& key) const { return find(key) != end(); } -void IppCollection::set(std::string key, IppAttr value) +void IppCollection::set(const std::string& key, IppAttr value) { insert_or_assign(key, value); } -IppAttrs IppAttrs::fromJSON(Json::object json) +IppAttrs IppAttrs::fromJSON(const Json::object& json) { IppAttrs attrs; for(const auto& [key, value] : json) diff --git a/lib/ippattr.h b/lib/ippattr.h index f82f45b..342a50e 100644 --- a/lib/ippattr.h +++ b/lib/ippattr.h @@ -55,7 +55,7 @@ struct IppIntRange bool operator==(const IppIntRange& other) const; std::string toStr() const; - static IppIntRange fromJSON(const Json::object json); + static IppIntRange fromJSON(const Json::object& json); Json::object toJSON() const; }; @@ -74,7 +74,7 @@ struct IppResolution bool operator==(const IppResolution& other) const; std::string toStr() const; - static IppResolution fromJSON(const Json::object json); + static IppResolution fromJSON(const Json::object& json); Json::object toJSON() const; }; @@ -93,7 +93,7 @@ struct IppDateTime bool operator==(const IppDateTime& other) const; std::string toStr() const; - static IppDateTime fromJSON(const Json::object json); + static IppDateTime fromJSON(const Json::object& json); Json toJSON() const; }; @@ -109,8 +109,8 @@ struct IppOneSetOf: public List struct IppCollection: public std::map { using std::map::map; - bool has(std::string key) const; - void set(std::string key, IppAttr value); + bool has(const std::string& key) const; + void set(const std::string& key, IppAttr value); }; class IppAttr : public IppValue @@ -124,14 +124,14 @@ class IppAttr : public IppValue IppTag tag() const {return _tag;} IppValue value() const {return *this;} - static IppAttr fromString(std::string string, IppTag tag); + static IppAttr fromString(const std::string& string, IppTag tag); - static IppAttr fromJSON(Json::object json); + static IppAttr fromJSON(const Json::object& json); Json toJSON() const; private: static IppValue valuefromJSON(IppTag tag, const Json& json); - static Json valueToJSON(IppValue value); + static Json valueToJSON(const IppValue& value); IppTag _tag; @@ -141,7 +141,7 @@ struct IppAttrs: public std::map { using std::map::map; - bool has(std::string key) const + bool has(const std::string& key) const { return find(key) != end(); } @@ -159,13 +159,13 @@ struct IppAttrs: public std::map } } - void set(std::string key, IppAttr value) + void set(const std::string& key, IppAttr value) { insert_or_assign(key, value); } template - List getList(std::string name) const + List getList(const std::string& name) const { List res; if(has(name)) @@ -178,7 +178,7 @@ struct IppAttrs: public std::map return res; } - static IppAttrs fromJSON(Json::object json); + static IppAttrs fromJSON(const Json::object& json); Json toJSON() const; }; diff --git a/lib/ippdiscovery.cpp b/lib/ippdiscovery.cpp index 406fe89..c28fb1d 100644 --- a/lib/ippdiscovery.cpp +++ b/lib/ippdiscovery.cpp @@ -11,22 +11,14 @@ using namespace std::literals; -#define A 1 -#define PTR 12 -#define TXT 16 -#define AAAA 28 -#define SRV 33 - -#define ALL 255 //for querying - -std::ostream& operator<<(std::ostream& os, List sl) +std::ostream& operator<<(std::ostream& os, const List& sl) { os << join_string(sl, ", "); return os; } template -std::ostream& operator<<(std::ostream& os, Map m) +std::ostream& operator<<(std::ostream& os, const Map& m) { List sl; for(const std::pair p : m) @@ -47,19 +39,19 @@ std::string ip4str(uint32_t ip) + std::to_string(ip & 0xff); } -std::string make_addr(std::string proto, uint16_t defaultPort, uint16_t port, std::string ip, std::string rp) +std::string make_addr(const std::string& proto, uint16_t defaultPort, uint16_t port, const std::string& ip, const std::string& rp) { std::string maybePort = port != defaultPort ? ":"+std::to_string(port) : ""; std::string addr = proto+"://"+ip+maybePort+"/"+rp; return addr; } -std::string make_ipp_addr(uint16_t port, std::string ip, std::string rp) +std::string make_ipp_addr(uint16_t port, const std::string& ip, const std::string& rp) { return make_addr("ipp", 631, port, ip, rp); } -std::string make_ipps_addr(uint16_t port, std::string ip, std::string rp) +std::string make_ipps_addr(uint16_t port, const std::string& ip, const std::string& rp) { return make_addr("ipps", 443, port, ip, rp); } @@ -96,15 +88,11 @@ std::string get_addr_str(Bytestream& bts) } IppDiscovery::IppDiscovery(std::function callback) -: _callback(callback) -{ -} - -IppDiscovery::~IppDiscovery() +: _callback(std::move(callback)) { } -void IppDiscovery::sendQuery(uint16_t qtype, List addrs) +void IppDiscovery::sendQuery(QType qtype, List addrs) { std::chrono::time_point nowClock = std::chrono::system_clock::now(); std::time_t now = std::chrono::system_clock::to_time_t(nowClock); @@ -128,7 +116,7 @@ void IppDiscovery::sendQuery(uint16_t qtype, List addrs) return; } - DBG(<< "querying " << qtype << " " << addrs); + DBG(<< "querying " << (uint16_t)qtype << " " << addrs); Bytestream query; Map suffixPositions; @@ -138,12 +126,13 @@ void IppDiscovery::sendQuery(uint16_t qtype, List addrs) query << _transactionId++ << flags << questions << (uint16_t)0 << (uint16_t)0 << (uint16_t)0; - for(std::string addr : addrs) + for(const std::string& addr : addrs) { _outstandingQueries.insert({{qtype, addr}, now}); List addrParts = split_string(addr, "."); - std::string addrPart, restAddr; + std::string addrPart; + std::string restAddr; while(!addrParts.empty()) { restAddr = join_string(addrParts, "."); @@ -166,7 +155,7 @@ void IppDiscovery::sendQuery(uint16_t qtype, List addrs) query << (uint8_t)0; } - query << qtype << (uint16_t)0x0001; + query << (uint16_t)qtype << (uint16_t)0x0001; } @@ -177,9 +166,10 @@ void IppDiscovery::sendQuery(uint16_t qtype, List addrs) void IppDiscovery::update() { List> ippsIpRps; - std::string target, rp; + std::string target; + std::string rp; - for(std::string it : _ippsPtrs) + for(const std::string& it : _ippsPtrs) { if(!_targets.contains(it) || !_ports.contains(it) || !(_TXTs.contains(it) && _TXTs[it].contains("rp"))) { @@ -192,7 +182,7 @@ void IppDiscovery::update() if(_As.contains(target)) { - for(std::string ip : _As.at(target)) + for(const std::string& ip : _As.at(target)) { std::string addr = make_ipps_addr(port, ip, rp); if(!_found.contains(addr)) @@ -205,7 +195,7 @@ void IppDiscovery::update() } } - for(std::string it : _ippPtrs) + for(const std::string& it : _ippPtrs) { if(!_targets.contains(it) || !_ports.contains(it) || !(_TXTs.contains(it) && _TXTs[it].contains("rp"))) { @@ -218,7 +208,7 @@ void IppDiscovery::update() if(_As.contains(target)) { - for(std::string ip : _As.at(target)) + for(const std::string& ip : _As.at(target)) { std::string addr = make_ipp_addr(port, ip, rp); if(!_found.contains(addr) && !ippsIpRps.contains({ip, rp})) @@ -231,9 +221,9 @@ void IppDiscovery::update() } } -void IppDiscovery::updateAndQueryPtrs(List& ptrs, List newPtrs) +void IppDiscovery::updateAndQueryPtrs(List& ptrs, const List& newPtrs) { - for(std::string ptr : newPtrs) + for(const std::string& ptr : newPtrs) { if(ptrs.contains(ptr)) { @@ -268,9 +258,17 @@ void IppDiscovery::discover() List newIppsPtrs; List newTargets; - std::string qaddr, aaddr, tmpname, target; + std::string qaddr; + std::string aaddr; + std::string tmpname; + std::string target; - uint16_t transactionidResp, flags, questions, answerRRs, authRRs, addRRs; + uint16_t transactionidResp; + uint16_t flags; + uint16_t questions; + uint16_t answerRRs; + uint16_t authRRs; + uint16_t addRRs; try { @@ -279,14 +277,17 @@ void IppDiscovery::discover() for(size_t i = 0; i < questions; i++) { - uint16_t qtype, qflags; + uint16_t qtype; + uint16_t qflags; qaddr = get_addr_str(resp); resp >> qtype >> qflags; } for(size_t i = 0; i < totalRRs; i++) { - uint16_t atype, aflags, len; + uint16_t atype; + uint16_t aflags; + uint16_t len; uint32_t ttl; aaddr = get_addr_str(resp); @@ -313,7 +314,7 @@ void IppDiscovery::discover() while(resp.pos() < pos_before+len) { std::string txt = resp.getString(resp.get()); - size_t pos = txt.find("="); + size_t pos = txt.find('='); std::string key = txt.substr(0, pos); std::string value = txt.substr(pos+1); _TXTs[aaddr][key] = value; @@ -322,7 +323,9 @@ void IppDiscovery::discover() } case SRV: { - uint16_t prio, w, port; + uint16_t prio; + uint16_t w; + uint16_t port; resp >> prio >> w >> port; target = get_addr_str(resp); _ports[aaddr] = port; @@ -375,7 +378,7 @@ void IppDiscovery::discover() List unresolvedAddrs; - for(std::string t : newTargets) + for(const std::string& t : newTargets) { // If target does not resolve to an address, query about it if(!_As.contains(t)) diff --git a/lib/ippdiscovery.h b/lib/ippdiscovery.h index c30be41..1035136 100644 --- a/lib/ippdiscovery.h +++ b/lib/ippdiscovery.h @@ -14,19 +14,28 @@ class IppDiscovery { public: + enum QType : uint16_t + { + A = 1, + PTR = 12, + TXT = 16, + AAAA = 28, + SRV = 33, + ALL = 255 //for querying + }; + IppDiscovery(std::function callback); IppDiscovery() = delete; IppDiscovery(const IppDiscovery&) = delete; IppDiscovery& operator=(const IppDiscovery&) = delete; - ~IppDiscovery(); void discover(); private: - void sendQuery(uint16_t qtype, List addrs); + void sendQuery(QType qtype, List addrs); void update(); - void updateAndQueryPtrs(List& ptrs, List new_ptrs); + void updateAndQueryPtrs(List& ptrs, const List& newPtrs); UdpSocket _socket = UdpSocket("224.0.0.251", 5353); @@ -41,7 +50,7 @@ class IppDiscovery Map> _As; - Map, std::time_t> _outstandingQueries; + Map, std::time_t> _outstandingQueries; List _found; std::function _callback; diff --git a/lib/ippmsg.cpp b/lib/ippmsg.cpp index 953fec1..9e566bc 100644 --- a/lib/ippmsg.cpp +++ b/lib/ippmsg.cpp @@ -4,18 +4,18 @@ uint32_t IppMsg::_reqId=1; -IppMsg::IppMsg(Bytestream& bts) +IppMsg::IppMsg(Bytestream& msg) { uint32_t reqId; - bts >> _majVsn >> _minVsn >> _opOrStatus >> reqId; + msg >> _majVsn >> _minVsn >> _opOrStatus >> reqId; IppAttrs attrs; IppTag currentAttrType = IppTag::EndAttrs; - while(!bts.atEnd()) + while(!msg.atEnd()) { - if(bts.peek() <= (uint8_t)IppTag::UnsupportedAttrs) + if(msg.peek() <= (uint8_t)IppTag::UnsupportedAttrs) { if(currentAttrType == IppTag::OpAttrs) { @@ -37,23 +37,22 @@ IppMsg::IppMsg(Bytestream& bts) #endif } - if(bts >>= (uint8_t)IppTag::EndAttrs) + if(msg >>= (uint8_t)IppTag::EndAttrs) { break; } - currentAttrType = (IppTag)bts.get(); + currentAttrType = (IppTag)msg.get(); attrs = IppAttrs(); } else { - consumeAttributes(attrs, bts); + consumeAttributes(attrs, msg); } } } -IppMsg::IppMsg(uint16_t opOrStatus, IppAttrs opAttrs, - IppAttrs jobAttrs, IppAttrs printerAttrs) +IppMsg::IppMsg(uint16_t opOrStatus, const IppAttrs& opAttrs, const IppAttrs& jobAttrs, const IppAttrs& printerAttrs) { _opOrStatus = opOrStatus; _opAttrs = opAttrs; @@ -105,7 +104,7 @@ Bytestream IppMsg::encode() const return ipp; } -void IppMsg::setOpAttr(std::string name, IppAttr attr) +void IppMsg::setOpAttr(const std::string& name, const IppAttr& attr) { _opAttrs.insert({name, attr}); } @@ -116,10 +115,10 @@ void IppMsg::setVersion(uint8_t majVsn, uint8_t minVsn) _minVsn = minVsn; } -IppAttrs IppMsg::baseOpAttrs(std::string url) +IppAttrs IppMsg::baseOpAttrs(const std::string& url) { char* user = getenv("USER"); - std::string name = user ? user : "anonymous"; + std::string name = user != nullptr ? user : "anonymous"; IppAttrs o { {"attributes-charset", IppAttr(IppTag::Charset, "utf-8")}, @@ -439,6 +438,7 @@ void IppMsg::encodeValue(Bytestream& msg, IppTag tag, const IppValue& val) const } } +#ifdef FUZZ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) { Bytestream bts(Data, Size); @@ -449,7 +449,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) } catch(const std::exception& e) { - } return 0; } +#endif diff --git a/lib/ippmsg.h b/lib/ippmsg.h index 60a4172..5c77181 100644 --- a/lib/ippmsg.h +++ b/lib/ippmsg.h @@ -38,8 +38,7 @@ class IppMsg IppMsg() = default; IppMsg(Bytestream& msg); - IppMsg(uint16_t opOrStatus, IppAttrs opAttrs, - IppAttrs jobAttrs=IppAttrs(), IppAttrs printerAttrs=IppAttrs()); + IppMsg(uint16_t opOrStatus, const IppAttrs& opAttrs, const IppAttrs& jobAttrs=IppAttrs(), const IppAttrs& printerAttrs=IppAttrs()); IppMsg(const IppMsg& other) = default; ~IppMsg() = default; @@ -49,10 +48,10 @@ class IppMsg uint16_t getStatus() const {return _opOrStatus;} Bytestream encode() const; - void setOpAttr(std::string name, IppAttr attr); + void setOpAttr(const std::string& name, const IppAttr& attr); void setVersion(uint8_t majVsn, uint8_t minVsn); - static IppAttrs baseOpAttrs(std::string url); + static IppAttrs baseOpAttrs(const std::string& url); static void setReqId(uint32_t reqId) { diff --git a/lib/ippprinter.cpp b/lib/ippprinter.cpp index 81a3cbb..078d672 100644 --- a/lib/ippprinter.cpp +++ b/lib/ippprinter.cpp @@ -7,7 +7,7 @@ #include -IppPrinter::IppPrinter(std::string addr, bool ignoreSslErrors) : _addr(addr), _ignoreSslErrors(ignoreSslErrors) +IppPrinter::IppPrinter(std::string addr, bool ignoreSslErrors) : _addr(std::move(addr)), _ignoreSslErrors(ignoreSslErrors) { _error = refresh(); } @@ -56,7 +56,8 @@ Error IppPrinter::refresh() return error; } -Error IppPrinter::runJob(IppPrintJob job, std::string inFile, std::string inFormat, int pages, ProgressFun progressFun) +Error IppPrinter::runJob(IppPrintJob& job, const std::string& inFile, const std::string& inFormat, int pages, + const ProgressFun& progressFun) { Error error; try @@ -133,7 +134,7 @@ Error IppPrinter::runJob(IppPrintJob job, std::string inFile, std::string inForm return error; } -Error IppPrinter::doPrint(IppPrintJob& job, std::string inFile, Converter::ConvertFun convertFun, Bytestream&& hdr, ProgressFun progressFun) +Error IppPrinter::doPrint(IppPrintJob& job, const std::string& inFile, const Converter::ConvertFun& convertFun, Bytestream&& hdr, const ProgressFun& progressFun) { Error error; CurlIppStreamer cr(_addr, true); @@ -151,7 +152,9 @@ Error IppPrinter::doPrint(IppPrintJob& job, std::string inFile, Converter::Conve WriteFun writeFun([&cr](Bytestream&& data) -> bool { if(data.size() == 0) + { return true; + } return cr.write(std::move(data)); }); @@ -187,7 +190,7 @@ Error IppPrinter::doPrint(IppPrintJob& job, std::string inFile, Converter::Conve return error; } -Error IppPrinter::doPrintToFile(IppPrintJob& job, std::string inFile, Converter::ConvertFun convertFun, ProgressFun progressFun) +Error IppPrinter::doPrintToFile(IppPrintJob& job, const std::string& inFile, const Converter::ConvertFun& convertFun, const ProgressFun& progressFun) { std::string fileName = std::filesystem::path(inFile).filename(); std::string ext = MiniMime::defaultExtension(job.targetFormat); @@ -431,7 +434,7 @@ Error IppPrinter::identify() return error; } -Error IppPrinter::setAttributes(List> attrStrs) +Error IppPrinter::setAttributes(const List>& attrStrs) { Error error; IppAttrs attrs; @@ -540,7 +543,7 @@ Error IppPrinter::_doRequest(const IppMsg& req, IppMsg& resp) return error; } -IppMsg IppPrinter::_mkMsg(uint16_t opOrStatus, IppAttrs opAttrs, IppAttrs jobAttrs, IppAttrs printerAttrs) +IppMsg IppPrinter::_mkMsg(uint16_t opOrStatus, IppAttrs opAttrs, const IppAttrs& jobAttrs, const IppAttrs& printerAttrs) { IppAttrs baseOpAttrs = IppMsg::baseOpAttrs(_addr); opAttrs.insert(baseOpAttrs.cbegin(), baseOpAttrs.cend()); @@ -563,7 +566,7 @@ void IppPrinter::_applyOverrides() Bytestream bts(ifs); std::string errStr; Json overridesJson = Json::parse(bts.getString(bts.size()), errStr); - if(errStr != "") + if(!errStr.empty()) { std::cerr << "Bad overrides file: " << errStr << std::endl; } diff --git a/lib/ippprinter.h b/lib/ippprinter.h index 674548f..1619c23 100644 --- a/lib/ippprinter.h +++ b/lib/ippprinter.h @@ -58,7 +58,7 @@ class IppPrinter return IppPrintJob(_printerAttrs, additionalDocumentFormats()); } - Error runJob(IppPrintJob job, std::string inFile, std::string inFormat, int pages, ProgressFun progressFun = noOpProgressfun); + Error runJob(IppPrintJob& job, const std::string& inFile, const std::string& inFormat, int pages, const ProgressFun& progressFun = noOpProgressfun); std::string name(); std::string uuid(); @@ -85,7 +85,7 @@ class IppPrinter bool identifySupported(); Error identify(); - Error setAttributes(List> attrStrs); + Error setAttributes(const List>& attrStrs); Error getJobs(List& jobInfos); Error cancelJob(int jobId); @@ -97,7 +97,7 @@ class IppPrinter private: Error _doRequest(IppMsg::Operation op, IppMsg& resp); Error _doRequest(const IppMsg& req, IppMsg& resp); - IppMsg _mkMsg(uint16_t opOrStatus, IppAttrs opAttrs=IppAttrs(), IppAttrs jobAttrs=IppAttrs(), IppAttrs printerAttrs=IppAttrs()); + IppMsg _mkMsg(uint16_t opOrStatus, IppAttrs opAttrs=IppAttrs(), const IppAttrs& jobAttrs=IppAttrs(), const IppAttrs& printerAttrs=IppAttrs()); void _applyOverrides(); std::string _addr; @@ -107,8 +107,8 @@ class IppPrinter Error _error; IppAttrs _printerAttrs; - Error doPrint(IppPrintJob& job, std::string inFile, Converter::ConvertFun convertFun, Bytestream&& hdr, ProgressFun progressFun); - Error doPrintToFile(IppPrintJob& job, std::string inFile, Converter::ConvertFun convertFun, ProgressFun progressFun); + Error doPrint(IppPrintJob& job, const std::string& inFile, const Converter::ConvertFun& convertFun, Bytestream&& hdr, const ProgressFun& progressFun); + Error doPrintToFile(IppPrintJob& job, const std::string& inFile, const Converter::ConvertFun& convertFun, const ProgressFun& progressFun); }; diff --git a/lib/ippprintjob.cpp b/lib/ippprintjob.cpp index a810c45..ae21d16 100644 --- a/lib/ippprintjob.cpp +++ b/lib/ippprintjob.cpp @@ -9,7 +9,7 @@ #include #include -Error IppPrintJob::finalize(std::string inputFormat, int pages) +Error IppPrintJob::finalize(const std::string& inputFormat, int pages) { targetFormat = determineTargetFormat(inputFormat); // Only set if regular supported format - else set OctetSteam @@ -110,7 +110,7 @@ Error IppPrintJob::finalize(std::string inputFormat, int pages) { for(const IppIntRange& range : jobAttrs.getList("page-ranges")) { - printParams.pageRangeList.push_back({range.low, range.high}); + printParams.pageSelection.push_back({range.low, range.high}); } pageRanges.unset(); } @@ -163,7 +163,7 @@ Error IppPrintJob::finalize(std::string inputFormat, int pages) return Error(); } -std::string IppPrintJob::determineTargetFormat(std::string inputFormat) +std::string IppPrintJob::determineTargetFormat(const std::string& inputFormat) { std::string targetFormat = documentFormat.get(MiniMime::OctetStream); bool canConvert = Converter::instance().canConvert(inputFormat, targetFormat); @@ -304,7 +304,7 @@ void IppPrintJob::adjustRasterSettings(int pages) { PageSequence seq = printParams.getPageSequence(pages); // No two different elements... - bool singlePageRange = std::adjacent_find(seq.cbegin(), seq.cend(), std::not_equal_to()) == seq.cend(); + bool singlePageRange = std::adjacent_find(seq.cbegin(), seq.cend(), std::not_equal_to<>()) == seq.cend(); if(pages == 1 || singlePageRange) { diff --git a/lib/ippprintjob.h b/lib/ippprintjob.h index 080d809..bdd28d0 100644 --- a/lib/ippprintjob.h +++ b/lib/ippprintjob.h @@ -58,7 +58,7 @@ class IppPrintJob ChoiceSetting leftMargin = ChoiceSetting(&_printerAttrs, &jobAttrs, IppTag::Integer, "media-left-margin", "media-col"); ChoiceSetting rightMargin = ChoiceSetting(&_printerAttrs, &jobAttrs, IppTag::Integer, "media-right-margin", "media-col"); - Error finalize(std::string inputFormat, int pages=0); + Error finalize(const std::string& inputFormat, int pages=0); bool canSaveSettings(); void restoreSettings(); @@ -84,8 +84,8 @@ class IppPrintJob private: - std::string determineTargetFormat(std::string inputFormat); - bool isImage(std::string format); + std::string determineTargetFormat(const std::string& inputFormat); + bool isImage(const std::string& format); void adjustRasterSettings(int pages); IppAttrs _printerAttrs; diff --git a/lib/minimime.cpp b/lib/minimime.cpp index a5951fa..5718f2a 100644 --- a/lib/minimime.cpp +++ b/lib/minimime.cpp @@ -19,7 +19,7 @@ const std::string MiniMime::GIF = "image/gif"; const std::string MiniMime::JPEG = "image/jpeg"; const std::string MiniMime::TIFF = "image/tiff"; -std::string MiniMime::getMimeType(std::string fileName) +std::string MiniMime::getMimeType(const std::string& fileName) { std::ifstream ifs(fileName, std::ios::in | std::ios::binary); Bytestream bts(ifs, 7); @@ -63,30 +63,30 @@ std::string MiniMime::getMimeType(std::string fileName) return OctetStream; } -bool MiniMime::isKnownImageFormat(std::string mimeType) +bool MiniMime::isKnownImageFormat(const std::string& mimeType) { static std::set knownImageFormats({PNG, GIF, JPEG, TIFF}); return knownImageFormats.find(mimeType) != knownImageFormats.cend(); } -bool MiniMime::isImage(std::string mimeType) +bool MiniMime::isImage(const std::string& mimeType) { return string_starts_with(mimeType, "image/") && !isPrinterRaster(mimeType); } -bool MiniMime::isPrinterRaster(std::string mimeType) +bool MiniMime::isPrinterRaster(const std::string& mimeType) { static std::set printerRasterFormats({PWG, URF}); return printerRasterFormats.find(mimeType) != printerRasterFormats.cend(); } -bool MiniMime::isMultiPage(std::string mimeType) +bool MiniMime::isMultiPage(const std::string& mimeType) { static std::set multiPageFormats({PDF, Postscript, PWG, URF}); return multiPageFormats.find(mimeType) != multiPageFormats.cend(); } -std::string MiniMime::defaultExtension(std::string mimeType) +std::string MiniMime::defaultExtension(const std::string& mimeType) { static std::map extensions {{PDF, ".pdf"}, {Postscript, ".ps"}, diff --git a/lib/minimime.h b/lib/minimime.h index 23209c1..b64c578 100644 --- a/lib/minimime.h +++ b/lib/minimime.h @@ -21,13 +21,13 @@ class MiniMime static const std::string JPEG; static const std::string TIFF; - static std::string getMimeType(std::string fileName); + static std::string getMimeType(const std::string& fileName); - static bool isKnownImageFormat(std::string mimeType); - static bool isImage(std::string mimeType); - static bool isPrinterRaster(std::string mimeType); - static bool isMultiPage(std::string mimeType); - static std::string defaultExtension(std::string mimeType); + static bool isKnownImageFormat(const std::string& mimeType); + static bool isImage(const std::string& mimeType); + static bool isPrinterRaster(const std::string& mimeType); + static bool isMultiPage(const std::string& mimeType); + static std::string defaultExtension(const std::string& mimeType); }; #endif // MINIMIME_H diff --git a/lib/pdf2printable.cpp b/lib/pdf2printable.cpp index 8af16df..fc803eb 100644 --- a/lib/pdf2printable.cpp +++ b/lib/pdf2printable.cpp @@ -13,8 +13,9 @@ #include #include +#include +#include #include -#include #include #define R_RELATIVE_LUMINOSITY 0.299 @@ -27,7 +28,6 @@ + (RGB32_G(RGB)*G_RELATIVE_LUMINOSITY) \ + (RGB32_B(RGB)*B_RELATIVE_LUMINOSITY))) -#define MAX3(A,B,C) std::max(std::max(A, B), C) #define SIXTEENTHS(parts, value) (parts*(value/16)) #ifndef PDF_CREATOR @@ -36,7 +36,7 @@ #define CHECK(call) if(!(call)) {return Error("Write error");} -void copy_raster_buffer(Bytestream& bmpBts, uint32_t* data, const PrintParameters& params); +void copy_raster_buffer(Bytestream& bmpBts, const uint32_t* data, const PrintParameters& params); void fixup_scale(double& xScale, double& yScale, double& xOffset, double& yOffset, bool& rotate, double& wIn, double& hIn, const PrintParameters& params); @@ -52,8 +52,7 @@ inline double round2(double d) return round(d*100)/100; } -Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParameters& params, - ProgressFun progressFun) +Error pdf_to_printable(const std::string& inFile, const WriteFun& writeFun, const PrintParameters& params, const ProgressFun& progressFun) { if(params.format == PrintParameters::URF && (params.hwResW != params.hwResH)) { @@ -82,9 +81,8 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet } else { - inFile = std::filesystem::absolute(inFile); std::string url("file://"); - url.append(inFile); + url += std::filesystem::absolute(inFile); doc = poppler_document_new_from_file(url.c_str(), nullptr, &error); } @@ -168,8 +166,12 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet if(pageNo != INVALID_PAGE) { // We are actually rendering a page and not just a blank... Pointer page(poppler_document_get_page(doc, pageNo-1), g_object_unref); - double pageWidth, pageHeight; - double xScale, yScale, xOffset, yOffset; + double pageWidth; + double pageHeight; + double xScale; + double yScale; + double xOffset; + double yOffset; bool rotate = false; poppler_page_get_size(page, &pageWidth, &pageHeight); @@ -204,7 +206,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet } CHECK(writeFun(std::move(outBts))); - outBts.reset(); + outBts = Bytestream(); progressFun(outPageNo, pageSequence.size()); } @@ -219,7 +221,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet return Error(); } -void copy_raster_buffer(Bytestream& bmpBts, uint32_t* data, const PrintParameters& params) +void copy_raster_buffer(Bytestream& bmpBts, const uint32_t* data, const PrintParameters& params) { size_t size = params.getPaperSizeInPixels(); bool black = params.isBlack(); @@ -294,7 +296,7 @@ void copy_raster_buffer(Bytestream& bmpBts, uint32_t* data, const PrintParameter { for(size_t i=0, j=0; i < size; i++, j+=4) { - uint32_t blackDiff = MAX3(RGB32_R(data[i]), RGB32_G(data[i]), RGB32_B(data[i])); + uint32_t blackDiff = std::max({RGB32_R(data[i]), RGB32_G(data[i]), RGB32_B(data[i])}); tmp[j] = (blackDiff - RGB32_R(data[i])); tmp[j+1] = (blackDiff - RGB32_G(data[i])); tmp[j+2] = (blackDiff - RGB32_B(data[i])); @@ -340,17 +342,20 @@ void fixup_scale(double& xScale, double& yScale, double& xOffset, double& yOffse // Finally, if we have an asymmetric resolution // and are not working in absolute dimensions (points), compensate for it. + // NB: This expects an integer scale. if(raster) { // URF will/should not end up here, but still... if(params.hwResW > params.hwResH) { - xScale *= (params.hwResW/params.hwResH); - xOffset *= (params.hwResW/params.hwResH); + size_t scale = params.hwResW / params.hwResH; + xScale *= scale; + xOffset *= scale; } else if(params.hwResH > params.hwResW) { - yScale *= (params.hwResH/params.hwResW); - yOffset *= (params.hwResH/params.hwResW); + size_t scale = params.hwResH / params.hwResW; + yScale *= scale; + yOffset *= scale; } } } diff --git a/lib/pdf2printable.h b/lib/pdf2printable.h index 947c3e9..aaf22c6 100644 --- a/lib/pdf2printable.h +++ b/lib/pdf2printable.h @@ -7,7 +7,7 @@ #include -Error pdf_to_printable(std::string infile, WriteFun writeFun, const PrintParameters& params, - ProgressFun progressFun = noOpProgressfun); +Error pdf_to_printable(const std::string& infile, const WriteFun& writeFun, const PrintParameters& params, + const ProgressFun& progressFun = noOpProgressfun); #endif //PDF2PRINTABLE_H diff --git a/lib/ppm2pwg.cpp b/lib/ppm2pwg.cpp index 0bf9a55..a0f49d3 100644 --- a/lib/ppm2pwg.cpp +++ b/lib/ppm2pwg.cpp @@ -6,9 +6,9 @@ #include "pwgpghdr.h" #include "urfpghdr.h" +#include #include #include -#include void make_pwg_hdr(Bytestream& outBts, const PrintParameters& params, bool backside); void make_urf_hdr(Bytestream& outBts, const PrintParameters& params); @@ -49,7 +49,7 @@ void bmp_to_pwg(Bytestream& bmpBts, Bytestream& outBts, size_t page, const Print uint8_t* raw = bmpBts.raw(); size_t bytesPerLine = params.getPaperSizeWInBytes(); int oneLine = backside && params.getBackVFlip() ? -bytesPerLine : bytesPerLine; - uint8_t* row0 = backside && params.getBackVFlip() ? raw + (yRes - 1) * bytesPerLine : raw; + uint8_t* row0 = backside && params.getBackVFlip() ? raw + ((yRes - 1) * bytesPerLine) : raw; Array tmpLine(bytesPerLine); size_t colors = params.getNumberOfColors(); @@ -61,7 +61,7 @@ void bmp_to_pwg(Bytestream& bmpBts, Bytestream& outBts, size_t page, const Print for(size_t y = 0; y < yRes; y++) { - uint8_t* thisLine = row0 + y * oneLine; + uint8_t* thisLine = row0 + (y * oneLine); uint8_t lineRepeat = 0; uint8_t* next_line = thisLine + oneLine; @@ -192,7 +192,7 @@ static const std::map {"photographic-high-gloss", UrfPgHdr::PhotographicHighGloss}, {"other", UrfPgHdr::OtherMediaType}}; -bool isUrfMediaType(std::string mediaType) +bool isUrfMediaType(const std::string& mediaType) { return UrfMediaTypeMappings.find(mediaType) != UrfMediaTypeMappings.cend(); } diff --git a/lib/ppm2pwg.h b/lib/ppm2pwg.h index bbc3537..56719ea 100644 --- a/lib/ppm2pwg.h +++ b/lib/ppm2pwg.h @@ -12,6 +12,6 @@ Bytestream make_urf_file_hdr(uint32_t pages); void bmp_to_pwg(Bytestream& bmpBts, Bytestream& outBts, size_t page, const PrintParameters& params); -bool isUrfMediaType(std::string mediaType); +bool isUrfMediaType(const std::string& mediaType); #endif //PPM2PWG_H diff --git a/lib/printparameters.cpp b/lib/printparameters.cpp index 475ae29..a1908b1 100644 --- a/lib/printparameters.cpp +++ b/lib/printparameters.cpp @@ -132,7 +132,7 @@ size_t PrintParameters::getPaperSizeInBytes() const PageSequence PrintParameters::getPageSequence(size_t pages) const { - PageRangeList tmp = pageRangeList.empty() ? PageRangeList {{1, pages}} : pageRangeList; + PageRangeList tmp = pageSelection.empty() ? PageRangeList {{1, pages}} : pageSelection; PageSequence seq; for(const auto& [first, last] : tmp) @@ -187,7 +187,7 @@ PageSequence PrintParameters::getPageSequence(size_t pages) const return seq; } -PageRangeList PrintParameters::parsePageRange(const std::string& rangesStr) +PageRangeList PrintParameters::parsePageSelection(const std::string& pageSelectionStr) { PageRangeList rangeList; const std::regex single("^([0-9]+)$"); @@ -195,7 +195,7 @@ PageRangeList PrintParameters::parsePageRange(const std::string& rangesStr) std::smatch match; size_t prevMax = 0; - List rangeStrList = split_string(rangesStr, ","); + List rangeStrList = split_string(pageSelectionStr, ","); for(const std::string& rangeStr : rangeStrList) { if(std::regex_match(rangeStr, match, single)) @@ -227,15 +227,15 @@ PageRangeList PrintParameters::parsePageRange(const std::string& rangesStr) return rangeList; } -bool PrintParameters::setPageRange(const std::string& rangeStr) +bool PrintParameters::setPageSelection(const std::string& pageSelectionStr) { - pageRangeList = parsePageRange(rangeStr); - return !pageRangeList.empty(); + pageSelection = parsePageSelection(pageSelectionStr); + return !pageSelection.empty(); } -double from_string(std::string str) +double from_string(const std::string& str) { - size_t dotAt = str.find("."); + size_t dotAt = str.find('.'); double res = std::stoul(str.substr(0, dotAt)); if(dotAt != std::string::npos) { diff --git a/lib/printparameters.h b/lib/printparameters.h index 9e910a8..92ec3e8 100644 --- a/lib/printparameters.h +++ b/lib/printparameters.h @@ -147,7 +147,7 @@ class PrintParameters size_t copies = 1; bool collatedCopies = true; - PageRangeList pageRangeList; + PageRangeList pageSelection; MediaPosition mediaPosition = AutomaticMediaPosition; std::string mediaType; @@ -168,8 +168,8 @@ class PrintParameters PageSequence getPageSequence(size_t pages) const; - static PageRangeList parsePageRange(const std::string& rangeStr); - bool setPageRange(const std::string& rangeStr); + static PageRangeList parsePageSelection(const std::string& pageSelectionStr); + bool setPageSelection(const std::string& pageSelectionStr); bool setPaperSize(const std::string& sizeStr); diff --git a/lib/pwg2ppm.cpp b/lib/pwg2ppm.cpp index 68e8466..ed85004 100644 --- a/lib/pwg2ppm.cpp +++ b/lib/pwg2ppm.cpp @@ -57,7 +57,7 @@ void raster_to_bmp(Bytestream& outBts, Bytestream& file, void write_ppm(Bytestream& outBts, size_t width, size_t height, size_t colors, size_t bits, bool black, - std::string outfilePrefix, int page) + const std::string& outfilePrefix, int page) { if(bits == 1 && !black) { @@ -89,7 +89,14 @@ void invert(Bytestream& bts) void cmyk2rgb(Bytestream& cmyk) { Bytestream rgb; - uint8_t c, m, y, k, r, g, b, w; + uint8_t c; + uint8_t m; + uint8_t y; + uint8_t k; + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t w; size_t size = cmyk.size()/4; for(size_t i=0; i < size; i++) { diff --git a/lib/pwg2ppm.h b/lib/pwg2ppm.h index 7e505ef..055a67f 100644 --- a/lib/pwg2ppm.h +++ b/lib/pwg2ppm.h @@ -8,7 +8,7 @@ void raster_to_bmp(Bytestream& outBts, Bytestream& file, void write_ppm(Bytestream& outBts,size_t width, size_t height, size_t colors, size_t bits, bool black, - std::string outFilePrefix, int page); + const std::string& outFilePrefix, int page); void invert(Bytestream& bts); diff --git a/lib/stringutils.h b/lib/stringutils.h index 62c62c8..6966c99 100644 --- a/lib/stringutils.h +++ b/lib/stringutils.h @@ -41,16 +41,14 @@ inline List split_string(const std::string& s, const std::string& t return res; } -inline std::string join_string(List list, const std::string& sep) +inline std::string join_string(const List& list, const std::string& sep) { std::string res; - if(!list.empty()) + bool first = true; + for(const std::string& e : list) { - res = list.takeFront(); - for(const std::string& e : list) - { - res += sep + e; - } + res += (first ? "" : sep) + e; + first = false; } return res; } diff --git a/lib/url.h b/lib/url.h index b32f3fa..aec560a 100644 --- a/lib/url.h +++ b/lib/url.h @@ -8,12 +8,12 @@ class Url { public: Url() = delete; - Url(std::string str) + Url(const std::string& str) { match(str); } - Url& operator=(std::string str) + Url& operator=(const std::string& str) { match(str); return *this; @@ -72,7 +72,7 @@ class Url private: - void match(std::string str) + void match(const std::string& str) { static const std::regex regex("(([a-z]+)://)([a-z0-9-.]+)(:([0-9]+))?(/.*)?$"); std::smatch match; diff --git a/tests/test.cpp b/tests/test.cpp index 000a9bd..055fcad 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -877,7 +877,7 @@ TEST(pagerange) seq = params.getPageSequence(100); ASSERT(seq.size() == 100); - params.pageRangeList = {{4, 7}}; + params.pageSelection = {{4, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 4); ASSERT(seq == PageSequence({4, 5, 6, 7})); @@ -885,7 +885,7 @@ TEST(pagerange) params.copies = 2; params.collatedCopies = true; params.format = PrintParameters::PWG; - params.pageRangeList = {{4, 7}}; + params.pageSelection = {{4, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({4, 5, 6, 7, @@ -895,7 +895,7 @@ TEST(pagerange) params.copies = 2; params.collatedCopies = true; params.format = PrintParameters::PWG; - params.pageRangeList = {{4, 6}}; + params.pageSelection = {{4, 6}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 6); ASSERT(seq == PageSequence({4, 5, 6, @@ -904,7 +904,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 3; params.collatedCopies = true; - params.pageRangeList = {{4, 6}}; + params.pageSelection = {{4, 6}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 9); ASSERT(seq == PageSequence({4, 5, 6, @@ -915,7 +915,7 @@ TEST(pagerange) params.copies = 2; params.collatedCopies = true; params.format = PrintParameters::PWG; - params.pageRangeList = {{4, 6}}; + params.pageSelection = {{4, 6}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({4, 5, 6, INVALID_PAGE, @@ -923,7 +923,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 3; - params.pageRangeList = {{4, 6}}; + params.pageSelection = {{4, 6}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 12); ASSERT(seq == PageSequence({4, 5, 6, INVALID_PAGE, @@ -933,7 +933,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {{4, 7}}; + params.pageSelection = {{4, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({4, 4, @@ -944,7 +944,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {{4, 7}}; + params.pageSelection = {{4, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({4, 5, @@ -955,7 +955,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 3; params.collatedCopies = true; - params.pageRangeList = {{1, 2}, {5, 7}}; + params.pageSelection = {{1, 2}, {5, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 15); ASSERT(seq == PageSequence({1, 2, 5, 6, 7, @@ -965,7 +965,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 3; params.collatedCopies = true; - params.pageRangeList = {{1, 2}, {5, 7}}; + params.pageSelection = {{1, 2}, {5, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 18); ASSERT(seq == PageSequence({1, 2, 5, 6, 7, INVALID_PAGE, @@ -975,7 +975,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 3; params.collatedCopies = true; - params.pageRangeList = {{1, 2}, {6, 7}}; + params.pageSelection = {{1, 2}, {6, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 12); ASSERT(seq == PageSequence({1, 2, 6, 7, @@ -985,7 +985,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {{1, 2}, {5, 7}}; + params.pageSelection = {{1, 2}, {5, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 10); ASSERT(seq == PageSequence({1, 1, @@ -997,7 +997,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {{1, 2}, {5, 7}}; + params.pageSelection = {{1, 2}, {5, 7}}; seq = params.getPageSequence(100); ASSERT(seq.size() == 12); ASSERT(seq == PageSequence({1, 2, @@ -1009,7 +1009,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 1; - params.pageRangeList = {}; + params.pageSelection = {}; seq = params.getPageSequence(3); ASSERT(seq.size() == 3); ASSERT(seq == PageSequence({1, 2, 3})); @@ -1017,7 +1017,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 2; params.collatedCopies = true; - params.pageRangeList = {}; + params.pageSelection = {}; seq = params.getPageSequence(3); ASSERT(seq.size() == 6); ASSERT(seq == PageSequence({1, 2, 3, @@ -1026,7 +1026,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {}; + params.pageSelection = {}; seq = params.getPageSequence(3); ASSERT(seq.size() == 6); ASSERT(seq == PageSequence({1, 1, @@ -1036,7 +1036,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 2; params.collatedCopies = true; - params.pageRangeList = {}; + params.pageSelection = {}; seq = params.getPageSequence(3); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({1, 2, 3, INVALID_PAGE, @@ -1045,7 +1045,7 @@ TEST(pagerange) params.duplexMode = PrintParameters::TwoSidedLongEdge; params.copies = 2; params.collatedCopies = false; - params.pageRangeList = {}; + params.pageSelection = {}; seq = params.getPageSequence(3); ASSERT(seq.size() == 8); ASSERT(seq == PageSequence({1, 2, @@ -1055,13 +1055,13 @@ TEST(pagerange) params.duplexMode = PrintParameters::OneSided; params.copies = 1; - params.pageRangeList = {{17,42}}; + params.pageSelection = {{17,42}}; seq = params.getPageSequence(42); ASSERT(seq.size() == 26); seq = params.getPageSequence(32); ASSERT(seq.size() == 16); - params.pageRangeList = {{1,2},{17,42}}; + params.pageSelection = {{1,2},{17,42}}; seq = params.getPageSequence(42); ASSERT(seq.size() == 28); seq = params.getPageSequence(32); @@ -1072,48 +1072,48 @@ TEST(pagerange) TEST(parse_pagerange) { PrintParameters params; - ASSERT(params.setPageRange("1")); - ASSERT(params.pageRangeList == PageRangeList({{1,1}})); - ASSERT(params.setPageRange("1-1")); - ASSERT(params.pageRangeList == PageRangeList({{1,1}})); - ASSERT(params.setPageRange("1,2,3")); - ASSERT(params.pageRangeList == PageRangeList({{1,1},{2,2},{3,3}})); - ASSERT(params.setPageRange("1-3,5")); - ASSERT(params.pageRangeList == PageRangeList({{1,3},{5,5}})); - ASSERT(params.setPageRange("1-3,5,6,17-42")); - ASSERT(params.pageRangeList == PageRangeList({{1,3},{5,5},{6,6},{17,42}})); - - params.pageRangeList = {}; - ASSERT_FALSE(params.setPageRange("1,")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange(",")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("-,-")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("fail")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("fail1")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("fail,1-2")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("1-2,fail")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("1-0")); - ASSERT(params.pageRangeList.empty()); - - ASSERT_FALSE(params.setPageRange("0-1")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("1-2,2-3")); - ASSERT(params.pageRangeList.empty()); - ASSERT_FALSE(params.setPageRange("1-,2-")); - ASSERT(params.pageRangeList.empty()); - - ASSERT(params.setPageRange("2-")); + ASSERT(params.setPageSelection("1")); + ASSERT(params.pageSelection == PageRangeList({{1,1}})); + ASSERT(params.setPageSelection("1-1")); + ASSERT(params.pageSelection == PageRangeList({{1,1}})); + ASSERT(params.setPageSelection("1,2,3")); + ASSERT(params.pageSelection == PageRangeList({{1,1},{2,2},{3,3}})); + ASSERT(params.setPageSelection("1-3,5")); + ASSERT(params.pageSelection == PageRangeList({{1,3},{5,5}})); + ASSERT(params.setPageSelection("1-3,5,6,17-42")); + ASSERT(params.pageSelection == PageRangeList({{1,3},{5,5},{6,6},{17,42}})); + + params.pageSelection = {}; + ASSERT_FALSE(params.setPageSelection("1,")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection(",")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("-,-")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("fail")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("fail1")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("fail,1-2")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("1-2,fail")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("1-0")); + ASSERT(params.pageSelection.empty()); + + ASSERT_FALSE(params.setPageSelection("0-1")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("1-2,2-3")); + ASSERT(params.pageSelection.empty()); + ASSERT_FALSE(params.setPageSelection("1-,2-")); + ASSERT(params.pageSelection.empty()); + + ASSERT(params.setPageSelection("2-")); ASSERT(params.getPageSequence(3) == PageSequence({2,3})); ASSERT(params.getPageSequence(4) == PageSequence({2,3,4})); - ASSERT(params.setPageRange("1,2-")); + ASSERT(params.setPageSelection("1,2-")); ASSERT(params.getPageSequence(3) == PageSequence({1,2,3})); ASSERT(params.getPageSequence(4) == PageSequence({1,2,3,4})); } @@ -1926,7 +1926,7 @@ TEST(finalize) ASSERT(ip.printParams.paperSizeName == "na_letter_8.5x11in"); ASSERT(ip.printParams.hwResW == 600); ASSERT(ip.printParams.hwResH == 600); - ASSERT((ip.printParams.pageRangeList == PageRangeList {{17, 42}})); + ASSERT((ip.printParams.pageSelection == PageRangeList {{17, 42}})); ASSERT(ip.printParams.duplexMode == PrintParameters::TwoSidedLongEdge); ASSERT(ip.printParams.quality == PrintParameters::HighQuality); ASSERT(ip.printParams.colorMode == PrintParameters::Gray8); diff --git a/utils/bsplit.cpp b/utils/bsplit.cpp index 693a42e..395b455 100644 --- a/utils/bsplit.cpp +++ b/utils/bsplit.cpp @@ -7,7 +7,7 @@ #define HELPTEXT "Negative numbers means \"all but\" number of bytes from the beginning/end." -inline void print_error(std::string hint, std::string argHelp) +inline void print_error(const std::string& hint, const std::string& argHelp) { std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl; } diff --git a/utils/ippclient.cpp b/utils/ippclient.cpp index 13dea6f..d1e65a0 100644 --- a/utils/ippclient.cpp +++ b/utils/ippclient.cpp @@ -14,7 +14,7 @@ #define HELPTEXT "" -inline void print_error(std::string hint, std::string argHelp) +inline void print_error(const std::string& hint, const std::string& argHelp) { std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl; } @@ -43,7 +43,7 @@ T pos_clamp(U value) return std::min(value, std::numeric_limits::max()); } -std::ostream& operator<<(std::ostream& os, List supplies) +std::ostream& operator<<(std::ostream& os, const List& supplies) { for(List::const_iterator supply = supplies.cbegin(); supply != supplies.cend(); supply++) { @@ -60,7 +60,7 @@ std::ostream& operator<<(std::ostream& os, List supplies) return os; } -std::ostream& operator<<(std::ostream& os, List firmwares) +std::ostream& operator<<(std::ostream& os, const List& firmwares) { for(List::const_iterator firmware = firmwares.cbegin(); firmware != firmwares.cend(); firmware++) { @@ -73,13 +73,13 @@ std::ostream& operator<<(std::ostream& os, List firmwares) return os; } -std::ostream& operator<<(std::ostream& os, List sl) +std::ostream& operator<<(std::ostream& os, const List& sl) { os << join_string(sl, ", "); return os; } -std::string resolution_list(List l) +std::string resolution_list(const List& l) { bool first = true; std::stringstream ss; @@ -92,14 +92,14 @@ std::string resolution_list(List l) ss << (first ? "" : ",") << r.toStr(); } - if(ss.str().length() == 0) + if(ss.str().empty()) { ss << "empty"; } return ss.str(); } -std::ostream& operator<<(std::ostream& os, List jobInfos) +std::ostream& operator<<(std::ostream& os, const List& jobInfos) { for(const IppPrinter::JobInfo& jobInfo : jobInfos) { @@ -115,7 +115,7 @@ std::ostream& operator<<(std::ostream& os, List jobInfos) } template -void print_if_set(std::string title, T value) +void print_if_set(const std::string& title, const T& value) { if(value != T()) { @@ -124,14 +124,14 @@ void print_if_set(std::string title, T value) } template -void set_or_fail(const O& opt, S& setting, V value, bool force) +void set_or_fail(const O& opt, S& setting, const V& value, bool force) { std::string docName = opt.docName() + " (ipp: " + setting.name() + ")"; set_or_fail(opt.isSet(), docName, "Valid values are: " + setting.supportedStr(), setting, value, force); } template -void set_or_fail(bool isSet, std::string docName, std::string inputHint, S& setting, V value, bool force) +void set_or_fail(bool isSet, const std::string& docName, const std::string& inputHint, S& setting, const V& value, bool force) { if(isSet) { @@ -408,7 +408,7 @@ int main(int argc, char** argv) if(pagesOpt.isSet()) { - PageRangeList pageRanges = PrintParameters::parsePageRange(pages); + PageRangeList pageRanges = PrintParameters::parsePageSelection(pages); if(pageRanges.empty()) { print_error("Invalid page range.", args.argHelp(args.subCommand())); diff --git a/utils/ippdiscover.cpp b/utils/ippdiscover.cpp index 568fbb5..3092897 100644 --- a/utils/ippdiscover.cpp +++ b/utils/ippdiscover.cpp @@ -4,7 +4,7 @@ #include "ippdiscovery.h" #include "log.h" -inline void print_error(std::string hint, std::string argHelp) +inline void print_error(const std::string& hint, const std::string& argHelp) { std::cerr << hint << std::endl << std::endl << argHelp << std::endl; } @@ -36,6 +36,6 @@ int main(int argc, char** argv) LogController::instance().enable(LogController::Debug); } - IppDiscovery ippDiscovery([](std::string addr){std::cout << addr << std::endl;}); + IppDiscovery ippDiscovery([](const std::string& addr){std::cout << addr << std::endl;}); ippDiscovery.discover(); } diff --git a/utils/pdf2printable_main.cpp b/utils/pdf2printable_main.cpp index 3d090aa..4dccf6b 100644 --- a/utils/pdf2printable_main.cpp +++ b/utils/pdf2printable_main.cpp @@ -13,7 +13,7 @@ #define HELPTEXT "Options from 'resolution' and onwards only affect raster output formats.\n" \ "Use \"-\" as filename for stdin/stdout." -inline void print_error(std::string hint, std::string argHelp) +inline void print_error(const std::string& hint, const std::string& argHelp) { std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl; } @@ -133,7 +133,7 @@ int main(int argc, char** argv) if(pagesOpt.isSet()) { - if(!params.setPageRange(pages)) + if(!params.setPageSelection(pages)) { print_error("Malformed page selection", args.argHelp()); return 1; diff --git a/utils/ppm2pwg_main.cpp b/utils/ppm2pwg_main.cpp index 747fcd4..6d330a4 100644 --- a/utils/ppm2pwg_main.cpp +++ b/utils/ppm2pwg_main.cpp @@ -1,8 +1,8 @@ +#include #include #include #include #include -#include #include "argget.h" #include "binfile.h" @@ -14,7 +14,7 @@ #define HELPTEXT "Use \"-\" as filename for stdin/stdout." -inline void print_error(std::string hint, std::string argHelp) +inline void print_error(const std::string& hint, const std::string& argHelp) { std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl; } @@ -171,7 +171,10 @@ int main(int argc, char** argv) outBts.reset(); page++; - std::string p, xs, ys, r; + std::string p; + std::string xs; + std::string ys; + std::string r; inFile >> p; ignore_comments(inFile);