Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize some C++ #1284

Merged
merged 4 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/crypto/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
also delete it here.
*/

#include <string.h>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>

#include "src/util/fatal_assert.h"
#include "src/crypto/base64.h"
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
also delete it here.
*/

#include <stdint.h>
#include <cstdint>

bool base64_decode( const char *b64, const size_t b64_len,
uint8_t *raw, size_t *raw_len );
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

/* Use our fallback implementation, which is correct for any endianness. */

#include <stdint.h>
#include <cstdint>

/* Make sure they aren't macros */
#undef htobe64
Expand Down
29 changes: 15 additions & 14 deletions src/crypto/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
also delete it here.
*/

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/resource.h>
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>

#include <sys/resource.h>

#include "src/crypto/byteorder.h"
#include "src/crypto/crypto.h"
#include "src/crypto/base64.h"
Expand Down Expand Up @@ -107,13 +108,13 @@ AlignedBuffer::AlignedBuffer( size_t len, const char *data )
}
}

Base64Key::Base64Key( string printable_key )
Base64Key::Base64Key( std::string printable_key )
{
if ( printable_key.length() != 22 ) {
throw CryptoException( "Key must be 22 letters long." );
}

string base64 = printable_key + "==";
std::string base64 = printable_key + "==";

size_t len = 16;
if ( !base64_decode( base64.data(), 24, key, &len ) ) {
Expand All @@ -140,19 +141,19 @@ Base64Key::Base64Key(PRNG &prng)
prng.fill( key, sizeof( key ) );
}

string Base64Key::printable_key( void ) const
std::string Base64Key::printable_key( void ) const
{
char base64[ 24 ];

base64_encode( key, 16, base64, 24 );

if ( (base64[ 23 ] != '=')
|| (base64[ 22 ] != '=') ) {
throw CryptoException( string( "Unexpected output from base64_encode: " ) + string( base64, 24 ) );
throw CryptoException( std::string( "Unexpected output from base64_encode: " ) + std::string( base64, 24 ) );
}

base64[ 22 ] = 0;
return string( base64 );
return std::string( base64 );
}

Session::Session( Base64Key s_key )
Expand Down Expand Up @@ -197,7 +198,7 @@ Nonce::Nonce( const char *s_bytes, size_t len )
memcpy( bytes + 4, s_bytes, 8 );
}

const string Session::encrypt( const Message & plaintext )
const std::string Session::encrypt( const Message & plaintext )
{
const size_t pt_len = plaintext.text.size();
const int ciphertext_len = pt_len + 16;
Expand Down Expand Up @@ -242,7 +243,7 @@ const string Session::encrypt( const Message & plaintext )
throw CryptoException( "Encrypted 2^47 blocks.", true );
}

string text( ciphertext_buffer.data(), ciphertext_len );
std::string text( ciphertext_buffer.data(), ciphertext_len );

return plaintext.nonce.cc_str() + text;
}
Expand Down Expand Up @@ -280,7 +281,7 @@ const Message Session::decrypt( const char *str, size_t len )
throw CryptoException( "Packet failed integrity check." );
}

const Message ret( nonce, string( plaintext_buffer.data(), pt_len ) );
const Message ret( nonce, std::string( plaintext_buffer.data(), pt_len ) );

return ret;
}
Expand Down
29 changes: 14 additions & 15 deletions src/crypto/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,24 @@
#define CRYPTO_HPP

#include "src/crypto/ae.h"
#include <string>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <string>


long int myatoi( const char *str );

class PRNG;

namespace Crypto {
using std::string;

class CryptoException : public std::exception {
public:
string text;
std::string text;
bool fatal;
CryptoException( string s_text, bool s_fatal = false )
CryptoException( std::string s_text, bool s_fatal = false )
: text( s_text ), fatal( s_fatal ) {};
const char *what() const throw () { return text.c_str(); }
~CryptoException() throw () {}
Expand Down Expand Up @@ -95,8 +94,8 @@ namespace Crypto {
public:
Base64Key(); /* random key */
Base64Key(PRNG &prng);
Base64Key( string printable_key );
string printable_key( void ) const;
Base64Key( std::string printable_key );
std::string printable_key( void ) const;
unsigned char *data( void ) { return key; }
};

Expand All @@ -111,22 +110,22 @@ namespace Crypto {
Nonce( uint64_t val );
Nonce( const char *s_bytes, size_t len );

string cc_str( void ) const { return string( bytes + 4, 8 ); }
std::string cc_str( void ) const { return std::string( bytes + 4, 8 ); }
const char *data( void ) const { return bytes; }
uint64_t val( void ) const;
};

class Message {
public:
const Nonce nonce;
const string text;
const std::string text;

Message( const char *nonce_bytes, size_t nonce_len,
const char *text_bytes, size_t text_len )
: nonce( nonce_bytes, nonce_len ),
text( text_bytes, text_len ) {}

Message( const Nonce & s_nonce, const string & s_text )
Message( const Nonce & s_nonce, const std::string & s_text )
: nonce( s_nonce ),
text( s_text ) {}
};
Expand All @@ -150,9 +149,9 @@ namespace Crypto {
Session( Base64Key s_key );
~Session();

const string encrypt( const Message & plaintext );
const std::string encrypt( const Message & plaintext );
const Message decrypt( const char *str, size_t len );
const Message decrypt( const string & ciphertext ) {
const Message decrypt( const std::string & ciphertext ) {
return decrypt( ciphertext.data(), ciphertext.size() );
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypto/ocb_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
#include "src/crypto/ae.h"
#include "src/crypto/crypto.h"
#include "src/util/fatal_assert.h"
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <cstring>
#if defined(HAVE_STRINGS_H)
#include <strings.h>
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#ifndef PRNG_HPP
#define PRNG_HPP

#include <string>
#include <stdint.h>
#include <cstdint>
#include <fstream>
#include <string>

#include "src/crypto/crypto.h"

Expand Down
29 changes: 15 additions & 14 deletions src/examples/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@

#include "src/include/config.h"

#include <errno.h>
#include <locale.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <cerrno>
#include <climits>
#include <clocale>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>

#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pwd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <exception>
#include <unistd.h>

#if HAVE_PTY_H
#include <pty.h>
Expand Down Expand Up @@ -106,9 +107,9 @@ int main( int argc, char **argv )
overlays.apply( *new_state );

/* calculate minimal difference from where we are */
const string diff( display.new_frame( false,
*local_framebuffer,
*new_state ) );
const std::string diff( display.new_frame( false,
*local_framebuffer,
*new_state ) );

/* make sure to use diff */
if ( diff.size() > INT_MAX ) {
Expand Down
2 changes: 1 addition & 1 deletion src/examples/decrypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
also delete it here.
*/

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <sstream>

Expand Down
4 changes: 2 additions & 2 deletions src/examples/encrypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
also delete it here.
*/

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <sstream>

Expand All @@ -56,7 +56,7 @@ int main( int argc, char *argv[] )

/* Encrypt message */

string ciphertext = session.encrypt( Message( nonce, input.str() ) );
std::string ciphertext = session.encrypt( Message( nonce, input.str() ) );

std::cerr << "Key: " << key.printable_key() << std::endl;

Expand Down
5 changes: 3 additions & 2 deletions src/examples/ntester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
also delete it here.
*/

#include <memory>

#include <termios.h>
#include <unistd.h>

Expand All @@ -38,7 +40,6 @@
#include "src/util/pty_compat.h"
#include "src/network/networktransport-impl.h"
#include "src/util/select.h"
#include "src/util/shared.h"

using namespace Network;

Expand All @@ -50,7 +51,7 @@ int main( int argc, char *argv[] )
char *port;

UserStream me, remote;
typedef shared::shared_ptr<Transport<UserStream, UserStream> > NetworkPointer;
using NetworkPointer = std::shared_ptr<Transport<UserStream, UserStream>>;
Transport<UserStream, UserStream> *raw_n;
try {
if ( argc > 1 ) {
Expand Down
21 changes: 11 additions & 10 deletions src/examples/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@

#include "src/include/config.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
#include <assert.h>
#include <wctype.h>
#include <cassert>
#include <cerrno>
#include <clocale>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <cwctype>
#include <typeinfo>

#include <termios.h>
#include <unistd.h>

#if HAVE_PTY_H
#include <pty.h>
Expand Down
31 changes: 16 additions & 15 deletions src/examples/termemu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@

#include "src/include/config.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
#include <assert.h>
#include <wctype.h>
#include <cassert>
#include <cerrno>
#include <clocale>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <typeinfo>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <exception>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>

#if HAVE_PTY_H
#include <pty.h>
Expand Down
Loading
Loading