tagfile.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.23 1999/02/21 08:38:53 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. return _error->Error("Unable to parse package file %s (2)",Fd.Name().c_str());
  112. return true;
  113. }
  114. /*}}}*/
  115. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* This looks for the first double new line in the data stream. It also
  118. indexes the tags in the section. This very simple hash function for the
  119. first 3 letters gives very good performance on the debian package files */
  120. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  121. {
  122. const char *End = Start + MaxLength;
  123. Stop = Section = Start;
  124. memset(AlphaIndexes,0,sizeof(AlphaIndexes));
  125. if (Stop == 0)
  126. return false;
  127. TagCount = 0;
  128. while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End)
  129. {
  130. // Start a new index and add it to the hash
  131. if (isspace(Stop[0]) == 0)
  132. {
  133. Indexes[TagCount++] = Stop - Section;
  134. unsigned char A = tolower(Stop[0]) - 'a';
  135. unsigned char B = tolower(Stop[1]) - 'a';
  136. unsigned char C = tolower(Stop[3]) - 'a';
  137. AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
  138. }
  139. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  140. if (Stop == 0)
  141. return false;
  142. for (; Stop[1] == '\r' && Stop+1 < End; Stop++);
  143. // Double newline marks the end of the record
  144. if (Stop+1 < End && Stop[1] == '\n')
  145. {
  146. Indexes[TagCount] = Stop - Section;
  147. for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++);
  148. return true;
  149. }
  150. Stop++;
  151. }
  152. return false;
  153. }
  154. /*}}}*/
  155. // TagSection::Find - Locate a tag /*{{{*/
  156. // ---------------------------------------------------------------------
  157. /* This searches the section for a tag that matches the given string. */
  158. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  159. const char *&End)
  160. {
  161. unsigned int Length = strlen(Tag);
  162. unsigned char A = tolower(Tag[0]) - 'a';
  163. unsigned char B = tolower(Tag[1]) - 'a';
  164. unsigned char C = tolower(Tag[3]) - 'a';
  165. unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
  166. if (I == 0)
  167. return false;
  168. I--;
  169. for (unsigned int Counter = 0; Counter != TagCount; Counter++,
  170. I = (I+1)%TagCount)
  171. {
  172. const char *St;
  173. St = Section + Indexes[I];
  174. if (strncasecmp(Tag,St,Length) != 0)
  175. continue;
  176. // Make sure the colon is in the right place
  177. const char *C = St + Length;
  178. for (; isspace(*C) != 0; C++);
  179. if (*C != ':')
  180. continue;
  181. // Strip off the gunk from the start end
  182. Start = C;
  183. End = Section + Indexes[I+1];
  184. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  185. for (; isspace(End[-1]) != 0 && End > Start; End--);
  186. return true;
  187. }
  188. Start = End = 0;
  189. return false;
  190. }
  191. /*}}}*/
  192. // TagSection::FindS - Find a string /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* */
  195. string pkgTagSection::FindS(const char *Tag)
  196. {
  197. const char *Start;
  198. const char *End;
  199. if (Find(Tag,Start,End) == false)
  200. return string();
  201. return string(Start,End);
  202. }
  203. /*}}}*/
  204. // TagSection::FindI - Find an integer /*{{{*/
  205. // ---------------------------------------------------------------------
  206. /* */
  207. signed int pkgTagSection::FindI(const char *Tag,signed long Default)
  208. {
  209. const char *Start;
  210. const char *Stop;
  211. if (Find(Tag,Start,Stop) == false)
  212. return Default;
  213. // Copy it into a temp buffer so we can use strtol
  214. char S[300];
  215. if ((unsigned)(Stop - Start) >= sizeof(S))
  216. return Default;
  217. strncpy(S,Start,Stop-Start);
  218. S[Stop - Start] = 0;
  219. char *End;
  220. signed long Result = strtol(S,&End,10);
  221. if (S == End)
  222. return Default;
  223. return Result;
  224. }
  225. /*}}}*/
  226. // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
  227. // ---------------------------------------------------------------------
  228. /* The bits marked in Flag are masked on/off in Flags */
  229. bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
  230. unsigned long Flag)
  231. {
  232. const char *Start;
  233. const char *Stop;
  234. if (Find(Tag,Start,Stop) == false)
  235. return true;
  236. switch (StringToBool(string(Start,Stop)))
  237. {
  238. case 0:
  239. Flags &= ~Flag;
  240. return true;
  241. case 1:
  242. Flags |= Flag;
  243. return true;
  244. default:
  245. _error->Warning("Unknown flag value");
  246. return true;
  247. }
  248. return true;
  249. }
  250. /*}}}*/