Fix tag file parser underrun #139328
[ntk/apt.git] / apt-pkg / tagfile.cc
index 5d11446..ab3647f 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: tagfile.cc,v 1.26 2001/02/20 07:03:17 jgg Exp $
+// $Id: tagfile.cc,v 1.32 2002/03/26 07:22:31 jgg Exp $
 /* ######################################################################
 
    Fast scanner for RFC-822 type header information
     
 #include <string>
 #include <stdio.h>
+#include <ctype.h>
                                                                        /*}}}*/
 
+using std::string;
+
 // TagFile::pkgTagFile - Constructor                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size)
 {
+   if (Fd.IsOpen() == false)
+   {
+      Buffer = 0;
+      Start = End = Buffer = 0;
+      Done = true;
+      iOffset = 0;
+      return;
+   }
+   
    Buffer = new char[Size];
    Start = End = Buffer;
-   Left = Fd.Size();
-   TotalSize = Fd.Size();
+   Done = false;
    iOffset = 0;
    Fill();
 }
@@ -57,13 +68,13 @@ bool pkgTagFile::Step(pkgTagSection &Tag)
         return false;
       
       if (Tag.Scan(Start,End - Start) == false)
-        return _error->Error(_("Unable to parse package file %s (1)"),Fd.Name().c_str());
+        return _error->Error(_("Unable to parse package file %s (1)"),
+                             Fd.Name().c_str());
    }   
    Start += Tag.size();
    iOffset += Tag.size();
 
    Tag.Trim();
-   
    return true;
 }
                                                                        /*}}}*/
@@ -79,7 +90,18 @@ bool pkgTagFile::Fill()
    Start = Buffer;
    End = Buffer + EndSize;
    
-   if (Left == 0)
+   if (Done == false)
+   {
+      // See if only a bit of the file is left
+      unsigned long Actual;
+      if (Fd.Read(End,Size - (End - Buffer),&Actual) == false)
+        return false;
+      if (Actual != Size - (End - Buffer))
+        Done = true;
+      End += Actual;
+   }
+   
+   if (Done == true)
    {
       if (EndSize <= 3)
         return false;
@@ -97,23 +119,6 @@ bool pkgTagFile::Fill()
       return true;
    }
    
-   // See if only a bit of the file is left
-   if (Left < Size - (End - Buffer))
-   {
-      if (Fd.Read(End,Left) == false)
-        return false;
-      
-      End += Left;
-      Left = 0;
-   }
-   else
-   {
-      if (Fd.Read(End,Size - (End - Buffer)) == false)
-        return false;
-      
-      Left -= Size - (End - Buffer);
-      End = Buffer + Size;
-   }   
    return true;
 }
                                                                        /*}}}*/
@@ -134,7 +139,7 @@ bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
 
    // Reposition and reload..
    iOffset = Offset;
-   Left = TotalSize - Offset;
+   Done = false;
    if (Fd.Seek(Offset) == false)
       return false;
    End = Start = Buffer;
@@ -178,7 +183,7 @@ bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
       return false;
    
    TagCount = 0;
-   while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
+   while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
    {
       // Start a new index and add it to the hash
       if (isspace(Stop[0]) == 0)
@@ -192,13 +197,13 @@ bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
       if (Stop == 0)
         return false;
       
-      for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
+      for (;  Stop+1 < End && Stop[1] == '\r'; Stop++);
 
       // Double newline marks the end of the record
       if (Stop+1 < End && Stop[1] == '\n')
       {
         Indexes[TagCount] = Stop - Section;
-        for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
+        for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
         return true;
       }
       
@@ -387,6 +392,7 @@ static const char *iTFRewritePackageOrder[] = {
                           "Filename",
                           "Size",
                           "MD5Sum",
+                          "SHA1Sum",
                            "MSDOS-Filename",   // Obsolete
                           "Description",
                           0};