]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.28 2001/03/13 06:51:46 jgg 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 /*{{{*/
15 #pragma implementation "apt-pkg/tagfile.h"
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
28 // TagFile::pkgTagFile - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
31 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long Size
) : Fd(*pFd
), Size(Size
)
33 if (Fd
.IsOpen() == false)
36 Start
= End
= Buffer
= 0;
42 Buffer
= new char[Size
];
45 TotalSize
= Fd
.Size();
50 // TagFile::~pkgTagFile - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
53 pkgTagFile::~pkgTagFile()
58 // TagFile::Step - Advance to the next section /*{{{*/
59 // ---------------------------------------------------------------------
60 /* If the Section Scanner fails we refill the buffer and try again. */
61 bool pkgTagFile::Step(pkgTagSection
&Tag
)
63 if (Tag
.Scan(Start
,End
- Start
) == false)
68 if (Tag
.Scan(Start
,End
- Start
) == false)
69 return _error
->Error(_("Unable to parse package file %s (1)"),Fd
.Name().c_str());
72 iOffset
+= Tag
.size();
79 // TagFile::Fill - Top up the buffer /*{{{*/
80 // ---------------------------------------------------------------------
81 /* This takes the bit at the end of the buffer and puts it at the start
82 then fills the rest from the file */
83 bool pkgTagFile::Fill()
85 unsigned long EndSize
= End
- Start
;
87 memmove(Buffer
,Start
,EndSize
);
89 End
= Buffer
+ EndSize
;
95 if (Size
- (End
- Buffer
) < 4)
98 // Append a double new line if one does not exist
99 unsigned int LineCount
= 0;
100 for (const char *E
= End
- 1; E
- End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
103 for (; LineCount
< 2; LineCount
++)
109 // See if only a bit of the file is left
110 if (Left
< Size
- (End
- Buffer
))
112 if (Fd
.Read(End
,Left
) == false)
120 if (Fd
.Read(End
,Size
- (End
- Buffer
)) == false)
123 Left
-= Size
- (End
- Buffer
);
129 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
130 // ---------------------------------------------------------------------
131 /* This jumps to a pre-recorded file location and reads the record
133 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
135 // We are within a buffer space of the next hit..
136 if (Offset
>= iOffset
&& iOffset
+ (End
- Start
) > Offset
)
138 unsigned long Dist
= Offset
- iOffset
;
144 // Reposition and reload..
146 Left
= TotalSize
- Offset
;
147 if (Fd
.Seek(Offset
) == false)
149 End
= Start
= Buffer
;
154 if (Tag
.Scan(Start
,End
- Start
) == true)
157 // This appends a double new line (for the real eof handling)
161 if (Tag
.Scan(Start
,End
- Start
) == false)
162 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
167 // TagSection::Scan - Scan for the end of the header information /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This looks for the first double new line in the data stream. It also
170 indexes the tags in the section. This very simple hash function for the
171 first 3 letters gives very good performance on the debian package files */
172 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
174 unsigned long Res
= 0;
175 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
176 Res
= (unsigned long)(*Text
) ^ (Res
<< 2);
180 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
182 const char *End
= Start
+ MaxLength
;
183 Stop
= Section
= Start
;
184 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
190 while (TagCount
< sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
192 // Start a new index and add it to the hash
193 if (isspace(Stop
[0]) == 0)
195 Indexes
[TagCount
++] = Stop
- Section
;
196 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
199 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
204 for (; Stop
[1] == '\r' && Stop
+1 < End
; Stop
++);
206 // Double newline marks the end of the record
207 if (Stop
+1 < End
&& Stop
[1] == '\n')
209 Indexes
[TagCount
] = Stop
- Section
;
210 for (; (Stop
[0] == '\n' || Stop
[0] == '\r') && Stop
< End
; Stop
++);
220 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
221 // ---------------------------------------------------------------------
222 /* There should be exactly 1 newline at the end of the buffer, no more. */
223 void pkgTagSection::Trim()
225 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
228 // TagSection::Find - Locate a tag /*{{{*/
229 // ---------------------------------------------------------------------
230 /* This searches the section for a tag that matches the given string. */
231 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
233 unsigned int Length
= strlen(Tag
);
234 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
239 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
243 St
= Section
+ Indexes
[I
];
244 if (strncasecmp(Tag
,St
,Length
) != 0)
247 // Make sure the colon is in the right place
248 const char *C
= St
+ Length
;
249 for (; isspace(*C
) != 0; C
++);
260 // TagSection::Find - Locate a tag /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This searches the section for a tag that matches the given string. */
263 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
264 const char *&End
) const
266 unsigned int Length
= strlen(Tag
);
267 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
272 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
276 St
= Section
+ Indexes
[I
];
277 if (strncasecmp(Tag
,St
,Length
) != 0)
280 // Make sure the colon is in the right place
281 const char *C
= St
+ Length
;
282 for (; isspace(*C
) != 0; C
++);
286 // Strip off the gunk from the start end
288 End
= Section
+ Indexes
[I
+1];
290 return _error
->Error("Internal parsing error");
292 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
293 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
302 // TagSection::FindS - Find a string /*{{{*/
303 // ---------------------------------------------------------------------
305 string
pkgTagSection::FindS(const char *Tag
) const
309 if (Find(Tag
,Start
,End
) == false)
311 return string(Start
,End
);
314 // TagSection::FindI - Find an integer /*{{{*/
315 // ---------------------------------------------------------------------
317 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
321 if (Find(Tag
,Start
,Stop
) == false)
324 // Copy it into a temp buffer so we can use strtol
326 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
328 strncpy(S
,Start
,Stop
-Start
);
332 signed long Result
= strtol(S
,&End
,10);
338 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
339 // ---------------------------------------------------------------------
340 /* The bits marked in Flag are masked on/off in Flags */
341 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
342 unsigned long Flag
) const
346 if (Find(Tag
,Start
,Stop
) == false)
349 switch (StringToBool(string(Start
,Stop
)))
360 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
367 // TFRewrite - Rewrite a control record /*{{{*/
368 // ---------------------------------------------------------------------
369 /* This writes the control record to stdout rewriting it as necessary. The
370 override map item specificies the rewriting rules to follow. This also
371 takes the time to sort the feild list. */
373 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
375 static const char *iTFRewritePackageOrder
[] = {
386 "Revision", // Obsolete
387 "Config-Version", // Obsolete
400 "MSDOS-Filename", // Obsolete
403 static const char *iTFRewriteSourceOrder
[] = {"Package",
411 "Build-Depends-Indep",
413 "Build-Conflicts-Indep",
421 /* Two levels of initialization are used because gcc will set the symbol
422 size of an array to the length of the array, causing dynamic relinking
423 errors. Doing this makes the symbol size constant */
424 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
425 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
427 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
428 TFRewriteData
*Rewrite
)
430 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
431 for (unsigned I
= 0; I
!= 256; I
++)
434 // Set new tag up as necessary.
435 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
437 if (Rewrite
[J
].NewTag
== 0)
438 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
441 // Write all all of the tags, in order.
442 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
444 bool Rewritten
= false;
446 // See if this is a field that needs to be rewritten
447 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
449 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
452 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
454 if (isspace(Rewrite
[J
].Rewrite
[0]))
455 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
457 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
465 // See if it is in the fragment
467 if (Tags
.Find(Order
[I
],Pos
) == false)
471 if (Rewritten
== true)
474 /* Write out this element, taking a moment to rewrite the tag
475 in case of changes of case. */
478 Tags
.Get(Start
,Stop
,Pos
);
480 if (fputs(Order
[I
],Output
) < 0)
481 return _error
->Errno("fputs","IO Error to output");
482 Start
+= strlen(Order
[I
]);
483 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
484 return _error
->Errno("fwrite","IO Error to output");
485 if (Stop
[-1] != '\n')
486 fprintf(Output
,"\n");
489 // Now write all the old tags that were missed.
490 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
492 if ((Visited
[I
] & 1) == 1)
497 Tags
.Get(Start
,Stop
,I
);
498 const char *End
= Start
;
499 for (; End
< Stop
&& *End
!= ':'; End
++);
501 // See if this is a field that needs to be rewritten
502 bool Rewritten
= false;
503 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
505 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
508 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
510 if (isspace(Rewrite
[J
].Rewrite
[0]))
511 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
513 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
521 if (Rewritten
== true)
524 // Write out this element
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");
531 // Now write all the rewrites that were missed
532 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
534 if ((Visited
[J
] & 2) == 2)
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
);