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