tagfile.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 pkgTagSection
  27. {
  28. const char *Section;
  29. struct TagData {
  30. unsigned int StartTag;
  31. unsigned int EndTag;
  32. unsigned int StartValue;
  33. unsigned int NextInBucket;
  34. TagData(unsigned int const StartTag) : StartTag(StartTag), NextInBucket(0) {}
  35. };
  36. std::vector<TagData> Tags;
  37. unsigned int LookupTable[0x100];
  38. // dpointer placeholder (for later in case we need it)
  39. void *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. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  72. inline unsigned long size() const {return Stop - Section;};
  73. void Trim();
  74. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  75. /** \brief amount of Tags in the current section
  76. *
  77. * Note: if a Tag is mentioned repeatly it will be counted multiple
  78. * times, but only the last occurance is available via Find methods.
  79. */
  80. unsigned int Count() const;
  81. bool Exists(const char* const Tag) const;
  82. inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
  83. {Start = Section + Tags[I].StartTag; Stop = Section + Tags[I+1].StartTag;}
  84. inline void GetSection(const char *&Start,const char *&Stop) const
  85. {
  86. Start = Section;
  87. Stop = this->Stop;
  88. };
  89. pkgTagSection();
  90. virtual ~pkgTagSection() {};
  91. };
  92. class pkgTagFilePrivate;
  93. class pkgTagFile
  94. {
  95. pkgTagFilePrivate *d;
  96. APT_HIDDEN bool Fill();
  97. APT_HIDDEN bool Resize();
  98. APT_HIDDEN bool Resize(unsigned long long const newSize);
  99. public:
  100. bool Step(pkgTagSection &Section);
  101. unsigned long Offset();
  102. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  103. void Init(FileFd *F,unsigned long long Size = 32*1024);
  104. pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
  105. virtual ~pkgTagFile();
  106. };
  107. /* This is the list of things to rewrite. The rewriter
  108. goes through and changes or adds each of these headers
  109. to suit. A zero forces the header to be erased, an empty string
  110. causes the old value to be used. (rewrite rule ignored) */
  111. struct TFRewriteData
  112. {
  113. const char *Tag;
  114. const char *Rewrite;
  115. const char *NewTag;
  116. };
  117. extern const char **TFRewritePackageOrder;
  118. extern const char **TFRewriteSourceOrder;
  119. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  120. TFRewriteData *Rewrite);
  121. #endif