From 63b1700fd93787682eedc95546a0364f449f036d Mon Sep 17 00:00:00 2001 From: Arch Librarian Date: Mon, 20 Sep 2004 16:57:09 +0000 Subject: More SHA-1 prep Author: jgg Date: 2001-03-06 07:15:29 GMT More SHA-1 prep --- apt-pkg/contrib/hashes.cc | 43 ++++++++++++++++++++++++++++ apt-pkg/contrib/hashes.h | 41 ++++++++++++++++++++++++++ apt-pkg/init.h | 6 ++-- apt-pkg/makefile | 9 +++--- debian/changelog | 1 + methods/ftp.cc | 18 ++++++------ methods/ftp.h | 6 ++-- methods/gzip.cc | 73 +++++++++++++++++++++++++++++++++++------------ methods/http.cc | 26 ++++++++--------- methods/http.h | 6 ++-- methods/rsh.cc | 14 ++++----- methods/rsh.h | 8 +++--- 12 files changed, 187 insertions(+), 64 deletions(-) create mode 100644 apt-pkg/contrib/hashes.cc create mode 100644 apt-pkg/contrib/hashes.h diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc new file mode 100644 index 000000000..af741b672 --- /dev/null +++ b/apt-pkg/contrib/hashes.cc @@ -0,0 +1,43 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $ +/* ###################################################################### + + Hashes - Simple wrapper around the hash functions + + This is just used to make building the methods simpler, this is the + only interface required.. + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#ifdef __GNUG__ +#pragma implementation "apt-pkg/hashes.h" +#endif + +#include + +#include +#include + /*}}}*/ + +// Hashes::AddFD - Add the contents of the FD /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool Hashes::AddFD(int Fd,unsigned long Size) +{ + unsigned char Buf[64*64]; + int Res = 0; + while (Size != 0) + { + Res = read(Fd,Buf,MIN(Size,sizeof(Buf))); + if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf))) + return false; + Size -= Res; + MD5.Add(Buf,Res); + SHA1.Add(Buf,Res); + } + return true; +} + /*}}}*/ + diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h new file mode 100644 index 000000000..e00eaeb2d --- /dev/null +++ b/apt-pkg/contrib/hashes.h @@ -0,0 +1,41 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: hashes.h,v 1.1 2001/03/06 07:15:29 jgg Exp $ +/* ###################################################################### + + Hashes - Simple wrapper around the hash functions + + This is just used to make building the methods simpler, this is the + only interface required.. + + ##################################################################### */ + /*}}}*/ +#ifndef APTPKG_HASHES_H +#define APTPKG_HASHES_H + +#ifdef __GNUG__ +#pragma interface "apt-pkg/hashesh.h" +#endif + +#include +#include + +class Hashes +{ + public: + + MD5Summation MD5; + SHA1Summation SHA1; + + inline bool Add(const unsigned char *Data,unsigned long Size) + { + MD5.Add(Data,Size); + SHA1.Add(Data,Size); + }; + inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; + bool AddFD(int Fd,unsigned long Size); + inline bool Add(const unsigned char *Beg,const unsigned char *End) + {return Add(Beg,End-Beg);}; +}; + +#endif diff --git a/apt-pkg/init.h b/apt-pkg/init.h index d6beb0532..998c073ea 100644 --- a/apt-pkg/init.h +++ b/apt-pkg/init.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: init.h,v 1.6 2001/03/03 23:02:39 tausq Exp $ +// $Id: init.h,v 1.7 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### Init - Initialize the package library @@ -19,8 +19,8 @@ // See the makefile #define APT_PKG_MAJOR 3 #define APT_PKG_MINOR 1 -#define APT_PKG_RELEASE 2 - +#define APT_PKG_RELEASE 3 + extern const char *pkgVersion; extern const char *pkgLibVersion; extern const char *pkgOS; diff --git a/apt-pkg/makefile b/apt-pkg/makefile index 4bc818155..9099cc12c 100644 --- a/apt-pkg/makefile +++ b/apt-pkg/makefile @@ -13,16 +13,17 @@ include ../buildlib/defaults.mak LIBRARY=apt-pkg LIBEXT=$(GLIBC_VER)$(LIBSTDCPP_VER) MAJOR=3.1 -MINOR=2 +MINOR=3 SLIBS=$(PTHREADLIB) # Source code for the contributed non-core things SOURCE = contrib/mmap.cc contrib/error.cc contrib/strutl.cc \ contrib/configuration.cc contrib/progress.cc contrib/cmndline.cc \ - contrib/md5.cc contrib/cdromutl.cc contrib/crc-16.cc \ - contrib/fileutl.cc contrib/sha1.cc + contrib/md5.cc contrib/sha1.cc contrib/hashes.cc \ + contrib/cdromutl.cc contrib/crc-16.cc \ + contrib/fileutl.cc HEADERS = mmap.h error.h configuration.h fileutl.h cmndline.h \ - md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h + md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h hashes.h # Source code for the core main library SOURCE+= pkgcache.cc version.cc depcache.cc \ diff --git a/debian/changelog b/debian/changelog index 59c9ec48f..c32f6bf86 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,6 +2,7 @@ apt (0.5.3) unstable; urgency=low * JoeyH's dpkg::preconfig not working. Closes: #88675 * Fixed apt override disparity + * Alfredo's SHA-1 and related patches -- Jason Gunthorpe Sun, 4 Mar 2001 15:39:43 -0700 diff --git a/methods/ftp.cc b/methods/ftp.cc index 524f799c3..edd150699 100644 --- a/methods/ftp.cc +++ b/methods/ftp.cc @@ -1,9 +1,9 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: ftp.cc,v 1.23 2001/02/23 07:19:49 jgg Exp $ +// $Id: ftp.cc,v 1.24 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### - HTTP Aquire Method - This is the FTP aquire method for APT. + FTP Aquire Method - This is the FTP aquire method for APT. This is a very simple implementation that does not try to optimize at all. Commands are sent syncronously with the FTP server (as the @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include @@ -825,7 +825,7 @@ bool FTPConn::Finalize() /* This opens a data connection, sends REST and RETR and then transfers the file over. */ bool FTPConn::Get(const char *Path,FileFd &To,unsigned long Resume, - MD5Summation &MD5,bool &Missing) + Hashes &Hash,bool &Missing) { Missing = false; if (CreateDataFd() == false) @@ -849,7 +849,7 @@ bool FTPConn::Get(const char *Path,FileFd &To,unsigned long Resume, if (Resume != 0) { - if (MD5.AddFD(To.Fd(),Resume) == false) + if (Hash.AddFD(To.Fd(),Resume) == false) { _error->Errno("read","Problem hashing file"); return false; @@ -893,7 +893,7 @@ bool FTPConn::Get(const char *Path,FileFd &To,unsigned long Resume, break; } - MD5.Add(Buffer,Res); + Hash.Add(Buffer,Res); if (To.Write(Buffer,Res) == false) { Close(); @@ -1022,7 +1022,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) } // Open the file - MD5Summation MD5; + Hashes Hash; { FileFd Fd(Itm->DestFile,FileFd::WriteAny); if (_error->PendingError() == true) @@ -1035,7 +1035,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) FailFd = Fd.Fd(); bool Missing; - if (Server->Get(File,Fd,Res.ResumePoint,MD5,Missing) == false) + if (Server->Get(File,Fd,Res.ResumePoint,Hash,Missing) == false) { Fd.Close(); @@ -1056,7 +1056,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) } Res.LastModified = FailTime; - Res.MD5Sum = MD5.Result(); + Res.MD5Sum = Hash.MD5.Result(); // Timestamp struct utimbuf UBuf; diff --git a/methods/ftp.h b/methods/ftp.h index f791195b3..1bcea41b6 100644 --- a/methods/ftp.h +++ b/methods/ftp.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- -// Description /*{{{*/// $Id: ftp.h,v 1.3 2001/02/20 07:03:18 jgg Exp $ -// $Id: ftp.h,v 1.3 2001/02/20 07:03:18 jgg Exp $ +// Description /*{{{*/// $Id: ftp.h,v 1.4 2001/03/06 07:15:29 jgg Exp $ +// $Id: ftp.h,v 1.4 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### FTP Aquire Method - This is the FTP aquire method for APT. @@ -56,7 +56,7 @@ class FTPConn bool Size(const char *Path,unsigned long &Size); bool ModTime(const char *Path, time_t &Time); bool Get(const char *Path,FileFd &To,unsigned long Resume, - MD5Summation &MD5,bool &Missing); + Hashes &MD5,bool &Missing); FTPConn(URI Srv); ~FTPConn(); diff --git a/methods/gzip.cc b/methods/gzip.cc index f58a7263d..4a52c7a0c 100644 --- a/methods/gzip.cc +++ b/methods/gzip.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: gzip.cc,v 1.11 2001/03/06 03:11:22 jgg Exp $ +// $Id: gzip.cc,v 1.12 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### GZip method - Take a file URI in and decompress it into the target @@ -13,11 +13,13 @@ #include #include #include +#include #include #include #include #include +#include /*}}}*/ class GzipMethod : public pkgAcqMethod @@ -29,6 +31,7 @@ class GzipMethod : public pkgAcqMethod GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {}; }; + // GzipMethod::Fetch - Decompress the passed URI /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -36,45 +39,74 @@ bool GzipMethod::Fetch(FetchItem *Itm) { URI Get = Itm->Uri; string Path = Get.Host + Get.Path; // To account for relative paths - string GzipPath = _config->Find("Dir::bin::gzip","gzip"); FetchResult Res; Res.Filename = Itm->DestFile; URIStart(Res); - // Open the source and destintation files + // Open the source and destination files FileFd From(Path,FileFd::ReadOnly); - FileFd To(Itm->DestFile,FileFd::WriteEmpty); - To.EraseOnFailure(); - if (_error->PendingError() == true) - return false; - + + int GzOut[2]; + if (pipe(GzOut) < 0) + return _error->Errno("pipe","Couldn't open pipe for gzip"); + // Fork gzip - int Process = fork(); - if (Process < 0) - return _error->Errno("fork",string("Couldn't fork "+GzipPath).c_str()); - - // The child + int Process = ExecFork(); if (Process == 0) { + close(GzOut[0]); dup2(From.Fd(),STDIN_FILENO); - dup2(To.Fd(),STDOUT_FILENO); + dup2(GzOut[1],STDOUT_FILENO); From.Close(); - To.Close(); + close(GzOut[1]); SetCloseExec(STDIN_FILENO,false); SetCloseExec(STDOUT_FILENO,false); const char *Args[3]; - Args[0] = GzipPath.c_str(); + Args[0] = _config->Find("Dir::bin::gzip","gzip").c_str(); Args[1] = "-d"; Args[2] = 0; execvp(Args[0],(char **)Args); - exit(100); + _exit(100); } From.Close(); + close(GzOut[1]); + + FileFd FromGz(GzOut[0]); // For autoclose + FileFd To(Itm->DestFile,FileFd::WriteEmpty); + To.EraseOnFailure(); + if (_error->PendingError() == true) + return false; + + // Read data from gzip, generate checksums and write + Hashes Hash; + bool Failed = false; + while (1) + { + unsigned char Buffer[4*1024]; + unsigned long Count; + + Count = read(GzOut[0],Buffer,sizeof(Buffer)); + if (Count < 0 && errno == EINTR) + continue; + + if (Count < 0) + { + _error->Errno("read", "Read error from gzip process"); + Failed = true; + break; + } + + if (Count == 0) + break; + + Hash.Add(Buffer,Count); + To.Write(Buffer,Count); + } // Wait for gzip to finish - if (ExecWait(Process,GzipPath.c_str(),false) == false) + if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false) { To.OpFail(); return false; @@ -82,6 +114,9 @@ bool GzipMethod::Fetch(FetchItem *Itm) To.Close(); + if (Failed == true) + return false; + // Transfer the modification times struct stat Buf; if (stat(Path.c_str(),&Buf) != 0) @@ -99,6 +134,8 @@ bool GzipMethod::Fetch(FetchItem *Itm) // Return a Done response Res.LastModified = Buf.st_mtime; Res.Size = Buf.st_size; + Res.MD5Sum = Hash.MD5.Result(); + URIDone(Res); return true; diff --git a/methods/http.cc b/methods/http.cc index c1c5d8a4a..0c520b33c 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: http.cc,v 1.49 2001/02/23 07:19:49 jgg Exp $ +// $Id: http.cc,v 1.50 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### HTTP Aquire Method - This is the HTTP aquire method for APT. @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include @@ -57,7 +57,7 @@ bool Debug = false; // CircleBuf::CircleBuf - Circular input buffer /*{{{*/ // --------------------------------------------------------------------- /* */ -CircleBuf::CircleBuf(unsigned long Size) : Size(Size), MD5(0) +CircleBuf::CircleBuf(unsigned long Size) : Size(Size), Hash(0) { Buf = new unsigned char[Size]; Reset(); @@ -73,10 +73,10 @@ void CircleBuf::Reset() StrPos = 0; MaxGet = (unsigned int)-1; OutQueue = string(); - if (MD5 != 0) + if (Hash != 0) { - delete MD5; - MD5 = new MD5Summation; + delete Hash; + Hash = new Hashes; } }; /*}}}*/ @@ -182,8 +182,8 @@ bool CircleBuf::Write(int Fd) return false; } - if (MD5 != 0) - MD5->Add(Buf + (OutP%Size),Res); + if (Hash != 0) + Hash->Add(Buf + (OutP%Size),Res); OutP += Res; } @@ -892,14 +892,14 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv) // Set the start point lseek(File->Fd(),0,SEEK_END); - delete Srv->In.MD5; - Srv->In.MD5 = new MD5Summation; + delete Srv->In.Hash; + Srv->In.Hash = new Hashes; - // Fill the MD5 Hash if the file is non-empty (resume) + // Fill the Hash if the file is non-empty (resume) if (Srv->StartPos > 0) { lseek(File->Fd(),0,SEEK_SET); - if (Srv->In.MD5->AddFD(File->Fd(),Srv->StartPos) == false) + if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false) { _error->Errno("read","Problem hashing file"); return 5; @@ -1109,7 +1109,7 @@ int HttpMethod::Loop() // Send status to APT if (Result == true) { - Res.MD5Sum = Server->In.MD5->Result(); + Res.MD5Sum = Server->In.Hash->MD5.Result(); URIDone(Res); } else diff --git a/methods/http.h b/methods/http.h index 2569c2921..07a65d702 100644 --- a/methods/http.h +++ b/methods/http.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- -// Description /*{{{*/// $Id: http.h,v 1.9 2001/02/20 07:03:18 jgg Exp $ -// $Id: http.h,v 1.9 2001/02/20 07:03:18 jgg Exp $ +// Description /*{{{*/// $Id: http.h,v 1.10 2001/03/06 07:15:29 jgg Exp $ +// $Id: http.h,v 1.10 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### HTTP Aquire Method - This is the HTTP aquire method for APT. @@ -46,7 +46,7 @@ class CircleBuf public: - MD5Summation *MD5; + Hashes *Hash; // Read data in bool Read(int Fd); diff --git a/methods/rsh.cc b/methods/rsh.cc index 9e521edec..5326e5098 100644 --- a/methods/rsh.cc +++ b/methods/rsh.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: rsh.cc,v 1.2 2001/02/20 07:03:18 jgg Exp $ +// $Id: rsh.cc,v 1.3 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### RSH method - Transfer files via rsh compatible program @@ -271,7 +271,7 @@ bool RSHConn::ModTime(const char *Path, time_t &Time) // --------------------------------------------------------------------- /* */ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume, - MD5Summation &MD5,bool &Missing, unsigned long Size) + Hashes &Hash,bool &Missing, unsigned long Size) { Missing = false; @@ -284,7 +284,7 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume, return false; if (Resume != 0) { - if (MD5.AddFD(To.Fd(),Resume) == false) { + if (Hash.AddFD(To.Fd(),Resume) == false) { _error->Errno("read","Problem hashing file"); return false; } @@ -323,7 +323,7 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume, } MyLen += Res; - MD5.Add(Buffer,Res); + Hash.Add(Buffer,Res); if (To.Write(Buffer,Res) == false) { Close(); @@ -428,7 +428,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) } // Open the file - MD5Summation MD5; + Hashes Hash; { FileFd Fd(Itm->DestFile,FileFd::WriteAny); if (_error->PendingError() == true) @@ -441,7 +441,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) FailFd = Fd.Fd(); bool Missing; - if (Server->Get(File,Fd,Res.ResumePoint,MD5,Missing,Res.Size) == false) + if (Server->Get(File,Fd,Res.ResumePoint,Hash,Missing,Res.Size) == false) { Fd.Close(); @@ -462,7 +462,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) } Res.LastModified = FailTime; - Res.MD5Sum = MD5.Result(); + Res.MD5Sum = Hash.MD5.Result(); // Timestamp struct utimbuf UBuf; diff --git a/methods/rsh.h b/methods/rsh.h index c3f3258e9..1b3bcaea4 100644 --- a/methods/rsh.h +++ b/methods/rsh.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- -// Description /*{{{*/// $Id: rsh.h,v 1.2 2001/02/20 07:03:18 jgg Exp $ -// $Id: rsh.h,v 1.2 2001/02/20 07:03:18 jgg Exp $ +// Description /*{{{*/// $Id: rsh.h,v 1.3 2001/03/06 07:15:29 jgg Exp $ +// $Id: rsh.h,v 1.3 2001/03/06 07:15:29 jgg Exp $ /* ###################################################################### RSH method - Transfer files via rsh compatible program @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include @@ -44,7 +44,7 @@ class RSHConn bool Size(const char *Path,unsigned long &Size); bool ModTime(const char *Path, time_t &Time); bool Get(const char *Path,FileFd &To,unsigned long Resume, - MD5Summation &MD5,bool &Missing, unsigned long Size); + Hashes &Hash,bool &Missing, unsigned long Size); RSHConn(URI Srv); ~RSHConn(); -- cgit v1.2.3