Const correctness and const-ref in Crypto and elsewhere.
This commit is contained in:
+10
-10
@@ -184,14 +184,14 @@ Nonce::Nonce( uint64_t val )
|
|||||||
memcpy( bytes + 4, &val_net, 8 );
|
memcpy( bytes + 4, &val_net, 8 );
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t Nonce::val( void )
|
uint64_t Nonce::val( void ) const
|
||||||
{
|
{
|
||||||
uint64_t ret;
|
uint64_t ret;
|
||||||
memcpy( &ret, bytes + 4, 8 );
|
memcpy( &ret, bytes + 4, 8 );
|
||||||
return be64toh( ret );
|
return be64toh( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
Nonce::Nonce( char *s_bytes, size_t len )
|
Nonce::Nonce( const char *s_bytes, size_t len )
|
||||||
{
|
{
|
||||||
if ( len != 8 ) {
|
if ( len != 8 ) {
|
||||||
throw CryptoException( "Nonce representation must be 8 octets long." );
|
throw CryptoException( "Nonce representation must be 8 octets long." );
|
||||||
@@ -201,18 +201,18 @@ Nonce::Nonce( char *s_bytes, size_t len )
|
|||||||
memcpy( bytes + 4, s_bytes, 8 );
|
memcpy( bytes + 4, s_bytes, 8 );
|
||||||
}
|
}
|
||||||
|
|
||||||
Message::Message( char *nonce_bytes, size_t nonce_len,
|
Message::Message( const char *nonce_bytes, size_t nonce_len,
|
||||||
char *text_bytes, size_t text_len )
|
const char *text_bytes, size_t text_len )
|
||||||
: nonce( nonce_bytes, nonce_len ),
|
: nonce( nonce_bytes, nonce_len ),
|
||||||
text( (char *)text_bytes, text_len )
|
text( text_bytes, text_len )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Message::Message( Nonce s_nonce, string s_text )
|
Message::Message( const Nonce & s_nonce, const string & s_text )
|
||||||
: nonce( s_nonce ),
|
: nonce( s_nonce ),
|
||||||
text( s_text )
|
text( s_text )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
string Session::encrypt( Message plaintext )
|
const string Session::encrypt( const Message & plaintext )
|
||||||
{
|
{
|
||||||
const size_t pt_len = plaintext.text.size();
|
const size_t pt_len = plaintext.text.size();
|
||||||
const int ciphertext_len = pt_len + 16;
|
const int ciphertext_len = pt_len + 16;
|
||||||
@@ -262,13 +262,13 @@ string Session::encrypt( Message plaintext )
|
|||||||
return plaintext.nonce.cc_str() + text;
|
return plaintext.nonce.cc_str() + text;
|
||||||
}
|
}
|
||||||
|
|
||||||
Message Session::decrypt( string ciphertext )
|
const Message Session::decrypt( const string & ciphertext )
|
||||||
{
|
{
|
||||||
if ( ciphertext.size() < 24 ) {
|
if ( ciphertext.size() < 24 ) {
|
||||||
throw CryptoException( "Ciphertext must contain nonce and tag." );
|
throw CryptoException( "Ciphertext must contain nonce and tag." );
|
||||||
}
|
}
|
||||||
|
|
||||||
char *str = (char *)ciphertext.data();
|
const char *str = ciphertext.data();
|
||||||
|
|
||||||
int body_len = ciphertext.size() - 8;
|
int body_len = ciphertext.size() - 8;
|
||||||
int pt_len = body_len - 16;
|
int pt_len = body_len - 16;
|
||||||
@@ -297,7 +297,7 @@ Message Session::decrypt( string ciphertext )
|
|||||||
throw CryptoException( "Packet failed integrity check." );
|
throw CryptoException( "Packet failed integrity check." );
|
||||||
}
|
}
|
||||||
|
|
||||||
Message ret( nonce, string( plaintext_buffer.data(), pt_len ) );
|
const Message ret( nonce, string( plaintext_buffer.data(), pt_len ) );
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -109,21 +109,21 @@ namespace Crypto {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Nonce( uint64_t val );
|
Nonce( uint64_t val );
|
||||||
Nonce( char *s_bytes, size_t len );
|
Nonce( const char *s_bytes, size_t len );
|
||||||
|
|
||||||
string cc_str( void ) const { return string( bytes + 4, 8 ); }
|
string cc_str( void ) const { return string( bytes + 4, 8 ); }
|
||||||
const char *data( void ) const { return bytes; }
|
const char *data( void ) const { return bytes; }
|
||||||
uint64_t val( void );
|
uint64_t val( void ) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Message {
|
class Message {
|
||||||
public:
|
public:
|
||||||
Nonce nonce;
|
const Nonce nonce;
|
||||||
string text;
|
const string text;
|
||||||
|
|
||||||
Message( char *nonce_bytes, size_t nonce_len,
|
Message( const char *nonce_bytes, size_t nonce_len,
|
||||||
char *text_bytes, size_t text_len );
|
const char *text_bytes, size_t text_len );
|
||||||
Message( Nonce s_nonce, string s_text );
|
Message( const Nonce & s_nonce, const string & s_text );
|
||||||
};
|
};
|
||||||
|
|
||||||
class Session {
|
class Session {
|
||||||
@@ -145,8 +145,8 @@ namespace Crypto {
|
|||||||
Session( Base64Key s_key );
|
Session( Base64Key s_key );
|
||||||
~Session();
|
~Session();
|
||||||
|
|
||||||
string encrypt( Message plaintext );
|
const string encrypt( const Message & plaintext );
|
||||||
Message decrypt( string ciphertext );
|
const Message decrypt( const string & ciphertext );
|
||||||
|
|
||||||
Session( const Session & );
|
Session( const Session & );
|
||||||
Session & operator=( const Session & );
|
Session & operator=( const Session & );
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ string Packet::tostring( Session *session )
|
|||||||
return session->encrypt( Message( Nonce( direction_seq ), timestamps + payload ) );
|
return session->encrypt( Message( Nonce( direction_seq ), timestamps + payload ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet Connection::new_packet( string &s_payload )
|
Packet Connection::new_packet( const string &s_payload )
|
||||||
{
|
{
|
||||||
uint16_t outgoing_timestamp_reply = -1;
|
uint16_t outgoing_timestamp_reply = -1;
|
||||||
|
|
||||||
@@ -391,7 +391,7 @@ Connection::Connection( const char *key_str, const char *ip, const char *port )
|
|||||||
set_MTU( remote_addr.sa.sa_family );
|
set_MTU( remote_addr.sa.sa_family );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::send( string s )
|
void Connection::send( const string & s )
|
||||||
{
|
{
|
||||||
if ( !has_remote_addr ) {
|
if ( !has_remote_addr ) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ namespace Network {
|
|||||||
string payload;
|
string payload;
|
||||||
|
|
||||||
Packet( Direction s_direction,
|
Packet( Direction s_direction,
|
||||||
uint16_t s_timestamp, uint16_t s_timestamp_reply, string s_payload )
|
uint16_t s_timestamp, uint16_t s_timestamp_reply, const string & s_payload )
|
||||||
: seq( Crypto::unique() ), direction( s_direction ),
|
: seq( Crypto::unique() ), direction( s_direction ),
|
||||||
timestamp( s_timestamp ), timestamp_reply( s_timestamp_reply ), payload( s_payload )
|
timestamp( s_timestamp ), timestamp_reply( s_timestamp_reply ), payload( s_payload )
|
||||||
{}
|
{}
|
||||||
@@ -190,7 +190,7 @@ namespace Network {
|
|||||||
bool have_send_exception;
|
bool have_send_exception;
|
||||||
NetworkException send_exception;
|
NetworkException send_exception;
|
||||||
|
|
||||||
Packet new_packet( string &s_payload );
|
Packet new_packet( const string &s_payload );
|
||||||
|
|
||||||
void hop_port( void );
|
void hop_port( void );
|
||||||
|
|
||||||
@@ -209,7 +209,7 @@ namespace Network {
|
|||||||
Connection( const char *desired_ip, const char *desired_port ); /* server */
|
Connection( const char *desired_ip, const char *desired_port ); /* server */
|
||||||
Connection( const char *key_str, const char *ip, const char *port ); /* client */
|
Connection( const char *key_str, const char *ip, const char *port ); /* client */
|
||||||
|
|
||||||
void send( string s );
|
void send( const string & s );
|
||||||
string recv( void );
|
string recv( void );
|
||||||
const std::vector< int > fds( void ) const;
|
const std::vector< int > fds( void ) const;
|
||||||
int get_MTU( void ) const { return MTU; }
|
int get_MTU( void ) const { return MTU; }
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ string Fragment::tostring( void )
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Fragment::Fragment( string &x )
|
Fragment::Fragment( const string &x )
|
||||||
: id( -1 ), fragment_num( -1 ), final( false ), initialized( true ),
|
: id( -1 ), fragment_num( -1 ), final( false ), initialized( true ),
|
||||||
contents()
|
contents()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -61,12 +61,12 @@ namespace Network {
|
|||||||
: id( -1 ), fragment_num( -1 ), final( false ), initialized( false ), contents()
|
: id( -1 ), fragment_num( -1 ), final( false ), initialized( false ), contents()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Fragment( uint64_t s_id, uint16_t s_fragment_num, bool s_final, string s_contents )
|
Fragment( uint64_t s_id, uint16_t s_fragment_num, bool s_final, const string & s_contents )
|
||||||
: id( s_id ), fragment_num( s_fragment_num ), final( s_final ), initialized( true ),
|
: id( s_id ), fragment_num( s_fragment_num ), final( s_final ), initialized( true ),
|
||||||
contents( s_contents )
|
contents( s_contents )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Fragment( string &x );
|
Fragment( const string &x );
|
||||||
|
|
||||||
string tostring( void );
|
string tostring( void );
|
||||||
|
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ void TransportSender<MyState>::add_sent_state( uint64_t the_timestamp, uint64_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class MyState>
|
template <class MyState>
|
||||||
void TransportSender<MyState>::send_to_receiver( string diff )
|
void TransportSender<MyState>::send_to_receiver( const string & diff )
|
||||||
{
|
{
|
||||||
uint64_t new_num;
|
uint64_t new_num;
|
||||||
if ( current_state == sent_states.back().state ) { /* previously sent */
|
if ( current_state == sent_states.back().state ) { /* previously sent */
|
||||||
@@ -310,7 +310,7 @@ const string TransportSender<MyState>::make_chaff( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class MyState>
|
template <class MyState>
|
||||||
void TransportSender<MyState>::send_in_fragments( string diff, uint64_t new_num )
|
void TransportSender<MyState>::send_in_fragments( const string & diff, uint64_t new_num )
|
||||||
{
|
{
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
|
|
||||||
|
|||||||
@@ -64,9 +64,9 @@ namespace Network {
|
|||||||
void update_assumed_receiver_state( void );
|
void update_assumed_receiver_state( void );
|
||||||
void attempt_prospective_resend_optimization( string &proposed_diff );
|
void attempt_prospective_resend_optimization( string &proposed_diff );
|
||||||
void rationalize_states( void );
|
void rationalize_states( void );
|
||||||
void send_to_receiver( string diff );
|
void send_to_receiver( const string & diff );
|
||||||
void send_empty_ack( void );
|
void send_empty_ack( void );
|
||||||
void send_in_fragments( string diff, uint64_t new_num );
|
void send_in_fragments( const string & diff, uint64_t new_num );
|
||||||
void add_sent_state( uint64_t the_timestamp, uint64_t num, MyState &state );
|
void add_sent_state( uint64_t the_timestamp, uint64_t num, MyState &state );
|
||||||
|
|
||||||
/* state of sender */
|
/* state of sender */
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Network {
|
|||||||
uint64_t num;
|
uint64_t num;
|
||||||
State state;
|
State state;
|
||||||
|
|
||||||
TimestampedState( uint64_t s_timestamp, uint64_t s_num, State &s_state )
|
TimestampedState( uint64_t s_timestamp, uint64_t s_num, const State &s_state )
|
||||||
: timestamp( s_timestamp ), num( s_num ), state( s_state )
|
: timestamp( s_timestamp ), num( s_num ), state( s_state )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ string Complete::init_diff( void ) const
|
|||||||
return diff_from( Complete( get_fb().ds.get_width(), get_fb().ds.get_height() ));
|
return diff_from( Complete( get_fb().ds.get_width(), get_fb().ds.get_height() ));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Complete::apply_string( string diff )
|
void Complete::apply_string( const string & diff )
|
||||||
{
|
{
|
||||||
HostBuffers::HostMessage input;
|
HostBuffers::HostMessage input;
|
||||||
fatal_assert( input.ParseFromString( diff ) );
|
fatal_assert( input.ParseFromString( diff ) );
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ namespace Terminal {
|
|||||||
void subtract( const Complete * ) const {}
|
void subtract( const Complete * ) const {}
|
||||||
std::string diff_from( const Complete &existing ) const;
|
std::string diff_from( const Complete &existing ) const;
|
||||||
std::string init_diff( void ) const;
|
std::string init_diff( void ) const;
|
||||||
void apply_string( std::string diff );
|
void apply_string( const std::string & diff );
|
||||||
bool operator==( const Complete &x ) const;
|
bool operator==( const Complete &x ) const;
|
||||||
|
|
||||||
bool compare( const Complete &other ) const;
|
bool compare( const Complete &other ) const;
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ string UserStream::diff_from( const UserStream &existing ) const
|
|||||||
return output.SerializeAsString();
|
return output.SerializeAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserStream::apply_string( string diff )
|
void UserStream::apply_string( const string &diff )
|
||||||
{
|
{
|
||||||
ClientBuffers::UserMessage input;
|
ClientBuffers::UserMessage input;
|
||||||
fatal_assert( input.ParseFromString( diff ) );
|
fatal_assert( input.ParseFromString( diff ) );
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ namespace Network {
|
|||||||
Parser::UserByte userbyte;
|
Parser::UserByte userbyte;
|
||||||
Parser::Resize resize;
|
Parser::Resize resize;
|
||||||
|
|
||||||
UserEvent( Parser::UserByte s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 ) {}
|
UserEvent( const Parser::UserByte & s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 ) {}
|
||||||
UserEvent( Parser::Resize s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}
|
UserEvent( const Parser::Resize & s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}
|
||||||
|
|
||||||
UserEvent() /* default constructor required by C++11 STL */
|
UserEvent() /* default constructor required by C++11 STL */
|
||||||
: type( UserByteType ),
|
: type( UserByteType ),
|
||||||
@@ -79,8 +79,8 @@ namespace Network {
|
|||||||
public:
|
public:
|
||||||
UserStream() : actions() {}
|
UserStream() : actions() {}
|
||||||
|
|
||||||
void push_back( Parser::UserByte s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }
|
void push_back( const Parser::UserByte & s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }
|
||||||
void push_back( Parser::Resize s_resize ) { actions.push_back( UserEvent( s_resize ) ); }
|
void push_back( const Parser::Resize & s_resize ) { actions.push_back( UserEvent( s_resize ) ); }
|
||||||
|
|
||||||
bool empty( void ) const { return actions.empty(); }
|
bool empty( void ) const { return actions.empty(); }
|
||||||
size_t size( void ) const { return actions.size(); }
|
size_t size( void ) const { return actions.size(); }
|
||||||
@@ -90,7 +90,7 @@ namespace Network {
|
|||||||
void subtract( const UserStream *prefix );
|
void subtract( const UserStream *prefix );
|
||||||
string diff_from( const UserStream &existing ) const;
|
string diff_from( const UserStream &existing ) const;
|
||||||
string init_diff( void ) const { assert( false ); return string(); };
|
string init_diff( void ) const { assert( false ); return string(); };
|
||||||
void apply_string( string diff );
|
void apply_string( const string &diff );
|
||||||
bool operator==( const UserStream &x ) const { return actions == x.actions; }
|
bool operator==( const UserStream &x ) const { return actions == x.actions; }
|
||||||
|
|
||||||
bool compare( const UserStream & ) { return false; }
|
bool compare( const UserStream & ) { return false; }
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ DispatchRegistry & Terminal::get_global_dispatch_registry( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void register_function( Function_Type type,
|
static void register_function( Function_Type type,
|
||||||
std::string dispatch_chars,
|
const std::string & dispatch_chars,
|
||||||
Function f )
|
Function f )
|
||||||
{
|
{
|
||||||
switch ( type ) {
|
switch ( type ) {
|
||||||
@@ -186,7 +186,7 @@ static void register_function( Function_Type type,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Function::Function( Function_Type type, std::string dispatch_chars,
|
Function::Function( Function_Type type, const std::string & dispatch_chars,
|
||||||
void (*s_function)( Framebuffer *, Dispatcher * ),
|
void (*s_function)( Framebuffer *, Dispatcher * ),
|
||||||
bool s_clears_wrap_state )
|
bool s_clears_wrap_state )
|
||||||
: function( s_function ), clears_wrap_state( s_clears_wrap_state )
|
: function( s_function ), clears_wrap_state( s_clears_wrap_state )
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ namespace Terminal {
|
|||||||
class Function {
|
class Function {
|
||||||
public:
|
public:
|
||||||
Function() : function( NULL ), clears_wrap_state( true ) {}
|
Function() : function( NULL ), clears_wrap_state( true ) {}
|
||||||
Function( Function_Type type, std::string dispatch_chars,
|
Function( Function_Type type, const std::string & dispatch_chars,
|
||||||
void (*s_function)( Framebuffer *, Dispatcher * ),
|
void (*s_function)( Framebuffer *, Dispatcher * ),
|
||||||
bool s_clears_wrap_state = true );
|
bool s_clears_wrap_state = true );
|
||||||
void (*function)( Framebuffer *, Dispatcher * );
|
void (*function)( Framebuffer *, Dispatcher * );
|
||||||
|
|||||||
@@ -49,9 +49,9 @@ namespace Terminal {
|
|||||||
|
|
||||||
FrameState( const Framebuffer &s_last );
|
FrameState( const Framebuffer &s_last );
|
||||||
|
|
||||||
void append( const char c ) { str.append( 1, c ); }
|
void append( char c ) { str.append( 1, c ); }
|
||||||
void append( const size_t s, const char c ) { str.append( s, c ); }
|
void append( size_t s, char c ) { str.append( s, c ); }
|
||||||
void append( const wchar_t wc ) { Cell::append_to_str( str, wc ); }
|
void append( wchar_t wc ) { Cell::append_to_str( str, wc ); }
|
||||||
void append( const char * s ) { str.append( s ); }
|
void append( const char * s ) { str.append( s ); }
|
||||||
void append_string( const std::string &append ) { str.append(append); }
|
void append_string( const std::string &append ) { str.append(append); }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user