]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/md5.cc
Apply patch from Sami Liedes <sliedes@cc.hut.fi> to avoid unecessary
[apt.git] / apt-pkg / contrib / md5.cc
index 381c3875392053042381915ec019656672a7a3ac..2bfd70f1b88c97db1bcec38ced081c52742f357b 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: md5.cc,v 1.5 1999/10/25 04:10:02 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 <string.h>
-#include <system.h>
 #include <unistd.h>
 #include <netinet/in.h>                          // For htonl
 #include <inttypes.h>
+#include <config.h>
+#include <system.h>
+
                                                                        /*}}}*/
 
 // byteSwap - Swap bytes in a buffer                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* Swap n 32 bit longs in given buffer */
-inline static void byteSwap(uint32_t *buf, unsigned words)
+#ifdef WORDS_BIGENDIAN
+static void byteSwap(uint32_t *buf, unsigned words)
 {
-   do
+   uint8_t *p = (uint8_t *)buf;
+   
+   do 
    {
-      *buf++ = htonl(*buf);
-   }
-   while (--words);   
+      *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
+        ((unsigned)p[1] << 8 | p[0]);
+      p += 4;
+   } while (--words);
 }
+#else
+#define byteSwap(buf,words)
+#endif
                                                                        /*}}}*/
 // MD5Transform - Alters an existing MD5 hash                          /*{{{*/
 // ---------------------------------------------------------------------
@@ -183,7 +188,7 @@ MD5SumValue::MD5SumValue()
 /* Converts the hex string into a set of chars */
 bool MD5SumValue::Set(string Str)
 {
-   return Hex2Num(Str.begin(),Str.end(),Sum,sizeof(Sum));
+   return Hex2Num(Str,Sum,sizeof(Sum));
 }
                                                                        /*}}}*/
 // MD5SumValue::Value - Convert the number into a string               /*{{{*/
@@ -289,11 +294,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);
    }