1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/* version.c - Alpine Package Keeper (APK)
*
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
* Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation. See http://www.gnu.org/ for details.
*/
#include <stdio.h>
#include <ctype.h>
#include "apk_defines.h"
#include "apk_version.h"
/* Gentoo version: {digit}{.digit}...{letter}{_suf{#}}...{-r#} */
enum PARTS {
TOKEN_INVALID = -1,
TOKEN_DIGIT_OR_ZERO,
TOKEN_DIGIT,
TOKEN_LETTER,
TOKEN_SUFFIX,
TOKEN_SUFFIX_NO,
TOKEN_REVISION_NO,
TOKEN_END,
};
static void next_token(int *type, apk_blob_t *blob)
{
int n = TOKEN_INVALID;
if (blob->len == 0 || blob->ptr[0] == 0) {
n = TOKEN_END;
} else if ((*type == TOKEN_DIGIT || *type == TOKEN_DIGIT_OR_ZERO) &&
islower(blob->ptr[0])) {
n = TOKEN_LETTER;
} else if (*type == TOKEN_LETTER && isdigit(blob->ptr[0])) {
n = TOKEN_DIGIT;
} else if (*type == TOKEN_SUFFIX && isdigit(blob->ptr[0])) {
n = TOKEN_SUFFIX_NO;
} else {
switch (blob->ptr[0]) {
case '.':
n = TOKEN_DIGIT_OR_ZERO;
break;
case '_':
n = TOKEN_SUFFIX;
break;
case '-':
if (blob->len > 1 && blob->ptr[1] == 'r') {
n = TOKEN_REVISION_NO;
blob->ptr++;
blob->len--;
} else
n = TOKEN_INVALID;
break;
}
blob->ptr++;
blob->len--;
}
if (n < *type) {
if (! ((n == TOKEN_DIGIT_OR_ZERO && *type == TOKEN_DIGIT) ||
(n == TOKEN_SUFFIX && *type == TOKEN_SUFFIX_NO) ||
(n == TOKEN_DIGIT && *type == TOKEN_LETTER)))
n = TOKEN_INVALID;
}
*type = n;
}
static int get_token(int *type, apk_blob_t *blob)
{
static const char *pre_suffixes[] = { "alpha", "beta", "pre", "rc" };
int v = 0, i = 0, nt = TOKEN_INVALID;
switch (*type) {
case TOKEN_DIGIT_OR_ZERO:
/* Leading zero digits get a special treatment */
if (blob->ptr[i] == '0') {
while (blob->ptr[i] == '0' && i < blob->len)
i++;
nt = TOKEN_DIGIT;
v = -i;
break;
}
case TOKEN_DIGIT:
case TOKEN_SUFFIX_NO:
case TOKEN_REVISION_NO:
while (isdigit(blob->ptr[i]) && i < blob->len) {
v *= 10;
v += blob->ptr[i++] - '0';
}
break;
case TOKEN_LETTER:
v = blob->ptr[i++];
break;
case TOKEN_SUFFIX:
for (v = 0; v < ARRAY_SIZE(pre_suffixes); v++) {
i = strlen(pre_suffixes[v]);
if (i < blob->len &&
strncmp(pre_suffixes[v], blob->ptr, i) == 0)
break;
}
if (v < ARRAY_SIZE(pre_suffixes)) {
nt = TOKEN_SUFFIX_NO;
v = v - ARRAY_SIZE(pre_suffixes);
break;
}
if (strncmp("p", blob->ptr, 1) == 0) {
nt = TOKEN_SUFFIX_NO;
v = 1;
break;
}
/* fallthrough: invalid suffix */
default:
*type = TOKEN_INVALID;
return -1;
}
blob->ptr += i;
blob->len -= i;
if (nt != TOKEN_INVALID)
*type = nt;
else
next_token(type, blob);
return v;
}
const char *apk_version_op_string(int mask)
{
switch (mask) {
case APK_VERSION_LESS:
return "<";
case APK_VERSION_LESS|APK_VERSION_EQUAL:
return "<=";
case APK_VERSION_EQUAL:
return "=";
case APK_VERSION_GREATER|APK_VERSION_EQUAL:
return ">=";
case APK_VERSION_GREATER:
return ">";
default:
return "?";
}
}
int apk_version_result_mask(const char *str)
{
int r = 0;
switch (*str) {
case '<':
r = APK_VERSION_LESS;
str++;
break;
case '>':
r = APK_VERSION_GREATER;
str++;
break;
}
if (*str == '=')
r |= APK_VERSION_EQUAL;
return r;
}
int apk_version_validate(apk_blob_t ver)
{
int t = TOKEN_DIGIT;
while (t != TOKEN_END && t != TOKEN_INVALID)
get_token(&t, &ver);
return t == TOKEN_END;
}
int apk_version_compare(const char *str1, const char *str2)
{
int at = TOKEN_DIGIT, bt = TOKEN_DIGIT;
int av = 0, bv = 0;
apk_blob_t a, b;
if (str1 == NULL || str2 == NULL) {
if (str1 == NULL && str2 == NULL)
return APK_VERSION_EQUAL;
return APK_VERSION_EQUAL | APK_VERSION_GREATER | APK_VERSION_LESS;
}
a = APK_BLOB_STR(str1);
b = APK_BLOB_STR(str2);
while (at == bt && at != TOKEN_END && at != TOKEN_INVALID && av == bv) {
av = get_token(&at, &a);
bv = get_token(&bt, &b);
#if 0
fprintf(stderr,
"av=%d, at=%d, a.len=%d\n"
"bv=%d, bt=%d, b.len=%d\n",
av, at, a.len, bv, bt, b.len);
#endif
}
/* value of this token differs? */
if (av < bv)
return APK_VERSION_LESS;
if (av > bv)
return APK_VERSION_GREATER;
if (at < bt)
return get_token(&at, &a) < 0 ?
APK_VERSION_LESS : APK_VERSION_GREATER;
if (bt < at)
return get_token(&bt, &b) > 0 ?
APK_VERSION_LESS : APK_VERSION_GREATER;
return APK_VERSION_EQUAL;
}
|