]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
Slovak translation updated
[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. 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)
200 {
201 unsigned long Res = 0;
202 for (; Text != End && *Text != ':' && *Text != 0; Text++)
203 Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
204 return Res & 0xFF;
205 }
206
207 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
208 {
209 const char *End = Start + MaxLength;
210 Stop = Section = Start;
211 memset(AlphaIndexes,0,sizeof(AlphaIndexes));
212
213 if (Stop == 0)
214 return false;
215
216 TagCount = 0;
217 while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
218 {
219 TrimRecord(true,End);
220
221 // Start a new index and add it to the hash
222 if (isspace(Stop[0]) == 0)
223 {
224 Indexes[TagCount++] = Stop - Section;
225 AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
226 }
227
228 Stop = (const char *)memchr(Stop,'\n',End - Stop);
229
230 if (Stop == 0)
231 return false;
232
233 for (; Stop+1 < End && Stop[1] == '\r'; Stop++);
234
235 // Double newline marks the end of the record
236 if (Stop+1 < End && Stop[1] == '\n')
237 {
238 Indexes[TagCount] = Stop - Section;
239 TrimRecord(false,End);
240 return true;
241 }
242
243 Stop++;
244 }
245
246 return false;
247 }
248 /*}}}*/
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)
253 {
254 if (BeforeRecord == true)
255 return;
256 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
257 }
258 /*}}}*/
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()
263 {
264 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
265 }
266 /*}}}*/
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
271 {
272 unsigned int Length = strlen(Tag);
273 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
274 if (I == 0)
275 return false;
276 I--;
277
278 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
279 I = (I+1)%TagCount)
280 {
281 const char *St;
282 St = Section + Indexes[I];
283 if (strncasecmp(Tag,St,Length) != 0)
284 continue;
285
286 // Make sure the colon is in the right place
287 const char *C = St + Length;
288 for (; isspace(*C) != 0; C++);
289 if (*C != ':')
290 continue;
291 Pos = I;
292 return true;
293 }
294
295 Pos = 0;
296 return false;
297 }
298 /*}}}*/
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
304 {
305 unsigned int Length = strlen(Tag);
306 unsigned int I = AlphaIndexes[AlphaHash(Tag)];
307 if (I == 0)
308 return false;
309 I--;
310
311 for (unsigned int Counter = 0; Counter != TagCount; Counter++,
312 I = (I+1)%TagCount)
313 {
314 const char *St;
315 St = Section + Indexes[I];
316 if (strncasecmp(Tag,St,Length) != 0)
317 continue;
318
319 // Make sure the colon is in the right place
320 const char *C = St + Length;
321 for (; isspace(*C) != 0; C++);
322 if (*C != ':')
323 continue;
324
325 // Strip off the gunk from the start end
326 Start = C;
327 End = Section + Indexes[I+1];
328 if (Start >= End)
329 return _error->Error("Internal parsing error");
330
331 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
332 for (; isspace(End[-1]) != 0 && End > Start; End--);
333
334 return true;
335 }
336
337 Start = End = 0;
338 return false;
339 }
340 /*}}}*/
341 // TagSection::FindS - Find a string /*{{{*/
342 // ---------------------------------------------------------------------
343 /* */
344 string pkgTagSection::FindS(const char *Tag) const
345 {
346 const char *Start;
347 const char *End;
348 if (Find(Tag,Start,End) == false)
349 return string();
350 return string(Start,End);
351 }
352 /*}}}*/
353 // TagSection::FindI - Find an integer /*{{{*/
354 // ---------------------------------------------------------------------
355 /* */
356 signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
357 {
358 const char *Start;
359 const char *Stop;
360 if (Find(Tag,Start,Stop) == false)
361 return Default;
362
363 // Copy it into a temp buffer so we can use strtol
364 char S[300];
365 if ((unsigned)(Stop - Start) >= sizeof(S))
366 return Default;
367 strncpy(S,Start,Stop-Start);
368 S[Stop - Start] = 0;
369
370 char *End;
371 signed long Result = strtol(S,&End,10);
372 if (S == End)
373 return Default;
374 return Result;
375 }
376 /*}}}*/
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
382 {
383 const char *Start;
384 const char *Stop;
385 if (Find(Tag,Start,Stop) == false)
386 return true;
387
388 switch (StringToBool(string(Start,Stop)))
389 {
390 case 0:
391 Flags &= ~Flag;
392 return true;
393
394 case 1:
395 Flags |= Flag;
396 return true;
397
398 default:
399 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
400 return true;
401 }
402 return true;
403 }
404 /*}}}*/
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. */
410
411 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
412 array. */
413 static const char *iTFRewritePackageOrder[] = {
414 "Package",
415 "Essential",
416 "Status",
417 "Priority",
418 "Section",
419 "Installed-Size",
420 "Maintainer",
421 "Architecture",
422 "Source",
423 "Version",
424 "Revision", // Obsolete
425 "Config-Version", // Obsolete
426 "Replaces",
427 "Provides",
428 "Depends",
429 "Pre-Depends",
430 "Recommends",
431 "Suggests",
432 "Conflicts",
433 "Breaks",
434 "Conffiles",
435 "Filename",
436 "Size",
437 "MD5Sum",
438 "SHA1",
439 "SHA256",
440 "MSDOS-Filename", // Obsolete
441 "Description",
442 0};
443 static const char *iTFRewriteSourceOrder[] = {"Package",
444 "Source",
445 "Binary",
446 "Version",
447 "Priority",
448 "Section",
449 "Maintainer",
450 "Build-Depends",
451 "Build-Depends-Indep",
452 "Build-Conflicts",
453 "Build-Conflicts-Indep",
454 "Architecture",
455 "Standards-Version",
456 "Format",
457 "Directory",
458 "Files",
459 0};
460
461 /* Two levels of initialization are used because gcc will set the symbol
462 size of an array to the length of the array, causing dynamic relinking
463 errors. Doing this makes the symbol size constant */
464 const char **TFRewritePackageOrder = iTFRewritePackageOrder;
465 const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
466
467 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
468 TFRewriteData *Rewrite)
469 {
470 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
471 for (unsigned I = 0; I != 256; I++)
472 Visited[I] = 0;
473
474 // Set new tag up as necessary.
475 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
476 {
477 if (Rewrite[J].NewTag == 0)
478 Rewrite[J].NewTag = Rewrite[J].Tag;
479 }
480
481 // Write all all of the tags, in order.
482 for (unsigned int I = 0; Order[I] != 0; I++)
483 {
484 bool Rewritten = false;
485
486 // See if this is a field that needs to be rewritten
487 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
488 {
489 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
490 {
491 Visited[J] |= 2;
492 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
493 {
494 if (isspace(Rewrite[J].Rewrite[0]))
495 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
496 else
497 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
498 }
499
500 Rewritten = true;
501 break;
502 }
503 }
504
505 // See if it is in the fragment
506 unsigned Pos;
507 if (Tags.Find(Order[I],Pos) == false)
508 continue;
509 Visited[Pos] |= 1;
510
511 if (Rewritten == true)
512 continue;
513
514 /* Write out this element, taking a moment to rewrite the tag
515 in case of changes of case. */
516 const char *Start;
517 const char *Stop;
518 Tags.Get(Start,Stop,Pos);
519
520 if (fputs(Order[I],Output) < 0)
521 return _error->Errno("fputs","IO Error to output");
522 Start += strlen(Order[I]);
523 if (fwrite(Start,Stop - Start,1,Output) != 1)
524 return _error->Errno("fwrite","IO Error to output");
525 if (Stop[-1] != '\n')
526 fprintf(Output,"\n");
527 }
528
529 // Now write all the old tags that were missed.
530 for (unsigned int I = 0; I != Tags.Count(); I++)
531 {
532 if ((Visited[I] & 1) == 1)
533 continue;
534
535 const char *Start;
536 const char *Stop;
537 Tags.Get(Start,Stop,I);
538 const char *End = Start;
539 for (; End < Stop && *End != ':'; End++);
540
541 // See if this is a field that needs to be rewritten
542 bool Rewritten = false;
543 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
544 {
545 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
546 {
547 Visited[J] |= 2;
548 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
549 {
550 if (isspace(Rewrite[J].Rewrite[0]))
551 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
552 else
553 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
554 }
555
556 Rewritten = true;
557 break;
558 }
559 }
560
561 if (Rewritten == true)
562 continue;
563
564 // Write out this element
565 if (fwrite(Start,Stop - Start,1,Output) != 1)
566 return _error->Errno("fwrite","IO Error to output");
567 if (Stop[-1] != '\n')
568 fprintf(Output,"\n");
569 }
570
571 // Now write all the rewrites that were missed
572 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
573 {
574 if ((Visited[J] & 2) == 2)
575 continue;
576
577 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
578 {
579 if (isspace(Rewrite[J].Rewrite[0]))
580 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
581 else
582 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
583 }
584 }
585
586 return true;
587 }
588 /*}}}*/