]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
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 /*{{{*/
14 #include <apt-pkg/tagfile.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/strutl.h>
27 // TagFile::pkgTagFile - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
30 pkgTagFile::pkgTagFile(FileFd
*pFd
,unsigned long Size
) :
34 if (Fd
.IsOpen() == false)
37 Start
= End
= Buffer
= 0;
43 Buffer
= new char[Size
];
50 // TagFile::~pkgTagFile - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
53 pkgTagFile::~pkgTagFile()
58 // TagFile::Resize - Resize the internal buffer /*{{{*/
59 // ---------------------------------------------------------------------
60 /* Resize the internal buffer (double it in size). Fail if a maximum size
63 bool pkgTagFile::Resize()
66 unsigned long EndSize
= End
- Start
;
68 // fail is the buffer grows too big
69 if(Size
> 1024*1024+1)
72 // get new buffer and use it
73 tmp
= new char[2*Size
];
74 memcpy(tmp
, Buffer
, Size
);
79 // update the start/end pointers to the new buffer
81 End
= Start
+ EndSize
;
85 // TagFile::Step - Advance to the next section /*{{{*/
86 // ---------------------------------------------------------------------
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.
91 bool pkgTagFile::Step(pkgTagSection
&Tag
)
93 while (Tag
.Scan(Start
,End
- Start
) == false)
98 if(Tag
.Scan(Start
,End
- Start
))
101 if (Resize() == false)
102 return _error
->Error(_("Unable to parse package file %s (1)"),
106 iOffset
+= Tag
.size();
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 */
116 bool pkgTagFile::Fill()
118 unsigned long EndSize
= End
- Start
;
119 unsigned long Actual
= 0;
121 memmove(Buffer
,Start
,EndSize
);
123 End
= Buffer
+ EndSize
;
127 // See if only a bit of the file is left
128 if (Fd
.Read(End
,Size
- (End
- Buffer
),&Actual
) == false)
130 if (Actual
!= Size
- (End
- Buffer
))
137 if (EndSize
<= 3 && Actual
== 0)
139 if (Size
- (End
- Buffer
) < 4)
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
--)
147 for (; LineCount
< 2; LineCount
++)
156 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This jumps to a pre-recorded file location and reads the record
160 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
162 // We are within a buffer space of the next hit..
163 if (Offset
>= iOffset
&& iOffset
+ (End
- Start
) > Offset
)
165 unsigned long Dist
= Offset
- iOffset
;
171 // Reposition and reload..
174 if (Fd
.Seek(Offset
) == false)
176 End
= Start
= Buffer
;
181 if (Tag
.Scan(Start
,End
- Start
) == true)
184 // This appends a double new line (for the real eof handling)
188 if (Tag
.Scan(Start
,End
- Start
) == false)
189 return _error
->Error(_("Unable to parse package file %s (2)"),Fd
.Name().c_str());
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
197 indexes the tags in the section. This very simple hash function for the
198 last 8 letters gives very good performance on the debian package files */
199 inline static unsigned long AlphaHash(const char *Text
, const char *End
= 0)
201 unsigned long Res
= 0;
202 for (; Text
!= End
&& *Text
!= ':' && *Text
!= 0; Text
++)
203 Res
= ((unsigned long)(*Text
) & 0xDF) ^ (Res
<< 1);
207 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
209 const char *End
= Start
+ MaxLength
;
210 Stop
= Section
= Start
;
211 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
217 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
219 TrimRecord(true,End
);
221 // Start a new index and add it to the hash
222 if (isspace(Stop
[0]) == 0)
224 Indexes
[TagCount
++] = Stop
- Section
;
225 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
228 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
233 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
235 // Double newline marks the end of the record
236 if (Stop
+1 < End
&& Stop
[1] == '\n')
238 Indexes
[TagCount
] = Stop
- Section
;
239 TrimRecord(false,End
);
249 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
250 // ---------------------------------------------------------------------
251 /* There should be exactly 2 newline at the end of the record, no more. */
252 void pkgTagSection::TrimRecord(bool BeforeRecord
, const char*& End
)
254 if (BeforeRecord
== true)
256 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
259 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
260 // ---------------------------------------------------------------------
261 /* There should be exactly 1 newline at the end of the buffer, no more. */
262 void pkgTagSection::Trim()
264 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
267 // TagSection::Find - Locate a tag /*{{{*/
268 // ---------------------------------------------------------------------
269 /* This searches the section for a tag that matches the given string. */
270 bool pkgTagSection::Find(const char *Tag
,unsigned &Pos
) const
272 unsigned int Length
= strlen(Tag
);
273 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
278 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
282 St
= Section
+ Indexes
[I
];
283 if (strncasecmp(Tag
,St
,Length
) != 0)
286 // Make sure the colon is in the right place
287 const char *C
= St
+ Length
;
288 for (; isspace(*C
) != 0; C
++);
299 // TagSection::Find - Locate a tag /*{{{*/
300 // ---------------------------------------------------------------------
301 /* This searches the section for a tag that matches the given string. */
302 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
303 const char *&End
) const
305 unsigned int Length
= strlen(Tag
);
306 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
311 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
315 St
= Section
+ Indexes
[I
];
316 if (strncasecmp(Tag
,St
,Length
) != 0)
319 // Make sure the colon is in the right place
320 const char *C
= St
+ Length
;
321 for (; isspace(*C
) != 0; C
++);
325 // Strip off the gunk from the start end
327 End
= Section
+ Indexes
[I
+1];
329 return _error
->Error("Internal parsing error");
331 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
332 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
341 // TagSection::FindS - Find a string /*{{{*/
342 // ---------------------------------------------------------------------
344 string
pkgTagSection::FindS(const char *Tag
) const
348 if (Find(Tag
,Start
,End
) == false)
350 return string(Start
,End
);
353 // TagSection::FindI - Find an integer /*{{{*/
354 // ---------------------------------------------------------------------
356 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
360 if (Find(Tag
,Start
,Stop
) == false)
363 // Copy it into a temp buffer so we can use strtol
365 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
367 strncpy(S
,Start
,Stop
-Start
);
371 signed long Result
= strtol(S
,&End
,10);
377 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
378 // ---------------------------------------------------------------------
379 /* The bits marked in Flag are masked on/off in Flags */
380 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
381 unsigned long Flag
) const
385 if (Find(Tag
,Start
,Stop
) == false)
388 switch (StringToBool(string(Start
,Stop
)))
399 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
405 // TFRewrite - Rewrite a control record /*{{{*/
406 // ---------------------------------------------------------------------
407 /* This writes the control record to stdout rewriting it as necessary. The
408 override map item specificies the rewriting rules to follow. This also
409 takes the time to sort the feild list. */
411 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
413 static const char *iTFRewritePackageOrder
[] = {
421 "Original-Maintainer",
425 "Revision", // Obsolete
426 "Config-Version", // Obsolete
441 "MSDOS-Filename", // Obsolete
444 static const char *iTFRewriteSourceOrder
[] = {"Package",
451 "Original-Maintainer",
453 "Build-Depends-Indep",
455 "Build-Conflicts-Indep",
463 /* Two levels of initialization are used because gcc will set the symbol
464 size of an array to the length of the array, causing dynamic relinking
465 errors. Doing this makes the symbol size constant */
466 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
467 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
469 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
470 TFRewriteData
*Rewrite
)
472 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
473 for (unsigned I
= 0; I
!= 256; I
++)
476 // Set new tag up as necessary.
477 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
479 if (Rewrite
[J
].NewTag
== 0)
480 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
483 // Write all all of the tags, in order.
484 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
486 bool Rewritten
= false;
488 // See if this is a field that needs to be rewritten
489 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
491 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
494 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
496 if (isspace(Rewrite
[J
].Rewrite
[0]))
497 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
499 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
507 // See if it is in the fragment
509 if (Tags
.Find(Order
[I
],Pos
) == false)
513 if (Rewritten
== true)
516 /* Write out this element, taking a moment to rewrite the tag
517 in case of changes of case. */
520 Tags
.Get(Start
,Stop
,Pos
);
522 if (fputs(Order
[I
],Output
) < 0)
523 return _error
->Errno("fputs","IO Error to output");
524 Start
+= strlen(Order
[I
]);
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 old tags that were missed.
532 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
534 if ((Visited
[I
] & 1) == 1)
539 Tags
.Get(Start
,Stop
,I
);
540 const char *End
= Start
;
541 for (; End
< Stop
&& *End
!= ':'; End
++);
543 // See if this is a field that needs to be rewritten
544 bool Rewritten
= false;
545 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
547 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
550 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
552 if (isspace(Rewrite
[J
].Rewrite
[0]))
553 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
555 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
563 if (Rewritten
== true)
566 // Write out this element
567 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
568 return _error
->Errno("fwrite","IO Error to output");
569 if (Stop
[-1] != '\n')
570 fprintf(Output
,"\n");
573 // Now write all the rewrites that were missed
574 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
576 if ((Visited
[J
] & 2) == 2)
579 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
581 if (isspace(Rewrite
[J
].Rewrite
[0]))
582 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
584 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);