tagfile.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.18 1998/12/08 05:24: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 %s",Fd.Name().c_str());
  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. memmove(Buffer,Start,EndSize);
  56. Start = Buffer;
  57. End = Buffer + EndSize;
  58. if (Left == 0)
  59. {
  60. if (EndSize <= 3)
  61. return false;
  62. if (Size - (End - Buffer) < 4)
  63. return true;
  64. // Append a double new line if one does not exist
  65. unsigned int LineCount = 0;
  66. for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--)
  67. if (*E == '\n')
  68. LineCount++;
  69. for (; LineCount < 2; LineCount++)
  70. *End++ = '\n';
  71. return true;
  72. }
  73. // See if only a bit of the file is left
  74. if (Left < Size - (End - Buffer))
  75. {
  76. if (Fd.Read(End,Left) == false)
  77. return false;
  78. End += Left;
  79. Left = 0;
  80. }
  81. else
  82. {
  83. if (Fd.Read(End,Size - (End - Buffer)) == false)
  84. return false;
  85. Left -= Size - (End - Buffer);
  86. End = Buffer + Size;
  87. }
  88. return true;
  89. }
  90. /*}}}*/
  91. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* This jumps to a pre-recorded file location and reads the record
  94. that is there */
  95. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
  96. {
  97. iOffset = Offset;
  98. Left = Fd.Size() - Offset;
  99. if (Fd.Seek(Offset) == false)
  100. return false;
  101. End = Start = Buffer;
  102. if (Fill() == false)
  103. return false;
  104. if (Tag.Scan(Start,End - Start) == false)
  105. return _error->Error("Unable to parse package file");
  106. return true;
  107. }
  108. /*}}}*/
  109. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* This looks for the first double new line in the data stream. It also
  112. indexes the tags in the section. This very simple hash function for the
  113. first 3 letters gives very good performance on the debian package files */
  114. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  115. {
  116. const char *End = Start + MaxLength;
  117. Stop = Section = Start;
  118. memset(AlphaIndexes,0,sizeof(AlphaIndexes));
  119. if (Stop == 0)
  120. return false;
  121. TagCount = 0;
  122. while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]))
  123. {
  124. if (isspace(Stop[0]) == 0)
  125. {
  126. Indexes[TagCount++] = Stop - Section;
  127. unsigned char A = tolower(Stop[0]) - 'a';
  128. unsigned char B = tolower(Stop[1]) - 'a';
  129. unsigned char C = tolower(Stop[3]) - 'a';
  130. AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
  131. }
  132. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  133. if (Stop == 0)
  134. return false;
  135. for (; Stop[1] == '\r' && Stop < End; Stop++);
  136. if (Stop[1] == '\n')
  137. {
  138. Indexes[TagCount] = Stop - Section;
  139. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  140. return true;
  141. }
  142. Stop++;
  143. }
  144. return false;
  145. }
  146. /*}}}*/
  147. // TagSection::Find - Locate a tag /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* This searches the section for a tag that matches the given string. */
  150. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  151. const char *&End)
  152. {
  153. unsigned int Length = strlen(Tag);
  154. unsigned char A = tolower(Tag[0]) - 'a';
  155. unsigned char B = tolower(Tag[1]) - 'a';
  156. unsigned char C = tolower(Tag[3]) - 'a';
  157. unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
  158. if (I == 0)
  159. return false;
  160. I--;
  161. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  162. I = (I+1)%TagCount)
  163. {
  164. const char *St;
  165. St = Section + Indexes[I];
  166. if (strncasecmp(Tag,St,Length) != 0)
  167. continue;
  168. // Make sure the colon is in the right place
  169. const char *C = St + Length;
  170. for (; isspace(*C) != 0; C++);
  171. if (*C != ':')
  172. continue;
  173. // Strip off the gunk from the start end
  174. Start = C;
  175. End = Section + Indexes[I+1];
  176. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  177. for (; isspace(End[-1]) != 0 && End > Start; End--);
  178. return true;
  179. }
  180. Start = End = 0;
  181. return false;
  182. }
  183. /*}}}*/
  184. // TagSection::FindS - Find a string /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* */
  187. string pkgTagSection::FindS(const char *Tag)
  188. {
  189. const char *Start;
  190. const char *End;
  191. if (Find(Tag,Start,End) == false)
  192. return string();
  193. return string(Start,End);
  194. }
  195. /*}}}*/
  196. // TagSection::FindI - Find an integer /*{{{*/
  197. // ---------------------------------------------------------------------
  198. /* */
  199. unsigned int pkgTagSection::FindI(const char *Tag)
  200. {
  201. const char *Start;
  202. const char *End;
  203. if (Find(Tag,Start,End) == false)
  204. return 0;
  205. return atoi(string(Start,End).c_str());
  206. }
  207. /*}}}*/