tagfile.h 4.9 KB

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