tagfile.cc 20 KB

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