DWORD LwStrIsAllSpace( PCSTR pszString, PBOOLEAN pbIsAllSpace ) { DWORD dwError = 0; PCSTR pszTmp = NULL; BOOLEAN bIsAllSpace = TRUE; LW_BAIL_ON_INVALID_POINTER(pszString); for (pszTmp = pszString; *pszTmp; pszTmp++) { if (!LwIsSpace(*pszTmp)) { bIsAllSpace = FALSE; break; } } *pbIsAllSpace = bIsAllSpace; cleanup: return dwError; error: *pbIsAllSpace = FALSE; goto cleanup; }
DWORD LwHexStrToByteArray( IN PCSTR pszHexString, IN OPTIONAL DWORD* pdwHexStringLength, OUT UCHAR** ppucByteArray, OUT DWORD* pdwByteArrayLength ) { DWORD dwError = 0; DWORD i = 0; DWORD dwHexChars = 0; UCHAR* pucByteArray = NULL; DWORD dwByteArrayLength = 0; LW_BAIL_ON_INVALID_POINTER(pszHexString); if (pdwHexStringLength) { dwHexChars = *pdwHexStringLength; } else { dwHexChars = strlen(pszHexString); } dwByteArrayLength = dwHexChars / 2; if ((dwHexChars & 0x00000001) != 0) { dwError = LW_ERROR_INVALID_PARAMETER; BAIL_ON_LW_ERROR(dwError); } dwError = LwAllocateMemory( sizeof(UCHAR)*(dwByteArrayLength), OUT_PPVOID(&pucByteArray) ); BAIL_ON_LW_ERROR(dwError); for (i = 0; i < dwByteArrayLength; i++) { CHAR hexHi = pszHexString[2*i]; CHAR hexLow = pszHexString[2*i + 1]; UCHAR ucHi = 0; UCHAR ucLow = 0; dwError = LwHexCharToByte(hexHi, &ucHi); BAIL_ON_LW_ERROR(dwError); dwError = LwHexCharToByte(hexLow, &ucLow); BAIL_ON_LW_ERROR(dwError); pucByteArray[i] = (ucHi * 16) + ucLow; } *ppucByteArray = pucByteArray; *pdwByteArrayLength = dwByteArrayLength; cleanup: return dwError; error: LW_SAFE_FREE_MEMORY(pucByteArray); *ppucByteArray = NULL; *pdwByteArrayLength = 0; goto cleanup; }
/* Escapes a string according to the directions given in RFC 2254. */ DWORD LwLdapEscapeString( PSTR *ppszResult, PCSTR pszInput ) { DWORD dwError = LW_ERROR_SUCCESS; size_t iOutputPos = 0; size_t iInputPos = 0; PSTR pszResult = NULL; //Empty strings are allowed, but not null strings LW_BAIL_ON_INVALID_POINTER(pszInput); // Calculate the length of the escaped output string for(; pszInput[iInputPos]; iInputPos++) { switch(pszInput[iInputPos]) { case '*': case '(': case ')': case '\\': iOutputPos += 3; break; default: iOutputPos ++; break; } } dwError = LwAllocateMemory(iOutputPos + 1, OUT_PPVOID(&pszResult)); iOutputPos = 0; for(iInputPos = 0; pszInput[iInputPos]; iInputPos++) { switch(pszInput[iInputPos]) { case '*': memcpy(pszResult + iOutputPos, "\\2a", 3); iOutputPos += 3; break; case '(': memcpy(pszResult + iOutputPos, "\\28", 3); iOutputPos += 3; break; case ')': memcpy(pszResult + iOutputPos, "\\29", 3); iOutputPos += 3; break; case '\\': memcpy(pszResult + iOutputPos, "\\5c", 3); iOutputPos += 3; break; default: pszResult[iOutputPos++] = pszInput[iInputPos]; break; } } pszResult[iOutputPos++] = '\0'; *ppszResult = pszResult; pszResult = NULL; error: LW_SAFE_FREE_STRING(pszResult); return dwError; }
DWORD LwLdapGetStringsWithExtDnResult( IN HANDLE hDirectory, IN LDAPMessage* pMessage, IN PCSTR pszFieldName, IN BOOLEAN bDoSidParsing, OUT PSTR** pppszValues, OUT PDWORD pdwNumValues ) { DWORD dwError = LW_ERROR_SUCCESS; PLW_LDAP_DIRECTORY_CONTEXT pDirectory = NULL; PSTR *ppszLDAPValues = NULL; PSTR *ppszValues = NULL; INT iNum = 0; DWORD dwNumValues = 0; int iValue = 0; LW_BAIL_ON_INVALID_HANDLE(hDirectory); pDirectory = (PLW_LDAP_DIRECTORY_CONTEXT)hDirectory; LW_BAIL_ON_INVALID_POINTER(pMessage); ppszLDAPValues = (PSTR*)ldap_get_values(pDirectory->ld, pMessage, pszFieldName); if (ppszLDAPValues) { iNum = ldap_count_values(ppszLDAPValues); if (iNum < 0) { dwError = LW_ERROR_LDAP_ERROR; BAIL_ON_LW_ERROR(dwError); } else if (iNum > 0) { dwError = LwAllocateMemory((iNum+1)*sizeof(PSTR), OUT_PPVOID(&ppszValues)); BAIL_ON_LW_ERROR(dwError); dwNumValues = 0; for (iValue = 0; iValue < iNum; iValue++) { if (bDoSidParsing) { dwError = LwLdapParseExtendedDNResult(ppszLDAPValues[iValue], &ppszValues[dwNumValues]); BAIL_ON_LW_ERROR(dwError); } else { dwError = LwAllocateString(ppszLDAPValues[iValue], &ppszValues[dwNumValues]); BAIL_ON_LW_ERROR(dwError); } if (ppszValues[dwNumValues]) { dwNumValues++; } } } } *pppszValues = ppszValues; *pdwNumValues = dwNumValues; cleanup: if (ppszLDAPValues) { ldap_value_free(ppszLDAPValues); } return dwError; error: LwFreeNullTerminatedStringArray(ppszValues); *pppszValues = NULL; *pdwNumValues = 0; goto cleanup; }