tagfile.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.h,v 1.20 2003/05/19 17:13:57 doogie Exp $
  4. /* ######################################################################
  5. Fast scanner for RFC-822 type header information
  6. This parser handles Debian package files (and others). Their form is
  7. RFC-822 type header fields in groups separated by a blank line.
  8. The parser reads the file and provides methods to step linearly
  9. over it or to jump to a pre-recorded start point and read that record.
  10. A second class is used to perform pre-parsing of the record. It works
  11. by indexing the start of each header field and providing lookup
  12. functions for header fields.
  13. ##################################################################### */
  14. /*}}}*/
  15. #ifndef PKGLIB_TAGFILE_H
  16. #define PKGLIB_TAGFILE_H
  17. #include <apt-pkg/macros.h>
  18. #include <stdio.h>
  19. #include <string>
  20. #include <vector>
  21. #include <list>
  22. #ifndef APT_8_CLEANER_HEADERS
  23. #include <apt-pkg/fileutl.h>
  24. #endif
  25. class FileFd;
  26. class pkgTagSectionPrivate;
  27. class pkgTagSection
  28. {
  29. const char *Section;
  30. // We have a limit of 256 tags per section with the old abi
  31. #if APT_PKG_ABI < 413
  32. APT_DEPRECATED unsigned int Indexes[256];
  33. #endif
  34. unsigned int AlphaIndexes[0x100];
  35. #if APT_PKG_ABI < 413
  36. APT_DEPRECATED unsigned int TagCount;
  37. #endif
  38. // dpointer placeholder (for later in case we need it)
  39. pkgTagSectionPrivate *d;
  40. protected:
  41. const char *Stop;
  42. public:
  43. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  44. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  45. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  46. bool Find(const char *Tag,unsigned int &Pos) const;
  47. std::string FindS(const char *Tag) const;
  48. signed int FindI(const char *Tag,signed long Default = 0) const;
  49. bool FindB(const char *Tag, bool const &Default = false) const;
  50. unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
  51. bool FindFlag(const char *Tag,unsigned long &Flags,
  52. unsigned long Flag) const;
  53. bool static FindFlag(unsigned long &Flags, unsigned long Flag,
  54. const char* Start, const char* Stop);
  55. /** \brief searches the boundaries of the current section
  56. *
  57. * While parameter Start marks the beginning of the section, this method
  58. * will search for the first double newline in the data stream which marks
  59. * the end of the section. It also does a first pass over the content of
  60. * the section parsing it as encountered for processing later on by Find
  61. *
  62. * @param Start is the beginning of the section
  63. * @param MaxLength is the size of valid data in the stream pointed to by Start
  64. * @param Restart if enabled internal state will be cleared, otherwise it is
  65. * assumed that now more data is available in the stream and the parsing will
  66. * start were it encountered insufficent data the last time.
  67. *
  68. * @return \b true if section end was found, \b false otherwise.
  69. * Beware that internal state will be inconsistent if \b false is returned!
  70. */
  71. #if APT_PKG_ABI >= 413
  72. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  73. #else
  74. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart);
  75. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength);
  76. #endif
  77. inline unsigned long size() const {return Stop - Section;};
  78. void Trim();
  79. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  80. /** \brief amount of Tags in the current section
  81. *
  82. * Note: if a Tag is mentioned repeatly it will be counted multiple
  83. * times, but only the last occurrence is available via Find methods.
  84. */
  85. unsigned int Count() const;
  86. #if APT_PKG_ABI >= 413
  87. bool Exists(const char* const Tag) const;
  88. #else
  89. bool Exists(const char* const Tag);
  90. #endif
  91. void Get(const char *&Start,const char *&Stop,unsigned int I) const;
  92. inline void GetSection(const char *&Start,const char *&Stop) const
  93. {
  94. Start = Section;
  95. Stop = this->Stop;
  96. };
  97. pkgTagSection();
  98. virtual ~pkgTagSection();
  99. };
  100. class pkgTagFilePrivate;
  101. class pkgTagFile
  102. {
  103. pkgTagFilePrivate *d;
  104. APT_HIDDEN bool Fill();
  105. APT_HIDDEN bool Resize();
  106. APT_HIDDEN bool Resize(unsigned long long const newSize);
  107. public:
  108. bool Step(pkgTagSection &Section);
  109. unsigned long Offset();
  110. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  111. void Init(FileFd *F,unsigned long long Size = 32*1024);
  112. pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
  113. virtual ~pkgTagFile();
  114. };
  115. /* This is the list of things to rewrite. The rewriter
  116. goes through and changes or adds each of these headers
  117. to suit. A zero forces the header to be erased, an empty string
  118. causes the old value to be used. (rewrite rule ignored) */
  119. struct TFRewriteData
  120. {
  121. const char *Tag;
  122. const char *Rewrite;
  123. const char *NewTag;
  124. };
  125. extern const char **TFRewritePackageOrder;
  126. extern const char **TFRewriteSourceOrder;
  127. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  128. TFRewriteData *Rewrite);
  129. #endif