tagfile.cc 5.0 KB

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