]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
Alfredo's vendor stuff
[apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.28 2001/03/13 06:51:46 jgg Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
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.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/tagfile.h"
16 #endif
17
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
21
22 #include <apti18n.h>
23
24 #include <string>
25 #include <stdio.h>
26 /*}}}*/
27
28 // TagFile::pkgTagFile - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
30 /* */
31 pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size)
32 {
33 if (Fd.IsOpen() == false)
34 {
35 Buffer = 0;
36 Start = End = Buffer = 0;
37 Left = 0;
38 iOffset = 0;
39 return;
40 }
41
42 Buffer = new char[Size];
43 Start = End = Buffer;
44 Left = Fd.Size();
45 TotalSize = Fd.Size();
46 iOffset = 0;
47 Fill();
48 }
49 /*}}}*/
50 // TagFile::~pkgTagFile - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 pkgTagFile::~pkgTagFile()
54 {
55 delete [] Buffer;
56 }
57 /*}}}*/
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)
62 {
63 if (Tag.Scan(Start,End - Start) == false)
64 {
65 if (Fill() == false)
66 return false;
67
68 if (Tag.Scan(Start,End - Start) == false)
69 return _error->Error(_("Unable to parse package file %s (1)"),Fd.Name().c_str());
70 }
71 Start += Tag.size();
72 iOffset += Tag.size();
73
74 Tag.Trim();
75
76 return true;
77 }
78 /*}}}*/
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()
84 {
85 unsigned long EndSize = End - Start;
86
87 memmove(Buffer,Start,EndSize);
88 Start = Buffer;
89 End = Buffer + EndSize;
90
91 if (Left == 0)
92 {
93 if (EndSize <= 3)
94 return false;
95 if (Size - (End - Buffer) < 4)
96 return true;
97
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--)
101 if (*E == '\n')
102 LineCount++;
103 for (; LineCount < 2; LineCount++)
104 *End++ = '\n';
105
106 return true;
107 }
108
109 // See if only a bit of the file is left
110 if (Left < Size - (End - Buffer))
111 {
112 if (Fd.Read(End,Left) == false)
113 return false;
114
115 End += Left;
116 Left = 0;
117 }
118 else
119 {
120 if (Fd.Read(End,Size - (End - Buffer)) == false)
121 return false;
122
123 Left -= Size - (End - Buffer);
124 End = Buffer + Size;
125 }
126 return true;
127 }
128 /*}}}*/
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
132 that is there */
133 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
134 {
135 // We are within a buffer space of the next hit..
136 if (Offset >= iOffset && iOffset + (End - Start) > Offset)
137 {
138 unsigned long Dist = Offset - iOffset;
139 Start += Dist;
140 iOffset += Dist;
141 return Step(Tag);
142 }
143
144 // Reposition and reload..
145 iOffset = Offset;
146 Left = TotalSize - Offset;
147 if (Fd.Seek(Offset) == false)
148 return false;
149 End = Start = Buffer;
150
151 if (Fill() == false)
152 return false;
153
154 if (Tag.Scan(Start,End - Start) == true)
155 return true;
156
157 // This appends a double new line (for the real eof handling)
158 if (Fill() == false)
159 return false;
160
161 if (Tag.Scan(Start,End - Start) == false)
162 return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
163
164 return true;
165 }
166 /*}}}*/
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)
173 {
174 unsigned long Res = 0;
175 for (; Text != End && *Text != ':' && *Text != 0; Text++)
176 Res = (unsigned long)(*Text) ^ (Res << 2);
177 return Res & 0xFF;
178 }
179
180 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
181 {
182 const char *End = Start + MaxLength;
183 Stop = Section = Start;
184 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
185
186 if (Stop == 0)
187 return false;
188
189 TagCount = 0;
190 while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
191 {
192 // Start a new index and add it to the hash
193 if (isspace(Stop[0]) == 0)
194 {
195 Indexes[TagCount++] = Stop - Section;
196 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
197 }
198
199 Stop = (const char *)memchr(Stop,'\n',End - Stop);
200
201 if (Stop == 0)
202 return false;
203
204 for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
205
206 // Double newline marks the end of the record
207 if (Stop+1 < End && Stop[1] == '\n')
208 {
209 Indexes[TagCount] = Stop - Section;
210 for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
211 return true;
212 }
213
214 Stop++;
215 }
216
217 return false;
218 }
219 /*}}}*/
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()
224 {
225 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
226 }
227 /*}}}*/
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
232 {
233 unsigned int Length = strlen(Tag);
234 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
235 if (I == 0)
236 return false;
237 I--;
238
239 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
240 I = (I+1)%TagCount)
241 {
242 const char *St;
243 St = Section + Indexes[I];
244 if (strncasecmp(Tag,St,Length) != 0)
245 continue;
246
247 // Make sure the colon is in the right place
248 const char *C = St + Length;
249 for (; isspace(*C) != 0; C++);
250 if (*C != ':')
251 continue;
252 Pos = I;
253 return true;
254 }
255
256 Pos = 0;
257 return false;
258 }
259 /*}}}*/
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
265 {
266 unsigned int Length = strlen(Tag);
267 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
268 if (I == 0)
269 return false;
270 I--;
271
272 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
273 I = (I+1)%TagCount)
274 {
275 const char *St;
276 St = Section + Indexes[I];
277 if (strncasecmp(Tag,St,Length) != 0)
278 continue;
279
280 // Make sure the colon is in the right place
281 const char *C = St + Length;
282 for (; isspace(*C) != 0; C++);
283 if (*C != ':')
284 continue;
285
286 // Strip off the gunk from the start end
287 Start = C;
288 End = Section + Indexes[I+1];
289 if (Start >= End)
290 return _error->Error("Internal parsing error");
291
292 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
293 for (; isspace(End[-1]) != 0 && End > Start; End--);
294
295 return true;
296 }
297
298 Start = End = 0;
299 return false;
300 }
301 /*}}}*/
302 // TagSection::FindS - Find a string /*{{{*/
303 // ---------------------------------------------------------------------
304 /* */
305 string pkgTagSection::FindS(const char *Tag) const
306 {
307 const char *Start;
308 const char *End;
309 if (Find(Tag,Start,End) == false)
310 return string();
311 return string(Start,End);
312 }
313 /*}}}*/
314 // TagSection::FindI - Find an integer /*{{{*/
315 // ---------------------------------------------------------------------
316 /* */
317 signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
318 {
319 const char *Start;
320 const char *Stop;
321 if (Find(Tag,Start,Stop) == false)
322 return Default;
323
324 // Copy it into a temp buffer so we can use strtol
325 char S[300];
326 if ((unsigned)(Stop - Start) >= sizeof(S))
327 return Default;
328 strncpy(S,Start,Stop-Start);
329 S[Stop - Start] = 0;
330
331 char *End;
332 signed long Result = strtol(S,&End,10);
333 if (S == End)
334 return Default;
335 return Result;
336 }
337 /*}}}*/
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
343 {
344 const char *Start;
345 const char *Stop;
346 if (Find(Tag,Start,Stop) == false)
347 return true;
348
349 switch (StringToBool(string(Start,Stop)))
350 {
351 case 0:
352 Flags &= ~Flag;
353 return true;
354
355 case 1:
356 Flags |= Flag;
357 return true;
358
359 default:
360 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
361 return true;
362 }
363 return true;
364 }
365 /*}}}*/
366
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. */
372
373 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
374 array. */
375 static const char *iTFRewritePackageOrder[] = {
376 "Package",
377 "Essential",
378 "Status",
379 "Priority",
380 "Section",
381 "Installed-Size",
382 "Maintainer",
383 "Architecture",
384 "Source",
385 "Version",
386 "Revision", // Obsolete
387 "Config-Version", // Obsolete
388 "Replaces",
389 "Provides",
390 "Depends",
391 "Pre-Depends",
392 "Recommends",
393 "Suggests",
394 "Conflicts",
395 "Conffiles",
396 "Filename",
397 "Size",
398 "MD5Sum",
399 "SHA1Sum",
400 "MSDOS-Filename", // Obsolete
401 "Description",
402 0};
403 static const char *iTFRewriteSourceOrder[] = {"Package",
404 "Source",
405 "Binary",
406 "Version",
407 "Priority",
408 "Section",
409 "Maintainer",
410 "Build-Depends",
411 "Build-Depends-Indep",
412 "Build-Conflicts",
413 "Build-Conflicts-Indep",
414 "Architecture",
415 "Standards-Version",
416 "Format",
417 "Directory",
418 "Files",
419 0};
420
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;
426
427 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
428 TFRewriteData *Rewrite)
429 {
430 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
431 for (unsigned I = 0; I != 256; I++)
432 Visited[I] = 0;
433
434 // Set new tag up as necessary.
435 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
436 {
437 if (Rewrite[J].NewTag == 0)
438 Rewrite[J].NewTag = Rewrite[J].Tag;
439 }
440
441 // Write all all of the tags, in order.
442 for (unsigned int I = 0; Order[I] != 0; I++)
443 {
444 bool Rewritten = false;
445
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++)
448 {
449 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
450 {
451 Visited[J] |= 2;
452 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
453 {
454 if (isspace(Rewrite[J].Rewrite[0]))
455 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
456 else
457 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
458 }
459
460 Rewritten = true;
461 break;
462 }
463 }
464
465 // See if it is in the fragment
466 unsigned Pos;
467 if (Tags.Find(Order[I],Pos) == false)
468 continue;
469 Visited[Pos] |= 1;
470
471 if (Rewritten == true)
472 continue;
473
474 /* Write out this element, taking a moment to rewrite the tag
475 in case of changes of case. */
476 const char *Start;
477 const char *Stop;
478 Tags.Get(Start,Stop,Pos);
479
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");
487 }
488
489 // Now write all the old tags that were missed.
490 for (unsigned int I = 0; I != Tags.Count(); I++)
491 {
492 if ((Visited[I] & 1) == 1)
493 continue;
494
495 const char *Start;
496 const char *Stop;
497 Tags.Get(Start,Stop,I);
498 const char *End = Start;
499 for (; End < Stop && *End != ':'; End++);
500
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++)
504 {
505 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
506 {
507 Visited[J] |= 2;
508 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
509 {
510 if (isspace(Rewrite[J].Rewrite[0]))
511 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
512 else
513 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
514 }
515
516 Rewritten = true;
517 break;
518 }
519 }
520
521 if (Rewritten == true)
522 continue;
523
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");
529 }
530
531 // Now write all the rewrites that were missed
532 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
533 {
534 if ((Visited[J] & 2) == 2)
535 continue;
536
537 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
538 {
539 if (isspace(Rewrite[J].Rewrite[0]))
540 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
541 else
542 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
543 }
544 }
545
546 return true;
547 }
548 /*}}}*/