]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
578bfd0a AL |
3 | /* ###################################################################### |
4 | ||
5 | Fast scanner for RFC-822 type header information | |
6 | ||
7 | This parser handles Debian package files (and others). Their form is | |
b2e465d6 | 8 | RFC-822 type header fields in groups separated by a blank line. |
578bfd0a | 9 | |
6fc33863 | 10 | The parser reads the file and provides methods to step linearly |
578bfd0a AL |
11 | over it or to jump to a pre-recorded start point and read that record. |
12 | ||
13 | A second class is used to perform pre-parsing of the record. It works | |
14 | by indexing the start of each header field and providing lookup | |
15 | functions for header fields. | |
16 | ||
17 | ##################################################################### */ | |
18 | /*}}}*/ | |
578bfd0a AL |
19 | #ifndef PKGLIB_TAGFILE_H |
20 | #define PKGLIB_TAGFILE_H | |
21 | ||
ce62f1de DK |
22 | #include <apt-pkg/macros.h> |
23 | ||
b2e465d6 | 24 | #include <stdio.h> |
1abbce9e | 25 | |
472ff00e | 26 | #include <string> |
8710a36a DK |
27 | #include <vector> |
28 | #include <list> | |
472ff00e | 29 | |
b9dadc24 DK |
30 | #ifndef APT_8_CLEANER_HEADERS |
31 | #include <apt-pkg/fileutl.h> | |
32 | #endif | |
33 | ||
472ff00e | 34 | class FileFd; |
bc4ccfeb | 35 | class pkgTagSectionPrivate; |
472ff00e | 36 | |
578bfd0a AL |
37 | class pkgTagSection |
38 | { | |
39 | const char *Section; | |
bc4ccfeb DK |
40 | // We have a limit of 256 tags per section with the old abi |
41 | #if APT_PKG_ABI < 413 | |
42 | APT_DEPRECATED unsigned int Indexes[256]; | |
43 | #endif | |
44 | unsigned int AlphaIndexes[0x100]; | |
45 | #if APT_PKG_ABI < 413 | |
46 | APT_DEPRECATED unsigned int TagCount; | |
47 | #endif | |
8710a36a | 48 | |
6c55f07a | 49 | pkgTagSectionPrivate * const d; |
c176c4d0 | 50 | |
81e9789b MV |
51 | protected: |
52 | const char *Stop; | |
53 | ||
578bfd0a | 54 | public: |
8d058ea5 | 55 | |
578bfd0a AL |
56 | inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;}; |
57 | inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;}; | |
8d058ea5 | 58 | |
b2e465d6 | 59 | bool Find(const char *Tag,const char *&Start, const char *&End) const; |
c8b860fb | 60 | bool Find(const char *Tag,unsigned int &Pos) const; |
8f3ba4e8 | 61 | std::string FindS(const char *Tag) const; |
8d058ea5 | 62 | std::string FindRawS(const char *Tag) const; |
a2fdb57f MV |
63 | signed int FindI(const char *Tag,signed long Default = 0) const; |
64 | bool FindB(const char *Tag, bool const &Default = false) const; | |
e2c66de5 | 65 | unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const; |
500827ed AL |
66 | bool FindFlag(const char *Tag,unsigned long &Flags, |
67 | unsigned long Flag) const; | |
d64e130a | 68 | bool static FindFlag(unsigned long &Flags, unsigned long Flag, |
fe0f7911 | 69 | const char* Start, const char* Stop); |
8710a36a DK |
70 | |
71 | /** \brief searches the boundaries of the current section | |
72 | * | |
73 | * While parameter Start marks the beginning of the section, this method | |
74 | * will search for the first double newline in the data stream which marks | |
75 | * the end of the section. It also does a first pass over the content of | |
76 | * the section parsing it as encountered for processing later on by Find | |
77 | * | |
78 | * @param Start is the beginning of the section | |
79 | * @param MaxLength is the size of valid data in the stream pointed to by Start | |
80 | * @param Restart if enabled internal state will be cleared, otherwise it is | |
81 | * assumed that now more data is available in the stream and the parsing will | |
82 | * start were it encountered insufficent data the last time. | |
83 | * | |
84 | * @return \b true if section end was found, \b false otherwise. | |
85 | * Beware that internal state will be inconsistent if \b false is returned! | |
86 | */ | |
fa5404ab | 87 | #if APT_PKG_ABI >= 413 |
8710a36a | 88 | APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true); |
fa5404ab DK |
89 | #else |
90 | APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart); | |
91 | APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength); | |
92 | #endif | |
93 | ||
b2e465d6 AL |
94 | inline unsigned long size() const {return Stop - Section;}; |
95 | void Trim(); | |
81e9789b | 96 | virtual void TrimRecord(bool BeforeRecord, const char* &End); |
8710a36a DK |
97 | |
98 | /** \brief amount of Tags in the current section | |
99 | * | |
100 | * Note: if a Tag is mentioned repeatly it will be counted multiple | |
c029a836 | 101 | * times, but only the last occurrence is available via Find methods. |
8710a36a DK |
102 | */ |
103 | unsigned int Count() const; | |
02e20767 | 104 | #if APT_PKG_ABI >= 413 |
8710a36a | 105 | bool Exists(const char* const Tag) const; |
02e20767 DK |
106 | #else |
107 | bool Exists(const char* const Tag); | |
108 | #endif | |
8710a36a | 109 | |
bc4ccfeb | 110 | void Get(const char *&Start,const char *&Stop,unsigned int I) const; |
8710a36a | 111 | |
b2e465d6 | 112 | inline void GetSection(const char *&Start,const char *&Stop) const |
a05599f1 AL |
113 | { |
114 | Start = Section; | |
115 | Stop = this->Stop; | |
116 | }; | |
8d058ea5 | 117 | |
b40394c0 | 118 | pkgTagSection(); |
862bafea | 119 | virtual ~pkgTagSection(); |
8d058ea5 DK |
120 | |
121 | struct Tag | |
122 | { | |
123 | enum ActionType { REMOVE, RENAME, REWRITE } Action; | |
124 | std::string Name; | |
125 | std::string Data; | |
126 | ||
127 | static Tag Remove(std::string const &Name); | |
128 | static Tag Rename(std::string const &OldName, std::string const &NewName); | |
129 | static Tag Rewrite(std::string const &Name, std::string const &Data); | |
130 | private: | |
131 | Tag(ActionType const Action, std::string const &Name, std::string const &Data) : | |
132 | Action(Action), Name(Name), Data(Data) {} | |
133 | }; | |
134 | ||
135 | /** Write this section (with optional rewrites) to a file | |
136 | * | |
137 | * @param File to write the section to | |
138 | * @param Order in which tags should appear in the file | |
3d8232bf | 139 | * @param Rewrite is a set of tags to be renamed, rewritten and/or removed |
8d058ea5 DK |
140 | * @return \b true if successful, otherwise \b false |
141 | */ | |
142 | bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const; | |
578bfd0a AL |
143 | }; |
144 | ||
1abbce9e | 145 | class pkgTagFilePrivate; |
578bfd0a AL |
146 | class pkgTagFile |
147 | { | |
6c55f07a | 148 | pkgTagFilePrivate * const d; |
75c541fd | 149 | |
ce62f1de DK |
150 | APT_HIDDEN bool Fill(); |
151 | APT_HIDDEN bool Resize(); | |
152 | APT_HIDDEN bool Resize(unsigned long long const newSize); | |
75c541fd | 153 | |
578bfd0a AL |
154 | public: |
155 | ||
156 | bool Step(pkgTagSection &Section); | |
4b2746d5 | 157 | unsigned long Offset(); |
650faab0 | 158 | bool Jump(pkgTagSection &Tag,unsigned long long Offset); |
29f7b36c | 159 | |
6c55f07a | 160 | void Init(FileFd * const F,unsigned long long const Size = 32*1024); |
feab34c5 | 161 | |
6c55f07a | 162 | pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024); |
43fb90dc | 163 | virtual ~pkgTagFile(); |
578bfd0a AL |
164 | }; |
165 | ||
8d058ea5 DK |
166 | extern const char **TFRewritePackageOrder; |
167 | extern const char **TFRewriteSourceOrder; | |
168 | ||
169 | // Use pkgTagSection::Tag and pkgTagSection::Write() instead | |
170 | APT_IGNORE_DEPRECATED_PUSH | |
171 | struct APT_DEPRECATED TFRewriteData | |
b2e465d6 AL |
172 | { |
173 | const char *Tag; | |
174 | const char *Rewrite; | |
175 | const char *NewTag; | |
176 | }; | |
8d058ea5 | 177 | APT_DEPRECATED bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[], |
b2e465d6 | 178 | TFRewriteData *Rewrite); |
8d058ea5 | 179 | APT_IGNORE_DEPRECATED_POP |
b2e465d6 | 180 | |
578bfd0a | 181 | #endif |