Bug 506394 - ExpandEnvironmentStringsW in CE shunt isn't quite right, r=vlad

This commit is contained in:
Paul O’Shannessy 2009-08-01 16:21:58 -07:00
Родитель 62af18f88c
Коммит 327a19dfc6
1 изменённых файлов: 28 добавлений и 18 удалений

Просмотреть файл

@ -198,43 +198,53 @@ SetEnvironmentVariableW(const unsigned short* name,
unsigned int ExpandEnvironmentStringsW(const unsigned short* lpSrc, unsigned int ExpandEnvironmentStringsW(const unsigned short* lpSrc,
unsigned short* lpDst, unsigned short* lpDst,
unsigned int nSize) unsigned int nSize)
{ {
if ( NULL == lpDst ) if ( NULL == lpDst )
return 0; return 0;
unsigned int size = 0; unsigned int size = 0;
unsigned int index = 0; unsigned int index = 0;
unsigned int origLen = wcslen(lpSrc); unsigned int origLen = wcslen(lpSrc);
const unsigned short *pIn = lpSrc; const unsigned short *pIn = lpSrc;
unsigned short *pOut = lpDst; unsigned short *pOut = lpDst;
while ( index < origLen ) { while ( index < origLen ) {
if (*pIn != L'%') { // Regular char, copy over if (*pIn != L'%') { // Regular char, copy over
if ( size < nSize ) *pOut = *pIn, pOut++; if ( size++ < nSize ) *pOut = *pIn, pOut++;
index++, size++, pIn++; index++, pIn++;
continue; continue;
} }
// Have a starting '%' - look for matching '%' // Have a starting '%' - look for matching '%'
int envlen = 0; int envlen = 0;
const unsigned short *pTmp = ++pIn; // Move past original '%' const unsigned short *pTmp = pIn + 1;
while ( L'%' != *pTmp ) { while ( *pTmp != L'%' && *pTmp != L' ' ) {
envlen++, pTmp++; envlen++, pTmp++;
if ( origLen < index + envlen ) { // Ran past end of original if ( origLen < index + envlen ) { // Ran past end of original
SetLastError(ERROR_INVALID_PARAMETER); // buffer without matching '%' while ( envlen-- ) {
return -1; if ( size++ < nSize ) *pOut = *pIn, pOut++;
index++, pIn++;
}
break;
} }
} }
if ( *pTmp == L' ' ) { // Need to append through space
while ( envlen-- ) {
if ( size++ < nSize ) *pOut = *pIn, pOut++;
index++, pIn++;
}
continue;
}
pIn++; // Move past original %
if ( 0 == envlen ) { // Encountered a "%%" - mapping to "%" if ( 0 == envlen ) { // Encountered a "%%" - mapping to "%"
size++; if ( size++ < nSize ) *pOut = *pIn, pOut++;
if ( size < nSize ) *pOut = *pIn, pOut++; index += 2, pIn++;
pIn++;
index += 2;
} else { } else {
// Encountered a "%something%" - mapping "something" // Encountered a "%something%" - mapping "something"
char key[256]; char key[256];
@ -252,7 +262,7 @@ unsigned int ExpandEnvironmentStringsW(const unsigned short* lpSrc,
pIn = ++pTmp; pIn = ++pTmp;
} }
} }
if ( size < nSize ) lpDst[size] = 0; if ( size < nSize ) lpDst[size] = 0;
return size; return size;
} }