Fix for coverity #29409 - Use char 0 instead of NULL
[supertux.git] / src / addon / md5.cpp
index 3020721..904e656 100644 (file)
@@ -12,8 +12,8 @@
 // implied warranty of any kind.
 //
 // The translators/modifiers do not claim:
-// (1) that MD5 will do what you think it does; 
-// (2) that this translation/ modification is accurate; or 
+// (1) that MD5 will do what you think it does;
+// (2) that this translation/ modification is accurate; or
 // (3) that this software is "merchantible."
 //
 // based on:
 // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
 // Algorithm" in all material mentioning or referencing this software
 // or this function.
-// 
+//
 // License is also granted to make and use derivative works provided
 // that such works are identified as "derived from the RSA Data
 // Security, Inc. MD5 Message-Digest Algorithm" in all material
 // mentioning or referencing the derived work.
-// 
+//
 // RSA Data Security, Inc. makes no representations concerning either
 // the merchantability of this software or the suitability of this
 // software for any particular purpose. It is provided "as is"
 // without express or implied warranty of any kind.
-// 
+//
 // These notices must be retained in any copies of any part of this
 // documentation and/or software.
-// 
+//
 
-#include "md5.hpp"
+#include "addon/md5.hpp"
 
 #include <assert.h>
-#include <strings.h>
-#include <iostream>
 #include <stdexcept>
 
-MD5::MD5() {
+MD5::MD5() :
+  buffer(),
+  digest(),
+  finalized()
+{
   init();
 }
 
@@ -67,7 +69,6 @@ void MD5::update (uint8_t* input, uint32_t input_length) {
 
   count[1] += ((uint32_t)input_length >> 29);
 
-
   buffer_space = 64 - buffer_index; // how much space is left in buffer
 
   // Transform as many times as possible.
@@ -85,56 +86,58 @@ void MD5::update (uint8_t* input, uint32_t input_length) {
   } else
     input_index=0; // so we can buffer the whole input
 
-
   // and here we do the buffering:
   memcpy(buffer+buffer_index, input+input_index, input_length-input_index);
 }
 
 void MD5::update(FILE *file) {
-  uint8_t buffer[1024];
+  uint8_t buffer_[1024];
   int len;
 
-  while ((len=fread(buffer, 1, 1024, file))) update(buffer, len);
+  while ((len=fread(buffer_, 1, 1024, file))) update(buffer_, len);
 
   fclose (file);
 }
 
 void MD5::update(std::istream& stream) {
-  uint8_t buffer[1024];
-  int len;
+  uint8_t buffer_[1024];
 
   while (stream.good()) {
-    stream.read((char*)buffer, 1024); // note that return value of read is unusable.
-    len=stream.gcount();
-    update(buffer, len);
+    stream.read((char*)buffer_, 1024); // note that return value of read is unusable.
+    int len = stream.gcount();
+    update(buffer_, len);
   }
 }
 
 void MD5::update(std::ifstream& stream) {
-  uint8_t buffer[1024];
-  int len;
+  uint8_t buffer_[1024];
 
   while (stream.good()) {
-    stream.read((char*)buffer, 1024); // note that return value of read is unusable.
-    len=stream.gcount();
-    update(buffer, len);
+    stream.read((char*)buffer_, 1024); // note that return value of read is unusable.
+    int len = stream.gcount();
+    update(buffer_, len);
   }
 }
 
-
-MD5::MD5(FILE *file) {
+MD5::MD5(FILE *file) :
+  finalized()
+{
   init(); // must be called be all constructors
   update(file);
   finalize ();
 }
 
-MD5::MD5(std::istream& stream) {
+MD5::MD5(std::istream& stream) :
+  finalized()
+{
   init(); // must called by all constructors
   update (stream);
   finalize();
 }
 
-MD5::MD5(std::ifstream& stream) {
+MD5::MD5(std::ifstream& stream) :
+  finalized()
+{
   init(); // must called by all constructors
   update (stream);
   finalize();
@@ -159,7 +162,11 @@ std::string MD5::hex_digest() {
 
   s[32]='\0';
 
-  return s;
+  // Create string from 's'
+  std::string s_str = std::string(s);
+  delete[] s;
+
+  return s_str;
 }
 
 std::ostream& operator<<(std::ostream &stream, MD5 context) {
@@ -167,7 +174,6 @@ std::ostream& operator<<(std::ostream &stream, MD5 context) {
   return stream;
 }
 
-
 // PRIVATE METHODS:
 
 void MD5::init() {
@@ -396,3 +402,5 @@ inline void MD5::II(uint32_t& a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
   a += I(b, c, d) + x + ac;
   a = rotate_left (a, s) +b;
 }
+
+/* EOF */