tagfile.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <stdio.h>
  18. #include <string>
  19. #ifndef APT_8_CLEANER_HEADERS
  20. #include <apt-pkg/fileutl.h>
  21. #endif
  22. class FileFd;
  23. class pkgTagSection
  24. {
  25. const char *Section;
  26. // We have a limit of 256 tags per section.
  27. unsigned int Indexes[256];
  28. unsigned int AlphaIndexes[0x100];
  29. unsigned int TagCount;
  30. // dpointer placeholder (for later in case we need it)
  31. void *d;
  32. /* This very simple hash function for the last 8 letters gives
  33. very good performance on the debian package files */
  34. inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
  35. {
  36. unsigned long Res = 0;
  37. for (; Text != End && *Text != ':' && *Text != 0; Text++)
  38. Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
  39. return Res & 0xFF;
  40. }
  41. protected:
  42. const char *Stop;
  43. public:
  44. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  45. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  46. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  47. bool Find(const char *Tag,unsigned int &Pos) const;
  48. std::string FindS(const char *Tag) const;
  49. signed int FindI(const char *Tag,signed long Default = 0) 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 const FindFlag(unsigned long &Flags, unsigned long Flag,
  54. const char* Start, const char* Stop);
  55. bool Scan(const char *Start,unsigned long MaxLength);
  56. inline unsigned long size() const {return Stop - Section;};
  57. void Trim();
  58. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  59. inline unsigned int Count() const {return TagCount;};
  60. bool Exists(const char* const Tag);
  61. inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
  62. {Start = Section + Indexes[I]; Stop = Section + Indexes[I+1];}
  63. inline void GetSection(const char *&Start,const char *&Stop) const
  64. {
  65. Start = Section;
  66. Stop = this->Stop;
  67. };
  68. pkgTagSection() : Section(0), TagCount(0), Stop(0), d(NULL) {};
  69. virtual ~pkgTagSection() {};
  70. };
  71. class pkgTagFilePrivate;
  72. class pkgTagFile
  73. {
  74. pkgTagFilePrivate *d;
  75. bool Fill();
  76. bool Resize();
  77. public:
  78. bool Step(pkgTagSection &Section);
  79. unsigned long Offset();
  80. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  81. pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
  82. virtual ~pkgTagFile();
  83. };
  84. /* This is the list of things to rewrite. The rewriter
  85. goes through and changes or adds each of these headers
  86. to suit. A zero forces the header to be erased, an empty string
  87. causes the old value to be used. (rewrite rule ignored) */
  88. struct TFRewriteData
  89. {
  90. const char *Tag;
  91. const char *Rewrite;
  92. const char *NewTag;
  93. };
  94. extern const char **TFRewritePackageOrder;
  95. extern const char **TFRewriteSourceOrder;
  96. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  97. TFRewriteData *Rewrite);
  98. #endif