RTPathParse.cpp.h revision 889f8d29a8325ed741d1466074c5371910a1fd63
af062818b47340eef15700d2f0211576ba3506eevboxsync/* $Id$ */
af062818b47340eef15700d2f0211576ba3506eevboxsync/** @file
af062818b47340eef15700d2f0211576ba3506eevboxsync * IPRT - RTPathParse - Code Template.
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file included multiple times with different path style macros.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright (C) 2006-2013 Oracle Corporation
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync * available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync * you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync *
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * The contents of this file may alternatively be used under the terms
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * of the Common Development and Distribution License Version 1.0
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * VirtualBox OSE distribution, in which case the provisions of the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * CDDL are applicable instead of those of the GPL.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync *
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * You may elect to license modified versions of this file under the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * terms and conditions of either the GPL or the CDDL or both.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/**
af062818b47340eef15700d2f0211576ba3506eevboxsync * @copydoc RTPathParse
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic int RTPATH_STYLE_FN(rtPathParse)(const char *pszPath, PRTPATHPARSED pParsed, size_t cbParsed, uint32_t fFlags)
af062818b47340eef15700d2f0211576ba3506eevboxsync{
af062818b47340eef15700d2f0211576ba3506eevboxsync /*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Parse the root specification if present and initialize the parser state
af062818b47340eef15700d2f0211576ba3506eevboxsync * (keep it on the stack for speed).
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t const cMaxComps = cbParsed < RT_UOFFSETOF(RTPATHPARSED, aComps[0xfff0])
af062818b47340eef15700d2f0211576ba3506eevboxsync ? (cbParsed - RT_UOFFSETOF(RTPATHPARSED, aComps)) / sizeof(pParsed->aComps[0])
af062818b47340eef15700d2f0211576ba3506eevboxsync : 0xfff0;
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t idxComp = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t cchPath;
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t offCur;
af062818b47340eef15700d2f0211576ba3506eevboxsync uint16_t fProps;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RTPATH_IS_SLASH(pszPath[0]))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync if (fFlags & RTPATHPARSE_FLAGS_NO_START)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 1;
af062818b47340eef15700d2f0211576ba3506eevboxsync while (RTPATH_IS_SLASH(pszPath[offCur]))
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur++;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (!pszPath[offCur])
af062818b47340eef15700d2f0211576ba3506eevboxsync return VERR_PATH_ZERO_LENGTH;
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_RELATIVE | RTPATH_PROP_EXTRA_SLASHES;
af062818b47340eef15700d2f0211576ba3506eevboxsync cchPath = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync#ifdef RTPATH_STYLE_DOS
af062818b47340eef15700d2f0211576ba3506eevboxsync else if ( RTPATH_IS_SLASH(pszPath[1])
af062818b47340eef15700d2f0211576ba3506eevboxsync && !RTPATH_IS_SLASH(pszPath[2])
af062818b47340eef15700d2f0211576ba3506eevboxsync && pszPath[2])
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync /* UNC - skip to the end of the potential namespace or computer name. */
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 2;
af062818b47340eef15700d2f0211576ba3506eevboxsync while (!RTPATH_IS_SLASH(pszPath[offCur]) && pszPath[offCur])
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur++;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* If there is another slash, we considered it a valid UNC path, if
af062818b47340eef15700d2f0211576ba3506eevboxsync not it's just a root path with an extra slash thrown in. */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RTPATH_IS_SLASH(pszPath[offCur]))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_UNC | RTPATH_PROP_ABSOLUTE;
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur++;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync else
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 1;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync#endif
af062818b47340eef15700d2f0211576ba3506eevboxsync else
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync#ifdef RTPATH_STYLE_DOS
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
af062818b47340eef15700d2f0211576ba3506eevboxsync#else
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
af062818b47340eef15700d2f0211576ba3506eevboxsync#endif
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 1;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync#ifdef RTPATH_STYLE_DOS
af062818b47340eef15700d2f0211576ba3506eevboxsync else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync if (!RTPATH_IS_SLASH(pszPath[2]))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE;
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 2;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync else
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 3;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync#endif
af062818b47340eef15700d2f0211576ba3506eevboxsync else
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps = RTPATH_PROP_RELATIVE;
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync cchPath = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Add it to the component array . */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (offCur && !(fFlags & RTPATHPARSE_FLAGS_NO_START))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync cchPath = offCur;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (idxComp < cMaxComps)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->aComps[idxComp].off = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->aComps[idxComp].cch = offCur;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync idxComp++;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Skip unnecessary slashes following the root-spec. */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RTPATH_IS_SLASH(pszPath[offCur]))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_EXTRA_SLASHES;
af062818b47340eef15700d2f0211576ba3506eevboxsync do
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur++;
af062818b47340eef15700d2f0211576ba3506eevboxsync while (RTPATH_IS_SLASH(pszPath[offCur]));
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Parse the rest.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (pszPath[offCur])
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync for (;;)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync Assert(!RTPATH_IS_SLASH(pszPath[offCur]));
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Find the end of the component. */
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t offStart = offCur;
af062818b47340eef15700d2f0211576ba3506eevboxsync char ch;
af062818b47340eef15700d2f0211576ba3506eevboxsync while ((ch = pszPath[offCur]) != '\0' && !RTPATH_IS_SLASH(ch))
af062818b47340eef15700d2f0211576ba3506eevboxsync offCur++;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (offCur >= _64K)
af062818b47340eef15700d2f0211576ba3506eevboxsync return VERR_FILENAME_TOO_LONG;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Add it. */
af062818b47340eef15700d2f0211576ba3506eevboxsync uint32_t cchComp = offCur - offStart;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (idxComp < cMaxComps)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->aComps[idxComp].off = offStart;
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->aComps[idxComp].cch = cchComp;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync idxComp++;
af062818b47340eef15700d2f0211576ba3506eevboxsync cchPath += cchComp;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Look for '.' and '..' references. */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (cchComp == 1 && pszPath[offCur - 1] == '.')
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_DOT_REFS;
af062818b47340eef15700d2f0211576ba3506eevboxsync else if (cchComp == 2 && pszPath[offCur - 1] == '.' && pszPath[offCur - 2] == '.')
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps &= ~RTPATH_PROP_ABSOLUTE;
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_RELATIVE;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Skip unnecessary slashes. Leave ch unchanged! */
af062818b47340eef15700d2f0211576ba3506eevboxsync char ch2 = ch;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (ch2)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync ch2 = pszPath[++offCur];
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RTPATH_IS_SLASH(ch2))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_EXTRA_SLASHES;
af062818b47340eef15700d2f0211576ba3506eevboxsync do
af062818b47340eef15700d2f0211576ba3506eevboxsync ch2 = pszPath[++offCur];
af062818b47340eef15700d2f0211576ba3506eevboxsync while (RTPATH_IS_SLASH(ch2));
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* The end? */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (ch2 == '\0')
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->offSuffix = offCur;
af062818b47340eef15700d2f0211576ba3506eevboxsync pParsed->cchSuffix = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (ch)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync if (!(fFlags & RTPATHPARSE_FLAGS_NO_END))
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_DIR_SLASH; /* (not counted) */
af062818b47340eef15700d2f0211576ba3506eevboxsync else
af062818b47340eef15700d2f0211576ba3506eevboxsync fProps |= RTPATH_PROP_EXTRA_SLASHES;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync else if (!(fFlags & RTPATHPARSE_FLAGS_NO_END))
{
fProps |= RTPATH_PROP_FILENAME;
/* look for an ? */
uint16_t cDots = 0;
uint32_t offSuffix = offStart + cchComp;
while (offSuffix-- > offStart)
if (pszPath[offSuffix] == '.')
{
uint32_t cchSuffix = offStart + cchComp - offSuffix;
if (cchSuffix > 1 && offStart != offSuffix)
{
pParsed->cchSuffix = cchSuffix;
pParsed->offSuffix = offSuffix;
fProps |= RTPATH_PROP_SUFFIX;
}
break;
}
}
break;
}
/* No, not the end. Account for an separator before we restart the loop. */
cchPath += sizeof(RTPATH_SLASH_STR) - 1;
}
}
else
{
pParsed->offSuffix = offCur;
pParsed->cchSuffix = 0;
}
if (offCur >= _64K)
return VERR_FILENAME_TOO_LONG;
/*
* Store the remainder of the state and we're done.
*/
pParsed->fProps = fProps;
pParsed->cchPath = cchPath;
pParsed->cComps = idxComp;
return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
}