convert a few for-loop char finds to proper strchr and memchr
[ntk/apt.git] / apt-pkg / deb / deblistparser.cc
index 2cfeb23..0562be4 100644 (file)
@@ -67,23 +67,12 @@ string debListParser::Package() {
                                                                        /*}}}*/
 // ListParser::Architecture - Return the package arch                  /*{{{*/
 // ---------------------------------------------------------------------
-/* This will return the Architecture of the package this section describes
-   Note that architecture "all" packages will get the architecture of the
-   Packages file parsed here. */
+/* This will return the Architecture of the package this section describes */
 string debListParser::Architecture() {
-   string const Result = Section.FindS("Architecture");
-   if (Result.empty() == true || Result == "all")
-   {
-      if (Arch.empty() == true)
-        /* FIXME: this is a problem for installed arch all
-           packages as we don't know from which arch this
-           package was installed - and therefore which
-           dependency this package resolves. */
-        return _config->Find("APT::Architecture");
-      else
-        return Arch;
-   }
-   return Result;
+   std::string const Arch = Section.FindS("Architecture");
+   if (Arch.empty() == true)
+      return _config->Find("APT::Architecture");
+   return Arch;
 }
                                                                        /*}}}*/
 // ListParser::ArchitectureAll                                         /*{{{*/
@@ -112,29 +101,40 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
    Ver->Section = UniqFindTagWrite("Section");
 
    // Parse multi-arch
-   if (Section.FindS("Architecture") == "all")
-      /* Arch all packages can't have a Multi-Arch field,
-         but we need a special treatment for them nonetheless */
-      Ver->MultiArch = pkgCache::Version::All;
-   else
-   {
-      string const MultiArch = Section.FindS("Multi-Arch");
-      if (MultiArch.empty() == true)
-        Ver->MultiArch = pkgCache::Version::None;
-      else if (MultiArch == "same")
-        Ver->MultiArch = pkgCache::Version::Same;
-      else if (MultiArch == "foreign")
-        Ver->MultiArch = pkgCache::Version::Foreign;
-      else if (MultiArch == "allowed")
-        Ver->MultiArch = pkgCache::Version::Allowed;
-      else
+   string const MultiArch = Section.FindS("Multi-Arch");
+   if (MultiArch.empty() == true)
+      Ver->MultiArch = pkgCache::Version::None;
+   else if (MultiArch == "same") {
+      // Parse multi-arch
+      if (ArchitectureAll() == true)
       {
-        _error->Warning("Unknown Multi-Arch type »%s« for package »%s«",
-                       MultiArch.c_str(), Section.FindS("Package").c_str());
+        /* Arch all packages can't be Multi-Arch: same */
+        _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
+                       Section.FindS("Package").c_str());
         Ver->MultiArch = pkgCache::Version::None;
       }
+      else
+        Ver->MultiArch = pkgCache::Version::Same;
+   }
+   else if (MultiArch == "foreign")
+      Ver->MultiArch = pkgCache::Version::Foreign;
+   else if (MultiArch == "allowed")
+      Ver->MultiArch = pkgCache::Version::Allowed;
+   else
+   {
+      _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
+                       MultiArch.c_str(), Section.FindS("Package").c_str());
+      Ver->MultiArch = pkgCache::Version::None;
    }
 
+   if (ArchitectureAll() == true)
+      switch (Ver->MultiArch)
+      {
+        case pkgCache::Version::Foreign: Ver->MultiArch = pkgCache::Version::AllForeign; break;
+        case pkgCache::Version::Allowed: Ver->MultiArch = pkgCache::Version::AllAllowed; break;
+        default: Ver->MultiArch = pkgCache::Version::All;
+      }
+
    // Archive Size
    Ver->Size = Section.FindULL("Size");
    // Unpacked Size (in K)
@@ -150,24 +150,6 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
         Ver->Priority = pkgCache::State::Extra;
    }
 
-   if (Ver->MultiArch == pkgCache::Version::All)
-   {
-      /* We maintain a "pseudo" arch=all package for architecture all versions
-        on which these versions can depend on. This pseudo package is many used
-        for downloading/installing: The other pseudo-packages will degenerate
-        to a NOP in the download/install step - this package will ensure that
-        it is downloaded only one time and installed only one time -- even if
-        the architecture bound versions coming in and out on regular basis. */
-      if (strcmp(Ver.Arch(true),"all") == 0)
-        return true;
-      else if (MultiArchEnabled == true)
-      {
-        // our pseudo packages have no size to not confuse the fetcher
-        Ver->Size = 0;
-        Ver->InstalledSize = 0;
-      }
-   }
-
    if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
       return false;
    if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
@@ -219,9 +201,9 @@ string debListParser::DescriptionLanguage()
    if (Section.FindS("Description").empty() == false)
       return "";
 
-   std::vector<string> const lang = APT::Configuration::getLanguages();
+   std::vector<string> const lang = APT::Configuration::getLanguages(true);
    for (std::vector<string>::const_iterator l = lang.begin();
-       l != lang.end(); l++)
+       l != lang.end(); ++l)
       if (Section.FindS(string("Description-").append(*l).c_str()).empty() == false)
         return *l;
 
@@ -302,18 +284,18 @@ unsigned short debListParser::VersionHash()
       /* Strip out any spaces from the text, this undoes dpkgs reformatting
          of certain fields. dpkg also has the rather interesting notion of
          reformatting depends operators < -> <= */
-      char *I = S;
+      char *J = S;
       for (; Start != End; Start++)
       {
         if (isspace(*Start) == 0)
-           *I++ = tolower_ascii(*Start);
+           *J++ = tolower_ascii(*Start);
         if (*Start == '<' && Start[1] != '<' && Start[1] != '=')
-           *I++ = '=';
+           *J++ = '=';
         if (*Start == '>' && Start[1] != '>' && Start[1] != '=')
-           *I++ = '=';
+           *J++ = '=';
       }
 
-      Result = AddCRC16(Result,S,I - S);
+      Result = AddCRC16(Result,S,J - S);
    }
    
    return Result;
@@ -479,8 +461,9 @@ const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
  *
  * The complete architecture, consisting of <kernel>-<cpu>.
  */
-static string CompleteArch(std::stringarch) {
+static string CompleteArch(std::string const &arch) {
     if (arch == "armel")              return "linux-arm";
+    if (arch == "armhf")              return "linux-arm";
     if (arch == "lpia")               return "linux-i386";
     if (arch == "powerpcspe")         return "linux-powerpc";
     if (arch == "uclibc-linux-armel") return "linux-arm";
@@ -504,7 +487,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
    // Parse off the package name
    const char *I = Start;
    for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
-       *I != ',' && *I != '|'; I++);
+       *I != ',' && *I != '|' && *I != '[' && *I != ']'; I++);
    
    // Malformed, no '('
    if (I != Stop && *I == ')')
@@ -517,9 +500,13 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
    Package.assign(Start,I - Start);
 
    // We don't want to confuse library users which can't handle MultiArch
+   string const arch = _config->Find("APT::Architecture");
    if (StripMultiArch == true) {
       size_t const found = Package.rfind(':');
-      if (found != string::npos)
+      if (found != string::npos &&
+         (strcmp(Package.c_str() + found, ":any") == 0 ||
+          strcmp(Package.c_str() + found, ":native") == 0 ||
+          strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
         Package = Package.substr(0,found);
    }
 
@@ -538,9 +525,9 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
       // Skip whitespace
       for (;I != Stop && isspace(*I) != 0; I++);
       Start = I;
-      for (;I != Stop && *I != ')'; I++);
-      if (I == Stop || Start == I)
-        return 0;     
+      I = (const char*) memchr(I, ')', Stop - I);
+      if (I == NULL || Start == I)
+        return 0;
       
       // Skip trailing whitespace
       const char *End = I;
@@ -560,7 +547,6 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
 
    if (ParseArchFlags == true)
    {
-      string arch = _config->Find("APT::Architecture");
       string completeArch = CompleteArch(arch);
 
       // Parse an architecture
@@ -643,13 +629,13 @@ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
       return true;
 
    string Package;
-   string const pkgArch = Ver.Arch(true);
+   string const pkgArch = Ver.Arch();
    string Version;
    unsigned int Op;
 
    while (1)
    {
-      Start = ParseDepends(Start,Stop,Package,Version,Op);
+      Start = ParseDepends(Start,Stop,Package,Version,Op,false,!MultiArchEnabled);
       if (Start == 0)
         return _error->Error("Problem parsing dependency %s",Tag);
 
@@ -682,7 +668,7 @@ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
    {
       string Package;
       string Version;
-      string const Arch = Ver.Arch(true);
+      string const Arch = Ver.Arch();
       unsigned int Op;
 
       while (1)
@@ -702,27 +688,28 @@ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
       }
    }
 
-   if (Ver->MultiArch == pkgCache::Version::Allowed)
+   if (MultiArchEnabled == false)
+      return true;
+   else if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
    {
       string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
-      NewProvides(Ver, Package, "any", Ver.VerStr());
+      return NewProvidesAllArch(Ver, Package, Ver.VerStr());
    }
+   else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
+      return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr());
 
-   if (Ver->MultiArch != pkgCache::Version::Foreign)
-      return true;
-
-   if (MultiArchEnabled == false)
-      return true;
-
-   string const Package = Ver.ParentPkg().Name();
-   string const Version = Ver.VerStr();
+   return true;
+}
+                                                                       /*}}}*/
+// ListParser::NewProvides - add provides for all architectures                /*{{{*/
+bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
+                               string const &Version) {
    for (std::vector<string>::const_iterator a = Architectures.begin();
        a != Architectures.end(); ++a)
    {
       if (NewProvides(Ver, Package, *a, Version) == false)
         return false;
    }
-
    return true;
 }
                                                                        /*}}}*/
@@ -757,7 +744,7 @@ bool debListParser::Step()
       if (Architecture.empty() == true)
         return true;
 
-      if (Arch.empty() == true || MultiArchEnabled == false)
+      if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false)
       {
         if (APT::Configuration::checkArchitecture(Architecture) == true)
            return true;
@@ -767,7 +754,7 @@ bool debListParser::Step()
         if (Architecture == Arch)
            return true;
 
-        if (Architecture == "all")
+        if (Architecture == "all" && Arch == _config->Find("APT::Architecture"))
            return true;
       }
 
@@ -782,39 +769,98 @@ bool debListParser::Step()
 bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI,
                                    FileFd &File, string component)
 {
-   pkgTagFile Tags(&File, File.Size() + 256); // XXX
-   pkgTagSection Section;
-   if (Tags.Step(Section) == false)
-      return false;
-
-   // FIXME: Do we need it now for multi-arch?
-   // mvo: I don't think we need to fill that in (it's unused since apt-0.6)
-//    FileI->Architecture = WriteUniqString(Arch);
-   
    // apt-secure does no longer download individual (per-section) Release
    // file. to provide Component pinning we use the section name now
    FileI->Component = WriteUniqString(component);
 
-   const char *Start;
-   const char *Stop;
-   if (Section.Find("Suite",Start,Stop) == true)
-      FileI->Archive = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Component",Start,Stop) == true)
-      FileI->Component = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Version",Start,Stop) == true)
-      FileI->Version = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Origin",Start,Stop) == true)
-      FileI->Origin = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Codename",Start,Stop) == true)
-      FileI->Codename = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Label",Start,Stop) == true)
-      FileI->Label = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Architecture",Start,Stop) == true)
-      FileI->Architecture = WriteUniqString(Start,Stop - Start);
-   
-   if (Section.FindFlag("NotAutomatic",FileI->Flags,
-                       pkgCache::Flag::NotAutomatic) == false)
-      _error->Warning("Bad NotAutomatic flag");
+   FILE* release = fdopen(dup(File.Fd()), "r");
+   if (release == NULL)
+      return false;
+
+   char buffer[101];
+   bool gpgClose = false;
+   while (fgets(buffer, sizeof(buffer), release) != NULL)
+   {
+      size_t len = 0;
+
+      // Skip empty lines
+      for (; buffer[len] == '\r' && buffer[len] == '\n'; ++len)
+         /* nothing */
+         ;
+      if (buffer[len] == '\0')
+        continue;
+
+      // only evalute the first GPG section
+      if (strncmp("-----", buffer, 5) == 0)
+      {
+        if (gpgClose == true)
+           break;
+        gpgClose = true;
+        continue;
+      }
+
+      // seperate the tag from the data
+      const char* dataStart = strchr(buffer + len, ':');
+      if (dataStart == NULL)
+        continue;
+      len = dataStart - buffer;
+      for (++dataStart; *dataStart == ' '; ++dataStart)
+         /* nothing */
+         ;
+      const char* dataEnd = (const char*)rawmemchr(dataStart, '\0');
+      // The last char should be a newline, but we can never be sure: #633350
+      const char* lineEnd = dataEnd;
+      for (--lineEnd; *lineEnd == '\r' || *lineEnd == '\n'; --lineEnd)
+         /* nothing */
+         ;
+      ++lineEnd;
+
+      // which datastorage need to be updated
+      map_ptrloc* writeTo = NULL;
+      if (buffer[0] == ' ')
+        ;
+      #define APT_PARSER_WRITETO(X, Y) else if (strncmp(Y, buffer, len) == 0) writeTo = &X;
+      APT_PARSER_WRITETO(FileI->Archive, "Suite")
+      APT_PARSER_WRITETO(FileI->Component, "Component")
+      APT_PARSER_WRITETO(FileI->Version, "Version")
+      APT_PARSER_WRITETO(FileI->Origin, "Origin")
+      APT_PARSER_WRITETO(FileI->Codename, "Codename")
+      APT_PARSER_WRITETO(FileI->Label, "Label")
+      #undef APT_PARSER_WRITETO
+      #define APT_PARSER_FLAGIT(X) else if (strncmp(#X, buffer, len) == 0) \
+        pkgTagSection::FindFlag(FileI->Flags, pkgCache::Flag:: X, dataStart, lineEnd);
+      APT_PARSER_FLAGIT(NotAutomatic)
+      APT_PARSER_FLAGIT(ButAutomaticUpgrades)
+      #undef APT_PARSER_FLAGIT
+
+      // load all data from the line and save it
+      string data;
+      if (writeTo != NULL)
+        data.append(dataStart, dataEnd);
+      if (sizeof(buffer) - 1 == (dataEnd - buffer))
+      {
+        while (fgets(buffer, sizeof(buffer), release) != NULL)
+        {
+           if (writeTo != NULL)
+              data.append(buffer);
+           if (strlen(buffer) != sizeof(buffer) - 1)
+              break;
+        }
+      }
+      if (writeTo != NULL)
+      {
+        // remove spaces and stuff from the end of the data line
+        for (std::string::reverse_iterator s = data.rbegin();
+             s != data.rend(); ++s)
+        {
+           if (*s != '\r' && *s != '\n' && *s != ' ')
+              break;
+           *s = '\0';
+        }
+        *writeTo = WriteUniqString(data);
+      }
+   }
+   fclose(release);
 
    return !_error->PendingError();
 }