| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2005 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
// Marked portions of this file are covered by the following |
| 20 |
// copyright: |
| 21 |
/* |
| 22 |
* libdpkg - Debian packaging suite library routines |
| 23 |
* vercmp.c - comparison of version numbers |
| 24 |
* utils.c - Helper functions for dpkg |
| 25 |
* |
| 26 |
* Copyright © 1995 Ian Jackson <[email protected]> |
| 27 |
* Copyright © 2001 Wichert Akkerman <[email protected]> |
| 28 |
* |
| 29 |
* This is free software; you can redistribute it and/or modify |
| 30 |
* it under the terms of the GNU General Public License as |
| 31 |
* published by the Free Software Foundation; either version 2, |
| 32 |
* or (at your option) any later version. |
| 33 |
* |
| 34 |
* This is distributed in the hope that it will be useful, but |
| 35 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 |
* GNU General Public License for more details. |
| 38 |
* |
| 39 |
* You should have received a copy of the GNU General Public License along |
| 40 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 41 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <string.h> |
| 45 |
#include "version.h" |
| 46 |
|
| 47 |
/* <<<<<<< originally from dpkg >>>>>>> */ |
| 48 |
|
| 49 |
/* Reimplementation of the standard ctype.h is* functions. Since gettext |
| 50 |
* has overloaded the meaning of LC_CTYPE we can't use that to force C |
| 51 |
* locale, so use these cis* functions instead. |
| 52 |
*/ |
| 53 |
static int cisdigit(int c) { |
| 54 |
return (c>='0') && (c<='9'); |
| 55 |
} |
| 56 |
|
| 57 |
static int cisalpha(int c) { |
| 58 |
return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z')); |
| 59 |
} |
| 60 |
|
| 61 |
/* assume ascii; warning: evaluates x multiple times! */ |
| 62 |
#define order(x) ((x) == '~' ? -1 \ |
| 63 |
: cisdigit((x)) ? 0 \ |
| 64 |
: !(x) ? 0 \ |
| 65 |
: cisalpha((x)) ? (x) \ |
| 66 |
: (x) + 256) |
| 67 |
|
| 68 |
static int verrevcmp(const char *val, const char *ref) { |
| 69 |
if (!val) val= ""; |
| 70 |
if (!ref) ref= ""; |
| 71 |
|
| 72 |
while (*val || *ref) { |
| 73 |
int first_diff= 0; |
| 74 |
|
| 75 |
while ( (*val && !cisdigit(*val)) || (*ref && !cisdigit(*ref)) ) { |
| 76 |
int vc= order(*val), rc= order(*ref); |
| 77 |
if (vc != rc) return vc - rc; |
| 78 |
val++; ref++; |
| 79 |
} |
| 80 |
|
| 81 |
while ( *val == '0' ) val++; |
| 82 |
while ( *ref == '0' ) ref++; |
| 83 |
while (cisdigit(*val) && cisdigit(*ref)) { |
| 84 |
if (!first_diff) first_diff= *val - *ref; |
| 85 |
val++; ref++; |
| 86 |
} |
| 87 |
if (cisdigit(*val)) return 1; |
| 88 |
if (cisdigit(*ref)) return -1; |
| 89 |
if (first_diff) return first_diff; |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
int versioncompare(const struct versionrevision *version, |
| 95 |
const struct versionrevision *refversion) { |
| 96 |
int r; |
| 97 |
|
| 98 |
if (version->epoch > refversion->epoch) return 1; |
| 99 |
if (version->epoch < refversion->epoch) return -1; |
| 100 |
r= verrevcmp(version->version,refversion->version); if (r) return r; |
| 101 |
return verrevcmp(version->revision,refversion->revision); |
| 102 |
} |
| 103 |
|
| 104 |
/* <<<<<<< END OF originally from dpkg >>>>>>> */ |
| 105 |
|
| 106 |
/* warning: modifies ver by adding nul chars; return pointers point |
| 107 |
* inside ver. */ |
| 108 |
bool parse_version(struct versionrevision *rv, char *ver, size_t len) |
| 109 |
{ |
| 110 |
rv->version = strchr(ver, ':'); |
| 111 |
if (rv->version == NULL) { |
| 112 |
rv->version = ver; |
| 113 |
} else { |
| 114 |
*(rv->version++) = '\0'; |
| 115 |
} |
| 116 |
rv->revision = strrchr(ver, '-'); |
| 117 |
|
| 118 |
if (rv->revision == NULL) { |
| 119 |
rv->revision = ver + len; |
| 120 |
} else { |
| 121 |
*(rv->revision++) = '\0'; |
| 122 |
} |
| 123 |
|
| 124 |
if (rv->version != ver) { |
| 125 |
rv->epoch = 0; |
| 126 |
for (char *p = ver; *p != '\0'; p++) { |
| 127 |
if (!('0' <= *p && *p <= '9')) return false; |
| 128 |
rv->epoch = rv->epoch * 10 + (*p - '0'); |
| 129 |
} |
| 130 |
} else { |
| 131 |
rv->epoch = 0; |
| 132 |
} |
| 133 |
|
| 134 |
for (char *p = rv->version; *p != '\0'; p++) { |
| 135 |
if (('a' <= *p && *p <= 'z') || |
| 136 |
('A' <= *p && *p <= 'Z') || |
| 137 |
('0' <= *p && *p <= '9') || |
| 138 |
*p == '.' || |
| 139 |
*p == '-' || |
| 140 |
*p == '+' || |
| 141 |
*p == ':' || |
| 142 |
*p == '~') continue; |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
for (char *p = rv->revision; *p != '\0'; p++) { |
| 147 |
if (('a' <= *p && *p <= 'z') || |
| 148 |
('A' <= *p && *p <= 'Z') || |
| 149 |
('0' <= *p && *p <= '9') || |
| 150 |
*p == '.' || |
| 151 |
*p == '+' || |
| 152 |
*p == '~') continue; |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
|