2014-06-30 08:39:45 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsVersionComparator.h"
|
|
|
|
|
2012-09-06 15:47:19 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2012-09-06 15:47:19 -07:00
|
|
|
#if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
|
|
|
|
#include <wchar.h>
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
struct VersionPart
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numA;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
const char* strB; // NOT null-terminated, can be a null pointer
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t strBlen;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numC;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
char* extraD; // null-terminated
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-12-03 05:41:12 -08:00
|
|
|
#ifdef XP_WIN
|
2014-06-26 18:35:39 -07:00
|
|
|
struct VersionPartW
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numA;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* strB; // NOT null-terminated, can be a null pointer
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t strBlen;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numC;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* extraD; // null-terminated
|
2008-12-03 05:41:12 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Parse a version part into a number and "extra text".
|
|
|
|
*
|
|
|
|
* @returns A pointer to the next versionpart, or null if none.
|
|
|
|
*/
|
|
|
|
static char*
|
2014-06-26 18:35:39 -07:00
|
|
|
ParseVP(char* aPart, VersionPart& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
char* dot;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
aResult.numA = 0;
|
|
|
|
aResult.strB = nullptr;
|
|
|
|
aResult.strBlen = 0;
|
|
|
|
aResult.numC = 0;
|
|
|
|
aResult.extraD = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aPart) {
|
|
|
|
return aPart;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
dot = strchr(aPart, '.');
|
|
|
|
if (dot) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*dot = '\0';
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aPart[0] == '*' && aPart[1] == '\0') {
|
|
|
|
aResult.numA = INT32_MAX;
|
|
|
|
aResult.strB = "";
|
|
|
|
} else {
|
|
|
|
aResult.numA = strtol(aPart, const_cast<char**>(&aResult.strB), 10);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
|
|
|
|
if (!*aResult.strB) {
|
|
|
|
aResult.strB = nullptr;
|
|
|
|
aResult.strBlen = 0;
|
|
|
|
} else {
|
|
|
|
if (aResult.strB[0] == '+') {
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char kPre[] = "pre";
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
++aResult.numA;
|
|
|
|
aResult.strB = kPre;
|
|
|
|
aResult.strBlen = sizeof(kPre) - 1;
|
|
|
|
} else {
|
|
|
|
const char* numstart = strpbrk(aResult.strB, "0123456789+-");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!numstart) {
|
2014-06-26 18:35:39 -07:00
|
|
|
aResult.strBlen = strlen(aResult.strB);
|
|
|
|
} else {
|
|
|
|
aResult.strBlen = numstart - aResult.strB;
|
|
|
|
|
|
|
|
aResult.numC = strtol(numstart, &aResult.extraD, 10);
|
|
|
|
if (!*aResult.extraD) {
|
|
|
|
aResult.extraD = nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dot) {
|
|
|
|
++dot;
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!*dot) {
|
2012-07-30 07:20:58 -07:00
|
|
|
dot = nullptr;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return dot;
|
|
|
|
}
|
|
|
|
|
2008-12-03 05:41:12 -08: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
|
2013-12-04 04:19:09 -08:00
|
|
|
static wchar_t*
|
2014-06-26 18:35:39 -07:00
|
|
|
ParseVP(wchar_t* aPart, VersionPartW& aResult)
|
2008-12-03 05:41:12 -08:00
|
|
|
{
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* dot;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
aResult.numA = 0;
|
|
|
|
aResult.strB = nullptr;
|
|
|
|
aResult.strBlen = 0;
|
|
|
|
aResult.numC = 0;
|
|
|
|
aResult.extraD = nullptr;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aPart) {
|
|
|
|
return aPart;
|
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
dot = wcschr(aPart, '.');
|
|
|
|
if (dot) {
|
2008-12-03 05:41:12 -08:00
|
|
|
*dot = '\0';
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aPart[0] == '*' && aPart[1] == '\0') {
|
|
|
|
aResult.numA = INT32_MAX;
|
|
|
|
aResult.strB = L"";
|
|
|
|
} else {
|
|
|
|
aResult.numA = wcstol(aPart, const_cast<wchar_t**>(&aResult.strB), 10);
|
2008-12-03 05:41:12 -08:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
|
|
|
|
if (!*aResult.strB) {
|
|
|
|
aResult.strB = nullptr;
|
|
|
|
aResult.strBlen = 0;
|
|
|
|
} else {
|
|
|
|
if (aResult.strB[0] == '+') {
|
2013-12-04 04:19:09 -08:00
|
|
|
static wchar_t kPre[] = L"pre";
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
++aResult.numA;
|
|
|
|
aResult.strB = kPre;
|
|
|
|
aResult.strBlen = sizeof(kPre) - 1;
|
|
|
|
} else {
|
|
|
|
const wchar_t* numstart = wcspbrk(aResult.strB, L"0123456789+-");
|
2008-12-03 05:41:12 -08:00
|
|
|
if (!numstart) {
|
2014-06-26 18:35:39 -07:00
|
|
|
aResult.strBlen = wcslen(aResult.strB);
|
|
|
|
} else {
|
|
|
|
aResult.strBlen = numstart - aResult.strB;
|
|
|
|
|
|
|
|
aResult.numC = wcstol(numstart, &aResult.extraD, 10);
|
|
|
|
if (!*aResult.extraD) {
|
|
|
|
aResult.extraD = nullptr;
|
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dot) {
|
|
|
|
++dot;
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!*dot) {
|
2012-07-30 07:20:58 -07:00
|
|
|
dot = nullptr;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dot;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// compare two null-terminated strings, which may be null pointers
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
ns_strcmp(const char* aStr1, const char* aStr2)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// any string is *before* no string
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aStr1) {
|
|
|
|
return aStr2 != 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aStr2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return strcmp(aStr1, aStr2);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// compare two length-specified string, which may be null pointers
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
ns_strnncmp(const char* aStr1, uint32_t aLen1,
|
|
|
|
const char* aStr2, uint32_t aLen2)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// any string is *before* no string
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aStr1) {
|
|
|
|
return aStr2 != 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aStr2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
for (; aLen1 && aLen2; --aLen1, --aLen2, ++aStr1, ++aStr2) {
|
|
|
|
if (*aStr1 < *aStr2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (*aStr1 > *aStr2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aLen1 == 0) {
|
|
|
|
return aLen2 == 0 ? 0 : -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
// compare two int32_t
|
|
|
|
static int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
ns_cmp(int32_t aNum1, int32_t aNum2)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aNum1 < aNum2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return aNum1 != aNum2;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two VersionParts
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
CompareVP(VersionPart& aVer1, VersionPart& aVer2)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
int32_t r = ns_cmp(aVer1.numA, aVer2.numA);
|
|
|
|
if (r) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
r = ns_strnncmp(aVer1.strB, aVer1.strBlen, aVer2.strB, aVer2.strBlen);
|
|
|
|
if (r) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
r = ns_cmp(aVer1.numC, aVer2.numC);
|
|
|
|
if (r) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return ns_strcmp(aVer1.extraD, aVer2.extraD);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-12-03 05:41:12 -08:00
|
|
|
/**
|
|
|
|
* Compares two VersionParts
|
|
|
|
*/
|
|
|
|
#ifdef XP_WIN
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
CompareVP(VersionPartW& aVer1, VersionPartW& aVer2)
|
2008-12-03 05:41:12 -08:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
int32_t r = ns_cmp(aVer1.numA, aVer2.numA);
|
|
|
|
if (r) {
|
2008-12-03 05:41:12 -08:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
r = wcsncmp(aVer1.strB, aVer2.strB, XPCOM_MIN(aVer1.strBlen, aVer2.strBlen));
|
|
|
|
if (r) {
|
2008-12-03 05:41:12 -08:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
r = ns_cmp(aVer1.numC, aVer2.numC);
|
|
|
|
if (r) {
|
2008-12-03 05:41:12 -08:00
|
|
|
return r;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aVer1.extraD) {
|
|
|
|
return aVer2.extraD != 0;
|
|
|
|
}
|
2009-05-20 14:00:45 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aVer2.extraD) {
|
2009-05-20 14:00:45 -07:00
|
|
|
return -1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2009-05-20 14:00:45 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return wcscmp(aVer1.extraD, aVer2.extraD);
|
2008-12-03 05:41:12 -08:00
|
|
|
}
|
2012-04-18 20:22:29 -07:00
|
|
|
#endif
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2012-04-18 20:22:29 -07:00
|
|
|
namespace mozilla {
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2012-04-18 20:22:29 -07:00
|
|
|
#ifdef XP_WIN
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
CompareVersions(const char16_t* aStrA, const char16_t* aStrB)
|
2008-12-03 05:41:12 -08:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* A2 = wcsdup(char16ptr_t(aStrA));
|
|
|
|
if (!A2) {
|
2008-12-03 05:41:12 -08:00
|
|
|
return 1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* B2 = wcsdup(char16ptr_t(aStrB));
|
2008-12-03 05:41:12 -08:00
|
|
|
if (!B2) {
|
|
|
|
free(A2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t result;
|
2014-06-26 18:35:39 -07:00
|
|
|
wchar_t* a = A2;
|
|
|
|
wchar_t* b = B2;
|
2008-12-03 05:41:12 -08:00
|
|
|
|
|
|
|
do {
|
|
|
|
VersionPartW va, vb;
|
|
|
|
|
|
|
|
a = ParseVP(a, va);
|
|
|
|
b = ParseVP(b, vb);
|
|
|
|
|
|
|
|
result = CompareVP(va, vb);
|
2014-06-26 18:35:39 -07:00
|
|
|
if (result) {
|
2008-12-03 05:41:12 -08:00
|
|
|
break;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2008-12-03 05:41:12 -08:00
|
|
|
|
|
|
|
} while (a || b);
|
|
|
|
|
|
|
|
free(A2);
|
|
|
|
free(B2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2014-06-26 18:35:39 -07:00
|
|
|
CompareVersions(const char* aStrA, const char* aStrB)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
char* A2 = strdup(aStrA);
|
|
|
|
if (!A2) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
char* B2 = strdup(aStrB);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!B2) {
|
|
|
|
free(A2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t result;
|
2014-06-26 18:35:39 -07:00
|
|
|
char* a = A2;
|
|
|
|
char* b = B2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
VersionPart va, vb;
|
|
|
|
|
|
|
|
a = ParseVP(a, va);
|
|
|
|
b = ParseVP(b, vb);
|
|
|
|
|
|
|
|
result = CompareVP(va, vb);
|
2014-06-26 18:35:39 -07:00
|
|
|
if (result) {
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
} while (a || b);
|
|
|
|
|
|
|
|
free(A2);
|
|
|
|
free(B2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-18 20:22:29 -07:00
|
|
|
} // namespace mozilla
|
|
|
|
|