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