[Liblas-commits] r1240 - in trunk: include/liblas include/liblas/detail test/unit

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Apr 20 11:59:31 EDT 2009


Author: mloskot
Date: Mon Apr 20 11:59:30 2009
New Revision: 1240
URL: http://liblas.org/changeset/1240

Log:
Check std::remove error code in lasfile_test. Use stricter casting in sha1 and guide classes.

Modified:
   trunk/include/liblas/detail/sha1.hpp
   trunk/include/liblas/guid.hpp
   trunk/test/unit/lasfile_test.cpp

Modified: trunk/include/liblas/detail/sha1.hpp
==============================================================================
--- trunk/include/liblas/detail/sha1.hpp	(original)
+++ trunk/include/liblas/detail/sha1.hpp	Mon Apr 20 11:59:30 2009
@@ -53,6 +53,8 @@
 #ifndef LIBLAS_SHA1_HPP_INCLUDED
 #define LIBLAS_SHA1_HPP_INCLUDED
 
+#include <cassert>
+
 namespace liblas { namespace detail {
 
 class SHA1
@@ -114,21 +116,21 @@
      *  Comments:
      *
      */
-    void Reset()
-    {
-        Length_Low          = 0;
-        Length_High         = 0;
-        Message_Block_Index = 0;
-    
-        H[0]        = 0x67452301;
-        H[1]        = 0xEFCDAB89;
-        H[2]        = 0x98BADCFE;
-        H[3]        = 0x10325476;
-        H[4]        = 0xC3D2E1F0;
-    
-        Computed    = false;
-        Corrupted   = false;
-    }
+	void Reset()
+	{
+		Length_Low          = 0;
+		Length_High         = 0;
+		Message_Block_Index = 0;
+
+		H[0]        = 0x67452301;
+		H[1]        = 0xEFCDAB89;
+		H[2]        = 0x98BADCFE;
+		H[3]        = 0x10325476;
+		H[4]        = 0xC3D2E1F0;
+
+		Computed    = false;
+		Corrupted   = false;
+	}
     
     /*  
      *  Result
@@ -148,9 +150,11 @@
      *  Comments:
      *
      */
-    bool Result(unsigned *message_digest_array)
+    bool Result(unsigned int* message_digest_array)
     {
-        int i;                                  // Counter
+		assert(0 != message_digest_array);
+
+        int i = 0; // Counter
     
         if (Corrupted)
         {
@@ -189,9 +193,10 @@
      *  Comments:
      *
      */
-    void Input( const unsigned char *message_array,
-                        unsigned            length)
+    void Input(unsigned char const* message_array, unsigned int length)
     {
+		assert(0 != message_array);
+
         if (!length)
         {
             return;
@@ -248,10 +253,15 @@
      *  Comments:
      *
      */
-    void Input( const char  *message_array,
-                        unsigned    length)
+    void Input(char const* message_array, unsigned length)
     {
-        Input((unsigned char *) message_array, length);
+		assert(0 != message_array);
+
+		typedef unsigned char const* target_type;
+		typedef void const* proxy_type;
+
+		target_type p = static_cast<target_type>(static_cast<proxy_type>(message_array));
+        Input(p, length);
     }
     
     /*  
@@ -270,10 +280,10 @@
      *  Comments:
      *
      */
-    void Input(unsigned char message_element)
-    {
-        Input(&message_element, 1);
-    }
+	void Input(unsigned char message_element)
+	{
+		Input(&message_element, 1);
+	}
     
     /*  
      *  Input
@@ -291,10 +301,11 @@
      *  Comments:
      *
      */
-    void Input(char message_element)
-    {
-        Input((unsigned char *) &message_element, 1);
-    }
+	void Input(char message_element)
+	{
+		unsigned char ch = static_cast<unsigned char>(message_element);
+		Input(&ch, 1);
+	}
     
     /*  
      *  operator<<
@@ -314,18 +325,18 @@
      *      Each character is assumed to hold 8 bits of information.
      *
      */
-    SHA1& operator<<(const char *message_array)
-    {
-        const char *p = message_array;
-    
-        while(*p)
-        {
-            Input(*p);
-            p++;
-        }
-    
-        return *this;
-    }
+	SHA1& operator<<(const char *message_array)
+	{
+		const char* p = message_array;
+
+		while(*p)
+		{
+			Input(*p);
+			p++;
+		}
+
+		return *this;
+	}
     
     /*  
      *  operator<<
@@ -347,6 +358,8 @@
      */
     SHA1& operator<<(const unsigned char *message_array)
     {
+		assert(0 != message_array);
+
         const unsigned char *p = message_array;
     
         while(*p)
@@ -426,94 +439,100 @@
      *      in the publication.
      *
      */
-    void ProcessMessageBlock()
-    {
-        const unsigned K[] =    {               // Constants defined for SHA-1
-                                    0x5A827999,
-                                    0x6ED9EBA1,
-                                    0x8F1BBCDC,
-                                    0xCA62C1D6
-                                };
-        int         t;                          // Loop counter
-        unsigned    temp;                       // Temporary word value
-        unsigned    W[80];                      // Word sequence
-        unsigned    A, B, C, D, E;              // Word buffers
-    
-        /*
-         *  Initialize the first 16 words in the array W
-         */
-        for(t = 0; t < 16; t++)
-        {
-            W[t] = ((unsigned) Message_Block[t * 4]) << 24;
-            W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
-            W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
-            W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
-        }
-    
-        for(t = 16; t < 80; t++)
-        {
-           W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
-        }
-    
-        A = H[0];
-        B = H[1];
-        C = H[2];
-        D = H[3];
-        E = H[4];
-    
-        for(t = 0; t < 20; t++)
-        {
-            temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
-            temp &= 0xFFFFFFFF;
-            E = D;
-            D = C;
-            C = CircularShift(30,B);
-            B = A;
-            A = temp;
-        }
-    
-        for(t = 20; t < 40; t++)
-        {
-            temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
-            temp &= 0xFFFFFFFF;
-            E = D;
-            D = C;
-            C = CircularShift(30,B);
-            B = A;
-            A = temp;
-        }
-    
-        for(t = 40; t < 60; t++)
-        {
-            temp = CircularShift(5,A) +
-                   ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
-            temp &= 0xFFFFFFFF;
-            E = D;
-            D = C;
-            C = CircularShift(30,B);
-            B = A;
-            A = temp;
-        }
-    
-        for(t = 60; t < 80; t++)
-        {
-            temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
-            temp &= 0xFFFFFFFF;
-            E = D;
-            D = C;
-            C = CircularShift(30,B);
-            B = A;
-            A = temp;
-        }
-    
-        H[0] = (H[0] + A) & 0xFFFFFFFF;
-        H[1] = (H[1] + B) & 0xFFFFFFFF;
-        H[2] = (H[2] + C) & 0xFFFFFFFF;
-        H[3] = (H[3] + D) & 0xFFFFFFFF;
-        H[4] = (H[4] + E) & 0xFFFFFFFF;
-    
-        Message_Block_Index = 0;
-    }
+	void ProcessMessageBlock()
+	{
+		const unsigned K[] =
+		{               // Constants defined for SHA-1
+			0x5A827999,
+			0x6ED9EBA1,
+			0x8F1BBCDC,
+			0xCA62C1D6
+		};
+
+		int t = 0; // Loop counter
+		unsigned int temp = 0; // Temporary word value
+		unsigned int W[80]; // Word sequence
+		unsigned int A = 0; // Word buffers
+		unsigned int B = 0;
+		unsigned int C = 0;
+		unsigned int D = 0;
+		unsigned int E = 0;
+
+		/*
+		*  Initialize the first 16 words in the array W
+		*/
+		for(t = 0; t < 16; t++)
+		{
+			W[t] = ((unsigned) Message_Block[t * 4]) << 24;
+			W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
+			W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
+			W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
+		}
+
+		for(t = 16; t < 80; t++)
+		{
+			W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
+		}
+
+		A = H[0];
+		B = H[1];
+		C = H[2];
+		D = H[3];
+		E = H[4];
+
+		for(t = 0; t < 20; t++)
+		{
+			temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
+			temp &= 0xFFFFFFFF;
+			E = D;
+			D = C;
+			C = CircularShift(30,B);
+			B = A;
+			A = temp;
+		}
+
+		for(t = 20; t < 40; t++)
+		{
+			temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
+			temp &= 0xFFFFFFFF;
+			E = D;
+			D = C;
+			C = CircularShift(30,B);
+			B = A;
+			A = temp;
+		}
+
+		for(t = 40; t < 60; t++)
+		{
+			temp = CircularShift(5,A) +
+				((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
+			temp &= 0xFFFFFFFF;
+			E = D;
+			D = C;
+			C = CircularShift(30,B);
+			B = A;
+			A = temp;
+		}
+
+		for(t = 60; t < 80; t++)
+		{
+			temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
+			temp &= 0xFFFFFFFF;
+			E = D;
+			D = C;
+			C = CircularShift(30,B);
+			B = A;
+			A = temp;
+		}
+
+		H[0] = (H[0] + A) & 0xFFFFFFFF;
+		H[1] = (H[1] + B) & 0xFFFFFFFF;
+		H[2] = (H[2] + C) & 0xFFFFFFFF;
+		H[3] = (H[3] + D) & 0xFFFFFFFF;
+		H[4] = (H[4] + E) & 0xFFFFFFFF;
+
+		Message_Block_Index = 0;
+	}
     
     /*  
      *  PadMessage
@@ -536,54 +555,52 @@
      *  Comments:
      *
      */
-    void PadMessage()
-    {
-        /*
-         *  Check to see if the current message block is too small to hold
-         *  the initial padding bits and length.  If so, we will pad the
-         *  block, process it, and then continue padding into a second block.
-         */
-        if (Message_Block_Index > 55)
-        {
-            Message_Block[Message_Block_Index++] = 0x80;
-            while(Message_Block_Index < 64)
-            {
-                Message_Block[Message_Block_Index++] = 0;
-            }
-    
-            ProcessMessageBlock();
-    
-            while(Message_Block_Index < 56)
-            {
-                Message_Block[Message_Block_Index++] = 0;
-            }
-        }
-        else
-        {
-            Message_Block[Message_Block_Index++] = 0x80;
-            while(Message_Block_Index < 56)
-            {
-                Message_Block[Message_Block_Index++] = 0;
-            }
-    
-        }
-    
-        /*
-         *  Store the message length as the last 8 octets
-         */
-        Message_Block[56] = static_cast<unsigned char>((Length_High >> 24) & 0xFF);
-        Message_Block[57] = static_cast<unsigned char>((Length_High >> 16) & 0xFF);
-        Message_Block[58] = static_cast<unsigned char>((Length_High >> 8) & 0xFF);
-        Message_Block[59] = static_cast<unsigned char>((Length_High) & 0xFF);
-        Message_Block[60] = static_cast<unsigned char>((Length_Low >> 24) & 0xFF);
-        Message_Block[61] = static_cast<unsigned char>((Length_Low >> 16) & 0xFF);
-        Message_Block[62] = static_cast<unsigned char>((Length_Low >> 8) & 0xFF);
-        Message_Block[63] = static_cast<unsigned char>((Length_Low) & 0xFF);
-    
-        ProcessMessageBlock();
-    }
-    
-    
+	void PadMessage()
+	{
+		/*
+		*  Check to see if the current message block is too small to hold
+		*  the initial padding bits and length.  If so, we will pad the
+		*  block, process it, and then continue padding into a second block.
+		*/
+		if (Message_Block_Index > 55)
+		{
+			Message_Block[Message_Block_Index++] = 0x80;
+			while(Message_Block_Index < 64)
+			{
+				Message_Block[Message_Block_Index++] = 0;
+			}
+
+			ProcessMessageBlock();
+
+			while(Message_Block_Index < 56)
+			{
+				Message_Block[Message_Block_Index++] = 0;
+			}
+		}
+		else
+		{
+			Message_Block[Message_Block_Index++] = 0x80;
+			while(Message_Block_Index < 56)
+			{
+				Message_Block[Message_Block_Index++] = 0;
+			}
+		}
+
+		/*
+		*  Store the message length as the last 8 octets
+		*/
+		Message_Block[56] = static_cast<unsigned char>((Length_High >> 24) & 0xFF);
+		Message_Block[57] = static_cast<unsigned char>((Length_High >> 16) & 0xFF);
+		Message_Block[58] = static_cast<unsigned char>((Length_High >> 8) & 0xFF);
+		Message_Block[59] = static_cast<unsigned char>((Length_High) & 0xFF);
+		Message_Block[60] = static_cast<unsigned char>((Length_Low >> 24) & 0xFF);
+		Message_Block[61] = static_cast<unsigned char>((Length_Low >> 16) & 0xFF);
+		Message_Block[62] = static_cast<unsigned char>((Length_Low >> 8) & 0xFF);
+		Message_Block[63] = static_cast<unsigned char>((Length_Low) & 0xFF);
+
+		ProcessMessageBlock();
+	}
+
     /*  
      *  CircularShift
      *
@@ -602,12 +619,13 @@
      *  Comments:
      *
      */
-    unsigned CircularShift(int bits, unsigned word)
-    {
-        return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits));
-    }
+	unsigned CircularShift(int bits, unsigned word)
+	{
+		return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits));
+	}
     
 private:
+
     unsigned H[5];                      // Message digest buffers
 
     unsigned Length_Low;                // Message length in bits

Modified: trunk/include/liblas/guid.hpp
==============================================================================
--- trunk/include/liblas/guid.hpp	(original)
+++ trunk/include/liblas/guid.hpp	Mon Apr 20 11:59:30 2009
@@ -435,7 +435,7 @@
         detail::SHA1 sha1;
         sha1.Input(namespace_guid.data_, namespace_guid.static_size);
         sha1.Input(name, name_length);
-        unsigned int digest[5];
+		unsigned int digest[5] = { 0 };
         
         if (sha1.Result(digest) == false)
         {

Modified: trunk/test/unit/lasfile_test.cpp
==============================================================================
--- trunk/test/unit/lasfile_test.cpp	(original)
+++ trunk/test/unit/lasfile_test.cpp	Mon Apr 20 11:59:30 2009
@@ -10,6 +10,7 @@
 #include <liblas/lasreader.hpp>
 #include <liblas/laswriter.hpp>
 #include <tut/tut.hpp>
+#include <iostream>
 #include <stdexcept>
 #include <string>
 #include "liblas_test.hpp"
@@ -30,7 +31,11 @@
         ~lasfile_data()
         {
             // remove temporary file after each test case
-            std::remove(tmpfile_.c_str());
+            int const ret = std::remove(tmpfile_.c_str());
+			if (0 != ret)
+			{
+				std::cerr << "Failed to remove \'" << tmpfile_ << "\' file\n";
+			}
         }
     };
 


More information about the Liblas-commits mailing list