]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
0e72dd52 | 3 | // $Id: tagfile.cc,v 1.27 2001/02/23 06:41:55 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | Fast scanner for RFC-822 type header information | |
7 | ||
ad00ae81 | 8 | This uses a rotating buffer to load the package information into. |
578bfd0a AL |
9 | The scanner runs over it and isolates and indexes a single section. |
10 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | // Include Files /*{{{*/ | |
6c139d6e | 14 | #ifdef __GNUG__ |
094a497d | 15 | #pragma implementation "apt-pkg/tagfile.h" |
6c139d6e AL |
16 | #endif |
17 | ||
094a497d AL |
18 | #include <apt-pkg/tagfile.h> |
19 | #include <apt-pkg/error.h> | |
cdcc6d34 | 20 | #include <apt-pkg/strutl.h> |
578bfd0a | 21 | |
b2e465d6 AL |
22 | #include <apti18n.h> |
23 | ||
578bfd0a AL |
24 | #include <string> |
25 | #include <stdio.h> | |
26 | /*}}}*/ | |
27 | ||
28 | // TagFile::pkgTagFile - Constructor /*{{{*/ | |
29 | // --------------------------------------------------------------------- | |
30 | /* */ | |
b2e465d6 | 31 | pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size) |
578bfd0a | 32 | { |
0e72dd52 AL |
33 | if (Fd.IsOpen() == false) |
34 | { | |
35 | Buffer = 0; | |
36 | Start = End = Buffer = 0; | |
37 | Left = 0; | |
38 | iOffset = 0; | |
39 | return; | |
40 | } | |
41 | ||
ad00ae81 AL |
42 | Buffer = new char[Size]; |
43 | Start = End = Buffer; | |
578bfd0a | 44 | Left = Fd.Size(); |
b2e465d6 | 45 | TotalSize = Fd.Size(); |
dcb79bae | 46 | iOffset = 0; |
578bfd0a AL |
47 | Fill(); |
48 | } | |
49 | /*}}}*/ | |
b2e465d6 | 50 | // TagFile::~pkgTagFile - Destructor /*{{{*/ |
29f7b36c AL |
51 | // --------------------------------------------------------------------- |
52 | /* */ | |
53 | pkgTagFile::~pkgTagFile() | |
54 | { | |
55 | delete [] Buffer; | |
56 | } | |
57 | /*}}}*/ | |
578bfd0a AL |
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) | |
b2e465d6 | 69 | return _error->Error(_("Unable to parse package file %s (1)"),Fd.Name().c_str()); |
578bfd0a | 70 | } |
dcb79bae AL |
71 | Start += Tag.size(); |
72 | iOffset += Tag.size(); | |
b2e465d6 AL |
73 | |
74 | Tag.Trim(); | |
dcb79bae | 75 | |
578bfd0a AL |
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 | { | |
ad00ae81 | 85 | unsigned long EndSize = End - Start; |
578bfd0a | 86 | |
c7b5ce1c AL |
87 | memmove(Buffer,Start,EndSize); |
88 | Start = Buffer; | |
89 | End = Buffer + EndSize; | |
90 | ||
578bfd0a AL |
91 | if (Left == 0) |
92 | { | |
c7b5ce1c | 93 | if (EndSize <= 3) |
578bfd0a | 94 | return false; |
c7b5ce1c AL |
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 | ||
578bfd0a AL |
106 | return true; |
107 | } | |
108 | ||
c88edf1d AL |
109 | // See if only a bit of the file is left |
110 | if (Left < Size - (End - Buffer)) | |
578bfd0a | 111 | { |
ad00ae81 | 112 | if (Fd.Read(End,Left) == false) |
578bfd0a | 113 | return false; |
c88edf1d | 114 | |
ad00ae81 | 115 | End += Left; |
578bfd0a AL |
116 | Left = 0; |
117 | } | |
118 | else | |
119 | { | |
ad00ae81 | 120 | if (Fd.Read(End,Size - (End - Buffer)) == false) |
578bfd0a | 121 | return false; |
c88edf1d | 122 | |
ad00ae81 AL |
123 | Left -= Size - (End - Buffer); |
124 | End = Buffer + Size; | |
578bfd0a AL |
125 | } |
126 | return true; | |
127 | } | |
128 | /*}}}*/ | |
ad00ae81 AL |
129 | // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/ |
130 | // --------------------------------------------------------------------- | |
03e39e59 AL |
131 | /* This jumps to a pre-recorded file location and reads the record |
132 | that is there */ | |
ad00ae81 AL |
133 | bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset) |
134 | { | |
b2e465d6 AL |
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.. | |
ad00ae81 | 145 | iOffset = Offset; |
b2e465d6 | 146 | Left = TotalSize - Offset; |
ad00ae81 AL |
147 | if (Fd.Seek(Offset) == false) |
148 | return false; | |
149 | End = Start = Buffer; | |
150 | ||
138d4b3d AL |
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) | |
ad00ae81 AL |
158 | if (Fill() == false) |
159 | return false; | |
160 | ||
161 | if (Tag.Scan(Start,End - Start) == false) | |
b2e465d6 | 162 | return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str()); |
06bba740 | 163 | |
ad00ae81 AL |
164 | return true; |
165 | } | |
166 | /*}}}*/ | |
578bfd0a AL |
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 | |
c1a22377 AL |
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 */ | |
b2e465d6 AL |
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 | ||
578bfd0a AL |
180 | bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) |
181 | { | |
182 | const char *End = Start + MaxLength; | |
183 | Stop = Section = Start; | |
c1a22377 | 184 | memset(AlphaIndexes,0,sizeof(AlphaIndexes)); |
c7b5ce1c AL |
185 | |
186 | if (Stop == 0) | |
187 | return false; | |
578bfd0a AL |
188 | |
189 | TagCount = 0; | |
f3bcc383 | 190 | while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End) |
578bfd0a | 191 | { |
90d64280 | 192 | // Start a new index and add it to the hash |
c1a22377 AL |
193 | if (isspace(Stop[0]) == 0) |
194 | { | |
195 | Indexes[TagCount++] = Stop - Section; | |
b2e465d6 | 196 | AlphaIndexes[AlphaHash(Stop,End)] = TagCount; |
c1a22377 | 197 | } |
0a8e3465 | 198 | |
c1a22377 | 199 | Stop = (const char *)memchr(Stop,'\n',End - Stop); |
0a8e3465 | 200 | |
c1a22377 AL |
201 | if (Stop == 0) |
202 | return false; | |
138d4b3d | 203 | |
90d64280 | 204 | for (; Stop[1] == '\r' && Stop+1 < End; Stop++); |
c1a22377 | 205 | |
f3bcc383 AL |
206 | // Double newline marks the end of the record |
207 | if (Stop+1 < End && Stop[1] == '\n') | |
578bfd0a | 208 | { |
578bfd0a | 209 | Indexes[TagCount] = Stop - Section; |
0a8e3465 | 210 | for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++); |
578bfd0a | 211 | return true; |
578bfd0a AL |
212 | } |
213 | ||
c1a22377 AL |
214 | Stop++; |
215 | } | |
138d4b3d | 216 | |
578bfd0a AL |
217 | return false; |
218 | } | |
219 | /*}}}*/ | |
b2e465d6 AL |
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 | /*}}}*/ | |
578bfd0a AL |
228 | // TagSection::Find - Locate a tag /*{{{*/ |
229 | // --------------------------------------------------------------------- | |
230 | /* This searches the section for a tag that matches the given string. */ | |
b2e465d6 | 231 | bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const |
578bfd0a AL |
232 | { |
233 | unsigned int Length = strlen(Tag); | |
b2e465d6 | 234 | unsigned int I = AlphaIndexes[AlphaHash(Tag)]; |
c1a22377 AL |
235 | if (I == 0) |
236 | return false; | |
237 | I--; | |
238 | ||
239 | for (unsigned int Counter = 0; Counter != TagCount; Counter++, | |
240 | I = (I+1)%TagCount) | |
578bfd0a | 241 | { |
c1a22377 AL |
242 | const char *St; |
243 | St = Section + Indexes[I]; | |
244 | if (strncasecmp(Tag,St,Length) != 0) | |
578bfd0a AL |
245 | continue; |
246 | ||
b2e465d6 AL |
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 | ||
578bfd0a | 280 | // Make sure the colon is in the right place |
c1a22377 | 281 | const char *C = St + Length; |
578bfd0a AL |
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]; | |
06bba740 AL |
289 | if (Start >= End) |
290 | return _error->Error("Internal parsing error"); | |
291 | ||
578bfd0a AL |
292 | for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++); |
293 | for (; isspace(End[-1]) != 0 && End > Start; End--); | |
06bba740 | 294 | |
578bfd0a AL |
295 | return true; |
296 | } | |
c1a22377 | 297 | |
578bfd0a AL |
298 | Start = End = 0; |
299 | return false; | |
300 | } | |
301 | /*}}}*/ | |
0e66b144 | 302 | // TagSection::FindS - Find a string /*{{{*/ |
a05599f1 AL |
303 | // --------------------------------------------------------------------- |
304 | /* */ | |
b2e465d6 | 305 | string pkgTagSection::FindS(const char *Tag) const |
a05599f1 AL |
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 | /* */ | |
b2e465d6 | 317 | signed int pkgTagSection::FindI(const char *Tag,signed long Default) const |
a05599f1 AL |
318 | { |
319 | const char *Start; | |
b0b4efb9 AL |
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, | |
b2e465d6 | 342 | unsigned long Flag) const |
b0b4efb9 AL |
343 | { |
344 | const char *Start; | |
345 | const char *Stop; | |
346 | if (Find(Tag,Start,Stop) == false) | |
347 | return true; | |
a05599f1 | 348 | |
b0b4efb9 AL |
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: | |
b2e465d6 | 360 | _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str()); |
b0b4efb9 AL |
361 | return true; |
362 | } | |
363 | return true; | |
a05599f1 AL |
364 | } |
365 | /*}}}*/ | |
b2e465d6 AL |
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 | "MSDOS-Filename", // Obsolete | |
400 | "Description", | |
401 | 0}; | |
402 | static 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 */ | |
423 | const char **TFRewritePackageOrder = iTFRewritePackageOrder; | |
424 | const char **TFRewriteSourceOrder = iTFRewriteSourceOrder; | |
425 | ||
426 | bool 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 | /*}}}*/ |