
From: levill_r <levill_r@4aad255d-cdde-0310-9447-f3009e2ae8c0> * libmd5/md5.hh, libmd5/driver.cc: Use modern C++ header names. Add missing `std::' qualifiers. * libmd5/md5.cc: Likewise. (MD5::update(std::ifstream&)): Cast the type of the buffer to make it compatible with std::istream::read. git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@4657 4aad255d-cdde-0310-9447-f3009e2ae8c0 --- dynamic-use-of-static-c++/ChangeLog | 10 ++++++ dynamic-use-of-static-c++/libmd5/driver.cc | 46 ++++++++++++++-------------- dynamic-use-of-static-c++/libmd5/md5.cc | 41 +++++++++++++----------- dynamic-use-of-static-c++/libmd5/md5.hh | 16 +++++----- 4 files changed, 63 insertions(+), 50 deletions(-) diff --git a/dynamic-use-of-static-c++/ChangeLog b/dynamic-use-of-static-c++/ChangeLog index 38756f3..a9dec35 100644 --- a/dynamic-use-of-static-c++/ChangeLog +++ b/dynamic-use-of-static-c++/ChangeLog @@ -1,5 +1,15 @@ 2009-10-20 Roland Levillain <roland@lrde.epita.fr> + Have libmd5 compile. + + * libmd5/md5.hh, libmd5/driver.cc: Use modern C++ header names. + Add missing `std::' qualifiers. + * libmd5/md5.cc: Likewise. + (MD5::update(std::ifstream&)): Cast the type of the buffer to make + it compatible with std::istream::read. + +2009-10-20 Roland Levillain <roland@lrde.epita.fr> + Import a C++ implementation of the MD5 algorithm. * libmd5/: New directory. diff --git a/dynamic-use-of-static-c++/libmd5/driver.cc b/dynamic-use-of-static-c++/libmd5/driver.cc index 3624a7d..acaa3f7 100644 --- a/dynamic-use-of-static-c++/libmd5/driver.cc +++ b/dynamic-use-of-static-c++/libmd5/driver.cc @@ -27,10 +27,10 @@ These notices must be retained in any copies of any part of this documentation and/or software. */ -#include <stdio.h> -#include <stdlib.h> -#include <time.h> -#include <string.h> +#include <cstdio> +#include <cstdlib> +#include <ctime> +#include <cstring> #include "md5.hh" @@ -62,11 +62,11 @@ int main (int argc, char *argv[]){ else if (strcmp (argv[i], "-x") == 0) MD5_testSuite (); else if (strcmp (argv[i], "-h") == 0) - cout << MD5_usage()<< flush; + std::cout << MD5_usage() << std::flush; else if (strcmp (argv[i], "-help")==0) - cout << MD5_usage()<< flush; + std::cout << MD5_usage() << std::flush; else if (argv[i][0] == '-'){ - cerr << argv[i] << " is an unknown option.\n" << MD5_usage()<< flush; + std::cerr << argv[i] << " is an unknown option.\n" << MD5_usage() << std::flush; exit (1); } else @@ -88,8 +88,8 @@ static void MD5_timeTrial () unsigned int i; - cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " "; - cout << TEST_BLOCK_COUNT << "-byte blocks ..."; + std::cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " "; + std::cout << TEST_BLOCK_COUNT << "-byte blocks ..."; // Initialize block for (i = 0; i < TEST_BLOCK_LEN; i++) @@ -107,15 +107,15 @@ static void MD5_timeTrial () // Stop timer time (&endTime); - cout << " done" << endl; + std::cout << " done" << std::endl; - cout << "Digest = " << context << endl; + std::cout << "Digest = " << context << std::endl; - cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl; + std::cout << "Time = "<< (long)(endTime-startTime) << " seconds" << std::endl; - cout << "Speed = "; - cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime); - cout << "bytes/second" <<endl; + std::cout << "Speed = "; + std::cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime); + std::cout << "bytes/second" << std::endl; } @@ -123,7 +123,7 @@ static void MD5_timeTrial () static void MD5_testSuite () { - cout << "MD5 test suite:" << endl; + std::cout << "MD5 test suite:" << std::endl; MD5_string ( (unsigned char*) ""); MD5_string ( (unsigned char*) "a"); @@ -142,13 +142,13 @@ static void MD5_testSuite () static void MD5_file (char *filename){ - ifstream file(filename); + std::ifstream file(filename); if (!file) - cerr << filename <<" can't be opened" << endl; + std::cerr << filename <<" can't be opened" << std::endl; else { MD5 context(file); - cout << "MD5 (" << filename << ") = " << context << endl; + std::cout << "MD5 (" << filename << ") = " << context << std::endl; } } @@ -157,10 +157,10 @@ static void MD5_file (char *filename){ static void MD5_filter () { - MD5 context(cin); // ie. run istream version of MD5 on cin. - // Could also run file version of MD5 on stdin. + MD5 context(std::cin); // ie. run istream version of MD5 on std::cin. + // Could also run file version of MD5 on stdin. - cout << context << endl; + std::cout << context << std::endl; } @@ -175,7 +175,7 @@ void MD5_string (unsigned char *string){ context.update (string, len); context.finalize (); - cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl; + std::cout << "MD5 (\"" << (char *)string << "\") = " << context << std::endl; } diff --git a/dynamic-use-of-static-c++/libmd5/md5.cc b/dynamic-use-of-static-c++/libmd5/md5.cc index 8a547bd..6d9cab5 100644 --- a/dynamic-use-of-static-c++/libmd5/md5.cc +++ b/dynamic-use-of-static-c++/libmd5/md5.cc @@ -45,11 +45,12 @@ documentation and/or software. -#include "md5.hh" - -#include <assert.h> +#include <cassert> #include <strings.h> -#include <iostream.h> + +#include <iostream> + +#include "md5.hh" @@ -75,7 +76,7 @@ void MD5::update (uint1 *input, uint4 input_length) { uint4 buffer_space; // how much space is left in buffer if (finalized){ // so we can't update! - cerr << "MD5::update: Can't update a finalized digest!" << endl; + std::cerr << "MD5::update: Can't update a finalized digest!" << std::endl; return; } @@ -134,16 +135,17 @@ void MD5::update(FILE *file){ -// MD5 update for istreams. +// MD5 update for std::istreams. // Like update for files; see above. -void MD5::update(istream& stream){ +void MD5::update(std::istream& stream){ unsigned char buffer[1024]; int len; + char* buffer_ = (char*)(buffer); while (stream.good()){ - stream.read(buffer, 1024); // note that return value of read is unusable. + stream.read(buffer_, 1024); // note that return value of read is unusable. len=stream.gcount(); update(buffer, len); } @@ -155,16 +157,17 @@ void MD5::update(istream& stream){ -// MD5 update for ifstreams. +// MD5 update for std::ifstreams. // Like update for files; see above. -void MD5::update(ifstream& stream){ +void MD5::update(std::ifstream& stream){ unsigned char buffer[1024]; int len; while (stream.good()){ - stream.read(buffer, 1024); // note that return value of read is unusable. + // FIXME: Cast BUFFER to char* to be compatible with std::istream::read. + stream.read((char*)buffer, 1024); // note that return value of read is unusable. len=stream.gcount(); update(buffer, len); } @@ -191,7 +194,7 @@ void MD5::finalize (){ }; if (finalized){ - cerr << "MD5::finalize: Already finalized this digest!" << endl; + std::cerr << "MD5::finalize: Already finalized this digest!" << std::endl; return; } @@ -229,7 +232,7 @@ MD5::MD5(FILE *file){ -MD5::MD5(istream& stream){ +MD5::MD5(std::istream& stream){ init(); // must called by all constructors update (stream); @@ -238,7 +241,7 @@ MD5::MD5(istream& stream){ -MD5::MD5(ifstream& stream){ +MD5::MD5(std::ifstream& stream){ init(); // must called by all constructors update (stream); @@ -252,8 +255,8 @@ unsigned char *MD5::raw_digest(){ uint1 *s = new uint1[16]; if (!finalized){ - cerr << "MD5::raw_digest: Can't get digest if you haven't "<< - "finalized the digest!" <<endl; + std::cerr << "MD5::raw_digest: Can't get digest if you haven't "<< + "finalized the digest!" << std::endl; return ( (unsigned char*) ""); } @@ -269,8 +272,8 @@ char *MD5::hex_digest(){ char *s= new char[33]; if (!finalized){ - cerr << "MD5::hex_digest: Can't get digest if you haven't "<< - "finalized the digest!" <<endl; + std::cerr << "MD5::hex_digest: Can't get digest if you haven't "<< + "finalized the digest!" << std::endl; return ""; } @@ -286,7 +289,7 @@ char *MD5::hex_digest(){ -ostream& operator<<(ostream &stream, MD5 context){ +std::ostream& operator<<(std::ostream &stream, MD5 context){ stream << context.hex_digest(); return stream; diff --git a/dynamic-use-of-static-c++/libmd5/md5.hh b/dynamic-use-of-static-c++/libmd5/md5.hh index b8bc075..5c72059 100644 --- a/dynamic-use-of-static-c++/libmd5/md5.hh +++ b/dynamic-use-of-static-c++/libmd5/md5.hh @@ -39,9 +39,9 @@ documentation and/or software. */ -#include <stdio.h> -#include <fstream.h> -#include <iostream.h> +#include <cstdio> +#include <fstream> +#include <iostream> class MD5 { @@ -49,22 +49,22 @@ public: // methods for controlled operation: MD5 (); // simple initializer void update (unsigned char *input, unsigned int input_length); - void update (istream& stream); + void update (std::istream& stream); void update (FILE *file); - void update (ifstream& stream); + void update (std::ifstream& stream); void finalize (); // constructors for special circumstances. All these constructors finalize // the MD5 context. MD5 (unsigned char *string); // digest string, finalize - MD5 (istream& stream); // digest stream, finalize + MD5 (std::istream& stream); // digest stream, finalize MD5 (FILE *file); // digest file, close, finalize - MD5 (ifstream& stream); // digest stream, close, finalize + MD5 (std::ifstream& stream); // digest stream, close, finalize // methods to acquire finalized result unsigned char *raw_digest (); // digest as a 16-byte binary array char * hex_digest (); // digest as a 33-byte ascii-hex string - friend ostream& operator<< (ostream&, MD5 context); + friend std::ostream& operator<< (std::ostream&, MD5 context); -- 1.6.5