tagfile.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.12 1998/10/24 04:58:06 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 - (End - Buffer))
  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. {
  79. cout << "boink" << endl;
  80. return false;
  81. }
  82. Left -= Size - (End - Buffer);
  83. End = Buffer + Size;
  84. }
  85. return true;
  86. }
  87. /*}}}*/
  88. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* This jumps to a pre-recorded file location and */
  91. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
  92. {
  93. iOffset = Offset;
  94. Left = Fd.Size() - Offset;
  95. if (Fd.Seek(Offset) == false)
  96. return false;
  97. End = Start = Buffer;
  98. if (Fill() == false)
  99. return false;
  100. if (Tag.Scan(Start,End - Start) == false)
  101. return _error->Error("Unable to parse package file");
  102. return true;
  103. }
  104. /*}}}*/
  105. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* This looks for the first double new line in the data stream. It also
  108. indexes the tags in the section. */
  109. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  110. {
  111. const char *End = Start + MaxLength;
  112. Stop = Section = Start;
  113. TagCount = 0;
  114. Indexes[TagCount++] = Stop - Section;
  115. Stop++;
  116. for (; Stop < End; Stop++)
  117. {
  118. if (Stop[-1] != '\n')
  119. continue;
  120. // Skip line feeds
  121. for (; Stop[0] == '\r' && Stop < End; Stop++);
  122. if (Stop[0] == '\n')
  123. {
  124. // Extra one at the end to simplify find
  125. Indexes[TagCount] = Stop - Section;
  126. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  127. return true;
  128. }
  129. if (isspace(Stop[0]) == 0)
  130. Indexes[TagCount++] = Stop - Section;
  131. // Just in case.
  132. if (TagCount > sizeof(Indexes)/sizeof(Indexes[0]))
  133. TagCount = sizeof(Indexes)/sizeof(Indexes[0]);
  134. }
  135. return false;
  136. }
  137. /*}}}*/
  138. // TagSection::Find - Locate a tag /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* This searches the section for a tag that matches the given string. */
  141. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  142. const char *&End)
  143. {
  144. unsigned int Length = strlen(Tag);
  145. for (unsigned int I = 0; I != TagCount; I++)
  146. {
  147. if (strncasecmp(Tag,Section + Indexes[I],Length) != 0)
  148. continue;
  149. // Make sure the colon is in the right place
  150. const char *C = Section + Length + Indexes[I];
  151. for (; isspace(*C) != 0; C++);
  152. if (*C != ':')
  153. continue;
  154. // Strip off the gunk from the start end
  155. Start = C;
  156. End = Section + Indexes[I+1];
  157. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  158. for (; isspace(End[-1]) != 0 && End > Start; End--);
  159. return true;
  160. }
  161. Start = End = 0;
  162. return false;
  163. }
  164. /*}}}*/