From: Marc-André Moreau Date: Fri, 10 Oct 2014 23:16:51 +0000 (-0400) Subject: libwinpr-environment: fix extended functions X-Git-Tag: 2.0.0-beta1+android10~800^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8ef4c14a6d41dfd6b63818945115a2fad0bc3e59;p=platform%2Fupstream%2Ffreerdp.git libwinpr-environment: fix extended functions --- diff --git a/winpr/include/winpr/environment.h b/winpr/include/winpr/environment.h index 96f6b49..71bdc05 100644 --- a/winpr/include/winpr/environment.h +++ b/winpr/include/winpr/environment.h @@ -73,11 +73,6 @@ WINPR_API DWORD ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSi WINPR_API BOOL FreeEnvironmentStringsA(LPCH lpszEnvironmentBlock); WINPR_API BOOL FreeEnvironmentStringsW(LPWCH lpszEnvironmentBlock); -WINPR_API LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge); - -WINPR_API DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize); -WINPR_API BOOL SetEnvironmentVariableEBA(LPSTR * envBlock,LPCSTR lpName, LPCSTR lpValue); - #ifdef __cplusplus } #endif @@ -110,5 +105,18 @@ WINPR_API BOOL SetEnvironmentVariableEBA(LPSTR * envBlock,LPCSTR lpName, LPCSTR #endif +#ifdef __cplusplus +extern "C" { +#endif + +WINPR_API LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge); + +WINPR_API DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize); +WINPR_API BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue); + +#ifdef __cplusplus +} +#endif + #endif /* WINPR_ENVIRONMENT_H */ diff --git a/winpr/libwinpr/environment/environment.c b/winpr/libwinpr/environment/environment.c index 2dc6087..f473bcd 100644 --- a/winpr/libwinpr/environment/environment.c +++ b/winpr/libwinpr/environment/environment.c @@ -167,63 +167,6 @@ DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize) return length; } -DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize) -{ - int vLength = 0; - char* env = NULL; - const char * penvb = envBlock; - char *foundEquals; - int nLength, fLength, lpNameLength; - - if (!lpName || NULL == envBlock) - return 0; - - lpNameLength = strlen(lpName); - if (0 == lpNameLength) - return 0; - - while (*penvb && *(penvb+1)) - { - fLength = strlen(penvb); - foundEquals = strstr(penvb,"="); - if (foundEquals == NULL) - { - /* if no = sign is found the envBlock is broken */ - return 0; - } - nLength = foundEquals - penvb; - if (nLength != lpNameLength) - { - penvb += (fLength +1); - continue; - } -#ifdef _WIN32 - if (strnicmp(penvb,lpName,nLength) == 0) -#else - if (strncmp(penvb,lpName,nLength) == 0) -#endif - { - env = foundEquals + 1; - break; - } - penvb += (fLength +1); - } - - if (!env) - return 0; - - vLength = strlen(env); - - if ((vLength + 1 > nSize) || (!lpBuffer)) - return vLength + 1; - - CopyMemory(lpBuffer, env, vLength + 1); - - return vLength; -} - - - DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize) { return 0; @@ -248,38 +191,6 @@ BOOL SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue) return TRUE; } -BOOL SetEnvironmentVariableEBA(LPSTR * envBlock,LPCSTR lpName, LPCSTR lpValue) -{ - int length; - char* envstr; - char* newEB; - - - if (!lpName) - return FALSE; - - if (lpValue) - { - length = strlen(lpName) + strlen(lpValue) + 2; /* +2 because of = and \0 */ - envstr = (char*) malloc(length + 1); /* +1 because of closing \0 */ - sprintf_s(envstr, length, "%s=%s", lpName, lpValue); - } - else - { - length = strlen(lpName) + 2; /* +2 because of = and \0 */ - envstr = (char*) malloc(length + 1); /* +1 because of closing \0 */ - sprintf_s(envstr, length, "%s=", lpName); - } - envstr[length] = '\0'; - newEB = MergeEnvironmentStrings((LPCSTR)*envBlock,envstr); - free(envstr); - if (*envBlock != NULL) - free(*envBlock); - *envBlock = newEB; - return TRUE; -} - - BOOL SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue) { return TRUE; @@ -342,40 +253,88 @@ LPCH GetEnvironmentStrings(VOID) return lpszEnvironmentBlock; } -LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) +LPWCH GetEnvironmentStringsW(VOID) { + return NULL; +} + +BOOL SetEnvironmentStringsA(LPCH NewEnvironment) +{ + return TRUE; +} + +BOOL SetEnvironmentStringsW(LPWCH NewEnvironment) +{ + return TRUE; +} - const char * cp; +DWORD ExpandEnvironmentStringsA(LPCSTR lpSrc, LPSTR lpDst, DWORD nSize) +{ + return 0; +} + +DWORD ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize) +{ + return 0; +} + +BOOL FreeEnvironmentStringsA(LPCH lpszEnvironmentBlock) +{ + if (lpszEnvironmentBlock) + free(lpszEnvironmentBlock); + + return TRUE; +} + +BOOL FreeEnvironmentStringsW(LPWCH lpszEnvironmentBlock) +{ + return TRUE; +} + +#endif + +LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) +{ + const char* cp; char* p; int offset; int length; const char* envp; DWORD cchEnvironmentBlock; LPCH lpszEnvironmentBlock; - const char **mergeStrings; - int mergeStringLenth; + const char** mergeStrings; + int mergeStringLength; int mergeArraySize = 128; int run; int mergeLength; int foundMerge; - char * foundEquals; - // first build an char ** of the merge env strings + char* foundEquals; + + mergeStrings = (LPCSTR*) calloc(mergeArraySize, sizeof(char*)); - mergeStrings = (LPCSTR*) malloc(mergeArraySize * sizeof(char *)); - ZeroMemory(mergeStrings,mergeArraySize * sizeof(char *)); - mergeStringLenth = 0; + if (!mergeStrings) + return NULL; + + mergeStringLength = 0; cp = merge; - while( *cp && *(cp+1)) { + + while (*cp && *(cp + 1)) + { length = strlen(cp); - if (mergeStringLenth == mergeArraySize ) { + + if (mergeStringLength == mergeArraySize) + { mergeArraySize += 128; - mergeStrings = (LPCSTR*) realloc(mergeStrings, mergeArraySize * sizeof(char *)); + mergeStrings = (LPCSTR*) realloc(mergeStrings, mergeArraySize * sizeof(char*)); + if (!mergeStrings) + return NULL; } - mergeStrings[mergeStringLenth] = cp; + + mergeStrings[mergeStringLength] = cp; cp += length + 1; - mergeStringLenth++; + mergeStringLength++; } offset = 0; @@ -383,6 +342,9 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) cchEnvironmentBlock = 128; lpszEnvironmentBlock = (LPCH) malloc(cchEnvironmentBlock * sizeof(CHAR)); + if (!lpszEnvironmentBlock) + return NULL; + envp = original; while ((original != NULL) && (*envp && *(envp+1))) @@ -393,37 +355,46 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) { cchEnvironmentBlock *= 2; lpszEnvironmentBlock = (LPCH) realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR)); + + if (!lpszEnvironmentBlock) + return NULL; } p = &(lpszEnvironmentBlock[offset]); // check if this value is in the mergeStrings foundMerge = 0; - for (run = 0; run < mergeStringLenth; run ++) { - if (mergeStrings[run] == NULL) { + + for (run = 0; run < mergeStringLength; run ++) + { + if (!mergeStrings[run]) continue; - } - mergeLength =strlen(mergeStrings[run]); - foundEquals = strstr(mergeStrings[run],"="); - if (foundEquals == NULL) { + + mergeLength = strlen(mergeStrings[run]); + foundEquals = strstr(mergeStrings[run], "="); + + if (!foundEquals) continue; - } -#ifdef _WIN32 - if (strnicmp(envp,mergeStrings[run],foundEquals - mergeStrings[run] + 1) == 0) { -#else - if (strncmp(envp,mergeStrings[run],foundEquals - mergeStrings[run] + 1) == 0) { -#endif + + if (strncmp(envp, mergeStrings[run], foundEquals - mergeStrings[run] + 1) == 0) + { // found variable in merge list ... use this .... - if (*(foundEquals + 1) == '\0') { + if (*(foundEquals + 1) == '\0') + { // check if the argument is set ... if not remove variable ... foundMerge = 1; - } else { - + } + else + { while ((offset + mergeLength + 8) > cchEnvironmentBlock) { cchEnvironmentBlock *= 2; lpszEnvironmentBlock = (LPCH) realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR)); + + if (!lpszEnvironmentBlock) + return NULL; } + foundMerge = 1; CopyMemory(p, mergeStrings[run], mergeLength); mergeStrings[run] = NULL; @@ -433,27 +404,31 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) } } - - if (foundMerge == 0) { + if (foundMerge == 0) + { CopyMemory(p, envp, length * sizeof(CHAR)); p[length] = '\0'; offset += (length + 1); } + envp += (length +1); } // now merge the not already merged env - for (run = 0; run < mergeStringLenth; run ++) { - if (mergeStrings[run] == NULL) { + for (run = 0; run < mergeStringLength; run ++) + { + if (!mergeStrings[run]) continue; - } - mergeLength =strlen(mergeStrings[run]); + mergeLength = strlen(mergeStrings[run]); while ((offset + mergeLength + 8) > cchEnvironmentBlock) { cchEnvironmentBlock *= 2; lpszEnvironmentBlock = (LPCH) realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR)); + + if (!lpszEnvironmentBlock) + return NULL; } p = &(lpszEnvironmentBlock[offset]); @@ -464,7 +439,6 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) offset += (mergeLength + 1); } - lpszEnvironmentBlock[offset] = '\0'; free(mergeStrings); @@ -472,44 +446,103 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge) return lpszEnvironmentBlock; } - -LPWCH GetEnvironmentStringsW(VOID) +DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize) { - return NULL; -} + int vLength = 0; + char* env = NULL; + char* foundEquals; + const char* penvb = envBlock; + int nLength, fLength, lpNameLength; -BOOL SetEnvironmentStringsA(LPCH NewEnvironment) -{ - return TRUE; -} + if (!lpName || NULL == envBlock) + return 0; -BOOL SetEnvironmentStringsW(LPWCH NewEnvironment) -{ - return TRUE; -} + lpNameLength = strlen(lpName); -DWORD ExpandEnvironmentStringsA(LPCSTR lpSrc, LPSTR lpDst, DWORD nSize) -{ - return 0; -} + if (lpNameLength < 1) + return 0; -DWORD ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize) -{ - return 0; -} + while (*penvb && *(penvb + 1)) + { + fLength = strlen(penvb); + foundEquals = strstr(penvb,"="); -BOOL FreeEnvironmentStringsA(LPCH lpszEnvironmentBlock) -{ - if (lpszEnvironmentBlock) - free(lpszEnvironmentBlock); + if (!foundEquals) + { + /* if no = sign is found the envBlock is broken */ + return 0; + } - return TRUE; + nLength = foundEquals - penvb; + + if (nLength != lpNameLength) + { + penvb += (fLength +1); + continue; + } + + if (strncmp(penvb, lpName, nLength) == 0) + { + env = foundEquals + 1; + break; + } + + penvb += (fLength +1); + } + + if (!env) + return 0; + + vLength = strlen(env); + + if ((vLength + 1 > nSize) || (!lpBuffer)) + return vLength + 1; + + CopyMemory(lpBuffer, env, vLength + 1); + + return vLength; } -BOOL FreeEnvironmentStringsW(LPWCH lpszEnvironmentBlock) +BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue) { - return TRUE; -} + int length; + char* envstr; + char* newEB; -#endif + if (!lpName) + return FALSE; + + if (lpValue) + { + length = strlen(lpName) + strlen(lpValue) + 2; /* +2 because of = and \0 */ + envstr = (char*) malloc(length + 1); /* +1 because of closing \0 */ + + if (!envstr) + return FALSE; + sprintf_s(envstr, length, "%s=%s", lpName, lpValue); + } + else + { + length = strlen(lpName) + 2; /* +2 because of = and \0 */ + envstr = (char*) malloc(length + 1); /* +1 because of closing \0 */ + + if (!envstr) + return FALSE; + + sprintf_s(envstr, length, "%s=", lpName); + } + + envstr[length] = '\0'; + + newEB = MergeEnvironmentStrings((LPCSTR) *envBlock, envstr); + + free(envstr); + + if (*envBlock) + free(*envBlock); + + *envBlock = newEB; + + return TRUE; +}