]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/md5.cc
apt-pkg/contrib/md5.cc: fix md5sum by using the right type (unsinged char*) and avoid...
[apt.git] / apt-pkg / contrib / md5.cc
index 2beaa120405f8f05c3f4738b3666205fd421e027..6820d39516c6c0c44cb0205375c524f82cb11cd7 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: md5.cc,v 1.6 1999/10/31 06:32:28 jgg Exp $
+// $Id: md5.cc,v 1.12 2001/05/13 05:15:03 jgg Exp $
 /* ######################################################################
    
    MD5Sum - MD5 Message Digest Algorithm.
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/md5.h"
-#endif
-
 #include <apt-pkg/md5.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/macros.h>
 
 #include <string.h>
-#include <system.h>
 #include <unistd.h>
 #include <netinet/in.h>                          // For htonl
 #include <inttypes.h>
+#include <config.h>
                                                                        /*}}}*/
 
 // byteSwap - Swap bytes in a buffer                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* Swap n 32 bit longs in given buffer */
 #ifdef WORDS_BIGENDIAN
-static void byteSwap(uint8_t *buf, unsigned words)
+static void byteSwap(uint32_t *buf, unsigned words)
 {
    uint8_t *p = (uint8_t *)buf;
    
    do 
    {
-      *buf++ = (UINT32)((unsigned)p[3] << 8 | p[2]) << 16 |
+      *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
         ((unsigned)p[1] << 8 | p[0]);
       p += 4;
    } while (--words);
@@ -168,61 +165,6 @@ static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
    buf[3] += d;
 }
                                                                        /*}}}*/
-// MD5SumValue::MD5SumValue - Constructs the summation from a string   /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a MD5 is a 32 character hex number */
-MD5SumValue::MD5SumValue(string Str)
-{
-   memset(Sum,0,sizeof(Sum));
-   Set(Str);
-}
-                                                                       /*}}}*/
-// MD5SumValue::MD5SumValue - Default constructor                      /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-MD5SumValue::MD5SumValue()
-{
-   memset(Sum,0,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// MD5SumValue::Set - Set the sum from a string                                /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool MD5SumValue::Set(string Str)
-{
-   return Hex2Num(Str.begin(),Str.end(),Sum,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// MD5SumValue::Value - Convert the number into a string               /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string MD5SumValue::Value() const
-{
-   char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
-                    'c','d','e','f'};
-   char Result[33];
-   Result[32] = 0;
-   
-   // Convert each char into two letters
-   int J = 0;
-   int I = 0;
-   for (; I != 32; J++, I += 2)
-   {
-      Result[I] = Conv[Sum[J] >> 4];
-      Result[I + 1] = Conv[Sum[J] & 0xF];
-   } 
-
-   return string(Result);
-}
-                                                                       /*}}}*/
-// MD5SumValue::operator == - Comparitor                               /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
-{
-   return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
-                                                                       /*}}}*/
 // MD5Summation::MD5Summation - Initialize the summer                  /*{{{*/
 // ---------------------------------------------------------------------
 /* This assigns the deep magic initial values */
@@ -296,11 +238,16 @@ bool MD5Summation::AddFD(int Fd,unsigned long Size)
 {
    unsigned char Buf[64*64];
    int Res = 0;
-   while (Size != 0)
+   int ToEOF = (Size == 0);
+   while (Size != 0 || ToEOF)
    {
-      Res = read(Fd,Buf,MIN(Size,sizeof(Buf)));
-      if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf)))
-        return false;
+      unsigned n = sizeof(Buf);
+      if (!ToEOF) n = min(Size,(unsigned long)n);
+      Res = read(Fd,Buf,n);
+      if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
+         return false;
+      if (ToEOF && Res == 0) // EOF
+         break;
       Size -= Res;
       Add(Buf,Res);
    }
@@ -351,7 +298,7 @@ MD5SumValue MD5Summation::Result()
    }
    
    MD5SumValue V;
-   memcpy(V.Sum,buf,16);
+   V.Set((unsigned char *)buf);
    return V;
 }
                                                                        /*}}}*/