1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 /* {{{ php_canonicalize_version() */
28 
29 PHPAPI char *
php_canonicalize_version(const char *version)30 php_canonicalize_version(const char *version)
31 {
32     size_t len = strlen(version);
33     char *buf = safe_emalloc(len, 2, 1), *q, lp, lq;
34     const char *p;
35 
36     if (len == 0) {
37         *buf = '\0';
38         return buf;
39     }
40 
41     p = version;
42     q = buf;
43     *q++ = lp = *p++;
44 
45     while (*p) {
46 /*  s/[-_+]/./g;
47  *  s/([^\d\.])([^\D\.])/$1.$2/g;
48  *  s/([^\D\.])([^\d\.])/$1.$2/g;
49  */
50 #define isdig(x) (isdigit(x)&&(x)!='.')
51 #define isndig(x) (!isdigit(x)&&(x)!='.')
52 #define isspecialver(x) ((x)=='-'||(x)=='_'||(x)=='+')
53 
54 		lq = *(q - 1);
55 		if (isspecialver(*p)) {
56 			if (lq != '.') {
57 				*q++ = '.';
58 			}
59 		} else if ((isndig(lp) && isdig(*p)) || (isdig(lp) && isndig(*p))) {
60 			if (lq != '.') {
61 				*q++ = '.';
62 			}
63 			*q++ = *p;
64 		} else if (!isalnum(*p)) {
65 			if (lq != '.') {
66 				*q++ = '.';
67 			}
68 		} else {
69 			*q++ = *p;
70 		}
71 		lp = *p++;
72     }
73     *q++ = '\0';
74     return buf;
75 }
76 
77 /* }}} */
78 /* {{{ compare_special_version_forms() */
79 
80 typedef struct {
81 	const char *name;
82 	int order;
83 } special_forms_t;
84 
85 static int
compare_special_version_forms(char *form1, char *form2)86 compare_special_version_forms(char *form1, char *form2)
87 {
88 	int found1 = -1, found2 = -1;
89 	special_forms_t special_forms[11] = {
90 		{"dev", 0},
91 		{"alpha", 1},
92 		{"a", 1},
93 		{"beta", 2},
94 		{"b", 2},
95 		{"RC", 3},
96 		{"rc", 3},
97 		{"#", 4},
98 		{"pl", 5},
99 		{"p", 5},
100 		{NULL, 0},
101 	};
102 	special_forms_t *pp;
103 
104 	for (pp = special_forms; pp && pp->name; pp++) {
105 		if (strncmp(form1, pp->name, strlen(pp->name)) == 0) {
106 			found1 = pp->order;
107 			break;
108 		}
109 	}
110 	for (pp = special_forms; pp && pp->name; pp++) {
111 		if (strncmp(form2, pp->name, strlen(pp->name)) == 0) {
112 			found2 = pp->order;
113 			break;
114 		}
115 	}
116 	return ZEND_NORMALIZE_BOOL(found1 - found2);
117 }
118 
119 /* }}} */
120 /* {{{ php_version_compare() */
121 
122 PHPAPI int
php_version_compare(const char *orig_ver1, const char *orig_ver2)123 php_version_compare(const char *orig_ver1, const char *orig_ver2)
124 {
125 	char *ver1;
126 	char *ver2;
127 	char *p1, *p2, *n1, *n2;
128 	long l1, l2;
129 	int compare = 0;
130 
131 	if (!*orig_ver1 || !*orig_ver2) {
132 		if (!*orig_ver1 && !*orig_ver2) {
133 			return 0;
134 		} else {
135 			return *orig_ver1 ? 1 : -1;
136 		}
137 	}
138 	if (orig_ver1[0] == '#') {
139 		ver1 = estrdup(orig_ver1);
140 	} else {
141 		ver1 = php_canonicalize_version(orig_ver1);
142 	}
143 	if (orig_ver2[0] == '#') {
144 		ver2 = estrdup(orig_ver2);
145 	} else {
146 		ver2 = php_canonicalize_version(orig_ver2);
147 	}
148 	p1 = n1 = ver1;
149 	p2 = n2 = ver2;
150 	while (*p1 && *p2 && n1 && n2) {
151 		if ((n1 = strchr(p1, '.')) != NULL) {
152 			*n1 = '\0';
153 		}
154 		if ((n2 = strchr(p2, '.')) != NULL) {
155 			*n2 = '\0';
156 		}
157 		if (isdigit(*p1) && isdigit(*p2)) {
158 			/* compare element numerically */
159 			l1 = strtol(p1, NULL, 10);
160 			l2 = strtol(p2, NULL, 10);
161 			compare = ZEND_NORMALIZE_BOOL(l1 - l2);
162 		} else if (!isdigit(*p1) && !isdigit(*p2)) {
163 			/* compare element names */
164 			compare = compare_special_version_forms(p1, p2);
165 		} else {
166 			/* mix of names and numbers */
167 			if (isdigit(*p1)) {
168 				compare = compare_special_version_forms("#N#", p2);
169 			} else {
170 				compare = compare_special_version_forms(p1, "#N#");
171 			}
172 		}
173 		if (compare != 0) {
174 			break;
175 		}
176 		if (n1 != NULL) {
177 			p1 = n1 + 1;
178 		}
179 		if (n2 != NULL) {
180 			p2 = n2 + 1;
181 		}
182 	}
183 	if (compare == 0) {
184 		if (n1 != NULL) {
185 			if (isdigit(*p1)) {
186 				compare = 1;
187 			} else {
188 				compare = php_version_compare(p1, "#N#");
189 			}
190 		} else if (n2 != NULL) {
191 			if (isdigit(*p2)) {
192 				compare = -1;
193 			} else {
194 				compare = php_version_compare("#N#", p2);
195 			}
196 		}
197 	}
198 	efree(ver1);
199 	efree(ver2);
200 	return compare;
201 }
202 
203 /* }}} */
204 /* {{{ proto int version_compare(string ver1, string ver2 [, string oper])
205   Compares two "PHP-standardized" version number strings */
206 
PHP_FUNCTIONnull207 PHP_FUNCTION(version_compare)
208 {
209 	char *v1, *v2, *op = NULL;
210 	size_t v1_len, v2_len, op_len = 0;
211 	int compare;
212 
213 	ZEND_PARSE_PARAMETERS_START(2, 3)
214 		Z_PARAM_STRING(v1, v1_len)
215 		Z_PARAM_STRING(v2, v2_len)
216 		Z_PARAM_OPTIONAL
217 		Z_PARAM_STRING(op, op_len)
218 	ZEND_PARSE_PARAMETERS_END();
219 
220 	compare = php_version_compare(v1, v2);
221 	if (!op) {
222 		RETURN_LONG(compare);
223 	}
224 	if (!strncmp(op, "<", op_len) || !strncmp(op, "lt", op_len)) {
225 		RETURN_BOOL(compare == -1);
226 	}
227 	if (!strncmp(op, "<=", op_len) || !strncmp(op, "le", op_len)) {
228 		RETURN_BOOL(compare != 1);
229 	}
230 	if (!strncmp(op, ">", op_len) || !strncmp(op, "gt", op_len)) {
231 		RETURN_BOOL(compare == 1);
232 	}
233 	if (!strncmp(op, ">=", op_len) || !strncmp(op, "ge", op_len)) {
234 		RETURN_BOOL(compare != -1);
235 	}
236 	if (!strncmp(op, "==", op_len) || !strncmp(op, "=", op_len) || !strncmp(op, "eq", op_len)) {
237 		RETURN_BOOL(compare == 0);
238 	}
239 	if (!strncmp(op, "!=", op_len) || !strncmp(op, "<>", op_len) || !strncmp(op, "ne", op_len)) {
240 		RETURN_BOOL(compare != 0);
241 	}
242 	RETURN_NULL();
243 }
244 
245 /* }}} */
246