]> git.saurik.com Git - apt.git/blame - apt-pkg/tagfile.cc
* apt-inst/contrib/arfile.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}
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;
578bfd0a
AL
215
216 TagCount = 0;
fd71171a 217 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
578bfd0a 218 {
90d64280 219 // Start a new index and add it to the hash
c1a22377
AL
220 if (isspace(Stop[0]) == 0)
221 {
222 Indexes[TagCount++] = Stop - Section;
b2e465d6 223 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
c1a22377 224 }
0a8e3465 225
c1a22377 226 Stop = (const char *)memchr(Stop,'\n',End - Stop);
0a8e3465 227
c1a22377 228 if (Stop == 0)
0852eaef 229 return false;
138d4b3d 230
b8b7c37d 231 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
c1a22377 232
f3bcc383
AL
233 // Double newline marks the end of the record
234 if (Stop+1 < End && Stop[1] == '\n')
578bfd0a 235 {
578bfd0a 236 Indexes[TagCount] = Stop - Section;
fd71171a 237 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
0852eaef 238 return true;
578bfd0a
AL
239 }
240
c1a22377
AL
241 Stop++;
242 }
138d4b3d 243
0852eaef 244 return false;
578bfd0a
AL
245}
246 /*}}}*/
b2e465d6
AL
247// TagSection::Trim - Trim off any trailing garbage /*{{{*/
248// ---------------------------------------------------------------------
249/* There should be exactly 1 newline at the end of the buffer, no more. */
250void pkgTagSection::Trim()
251{
252 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
253}
254 /*}}}*/
578bfd0a
AL
255// TagSection::Find - Locate a tag /*{{{*/
256// ---------------------------------------------------------------------
257/* This searches the section for a tag that matches the given string. */
b2e465d6 258bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const
578bfd0a
AL
259{
260 unsigned int Length = strlen(Tag);
b2e465d6 261 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
c1a22377
AL
262 if (I == 0)
263 return false;
264 I--;
265
266 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
267 I = (I+1)%TagCount)
578bfd0a 268 {
c1a22377
AL
269 const char *St;
270 St = Section + Indexes[I];
271 if (strncasecmp(Tag,St,Length) != 0)
578bfd0a
AL
272 continue;
273
b2e465d6
AL
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. */
290bool 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
578bfd0a 307 // Make sure the colon is in the right place
c1a22377 308 const char *C = St + Length;
578bfd0a
AL
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];
06bba740
AL
316 if (Start >= End)
317 return _error->Error("Internal parsing error");
318
578bfd0a
AL
319 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
320 for (; isspace(End[-1]) != 0 && End > Start; End--);
06bba740 321
578bfd0a
AL
322 return true;
323 }
c1a22377 324
578bfd0a
AL
325 Start = End = 0;
326 return false;
327}
328 /*}}}*/
0e66b144 329// TagSection::FindS - Find a string /*{{{*/
a05599f1
AL
330// ---------------------------------------------------------------------
331/* */
b2e465d6 332string pkgTagSection::FindS(const char *Tag) const
a05599f1
AL
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/* */
b2e465d6 344signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
a05599f1
AL
345{
346 const char *Start;
b0b4efb9
AL
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 */
368bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
b2e465d6 369 unsigned long Flag) const
b0b4efb9
AL
370{
371 const char *Start;
372 const char *Stop;
373 if (Find(Tag,Start,Stop) == false)
374 return true;
a05599f1 375
b0b4efb9
AL
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:
b2e465d6 387 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
b0b4efb9
AL
388 return true;
389 }
390 return true;
a05599f1
AL
391}
392 /*}}}*/
b2e465d6
AL
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. */
402static const char *iTFRewritePackageOrder[] = {
403 "Package",
404 "Essential",
405 "Status",
406 "Priority",
407 "Section",
408 "Installed-Size",
409 "Maintainer",
47e7ebb3 410 "Original-Maintainer",
b2e465d6
AL
411 "Architecture",
412 "Source",
413 "Version",
414 "Revision", // Obsolete
415 "Config-Version", // Obsolete
416 "Replaces",
417 "Provides",
418 "Depends",
419 "Pre-Depends",
420 "Recommends",
421 "Suggests",
422 "Conflicts",
308c7d30 423 "Breaks",
b2e465d6
AL
424 "Conffiles",
425 "Filename",
426 "Size",
427 "MD5Sum",
cde41ae8
MV
428 "SHA1",
429 "SHA256",
b2e465d6
AL
430 "MSDOS-Filename", // Obsolete
431 "Description",
432 0};
433static const char *iTFRewriteSourceOrder[] = {"Package",
434 "Source",
435 "Binary",
436 "Version",
437 "Priority",
438 "Section",
439 "Maintainer",
47e7ebb3 440 "Original-Maintainer",
b2e465d6
AL
441 "Build-Depends",
442 "Build-Depends-Indep",
443 "Build-Conflicts",
444 "Build-Conflicts-Indep",
445 "Architecture",
446 "Standards-Version",
447 "Format",
448 "Directory",
449 "Files",
450 0};
451
452/* Two levels of initialization are used because gcc will set the symbol
453 size of an array to the length of the array, causing dynamic relinking
454 errors. Doing this makes the symbol size constant */
455const char **TFRewritePackageOrder = iTFRewritePackageOrder;
456const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
457
458bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
459 TFRewriteData *Rewrite)
460{
461 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
462 for (unsigned I = 0; I != 256; I++)
463 Visited[I] = 0;
464
465 // Set new tag up as necessary.
466 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
467 {
468 if (Rewrite[J].NewTag == 0)
469 Rewrite[J].NewTag = Rewrite[J].Tag;
470 }
471
472 // Write all all of the tags, in order.
473 for (unsigned int I = 0; Order[I] != 0; I++)
474 {
475 bool Rewritten = false;
476
477 // See if this is a field that needs to be rewritten
478 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
479 {
480 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
481 {
482 Visited[J] |= 2;
483 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
484 {
485 if (isspace(Rewrite[J].Rewrite[0]))
486 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
487 else
488 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
489 }
490
491 Rewritten = true;
492 break;
493 }
494 }
495
496 // See if it is in the fragment
497 unsigned Pos;
498 if (Tags.Find(Order[I],Pos) == false)
499 continue;
500 Visited[Pos] |= 1;
501
502 if (Rewritten == true)
503 continue;
504
505 /* Write out this element, taking a moment to rewrite the tag
506 in case of changes of case. */
507 const char *Start;
508 const char *Stop;
509 Tags.Get(Start,Stop,Pos);
510
511 if (fputs(Order[I],Output) < 0)
512 return _error->Errno("fputs","IO Error to output");
513 Start += strlen(Order[I]);
514 if (fwrite(Start,Stop - Start,1,Output) != 1)
515 return _error->Errno("fwrite","IO Error to output");
516 if (Stop[-1] != '\n')
517 fprintf(Output,"\n");
518 }
519
520 // Now write all the old tags that were missed.
521 for (unsigned int I = 0; I != Tags.Count(); I++)
522 {
523 if ((Visited[I] & 1) == 1)
524 continue;
525
526 const char *Start;
527 const char *Stop;
528 Tags.Get(Start,Stop,I);
529 const char *End = Start;
530 for (; End < Stop && *End != ':'; End++);
531
532 // See if this is a field that needs to be rewritten
533 bool Rewritten = false;
534 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
535 {
536 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
537 {
538 Visited[J] |= 2;
539 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
540 {
541 if (isspace(Rewrite[J].Rewrite[0]))
542 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
543 else
544 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
545 }
546
547 Rewritten = true;
548 break;
549 }
550 }
551
552 if (Rewritten == true)
553 continue;
554
555 // Write out this element
556 if (fwrite(Start,Stop - Start,1,Output) != 1)
557 return _error->Errno("fwrite","IO Error to output");
558 if (Stop[-1] != '\n')
559 fprintf(Output,"\n");
560 }
561
562 // Now write all the rewrites that were missed
563 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
564 {
565 if ((Visited[J] & 2) == 2)
566 continue;
567
568 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
569 {
570 if (isspace(Rewrite[J].Rewrite[0]))
571 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
572 else
573 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
574 }
575 }
576
577 return true;
578}
579 /*}}}*/