tagfile.cc 7.5 KB

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