2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
#include "nsVersionComparator.h"
|
|
|
|
|
2012-09-06 22:47:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-09-08 18:44:33 +00:00
|
|
|
#include <mozilla/StandardInteger.h>
|
2012-09-06 22:47:19 +00:00
|
|
|
#if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
|
|
|
|
#include <wchar.h>
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#endif
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
struct VersionPart {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t numA;
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
const char *strB; // NOT null-terminated, can be a null pointer
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t strBlen;
|
2005-07-23 14:05:25 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t numC;
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
char *extraD; // null-terminated
|
|
|
|
};
|
|
|
|
|
2008-12-03 13:41:12 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
struct VersionPartW {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t numA;
|
2008-12-03 13:41:12 +00:00
|
|
|
|
|
|
|
const PRUnichar *strB; // NOT null-terminated, can be a null pointer
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t strBlen;
|
2008-12-03 13:41:12 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t numC;
|
2008-12-03 13:41:12 +00:00
|
|
|
|
|
|
|
PRUnichar *extraD; // null-terminated
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2005-07-23 14:05:25 +00:00
|
|
|
/**
|
|
|
|
* Parse a version part into a number and "extra text".
|
|
|
|
*
|
|
|
|
* @returns A pointer to the next versionpart, or null if none.
|
|
|
|
*/
|
|
|
|
static char*
|
|
|
|
ParseVP(char *part, VersionPart &result)
|
|
|
|
{
|
2005-08-25 12:14:13 +00:00
|
|
|
char *dot;
|
|
|
|
|
2005-10-17 20:27:22 +00:00
|
|
|
result.numA = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
result.strB = nullptr;
|
2005-10-17 20:27:22 +00:00
|
|
|
result.strBlen = 0;
|
|
|
|
result.numC = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
result.extraD = nullptr;
|
2005-10-17 20:27:22 +00:00
|
|
|
|
|
|
|
if (!part)
|
2005-08-25 12:14:13 +00:00
|
|
|
return part;
|
|
|
|
|
|
|
|
dot = strchr(part, '.');
|
2005-07-23 14:05:25 +00:00
|
|
|
if (dot)
|
|
|
|
*dot = '\0';
|
|
|
|
|
2005-10-04 02:47:02 +00:00
|
|
|
if (part[0] == '*' && part[1] == '\0') {
|
2012-09-06 21:54:59 +00:00
|
|
|
result.numA = INT32_MAX;
|
2005-10-04 02:47:02 +00:00
|
|
|
result.strB = "";
|
|
|
|
}
|
|
|
|
else {
|
2007-07-08 07:08:04 +00:00
|
|
|
result.numA = strtol(part, const_cast<char**>(&result.strB), 10);
|
2005-10-04 02:47:02 +00:00
|
|
|
}
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
if (!*result.strB) {
|
2012-07-30 14:20:58 +00:00
|
|
|
result.strB = nullptr;
|
2005-07-23 14:05:25 +00:00
|
|
|
result.strBlen = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (result.strB[0] == '+') {
|
|
|
|
static const char kPre[] = "pre";
|
|
|
|
|
|
|
|
++result.numA;
|
|
|
|
result.strB = kPre;
|
|
|
|
result.strBlen = sizeof(kPre) - 1;
|
|
|
|
}
|
|
|
|
else {
|
2005-08-09 19:23:29 +00:00
|
|
|
const char *numstart = strpbrk(result.strB, "0123456789+-");
|
2005-07-23 14:05:25 +00:00
|
|
|
if (!numstart) {
|
|
|
|
result.strBlen = strlen(result.strB);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.strBlen = numstart - result.strB;
|
|
|
|
|
|
|
|
result.numC = strtol(numstart, &result.extraD, 10);
|
|
|
|
if (!*result.extraD)
|
2012-07-30 14:20:58 +00:00
|
|
|
result.extraD = nullptr;
|
2005-07-23 14:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dot) {
|
|
|
|
++dot;
|
|
|
|
|
|
|
|
if (!*dot)
|
2012-07-30 14:20:58 +00:00
|
|
|
dot = nullptr;
|
2005-07-23 14:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dot;
|
2005-07-23 15:35:31 +00:00
|
|
|
}
|
2005-07-23 14:05:25 +00:00
|
|
|
|
2008-12-03 13:41:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a version part into a number and "extra text".
|
|
|
|
*
|
|
|
|
* @returns A pointer to the next versionpart, or null if none.
|
|
|
|
*/
|
|
|
|
#ifdef XP_WIN
|
|
|
|
static PRUnichar*
|
|
|
|
ParseVP(PRUnichar *part, VersionPartW &result)
|
|
|
|
{
|
|
|
|
|
|
|
|
PRUnichar *dot;
|
|
|
|
|
|
|
|
result.numA = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
result.strB = nullptr;
|
2008-12-03 13:41:12 +00:00
|
|
|
result.strBlen = 0;
|
|
|
|
result.numC = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
result.extraD = nullptr;
|
2008-12-03 13:41:12 +00:00
|
|
|
|
|
|
|
if (!part)
|
|
|
|
return part;
|
|
|
|
|
|
|
|
dot = wcschr(part, '.');
|
|
|
|
if (dot)
|
|
|
|
*dot = '\0';
|
|
|
|
|
|
|
|
if (part[0] == '*' && part[1] == '\0') {
|
2012-09-28 06:57:33 +00:00
|
|
|
result.numA = INT32_MAX;
|
2008-12-03 13:41:12 +00:00
|
|
|
result.strB = L"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.numA = wcstol(part, const_cast<PRUnichar**>(&result.strB), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*result.strB) {
|
2012-07-30 14:20:58 +00:00
|
|
|
result.strB = nullptr;
|
2008-12-03 13:41:12 +00:00
|
|
|
result.strBlen = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (result.strB[0] == '+') {
|
|
|
|
static const PRUnichar kPre[] = L"pre";
|
|
|
|
|
|
|
|
++result.numA;
|
|
|
|
result.strB = kPre;
|
|
|
|
result.strBlen = sizeof(kPre) - 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const PRUnichar *numstart = wcspbrk(result.strB, L"0123456789+-");
|
|
|
|
if (!numstart) {
|
|
|
|
result.strBlen = wcslen(result.strB);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.strBlen = numstart - result.strB;
|
|
|
|
|
|
|
|
result.numC = wcstol(numstart, &result.extraD, 10);
|
|
|
|
if (!*result.extraD)
|
2012-07-30 14:20:58 +00:00
|
|
|
result.extraD = nullptr;
|
2008-12-03 13:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dot) {
|
|
|
|
++dot;
|
|
|
|
|
|
|
|
if (!*dot)
|
2012-07-30 14:20:58 +00:00
|
|
|
dot = nullptr;
|
2008-12-03 13:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dot;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-23 14:05:25 +00:00
|
|
|
// compare two null-terminated strings, which may be null pointers
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t
|
2005-07-23 14:05:25 +00:00
|
|
|
ns_strcmp(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
// any string is *before* no string
|
|
|
|
if (!str1)
|
|
|
|
return str2 != 0;
|
|
|
|
|
|
|
|
if (!str2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return strcmp(str1, str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare two length-specified string, which may be null pointers
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t
|
|
|
|
ns_strnncmp(const char *str1, uint32_t len1, const char *str2, uint32_t len2)
|
2005-07-23 14:05:25 +00:00
|
|
|
{
|
|
|
|
// any string is *before* no string
|
|
|
|
if (!str1)
|
|
|
|
return str2 != 0;
|
|
|
|
|
|
|
|
if (!str2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (; len1 && len2; --len1, --len2, ++str1, ++str2) {
|
|
|
|
if (*str1 < *str2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*str1 > *str2)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len1 == 0)
|
|
|
|
return len2 == 0 ? 0 : -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
// compare two int32_t
|
|
|
|
static int32_t
|
|
|
|
ns_cmp(int32_t n1, int32_t n2)
|
2005-07-23 14:05:25 +00:00
|
|
|
{
|
|
|
|
if (n1 < n2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return n1 != n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two VersionParts
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t
|
2005-07-23 14:05:25 +00:00
|
|
|
CompareVP(VersionPart &v1, VersionPart &v2)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t r = ns_cmp(v1.numA, v2.numA);
|
2005-07-23 14:05:25 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = ns_strnncmp(v1.strB, v1.strBlen, v2.strB, v2.strBlen);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = ns_cmp(v1.numC, v2.numC);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return ns_strcmp(v1.extraD, v2.extraD);
|
|
|
|
}
|
|
|
|
|
2008-12-03 13:41:12 +00:00
|
|
|
/**
|
|
|
|
* Compares two VersionParts
|
|
|
|
*/
|
|
|
|
#ifdef XP_WIN
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t
|
2008-12-03 13:41:12 +00:00
|
|
|
CompareVP(VersionPartW &v1, VersionPartW &v2)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t r = ns_cmp(v1.numA, v2.numA);
|
2008-12-03 13:41:12 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2013-01-15 12:22:03 +00:00
|
|
|
r = wcsncmp(v1.strB, v2.strB, XPCOM_MIN(v1.strBlen,v2.strBlen));
|
2008-12-03 13:41:12 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = ns_cmp(v1.numC, v2.numC);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2009-05-20 21:00:45 +00:00
|
|
|
if (!v1.extraD)
|
|
|
|
return v2.extraD != 0;
|
|
|
|
|
|
|
|
if (!v2.extraD)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-03 13:41:12 +00:00
|
|
|
return wcscmp(v1.extraD, v2.extraD);
|
|
|
|
}
|
2012-04-19 03:22:29 +00:00
|
|
|
#endif
|
2008-12-03 13:41:12 +00:00
|
|
|
|
2012-04-19 03:22:29 +00:00
|
|
|
namespace mozilla {
|
2008-12-03 13:41:12 +00:00
|
|
|
|
2012-04-19 03:22:29 +00:00
|
|
|
#ifdef XP_WIN
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2012-04-19 03:22:29 +00:00
|
|
|
CompareVersions(const PRUnichar *A, const PRUnichar *B)
|
2008-12-03 13:41:12 +00:00
|
|
|
{
|
|
|
|
PRUnichar *A2 = wcsdup(A);
|
|
|
|
if (!A2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
PRUnichar *B2 = wcsdup(B);
|
|
|
|
if (!B2) {
|
|
|
|
free(A2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t result;
|
2008-12-03 13:41:12 +00:00
|
|
|
PRUnichar *a = A2, *b = B2;
|
|
|
|
|
|
|
|
do {
|
|
|
|
VersionPartW va, vb;
|
|
|
|
|
|
|
|
a = ParseVP(a, va);
|
|
|
|
b = ParseVP(b, vb);
|
|
|
|
|
|
|
|
result = CompareVP(va, vb);
|
|
|
|
if (result)
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (a || b);
|
|
|
|
|
|
|
|
free(A2);
|
|
|
|
free(B2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2012-04-19 03:22:29 +00:00
|
|
|
CompareVersions(const char *A, const char *B)
|
2005-07-23 14:05:25 +00:00
|
|
|
{
|
|
|
|
char *A2 = strdup(A);
|
|
|
|
if (!A2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char *B2 = strdup(B);
|
|
|
|
if (!B2) {
|
|
|
|
free(A2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t result;
|
2005-07-23 14:05:25 +00:00
|
|
|
char *a = A2, *b = B2;
|
|
|
|
|
|
|
|
do {
|
|
|
|
VersionPart va, vb;
|
|
|
|
|
|
|
|
a = ParseVP(a, va);
|
|
|
|
b = ParseVP(b, vb);
|
|
|
|
|
|
|
|
result = CompareVP(va, vb);
|
|
|
|
if (result)
|
|
|
|
break;
|
|
|
|
|
2005-08-25 12:14:13 +00:00
|
|
|
} while (a || b);
|
2005-07-23 14:05:25 +00:00
|
|
|
|
|
|
|
free(A2);
|
|
|
|
free(B2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-19 03:22:29 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|