PRNG: Read input using C++
In the old code, cppcheck complained about throwing in the destructor, but like, seriously? Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
committed by
Keith Winstein
parent
14ef590220
commit
ecdd2dd648
+5
-17
@@ -34,10 +34,8 @@
|
||||
#define PRNG_HPP
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <fstream>
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
@@ -51,32 +49,22 @@ using namespace Crypto;
|
||||
|
||||
class PRNG {
|
||||
private:
|
||||
FILE *randfile;
|
||||
std::ifstream randfile;
|
||||
|
||||
/* unimplemented to satisfy -Weffc++ */
|
||||
PRNG( const PRNG & );
|
||||
PRNG & operator=( const PRNG & );
|
||||
|
||||
public:
|
||||
PRNG() : randfile( fopen( rdev, "rb" ) )
|
||||
{
|
||||
if ( randfile == NULL ) {
|
||||
throw CryptoException( std::string( rdev ) + ": " + strerror( errno ) );
|
||||
}
|
||||
}
|
||||
|
||||
~PRNG() {
|
||||
if ( 0 != fclose( randfile ) ) {
|
||||
throw CryptoException( std::string( rdev ) + ": " + strerror( errno ) );
|
||||
}
|
||||
}
|
||||
PRNG() : randfile( rdev, std::ifstream::in | std::ifstream::binary ) {}
|
||||
|
||||
void fill( void *dest, size_t size ) {
|
||||
if ( 0 == size ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 1 != fread( dest, size, 1, randfile ) ) {
|
||||
randfile.read( static_cast<char *>( dest ), size );
|
||||
if ( !randfile ) {
|
||||
throw CryptoException( "Could not read from " + std::string( rdev ) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user