static INT WINAPI ConvertAddrinfoFromUnicodeToAnsi(IN PADDRINFOW Addrinfo) { LPSTR AnsiName; LPWSTR *UnicodeName; /* Make sure we have a valid pointer */ if (Addrinfo) { do { /* Get the name */ UnicodeName = &Addrinfo->ai_canonname; /* Check if it exists */ if (*UnicodeName) { /* Convert it */ AnsiName = AnsiDupFromUnicode(*UnicodeName); if (AnsiName) { /* Free the old one */ HeapFree(WsSockHeap, 0, *UnicodeName); /* Set the new one */ *UnicodeName = (LPWSTR)AnsiName; } else { return GetLastError(); } } } while ((Addrinfo = Addrinfo->ai_next)); } /* All done */ return ERROR_SUCCESS; }
INT WSAAPI MapUnicodeQuerySetToAnsi(OUT LPWSAQUERYSETW UnicodeSet, IN OUT PSIZE_T SetSize, IN LPWSAQUERYSETA AnsiSet) { INT ErrorCode = ERROR_SUCCESS; SIZE_T UnicodeSize, AnsiSize; LPWSAQUERYSETW UnicodeCopy = NULL; LPWSAQUERYSETA AnsiCopy; LPSTR ServiceCopy = NULL, CommentCopy = NULL; LPSTR ContextCopy = NULL, QueryCopy = NULL; /* Calculate the size of the Ansi version and allocate space for a copy */ UnicodeSize = WSAComputeQuerySetSizeW(UnicodeSet); UnicodeCopy = HeapAlloc(WsSockHeap, 0, UnicodeSize); if (!UnicodeCopy) { /* Fail, couldn't allocate memory */ ErrorCode = WSA_NOT_ENOUGH_MEMORY; goto error; } /* Build the relative buffer version */ ErrorCode = WSABuildQuerySetBufferW(UnicodeSet, UnicodeSize, UnicodeCopy); if (ErrorCode != ERROR_SUCCESS) goto error; /* Re-use the Unicode version since the fields match */ AnsiCopy = (LPWSAQUERYSETA)UnicodeCopy; /* Check if we have a service instance name */ if (UnicodeCopy->lpszServiceInstanceName) { /* Duplicate it into unicode form */ ServiceCopy = AnsiDupFromUnicode(UnicodeCopy->lpszServiceInstanceName); if (!ServiceCopy) { /* Fail */ ErrorCode = WSA_NOT_ENOUGH_MEMORY; goto error; } /* Set the new string pointer */ AnsiCopy->lpszServiceInstanceName = ServiceCopy; } /* Check if we have a service instance name */ if (UnicodeCopy->lpszContext) { /* Duplicate it into unicode form */ ContextCopy = AnsiDupFromUnicode(UnicodeCopy->lpszContext); if (!ContextCopy) { /* Fail */ ErrorCode = WSA_NOT_ENOUGH_MEMORY; goto error; } /* Set the new string pointer */ AnsiCopy->lpszContext = ContextCopy; } /* Check if we have a service instance name */ if (UnicodeCopy->lpszComment) { /* Duplicate it into unicode form */ CommentCopy = AnsiDupFromUnicode(UnicodeCopy->lpszComment); if (!CommentCopy) { /* Fail */ ErrorCode = WSA_NOT_ENOUGH_MEMORY; goto error; } /* Set the new string pointer */ AnsiCopy->lpszComment = CommentCopy; } /* Check if we have a query name */ if (UnicodeCopy->lpszQueryString) { /* Duplicate it into unicode form */ QueryCopy = AnsiDupFromUnicode(UnicodeCopy->lpszQueryString); if (!QueryCopy) { /* Fail */ ErrorCode = WSA_NOT_ENOUGH_MEMORY; goto error; } /* Set the new string pointer */ AnsiCopy->lpszQueryString = QueryCopy; } /* Now that we have the absolute unicode buffer, calculate its size */ AnsiSize = WSAComputeQuerySetSizeA(AnsiCopy); if (AnsiSize > *SetSize) { /* The buffer wasn't large enough; return how much we need */ *SetSize = AnsiSize; ErrorCode = WSAEFAULT; goto error; } /* Build the relative unicode buffer */ ErrorCode = WSABuildQuerySetBufferA(AnsiCopy, *SetSize, AnsiSet); error: /* Free the Ansi copy if we had one */ if (UnicodeCopy) HeapFree(WsSockHeap, 0, UnicodeCopy); /* Free all the strings */ if (ServiceCopy) HeapFree(WsSockHeap, 0, ServiceCopy); if (CommentCopy) HeapFree(WsSockHeap, 0, CommentCopy); if (ContextCopy) HeapFree(WsSockHeap, 0, ContextCopy); if (QueryCopy) HeapFree(WsSockHeap, 0, QueryCopy); /* Return error code */ return ErrorCode; }