tagfile.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.13 1998/10/30 07:53:41 jgg 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. #ifdef __GNUG__
  12. #pragma implementation "apt-pkg/tagfile.h"
  13. #endif
  14. #include <apt-pkg/tagfile.h>
  15. #include <apt-pkg/error.h>
  16. #include <string>
  17. #include <stdio.h>
  18. /*}}}*/
  19. // TagFile::pkgTagFile - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* */
  22. pkgTagFile::pkgTagFile(FileFd &Fd,unsigned long Size) : Fd(Fd), Size(Size)
  23. {
  24. Buffer = new char[Size];
  25. Start = End = Buffer;
  26. Left = Fd.Size();
  27. iOffset = 0;
  28. Fill();
  29. }
  30. /*}}}*/
  31. // TagFile::Step - Advance to the next section /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* If the Section Scanner fails we refill the buffer and try again. */
  34. bool pkgTagFile::Step(pkgTagSection &Tag)
  35. {
  36. if (Tag.Scan(Start,End - Start) == false)
  37. {
  38. if (Fill() == false)
  39. return false;
  40. if (Tag.Scan(Start,End - Start) == false)
  41. return _error->Error("Unable to parse package file");
  42. }
  43. Start += Tag.size();
  44. iOffset += Tag.size();
  45. return true;
  46. }
  47. /*}}}*/
  48. // TagFile::Fill - Top up the buffer /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* This takes the bit at the end of the buffer and puts it at the start
  51. then fills the rest from the file */
  52. bool pkgTagFile::Fill()
  53. {
  54. unsigned long EndSize = End - Start;
  55. if (Left == 0)
  56. {
  57. if (EndSize <= 1)
  58. return false;
  59. return true;
  60. }
  61. memmove(Buffer,Start,EndSize);
  62. Start = Buffer;
  63. End = Buffer + EndSize;
  64. // See if only a bit of the file is left
  65. if (Left < Size - (End - Buffer))
  66. {
  67. if (Fd.Read(End,Left) == false)
  68. return false;
  69. End += Left;
  70. Left = 0;
  71. }
  72. else
  73. {
  74. if (Fd.Read(End,Size - (End - Buffer)) == false)
  75. return false;
  76. Left -= Size - (End - Buffer);
  77. End = Buffer + Size;
  78. }
  79. return true;
  80. }
  81. /*}}}*/
  82. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* This jumps to a pre-recorded file location and */
  85. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
  86. {
  87. iOffset = Offset;
  88. Left = Fd.Size() - Offset;
  89. if (Fd.Seek(Offset) == false)
  90. return false;
  91. End = Start = Buffer;
  92. if (Fill() == false)
  93. return false;
  94. if (Tag.Scan(Start,End - Start) == false)
  95. return _error->Error("Unable to parse package file");
  96. return true;
  97. }
  98. /*}}}*/
  99. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* This looks for the first double new line in the data stream. It also
  102. indexes the tags in the section. */
  103. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  104. {
  105. const char *End = Start + MaxLength;
  106. Stop = Section = Start;
  107. TagCount = 0;
  108. Indexes[TagCount++] = Stop - Section;
  109. Stop++;
  110. for (; Stop < End; Stop++)
  111. {
  112. if (Stop[-1] != '\n')
  113. continue;
  114. // Skip line feeds
  115. for (; Stop[0] == '\r' && Stop < End; Stop++);
  116. if (Stop[0] == '\n')
  117. {
  118. // Extra one at the end to simplify find
  119. Indexes[TagCount] = Stop - Section;
  120. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  121. return true;
  122. }
  123. if (isspace(Stop[0]) == 0)
  124. Indexes[TagCount++] = Stop - Section;
  125. // Just in case.
  126. if (TagCount > sizeof(Indexes)/sizeof(Indexes[0]))
  127. TagCount = sizeof(Indexes)/sizeof(Indexes[0]);
  128. }
  129. return false;
  130. }
  131. /*}}}*/
  132. // TagSection::Find - Locate a tag /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* This searches the section for a tag that matches the given string. */
  135. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  136. const char *&End)
  137. {
  138. unsigned int Length = strlen(Tag);
  139. for (unsigned int I = 0; I != TagCount; I++)
  140. {
  141. if (strncasecmp(Tag,Section + Indexes[I],Length) != 0)
  142. continue;
  143. // Make sure the colon is in the right place
  144. const char *C = Section + Length + Indexes[I];
  145. for (; isspace(*C) != 0; C++);
  146. if (*C != ':')
  147. continue;
  148. // Strip off the gunk from the start end
  149. Start = C;
  150. End = Section + Indexes[I+1];
  151. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  152. for (; isspace(End[-1]) != 0 && End > Start; End--);
  153. return true;
  154. }
  155. Start = End = 0;
  156. return false;
  157. }
  158. /*}}}*/