strversion.cpp revision 4b783d68e6d569ab798901917ace422a4810edf0
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/* $Id$ */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @file
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * IPRT - Version String Parsing.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * The contents of this file may alternatively be used under the terms
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * of the Common Development and Distribution License Version 1.0
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * VirtualBox OSE distribution, in which case the provisions of the
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * CDDL are applicable instead of those of the GPL.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * You may elect to license modified versions of this file under the
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * terms and conditions of either the GPL or the CDDL or both.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * additional information or have any questions.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*******************************************************************************
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync* Header Files *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync*******************************************************************************/
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/string.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "internal/iprt.h"
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/assert.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/err.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/mem.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*******************************************************************************
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync* Defined Constants *
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync*******************************************************************************/
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*******************************************************************************
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync* Internal Functions *
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync*******************************************************************************/
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncstatic uint16_t RTStrVersionGetBlockCount(const char *pszVer)
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync{
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync uint16_t l = 0;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync const char *pszCur = pszVer;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync while (pszCur = RTStrStr(pszCur, "."))
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync {
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync if (pszCur == NULL)
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync break;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync l++;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync pszCur++;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Adjust block count to also count in the very first block */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (*pszVer != '\0')
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync l++;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return l;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncstatic int RTStrVersionGetUInt32(const char *pszVer, uint16_t u16Block, uint32_t *pu32)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* First make a copy of the version string so that we can modify it */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync char *pszString;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int rc = RTStrDupEx(&pszString, pszVer);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (RT_FAILURE(rc))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return rc;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Go to the beginning of the block we want to parse */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync char *pszCur = pszString;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync for (uint16_t i = 0; i < u16Block; i++)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pszCur = RTStrStr(pszCur, ".");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (pszCur == NULL)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (*pszCur != '\0')
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pszCur++;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (pszCur != NULL && *pszCur != '\0')
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Skip trailing non-digits at the start of the block */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync while (pszCur && *pszCur != '\0')
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (ISDIGIT(*pszCur))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pszCur++;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Mark ending of the block */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync char *pszEnd = RTStrStr(pszCur, ".");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (NULL != pszEnd)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *pszEnd = '\0';
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Convert to number */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = RTStrToUInt32Ex(pszCur, NULL /* ppszNext */, 10 /* Base */, pu32);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* Skip trailing warnings */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if ( rc == VWRN_TRAILING_CHARS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync || rc == VWRN_TRAILING_SPACES)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = VINF_SUCCESS;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = VERR_NOT_FOUND;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RTStrFree(pszString);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return rc;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/**
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Compares two version strings and returns the result. The version string has
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * to be made of at least one number section, each section delimited by a ".",
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * e.g. "123.45.67". Trailing zeros at the beginning and non-digits in a section
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * will be skipped, so "12.foo006" becomes "12.6".
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns iprt status code.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Warnings are used to indicate convertion problems.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VWRN_NUMBER_TOO_BIG
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VWRN_TRAILING_CHARS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VWRN_TRAILING_SPACES
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VINF_SUCCESS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VERR_NO_MEMORY
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @retval VERR_NO_DIGITS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @todo Deal with prefixes and suffixes!
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pszVer1 First version string to compare.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pszVer2 First version string to compare.*
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pui8Res Pointer uint8_t value where to store the comparison result:
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * 0 if equal, 1 if pszVer1 is greater, 2 if pszVer2 is greater.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncint RTStrVersionCompare(const char *pszVer1, const char *pszVer2, uint8_t *pui8Res)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertPtr(pszVer1);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertPtr(pszVer2);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync uint16_t len1 = RTStrVersionGetBlockCount(pszVer1);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync uint16_t len2 = RTStrVersionGetBlockCount(pszVer2);
int rc = 0;
if (len1 > 0 && len2 > 0)
{
/* Figure out which version string is longer and set the corresponding
* pointers */
uint16_t range;
uint16_t padding;
const char *pszShorter, *pszLonger;
if (len1 >= len2)
{
range = len1;
padding = len1 - len2;
pszLonger = pszVer1;
pszShorter = pszVer2;
}
else if (len2 > len1)
{
range = len2;
padding = len2 - len1;
pszLonger = pszVer2;
pszShorter = pszVer1;
}
/* Now process each section (delimited by a ".") */
AssertPtr(pszShorter);
AssertPtr(pszLonger);
AssertPtr(pui8Res);
*pui8Res = 0;
uint32_t val1, val2;
for (uint16_t i = 0; i < range
&& *pui8Res == 0
&& RT_SUCCESS(rc)
; i++)
{
rc = RTStrVersionGetUInt32(pszLonger, i, &val1);
if (RT_SUCCESS(rc))
{
if (i >= range - padding)
{
/* If we're in the padding range, there are no numbers left
* to compare with anymore, so just assume "0" then */
val2 = 0;
}
else
{
rc = RTStrVersionGetUInt32(pszShorter, i, &val2);
}
}
if (RT_SUCCESS(rc))
{
if (val1 > val2)
{
*pui8Res = (pszLonger == pszVer1) ? 1 : 2;
break;
}
else if (val2 > val1)
{
*pui8Res = (pszShorter == pszVer1) ? 1 : 2;
break;
}
}
}
}
else
{
rc = VERR_NO_DIGITS;
}
if (RT_FAILURE(rc))
*pui8Res = 0; /* Zero out value */
return rc;
}
RT_EXPORT_SYMBOL(RTStrVersionCompare);