]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/tagfile.cc
s390 archtable entry. Closes: #88232
[apt.git] / apt-pkg / tagfile.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: tagfile.cc,v 1.27 2001/02/23 06:41:55 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/* */
31pkgTagFile::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/* */
53pkgTagFile::~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. */
61bool 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 */
83bool 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 */
133bool 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 */
172inline 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
180bool 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. */
223void 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. */
231bool 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. */
263bool 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/* */
305string 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/* */
317signed 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 */
341bool 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. */
375static 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 "MSDOS-Filename", // Obsolete
400 "Description",
401 0};
402static const char *iTFRewriteSourceOrder[] = {"Package",
403 "Source",
404 "Binary",
405 "Version",
406 "Priority",
407 "Section",
408 "Maintainer",
409 "Build-Depends",
410 "Build-Depends-Indep",
411 "Build-Conflicts",
412 "Build-Conflicts-Indep",
413 "Architecture",
414 "Standards-Version",
415 "Format",
416 "Directory",
417 "Files",
418 0};
419
420/* Two levels of initialization are used because gcc will set the symbol
421 size of an array to the length of the array, causing dynamic relinking
422 errors. Doing this makes the symbol size constant */
423const char **TFRewritePackageOrder = iTFRewritePackageOrder;
424const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
425
426bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
427 TFRewriteData *Rewrite)
428{
429 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
430 for (unsigned I = 0; I != 256; I++)
431 Visited[I] = 0;
432
433 // Set new tag up as necessary.
434 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
435 {
436 if (Rewrite[J].NewTag == 0)
437 Rewrite[J].NewTag = Rewrite[J].Tag;
438 }
439
440 // Write all all of the tags, in order.
441 for (unsigned int I = 0; Order[I] != 0; I++)
442 {
443 bool Rewritten = false;
444
445 // See if this is a field that needs to be rewritten
446 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
447 {
448 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
449 {
450 Visited[J] |= 2;
451 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
452 {
453 if (isspace(Rewrite[J].Rewrite[0]))
454 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
455 else
456 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
457 }
458
459 Rewritten = true;
460 break;
461 }
462 }
463
464 // See if it is in the fragment
465 unsigned Pos;
466 if (Tags.Find(Order[I],Pos) == false)
467 continue;
468 Visited[Pos] |= 1;
469
470 if (Rewritten == true)
471 continue;
472
473 /* Write out this element, taking a moment to rewrite the tag
474 in case of changes of case. */
475 const char *Start;
476 const char *Stop;
477 Tags.Get(Start,Stop,Pos);
478
479 if (fputs(Order[I],Output) < 0)
480 return _error->Errno("fputs","IO Error to output");
481 Start += strlen(Order[I]);
482 if (fwrite(Start,Stop - Start,1,Output) != 1)
483 return _error->Errno("fwrite","IO Error to output");
484 if (Stop[-1] != '\n')
485 fprintf(Output,"\n");
486 }
487
488 // Now write all the old tags that were missed.
489 for (unsigned int I = 0; I != Tags.Count(); I++)
490 {
491 if ((Visited[I] & 1) == 1)
492 continue;
493
494 const char *Start;
495 const char *Stop;
496 Tags.Get(Start,Stop,I);
497 const char *End = Start;
498 for (; End < Stop && *End != ':'; End++);
499
500 // See if this is a field that needs to be rewritten
501 bool Rewritten = false;
502 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
503 {
504 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
505 {
506 Visited[J] |= 2;
507 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
508 {
509 if (isspace(Rewrite[J].Rewrite[0]))
510 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
511 else
512 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
513 }
514
515 Rewritten = true;
516 break;
517 }
518 }
519
520 if (Rewritten == true)
521 continue;
522
523 // Write out this element
524 if (fwrite(Start,Stop - Start,1,Output) != 1)
525 return _error->Errno("fwrite","IO Error to output");
526 if (Stop[-1] != '\n')
527 fprintf(Output,"\n");
528 }
529
530 // Now write all the rewrites that were missed
531 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
532 {
533 if ((Visited[J] & 2) == 2)
534 continue;
535
536 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
537 {
538 if (isspace(Rewrite[J].Rewrite[0]))
539 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
540 else
541 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
542 }
543 }
544
545 return true;
546}
547 /*}}}*/