memlockbytes.c 16.6 KB
Newer Older
1 2 3 4 5 6
/******************************************************************************
 *
 * Global memory implementation of ILockBytes.
 *
 * Copyright 1999 Thuy Nguyen
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23
#include "config.h"

24
#include <assert.h>
25
#include <stdarg.h>
26
#include <string.h>
27

28
#define COBJMACROS
29 30
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
31

32
#include "windef.h"
33
#include "winbase.h"
34
#include "winuser.h"
35 36
#include "objbase.h"
#include "ole2.h"
37 38
#include "winerror.h"

39
#include "wine/debug.h"
40

41
WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 43 44 45

/******************************************************************************
 * HGLOBALLockBytesImpl definition.
 *
Huw Davies's avatar
Huw Davies committed
46
 * This class implements the ILockBytes interface and represents a byte array
47 48 49 50 51
 * object supported by an HGLOBAL pointer.
 */
struct HGLOBALLockBytesImpl
{
  /*
52
   * Needs to be the first item in the struct
53 54
   * since we want to cast this in an ILockBytes pointer
   */
55
  const ILockBytesVtbl *lpVtbl;
56 57 58 59

  /*
   * Reference count
   */
60
  LONG        ref;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  /*
   * Support for the LockBytes object
   */
  HGLOBAL supportHandle;

  /*
   * This flag is TRUE if the HGLOBAL is destroyed when the object
   * is finally released.
   */
  BOOL    deleteOnRelease;

  /*
   * Helper variable that contains the size of the byte array
   */
  ULARGE_INTEGER     byteArraySize;
};

typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;

/*
 * Method definition for the HGLOBALLockBytesImpl class.
 */
84
static HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
85 86 87
    HGLOBAL  hGlobal,
    BOOL     fDeleteOnRelease);

88
static void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
89

90
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize( ILockBytes* iface, ULARGE_INTEGER libNewSize );
91

92
static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl;
93 94

/******************************************************************************
95
 *           CreateILockBytesOnHGlobal     [OLE32.@]
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
 *
 * Create a byte array object which is intended to be the compound file foundation.
 * This object supports a COM implementation of the ILockBytes interface.
 *
 * PARAMS
 *  hGlobal           [ I] Global memory handle
 *  fDeleteOnRelease  [ I] Whether the handle should be freed when the object is released. 
 *  ppLkbyt           [ O] Address of ILockBytes pointer that receives
 *                         the interface pointer to the new byte array object.
 *
 * RETURNS
 *  Success: S_OK
 *
 * NOTES
 *  The supplied ILockBytes pointer can be used by the StgCreateDocfileOnILockBytes
 *  function to build a compound file on top of this byte array object.
 *  The ILockBytes interface instance calls the GlobalReAlloc function to grow
 *  the memory block as required.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 */
HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL      hGlobal,
                                         BOOL         fDeleteOnRelease,
                                         LPLOCKBYTES* ppLkbyt)
{
  HGLOBALLockBytesImpl* newLockBytes;

  newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);

  if (newLockBytes != NULL)
  {
    return IUnknown_QueryInterface((IUnknown*)newLockBytes,
                                   &IID_ILockBytes,
                                   (void**)ppLkbyt);
  }

  return E_OUTOFMEMORY;
}

/******************************************************************************
134
 *           GetHGlobalFromILockBytes     [OLE32.@]
135 136 137 138 139 140 141 142 143 144 145
 *
 * Retrieve a global memory handle to a byte array object created
 * using the CreateILockBytesOnHGlobal function.
 *
 * PARAMS
 *  plkbyt   [ I]  Pointer to the ILockBytes interface on byte array object
 *  phglobal [ O]  Address to store a global memory handle
 * RETURNS
 *  S_OK          if *phglobal has a correct value
 *  E_INVALIDARG  if any parameters are invalid
 *  
146 147 148 149
 */
HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
{
  HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
150 151 152 153
  STATSTG stbuf;
  HRESULT hres;
  ULARGE_INTEGER start;
  ULONG xread;
154

155
  *phglobal = 0;
156
  if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
157
    *phglobal = pMemLockBytes->supportHandle;
158 159 160 161 162
    if (*phglobal == 0)
      return E_INVALIDARG;
    return S_OK;
  }
  /* It is not our lockbytes implementation, so use a more generic way */
163
  hres = ILockBytes_Stat(plkbyt,&stbuf,STATFLAG_NONAME);
164
  if (hres != S_OK) {
165
     ERR("Cannot ILockBytes_Stat, %x\n",hres);
166 167
     return hres;
  }
168
  FIXME("cbSize is %d\n",stbuf.cbSize.u.LowPart);
169
  *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
170
  if (!*phglobal)
171
    return E_INVALIDARG;
172
  memset(&start,0,sizeof(start));
173
  hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
174 175
  GlobalUnlock(*phglobal);
  if (hres != S_OK) {
176
    FIXME("%p->ReadAt failed with %x\n",plkbyt,hres);
177 178
    return hres;
  }
179
  if (stbuf.cbSize.u.LowPart != xread) {
180
    FIXME("Read size is not requested size %d vs %d?\n",stbuf.cbSize.u.LowPart, xread);
181
  }
182 183 184 185 186 187 188 189 190 191 192 193
  return S_OK;
}

/******************************************************************************
 *
 * HGLOBALLockBytesImpl implementation
 *
 */

/******************************************************************************
 * This is the constructor for the HGLOBALLockBytesImpl class.
 *
194 195 196 197
 * PARAMS
 *    hGlobal          [ I] Handle that will support the stream. can be NULL.
 *    fDeleteOnRelease [ I] Flag set to TRUE if the HGLOBAL will be released
 *                          when the IStream object is destroyed.
198
 */
199 200
static HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
                                                            BOOL    fDeleteOnRelease)
201 202 203
{
  HGLOBALLockBytesImpl* newLockBytes;
  newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
204

205 206 207 208 209
  if (newLockBytes!=0)
  {
    /*
     * Set up the virtual function table and reference count.
     */
210
    newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
211
    newLockBytes->ref    = 0;
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    /*
     * Initialize the support.
     */
    newLockBytes->supportHandle = hGlobal;
    newLockBytes->deleteOnRelease = fDeleteOnRelease;

    /*
     * This method will allocate a handle if one is not supplied.
     */
    if (newLockBytes->supportHandle == 0)
    {
      newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
                                                GMEM_NODISCARD,
                                                0);
    }

    /*
     * Initialize the size of the array to the size of the handle.
     */
232 233
    newLockBytes->byteArraySize.u.HighPart = 0;
    newLockBytes->byteArraySize.u.LowPart  = GlobalSize(
234 235 236 237 238 239 240 241 242 243 244 245 246
                                              newLockBytes->supportHandle);
  }

  return newLockBytes;
}

/******************************************************************************
 * This is the destructor of the HGLOBALStreamImpl class.
 *
 * This method will clean-up all the resources used-up by the given
 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
 * freed and will not be valid anymore.
 */
247
static void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
{
  /*
   * Release the HGlobal if the constructor asked for that.
   */
  if (This->deleteOnRelease)
  {
    GlobalFree(This->supportHandle);
    This->supportHandle = 0;
  }

  /*
   * Finally, free the memory used-up by the class.
   */
  HeapFree(GetProcessHeap(), 0, This);
}

/******************************************************************************
 * This implements the IUnknown method QueryInterface for this
 * class
 */
268
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      ILockBytes*  iface,
      REFIID       riid,        /* [in] */
      void**       ppvObject)   /* [iid_is][out] */
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;

  /*
   * Perform a sanity check on the parameters.
   */
  if (ppvObject==0)
    return E_INVALIDARG;

  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;

  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
289 290
  if (IsEqualIID(riid, &IID_IUnknown) ||
      IsEqualIID(riid, &IID_ILockBytes))
291
  {
292
    *ppvObject = This;
293 294 295 296 297 298 299 300 301 302 303 304
  }

  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
    return E_NOINTERFACE;

  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
305
  IUnknown_AddRef(iface);
306

307
  return S_OK;
308 309 310 311 312 313
}

/******************************************************************************
 * This implements the IUnknown method AddRef for this
 * class
 */
314
static ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
315 316
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
317
  return InterlockedIncrement(&This->ref);
318 319 320 321 322 323
}

/******************************************************************************
 * This implements the IUnknown method Release for this
 * class
 */
324
static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
325 326
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
327
  ULONG ref;
328

329
  ref = InterlockedDecrement(&This->ref);
330 331 332 333

  /*
   * If the reference count goes down to 0, perform suicide.
   */
334
  if (ref==0)
335 336 337 338
  {
    HGLOBALLockBytesImpl_Destroy(This);
  }

339
  return ref;
340 341 342 343 344 345 346 347 348 349
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * It reads a block of information from the byte array at the specified
 * offset.
 *
 * See the documentation of ILockBytes for more info.
 */
350
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
      ILockBytes*    iface,
      ULARGE_INTEGER ulOffset,  /* [in] */
      void*          pv,        /* [length_is][size_is][out] */
      ULONG          cb,        /* [in] */
      ULONG*         pcbRead)   /* [out] */
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;

  void* supportBuffer;
  ULONG bytesReadBuffer = 0;
  ULONG bytesToReadFromBuffer;

  /*
   * If the caller is not interested in the number of bytes read,
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbRead == 0)
    pcbRead = &bytesReadBuffer;

  /*
   * Make sure the offset is valid.
   */
373
  if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
374 375 376 377 378 379
    return E_FAIL;

  /*
   * Using the known size of the array, calculate the number of bytes
   * to read.
   */
380 381
  bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
                              ulOffset.u.LowPart, cb);
382 383 384 385 386 387 388

  /*
   * Lock the buffer in position and copy the data.
   */
  supportBuffer = GlobalLock(This->supportHandle);

  memcpy(pv,
389
         (char *) supportBuffer + ulOffset.u.LowPart,
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
         bytesToReadFromBuffer);

  /*
   * Return the number of bytes read.
   */
  *pcbRead = bytesToReadFromBuffer;

  /*
   * Cleanup
   */
  GlobalUnlock(This->supportHandle);

  /*
   * The function returns S_OK if the specified number of bytes were read
   * or the end of the array was reached.
405
   * It returns STG_E_READFAULT if the number of bytes to read does not equal
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
   * the number of bytes actually read.
   */
  if(*pcbRead == cb)
    return S_OK;

  return STG_E_READFAULT;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * It writes the specified bytes at the specified offset.
 * position. If the array is too small, it will be resized.
 *
 * See the documentation of ILockBytes for more info.
 */
422
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
      ILockBytes*    iface,
      ULARGE_INTEGER ulOffset,    /* [in] */
      const void*    pv,          /* [size_is][in] */
      ULONG          cb,          /* [in] */
      ULONG*         pcbWritten)  /* [out] */
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;

  void*          supportBuffer;
  ULARGE_INTEGER newSize;
  ULONG          bytesWritten = 0;

  /*
   * If the caller is not interested in the number of bytes written,
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbWritten == 0)
    pcbWritten = &bytesWritten;

  if (cb == 0)
  {
    return S_OK;
  }
  else
  {
448 449
    newSize.u.HighPart = 0;
    newSize.u.LowPart = ulOffset.u.LowPart + cb;
450 451 452 453 454
  }

  /*
   * Verify if we need to grow the stream
   */
455
  if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
456 457 458 459 460 461 462 463 464 465 466
  {
    /* grow stream */
    if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
      return STG_E_MEDIUMFULL;
  }

  /*
   * Lock the buffer in position and copy the data.
   */
  supportBuffer = GlobalLock(This->supportHandle);

467
  memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

  /*
   * Return the number of bytes written.
   */
  *pcbWritten = cb;

  /*
   * Cleanup
   */
  GlobalUnlock(This->supportHandle);

  return S_OK;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * See the documentation of ILockBytes for more info.
 */
487
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
488 489 490 491 492 493 494 495 496 497 498
{
  return S_OK;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * It will change the size of the byte array.
 *
 * See the documentation of ILockBytes for more info.
 */
499
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
500 501 502 503
      ILockBytes*     iface,
      ULARGE_INTEGER  libNewSize)   /* [in] */
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
504
  HGLOBAL supportHandle;
505 506 507 508

  /*
   * As documented.
   */
509
  if (libNewSize.u.HighPart != 0)
510
    return STG_E_INVALIDFUNCTION;
511

512
  if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
513 514 515 516 517
    return S_OK;

  /*
   * Re allocate the HGlobal to fit the new size of the stream.
   */
518
  supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
519

520
  if (supportHandle == 0)
521 522
    return STG_E_MEDIUMFULL;

523
  This->supportHandle = supportHandle;
524
  This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
525

526 527 528 529 530 531 532 533 534 535
  return S_OK;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * The global memory implementation of ILockBytes does not support locking.
 *
 * See the documentation of ILockBytes for more info.
 */
536
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
      ILockBytes*    iface,
      ULARGE_INTEGER libOffset,   /* [in] */
      ULARGE_INTEGER cb,          /* [in] */
      DWORD          dwLockType)  /* [in] */
{
  return STG_E_INVALIDFUNCTION;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * The global memory implementation of ILockBytes does not support locking.
 *
 * See the documentation of ILockBytes for more info.
 */
552
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
      ILockBytes*    iface,
      ULARGE_INTEGER libOffset,   /* [in] */
      ULARGE_INTEGER cb,          /* [in] */
      DWORD          dwLockType)  /* [in] */
{
  return STG_E_INVALIDFUNCTION;
}

/******************************************************************************
 * This method is part of the ILockBytes interface.
 *
 * This method returns information about the current
 * byte array object.
 *
 * See the documentation of ILockBytes for more info.
 */
569
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
570 571 572 573 574 575 576 577 578 579 580 581 582 583
      ILockBytes*  iface,
      STATSTG*     pstatstg,     /* [out] */
      DWORD        grfStatFlag)  /* [in] */
{
  HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;

  memset(pstatstg, 0, sizeof(STATSTG));

  pstatstg->pwcsName = NULL;
  pstatstg->type     = STGTY_LOCKBYTES;
  pstatstg->cbSize   = This->byteArraySize;

  return S_OK;
}
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

/*
 * Virtual function table for the HGLOBALLockBytesImpl class.
 */
static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
{
    HGLOBALLockBytesImpl_QueryInterface,
    HGLOBALLockBytesImpl_AddRef,
    HGLOBALLockBytesImpl_Release,
    HGLOBALLockBytesImpl_ReadAt,
    HGLOBALLockBytesImpl_WriteAt,
    HGLOBALLockBytesImpl_Flush,
    HGLOBALLockBytesImpl_SetSize,
    HGLOBALLockBytesImpl_LockRegion,
    HGLOBALLockBytesImpl_UnlockRegion,
    HGLOBALLockBytesImpl_Stat,
};