tagfile.cc 8.0 KB

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