From ecdd2dd6488bf651e00197bf9f5cd0b136527ce2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 18 Feb 2013 02:25:16 -0500 Subject: [PATCH] PRNG: Read input using C++ In the old code, cppcheck complained about throwing in the destructor, but like, seriously? Signed-off-by: Anders Kaseorg --- src/crypto/prng.h | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/crypto/prng.h b/src/crypto/prng.h index 0de7747..efcddf0 100644 --- a/src/crypto/prng.h +++ b/src/crypto/prng.h @@ -34,10 +34,8 @@ #define PRNG_HPP #include -#include -#include -#include #include +#include #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( dest ), size ); + if ( !randfile ) { throw CryptoException( "Could not read from " + std::string( rdev ) ); } }