tagfile.h 3.7 KB

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