2 * Compound Storage (32 bit version)
3 * Storage implementation
5 * This file contains the compound file implementation
6 * of the storage interface.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Sylvain St-Germain
10 * Copyright 1999 Thuy Nguyen
11 * Copyright 2005 Mike McCormack
12 * Copyright 2005 Juan Lang
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 * - I don't honor the maximum property set size.
30 * - Certain bogus files could result in reading past the end of a buffer.
31 * - Mac-generated files won't be read correctly, even if they're little
32 * endian, because I disregard whether the generator was a Mac. This means
33 * strings will probably be munged (as I don't understand Mac scripts.)
34 * - Not all PROPVARIANT types are supported.
35 * - User defined properties are not supported, see comment in
36 * PropertyStorage_ReadFromStream
40 #include "wine/port.h"
49 #define NONAMELESSUNION
50 #define NONAMELESSSTRUCT
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
58 #include "dictionary.h"
59 #include "storage32.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(storage);
65 static inline StorageImpl *impl_from_IPropertySetStorage( IPropertySetStorage *iface )
67 return CONTAINING_RECORD(iface, StorageImpl, base.IPropertySetStorage_iface);
70 /* These are documented in MSDN,
71 * but they don't seem to be in any header file.
73 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
74 #define PROPSETHDR_OSVER_KIND_WIN16 0
75 #define PROPSETHDR_OSVER_KIND_MAC 1
76 #define PROPSETHDR_OSVER_KIND_WIN32 2
78 #define CP_UNICODE 1200
80 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
82 #define CFTAG_WINDOWS (-1L)
83 #define CFTAG_MACINTOSH (-2L)
84 #define CFTAG_FMTID (-3L)
85 #define CFTAG_NODATA 0L
87 typedef struct tagPROPERTYSETHEADER
89 WORD wByteOrder; /* always 0xfffe */
90 WORD wFormat; /* can be zero or one */
91 DWORD dwOSVer; /* OS version of originating system */
92 CLSID clsid; /* application CLSID */
93 DWORD reserved; /* always 1 */
96 typedef struct tagFORMATIDOFFSET
99 DWORD dwOffset; /* from beginning of stream */
102 typedef struct tagPROPERTYSECTIONHEADER
106 } PROPERTYSECTIONHEADER;
108 typedef struct tagPROPERTYIDOFFSET
111 DWORD dwOffset; /* from beginning of section */
114 typedef struct tagPropertyStorage_impl PropertyStorage_impl;
116 /* Initializes the property storage from the stream (and undoes any uncommitted
117 * changes in the process.) Returns an error if there is an error reading or
118 * if the stream format doesn't match what's expected.
120 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *);
122 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *);
124 /* Creates the dictionaries used by the property storage. If successful, all
125 * the dictionaries have been created. If failed, none has been. (This makes
126 * it a bit easier to deal with destroying them.)
128 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *);
130 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *);
132 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
133 * string using PropertyStorage_StringCopy.
135 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
136 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP);
138 /* Copies the string src, which is encoded using code page srcCP, and returns
139 * it in *dst, in the code page specified by targetCP. The returned string is
140 * allocated using CoTaskMemAlloc.
141 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
142 * is CP_UNICODE, the returned string is in fact an LPWSTR.
143 * Returns S_OK on success, something else on failure.
145 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
148 static const IPropertyStorageVtbl IPropertyStorage_Vtbl;
149 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl;
150 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl;
151 static HRESULT create_EnumSTATPROPSETSTG(StorageImpl *, IEnumSTATPROPSETSTG**);
152 static HRESULT create_EnumSTATPROPSTG(PropertyStorage_impl *, IEnumSTATPROPSTG**);
154 /***********************************************************************
155 * Implementation of IPropertyStorage
157 struct tagPropertyStorage_impl
159 IPropertyStorage IPropertyStorage_iface;
173 struct dictionary *name_to_propid;
174 struct dictionary *propid_to_name;
175 struct dictionary *propid_to_prop;
178 static inline PropertyStorage_impl *impl_from_IPropertyStorage(IPropertyStorage *iface)
180 return CONTAINING_RECORD(iface, PropertyStorage_impl, IPropertyStorage_iface);
183 /************************************************************************
184 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
186 static HRESULT WINAPI IPropertyStorage_fnQueryInterface(
187 IPropertyStorage *iface,
191 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
193 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
200 if (IsEqualGUID(&IID_IUnknown, riid) ||
201 IsEqualGUID(&IID_IPropertyStorage, riid))
203 IPropertyStorage_AddRef(iface);
208 return E_NOINTERFACE;
211 /************************************************************************
212 * IPropertyStorage_fnAddRef (IPropertyStorage)
214 static ULONG WINAPI IPropertyStorage_fnAddRef(
215 IPropertyStorage *iface)
217 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
218 return InterlockedIncrement(&This->ref);
221 /************************************************************************
222 * IPropertyStorage_fnRelease (IPropertyStorage)
224 static ULONG WINAPI IPropertyStorage_fnRelease(
225 IPropertyStorage *iface)
227 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
230 ref = InterlockedDecrement(&This->ref);
233 TRACE("Destroying %p\n", This);
235 IPropertyStorage_Commit(iface, STGC_DEFAULT);
236 IStream_Release(This->stm);
237 This->cs.DebugInfo->Spare[0] = 0;
238 DeleteCriticalSection(&This->cs);
239 PropertyStorage_DestroyDictionaries(This);
240 HeapFree(GetProcessHeap(), 0, This);
245 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
248 PROPVARIANT *ret = NULL;
250 dictionary_find(This->propid_to_prop, UlongToPtr(propid), (void **)&ret);
251 TRACE("returning %p\n", ret);
255 /* Returns NULL if name is NULL. */
256 static PROPVARIANT *PropertyStorage_FindPropertyByName(
257 PropertyStorage_impl *This, LPCWSTR name)
259 PROPVARIANT *ret = NULL;
264 if (This->codePage == CP_UNICODE)
266 if (dictionary_find(This->name_to_propid, name, &propid))
267 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
272 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
273 &ansiName, This->codePage);
277 if (dictionary_find(This->name_to_propid, ansiName, &propid))
278 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
279 CoTaskMemFree(ansiName);
282 TRACE("returning %p\n", ret);
286 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
291 dictionary_find(This->propid_to_name, UlongToPtr(propid), (void **)&ret);
292 TRACE("returning %p\n", ret);
296 /************************************************************************
297 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
299 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
300 IPropertyStorage* iface,
302 const PROPSPEC rgpspec[],
303 PROPVARIANT rgpropvar[])
305 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
309 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
313 if (!rgpspec || !rgpropvar)
315 EnterCriticalSection(&This->cs);
316 for (i = 0; i < cpspec; i++)
318 PropVariantInit(&rgpropvar[i]);
319 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
321 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
322 rgpspec[i].u.lpwstr);
325 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
330 switch (rgpspec[i].u.propid)
333 rgpropvar[i].vt = VT_I2;
334 rgpropvar[i].u.iVal = This->codePage;
337 rgpropvar[i].vt = VT_I4;
338 rgpropvar[i].u.lVal = This->locale;
342 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
343 rgpspec[i].u.propid);
346 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
347 GetACP(), This->codePage);
354 LeaveCriticalSection(&This->cs);
358 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
364 TRACE("%s, %p, %d, %d\n",
365 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
374 if (dstCP == CP_UNICODE)
375 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
377 len = strlen(src) + 1;
378 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
380 hr = STG_E_INSUFFICIENTMEMORY;
382 memcpy(*dst, src, len);
386 if (dstCP == CP_UNICODE)
388 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
389 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
391 hr = STG_E_INSUFFICIENTMEMORY;
393 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
397 LPCWSTR wideStr = NULL;
398 LPWSTR wideStr_tmp = NULL;
400 if (srcCP == CP_UNICODE)
401 wideStr = (LPCWSTR)src;
404 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
405 wideStr_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
408 MultiByteToWideChar(srcCP, 0, src, -1, wideStr_tmp, len);
409 wideStr = wideStr_tmp;
412 hr = STG_E_INSUFFICIENTMEMORY;
416 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
418 *dst = CoTaskMemAlloc(len);
420 hr = STG_E_INSUFFICIENTMEMORY;
423 BOOL defCharUsed = FALSE;
425 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
426 NULL, &defCharUsed) == 0 || defCharUsed)
430 hr = HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
434 HeapFree(GetProcessHeap(), 0, wideStr_tmp);
437 TRACE("returning 0x%08x (%s)\n", hr,
438 dstCP == CP_UNICODE ? debugstr_w((LPCWSTR)*dst) : debugstr_a(*dst));
442 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
443 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
449 if (propvar->vt == VT_LPSTR)
451 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
452 &prop->u.pszVal, targetCP);
457 PropVariantCopy(prop, propvar);
461 /* Stores the property with id propid and value propvar into this property
462 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
463 * type is VT_LPSTR, converts the string using lcid as the source code page
464 * and This->codePage as the target code page before storing.
465 * As a side effect, may change This->format to 1 if the type of propvar is
466 * a version 1-only property.
468 static HRESULT PropertyStorage_StorePropWithId(PropertyStorage_impl *This,
469 PROPID propid, const PROPVARIANT *propvar, LCID lcid)
472 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
475 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
483 case VT_VECTOR|VT_I1:
486 TRACE("Setting 0x%08x to type %d\n", propid, propvar->vt);
489 PropVariantClear(prop);
490 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
495 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
496 sizeof(PROPVARIANT));
499 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
503 dictionary_insert(This->propid_to_prop, UlongToPtr(propid), prop);
504 if (propid > This->highestProp)
505 This->highestProp = propid;
508 HeapFree(GetProcessHeap(), 0, prop);
511 hr = STG_E_INSUFFICIENTMEMORY;
516 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
517 * srcName is encoded in code page cp, and is converted to This->codePage.
518 * If cp is CP_UNICODE, srcName is actually a unicode string.
519 * As a side effect, may change This->format to 1 if srcName is too long for
520 * a version 0 property storage.
521 * Doesn't validate id.
523 static HRESULT PropertyStorage_StoreNameWithId(PropertyStorage_impl *This,
524 LPCSTR srcName, LCID cp, PROPID id)
531 hr = PropertyStorage_StringCopy(srcName, cp, &name, This->codePage);
534 if (This->codePage == CP_UNICODE)
536 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
541 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
544 TRACE("Adding prop name %s, propid %d\n",
545 This->codePage == CP_UNICODE ? debugstr_w((LPCWSTR)name) :
546 debugstr_a(name), id);
547 dictionary_insert(This->name_to_propid, name, UlongToPtr(id));
548 dictionary_insert(This->propid_to_name, UlongToPtr(id), name);
553 /************************************************************************
554 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
556 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
557 IPropertyStorage* iface,
559 const PROPSPEC rgpspec[],
560 const PROPVARIANT rgpropvar[],
561 PROPID propidNameFirst)
563 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
567 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
569 if (cpspec && (!rgpspec || !rgpropvar))
571 if (!(This->grfMode & STGM_READWRITE))
572 return STG_E_ACCESSDENIED;
573 EnterCriticalSection(&This->cs);
575 This->originatorOS = (DWORD)MAKELONG(LOWORD(GetVersion()),
576 PROPSETHDR_OSVER_KIND_WIN32) ;
577 for (i = 0; i < cpspec; i++)
579 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
581 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
582 rgpspec[i].u.lpwstr);
585 PropVariantCopy(prop, &rgpropvar[i]);
588 /* Note that I don't do the special cases here that I do below,
589 * because naming the special PIDs isn't supported.
591 if (propidNameFirst < PID_FIRST_USABLE ||
592 propidNameFirst >= PID_MIN_READONLY)
593 hr = STG_E_INVALIDPARAMETER;
596 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
598 hr = PropertyStorage_StoreNameWithId(This,
599 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
601 hr = PropertyStorage_StorePropWithId(This, nextId,
602 &rgpropvar[i], GetACP());
608 switch (rgpspec[i].u.propid)
611 /* Can't set the dictionary */
612 hr = STG_E_INVALIDPARAMETER;
615 /* Can only set the code page if nothing else has been set */
616 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
617 rgpropvar[i].vt == VT_I2)
619 This->codePage = rgpropvar[i].u.iVal;
620 if (This->codePage == CP_UNICODE)
621 This->grfFlags &= ~PROPSETFLAG_ANSI;
623 This->grfFlags |= PROPSETFLAG_ANSI;
626 hr = STG_E_INVALIDPARAMETER;
629 /* Can only set the locale if nothing else has been set */
630 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
631 rgpropvar[i].vt == VT_I4)
632 This->locale = rgpropvar[i].u.lVal;
634 hr = STG_E_INVALIDPARAMETER;
637 /* silently ignore like MSDN says */
640 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
641 hr = STG_E_INVALIDPARAMETER;
643 hr = PropertyStorage_StorePropWithId(This,
644 rgpspec[i].u.propid, &rgpropvar[i], GetACP());
648 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
649 IPropertyStorage_Commit(iface, STGC_DEFAULT);
650 LeaveCriticalSection(&This->cs);
654 /************************************************************************
655 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
657 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
658 IPropertyStorage* iface,
660 const PROPSPEC rgpspec[])
662 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
666 TRACE("(%p, %d, %p)\n", iface, cpspec, rgpspec);
668 if (cpspec && !rgpspec)
670 if (!(This->grfMode & STGM_READWRITE))
671 return STG_E_ACCESSDENIED;
673 EnterCriticalSection(&This->cs);
675 for (i = 0; i < cpspec; i++)
677 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
681 if (dictionary_find(This->name_to_propid, rgpspec[i].u.lpwstr, &propid))
682 dictionary_remove(This->propid_to_prop, propid);
686 if (rgpspec[i].u.propid >= PID_FIRST_USABLE &&
687 rgpspec[i].u.propid < PID_MIN_READONLY)
688 dictionary_remove(This->propid_to_prop, UlongToPtr(rgpspec[i].u.propid));
690 hr = STG_E_INVALIDPARAMETER;
693 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
694 IPropertyStorage_Commit(iface, STGC_DEFAULT);
695 LeaveCriticalSection(&This->cs);
699 /************************************************************************
700 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
702 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
703 IPropertyStorage* iface,
705 const PROPID rgpropid[],
706 LPOLESTR rglpwstrName[])
708 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
710 HRESULT hr = S_FALSE;
712 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
714 if (cpropid && (!rgpropid || !rglpwstrName))
716 EnterCriticalSection(&This->cs);
717 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
719 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
723 size_t len = lstrlenW(name);
726 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
728 memcpy(rglpwstrName[i], name, (len + 1) * sizeof(WCHAR));
730 hr = STG_E_INSUFFICIENTMEMORY;
733 rglpwstrName[i] = NULL;
735 LeaveCriticalSection(&This->cs);
739 /************************************************************************
740 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
742 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
743 IPropertyStorage* iface,
745 const PROPID rgpropid[],
746 const LPOLESTR rglpwstrName[])
748 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
752 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
754 if (cpropid && (!rgpropid || !rglpwstrName))
756 if (!(This->grfMode & STGM_READWRITE))
757 return STG_E_ACCESSDENIED;
759 EnterCriticalSection(&This->cs);
761 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
763 if (rgpropid[i] != PID_ILLEGAL)
764 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
765 CP_UNICODE, rgpropid[i]);
767 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
768 IPropertyStorage_Commit(iface, STGC_DEFAULT);
769 LeaveCriticalSection(&This->cs);
773 /************************************************************************
774 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
776 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
777 IPropertyStorage* iface,
779 const PROPID rgpropid[])
781 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
785 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
787 if (cpropid && !rgpropid)
789 if (!(This->grfMode & STGM_READWRITE))
790 return STG_E_ACCESSDENIED;
792 EnterCriticalSection(&This->cs);
794 for (i = 0; i < cpropid; i++)
798 if (dictionary_find(This->propid_to_name, UlongToPtr(rgpropid[i]), (void **)&name))
800 dictionary_remove(This->propid_to_name, UlongToPtr(rgpropid[i]));
801 dictionary_remove(This->name_to_propid, name);
804 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
805 IPropertyStorage_Commit(iface, STGC_DEFAULT);
806 LeaveCriticalSection(&This->cs);
810 /************************************************************************
811 * IPropertyStorage_fnCommit (IPropertyStorage)
813 static HRESULT WINAPI IPropertyStorage_fnCommit(
814 IPropertyStorage* iface,
815 DWORD grfCommitFlags)
817 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
820 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
822 if (!(This->grfMode & STGM_READWRITE))
823 return STG_E_ACCESSDENIED;
824 EnterCriticalSection(&This->cs);
826 hr = PropertyStorage_WriteToStream(This);
829 LeaveCriticalSection(&This->cs);
833 /************************************************************************
834 * IPropertyStorage_fnRevert (IPropertyStorage)
836 static HRESULT WINAPI IPropertyStorage_fnRevert(
837 IPropertyStorage* iface)
840 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
842 TRACE("%p\n", iface);
844 EnterCriticalSection(&This->cs);
847 PropertyStorage_DestroyDictionaries(This);
848 hr = PropertyStorage_CreateDictionaries(This);
850 hr = PropertyStorage_ReadFromStream(This);
854 LeaveCriticalSection(&This->cs);
858 /************************************************************************
859 * IPropertyStorage_fnEnum (IPropertyStorage)
861 static HRESULT WINAPI IPropertyStorage_fnEnum(
862 IPropertyStorage* iface,
863 IEnumSTATPROPSTG** ppenum)
865 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
866 return create_EnumSTATPROPSTG(This, ppenum);
869 /************************************************************************
870 * IPropertyStorage_fnSetTimes (IPropertyStorage)
872 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
873 IPropertyStorage* iface,
874 const FILETIME* pctime,
875 const FILETIME* patime,
876 const FILETIME* pmtime)
882 /************************************************************************
883 * IPropertyStorage_fnSetClass (IPropertyStorage)
885 static HRESULT WINAPI IPropertyStorage_fnSetClass(
886 IPropertyStorage* iface,
889 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
891 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
895 if (!(This->grfMode & STGM_READWRITE))
896 return STG_E_ACCESSDENIED;
897 This->clsid = *clsid;
899 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
900 IPropertyStorage_Commit(iface, STGC_DEFAULT);
904 /************************************************************************
905 * IPropertyStorage_fnStat (IPropertyStorage)
907 static HRESULT WINAPI IPropertyStorage_fnStat(
908 IPropertyStorage* iface,
909 STATPROPSETSTG* statpsstg)
911 PropertyStorage_impl *This = impl_from_IPropertyStorage(iface);
915 TRACE("%p, %p\n", iface, statpsstg);
920 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
923 statpsstg->fmtid = This->fmtid;
924 statpsstg->clsid = This->clsid;
925 statpsstg->grfFlags = This->grfFlags;
926 statpsstg->mtime = stat.mtime;
927 statpsstg->ctime = stat.ctime;
928 statpsstg->atime = stat.atime;
929 statpsstg->dwOSVersion = This->originatorOS;
934 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
937 PropertyStorage_impl *This = extra;
939 if (This->codePage == CP_UNICODE)
941 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
942 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
943 return lstrcmpW(a, b);
945 return lstrcmpiW(a, b);
949 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
950 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
951 return lstrcmpA(a, b);
953 return lstrcmpiA(a, b);
957 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
962 static int PropertyStorage_PropCompare(const void *a, const void *b,
965 TRACE("(%d, %d)\n", PtrToUlong(a), PtrToUlong(b));
966 return PtrToUlong(a) - PtrToUlong(b);
969 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
972 HeapFree(GetProcessHeap(), 0, d);
975 #ifdef WORDS_BIGENDIAN
976 /* Swaps each character in str to or from little endian; assumes the conversion
977 * is symmetric, that is, that lendian16toh is equivalent to htole16.
979 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
983 /* Swap characters to host order.
986 for (i = 0; i < len; i++)
987 str[i] = lendian16toh(str[i]);
990 #define PropertyStorage_ByteSwapString(s, l)
993 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
994 * the entries according to the values of This->codePage and This->locale.
995 * FIXME: there isn't any checking whether the read property extends past the
998 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
1001 DWORD numEntries, i;
1004 assert(This->name_to_propid);
1005 assert(This->propid_to_name);
1007 StorageUtl_ReadDWord(ptr, 0, &numEntries);
1008 TRACE("Reading %d entries:\n", numEntries);
1009 ptr += sizeof(DWORD);
1010 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1015 StorageUtl_ReadDWord(ptr, 0, &propid);
1016 ptr += sizeof(PROPID);
1017 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1018 ptr += sizeof(DWORD);
1019 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1020 /* Make sure the source string is NULL-terminated */
1021 if (This->codePage != CP_UNICODE)
1022 ptr[cbEntry - 1] = '\0';
1024 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1025 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1026 if (This->codePage == CP_UNICODE)
1028 /* Unicode entries are padded to DWORD boundaries */
1029 if (cbEntry % sizeof(DWORD))
1030 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1032 ptr += sizeof(DWORD) + cbEntry;
1038 #define __thiscall __stdcall
1040 #define __thiscall __cdecl
1043 static __thiscall void* Allocate_CoTaskMemAlloc(void *userdata, ULONG size)
1045 return CoTaskMemAlloc(size);
1048 /* FIXME: there isn't any checking whether the read property extends past the
1049 * end of the buffer.
1051 static HRESULT PropertyStorage_ReadProperty(PROPVARIANT *prop, const BYTE *data,
1052 UINT codepage, void* (__thiscall *allocate)(void *userdata, ULONG size), void *allocate_data)
1058 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1059 data += sizeof(DWORD);
1066 prop->u.cVal = *(const char *)data;
1067 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1070 prop->u.bVal = *data;
1071 TRACE("Read byte 0x%x\n", prop->u.bVal);
1074 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1075 TRACE("Read short %d\n", prop->u.iVal);
1078 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1079 TRACE("Read ushort %d\n", prop->u.uiVal);
1083 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1084 TRACE("Read long %d\n", prop->u.lVal);
1088 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1089 TRACE("Read ulong %d\n", prop->u.ulVal);
1095 StorageUtl_ReadDWord(data, 0, &count);
1096 if (codepage == CP_UNICODE && count % 2)
1098 WARN("Unicode string has odd number of bytes\n");
1099 hr = STG_E_INVALIDHEADER;
1103 prop->u.pszVal = allocate(allocate_data, count);
1106 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1107 /* This is stored in the code page specified in codepage.
1108 * Don't convert it, the caller will just store it as-is.
1110 if (codepage == CP_UNICODE)
1112 /* Make sure it's NULL-terminated */
1113 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1114 TRACE("Read string value %s\n",
1115 debugstr_w(prop->u.pwszVal));
1119 /* Make sure it's NULL-terminated */
1120 prop->u.pszVal[count - 1] = '\0';
1121 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1125 hr = STG_E_INSUFFICIENTMEMORY;
1131 DWORD count, wcount;
1133 StorageUtl_ReadDWord(data, 0, &count);
1134 if (codepage == CP_UNICODE && count % 2)
1136 WARN("Unicode string has odd number of bytes\n");
1137 hr = STG_E_INVALIDHEADER;
1141 if (codepage == CP_UNICODE)
1144 wcount = MultiByteToWideChar(codepage, 0, (LPCSTR)(data + sizeof(DWORD)), count, NULL, 0);
1146 prop->u.bstrVal = SysAllocStringLen(NULL, wcount); /* FIXME: use allocator? */
1148 if (prop->u.bstrVal)
1150 if (codepage == CP_UNICODE)
1151 memcpy(prop->u.bstrVal, data + sizeof(DWORD), count);
1153 MultiByteToWideChar(codepage, 0, (LPCSTR)(data + sizeof(DWORD)), count, prop->u.bstrVal, wcount);
1155 prop->u.bstrVal[wcount - 1] = '\0';
1156 TRACE("Read string value %s\n", debugstr_w(prop->u.bstrVal));
1159 hr = STG_E_INSUFFICIENTMEMORY;
1167 StorageUtl_ReadDWord(data, 0, &count);
1168 prop->u.blob.cbSize = count;
1169 prop->u.blob.pBlobData = allocate(allocate_data, count);
1170 if (prop->u.blob.pBlobData)
1172 memcpy(prop->u.blob.pBlobData, data + sizeof(DWORD), count);
1173 TRACE("Read blob value of size %d\n", count);
1176 hr = STG_E_INSUFFICIENTMEMORY;
1183 StorageUtl_ReadDWord(data, 0, &count);
1184 prop->u.pwszVal = allocate(allocate_data, count * sizeof(WCHAR));
1185 if (prop->u.pwszVal)
1187 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1188 count * sizeof(WCHAR));
1189 /* make sure string is NULL-terminated */
1190 prop->u.pwszVal[count - 1] = '\0';
1191 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1192 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1195 hr = STG_E_INSUFFICIENTMEMORY;
1199 StorageUtl_ReadULargeInteger(data, 0,
1200 (ULARGE_INTEGER *)&prop->u.filetime);
1204 DWORD len = 0, tag = 0;
1206 StorageUtl_ReadDWord(data, 0, &len);
1207 StorageUtl_ReadDWord(data, 4, &tag);
1211 prop->u.pclipdata = allocate(allocate_data, sizeof (CLIPDATA));
1212 prop->u.pclipdata->cbSize = len;
1213 prop->u.pclipdata->ulClipFmt = tag;
1214 prop->u.pclipdata->pClipData = allocate(allocate_data, len - sizeof(prop->u.pclipdata->ulClipFmt));
1215 memcpy(prop->u.pclipdata->pClipData, data+8, len - sizeof(prop->u.pclipdata->ulClipFmt));
1218 hr = STG_E_INVALIDPARAMETER;
1222 FIXME("unsupported type %d\n", prop->vt);
1223 hr = STG_E_INVALIDPARAMETER;
1228 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1229 PROPERTYSETHEADER *hdr)
1231 BYTE buf[sizeof(PROPERTYSETHEADER)];
1237 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1240 if (count != sizeof(buf))
1242 WARN("read only %d\n", count);
1243 hr = STG_E_INVALIDHEADER;
1247 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1249 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1251 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1253 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1255 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1259 TRACE("returning 0x%08x\n", hr);
1263 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1264 FORMATIDOFFSET *fmt)
1266 BYTE buf[sizeof(FORMATIDOFFSET)];
1272 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1275 if (count != sizeof(buf))
1277 WARN("read only %d\n", count);
1278 hr = STG_E_INVALIDHEADER;
1282 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1284 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1288 TRACE("returning 0x%08x\n", hr);
1292 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1293 PROPERTYSECTIONHEADER *hdr)
1295 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1301 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1304 if (count != sizeof(buf))
1306 WARN("read only %d\n", count);
1307 hr = STG_E_INVALIDHEADER;
1311 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1312 cbSection), &hdr->cbSection);
1313 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1314 cProperties), &hdr->cProperties);
1317 TRACE("returning 0x%08x\n", hr);
1321 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1323 PROPERTYSETHEADER hdr;
1324 FORMATIDOFFSET fmtOffset;
1325 PROPERTYSECTIONHEADER sectionHdr;
1332 DWORD dictOffset = 0;
1334 This->dirty = FALSE;
1335 This->highestProp = 0;
1336 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1339 if (stat.cbSize.u.HighPart)
1341 WARN("stream too big\n");
1342 /* maximum size varies, but it can't be this big */
1343 hr = STG_E_INVALIDHEADER;
1346 if (stat.cbSize.u.LowPart == 0)
1348 /* empty stream is okay */
1352 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1353 sizeof(FORMATIDOFFSET))
1355 WARN("stream too small\n");
1356 hr = STG_E_INVALIDHEADER;
1360 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1363 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1364 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1367 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1369 WARN("bad magic in prop set header\n");
1370 hr = STG_E_INVALIDHEADER;
1373 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1375 WARN("bad format version %d\n", hdr.wFormat);
1376 hr = STG_E_INVALIDHEADER;
1379 This->format = hdr.wFormat;
1380 This->clsid = hdr.clsid;
1381 This->originatorOS = hdr.dwOSVer;
1382 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1383 WARN("File comes from a Mac, strings will probably be screwed up\n");
1384 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1387 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1389 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1390 stat.cbSize.u.LowPart);
1391 hr = STG_E_INVALIDHEADER;
1394 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1395 * follow not one, but two sections. The first is the standard properties
1396 * for the document summary information, and the second is user-defined
1397 * properties. This is the only case in which multiple sections are
1399 * Reading the second stream isn't implemented yet.
1401 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, §ionHdr);
1404 /* The section size includes the section header, so check it */
1405 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1407 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1408 hr = STG_E_INVALIDHEADER;
1411 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1412 sizeof(PROPERTYSECTIONHEADER));
1415 hr = STG_E_INSUFFICIENTMEMORY;
1418 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1419 sizeof(PROPERTYSECTIONHEADER), &count);
1422 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1423 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1425 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1426 i * sizeof(PROPERTYIDOFFSET));
1428 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1429 idOffset->dwOffset > sectionHdr.cbSection - sizeof(DWORD))
1430 hr = STG_E_INVALIDPOINTER;
1433 if (idOffset->propid >= PID_FIRST_USABLE &&
1434 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1436 This->highestProp = idOffset->propid;
1437 if (idOffset->propid == PID_DICTIONARY)
1439 /* Don't read the dictionary yet, its entries depend on the
1440 * code page. Just store the offset so we know to read it
1443 dictOffset = idOffset->dwOffset;
1444 TRACE("Dictionary offset is %d\n", dictOffset);
1450 PropVariantInit(&prop);
1451 if (SUCCEEDED(PropertyStorage_ReadProperty(&prop,
1452 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER),
1453 This->codePage, Allocate_CoTaskMemAlloc, NULL)))
1455 TRACE("Read property with ID 0x%08x, type %d\n",
1456 idOffset->propid, prop.vt);
1457 switch(idOffset->propid)
1460 if (prop.vt == VT_I2)
1461 This->codePage = (UINT)prop.u.iVal;
1464 if (prop.vt == VT_I4)
1465 This->locale = (LCID)prop.u.lVal;
1468 if (prop.vt == VT_I4 && prop.u.lVal)
1469 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1470 /* The format should already be 1, but just in case */
1474 hr = PropertyStorage_StorePropWithId(This,
1475 idOffset->propid, &prop, This->codePage);
1478 PropVariantClear(&prop);
1482 if (!This->codePage)
1484 /* default to Unicode unless told not to, as specified on msdn */
1485 if (This->grfFlags & PROPSETFLAG_ANSI)
1486 This->codePage = GetACP();
1488 This->codePage = CP_UNICODE;
1491 This->locale = LOCALE_SYSTEM_DEFAULT;
1492 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1494 hr = PropertyStorage_ReadDictionary(This,
1495 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1498 HeapFree(GetProcessHeap(), 0, buf);
1501 dictionary_destroy(This->name_to_propid);
1502 This->name_to_propid = NULL;
1503 dictionary_destroy(This->propid_to_name);
1504 This->propid_to_name = NULL;
1505 dictionary_destroy(This->propid_to_prop);
1506 This->propid_to_prop = NULL;
1511 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1512 PROPERTYSETHEADER *hdr)
1515 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1516 PROPSETHDR_BYTEORDER_MAGIC);
1517 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1518 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1519 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1520 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1523 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1524 FORMATIDOFFSET *fmtOffset)
1527 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1528 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1529 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1532 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1533 PROPERTYSECTIONHEADER *hdr)
1536 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1537 StorageUtl_WriteDWord((BYTE *)hdr,
1538 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1541 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1542 PROPERTYIDOFFSET *propIdOffset)
1544 assert(propIdOffset);
1545 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1546 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1547 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1550 static inline HRESULT PropertStorage_WriteWStringToStream(IStream *stm,
1551 LPCWSTR str, DWORD len, DWORD *written)
1553 #ifdef WORDS_BIGENDIAN
1554 WCHAR *leStr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1558 return E_OUTOFMEMORY;
1559 memcpy(leStr, str, len * sizeof(WCHAR));
1560 PropertyStorage_ByteSwapString(leStr, len);
1561 hr = IStream_Write(stm, leStr, len, written);
1562 HeapFree(GetProcessHeap(), 0, leStr);
1565 return IStream_Write(stm, str, len, written);
1569 struct DictionaryClosure
1575 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1576 const void *value, void *extra, void *closure)
1578 PropertyStorage_impl *This = extra;
1579 struct DictionaryClosure *c = closure;
1585 StorageUtl_WriteDWord((LPBYTE)&propid, 0, PtrToUlong(value));
1586 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1589 c->bytesWritten += sizeof(DWORD);
1590 if (This->codePage == CP_UNICODE)
1592 DWORD keyLen, pad = 0;
1594 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1595 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1596 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1599 c->bytesWritten += sizeof(DWORD);
1600 c->hr = PropertStorage_WriteWStringToStream(This->stm, key, keyLen,
1604 c->bytesWritten += keyLen * sizeof(WCHAR);
1605 if (keyLen % sizeof(DWORD))
1607 c->hr = IStream_Write(This->stm, &pad,
1608 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1611 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1618 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1619 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1622 c->bytesWritten += sizeof(DWORD);
1623 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1626 c->bytesWritten += keyLen;
1629 return SUCCEEDED(c->hr);
1632 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1634 /* Writes the dictionary to the stream. Assumes without checking that the
1635 * dictionary isn't empty.
1637 static HRESULT PropertyStorage_WriteDictionaryToStream(
1638 PropertyStorage_impl *This, DWORD *sectionOffset)
1642 PROPERTYIDOFFSET propIdOffset;
1645 struct DictionaryClosure closure;
1647 assert(sectionOffset);
1649 /* The dictionary's always the first property written, so seek to its
1652 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1653 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1656 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1658 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1662 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1663 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1666 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1667 dictionary_num_entries(This->name_to_propid));
1668 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1671 *sectionOffset += sizeof(dwTemp);
1674 closure.bytesWritten = 0;
1675 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1680 *sectionOffset += closure.bytesWritten;
1681 if (closure.bytesWritten % sizeof(DWORD))
1683 DWORD padding = sizeof(DWORD) - closure.bytesWritten % sizeof(DWORD);
1684 TRACE("adding %d bytes of padding\n", padding);
1685 *sectionOffset += padding;
1692 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1693 DWORD propNum, DWORD propid, const PROPVARIANT *var, DWORD *sectionOffset)
1697 PROPERTYIDOFFSET propIdOffset;
1699 DWORD dwType, bytesWritten;
1702 assert(sectionOffset);
1704 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This, propNum, propid, var->vt,
1707 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1708 propNum * sizeof(PROPERTYIDOFFSET);
1709 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1712 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1713 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1717 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1718 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1721 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1722 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1725 *sectionOffset += sizeof(dwType);
1735 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1737 bytesWritten = count;
1744 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1745 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1746 bytesWritten = count;
1754 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1755 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1756 bytesWritten = count;
1763 if (This->codePage == CP_UNICODE)
1764 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1766 len = lstrlenA(var->u.pszVal) + 1;
1767 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1768 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1771 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1772 bytesWritten = count + sizeof(DWORD);
1777 DWORD len = lstrlenW(var->u.pwszVal) + 1, dwTemp;
1779 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1780 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1783 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1785 bytesWritten = count + sizeof(DWORD);
1792 StorageUtl_WriteULargeInteger((BYTE *)&temp, 0,
1793 (const ULARGE_INTEGER *)&var->u.filetime);
1794 hr = IStream_Write(This->stm, &temp, sizeof(FILETIME), &count);
1795 bytesWritten = count;
1800 DWORD cf_hdr[2], len;
1802 len = var->u.pclipdata->cbSize;
1803 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[0], 0, len + 8);
1804 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[1], 0, var->u.pclipdata->ulClipFmt);
1805 hr = IStream_Write(This->stm, cf_hdr, sizeof(cf_hdr), &count);
1808 hr = IStream_Write(This->stm, var->u.pclipdata->pClipData,
1809 len - sizeof(var->u.pclipdata->ulClipFmt), &count);
1812 bytesWritten = count + sizeof cf_hdr;
1819 StorageUtl_WriteGUID((BYTE *)&temp, 0, var->u.puuid);
1820 hr = IStream_Write(This->stm, &temp, sizeof(temp), &count);
1821 bytesWritten = count;
1825 FIXME("unsupported type: %d\n", var->vt);
1826 return STG_E_INVALIDPARAMETER;
1831 *sectionOffset += bytesWritten;
1832 if (bytesWritten % sizeof(DWORD))
1834 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1835 TRACE("adding %d bytes of padding\n", padding);
1836 *sectionOffset += padding;
1844 struct PropertyClosure
1848 DWORD *sectionOffset;
1851 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1852 void *extra, void *closure)
1854 PropertyStorage_impl *This = extra;
1855 struct PropertyClosure *c = closure;
1861 c->hr = PropertyStorage_WritePropertyToStream(This, c->propNum++,
1862 PtrToUlong(key), value, c->sectionOffset);
1863 return SUCCEEDED(c->hr);
1866 static HRESULT PropertyStorage_WritePropertiesToStream(
1867 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1869 struct PropertyClosure closure;
1871 assert(sectionOffset);
1873 closure.propNum = startingPropNum;
1874 closure.sectionOffset = sectionOffset;
1875 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1880 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1884 LARGE_INTEGER seek = { {0} };
1885 PROPERTYSETHEADER hdr;
1886 FORMATIDOFFSET fmtOffset;
1888 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1891 PropertyStorage_MakeHeader(This, &hdr);
1892 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1895 if (count != sizeof(hdr))
1897 hr = STG_E_WRITEFAULT;
1901 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1902 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1905 if (count != sizeof(fmtOffset))
1907 hr = STG_E_WRITEFAULT;
1916 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1918 PROPERTYSECTIONHEADER sectionHdr;
1922 DWORD numProps, prop, sectionOffset, dwTemp;
1925 PropertyStorage_WriteHeadersToStream(This);
1927 /* Count properties. Always at least one property, the code page */
1929 if (dictionary_num_entries(This->name_to_propid))
1931 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1933 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1935 numProps += dictionary_num_entries(This->propid_to_prop);
1937 /* Write section header with 0 bytes right now, I'll adjust it after
1938 * writing properties.
1940 PropertyStorage_MakeSectionHdr(0, numProps, §ionHdr);
1941 seek.QuadPart = SECTIONHEADER_OFFSET;
1942 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1945 hr = IStream_Write(This->stm, §ionHdr, sizeof(sectionHdr), &count);
1950 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1951 numProps * sizeof(PROPERTYIDOFFSET);
1953 if (dictionary_num_entries(This->name_to_propid))
1956 hr = PropertyStorage_WriteDictionaryToStream(This, §ionOffset);
1961 PropVariantInit(&var);
1964 var.u.iVal = This->codePage;
1965 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1966 &var, §ionOffset);
1970 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1973 var.u.lVal = This->locale;
1974 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1975 &var, §ionOffset);
1980 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1984 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1985 &var, §ionOffset);
1990 hr = PropertyStorage_WritePropertiesToStream(This, prop, §ionOffset);
1994 /* Now write the byte count of the section */
1995 seek.QuadPart = SECTIONHEADER_OFFSET;
1996 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1999 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
2000 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
2006 /***********************************************************************
2007 * PropertyStorage_Construct
2009 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
2011 dictionary_destroy(This->name_to_propid);
2012 This->name_to_propid = NULL;
2013 dictionary_destroy(This->propid_to_name);
2014 This->propid_to_name = NULL;
2015 dictionary_destroy(This->propid_to_prop);
2016 This->propid_to_prop = NULL;
2019 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
2023 This->name_to_propid = dictionary_create(
2024 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
2026 if (!This->name_to_propid)
2028 hr = STG_E_INSUFFICIENTMEMORY;
2031 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
2033 if (!This->propid_to_name)
2035 hr = STG_E_INSUFFICIENTMEMORY;
2038 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
2039 PropertyStorage_PropertyDestroy, This);
2040 if (!This->propid_to_prop)
2042 hr = STG_E_INSUFFICIENTMEMORY;
2047 PropertyStorage_DestroyDictionaries(This);
2051 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
2052 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
2058 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
2060 return E_OUTOFMEMORY;
2062 (*pps)->IPropertyStorage_iface.lpVtbl = &IPropertyStorage_Vtbl;
2064 InitializeCriticalSection(&(*pps)->cs);
2065 (*pps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PropertyStorage_impl.cs");
2067 (*pps)->fmtid = *rfmtid;
2068 (*pps)->grfMode = grfMode;
2070 hr = PropertyStorage_CreateDictionaries(*pps);
2073 IStream_Release(stm);
2074 (*pps)->cs.DebugInfo->Spare[0] = 0;
2075 DeleteCriticalSection(&(*pps)->cs);
2076 HeapFree(GetProcessHeap(), 0, *pps);
2083 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
2084 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2086 PropertyStorage_impl *ps;
2090 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2093 hr = PropertyStorage_ReadFromStream(ps);
2096 *pps = &ps->IPropertyStorage_iface;
2097 TRACE("PropertyStorage %p constructed\n", ps);
2102 PropertyStorage_DestroyDictionaries(ps);
2103 HeapFree(GetProcessHeap(), 0, ps);
2109 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2110 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2112 PropertyStorage_impl *ps;
2116 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2120 ps->grfFlags = grfFlags;
2121 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2123 /* default to Unicode unless told not to, as specified on msdn */
2124 if (ps->grfFlags & PROPSETFLAG_ANSI)
2125 ps->codePage = GetACP();
2127 ps->codePage = CP_UNICODE;
2128 ps->locale = LOCALE_SYSTEM_DEFAULT;
2129 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2130 *pps = &ps->IPropertyStorage_iface;
2131 TRACE("PropertyStorage %p constructed\n", ps);
2138 /***********************************************************************
2139 * Implementation of IPropertySetStorage
2142 /************************************************************************
2143 * IPropertySetStorage_fnQueryInterface (IUnknown)
2145 * This method forwards to the common QueryInterface implementation
2147 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2148 IPropertySetStorage *ppstg,
2152 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2153 return IStorage_QueryInterface( &This->base.IStorage_iface, riid, ppvObject );
2156 /************************************************************************
2157 * IPropertySetStorage_fnAddRef (IUnknown)
2159 * This method forwards to the common AddRef implementation
2161 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2162 IPropertySetStorage *ppstg)
2164 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2165 return IStorage_AddRef( &This->base.IStorage_iface );
2168 /************************************************************************
2169 * IPropertySetStorage_fnRelease (IUnknown)
2171 * This method forwards to the common Release implementation
2173 static ULONG WINAPI IPropertySetStorage_fnRelease(
2174 IPropertySetStorage *ppstg)
2176 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2177 return IStorage_Release( &This->base.IStorage_iface );
2180 /************************************************************************
2181 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2183 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2184 IPropertySetStorage *ppstg,
2186 const CLSID* pclsid,
2189 IPropertyStorage** ppprstg)
2191 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2192 WCHAR name[CCH_MAX_PROPSTG_NAME];
2193 IStream *stm = NULL;
2196 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2200 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2202 r = STG_E_INVALIDFLAG;
2212 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2213 * storage, not a stream. For now, disallow it.
2215 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2217 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2218 r = STG_E_INVALIDFLAG;
2222 r = FmtIdToPropStgName(rfmtid, name);
2226 r = IStorage_CreateStream( &This->base.IStorage_iface, name, grfMode, 0, 0, &stm );
2230 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2233 TRACE("returning 0x%08x\n", r);
2237 /************************************************************************
2238 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2240 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2241 IPropertySetStorage *ppstg,
2244 IPropertyStorage** ppprstg)
2246 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2247 IStream *stm = NULL;
2248 WCHAR name[CCH_MAX_PROPSTG_NAME];
2251 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2254 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2255 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2257 r = STG_E_INVALIDFLAG;
2267 r = FmtIdToPropStgName(rfmtid, name);
2271 r = IStorage_OpenStream( &This->base.IStorage_iface, name, 0, grfMode, 0, &stm );
2275 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2278 TRACE("returning 0x%08x\n", r);
2282 /************************************************************************
2283 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2285 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2286 IPropertySetStorage *ppstg,
2289 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2290 WCHAR name[CCH_MAX_PROPSTG_NAME];
2293 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2296 return E_INVALIDARG;
2298 r = FmtIdToPropStgName(rfmtid, name);
2302 return IStorage_DestroyElement(&This->base.IStorage_iface, name);
2305 /************************************************************************
2306 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2308 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2309 IPropertySetStorage *ppstg,
2310 IEnumSTATPROPSETSTG** ppenum)
2312 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2313 return create_EnumSTATPROPSETSTG(This, ppenum);
2316 /************************************************************************
2317 * Implement IEnumSTATPROPSETSTG using enumx
2319 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2320 IEnumSTATPROPSETSTG *iface,
2324 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2327 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2328 IEnumSTATPROPSETSTG *iface)
2330 return enumx_AddRef((enumx_impl*)iface);
2333 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2334 IEnumSTATPROPSETSTG *iface)
2336 return enumx_Release((enumx_impl*)iface);
2339 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2340 IEnumSTATPROPSETSTG *iface,
2342 STATPROPSETSTG *rgelt,
2343 ULONG *pceltFetched)
2345 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2348 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2349 IEnumSTATPROPSETSTG *iface,
2352 return enumx_Skip((enumx_impl*)iface, celt);
2355 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2356 IEnumSTATPROPSETSTG *iface)
2358 return enumx_Reset((enumx_impl*)iface);
2361 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2362 IEnumSTATPROPSETSTG *iface,
2363 IEnumSTATPROPSETSTG **ppenum)
2365 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2368 static HRESULT create_EnumSTATPROPSETSTG(
2370 IEnumSTATPROPSETSTG** ppenum)
2372 IStorage *stg = &This->base.IStorage_iface;
2373 IEnumSTATSTG *penum = NULL;
2377 STATPROPSETSTG statpss;
2380 TRACE("%p %p\n", This, ppenum);
2382 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2383 &IEnumSTATPROPSETSTG_Vtbl,
2384 sizeof (STATPROPSETSTG));
2386 /* add all the property set elements into a list */
2387 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2389 return E_OUTOFMEMORY;
2394 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2401 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2403 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2404 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2405 debugstr_guid(&statpss.fmtid));
2406 statpss.mtime = stat.mtime;
2407 statpss.atime = stat.atime;
2408 statpss.ctime = stat.ctime;
2409 statpss.grfFlags = stat.grfMode;
2410 statpss.clsid = stat.clsid;
2411 enumx_add_element(enumx, &statpss);
2413 CoTaskMemFree(stat.pwcsName);
2415 IEnumSTATSTG_Release(penum);
2417 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2422 /************************************************************************
2423 * Implement IEnumSTATPROPSTG using enumx
2425 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2426 IEnumSTATPROPSTG *iface,
2430 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2433 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2434 IEnumSTATPROPSTG *iface)
2436 return enumx_AddRef((enumx_impl*)iface);
2439 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2440 IEnumSTATPROPSTG *iface)
2442 return enumx_Release((enumx_impl*)iface);
2445 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2446 IEnumSTATPROPSTG *iface,
2449 ULONG *pceltFetched)
2451 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2454 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2455 IEnumSTATPROPSTG *iface,
2458 return enumx_Skip((enumx_impl*)iface, celt);
2461 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2462 IEnumSTATPROPSTG *iface)
2464 return enumx_Reset((enumx_impl*)iface);
2467 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2468 IEnumSTATPROPSTG *iface,
2469 IEnumSTATPROPSTG **ppenum)
2471 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2474 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2476 enumx_impl *enumx = arg;
2477 PROPID propid = PtrToUlong(k);
2478 const PROPVARIANT *prop = v;
2481 stat.lpwstrName = NULL;
2482 stat.propid = propid;
2485 enumx_add_element(enumx, &stat);
2490 static HRESULT create_EnumSTATPROPSTG(
2491 PropertyStorage_impl *This,
2492 IEnumSTATPROPSTG** ppenum)
2496 TRACE("%p %p\n", This, ppenum);
2498 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2499 &IEnumSTATPROPSTG_Vtbl,
2500 sizeof (STATPROPSTG));
2502 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2504 *ppenum = (IEnumSTATPROPSTG*) enumx;
2509 /***********************************************************************
2512 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2514 IPropertySetStorage_fnQueryInterface,
2515 IPropertySetStorage_fnAddRef,
2516 IPropertySetStorage_fnRelease,
2517 IPropertySetStorage_fnCreate,
2518 IPropertySetStorage_fnOpen,
2519 IPropertySetStorage_fnDelete,
2520 IPropertySetStorage_fnEnum
2523 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2525 IPropertyStorage_fnQueryInterface,
2526 IPropertyStorage_fnAddRef,
2527 IPropertyStorage_fnRelease,
2528 IPropertyStorage_fnReadMultiple,
2529 IPropertyStorage_fnWriteMultiple,
2530 IPropertyStorage_fnDeleteMultiple,
2531 IPropertyStorage_fnReadPropertyNames,
2532 IPropertyStorage_fnWritePropertyNames,
2533 IPropertyStorage_fnDeletePropertyNames,
2534 IPropertyStorage_fnCommit,
2535 IPropertyStorage_fnRevert,
2536 IPropertyStorage_fnEnum,
2537 IPropertyStorage_fnSetTimes,
2538 IPropertyStorage_fnSetClass,
2539 IPropertyStorage_fnStat,
2542 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2544 IEnumSTATPROPSETSTG_fnQueryInterface,
2545 IEnumSTATPROPSETSTG_fnAddRef,
2546 IEnumSTATPROPSETSTG_fnRelease,
2547 IEnumSTATPROPSETSTG_fnNext,
2548 IEnumSTATPROPSETSTG_fnSkip,
2549 IEnumSTATPROPSETSTG_fnReset,
2550 IEnumSTATPROPSETSTG_fnClone,
2553 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2555 IEnumSTATPROPSTG_fnQueryInterface,
2556 IEnumSTATPROPSTG_fnAddRef,
2557 IEnumSTATPROPSTG_fnRelease,
2558 IEnumSTATPROPSTG_fnNext,
2559 IEnumSTATPROPSTG_fnSkip,
2560 IEnumSTATPROPSTG_fnReset,
2561 IEnumSTATPROPSTG_fnClone,
2564 /***********************************************************************
2565 * Format ID <-> name conversion
2567 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2568 'I','n','f','o','r','m','a','t','i','o','n',0 };
2569 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2570 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2572 #define BITS_PER_BYTE 8
2573 #define CHARMASK 0x1f
2574 #define BITS_IN_CHARMASK 5
2575 #define NUM_ALPHA_CHARS 26
2577 /***********************************************************************
2578 * FmtIdToPropStgName [ole32.@]
2579 * Returns the storage name of the format ID rfmtid.
2581 * rfmtid [I] Format ID for which to return a storage name
2582 * str [O] Storage name associated with rfmtid.
2585 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2588 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2590 HRESULT WINAPI FmtIdToPropStgName(const FMTID *rfmtid, LPOLESTR str)
2592 static const char fmtMap[] = "abcdefghijklmnopqrstuvwxyz012345";
2594 TRACE("%s, %p\n", debugstr_guid(rfmtid), str);
2596 if (!rfmtid) return E_INVALIDARG;
2597 if (!str) return E_INVALIDARG;
2599 if (IsEqualGUID(&FMTID_SummaryInformation, rfmtid))
2600 lstrcpyW(str, szSummaryInfo);
2601 else if (IsEqualGUID(&FMTID_DocSummaryInformation, rfmtid))
2602 lstrcpyW(str, szDocSummaryInfo);
2603 else if (IsEqualGUID(&FMTID_UserDefinedProperties, rfmtid))
2604 lstrcpyW(str, szDocSummaryInfo);
2609 ULONG bitsRemaining = BITS_PER_BYTE;
2612 for (fmtptr = (const BYTE *)rfmtid; fmtptr < (const BYTE *)rfmtid + sizeof(FMTID); )
2614 ULONG i = *fmtptr >> (BITS_PER_BYTE - bitsRemaining);
2616 if (bitsRemaining >= BITS_IN_CHARMASK)
2618 *pstr = (WCHAR)(fmtMap[i & CHARMASK]);
2619 if (bitsRemaining == BITS_PER_BYTE && *pstr >= 'a' &&
2623 bitsRemaining -= BITS_IN_CHARMASK;
2624 if (bitsRemaining == 0)
2627 bitsRemaining = BITS_PER_BYTE;
2632 if (++fmtptr < (const BYTE *)rfmtid + sizeof(FMTID))
2633 i |= *fmtptr << bitsRemaining;
2634 *pstr++ = (WCHAR)(fmtMap[i & CHARMASK]);
2635 bitsRemaining += BITS_PER_BYTE - BITS_IN_CHARMASK;
2640 TRACE("returning %s\n", debugstr_w(str));
2644 /***********************************************************************
2645 * PropStgNameToFmtId [ole32.@]
2646 * Returns the format ID corresponding to the given name.
2648 * str [I] Storage name to convert to a format ID.
2649 * rfmtid [O] Format ID corresponding to str.
2652 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2653 * a format ID, S_OK otherwise.
2655 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2657 HRESULT hr = STG_E_INVALIDNAME;
2659 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2661 if (!rfmtid) return E_INVALIDARG;
2662 if (!str) return STG_E_INVALIDNAME;
2664 if (!lstrcmpiW(str, szDocSummaryInfo))
2666 *rfmtid = FMTID_DocSummaryInformation;
2669 else if (!lstrcmpiW(str, szSummaryInfo))
2671 *rfmtid = FMTID_SummaryInformation;
2677 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2678 const WCHAR *pstr = str;
2680 memset(rfmtid, 0, sizeof(*rfmtid));
2681 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2682 bits += BITS_IN_CHARMASK)
2684 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2690 if (wc > NUM_ALPHA_CHARS)
2693 if (wc > NUM_ALPHA_CHARS)
2695 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2698 WARN("invalid character (%d)\n", *pstr);
2703 *fmtptr |= wc << bitsUsed;
2704 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2705 if (bitsStored < BITS_IN_CHARMASK)
2707 wc >>= BITS_PER_BYTE - bitsUsed;
2708 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2712 WARN("extra bits\n");
2718 *fmtptr |= (BYTE)wc;
2727 #ifdef __i386__ /* thiscall functions are i386-specific */
2729 #define DEFINE_STDCALL_WRAPPER(num,func,args) \
2730 __ASM_STDCALL_FUNC(func, args, \
2734 "movl (%ecx), %eax\n\t" \
2735 "jmp *(4*(" #num "))(%eax)" )
2737 DEFINE_STDCALL_WRAPPER(0,Allocate_PMemoryAllocator,8)
2738 extern void* __thiscall Allocate_PMemoryAllocator(void *this, ULONG cbSize);
2742 static void* __thiscall Allocate_PMemoryAllocator(void *this, ULONG cbSize)
2744 void* (__thiscall *fn)(void*,ULONG) = **(void***)this;
2745 return fn(this, cbSize);
2750 BOOLEAN WINAPI StgConvertPropertyToVariant(const SERIALIZEDPROPERTYVALUE* prop,
2751 USHORT CodePage, PROPVARIANT* pvar, void* pma)
2755 hr = PropertyStorage_ReadProperty(pvar, (const BYTE*)prop, CodePage, Allocate_PMemoryAllocator, pma);
2759 FIXME("should raise C++ exception on failure\n");
2760 PropVariantInit(pvar);
2766 SERIALIZEDPROPERTYVALUE* WINAPI StgConvertVariantToProperty(const PROPVARIANT *pvar,
2767 USHORT CodePage, SERIALIZEDPROPERTYVALUE *pprop, ULONG *pcb, PROPID pid,
2768 BOOLEAN fReserved, ULONG *pcIndirect)
2770 FIXME("%p,%d,%p,%p,%d,%d,%p\n", pvar, CodePage, pprop, pcb, pid, fReserved, pcIndirect);