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