Free space check, fixed parser jump bug, added importat
[ntk/apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.23 1999/02/21 08:38:53 jgg Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
8 This uses a rotating buffer to load the package information into.
9 The scanner runs over it and isolates and indexes a single section.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/tagfile.h"
16 #endif
17
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
21
22 #include <string>
23 #include <stdio.h>
24 /*}}}*/
25
26 // TagFile::pkgTagFile - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
28 /* */
29 pkgTagFile::pkgTagFile(FileFd &Fd,unsigned long Size) : Fd(Fd), Size(Size)
30 {
31 Buffer = new char[Size];
32 Start = End = Buffer;
33 Left = Fd.Size();
34 iOffset = 0;
35 Fill();
36 }
37 /*}}}*/
38 // TagFile::Step - Advance to the next section /*{{{*/
39 // ---------------------------------------------------------------------
40 /* If the Section Scanner fails we refill the buffer and try again. */
41 bool pkgTagFile::Step(pkgTagSection &Tag)
42 {
43 if (Tag.Scan(Start,End - Start) == false)
44 {
45 if (Fill() == false)
46 return false;
47
48 if (Tag.Scan(Start,End - Start) == false)
49 return _error->Error("Unable to parse package file %s (1)",Fd.Name().c_str());
50 }
51 Start += Tag.size();
52 iOffset += Tag.size();
53
54 return true;
55 }
56 /*}}}*/
57 // TagFile::Fill - Top up the buffer /*{{{*/
58 // ---------------------------------------------------------------------
59 /* This takes the bit at the end of the buffer and puts it at the start
60 then fills the rest from the file */
61 bool pkgTagFile::Fill()
62 {
63 unsigned long EndSize = End - Start;
64
65 memmove(Buffer,Start,EndSize);
66 Start = Buffer;
67 End = Buffer + EndSize;
68
69 if (Left == 0)
70 {
71 if (EndSize <= 3)
72 return false;
73 if (Size - (End - Buffer) < 4)
74 return true;
75
76 // Append a double new line if one does not exist
77 unsigned int LineCount = 0;
78 for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--)
79 if (*E == '\n')
80 LineCount++;
81 for (; LineCount < 2; LineCount++)
82 *End++ = '\n';
83
84 return true;
85 }
86
87 // See if only a bit of the file is left
88 if (Left < Size - (End - Buffer))
89 {
90 if (Fd.Read(End,Left) == false)
91 return false;
92
93 End += Left;
94 Left = 0;
95 }
96 else
97 {
98 if (Fd.Read(End,Size - (End - Buffer)) == false)
99 return false;
100
101 Left -= Size - (End - Buffer);
102 End = Buffer + Size;
103 }
104 return true;
105 }
106 /*}}}*/
107 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This jumps to a pre-recorded file location and reads the record
110 that is there */
111 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
112 {
113 iOffset = Offset;
114 Left = Fd.Size() - Offset;
115 if (Fd.Seek(Offset) == false)
116 return false;
117 End = Start = Buffer;
118
119 if (Fill() == false)
120 return false;
121
122 if (Tag.Scan(Start,End - Start) == true)
123 return true;
124
125 // This appends a double new line (for the real eof handling)
126 if (Fill() == false)
127 return false;
128
129 if (Tag.Scan(Start,End - Start) == false)
130 return _error->Error("Unable to parse package file %s (2)",Fd.Name().c_str());
131
132 return true;
133 }
134 /*}}}*/
135 // TagSection::Scan - Scan for the end of the header information /*{{{*/
136 // ---------------------------------------------------------------------
137 /* This looks for the first double new line in the data stream. It also
138 indexes the tags in the section. This very simple hash function for the
139 first 3 letters gives very good performance on the debian package files */
140 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
141 {
142 const char *End = Start + MaxLength;
143 Stop = Section = Start;
144 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
145
146 if (Stop == 0)
147 return false;
148
149 TagCount = 0;
150 while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
151 {
152 // Start a new index and add it to the hash
153 if (isspace(Stop[0]) == 0)
154 {
155 Indexes[TagCount++] = Stop - Section;
156 unsigned char A = tolower(Stop[0]) - 'a';
157 unsigned char B = tolower(Stop[1]) - 'a';
158 unsigned char C = tolower(Stop[3]) - 'a';
159 AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
160 }
161
162 Stop = (const char *)memchr(Stop,'\n',End - Stop);
163
164 if (Stop == 0)
165 return false;
166
167 for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
168
169 // Double newline marks the end of the record
170 if (Stop+1 < End && Stop[1] == '\n')
171 {
172 Indexes[TagCount] = Stop - Section;
173 for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
174 return true;
175 }
176
177 Stop++;
178 }
179
180 return false;
181 }
182 /*}}}*/
183 // TagSection::Find - Locate a tag /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This searches the section for a tag that matches the given string. */
186 bool pkgTagSection::Find(const char *Tag,const char *&Start,
187 const char *&End)
188 {
189 unsigned int Length = strlen(Tag);
190 unsigned char A = tolower(Tag[0]) - 'a';
191 unsigned char B = tolower(Tag[1]) - 'a';
192 unsigned char C = tolower(Tag[3]) - 'a';
193 unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
194 if (I == 0)
195 return false;
196 I--;
197
198 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
199 I = (I+1)%TagCount)
200 {
201 const char *St;
202 St = Section + Indexes[I];
203 if (strncasecmp(Tag,St,Length) != 0)
204 continue;
205
206 // Make sure the colon is in the right place
207 const char *C = St + Length;
208 for (; isspace(*C) != 0; C++);
209 if (*C != ':')
210 continue;
211
212 // Strip off the gunk from the start end
213 Start = C;
214 End = Section + Indexes[I+1];
215 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
216 for (; isspace(End[-1]) != 0 && End > Start; End--);
217
218 return true;
219 }
220
221 Start = End = 0;
222 return false;
223 }
224 /*}}}*/
225 // TagSection::FindS - Find a string /*{{{*/
226 // ---------------------------------------------------------------------
227 /* */
228 string pkgTagSection::FindS(const char *Tag)
229 {
230 const char *Start;
231 const char *End;
232 if (Find(Tag,Start,End) == false)
233 return string();
234 return string(Start,End);
235 }
236 /*}}}*/
237 // TagSection::FindI - Find an integer /*{{{*/
238 // ---------------------------------------------------------------------
239 /* */
240 signed int pkgTagSection::FindI(const char *Tag,signed long Default)
241 {
242 const char *Start;
243 const char *Stop;
244 if (Find(Tag,Start,Stop) == false)
245 return Default;
246
247 // Copy it into a temp buffer so we can use strtol
248 char S[300];
249 if ((unsigned)(Stop - Start) >= sizeof(S))
250 return Default;
251 strncpy(S,Start,Stop-Start);
252 S[Stop - Start] = 0;
253
254 char *End;
255 signed long Result = strtol(S,&End,10);
256 if (S == End)
257 return Default;
258 return Result;
259 }
260 /*}}}*/
261 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
262 // ---------------------------------------------------------------------
263 /* The bits marked in Flag are masked on/off in Flags */
264 bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
265 unsigned long Flag)
266 {
267 const char *Start;
268 const char *Stop;
269 if (Find(Tag,Start,Stop) == false)
270 return true;
271
272 switch (StringToBool(string(Start,Stop)))
273 {
274 case 0:
275 Flags &= ~Flag;
276 return true;
277
278 case 1:
279 Flags |= Flag;
280 return true;
281
282 default:
283 _error->Warning("Unknown flag value");
284 return true;
285 }
286 return true;
287 }
288 /*}}}*/
289
290