tagfile.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.26 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. Fast scanner for RFC-822 type header information
  6. This uses a rotating buffer to load the package information into.
  7. The scanner runs over it and isolates and indexes a single section.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #ifdef __GNUG__
  12. #pragma implementation "apt-pkg/tagfile.h"
  13. #endif
  14. #include <apt-pkg/tagfile.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apti18n.h>
  18. #include <string>
  19. #include <stdio.h>
  20. /*}}}*/
  21. // TagFile::pkgTagFile - Constructor /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* */
  24. pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) : Fd(*pFd), Size(Size)
  25. {
  26. Buffer = new char[Size];
  27. Start = End = Buffer;
  28. Left = Fd.Size();
  29. TotalSize = Fd.Size();
  30. iOffset = 0;
  31. Fill();
  32. }
  33. /*}}}*/
  34. // TagFile::~pkgTagFile - Destructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgTagFile::~pkgTagFile()
  38. {
  39. delete [] Buffer;
  40. }
  41. /*}}}*/
  42. // TagFile::Step - Advance to the next section /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* If the Section Scanner fails we refill the buffer and try again. */
  45. bool pkgTagFile::Step(pkgTagSection &Tag)
  46. {
  47. if (Tag.Scan(Start,End - Start) == false)
  48. {
  49. if (Fill() == false)
  50. return false;
  51. if (Tag.Scan(Start,End - Start) == false)
  52. return _error->Error(_("Unable to parse package file %s (1)"),Fd.Name().c_str());
  53. }
  54. Start += Tag.size();
  55. iOffset += Tag.size();
  56. Tag.Trim();
  57. return true;
  58. }
  59. /*}}}*/
  60. // TagFile::Fill - Top up the buffer /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* This takes the bit at the end of the buffer and puts it at the start
  63. then fills the rest from the file */
  64. bool pkgTagFile::Fill()
  65. {
  66. unsigned long EndSize = End - Start;
  67. memmove(Buffer,Start,EndSize);
  68. Start = Buffer;
  69. End = Buffer + EndSize;
  70. if (Left == 0)
  71. {
  72. if (EndSize <= 3)
  73. return false;
  74. if (Size - (End - Buffer) < 4)
  75. return true;
  76. // Append a double new line if one does not exist
  77. unsigned int LineCount = 0;
  78. for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--)
  79. if (*E == '\n')
  80. LineCount++;
  81. for (; LineCount < 2; LineCount++)
  82. *End++ = '\n';
  83. return true;
  84. }
  85. // See if only a bit of the file is left
  86. if (Left < Size - (End - Buffer))
  87. {
  88. if (Fd.Read(End,Left) == false)
  89. return false;
  90. End += Left;
  91. Left = 0;
  92. }
  93. else
  94. {
  95. if (Fd.Read(End,Size - (End - Buffer)) == false)
  96. return false;
  97. Left -= Size - (End - Buffer);
  98. End = Buffer + Size;
  99. }
  100. return true;
  101. }
  102. /*}}}*/
  103. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  104. // ---------------------------------------------------------------------
  105. /* This jumps to a pre-recorded file location and reads the record
  106. that is there */
  107. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
  108. {
  109. // We are within a buffer space of the next hit..
  110. if (Offset >= iOffset && iOffset + (End - Start) > Offset)
  111. {
  112. unsigned long Dist = Offset - iOffset;
  113. Start += Dist;
  114. iOffset += Dist;
  115. return Step(Tag);
  116. }
  117. // Reposition and reload..
  118. iOffset = Offset;
  119. Left = TotalSize - Offset;
  120. if (Fd.Seek(Offset) == false)
  121. return false;
  122. End = Start = Buffer;
  123. if (Fill() == false)
  124. return false;
  125. if (Tag.Scan(Start,End - Start) == true)
  126. return true;
  127. // This appends a double new line (for the real eof handling)
  128. if (Fill() == false)
  129. return false;
  130. if (Tag.Scan(Start,End - Start) == false)
  131. return _error->Error(_("Unable to parse package file %s (2)"),Fd.Name().c_str());
  132. return true;
  133. }
  134. /*}}}*/
  135. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  136. // ---------------------------------------------------------------------
  137. /* This looks for the first double new line in the data stream. It also
  138. indexes the tags in the section. This very simple hash function for the
  139. first 3 letters gives very good performance on the debian package files */
  140. inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
  141. {
  142. unsigned long Res = 0;
  143. for (; Text != End && *Text != ':' && *Text != 0; Text++)
  144. Res = (unsigned long)(*Text) ^ (Res << 2);
  145. return Res & 0xFF;
  146. }
  147. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  148. {
  149. const char *End = Start + MaxLength;
  150. Stop = Section = Start;
  151. memset(AlphaIndexes,0,sizeof(AlphaIndexes));
  152. if (Stop == 0)
  153. return false;
  154. TagCount = 0;
  155. while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
  156. {
  157. // Start a new index and add it to the hash
  158. if (isspace(Stop[0]) == 0)
  159. {
  160. Indexes[TagCount++] = Stop - Section;
  161. AlphaIndexes[AlphaHash(Stop,End)] = TagCount;
  162. }
  163. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  164. if (Stop == 0)
  165. return false;
  166. for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
  167. // Double newline marks the end of the record
  168. if (Stop+1 < End && Stop[1] == '\n')
  169. {
  170. Indexes[TagCount] = Stop - Section;
  171. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  172. return true;
  173. }
  174. Stop++;
  175. }
  176. return false;
  177. }
  178. /*}}}*/
  179. // TagSection::Trim - Trim off any trailing garbage /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* There should be exactly 1 newline at the end of the buffer, no more. */
  182. void pkgTagSection::Trim()
  183. {
  184. for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
  185. }
  186. /*}}}*/
  187. // TagSection::Find - Locate a tag /*{{{*/
  188. // ---------------------------------------------------------------------
  189. /* This searches the section for a tag that matches the given string. */
  190. bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const
  191. {
  192. unsigned int Length = strlen(Tag);
  193. unsigned int I = AlphaIndexes[AlphaHash(Tag)];
  194. if (I == 0)
  195. return false;
  196. I--;
  197. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  198. I = (I+1)%TagCount)
  199. {
  200. const char *St;
  201. St = Section + Indexes[I];
  202. if (strncasecmp(Tag,St,Length) != 0)
  203. continue;
  204. // Make sure the colon is in the right place
  205. const char *C = St + Length;
  206. for (; isspace(*C) != 0; C++);
  207. if (*C != ':')
  208. continue;
  209. Pos = I;
  210. return true;
  211. }
  212. Pos = 0;
  213. return false;
  214. }
  215. /*}}}*/
  216. // TagSection::Find - Locate a tag /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* This searches the section for a tag that matches the given string. */
  219. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  220. const char *&End) const
  221. {
  222. unsigned int Length = strlen(Tag);
  223. unsigned int I = AlphaIndexes[AlphaHash(Tag)];
  224. if (I == 0)
  225. return false;
  226. I--;
  227. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  228. I = (I+1)%TagCount)
  229. {
  230. const char *St;
  231. St = Section + Indexes[I];
  232. if (strncasecmp(Tag,St,Length) != 0)
  233. continue;
  234. // Make sure the colon is in the right place
  235. const char *C = St + Length;
  236. for (; isspace(*C) != 0; C++);
  237. if (*C != ':')
  238. continue;
  239. // Strip off the gunk from the start end
  240. Start = C;
  241. End = Section + Indexes[I+1];
  242. if (Start >= End)
  243. return _error->Error("Internal parsing error");
  244. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  245. for (; isspace(End[-1]) != 0 && End > Start; End--);
  246. return true;
  247. }
  248. Start = End = 0;
  249. return false;
  250. }
  251. /*}}}*/
  252. // TagSection::FindS - Find a string /*{{{*/
  253. // ---------------------------------------------------------------------
  254. /* */
  255. string pkgTagSection::FindS(const char *Tag) const
  256. {
  257. const char *Start;
  258. const char *End;
  259. if (Find(Tag,Start,End) == false)
  260. return string();
  261. return string(Start,End);
  262. }
  263. /*}}}*/
  264. // TagSection::FindI - Find an integer /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* */
  267. signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
  268. {
  269. const char *Start;
  270. const char *Stop;
  271. if (Find(Tag,Start,Stop) == false)
  272. return Default;
  273. // Copy it into a temp buffer so we can use strtol
  274. char S[300];
  275. if ((unsigned)(Stop - Start) >= sizeof(S))
  276. return Default;
  277. strncpy(S,Start,Stop-Start);
  278. S[Stop - Start] = 0;
  279. char *End;
  280. signed long Result = strtol(S,&End,10);
  281. if (S == End)
  282. return Default;
  283. return Result;
  284. }
  285. /*}}}*/
  286. // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
  287. // ---------------------------------------------------------------------
  288. /* The bits marked in Flag are masked on/off in Flags */
  289. bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
  290. unsigned long Flag) const
  291. {
  292. const char *Start;
  293. const char *Stop;
  294. if (Find(Tag,Start,Stop) == false)
  295. return true;
  296. switch (StringToBool(string(Start,Stop)))
  297. {
  298. case 0:
  299. Flags &= ~Flag;
  300. return true;
  301. case 1:
  302. Flags |= Flag;
  303. return true;
  304. default:
  305. _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
  306. return true;
  307. }
  308. return true;
  309. }
  310. /*}}}*/
  311. // TFRewrite - Rewrite a control record /*{{{*/
  312. // ---------------------------------------------------------------------
  313. /* This writes the control record to stdout rewriting it as necessary. The
  314. override map item specificies the rewriting rules to follow. This also
  315. takes the time to sort the feild list. */
  316. /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
  317. array. */
  318. static const char *iTFRewritePackageOrder[] = {
  319. "Package",
  320. "Essential",
  321. "Status",
  322. "Priority",
  323. "Section",
  324. "Installed-Size",
  325. "Maintainer",
  326. "Architecture",
  327. "Source",
  328. "Version",
  329. "Revision", // Obsolete
  330. "Config-Version", // Obsolete
  331. "Replaces",
  332. "Provides",
  333. "Depends",
  334. "Pre-Depends",
  335. "Recommends",
  336. "Suggests",
  337. "Conflicts",
  338. "Conffiles",
  339. "Filename",
  340. "Size",
  341. "MD5Sum",
  342. "MSDOS-Filename", // Obsolete
  343. "Description",
  344. 0};
  345. static const char *iTFRewriteSourceOrder[] = {"Package",
  346. "Source",
  347. "Binary",
  348. "Version",
  349. "Priority",
  350. "Section",
  351. "Maintainer",
  352. "Build-Depends",
  353. "Build-Depends-Indep",
  354. "Build-Conflicts",
  355. "Build-Conflicts-Indep",
  356. "Architecture",
  357. "Standards-Version",
  358. "Format",
  359. "Directory",
  360. "Files",
  361. 0};
  362. /* Two levels of initialization are used because gcc will set the symbol
  363. size of an array to the length of the array, causing dynamic relinking
  364. errors. Doing this makes the symbol size constant */
  365. const char **TFRewritePackageOrder = iTFRewritePackageOrder;
  366. const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
  367. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  368. TFRewriteData *Rewrite)
  369. {
  370. unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
  371. for (unsigned I = 0; I != 256; I++)
  372. Visited[I] = 0;
  373. // Set new tag up as necessary.
  374. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  375. {
  376. if (Rewrite[J].NewTag == 0)
  377. Rewrite[J].NewTag = Rewrite[J].Tag;
  378. }
  379. // Write all all of the tags, in order.
  380. for (unsigned int I = 0; Order[I] != 0; I++)
  381. {
  382. bool Rewritten = false;
  383. // See if this is a field that needs to be rewritten
  384. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  385. {
  386. if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
  387. {
  388. Visited[J] |= 2;
  389. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  390. {
  391. if (isspace(Rewrite[J].Rewrite[0]))
  392. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  393. else
  394. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  395. }
  396. Rewritten = true;
  397. break;
  398. }
  399. }
  400. // See if it is in the fragment
  401. unsigned Pos;
  402. if (Tags.Find(Order[I],Pos) == false)
  403. continue;
  404. Visited[Pos] |= 1;
  405. if (Rewritten == true)
  406. continue;
  407. /* Write out this element, taking a moment to rewrite the tag
  408. in case of changes of case. */
  409. const char *Start;
  410. const char *Stop;
  411. Tags.Get(Start,Stop,Pos);
  412. if (fputs(Order[I],Output) < 0)
  413. return _error->Errno("fputs","IO Error to output");
  414. Start += strlen(Order[I]);
  415. if (fwrite(Start,Stop - Start,1,Output) != 1)
  416. return _error->Errno("fwrite","IO Error to output");
  417. if (Stop[-1] != '\n')
  418. fprintf(Output,"\n");
  419. }
  420. // Now write all the old tags that were missed.
  421. for (unsigned int I = 0; I != Tags.Count(); I++)
  422. {
  423. if ((Visited[I] & 1) == 1)
  424. continue;
  425. const char *Start;
  426. const char *Stop;
  427. Tags.Get(Start,Stop,I);
  428. const char *End = Start;
  429. for (; End < Stop && *End != ':'; End++);
  430. // See if this is a field that needs to be rewritten
  431. bool Rewritten = false;
  432. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  433. {
  434. if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
  435. {
  436. Visited[J] |= 2;
  437. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  438. {
  439. if (isspace(Rewrite[J].Rewrite[0]))
  440. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  441. else
  442. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  443. }
  444. Rewritten = true;
  445. break;
  446. }
  447. }
  448. if (Rewritten == true)
  449. continue;
  450. // Write out this element
  451. if (fwrite(Start,Stop - Start,1,Output) != 1)
  452. return _error->Errno("fwrite","IO Error to output");
  453. if (Stop[-1] != '\n')
  454. fprintf(Output,"\n");
  455. }
  456. // Now write all the rewrites that were missed
  457. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  458. {
  459. if ((Visited[J] & 2) == 2)
  460. continue;
  461. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  462. {
  463. if (isspace(Rewrite[J].Rewrite[0]))
  464. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  465. else
  466. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  467. }
  468. }
  469. return true;
  470. }
  471. /*}}}*/