tagfile.cc 16 KB

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