tagfile.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Fast scanner for RFC-822 type header information
  5. This parser handles Debian package files (and others). Their form is
  6. RFC-822 type header fields in groups separated by a blank line.
  7. The parser reads the file and provides methods to step linearly
  8. over it or to jump to a pre-recorded start point and read that record.
  9. A second class is used to perform pre-parsing of the record. It works
  10. by indexing the start of each header field and providing lookup
  11. functions for header fields.
  12. ##################################################################### */
  13. /*}}}*/
  14. #ifndef PKGLIB_TAGFILE_H
  15. #define PKGLIB_TAGFILE_H
  16. #include <apt-pkg/macros.h>
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <string>
  20. #include <vector>
  21. #include <list>
  22. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  23. #include <apt-pkg/string_view.h>
  24. #endif
  25. #ifndef APT_8_CLEANER_HEADERS
  26. #include <apt-pkg/fileutl.h>
  27. #endif
  28. class FileFd;
  29. class pkgTagSectionPrivate;
  30. class pkgTagFilePrivate;
  31. /** \class pkgTagSection parses a single deb822 stanza and provides various Find methods
  32. * to extract the included values. It can also be used to modify and write a
  33. * valid deb822 stanza optionally (re)ordering the fields inside the stanza.
  34. *
  35. * Beware: This class does \b NOT support (#-)comments in in- or output!
  36. * If the input contains comments they have to be stripped first like pkgTagFile
  37. * does with SUPPORT_COMMENTS flag set. */
  38. class pkgTagSection
  39. {
  40. const char *Section;
  41. unsigned int AlphaIndexes[0x100];
  42. pkgTagSectionPrivate * const d;
  43. protected:
  44. const char *Stop;
  45. public:
  46. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  47. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  48. #if !defined(APT_PKG_EXPOSE_STRING_VIEW) || defined(APT_COMPILING_TAGFILE_COMPAT_CC)
  49. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  50. bool Find(const char *Tag,unsigned int &Pos) const;
  51. signed int FindI(const char *Tag,signed long Default = 0) const;
  52. bool FindB(const char *Tag, bool const &Default = false) const;
  53. unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
  54. bool FindFlag(const char * const Tag,uint8_t &Flags,
  55. uint8_t const Flag) const;
  56. bool FindFlag(const char *Tag,unsigned long &Flags,
  57. unsigned long Flag) const;
  58. bool Exists(const char* const Tag) const;
  59. #endif
  60. // TODO: Remove internally
  61. std::string FindS(const char *Tag) const;
  62. std::string FindRawS(const char *Tag) const;
  63. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  64. APT_HIDDEN bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
  65. APT_HIDDEN bool Find(APT::StringView Tag,unsigned int &Pos) const;
  66. APT_HIDDEN APT::StringView Find(APT::StringView Tag) const;
  67. APT_HIDDEN APT::StringView FindRaw(APT::StringView Tag) const;
  68. APT_HIDDEN signed int FindI(APT::StringView Tag,signed long Default = 0) const;
  69. APT_HIDDEN bool FindB(APT::StringView, bool Default = false) const;
  70. APT_HIDDEN unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
  71. APT_HIDDEN bool FindFlag(APT::StringView Tag,uint8_t &Flags,
  72. uint8_t const Flag) const;
  73. APT_HIDDEN bool FindFlag(APT::StringView Tag,unsigned long &Flags,
  74. unsigned long Flag) const;
  75. APT_HIDDEN bool Exists(APT::StringView Tag) const;
  76. #endif
  77. bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
  78. const char* const Start, const char* const Stop);
  79. bool static FindFlag(unsigned long &Flags, unsigned long Flag,
  80. const char* Start, const char* Stop);
  81. /** \brief searches the boundaries of the current section
  82. *
  83. * While parameter Start marks the beginning of the section, this method
  84. * will search for the first double newline in the data stream which marks
  85. * the end of the section. It also does a first pass over the content of
  86. * the section parsing it as encountered for processing later on by Find
  87. *
  88. * @param Start is the beginning of the section
  89. * @param MaxLength is the size of valid data in the stream pointed to by Start
  90. * @param Restart if enabled internal state will be cleared, otherwise it is
  91. * assumed that now more data is available in the stream and the parsing will
  92. * start were it encountered insufficent data the last time.
  93. *
  94. * @return \b true if section end was found, \b false otherwise.
  95. * Beware that internal state will be inconsistent if \b false is returned!
  96. */
  97. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  98. inline unsigned long size() const {return Stop - Section;};
  99. void Trim();
  100. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  101. /** \brief amount of Tags in the current section
  102. *
  103. * Note: if a Tag is mentioned repeatly it will be counted multiple
  104. * times, but only the last occurrence is available via Find methods.
  105. */
  106. unsigned int Count() const;
  107. void Get(const char *&Start,const char *&Stop,unsigned int I) const;
  108. inline void GetSection(const char *&Start,const char *&Stop) const
  109. {
  110. Start = Section;
  111. Stop = this->Stop;
  112. };
  113. pkgTagSection();
  114. virtual ~pkgTagSection();
  115. struct Tag
  116. {
  117. enum ActionType { REMOVE, RENAME, REWRITE } Action;
  118. std::string Name;
  119. std::string Data;
  120. static Tag Remove(std::string const &Name);
  121. static Tag Rename(std::string const &OldName, std::string const &NewName);
  122. static Tag Rewrite(std::string const &Name, std::string const &Data);
  123. private:
  124. Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
  125. Action(Action), Name(Name), Data(Data) {}
  126. };
  127. /** Write this section (with optional rewrites) to a file
  128. *
  129. * @param File to write the section to
  130. * @param Order in which tags should appear in the file
  131. * @param Rewrite is a set of tags to be renamed, rewritten and/or removed
  132. * @return \b true if successful, otherwise \b false
  133. */
  134. bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
  135. };
  136. class APT_DEPRECATED_MSG("Use pkgTagFile with the SUPPORT_COMMENTS flag instead") pkgUserTagSection : public pkgTagSection
  137. {
  138. virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE;
  139. };
  140. /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing
  141. * via #pkgTagSection. The default mode tries to be as fast as possible and
  142. * assumes perfectly valid (machine generated) files like Packages. Support
  143. * for comments e.g. needs to be enabled explicitly. */
  144. class pkgTagFile
  145. {
  146. pkgTagFilePrivate * const d;
  147. APT_HIDDEN bool Fill();
  148. APT_HIDDEN bool Resize();
  149. APT_HIDDEN bool Resize(unsigned long long const newSize);
  150. public:
  151. bool Step(pkgTagSection &Section);
  152. unsigned long Offset();
  153. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  154. enum Flags
  155. {
  156. STRICT = 0,
  157. SUPPORT_COMMENTS = 1 << 0,
  158. };
  159. void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
  160. void Init(FileFd * const F,unsigned long long const Size = 32*1024);
  161. pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
  162. pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
  163. virtual ~pkgTagFile();
  164. };
  165. extern const char **TFRewritePackageOrder;
  166. extern const char **TFRewriteSourceOrder;
  167. APT_IGNORE_DEPRECATED_PUSH
  168. struct APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") TFRewriteData
  169. {
  170. const char *Tag;
  171. const char *Rewrite;
  172. const char *NewTag;
  173. };
  174. APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  175. TFRewriteData *Rewrite);
  176. APT_IGNORE_DEPRECATED_POP
  177. #endif