tagfile.cc 5.0 KB

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