tagfile.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.17 1998/12/07 07:26:22 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. This very simple hash function for the
  104. first 3 letters gives very good performance on the debian package files */
  105. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  106. {
  107. const char *End = Start + MaxLength;
  108. Stop = Section = Start;
  109. memset(AlphaIndexes,0,sizeof(AlphaIndexes));
  110. TagCount = 0;
  111. while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]))
  112. {
  113. if (isspace(Stop[0]) == 0)
  114. {
  115. Indexes[TagCount++] = Stop - Section;
  116. unsigned char A = tolower(Stop[0]) - 'a';
  117. unsigned char B = tolower(Stop[1]) - 'a';
  118. unsigned char C = tolower(Stop[3]) - 'a';
  119. AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
  120. }
  121. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  122. if (Stop == 0)
  123. return false;
  124. for (; Stop[1] == '\r' && Stop < End; Stop++);
  125. if (Stop[1] == '\n')
  126. {
  127. Indexes[TagCount] = Stop - Section;
  128. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  129. return true;
  130. }
  131. Stop++;
  132. }
  133. return false;
  134. }
  135. /*}}}*/
  136. // TagSection::Find - Locate a tag /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* This searches the section for a tag that matches the given string. */
  139. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  140. const char *&End)
  141. {
  142. unsigned int Length = strlen(Tag);
  143. unsigned char A = tolower(Tag[0]) - 'a';
  144. unsigned char B = tolower(Tag[1]) - 'a';
  145. unsigned char C = tolower(Tag[3]) - 'a';
  146. unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
  147. if (I == 0)
  148. return false;
  149. I--;
  150. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  151. I = (I+1)%TagCount)
  152. {
  153. const char *St;
  154. St = Section + Indexes[I];
  155. if (strncasecmp(Tag,St,Length) != 0)
  156. continue;
  157. // Make sure the colon is in the right place
  158. const char *C = St + Length;
  159. for (; isspace(*C) != 0; C++);
  160. if (*C != ':')
  161. continue;
  162. // Strip off the gunk from the start end
  163. Start = C;
  164. End = Section + Indexes[I+1];
  165. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  166. for (; isspace(End[-1]) != 0 && End > Start; End--);
  167. return true;
  168. }
  169. Start = End = 0;
  170. return false;
  171. }
  172. /*}}}*/
  173. // TagSection::FindS - Find a string /*{{{*/
  174. // ---------------------------------------------------------------------
  175. /* */
  176. string pkgTagSection::FindS(const char *Tag)
  177. {
  178. const char *Start;
  179. const char *End;
  180. if (Find(Tag,Start,End) == false)
  181. return string();
  182. return string(Start,End);
  183. }
  184. /*}}}*/
  185. // TagSection::FindI - Find an integer /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* */
  188. unsigned int pkgTagSection::FindI(const char *Tag)
  189. {
  190. const char *Start;
  191. const char *End;
  192. if (Find(Tag,Start,End) == false)
  193. return 0;
  194. return atoi(string(Start,End).c_str());
  195. }
  196. /*}}}*/