Skip to content

Commit

Permalink
SecurityPlugin: prefer wcs* functions to lstr*W
Browse files Browse the repository at this point in the history
It's more in line with the style of platforms/win32
(though it's still quite noisy)
  • Loading branch information
nicolas-cellier-aka-nice committed Jan 3, 2019
1 parent 2bfdddc commit bf754ec
Showing 1 changed file with 27 additions and 42 deletions.
69 changes: 27 additions & 42 deletions platforms/win32/plugins/SecurityPlugin/sqWin32Security.c
Expand Up @@ -68,27 +68,12 @@ static int testDotDot(WCHAR *pathName, int index) {
return 1;
}

static int lstrncmpW(WCHAR *s1, WCHAR *s2, int len) {
int s1Len = lstrlenW(s1);
int s2Len = lstrlenW(s2);
int max = min(s1Len, min(s2Len, len));
int i;
for (i = 0; i < max; i++) {
if (s1[i] > s2[i]) {
return 1;
} else if (s1[i] < s2[i]) {
return -1;
}
}
return 0;
}

static int isAccessiblePathName(WCHAR *pathName, int writeFlag) {
int pathLen = lstrlenW(pathName);
int pathLen = wcslen(pathName);
if (pathLen > (MAX_PATH - 1)) return 0;

if (pathLen >= untrustedUserDirectoryLen
&& 0 == lstrncmpW(pathName, untrustedUserDirectory, untrustedUserDirectoryLen)) {
&& 0 == wcsncmp(pathName, untrustedUserDirectory, untrustedUserDirectoryLen)) {
if (pathLen > untrustedUserDirectoryLen + 2)
return testDotDot(pathName, untrustedUserDirectoryLen+2);
return 1;
Expand All @@ -97,7 +82,7 @@ static int isAccessiblePathName(WCHAR *pathName, int writeFlag) {
return 0;

if (pathLen >= resourceDirectoryLen
&& 0 == lstrncmpW(pathName, resourceDirectory, resourceDirectoryLen)) {
&& 0 == wcsncmp(pathName, resourceDirectory, resourceDirectoryLen)) {
if (pathLen > resourceDirectoryLen + 2)
return testDotDot(pathName, resourceDirectoryLen+2);
return 1;
Expand Down Expand Up @@ -221,13 +206,13 @@ int expandMyDocuments(WCHAR *pathname, WCHAR *replacement, WCHAR *result)
/* WCHAR search4[MAX_PATH+1];
WCHAR *start;
lstrcpyW(search4, L"%MYDOCUMENTS%");
wcscpy(search4, L"%MYDOCUMENTS%");
if(!(start = wstrstr(pathname, search4))) return 0;
if(!(start = wcsstr(pathname, search4))) return 0;
wstrncpy(result, pathname, start-pathname);
result[start-pathname] = '\0';
sprintf(result+(start-pathname),"%s%s", replacement, start+lstrlenW(search4));
wcsncpy(result, pathname, start-pathname);
result[start-pathname] = L'\0';
swprintf(result+(start-pathname),L"%s%s", replacement, start+wcslen(search4));
*/
/* TODO: Implement this properly. */
return 0;
Expand All @@ -236,12 +221,12 @@ int expandMyDocuments(WCHAR *pathname, WCHAR *replacement, WCHAR *result)
static void expandVariableInDirectory(WCHAR *directory, WCHAR *wDir, WCHAR *wTmp)
{
/* Expand environment variables. */
lstrcpyW(wDir, directory);
wcscpy(wDir, directory);
ExpandEnvironmentStringsW(wDir, wTmp, MAX_PATH - 1);

/* Expand relative paths to absolute paths */
GetFullPathNameW(wTmp, MAX_PATH, wDir, NULL);
lstrcpyW(directory, wDir);
wcscpy(directory, wDir);
}

/* note: following is called from VM directly, not from plugin */
Expand All @@ -256,17 +241,17 @@ sqInt ioInitSecurity(void) {

/* establish the secure user directory */
sqUTF8ToUTF16Copy(secureUserDirectory, sizeof(secureUserDirectory)/sizeof(secureUserDirectory[0]), sqGetCurrentImagePath());
dirLen = lstrlenW(secureUserDirectory);
dirLen = wcslen(secureUserDirectory);
dwSize = MAX_PATH-dirLen;
GetUserNameW(secureUserDirectory+dirLen, &dwSize);

/* establish untrusted user directory */
lstrcpyW(untrustedUserDirectory, L"C:\\My Squeak\\%USERNAME%");
wcscpy(untrustedUserDirectory, L"C:\\My Squeak\\%USERNAME%");

/* establish untrusted user directory */
sqUTF8ToUTF16Copy(resourceDirectory, sizeof(resourceDirectory) / sizeof(resourceDirectory[0]), sqGetCurrentImagePath());
if (resourceDirectory[lstrlenW(resourceDirectory)-1] == '\\') {
resourceDirectory[lstrlenW(resourceDirectory)-1] = 0;
if (resourceDirectory[wcslen(resourceDirectory)-1] == '\\') {
resourceDirectory[wcslen(resourceDirectory)-1] = 0;
}

/* Look up shGetFolderPathW */
Expand All @@ -278,11 +263,11 @@ sqInt ioInitSecurity(void) {
int sz;
/*shGetfolderPath does not return utf8*/
if(shGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, untrustedUserDirectory) == S_OK) {
sz = lstrlenW(untrustedUserDirectory);
sz = wcslen(untrustedUserDirectory);
if(untrustedUserDirectory[sz-1] != '\\')
lstrcatW(untrustedUserDirectory, L"\\");
lstrcpyW(myDocumentsFolder,untrustedUserDirectory);
lstrcatW(untrustedUserDirectory, L"My Squeak");
wcscat(untrustedUserDirectory, L"\\");
wcscpy(myDocumentsFolder,untrustedUserDirectory);
wcscat(untrustedUserDirectory, L"My Squeak");
}
}

Expand Down Expand Up @@ -317,7 +302,7 @@ sqInt ioInitSecurity(void) {
tmp[dwSize/2-1] = '\\';
tmp[dwSize/2] = 0;
}
lstrcpyW(secureUserDirectory, tmp);
wcscpy(secureUserDirectory, tmp);
}

/* Read the user directory from the subkey. */
Expand All @@ -329,7 +314,7 @@ sqInt ioInitSecurity(void) {
tmp[dwSize/2-1] = '\\';
tmp[dwSize/2] = 0;
}
lstrcpyW(untrustedUserDirectory, tmp);
wcscpy(untrustedUserDirectory, tmp);
}

/* Read the resource directory from the subkey. */
Expand All @@ -341,33 +326,33 @@ sqInt ioInitSecurity(void) {
tmp[dwSize/2-1] = '\\';
tmp[dwSize/2] = 0;
}
lstrcpyW(resourceDirectory, tmp);
wcscpy(resourceDirectory, tmp);
}

RegCloseKey(hk);

if(shGetFolderPath) {
dwSize = expandMyDocuments(untrustedUserDirectory, myDocumentsFolder, tmp);
if(dwSize > 0 && dwSize < MAX_PATH)
lstrcpyW(untrustedUserDirectory, tmp);
wcscpy(untrustedUserDirectory, tmp);

dwSize = expandMyDocuments(secureUserDirectory, myDocumentsFolder, tmp);
if(dwSize > 0 && dwSize < MAX_PATH)
lstrcpyW(secureUserDirectory, tmp);
wcscpy(secureUserDirectory, tmp);

dwSize = expandMyDocuments(resourceDirectory, myDocumentsFolder, tmp);
if(dwSize > 0 && dwSize < MAX_PATH)
lstrcpyW(resourceDirectory, tmp);
wcscpy(resourceDirectory, tmp);
}

/* Expand the directories. */
expandVariableInDirectory(untrustedUserDirectory, wDir, wTmp);
expandVariableInDirectory(secureUserDirectory, wDir, wTmp);
expandVariableInDirectory(resourceDirectory, wDir, wTmp);

secureUserDirectoryLen = lstrlenW(secureUserDirectory);
untrustedUserDirectoryLen = lstrlenW(untrustedUserDirectory);
resourceDirectoryLen = lstrlenW(resourceDirectory);
secureUserDirectoryLen = wcslen(secureUserDirectory);
untrustedUserDirectoryLen = wcslen(untrustedUserDirectory);
resourceDirectoryLen = wcslen(resourceDirectory);

/* Keep a UTF-8 copy*/
sqUTF16ToUTF8Copy(untrustedUserDirectoryUTF8, sizeof(untrustedUserDirectoryUTF8), untrustedUserDirectory);
Expand Down

0 comments on commit bf754ec

Please sign in to comment.