tagfile.cc 18 KB

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