VirtualBox

Changeset 51997 in vbox for trunk


Ignore:
Timestamp:
Jul 11, 2014 9:04:44 PM (11 years ago)
Author:
vboxsync
Message:

include,Main,Additions: SHFLSTRING cleanup.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/shflsvc.h

    r51254 r51997  
    153153 * Shared folder string buffer structure.
    154154 */
     155#pragma pack(1)
    155156typedef struct _SHFLSTRING
    156157{
    157     /** Size of the String member in bytes. */
     158    /** Allocated size of the String member in bytes. */
    158159    uint16_t u16Size;
    159160
     
    168169    } String;
    169170} SHFLSTRING;
     171#pragma pack()
     172
     173#define SHFLSTRING_HEADER_SIZE RT_UOFFSETOF(SHFLSTRING, String)
     174AssertCompile(SHFLSTRING_HEADER_SIZE == 4);
    170175
    171176/** Pointer to a shared folder string buffer. */
     
    188193{
    189194    PSHFLSTRING pString = NULL;
    190     const uint32_t u32HeaderSize = sizeof(SHFLSTRING);
     195    const uint32_t u32HeaderSize = SHFLSTRING_HEADER_SIZE;
    191196
    192197    /*
     
    199204        pString->u16Size = u32Size - u32HeaderSize;
    200205        pString->u16Length = 0;
     206        if (pString->u16Size >= sizeof(pString->String.ucs2[0]))
     207            pString->String.ucs2[0] = 0;
     208        else if (pString->u16Size >= sizeof(pString->String.utf8[0]))
     209            pString->String.utf8[0] = 0;
    201210    }
    202211
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/info.c

    r51254 r51997  
    113113    if (Template->Length)
    114114    {
    115         ULONG ParsedPathSize, len;
    116 
    117         /* Calculate length required for parsed path. */
    118         ParsedPathSize = sizeof(SHFLSTRING) + DirectoryName->Length + Template->Length + 3 * sizeof(WCHAR);
     115        ULONG ParsedPathSize, cch;
     116
     117        /* Calculate size required for parsed path: dir + \ + template + 0. */
     118        ParsedPathSize = SHFLSTRING_HEADER_SIZE + Template->Length + sizeof(WCHAR);
     119        if (DirectoryName->Length)
     120            ParsedPathSize += DirectoryName->Length + sizeof(WCHAR);
    119121        Log(("VBOXSF: MrxQueryDirectory: ParsedPathSize = %d\n", ParsedPathSize));
    120122
     
    126128        }
    127129
    128         RtlZeroMemory(ParsedPath, ParsedPathSize);
    129130        if (!ShflStringInitBuffer(ParsedPath, ParsedPathSize))
    130131        {
     
    133134        }
    134135
    135         ParsedPath->u16Size = DirectoryName->Length + Template->Length + sizeof(WCHAR);
    136         ParsedPath->u16Length = ParsedPath->u16Size - sizeof(WCHAR); /* Without terminating null. */
    137 
    138         len = 0;
     136        cch = 0;
    139137        if (DirectoryName->Length)
    140138        {
    141139            /* Copy directory name into ParsedPath. */
    142140            RtlCopyMemory(ParsedPath->String.ucs2, DirectoryName->Buffer, DirectoryName->Length);
    143             len = DirectoryName->Length / sizeof(WCHAR);
     141            cch += DirectoryName->Length / sizeof(WCHAR);
    144142
    145143            /* Add terminating backslash. */
    146             ParsedPath->String.ucs2[len] = L'\\';
    147             len++;
    148             ParsedPath->u16Length += sizeof(WCHAR);
    149             ParsedPath->u16Size += sizeof(WCHAR);
    150         }
    151         RtlCopyMemory (&ParsedPath->String.ucs2[len], Template->Buffer, Template->Length);
     144            ParsedPath->String.ucs2[cch] = L'\\';
     145            cch++;
     146        }
     147
     148        RtlCopyMemory (&ParsedPath->String.ucs2[cch], Template->Buffer, Template->Length);
     149        cch += Template->Length / sizeof(WCHAR);
     150
     151        /* Add terminating nul. */
     152        ParsedPath->String.ucs2[cch] = 0;
     153
     154        /* cch is the number of chars without trailing nul. */
     155        ParsedPath->u16Length = (uint16_t)(cch * sizeof(WCHAR));
     156
     157        AssertMsg(ParsedPath->u16Length + sizeof(WCHAR) == ParsedPath->u16Size,
     158                  ("u16Length %d, u16Size %d\n", ParsedPath->u16Length, ParsedPath->u16Size));
    152159
    153160        Log(("VBOXSF: MrxQueryDirectory: ParsedPath = %.*ls\n",
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/net.c

    r51254 r51997  
    169169        int vboxRC;
    170170        PSHFLSTRING ParsedPath = 0;
    171         ULONG ParsedPathSize;
    172171
    173172        Log(("VBOXSF: MRxCreateVNetRoot: initialize NET_ROOT\n"));
     
    200199                 RootNameLength, RootNameLength / sizeof(WCHAR), pRootName));
    201200
    202             /* Calculate the length required for parsed path. */
    203             ParsedPathSize = sizeof(SHFLSTRING) + RootNameLength + sizeof(WCHAR);
    204             ParsedPath = (PSHFLSTRING)vbsfAllocNonPagedMem(ParsedPathSize);
    205             if (!ParsedPath)
     201            Status = vbsfShflStringFromUnicodeAlloc(&ParsedPath, pRootName, (uint16_t)RootNameLength);
     202            if (Status != STATUS_SUCCESS)
    206203            {
    207                 Status = STATUS_INSUFFICIENT_RESOURCES;
    208204                goto l_Exit;
    209205            }
    210             memset(ParsedPath, 0, ParsedPathSize);
    211             if (!ShflStringInitBuffer(ParsedPath, ParsedPathSize))
    212             {
    213                 vbsfFreeNonPagedMem(ParsedPath);
    214                 Status = STATUS_INSUFFICIENT_RESOURCES;
    215                 goto l_Exit;
    216             }
    217             ParsedPath->u16Length = ParsedPath->u16Size - sizeof(WCHAR); /* without terminating null */
    218             RtlCopyMemory(ParsedPath->String.ucs2, pRootName, ParsedPath->u16Length);
    219206
    220207            vboxRC = vboxCallMapFolder(&pDeviceExtension->hgcmClient, ParsedPath, &pNetRootExtension->map);
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/path.c

    r51254 r51997  
    248248    {
    249249        PSHFLSTRING ParsedPath;
    250         ULONG ParsedPathSize;
    251 
    252         /* Calculate length required for parsed path.
    253          */
    254         ParsedPathSize = sizeof(*ParsedPath) + (RemainingName->Length + sizeof(WCHAR));
    255         Log(("VBOXSF: vbsfProcessCreate: ParsedPathSize = %d\n", ParsedPathSize));
    256 
    257         ParsedPath = (PSHFLSTRING)vbsfAllocNonPagedMem(ParsedPathSize);
    258         if (!ParsedPath)
     250        Log(("VBOXSF: vbsfProcessCreate: RemainingName->Length = %d\n", RemainingName->Length));
     251
     252        Status = vbsfShflStringFromUnicodeAlloc(&ParsedPath, RemainingName->Buffer, RemainingName->Length);
     253        if (Status != STATUS_SUCCESS)
    259254        {
    260             Status = STATUS_INSUFFICIENT_RESOURCES;
    261255            goto failure;
    262256        }
    263257
    264         if (!ShflStringInitBuffer(ParsedPath, ParsedPathSize))
    265         {
    266             vbsfFreeNonPagedMem(ParsedPath);
    267             Status = STATUS_INSUFFICIENT_RESOURCES;
    268             goto failure;
    269         }
    270 
    271         ParsedPath->u16Length = ParsedPath->u16Size - sizeof(WCHAR); /* without terminating null */
    272         RtlCopyMemory (ParsedPath->String.ucs2, RemainingName->Buffer, ParsedPath->u16Length);
    273258        Log(("VBOXSF: ParsedPath: %.*ls\n",
    274259             ParsedPath->u16Length / sizeof(WCHAR), ParsedPath->String.ucs2));
     
    839824    int vboxRC;
    840825    PSHFLSTRING ParsedPath = NULL;
    841     ULONG ParsedPathSize;
    842826
    843827    Log(("VBOXSF: vbsfRemove: Delete %.*ls. open count = %d\n",
     
    848832        vbsfCloseFileHandle(pDeviceExtension, pNetRootExtension, pVBoxFobx);
    849833
    850     /* Calculate length required for parsed path. */
    851     ParsedPathSize = sizeof(SHFLSTRING) + RemainingName->Length + sizeof(WCHAR);
    852     Log(("VBOXSF: vbsfRemove: ParsedPathSize %d\n", ParsedPathSize));
    853 
    854     ParsedPath = (PSHFLSTRING)vbsfAllocNonPagedMem(ParsedPathSize);
    855     if (!ParsedPath)
    856         return STATUS_INSUFFICIENT_RESOURCES;
    857 
    858     if (!ShflStringInitBuffer(ParsedPath, ParsedPathSize))
    859     {
    860         vbsfFreeNonPagedMem(ParsedPath);
    861         return STATUS_INSUFFICIENT_RESOURCES;
    862     }
    863 
    864     Log(("VBOXSF: vbsfRemove: Setup ParsedPath\n"));
    865     ParsedPath->u16Length = ParsedPath->u16Size - sizeof(WCHAR); /* without terminating null */
    866     RtlCopyMemory(ParsedPath->String.ucs2, RemainingName->Buffer, ParsedPath->u16Length);
     834    Log(("VBOXSF: vbsfRemove: RemainingName->Length %d\n", RemainingName->Length));
     835    Status = vbsfShflStringFromUnicodeAlloc(&ParsedPath, RemainingName->Buffer, RemainingName->Length);
     836    if (Status != STATUS_SUCCESS)
     837        return Status;
    867838
    868839    /* Call host. */
     
    905876    int vboxRC;
    906877    PSHFLSTRING SrcPath = 0, DestPath = 0;
    907     ULONG ParsedPathSize, flags;
     878    ULONG flags;
    908879
    909880    Assert(FileInformationClass == FileRenameInformation);
     
    919890    SetFlag(pSrvOpen->Flags, SRVOPEN_FLAG_FILE_RENAMED);
    920891
    921     /* Calculate length required for destination path. */
    922     ParsedPathSize = sizeof(SHFLSTRING) + RenameInformation->FileNameLength + sizeof(WCHAR);
    923     Log(("VBOXSF: vbsfRename: ParsedPathSize = %d\n", ParsedPathSize));
    924 
    925     DestPath = (PSHFLSTRING)vbsfAllocNonPagedMem(ParsedPathSize);
    926     if (!DestPath)
    927         return STATUS_INSUFFICIENT_RESOURCES;
    928 
    929     RtlZeroMemory(DestPath, ParsedPathSize);
    930     if (!ShflStringInitBuffer(DestPath, ParsedPathSize))
    931     {
    932         vbsfFreeNonPagedMem(DestPath);
    933         return STATUS_INSUFFICIENT_RESOURCES;
    934     }
    935 
    936     Log(("VBOXSF: vbsfRename: Setting up destination path\n"));
    937     DestPath->u16Length = DestPath->u16Size - sizeof(WCHAR); /* without terminating null */
    938     RtlCopyMemory(DestPath->String.ucs2, RenameInformation->FileName, DestPath->u16Length);
     892    Log(("VBOXSF: vbsfRename: RenameInformation->FileNameLength = %d\n", RenameInformation->FileNameLength));
     893    Status = vbsfShflStringFromUnicodeAlloc(&DestPath, RenameInformation->FileName, (uint16_t)RenameInformation->FileNameLength);
     894    if (Status != STATUS_SUCCESS)
     895        return Status;
    939896
    940897    Log(("VBOXSF: vbsfRename: Destination path = %.*ls\n",
    941898         DestPath->u16Length / sizeof(WCHAR), &DestPath->String.ucs2[0]));
    942899
    943     /* Calculate length required for source path */
    944     ParsedPathSize = sizeof(*DestPath) + (RemainingName->Length + sizeof(WCHAR));
    945 
    946     Log(("VBOXSF: vbsfRename: ParsedPathSize = %d\n", ParsedPathSize));
    947 
    948     SrcPath = (PSHFLSTRING)vbsfAllocNonPagedMem(ParsedPathSize);
    949     if (!SrcPath)
     900    Log(("VBOXSF: vbsfRename: RemainingName->Length = %d\n", RemainingName->Length));
     901    Status = vbsfShflStringFromUnicodeAlloc(&SrcPath, RemainingName->Buffer, RemainingName->Length);
     902    if (Status != STATUS_SUCCESS)
    950903    {
    951904        vbsfFreeNonPagedMem(DestPath);
    952         return STATUS_INSUFFICIENT_RESOURCES;
    953     }
    954 
    955     RtlZeroMemory(SrcPath, ParsedPathSize);
    956     if (!ShflStringInitBuffer(SrcPath, ParsedPathSize))
    957     {
    958         vbsfFreeNonPagedMem(DestPath);
    959         vbsfFreeNonPagedMem(SrcPath);
    960         return STATUS_INSUFFICIENT_RESOURCES;
    961     }
    962 
    963     Log(("VBOXSF: vbsfRename: Setting up source path\n"));
    964 
    965     SrcPath->u16Length = SrcPath->u16Size - sizeof(WCHAR); /* without terminating null */
    966     RtlCopyMemory(SrcPath->String.ucs2, RemainingName->Buffer, SrcPath->u16Length);
     905        return Status;
     906    }
    967907
    968908    Log(("VBOXSF: vbsfRename: Source path = %.*ls\n",
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsf.c

    r51254 r51997  
    663663                    }
    664664
    665                     if (cbOut >= _MRX_MAX_DRIVE_LETTERS && !pu8Out)
     665                    if (cbOut >= _MRX_MAX_DRIVE_LETTERS && pu8Out)
    666666                    {
    667667                        BOOLEAN fLocked = FALSE;
     
    706706                        break;
    707707
    708                     if (cbOut >= _MRX_MAX_DRIVE_LETTERS && !pu8Out)
     708                    if (cbOut >= _MRX_MAX_DRIVE_LETTERS && pu8Out)
    709709                    {
    710710                        SHFLMAPPING mappings[_MRX_MAX_DRIVE_LETTERS];
     
    762762                    Log(("VBOXSF: MRxDevFcbXXXControlFile: IOCTL_MRX_VBOX_GETCONN: Looking up connection name and connections\n"));
    763763
    764                     if (cbConnectName > sizeof(WCHAR) && !pwcConnectName)
     764                    if (cbConnectName > sizeof(WCHAR) && pwcConnectName)
    765765                    {
    766766                        ULONG cbLocalConnectionName;
     
    829829                    int vboxRC;
    830830                    PSHFLSTRING pString;
    831                     uint32_t cbString;
    832831
    833832                    Log(("VBOXSF: MRxDevFcbXXXControlFile: IOCTL_MRX_VBOX_GETGLOBALCONN: Connection ID = %d, RemoteName = 0x%x, Len = %d\n",
    834833                         *pConnectId, pwcRemoteName, cbRemoteName));
    835834
    836                     cbString = sizeof(SHFLSTRING) + cbRemoteName;
    837                     pString = (PSHFLSTRING)vbsfAllocNonPagedMem(cbString);
    838                     if (!pString)
    839                     {
    840                         Status = STATUS_INSUFFICIENT_RESOURCES;
     835                    /* Allocate empty string where the host can store cbRemoteName bytes. */
     836                    Status = vbsfShflStringFromUnicodeAlloc(&pString, NULL, (uint16_t)cbRemoteName);
     837                    if (Status != STATUS_SUCCESS)
    841838                        break;
    842                     }
    843                     memset(pString, 0, cbString);
    844                     if (!ShflStringInitBuffer(pString, cbString))
    845                     {
    846                         vbsfFreeNonPagedMem(pString);
    847                         Status = STATUS_BAD_NETWORK_NAME;
    848                         break;
    849                     }
    850839
    851840                    vboxRC = vboxCallQueryMapName(&pDeviceExtension->hgcmClient,
    852841                                                  (*pConnectId) & ~0x80 /** @todo fix properly */,
    853                                                   pString, cbString);
     842                                                  pString, ShflStringSizeOfBuffer(pString));
    854843                    if (   vboxRC == VINF_SUCCESS
    855844                        && pString->u16Length < cbRemoteName)
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsfhlp.c

    r40986 r51997  
    434434}
    435435#endif
     436
     437/** Allocate and initialize a SHFLSTRING from a UNICODE string.
     438 *
     439 *  @param ppShflString Where to store the pointer to the allocated SHFLSTRING structure.
     440 *                      The structure must be deallocated with vbsfFreeNonPagedMem.
     441 *  @param pwc          The UNICODE string. If NULL then SHFL is only allocated.
     442 *  @param cb           Size of the UNICODE string in bytes without the trailing nul.
     443 *
     444 *  @return Status code.
     445 */
     446NTSTATUS vbsfShflStringFromUnicodeAlloc(PSHFLSTRING *ppShflString, const WCHAR *pwc, uint16_t cb)
     447{
     448    NTSTATUS Status = STATUS_SUCCESS;
     449
     450    PSHFLSTRING pShflString;
     451    ULONG ulShflStringSize;
     452
     453    /* Calculate length required for the SHFL structure: header + chars + nul. */
     454    ulShflStringSize = SHFLSTRING_HEADER_SIZE + cb + sizeof(WCHAR);
     455    pShflString = (PSHFLSTRING)vbsfAllocNonPagedMem(ulShflStringSize);
     456    if (pShflString)
     457    {
     458        if (ShflStringInitBuffer(pShflString, ulShflStringSize))
     459        {
     460            if (pwc)
     461            {
     462                RtlCopyMemory(pShflString->String.ucs2, pwc, cb);
     463                pShflString->String.ucs2[cb / sizeof(WCHAR)] = 0;
     464                pShflString->u16Length = cb; /* without terminating null */
     465                AssertMsg(pShflString->u16Length + sizeof(WCHAR) == pShflString->u16Size,
     466                          ("u16Length %d, u16Size %d\n", pShflString->u16Length, pShflString->u16Size));
     467            }
     468            else
     469            {
     470                RtlZeroMemory(pShflString->String.ucs2, cb + sizeof(WCHAR));
     471                pShflString->u16Length = 0; /* without terminating null */
     472                AssertMsg(pShflString->u16Size >= sizeof(WCHAR),
     473                          ("u16Size %d\n", pShflString->u16Size));
     474            }
     475
     476            *ppShflString = pShflString;
     477        }
     478        else
     479        {
     480            vbsfFreeNonPagedMem(pShflString);
     481            Status = STATUS_INSUFFICIENT_RESOURCES;
     482        }
     483    }
     484    else
     485    {
     486        Status = STATUS_INSUFFICIENT_RESOURCES;
     487    }
     488
     489    return Status;
     490}
  • trunk/src/VBox/Additions/WINNT/SharedFolders/driver/vbsfhlp.h

    r40319 r51997  
    6363#endif
    6464
     65NTSTATUS vbsfShflStringFromUnicodeAlloc(PSHFLSTRING *ppShflString, const WCHAR *pwc, uint16_t cb);
     66
    6567#endif /* __VBSFHLP__H */
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibSharedFolders.cpp

    r51254 r51997  
    245245
    246246    int         rc;
    247     uint32_t    cbString = sizeof(SHFLSTRING) + SHFL_MAX_LEN;
     247    uint32_t    cbString = SHFLSTRING_HEADER_SIZE + SHFL_MAX_LEN;
    248248    PSHFLSTRING pString = (PSHFLSTRING)RTMemAlloc(cbString);
    249249    if (pString)
    250250    {
    251         RT_ZERO(*pString);
    252         if (!ShflStringInitBuffer(pString, SHFL_MAX_LEN))
     251        if (!ShflStringInitBuffer(pString, cbString))
    253252        {
    254253            RTMemFree(pString);
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r51925 r51997  
    78377837    if (cbString >= UINT16_MAX)
    78387838        return setError(E_INVALIDARG, tr("The name is too long"));
    7839     pFolderName = (SHFLSTRING*)RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
     7839    pFolderName = (SHFLSTRING*)RTMemAllocZ(SHFLSTRING_HEADER_SIZE + cbString);
    78407840    Assert(pFolderName);
    78417841    memcpy(pFolderName->String.ucs2, bstrHostPath.raw(), cbString);
     
    78467846    parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
    78477847    parms[0].u.pointer.addr = pFolderName;
    7848     parms[0].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
     7848    parms[0].u.pointer.size = ShflStringSizeOfBuffer(pFolderName);
    78497849
    78507850    cbString = (bstrName.length() + 1) * sizeof(RTUTF16);
     
    78547854        return setError(E_INVALIDARG, tr("The host path is too long"));
    78557855    }
    7856     pMapName = (SHFLSTRING*)RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
     7856    pMapName = (SHFLSTRING*)RTMemAllocZ(SHFLSTRING_HEADER_SIZE + cbString);
    78577857    Assert(pMapName);
    78587858    memcpy(pMapName->String.ucs2, bstrName.raw(), cbString);
     
    78637863    parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
    78647864    parms[1].u.pointer.addr = pMapName;
    7865     parms[1].u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
     7865    parms[1].u.pointer.size = ShflStringSizeOfBuffer(pMapName);
    78667866
    78677867    parms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
     
    79177917    if (cbString >= UINT16_MAX)
    79187918        return setError(E_INVALIDARG, tr("The name is too long"));
    7919     pMapName = (SHFLSTRING *) RTMemAllocZ(sizeof(SHFLSTRING) + cbString);
     7919    pMapName = (SHFLSTRING *) RTMemAllocZ(SHFLSTRING_HEADER_SIZE + cbString);
    79207920    Assert(pMapName);
    79217921    memcpy(pMapName->String.ucs2, bstrName.raw(), cbString);
     
    79267926    parms.type = VBOX_HGCM_SVC_PARM_PTR;
    79277927    parms.u.pointer.addr = pMapName;
    7928     parms.u.pointer.size = sizeof(SHFLSTRING) + (uint16_t)cbString;
     7928    parms.u.pointer.size = ShflStringSizeOfBuffer(pMapName);
    79297929
    79307930    int vrc = m_pVMMDev->hgcmHostCall("VBoxSharedFolders",
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette