]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/sha1.cc
merged from debian-sid
[apt.git] / apt-pkg / contrib / sha1.cc
index 1f3927c1cbd7b68e72f93c2c68d7aac66baf1f15..0b1c16dc3af3ae111cc30715500fbdd006a1911b 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                          /*{{{*/
-// $Id: sha1.cc,v 1.1 2001/03/06 05:03:49 jgg Exp $
+// $Id: sha1.cc,v 1.3 2001/05/13 05:15:03 jgg Exp $
 /* ######################################################################
    
    SHA1 - SHA-1 Secure Hash Algorithm.
  */
                                                                        /*}}} */
 // Include Files                                                        /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/sha1.h"
-#endif
-
 #include <apt-pkg/sha1.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/macros.h>
 
 #include <string.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include <config.h>
-#include <system.h>
                                                                        /*}}}*/
 
 // SHA1Transform - Alters an existing SHA-1 hash                       /*{{{*/
@@ -182,67 +178,6 @@ static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
 }
                                                                        /*}}}*/
 
-// SHA1SumValue::SHA1SumValue - Constructs the summation from a string  /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a SHA1 is a 40 character hex number */
-SHA1SumValue::SHA1SumValue(string Str)
-{
-   memset(Sum,0,sizeof(Sum));
-   Set(Str);
-}
-
-                                                                       /*}}} */
-// SHA1SumValue::SHA1SumValue - Default constructor                     /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-SHA1SumValue::SHA1SumValue()
-{
-   memset(Sum,0,sizeof(Sum));
-}
-
-                                                                       /*}}} */
-// SHA1SumValue::Set - Set the sum from a string                        /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool SHA1SumValue::Set(string Str)
-{
-   return Hex2Num(Str.begin(),Str.end(),Sum,sizeof(Sum));
-}
-
-                                                                       /*}}} */
-// SHA1SumValue::Value - Convert the number into a string               /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string SHA1SumValue::Value() const
-{
-   char Conv[16] =
-      { '0','1','2','3','4','5','6','7','8','9','a','b',
-      'c','d','e','f'
-   };
-   char Result[41];
-   Result[40] = 0;
-
-   // Convert each char into two letters
-   int J = 0;
-   int I = 0;
-   for (; I != 40; J++,I += 2)
-   {
-      Result[I] = Conv[Sum[J] >> 4];
-      Result[I + 1] = Conv[Sum[J] & 0xF];
-   }
-
-   return string(Result);
-}
-
-                                                                       /*}}} */
-// SHA1SumValue::operator == - Comparator                               /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool SHA1SumValue::operator == (const SHA1SumValue & rhs) const
-{
-   return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
-                                                                       /*}}}*/
 // SHA1Summation::SHA1Summation - Constructor                           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -269,7 +204,6 @@ SHA1SumValue SHA1Summation::Result()
 {
    uint32_t *state = (uint32_t *)State;
    uint32_t *count = (uint32_t *)Count;
-   uint8_t *buffer = (uint8_t *)Buffer;
    
    // Apply the padding
    if (Done == false)
@@ -295,11 +229,13 @@ SHA1SumValue SHA1Summation::Result()
 
    // Transfer over the result
    SHA1SumValue Value;
+   char res[20];
    for (unsigned i = 0; i < 20; i++)
    {
-      Value.Sum[i] = (unsigned char)
+      res[i] = (unsigned char)
         ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
    }
+   Value.Set(res);
    return Value;
 }
                                                                        /*}}}*/
@@ -344,11 +280,16 @@ bool SHA1Summation::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)))
+      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);
    }