]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
3b491fcd2aca8648e1d73fbfd4529b4ff163f29f
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
4 /* ######################################################################
6 Fast scanner for RFC-822 type header information
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.
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/strutl.h>
29 class pkgTagFilePrivate
32 pkgTagFilePrivate(FileFd
*pFd
, unsigned long Size
) : Fd(*pFd
), Size(Size
)
40 unsigned long iOffset
;
44 // TagFile::pkgTagFile - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
47 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long Size
)
49 d
= new pkgTagFilePrivate(pFd
, Size
);
51 if (d
->Fd
.IsOpen() == false)
53 d
->Start
= d
->End
= d
->Buffer
= 0;
59 d
->Buffer
= new char[Size
];
60 d
->Start
= d
->End
= d
->Buffer
;
66 // TagFile::~pkgTagFile - Destructor /*{{{*/
67 // ---------------------------------------------------------------------
69 pkgTagFile::~pkgTagFile()
75 // TagFile::Offset - Return the current offset in the buffer /*{{{*/
76 unsigned long pkgTagFile::Offset()
81 // TagFile::Resize - Resize the internal buffer /*{{{*/
82 // ---------------------------------------------------------------------
83 /* Resize the internal buffer (double it in size). Fail if a maximum size
86 bool pkgTagFile::Resize()
89 unsigned long EndSize
= d
->End
- d
->Start
;
91 // fail is the buffer grows too big
92 if(d
->Size
> 1024*1024+1)
95 // get new buffer and use it
96 tmp
= new char[2*d
->Size
];
97 memcpy(tmp
, d
->Buffer
, d
->Size
);
102 // update the start/end pointers to the new buffer
103 d
->Start
= d
->Buffer
;
104 d
->End
= d
->Start
+ EndSize
;
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.
114 bool pkgTagFile::Step(pkgTagSection
&Tag
)
116 while (Tag
.Scan(d
->Start
,d
->End
- d
->Start
) == false)
121 if(Tag
.Scan(d
->Start
,d
->End
- d
->Start
))
124 if (Resize() == false)
125 return _error
->Error(_("Unable to parse package file %s (1)"),
126 d
->Fd
.Name().c_str());
128 d
->Start
+= Tag
.size();
129 d
->iOffset
+= Tag
.size();
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()
141 unsigned long EndSize
= d
->End
- d
->Start
;
142 unsigned long Actual
= 0;
144 memmove(d
->Buffer
,d
->Start
,EndSize
);
145 d
->Start
= d
->Buffer
;
146 d
->End
= d
->Buffer
+ EndSize
;
148 if (d
->Done
== false)
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)
153 if (Actual
!= d
->Size
- (d
->End
- d
->Buffer
))
160 if (EndSize
<= 3 && Actual
== 0)
162 if (d
->Size
- (d
->End
- d
->Buffer
) < 4)
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
--)
170 for (; LineCount
< 2; LineCount
++)
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
183 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
185 // We are within a buffer space of the next hit..
186 if (Offset
>= d
->iOffset
&& d
->iOffset
+ (d
->End
- d
->Start
) > Offset
)
188 unsigned long Dist
= Offset
- d
->iOffset
;
194 // Reposition and reload..
197 if (d
->Fd
.Seek(Offset
) == false)
199 d
->End
= d
->Start
= d
->Buffer
;
204 if (Tag
.Scan(d
->Start
, d
->End
- d
->Start
) == true)
207 // This appends a double new line (for the real eof handling)
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());
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
)
223 const char *End
= Start
+ MaxLength
;
224 Stop
= Section
= Start
;
225 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
231 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
233 TrimRecord(true,End
);
235 // Start a new index and add it to the hash
236 if (isspace(Stop
[0]) == 0)
238 Indexes
[TagCount
++] = Stop
- Section
;
239 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
242 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
247 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
249 // Double newline marks the end of the record
250 if (Stop
+1 < End
&& Stop
[1] == '\n')
252 Indexes
[TagCount
] = Stop
- Section
;
253 TrimRecord(false,End
);
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
)
268 if (BeforeRecord
== true)
270 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
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()
278 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
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
286 unsigned int Length
= strlen(Tag
);
287 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
292 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
296 St
= Section
+ Indexes
[I
];
297 if (strncasecmp(Tag
,St
,Length
) != 0)
300 // Make sure the colon is in the right place
301 const char *C
= St
+ Length
;
302 for (; isspace(*C
) != 0; C
++);
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
319 unsigned int Length
= strlen(Tag
);
320 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
325 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
329 St
= Section
+ Indexes
[I
];
330 if (strncasecmp(Tag
,St
,Length
) != 0)
333 // Make sure the colon is in the right place
334 const char *C
= St
+ Length
;
335 for (; isspace(*C
) != 0; C
++);
339 // Strip off the gunk from the start end
341 End
= Section
+ Indexes
[I
+1];
343 return _error
->Error("Internal parsing error");
345 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
346 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
355 // TagSection::FindS - Find a string /*{{{*/
356 // ---------------------------------------------------------------------
358 string
pkgTagSection::FindS(const char *Tag
) const
362 if (Find(Tag
,Start
,End
) == false)
364 return string(Start
,End
);
367 // TagSection::FindI - Find an integer /*{{{*/
368 // ---------------------------------------------------------------------
370 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
374 if (Find(Tag
,Start
,Stop
) == false)
377 // Copy it into a temp buffer so we can use strtol
379 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
381 strncpy(S
,Start
,Stop
-Start
);
385 signed long Result
= strtol(S
,&End
,10);
391 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
392 // ---------------------------------------------------------------------
394 unsigned long long pkgTagSection::FindULL(const char *Tag
, unsigned long long const &Default
) const
398 if (Find(Tag
,Start
,Stop
) == false)
401 // Copy it into a temp buffer so we can use strtoull
403 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
405 strncpy(S
,Start
,Stop
-Start
);
409 unsigned long long Result
= strtoull(S
,&End
,10);
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
423 if (Find(Tag
,Start
,Stop
) == false)
425 return FindFlag(Flags
, Flag
, Start
, Stop
);
427 bool const pkgTagSection::FindFlag(unsigned long &Flags
, unsigned long Flag
,
428 char const* Start
, char const* Stop
)
430 switch (StringToBool(string(Start
, Stop
)))
441 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
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. */
453 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
455 static const char *iTFRewritePackageOrder
[] = {
463 "Original-Maintainer",
467 "Revision", // Obsolete
468 "Config-Version", // Obsolete
484 "MSDOS-Filename", // Obsolete
487 static const char *iTFRewriteSourceOrder
[] = {"Package",
494 "Original-Maintainer",
496 "Build-Depends-Indep",
498 "Build-Conflicts-Indep",
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
;
512 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
513 TFRewriteData
*Rewrite
)
515 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
516 for (unsigned I
= 0; I
!= 256; I
++)
519 // Set new tag up as necessary.
520 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
522 if (Rewrite
[J
].NewTag
== 0)
523 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
526 // Write all all of the tags, in order.
527 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
529 bool Rewritten
= false;
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
++)
534 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
537 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
539 if (isspace(Rewrite
[J
].Rewrite
[0]))
540 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
542 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
550 // See if it is in the fragment
552 if (Tags
.Find(Order
[I
],Pos
) == false)
556 if (Rewritten
== true)
559 /* Write out this element, taking a moment to rewrite the tag
560 in case of changes of case. */
563 Tags
.Get(Start
,Stop
,Pos
);
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");
574 // Now write all the old tags that were missed.
575 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
577 if ((Visited
[I
] & 1) == 1)
582 Tags
.Get(Start
,Stop
,I
);
583 const char *End
= Start
;
584 for (; End
< Stop
&& *End
!= ':'; End
++);
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
++)
590 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
593 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
595 if (isspace(Rewrite
[J
].Rewrite
[0]))
596 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
598 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
606 if (Rewritten
== true)
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");
616 // Now write all the rewrites that were missed
617 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
619 if ((Visited
[J
] & 2) == 2)
622 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
624 if (isspace(Rewrite
[J
].Rewrite
[0]))
625 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
627 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);