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