tagfile.h 5.9 KB

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