]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
72cd08596089c30eb453a43fd891a80ea8802827
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 /*{{{*/
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>
31 // TagFile::pkgTagFile - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
34 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long Size
) :
38 if (Fd
.IsOpen() == false)
41 Start
= End
= Buffer
= 0;
47 Buffer
= new char[Size
];
54 // TagFile::~pkgTagFile - Destructor /*{{{*/
55 // ---------------------------------------------------------------------
57 pkgTagFile::~pkgTagFile()
62 // TagFile::Step - Advance to the next section /*{{{*/
63 // ---------------------------------------------------------------------
64 /* If the Section Scanner fails we refill the buffer and try again. */
65 bool pkgTagFile::Step(pkgTagSection
&Tag
)
67 if (Tag
.Scan(Start
,End
- Start
) == false)
72 if (Tag
.Scan(Start
,End
- Start
) == false)
73 return _error
->Error(_("Unable to parse package file %s (1)"),
77 iOffset
+= Tag
.size();
83 // TagFile::Fill - Top up the buffer /*{{{*/
84 // ---------------------------------------------------------------------
85 /* This takes the bit at the end of the buffer and puts it at the start
86 then fills the rest from the file */
87 bool pkgTagFile::Fill()
89 unsigned long EndSize
= End
- Start
;
90 unsigned long Actual
= 0;
92 memmove(Buffer
,Start
,EndSize
);
94 End
= Buffer
+ EndSize
;
98 // See if only a bit of the file is left
99 if (Fd
.Read(End
,Size
- (End
- Buffer
),&Actual
) == false)
101 if (Actual
!= Size
- (End
- Buffer
))
108 if (EndSize
<= 3 && Actual
== 0)
110 if (Size
- (End
- Buffer
) < 4)
113 // Append a double new line if one does not exist
114 unsigned int LineCount
= 0;
115 for (const char *E
= End
- 1; E
- End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
118 for (; LineCount
< 2; LineCount
++)
127 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This jumps to a pre-recorded file location and reads the record
131 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
133 // We are within a buffer space of the next hit..
134 if (Offset
>= iOffset
&& iOffset
+ (End
- Start
) > Offset
)
136 unsigned long Dist
= Offset
- iOffset
;
142 // Reposition and reload..
145 if (Fd
.Seek(Offset
) == false)
147 End
= Start
= Buffer
;
152 if (Tag
.Scan(Start
,End
- Start
) == true)
155 // This appends a double new line (for the real eof handling)
159 if (Tag
.Scan(Start
,End
- Start
) == false)
160 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
165 // TagSection::Scan - Scan for the end of the header information /*{{{*/
166 // ---------------------------------------------------------------------
167 /* This looks for the first double new line in the data stream. It also
168 indexes the tags in the section. This very simple hash function for the
169 last 8 letters gives very good performance on the debian package files */
170 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
172 unsigned long Res
= 0;
173 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
174 Res
= ((unsigned long)(*Text
) & 0xDF) ^ (Res
<< 1);
178 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
180 const char *End
= Start
+ MaxLength
;
181 Stop
= Section
= Start
;
182 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
188 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
190 // Start a new index and add it to the hash
191 if (isspace(Stop
[0]) == 0)
193 Indexes
[TagCount
++] = Stop
- Section
;
194 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
197 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
202 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
204 // Double newline marks the end of the record
205 if (Stop
+1 < End
&& Stop
[1] == '\n')
207 Indexes
[TagCount
] = Stop
- Section
;
208 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
218 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
219 // ---------------------------------------------------------------------
220 /* There should be exactly 1 newline at the end of the buffer, no more. */
221 void pkgTagSection::Trim()
223 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
226 // TagSection::Find - Locate a tag /*{{{*/
227 // ---------------------------------------------------------------------
228 /* This searches the section for a tag that matches the given string. */
229 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
231 unsigned int Length
= strlen(Tag
);
232 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
237 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
241 St
= Section
+ Indexes
[I
];
242 if (strncasecmp(Tag
,St
,Length
) != 0)
245 // Make sure the colon is in the right place
246 const char *C
= St
+ Length
;
247 for (; isspace(*C
) != 0; C
++);
258 // TagSection::Find - Locate a tag /*{{{*/
259 // ---------------------------------------------------------------------
260 /* This searches the section for a tag that matches the given string. */
261 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
262 const char *&End
) const
264 unsigned int Length
= strlen(Tag
);
265 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
270 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
274 St
= Section
+ Indexes
[I
];
275 if (strncasecmp(Tag
,St
,Length
) != 0)
278 // Make sure the colon is in the right place
279 const char *C
= St
+ Length
;
280 for (; isspace(*C
) != 0; C
++);
284 // Strip off the gunk from the start end
286 End
= Section
+ Indexes
[I
+1];
288 return _error
->Error("Internal parsing error");
290 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
291 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
300 // TagSection::FindS - Find a string /*{{{*/
301 // ---------------------------------------------------------------------
303 string
pkgTagSection::FindS(const char *Tag
) const
307 if (Find(Tag
,Start
,End
) == false)
309 return string(Start
,End
);
312 // TagSection::FindI - Find an integer /*{{{*/
313 // ---------------------------------------------------------------------
315 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
319 if (Find(Tag
,Start
,Stop
) == false)
322 // Copy it into a temp buffer so we can use strtol
324 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
326 strncpy(S
,Start
,Stop
-Start
);
330 signed long Result
= strtol(S
,&End
,10);
336 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
337 // ---------------------------------------------------------------------
338 /* The bits marked in Flag are masked on/off in Flags */
339 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
340 unsigned long Flag
) const
344 if (Find(Tag
,Start
,Stop
) == false)
347 switch (StringToBool(string(Start
,Stop
)))
358 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
365 // TFRewrite - Rewrite a control record /*{{{*/
366 // ---------------------------------------------------------------------
367 /* This writes the control record to stdout rewriting it as necessary. The
368 override map item specificies the rewriting rules to follow. This also
369 takes the time to sort the feild list. */
371 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
373 static const char *iTFRewritePackageOrder
[] = {
384 "Revision", // Obsolete
385 "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
);