]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
* merged from apt--mvo
[apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz 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 #include <apt-pkg/tagfile.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/strutl.h>
17
18 #include <apti18n.h>
19
20 #include <string>
21 #include <stdio.h>
22 #include <ctype.h>
23 /*}}}*/
24
25 using std::string;
26
27 // TagFile::pkgTagFile - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) :
31 Fd(*pFd),
32 Size(Size)
33 {
34 if (Fd.IsOpen() == false)
35 {
36 Buffer = 0;
37 Start = End = Buffer = 0;
38 Done = true;
39 iOffset = 0;
40 return;
41 }
42
43 Buffer = new char[Size];
44 Start = End = Buffer;
45 Done = false;
46 iOffset = 0;
47 Fill();
48 }
49 /*}}}*/
50 // TagFile::~pkgTagFile - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 pkgTagFile::~pkgTagFile()
54 {
55 delete [] Buffer;
56 }
57 /*}}}*/
58 // TagFile::Resize - Resize the internal buffer /*{{{*/
59 // ---------------------------------------------------------------------
60 /* Resize the internal buffer (double it in size). Fail if a maximum size
61 * size is reached.
62 */
63 bool pkgTagFile::Resize()
64 {
65 char *tmp;
66 unsigned long EndSize = End - Start;
67
68 // fail is the buffer grows too big
69 if(Size > 1024*1024+1)
70 return false;
71
72 // get new buffer and use it
73 tmp = new char[2*Size];
74 memcpy(tmp, Buffer, Size);
75 Size = Size*2;
76 delete [] Buffer;
77 Buffer = tmp;
78
79 // update the start/end pointers to the new buffer
80 Start = Buffer;
81 End = Start + EndSize;
82 return true;
83 }
84
85 // TagFile::Step - Advance to the next section /*{{{*/
86 // ---------------------------------------------------------------------
87 /* If the Section Scanner fails we refill the buffer and try again.
88 * If that fails too, double the buffer size and try again until a
89 * maximum buffer is reached.
90 */
91 bool pkgTagFile::Step(pkgTagSection &Tag)
92 {
93 while (Tag.Scan(Start,End - Start) == false)
94 {
95 if (Fill() == false)
96 return false;
97
98 if(Tag.Scan(Start,End - Start))
99 break;
100
101 if (Resize() == false)
102 return _error->Error(_("Unable to parse package file %s (1)"),
103 Fd.Name().c_str());
104 }
105 Start += Tag.size();
106 iOffset += Tag.size();
107
108 Tag.Trim();
109 return true;
110 }
111 /*}}}*/
112 // TagFile::Fill - Top up the buffer /*{{{*/
113 // ---------------------------------------------------------------------
114 /* This takes the bit at the end of the buffer and puts it at the start
115 then fills the rest from the file */
116 bool pkgTagFile::Fill()
117 {
118 unsigned long EndSize = End - Start;
119 unsigned long Actual = 0;
120
121 memmove(Buffer,Start,EndSize);
122 Start = Buffer;
123 End = Buffer + EndSize;
124
125 if (Done == false)
126 {
127 // See if only a bit of the file is left
128 if (Fd.Read(End,Size - (End - Buffer),&Actual) == false)
129 return false;
130 if (Actual != Size - (End - Buffer))
131 Done = true;
132 End += Actual;
133 }
134
135 if (Done == true)
136 {
137 if (EndSize <= 3 && Actual == 0)
138 return false;
139 if (Size - (End - Buffer) < 4)
140 return true;
141
142 // Append a double new line if one does not exist
143 unsigned int LineCount = 0;
144 for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--)
145 if (*E == '\n')
146 LineCount++;
147 for (; LineCount < 2; LineCount++)
148 *End++ = '\n';
149
150 return true;
151 }
152
153 return true;
154 }
155 /*}}}*/
156 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This jumps to a pre-recorded file location and reads the record
159 that is there */
160 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
161 {
162 // We are within a buffer space of the next hit..
163 if (Offset >= iOffset && iOffset + (End - Start) > Offset)
164 {
165 unsigned long Dist = Offset - iOffset;
166 Start += Dist;
167 iOffset += Dist;
168 return Step(Tag);
169 }
170
171 // Reposition and reload..
172 iOffset = Offset;
173 Done = false;
174 if (Fd.Seek(Offset) == false)
175 return false;
176 End = Start = Buffer;
177
178 if (Fill() == false)
179 return false;
180
181 if (Tag.Scan(Start,End - Start) == true)
182 return true;
183
184 // This appends a double new line (for the real eof handling)
185 if (Fill() == false)
186 return false;
187
188 if (Tag.Scan(Start,End - Start) == false)
189 return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
190
191 return true;
192 }
193 /*}}}*/
194 // TagSection::Scan - Scan for the end of the header information /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This looks for the first double new line in the data stream. It also
197 indexes the tags in the section. This very simple hash function for the
198 last 8 letters gives very good performance on the debian package files */
199 inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
200 {
201 unsigned long Res = 0;
202 for (; Text != End && *Text != ':' && *Text != 0; Text++)
203 Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
204 return Res & 0xFF;
205 }
206
207 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
208 {
209 const char *End = Start + MaxLength;
210 Stop = Section = Start;
211 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
212
213 if (Stop == 0)
214 return false;
215
216 TagCount = 0;
217 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
218 {
219 // Start a new index and add it to the hash
220 if (isspace(Stop[0]) == 0)
221 {
222 Indexes[TagCount++] = Stop - Section;
223 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
224 }
225
226 Stop = (const char *)memchr(Stop,'\n',End - Stop);
227
228 if (Stop == 0)
229 return false;
230
231 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
232
233 // Double newline marks the end of the record
234 if (Stop+1 < End && Stop[1] == '\n')
235 {
236 Indexes[TagCount] = Stop - Section;
237 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
238 return true;
239 }
240
241 Stop++;
242 }
243
244 return false;
245 }
246 /*}}}*/
247 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
248 // ---------------------------------------------------------------------
249 /* There should be exactly 1 newline at the end of the buffer, no more. */
250 void pkgTagSection::Trim()
251 {
252 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
253 }
254 /*}}}*/
255 // TagSection::Find - Locate a tag /*{{{*/
256 // ---------------------------------------------------------------------
257 /* This searches the section for a tag that matches the given string. */
258 bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const
259 {
260 unsigned int Length = strlen(Tag);
261 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
262 if (I == 0)
263 return false;
264 I--;
265
266 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
267 I = (I+1)%TagCount)
268 {
269 const char *St;
270 St = Section + Indexes[I];
271 if (strncasecmp(Tag,St,Length) != 0)
272 continue;
273
274 // Make sure the colon is in the right place
275 const char *C = St + Length;
276 for (; isspace(*C) != 0; C++);
277 if (*C != ':')
278 continue;
279 Pos = I;
280 return true;
281 }
282
283 Pos = 0;
284 return false;
285 }
286 /*}}}*/
287 // TagSection::Find - Locate a tag /*{{{*/
288 // ---------------------------------------------------------------------
289 /* This searches the section for a tag that matches the given string. */
290 bool pkgTagSection::Find(const char *Tag,const char *&Start,
291 const char *&End) const
292 {
293 unsigned int Length = strlen(Tag);
294 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
295 if (I == 0)
296 return false;
297 I--;
298
299 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
300 I = (I+1)%TagCount)
301 {
302 const char *St;
303 St = Section + Indexes[I];
304 if (strncasecmp(Tag,St,Length) != 0)
305 continue;
306
307 // Make sure the colon is in the right place
308 const char *C = St + Length;
309 for (; isspace(*C) != 0; C++);
310 if (*C != ':')
311 continue;
312
313 // Strip off the gunk from the start end
314 Start = C;
315 End = Section + Indexes[I+1];
316 if (Start >= End)
317 return _error->Error("Internal parsing error");
318
319 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
320 for (; isspace(End[-1]) != 0 && End > Start; End--);
321
322 return true;
323 }
324
325 Start = End = 0;
326 return false;
327 }
328 /*}}}*/
329 // TagSection::FindS - Find a string /*{{{*/
330 // ---------------------------------------------------------------------
331 /* */
332 string pkgTagSection::FindS(const char *Tag) const
333 {
334 const char *Start;
335 const char *End;
336 if (Find(Tag,Start,End) == false)
337 return string();
338 return string(Start,End);
339 }
340 /*}}}*/
341 // TagSection::FindI - Find an integer /*{{{*/
342 // ---------------------------------------------------------------------
343 /* */
344 signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
345 {
346 const char *Start;
347 const char *Stop;
348 if (Find(Tag,Start,Stop) == false)
349 return Default;
350
351 // Copy it into a temp buffer so we can use strtol
352 char S[300];
353 if ((unsigned)(Stop - Start) >= sizeof(S))
354 return Default;
355 strncpy(S,Start,Stop-Start);
356 S[Stop - Start] = 0;
357
358 char *End;
359 signed long Result = strtol(S,&End,10);
360 if (S == End)
361 return Default;
362 return Result;
363 }
364 /*}}}*/
365 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
366 // ---------------------------------------------------------------------
367 /* The bits marked in Flag are masked on/off in Flags */
368 bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
369 unsigned long Flag) const
370 {
371 const char *Start;
372 const char *Stop;
373 if (Find(Tag,Start,Stop) == false)
374 return true;
375
376 switch (StringToBool(string(Start,Stop)))
377 {
378 case 0:
379 Flags &= ~Flag;
380 return true;
381
382 case 1:
383 Flags |= Flag;
384 return true;
385
386 default:
387 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
388 return true;
389 }
390 return true;
391 }
392 /*}}}*/
393
394 // TFRewrite - Rewrite a control record /*{{{*/
395 // ---------------------------------------------------------------------
396 /* This writes the control record to stdout rewriting it as necessary. The
397 override map item specificies the rewriting rules to follow. This also
398 takes the time to sort the feild list. */
399
400 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
401 array. */
402 static const char *iTFRewritePackageOrder[] = {
403 "Package",
404 "Essential",
405 "Status",
406 "Priority",
407 "Section",
408 "Installed-Size",
409 "Maintainer",
410 "Architecture",
411 "Source",
412 "Version",
413 "Revision", // Obsolete
414 "Config-Version", // Obsolete
415 "Replaces",
416 "Provides",
417 "Depends",
418 "Pre-Depends",
419 "Recommends",
420 "Suggests",
421 "Conflicts",
422 "Breaks",
423 "Conffiles",
424 "Filename",
425 "Size",
426 "MD5Sum",
427 "SHA1",
428 "SHA256",
429 "MSDOS-Filename", // Obsolete
430 "Description",
431 0};
432 static const char *iTFRewriteSourceOrder[] = {"Package",
433 "Source",
434 "Binary",
435 "Version",
436 "Priority",
437 "Section",
438 "Maintainer",
439 "Build-Depends",
440 "Build-Depends-Indep",
441 "Build-Conflicts",
442 "Build-Conflicts-Indep",
443 "Architecture",
444 "Standards-Version",
445 "Format",
446 "Directory",
447 "Files",
448 0};
449
450 /* Two levels of initialization are used because gcc will set the symbol
451 size of an array to the length of the array, causing dynamic relinking
452 errors. Doing this makes the symbol size constant */
453 const char **TFRewritePackageOrder = iTFRewritePackageOrder;
454 const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
455
456 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
457 TFRewriteData *Rewrite)
458 {
459 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
460 for (unsigned I = 0; I != 256; I++)
461 Visited[I] = 0;
462
463 // Set new tag up as necessary.
464 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
465 {
466 if (Rewrite[J].NewTag == 0)
467 Rewrite[J].NewTag = Rewrite[J].Tag;
468 }
469
470 // Write all all of the tags, in order.
471 for (unsigned int I = 0; Order[I] != 0; I++)
472 {
473 bool Rewritten = false;
474
475 // See if this is a field that needs to be rewritten
476 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
477 {
478 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
479 {
480 Visited[J] |= 2;
481 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
482 {
483 if (isspace(Rewrite[J].Rewrite[0]))
484 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
485 else
486 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
487 }
488
489 Rewritten = true;
490 break;
491 }
492 }
493
494 // See if it is in the fragment
495 unsigned Pos;
496 if (Tags.Find(Order[I],Pos) == false)
497 continue;
498 Visited[Pos] |= 1;
499
500 if (Rewritten == true)
501 continue;
502
503 /* Write out this element, taking a moment to rewrite the tag
504 in case of changes of case. */
505 const char *Start;
506 const char *Stop;
507 Tags.Get(Start,Stop,Pos);
508
509 if (fputs(Order[I],Output) < 0)
510 return _error->Errno("fputs","IO Error to output");
511 Start += strlen(Order[I]);
512 if (fwrite(Start,Stop - Start,1,Output) != 1)
513 return _error->Errno("fwrite","IO Error to output");
514 if (Stop[-1] != '\n')
515 fprintf(Output,"\n");
516 }
517
518 // Now write all the old tags that were missed.
519 for (unsigned int I = 0; I != Tags.Count(); I++)
520 {
521 if ((Visited[I] & 1) == 1)
522 continue;
523
524 const char *Start;
525 const char *Stop;
526 Tags.Get(Start,Stop,I);
527 const char *End = Start;
528 for (; End < Stop && *End != ':'; End++);
529
530 // See if this is a field that needs to be rewritten
531 bool Rewritten = false;
532 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
533 {
534 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
535 {
536 Visited[J] |= 2;
537 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
538 {
539 if (isspace(Rewrite[J].Rewrite[0]))
540 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
541 else
542 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
543 }
544
545 Rewritten = true;
546 break;
547 }
548 }
549
550 if (Rewritten == true)
551 continue;
552
553 // Write out this element
554 if (fwrite(Start,Stop - Start,1,Output) != 1)
555 return _error->Errno("fwrite","IO Error to output");
556 if (Stop[-1] != '\n')
557 fprintf(Output,"\n");
558 }
559
560 // Now write all the rewrites that were missed
561 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
562 {
563 if ((Visited[J] & 2) == 2)
564 continue;
565
566 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
567 {
568 if (isspace(Rewrite[J].Rewrite[0]))
569 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
570 else
571 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
572 }
573 }
574
575 return true;
576 }
577 /*}}}*/