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