From a257a44837e78d283f1735b4685618c270aa54bc Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Sun, 20 Apr 2014 23:35:04 +0200 Subject: use boost::iostreams instead of std::stringstreams boost::iostreams::array_source instead of istringstream boost::iostreams::back_insert_device instead of ostringstream this should save at least one copy each and still has length checks (input) or dynamic growth (output) --- rfc4251.h | 4 ++++ ssh-agent-filter.C | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/rfc4251.h b/rfc4251.h index 876db07..3c291a2 100644 --- a/rfc4251.h +++ b/rfc4251.h @@ -164,6 +164,10 @@ struct rfc4251string : boost::totally_ordered { explicit rfc4251string (mpz_class const & x) : rfc4251string{x.get_mpz_t()} {} inline explicit rfc4251string (std::istream &); + size_t size () const { return value.size(); } + char const * data () const { return value.data(); } + char * data () { return value.data(); } + operator std::string () const { return {value.begin(), value.end()}; } operator std::vector () const; operator mpz_class () const; diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C index a7844f9..ff28601 100644 --- a/ssh-agent-filter.C +++ b/ssh-agent-filter.C @@ -26,6 +26,8 @@ namespace po = boost::program_options; namespace fs = boost::filesystem; #include +#include +#include #include namespace io = boost::iostreams; @@ -33,7 +35,6 @@ namespace io = boost::iostreams; #include #include #include -#include #include #include @@ -208,7 +209,8 @@ void setup_filters () { agent.exceptions(std::ios::badbit | std::ios::failbit); agent << rfc4251string{std::string{SSH2_AGENTC_REQUEST_IDENTITIES}}; - std::istringstream answer_iss{rfc4251string{agent}}; + rfc4251string answer{agent}; + io::stream answer_iss{answer.data(), answer.size()}; answer_iss.exceptions(std::ios::badbit | std::ios::failbit); rfc4251byte resp_code{answer_iss}; if (resp_code != SSH2_AGENT_IDENTITIES_ANSWER) @@ -292,7 +294,7 @@ bool confirm (std::string const & question) { } bool dissect_auth_data_ssh (rfc4251string const & data, std::string & request_description) try { - std::istringstream datastream{data}; + io::stream datastream{data.data(), data.size()}; datastream.exceptions(std::ios::badbit | std::ios::failbit); // Format specified in RFC 4252 Section 7 @@ -313,8 +315,9 @@ bool dissect_auth_data_ssh (rfc4251string const & data, std::string & request_de } rfc4251string handle_request (rfc4251string const & r) { - std::istringstream request{r}; - std::ostringstream answer; + io::stream request{r.data(), r.size()}; + rfc4251string ret; + io::stream>> answer{ret.value}; request.exceptions(std::ios::badbit | std::ios::failbit); answer.exceptions(std::ios::badbit | std::ios::failbit); rfc4251byte request_code{request}; @@ -326,7 +329,8 @@ rfc4251string handle_request (rfc4251string const & r) { agent << rfc4251string{std::string{SSH2_AGENTC_REQUEST_IDENTITIES}}; // temp to test key filtering when signing //return rfc4251string{agent}; - std::istringstream agent_answer_iss{rfc4251string{agent}}; + rfc4251string agent_answer{agent}; + io::stream agent_answer_iss{agent_answer.data(), agent_answer.size()}; agent_answer_iss.exceptions(std::ios::badbit | std::ios::failbit); rfc4251byte answer_code{agent_answer_iss}; rfc4251uint32 keycount{agent_answer_iss}; @@ -409,7 +413,8 @@ rfc4251string handle_request (rfc4251string const & r) { break; } - return rfc4251string{answer.str()}; + answer << std::flush; + return ret; } void handle_client (int const sock) try { -- cgit v1.2.3