tagfile.cc 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 <list>
  17. #include <string>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <apti18n.h>
  23. /*}}}*/
  24. using std::string;
  25. class APT_HIDDEN pkgTagFilePrivate /*{{{*/
  26. {
  27. public:
  28. void Reset(FileFd * const pFd, unsigned long long const pSize, pkgTagFile::Flags const pFlags)
  29. {
  30. if (Buffer != NULL)
  31. free(Buffer);
  32. Buffer = NULL;
  33. Fd = pFd;
  34. Flags = pFlags;
  35. Start = NULL;
  36. End = NULL;
  37. Done = false;
  38. iOffset = 0;
  39. Size = pSize;
  40. isCommentedLine = false;
  41. chunks.clear();
  42. }
  43. pkgTagFilePrivate(FileFd * const pFd, unsigned long long const Size, pkgTagFile::Flags const pFlags) : Buffer(NULL)
  44. {
  45. Reset(pFd, Size, pFlags);
  46. }
  47. FileFd * Fd;
  48. pkgTagFile::Flags Flags;
  49. char *Buffer;
  50. char *Start;
  51. char *End;
  52. bool Done;
  53. unsigned long long iOffset;
  54. unsigned long long Size;
  55. bool isCommentedLine;
  56. struct FileChunk
  57. {
  58. bool const good;
  59. size_t length;
  60. FileChunk(bool const pgood, size_t const plength) : good(pgood), length(plength) {}
  61. };
  62. std::list<FileChunk> chunks;
  63. ~pkgTagFilePrivate()
  64. {
  65. if (Buffer != NULL)
  66. free(Buffer);
  67. }
  68. };
  69. /*}}}*/
  70. class APT_HIDDEN pkgTagSectionPrivate /*{{{*/
  71. {
  72. public:
  73. pkgTagSectionPrivate()
  74. {
  75. }
  76. struct TagData {
  77. unsigned int StartTag;
  78. unsigned int EndTag;
  79. unsigned int StartValue;
  80. unsigned int NextInBucket;
  81. explicit TagData(unsigned int const StartTag) : StartTag(StartTag), EndTag(0), StartValue(0), NextInBucket(0) {}
  82. };
  83. std::vector<TagData> Tags;
  84. };
  85. /*}}}*/
  86. static unsigned long AlphaHash(const char *Text, size_t Length) /*{{{*/
  87. {
  88. /* This very simple hash function for the last 8 letters gives
  89. very good performance on the debian package files */
  90. if (Length > 8)
  91. {
  92. Text += (Length - 8);
  93. Length = 8;
  94. }
  95. unsigned long Res = 0;
  96. for (size_t i = 0; i < Length; ++i)
  97. Res = ((unsigned long)(Text[i]) & 0xDF) ^ (Res << 1);
  98. return Res & 0xFF;
  99. }
  100. /*}}}*/
  101. // TagFile::pkgTagFile - Constructor /*{{{*/
  102. pkgTagFile::pkgTagFile(FileFd * const pFd,pkgTagFile::Flags const pFlags, unsigned long long const Size)
  103. : d(new pkgTagFilePrivate(pFd, Size + 4, pFlags))
  104. {
  105. Init(pFd, pFlags, Size);
  106. }
  107. pkgTagFile::pkgTagFile(FileFd * const pFd,unsigned long long const Size)
  108. : pkgTagFile(pFd, pkgTagFile::STRICT, Size)
  109. {
  110. }
  111. void pkgTagFile::Init(FileFd * const pFd, pkgTagFile::Flags const pFlags, unsigned long long Size)
  112. {
  113. /* The size is increased by 4 because if we start with the Size of the
  114. filename we need to try to read 1 char more to see an EOF faster, 1
  115. char the end-pointer can be on and maybe 2 newlines need to be added
  116. to the end of the file -> 4 extra chars */
  117. Size += 4;
  118. d->Reset(pFd, Size, pFlags);
  119. if (d->Fd->IsOpen() == false)
  120. d->Start = d->End = d->Buffer = 0;
  121. else
  122. d->Buffer = (char*)malloc(sizeof(char) * Size);
  123. if (d->Buffer == NULL)
  124. d->Done = true;
  125. else
  126. d->Done = false;
  127. d->Start = d->End = d->Buffer;
  128. d->iOffset = 0;
  129. if (d->Done == false)
  130. Fill();
  131. }
  132. void pkgTagFile::Init(FileFd * const pFd,unsigned long long Size)
  133. {
  134. Init(pFd, pkgTagFile::STRICT, Size);
  135. }
  136. /*}}}*/
  137. // TagFile::~pkgTagFile - Destructor /*{{{*/
  138. pkgTagFile::~pkgTagFile()
  139. {
  140. delete d;
  141. }
  142. /*}}}*/
  143. // TagFile::Offset - Return the current offset in the buffer /*{{{*/
  144. APT_PURE unsigned long pkgTagFile::Offset()
  145. {
  146. return d->iOffset;
  147. }
  148. /*}}}*/
  149. // TagFile::Resize - Resize the internal buffer /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* Resize the internal buffer (double it in size). Fail if a maximum size
  152. * size is reached.
  153. */
  154. bool pkgTagFile::Resize()
  155. {
  156. // fail is the buffer grows too big
  157. if(d->Size > 1024*1024+1)
  158. return false;
  159. return Resize(d->Size * 2);
  160. }
  161. bool pkgTagFile::Resize(unsigned long long const newSize)
  162. {
  163. unsigned long long const EndSize = d->End - d->Start;
  164. // get new buffer and use it
  165. char* const newBuffer = static_cast<char*>(realloc(d->Buffer, sizeof(char) * newSize));
  166. if (newBuffer == NULL)
  167. return false;
  168. d->Buffer = newBuffer;
  169. d->Size = newSize;
  170. // update the start/end pointers to the new buffer
  171. d->Start = d->Buffer;
  172. d->End = d->Start + EndSize;
  173. return true;
  174. }
  175. /*}}}*/
  176. // TagFile::Step - Advance to the next section /*{{{*/
  177. // ---------------------------------------------------------------------
  178. /* If the Section Scanner fails we refill the buffer and try again.
  179. * If that fails too, double the buffer size and try again until a
  180. * maximum buffer is reached.
  181. */
  182. bool pkgTagFile::Step(pkgTagSection &Tag)
  183. {
  184. if(Tag.Scan(d->Start,d->End - d->Start) == false)
  185. {
  186. do
  187. {
  188. if (Fill() == false)
  189. return false;
  190. if(Tag.Scan(d->Start,d->End - d->Start, false))
  191. break;
  192. if (Resize() == false)
  193. return _error->Error(_("Unable to parse package file %s (%d)"),
  194. d->Fd->Name().c_str(), 1);
  195. } while (Tag.Scan(d->Start,d->End - d->Start, false) == false);
  196. }
  197. size_t tagSize = Tag.size();
  198. d->Start += tagSize;
  199. if ((d->Flags & pkgTagFile::SUPPORT_COMMENTS) == 0)
  200. d->iOffset += tagSize;
  201. else
  202. {
  203. auto first = d->chunks.begin();
  204. for (; first != d->chunks.end(); ++first)
  205. {
  206. if (first->good == false)
  207. d->iOffset += first->length;
  208. else
  209. {
  210. if (tagSize < first->length)
  211. {
  212. first->length -= tagSize;
  213. d->iOffset += tagSize;
  214. break;
  215. }
  216. else
  217. {
  218. tagSize -= first->length;
  219. d->iOffset += first->length;
  220. }
  221. }
  222. }
  223. d->chunks.erase(d->chunks.begin(), first);
  224. }
  225. Tag.Trim();
  226. return true;
  227. }
  228. /*}}}*/
  229. // TagFile::Fill - Top up the buffer /*{{{*/
  230. // ---------------------------------------------------------------------
  231. /* This takes the bit at the end of the buffer and puts it at the start
  232. then fills the rest from the file */
  233. static bool FillBuffer(pkgTagFilePrivate * const d)
  234. {
  235. unsigned long long Actual = 0;
  236. // See if only a bit of the file is left
  237. unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1);
  238. if (d->Fd->Read(d->End, dataSize, &Actual) == false)
  239. return false;
  240. if (Actual != dataSize)
  241. d->Done = true;
  242. d->End += Actual;
  243. return true;
  244. }
  245. static void RemoveCommentsFromBuffer(pkgTagFilePrivate * const d)
  246. {
  247. // look for valid comments in the buffer
  248. char * good_start = nullptr, * bad_start = nullptr;
  249. char * current = d->Start;
  250. if (d->isCommentedLine == false)
  251. {
  252. if (d->Start == d->Buffer)
  253. {
  254. // the start of the buffer is a newline as a record can't start
  255. // in the middle of a line by definition.
  256. if (*d->Start == '#')
  257. {
  258. d->isCommentedLine = true;
  259. ++current;
  260. if (current > d->End)
  261. d->chunks.emplace_back(false, 1);
  262. }
  263. }
  264. if (d->isCommentedLine == false)
  265. good_start = d->Start;
  266. else
  267. bad_start = d->Start;
  268. }
  269. else
  270. bad_start = d->Start;
  271. std::vector<std::pair<char*, size_t>> good_parts;
  272. while (current <= d->End)
  273. {
  274. size_t const restLength = (d->End - current) + 1;
  275. if (d->isCommentedLine == false)
  276. {
  277. current = static_cast<char*>(memchr(current, '#', restLength));
  278. if (current == nullptr)
  279. {
  280. size_t const goodLength = d->End - good_start;
  281. d->chunks.emplace_back(true, goodLength);
  282. if (good_start != d->Start)
  283. good_parts.push_back(std::make_pair(good_start, goodLength));
  284. break;
  285. }
  286. bad_start = current;
  287. --current;
  288. // ensure that this is really a comment and not a '#' in the middle of a line
  289. if (*current == '\n')
  290. {
  291. size_t const goodLength = (current - good_start) + 1;
  292. d->chunks.emplace_back(true, goodLength);
  293. good_parts.push_back(std::make_pair(good_start, goodLength));
  294. good_start = nullptr;
  295. d->isCommentedLine = true;
  296. }
  297. current += 2;
  298. }
  299. else // the current line is a comment
  300. {
  301. current = static_cast<char*>(memchr(current, '\n', restLength));
  302. if (current == nullptr)
  303. {
  304. d->chunks.emplace_back(false, (d->End - bad_start));
  305. break;
  306. }
  307. ++current;
  308. // is the next line a comment, too?
  309. if (current > d->End || *current != '#')
  310. {
  311. d->chunks.emplace_back(false, (current - bad_start));
  312. good_start = current;
  313. bad_start = nullptr;
  314. d->isCommentedLine = false;
  315. }
  316. ++current;
  317. }
  318. }
  319. if (good_parts.empty() == false)
  320. {
  321. // we found comments, so move later parts over them
  322. current = d->Start;
  323. for (auto const &good: good_parts)
  324. {
  325. memmove(current, good.first, good.second);
  326. current += good.second;
  327. }
  328. d->End = current;
  329. }
  330. if (d->isCommentedLine == true)
  331. {
  332. // deal with a buffer containing only comments
  333. // or an (unfinished) comment at the end
  334. if (good_parts.empty() == true)
  335. d->End = d->Start;
  336. else
  337. d->Start = d->End;
  338. }
  339. else
  340. {
  341. // the buffer was all comment, but ended with the buffer
  342. if (good_parts.empty() == true && good_start >= d->End)
  343. d->End = d->Start;
  344. else
  345. d->Start = d->End;
  346. }
  347. }
  348. bool pkgTagFile::Fill()
  349. {
  350. unsigned long long const EndSize = d->End - d->Start;
  351. if (EndSize != 0)
  352. {
  353. memmove(d->Buffer,d->Start,EndSize);
  354. d->Start = d->End = d->Buffer + EndSize;
  355. }
  356. else
  357. d->Start = d->End = d->Buffer;
  358. unsigned long long Actual = 0;
  359. while (d->Done == false && d->Size > (Actual + 1))
  360. {
  361. if (FillBuffer(d) == false)
  362. return false;
  363. if ((d->Flags & pkgTagFile::SUPPORT_COMMENTS) != 0)
  364. RemoveCommentsFromBuffer(d);
  365. Actual = d->End - d->Buffer;
  366. }
  367. d->Start = d->Buffer;
  368. if (d->Done == true)
  369. {
  370. if (EndSize <= 3 && Actual == 0)
  371. return false;
  372. if (d->Size - (d->End - d->Buffer) < 4)
  373. return true;
  374. // Append a double new line if one does not exist
  375. unsigned int LineCount = 0;
  376. for (const char *E = d->End - 1; E - d->End < 6 && (*E == '\n' || *E == '\r'); E--)
  377. if (*E == '\n')
  378. ++LineCount;
  379. if (LineCount < 2)
  380. {
  381. if (static_cast<unsigned long long>(d->End - d->Buffer) >= d->Size)
  382. Resize(d->Size + 3);
  383. for (; LineCount < 2; ++LineCount)
  384. *d->End++ = '\n';
  385. }
  386. }
  387. return true;
  388. }
  389. /*}}}*/
  390. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  391. // ---------------------------------------------------------------------
  392. /* This jumps to a pre-recorded file location and reads the record
  393. that is there */
  394. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset)
  395. {
  396. if ((d->Flags & pkgTagFile::SUPPORT_COMMENTS) == 0 &&
  397. // We are within a buffer space of the next hit..
  398. Offset >= d->iOffset && d->iOffset + (d->End - d->Start) > Offset)
  399. {
  400. unsigned long long Dist = Offset - d->iOffset;
  401. d->Start += Dist;
  402. d->iOffset += Dist;
  403. // if we have seen the end, don't ask for more
  404. if (d->Done == true)
  405. return Tag.Scan(d->Start, d->End - d->Start);
  406. else
  407. return Step(Tag);
  408. }
  409. // Reposition and reload..
  410. d->iOffset = Offset;
  411. d->Done = false;
  412. if (d->Fd->Seek(Offset) == false)
  413. return false;
  414. d->End = d->Start = d->Buffer;
  415. d->isCommentedLine = false;
  416. d->chunks.clear();
  417. if (Fill() == false)
  418. return false;
  419. if (Tag.Scan(d->Start, d->End - d->Start) == true)
  420. return true;
  421. // This appends a double new line (for the real eof handling)
  422. if (Fill() == false)
  423. return false;
  424. if (Tag.Scan(d->Start, d->End - d->Start, false) == false)
  425. return _error->Error(_("Unable to parse package file %s (%d)"),d->Fd->Name().c_str(), 2);
  426. return true;
  427. }
  428. /*}}}*/
  429. // pkgTagSection::pkgTagSection - Constructor /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* */
  432. APT_IGNORE_DEPRECATED_PUSH
  433. pkgTagSection::pkgTagSection()
  434. : Section(0), d(new pkgTagSectionPrivate()), Stop(0)
  435. {
  436. memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
  437. }
  438. APT_IGNORE_DEPRECATED_POP
  439. /*}}}*/
  440. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  441. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const Restart)
  442. {
  443. Section = Start;
  444. const char *End = Start + MaxLength;
  445. if (Restart == false && d->Tags.empty() == false)
  446. {
  447. Stop = Section + d->Tags.back().StartTag;
  448. if (End <= Stop)
  449. return false;
  450. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  451. if (Stop == NULL)
  452. return false;
  453. ++Stop;
  454. }
  455. else
  456. {
  457. Stop = Section;
  458. if (d->Tags.empty() == false)
  459. {
  460. memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
  461. d->Tags.clear();
  462. }
  463. d->Tags.reserve(0x100);
  464. }
  465. unsigned int TagCount = d->Tags.size();
  466. if (Stop == 0)
  467. return false;
  468. pkgTagSectionPrivate::TagData lastTagData(0);
  469. lastTagData.EndTag = 0;
  470. unsigned long lastTagHash = 0;
  471. while (Stop < End)
  472. {
  473. TrimRecord(true,End);
  474. // this can happen when TrimRecord trims away the entire Record
  475. // (e.g. because it just contains comments)
  476. if(Stop == End)
  477. return true;
  478. // Start a new index and add it to the hash
  479. if (isspace_ascii(Stop[0]) == 0)
  480. {
  481. // store the last found tag
  482. if (lastTagData.EndTag != 0)
  483. {
  484. if (AlphaIndexes[lastTagHash] != 0)
  485. lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
  486. APT_IGNORE_DEPRECATED_PUSH
  487. AlphaIndexes[lastTagHash] = TagCount;
  488. APT_IGNORE_DEPRECATED_POP
  489. d->Tags.push_back(lastTagData);
  490. }
  491. APT_IGNORE_DEPRECATED(++TagCount;)
  492. lastTagData = pkgTagSectionPrivate::TagData(Stop - Section);
  493. // find the colon separating tag and value
  494. char const * Colon = (char const *) memchr(Stop, ':', End - Stop);
  495. if (Colon == NULL)
  496. return false;
  497. // find the end of the tag (which might or might not be the colon)
  498. char const * EndTag = Colon;
  499. --EndTag;
  500. for (; EndTag > Stop && isspace_ascii(*EndTag) != 0; --EndTag)
  501. ;
  502. ++EndTag;
  503. lastTagData.EndTag = EndTag - Section;
  504. lastTagHash = AlphaHash(Stop, EndTag - Stop);
  505. // find the beginning of the value
  506. Stop = Colon + 1;
  507. for (; Stop < End && isspace_ascii(*Stop) != 0; ++Stop)
  508. if (*Stop == '\n' && Stop[1] != ' ')
  509. break;
  510. if (Stop >= End)
  511. return false;
  512. lastTagData.StartValue = Stop - Section;
  513. }
  514. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  515. if (Stop == 0)
  516. return false;
  517. for (; Stop+1 < End && Stop[1] == '\r'; Stop++)
  518. /* nothing */
  519. ;
  520. // Double newline marks the end of the record
  521. if (Stop+1 < End && Stop[1] == '\n')
  522. {
  523. if (lastTagData.EndTag != 0)
  524. {
  525. if (AlphaIndexes[lastTagHash] != 0)
  526. lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
  527. APT_IGNORE_DEPRECATED(AlphaIndexes[lastTagHash] = TagCount;)
  528. d->Tags.push_back(lastTagData);
  529. }
  530. pkgTagSectionPrivate::TagData const td(Stop - Section);
  531. d->Tags.push_back(td);
  532. TrimRecord(false,End);
  533. return true;
  534. }
  535. Stop++;
  536. }
  537. return false;
  538. }
  539. /*}}}*/
  540. // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
  541. // ---------------------------------------------------------------------
  542. /* There should be exactly 2 newline at the end of the record, no more. */
  543. void pkgTagSection::TrimRecord(bool BeforeRecord, const char*& End)
  544. {
  545. if (BeforeRecord == true)
  546. return;
  547. for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
  548. }
  549. /*}}}*/
  550. // TagSection::Trim - Trim off any trailing garbage /*{{{*/
  551. // ---------------------------------------------------------------------
  552. /* There should be exactly 1 newline at the end of the buffer, no more. */
  553. void pkgTagSection::Trim()
  554. {
  555. for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
  556. }
  557. /*}}}*/
  558. // TagSection::Exists - return True if a tag exists /*{{{*/
  559. bool pkgTagSection::Exists(const char* const Tag) const
  560. {
  561. unsigned int tmp;
  562. return Find(Tag, tmp);
  563. }
  564. /*}}}*/
  565. // TagSection::Find - Locate a tag /*{{{*/
  566. // ---------------------------------------------------------------------
  567. /* This searches the section for a tag that matches the given string. */
  568. bool pkgTagSection::Find(const char *Tag,unsigned int &Pos) const
  569. {
  570. size_t const Length = strlen(Tag);
  571. unsigned int Bucket = AlphaIndexes[AlphaHash(Tag, Length)];
  572. if (Bucket == 0)
  573. return false;
  574. for (; Bucket != 0; Bucket = d->Tags[Bucket - 1].NextInBucket)
  575. {
  576. if ((d->Tags[Bucket - 1].EndTag - d->Tags[Bucket - 1].StartTag) != Length)
  577. continue;
  578. char const * const St = Section + d->Tags[Bucket - 1].StartTag;
  579. if (strncasecmp(Tag,St,Length) != 0)
  580. continue;
  581. Pos = Bucket - 1;
  582. return true;
  583. }
  584. Pos = 0;
  585. return false;
  586. }
  587. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  588. const char *&End) const
  589. {
  590. unsigned int Pos;
  591. if (Find(Tag, Pos) == false)
  592. return false;
  593. Start = Section + d->Tags[Pos].StartValue;
  594. // Strip off the gunk from the end
  595. End = Section + d->Tags[Pos + 1].StartTag;
  596. if (unlikely(Start > End))
  597. return _error->Error("Internal parsing error");
  598. for (; isspace_ascii(End[-1]) != 0 && End > Start; --End);
  599. return true;
  600. }
  601. /*}}}*/
  602. // TagSection::FindS - Find a string /*{{{*/
  603. string pkgTagSection::FindS(const char *Tag) const
  604. {
  605. const char *Start;
  606. const char *End;
  607. if (Find(Tag,Start,End) == false)
  608. return string();
  609. return string(Start,End);
  610. }
  611. /*}}}*/
  612. // TagSection::FindRawS - Find a string /*{{{*/
  613. string pkgTagSection::FindRawS(const char *Tag) const
  614. {
  615. unsigned int Pos;
  616. if (Find(Tag, Pos) == false)
  617. return "";
  618. char const *Start = (char const *) memchr(Section + d->Tags[Pos].EndTag, ':', d->Tags[Pos].StartValue - d->Tags[Pos].EndTag);
  619. ++Start;
  620. char const *End = Section + d->Tags[Pos + 1].StartTag;
  621. if (unlikely(Start > End))
  622. return "";
  623. for (; isspace_ascii(End[-1]) != 0 && End > Start; --End);
  624. return std::string(Start, End - Start);
  625. }
  626. /*}}}*/
  627. // TagSection::FindI - Find an integer /*{{{*/
  628. // ---------------------------------------------------------------------
  629. /* */
  630. signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
  631. {
  632. const char *Start;
  633. const char *Stop;
  634. if (Find(Tag,Start,Stop) == false)
  635. return Default;
  636. // Copy it into a temp buffer so we can use strtol
  637. char S[300];
  638. if ((unsigned)(Stop - Start) >= sizeof(S))
  639. return Default;
  640. strncpy(S,Start,Stop-Start);
  641. S[Stop - Start] = 0;
  642. errno = 0;
  643. char *End;
  644. signed long Result = strtol(S,&End,10);
  645. if (errno == ERANGE ||
  646. Result < std::numeric_limits<int>::min() || Result > std::numeric_limits<int>::max()) {
  647. errno = ERANGE;
  648. _error->Error(_("Cannot convert %s to integer: out of range"), S);
  649. }
  650. if (S == End)
  651. return Default;
  652. return Result;
  653. }
  654. /*}}}*/
  655. // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
  656. // ---------------------------------------------------------------------
  657. /* */
  658. unsigned long long pkgTagSection::FindULL(const char *Tag, unsigned long long const &Default) const
  659. {
  660. const char *Start;
  661. const char *Stop;
  662. if (Find(Tag,Start,Stop) == false)
  663. return Default;
  664. // Copy it into a temp buffer so we can use strtoull
  665. char S[100];
  666. if ((unsigned)(Stop - Start) >= sizeof(S))
  667. return Default;
  668. strncpy(S,Start,Stop-Start);
  669. S[Stop - Start] = 0;
  670. char *End;
  671. unsigned long long Result = strtoull(S,&End,10);
  672. if (S == End)
  673. return Default;
  674. return Result;
  675. }
  676. /*}}}*/
  677. // TagSection::FindB - Find boolean value /*{{{*/
  678. // ---------------------------------------------------------------------
  679. /* */
  680. bool pkgTagSection::FindB(const char *Tag, bool const &Default) const
  681. {
  682. const char *Start, *Stop;
  683. if (Find(Tag, Start, Stop) == false)
  684. return Default;
  685. return StringToBool(string(Start, Stop));
  686. }
  687. /*}}}*/
  688. // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
  689. // ---------------------------------------------------------------------
  690. /* The bits marked in Flag are masked on/off in Flags */
  691. bool pkgTagSection::FindFlag(const char * const Tag, uint8_t &Flags,
  692. uint8_t const Flag) const
  693. {
  694. const char *Start;
  695. const char *Stop;
  696. if (Find(Tag,Start,Stop) == false)
  697. return true;
  698. return FindFlag(Flags, Flag, Start, Stop);
  699. }
  700. bool pkgTagSection::FindFlag(uint8_t &Flags, uint8_t const Flag,
  701. char const* const Start, char const* const Stop)
  702. {
  703. switch (StringToBool(string(Start, Stop)))
  704. {
  705. case 0:
  706. Flags &= ~Flag;
  707. return true;
  708. case 1:
  709. Flags |= Flag;
  710. return true;
  711. default:
  712. _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
  713. return true;
  714. }
  715. return true;
  716. }
  717. bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
  718. unsigned long Flag) const
  719. {
  720. const char *Start;
  721. const char *Stop;
  722. if (Find(Tag,Start,Stop) == false)
  723. return true;
  724. return FindFlag(Flags, Flag, Start, Stop);
  725. }
  726. bool pkgTagSection::FindFlag(unsigned long &Flags, unsigned long Flag,
  727. char const* Start, char const* Stop)
  728. {
  729. switch (StringToBool(string(Start, Stop)))
  730. {
  731. case 0:
  732. Flags &= ~Flag;
  733. return true;
  734. case 1:
  735. Flags |= Flag;
  736. return true;
  737. default:
  738. _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
  739. return true;
  740. }
  741. return true;
  742. }
  743. /*}}}*/
  744. void pkgTagSection::Get(const char *&Start,const char *&Stop,unsigned int I) const/*{{{*/
  745. {
  746. Start = Section + d->Tags[I].StartTag;
  747. Stop = Section + d->Tags[I+1].StartTag;
  748. }
  749. /*}}}*/
  750. APT_PURE unsigned int pkgTagSection::Count() const { /*{{{*/
  751. if (d->Tags.empty() == true)
  752. return 0;
  753. // the last element is just marking the end and isn't a real one
  754. return d->Tags.size() - 1;
  755. }
  756. /*}}}*/
  757. // TagSection::Write - Ordered (re)writing of fields /*{{{*/
  758. pkgTagSection::Tag pkgTagSection::Tag::Remove(std::string const &Name)
  759. {
  760. return Tag(REMOVE, Name, "");
  761. }
  762. pkgTagSection::Tag pkgTagSection::Tag::Rename(std::string const &OldName, std::string const &NewName)
  763. {
  764. return Tag(RENAME, OldName, NewName);
  765. }
  766. pkgTagSection::Tag pkgTagSection::Tag::Rewrite(std::string const &Name, std::string const &Data)
  767. {
  768. if (Data.empty() == true)
  769. return Tag(REMOVE, Name, "");
  770. else
  771. return Tag(REWRITE, Name, Data);
  772. }
  773. static bool WriteTag(FileFd &File, std::string Tag, std::string const &Value)
  774. {
  775. if (Value.empty() || isspace_ascii(Value[0]) != 0)
  776. Tag.append(":");
  777. else
  778. Tag.append(": ");
  779. Tag.append(Value);
  780. Tag.append("\n");
  781. return File.Write(Tag.c_str(), Tag.length());
  782. }
  783. static bool RewriteTags(FileFd &File, pkgTagSection const * const This, char const * const Tag,
  784. std::vector<pkgTagSection::Tag>::const_iterator &R,
  785. std::vector<pkgTagSection::Tag>::const_iterator const &REnd)
  786. {
  787. size_t const TagLen = strlen(Tag);
  788. for (; R != REnd; ++R)
  789. {
  790. std::string data;
  791. if (R->Name.length() == TagLen && strncasecmp(R->Name.c_str(), Tag, R->Name.length()) == 0)
  792. {
  793. if (R->Action != pkgTagSection::Tag::REWRITE)
  794. break;
  795. data = R->Data;
  796. }
  797. else if(R->Action == pkgTagSection::Tag::RENAME && R->Data.length() == TagLen &&
  798. strncasecmp(R->Data.c_str(), Tag, R->Data.length()) == 0)
  799. data = This->FindRawS(R->Name.c_str());
  800. else
  801. continue;
  802. return WriteTag(File, Tag, data);
  803. }
  804. return true;
  805. }
  806. bool pkgTagSection::Write(FileFd &File, char const * const * const Order, std::vector<Tag> const &Rewrite) const
  807. {
  808. // first pass: Write everything we have an order for
  809. if (Order != NULL)
  810. {
  811. for (unsigned int I = 0; Order[I] != 0; ++I)
  812. {
  813. std::vector<Tag>::const_iterator R = Rewrite.begin();
  814. if (RewriteTags(File, this, Order[I], R, Rewrite.end()) == false)
  815. return false;
  816. if (R != Rewrite.end())
  817. continue;
  818. if (Exists(Order[I]) == false)
  819. continue;
  820. if (WriteTag(File, Order[I], FindRawS(Order[I])) == false)
  821. return false;
  822. }
  823. }
  824. // second pass: See if we have tags which aren't ordered
  825. if (d->Tags.empty() == false)
  826. {
  827. for (std::vector<pkgTagSectionPrivate::TagData>::const_iterator T = d->Tags.begin(); T != d->Tags.end() - 1; ++T)
  828. {
  829. char const * const fieldname = Section + T->StartTag;
  830. size_t fieldnamelen = T->EndTag - T->StartTag;
  831. if (Order != NULL)
  832. {
  833. unsigned int I = 0;
  834. for (; Order[I] != 0; ++I)
  835. {
  836. if (fieldnamelen == strlen(Order[I]) && strncasecmp(fieldname, Order[I], fieldnamelen) == 0)
  837. break;
  838. }
  839. if (Order[I] != 0)
  840. continue;
  841. }
  842. std::string const name(fieldname, fieldnamelen);
  843. std::vector<Tag>::const_iterator R = Rewrite.begin();
  844. if (RewriteTags(File, this, name.c_str(), R, Rewrite.end()) == false)
  845. return false;
  846. if (R != Rewrite.end())
  847. continue;
  848. if (WriteTag(File, name, FindRawS(name.c_str())) == false)
  849. return false;
  850. }
  851. }
  852. // last pass: see if there are any rewrites remaining we haven't done yet
  853. for (std::vector<Tag>::const_iterator R = Rewrite.begin(); R != Rewrite.end(); ++R)
  854. {
  855. if (R->Action == Tag::REMOVE)
  856. continue;
  857. std::string const name = ((R->Action == Tag::RENAME) ? R->Data : R->Name);
  858. if (Exists(name.c_str()))
  859. continue;
  860. if (Order != NULL)
  861. {
  862. unsigned int I = 0;
  863. for (; Order[I] != 0; ++I)
  864. {
  865. if (strncasecmp(name.c_str(), Order[I], name.length()) == 0 && name.length() == strlen(Order[I]))
  866. break;
  867. }
  868. if (Order[I] != 0)
  869. continue;
  870. }
  871. if (WriteTag(File, name, ((R->Action == Tag::RENAME) ? FindRawS(R->Name.c_str()) : R->Data)) == false)
  872. return false;
  873. }
  874. return true;
  875. }
  876. /*}}}*/
  877. void pkgUserTagSection::TrimRecord(bool /*BeforeRecord*/, const char* &End)/*{{{*/
  878. {
  879. for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++)
  880. if (Stop[0] == '#')
  881. Stop = (const char*) memchr(Stop,'\n',End-Stop);
  882. }
  883. /*}}}*/
  884. #include "tagfile-order.c"
  885. // TFRewrite - Rewrite a control record /*{{{*/
  886. // ---------------------------------------------------------------------
  887. /* This writes the control record to stdout rewriting it as necessary. The
  888. override map item specificies the rewriting rules to follow. This also
  889. takes the time to sort the feild list. */
  890. APT_IGNORE_DEPRECATED_PUSH
  891. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  892. TFRewriteData *Rewrite)
  893. {
  894. unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
  895. for (unsigned I = 0; I != 256; I++)
  896. Visited[I] = 0;
  897. // Set new tag up as necessary.
  898. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  899. {
  900. if (Rewrite[J].NewTag == 0)
  901. Rewrite[J].NewTag = Rewrite[J].Tag;
  902. }
  903. // Write all all of the tags, in order.
  904. if (Order != NULL)
  905. {
  906. for (unsigned int I = 0; Order[I] != 0; I++)
  907. {
  908. bool Rewritten = false;
  909. // See if this is a field that needs to be rewritten
  910. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  911. {
  912. if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
  913. {
  914. Visited[J] |= 2;
  915. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  916. {
  917. if (isspace_ascii(Rewrite[J].Rewrite[0]))
  918. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  919. else
  920. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  921. }
  922. Rewritten = true;
  923. break;
  924. }
  925. }
  926. // See if it is in the fragment
  927. unsigned Pos;
  928. if (Tags.Find(Order[I],Pos) == false)
  929. continue;
  930. Visited[Pos] |= 1;
  931. if (Rewritten == true)
  932. continue;
  933. /* Write out this element, taking a moment to rewrite the tag
  934. in case of changes of case. */
  935. const char *Start;
  936. const char *Stop;
  937. Tags.Get(Start,Stop,Pos);
  938. if (fputs(Order[I],Output) < 0)
  939. return _error->Errno("fputs","IO Error to output");
  940. Start += strlen(Order[I]);
  941. if (fwrite(Start,Stop - Start,1,Output) != 1)
  942. return _error->Errno("fwrite","IO Error to output");
  943. if (Stop[-1] != '\n')
  944. fprintf(Output,"\n");
  945. }
  946. }
  947. // Now write all the old tags that were missed.
  948. for (unsigned int I = 0; I != Tags.Count(); I++)
  949. {
  950. if ((Visited[I] & 1) == 1)
  951. continue;
  952. const char *Start;
  953. const char *Stop;
  954. Tags.Get(Start,Stop,I);
  955. const char *End = Start;
  956. for (; End < Stop && *End != ':'; End++);
  957. // See if this is a field that needs to be rewritten
  958. bool Rewritten = false;
  959. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  960. {
  961. if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
  962. {
  963. Visited[J] |= 2;
  964. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  965. {
  966. if (isspace_ascii(Rewrite[J].Rewrite[0]))
  967. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  968. else
  969. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  970. }
  971. Rewritten = true;
  972. break;
  973. }
  974. }
  975. if (Rewritten == true)
  976. continue;
  977. // Write out this element
  978. if (fwrite(Start,Stop - Start,1,Output) != 1)
  979. return _error->Errno("fwrite","IO Error to output");
  980. if (Stop[-1] != '\n')
  981. fprintf(Output,"\n");
  982. }
  983. // Now write all the rewrites that were missed
  984. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  985. {
  986. if ((Visited[J] & 2) == 2)
  987. continue;
  988. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  989. {
  990. if (isspace_ascii(Rewrite[J].Rewrite[0]))
  991. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  992. else
  993. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  994. }
  995. }
  996. return true;
  997. }
  998. APT_IGNORE_DEPRECATED_POP
  999. /*}}}*/
  1000. pkgTagSection::~pkgTagSection() { delete d; }