]>
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.
197 It also indexes the tags in the section. */
198 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
200 const char *End
= Start
+ MaxLength
;
201 Stop
= Section
= Start
;
202 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
208 while (TagCount
+1 < sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
210 TrimRecord(true,End
);
212 // Start a new index and add it to the hash
213 if (isspace(Stop
[0]) == 0)
215 Indexes
[TagCount
++] = Stop
- Section
;
216 AlphaIndexes
[AlphaHash(Stop
,End
)] = TagCount
;
219 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
224 for (; Stop
+1 < End
&& Stop
[1] == '\r'; Stop
++);
226 // Double newline marks the end of the record
227 if (Stop
+1 < End
&& Stop
[1] == '\n')
229 Indexes
[TagCount
] = Stop
- Section
;
230 TrimRecord(false,End
);
240 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
241 // ---------------------------------------------------------------------
242 /* There should be exactly 2 newline at the end of the record, no more. */
243 void pkgTagSection::TrimRecord(bool BeforeRecord
, const char*& End
)
245 if (BeforeRecord
== true)
247 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r'); Stop
++);
250 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
251 // ---------------------------------------------------------------------
252 /* There should be exactly 1 newline at the end of the buffer, no more. */
253 void pkgTagSection::Trim()
255 for (; Stop
> Section
+ 2 && (Stop
[-2] == '\n' || Stop
[-2] == '\r'); Stop
--);
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
,unsigned &Pos
) const
263 unsigned int Length
= strlen(Tag
);
264 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
269 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
273 St
= Section
+ Indexes
[I
];
274 if (strncasecmp(Tag
,St
,Length
) != 0)
277 // Make sure the colon is in the right place
278 const char *C
= St
+ Length
;
279 for (; isspace(*C
) != 0; C
++);
290 // TagSection::Find - Locate a tag /*{{{*/
291 // ---------------------------------------------------------------------
292 /* This searches the section for a tag that matches the given string. */
293 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
294 const char *&End
) const
296 unsigned int Length
= strlen(Tag
);
297 unsigned int I
= AlphaIndexes
[AlphaHash(Tag
)];
302 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
306 St
= Section
+ Indexes
[I
];
307 if (strncasecmp(Tag
,St
,Length
) != 0)
310 // Make sure the colon is in the right place
311 const char *C
= St
+ Length
;
312 for (; isspace(*C
) != 0; C
++);
316 // Strip off the gunk from the start end
318 End
= Section
+ Indexes
[I
+1];
320 return _error
->Error("Internal parsing error");
322 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
323 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
332 // TagSection::FindS - Find a string /*{{{*/
333 // ---------------------------------------------------------------------
335 string
pkgTagSection::FindS(const char *Tag
) const
339 if (Find(Tag
,Start
,End
) == false)
341 return string(Start
,End
);
344 // TagSection::FindI - Find an integer /*{{{*/
345 // ---------------------------------------------------------------------
347 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
) const
351 if (Find(Tag
,Start
,Stop
) == false)
354 // Copy it into a temp buffer so we can use strtol
356 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
358 strncpy(S
,Start
,Stop
-Start
);
362 signed long Result
= strtol(S
,&End
,10);
368 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
369 // ---------------------------------------------------------------------
371 unsigned long long pkgTagSection::FindULL(const char *Tag
, unsigned long long const &Default
) const
375 if (Find(Tag
,Start
,Stop
) == false)
378 // Copy it into a temp buffer so we can use strtoull
380 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
382 strncpy(S
,Start
,Stop
-Start
);
386 unsigned long long Result
= strtoull(S
,&End
,10);
392 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
393 // ---------------------------------------------------------------------
394 /* The bits marked in Flag are masked on/off in Flags */
395 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
396 unsigned long Flag
) const
400 if (Find(Tag
,Start
,Stop
) == false)
402 return FindFlag(Flags
, Flag
, Start
, Stop
);
404 bool const pkgTagSection::FindFlag(unsigned long &Flags
, unsigned long Flag
,
405 char const* Start
, char const* Stop
)
407 switch (StringToBool(string(Start
, Stop
)))
418 _error
->Warning("Unknown flag value: %s",string(Start
,Stop
).c_str());
424 // TFRewrite - Rewrite a control record /*{{{*/
425 // ---------------------------------------------------------------------
426 /* This writes the control record to stdout rewriting it as necessary. The
427 override map item specificies the rewriting rules to follow. This also
428 takes the time to sort the feild list. */
430 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
432 static const char *iTFRewritePackageOrder
[] = {
440 "Original-Maintainer",
444 "Revision", // Obsolete
445 "Config-Version", // Obsolete
461 "MSDOS-Filename", // Obsolete
464 static const char *iTFRewriteSourceOrder
[] = {"Package",
471 "Original-Maintainer",
473 "Build-Depends-Indep",
475 "Build-Conflicts-Indep",
483 /* Two levels of initialization are used because gcc will set the symbol
484 size of an array to the length of the array, causing dynamic relinking
485 errors. Doing this makes the symbol size constant */
486 const char **TFRewritePackageOrder
= iTFRewritePackageOrder
;
487 const char **TFRewriteSourceOrder
= iTFRewriteSourceOrder
;
489 bool TFRewrite(FILE *Output
,pkgTagSection
const &Tags
,const char *Order
[],
490 TFRewriteData
*Rewrite
)
492 unsigned char Visited
[256]; // Bit 1 is Order, Bit 2 is Rewrite
493 for (unsigned I
= 0; I
!= 256; I
++)
496 // Set new tag up as necessary.
497 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
499 if (Rewrite
[J
].NewTag
== 0)
500 Rewrite
[J
].NewTag
= Rewrite
[J
].Tag
;
503 // Write all all of the tags, in order.
504 for (unsigned int I
= 0; Order
[I
] != 0; I
++)
506 bool Rewritten
= false;
508 // See if this is a field that needs to be rewritten
509 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
511 if (strcasecmp(Rewrite
[J
].Tag
,Order
[I
]) == 0)
514 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
516 if (isspace(Rewrite
[J
].Rewrite
[0]))
517 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
519 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
527 // See if it is in the fragment
529 if (Tags
.Find(Order
[I
],Pos
) == false)
533 if (Rewritten
== true)
536 /* Write out this element, taking a moment to rewrite the tag
537 in case of changes of case. */
540 Tags
.Get(Start
,Stop
,Pos
);
542 if (fputs(Order
[I
],Output
) < 0)
543 return _error
->Errno("fputs","IO Error to output");
544 Start
+= strlen(Order
[I
]);
545 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
546 return _error
->Errno("fwrite","IO Error to output");
547 if (Stop
[-1] != '\n')
548 fprintf(Output
,"\n");
551 // Now write all the old tags that were missed.
552 for (unsigned int I
= 0; I
!= Tags
.Count(); I
++)
554 if ((Visited
[I
] & 1) == 1)
559 Tags
.Get(Start
,Stop
,I
);
560 const char *End
= Start
;
561 for (; End
< Stop
&& *End
!= ':'; End
++);
563 // See if this is a field that needs to be rewritten
564 bool Rewritten
= false;
565 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
567 if (stringcasecmp(Start
,End
,Rewrite
[J
].Tag
) == 0)
570 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
572 if (isspace(Rewrite
[J
].Rewrite
[0]))
573 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
575 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
583 if (Rewritten
== true)
586 // Write out this element
587 if (fwrite(Start
,Stop
- Start
,1,Output
) != 1)
588 return _error
->Errno("fwrite","IO Error to output");
589 if (Stop
[-1] != '\n')
590 fprintf(Output
,"\n");
593 // Now write all the rewrites that were missed
594 for (unsigned int J
= 0; Rewrite
!= 0 && Rewrite
[J
].Tag
!= 0; J
++)
596 if ((Visited
[J
] & 2) == 2)
599 if (Rewrite
[J
].Rewrite
!= 0 && Rewrite
[J
].Rewrite
[0] != 0)
601 if (isspace(Rewrite
[J
].Rewrite
[0]))
602 fprintf(Output
,"%s:%s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);
604 fprintf(Output
,"%s: %s\n",Rewrite
[J
].NewTag
,Rewrite
[J
].Rewrite
);