tagfile.cc 17 KB

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