tagfile.cc 17 KB

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