9 #include "io/opensslBIO.h"
10 #include "log/logger.hpp"
12 std::string toHexAndChecksum( const std::string& src ) {
15 ss << ':' << std::hex << std::setfill( '0' ) << std::uppercase;
18 ss << std::setw( 2 ) << ( ( ( uint32_t ) c ) & 0xFF );
22 ss << std::setw( 2 ) << ( ( ( uint32_t )( ~checksum ) ) & 0xFF );
27 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio ) {
30 logger::debugf( "Record payload length: %s", ss.str() );
33 head.totalLength = data.size();
35 size_t toTransfer = std::min(static_cast<size_t>(0xF000), data.size() - pos);
36 head.payloadLength = toTransfer;
39 s += head.packToString();
40 s += data.substr(pos, toTransfer);
42 std::string res = toHexAndChecksum( s );
44 logger::debug( "FINE: RECORD output: ", res );
46 bio->write( res.data(), res.size() );
50 } while(pos < data.size());
53 int32_t fromHexDigit( char c ) {
56 if( c >= '0' && c <= '9' ) {
60 if( c >= 'A' && c <= 'F' ) {
67 std::string parseCommand( RecordHeader& head, const std::string& input) {
68 logger::debug( "FINE: RECORD input: ", input );
70 int32_t dlen = ( input.size() - 2 ) / 2;
74 std::string str( std::max( dlen, RECORD_HEADER_SIZE ), 0 );
76 for( int i = 0; i < dlen; i++ ) {
79 digit = fromHexDigit( input[i * 2 + 1] );
83 digit = fromHexDigit( input[i * 2 + 2] );
90 head.unpackFromString( str.substr( 0, RECORD_HEADER_SIZE ) );
91 uint32_t len = head.payloadLength;
92 uint32_t expectedTotalLength = ( RECORD_HEADER_SIZE + len + 1 /*checksum*/ ) * 2 + 2;
93 std::string data = str.substr( RECORD_HEADER_SIZE, str.size() - RECORD_HEADER_SIZE );
95 if( expectedTotalLength != input.size() ) {
97 ss << "Expected: " << expectedTotalLength << ", Got: " << input.size();
98 logger::error( ss.str() );
99 throw "Error, invalid length";
101 if( checksum != -1 || error || dlen < RECORD_HEADER_SIZE ) {
102 throw "Error, invalid checksum";
109 std::string parseCommandChunked( RecordHeader& head, std::shared_ptr<OpensslBIOWrapper> io){
110 logger::note("reading");
111 std::string payload = parseCommand( head, io->readLine() );
112 std::string all(head.totalLength, ' ');
113 auto target = all.begin();
116 pos += head.payloadLength;
117 target = std::copy ( payload.begin(), payload.end(), target);
118 if(pos >= head.totalLength) {
121 logger::note("chunk digested, reading next one");
122 payload = parseCommand( head, io->readLine() );