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
28 * There's a decent overview of property set storage here:
29 * http://msdn.microsoft.com/archive/en-us/dnarolegen/html/msdn_propset.asp
30 * It's a little bit out of date, and more definitive references are given
31 * below, but it gives the best "big picture" that I've found.
34 * - I don't honor the maximum property set size.
35 * - Certain bogus files could result in reading past the end of a buffer.
36 * - Mac-generated files won't be read correctly, even if they're little
37 * endian, because I disregard whether the generator was a Mac. This means
38 * strings will probably be munged (as I don't understand Mac scripts.)
39 * - Not all PROPVARIANT types are supported.
40 * - User defined properties are not supported, see comment in
41 * PropertyStorage_ReadFromStream
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
60 #include "dictionary.h"
61 #include "storage32.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(storage);
66 static inline StorageImpl *impl_from_IPropertySetStorage( IPropertySetStorage *iface )
68 return (StorageImpl *)((char*)iface - FIELD_OFFSET(StorageImpl, base.pssVtbl));
71 /* These are documented in MSDN, e.g.
72 * http://msdn.microsoft.com/library/en-us/stg/stg/property_set_header.asp
73 * http://msdn.microsoft.com/library/library/en-us/stg/stg/section.asp
74 * but they don't seem to be in any header file.
76 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
77 #define PROPSETHDR_OSVER_KIND_WIN16 0
78 #define PROPSETHDR_OSVER_KIND_MAC 1
79 #define PROPSETHDR_OSVER_KIND_WIN32 2
81 #define CP_UNICODE 1200
83 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
85 #define CFTAG_WINDOWS (-1L)
86 #define CFTAG_MACINTOSH (-2L)
87 #define CFTAG_FMTID (-3L)
88 #define CFTAG_NODATA 0L
90 /* The format version (and what it implies) is described here:
91 * http://msdn.microsoft.com/library/en-us/stg/stg/format_version.asp
93 typedef struct tagPROPERTYSETHEADER
95 WORD wByteOrder; /* always 0xfffe */
96 WORD wFormat; /* can be zero or one */
97 DWORD dwOSVer; /* OS version of originating system */
98 CLSID clsid; /* application CLSID */
99 DWORD reserved; /* always 1 */
102 typedef struct tagFORMATIDOFFSET
105 DWORD dwOffset; /* from beginning of stream */
108 typedef struct tagPROPERTYSECTIONHEADER
112 } PROPERTYSECTIONHEADER;
114 typedef struct tagPROPERTYIDOFFSET
117 DWORD dwOffset; /* from beginning of section */
120 typedef struct tagPropertyStorage_impl PropertyStorage_impl;
122 /* Initializes the property storage from the stream (and undoes any uncommitted
123 * changes in the process.) Returns an error if there is an error reading or
124 * if the stream format doesn't match what's expected.
126 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *);
128 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *);
130 /* Creates the dictionaries used by the property storage. If successful, all
131 * the dictionaries have been created. If failed, none has been. (This makes
132 * it a bit easier to deal with destroying them.)
134 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *);
136 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *);
138 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
139 * string using PropertyStorage_StringCopy.
141 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
142 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP);
144 /* Copies the string src, which is encoded using code page srcCP, and returns
145 * it in *dst, in the code page specified by targetCP. The returned string is
146 * allocated using CoTaskMemAlloc.
147 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
148 * is CP_UNICODE, the returned string is in fact an LPWSTR.
149 * Returns S_OK on success, something else on failure.
151 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
154 static const IPropertyStorageVtbl IPropertyStorage_Vtbl;
155 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl;
156 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl;
157 static HRESULT create_EnumSTATPROPSETSTG(StorageImpl *, IEnumSTATPROPSETSTG**);
158 static HRESULT create_EnumSTATPROPSTG(PropertyStorage_impl *, IEnumSTATPROPSTG**);
160 /***********************************************************************
161 * Implementation of IPropertyStorage
163 struct tagPropertyStorage_impl
165 const IPropertyStorageVtbl *vtbl;
179 struct dictionary *name_to_propid;
180 struct dictionary *propid_to_name;
181 struct dictionary *propid_to_prop;
184 /************************************************************************
185 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
187 static HRESULT WINAPI IPropertyStorage_fnQueryInterface(
188 IPropertyStorage *iface,
192 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
194 if ( (This==0) || (ppvObject==0) )
199 if (IsEqualGUID(&IID_IUnknown, riid) ||
200 IsEqualGUID(&IID_IPropertyStorage, riid))
202 IPropertyStorage_AddRef(iface);
203 *ppvObject = (IPropertyStorage*)iface;
207 return E_NOINTERFACE;
210 /************************************************************************
211 * IPropertyStorage_fnAddRef (IPropertyStorage)
213 static ULONG WINAPI IPropertyStorage_fnAddRef(
214 IPropertyStorage *iface)
216 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
217 return InterlockedIncrement(&This->ref);
220 /************************************************************************
221 * IPropertyStorage_fnRelease (IPropertyStorage)
223 static ULONG WINAPI IPropertyStorage_fnRelease(
224 IPropertyStorage *iface)
226 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
229 ref = InterlockedDecrement(&This->ref);
232 TRACE("Destroying %p\n", This);
234 IPropertyStorage_Commit(iface, STGC_DEFAULT);
235 IStream_Release(This->stm);
236 This->cs.DebugInfo->Spare[0] = 0;
237 DeleteCriticalSection(&This->cs);
238 PropertyStorage_DestroyDictionaries(This);
239 HeapFree(GetProcessHeap(), 0, This);
244 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
247 PROPVARIANT *ret = NULL;
249 dictionary_find(This->propid_to_prop, (void *)propid, (void **)&ret);
250 TRACE("returning %p\n", ret);
254 /* Returns NULL if name is NULL. */
255 static PROPVARIANT *PropertyStorage_FindPropertyByName(
256 PropertyStorage_impl *This, LPCWSTR name)
258 PROPVARIANT *ret = NULL;
263 if (This->codePage == CP_UNICODE)
265 if (dictionary_find(This->name_to_propid, name, (void **)&propid))
266 ret = PropertyStorage_FindProperty(This, (PROPID)propid);
271 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
272 &ansiName, This->codePage);
276 if (dictionary_find(This->name_to_propid, ansiName,
278 ret = PropertyStorage_FindProperty(This, (PROPID)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, (void *)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 = (PropertyStorage_impl *)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, (void *)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((LPCSTR)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, (void *)id);
548 dictionary_insert(This->propid_to_name, (void *)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 = (PropertyStorage_impl *)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 = (PropertyStorage_impl *)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,
682 (void *)rgpspec[i].u.lpwstr, (void **)&propid))
683 dictionary_remove(This->propid_to_prop, (void *)propid);
687 if (rgpspec[i].u.propid >= PID_FIRST_USABLE &&
688 rgpspec[i].u.propid < PID_MIN_READONLY)
689 dictionary_remove(This->propid_to_prop,
690 (void *)rgpspec[i].u.propid);
692 hr = STG_E_INVALIDPARAMETER;
695 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
696 IPropertyStorage_Commit(iface, STGC_DEFAULT);
697 LeaveCriticalSection(&This->cs);
701 /************************************************************************
702 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
704 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
705 IPropertyStorage* iface,
707 const PROPID rgpropid[],
708 LPOLESTR rglpwstrName[])
710 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
712 HRESULT hr = S_FALSE;
714 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
716 if (cpropid && (!rgpropid || !rglpwstrName))
718 EnterCriticalSection(&This->cs);
719 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
721 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
725 size_t len = lstrlenW(name);
728 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
730 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
732 hr = STG_E_INSUFFICIENTMEMORY;
735 rglpwstrName[i] = NULL;
737 LeaveCriticalSection(&This->cs);
741 /************************************************************************
742 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
744 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
745 IPropertyStorage* iface,
747 const PROPID rgpropid[],
748 const LPOLESTR rglpwstrName[])
750 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
754 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
756 if (cpropid && (!rgpropid || !rglpwstrName))
758 if (!(This->grfMode & STGM_READWRITE))
759 return STG_E_ACCESSDENIED;
761 EnterCriticalSection(&This->cs);
763 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
765 if (rgpropid[i] != PID_ILLEGAL)
766 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
767 CP_UNICODE, rgpropid[i]);
769 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
770 IPropertyStorage_Commit(iface, STGC_DEFAULT);
771 LeaveCriticalSection(&This->cs);
775 /************************************************************************
776 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
778 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
779 IPropertyStorage* iface,
781 const PROPID rgpropid[])
783 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
787 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
789 if (cpropid && !rgpropid)
791 if (!(This->grfMode & STGM_READWRITE))
792 return STG_E_ACCESSDENIED;
794 EnterCriticalSection(&This->cs);
796 for (i = 0; i < cpropid; i++)
800 if (dictionary_find(This->propid_to_name, (void *)rgpropid[i],
803 dictionary_remove(This->propid_to_name, (void *)rgpropid[i]);
804 dictionary_remove(This->name_to_propid, name);
807 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
808 IPropertyStorage_Commit(iface, STGC_DEFAULT);
809 LeaveCriticalSection(&This->cs);
813 /************************************************************************
814 * IPropertyStorage_fnCommit (IPropertyStorage)
816 static HRESULT WINAPI IPropertyStorage_fnCommit(
817 IPropertyStorage* iface,
818 DWORD grfCommitFlags)
820 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
823 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
825 if (!(This->grfMode & STGM_READWRITE))
826 return STG_E_ACCESSDENIED;
827 EnterCriticalSection(&This->cs);
829 hr = PropertyStorage_WriteToStream(This);
832 LeaveCriticalSection(&This->cs);
836 /************************************************************************
837 * IPropertyStorage_fnRevert (IPropertyStorage)
839 static HRESULT WINAPI IPropertyStorage_fnRevert(
840 IPropertyStorage* iface)
843 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
845 TRACE("%p\n", iface);
847 EnterCriticalSection(&This->cs);
850 PropertyStorage_DestroyDictionaries(This);
851 hr = PropertyStorage_CreateDictionaries(This);
853 hr = PropertyStorage_ReadFromStream(This);
857 LeaveCriticalSection(&This->cs);
861 /************************************************************************
862 * IPropertyStorage_fnEnum (IPropertyStorage)
864 static HRESULT WINAPI IPropertyStorage_fnEnum(
865 IPropertyStorage* iface,
866 IEnumSTATPROPSTG** ppenum)
868 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
869 return create_EnumSTATPROPSTG(This, ppenum);
872 /************************************************************************
873 * IPropertyStorage_fnSetTimes (IPropertyStorage)
875 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
876 IPropertyStorage* iface,
877 const FILETIME* pctime,
878 const FILETIME* patime,
879 const FILETIME* pmtime)
885 /************************************************************************
886 * IPropertyStorage_fnSetClass (IPropertyStorage)
888 static HRESULT WINAPI IPropertyStorage_fnSetClass(
889 IPropertyStorage* iface,
892 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
894 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
898 if (!(This->grfMode & STGM_READWRITE))
899 return STG_E_ACCESSDENIED;
900 memcpy(&This->clsid, clsid, sizeof(This->clsid));
902 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
903 IPropertyStorage_Commit(iface, STGC_DEFAULT);
907 /************************************************************************
908 * IPropertyStorage_fnStat (IPropertyStorage)
910 static HRESULT WINAPI IPropertyStorage_fnStat(
911 IPropertyStorage* iface,
912 STATPROPSETSTG* statpsstg)
914 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
918 TRACE("%p, %p\n", iface, statpsstg);
923 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
926 memcpy(&statpsstg->fmtid, &This->fmtid, sizeof(statpsstg->fmtid));
927 memcpy(&statpsstg->clsid, &This->clsid, sizeof(statpsstg->clsid));
928 statpsstg->grfFlags = This->grfFlags;
929 memcpy(&statpsstg->mtime, &stat.mtime, sizeof(statpsstg->mtime));
930 memcpy(&statpsstg->ctime, &stat.ctime, sizeof(statpsstg->ctime));
931 memcpy(&statpsstg->atime, &stat.atime, sizeof(statpsstg->atime));
932 statpsstg->dwOSVersion = This->originatorOS;
937 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
940 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
942 if (This->codePage == CP_UNICODE)
944 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
945 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
946 return lstrcmpW((LPCWSTR)a, (LPCWSTR)b);
948 return lstrcmpiW((LPCWSTR)a, (LPCWSTR)b);
952 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
953 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
954 return lstrcmpA((LPCSTR)a, (LPCSTR)b);
956 return lstrcmpiA((LPCSTR)a, (LPCSTR)b);
960 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
965 static int PropertyStorage_PropCompare(const void *a, const void *b,
968 TRACE("(%d, %d)\n", (PROPID)a, (PROPID)b);
969 return (PROPID)a - (PROPID)b;
972 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
974 PropVariantClear((PROPVARIANT *)d);
975 HeapFree(GetProcessHeap(), 0, d);
978 #ifdef WORDS_BIGENDIAN
979 /* Swaps each character in str to or from little endian; assumes the conversion
980 * is symmetric, that is, that le16toh is equivalent to htole16.
982 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
986 /* Swap characters to host order.
989 for (i = 0; i < len; i++)
990 str[i] = le16toh(str[i]);
993 #define PropertyStorage_ByteSwapString(s, l)
996 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
997 * the entries according to the values of This->codePage and This->locale.
998 * FIXME: there isn't any checking whether the read property extends past the
1001 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
1004 DWORD numEntries, i;
1007 assert(This->name_to_propid);
1008 assert(This->propid_to_name);
1010 StorageUtl_ReadDWord(ptr, 0, &numEntries);
1011 TRACE("Reading %d entries:\n", numEntries);
1012 ptr += sizeof(DWORD);
1013 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1018 StorageUtl_ReadDWord(ptr, 0, &propid);
1019 ptr += sizeof(PROPID);
1020 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1021 ptr += sizeof(DWORD);
1022 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1023 /* Make sure the source string is NULL-terminated */
1024 if (This->codePage != CP_UNICODE)
1025 ptr[cbEntry - 1] = '\0';
1027 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1028 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1029 if (This->codePage == CP_UNICODE)
1031 /* Unicode entries are padded to DWORD boundaries */
1032 if (cbEntry % sizeof(DWORD))
1033 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1035 ptr += sizeof(DWORD) + cbEntry;
1040 /* FIXME: there isn't any checking whether the read property extends past the
1041 * end of the buffer.
1043 static HRESULT PropertyStorage_ReadProperty(PropertyStorage_impl *This,
1044 PROPVARIANT *prop, const BYTE *data)
1050 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1051 data += sizeof(DWORD);
1058 prop->u.cVal = *(const char *)data;
1059 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1062 prop->u.bVal = *(const UCHAR *)data;
1063 TRACE("Read byte 0x%x\n", prop->u.bVal);
1066 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1067 TRACE("Read short %d\n", prop->u.iVal);
1070 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1071 TRACE("Read ushort %d\n", prop->u.uiVal);
1075 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1076 TRACE("Read long %ld\n", prop->u.lVal);
1080 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1081 TRACE("Read ulong %d\n", prop->u.ulVal);
1087 StorageUtl_ReadDWord(data, 0, &count);
1088 if (This->codePage == CP_UNICODE && count / 2)
1090 WARN("Unicode string has odd number of bytes\n");
1091 hr = STG_E_INVALIDHEADER;
1095 prop->u.pszVal = CoTaskMemAlloc(count);
1098 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1099 /* This is stored in the code page specified in This->codePage.
1100 * Don't convert it, the caller will just store it as-is.
1102 if (This->codePage == CP_UNICODE)
1104 /* Make sure it's NULL-terminated */
1105 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1106 TRACE("Read string value %s\n",
1107 debugstr_w(prop->u.pwszVal));
1111 /* Make sure it's NULL-terminated */
1112 prop->u.pszVal[count - 1] = '\0';
1113 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1117 hr = STG_E_INSUFFICIENTMEMORY;
1125 StorageUtl_ReadDWord(data, 0, &count);
1126 prop->u.pwszVal = CoTaskMemAlloc(count * sizeof(WCHAR));
1127 if (prop->u.pwszVal)
1129 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1130 count * sizeof(WCHAR));
1131 /* make sure string is NULL-terminated */
1132 prop->u.pwszVal[count - 1] = '\0';
1133 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1134 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1137 hr = STG_E_INSUFFICIENTMEMORY;
1141 StorageUtl_ReadULargeInteger(data, 0,
1142 (ULARGE_INTEGER *)&prop->u.filetime);
1146 DWORD len = 0, tag = 0;
1148 StorageUtl_ReadDWord(data, 0, &len);
1149 StorageUtl_ReadDWord(data, 4, &tag);
1153 prop->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
1154 prop->u.pclipdata->cbSize = len;
1155 prop->u.pclipdata->ulClipFmt = tag;
1156 prop->u.pclipdata->pClipData = CoTaskMemAlloc(len);
1157 memcpy(prop->u.pclipdata->pClipData, data+8, len);
1160 hr = STG_E_INVALIDPARAMETER;
1164 FIXME("unsupported type %d\n", prop->vt);
1165 hr = STG_E_INVALIDPARAMETER;
1170 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1171 PROPERTYSETHEADER *hdr)
1173 BYTE buf[sizeof(PROPERTYSETHEADER)];
1179 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1182 if (count != sizeof(buf))
1184 WARN("read only %d\n", count);
1185 hr = STG_E_INVALIDHEADER;
1189 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1191 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1193 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1195 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1197 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1201 TRACE("returning 0x%08x\n", hr);
1205 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1206 FORMATIDOFFSET *fmt)
1208 BYTE buf[sizeof(FORMATIDOFFSET)];
1214 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1217 if (count != sizeof(buf))
1219 WARN("read only %d\n", count);
1220 hr = STG_E_INVALIDHEADER;
1224 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1226 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1230 TRACE("returning 0x%08x\n", hr);
1234 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1235 PROPERTYSECTIONHEADER *hdr)
1237 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1243 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1246 if (count != sizeof(buf))
1248 WARN("read only %d\n", count);
1249 hr = STG_E_INVALIDHEADER;
1253 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1254 cbSection), &hdr->cbSection);
1255 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1256 cProperties), &hdr->cProperties);
1259 TRACE("returning 0x%08x\n", hr);
1263 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1265 PROPERTYSETHEADER hdr;
1266 FORMATIDOFFSET fmtOffset;
1267 PROPERTYSECTIONHEADER sectionHdr;
1274 DWORD dictOffset = 0;
1276 This->dirty = FALSE;
1277 This->highestProp = 0;
1278 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1281 if (stat.cbSize.u.HighPart)
1283 WARN("stream too big\n");
1284 /* maximum size varies, but it can't be this big */
1285 hr = STG_E_INVALIDHEADER;
1288 if (stat.cbSize.u.LowPart == 0)
1290 /* empty stream is okay */
1294 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1295 sizeof(FORMATIDOFFSET))
1297 WARN("stream too small\n");
1298 hr = STG_E_INVALIDHEADER;
1302 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1305 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1306 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1309 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1311 WARN("bad magic in prop set header\n");
1312 hr = STG_E_INVALIDHEADER;
1315 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1317 WARN("bad format version %d\n", hdr.wFormat);
1318 hr = STG_E_INVALIDHEADER;
1321 This->format = hdr.wFormat;
1322 memcpy(&This->clsid, &hdr.clsid, sizeof(This->clsid));
1323 This->originatorOS = hdr.dwOSVer;
1324 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1325 WARN("File comes from a Mac, strings will probably be screwed up\n");
1326 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1329 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1331 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1332 stat.cbSize.u.LowPart);
1333 hr = STG_E_INVALIDHEADER;
1336 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1337 * follow not one, but two sections. The first is the standard properties
1338 * for the document summary information, and the second is user-defined
1339 * properties. This is the only case in which multiple sections are
1341 * Reading the second stream isn't implemented yet.
1343 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, §ionHdr);
1346 /* The section size includes the section header, so check it */
1347 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1349 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1350 hr = STG_E_INVALIDHEADER;
1353 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1354 sizeof(PROPERTYSECTIONHEADER));
1357 hr = STG_E_INSUFFICIENTMEMORY;
1360 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1361 sizeof(PROPERTYSECTIONHEADER), &count);
1364 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1365 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1367 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1368 i * sizeof(PROPERTYIDOFFSET));
1370 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1371 idOffset->dwOffset >= sectionHdr.cbSection - sizeof(DWORD))
1372 hr = STG_E_INVALIDPOINTER;
1375 if (idOffset->propid >= PID_FIRST_USABLE &&
1376 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1378 This->highestProp = idOffset->propid;
1379 if (idOffset->propid == PID_DICTIONARY)
1381 /* Don't read the dictionary yet, its entries depend on the
1382 * code page. Just store the offset so we know to read it
1385 dictOffset = idOffset->dwOffset;
1386 TRACE("Dictionary offset is %d\n", dictOffset);
1392 PropVariantInit(&prop);
1393 if (SUCCEEDED(PropertyStorage_ReadProperty(This, &prop,
1394 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER))))
1396 TRACE("Read property with ID 0x%08x, type %d\n",
1397 idOffset->propid, prop.vt);
1398 switch(idOffset->propid)
1401 if (prop.vt == VT_I2)
1402 This->codePage = (UINT)prop.u.iVal;
1405 if (prop.vt == VT_I4)
1406 This->locale = (LCID)prop.u.lVal;
1409 if (prop.vt == VT_I4 && prop.u.lVal)
1410 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1411 /* The format should already be 1, but just in case */
1415 hr = PropertyStorage_StorePropWithId(This,
1416 idOffset->propid, &prop, This->codePage);
1422 if (!This->codePage)
1424 /* default to Unicode unless told not to, as specified here:
1425 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
1427 if (This->grfFlags & PROPSETFLAG_ANSI)
1428 This->codePage = GetACP();
1430 This->codePage = CP_UNICODE;
1433 This->locale = LOCALE_SYSTEM_DEFAULT;
1434 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1436 hr = PropertyStorage_ReadDictionary(This,
1437 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1440 HeapFree(GetProcessHeap(), 0, buf);
1443 dictionary_destroy(This->name_to_propid);
1444 This->name_to_propid = NULL;
1445 dictionary_destroy(This->propid_to_name);
1446 This->propid_to_name = NULL;
1447 dictionary_destroy(This->propid_to_prop);
1448 This->propid_to_prop = NULL;
1453 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1454 PROPERTYSETHEADER *hdr)
1457 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1458 PROPSETHDR_BYTEORDER_MAGIC);
1459 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1460 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1461 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1462 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1465 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1466 FORMATIDOFFSET *fmtOffset)
1469 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1470 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1471 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1474 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1475 PROPERTYSECTIONHEADER *hdr)
1478 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1479 StorageUtl_WriteDWord((BYTE *)hdr,
1480 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1483 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1484 PROPERTYIDOFFSET *propIdOffset)
1486 assert(propIdOffset);
1487 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1488 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1489 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1492 struct DictionaryClosure
1498 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1499 const void *value, void *extra, void *closure)
1501 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
1502 struct DictionaryClosure *c = (struct DictionaryClosure *)closure;
1508 StorageUtl_WriteDWord((LPBYTE)&propid, 0, (DWORD)value);
1509 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1512 c->bytesWritten += sizeof(DWORD);
1513 if (This->codePage == CP_UNICODE)
1515 DWORD keyLen, pad = 0;
1517 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1518 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1519 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1522 c->bytesWritten += sizeof(DWORD);
1523 /* Rather than allocate a copy, I'll swap the string to little-endian
1524 * in-place, write it, then swap it back.
1526 PropertyStorage_ByteSwapString(key, keyLen);
1527 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1528 PropertyStorage_ByteSwapString(key, keyLen);
1531 c->bytesWritten += keyLen;
1532 if (keyLen % sizeof(DWORD))
1534 c->hr = IStream_Write(This->stm, &pad,
1535 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1538 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1545 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1546 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1549 c->bytesWritten += sizeof(DWORD);
1550 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1553 c->bytesWritten += keyLen;
1556 return SUCCEEDED(c->hr);
1559 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1561 /* Writes the dictionary to the stream. Assumes without checking that the
1562 * dictionary isn't empty.
1564 static HRESULT PropertyStorage_WriteDictionaryToStream(
1565 PropertyStorage_impl *This, DWORD *sectionOffset)
1569 PROPERTYIDOFFSET propIdOffset;
1572 struct DictionaryClosure closure;
1574 assert(sectionOffset);
1576 /* The dictionary's always the first property written, so seek to its
1579 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1580 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1583 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1585 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1589 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1590 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1593 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1594 dictionary_num_entries(This->name_to_propid));
1595 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1598 *sectionOffset += sizeof(dwTemp);
1601 closure.bytesWritten = 0;
1602 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1607 *sectionOffset += closure.bytesWritten;
1608 if (closure.bytesWritten % sizeof(DWORD))
1610 DWORD padding = sizeof(DWORD) - closure.bytesWritten % sizeof(DWORD);
1611 TRACE("adding %d bytes of padding\n", padding);
1612 *sectionOffset += padding;
1619 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1620 DWORD propNum, DWORD propid, const PROPVARIANT *var, DWORD *sectionOffset)
1624 PROPERTYIDOFFSET propIdOffset;
1626 DWORD dwType, bytesWritten;
1629 assert(sectionOffset);
1631 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This, propNum, propid, var->vt,
1634 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1635 propNum * sizeof(PROPERTYIDOFFSET);
1636 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1639 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1640 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1644 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1645 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1648 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1649 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1652 *sectionOffset += sizeof(dwType);
1662 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1664 bytesWritten = count;
1671 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1672 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1673 bytesWritten = count;
1681 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1682 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1683 bytesWritten = count;
1690 if (This->codePage == CP_UNICODE)
1691 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1693 len = lstrlenA(var->u.pszVal) + 1;
1694 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1695 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1698 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1699 bytesWritten = count + sizeof(DWORD);
1704 DWORD len = lstrlenW(var->u.pwszVal) + 1, dwTemp;
1706 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1707 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1710 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1712 bytesWritten = count + sizeof(DWORD);
1719 StorageUtl_WriteULargeInteger((BYTE *)&temp, 0,
1720 (const ULARGE_INTEGER *)&var->u.filetime);
1721 hr = IStream_Write(This->stm, &temp, sizeof(FILETIME), &count);
1722 bytesWritten = count;
1727 DWORD cf_hdr[2], len;
1729 len = var->u.pclipdata->cbSize;
1730 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[0], 0, len + 8);
1731 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[1], 0, var->u.pclipdata->ulClipFmt);
1732 hr = IStream_Write(This->stm, &cf_hdr, sizeof(cf_hdr), &count);
1735 hr = IStream_Write(This->stm, &var->u.pclipdata->pClipData, len, &count);
1738 bytesWritten = count + sizeof cf_hdr;
1742 FIXME("unsupported type: %d\n", var->vt);
1743 return STG_E_INVALIDPARAMETER;
1748 *sectionOffset += bytesWritten;
1749 if (bytesWritten % sizeof(DWORD))
1751 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1752 TRACE("adding %d bytes of padding\n", padding);
1753 *sectionOffset += padding;
1761 struct PropertyClosure
1765 DWORD *sectionOffset;
1768 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1769 void *extra, void *closure)
1771 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
1772 struct PropertyClosure *c = (struct PropertyClosure *)closure;
1778 c->hr = PropertyStorage_WritePropertyToStream(This, c->propNum++,
1779 (DWORD)key, value, c->sectionOffset);
1780 return SUCCEEDED(c->hr);
1783 static HRESULT PropertyStorage_WritePropertiesToStream(
1784 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1786 struct PropertyClosure closure;
1788 assert(sectionOffset);
1790 closure.propNum = startingPropNum;
1791 closure.sectionOffset = sectionOffset;
1792 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1797 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1801 LARGE_INTEGER seek = { {0} };
1802 PROPERTYSETHEADER hdr;
1803 FORMATIDOFFSET fmtOffset;
1805 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1808 PropertyStorage_MakeHeader(This, &hdr);
1809 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1812 if (count != sizeof(hdr))
1814 hr = STG_E_WRITEFAULT;
1818 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1819 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1822 if (count != sizeof(fmtOffset))
1824 hr = STG_E_WRITEFAULT;
1833 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1835 PROPERTYSECTIONHEADER sectionHdr;
1839 DWORD numProps, prop, sectionOffset, dwTemp;
1842 PropertyStorage_WriteHeadersToStream(This);
1844 /* Count properties. Always at least one property, the code page */
1846 if (dictionary_num_entries(This->name_to_propid))
1848 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1850 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1852 numProps += dictionary_num_entries(This->propid_to_prop);
1854 /* Write section header with 0 bytes right now, I'll adjust it after
1855 * writing properties.
1857 PropertyStorage_MakeSectionHdr(0, numProps, §ionHdr);
1858 seek.QuadPart = SECTIONHEADER_OFFSET;
1859 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1862 hr = IStream_Write(This->stm, §ionHdr, sizeof(sectionHdr), &count);
1867 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1868 numProps * sizeof(PROPERTYIDOFFSET);
1870 if (dictionary_num_entries(This->name_to_propid))
1873 hr = PropertyStorage_WriteDictionaryToStream(This, §ionOffset);
1878 PropVariantInit(&var);
1881 var.u.iVal = This->codePage;
1882 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1883 &var, §ionOffset);
1887 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1890 var.u.lVal = This->locale;
1891 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1892 &var, §ionOffset);
1897 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1901 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1902 &var, §ionOffset);
1907 hr = PropertyStorage_WritePropertiesToStream(This, prop, §ionOffset);
1911 /* Now write the byte count of the section */
1912 seek.QuadPart = SECTIONHEADER_OFFSET;
1913 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1916 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1917 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1923 /***********************************************************************
1924 * PropertyStorage_Construct
1926 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1928 dictionary_destroy(This->name_to_propid);
1929 This->name_to_propid = NULL;
1930 dictionary_destroy(This->propid_to_name);
1931 This->propid_to_name = NULL;
1932 dictionary_destroy(This->propid_to_prop);
1933 This->propid_to_prop = NULL;
1936 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
1940 This->name_to_propid = dictionary_create(
1941 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1943 if (!This->name_to_propid)
1945 hr = STG_E_INSUFFICIENTMEMORY;
1948 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1950 if (!This->propid_to_name)
1952 hr = STG_E_INSUFFICIENTMEMORY;
1955 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1956 PropertyStorage_PropertyDestroy, This);
1957 if (!This->propid_to_prop)
1959 hr = STG_E_INSUFFICIENTMEMORY;
1964 PropertyStorage_DestroyDictionaries(This);
1968 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1969 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1975 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1977 return E_OUTOFMEMORY;
1979 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1981 InitializeCriticalSection(&(*pps)->cs);
1982 (*pps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PropertyStorage_impl.cs");
1984 memcpy(&(*pps)->fmtid, rfmtid, sizeof((*pps)->fmtid));
1985 (*pps)->grfMode = grfMode;
1987 hr = PropertyStorage_CreateDictionaries(*pps);
1990 IStream_Release(stm);
1991 (*pps)->cs.DebugInfo->Spare[0] = 0;
1992 DeleteCriticalSection(&(*pps)->cs);
1993 HeapFree(GetProcessHeap(), 0, *pps);
2000 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
2001 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2003 PropertyStorage_impl *ps;
2007 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2010 hr = PropertyStorage_ReadFromStream(ps);
2013 *pps = (IPropertyStorage *)ps;
2014 TRACE("PropertyStorage %p constructed\n", ps);
2019 PropertyStorage_DestroyDictionaries(ps);
2020 HeapFree(GetProcessHeap(), 0, ps);
2026 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2027 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2029 PropertyStorage_impl *ps;
2033 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2037 ps->grfFlags = grfFlags;
2038 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2040 /* default to Unicode unless told not to, as specified here:
2041 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2043 if (ps->grfFlags & PROPSETFLAG_ANSI)
2044 ps->codePage = GetACP();
2046 ps->codePage = CP_UNICODE;
2047 ps->locale = LOCALE_SYSTEM_DEFAULT;
2048 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2049 *pps = (IPropertyStorage *)ps;
2050 TRACE("PropertyStorage %p constructed\n", ps);
2057 /***********************************************************************
2058 * Implementation of IPropertySetStorage
2061 /************************************************************************
2062 * IPropertySetStorage_fnQueryInterface (IUnknown)
2064 * This method forwards to the common QueryInterface implementation
2066 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2067 IPropertySetStorage *ppstg,
2071 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2072 return IStorage_QueryInterface( (IStorage*)This, riid, ppvObject );
2075 /************************************************************************
2076 * IPropertySetStorage_fnAddRef (IUnknown)
2078 * This method forwards to the common AddRef implementation
2080 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2081 IPropertySetStorage *ppstg)
2083 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2084 return IStorage_AddRef( (IStorage*)This );
2087 /************************************************************************
2088 * IPropertySetStorage_fnRelease (IUnknown)
2090 * This method forwards to the common Release implementation
2092 static ULONG WINAPI IPropertySetStorage_fnRelease(
2093 IPropertySetStorage *ppstg)
2095 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2096 return IStorage_Release( (IStorage*)This );
2099 /************************************************************************
2100 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2102 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2103 IPropertySetStorage *ppstg,
2105 const CLSID* pclsid,
2108 IPropertyStorage** ppprstg)
2110 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2111 WCHAR name[CCH_MAX_PROPSTG_NAME];
2112 IStream *stm = NULL;
2115 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2119 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2121 r = STG_E_INVALIDFLAG;
2131 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2132 * storage, not a stream. For now, disallow it.
2134 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2136 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2137 r = STG_E_INVALIDFLAG;
2141 r = FmtIdToPropStgName(rfmtid, name);
2145 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2149 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2152 TRACE("returning 0x%08x\n", r);
2156 /************************************************************************
2157 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2159 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2160 IPropertySetStorage *ppstg,
2163 IPropertyStorage** ppprstg)
2165 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2166 IStream *stm = NULL;
2167 WCHAR name[CCH_MAX_PROPSTG_NAME];
2170 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2173 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2174 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2176 r = STG_E_INVALIDFLAG;
2186 r = FmtIdToPropStgName(rfmtid, name);
2190 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2194 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2197 TRACE("returning 0x%08x\n", r);
2201 /************************************************************************
2202 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2204 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2205 IPropertySetStorage *ppstg,
2208 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2209 IStorage *stg = NULL;
2210 WCHAR name[CCH_MAX_PROPSTG_NAME];
2213 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2216 return E_INVALIDARG;
2218 r = FmtIdToPropStgName(rfmtid, name);
2222 stg = (IStorage*) This;
2223 return IStorage_DestroyElement(stg, name);
2226 /************************************************************************
2227 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2229 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2230 IPropertySetStorage *ppstg,
2231 IEnumSTATPROPSETSTG** ppenum)
2233 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2234 return create_EnumSTATPROPSETSTG(This, ppenum);
2237 /************************************************************************
2238 * Implement IEnumSTATPROPSETSTG using enumx
2240 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2241 IEnumSTATPROPSETSTG *iface,
2245 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2248 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2249 IEnumSTATPROPSETSTG *iface)
2251 return enumx_AddRef((enumx_impl*)iface);
2254 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2255 IEnumSTATPROPSETSTG *iface)
2257 return enumx_Release((enumx_impl*)iface);
2260 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2261 IEnumSTATPROPSETSTG *iface,
2263 STATPROPSETSTG *rgelt,
2264 ULONG *pceltFetched)
2266 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2269 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2270 IEnumSTATPROPSETSTG *iface,
2273 return enumx_Skip((enumx_impl*)iface, celt);
2276 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2277 IEnumSTATPROPSETSTG *iface)
2279 return enumx_Reset((enumx_impl*)iface);
2282 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2283 IEnumSTATPROPSETSTG *iface,
2284 IEnumSTATPROPSETSTG **ppenum)
2286 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2289 static HRESULT create_EnumSTATPROPSETSTG(
2291 IEnumSTATPROPSETSTG** ppenum)
2293 IStorage *stg = (IStorage*) &This->base.lpVtbl;
2294 IEnumSTATSTG *penum = NULL;
2298 STATPROPSETSTG statpss;
2301 TRACE("%p %p\n", This, ppenum);
2303 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2304 &IEnumSTATPROPSETSTG_Vtbl,
2305 sizeof (STATPROPSETSTG));
2307 /* add all the property set elements into a list */
2308 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2310 return E_OUTOFMEMORY;
2315 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2322 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2324 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2325 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2326 debugstr_guid(&statpss.fmtid));
2327 statpss.mtime = stat.mtime;
2328 statpss.atime = stat.atime;
2329 statpss.ctime = stat.ctime;
2330 statpss.grfFlags = stat.grfMode;
2331 memcpy(&statpss.clsid, &stat.clsid, sizeof stat.clsid);
2332 enumx_add_element(enumx, &statpss);
2334 CoTaskMemFree(stat.pwcsName);
2336 IEnumSTATSTG_Release(penum);
2338 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2343 /************************************************************************
2344 * Implement IEnumSTATPROPSTG using enumx
2346 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2347 IEnumSTATPROPSTG *iface,
2351 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2354 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2355 IEnumSTATPROPSTG *iface)
2357 return enumx_AddRef((enumx_impl*)iface);
2360 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2361 IEnumSTATPROPSTG *iface)
2363 return enumx_Release((enumx_impl*)iface);
2366 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2367 IEnumSTATPROPSTG *iface,
2370 ULONG *pceltFetched)
2372 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2375 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2376 IEnumSTATPROPSTG *iface,
2379 return enumx_Skip((enumx_impl*)iface, celt);
2382 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2383 IEnumSTATPROPSTG *iface)
2385 return enumx_Reset((enumx_impl*)iface);
2388 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2389 IEnumSTATPROPSTG *iface,
2390 IEnumSTATPROPSTG **ppenum)
2392 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2395 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2397 enumx_impl *enumx = arg;
2398 PROPID propid = (PROPID) k;
2399 const PROPVARIANT *prop = v;
2402 stat.lpwstrName = NULL;
2403 stat.propid = propid;
2406 enumx_add_element(enumx, &stat);
2411 static HRESULT create_EnumSTATPROPSTG(
2412 PropertyStorage_impl *This,
2413 IEnumSTATPROPSTG** ppenum)
2417 TRACE("%p %p\n", This, ppenum);
2419 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2420 &IEnumSTATPROPSTG_Vtbl,
2421 sizeof (STATPROPSTG));
2423 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2425 *ppenum = (IEnumSTATPROPSTG*) enumx;
2430 /***********************************************************************
2433 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2435 IPropertySetStorage_fnQueryInterface,
2436 IPropertySetStorage_fnAddRef,
2437 IPropertySetStorage_fnRelease,
2438 IPropertySetStorage_fnCreate,
2439 IPropertySetStorage_fnOpen,
2440 IPropertySetStorage_fnDelete,
2441 IPropertySetStorage_fnEnum
2444 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2446 IPropertyStorage_fnQueryInterface,
2447 IPropertyStorage_fnAddRef,
2448 IPropertyStorage_fnRelease,
2449 IPropertyStorage_fnReadMultiple,
2450 IPropertyStorage_fnWriteMultiple,
2451 IPropertyStorage_fnDeleteMultiple,
2452 IPropertyStorage_fnReadPropertyNames,
2453 IPropertyStorage_fnWritePropertyNames,
2454 IPropertyStorage_fnDeletePropertyNames,
2455 IPropertyStorage_fnCommit,
2456 IPropertyStorage_fnRevert,
2457 IPropertyStorage_fnEnum,
2458 IPropertyStorage_fnSetTimes,
2459 IPropertyStorage_fnSetClass,
2460 IPropertyStorage_fnStat,
2463 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2465 IEnumSTATPROPSETSTG_fnQueryInterface,
2466 IEnumSTATPROPSETSTG_fnAddRef,
2467 IEnumSTATPROPSETSTG_fnRelease,
2468 IEnumSTATPROPSETSTG_fnNext,
2469 IEnumSTATPROPSETSTG_fnSkip,
2470 IEnumSTATPROPSETSTG_fnReset,
2471 IEnumSTATPROPSETSTG_fnClone,
2474 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2476 IEnumSTATPROPSTG_fnQueryInterface,
2477 IEnumSTATPROPSTG_fnAddRef,
2478 IEnumSTATPROPSTG_fnRelease,
2479 IEnumSTATPROPSTG_fnNext,
2480 IEnumSTATPROPSTG_fnSkip,
2481 IEnumSTATPROPSTG_fnReset,
2482 IEnumSTATPROPSTG_fnClone,
2485 /***********************************************************************
2486 * Format ID <-> name conversion
2488 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2489 'I','n','f','o','r','m','a','t','i','o','n',0 };
2490 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2491 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2493 #define BITS_PER_BYTE 8
2494 #define CHARMASK 0x1f
2495 #define BITS_IN_CHARMASK 5
2496 #define NUM_ALPHA_CHARS 26
2498 /***********************************************************************
2499 * FmtIdToPropStgName [ole32.@]
2500 * Returns the storage name of the format ID rfmtid.
2502 * rfmtid [I] Format ID for which to return a storage name
2503 * str [O] Storage name associated with rfmtid.
2506 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2509 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2510 * Based on the algorithm described here:
2511 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2513 HRESULT WINAPI FmtIdToPropStgName(const FMTID *rfmtid, LPOLESTR str)
2515 static const char fmtMap[] = "abcdefghijklmnopqrstuvwxyz012345";
2517 TRACE("%s, %p\n", debugstr_guid(rfmtid), str);
2519 if (!rfmtid) return E_INVALIDARG;
2520 if (!str) return E_INVALIDARG;
2522 if (IsEqualGUID(&FMTID_SummaryInformation, rfmtid))
2523 lstrcpyW(str, szSummaryInfo);
2524 else if (IsEqualGUID(&FMTID_DocSummaryInformation, rfmtid))
2525 lstrcpyW(str, szDocSummaryInfo);
2526 else if (IsEqualGUID(&FMTID_UserDefinedProperties, rfmtid))
2527 lstrcpyW(str, szDocSummaryInfo);
2532 ULONG bitsRemaining = BITS_PER_BYTE;
2535 for (fmtptr = (const BYTE *)rfmtid; fmtptr < (const BYTE *)rfmtid + sizeof(FMTID); )
2537 ULONG i = *fmtptr >> (BITS_PER_BYTE - bitsRemaining);
2539 if (bitsRemaining >= BITS_IN_CHARMASK)
2541 *pstr = (WCHAR)(fmtMap[i & CHARMASK]);
2542 if (bitsRemaining == BITS_PER_BYTE && *pstr >= 'a' &&
2546 bitsRemaining -= BITS_IN_CHARMASK;
2547 if (bitsRemaining == 0)
2550 bitsRemaining = BITS_PER_BYTE;
2555 if (++fmtptr < (const BYTE *)rfmtid + sizeof(FMTID))
2556 i |= *fmtptr << bitsRemaining;
2557 *pstr++ = (WCHAR)(fmtMap[i & CHARMASK]);
2558 bitsRemaining += BITS_PER_BYTE - BITS_IN_CHARMASK;
2563 TRACE("returning %s\n", debugstr_w(str));
2567 /***********************************************************************
2568 * PropStgNameToFmtId [ole32.@]
2569 * Returns the format ID corresponding to the given name.
2571 * str [I] Storage name to convert to a format ID.
2572 * rfmtid [O] Format ID corresponding to str.
2575 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2576 * a format ID, S_OK otherwise.
2579 * Based on the algorithm described here:
2580 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2582 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2584 HRESULT hr = STG_E_INVALIDNAME;
2586 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2588 if (!rfmtid) return E_INVALIDARG;
2589 if (!str) return STG_E_INVALIDNAME;
2591 if (!lstrcmpiW(str, szDocSummaryInfo))
2593 memcpy(rfmtid, &FMTID_DocSummaryInformation, sizeof(*rfmtid));
2596 else if (!lstrcmpiW(str, szSummaryInfo))
2598 memcpy(rfmtid, &FMTID_SummaryInformation, sizeof(*rfmtid));
2604 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2605 const WCHAR *pstr = str;
2607 memset(rfmtid, 0, sizeof(*rfmtid));
2608 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2609 bits += BITS_IN_CHARMASK)
2611 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2617 if (wc > NUM_ALPHA_CHARS)
2620 if (wc > NUM_ALPHA_CHARS)
2622 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2625 WARN("invalid character (%d)\n", *pstr);
2630 *fmtptr |= wc << bitsUsed;
2631 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2632 if (bitsStored < BITS_IN_CHARMASK)
2634 wc >>= BITS_PER_BYTE - bitsUsed;
2635 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2639 WARN("extra bits\n");
2645 *fmtptr |= (BYTE)wc;