tagfile.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.25 1999/07/03 06:45:40 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 <apt-pkg/strutl.h>
  17. #include <string>
  18. #include <stdio.h>
  19. /*}}}*/
  20. // TagFile::pkgTagFile - Constructor /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* */
  23. pkgTagFile::pkgTagFile(FileFd &Fd,unsigned long Size) : Fd(Fd), Size(Size)
  24. {
  25. Buffer = new char[Size];
  26. Start = End = Buffer;
  27. Left = Fd.Size();
  28. iOffset = 0;
  29. Fill();
  30. }
  31. /*}}}*/
  32. // pkgTagFile::~pkgTagFile - Destructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgTagFile::~pkgTagFile()
  36. {
  37. delete [] Buffer;
  38. }
  39. /*}}}*/
  40. // TagFile::Step - Advance to the next section /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* If the Section Scanner fails we refill the buffer and try again. */
  43. bool pkgTagFile::Step(pkgTagSection &Tag)
  44. {
  45. if (Tag.Scan(Start,End - Start) == false)
  46. {
  47. if (Fill() == false)
  48. return false;
  49. if (Tag.Scan(Start,End - Start) == false)
  50. return _error->Error("Unable to parse package file %s (1)",Fd.Name().c_str());
  51. }
  52. Start += Tag.size();
  53. iOffset += Tag.size();
  54. return true;
  55. }
  56. /*}}}*/
  57. // TagFile::Fill - Top up the buffer /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* This takes the bit at the end of the buffer and puts it at the start
  60. then fills the rest from the file */
  61. bool pkgTagFile::Fill()
  62. {
  63. unsigned long EndSize = End - Start;
  64. memmove(Buffer,Start,EndSize);
  65. Start = Buffer;
  66. End = Buffer + EndSize;
  67. if (Left == 0)
  68. {
  69. if (EndSize <= 3)
  70. return false;
  71. if (Size - (End - Buffer) < 4)
  72. return true;
  73. // Append a double new line if one does not exist
  74. unsigned int LineCount = 0;
  75. for (const char *E = End - 1; E - End < 6 && (*E == '\n' || *E == '\r'); E--)
  76. if (*E == '\n')
  77. LineCount++;
  78. for (; LineCount < 2; LineCount++)
  79. *End++ = '\n';
  80. return true;
  81. }
  82. // See if only a bit of the file is left
  83. if (Left < Size - (End - Buffer))
  84. {
  85. if (Fd.Read(End,Left) == false)
  86. return false;
  87. End += Left;
  88. Left = 0;
  89. }
  90. else
  91. {
  92. if (Fd.Read(End,Size - (End - Buffer)) == false)
  93. return false;
  94. Left -= Size - (End - Buffer);
  95. End = Buffer + Size;
  96. }
  97. return true;
  98. }
  99. /*}}}*/
  100. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* This jumps to a pre-recorded file location and reads the record
  103. that is there */
  104. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
  105. {
  106. iOffset = Offset;
  107. Left = Fd.Size() - Offset;
  108. if (Fd.Seek(Offset) == false)
  109. return false;
  110. End = Start = Buffer;
  111. if (Fill() == false)
  112. return false;
  113. if (Tag.Scan(Start,End - Start) == true)
  114. return true;
  115. // This appends a double new line (for the real eof handling)
  116. if (Fill() == false)
  117. return false;
  118. if (Tag.Scan(Start,End - Start) == false)
  119. {
  120. cout << string(Start,End) << endl;
  121. return _error->Error("Unable to parse package file %s (2)",Fd.Name().c_str());
  122. }
  123. return true;
  124. }
  125. /*}}}*/
  126. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* This looks for the first double new line in the data stream. It also
  129. indexes the tags in the section. This very simple hash function for the
  130. first 3 letters gives very good performance on the debian package files */
  131. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  132. {
  133. const char *End = Start + MaxLength;
  134. Stop = Section = Start;
  135. memset(AlphaIndexes,0,sizeof(AlphaIndexes));
  136. if (Stop == 0)
  137. return false;
  138. TagCount = 0;
  139. while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
  140. {
  141. // Start a new index and add it to the hash
  142. if (isspace(Stop[0]) == 0)
  143. {
  144. Indexes[TagCount++] = Stop - Section;
  145. unsigned char A = tolower(Stop[0]) - 'a';
  146. unsigned char B = tolower(Stop[1]) - 'a';
  147. unsigned char C = tolower(Stop[3]) - 'a';
  148. AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
  149. }
  150. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  151. if (Stop == 0)
  152. return false;
  153. for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
  154. // Double newline marks the end of the record
  155. if (Stop+1 < End && Stop[1] == '\n')
  156. {
  157. Indexes[TagCount] = Stop - Section;
  158. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  159. return true;
  160. }
  161. Stop++;
  162. }
  163. return false;
  164. }
  165. /*}}}*/
  166. // TagSection::Find - Locate a tag /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* This searches the section for a tag that matches the given string. */
  169. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  170. const char *&End)
  171. {
  172. unsigned int Length = strlen(Tag);
  173. unsigned char A = tolower(Tag[0]) - 'a';
  174. unsigned char B = tolower(Tag[1]) - 'a';
  175. unsigned char C = tolower(Tag[3]) - 'a';
  176. unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
  177. if (I == 0)
  178. return false;
  179. I--;
  180. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  181. I = (I+1)%TagCount)
  182. {
  183. const char *St;
  184. St = Section + Indexes[I];
  185. if (strncasecmp(Tag,St,Length) != 0)
  186. continue;
  187. // Make sure the colon is in the right place
  188. const char *C = St + Length;
  189. for (; isspace(*C) != 0; C++);
  190. if (*C != ':')
  191. continue;
  192. // Strip off the gunk from the start end
  193. Start = C;
  194. End = Section + Indexes[I+1];
  195. if (Start >= End)
  196. return _error->Error("Internal parsing error");
  197. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  198. for (; isspace(End[-1]) != 0 && End > Start; End--);
  199. return true;
  200. }
  201. Start = End = 0;
  202. return false;
  203. }
  204. /*}}}*/
  205. // TagSection::FindS - Find a string /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* */
  208. string pkgTagSection::FindS(const char *Tag)
  209. {
  210. const char *Start;
  211. const char *End;
  212. if (Find(Tag,Start,End) == false)
  213. return string();
  214. return string(Start,End);
  215. }
  216. /*}}}*/
  217. // TagSection::FindI - Find an integer /*{{{*/
  218. // ---------------------------------------------------------------------
  219. /* */
  220. signed int pkgTagSection::FindI(const char *Tag,signed long Default)
  221. {
  222. const char *Start;
  223. const char *Stop;
  224. if (Find(Tag,Start,Stop) == false)
  225. return Default;
  226. // Copy it into a temp buffer so we can use strtol
  227. char S[300];
  228. if ((unsigned)(Stop - Start) >= sizeof(S))
  229. return Default;
  230. strncpy(S,Start,Stop-Start);
  231. S[Stop - Start] = 0;
  232. char *End;
  233. signed long Result = strtol(S,&End,10);
  234. if (S == End)
  235. return Default;
  236. return Result;
  237. }
  238. /*}}}*/
  239. // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
  240. // ---------------------------------------------------------------------
  241. /* The bits marked in Flag are masked on/off in Flags */
  242. bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
  243. unsigned long Flag)
  244. {
  245. const char *Start;
  246. const char *Stop;
  247. if (Find(Tag,Start,Stop) == false)
  248. return true;
  249. switch (StringToBool(string(Start,Stop)))
  250. {
  251. case 0:
  252. Flags &= ~Flag;
  253. return true;
  254. case 1:
  255. Flags |= Flag;
  256. return true;
  257. default:
  258. _error->Warning("Unknown flag value");
  259. return true;
  260. }
  261. return true;
  262. }
  263. /*}}}*/