Optimizations
[ntk/apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.17 1998/12/07 07:26:22 jgg Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
8 This uses a rotating buffer to load the package information into.
9 The scanner runs over it and isolates and indexes a single section.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/tagfile.h"
16 #endif
17
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20
21 #include <string>
22 #include <stdio.h>
23 /*}}}*/
24
25 // TagFile::pkgTagFile - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
27 /* */
28 pkgTagFile::pkgTagFile(FileFd &Fd,unsigned long Size) : Fd(Fd), Size(Size)
29 {
30 Buffer = new char[Size];
31 Start = End = Buffer;
32 Left = Fd.Size();
33 iOffset = 0;
34 Fill();
35 }
36 /*}}}*/
37 // TagFile::Step - Advance to the next section /*{{{*/
38 // ---------------------------------------------------------------------
39 /* If the Section Scanner fails we refill the buffer and try again. */
40 bool pkgTagFile::Step(pkgTagSection &Tag)
41 {
42 if (Tag.Scan(Start,End - Start) == false)
43 {
44 if (Fill() == false)
45 return false;
46
47 if (Tag.Scan(Start,End - Start) == false)
48 return _error->Error("Unable to parse package file");
49 }
50 Start += Tag.size();
51 iOffset += Tag.size();
52
53 return true;
54 }
55 /*}}}*/
56 // TagFile::Fill - Top up the buffer /*{{{*/
57 // ---------------------------------------------------------------------
58 /* This takes the bit at the end of the buffer and puts it at the start
59 then fills the rest from the file */
60 bool pkgTagFile::Fill()
61 {
62 unsigned long EndSize = End - Start;
63
64 if (Left == 0)
65 {
66 if (EndSize <= 1)
67 return false;
68 return true;
69 }
70
71 memmove(Buffer,Start,EndSize);
72 Start = Buffer;
73 End = Buffer + EndSize;
74
75 // See if only a bit of the file is left
76 if (Left < Size - (End - Buffer))
77 {
78 if (Fd.Read(End,Left) == false)
79 return false;
80
81 End += Left;
82 Left = 0;
83 }
84 else
85 {
86 if (Fd.Read(End,Size - (End - Buffer)) == false)
87 return false;
88
89 Left -= Size - (End - Buffer);
90 End = Buffer + Size;
91 }
92 return true;
93 }
94 /*}}}*/
95 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
96 // ---------------------------------------------------------------------
97 /* This jumps to a pre-recorded file location and reads the record
98 that is there */
99 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset)
100 {
101 iOffset = Offset;
102 Left = Fd.Size() - Offset;
103 if (Fd.Seek(Offset) == false)
104 return false;
105 End = Start = Buffer;
106
107 if (Fill() == false)
108 return false;
109
110 if (Tag.Scan(Start,End - Start) == false)
111 return _error->Error("Unable to parse package file");
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
126 TagCount = 0;
127 while (TagCount < sizeof(Indexes)/sizeof(Indexes[0]))
128 {
129 if (isspace(Stop[0]) == 0)
130 {
131 Indexes[TagCount++] = Stop - Section;
132 unsigned char A = tolower(Stop[0]) - 'a';
133 unsigned char B = tolower(Stop[1]) - 'a';
134 unsigned char C = tolower(Stop[3]) - 'a';
135 AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)] = TagCount;
136 }
137
138 Stop = (const char *)memchr(Stop,'\n',End - Stop);
139
140 if (Stop == 0)
141 return false;
142 for (; Stop[1] == '\r' && Stop < End; Stop++);
143
144 if (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
151 Stop++;
152 }
153
154 return false;
155 }
156 /*}}}*/
157 // TagSection::Find - Locate a tag /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This searches the section for a tag that matches the given string. */
160 bool pkgTagSection::Find(const char *Tag,const char *&Start,
161 const char *&End)
162 {
163 unsigned int Length = strlen(Tag);
164 unsigned char A = tolower(Tag[0]) - 'a';
165 unsigned char B = tolower(Tag[1]) - 'a';
166 unsigned char C = tolower(Tag[3]) - 'a';
167 unsigned int I = AlphaIndexes[((A + C/3)%26) + 26*((B + C/2)%26)];
168 if (I == 0)
169 return false;
170 I--;
171
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
180 // Make sure the colon is in the right place
181 const char *C = St + Length;
182 for (; isspace(*C) != 0; C++);
183 if (*C != ':')
184 continue;
185
186 // Strip off the gunk from the start end
187 Start = C;
188 End = Section + Indexes[I+1];
189 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
190 for (; isspace(End[-1]) != 0 && End > Start; End--);
191
192 return true;
193 }
194
195 Start = End = 0;
196 return false;
197 }
198 /*}}}*/
199 // TagSection::FindS - Find a string /*{{{*/
200 // ---------------------------------------------------------------------
201 /* */
202 string pkgTagSection::FindS(const char *Tag)
203 {
204 const char *Start;
205 const char *End;
206 if (Find(Tag,Start,End) == false)
207 return string();
208 return string(Start,End);
209 }
210 /*}}}*/
211 // TagSection::FindI - Find an integer /*{{{*/
212 // ---------------------------------------------------------------------
213 /* */
214 unsigned int pkgTagSection::FindI(const char *Tag)
215 {
216 const char *Start;
217 const char *End;
218 if (Find(Tag,Start,End) == false)
219 return 0;
220
221 return atoi(string(Start,End).c_str());
222 }
223 /*}}}*/
224