tagfile.cc 16 KB

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