merge stuff from donkult
[ntk/apt.git] / ftparchive / cachedb.cc
index 7d8718c..c2318bf 100644 (file)
 #include <apt-pkg/sha2.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/configuration.h>
-    
+#include <apt-pkg/fileutl.h>
+
 #include <netinet/in.h>       // htonl, etc
 
-#include <apti18n.h>
 #include "cachedb.h"
+
+#include <apti18n.h>
                                                                        /*}}}*/
 
 // CacheDB::ReadyDB - Ready the DB2                                    /*{{{*/
@@ -299,11 +301,15 @@ bool CacheDB::LoadContents(bool const &GenOnly)
                                                                        /*}}}*/
 
 static std::string bytes2hex(uint8_t *bytes, size_t length) {
-   char space[65];
-   if (length * 2 > sizeof(space) - 1) length = (sizeof(space) - 1) / 2;
-   for (size_t i = 0; i < length; i++)
-      snprintf(&space[i*2], 3, "%02x", bytes[i]);
-   return std::string(space);
+   char buf[3];
+   std::string space;
+
+   space.reserve(length*2 + 1);
+   for (size_t i = 0; i < length; i++) {
+      snprintf(buf, sizeof(buf), "%02x", bytes[i]);
+      space.append(buf);
+   }
+   return space;
 }
 
 static inline unsigned char xdig2num(char const &dig) {
@@ -346,7 +352,7 @@ bool CacheDB::GetMD5(bool const &GenOnly)
       return false;
    }
    MD5Summation MD5;
-   if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    MD5Res = MD5.Result();
@@ -377,7 +383,7 @@ bool CacheDB::GetSHA1(bool const &GenOnly)
       return false;
    }
    SHA1Summation SHA1;
-   if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    SHA1Res = SHA1.Result();
@@ -408,7 +414,7 @@ bool CacheDB::GetSHA256(bool const &GenOnly)
       return false;
    }
    SHA256Summation SHA256;
-   if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    SHA256Res = SHA256.Result();
@@ -439,7 +445,7 @@ bool CacheDB::GetSHA512(bool const &GenOnly)
       return false;
    }
    SHA512Summation SHA512;
-   if (Fd->Seek(0) == false || SHA512.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    SHA512Res = SHA512.Result();