]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.h
2f26846efd003b774b78a2870f883bcbd9aa15a0
[apt.git] / apt-pkg / tagfile.h
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
31 #ifndef APT_8_CLEANER_HEADERS
32 #include <apt-pkg/fileutl.h>
33 #endif
34
35 class FileFd;
36 class pkgTagSectionPrivate;
37 class pkgTagFilePrivate;
38
39 /** \class pkgTagSection parses a single deb822 stanza and provides various Find methods
40 * to extract the included values. It can also be used to modify and write a
41 * valid deb822 stanza optionally (re)ordering the fields inside the stanza.
42 *
43 * Beware: This class does \b NOT support (#-)comments in in- or output!
44 * If the input contains comments they have to be stripped first like pkgTagFile
45 * does with SUPPORT_COMMENTS flag set. */
46 class pkgTagSection
47 {
48 const char *Section;
49 unsigned int AlphaIndexes[0x100];
50
51 pkgTagSectionPrivate * const d;
52
53 protected:
54 const char *Stop;
55
56 public:
57
58 inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
59 inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
60
61 bool Find(const char *Tag,const char *&Start, const char *&End) const;
62 bool Find(const char *Tag,unsigned int &Pos) const;
63 std::string FindS(const char *Tag) const;
64 std::string FindRawS(const char *Tag) const;
65 signed int FindI(const char *Tag,signed long Default = 0) const;
66 bool FindB(const char *Tag, bool const &Default = false) const;
67 unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
68 bool FindFlag(const char * const Tag,uint8_t &Flags,
69 uint8_t const Flag) const;
70 bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
71 const char* const Start, const char* const Stop);
72 bool FindFlag(const char *Tag,unsigned long &Flags,
73 unsigned long Flag) const;
74 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
75 const char* Start, const char* Stop);
76
77 /** \brief searches the boundaries of the current section
78 *
79 * While parameter Start marks the beginning of the section, this method
80 * will search for the first double newline in the data stream which marks
81 * the end of the section. It also does a first pass over the content of
82 * the section parsing it as encountered for processing later on by Find
83 *
84 * @param Start is the beginning of the section
85 * @param MaxLength is the size of valid data in the stream pointed to by Start
86 * @param Restart if enabled internal state will be cleared, otherwise it is
87 * assumed that now more data is available in the stream and the parsing will
88 * start were it encountered insufficent data the last time.
89 *
90 * @return \b true if section end was found, \b false otherwise.
91 * Beware that internal state will be inconsistent if \b false is returned!
92 */
93 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
94
95 inline unsigned long size() const {return Stop - Section;};
96 void Trim();
97 virtual void TrimRecord(bool BeforeRecord, const char* &End);
98
99 /** \brief amount of Tags in the current section
100 *
101 * Note: if a Tag is mentioned repeatly it will be counted multiple
102 * times, but only the last occurrence is available via Find methods.
103 */
104 unsigned int Count() const;
105 bool Exists(const char* const Tag) const;
106
107 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
108
109 inline void GetSection(const char *&Start,const char *&Stop) const
110 {
111 Start = Section;
112 Stop = this->Stop;
113 };
114
115 pkgTagSection();
116 virtual ~pkgTagSection();
117
118 struct Tag
119 {
120 enum ActionType { REMOVE, RENAME, REWRITE } Action;
121 std::string Name;
122 std::string Data;
123
124 static Tag Remove(std::string const &Name);
125 static Tag Rename(std::string const &OldName, std::string const &NewName);
126 static Tag Rewrite(std::string const &Name, std::string const &Data);
127 private:
128 Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
129 Action(Action), Name(Name), Data(Data) {}
130 };
131
132 /** Write this section (with optional rewrites) to a file
133 *
134 * @param File to write the section to
135 * @param Order in which tags should appear in the file
136 * @param Rewrite is a set of tags to be renamed, rewritten and/or removed
137 * @return \b true if successful, otherwise \b false
138 */
139 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
140 };
141
142
143 /* For user generated file the parser should be a bit more relaxed in exchange
144 for being a bit slower to allow comments and new lines all over the place */
145 class pkgUserTagSection : public pkgTagSection
146 {
147 virtual void TrimRecord(bool BeforeRecord, const char* &End) APT_OVERRIDE;
148 };
149
150 /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing
151 * via #pkgTagSection. The default mode tries to be as fast as possible and
152 * assumes perfectly valid (machine generated) files like Packages. Support
153 * for comments e.g. needs to be enabled explicitly. */
154 class pkgTagFile
155 {
156 pkgTagFilePrivate * const d;
157
158 APT_HIDDEN bool Fill();
159 APT_HIDDEN bool Resize();
160 APT_HIDDEN bool Resize(unsigned long long const newSize);
161
162 public:
163
164 bool Step(pkgTagSection &Section);
165 unsigned long Offset();
166 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
167
168 enum Flags
169 {
170 STRICT = 0,
171 SUPPORT_COMMENTS = 1 << 0,
172 };
173
174 void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
175 void Init(FileFd * const F,unsigned long long const Size = 32*1024);
176
177 pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
178 pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
179 virtual ~pkgTagFile();
180 };
181
182 extern const char **TFRewritePackageOrder;
183 extern const char **TFRewriteSourceOrder;
184
185 APT_IGNORE_DEPRECATED_PUSH
186 struct APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") TFRewriteData
187 {
188 const char *Tag;
189 const char *Rewrite;
190 const char *NewTag;
191 };
192 APT_DEPRECATED_MSG("Use pkgTagSection::Tag and pkgTagSection::Write() instead") bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
193 TFRewriteData *Rewrite);
194 APT_IGNORE_DEPRECATED_POP
195
196 #endif