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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
/**
* @file version_spec.c
*/
#include "spm.h"
/**
*
* @param str
* @return
*/
char *version_suffix_get_alpha(char *str) {
size_t i;
size_t len = strlen(str);
for (i = 0; i < len; i++) {
// return pointer to the first alphabetic character we find
if (isalpha(str[i])) {
return &str[i];
}
}
return NULL;
}
/**
*
* @param str
* @return
*/
char *version_suffix_get_modifier(char *str) {
size_t i;
char *modifiers[] = {
"r", // VCS revision
"rc", // Release Candidate
"pre", // Pre-Release
"dev", // Development
"post", // Post-Release
NULL,
};
for (i = 0; i < strlen(str); i++) {
for (int m = 0; modifiers[m] != NULL; m++) {
if (strncasecmp(&str[i], modifiers[m], strlen(modifiers[m])) == 0) {
return &str[i];
}
}
}
return NULL;
}
/**
*
* @param str
* @return
*/
int64_t version_suffix_modifier_calc(char *str) {
int64_t result = 0;
char *tmp_s = str;
if (strncasecmp(str, "rc", 2) == 0) {
// do rc
tmp_s += strlen("rc");
if (isdigit(*tmp_s)) {
result -= atoi(tmp_s);
}
else {
result -= 1;
}
}
else if (strncasecmp(str, "pre", 3) == 0) {
// do pre
tmp_s += strlen("pre");
if (isdigit(*tmp_s)) {
result -= atoi(tmp_s);
}
else {
result -= 1;
}
}
else if (strncasecmp(str, "dev", 3) == 0) {
// do dev
tmp_s += strlen("dev");
if (isdigit(*tmp_s)) {
result -= atoi(tmp_s);
}
else {
result -= 1;
}
}
else if (strncasecmp(str, "post", 4) == 0) {
// do post
tmp_s += strlen("post");
if (isdigit(*tmp_s)) {
result += atoi(tmp_s);
}
else {
result += 1;
}
}
return result;
}
/**
*
* @param str
* @return
*/
int version_suffix_alpha_calc(char *str) {
int x = 0;
char chs[255];
char *ch = chs;
memset(chs, '\0', sizeof(chs));
strncpy(chs, str, strlen(str));
// Handle cases where the two suffixes are not delimited by anything
// Start scanning one character ahead of the alphabetic suffix and terminate the string
// when/if we reach another alphabetic character (presumably a version modifer)
for (int i = 1; chs[i] != '\0'; i++) {
if (isalpha(chs[i])) {
chs[i] = '\0';
}
}
// Convert character to hex-ish
x = (*ch - 'a') + 0xa;
// Ensure the string ends with a digit
if (strlen(str) == 1) {
strcat(ch, "0");
}
// Convert trailing numerical value to an integer
while (*ch != '\0') {
if (!isdigit(*ch)) {
ch++;
continue;
}
x += (int)strtol(ch, NULL, 10);
break;
}
return x;
}
static int isdigits(char *s) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (isdigit(s[i]) == 0) {
return 0; // non-digit found, fail
}
}
return 1; // all digits, succeed
}
static int reindex(char c) {
int result = c - '0';
return result;
}
static char *tolower_s(char *s) {
for (size_t i = 0; s[i] != '\0'; i++) {
s[i] = tolower(s[i]);
}
return s;
}
/**
* Convert a string (`x.y.z.nnnn` or `1abcdefg2` - doesn't matter) to an integer
* @param s
* @return
*/
uint64_t version_from(const char *str) {
uint64_t result;
uint64_t addendum;
char *vstr;
char *result_tmp;
char **part;
StrList *vconv;
// Check input string isn't NULL
if (str == NULL) {
return 0;
}
// Make a copy of the input string
vstr = strdup(str);
if (vstr == NULL) {
spmerrno = errno;
return 0;
}
// Initialize StrList
vconv = strlist_init();
if (vconv == NULL) {
spmerrno = errno;
return 0;
}
// Convert any uppercase letters to lowercase
vstr = tolower_s(vstr);
// Pop local version suffix
char *local_version = strstr(vstr, VERSION_LOCAL);
if (local_version != NULL) {
*local_version = '\0';
}
// Split version string on periods, if any
part = split(vstr, VERSION_DELIM);
if (part == NULL) {
spmerrno = errno;
return 0;
}
// Populate our StrList with version information
for (size_t i = 0; part[i] != NULL; i++) {
char tmp[255] = {0};
memset(tmp, '\0', sizeof(tmp));
if (isdigits(part[i])) {
// Every character in the string is a digit, so append it as-is
strlist_append(vconv, part[i]);
} else {
// Not all characters were digits
char *data = part[i];
int digit = isdigit(*data);
// The first character is a digit. Try to consume the whole value
if (digit > 0) {
for (size_t ch = 0; *data != '\0' && isdigit(*data) > 0; ch++, data++) {
tmp[ch] = *data;
}
strlist_append(vconv, tmp);
}
while (*data != '\0') {
// The "+" prefix below indicates the value shall be added to the addendum
// Not a digit so subtract its ASCII value from '0' with reindex
// Calculate character index + 1 (i.e. a=1, b=2, ..., z=25)
sprintf(tmp, "+%d", reindex(*data) + 1);
strlist_append(vconv, tmp);
data++;
}
}
}
split_free(part);
// Construct the suffix portion of the version. This value is added to the result to maximize
// the resolution between different versions (making (1.1.0a > 1.1.0 && 1.1.1 > 1.1.0a) possible)
addendum = 0L;
for (size_t i = 0; i < strlist_count(vconv); i++) {
char *item = strlist_item(vconv, i);
if (*item == '+') {
addendum += strtoull(&item[1], NULL, VERSION_BASE);
// Purge this record from the StrList now that it has fulfilled its purpose
strlist_remove(vconv, i);
i--;
}
}
result_tmp = join(vconv->data, "");
result = strtoull(result_tmp, NULL, VERSION_BASE);
result <<= VERSION_ADDENDUM_BITS;
result += addendum + strlen(result_tmp);
free(vstr);
strlist_free(vconv);
return result;
}
/**
*
* @param version_str
* @return
*/
int64_t version_from_old(const char *version_str) {
const char *delim = ".";
int64_t result = 0;
if (version_str == NULL) {
return 0;
}
int seen_alpha = 0; // Does the tail contain a single character, but not a modifier?
int seen_modifier = 0; // Does the tail contain "rc", "dev", "pre", and so forth?
char head[255]; // digits of the string
char tail[255]; // alphabetic characters of the string
char *suffix_alpha = NULL; // pointer to location of the first character after the version
char *suffix_modifier = NULL; // pointer to location of the modifier after the version
char *x = NULL; // pointer to each string delimited by "."
char *vstr = strdup(version_str);
if (!vstr) {
perror("Version string copy");
return -1;
}
memset(head, '\0', sizeof(head));
memset(tail, '\0', sizeof(tail));
// Split the version into parts
while ((x = strsep(&vstr, delim)) != NULL) {
int64_t tmp = 0;
// populate the head (numeric characters)
int has_digit = isdigit(*x);
int has_alpha = isalpha(*x);
strncpy(head, x, strlen(x));
for (size_t i = 0; i < strlen(head); i++) {
if (isalpha(head[i])) {
// populate the tail (alphabetic characters)
strncpy(tail, &head[i], strlen(&head[i]));
head[i] = '\0';
break;
}
}
// Detect alphabetic suffix
if (!seen_alpha) {
if ((suffix_alpha = version_suffix_get_alpha(x)) != NULL) {
seen_alpha = 1;
}
}
// Detect modifier suffix
if (!seen_modifier) {
if ((suffix_modifier = version_suffix_get_modifier(x)) != NULL) {
seen_modifier = 1;
}
}
// Stop processing if the head starts with something other than numbers
if (!isdigit(head[0])) {
break;
}
// Convert the head to an integer
tmp = strtoul(head, NULL, 10);
// Update result. Each portion of the numeric version is its own byte
// Version PARTS are limited to 255
result = result << 8 | tmp;
}
if (suffix_alpha != NULL) {
// Convert the alphabetic suffix to an integer
int64_t sac = version_suffix_alpha_calc(suffix_alpha);
result += sac;
}
if (suffix_modifier != NULL) {
// Convert the modifier string to an integer
int64_t smc = version_suffix_modifier_calc(suffix_modifier);
if (smc < 0) {
result -= ~smc + 1;
}
else {
result += smc;
}
}
free(vstr);
return result;
}
/**
*
* @param op
* @return
*/
int version_spec_from(const char *op) {
int flags = VERSION_NOOP;
size_t len = strlen(op);
for (size_t i = 0; i < len; i++) {
if (op[i] == '>') {
flags |= VERSION_GT;
}
else if (op[i] == '<') {
flags |= VERSION_LT;
}
else if (op[i] == '=' || (len > 1 && strncmp(&op[i], "==", 2) == 0)) {
flags |= VERSION_EQ;
}
else if (op[i] == '!') {
flags |= VERSION_NE;
}
else if (op[i] == '~') {
flags |= VERSION_COMPAT;
}
}
return flags;
}
/**
*
* @param a
* @param b
* @return
*/
static int _find_by_spec_compare(const void *a, const void *b) {
const ManifestPackage *aa = *(const ManifestPackage**)a;
const ManifestPackage *bb = *(const ManifestPackage**)b;
int64_t version_a = version_from(aa->version);
int64_t version_b = version_from(bb->version);
return version_a > version_b;
}
/**
*
* @param manifest
* @param name
* @param op
* @param version_str
* @return
*/
ManifestPackage **find_by_spec(const Manifest *manifest, const char *name, const char *op, const char *version_str) {
size_t record = 0;
ManifestPackage **list = (ManifestPackage **) calloc(manifest->records + 1, sizeof(ManifestPackage *));
if (!list) {
perror("ManifestPackage array");
fprintf(SYSERROR);
return NULL;
}
for (size_t i = 0; i < manifest->records; i++) {
if (strcmp(manifest->packages[i]->name, name) == 0) {
int64_t version_a = version_from(manifest->packages[i]->version);
int64_t version_b = version_from(version_str);
int spec = version_spec_from(op);
int res = 0;
if (spec & VERSION_GT && spec & VERSION_EQ) {
res = version_a >= version_b;
}
else if (spec & VERSION_LT && spec & VERSION_EQ) {
res = version_a <= version_b;
}
else if (spec & VERSION_NE && spec & VERSION_EQ) {
res = version_a != version_b;
}
else if (spec & VERSION_GT) {
res = version_a > version_b;
}
else if (spec & VERSION_LT) {
res = version_a < version_b;
}
else if (spec & VERSION_COMPAT) {
// TODO
}
else if (spec & VERSION_EQ) {
res = version_a == version_b;
}
if (res != 0) {
list[record] = manifest_package_copy(manifest->packages[i]);
if (!list[record]) {
perror("Unable to allocate memory for manifest record");
fprintf(SYSERROR);
return NULL;
}
record++;
}
}
}
qsort(list, record, sizeof(ManifestPackage *), _find_by_spec_compare);
return list;
}
static void get_name(char **buf, const char *_str) {
char *str = strdup(_str);
int has_relational = 0;
int is_archive = endswith(str, SPM_PACKAGE_EXTENSION);
for (size_t i = 0; str[i] != '\0'; i++) {
if (isrelational(str[i]))
has_relational = 1;
}
if (is_archive == 0 && !has_relational) {
strcpy((*buf), str);
}
else if (has_relational) {
size_t stop = 0;
for (stop = 0; !isrelational(str[stop]); stop++);
strncpy((*buf), str, stop);
(*buf)[stop] = '\0';
} else {
StrList *tmp = strlist_init();
char sep[2];
sep[0] = SPM_PACKAGE_MEMBER_SEPARATOR;
sep[1] = '\0';
char **parts = split(str, sep);
if (parts != NULL) {
for (size_t i = 0; parts[i] != NULL; i++) {
strlist_append(tmp, parts[i]);
}
}
split_free(parts);
if (strlist_count(tmp) > SPM_PACKAGE_MIN_DELIM) {
strlist_set(tmp, strlist_count(tmp) - SPM_PACKAGE_MIN_DELIM, NULL);
}
char *result = join(tmp->data, sep);
strcpy((*buf), result);
free(result);
strlist_free(tmp);
}
free(str);
}
static char *get_operators(char **op, const char *_strspec) {
const char *operators = VERSION_OPERATORS; // note: whitespace is synonymous with ">=" if no operators are present
char *pos = NULL;
pos = strpbrk(_strspec, operators);
if (pos != NULL) {
for (size_t i = 0; !isalnum(*pos) || *pos == '.'; i++) {
(*op)[i] = *pos++;
}
}
return pos;
}
ManifestPackage *find_by_strspec(const Manifest *manifest, const char *_strspec) {
char *pos = NULL;
char s_op[NAME_MAX];
char s_name[NAME_MAX];
char s_version[NAME_MAX];
char *op = s_op;
char *name = s_name;
char *version = s_version;
char *strspec = strdup(_strspec);
ManifestPackage **m = NULL;
ManifestPackage *selected = NULL;
memset(op, '\0', NAME_MAX);
memset(name, '\0', NAME_MAX);
memset(version, '\0', NAME_MAX);
// Parse package name
get_name(&name, strspec);
// Get the starting address of any operator(s) (>, <, =, etc)
pos = get_operators(&op, strspec);
// No operator(s) found
if (pos == NULL) {
// Try `name >= 0`
m = find_by_spec(manifest, name, ">=", NULL);
}
// When `m` is still NULL after applying the default operator
if (m == NULL) {
// Parse the version string if it's there
for (size_t i = 0; *(pos + i) != '\0'; i++) {
version[i] = *(pos + i);
}
// Try `name op version`
m = find_by_spec(manifest, name, op, version);
}
// Select the highest priority package
if (m != NULL) {
// (m[0] == default manifest, m[>0] == user-defined manifest)
for (size_t i = 0; m[i] != NULL; i++) {
selected = m[i];
}
}
free(strspec);
return selected; // or NULL
}
|