]>
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> |
f2adfc08 | 25 | #include <stdint.h> |
1abbce9e | 26 | |
472ff00e | 27 | #include <string> |
8710a36a DK |
28 | #include <vector> |
29 | #include <list> | |
eff0c22e JAK |
30 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
31 | #include <apt-pkg/string_view.h> | |
32 | #endif | |
472ff00e | 33 | |
b9dadc24 DK |
34 | #ifndef APT_8_CLEANER_HEADERS |
35 | #include <apt-pkg/fileutl.h> | |
36 | #endif | |
37 | ||
472ff00e | 38 | class FileFd; |
bc4ccfeb | 39 | class pkgTagSectionPrivate; |
55153bf9 | 40 | class pkgTagFilePrivate; |
472ff00e | 41 | |
55153bf9 DK |
42 | /** \class pkgTagSection parses a single deb822 stanza and provides various Find methods |
43 | * to extract the included values. It can also be used to modify and write a | |
44 | * valid deb822 stanza optionally (re)ordering the fields inside the stanza. | |
45 | * | |
46 | * Beware: This class does \b NOT support (#-)comments in in- or output! | |
47 | * If the input contains comments they have to be stripped first like pkgTagFile | |
48 | * does with SUPPORT_COMMENTS flag set. */ | |
578bfd0a AL |
49 | class pkgTagSection |
50 | { | |
51 | const char *Section; | |
3e633069 JAK |
52 | unsigned int AlphaIndexes[128]; |
53 | unsigned int BetaIndexes[128]; | |
8710a36a | 54 | |
6c55f07a | 55 | pkgTagSectionPrivate * const d; |
c176c4d0 | 56 | |
45ecab44 JAK |
57 | APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const; |
58 | #if defined(APT_PKG_EXPOSE_STRING_VIEW) | |
59 | APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const; | |
60 | APT_HIDDEN APT::StringView FindRawInternal(unsigned int Pos) const; | |
61 | #endif | |
62 | APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const; | |
63 | APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const; | |
64 | APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const; | |
65 | APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const; | |
66 | APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const; | |
67 | ||
81e9789b MV |
68 | protected: |
69 | const char *Stop; | |
70 | ||
578bfd0a | 71 | public: |
8d058ea5 | 72 | |
578bfd0a AL |
73 | inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;}; |
74 | inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;}; | |
8d058ea5 | 75 | |
eff0c22e | 76 | #if !defined(APT_PKG_EXPOSE_STRING_VIEW) || defined(APT_COMPILING_TAGFILE_COMPAT_CC) |
b2e465d6 | 77 | bool Find(const char *Tag,const char *&Start, const char *&End) const; |
c8b860fb | 78 | bool Find(const char *Tag,unsigned int &Pos) const; |
a2fdb57f MV |
79 | signed int FindI(const char *Tag,signed long Default = 0) const; |
80 | bool FindB(const char *Tag, bool const &Default = false) const; | |
e2c66de5 | 81 | unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const; |
dfe66c72 DK |
82 | bool FindFlag(const char * const Tag,uint8_t &Flags, |
83 | uint8_t const Flag) const; | |
500827ed AL |
84 | bool FindFlag(const char *Tag,unsigned long &Flags, |
85 | unsigned long Flag) const; | |
eff0c22e JAK |
86 | bool Exists(const char* const Tag) const; |
87 | #endif | |
88 | // TODO: Remove internally | |
89 | std::string FindS(const char *Tag) const; | |
90 | std::string FindRawS(const char *Tag) const; | |
91 | ||
abfd0770 JAK |
92 | // Functions for lookup with a perfect hash function |
93 | enum class Key; | |
94 | APT_HIDDEN bool Find(Key key,const char *&Start, const char *&End) const; | |
95 | APT_HIDDEN bool Find(Key key,unsigned int &Pos) const; | |
96 | APT_HIDDEN signed int FindI(Key key,signed long Default = 0) const; | |
97 | APT_HIDDEN bool FindB(Key key, bool Default = false) const; | |
98 | APT_HIDDEN unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const; | |
99 | APT_HIDDEN bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const; | |
100 | APT_HIDDEN bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const; | |
101 | APT_HIDDEN bool Exists(Key key) const; | |
eff0c22e | 102 | #ifdef APT_PKG_EXPOSE_STRING_VIEW |
abfd0770 JAK |
103 | APT_HIDDEN APT::StringView Find(Key key) const; |
104 | APT_HIDDEN APT::StringView FindRaw(Key key) const; | |
eff0c22e JAK |
105 | APT_HIDDEN bool Find(APT::StringView Tag,const char *&Start, const char *&End) const; |
106 | APT_HIDDEN bool Find(APT::StringView Tag,unsigned int &Pos) const; | |
107 | APT_HIDDEN APT::StringView Find(APT::StringView Tag) const; | |
108 | APT_HIDDEN APT::StringView FindRaw(APT::StringView Tag) const; | |
109 | APT_HIDDEN signed int FindI(APT::StringView Tag,signed long Default = 0) const; | |
110 | APT_HIDDEN bool FindB(APT::StringView, bool Default = false) const; | |
111 | APT_HIDDEN unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const; | |
112 | ||
113 | APT_HIDDEN bool FindFlag(APT::StringView Tag,uint8_t &Flags, | |
114 | uint8_t const Flag) const; | |
115 | APT_HIDDEN bool FindFlag(APT::StringView Tag,unsigned long &Flags, | |
116 | unsigned long Flag) const; | |
117 | APT_HIDDEN bool Exists(APT::StringView Tag) const; | |
118 | #endif | |
119 | ||
120 | bool static FindFlag(uint8_t &Flags, uint8_t const Flag, | |
121 | const char* const Start, const char* const Stop); | |
d64e130a | 122 | bool static FindFlag(unsigned long &Flags, unsigned long Flag, |
fe0f7911 | 123 | const char* Start, const char* Stop); |
8710a36a DK |
124 | |
125 | /** \brief searches the boundaries of the current section | |
126 | * | |
127 | * While parameter Start marks the beginning of the section, this method | |
128 | * will search for the first double newline in the data stream which marks | |
129 | * the end of the section. It also does a first pass over the content of | |
130 | * the section parsing it as encountered for processing later on by Find | |
131 | * | |
132 | * @param Start is the beginning of the section | |
133 | * @param MaxLength is the size of valid data in the stream pointed to by Start | |
134 | * @param Restart if enabled internal state will be cleared, otherwise it is | |
135 | * assumed that now more data is available in the stream and the parsing will | |
136 | * start were it encountered insufficent data the last time. | |
137 | * | |
138 | * @return \b true if section end was found, \b false otherwise. | |
139 | * Beware that internal state will be inconsistent if \b false is returned! | |
140 | */ | |
141 | APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true); | |
fa5404ab | 142 | |
b2e465d6 AL |
143 | inline unsigned long size() const {return Stop - Section;}; |
144 | void Trim(); | |
81e9789b | 145 | virtual void TrimRecord(bool BeforeRecord, const char* &End); |
8710a36a DK |
146 | |
147 | /** \brief amount of Tags in the current section | |
148 | * | |
149 | * Note: if a Tag is mentioned repeatly it will be counted multiple | |
c029a836 | 150 | * times, but only the last occurrence is available via Find methods. |
8710a36a DK |
151 | */ |
152 | unsigned int Count() const; | |
8710a36a | 153 | |
bc4ccfeb | 154 | void Get(const char *&Start,const char *&Stop,unsigned int I) const; |
8710a36a | 155 | |
b2e465d6 | 156 | inline void GetSection(const char *&Start,const char *&Stop) const |
a05599f1 AL |
157 | { |
158 | Start = Section; | |
159 | Stop = this->Stop; | |
160 | }; | |
8d058ea5 | 161 | |
b40394c0 | 162 | pkgTagSection(); |
862bafea | 163 | virtual ~pkgTagSection(); |
8d058ea5 DK |
164 | |
165 | struct Tag | |
166 | { | |
167 | enum ActionType { REMOVE, RENAME, REWRITE } Action; | |
168 | std::string Name; | |
169 | std::string Data; | |
170 | ||
171 | static Tag Remove(std::string const &Name); | |
172 | static Tag Rename(std::string const &OldName, std::string const &NewName); | |
173 | static Tag Rewrite(std::string const &Name, std::string const &Data); | |
174 | private: | |
175 | Tag(ActionType const Action, std::string const &Name, std::string const &Data) : | |
176 | Action(Action), Name(Name), Data(Data) {} | |
177 | }; | |
178 | ||
179 | /** Write this section (with optional rewrites) to a file | |
180 | * | |
181 | * @param File to write the section to | |
182 | * @param Order in which tags should appear in the file | |
3d8232bf | 183 | * @param Rewrite is a set of tags to be renamed, rewritten and/or removed |
8d058ea5 DK |
184 | * @return \b true if successful, otherwise \b false |
185 | */ | |
186 | bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const; | |
578bfd0a AL |
187 | }; |
188 | ||
81460e32 | 189 | |
f6459e64 | 190 | class APT_DEPRECATED_MSG("Use pkgTagFile with the SUPPORT_COMMENTS flag instead") pkgUserTagSection : public pkgTagSection |
81460e32 | 191 | { |
3b302846 | 192 | virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE; |
81460e32 DK |
193 | }; |
194 | ||
55153bf9 DK |
195 | /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing |
196 | * via #pkgTagSection. The default mode tries to be as fast as possible and | |
197 | * assumes perfectly valid (machine generated) files like Packages. Support | |
198 | * for comments e.g. needs to be enabled explicitly. */ | |
578bfd0a AL |
199 | class pkgTagFile |
200 | { | |
6c55f07a | 201 | pkgTagFilePrivate * const d; |
75c541fd | 202 | |
ce62f1de DK |
203 | APT_HIDDEN bool Fill(); |
204 | APT_HIDDEN bool Resize(); | |
205 | APT_HIDDEN bool Resize(unsigned long long const newSize); | |
75c541fd | 206 | |
55153bf9 | 207 | public: |
578bfd0a AL |
208 | |
209 | bool Step(pkgTagSection &Section); | |
4b2746d5 | 210 | unsigned long Offset(); |
650faab0 | 211 | bool Jump(pkgTagSection &Tag,unsigned long long Offset); |
29f7b36c | 212 | |
55153bf9 DK |
213 | enum Flags |
214 | { | |
215 | STRICT = 0, | |
216 | SUPPORT_COMMENTS = 1 << 0, | |
217 | }; | |
218 | ||
219 | void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024); | |
6c55f07a | 220 | void Init(FileFd * const F,unsigned long long const Size = 32*1024); |
feab34c5 | 221 | |
55153bf9 | 222 | pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024); |
6c55f07a | 223 | pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024); |
43fb90dc | 224 | virtual ~pkgTagFile(); |
578bfd0a AL |
225 | }; |
226 | ||
8d058ea5 DK |
227 | extern const char **TFRewritePackageOrder; |
228 | extern const char **TFRewriteSourceOrder; | |
229 | ||
8d058ea5 | 230 | APT_IGNORE_DEPRECATED_PUSH |
5dd00edb | 231 | struct APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") TFRewriteData |
b2e465d6 AL |
232 | { |
233 | const char *Tag; | |
234 | const char *Rewrite; | |
235 | const char *NewTag; | |
236 | }; | |
5dd00edb | 237 | APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[], |
b2e465d6 | 238 | TFRewriteData *Rewrite); |
8d058ea5 | 239 | APT_IGNORE_DEPRECATED_POP |
b2e465d6 | 240 | |
578bfd0a | 241 | #endif |