Ignore:
Timestamp:
Sep 5, 2001, 3:19:02 PM (24 years ago)
Author:
bird
Message:

Added $Id:$ keyword.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/oleaut32/safearray.c

    r6507 r6648  
     1/* $Id: safearray.c,v 1.3 2001-09-05 13:19:01 bird Exp $ */
    12/*************************************************************************
    23 * OLE Automation
     
    2526
    2627/* Localy used methods */
    27 static INT 
     28static INT
    2829endOfDim(LONG *coor, SAFEARRAYBOUND *mat, LONG dim, LONG realDim);
    2930
    30 static ULONG   
     31static ULONG
    3132calcDisplacement(LONG *coor, SAFEARRAYBOUND *mat, LONG dim);
    3233
    33 static BOOL 
     34static BOOL
    3435isPointer(USHORT feature);
    3536
    36 static INT   
     37static INT
    3738getFeatures(VARTYPE vt);
    3839
    39 static BOOL 
     40static BOOL
    4041validCoordinate(LONG *coor, SAFEARRAY *psa);
    4142
    42 static BOOL 
     43static BOOL
    4344resizeSafeArray(SAFEARRAY *psa, LONG lDelta);
    4445
    45 static BOOL 
     46static BOOL
    4647validArg(SAFEARRAY *psa);
    4748
    48 static ULONG   
     49static ULONG
    4950getArraySize(SAFEARRAY *psa);
    5051
    51 static HRESULT 
     52static HRESULT
    5253duplicateData(SAFEARRAY *psa, SAFEARRAY **ppsaOut);
    5354
     
    5960{
    6061  /* this is taken from wtypes.h.  Only [S]es are supported by the SafeArray */
    61 VARTYPE_NOT_SUPPORTED,  /* VT_EMPTY    [V]   [P]    nothing                     */
    62 VARTYPE_NOT_SUPPORTED,  /* VT_NULL     [V]   [P]    SQL style Nul       */
    63 2,                          /* VT_I2       [V][T][P][S] 2 byte signed int */
    64 4,                          /* VT_I4       [V][T][P][S] 4 byte signed int */
    65 4,                          /* VT_R4       [V][T][P][S] 4 byte real     */
    66 8,                          /* VT_R8       [V][T][P][S] 8 byte real     */
     62VARTYPE_NOT_SUPPORTED,  /* VT_EMPTY    [V]   [P]    nothing         */
     63VARTYPE_NOT_SUPPORTED,  /* VT_NULL     [V]   [P]    SQL style Nul   */
     642,                      /* VT_I2       [V][T][P][S] 2 byte signed int */
     654,                      /* VT_I4       [V][T][P][S] 4 byte signed int */
     664,                      /* VT_R4       [V][T][P][S] 4 byte real */
     678,                      /* VT_R8       [V][T][P][S] 8 byte real */
    67688,                      /* VT_CY       [V][T][P][S] currency */
    68 8,                          /* VT_DATE     [V][T][P][S] date */
    69 4,                          /* VT_BSTR     [V][T][P][S] OLE Automation string*/
    70 4,                          /* VT_DISPATCH [V][T][P][S] IDispatch *     */
    71 4,                      /* VT_ERROR    [V][T]   [S] SCODE       */
    72 4,                          /* VT_BOOL     [V][T][P][S] True=-1, False=0*/
    73 24,                     /* VT_VARIANT  [V][T][P][S] VARIANT *   */
    74 4,                          /* VT_UNKNOWN  [V][T]   [S] IUnknown * */
    75 16,                         /* VT_DECIMAL  [V][T]   [S] 16 byte fixed point     */
     698,                      /* VT_DATE     [V][T][P][S] date */
     704,                      /* VT_BSTR     [V][T][P][S] OLE Automation string*/
     714,                      /* VT_DISPATCH [V][T][P][S] IDispatch * */
     724,                      /* VT_ERROR    [V][T]   [S] SCODE   */
     734,                      /* VT_BOOL     [V][T][P][S] True=-1, False=0*/
     7424,                     /* VT_VARIANT  [V][T][P][S] VARIANT *   */
     754,                      /* VT_UNKNOWN  [V][T]   [S] IUnknown * */
     7616,                     /* VT_DECIMAL  [V][T]   [S] 16 byte fixed point */
    7677VARTYPE_NOT_SUPPORTED,                         /* no VARTYPE here..... */
    77 VARTYPE_NOT_SUPPORTED,  /* VT_I1          [T]       signed char */
    78 1,                          /* VT_UI1      [V][T][P][S] unsigned char                   */
    79 VARTYPE_NOT_SUPPORTED,  /* VT_UI2         [T][P]    unsigned short      */
    80 VARTYPE_NOT_SUPPORTED,  /* VT_UI4         [T][P]    unsigned short      */
    81 VARTYPE_NOT_SUPPORTED,  /* VT_I8          [T][P]    signed 64-bit int                   */
    82 VARTYPE_NOT_SUPPORTED,  /* VT_UI8         [T][P]    unsigned 64-bit int         */
    83 VARTYPE_NOT_SUPPORTED,  /* VT_INT         [T]       signed machine int          */
    84 VARTYPE_NOT_SUPPORTED,  /* VT_UINT        [T]       unsigned machine int        */
    85 VARTYPE_NOT_SUPPORTED,  /* VT_VOID        [T]       C style void                        */
    86 VARTYPE_NOT_SUPPORTED,  /* VT_HRESULT     [T]       Standard return type        */
    87 VARTYPE_NOT_SUPPORTED,  /* VT_PTR         [T]       pointer type                        */
    88 VARTYPE_NOT_SUPPORTED,  /* VT_SAFEARRAY   [T]       (use VT_ARRAY in VARIANT)*/
    89 VARTYPE_NOT_SUPPORTED,  /* VT_CARRAY      [T]       C style array                       */
    90 VARTYPE_NOT_SUPPORTED,  /* VT_USERDEFINED [T]       user defined type                   */
    91 VARTYPE_NOT_SUPPORTED,  /* VT_LPSTR       [T][P]    null terminated string      */
    92 VARTYPE_NOT_SUPPORTED,  /* VT_LPWSTR      [T][P]    wide null term string               */
    93 VARTYPE_NOT_SUPPORTED,  /* VT_FILETIME       [P]    FILETIME                    */
    94 VARTYPE_NOT_SUPPORTED,  /* VT_BLOB           [P]    Length prefixed bytes */
    95 VARTYPE_NOT_SUPPORTED,  /* VT_STREAM         [P]    Name of stream follows              */
    96 VARTYPE_NOT_SUPPORTED,  /* VT_STORAGE        [P]    Name of storage follows     */
    97 VARTYPE_NOT_SUPPORTED,  /* VT_STREAMED_OBJECT[P]    Stream contains an object*/
    98 VARTYPE_NOT_SUPPORTED,  /* VT_STORED_OBJECT  [P]    Storage contains object*/
    99 VARTYPE_NOT_SUPPORTED,  /* VT_BLOB_OBJECT    [P]    Blob contains an object*/
    100 VARTYPE_NOT_SUPPORTED,  /* VT_CF             [P]    Clipboard format                    */
    101 VARTYPE_NOT_SUPPORTED,  /* VT_CLSID          [P]    A Class ID                  */
    102 VARTYPE_NOT_SUPPORTED,  /* VT_VECTOR         [P]    simple counted array                */
    103 VARTYPE_NOT_SUPPORTED,  /* VT_ARRAY    [V]          SAFEARRAY*                  */
    104 VARTYPE_NOT_SUPPORTED   /* VT_BYREF    [V]          void* for local use */
     78VARTYPE_NOT_SUPPORTED,  /* VT_I1          [T]       signed char */
     791,                      /* VT_UI1      [V][T][P][S] unsigned char           */
     80VARTYPE_NOT_SUPPORTED,  /* VT_UI2         [T][P]    unsigned short  */
     81VARTYPE_NOT_SUPPORTED,  /* VT_UI4         [T][P]    unsigned short  */
     82VARTYPE_NOT_SUPPORTED,  /* VT_I8          [T][P]    signed 64-bit int           */
     83VARTYPE_NOT_SUPPORTED,  /* VT_UI8         [T][P]    unsigned 64-bit int     */
     84VARTYPE_NOT_SUPPORTED,  /* VT_INT         [T]       signed machine int      */
     85VARTYPE_NOT_SUPPORTED,  /* VT_UINT        [T]       unsigned machine int    */
     86VARTYPE_NOT_SUPPORTED,  /* VT_VOID        [T]       C style void            */
     87VARTYPE_NOT_SUPPORTED,  /* VT_HRESULT     [T]       Standard return type    */
     88VARTYPE_NOT_SUPPORTED,  /* VT_PTR         [T]       pointer type            */
     89VARTYPE_NOT_SUPPORTED,  /* VT_SAFEARRAY   [T]       (use VT_ARRAY in VARIANT)*/
     90VARTYPE_NOT_SUPPORTED,  /* VT_CARRAY      [T]       C style array           */
     91VARTYPE_NOT_SUPPORTED,  /* VT_USERDEFINED [T]       user defined type           */
     92VARTYPE_NOT_SUPPORTED,  /* VT_LPSTR       [T][P]    null terminated string  */
     93VARTYPE_NOT_SUPPORTED,  /* VT_LPWSTR      [T][P]    wide null term string       */
     94VARTYPE_NOT_SUPPORTED,  /* VT_FILETIME       [P]    FILETIME            */
     95VARTYPE_NOT_SUPPORTED,  /* VT_BLOB           [P]    Length prefixed bytes */
     96VARTYPE_NOT_SUPPORTED,  /* VT_STREAM         [P]    Name of stream follows      */
     97VARTYPE_NOT_SUPPORTED,  /* VT_STORAGE        [P]    Name of storage follows */
     98VARTYPE_NOT_SUPPORTED,  /* VT_STREAMED_OBJECT[P]    Stream contains an object*/
     99VARTYPE_NOT_SUPPORTED,  /* VT_STORED_OBJECT  [P]    Storage contains object*/
     100VARTYPE_NOT_SUPPORTED,  /* VT_BLOB_OBJECT    [P]    Blob contains an object*/
     101VARTYPE_NOT_SUPPORTED,  /* VT_CF             [P]    Clipboard format            */
     102VARTYPE_NOT_SUPPORTED,  /* VT_CLSID          [P]    A Class ID          */
     103VARTYPE_NOT_SUPPORTED,  /* VT_VECTOR         [P]    simple counted array        */
     104VARTYPE_NOT_SUPPORTED,  /* VT_ARRAY    [V]          SAFEARRAY*          */
     105VARTYPE_NOT_SUPPORTED   /* VT_BYREF    [V]          void* for local use */
    105106};
    106107
     
    109110
    110111/*************************************************************************
    111  *              SafeArrayAllocDescriptor
     112 *      SafeArrayAllocDescriptor
    112113 * Allocate the appropriate amount of memory for the SafeArray descriptor
    113114 */
    114 HRESULT WINAPI SafeArrayAllocDescriptor( 
    115   UINT    cDims, 
    116   SAFEARRAY **ppsaOut) 
     115HRESULT WINAPI SafeArrayAllocDescriptor(
     116  UINT    cDims,
     117  SAFEARRAY **ppsaOut)
    117118{
    118119  SAFEARRAYBOUND *sab;
     
    124125
    125126  /* Allocate memory for SAFEARRAY struc */
    126   if(( (*ppsaOut)=HeapAlloc( 
     127  if(( (*ppsaOut)=HeapAlloc(
    127128        GetProcessHeap(), HEAP_ZERO_MEMORY, allocSize)) == NULL){
    128129    return(E_UNEXPECTED);
     
    134135
    135136/*************************************************************************
    136  *              SafeArrayAllocData
     137 *      SafeArrayAllocData
    137138 * Allocate the appropriate amount of data for the SafeArray data
    138139 */
    139140HRESULT WINAPI SafeArrayAllocData(
    140   SAFEARRAY *psa) 
     141  SAFEARRAY *psa)
    141142{
    142143  ULONG  ulWholeArraySize;   /* to store the size of the whole thing */
     
    144145  dprintf(("SafeArrayAllocData %x", psa));
    145146
    146   if(! validArg(psa)) 
     147  if(! validArg(psa))
    147148    return E_INVALIDARG;
    148149
     
    150151
    151152  /* Allocate memory for the data itself */
    152   if((psa->pvData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
     153  if((psa->pvData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
    153154        psa->cbElements*ulWholeArraySize)) == NULL)
    154155    return(E_UNEXPECTED);
    155156
    156   TRACE("SafeArray: %lu bytes allocated for data at %p (%lu objects).\n", 
     157  TRACE("SafeArray: %lu bytes allocated for data at %p (%lu objects).\n",
    157158    psa->cbElements*ulWholeArraySize, psa->pvData, ulWholeArraySize);
    158159
     
    161162
    162163/*************************************************************************
    163  *              SafeArrayCreate
    164  * Create a SafeArray object by encapsulating AllocDescriptor and AllocData 
     164 *      SafeArrayCreate
     165 * Create a SafeArray object by encapsulating AllocDescriptor and AllocData
    165166 */
    166167SAFEARRAY* WINAPI SafeArrayCreate(
    167   VARTYPE        vt, 
    168   UINT         cDims, 
     168  VARTYPE        vt,
     169  UINT         cDims,
    169170  SAFEARRAYBOUND *rgsabound)
    170171{
     
    184185    return NULL;
    185186
    186   /* setup data members... */ 
     187  /* setup data members... */
    187188  psa->cDims     = cDims;
    188189  psa->fFeatures = getFeatures(vt);
     
    197198  }
    198199
    199   /* allocate memory for the data... */ 
     200  /* allocate memory for the data... */
    200201  if( FAILED( hRes = SafeArrayAllocData(psa))) {
    201     SafeArrayDestroyDescriptor(psa); 
     202    SafeArrayDestroyDescriptor(psa);
    202203    ERR("() : Failed to allocate the Safe Array data\n");
    203204    return NULL;
    204205  }
    205206
    206   return(psa); 
    207 }
    208 
    209 /*************************************************************************
    210  *              SafeArrayDestroyDescriptor
     207  return(psa);
     208}
     209
     210/*************************************************************************
     211 *      SafeArrayDestroyDescriptor
    211212 * Frees the memory associated with the descriptor.
    212213 */
     
    217218
    218219  /* Check for lockness before to free... */
    219   if(psa->cLocks > 0) 
     220  if(psa->cLocks > 0)
    220221    return DISP_E_ARRAYISLOCKED;
    221222
    222223  /* The array is unlocked, then, deallocate memory */
    223   if(HeapFree( GetProcessHeap(), 0, psa) == FALSE) 
     224  if(HeapFree( GetProcessHeap(), 0, psa) == FALSE)
    224225    return E_UNEXPECTED;
    225  
     226
    226227  return(S_OK);
    227228}
     
    229230
    230231/*************************************************************************
    231  *              SafeArrayLock
     232 *      SafeArrayLock
    232233 * Increment the lock counter
    233234 *
    234235 * Doc says (MSDN Library ) that psa->pvData should be made available (!= NULL)
    235  * only when psa->cLocks is > 0... I don't get it since pvData is allocated 
    236  * before the array is locked, therefore 
     236 * only when psa->cLocks is > 0... I don't get it since pvData is allocated
     237 * before the array is locked, therefore
    237238 */
    238239HRESULT WINAPI SafeArrayLock(
     
    241242  dprintf(("SafeArrayLock %x", psa));
    242243
    243   if(! validArg(psa))     
     244  if(! validArg(psa))
    244245    return E_INVALIDARG;
    245246
     
    250251
    251252/*************************************************************************
    252  *              SafeArrayUnlock
     253 *      SafeArrayUnlock
    253254 * Decrement the lock counter
    254255 */
     
    258259  dprintf(("SafeArrayUnlock %x", psa));
    259260
    260   if(! validArg(psa)) 
    261     return E_INVALIDARG;
    262 
    263   if (psa->cLocks > 0) 
     261  if(! validArg(psa))
     262    return E_INVALIDARG;
     263
     264  if (psa->cLocks > 0)
    264265    psa->cLocks--;
    265266
     
    269270
    270271/*************************************************************************
    271  *              SafeArrayPutElement
     272 *      SafeArrayPutElement
    272273 * Set the data at the given coordinate
    273274 */
    274275HRESULT WINAPI SafeArrayPutElement(
    275   SAFEARRAY *psa, 
    276   LONG      *rgIndices, 
     276  SAFEARRAY *psa,
     277  LONG      *rgIndices,
    277278  void      *pv)
    278279{
    279   ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to 
     280  ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to
    280281                                         the desired one... */
    281282  PVOID elementStorageAddress = NULL; /* Adress to store the data */
     
    285286
    286287  /* Validate the index given */
    287   if(! validCoordinate(rgIndices, psa)) 
     288  if(! validCoordinate(rgIndices, psa))
    288289    return DISP_E_BADINDEX;
    289290  if(! validArg(psa))
     
    294295    /* Figure out the number of items to skip */
    295296    stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
    296  
     297
    297298    /* Figure out the number of byte to skip ... */
    298299    elementStorageAddress = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
    299  
     300
    300301    if(isPointer(psa->fFeatures)) { /* increment ref count for this pointer */
    301302
    302       *((VOID**)elementStorageAddress) = *(VOID**)pv; 
     303      *((VOID**)elementStorageAddress) = *(VOID**)pv;
    303304      IUnknown_AddRef( *(IUnknown**)pv);
    304305
    305     } else { 
     306    } else {
    306307
    307308      if(psa->fFeatures == FADF_BSTR) { /* Create a new object */
    308309
    309310        if((pbstrReAllocStr = SysAllocString( (OLECHAR*)pv )) == NULL) {
    310           SafeArrayUnlock(psa); 
     311          SafeArrayUnlock(psa);
    311312          return E_OUTOFMEMORY;
    312         } else 
     313        } else
    313314          *((BSTR*)elementStorageAddress) = pbstrReAllocStr;
    314315
     
    323324
    324325  TRACE("SafeArray: item put at adress %p.\n",elementStorageAddress);
    325   return SafeArrayUnlock(psa); 
    326 }
    327 
    328 
    329 /*************************************************************************
    330  *              SafeArrayGetElement
     326  return SafeArrayUnlock(psa);
     327}
     328
     329
     330/*************************************************************************
     331 *      SafeArrayGetElement
    331332 * Return the data element corresponding the the given coordinate
    332333 */
    333334HRESULT WINAPI SafeArrayGetElement(
    334   SAFEARRAY *psa, 
    335   LONG      *rgIndices, 
     335  SAFEARRAY *psa,
     336  LONG      *rgIndices,
    336337  void      *pv)
    337338{
    338   ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to 
     339  ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to
    339340                                         the desired one... */
    340341  PVOID elementStorageAddress = NULL; /* Adress to store the data */
     
    343344  dprintf(("SafeArrayGetElement %x %x %x", psa, rgIndices, pv));
    344345
    345   if(! validArg(psa)) 
    346     return E_INVALIDARG;
    347  
     346  if(! validArg(psa))
     347    return E_INVALIDARG;
     348
    348349  if(! validCoordinate(rgIndices, psa)) /* Validate the index given */
    349350    return(DISP_E_BADINDEX);
     
    353354    /* Figure out the number of items to skip */
    354355    stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
    355  
     356
    356357    /* Figure out the number of byte to skip ... */
    357358    elementStorageAddress = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
    358  
     359
    359360    if( psa->fFeatures == FADF_BSTR) {           /* reallocate the obj */
    360       if( (pbstrReturnedStr = 
     361      if( (pbstrReturnedStr =
    361362          SysAllocString( *(OLECHAR**)elementStorageAddress )) == NULL) {
    362363        SafeArrayUnlock(psa);
    363364        return E_OUTOFMEMORY;
    364       } else 
    365         *((BSTR*)pv) = pbstrReturnedStr; 
    366        
     365      } else
     366        *((BSTR*)pv) = pbstrReturnedStr;
     367
    367368    } else if( isPointer(psa->fFeatures) )       /* simply copy the pointer */
    368        pv = *((PVOID*)elementStorageAddress); 
     369       pv = *((PVOID*)elementStorageAddress);
    369370    else                                         /* copy the bytes */
    370371      memcpy(pv, elementStorageAddress, SafeArrayGetElemsize(psa) );
     
    375376  }
    376377
    377   return( SafeArrayUnlock(psa) ); 
    378 }
    379 
    380 /*************************************************************************
    381  *              SafeArrayGetUBound
     378  return( SafeArrayUnlock(psa) );
     379}
     380
     381/*************************************************************************
     382 *      SafeArrayGetUBound
    382383 * return the UP bound for a given array dimension
    383384 */
    384385HRESULT WINAPI SafeArrayGetUBound(
    385   SAFEARRAY *psa, 
     386  SAFEARRAY *psa,
    386387  UINT    nDim,
    387388  LONG      *plUbound)
     
    390391  dprintf(("SafeArrayGetUBound %x %x %x", psa, nDim, plUbound));
    391392
    392   if(! validArg(psa))   
    393     return E_INVALIDARG;
    394 
    395   if(nDim > psa->cDims) 
     393  if(! validArg(psa))
     394    return E_INVALIDARG;
     395
     396  if(nDim > psa->cDims)
    396397    return DISP_E_BADINDEX;
    397398
    398   *plUbound = psa->rgsabound[nDim-1].lLbound + 
     399  *plUbound = psa->rgsabound[nDim-1].lLbound +
    399400              psa->rgsabound[nDim-1].cElements - 1;
    400401
     
    403404
    404405/*************************************************************************
    405  *              SafeArrayGetLBound
    406  * Return the LO bound for a given array dimension 
     406 *      SafeArrayGetLBound
     407 * Return the LO bound for a given array dimension
    407408 */
    408409HRESULT WINAPI SafeArrayGetLBound(
    409410  SAFEARRAY *psa,
    410   UINT    nDim, 
     411  UINT    nDim,
    411412  LONG      *plLbound)
    412413{
    413   if(! validArg(psa))   
    414     return E_INVALIDARG;
    415 
    416   if(nDim > psa->cDims) 
     414  if(! validArg(psa))
     415    return E_INVALIDARG;
     416
     417  if(nDim > psa->cDims)
    417418    return DISP_E_BADINDEX;
    418419
     
    422423
    423424/*************************************************************************
    424  *              SafeArrayGetDim
     425 *      SafeArrayGetDim
    425426 * returns the number of dimension in the array
    426427 */
    427428UINT WINAPI SafeArrayGetDim(
    428429  SAFEARRAY * psa)
    429 { 
     430{
    430431  /*
    431432   * A quick test in Windows shows that the behavior here for an invalid
    432433   * pointer is to return 0.
    433434   */
    434   if(! validArg(psa)) 
     435  if(! validArg(psa))
    435436    return 0;
    436437
     
    439440
    440441/*************************************************************************
    441  *              SafeArrayGetElemsize
     442 *      SafeArrayGetElemsize
    442443 * Return the size of the element in the array
    443444 */
    444445UINT WINAPI SafeArrayGetElemsize(
    445446  SAFEARRAY * psa)
    446 { 
     447{
    447448  /*
    448449   * A quick test in Windows shows that the behavior here for an invalid
    449450   * pointer is to return 0.
    450451   */
    451   if(! validArg(psa)) 
     452  if(! validArg(psa))
    452453    return 0;
    453454
     
    456457
    457458/*************************************************************************
    458  *              SafeArrayAccessData
    459  * increment the access count and return the data 
     459 *      SafeArrayAccessData
     460 * increment the access count and return the data
    460461 */
    461462HRESULT WINAPI SafeArrayAccessData(
    462   SAFEARRAY *psa, 
     463  SAFEARRAY *psa,
    463464  void      **ppvData)
    464 { 
     465{
    465466  HRESULT hRes;
    466467
    467   if(! validArg(psa)) 
     468  if(! validArg(psa))
    468469    return E_INVALIDARG;
    469470
     
    471472
    472473  switch (hRes) {
    473     case S_OK: 
     474    case S_OK:
    474475      (*ppvData) = psa->pvData;
    475476      break;
     
    478479      return E_INVALIDARG;
    479480  }
    480  
     481
    481482  return S_OK;
    482483}
     
    484485
    485486/*************************************************************************
    486  *              SafeArrayUnaccessData
     487 *      SafeArrayUnaccessData
    487488 * Decrement the access count
    488489 */
    489490HRESULT WINAPI SafeArrayUnaccessData(
    490491  SAFEARRAY * psa)
    491 { 
    492   if(! validArg(psa)) 
     492{
     493  if(! validArg(psa))
    493494    return E_INVALIDARG;
    494495
     
    496497}
    497498
    498 /************************************************************************ 
    499  *              SafeArrayPtrOfIndex
     499/************************************************************************
     500 *      SafeArrayPtrOfIndex
    500501 * Return a pointer to the element at rgIndices
    501502 */
    502503HRESULT WINAPI SafeArrayPtrOfIndex(
    503   SAFEARRAY *psa, 
    504   LONG      *rgIndices, 
     504  SAFEARRAY *psa,
     505  LONG      *rgIndices,
    505506  void      **ppvData)
    506 { 
    507   ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to 
     507{
     508  ULONG stepCountInSAData     = 0;    /* Number of array item to skip to get to
    508509                                         the desired one... */
    509510
    510   if(! validArg(psa))                   
    511     return E_INVALIDARG;
    512 
    513   if(! validCoordinate(rgIndices, psa)) 
     511  if(! validArg(psa))
     512    return E_INVALIDARG;
     513
     514  if(! validCoordinate(rgIndices, psa))
    514515    return DISP_E_BADINDEX;
    515516
    516517  /* Figure out the number of items to skip */
    517518  stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
    518  
     519
    519520  *ppvData = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
    520521
     
    522523}
    523524
    524 /************************************************************************ 
    525  *              SafeArrayDestroyData
     525/************************************************************************
     526 *      SafeArrayDestroyData
    526527 * Frees the memory data bloc
    527528 */
    528529HRESULT WINAPI SafeArrayDestroyData(
    529530  SAFEARRAY *psa)
    530 { 
     531{
    531532  HRESULT  hRes;
    532533  ULONG    ulWholeArraySize; /* count spot in array  */
     
    535536  BSTR   bstr;
    536537
    537   if(! validArg(psa)) 
    538     return E_INVALIDARG;
    539 
    540   if(psa->cLocks > 0) 
     538  if(! validArg(psa))
     539    return E_INVALIDARG;
     540
     541  if(psa->cLocks > 0)
    541542    return DISP_E_ARRAYISLOCKED;
    542543
     
    546547
    547548    for(ulDataIter=0; ulDataIter < ulWholeArraySize; ulDataIter++) {
    548       punk = *(IUnknown**)((char *) psa->pvData+(ulDataIter*(psa->cbElements)));       
    549 
    550       if( punk != NULL) 
     549      punk = *(IUnknown**)((char *) psa->pvData+(ulDataIter*(psa->cbElements)));
     550
     551      if( punk != NULL)
    551552        IUnknown_Release(punk);
    552553    }
     
    557558      bstr = *(BSTR*)((char *) psa->pvData+(ulDataIter*(psa->cbElements)));
    558559
    559       if( bstr != NULL) 
     560      if( bstr != NULL)
    560561        SysFreeString( bstr );
    561562    }
    562563  }
    563      
    564   /* check if this array is a Vector, in which case do not free the data 
     564
     565  /* check if this array is a Vector, in which case do not free the data
    565566     block since it has been allocated by AllocDescriptor and therefore
    566567     deserve to be freed by DestroyDescriptor */
     
    573574    psa->pvData = NULL;
    574575  }
    575  
     576
    576577  return S_OK;
    577578}
    578579
    579 /************************************************************************ 
    580  *              SafeArrayCopyData
     580/************************************************************************
     581 *      SafeArrayCopyData
    581582 * Copy the psaSource's data block into psaTarget if dimension and size
    582583 * permits it.
     
    585586  SAFEARRAY *psaSource,
    586587  SAFEARRAY **psaTarget)
    587 { 
     588{
    588589  USHORT   cDimCount;        /* looper */
    589590  LONG     lDelta;           /* looper */
    590   IUnknown *punk;   
     591  IUnknown *punk;
    591592  ULONG    ulWholeArraySize; /* Number of item in SA */
    592593  BSTR   bstr;
    593594
    594   if(! (validArg(psaSource) && validArg(*psaTarget)) ) 
     595  if(! (validArg(psaSource) && validArg(*psaTarget)) )
    595596    return E_INVALIDARG;
    596597
     
    598599    return E_INVALIDARG;
    599600
    600   ulWholeArraySize = getArraySize(psaSource); 
     601  ulWholeArraySize = getArraySize(psaSource);
    601602
    602603  /* The two arrays boundaries must be of same lenght */
    603604  for(cDimCount=0;cDimCount < psaSource->cDims; cDimCount++)
    604     if( psaSource->rgsabound[cDimCount].cElements != 
     605    if( psaSource->rgsabound[cDimCount].cElements !=
    605606      (*psaTarget)->rgsabound[cDimCount].cElements)
    606607      return E_INVALIDARG;
    607608
    608   if( isPointer((*psaTarget)->fFeatures) ) {         /* the target contains ptr 
     609  if( isPointer((*psaTarget)->fFeatures) ) {         /* the target contains ptr
    609610                                                        that must be released */
    610611    for(lDelta=0;lDelta < ulWholeArraySize; lDelta++) {
     
    612613        ((char *) (*psaTarget)->pvData + (lDelta * (*psaTarget)->cbElements));
    613614
    614       if( punk != NULL) 
     615      if( punk != NULL)
    615616        IUnknown_Release(punk);
    616617    }
    617618
    618619  } else if( (*psaTarget)->fFeatures & FADF_BSTR) {  /* the target contain BSTR
    619                                                         that must be freed */ 
     620                                                        that must be freed */
    620621    for(lDelta=0;lDelta < ulWholeArraySize; lDelta++) {
    621       bstr = 
     622      bstr =
    622623        *(BSTR*)((char *) (*psaTarget)->pvData + (lDelta * (*psaTarget)->cbElements));
    623624
    624       if( bstr != NULL) 
     625      if( bstr != NULL)
    625626        SysFreeString( bstr );
    626627    }
     
    630631}
    631632
    632 /************************************************************************ 
    633  *              SafeArrayDestroy
     633/************************************************************************
     634 *      SafeArrayDestroy
    634635 * Deallocates all memory reserved for the SafeArray
    635636 */
    636637HRESULT WINAPI SafeArrayDestroy(
    637638  SAFEARRAY * psa)
    638 { 
     639{
    639640  HRESULT hRes;
    640641
    641   if(! validArg(psa)) 
    642     return E_INVALIDARG;
    643 
    644   if(psa->cLocks > 0) 
     642  if(! validArg(psa))
     643    return E_INVALIDARG;
     644
     645  if(psa->cLocks > 0)
    645646    return DISP_E_ARRAYISLOCKED;
    646647
     
    652653}
    653654
    654 /************************************************************************ 
    655  *              SafeArrayCopy
     655/************************************************************************
     656 *      SafeArrayCopy
    656657 * Make a dupplicate of a SafeArray
    657658 */
    658659HRESULT WINAPI SafeArrayCopy(
    659   SAFEARRAY *psa, 
     660  SAFEARRAY *psa,
    660661  SAFEARRAY **ppsaOut)
    661 { 
     662{
    662663  HRESULT hRes;
    663664  DWORD   dAllocSize;
    664665  ULONG   ulWholeArraySize; /* size of the thing */
    665666
    666   if(! validArg(psa)) 
     667  if(! validArg(psa))
    667668    return E_INVALIDARG;
    668669
     
    670671
    671672    /* Duplicate the SAFEARRAY struc */
    672     memcpy(*ppsaOut, psa, 
     673    memcpy(*ppsaOut, psa,
    673674            sizeof(*psa)+(sizeof(*(psa->rgsabound))*(psa->cDims-1)));
    674675
     
    677678    /* make sure the new safe array doesn't have the FADF_CREATEVECTOR flag,
    678679       because the data has not been allocated with the descriptor. */
    679     (*ppsaOut)->fFeatures &= ~FADF_CREATEVECTOR; 
    680  
    681     /* Get the allocated memory size for source and allocate it for target */ 
     680    (*ppsaOut)->fFeatures &= ~FADF_CREATEVECTOR;
     681
     682    /* Get the allocated memory size for source and allocate it for target */
    682683    ulWholeArraySize = getArraySize(psa); /* Number of item in SA */
    683684    dAllocSize = ulWholeArraySize*psa->cbElements;
    684685
    685     (*ppsaOut)->pvData = 
     686    (*ppsaOut)->pvData =
    686687      HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dAllocSize);
    687688    if( (*ppsaOut)->pvData != NULL) {   /* HeapAlloc succeed */
     
    693694        return hRes;
    694695      }
    695        
     696
    696697    } else { /* failed to allocate or dupplicate... */
    697698      SafeArrayDestroyDescriptor(*ppsaOut);
     
    705706}
    706707
    707 /************************************************************************ 
    708  *              SafeArrayCreateVector
    709  * Creates a one dimension safearray where the data is next to the 
     708/************************************************************************
     709 *      SafeArrayCreateVector
     710 * Creates a one dimension safearray where the data is next to the
    710711 * SAFEARRAY structure.
    711712 */
    712713SAFEARRAY* WINAPI SafeArrayCreateVector(
    713   VARTYPE vt, 
    714   LONG    lLbound, 
    715   ULONG   cElements) 
    716 { 
     714  VARTYPE vt,
     715  LONG    lLbound,
     716  ULONG   cElements)
     717{
    717718  SAFEARRAY *psa;
    718719
     
    723724
    724725  /* Allocate memory for the array descriptor and data contiguously  */
    725   if( FAILED( psa = HeapAlloc( GetProcessHeap(), 
    726                       HEAP_ZERO_MEMORY, 
     726  if( FAILED( psa = HeapAlloc( GetProcessHeap(),
     727                      HEAP_ZERO_MEMORY,
    727728                      (sizeof(*psa) + (VARTYPE_SIZE[vt] * cElements))))) {
    728729    return NULL;
    729730  }
    730                                                                                
    731   /* setup data members... */ 
     731
     732  /* setup data members... */
    732733  psa->cDims      = 1; /* always and forever */
    733734  psa->fFeatures  = getFeatures(vt) | FADF_CREATEVECTOR;  /* undocumented flag used by Microsoft */
     
    739740  psa->rgsabound[0].lLbound   = lLbound;
    740741
    741   return(psa);                           
    742 } 
    743 
    744 /************************************************************************ 
    745  *              SafeArrayRedim
     742  return(psa);
     743}
     744
     745/************************************************************************
     746 *      SafeArrayRedim
    746747 * Changes the caracteristics of the last dimension of the SafeArray
    747748 */
    748749HRESULT WINAPI SafeArrayRedim(
    749   SAFEARRAY      *psa, 
     750  SAFEARRAY      *psa,
    750751  SAFEARRAYBOUND *psaboundNew)
    751 { 
     752{
    752753  LONG   lDelta;  /* hold difference in size */
    753754  USHORT cDims=1; /* dims counter */
    754755
    755   if( !validArg(psa) )                   
    756     return E_INVALIDARG;
    757 
    758   if( psa->cLocks > 0 )                   
     756  if( !validArg(psa) )
     757    return E_INVALIDARG;
     758
     759  if( psa->cLocks > 0 )
    759760    return DISP_E_ARRAYISLOCKED;
    760761
    761   if( psa->fFeatures & FADF_FIXEDSIZE )   
    762     return E_INVALIDARG;
    763 
    764   if( SafeArrayLock(psa)==E_UNEXPECTED ) 
     762  if( psa->fFeatures & FADF_FIXEDSIZE )
     763    return E_INVALIDARG;
     764
     765  if( SafeArrayLock(psa)==E_UNEXPECTED )
    765766    return E_UNEXPECTED;/* UNDOC error condition */
    766767
     
    774775
    775776  } else /* need to enlarge (lDelta +) reduce (lDelta -) */
    776     if(! resizeSafeArray(psa, lDelta)) 
     777    if(! resizeSafeArray(psa, lDelta))
    777778      return E_UNEXPECTED; /* UNDOC error condition */
    778779
    779   /* the only modifyable dimension sits in [0] as the dimensions were reversed 
     780  /* the only modifyable dimension sits in [0] as the dimensions were reversed
    780781     at array creation time... */
    781782  psa->rgsabound[0].cElements = psaboundNew->cElements;
     
    789790 ************************************************************************/
    790791
    791 /************************************************************************ 
     792/************************************************************************
    792793 * Used to validate the SAFEARRAY type of arg
    793794 */
    794795static BOOL validArg(
    795   SAFEARRAY *psa) 
     796  SAFEARRAY *psa)
    796797{
    797798  SAFEARRAYBOUND *sab;
     
    822823}
    823824
    824 /************************************************************************ 
     825/************************************************************************
    825826 * Used to reallocate memory
    826827 */
    827828static BOOL resizeSafeArray(
    828   SAFEARRAY *psa, 
     829  SAFEARRAY *psa,
    829830  LONG lDelta)
    830831{
    831832  ULONG    ulWholeArraySize;  /* use as multiplicator */
    832   PVOID    pvNewBlock = NULL; 
     833  PVOID    pvNewBlock = NULL;
    833834  IUnknown *punk;
    834835  BSTR   bstr;
     
    838839  if(lDelta < 0) {                    /* array needs to be shorthen  */
    839840    if( isPointer(psa->fFeatures))    /* ptr that need to be released */
    840             for(;lDelta < 0; lDelta++) {
    841               punk = *(IUnknown**)
     841        for(;lDelta < 0; lDelta++) {
     842          punk = *(IUnknown**)
    842843          ((char *) psa->pvData+((ulWholeArraySize+lDelta)*psa->cbElements));
    843        
     844
    844845        if( punk != NULL )
    845846          IUnknown_Release(punk);
    846             }
     847        }
    847848
    848849    else if(psa->fFeatures & FADF_BSTR)  /* BSTR that need to be freed */
    849             for(;lDelta < 0; lDelta++) {
     850        for(;lDelta < 0; lDelta++) {
    850851        bstr = *(BSTR*)
    851852          ((char *) psa->pvData+((ulWholeArraySize+lDelta)*psa->cbElements));
     
    858859  if (!(psa->fFeatures & FADF_CREATEVECTOR))
    859860  {
    860     /* Ok now, if we are enlarging the array, we *MUST* move the whole block 
     861    /* Ok now, if we are enlarging the array, we *MUST* move the whole block
    861862       pointed to by pvData.   If we are shorthening the array, this move is
    862        optional but we do it anyway becuase the benefit is that we are 
     863       optional but we do it anyway becuase the benefit is that we are
    863864       releasing to the system the unused memory */
    864865
    865     if((pvNewBlock = HeapReAlloc(GetProcessHeap(), 0, psa->pvData, 
    866        (ulWholeArraySize + lDelta) * psa->cbElements)) == NULL) 
     866    if((pvNewBlock = HeapReAlloc(GetProcessHeap(), 0, psa->pvData,
     867       (ulWholeArraySize + lDelta) * psa->cbElements)) == NULL)
    867868        return FALSE; /* TODO If we get here it means:
    868869                         SHRINK situation :  we've deleted the undesired
     
    873874  else
    874875  {
    875     /* Allocate a new block, because the previous data has been allocated with 
     876    /* Allocate a new block, because the previous data has been allocated with
    876877       the descriptor in SafeArrayCreateVector function. */
    877878
    878879    if((pvNewBlock = HeapAlloc(GetProcessHeap(), 0,
    879        ulWholeArraySize * psa->cbElements)) == NULL) 
     880       ulWholeArraySize * psa->cbElements)) == NULL)
    880881        return FALSE;
    881882
     
    887888}
    888889
    889 /************************************************************************ 
    890  * Used to set the fFeatures data member of the SAFEARRAY structure. 
     890/************************************************************************
     891 * Used to set the fFeatures data member of the SAFEARRAY structure.
    891892 */
    892893static INT getFeatures(
    893   VARTYPE vt) 
     894  VARTYPE vt)
    894895{
    895896  switch(vt) {
     
    901902}
    902903
    903 /************************************************************************ 
    904  * Used to figure out if the fFeatures data member of the SAFEARRAY 
    905  * structure contain any information about the type of data stored... 
     904/************************************************************************
     905 * Used to figure out if the fFeatures data member of the SAFEARRAY
     906 * structure contain any information about the type of data stored...
    906907 */
    907908static BOOL isPointer(
    908   USHORT feature) 
     909  USHORT feature)
    909910{
    910911  switch(feature) {
     
    915916}
    916917
    917 /************************************************************************ 
    918  * Used to calculate the displacement when accessing or modifying 
     918/************************************************************************
     919 * Used to calculate the displacement when accessing or modifying
    919920 * safearray data set.
    920921 *
     
    926927 */
    927928static ULONG calcDisplacement(
    928   LONG           *coor, 
    929   SAFEARRAYBOUND *mat, 
    930   LONG           dim) 
     929  LONG           *coor,
     930  SAFEARRAYBOUND *mat,
     931  LONG           dim)
    931932{
    932933  ULONG res = 0;
    933934  LONG  iterDim;
    934935
    935   for(iterDim=0; iterDim<dim; iterDim++) 
     936  for(iterDim=0; iterDim<dim; iterDim++)
    936937    /* the -mat[dim] bring coor[dim] relative to 0 for calculation */
    937     res += ((coor[iterDim]-mat[iterDim].lLbound) * 
     938    res += ((coor[iterDim]-mat[iterDim].lLbound) *
    938939            endOfDim(coor, mat, iterDim+1, dim));
    939940
     
    942943}
    943944
    944 /************************************************************************ 
    945  * Recursivity agent for calcDisplacement method.  Used within Put and 
     945/************************************************************************
     946 * Recursivity agent for calcDisplacement method.  Used within Put and
    946947 * Get methods.
    947948 */
    948949static INT endOfDim(
    949   LONG           *coor, 
    950   SAFEARRAYBOUND *mat, 
    951   LONG           dim, 
    952   LONG           realDim) 
    953 {
    954   if(dim==realDim) 
     950  LONG           *coor,
     951  SAFEARRAYBOUND *mat,
     952  LONG           dim,
     953  LONG           realDim)
     954{
     955  if(dim==realDim)
    955956    return 1;
    956   else 
     957  else
    957958    return (endOfDim(coor, mat, dim+1, realDim) * mat[dim].cElements);
    958959}
    959960
    960961
    961 /************************************************************************ 
    962  * Method used to validate the coordinate received in Put and Get 
     962/************************************************************************
     963 * Method used to validate the coordinate received in Put and Get
    963964 * methods.
    964965 */
    965966static BOOL validCoordinate(
    966   LONG      *coor, 
    967   SAFEARRAY *psa) 
     967  LONG      *coor,
     968  SAFEARRAY *psa)
    968969{
    969970  INT   iter=0;
     
    977978    if((hRes = SafeArrayGetUBound(psa, iter, &lUBound)) != S_OK)
    978979      return FALSE;
    979  
    980     if(lLBound == lUBound) 
    981       return FALSE; 
    982    
     980
     981    if(lLBound == lUBound)
     982      return FALSE;
     983
    983984    if((coor[iter] >= lLBound) && (coor[iter] <= lUBound))
    984985      return TRUE;
     
    987988  }
    988989  return FALSE;
    989 } 
    990 
    991 /************************************************************************ 
     990}
     991
     992/************************************************************************
    992993 * Method used to calculate the number of cells of the SA
    993994 */
    994995static ULONG getArraySize(
    995   SAFEARRAY *psa) 
    996 {
    997   USHORT cCount; 
     996  SAFEARRAY *psa)
     997{
     998  USHORT cCount;
    998999  ULONG  ulWholeArraySize = 1;
    9991000
     
    10011002    ulWholeArraySize *= psa->rgsabound[cCount].cElements;
    10021003
    1003   return ulWholeArraySize; 
    1004 }
    1005 
    1006 
    1007 /************************************************************************ 
     1004  return ulWholeArraySize;
     1005}
     1006
     1007
     1008/************************************************************************
    10081009 * Method used to handle data space dupplication for Copy32 and CopyData32
    10091010 */
    10101011static HRESULT duplicateData(
    1011   SAFEARRAY *psa, 
    1012   SAFEARRAY **ppsaOut) 
     1012  SAFEARRAY *psa,
     1013  SAFEARRAY **ppsaOut)
    10131014{
    10141015  ULONG    ulWholeArraySize; /* size of the thing */
     
    10181019
    10191020  ulWholeArraySize = getArraySize(psa); /* Number of item in SA */
    1020  
     1021
    10211022  SafeArrayLock(*ppsaOut);
    10221023
    1023   if( isPointer(psa->fFeatures) ) {  /* If datatype is object increment 
     1024  if( isPointer(psa->fFeatures) ) {  /* If datatype is object increment
    10241025                                        object's reference count */
    10251026
     
    10321033
    10331034    /* Copy the source array data into target array */
    1034     memcpy((*ppsaOut)->pvData, psa->pvData, 
     1035    memcpy((*ppsaOut)->pvData, psa->pvData,
    10351036      ulWholeArraySize*psa->cbElements);
    10361037
    1037   } else if( psa->fFeatures & FADF_BSTR ) { /* if datatype is BSTR allocate 
     1038  } else if( psa->fFeatures & FADF_BSTR ) { /* if datatype is BSTR allocate
    10381039                                               the BSTR in the new array */
    10391040
     
    10461047      }
    10471048
    1048       *((BSTR*)((char *) (*ppsaOut)->pvData+(lDelta * psa->cbElements))) = 
     1049      *((BSTR*)((char *) (*ppsaOut)->pvData+(lDelta * psa->cbElements))) =
    10491050        pbstrReAllocStr;
    10501051    }
     
    10521053  } else { /* Simply copy the source array data into target array */
    10531054
    1054     memcpy((*ppsaOut)->pvData, psa->pvData, 
     1055    memcpy((*ppsaOut)->pvData, psa->pvData,
    10551056      ulWholeArraySize*psa->cbElements);
    10561057  }
     
    10621063
    10631064
    1064 /************************************************************************ 
    1065  *              SafeArrayGetVarType
     1065/************************************************************************
     1066 *      SafeArrayGetVarType
    10661067 * Returns the VARTYPE stored in the given safearray
    10671068 */
     
    10991100    hr = S_OK;
    11001101  }
    1101  
     1102
    11021103  TRACE("HRESULT = %08lx", hr);
    11031104  return hr;
Note: See TracChangeset for help on using the changeset viewer.