src/examples/{encrypt,decrypt}: Read input using C++

cppcheck found an exception safety bug in the old code, but like,
really now.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2013-02-18 01:58:37 -05:00
committed by Keith Winstein
parent ecdd2dd648
commit 5f807dfdf8
2 changed files with 8 additions and 54 deletions
+4 -27
View File
@@ -31,12 +31,8 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include "crypto.h"
@@ -56,31 +52,12 @@ int main( int argc, char *argv[] )
Nonce nonce( myatoi( argv[ 1 ] ) );
/* Read input */
char *input = NULL;
int total_size = 0;
while ( 1 ) {
unsigned char buf[ 16384 ];
ssize_t bytes_read = read( STDIN_FILENO, buf, 16384 );
if ( bytes_read == 0 ) { /* EOF */
break;
} else if ( bytes_read < 0 ) {
perror( "read" );
exit( 1 );
} else {
input = (char *)realloc( input, total_size + bytes_read );
assert( input );
memcpy( input + total_size, buf, bytes_read );
total_size += bytes_read;
}
}
string plaintext( input, total_size );
free( input );
ostringstream input;
input << cin.rdbuf();
/* Encrypt message */
string ciphertext = session.encrypt( Message( nonce, plaintext ) );
string ciphertext = session.encrypt( Message( nonce, input.str() ) );
cerr << "Key: " << key.printable_key() << endl;