ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

qshelpr.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS WinSock 2 API
00004  * FILE:        qshelpr.c
00005  * PURPOSE:     Query Set Conversion/Packing Helpers
00006  * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
00007  */
00008 
00009 /* INCLUDES ******************************************************************/
00010 #include "ws2_32.h"
00011 
00012 //#define NDEBUG
00013 #include <debug.h>
00014 
00015 /* DATA **********************************************************************/
00016 
00017 /* FUNCTIONS *****************************************************************/
00018 
00019 LPSTR
00020 WSAAPI
00021 AnsiDupFromUnicode(IN LPWSTR UnicodeString)
00022 {
00023     INT Length = 0;
00024     BOOL GoOn = TRUE;
00025     LPSTR DuplicatedString = NULL;
00026     INT ReturnValue;
00027 
00028     /* Start a loop (which should only run twice) */
00029     while (GoOn)
00030     {
00031         /* Call the conversion function */
00032         ReturnValue = WideCharToMultiByte(CP_ACP,
00033                                           0,
00034                                           UnicodeString,
00035                                           -1,
00036                                           DuplicatedString,
00037                                           Length,
00038                                           NULL,
00039                                           NULL);
00040         if (ReturnValue > Length)
00041         {
00042             /* This is the first loop, and we have the real size now */
00043             Length = ReturnValue;
00044 
00045             /* Allocate buffer for it */
00046             DuplicatedString = HeapAlloc(WsSockHeap, 0, Length);
00047             if (!DuplicatedString) GoOn = FALSE;
00048         }
00049         else if (ReturnValue > 0)
00050         {
00051             /* The second loop was sucesfull and we have the string */
00052             GoOn = FALSE;
00053         }
00054         else
00055         {
00056             /* Some weird error happened */
00057             if (DuplicatedString) HeapFree(WsSockHeap, 0, DuplicatedString);
00058             DuplicatedString = NULL;
00059             GoOn = FALSE;
00060         }
00061     }
00062 
00063     /* Return the duplicate */
00064     return DuplicatedString;
00065 }
00066 
00067 LPWSTR
00068 WSAAPI
00069 UnicodeDupFromAnsi(IN LPSTR AnsiString)
00070 {
00071     INT Length = 0;
00072     BOOL GoOn = TRUE;
00073     LPWSTR DuplicatedString = NULL;
00074     INT ReturnValue;
00075 
00076     /* Start a loop (which should only run twice) */
00077     while (GoOn)
00078     {
00079         /* Call the conversion function */
00080         ReturnValue = MultiByteToWideChar(CP_ACP,
00081                                           0,
00082                                           AnsiString,
00083                                           -1,
00084                                           DuplicatedString,
00085                                           Length);
00086         if (ReturnValue > Length)
00087         {
00088             /* This is the first loop, and we have the real size now */
00089             Length = ReturnValue;
00090 
00091             /* Allocate buffer for it */
00092             DuplicatedString = HeapAlloc(WsSockHeap, 0, Length * sizeof(WCHAR));
00093             if (!DuplicatedString) GoOn = FALSE;
00094         }
00095         else if (ReturnValue > 0)
00096         {
00097             /* The second loop was sucesfull and we have the string */
00098             GoOn = FALSE;
00099         }
00100         else
00101         {
00102             /* Some weird error happened */
00103             if (DuplicatedString) HeapFree(WsSockHeap, 0, DuplicatedString);
00104             DuplicatedString = NULL;
00105             GoOn = FALSE;
00106         }
00107     }
00108 
00109     /* Return the duplicate */
00110     return DuplicatedString;
00111 }
00112 
00113 SIZE_T
00114 WSAAPI
00115 ComputeStringSize(IN LPSTR String,
00116                   IN BOOLEAN IsUnicode)
00117 {
00118     /* Return the size of the string, in bytes, including null-char */
00119     return (IsUnicode) ? (wcslen((LPWSTR)String) + 1 ) * sizeof(WCHAR) :
00120                           strlen(String) + sizeof(CHAR);
00121 }
00122 
00123 SIZE_T
00124 WSAAPI
00125 ComputeQuerySetSize(IN LPWSAQUERYSETA AnsiSet,
00126                     IN BOOLEAN IsUnicode)
00127 {
00128     SIZE_T Size = sizeof(WSAQUERYSETA);
00129     //LPWSAQUERYSETW UnicodeSet = (LPWSAQUERYSETW)AnsiSet;
00130     DWORD i;
00131 
00132     /* Check for instance name */
00133     if (AnsiSet->lpszServiceInstanceName)
00134     {
00135         /* Add its size */
00136         Size += ComputeStringSize(AnsiSet->lpszServiceInstanceName, IsUnicode);
00137     }
00138 
00139     /* Check for Service Class ID */
00140     if (AnsiSet->lpServiceClassId)
00141     {
00142         /* Align the current size and add GUID size */
00143         Size = (Size + 3) & ~3;
00144         Size += sizeof(GUID);
00145     }
00146 
00147     /* Check for version data */
00148     if (AnsiSet->lpVersion)
00149     {
00150         /* Align the current size and add GUID size */
00151         Size = (Size + 3) & ~3;
00152         Size += sizeof(WSAVERSION);
00153     }
00154 
00155     /* Check for comment */
00156     if (AnsiSet->lpszComment)
00157     {
00158         /* Align the current size and add string size */
00159         Size = (Size + 1) & ~1;
00160         Size += ComputeStringSize(AnsiSet->lpszComment, IsUnicode);
00161     }
00162 
00163     /* Check for Provider ID */
00164     if (AnsiSet->lpNSProviderId)
00165     {
00166         /* Align the current size and add GUID size */
00167         Size = (Size + 3) & ~3;
00168         Size += sizeof(GUID);
00169     }
00170 
00171     /* Check for context */
00172     if (AnsiSet->lpszContext)
00173     {
00174         /* Align the current size and add string size */
00175         Size = (Size + 1) & ~1;
00176         Size += ComputeStringSize(AnsiSet->lpszContext, IsUnicode);
00177     }
00178 
00179     /* Check for query string */
00180     if (AnsiSet->lpszQueryString)
00181     {
00182         /* Align the current size and add string size */
00183         Size = (Size + 1) & ~1;
00184         Size += ComputeStringSize(AnsiSet->lpszQueryString, IsUnicode);
00185     }
00186 
00187     /* Check for AF Protocol data */
00188     if (AnsiSet->lpafpProtocols)
00189     {
00190         /* Align the current size and add AFP size */
00191         Size = (Size + 3) & ~3;
00192         Size += sizeof(AFPROTOCOLS) * AnsiSet->dwNumberOfProtocols;
00193     }
00194 
00195     /* Check for CSADDR buffer */
00196     if (AnsiSet->lpcsaBuffer)
00197     {
00198         /* Align the current size */
00199         Size = (Size + 3) & ~3;
00200 
00201         /* Loop all the addresses in the array */
00202         for (i = 0; i < AnsiSet->dwNumberOfCsAddrs; i++)
00203         {
00204             /* Check for local sockaddr */
00205             if (&AnsiSet->lpcsaBuffer[i].LocalAddr)
00206             {
00207                 /* Align the current size and add the sockaddr's length */
00208                 Size = (Size + 3) & ~3; 
00209                 Size += AnsiSet->lpcsaBuffer[i].LocalAddr.iSockaddrLength;
00210             }
00211             /* Check for remote sockaddr */
00212             if (&AnsiSet->lpcsaBuffer[i].RemoteAddr)
00213             {
00214                 /* Align the current size and add the sockaddr's length */
00215                 Size = (Size + 3) & ~3; 
00216                 Size += AnsiSet->lpcsaBuffer[i].RemoteAddr.iSockaddrLength;
00217             }
00218 
00219             /* Add the sockaddr size itself */
00220             Size += sizeof(CSADDR_INFO);
00221         }
00222     }
00223 
00224     /* Check for blob data */
00225     if (AnsiSet->lpBlob)
00226     {
00227         /* Align the current size and add blob size */
00228         Size = (Size + 3) & ~3;
00229         Size += sizeof(BLOB);
00230 
00231         /* Also add the actual blob data size, if it exists */
00232         if (AnsiSet->lpBlob) Size += AnsiSet->lpBlob->cbSize;
00233     }
00234 
00235     /* Return the total size */
00236     return Size;
00237 }
00238 
00239 SIZE_T
00240 WSAAPI
00241 WSAComputeQuerySetSizeA(IN LPWSAQUERYSETA AnsiSet)
00242 {
00243     /* Call the generic helper */
00244     return ComputeQuerySetSize(AnsiSet, FALSE);
00245 }
00246 
00247 SIZE_T
00248 WSAAPI
00249 WSAComputeQuerySetSizeW(IN LPWSAQUERYSETW UnicodeSet)
00250 {
00251     /* Call the generic helper */
00252     return ComputeQuerySetSize((LPWSAQUERYSETA)UnicodeSet, TRUE);
00253 }
00254 
00255 PVOID
00256 WSAAPI
00257 WsBufferAllocate(IN PWS_BUFFER Buffer,
00258                  IN SIZE_T Size,
00259                  IN DWORD Align)
00260 {
00261     PVOID NewPosition;
00262 
00263     /* Align the current usage */
00264     Buffer->BytesUsed = (Buffer->BytesUsed + Align - 1) & ~(Align - 1);
00265 
00266     /* Update our location */
00267     NewPosition = (PVOID)(Buffer->Position + Buffer->BytesUsed);
00268 
00269     /* Update the usage */
00270     Buffer->BytesUsed += Size;
00271 
00272     /* Return new location */
00273     return NewPosition;
00274 }
00275 
00276 VOID
00277 WSAAPI
00278 CopyBlobIndirect(IN PWS_BUFFER Buffer,
00279                  IN OUT LPBLOB RelativeBlob,
00280                  IN LPBLOB Blob)
00281 {
00282     /* Make sure we have blob data */
00283     if ((Blob->pBlobData) && (Blob->cbSize))
00284     {
00285         /* Allocate and copy the blob data */
00286         RelativeBlob->pBlobData = WsBufferAllocate(Buffer,
00287                                                    Blob->cbSize,
00288                                                    sizeof(PVOID));
00289         RtlCopyMemory(RelativeBlob->pBlobData,
00290                       Blob->pBlobData,
00291                       Blob->cbSize);
00292     }
00293 }
00294 
00295 VOID
00296 WSAAPI
00297 CopyAddrInfoArrayIndirect(IN PWS_BUFFER Buffer,
00298                           IN OUT LPCSADDR_INFO RelativeCsaddr,
00299                           IN DWORD Addresses,
00300                           IN LPCSADDR_INFO Csaddr)
00301 {
00302     DWORD i;
00303 
00304     /* Loop for every address inside */
00305     for (i = 0; i < Addresses; i++)
00306     {
00307         /* Check for a local address */
00308         if ((Csaddr[i].LocalAddr.lpSockaddr) &&
00309             (Csaddr[i].LocalAddr.iSockaddrLength))
00310         {
00311             /* Allocate and copy the address */
00312             RelativeCsaddr[i].LocalAddr.lpSockaddr =
00313                 WsBufferAllocate(Buffer,
00314                                  Csaddr[i].LocalAddr.iSockaddrLength,
00315                                  sizeof(PVOID));
00316             RtlCopyMemory(RelativeCsaddr[i].LocalAddr.lpSockaddr,
00317                           Csaddr[i].LocalAddr.lpSockaddr,
00318                           Csaddr[i].LocalAddr.iSockaddrLength);
00319         }
00320         else
00321         {
00322             /* Nothing in this address */
00323             Csaddr[i].LocalAddr.lpSockaddr = NULL;
00324             Csaddr[i].LocalAddr.iSockaddrLength = 0;
00325         }
00326 
00327         /* Check for a remote address */
00328         if ((Csaddr[i].RemoteAddr.lpSockaddr) &&
00329             (Csaddr[i].RemoteAddr.iSockaddrLength))
00330         {
00331             /* Allocate and copy the address */
00332             RelativeCsaddr[i].RemoteAddr.lpSockaddr =
00333                 WsBufferAllocate(Buffer,
00334                                  Csaddr[i].RemoteAddr.iSockaddrLength,
00335                                  sizeof(PVOID));
00336             RtlCopyMemory(RelativeCsaddr[i].RemoteAddr.lpSockaddr,
00337                           Csaddr[i].RemoteAddr.lpSockaddr,
00338                           Csaddr[i].RemoteAddr.iSockaddrLength);
00339         }
00340         else
00341         {
00342             /* Nothing in this address */
00343             Csaddr[i].RemoteAddr.lpSockaddr = NULL;
00344             Csaddr[i].RemoteAddr.iSockaddrLength = 0;
00345         }
00346     }
00347 }
00348 
00349 VOID
00350 WSAAPI
00351 CopyQuerySetIndirectA(IN PWS_BUFFER Buffer,
00352                       IN OUT LPWSAQUERYSETA RelativeSet,
00353                       IN LPWSAQUERYSETA AnsiSet)
00354 {
00355     LPSTR AnsiString;
00356 
00357     /* Get the service name */
00358     AnsiString = AnsiSet->lpszServiceInstanceName;
00359     if (AnsiString)
00360     {
00361         /* One exists, allocate a space in the buffer for it */
00362         RelativeSet->lpszServiceInstanceName = WsBufferAllocate(Buffer,
00363                                                                 strlen(AnsiString) + 1,
00364                                                                 sizeof(CHAR));
00365         /* Copy it into the buffer */
00366         strcpy(RelativeSet->lpszServiceInstanceName, AnsiString);
00367     }
00368     else
00369     {
00370         /* Nothing in the buffer */
00371         RelativeSet->lpszServiceInstanceName = NULL;
00372     }
00373 
00374     /* Check for the service class ID */
00375     if (AnsiSet->lpServiceClassId)
00376     {
00377         /* One exists, allocate a space in the buffer for it */
00378         RelativeSet->lpServiceClassId = WsBufferAllocate(Buffer,
00379                                                          sizeof(GUID),
00380                                                          sizeof(PVOID));
00381         /* Copy it into the buffer */
00382         *(RelativeSet->lpServiceClassId) = *(AnsiSet->lpServiceClassId);
00383     }
00384     else
00385     {
00386         /* Nothing in the buffer */
00387         RelativeSet->lpServiceClassId = NULL;
00388     }
00389 
00390     /* Get the version data */
00391     if (AnsiSet->lpVersion)
00392     {
00393         /* One exists, allocate a space in the buffer for it */
00394         RelativeSet->lpVersion = WsBufferAllocate(Buffer,
00395                                                   sizeof(WSAVERSION),
00396                                                   sizeof(PVOID));
00397         /* Copy it into the buffer */
00398         *(RelativeSet->lpVersion) = *(AnsiSet->lpVersion);
00399     }
00400     else
00401     {
00402         /* Nothing in the buffer */
00403         RelativeSet->lpVersion = NULL;
00404     }
00405 
00406     /* Get the comment */
00407     AnsiString = AnsiSet->lpszComment;
00408     if (AnsiString)
00409     {
00410         /* One exists, allocate a space in the buffer for it */
00411         RelativeSet->lpszComment = WsBufferAllocate(Buffer,
00412                                                     strlen(AnsiString) + 1,
00413                                                     sizeof(CHAR));
00414         /* Copy it into the buffer */
00415         strcpy(RelativeSet->lpszComment, AnsiString);
00416     }
00417     else
00418     {
00419         /* Nothing in the buffer */
00420         RelativeSet->lpszComment = NULL;
00421     }
00422 
00423     /* Get the NS Provider ID */
00424     if (AnsiSet->lpNSProviderId)
00425     {
00426         /* One exists, allocate a space in the buffer for it */
00427         RelativeSet->lpNSProviderId = WsBufferAllocate(Buffer,
00428                                                        sizeof(GUID),
00429                                                        sizeof(PVOID));
00430         /* Copy it into the buffer */
00431         *(RelativeSet->lpNSProviderId) = *(AnsiSet->lpNSProviderId);
00432     }
00433     else
00434     {
00435         /* Nothing in the buffer */
00436         RelativeSet->lpNSProviderId = NULL;
00437     }
00438 
00439     /* Get the context */
00440     AnsiString = AnsiSet->lpszContext;
00441     if (AnsiString)
00442     {
00443         /* One exists, allocate a space in the buffer for it */
00444         RelativeSet->lpszContext = WsBufferAllocate(Buffer,
00445                                                     strlen(AnsiString) + 1,
00446                                                     sizeof(CHAR));
00447         /* Copy it into the buffer */
00448         strcpy(RelativeSet->lpszContext, AnsiString);
00449     }
00450     else
00451     {
00452         /* Nothing in the buffer */
00453         RelativeSet->lpszContext = NULL;
00454     }
00455 
00456     /* Get the query string */
00457     AnsiString = AnsiSet->lpszQueryString;
00458     if (AnsiString)
00459     {
00460         /* One exists, allocate a space in the buffer for it */
00461         RelativeSet->lpszQueryString = WsBufferAllocate(Buffer,
00462                                                         strlen(AnsiString) + 1,
00463                                                         sizeof(CHAR));
00464         /* Copy it into the buffer */
00465         strcpy(RelativeSet->lpszQueryString, AnsiString);
00466     }
00467     else
00468     {
00469         /* Nothing in the buffer */
00470         RelativeSet->lpszQueryString = NULL;
00471     }
00472 
00473     /* Check for a protocol structure with non-zero protocols */
00474     if ((AnsiSet->lpafpProtocols) && (AnsiSet->dwNumberOfProtocols))
00475     {
00476         /* One exists, allocate space for it */
00477         RelativeSet->lpafpProtocols = WsBufferAllocate(Buffer,
00478                                                        AnsiSet->dwNumberOfProtocols *
00479                                                        sizeof(AFPROTOCOLS),
00480                                                        sizeof(PVOID));
00481         /* Copy it into the buffer */
00482         RtlCopyMemory(RelativeSet->lpafpProtocols,
00483                       AnsiSet->lpafpProtocols,
00484                       AnsiSet->dwNumberOfProtocols * sizeof(AFPROTOCOLS));
00485     }
00486     else
00487     {
00488         /* Nothing in the buffer */
00489         RelativeSet->lpafpProtocols = NULL;
00490         RelativeSet->dwNumberOfProtocols = 0;
00491     }
00492 
00493     /* Check if we have a CSADDR with addresses inside */
00494     if ((AnsiSet->lpcsaBuffer) && (AnsiSet->dwNumberOfCsAddrs))
00495     {
00496         /* Allocate and copy the CSADDR structure itself */
00497         RelativeSet->lpcsaBuffer = WsBufferAllocate(Buffer,
00498                                                     AnsiSet->dwNumberOfCsAddrs *
00499                                                     sizeof(CSADDR_INFO),
00500                                                     sizeof(PVOID));
00501 
00502         /* Copy it into the buffer */
00503         RtlCopyMemory(RelativeSet->lpafpProtocols,
00504                       AnsiSet->lpafpProtocols,
00505                       AnsiSet->dwNumberOfCsAddrs * sizeof(CSADDR_INFO));
00506 
00507         /* Copy the addresses inside the CSADDR */
00508         CopyAddrInfoArrayIndirect(Buffer,
00509                                   RelativeSet->lpcsaBuffer,
00510                                   AnsiSet->dwNumberOfCsAddrs,
00511                                   AnsiSet->lpcsaBuffer);
00512     }
00513     else
00514     {
00515         /* Nothing in the buffer */
00516         RelativeSet->lpcsaBuffer = NULL;
00517         RelativeSet->dwNumberOfCsAddrs = 0;
00518     }
00519 
00520     /* Check for blob data */
00521     if (AnsiSet->lpBlob)
00522     {
00523         /* Allocate and copy the blob itself */
00524         RelativeSet->lpBlob = WsBufferAllocate(Buffer,
00525                                                sizeof(BLOB),
00526                                                sizeof(PVOID));
00527         *(RelativeSet->lpBlob) = *(AnsiSet->lpBlob);
00528 
00529         /* Copy the data inside the blob */
00530         CopyBlobIndirect(Buffer, RelativeSet->lpBlob, AnsiSet->lpBlob);
00531     }
00532     else
00533     {
00534         /* Nothing in the buffer */
00535         RelativeSet->lpBlob = NULL;
00536     }
00537 }
00538 
00539 VOID
00540 WSAAPI
00541 CopyQuerySetIndirectW(IN PWS_BUFFER Buffer,
00542                       IN OUT LPWSAQUERYSETW RelativeSet,
00543                       IN LPWSAQUERYSETW UnicodeSet)
00544 {
00545     LPWSTR UnicodeString;
00546 
00547     /* Get the service name */
00548     UnicodeString = UnicodeSet->lpszServiceInstanceName;
00549     if (UnicodeString)
00550     {
00551         /* One exists, allocate a space in the buffer for it */
00552         RelativeSet->lpszServiceInstanceName = WsBufferAllocate(Buffer,
00553                                                                 (wcslen(UnicodeString) + 1) *
00554                                                                 sizeof(WCHAR),
00555                                                                 sizeof(CHAR));
00556         /* Copy it into the buffer */
00557         wcscpy(RelativeSet->lpszServiceInstanceName, UnicodeString);
00558     }
00559     else
00560     {
00561         /* Nothing in the buffer */
00562         RelativeSet->lpszServiceInstanceName = NULL;
00563     }
00564 
00565     /* Check for the service class ID */
00566     if (UnicodeSet->lpServiceClassId)
00567     {
00568         /* One exists, allocate a space in the buffer for it */
00569         RelativeSet->lpServiceClassId = WsBufferAllocate(Buffer,
00570                                                          sizeof(GUID),
00571                                                          sizeof(PVOID));
00572         /* Copy it into the buffer */
00573         *(RelativeSet->lpServiceClassId) = *(UnicodeSet->lpServiceClassId);
00574     }
00575     else
00576     {
00577         /* Nothing in the buffer */
00578         RelativeSet->lpServiceClassId = NULL;
00579     }
00580 
00581     /* Get the version data */
00582     if (UnicodeSet->lpVersion)
00583     {
00584         /* One exists, allocate a space in the buffer for it */
00585         RelativeSet->lpVersion = WsBufferAllocate(Buffer,
00586                                                   sizeof(WSAVERSION),
00587                                                   sizeof(PVOID));
00588         /* Copy it into the buffer */
00589         *(RelativeSet->lpVersion) = *(UnicodeSet->lpVersion);
00590     }
00591     else
00592     {
00593         /* Nothing in the buffer */
00594         RelativeSet->lpVersion = NULL;
00595     }
00596 
00597     /* Get the comment */
00598     UnicodeString = UnicodeSet->lpszComment;
00599     if (UnicodeString)
00600     {
00601         /* One exists, allocate a space in the buffer for it */
00602         RelativeSet->lpszComment = WsBufferAllocate(Buffer,
00603                                                     (wcslen(UnicodeString) + 1) *
00604                                                     sizeof(WCHAR),
00605                                                     sizeof(CHAR));
00606         /* Copy it into the buffer */
00607         wcscpy(RelativeSet->lpszComment, UnicodeString);
00608     }
00609     else
00610     {
00611         /* Nothing in the buffer */
00612         RelativeSet->lpszComment = NULL;
00613     }
00614 
00615     /* Get the NS Provider ID */
00616     if (UnicodeSet->lpNSProviderId)
00617     {
00618         /* One exists, allocate a space in the buffer for it */
00619         RelativeSet->lpNSProviderId = WsBufferAllocate(Buffer,
00620                                                        sizeof(GUID),
00621                                                        sizeof(PVOID));
00622         /* Copy it into the buffer */
00623         *(RelativeSet->lpNSProviderId) = *(UnicodeSet->lpNSProviderId);
00624     }
00625     else
00626     {
00627         /* Nothing in the buffer */
00628         RelativeSet->lpNSProviderId = NULL;
00629     }
00630 
00631     /* Get the context */
00632     UnicodeString = UnicodeSet->lpszContext;
00633     if (UnicodeString)
00634     {
00635         /* One exists, allocate a space in the buffer for it */
00636         RelativeSet->lpszContext = WsBufferAllocate(Buffer,
00637                                                     (wcslen(UnicodeString) + 1) *
00638                                                     sizeof(WCHAR),
00639                                                     sizeof(CHAR));
00640         /* Copy it into the buffer */
00641         wcscpy(RelativeSet->lpszContext, UnicodeString);
00642     }
00643     else
00644     {
00645         /* Nothing in the buffer */
00646         RelativeSet->lpszContext = NULL;
00647     }
00648 
00649     /* Get the query string */
00650     UnicodeString = UnicodeSet->lpszQueryString;
00651     if (UnicodeString)
00652     {
00653         /* One exists, allocate a space in the buffer for it */
00654         RelativeSet->lpszQueryString = WsBufferAllocate(Buffer,
00655                                                         (wcslen(UnicodeString) + 1) *
00656                                                         sizeof(WCHAR),
00657                                                         sizeof(CHAR));
00658         /* Copy it into the buffer */
00659         wcscpy(RelativeSet->lpszQueryString, UnicodeString);
00660     }
00661     else
00662     {
00663         /* Nothing in the buffer */
00664         RelativeSet->lpszQueryString = NULL;
00665     }
00666 
00667     /* Check for a protocol structure with non-zero protocols */
00668     if ((UnicodeSet->lpafpProtocols) && (UnicodeSet->dwNumberOfProtocols))
00669     {
00670         /* One exists, allocate space for it */
00671         RelativeSet->lpafpProtocols = WsBufferAllocate(Buffer,
00672                                                        UnicodeSet->dwNumberOfProtocols *
00673                                                        sizeof(AFPROTOCOLS),
00674                                                        sizeof(PVOID));
00675         /* Copy it into the buffer */
00676         RtlCopyMemory(RelativeSet->lpafpProtocols,
00677                       UnicodeSet->lpafpProtocols,
00678                       UnicodeSet->dwNumberOfProtocols * sizeof(AFPROTOCOLS));
00679     }
00680     else
00681     {
00682         /* Nothing in the buffer */
00683         RelativeSet->lpafpProtocols = NULL;
00684         RelativeSet->dwNumberOfProtocols = 0;
00685     }
00686 
00687     /* Check if we have a CSADDR with addresses inside */
00688     if ((UnicodeSet->lpcsaBuffer) && (UnicodeSet->dwNumberOfCsAddrs))
00689     {
00690         /* Allocate and copy the CSADDR structure itself */
00691         RelativeSet->lpcsaBuffer = WsBufferAllocate(Buffer,
00692                                                     UnicodeSet->dwNumberOfCsAddrs *
00693                                                     sizeof(CSADDR_INFO),
00694                                                     sizeof(PVOID));
00695 
00696         /* Copy it into the buffer */
00697         RtlCopyMemory(RelativeSet->lpafpProtocols,
00698                       UnicodeSet->lpafpProtocols,
00699                       UnicodeSet->dwNumberOfCsAddrs * sizeof(CSADDR_INFO));
00700 
00701         /* Copy the addresses inside the CSADDR */
00702         CopyAddrInfoArrayIndirect(Buffer,
00703                                   RelativeSet->lpcsaBuffer,
00704                                   UnicodeSet->dwNumberOfCsAddrs,
00705                                   UnicodeSet->lpcsaBuffer);
00706     }
00707     else
00708     {
00709         /* Nothing in the buffer */
00710         RelativeSet->lpcsaBuffer = NULL;
00711         RelativeSet->dwNumberOfCsAddrs = 0;
00712     }
00713 
00714     /* Check for blob data */
00715     if (UnicodeSet->lpBlob)
00716     {
00717         /* Allocate and copy the blob itself */
00718         RelativeSet->lpBlob = WsBufferAllocate(Buffer,
00719                                                sizeof(BLOB),
00720                                                sizeof(PVOID));
00721         *(RelativeSet->lpBlob) = *(UnicodeSet->lpBlob);
00722 
00723         /* Copy the data inside the blob */
00724         CopyBlobIndirect(Buffer, RelativeSet->lpBlob, UnicodeSet->lpBlob);
00725     }
00726     else
00727     {
00728         /* Nothing in the buffer */
00729         RelativeSet->lpBlob = NULL;
00730     }
00731 }
00732 
00733 INT
00734 WSAAPI
00735 WSABuildQuerySetBufferA(IN LPWSAQUERYSETA AnsiSet,
00736                         IN SIZE_T BufferSize,
00737                         OUT LPWSAQUERYSETA RelativeSet)
00738 {
00739     INT ErrorCode = ERROR_SUCCESS;
00740     SIZE_T SetSize;
00741     WS_BUFFER Buffer;
00742     LPWSAQUERYSETA NewSet;
00743 
00744     /* Find out how big the set really is */
00745     SetSize = WSAComputeQuerySetSizeA(AnsiSet);
00746     if (SetSize <= BufferSize)
00747     {
00748         /* Configure the buffer */
00749         Buffer.Position = (ULONG_PTR)RelativeSet;
00750         Buffer.MaxSize = SetSize;
00751         Buffer.BytesUsed = 0;
00752 
00753         /* Copy the set itself into the buffer */
00754         NewSet = WsBufferAllocate(&Buffer, sizeof(*AnsiSet), sizeof(PVOID));
00755         *NewSet = *AnsiSet;
00756 
00757         /* Now copy the data inside */
00758         CopyQuerySetIndirectA(&Buffer, NewSet, AnsiSet);
00759     }
00760     else
00761     {
00762         /* We failed */
00763         ErrorCode = SOCKET_ERROR;
00764     }
00765 
00766     /* Return to caller */
00767     return ErrorCode;
00768 }
00769 
00770 INT
00771 WSAAPI
00772 WSABuildQuerySetBufferW(IN LPWSAQUERYSETW UnicodeSet,
00773                         IN SIZE_T BufferSize,
00774                         OUT LPWSAQUERYSETW RelativeSet)
00775 {
00776     INT ErrorCode = ERROR_SUCCESS;
00777     SIZE_T SetSize;
00778     WS_BUFFER Buffer;
00779     LPWSAQUERYSETW NewSet;
00780 
00781     /* Find out how big the set really is */
00782     SetSize = WSAComputeQuerySetSizeW(UnicodeSet);
00783     if (SetSize <= BufferSize)
00784     {
00785         /* Configure the buffer */
00786         Buffer.Position = (ULONG_PTR)RelativeSet;
00787         Buffer.MaxSize = SetSize;
00788         Buffer.BytesUsed = 0;
00789 
00790         /* Copy the set itself into the buffer */
00791         NewSet = WsBufferAllocate(&Buffer, sizeof(*UnicodeSet), sizeof(PVOID));
00792         *NewSet = *UnicodeSet;
00793 
00794         /* Now copy the data inside */
00795         CopyQuerySetIndirectW(&Buffer, NewSet, UnicodeSet);
00796     }
00797     else
00798     {
00799         /* We failed */
00800         ErrorCode = SOCKET_ERROR;
00801     }
00802 
00803     /* Return to caller */
00804     return ErrorCode;
00805 }
00806 
00807 INT
00808 WSAAPI
00809 MapAnsiQuerySetToUnicode(IN LPWSAQUERYSETA AnsiSet,
00810                          IN OUT PSIZE_T SetSize,
00811                          OUT LPWSAQUERYSETW UnicodeSet)
00812 {
00813     INT ErrorCode = ERROR_SUCCESS;
00814     SIZE_T AnsiSize, UnicodeSize;
00815     LPWSAQUERYSETA AnsiCopy = NULL;
00816     LPWSAQUERYSETW UnicodeCopy;
00817     LPWSTR ServiceCopy = NULL, CommentCopy = NULL;
00818     LPWSTR ContextCopy = NULL, QueryCopy = NULL;
00819 
00820     /* Calculate the size of the Ansi version and allocate space for a copy */
00821     AnsiSize = WSAComputeQuerySetSizeA(AnsiSet);
00822     AnsiCopy = HeapAlloc(WsSockHeap, 0, AnsiSize);
00823     if (!AnsiCopy)
00824     {
00825         /* Fail, couldn't allocate memory */
00826         ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00827         goto error;
00828     }
00829     
00830     /* Build the relative buffer version */
00831     ErrorCode = WSABuildQuerySetBufferA(AnsiSet, AnsiSize, AnsiCopy);
00832     if (ErrorCode != ERROR_SUCCESS) goto error;
00833         
00834     /* Re-use the ANSI version since the fields match */
00835     UnicodeCopy = (LPWSAQUERYSETW)AnsiCopy;
00836 
00837     /* Check if we have a service instance name */
00838     if (AnsiCopy->lpszServiceInstanceName)
00839     {
00840         /* Duplicate it into unicode form */
00841         ServiceCopy = UnicodeDupFromAnsi(AnsiCopy->lpszServiceInstanceName);
00842         if (!ServiceCopy)
00843         {
00844             /* Fail */
00845             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00846             goto error;
00847         }
00848 
00849         /* Set the new string pointer */
00850         UnicodeCopy->lpszServiceInstanceName = ServiceCopy;
00851     }
00852 
00853     /* Check if we have a service instance name */
00854     if (AnsiCopy->lpszContext)
00855     {
00856         /* Duplicate it into unicode form */
00857         ContextCopy = UnicodeDupFromAnsi(AnsiCopy->lpszContext);
00858         if (!ContextCopy)
00859         {
00860             /* Fail */
00861             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00862             goto error;
00863         }
00864 
00865         /* Set the new string pointer */
00866         UnicodeCopy->lpszContext = ContextCopy;
00867     }
00868 
00869     /* Check if we have a service instance name */
00870     if (AnsiCopy->lpszComment)
00871     {
00872         /* Duplicate it into unicode form */
00873         CommentCopy = UnicodeDupFromAnsi(AnsiCopy->lpszComment);
00874         if (!CommentCopy)
00875         {
00876             /* Fail */
00877             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00878             goto error;
00879         }
00880 
00881         /* Set the new string pointer */
00882         UnicodeCopy->lpszComment = CommentCopy;
00883     }
00884 
00885     /* Check if we have a query name */
00886     if (AnsiCopy->lpszQueryString)
00887     {
00888         /* Duplicate it into unicode form */
00889         QueryCopy = UnicodeDupFromAnsi(AnsiCopy->lpszQueryString);
00890         if (!QueryCopy)
00891         {
00892             /* Fail */
00893             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00894             goto error;
00895         }
00896 
00897         /* Set the new string pointer */
00898         UnicodeCopy->lpszQueryString = QueryCopy;
00899     }
00900 
00901     /* Now that we have the absolute unicode buffer, calculate its size */
00902     UnicodeSize = WSAComputeQuerySetSizeW(UnicodeCopy);
00903     if (UnicodeSize > *SetSize)
00904     {
00905         /* The buffer wasn't large enough; return how much we need */
00906         *SetSize = UnicodeSize;
00907         ErrorCode = WSAEFAULT;
00908         goto error;
00909     }
00910 
00911     /* Build the relative unicode buffer */
00912     ErrorCode = WSABuildQuerySetBufferW(UnicodeCopy, *SetSize, UnicodeSet);
00913 
00914 error:
00915     /* Free the Ansi copy if we had one */
00916     if (AnsiCopy) HeapFree(WsSockHeap, 0, AnsiCopy);
00917 
00918     /* Free all the strings */
00919     if (ServiceCopy) HeapFree(WsSockHeap, 0, ServiceCopy);
00920     if (CommentCopy) HeapFree(WsSockHeap, 0, CommentCopy);
00921     if (ContextCopy) HeapFree(WsSockHeap, 0, ContextCopy);
00922     if (QueryCopy) HeapFree(WsSockHeap, 0, QueryCopy);
00923 
00924     /* Return error code */
00925     return ErrorCode;
00926 }
00927 
00928 INT
00929 WSAAPI
00930 MapUnicodeQuerySetToAnsi(OUT LPWSAQUERYSETW UnicodeSet,
00931                          IN OUT PSIZE_T SetSize,
00932                          IN LPWSAQUERYSETA AnsiSet)
00933 {
00934     INT ErrorCode = ERROR_SUCCESS;
00935     SIZE_T UnicodeSize, AnsiSize;
00936     LPWSAQUERYSETW UnicodeCopy = NULL;
00937     LPWSAQUERYSETA AnsiCopy;
00938     LPSTR ServiceCopy = NULL, CommentCopy = NULL;
00939     LPSTR ContextCopy = NULL, QueryCopy = NULL;
00940 
00941     /* Calculate the size of the Ansi version and allocate space for a copy */
00942     UnicodeSize = WSAComputeQuerySetSizeW(UnicodeSet);
00943     UnicodeCopy = HeapAlloc(WsSockHeap, 0, UnicodeSize);
00944     if (!UnicodeCopy)
00945     {
00946         /* Fail, couldn't allocate memory */
00947         ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00948         goto error;
00949     }
00950     
00951     /* Build the relative buffer version */
00952     ErrorCode = WSABuildQuerySetBufferW(UnicodeSet, UnicodeSize, UnicodeCopy);
00953     if (ErrorCode != ERROR_SUCCESS) goto error;
00954         
00955     /* Re-use the Unicode version since the fields match */
00956     AnsiCopy = (LPWSAQUERYSETA)UnicodeCopy;
00957 
00958     /* Check if we have a service instance name */
00959     if (UnicodeCopy->lpszServiceInstanceName)
00960     {
00961         /* Duplicate it into unicode form */
00962         ServiceCopy = AnsiDupFromUnicode(UnicodeCopy->lpszServiceInstanceName);
00963         if (!ServiceCopy)
00964         {
00965             /* Fail */
00966             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00967             goto error;
00968         }
00969 
00970         /* Set the new string pointer */
00971         AnsiCopy->lpszServiceInstanceName = ServiceCopy;
00972     }
00973 
00974     /* Check if we have a service instance name */
00975     if (UnicodeCopy->lpszContext)
00976     {
00977         /* Duplicate it into unicode form */
00978         ContextCopy = AnsiDupFromUnicode(UnicodeCopy->lpszContext);
00979         if (!ContextCopy)
00980         {
00981             /* Fail */
00982             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00983             goto error;
00984         }
00985 
00986         /* Set the new string pointer */
00987         AnsiCopy->lpszContext = ContextCopy;
00988     }
00989 
00990     /* Check if we have a service instance name */
00991     if (UnicodeCopy->lpszComment)
00992     {
00993         /* Duplicate it into unicode form */
00994         CommentCopy = AnsiDupFromUnicode(UnicodeCopy->lpszComment);
00995         if (!CommentCopy)
00996         {
00997             /* Fail */
00998             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
00999             goto error;
01000         }
01001 
01002         /* Set the new string pointer */
01003         AnsiCopy->lpszComment = CommentCopy;
01004     }
01005 
01006     /* Check if we have a query name */
01007     if (UnicodeCopy->lpszQueryString)
01008     {
01009         /* Duplicate it into unicode form */
01010         QueryCopy = AnsiDupFromUnicode(UnicodeCopy->lpszQueryString);
01011         if (!QueryCopy)
01012         {
01013             /* Fail */
01014             ErrorCode = WSA_NOT_ENOUGH_MEMORY;
01015             goto error;
01016         }
01017 
01018         /* Set the new string pointer */
01019         AnsiCopy->lpszQueryString = QueryCopy;
01020     }
01021 
01022     /* Now that we have the absolute unicode buffer, calculate its size */
01023     AnsiSize = WSAComputeQuerySetSizeA(AnsiCopy);
01024     if (AnsiSize > *SetSize)
01025     {
01026         /* The buffer wasn't large enough; return how much we need */
01027         *SetSize = AnsiSize;
01028         ErrorCode = WSAEFAULT;
01029         goto error;
01030     }
01031 
01032     /* Build the relative unicode buffer */
01033     ErrorCode = WSABuildQuerySetBufferA(AnsiCopy, *SetSize, AnsiSet);
01034 
01035 error:
01036     /* Free the Ansi copy if we had one */
01037     if (UnicodeCopy) HeapFree(WsSockHeap, 0, UnicodeCopy);
01038 
01039     /* Free all the strings */
01040     if (ServiceCopy) HeapFree(WsSockHeap, 0, ServiceCopy);
01041     if (CommentCopy) HeapFree(WsSockHeap, 0, CommentCopy);
01042     if (ContextCopy) HeapFree(WsSockHeap, 0, ContextCopy);
01043     if (QueryCopy) HeapFree(WsSockHeap, 0, QueryCopy);
01044 
01045     /* Return error code */
01046     return ErrorCode;
01047 }
01048 
01049 INT
01050 WSAAPI
01051 CopyQuerySetW(IN LPWSAQUERYSETW UnicodeSet,
01052               OUT LPWSAQUERYSETW *UnicodeCopy)
01053 {
01054     SIZE_T SetSize;
01055 
01056     /* Get the size */
01057     SetSize = WSAComputeQuerySetSizeW(UnicodeSet);
01058 
01059     /* Allocate memory for copy */
01060     *UnicodeCopy = HeapAlloc(WsSockHeap, 0, SetSize);
01061     if (!(*UnicodeCopy)) return WSA_NOT_ENOUGH_MEMORY;
01062 
01063     /* Build a copy and return */
01064     return WSABuildQuerySetBufferW(UnicodeSet, SetSize, *UnicodeCopy);
01065 }

Generated on Sat May 26 2012 04:25:40 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.