versioning.c 5.9 KB

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