tagfile.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <string>
  19. #include <vector>
  20. #include <list>
  21. #ifndef APT_8_CLEANER_HEADERS
  22. #include <apt-pkg/fileutl.h>
  23. #endif
  24. class FileFd;
  25. class pkgTagSectionPrivate;
  26. class pkgTagSection
  27. {
  28. const char *Section;
  29. unsigned int AlphaIndexes[0x100];
  30. pkgTagSectionPrivate * const d;
  31. protected:
  32. const char *Stop;
  33. public:
  34. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  35. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  36. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  37. bool Find(const char *Tag,unsigned int &Pos) const;
  38. std::string FindS(const char *Tag) const;
  39. std::string FindRawS(const char *Tag) const;
  40. signed int FindI(const char *Tag,signed long Default = 0) const;
  41. bool FindB(const char *Tag, bool const &Default = false) const;
  42. unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
  43. bool FindFlag(const char * const Tag,uint8_t &Flags,
  44. uint8_t const Flag) const;
  45. bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
  46. const char* const Start, const char* const Stop);
  47. bool FindFlag(const char *Tag,unsigned long &Flags,
  48. unsigned long Flag) const;
  49. bool static FindFlag(unsigned long &Flags, unsigned long Flag,
  50. const char* Start, const char* Stop);
  51. /** \brief searches the boundaries of the current section
  52. *
  53. * While parameter Start marks the beginning of the section, this method
  54. * will search for the first double newline in the data stream which marks
  55. * the end of the section. It also does a first pass over the content of
  56. * the section parsing it as encountered for processing later on by Find
  57. *
  58. * @param Start is the beginning of the section
  59. * @param MaxLength is the size of valid data in the stream pointed to by Start
  60. * @param Restart if enabled internal state will be cleared, otherwise it is
  61. * assumed that now more data is available in the stream and the parsing will
  62. * start were it encountered insufficent data the last time.
  63. *
  64. * @return \b true if section end was found, \b false otherwise.
  65. * Beware that internal state will be inconsistent if \b false is returned!
  66. */
  67. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  68. inline unsigned long size() const {return Stop - Section;};
  69. void Trim();
  70. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  71. /** \brief amount of Tags in the current section
  72. *
  73. * Note: if a Tag is mentioned repeatly it will be counted multiple
  74. * times, but only the last occurrence is available via Find methods.
  75. */
  76. unsigned int Count() const;
  77. bool Exists(const char* const Tag) const;
  78. void Get(const char *&Start,const char *&Stop,unsigned int I) const;
  79. inline void GetSection(const char *&Start,const char *&Stop) const
  80. {
  81. Start = Section;
  82. Stop = this->Stop;
  83. };
  84. pkgTagSection();
  85. virtual ~pkgTagSection();
  86. struct Tag
  87. {
  88. enum ActionType { REMOVE, RENAME, REWRITE } Action;
  89. std::string Name;
  90. std::string Data;
  91. static Tag Remove(std::string const &Name);
  92. static Tag Rename(std::string const &OldName, std::string const &NewName);
  93. static Tag Rewrite(std::string const &Name, std::string const &Data);
  94. private:
  95. Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
  96. Action(Action), Name(Name), Data(Data) {}
  97. };
  98. /** Write this section (with optional rewrites) to a file
  99. *
  100. * @param File to write the section to
  101. * @param Order in which tags should appear in the file
  102. * @param Rewrite is a set of tags to be renamed, rewritten and/or removed
  103. * @return \b true if successful, otherwise \b false
  104. */
  105. bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
  106. };
  107. /* For user generated file the parser should be a bit more relaxed in exchange
  108. for being a bit slower to allow comments and new lines all over the place */
  109. class pkgUserTagSection : public pkgTagSection
  110. {
  111. virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE;
  112. };
  113. class pkgTagFilePrivate;
  114. class pkgTagFile
  115. {
  116. pkgTagFilePrivate * const d;
  117. APT_HIDDEN bool Fill();
  118. APT_HIDDEN bool Resize();
  119. APT_HIDDEN bool Resize(unsigned long long const newSize);
  120. public:
  121. bool Step(pkgTagSection &Section);
  122. unsigned long Offset();
  123. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  124. void Init(FileFd * const F,unsigned long long const Size = 32*1024);
  125. pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
  126. virtual ~pkgTagFile();
  127. };
  128. extern const char **TFRewritePackageOrder;
  129. extern const char **TFRewriteSourceOrder;
  130. // Use pkgTagSection::Tag and pkgTagSection::Write() instead
  131. APT_IGNORE_DEPRECATED_PUSH
  132. struct APT_DEPRECATED TFRewriteData
  133. {
  134. const char *Tag;
  135. const char *Rewrite;
  136. const char *NewTag;
  137. };
  138. APT_DEPRECATED bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  139. TFRewriteData *Rewrite);
  140. APT_IGNORE_DEPRECATED_POP
  141. #endif