X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/7bb525cb8a81e77923213a83039659ca375c4239..aac2e51078714d5130861035e9b3b5d4d6b84471:/apt-pkg/tagfile.cc?ds=inline diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 1e5bc81a4..79ff18de4 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: tagfile.cc,v 1.36 2003/04/27 05:59:14 doogie Exp $ +// $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $ /* ###################################################################### Fast scanner for RFC-822 type header information @@ -31,23 +31,24 @@ using std::string; // TagFile::pkgTagFile - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ -pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size) +pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : + Fd(*pFd), + Size(Size) { - if (Fd.IsOpen() == false) + if (Fd.IsOpen() == false || Fd.Size() == 0) { Buffer = 0; Start = End = Buffer = 0; - Done = true; iOffset = 0; + Map = NULL; return; } - CurSize = Size; - Buffer = new char[Size]; - Start = End = Buffer; - Done = false; + Map = new MMap (Fd, MMap::Public | MMap::ReadOnly); + Buffer = (char *) Map->Data (); + Start = Buffer; + End = Buffer + Map->Size (); iOffset = 0; - Fill(); } /*}}}*/ // TagFile::~pkgTagFile - Destructor /*{{{*/ @@ -55,7 +56,7 @@ pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size) /* */ pkgTagFile::~pkgTagFile() { - delete [] Buffer; + delete Map; } /*}}}*/ // TagFile::Step - Advance to the next section /*{{{*/ @@ -63,71 +64,18 @@ pkgTagFile::~pkgTagFile() /* If the Section Scanner fails we refill the buffer and try again. */ bool pkgTagFile::Step(pkgTagSection &Tag) { - pkgTagSection::ScanFlags ret = Tag.Scan(Start,End - Start); - if (ret == pkgTagSection::ScanEOF) { - CurSize <<= 1; - if (Fill() == false) - return false; - do { - ret = Tag.Scan(Start,End - Start); - if (ret == pkgTagSection::ScanEOF) { - CurSize <<= 1; - if (Fill() == false) - break; - } - } while (ret == pkgTagSection::ScanEOF); - } - if (ret != pkgTagSection::ScanSuccess) + if (Start == End) + return false; + + if (Tag.Scan(Start,End - Start) == false) + { return _error->Error(_("Unable to parse package file %s (1)"), - Fd.Name().c_str()); + Fd.Name().c_str()); + } Start += Tag.size(); iOffset += Tag.size(); Tag.Trim(); - return true; -} - /*}}}*/ -// TagFile::Fill - Top up the buffer /*{{{*/ -// --------------------------------------------------------------------- -/* This takes the bit at the end of the buffer and puts it at the start - then fills the rest from the file */ -bool pkgTagFile::Fill() -{ - unsigned long EndSize = End - Start; - unsigned long Actual = 0; - - memmove(Buffer,Start,EndSize); - Start = Buffer; - End = Buffer + EndSize; - - if (Done == false) - { - // See if only a bit of the file is left - 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 && Actual == 0) - return false; - if (Size - (End - Buffer) < 4) - return true; - - // Append a double new line if one does not exist - unsigned int LineCount = 0; - for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--) - if (*E == '\n') - LineCount++; - for (; LineCount < 2; LineCount++) - *End++ = '\n'; - - return true; - } - return true; } /*}}}*/ @@ -148,29 +96,10 @@ bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset) // Reposition and reload.. iOffset = Offset; - Done = false; - if (Fd.Seek(Offset) == false) - return false; - End = Start = Buffer; + Start = Buffer + iOffset; - if (Fill() == false) - return false; - - pkgTagSection::ScanFlags ret = Tag.Scan(Start,End - Start); - if (ret == pkgTagSection::ScanEOF) { - CurSize <<= 1; - if (Fill() == false) - return false; - do { - ret = Tag.Scan(Start,End - Start); - if (ret == pkgTagSection::ScanEOF) { - CurSize <<= 1; - if (Fill() == false) - break; - } - } while (ret == pkgTagSection::ScanEOF); - } - if (ret != pkgTagSection::ScanSuccess) + // Start != End is a special case to not fail on empty TagFiles + if (Start != End && Tag.Scan(Start,End - Start) == false) return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str()); return true; @@ -189,14 +118,14 @@ inline static unsigned long AlphaHash(const char *Text, const char *End = 0) return Res & 0xFF; } -enum pkgTagSection::ScanFlags pkgTagSection::Scan(const char *Start,unsigned long MaxLength) +bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) { const char *End = Start + MaxLength; Stop = Section = Start; memset(AlphaIndexes,0,sizeof(AlphaIndexes)); - if (Stop == 0) - return ScanError; + if (Stop == 0 || MaxLength == 0) + return false; TagCount = 0; while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End) @@ -211,7 +140,7 @@ enum pkgTagSection::ScanFlags pkgTagSection::Scan(const char *Start,unsigned lon Stop = (const char *)memchr(Stop,'\n',End - Stop); if (Stop == 0) - return ScanEOF; + return false; for (; Stop+1 < End && Stop[1] == '\r'; Stop++); @@ -220,13 +149,19 @@ enum pkgTagSection::ScanFlags pkgTagSection::Scan(const char *Start,unsigned lon { Indexes[TagCount] = Stop - Section; for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++); - return ScanSuccess; + return true; } Stop++; } - return ScanEOF; + if ((Stop+1 >= End) && (End[-1] == '\n' || End[-1] == '\r')) + { + Indexes[TagCount] = (End - 1) - Section; + return true; + } + + return false; } /*}}}*/ // TagSection::Trim - Trim off any trailing garbage /*{{{*/