]> git.saurik.com Git - apt.git/blame - apt-pkg/tagfile.cc
* cmdline/apt-get.cc, apt-pkg/cdrom.cc:
[apt.git] / apt-pkg / tagfile.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
ad00ae81 8 This uses a rotating buffer to load the package information into.
578bfd0a
AL
9 The scanner runs over it and isolates and indexes a single section.
10
11 ##################################################################### */
12 /*}}}*/
13// Include Files /*{{{*/
094a497d
AL
14#include <apt-pkg/tagfile.h>
15#include <apt-pkg/error.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
578bfd0a 17
b2e465d6
AL
18#include <apti18n.h>
19
578bfd0a
AL
20#include <string>
21#include <stdio.h>
851a45a8 22#include <ctype.h>
578bfd0a
AL
23 /*}}}*/
24
851a45a8
AL
25using std::string;
26
578bfd0a
AL
27// TagFile::pkgTagFile - Constructor /*{{{*/
28// ---------------------------------------------------------------------
29/* */
b3d44315
MV
30pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) :
31 Fd(*pFd),
2ca99a0d 32 Size(Size)
578bfd0a 33{
99c2e5ac 34 if (Fd.IsOpen() == false)
0e72dd52 35 {
2ca99a0d 36 Buffer = 0;
0e72dd52 37 Start = End = Buffer = 0;
99c2e5ac 38 Done = true;
0e72dd52
AL
39 iOffset = 0;
40 return;
41 }
42
2ca99a0d
MV
43 Buffer = new char[Size];
44 Start = End = Buffer;
45 Done = false;
dcb79bae 46 iOffset = 0;
2ca99a0d 47 Fill();
578bfd0a
AL
48}
49 /*}}}*/
b2e465d6 50// TagFile::~pkgTagFile - Destructor /*{{{*/
29f7b36c
AL
51// ---------------------------------------------------------------------
52/* */
53pkgTagFile::~pkgTagFile()
54{
2ca99a0d 55 delete [] Buffer;
29f7b36c
AL
56}
57 /*}}}*/
75c541fd
MV
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 */
63bool 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}
81e9789b 84 /*}}}*/
578bfd0a
AL
85// TagFile::Step - Advance to the next section /*{{{*/
86// ---------------------------------------------------------------------
75c541fd
MV
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 */
578bfd0a
AL
91bool pkgTagFile::Step(pkgTagSection &Tag)
92{
75c541fd 93 while (Tag.Scan(Start,End - Start) == false)
0852eaef 94 {
99c2e5ac
MV
95 if (Fill() == false)
96 return false;
97
75c541fd
MV
98 if(Tag.Scan(Start,End - Start))
99 break;
100
101 if (Resize() == false)
99c2e5ac 102 return _error->Error(_("Unable to parse package file %s (1)"),
75c541fd 103 Fd.Name().c_str());
613f9499 104 }
dcb79bae
AL
105 Start += Tag.size();
106 iOffset += Tag.size();
b2e465d6
AL
107
108 Tag.Trim();
99c2e5ac
MV
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 */
116bool 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
578bfd0a
AL
153 return true;
154}
155 /*}}}*/
ad00ae81
AL
156// TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
157// ---------------------------------------------------------------------
03e39e59
AL
158/* This jumps to a pre-recorded file location and reads the record
159 that is there */
ad00ae81
AL
160bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
161{
b2e465d6
AL
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
2ca99a0d 171 // Reposition and reload..
ad00ae81 172 iOffset = Offset;
2ca99a0d
MV
173 Done = false;
174 if (Fd.Seek(Offset) == false)
175 return false;
176 End = Start = Buffer;
99c2e5ac 177
2ca99a0d
MV
178 if (Fill() == false)
179 return false;
99c2e5ac 180
2ca99a0d
MV
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;
0852eaef 187
99c2e5ac 188 if (Tag.Scan(Start,End - Start) == false)
b2e465d6 189 return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
06bba740 190
ad00ae81
AL
191 return true;
192}
193 /*}}}*/
578bfd0a
AL
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
c1a22377 197 indexes the tags in the section. This very simple hash function for the
2c4eafff 198 last 8 letters gives very good performance on the debian package files */
b2e465d6
AL
199inline 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++)
2c4eafff 203 Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
b2e465d6
AL
204 return Res & 0xFF;
205}
206
0852eaef 207bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
578bfd0a
AL
208{
209 const char *End = Start + MaxLength;
210 Stop = Section = Start;
c1a22377 211 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
c7b5ce1c 212
2ca99a0d 213 if (Stop == 0)
0852eaef 214 return false;
81e9789b 215
578bfd0a 216 TagCount = 0;
fd71171a 217 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
578bfd0a 218 {
81e9789b
MV
219 TrimRecord(true,End);
220
90d64280 221 // Start a new index and add it to the hash
c1a22377
AL
222 if (isspace(Stop[0]) == 0)
223 {
224 Indexes[TagCount++] = Stop - Section;
b2e465d6 225 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
c1a22377 226 }
0a8e3465 227
c1a22377 228 Stop = (const char *)memchr(Stop,'\n',End - Stop);
0a8e3465 229
c1a22377 230 if (Stop == 0)
0852eaef 231 return false;
81e9789b 232
b8b7c37d 233 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
c1a22377 234
f3bcc383
AL
235 // Double newline marks the end of the record
236 if (Stop+1 < End && Stop[1] == '\n')
578bfd0a 237 {
578bfd0a 238 Indexes[TagCount] = Stop - Section;
81e9789b 239 TrimRecord(false,End);
0852eaef 240 return true;
578bfd0a
AL
241 }
242
c1a22377
AL
243 Stop++;
244 }
138d4b3d 245
0852eaef 246 return false;
578bfd0a
AL
247}
248 /*}}}*/
81e9789b
MV
249// TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
250// ---------------------------------------------------------------------
251/* There should be exactly 2 newline at the end of the record, no more. */
252void pkgTagSection::TrimRecord(bool BeforeRecord, const char*& End)
253{
254 if (BeforeRecord == true)
255 return;
256 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
257}
258 /*}}}*/
b2e465d6
AL
259// TagSection::Trim - Trim off any trailing garbage /*{{{*/
260// ---------------------------------------------------------------------
261/* There should be exactly 1 newline at the end of the buffer, no more. */
262void pkgTagSection::Trim()
263{
264 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
265}
266 /*}}}*/
578bfd0a
AL
267// TagSection::Find - Locate a tag /*{{{*/
268// ---------------------------------------------------------------------
269/* This searches the section for a tag that matches the given string. */
b2e465d6 270bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const
578bfd0a
AL
271{
272 unsigned int Length = strlen(Tag);
b2e465d6 273 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
c1a22377
AL
274 if (I == 0)
275 return false;
276 I--;
277
278 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
279 I = (I+1)%TagCount)
578bfd0a 280 {
c1a22377
AL
281 const char *St;
282 St = Section + Indexes[I];
283 if (strncasecmp(Tag,St,Length) != 0)
578bfd0a
AL
284 continue;
285
b2e465d6
AL
286 // Make sure the colon is in the right place
287 const char *C = St + Length;
288 for (; isspace(*C) != 0; C++);
289 if (*C != ':')
290 continue;
291 Pos = I;
292 return true;
293 }
294
295 Pos = 0;
296 return false;
297}
298 /*}}}*/
299// TagSection::Find - Locate a tag /*{{{*/
300// ---------------------------------------------------------------------
301/* This searches the section for a tag that matches the given string. */
302bool pkgTagSection::Find(const char *Tag,const char *&Start,
303 const char *&End) const
304{
305 unsigned int Length = strlen(Tag);
306 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
307 if (I == 0)
308 return false;
309 I--;
310
311 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
312 I = (I+1)%TagCount)
313 {
314 const char *St;
315 St = Section + Indexes[I];
316 if (strncasecmp(Tag,St,Length) != 0)
317 continue;
318
578bfd0a 319 // Make sure the colon is in the right place
c1a22377 320 const char *C = St + Length;
578bfd0a
AL
321 for (; isspace(*C) != 0; C++);
322 if (*C != ':')
323 continue;
324
325 // Strip off the gunk from the start end
326 Start = C;
327 End = Section + Indexes[I+1];
06bba740
AL
328 if (Start >= End)
329 return _error->Error("Internal parsing error");
330
578bfd0a
AL
331 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
332 for (; isspace(End[-1]) != 0 && End > Start; End--);
06bba740 333
578bfd0a
AL
334 return true;
335 }
c1a22377 336
578bfd0a
AL
337 Start = End = 0;
338 return false;
339}
340 /*}}}*/
0e66b144 341// TagSection::FindS - Find a string /*{{{*/
a05599f1
AL
342// ---------------------------------------------------------------------
343/* */
b2e465d6 344string pkgTagSection::FindS(const char *Tag) const
a05599f1
AL
345{
346 const char *Start;
347 const char *End;
348 if (Find(Tag,Start,End) == false)
349 return string();
350 return string(Start,End);
351}
352 /*}}}*/
353// TagSection::FindI - Find an integer /*{{{*/
354// ---------------------------------------------------------------------
355/* */
b2e465d6 356signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
a05599f1
AL
357{
358 const char *Start;
b0b4efb9
AL
359 const char *Stop;
360 if (Find(Tag,Start,Stop) == false)
361 return Default;
362
363 // Copy it into a temp buffer so we can use strtol
364 char S[300];
365 if ((unsigned)(Stop - Start) >= sizeof(S))
366 return Default;
367 strncpy(S,Start,Stop-Start);
368 S[Stop - Start] = 0;
369
370 char *End;
371 signed long Result = strtol(S,&End,10);
372 if (S == End)
373 return Default;
374 return Result;
375}
376 /*}}}*/
377// TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
378// ---------------------------------------------------------------------
379/* The bits marked in Flag are masked on/off in Flags */
380bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
b2e465d6 381 unsigned long Flag) const
b0b4efb9
AL
382{
383 const char *Start;
384 const char *Stop;
385 if (Find(Tag,Start,Stop) == false)
386 return true;
a05599f1 387
b0b4efb9
AL
388 switch (StringToBool(string(Start,Stop)))
389 {
390 case 0:
391 Flags &= ~Flag;
392 return true;
393
394 case 1:
395 Flags |= Flag;
396 return true;
397
398 default:
b2e465d6 399 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
b0b4efb9
AL
400 return true;
401 }
402 return true;
a05599f1
AL
403}
404 /*}}}*/
b2e465d6
AL
405// TFRewrite - Rewrite a control record /*{{{*/
406// ---------------------------------------------------------------------
407/* This writes the control record to stdout rewriting it as necessary. The
408 override map item specificies the rewriting rules to follow. This also
409 takes the time to sort the feild list. */
410
411/* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
412 array. */
413static const char *iTFRewritePackageOrder[] = {
414 "Package",
415 "Essential",
416 "Status",
417 "Priority",
418 "Section",
419 "Installed-Size",
420 "Maintainer",
47e7ebb3 421 "Original-Maintainer",
b2e465d6
AL
422 "Architecture",
423 "Source",
424 "Version",
425 "Revision", // Obsolete
426 "Config-Version", // Obsolete
427 "Replaces",
428 "Provides",
429 "Depends",
430 "Pre-Depends",
431 "Recommends",
432 "Suggests",
433 "Conflicts",
308c7d30 434 "Breaks",
b2e465d6
AL
435 "Conffiles",
436 "Filename",
437 "Size",
438 "MD5Sum",
cde41ae8
MV
439 "SHA1",
440 "SHA256",
b2e465d6
AL
441 "MSDOS-Filename", // Obsolete
442 "Description",
443 0};
444static const char *iTFRewriteSourceOrder[] = {"Package",
445 "Source",
446 "Binary",
447 "Version",
448 "Priority",
449 "Section",
450 "Maintainer",
47e7ebb3 451 "Original-Maintainer",
b2e465d6
AL
452 "Build-Depends",
453 "Build-Depends-Indep",
454 "Build-Conflicts",
455 "Build-Conflicts-Indep",
456 "Architecture",
457 "Standards-Version",
458 "Format",
459 "Directory",
460 "Files",
461 0};
462
463/* Two levels of initialization are used because gcc will set the symbol
464 size of an array to the length of the array, causing dynamic relinking
465 errors. Doing this makes the symbol size constant */
466const char **TFRewritePackageOrder = iTFRewritePackageOrder;
467const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
468
469bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
470 TFRewriteData *Rewrite)
471{
472 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
473 for (unsigned I = 0; I != 256; I++)
474 Visited[I] = 0;
475
476 // Set new tag up as necessary.
477 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
478 {
479 if (Rewrite[J].NewTag == 0)
480 Rewrite[J].NewTag = Rewrite[J].Tag;
481 }
482
483 // Write all all of the tags, in order.
484 for (unsigned int I = 0; Order[I] != 0; I++)
485 {
486 bool Rewritten = false;
487
488 // See if this is a field that needs to be rewritten
489 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
490 {
491 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
492 {
493 Visited[J] |= 2;
494 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
495 {
496 if (isspace(Rewrite[J].Rewrite[0]))
497 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
498 else
499 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
500 }
501
502 Rewritten = true;
503 break;
504 }
505 }
506
507 // See if it is in the fragment
508 unsigned Pos;
509 if (Tags.Find(Order[I],Pos) == false)
510 continue;
511 Visited[Pos] |= 1;
512
513 if (Rewritten == true)
514 continue;
515
516 /* Write out this element, taking a moment to rewrite the tag
517 in case of changes of case. */
518 const char *Start;
519 const char *Stop;
520 Tags.Get(Start,Stop,Pos);
521
522 if (fputs(Order[I],Output) < 0)
523 return _error->Errno("fputs","IO Error to output");
524 Start += strlen(Order[I]);
525 if (fwrite(Start,Stop - Start,1,Output) != 1)
526 return _error->Errno("fwrite","IO Error to output");
527 if (Stop[-1] != '\n')
528 fprintf(Output,"\n");
529 }
530
531 // Now write all the old tags that were missed.
532 for (unsigned int I = 0; I != Tags.Count(); I++)
533 {
534 if ((Visited[I] & 1) == 1)
535 continue;
536
537 const char *Start;
538 const char *Stop;
539 Tags.Get(Start,Stop,I);
540 const char *End = Start;
541 for (; End < Stop && *End != ':'; End++);
542
543 // See if this is a field that needs to be rewritten
544 bool Rewritten = false;
545 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
546 {
547 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
548 {
549 Visited[J] |= 2;
550 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
551 {
552 if (isspace(Rewrite[J].Rewrite[0]))
553 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
554 else
555 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
556 }
557
558 Rewritten = true;
559 break;
560 }
561 }
562
563 if (Rewritten == true)
564 continue;
565
566 // Write out this element
567 if (fwrite(Start,Stop - Start,1,Output) != 1)
568 return _error->Errno("fwrite","IO Error to output");
569 if (Stop[-1] != '\n')
570 fprintf(Output,"\n");
571 }
572
573 // Now write all the rewrites that were missed
574 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
575 {
576 if ((Visited[J] & 2) == 2)
577 continue;
578
579 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
580 {
581 if (isspace(Rewrite[J].Rewrite[0]))
582 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
583 else
584 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
585 }
586 }
587
588 return true;
589}
590 /*}}}*/