1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Stig Sæther Bakken <ssb@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "php.h"
25 #include "php_versioning.h"
26
27 #define sign(n) ((n)<0?-1:((n)>0?1:0))
28
29 /* {{{ php_canonicalize_version() */
30
31 PHPAPI char *
php_canonicalize_version(const char * version)32 php_canonicalize_version(const char *version)
33 {
34 size_t len = strlen(version);
35 char *buf = safe_emalloc(len, 2, 1), *q, lp, lq;
36 const char *p;
37
38 if (len == 0) {
39 *buf = '\0';
40 return buf;
41 }
42
43 p = version;
44 q = buf;
45 *q++ = lp = *p++;
46
47 while (*p) {
48 /* s/[-_+]/./g;
49 * s/([^\d\.])([^\D\.])/$1.$2/g;
50 * s/([^\D\.])([^\d\.])/$1.$2/g;
51 */
52 #define isdig(x) (isdigit(x)&&(x)!='.')
53 #define isndig(x) (!isdigit(x)&&(x)!='.')
54 #define isspecialver(x) ((x)=='-'||(x)=='_'||(x)=='+')
55
56 lq = *(q - 1);
57 if (isspecialver(*p)) {
58 if (lq != '.') {
59 *q++ = '.';
60 }
61 } else if ((isndig(lp) && isdig(*p)) || (isdig(lp) && isndig(*p))) {
62 if (lq != '.') {
63 *q++ = '.';
64 }
65 *q++ = *p;
66 } else if (!isalnum(*p)) {
67 if (lq != '.') {
68 *q++ = '.';
69 }
70 } else {
71 *q++ = *p;
72 }
73 lp = *p++;
74 }
75 *q++ = '\0';
76 return buf;
77 }
78
79 /* }}} */
80 /* {{{ compare_special_version_forms() */
81
82 typedef struct {
83 const char *name;
84 int order;
85 } special_forms_t;
86
87 static int
compare_special_version_forms(char * form1,char * form2)88 compare_special_version_forms(char *form1, char *form2)
89 {
90 int found1 = -1, found2 = -1;
91 special_forms_t special_forms[11] = {
92 {"dev", 0},
93 {"alpha", 1},
94 {"a", 1},
95 {"beta", 2},
96 {"b", 2},
97 {"RC", 3},
98 {"rc", 3},
99 {"#", 4},
100 {"pl", 5},
101 {"p", 5},
102 {NULL, 0},
103 };
104 special_forms_t *pp;
105
106 for (pp = special_forms; pp && pp->name; pp++) {
107 if (strncmp(form1, pp->name, strlen(pp->name)) == 0) {
108 found1 = pp->order;
109 break;
110 }
111 }
112 for (pp = special_forms; pp && pp->name; pp++) {
113 if (strncmp(form2, pp->name, strlen(pp->name)) == 0) {
114 found2 = pp->order;
115 break;
116 }
117 }
118 return sign(found1 - found2);
119 }
120
121 /* }}} */
122 /* {{{ php_version_compare() */
123
124 PHPAPI int
php_version_compare(const char * orig_ver1,const char * orig_ver2)125 php_version_compare(const char *orig_ver1, const char *orig_ver2)
126 {
127 char *ver1;
128 char *ver2;
129 char *p1, *p2, *n1, *n2;
130 long l1, l2;
131 int compare = 0;
132
133 if (!*orig_ver1 || !*orig_ver2) {
134 if (!*orig_ver1 && !*orig_ver2) {
135 return 0;
136 } else {
137 return *orig_ver1 ? 1 : -1;
138 }
139 }
140 if (orig_ver1[0] == '#') {
141 ver1 = estrdup(orig_ver1);
142 } else {
143 ver1 = php_canonicalize_version(orig_ver1);
144 }
145 if (orig_ver2[0] == '#') {
146 ver2 = estrdup(orig_ver2);
147 } else {
148 ver2 = php_canonicalize_version(orig_ver2);
149 }
150 p1 = n1 = ver1;
151 p2 = n2 = ver2;
152 while (*p1 && *p2 && n1 && n2) {
153 if ((n1 = strchr(p1, '.')) != NULL) {
154 *n1 = '\0';
155 }
156 if ((n2 = strchr(p2, '.')) != NULL) {
157 *n2 = '\0';
158 }
159 if (isdigit(*p1) && isdigit(*p2)) {
160 /* compare element numerically */
161 l1 = strtol(p1, NULL, 10);
162 l2 = strtol(p2, NULL, 10);
163 compare = sign(l1 - l2);
164 } else if (!isdigit(*p1) && !isdigit(*p2)) {
165 /* compare element names */
166 compare = compare_special_version_forms(p1, p2);
167 } else {
168 /* mix of names and numbers */
169 if (isdigit(*p1)) {
170 compare = compare_special_version_forms("#N#", p2);
171 } else {
172 compare = compare_special_version_forms(p1, "#N#");
173 }
174 }
175 if (compare != 0) {
176 break;
177 }
178 if (n1 != NULL) {
179 p1 = n1 + 1;
180 }
181 if (n2 != NULL) {
182 p2 = n2 + 1;
183 }
184 }
185 if (compare == 0) {
186 if (n1 != NULL) {
187 if (isdigit(*p1)) {
188 compare = 1;
189 } else {
190 compare = php_version_compare(p1, "#N#");
191 }
192 } else if (n2 != NULL) {
193 if (isdigit(*p2)) {
194 compare = -1;
195 } else {
196 compare = php_version_compare("#N#", p2);
197 }
198 }
199 }
200 efree(ver1);
201 efree(ver2);
202 return compare;
203 }
204
205 /* }}} */
206 /* {{{ proto int version_compare(string ver1, string ver2 [, string oper])
207 Compares two "PHP-standardized" version number strings */
208
PHP_FUNCTION(version_compare)209 PHP_FUNCTION(version_compare)
210 {
211 char *v1, *v2, *op = NULL;
212 size_t v1_len, v2_len, op_len = 0;
213 int compare;
214
215 ZEND_PARSE_PARAMETERS_START(2, 3)
216 Z_PARAM_STRING(v1, v1_len)
217 Z_PARAM_STRING(v2, v2_len)
218 Z_PARAM_OPTIONAL
219 Z_PARAM_STRING(op, op_len)
220 ZEND_PARSE_PARAMETERS_END();
221
222 compare = php_version_compare(v1, v2);
223 if (!op) {
224 RETURN_LONG(compare);
225 }
226 if (!strncmp(op, "<", op_len) || !strncmp(op, "lt", op_len)) {
227 RETURN_BOOL(compare == -1);
228 }
229 if (!strncmp(op, "<=", op_len) || !strncmp(op, "le", op_len)) {
230 RETURN_BOOL(compare != 1);
231 }
232 if (!strncmp(op, ">", op_len) || !strncmp(op, "gt", op_len)) {
233 RETURN_BOOL(compare == 1);
234 }
235 if (!strncmp(op, ">=", op_len) || !strncmp(op, "ge", op_len)) {
236 RETURN_BOOL(compare != -1);
237 }
238 if (!strncmp(op, "==", op_len) || !strncmp(op, "=", op_len) || !strncmp(op, "eq", op_len)) {
239 RETURN_BOOL(compare == 0);
240 }
241 if (!strncmp(op, "!=", op_len) || !strncmp(op, "<>", op_len) || !strncmp(op, "ne", op_len)) {
242 RETURN_BOOL(compare != 0);
243 }
244 RETURN_NULL();
245 }
246
247 /* }}} */
248
249 /*
250 * Local variables:
251 * tab-width: 4
252 * c-basic-offset: 4
253 * indent-tabs-mode: t
254 * End:
255 */
256