]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debversion.cc
* apt-pkg/deb/debversion.cc:
[apt.git] / apt-pkg / deb / debversion.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debversion.cc,v 1.8 2003/09/10 23:39:49 mdz Exp $
4 /* ######################################################################
5
6 Debian Version - Versioning system for Debian
7
8 This implements the standard Debian versioning system.
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #include <config.h>
14
15 #include <apt-pkg/debversion.h>
16 #include <apt-pkg/pkgcache.h>
17
18 #include <stdlib.h>
19 #include <ctype.h>
20 /*}}}*/
21
22 debVersioningSystem debVS;
23
24 // debVS::debVersioningSystem - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
26 /* */
27 debVersioningSystem::debVersioningSystem()
28 {
29 Label = "Standard .deb";
30 }
31 /*}}}*/
32
33 // debVS::CmpFragment - Compare versions /*{{{*/
34 // ---------------------------------------------------------------------
35 /* This compares a fragment of the version. This is a slightly adapted
36 version of what dpkg uses. */
37 #define order(x) ((x) == '~' ? -1 \
38 : isdigit((x)) ? 0 \
39 : !(x) ? 0 \
40 : isalpha((x)) ? (x) \
41 : (x) + 256)
42 int debVersioningSystem::CmpFragment(const char *A,const char *AEnd,
43 const char *B,const char *BEnd)
44 {
45 if (A >= AEnd && B >= BEnd)
46 return 0;
47 if (A >= AEnd)
48 {
49 if (*B == '~') return 1;
50 return -1;
51 }
52 if (B >= BEnd)
53 {
54 if (*A == '~') return -1;
55 return 1;
56 }
57
58 /* Iterate over the whole string
59 What this does is to split the whole string into groups of
60 numeric and non numeric portions. For instance:
61 a67bhgs89
62 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
63 2.7.2-linux-1
64 Has '2', '.', '7', '.' ,'-linux-','1' */
65 const char *lhs = A;
66 const char *rhs = B;
67 while (lhs != AEnd && rhs != BEnd)
68 {
69 int first_diff = 0;
70
71 while (lhs != AEnd && rhs != BEnd &&
72 (!isdigit(*lhs) || !isdigit(*rhs)))
73 {
74 int vc = order(*lhs);
75 int rc = order(*rhs);
76 if (vc != rc)
77 return vc - rc;
78 lhs++; rhs++;
79 }
80
81 while (*lhs == '0')
82 lhs++;
83 while (*rhs == '0')
84 rhs++;
85 while (isdigit(*lhs) && isdigit(*rhs))
86 {
87 if (!first_diff)
88 first_diff = *lhs - *rhs;
89 lhs++;
90 rhs++;
91 }
92
93 if (isdigit(*lhs))
94 return 1;
95 if (isdigit(*rhs))
96 return -1;
97 if (first_diff)
98 return first_diff;
99 }
100
101 // The strings must be equal
102 if (lhs == AEnd && rhs == BEnd)
103 return 0;
104
105 // lhs is shorter
106 if (lhs == AEnd)
107 {
108 if (*rhs == '~') return 1;
109 return -1;
110 }
111
112 // rhs is shorter
113 if (rhs == BEnd)
114 {
115 if (*lhs == '~') return -1;
116 return 1;
117 }
118
119 // Shouldnt happen
120 return 1;
121 }
122 /*}}}*/
123 // debVS::CmpVersion - Comparison for versions /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This fragments the version into E:V-R triples and compares each
126 portion separately. */
127 int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
128 const char *B,const char *BEnd)
129 {
130 // Strip off the epoch and compare it
131 const char *lhs = (const char*) memchr(A, ':', AEnd - A);
132 const char *rhs = (const char*) memchr(B, ':', BEnd - B);
133 if (lhs == NULL)
134 lhs = A;
135 if (rhs == NULL)
136 rhs = B;
137
138 // Special case: a zero epoch is the same as no epoch,
139 // so remove it.
140 if (lhs != A)
141 {
142 for (; *A == '0'; ++A);
143 if (A == lhs)
144 {
145 ++A;
146 ++lhs;
147 }
148 }
149 if (rhs != B)
150 {
151 for (; *B == '0'; ++B);
152 if (B == rhs)
153 {
154 ++B;
155 ++rhs;
156 }
157 }
158
159 // Compare the epoch
160 int Res = CmpFragment(A,lhs,B,rhs);
161 if (Res != 0)
162 return Res;
163
164 // Skip the :
165 if (lhs != A)
166 lhs++;
167 if (rhs != B)
168 rhs++;
169
170 // Find the last -
171 const char *dlhs = (const char*) memrchr(lhs, '-', AEnd - lhs);
172 const char *drhs = (const char*) memrchr(rhs, '-', BEnd - rhs);
173 if (dlhs == NULL)
174 dlhs = AEnd;
175 if (drhs == NULL)
176 drhs = BEnd;
177
178 // Compare the main version
179 Res = CmpFragment(lhs,dlhs,rhs,drhs);
180 if (Res != 0)
181 return Res;
182
183 // Skip the -
184 if (dlhs != lhs)
185 dlhs++;
186 if (drhs != rhs)
187 drhs++;
188
189 // no debian revision need to be treated like -0
190 if (*(dlhs-1) == '-' && *(drhs-1) == '-')
191 return CmpFragment(dlhs,AEnd,drhs,BEnd);
192 else if (*(dlhs-1) == '-')
193 {
194 const char* null = "0";
195 return CmpFragment(dlhs,AEnd,null, null+1);
196 }
197 else if (*(drhs-1) == '-')
198 {
199 const char* null = "0";
200 return CmpFragment(null, null+1, drhs, BEnd);
201 }
202 else
203 return 0;
204 }
205 /*}}}*/
206 // debVS::CheckDep - Check a single dependency /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This simply preforms the version comparison and switch based on
209 operator. If DepVer is 0 then we are comparing against a provides
210 with no version. */
211 bool debVersioningSystem::CheckDep(const char *PkgVer,
212 int Op,const char *DepVer)
213 {
214 if (DepVer == 0 || DepVer[0] == 0)
215 return true;
216 if (PkgVer == 0 || PkgVer[0] == 0)
217 return false;
218 Op &= 0x0F;
219
220 size_t const lenPkgVer = strlen(PkgVer);
221 size_t const lenDepVer = strlen(DepVer);
222
223 // take a shortcut for equals which are string-equal as well
224 if (Op == pkgCache::Dep::Equals && lenPkgVer == lenDepVer &&
225 memcmp(PkgVer, DepVer, lenPkgVer) == 0)
226 return true;
227
228 // Perform the actual comparision.
229 int const Res = DoCmpVersion(PkgVer, PkgVer + lenPkgVer, DepVer, DepVer + lenDepVer);
230 switch (Op)
231 {
232 case pkgCache::Dep::LessEq:
233 if (Res <= 0)
234 return true;
235 break;
236
237 case pkgCache::Dep::GreaterEq:
238 if (Res >= 0)
239 return true;
240 break;
241
242 case pkgCache::Dep::Less:
243 if (Res < 0)
244 return true;
245 break;
246
247 case pkgCache::Dep::Greater:
248 if (Res > 0)
249 return true;
250 break;
251
252 case pkgCache::Dep::Equals:
253 if (Res == 0)
254 return true;
255 break;
256
257 case pkgCache::Dep::NotEquals:
258 if (Res != 0)
259 return true;
260 break;
261 }
262
263 return false;
264 }
265 /*}}}*/
266 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This strips all the debian specific information from the version number */
269 std::string debVersioningSystem::UpstreamVersion(const char *Ver)
270 {
271 // Strip off the bit before the first colon
272 const char *I = Ver;
273 for (; *I != 0 && *I != ':'; I++);
274 if (*I == ':')
275 Ver = I + 1;
276
277 // Chop off the trailing -
278 I = Ver;
279 unsigned Last = strlen(Ver);
280 for (; *I != 0; I++)
281 if (*I == '-')
282 Last = I - Ver;
283
284 return std::string(Ver,Last);
285 }
286 /*}}}*/