tagfile.cc 22 KB

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