]> WPIA git - cassiopeia.git/blob - src/io/record.cpp
upd: only throwing exceptions now
[cassiopeia.git] / src / io / record.cpp
1 #include "record.h"
2
3 #include <iomanip>
4 #include <iostream>
5 #include <memory>
6 #include <sstream>
7
8 #include "io/bios.h"
9 #include "io/opensslBIO.h"
10 #include "log/logger.hpp"
11
12 std::string toHexAndChecksum( const std::string& src ) {
13     char checksum = 0;
14     std::stringstream ss;
15     ss << ':' << std::hex << std::setfill( '0' ) << std::uppercase;
16
17     for( auto c : src ) {
18         ss << std::setw( 2 ) << ( ( ( uint32_t ) c ) & 0xFF );
19         checksum += c;
20     }
21
22     ss << std::setw( 2 ) << ( ( ( uint32_t )( ~checksum ) ) & 0xFF );
23     ss << '\n';
24     return ss.str();
25 }
26
27 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio ) {
28     std::stringstream ss;
29     ss << data.size();
30     logger::debugf( "Record payload length: %s", ss.str() ); 
31     size_t pos = 0;
32     head.offset = 0;
33     head.totalLength = data.size();
34     do {
35         size_t toTransfer = std::min(static_cast<size_t>(0xF000), data.size() - pos);
36         head.payloadLength = toTransfer;
37
38         std::string s;
39         s += head.packToString();
40         s += data.substr(pos, toTransfer);
41
42         std::string res = toHexAndChecksum( s );
43
44         logger::debug( "FINE: RECORD output: ", res );
45
46         bio->write( res.data(), res.size() );
47
48         pos += toTransfer;
49         head.offset += 1;
50     } while(pos < data.size());
51 }
52
53 int32_t fromHexDigit( char c ) {
54     int32_t res = -1;
55
56     if( c >= '0' && c <= '9' ) {
57         res = c - '0';
58     }
59
60     if( c >= 'A' && c <= 'F' ) {
61         res = c - 'A' + 10;
62     }
63
64     return res;
65 }
66
67 std::string parseCommand( RecordHeader& head, const std::string& input) {
68     logger::debug( "FINE: RECORD input: ", input );
69
70     int32_t dlen = ( input.size() - 2 ) / 2;
71     char checksum = 0;
72     bool error = false;
73
74     std::string str( std::max( dlen, RECORD_HEADER_SIZE ), 0 );
75
76     for( int i = 0; i < dlen; i++ ) {
77         int32_t digit;
78         int32_t accum;
79         digit = fromHexDigit( input[i * 2 + 1] );
80         error |= digit == -1;
81         accum = digit;
82         accum <<= 4;
83         digit = fromHexDigit( input[i * 2 + 2] );
84         error |= digit == -1;
85         accum += digit;
86         str[i] = accum;
87         checksum += str[i];
88     }
89
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 );
94
95     if( expectedTotalLength != input.size() ) {
96         std::stringstream ss;
97         ss << "Expected: " << expectedTotalLength << ", Got: " << input.size();
98         logger::error( ss.str() );
99         throw std::length_error("Error, invalid length");
100     }
101     if( checksum != -1 || error || dlen < RECORD_HEADER_SIZE ) {
102         throw std::runtime_error("Error, invalid checksum");
103     }
104
105     data.pop_back();
106
107     return data;
108 }
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();
114     size_t pos = 0;
115     RecordHeader head2;
116     while(true) {
117         pos += head.payloadLength;
118         target = std::copy ( payload.begin(), payload.end(), target);
119         if(pos >= head.totalLength) {
120             break;
121         }
122         logger::note("chunk digested, reading next one");
123         payload = parseCommand( head2, io->readLine() );
124         if(!head2.isFollowupOf(head)){
125             throw std::runtime_error("Error, header of follow up chunk was malformed");
126         }
127         head = head2;
128     }
129     return all;
130 }