X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/1c73b0fc41c23a08994ef1464c529e0aacff16de..a473295d01ed9c599926d9a8c212d4e1a404f78b:/apt-pkg/deb/deblistparser.cc diff --git a/apt-pkg/deb/deblistparser.cc b/apt-pkg/deb/deblistparser.cc index d88e25e6f..878170d00 100644 --- a/apt-pkg/deb/deblistparser.cc +++ b/apt-pkg/deb/deblistparser.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -34,14 +35,15 @@ /*}}}*/ using std::string; +using APT::StringView; -static debListParser::WordList PrioList[] = { +static const debListParser::WordList PrioList[] = { {"required",pkgCache::State::Required}, {"important",pkgCache::State::Important}, {"standard",pkgCache::State::Standard}, {"optional",pkgCache::State::Optional}, {"extra",pkgCache::State::Extra}, - {NULL, 0}}; + {"", 0}}; // ListParser::debListParser - Constructor /*{{{*/ // --------------------------------------------------------------------- @@ -49,15 +51,30 @@ static debListParser::WordList PrioList[] = { in Step(), if no Architecture is given we will accept every arch we would accept in general with checkArchitecture() */ debListParser::debListParser(FileFd *File) : - pkgCacheListParser(), d(NULL), Tags(File) + pkgCacheListParser(), Tags(File) { + // this dance allows an empty value to override the default + if (_config->Exists("pkgCacheGen::ForceEssential")) + { + forceEssential = _config->FindVector("pkgCacheGen::ForceEssential"); + if (forceEssential.empty() == false && _config->Find("pkgCacheGen::ForceEssential").empty()) + forceEssential.emplace_back("apt"); + } + else + forceEssential.emplace_back("apt"); + forceImportant = _config->FindVector("pkgCacheGen::ForceImportant"); } /*}}}*/ // ListParser::Package - Return the package name /*{{{*/ // --------------------------------------------------------------------- /* This is to return the name of the package this section describes */ string debListParser::Package() { - string const Result = Section.FindS("Package"); + string Result = Section.Find(pkgTagSection::Key::Package).to_string(); + + // Normalize mixed case package names to lower case, like dpkg does + // See Bug#807012 for details + std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii); + if(unlikely(Result.empty() == true)) _error->Error("Encountered a section with no Package: header"); return Result; @@ -66,8 +83,8 @@ string debListParser::Package() { // ListParser::Architecture - Return the package arch /*{{{*/ // --------------------------------------------------------------------- /* This will return the Architecture of the package this section describes */ -string debListParser::Architecture() { - std::string const Arch = Section.FindS("Architecture"); +APT::StringView debListParser::Architecture() { + auto const Arch = Section.Find(pkgTagSection::Key::Architecture); return Arch.empty() ? "none" : Arch; } /*}}}*/ @@ -75,7 +92,7 @@ string debListParser::Architecture() { // --------------------------------------------------------------------- /* */ bool debListParser::ArchitectureAll() { - return Section.FindS("Architecture") == "all"; + return Section.Find(pkgTagSection::Key::Architecture) == "all"; } /*}}}*/ // ListParser::Version - Return the version string /*{{{*/ @@ -83,15 +100,15 @@ bool debListParser::ArchitectureAll() { /* This is to return the string describing the version in debian form, epoch:upstream-release. If this returns the blank string then the entry is assumed to only describe package properties */ -string debListParser::Version() +APT::StringView debListParser::Version() { - return Section.FindS("Version"); + return Section.Find(pkgTagSection::Key::Version); } /*}}}*/ unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/ { unsigned char MA; - string const MultiArch = Section.FindS("Multi-Arch"); + auto const MultiArch = Section.Find(pkgTagSection::Key::Multi_Arch); if (MultiArch.empty() == true || MultiArch == "no") MA = pkgCache::Version::No; else if (MultiArch == "same") { @@ -113,7 +130,7 @@ unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/ { if (showErrors == true) _error->Warning("Unknown Multi-Arch type '%s' for package '%s'", - MultiArch.c_str(), Section.FindS("Package").c_str()); + MultiArch.to_string().c_str(), Section.FindS("Package").c_str()); MA = pkgCache::Version::No; } @@ -132,40 +149,41 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver) const char *Stop; // Parse the section - if (Section.Find("Section",Start,Stop) == true) + if (Section.Find(pkgTagSection::Key::Section,Start,Stop) == true) { map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start); Ver->Section = idx; } // Parse the source package name - pkgCache::GrpIterator const G = Ver.ParentPkg().Group(); + pkgCache::GrpIterator G = Ver.ParentPkg().Group(); Ver->SourcePkgName = G->Name; Ver->SourceVerStr = Ver->VerStr; - if (Section.Find("Source",Start,Stop) == true) + if (Section.Find(pkgTagSection::Key::Source,Start,Stop) == true) { const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start); pkgCache::VerIterator V; if (Space != NULL) { - Stop = Space; const char * const Open = (const char * const) memchr(Space, '(', Stop - Space); if (likely(Open != NULL)) { const char * const Close = (const char * const) memchr(Open, ')', Stop - Open); if (likely(Close != NULL)) { - std::string const version(Open + 1, (Close - Open) - 1); + APT::StringView const version(Open + 1, (Close - Open) - 1); if (version != Ver.VerStr()) { map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version); + G = Ver.ParentPkg().Group(); Ver->SourceVerStr = idx; } } } + Stop = Space; } - std::string const pkgname(Start, Stop - Start); + APT::StringView const pkgname(Start, Stop - Start); if (pkgname != G.Name()) { for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P)) @@ -184,6 +202,7 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver) if (V.end() == true) { map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname); + G = Ver.ParentPkg().Group(); Ver->SourcePkgName = idx; } } @@ -191,68 +210,63 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver) Ver->MultiArch = ParseMultiArch(true); // Archive Size - Ver->Size = Section.FindULL("Size"); + Ver->Size = Section.FindULL(pkgTagSection::Key::Size); // Unpacked Size (in K) - Ver->InstalledSize = Section.FindULL("Installed-Size"); + Ver->InstalledSize = Section.FindULL(pkgTagSection::Key::Installed_Size); Ver->InstalledSize *= 1024; // Priority - if (Section.Find("Priority",Start,Stop) == true) + if (Section.Find(pkgTagSection::Key::Priority,Start,Stop) == true) { - if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false) + if (GrabWord(StringView(Start,Stop-Start),PrioList,Ver->Priority) == false) Ver->Priority = pkgCache::State::Extra; } - if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Pre_Depends,pkgCache::Dep::PreDepends) == false) return false; - if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Depends,pkgCache::Dep::Depends) == false) return false; - if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Conflicts,pkgCache::Dep::Conflicts) == false) return false; - if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Breaks,pkgCache::Dep::DpkgBreaks) == false) return false; - if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Recommends,pkgCache::Dep::Recommends) == false) return false; - if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Suggests,pkgCache::Dep::Suggests) == false) return false; - if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Replaces,pkgCache::Dep::Replaces) == false) return false; - if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Enhances,pkgCache::Dep::Enhances) == false) return false; // Obsolete. - if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false) + if (ParseDepends(Ver,pkgTagSection::Key::Optional,pkgCache::Dep::Suggests) == false) return false; if (ParseProvides(Ver) == false) return false; + if (ParseTag(Ver) == false) + return false; return true; } /*}}}*/ -// ListParser::Description - Return the description string /*{{{*/ -// --------------------------------------------------------------------- -/* This is to return the string describing the package in debian - form. If this returns the blank string then the entry is assumed to - only describe package properties */ -string debListParser::Description(std::string const &lang) -{ - if (lang.empty()) - return Section.FindS("Description"); - else - return Section.FindS(string("Description-").append(lang).c_str()); -} - /*}}}*/ // ListParser::AvailableDescriptionLanguages /*{{{*/ std::vector debListParser::AvailableDescriptionLanguages() { std::vector const understood = APT::Configuration::getLanguages(); std::vector avail; + static constexpr int prefixLen = 12; + char buf[32] = "Description-"; if (Section.Exists("Description") == true) avail.push_back(""); for (std::vector::const_iterator lang = understood.begin(); lang != understood.end(); ++lang) { - std::string const tagname = "Description-" + *lang; - if (Section.Exists(tagname.c_str()) == true) + if (unlikely(lang->size() > sizeof(buf) - prefixLen)) { + _error->Warning("Ignoring translated description %s", lang->c_str()); + continue; + } + memcpy(buf + prefixLen, lang->c_str(), lang->size()); + if (Section.Exists(StringView(buf, prefixLen + lang->size())) == true) avail.push_back(*lang); } return avail; @@ -264,28 +278,27 @@ std::vector debListParser::AvailableDescriptionLanguages() description. If no Description-md5 is found in the section it will be calculated. */ -MD5SumValue debListParser::Description_md5() +APT::StringView debListParser::Description_md5() { - string const value = Section.FindS("Description-md5"); - if (value.empty() == true) + StringView const value = Section.Find(pkgTagSection::Key::Description_md5); + if (unlikely(value.empty() == true)) { - std::string const desc = Description("") + "\n"; + StringView const desc = Section.Find(pkgTagSection::Key::Description); if (desc == "\n") - return MD5SumValue(); + return StringView(); MD5Summation md5; - md5.Add(desc.c_str()); - return md5.Result(); + md5.Add(desc.data(), desc.size()); + md5.Add("\n"); + MD5Buffer = md5.Result(); + return StringView(MD5Buffer); } else if (likely(value.size() == 32)) { - if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos)) - return MD5SumValue(value); - _error->Error("Malformed Description-md5 line; includes invalid character '%s'", value.c_str()); - return MD5SumValue(); + return value; } - _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%s'", (int)value.size(), value.c_str()); - return MD5SumValue(); + _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%.*s'", (int)value.size(), (int)value.length(), value.data()); + return StringView(); } /*}}}*/ // ListParser::UsePackage - Update a package structure /*{{{*/ @@ -301,12 +314,12 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg, string const static essential = _config->Find("pkgCacheGen::Essential", "all"); if (essential == "all" || (essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch())) - if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false) + if (Section.FindFlag(pkgTagSection::Key::Essential,Pkg->Flags,pkgCache::Flag::Essential) == false) return false; - if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false) + if (Section.FindFlag(pkgTagSection::Key::Important,Pkg->Flags,pkgCache::Flag::Important) == false) return false; - if (strcmp(Pkg.Name(),"apt") == 0) + if (std::find(forceEssential.begin(), forceEssential.end(), Pkg.Name()) != forceEssential.end()) { if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) || essential == "all") @@ -314,6 +327,8 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg, else Pkg->Flags |= pkgCache::Flag::Important; } + else if (std::find(forceImportant.begin(), forceImportant.end(), Pkg.Name()) != forceImportant.end()) + Pkg->Flags |= pkgCache::Flag::Important; if (ParseStatus(Pkg,Ver) == false) return false; @@ -325,38 +340,35 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg, /* */ unsigned short debListParser::VersionHash() { - const char *Sections[] ={"Installed-Size", - "Depends", - "Pre-Depends", -// "Suggests", -// "Recommends", - "Conflicts", - "Breaks", - "Replaces",0}; + static constexpr pkgTagSection::Key Sections[] ={ + pkgTagSection::Key::Installed_Size, + pkgTagSection::Key::Depends, + pkgTagSection::Key::Pre_Depends, +// pkgTagSection::Key::Suggests, +// pkgTagSection::Key::Recommends", + pkgTagSection::Key::Conflicts, + pkgTagSection::Key::Breaks, + pkgTagSection::Key::Replaces}; unsigned long Result = INIT_FCS; - char S[1024]; - for (const char * const *I = Sections; *I != 0; ++I) + for (auto I : Sections) { const char *Start; const char *End; - if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S)) + if (Section.Find(I,Start,End) == false) continue; /* 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 *J = S; + reformatting depends operators < -> <=, so we drop all = from the + string to make that not matter. */ for (; Start != End; ++Start) { - if (isspace(*Start) != 0) + if (isspace_ascii(*Start) != 0 || *Start == '=') continue; - *J++ = tolower_ascii(*Start); - - if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=') - *J++ = '='; + Result = AddCRC16Byte(Result, tolower_ascii_unsafe(*Start)); } - Result = AddCRC16(Result,S,J - S); + } return Result; @@ -371,8 +383,8 @@ unsigned short debListParser::VersionHash() status = not-installed, config-files, half-installed, unpacked, half-configured, triggers-awaited, triggers-pending, installed */ -bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg, - pkgCache::VerIterator &Ver) +bool debListParser::ParseStatus(pkgCache::PkgIterator &, + pkgCache::VerIterator &) { return true; } @@ -381,13 +393,13 @@ bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg, { const char *Start; const char *Stop; - if (Section.Find("Status",Start,Stop) == false) + if (Section.Find(pkgTagSection::Key::Status,Start,Stop) == false) return true; // UsePackage() is responsible for setting the flag in the default case bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed"; if (essential == true && - Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false) + Section.FindFlag(pkgTagSection::Key::Essential,Pkg->Flags,pkgCache::Flag::Essential) == false) return false; // Isolate the first word @@ -402,8 +414,8 @@ bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg, {"hold",pkgCache::State::Hold}, {"deinstall",pkgCache::State::DeInstall}, {"purge",pkgCache::State::Purge}, - {NULL, 0}}; - if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false) + {"", 0}}; + if (GrabWord(StringView(Start,I-Start),WantList,Pkg->SelectedState) == false) return _error->Error("Malformed 1st word in the Status line"); // Isloate the next word @@ -418,8 +430,8 @@ bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg, {"reinstreq",pkgCache::State::ReInstReq}, {"hold",pkgCache::State::HoldInst}, {"hold-reinstreq",pkgCache::State::HoldReInstReq}, - {NULL, 0}}; - if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false) + {"", 0}}; + if (GrabWord(StringView(Start,I-Start),FlagList,Pkg->InstState) == false) return _error->Error("Malformed 2nd word in the Status line"); // Isloate the last word @@ -438,8 +450,8 @@ bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg, {"triggers-awaited",pkgCache::State::TriggersAwaited}, {"triggers-pending",pkgCache::State::TriggersPending}, {"installed",pkgCache::State::Installed}, - {NULL, 0}}; - if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false) + {"", 0}}; + if (GrabWord(StringView(Start,I-Start),StatusList,Pkg->CurrentState) == false) return _error->Error("Malformed 3rd word in the Status line"); /* A Status line marks the package as indicating the current @@ -536,13 +548,29 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, unsigned int &Op, bool const &ParseArchFlags, bool const &StripMultiArch, bool const &ParseRestrictionsList) +{ + StringView PackageView; + StringView VerView; + + auto res = ParseDepends(Start, Stop, PackageView, VerView, Op, (bool)ParseArchFlags, + (bool) StripMultiArch, (bool) ParseRestrictionsList); + Package = PackageView.to_string(); + Ver = VerView.to_string(); + + return res; +} +const char *debListParser::ParseDepends(const char *Start,const char *Stop, + StringView &Package,StringView &Ver, + unsigned int &Op, bool ParseArchFlags, + bool StripMultiArch, + bool ParseRestrictionsList) { // Strip off leading space - for (;Start != Stop && isspace(*Start) != 0; ++Start); + for (;Start != Stop && isspace_ascii(*Start) != 0; ++Start); // Parse off the package name const char *I = Start; - for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' && + for (;I != Stop && isspace_ascii(*I) == 0 && *I != '(' && *I != ')' && *I != ',' && *I != '|' && *I != '[' && *I != ']' && *I != '<' && *I != '>'; ++I); @@ -554,33 +582,33 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, return 0; // Stash the package name - Package.assign(Start,I - Start); + Package = StringView(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) { + string const arch = _config->Find("APT::Architecture"); size_t const found = Package.rfind(':'); - 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)) + if (found != StringView::npos && + (Package.substr(found) == ":any" || + Package.substr(found) == ":native" || + Package.substr(found +1) == arch)) Package = Package.substr(0,found); } // Skip white space to the '(' - for (;I != Stop && isspace(*I) != 0 ; I++); + for (;I != Stop && isspace_ascii(*I) != 0 ; I++); // Parse a version if (I != Stop && *I == '(') { // Skip the '(' - for (I++; I != Stop && isspace(*I) != 0 ; I++); + for (I++; I != Stop && isspace_ascii(*I) != 0 ; I++); if (I + 3 >= Stop) return 0; I = ConvertRelation(I,Op); // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); Start = I; I = (const char*) memchr(I, ')', Stop - I); if (I == NULL || Start == I) @@ -588,22 +616,23 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, // Skip trailing whitespace const char *End = I; - for (; End > Start && isspace(End[-1]); End--); + for (; End > Start && isspace_ascii(End[-1]); End--); - Ver.assign(Start,End-Start); + Ver = StringView(Start,End-Start); I++; } else { - Ver.clear(); + Ver = StringView(); Op = pkgCache::Dep::NoOp; } // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); - if (ParseArchFlags == true) + if (unlikely(ParseArchFlags == true)) { + string const arch = _config->Find("APT::Architecture"); APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false); // Parse an architecture @@ -620,7 +649,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, while (I != Stop) { // look for whitespace or ending ']' - for (;End != Stop && !isspace(*End) && *End != ']'; ++End); + for (;End != Stop && !isspace_ascii(*End) && *End != ']'; ++End); if (unlikely(End == Stop)) return 0; @@ -631,7 +660,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, ++I; } - std::string arch(I, End); + std::string const arch(I, End); if (arch.empty() == false && matchesArch(arch.c_str()) == true) { Found = true; @@ -647,7 +676,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, } I = End; - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); } if (NegArch == true) @@ -658,10 +687,10 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, } // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); } - if (ParseRestrictionsList == true) + if (unlikely(ParseRestrictionsList == true)) { // Parse a restrictions formula which is in disjunctive normal form: // (foo AND bar) OR (blub AND bla) @@ -691,7 +720,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, for (;End != Stop && *End != '>'; ++End); I = ++End; // skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); } else { bool applies2 = true; // all the conditions inside a restriction list have to be @@ -701,7 +730,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, { // look for whitespace or ending '>' // End now points to the character after the current term - for (;End != Stop && !isspace(*End) && *End != '>'; ++End); + for (;End != Stop && !isspace_ascii(*End) && *End != '>'; ++End); if (unlikely(End == Stop)) return 0; @@ -713,8 +742,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, ++I; } - std::string restriction(I, End); - + std::string const restriction(I, End); if (restriction.empty() == false && profiles.empty() == false && std::find(profiles.begin(), profiles.end(), restriction) != profiles.end()) { @@ -734,13 +762,13 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, if (*End++ == '>') { I = End; // skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); break; } I = End; // skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); } if (applies2) { applies1 = true; @@ -759,7 +787,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, if (I == Stop || *I == ',' || *I == '|') { if (I != Stop) - for (I++; I != Stop && isspace(*I) != 0; I++); + for (I++; I != Stop && isspace_ascii(*I) != 0; I++); return I; } @@ -771,40 +799,47 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, /* This is the higher level depends parser. It takes a tag and generates a complete depends tree for the given version. */ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver, - const char *Tag,unsigned int Type) + pkgTagSection::Key Key,unsigned int Type) { const char *Start; const char *Stop; - if (Section.Find(Tag,Start,Stop) == false) + if (Section.Find(Key,Start,Stop) == false || Start == Stop) return true; string const pkgArch = Ver.Arch(); while (1) { - string Package; - string Version; + StringView Package; + StringView Version; unsigned int Op; Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false); if (Start == 0) - return _error->Error("Problem parsing dependency %s",Tag); + return _error->Error("Problem parsing dependency %zu",static_cast(Key)); // TODO size_t const found = Package.rfind(':'); - if (found == string::npos || strcmp(Package.c_str() + found, ":any") == 0) + if (found == string::npos) { if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false) return false; } + else if (Package.substr(found) == ":any") + { + if (NewDepends(Ver,Package,"any",Version,Op,Type) == false) + return false; + } else { - string Arch = Package.substr(found+1, string::npos); - Package = Package.substr(0, found); // Such dependencies are not supposed to be accepted … // … but this is probably the best thing to do anyway - if (Arch == "native") - Arch = _config->Find("APT::Architecture"); - if (NewDepends(Ver,Package,Arch,Version,Op | pkgCache::Dep::ArchSpecific,Type) == false) + if (Package.substr(found + 1) == "native") + { + std::string const Pkg = Package.substr(0, found).to_string() + ':' + Ver.Cache()->NativeArch(); + if (NewDepends(Ver, Pkg, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false) + return false; + } + else if (NewDepends(Ver, Package, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false) return false; } @@ -819,48 +854,143 @@ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver, /* */ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver) { + /* it is unlikely, but while parsing dependencies, we might have already + picked up multi-arch implicit provides which we do not want to duplicate here */ + bool hasProvidesAlready = false; + std::string const spzName = Ver.ParentPkg().FullName(false); + { + for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv) + { + if (Prv.IsMultiArchImplicit() == false || (Prv->Flags & pkgCache::Flag::ArchSpecific) == 0) + continue; + if (spzName != Prv.OwnerPkg().FullName(false)) + continue; + hasProvidesAlready = true; + break; + } + } + + string const Arch = Ver.Arch(); const char *Start; const char *Stop; - if (Section.Find("Provides",Start,Stop) == true) + if (Section.Find(pkgTagSection::Key::Provides,Start,Stop) == true) { - string Package; - string Version; - string const Arch = Ver.Arch(); + StringView Package; + StringView Version; unsigned int Op; - while (1) + do { - Start = ParseDepends(Start,Stop,Package,Version,Op); + Start = ParseDepends(Start,Stop,Package,Version,Op, false, false, false); const size_t archfound = Package.rfind(':'); if (Start == 0) return _error->Error("Problem parsing Provides line"); - if (Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals) { - _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.c_str()); + if (unlikely(Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals)) { + _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.to_string().c_str()); } else if (archfound != string::npos) { - string OtherArch = Package.substr(archfound+1, string::npos); - Package = Package.substr(0, archfound); - if (NewProvides(Ver, Package, OtherArch, Version, pkgCache::Flag::ArchSpecific) == false) + StringView spzArch = Package.substr(archfound + 1); + if (spzArch != "any") + { + if (NewProvides(Ver, Package.substr(0, archfound), spzArch, Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } + if (NewProvides(Ver, Package, "any", Version, pkgCache::Flag::ArchSpecific) == false) return false; } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) { - if (NewProvidesAllArch(Ver, Package, Version, 0) == false) + if (APT::Configuration::checkArchitecture(Arch)) + { + if (NewProvidesAllArch(Ver, Package, Version, 0) == false) + return false; + } + else if (NewProvides(Ver, Package, Arch, Version, 0) == false) return false; } else { + if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + { + if (NewProvides(Ver, Package.to_string().append(":any"), "any", Version, pkgCache::Flag::MultiArchImplicit) == false) + return false; + } if (NewProvides(Ver, Package, Arch, Version, 0) == false) return false; } + if (archfound == std::string::npos) + { + string spzName = Package.to_string(); + spzName.push_back(':'); + spzName.append(Ver.ParentPkg().Arch()); + pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any"); + if (spzPkg.end() == false) + { + if (NewProvides(Ver, spzName, "any", Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } + } + } while (Start != Stop); + } - if (Start == Stop) - break; + if (APT::Configuration::checkArchitecture(Arch)) + { + if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + { + string const Package = string(Ver.ParentPkg().Name()).append(":").append("any"); + if (NewProvides(Ver, Package, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false) + return false; + } + else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) + { + if (NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false) + return false; } } - if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + if (hasProvidesAlready == false) { - string const Package = string(Ver.ParentPkg().Name()).append(":").append("any"); - return NewProvidesAllArch(Ver, Package, Ver.VerStr(), pkgCache::Flag::MultiArchImplicit); + pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any"); + if (spzPkg.end() == false) + { + if (NewProvides(Ver, spzName, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } + } + return true; +} + /*}}}*/ +// ListParser::ParseTag - Parse the tag list /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool debListParser::ParseTag(pkgCache::VerIterator &Ver) +{ + const char *Start; + const char *Stop; + if (Section.Find("Tag",Start,Stop) == false) + return true; + + while (1) { + while (1) { + if (Start == Stop) + return true; + if (Stop[-1] != ' ' && Stop[-1] != '\t') + break; + --Stop; + } + + const char *Begin = Stop - 1; + while (Begin != Start && Begin[-1] != ' ' && Begin[-1] != ',') + --Begin; + + if (NewTag(Ver, Begin, Stop - Begin) == false) + return false; + + while (1) { + if (Begin == Start) + return true; + if (Begin[-1] == ',') + break; + --Begin; + } + + Stop = Begin - 1; } - else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) - return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit); return true; } @@ -868,11 +998,12 @@ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver) // ListParser::GrabWord - Matches a word and returns /*{{{*/ // --------------------------------------------------------------------- /* Looks for a word in a list of words - for ParseStatus */ -bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out) +bool debListParser::GrabWord(StringView Word, WordList const *List, unsigned char &Out) { - for (unsigned int C = 0; List[C].Str != 0; C++) + for (unsigned int C = 0; List[C].Str.empty() == false; C++) { - if (strcasecmp(Word.c_str(),List[C].Str) == 0) + if (Word.length() == List[C].Str.length() && + strncasecmp(Word.data(), List[C].Str.data(), Word.length()) == 0) { Out = List[C].Val; return true; @@ -910,8 +1041,8 @@ bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/ // status file has no (Download)Size, but all others are fair game // status file is parsed last, so the first version we encounter is // probably also the version we have downloaded - unsigned long long const Size = Section.FindULL("Size"); - if (Size != 0 && Size != Ver->Size) + unsigned long long const Size = Section.FindULL(pkgTagSection::Key::Size); + if (Size != 0 && Ver->Size != 0 && Size != Ver->Size) return false; // available everywhere, but easier to check here than to include in VersionHash unsigned char MultiArch = ParseMultiArch(false);