]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | /* ###################################################################### | |
4 | ||
5 | Fast scanner for RFC-822 type header information | |
6 | ||
7 | This parser handles Debian package files (and others). Their form is | |
8 | RFC-822 type header fields in groups separated by a blank line. | |
9 | ||
10 | The parser reads the file and provides methods to step linearly | |
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 | /*}}}*/ | |
19 | #ifndef PKGLIB_TAGFILE_H | |
20 | #define PKGLIB_TAGFILE_H | |
21 | ||
22 | #include <apt-pkg/macros.h> | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdint.h> | |
26 | ||
27 | #include <string> | |
28 | #include <vector> | |
29 | #include <list> | |
30 | #ifdef APT_PKG_EXPOSE_STRING_VIEW | |
31 | #include <apt-pkg/string_view.h> | |
32 | #endif | |
33 | ||
34 | #ifndef APT_8_CLEANER_HEADERS | |
35 | #include <apt-pkg/fileutl.h> | |
36 | #endif | |
37 | ||
38 | class FileFd; | |
39 | class pkgTagSectionPrivate; | |
40 | class pkgTagFilePrivate; | |
41 | ||
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. */ | |
49 | class pkgTagSection | |
50 | { | |
51 | const char *Section; | |
52 | unsigned int AlphaIndexes[0x100]; | |
53 | ||
54 | pkgTagSectionPrivate * const d; | |
55 | ||
56 | protected: | |
57 | const char *Stop; | |
58 | ||
59 | public: | |
60 | ||
61 | inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;}; | |
62 | inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;}; | |
63 | ||
64 | #if !defined(APT_PKG_EXPOSE_STRING_VIEW) || defined(APT_COMPILING_TAGFILE_COMPAT_CC) | |
65 | bool Find(const char *Tag,const char *&Start, const char *&End) const; | |
66 | bool Find(const char *Tag,unsigned int &Pos) const; | |
67 | signed int FindI(const char *Tag,signed long Default = 0) const; | |
68 | bool FindB(const char *Tag, bool const &Default = false) const; | |
69 | unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const; | |
70 | bool FindFlag(const char * const Tag,uint8_t &Flags, | |
71 | uint8_t const Flag) const; | |
72 | bool FindFlag(const char *Tag,unsigned long &Flags, | |
73 | unsigned long Flag) const; | |
74 | bool Exists(const char* const Tag) const; | |
75 | #endif | |
76 | // TODO: Remove internally | |
77 | std::string FindS(const char *Tag) const; | |
78 | std::string FindRawS(const char *Tag) const; | |
79 | ||
80 | #ifdef APT_PKG_EXPOSE_STRING_VIEW | |
81 | APT_HIDDEN bool Find(APT::StringView Tag,const char *&Start, const char *&End) const; | |
82 | APT_HIDDEN bool Find(APT::StringView Tag,unsigned int &Pos) const; | |
83 | APT_HIDDEN APT::StringView Find(APT::StringView Tag) const; | |
84 | APT_HIDDEN APT::StringView FindRaw(APT::StringView Tag) const; | |
85 | APT_HIDDEN signed int FindI(APT::StringView Tag,signed long Default = 0) const; | |
86 | APT_HIDDEN bool FindB(APT::StringView, bool Default = false) const; | |
87 | APT_HIDDEN unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const; | |
88 | ||
89 | APT_HIDDEN bool FindFlag(APT::StringView Tag,uint8_t &Flags, | |
90 | uint8_t const Flag) const; | |
91 | APT_HIDDEN bool FindFlag(APT::StringView Tag,unsigned long &Flags, | |
92 | unsigned long Flag) const; | |
93 | APT_HIDDEN bool Exists(APT::StringView Tag) const; | |
94 | #endif | |
95 | ||
96 | bool static FindFlag(uint8_t &Flags, uint8_t const Flag, | |
97 | const char* const Start, const char* const Stop); | |
98 | bool static FindFlag(unsigned long &Flags, unsigned long Flag, | |
99 | const char* Start, const char* Stop); | |
100 | ||
101 | /** \brief searches the boundaries of the current section | |
102 | * | |
103 | * While parameter Start marks the beginning of the section, this method | |
104 | * will search for the first double newline in the data stream which marks | |
105 | * the end of the section. It also does a first pass over the content of | |
106 | * the section parsing it as encountered for processing later on by Find | |
107 | * | |
108 | * @param Start is the beginning of the section | |
109 | * @param MaxLength is the size of valid data in the stream pointed to by Start | |
110 | * @param Restart if enabled internal state will be cleared, otherwise it is | |
111 | * assumed that now more data is available in the stream and the parsing will | |
112 | * start were it encountered insufficent data the last time. | |
113 | * | |
114 | * @return \b true if section end was found, \b false otherwise. | |
115 | * Beware that internal state will be inconsistent if \b false is returned! | |
116 | */ | |
117 | APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true); | |
118 | ||
119 | inline unsigned long size() const {return Stop - Section;}; | |
120 | void Trim(); | |
121 | virtual void TrimRecord(bool BeforeRecord, const char* &End); | |
122 | ||
123 | /** \brief amount of Tags in the current section | |
124 | * | |
125 | * Note: if a Tag is mentioned repeatly it will be counted multiple | |
126 | * times, but only the last occurrence is available via Find methods. | |
127 | */ | |
128 | unsigned int Count() const; | |
129 | ||
130 | void Get(const char *&Start,const char *&Stop,unsigned int I) const; | |
131 | ||
132 | inline void GetSection(const char *&Start,const char *&Stop) const | |
133 | { | |
134 | Start = Section; | |
135 | Stop = this->Stop; | |
136 | }; | |
137 | ||
138 | pkgTagSection(); | |
139 | virtual ~pkgTagSection(); | |
140 | ||
141 | struct Tag | |
142 | { | |
143 | enum ActionType { REMOVE, RENAME, REWRITE } Action; | |
144 | std::string Name; | |
145 | std::string Data; | |
146 | ||
147 | static Tag Remove(std::string const &Name); | |
148 | static Tag Rename(std::string const &OldName, std::string const &NewName); | |
149 | static Tag Rewrite(std::string const &Name, std::string const &Data); | |
150 | private: | |
151 | Tag(ActionType const Action, std::string const &Name, std::string const &Data) : | |
152 | Action(Action), Name(Name), Data(Data) {} | |
153 | }; | |
154 | ||
155 | /** Write this section (with optional rewrites) to a file | |
156 | * | |
157 | * @param File to write the section to | |
158 | * @param Order in which tags should appear in the file | |
159 | * @param Rewrite is a set of tags to be renamed, rewritten and/or removed | |
160 | * @return \b true if successful, otherwise \b false | |
161 | */ | |
162 | bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const; | |
163 | }; | |
164 | ||
165 | ||
166 | class APT_DEPRECATED_MSG("Use pkgTagFile with the SUPPORT_COMMENTS flag instead") pkgUserTagSection : public pkgTagSection | |
167 | { | |
168 | virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE; | |
169 | }; | |
170 | ||
171 | /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing | |
172 | * via #pkgTagSection. The default mode tries to be as fast as possible and | |
173 | * assumes perfectly valid (machine generated) files like Packages. Support | |
174 | * for comments e.g. needs to be enabled explicitly. */ | |
175 | class pkgTagFile | |
176 | { | |
177 | pkgTagFilePrivate * const d; | |
178 | ||
179 | APT_HIDDEN bool Fill(); | |
180 | APT_HIDDEN bool Resize(); | |
181 | APT_HIDDEN bool Resize(unsigned long long const newSize); | |
182 | ||
183 | public: | |
184 | ||
185 | bool Step(pkgTagSection &Section); | |
186 | unsigned long Offset(); | |
187 | bool Jump(pkgTagSection &Tag,unsigned long long Offset); | |
188 | ||
189 | enum Flags | |
190 | { | |
191 | STRICT = 0, | |
192 | SUPPORT_COMMENTS = 1 << 0, | |
193 | }; | |
194 | ||
195 | void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024); | |
196 | void Init(FileFd * const F,unsigned long long const Size = 32*1024); | |
197 | ||
198 | pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024); | |
199 | pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024); | |
200 | virtual ~pkgTagFile(); | |
201 | }; | |
202 | ||
203 | extern const char **TFRewritePackageOrder; | |
204 | extern const char **TFRewriteSourceOrder; | |
205 | ||
206 | APT_IGNORE_DEPRECATED_PUSH | |
207 | struct APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") TFRewriteData | |
208 | { | |
209 | const char *Tag; | |
210 | const char *Rewrite; | |
211 | const char *NewTag; | |
212 | }; | |
213 | APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[], | |
214 | TFRewriteData *Rewrite); | |
215 | APT_IGNORE_DEPRECATED_POP | |
216 | ||
217 | #endif |