X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fio%2Frecord.cpp;h=268897d79e2cdf5626e3dca35a1a9bd5af28bb3f;hb=5163a9658ba7ef4a875ee1103e82c0be81698689;hp=8f81d02e683679fb764db59d16527e023d38f565;hpb=9e866a1a2facc8cb1565cd660c6b6d482f18ecb1;p=cassiopeia.git diff --git a/src/io/record.cpp b/src/io/record.cpp index 8f81d02..268897d 100644 --- a/src/io/record.cpp +++ b/src/io/record.cpp @@ -5,8 +5,9 @@ #include #include -#include "bios.h" -#include "opensslBIO.h" +#include "io/bios.h" +#include "io/opensslBIO.h" +#include "log/logger.hpp" std::string toHexAndChecksum( const std::string& src ) { char checksum = 0; @@ -23,19 +24,31 @@ std::string toHexAndChecksum( const std::string& src ) { return ss.str(); } -void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr bio, std::shared_ptr log ) { - head.payloadLength = data.size(); - std::string s; - s += head.packToString(); - s += data; +void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr bio ) { + std::stringstream ss; + ss << data.size(); + logger::debugf( "Record payload length: %s", ss.str() ); + size_t pos = 0; + head.offset = 0; + head.totalLength = data.size(); - std::string res = toHexAndChecksum( s ); + do { + size_t toTransfer = std::min( static_cast( 0xF000 ), data.size() - pos ); + head.payloadLength = toTransfer; - if( log ) { - ( *log.get() ) << "FINE: RECORD output: " << res << std::endl; - } + std::string s; + s += head.packToString(); + s += data.substr( pos, toTransfer ); + + std::string res = toHexAndChecksum( s ); + + logger::debug( "FINE: RECORD output: ", res ); - bio->write( res.data(), res.size() ); + bio->write( res.data(), res.size() ); + + pos += toTransfer; + head.offset += 1; + } while( pos < data.size() ); } int32_t fromHexDigit( char c ) { @@ -52,17 +65,14 @@ int32_t fromHexDigit( char c ) { return res; } -std::string parseCommand( RecordHeader& head, const std::string input, std::shared_ptr log ) { - if( log ) { - ( *log.get() ) << "FINE: RECORD input: " << input << std::endl; - } +std::string parseCommand( RecordHeader& head, const std::string& input ) { + logger::debug( "FINE: RECORD input: ", input ); int32_t dlen = ( input.size() - 2 ) / 2; char checksum = 0; bool error = false; std::string str( std::max( dlen, RECORD_HEADER_SIZE ), 0 ); - str.append( 0, dlen ); for( int i = 0; i < dlen; i++ ) { int32_t digit; @@ -83,39 +93,47 @@ std::string parseCommand( RecordHeader& head, const std::string input, std::shar uint32_t expectedTotalLength = ( RECORD_HEADER_SIZE + len + 1 /*checksum*/ ) * 2 + 2; std::string data = str.substr( RECORD_HEADER_SIZE, str.size() - RECORD_HEADER_SIZE ); - if( checksum != -1 || expectedTotalLength != input.size() || error || dlen < RECORD_HEADER_SIZE ) { - throw "Error, invalid checksum"; + if( expectedTotalLength != input.size() ) { + std::stringstream ss; + ss << "Expected: " << expectedTotalLength << ", Got: " << input.size(); + logger::error( ss.str() ); + throw std::length_error( "Error, invalid length" ); + } + + if( checksum != -1 || error || dlen < RECORD_HEADER_SIZE ) { + throw std::runtime_error( "Error, invalid checksum" ); } data.pop_back(); return data; } +std::string parseCommandChunked( RecordHeader& head, std::shared_ptr io ) { + logger::note( "reading" ); + std::string payload = parseCommand( head, io->readLine() ); + std::string all( head.totalLength, ' ' ); + auto target = all.begin(); + size_t pos = 0; + RecordHeader head2; -/* -int main( int argc, char* argv[] ) { - OpensslBIOWrapper *bio = new OpensslBIOWrapper(BIO_new_fd(0, 0)); - std::string data = "halloPayload"; - RecordHeader head; - head.command = 0x7; - head.flags = 1; - head.sessid = 13; - head.command_count = 0xA0B; - head.totalLength = 9; - sendCommand( head, data, std::shared_ptr(bio) ); - head.command = 0x8; - - try { - std::string c = parseCommand( head, ":0700010D0000000B0A0900000000000C0068616C6C6F5061796C6F6164E6\n" ); - - std::cout << "res: " << std::endl; - std::cout << head.payloadLength << std::endl; - std::cout << c << std::endl; - } catch( char const* c ) { - std::cout << "err: " << c << std::endl; - } + while( true ) { + pos += head.payloadLength; + target = std::copy( payload.begin(), payload.end(), target ); + + if( pos >= head.totalLength ) { + break; + } + + logger::note( "chunk digested, reading next one" ); + payload = parseCommand( head2, io->readLine() ); + + if( !head2.isFollowupOf( head ) ) { + throw std::runtime_error( "Error, header of follow up chunk was malformed" ); + } + + head = head2; + } - return 0; + return all; } -*/