You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

242 lines
9.4 KiB

  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 <stdint.h>
  18. #include <stdio.h>
  19. #include <list>
  20. #include <string>
  21. #include <vector>
  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[128];
  42. unsigned int BetaIndexes[128];
  43. pkgTagSectionPrivate * const d;
  44. APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const;
  45. #if defined(APT_PKG_EXPOSE_STRING_VIEW)
  46. APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const;
  47. APT_HIDDEN APT::StringView FindRawInternal(unsigned int Pos) const;
  48. #endif
  49. APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const;
  50. APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const;
  51. APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const;
  52. APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const;
  53. APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const;
  54. protected:
  55. const char *Stop;
  56. public:
  57. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  58. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  59. #if !defined(APT_PKG_EXPOSE_STRING_VIEW) || defined(APT_COMPILING_TAGFILE_COMPAT_CC)
  60. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  61. bool Find(const char *Tag,unsigned int &Pos) const;
  62. signed int FindI(const char *Tag,signed long Default = 0) const;
  63. bool FindB(const char *Tag, bool const &Default = false) const;
  64. unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
  65. bool FindFlag(const char * const Tag,uint8_t &Flags,
  66. uint8_t const Flag) const;
  67. bool FindFlag(const char *Tag,unsigned long &Flags,
  68. unsigned long Flag) const;
  69. bool Exists(const char* const Tag) const;
  70. #endif
  71. // TODO: Remove internally
  72. std::string FindS(const char *Tag) const;
  73. std::string FindRawS(const char *Tag) const;
  74. // Functions for lookup with a perfect hash function
  75. enum class Key;
  76. APT_HIDDEN bool Find(Key key,const char *&Start, const char *&End) const;
  77. APT_HIDDEN bool Find(Key key,unsigned int &Pos) const;
  78. APT_HIDDEN signed int FindI(Key key,signed long Default = 0) const;
  79. APT_HIDDEN bool FindB(Key key, bool Default = false) const;
  80. APT_HIDDEN unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
  81. APT_HIDDEN bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
  82. APT_HIDDEN bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
  83. APT_HIDDEN bool Exists(Key key) const;
  84. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  85. APT_HIDDEN APT::StringView Find(Key key) const;
  86. APT_HIDDEN APT::StringView FindRaw(Key key) const;
  87. APT_HIDDEN bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
  88. APT_HIDDEN bool Find(APT::StringView Tag,unsigned int &Pos) const;
  89. APT_HIDDEN APT::StringView Find(APT::StringView Tag) const;
  90. APT_HIDDEN APT::StringView FindRaw(APT::StringView Tag) const;
  91. APT_HIDDEN signed int FindI(APT::StringView Tag,signed long Default = 0) const;
  92. APT_HIDDEN bool FindB(APT::StringView, bool Default = false) const;
  93. APT_HIDDEN unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
  94. APT_HIDDEN bool FindFlag(APT::StringView Tag,uint8_t &Flags,
  95. uint8_t const Flag) const;
  96. APT_HIDDEN bool FindFlag(APT::StringView Tag,unsigned long &Flags,
  97. unsigned long Flag) const;
  98. APT_HIDDEN bool Exists(APT::StringView Tag) const;
  99. #endif
  100. bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
  101. const char* const Start, const char* const Stop);
  102. bool static FindFlag(unsigned long &Flags, unsigned long Flag,
  103. const char* Start, const char* Stop);
  104. /** \brief searches the boundaries of the current section
  105. *
  106. * While parameter Start marks the beginning of the section, this method
  107. * will search for the first double newline in the data stream which marks
  108. * the end of the section. It also does a first pass over the content of
  109. * the section parsing it as encountered for processing later on by Find
  110. *
  111. * @param Start is the beginning of the section
  112. * @param MaxLength is the size of valid data in the stream pointed to by Start
  113. * @param Restart if enabled internal state will be cleared, otherwise it is
  114. * assumed that now more data is available in the stream and the parsing will
  115. * start were it encountered insufficient data the last time.
  116. *
  117. * @return \b true if section end was found, \b false otherwise.
  118. * Beware that internal state will be inconsistent if \b false is returned!
  119. */
  120. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  121. inline unsigned long size() const {return Stop - Section;};
  122. void Trim();
  123. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  124. /** \brief amount of Tags in the current section
  125. *
  126. * Note: if a Tag is mentioned repeatedly it will be counted multiple
  127. * times, but only the last occurrence is available via Find methods.
  128. */
  129. unsigned int Count() const;
  130. void Get(const char *&Start,const char *&Stop,unsigned int I) const;
  131. inline void GetSection(const char *&Start,const char *&Stop) const
  132. {
  133. Start = Section;
  134. Stop = this->Stop;
  135. };
  136. pkgTagSection();
  137. virtual ~pkgTagSection();
  138. struct Tag
  139. {
  140. enum ActionType { REMOVE, RENAME, REWRITE } Action;
  141. std::string Name;
  142. std::string Data;
  143. static Tag Remove(std::string const &Name);
  144. static Tag Rename(std::string const &OldName, std::string const &NewName);
  145. static Tag Rewrite(std::string const &Name, std::string const &Data);
  146. private:
  147. Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
  148. Action(Action), Name(Name), Data(Data) {}
  149. };
  150. /** Write this section (with optional rewrites) to a file
  151. *
  152. * @param File to write the section to
  153. * @param Order in which tags should appear in the file
  154. * @param Rewrite is a set of tags to be renamed, rewritten and/or removed
  155. * @return \b true if successful, otherwise \b false
  156. */
  157. bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
  158. };
  159. class APT_DEPRECATED_MSG("Use pkgTagFile with the SUPPORT_COMMENTS flag instead") pkgUserTagSection : public pkgTagSection
  160. {
  161. virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE;
  162. };
  163. /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing
  164. * via #pkgTagSection. The default mode tries to be as fast as possible and
  165. * assumes perfectly valid (machine generated) files like Packages. Support
  166. * for comments e.g. needs to be enabled explicitly. */
  167. class pkgTagFile
  168. {
  169. pkgTagFilePrivate * const d;
  170. APT_HIDDEN bool Fill();
  171. APT_HIDDEN bool Resize();
  172. APT_HIDDEN bool Resize(unsigned long long const newSize);
  173. public:
  174. bool Step(pkgTagSection &Section);
  175. unsigned long Offset();
  176. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  177. enum Flags
  178. {
  179. STRICT = 0,
  180. SUPPORT_COMMENTS = 1 << 0,
  181. };
  182. void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
  183. void Init(FileFd * const F,unsigned long long const Size = 32*1024);
  184. pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
  185. pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
  186. virtual ~pkgTagFile();
  187. };
  188. extern const char **TFRewritePackageOrder;
  189. extern const char **TFRewriteSourceOrder;
  190. APT_IGNORE_DEPRECATED_PUSH
  191. struct APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") TFRewriteData
  192. {
  193. const char *Tag;
  194. const char *Rewrite;
  195. const char *NewTag;
  196. };
  197. APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  198. TFRewriteData *Rewrite);
  199. APT_IGNORE_DEPRECATED_POP
  200. #endif