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