]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz 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> | |
851a45a8 | 26 | #include <ctype.h> |
578bfd0a AL |
27 | /*}}}*/ |
28 | ||
851a45a8 AL |
29 | using std::string; |
30 | ||
578bfd0a AL |
31 | // TagFile::pkgTagFile - Constructor /*{{{*/ |
32 | // --------------------------------------------------------------------- | |
33 | /* */ | |
b3d44315 MV |
34 | pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : |
35 | Fd(*pFd), | |
2ca99a0d | 36 | Size(Size) |
578bfd0a | 37 | { |
99c2e5ac | 38 | if (Fd.IsOpen() == false) |
0e72dd52 | 39 | { |
2ca99a0d | 40 | Buffer = 0; |
0e72dd52 | 41 | Start = End = Buffer = 0; |
99c2e5ac | 42 | Done = true; |
0e72dd52 AL |
43 | iOffset = 0; |
44 | return; | |
45 | } | |
46 | ||
2ca99a0d MV |
47 | Buffer = new char[Size]; |
48 | Start = End = Buffer; | |
49 | Done = false; | |
dcb79bae | 50 | iOffset = 0; |
2ca99a0d | 51 | Fill(); |
578bfd0a AL |
52 | } |
53 | /*}}}*/ | |
b2e465d6 | 54 | // TagFile::~pkgTagFile - Destructor /*{{{*/ |
29f7b36c AL |
55 | // --------------------------------------------------------------------- |
56 | /* */ | |
57 | pkgTagFile::~pkgTagFile() | |
58 | { | |
2ca99a0d | 59 | delete [] Buffer; |
29f7b36c AL |
60 | } |
61 | /*}}}*/ | |
578bfd0a AL |
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 | { | |
0852eaef AL |
67 | if (Tag.Scan(Start,End - Start) == false) |
68 | { | |
99c2e5ac MV |
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()); | |
613f9499 | 75 | } |
dcb79bae AL |
76 | Start += Tag.size(); |
77 | iOffset += Tag.size(); | |
b2e465d6 AL |
78 | |
79 | Tag.Trim(); | |
99c2e5ac MV |
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 | ||
578bfd0a AL |
124 | return true; |
125 | } | |
126 | /*}}}*/ | |
ad00ae81 AL |
127 | // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/ |
128 | // --------------------------------------------------------------------- | |
03e39e59 AL |
129 | /* This jumps to a pre-recorded file location and reads the record |
130 | that is there */ | |
ad00ae81 AL |
131 | bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset) |
132 | { | |
b2e465d6 AL |
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 | ||
2ca99a0d | 142 | // Reposition and reload.. |
ad00ae81 | 143 | iOffset = Offset; |
2ca99a0d MV |
144 | Done = false; |
145 | if (Fd.Seek(Offset) == false) | |
146 | return false; | |
147 | End = Start = Buffer; | |
99c2e5ac | 148 | |
2ca99a0d MV |
149 | if (Fill() == false) |
150 | return false; | |
99c2e5ac | 151 | |
2ca99a0d MV |
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; | |
0852eaef | 158 | |
99c2e5ac | 159 | if (Tag.Scan(Start,End - Start) == false) |
b2e465d6 | 160 | return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str()); |
06bba740 | 161 | |
ad00ae81 AL |
162 | return true; |
163 | } | |
164 | /*}}}*/ | |
578bfd0a AL |
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 | |
c1a22377 | 168 | indexes the tags in the section. This very simple hash function for the |
2c4eafff | 169 | last 8 letters gives very good performance on the debian package files */ |
b2e465d6 AL |
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++) | |
2c4eafff | 174 | Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1); |
b2e465d6 AL |
175 | return Res & 0xFF; |
176 | } | |
177 | ||
0852eaef | 178 | bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) |
578bfd0a AL |
179 | { |
180 | const char *End = Start + MaxLength; | |
181 | Stop = Section = Start; | |
c1a22377 | 182 | memset(AlphaIndexes,0,sizeof(AlphaIndexes)); |
c7b5ce1c | 183 | |
2ca99a0d | 184 | if (Stop == 0) |
0852eaef | 185 | return false; |
578bfd0a AL |
186 | |
187 | TagCount = 0; | |
fd71171a | 188 | while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End) |
578bfd0a | 189 | { |
90d64280 | 190 | // Start a new index and add it to the hash |
c1a22377 AL |
191 | if (isspace(Stop[0]) == 0) |
192 | { | |
193 | Indexes[TagCount++] = Stop - Section; | |
b2e465d6 | 194 | AlphaIndexes[AlphaHash(Stop,End)] = TagCount; |
c1a22377 | 195 | } |
0a8e3465 | 196 | |
c1a22377 | 197 | Stop = (const char *)memchr(Stop,'\n',End - Stop); |
0a8e3465 | 198 | |
c1a22377 | 199 | if (Stop == 0) |
0852eaef | 200 | return false; |
138d4b3d | 201 | |
b8b7c37d | 202 | for (; Stop+1 < End && Stop[1] == '\r'; Stop++); |
c1a22377 | 203 | |
f3bcc383 AL |
204 | // Double newline marks the end of the record |
205 | if (Stop+1 < End && Stop[1] == '\n') | |
578bfd0a | 206 | { |
578bfd0a | 207 | Indexes[TagCount] = Stop - Section; |
fd71171a | 208 | for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++); |
0852eaef | 209 | return true; |
578bfd0a AL |
210 | } |
211 | ||
c1a22377 AL |
212 | Stop++; |
213 | } | |
138d4b3d | 214 | |
0852eaef | 215 | return false; |
578bfd0a AL |
216 | } |
217 | /*}}}*/ | |
b2e465d6 AL |
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 | /*}}}*/ | |
578bfd0a AL |
226 | // TagSection::Find - Locate a tag /*{{{*/ |
227 | // --------------------------------------------------------------------- | |
228 | /* This searches the section for a tag that matches the given string. */ | |
b2e465d6 | 229 | bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const |
578bfd0a AL |
230 | { |
231 | unsigned int Length = strlen(Tag); | |
b2e465d6 | 232 | unsigned int I = AlphaIndexes[AlphaHash(Tag)]; |
c1a22377 AL |
233 | if (I == 0) |
234 | return false; | |
235 | I--; | |
236 | ||
237 | for (unsigned int Counter = 0; Counter != TagCount; Counter++, | |
238 | I = (I+1)%TagCount) | |
578bfd0a | 239 | { |
c1a22377 AL |
240 | const char *St; |
241 | St = Section + Indexes[I]; | |
242 | if (strncasecmp(Tag,St,Length) != 0) | |
578bfd0a AL |
243 | continue; |
244 | ||
b2e465d6 AL |
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 | ||
578bfd0a | 278 | // Make sure the colon is in the right place |
c1a22377 | 279 | const char *C = St + Length; |
578bfd0a AL |
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]; | |
06bba740 AL |
287 | if (Start >= End) |
288 | return _error->Error("Internal parsing error"); | |
289 | ||
578bfd0a AL |
290 | for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++); |
291 | for (; isspace(End[-1]) != 0 && End > Start; End--); | |
06bba740 | 292 | |
578bfd0a AL |
293 | return true; |
294 | } | |
c1a22377 | 295 | |
578bfd0a AL |
296 | Start = End = 0; |
297 | return false; | |
298 | } | |
299 | /*}}}*/ | |
0e66b144 | 300 | // TagSection::FindS - Find a string /*{{{*/ |
a05599f1 AL |
301 | // --------------------------------------------------------------------- |
302 | /* */ | |
b2e465d6 | 303 | string pkgTagSection::FindS(const char *Tag) const |
a05599f1 AL |
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 | /* */ | |
b2e465d6 | 315 | signed int pkgTagSection::FindI(const char *Tag,signed long Default) const |
a05599f1 AL |
316 | { |
317 | const char *Start; | |
b0b4efb9 AL |
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, | |
b2e465d6 | 340 | unsigned long Flag) const |
b0b4efb9 AL |
341 | { |
342 | const char *Start; | |
343 | const char *Stop; | |
344 | if (Find(Tag,Start,Stop) == false) | |
345 | return true; | |
a05599f1 | 346 | |
b0b4efb9 AL |
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: | |
b2e465d6 | 358 | _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str()); |
b0b4efb9 AL |
359 | return true; |
360 | } | |
361 | return true; | |
a05599f1 AL |
362 | } |
363 | /*}}}*/ | |
b2e465d6 AL |
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 | "Architecture", | |
382 | "Source", | |
383 | "Version", | |
384 | "Revision", // Obsolete | |
385 | "Config-Version", // Obsolete | |
386 | "Replaces", | |
387 | "Provides", | |
388 | "Depends", | |
389 | "Pre-Depends", | |
390 | "Recommends", | |
391 | "Suggests", | |
392 | "Conflicts", | |
393 | "Conffiles", | |
394 | "Filename", | |
395 | "Size", | |
396 | "MD5Sum", | |
cde41ae8 MV |
397 | "SHA1", |
398 | "SHA256", | |
b2e465d6 AL |
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 | /*}}}*/ |