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 DeleteCriticalSection(&This->cs);
237 PropertyStorage_DestroyDictionaries(This);
238 HeapFree(GetProcessHeap(), 0, This);
243 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
246 PROPVARIANT *ret = NULL;
248 dictionary_find(This->propid_to_prop, (void *)propid, (void **)&ret);
249 TRACE("returning %p\n", ret);
253 /* Returns NULL if name is NULL. */
254 static PROPVARIANT *PropertyStorage_FindPropertyByName(
255 PropertyStorage_impl *This, LPCWSTR name)
257 PROPVARIANT *ret = NULL;
262 if (This->codePage == CP_UNICODE)
264 if (dictionary_find(This->name_to_propid, name, (void **)&propid))
265 ret = PropertyStorage_FindProperty(This, (PROPID)propid);
270 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
271 &ansiName, This->codePage);
275 if (dictionary_find(This->name_to_propid, ansiName,
277 ret = PropertyStorage_FindProperty(This, (PROPID)propid);
278 CoTaskMemFree(ansiName);
281 TRACE("returning %p\n", ret);
285 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
290 dictionary_find(This->propid_to_name, (void *)propid, (void **)&ret);
291 TRACE("returning %p\n", ret);
295 /************************************************************************
296 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
298 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
299 IPropertyStorage* iface,
301 const PROPSPEC rgpspec[],
302 PROPVARIANT rgpropvar[])
304 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
308 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
312 if (!rgpspec || !rgpropvar)
314 EnterCriticalSection(&This->cs);
315 for (i = 0; i < cpspec; i++)
317 PropVariantInit(&rgpropvar[i]);
318 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
320 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
321 rgpspec[i].u.lpwstr);
324 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
329 switch (rgpspec[i].u.propid)
332 rgpropvar[i].vt = VT_I2;
333 rgpropvar[i].u.iVal = This->codePage;
336 rgpropvar[i].vt = VT_I4;
337 rgpropvar[i].u.lVal = This->locale;
341 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
342 rgpspec[i].u.propid);
345 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
346 GetACP(), This->codePage);
353 LeaveCriticalSection(&This->cs);
357 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
363 TRACE("%s, %p, %d, %d\n",
364 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
373 if (dstCP == CP_UNICODE)
374 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
376 len = strlen(src) + 1;
377 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
379 hr = STG_E_INSUFFICIENTMEMORY;
381 memcpy(*dst, src, len);
385 if (dstCP == CP_UNICODE)
387 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
388 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
390 hr = STG_E_INSUFFICIENTMEMORY;
392 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
396 LPCWSTR wideStr = NULL;
397 LPWSTR wideStr_tmp = NULL;
399 if (srcCP == CP_UNICODE)
400 wideStr = (LPCWSTR)src;
403 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
404 wideStr_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
407 MultiByteToWideChar(srcCP, 0, src, -1, wideStr_tmp, len);
408 wideStr = wideStr_tmp;
411 hr = STG_E_INSUFFICIENTMEMORY;
415 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
417 *dst = CoTaskMemAlloc(len);
419 hr = STG_E_INSUFFICIENTMEMORY;
422 BOOL defCharUsed = FALSE;
424 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
425 NULL, &defCharUsed) == 0 || defCharUsed)
429 hr = HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
433 HeapFree(GetProcessHeap(), 0, wideStr_tmp);
436 TRACE("returning 0x%08x (%s)\n", hr,
437 dstCP == CP_UNICODE ? debugstr_w((LPCWSTR)*dst) : debugstr_a(*dst));
441 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
442 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
448 if (propvar->vt == VT_LPSTR)
450 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
451 &prop->u.pszVal, targetCP);
456 PropVariantCopy(prop, propvar);
460 /* Stores the property with id propid and value propvar into this property
461 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
462 * type is VT_LPSTR, converts the string using lcid as the source code page
463 * and This->codePage as the target code page before storing.
464 * As a side effect, may change This->format to 1 if the type of propvar is
465 * a version 1-only property.
467 static HRESULT PropertyStorage_StorePropWithId(PropertyStorage_impl *This,
468 PROPID propid, const PROPVARIANT *propvar, LCID lcid)
471 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
474 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
482 case VT_VECTOR|VT_I1:
485 TRACE("Setting 0x%08x to type %d\n", propid, propvar->vt);
488 PropVariantClear(prop);
489 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
494 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
495 sizeof(PROPVARIANT));
498 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
502 dictionary_insert(This->propid_to_prop, (void *)propid, prop);
503 if (propid > This->highestProp)
504 This->highestProp = propid;
507 HeapFree(GetProcessHeap(), 0, prop);
510 hr = STG_E_INSUFFICIENTMEMORY;
515 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
516 * srcName is encoded in code page cp, and is converted to This->codePage.
517 * If cp is CP_UNICODE, srcName is actually a unicode string.
518 * As a side effect, may change This->format to 1 if srcName is too long for
519 * a version 0 property storage.
520 * Doesn't validate id.
522 static HRESULT PropertyStorage_StoreNameWithId(PropertyStorage_impl *This,
523 LPCSTR srcName, LCID cp, PROPID id)
530 hr = PropertyStorage_StringCopy((LPCSTR)srcName, cp, &name, This->codePage);
533 if (This->codePage == CP_UNICODE)
535 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
540 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
543 TRACE("Adding prop name %s, propid %d\n",
544 This->codePage == CP_UNICODE ? debugstr_w((LPCWSTR)name) :
545 debugstr_a(name), id);
546 dictionary_insert(This->name_to_propid, name, (void *)id);
547 dictionary_insert(This->propid_to_name, (void *)id, name);
552 /************************************************************************
553 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
555 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
556 IPropertyStorage* iface,
558 const PROPSPEC rgpspec[],
559 const PROPVARIANT rgpropvar[],
560 PROPID propidNameFirst)
562 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
566 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
568 if (cpspec && (!rgpspec || !rgpropvar))
570 if (!(This->grfMode & STGM_READWRITE))
571 return STG_E_ACCESSDENIED;
572 EnterCriticalSection(&This->cs);
574 This->originatorOS = (DWORD)MAKELONG(LOWORD(GetVersion()),
575 PROPSETHDR_OSVER_KIND_WIN32) ;
576 for (i = 0; i < cpspec; i++)
578 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
580 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
581 rgpspec[i].u.lpwstr);
584 PropVariantCopy(prop, &rgpropvar[i]);
587 /* Note that I don't do the special cases here that I do below,
588 * because naming the special PIDs isn't supported.
590 if (propidNameFirst < PID_FIRST_USABLE ||
591 propidNameFirst >= PID_MIN_READONLY)
592 hr = STG_E_INVALIDPARAMETER;
595 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
597 hr = PropertyStorage_StoreNameWithId(This,
598 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
600 hr = PropertyStorage_StorePropWithId(This, nextId,
601 &rgpropvar[i], GetACP());
607 switch (rgpspec[i].u.propid)
610 /* Can't set the dictionary */
611 hr = STG_E_INVALIDPARAMETER;
614 /* Can only set the code page if nothing else has been set */
615 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
616 rgpropvar[i].vt == VT_I2)
618 This->codePage = rgpropvar[i].u.iVal;
619 if (This->codePage == CP_UNICODE)
620 This->grfFlags &= ~PROPSETFLAG_ANSI;
622 This->grfFlags |= PROPSETFLAG_ANSI;
625 hr = STG_E_INVALIDPARAMETER;
628 /* Can only set the locale if nothing else has been set */
629 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
630 rgpropvar[i].vt == VT_I4)
631 This->locale = rgpropvar[i].u.lVal;
633 hr = STG_E_INVALIDPARAMETER;
636 /* silently ignore like MSDN says */
639 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
640 hr = STG_E_INVALIDPARAMETER;
642 hr = PropertyStorage_StorePropWithId(This,
643 rgpspec[i].u.propid, &rgpropvar[i], GetACP());
647 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
648 IPropertyStorage_Commit(iface, STGC_DEFAULT);
649 LeaveCriticalSection(&This->cs);
653 /************************************************************************
654 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
656 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
657 IPropertyStorage* iface,
659 const PROPSPEC rgpspec[])
661 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
665 TRACE("(%p, %d, %p)\n", iface, cpspec, rgpspec);
667 if (cpspec && !rgpspec)
669 if (!(This->grfMode & STGM_READWRITE))
670 return STG_E_ACCESSDENIED;
672 EnterCriticalSection(&This->cs);
674 for (i = 0; i < cpspec; i++)
676 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
680 if (dictionary_find(This->name_to_propid,
681 (void *)rgpspec[i].u.lpwstr, (void **)&propid))
682 dictionary_remove(This->propid_to_prop, (void *)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,
689 (void *)rgpspec[i].u.propid);
691 hr = STG_E_INVALIDPARAMETER;
694 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
695 IPropertyStorage_Commit(iface, STGC_DEFAULT);
696 LeaveCriticalSection(&This->cs);
700 /************************************************************************
701 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
703 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
704 IPropertyStorage* iface,
706 const PROPID rgpropid[],
707 LPOLESTR rglpwstrName[])
709 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
711 HRESULT hr = S_FALSE;
713 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
715 if (cpropid && (!rgpropid || !rglpwstrName))
717 EnterCriticalSection(&This->cs);
718 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
720 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
724 size_t len = lstrlenW(name);
727 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
729 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
731 hr = STG_E_INSUFFICIENTMEMORY;
734 rglpwstrName[i] = NULL;
736 LeaveCriticalSection(&This->cs);
740 /************************************************************************
741 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
743 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
744 IPropertyStorage* iface,
746 const PROPID rgpropid[],
747 const LPOLESTR rglpwstrName[])
749 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
753 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
755 if (cpropid && (!rgpropid || !rglpwstrName))
757 if (!(This->grfMode & STGM_READWRITE))
758 return STG_E_ACCESSDENIED;
760 EnterCriticalSection(&This->cs);
762 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
764 if (rgpropid[i] != PID_ILLEGAL)
765 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
766 CP_UNICODE, rgpropid[i]);
768 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
769 IPropertyStorage_Commit(iface, STGC_DEFAULT);
770 LeaveCriticalSection(&This->cs);
774 /************************************************************************
775 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
777 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
778 IPropertyStorage* iface,
780 const PROPID rgpropid[])
782 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
786 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
788 if (cpropid && !rgpropid)
790 if (!(This->grfMode & STGM_READWRITE))
791 return STG_E_ACCESSDENIED;
793 EnterCriticalSection(&This->cs);
795 for (i = 0; i < cpropid; i++)
799 if (dictionary_find(This->propid_to_name, (void *)rgpropid[i],
802 dictionary_remove(This->propid_to_name, (void *)rgpropid[i]);
803 dictionary_remove(This->name_to_propid, name);
806 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
807 IPropertyStorage_Commit(iface, STGC_DEFAULT);
808 LeaveCriticalSection(&This->cs);
812 /************************************************************************
813 * IPropertyStorage_fnCommit (IPropertyStorage)
815 static HRESULT WINAPI IPropertyStorage_fnCommit(
816 IPropertyStorage* iface,
817 DWORD grfCommitFlags)
819 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
822 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
824 if (!(This->grfMode & STGM_READWRITE))
825 return STG_E_ACCESSDENIED;
826 EnterCriticalSection(&This->cs);
828 hr = PropertyStorage_WriteToStream(This);
831 LeaveCriticalSection(&This->cs);
835 /************************************************************************
836 * IPropertyStorage_fnRevert (IPropertyStorage)
838 static HRESULT WINAPI IPropertyStorage_fnRevert(
839 IPropertyStorage* iface)
842 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
844 TRACE("%p\n", iface);
846 EnterCriticalSection(&This->cs);
849 PropertyStorage_DestroyDictionaries(This);
850 hr = PropertyStorage_CreateDictionaries(This);
852 hr = PropertyStorage_ReadFromStream(This);
856 LeaveCriticalSection(&This->cs);
860 /************************************************************************
861 * IPropertyStorage_fnEnum (IPropertyStorage)
863 static HRESULT WINAPI IPropertyStorage_fnEnum(
864 IPropertyStorage* iface,
865 IEnumSTATPROPSTG** ppenum)
867 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
868 return create_EnumSTATPROPSTG(This, ppenum);
871 /************************************************************************
872 * IPropertyStorage_fnSetTimes (IPropertyStorage)
874 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
875 IPropertyStorage* iface,
876 const FILETIME* pctime,
877 const FILETIME* patime,
878 const FILETIME* pmtime)
884 /************************************************************************
885 * IPropertyStorage_fnSetClass (IPropertyStorage)
887 static HRESULT WINAPI IPropertyStorage_fnSetClass(
888 IPropertyStorage* iface,
891 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
893 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
897 if (!(This->grfMode & STGM_READWRITE))
898 return STG_E_ACCESSDENIED;
899 memcpy(&This->clsid, clsid, sizeof(This->clsid));
901 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
902 IPropertyStorage_Commit(iface, STGC_DEFAULT);
906 /************************************************************************
907 * IPropertyStorage_fnStat (IPropertyStorage)
909 static HRESULT WINAPI IPropertyStorage_fnStat(
910 IPropertyStorage* iface,
911 STATPROPSETSTG* statpsstg)
913 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
917 TRACE("%p, %p\n", iface, statpsstg);
922 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
925 memcpy(&statpsstg->fmtid, &This->fmtid, sizeof(statpsstg->fmtid));
926 memcpy(&statpsstg->clsid, &This->clsid, sizeof(statpsstg->clsid));
927 statpsstg->grfFlags = This->grfFlags;
928 memcpy(&statpsstg->mtime, &stat.mtime, sizeof(statpsstg->mtime));
929 memcpy(&statpsstg->ctime, &stat.ctime, sizeof(statpsstg->ctime));
930 memcpy(&statpsstg->atime, &stat.atime, sizeof(statpsstg->atime));
931 statpsstg->dwOSVersion = This->originatorOS;
936 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
939 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
941 if (This->codePage == CP_UNICODE)
943 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
944 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
945 return lstrcmpW((LPCWSTR)a, (LPCWSTR)b);
947 return lstrcmpiW((LPCWSTR)a, (LPCWSTR)b);
951 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
952 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
953 return lstrcmpA((LPCSTR)a, (LPCSTR)b);
955 return lstrcmpiA((LPCSTR)a, (LPCSTR)b);
959 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
964 static int PropertyStorage_PropCompare(const void *a, const void *b,
967 TRACE("(%d, %d)\n", (PROPID)a, (PROPID)b);
968 return (PROPID)a - (PROPID)b;
971 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
973 PropVariantClear((PROPVARIANT *)d);
974 HeapFree(GetProcessHeap(), 0, d);
977 #ifdef WORDS_BIGENDIAN
978 /* Swaps each character in str to or from little endian; assumes the conversion
979 * is symmetric, that is, that le16toh is equivalent to htole16.
981 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
985 /* Swap characters to host order.
988 for (i = 0; i < len; i++)
989 str[i] = le16toh(str[i]);
992 #define PropertyStorage_ByteSwapString(s, l)
995 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
996 * the entries according to the values of This->codePage and This->locale.
997 * FIXME: there isn't any checking whether the read property extends past the
1000 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
1003 DWORD numEntries, i;
1006 assert(This->name_to_propid);
1007 assert(This->propid_to_name);
1009 StorageUtl_ReadDWord(ptr, 0, &numEntries);
1010 TRACE("Reading %d entries:\n", numEntries);
1011 ptr += sizeof(DWORD);
1012 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1017 StorageUtl_ReadDWord(ptr, 0, &propid);
1018 ptr += sizeof(PROPID);
1019 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1020 ptr += sizeof(DWORD);
1021 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1022 /* Make sure the source string is NULL-terminated */
1023 if (This->codePage != CP_UNICODE)
1024 ptr[cbEntry - 1] = '\0';
1026 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1027 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1028 if (This->codePage == CP_UNICODE)
1030 /* Unicode entries are padded to DWORD boundaries */
1031 if (cbEntry % sizeof(DWORD))
1032 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1034 ptr += sizeof(DWORD) + cbEntry;
1039 /* FIXME: there isn't any checking whether the read property extends past the
1040 * end of the buffer.
1042 static HRESULT PropertyStorage_ReadProperty(PropertyStorage_impl *This,
1043 PROPVARIANT *prop, const BYTE *data)
1049 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1050 data += sizeof(DWORD);
1057 prop->u.cVal = *(const char *)data;
1058 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1061 prop->u.bVal = *(const UCHAR *)data;
1062 TRACE("Read byte 0x%x\n", prop->u.bVal);
1065 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1066 TRACE("Read short %d\n", prop->u.iVal);
1069 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1070 TRACE("Read ushort %d\n", prop->u.uiVal);
1074 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1075 TRACE("Read long %ld\n", prop->u.lVal);
1079 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1080 TRACE("Read ulong %d\n", prop->u.ulVal);
1086 StorageUtl_ReadDWord(data, 0, &count);
1087 if (This->codePage == CP_UNICODE && count / 2)
1089 WARN("Unicode string has odd number of bytes\n");
1090 hr = STG_E_INVALIDHEADER;
1094 prop->u.pszVal = CoTaskMemAlloc(count);
1097 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1098 /* This is stored in the code page specified in This->codePage.
1099 * Don't convert it, the caller will just store it as-is.
1101 if (This->codePage == CP_UNICODE)
1103 /* Make sure it's NULL-terminated */
1104 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1105 TRACE("Read string value %s\n",
1106 debugstr_w(prop->u.pwszVal));
1110 /* Make sure it's NULL-terminated */
1111 prop->u.pszVal[count - 1] = '\0';
1112 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1116 hr = STG_E_INSUFFICIENTMEMORY;
1124 StorageUtl_ReadDWord(data, 0, &count);
1125 prop->u.pwszVal = CoTaskMemAlloc(count * sizeof(WCHAR));
1126 if (prop->u.pwszVal)
1128 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1129 count * sizeof(WCHAR));
1130 /* make sure string is NULL-terminated */
1131 prop->u.pwszVal[count - 1] = '\0';
1132 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1133 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1136 hr = STG_E_INSUFFICIENTMEMORY;
1140 StorageUtl_ReadULargeInteger(data, 0,
1141 (ULARGE_INTEGER *)&prop->u.filetime);
1145 DWORD len = 0, tag = 0;
1147 StorageUtl_ReadDWord(data, 0, &len);
1148 StorageUtl_ReadDWord(data, 4, &tag);
1152 prop->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
1153 prop->u.pclipdata->cbSize = len;
1154 prop->u.pclipdata->ulClipFmt = tag;
1155 prop->u.pclipdata->pClipData = CoTaskMemAlloc(len);
1156 memcpy(prop->u.pclipdata->pClipData, data+8, len);
1159 hr = STG_E_INVALIDPARAMETER;
1163 FIXME("unsupported type %d\n", prop->vt);
1164 hr = STG_E_INVALIDPARAMETER;
1169 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1170 PROPERTYSETHEADER *hdr)
1172 BYTE buf[sizeof(PROPERTYSETHEADER)];
1178 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1181 if (count != sizeof(buf))
1183 WARN("read only %d\n", count);
1184 hr = STG_E_INVALIDHEADER;
1188 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1190 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1192 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1194 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1196 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1200 TRACE("returning 0x%08x\n", hr);
1204 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1205 FORMATIDOFFSET *fmt)
1207 BYTE buf[sizeof(FORMATIDOFFSET)];
1213 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1216 if (count != sizeof(buf))
1218 WARN("read only %d\n", count);
1219 hr = STG_E_INVALIDHEADER;
1223 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1225 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1229 TRACE("returning 0x%08x\n", hr);
1233 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1234 PROPERTYSECTIONHEADER *hdr)
1236 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1242 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1245 if (count != sizeof(buf))
1247 WARN("read only %d\n", count);
1248 hr = STG_E_INVALIDHEADER;
1252 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1253 cbSection), &hdr->cbSection);
1254 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1255 cProperties), &hdr->cProperties);
1258 TRACE("returning 0x%08x\n", hr);
1262 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1264 PROPERTYSETHEADER hdr;
1265 FORMATIDOFFSET fmtOffset;
1266 PROPERTYSECTIONHEADER sectionHdr;
1273 DWORD dictOffset = 0;
1275 This->dirty = FALSE;
1276 This->highestProp = 0;
1277 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1280 if (stat.cbSize.u.HighPart)
1282 WARN("stream too big\n");
1283 /* maximum size varies, but it can't be this big */
1284 hr = STG_E_INVALIDHEADER;
1287 if (stat.cbSize.u.LowPart == 0)
1289 /* empty stream is okay */
1293 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1294 sizeof(FORMATIDOFFSET))
1296 WARN("stream too small\n");
1297 hr = STG_E_INVALIDHEADER;
1301 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1304 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1305 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1308 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1310 WARN("bad magic in prop set header\n");
1311 hr = STG_E_INVALIDHEADER;
1314 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1316 WARN("bad format version %d\n", hdr.wFormat);
1317 hr = STG_E_INVALIDHEADER;
1320 This->format = hdr.wFormat;
1321 memcpy(&This->clsid, &hdr.clsid, sizeof(This->clsid));
1322 This->originatorOS = hdr.dwOSVer;
1323 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1324 WARN("File comes from a Mac, strings will probably be screwed up\n");
1325 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1328 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1330 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1331 stat.cbSize.u.LowPart);
1332 hr = STG_E_INVALIDHEADER;
1335 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1336 * follow not one, but two sections. The first is the standard properties
1337 * for the document summary information, and the second is user-defined
1338 * properties. This is the only case in which multiple sections are
1340 * Reading the second stream isn't implemented yet.
1342 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, §ionHdr);
1345 /* The section size includes the section header, so check it */
1346 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1348 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1349 hr = STG_E_INVALIDHEADER;
1352 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1353 sizeof(PROPERTYSECTIONHEADER));
1356 hr = STG_E_INSUFFICIENTMEMORY;
1359 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1360 sizeof(PROPERTYSECTIONHEADER), &count);
1363 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1364 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1366 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1367 i * sizeof(PROPERTYIDOFFSET));
1369 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1370 idOffset->dwOffset >= sectionHdr.cbSection - sizeof(DWORD))
1371 hr = STG_E_INVALIDPOINTER;
1374 if (idOffset->propid >= PID_FIRST_USABLE &&
1375 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1377 This->highestProp = idOffset->propid;
1378 if (idOffset->propid == PID_DICTIONARY)
1380 /* Don't read the dictionary yet, its entries depend on the
1381 * code page. Just store the offset so we know to read it
1384 dictOffset = idOffset->dwOffset;
1385 TRACE("Dictionary offset is %d\n", dictOffset);
1391 PropVariantInit(&prop);
1392 if (SUCCEEDED(PropertyStorage_ReadProperty(This, &prop,
1393 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER))))
1395 TRACE("Read property with ID 0x%08x, type %d\n",
1396 idOffset->propid, prop.vt);
1397 switch(idOffset->propid)
1400 if (prop.vt == VT_I2)
1401 This->codePage = (UINT)prop.u.iVal;
1404 if (prop.vt == VT_I4)
1405 This->locale = (LCID)prop.u.lVal;
1408 if (prop.vt == VT_I4 && prop.u.lVal)
1409 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1410 /* The format should already be 1, but just in case */
1414 hr = PropertyStorage_StorePropWithId(This,
1415 idOffset->propid, &prop, This->codePage);
1421 if (!This->codePage)
1423 /* default to Unicode unless told not to, as specified here:
1424 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
1426 if (This->grfFlags & PROPSETFLAG_ANSI)
1427 This->codePage = GetACP();
1429 This->codePage = CP_UNICODE;
1432 This->locale = LOCALE_SYSTEM_DEFAULT;
1433 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1435 hr = PropertyStorage_ReadDictionary(This,
1436 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1439 HeapFree(GetProcessHeap(), 0, buf);
1442 dictionary_destroy(This->name_to_propid);
1443 This->name_to_propid = NULL;
1444 dictionary_destroy(This->propid_to_name);
1445 This->propid_to_name = NULL;
1446 dictionary_destroy(This->propid_to_prop);
1447 This->propid_to_prop = NULL;
1452 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1453 PROPERTYSETHEADER *hdr)
1456 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1457 PROPSETHDR_BYTEORDER_MAGIC);
1458 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1459 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1460 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1461 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1464 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1465 FORMATIDOFFSET *fmtOffset)
1468 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1469 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1470 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1473 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1474 PROPERTYSECTIONHEADER *hdr)
1477 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1478 StorageUtl_WriteDWord((BYTE *)hdr,
1479 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1482 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1483 PROPERTYIDOFFSET *propIdOffset)
1485 assert(propIdOffset);
1486 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1487 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1488 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1491 struct DictionaryClosure
1497 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1498 const void *value, void *extra, void *closure)
1500 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
1501 struct DictionaryClosure *c = (struct DictionaryClosure *)closure;
1507 StorageUtl_WriteDWord((LPBYTE)&propid, 0, (DWORD)value);
1508 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1511 c->bytesWritten += sizeof(DWORD);
1512 if (This->codePage == CP_UNICODE)
1514 DWORD keyLen, pad = 0;
1516 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1517 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1518 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1521 c->bytesWritten += sizeof(DWORD);
1522 /* Rather than allocate a copy, I'll swap the string to little-endian
1523 * in-place, write it, then swap it back.
1525 PropertyStorage_ByteSwapString(key, keyLen);
1526 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1527 PropertyStorage_ByteSwapString(key, keyLen);
1530 c->bytesWritten += keyLen;
1531 if (keyLen % sizeof(DWORD))
1533 c->hr = IStream_Write(This->stm, &pad,
1534 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1537 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1544 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1545 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1548 c->bytesWritten += sizeof(DWORD);
1549 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1552 c->bytesWritten += keyLen;
1555 return SUCCEEDED(c->hr);
1558 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1560 /* Writes the dictionary to the stream. Assumes without checking that the
1561 * dictionary isn't empty.
1563 static HRESULT PropertyStorage_WriteDictionaryToStream(
1564 PropertyStorage_impl *This, DWORD *sectionOffset)
1568 PROPERTYIDOFFSET propIdOffset;
1571 struct DictionaryClosure closure;
1573 assert(sectionOffset);
1575 /* The dictionary's always the first property written, so seek to its
1578 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1579 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1582 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1584 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1588 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1589 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1592 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1593 dictionary_num_entries(This->name_to_propid));
1594 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1597 *sectionOffset += sizeof(dwTemp);
1600 closure.bytesWritten = 0;
1601 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1606 *sectionOffset += closure.bytesWritten;
1607 if (closure.bytesWritten % sizeof(DWORD))
1609 DWORD padding = sizeof(DWORD) - closure.bytesWritten % sizeof(DWORD);
1610 TRACE("adding %d bytes of padding\n", padding);
1611 *sectionOffset += padding;
1618 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1619 DWORD propNum, DWORD propid, PROPVARIANT *var, DWORD *sectionOffset)
1623 PROPERTYIDOFFSET propIdOffset;
1625 DWORD dwType, bytesWritten;
1628 assert(sectionOffset);
1630 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This, propNum, propid, var->vt,
1633 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1634 propNum * sizeof(PROPERTYIDOFFSET);
1635 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1638 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1639 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1643 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1644 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1647 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1648 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1651 *sectionOffset += sizeof(dwType);
1661 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1663 bytesWritten = count;
1670 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1671 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1672 bytesWritten = count;
1680 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1681 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1682 bytesWritten = count;
1689 if (This->codePage == CP_UNICODE)
1690 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1692 len = lstrlenA(var->u.pszVal) + 1;
1693 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1694 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1697 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1698 bytesWritten = count + sizeof(DWORD);
1703 DWORD len = lstrlenW(var->u.pwszVal) + 1, dwTemp;
1705 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1706 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1709 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1711 bytesWritten = count + sizeof(DWORD);
1718 StorageUtl_WriteULargeInteger((BYTE *)&temp, 0,
1719 (ULARGE_INTEGER *)&var->u.filetime);
1720 hr = IStream_Write(This->stm, &temp, sizeof(FILETIME), &count);
1721 bytesWritten = count;
1726 DWORD cf_hdr[2], len;
1728 len = var->u.pclipdata->cbSize;
1729 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[0], 0, len + 8);
1730 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[1], 0, var->u.pclipdata->ulClipFmt);
1731 hr = IStream_Write(This->stm, &cf_hdr, sizeof(cf_hdr), &count);
1734 hr = IStream_Write(This->stm, &var->u.pclipdata->pClipData, len, &count);
1737 bytesWritten = count + sizeof cf_hdr;
1741 FIXME("unsupported type: %d\n", var->vt);
1742 return STG_E_INVALIDPARAMETER;
1747 *sectionOffset += bytesWritten;
1748 if (bytesWritten % sizeof(DWORD))
1750 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1751 TRACE("adding %d bytes of padding\n", padding);
1752 *sectionOffset += padding;
1760 struct PropertyClosure
1764 DWORD *sectionOffset;
1767 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1768 void *extra, void *closure)
1770 PropertyStorage_impl *This = (PropertyStorage_impl *)extra;
1771 struct PropertyClosure *c = (struct PropertyClosure *)closure;
1777 c->hr = PropertyStorage_WritePropertyToStream(This,
1778 c->propNum++, (DWORD)key, (PROPVARIANT *)value, c->sectionOffset);
1779 return SUCCEEDED(c->hr);
1782 static HRESULT PropertyStorage_WritePropertiesToStream(
1783 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1785 struct PropertyClosure closure;
1787 assert(sectionOffset);
1789 closure.propNum = startingPropNum;
1790 closure.sectionOffset = sectionOffset;
1791 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1796 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1800 LARGE_INTEGER seek = { {0} };
1801 PROPERTYSETHEADER hdr;
1802 FORMATIDOFFSET fmtOffset;
1804 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1807 PropertyStorage_MakeHeader(This, &hdr);
1808 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1811 if (count != sizeof(hdr))
1813 hr = STG_E_WRITEFAULT;
1817 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1818 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1821 if (count != sizeof(fmtOffset))
1823 hr = STG_E_WRITEFAULT;
1832 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1834 PROPERTYSECTIONHEADER sectionHdr;
1838 DWORD numProps, prop, sectionOffset, dwTemp;
1841 PropertyStorage_WriteHeadersToStream(This);
1843 /* Count properties. Always at least one property, the code page */
1845 if (dictionary_num_entries(This->name_to_propid))
1847 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1849 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1851 numProps += dictionary_num_entries(This->propid_to_prop);
1853 /* Write section header with 0 bytes right now, I'll adjust it after
1854 * writing properties.
1856 PropertyStorage_MakeSectionHdr(0, numProps, §ionHdr);
1857 seek.QuadPart = SECTIONHEADER_OFFSET;
1858 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1861 hr = IStream_Write(This->stm, §ionHdr, sizeof(sectionHdr), &count);
1866 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1867 numProps * sizeof(PROPERTYIDOFFSET);
1869 if (dictionary_num_entries(This->name_to_propid))
1872 hr = PropertyStorage_WriteDictionaryToStream(This, §ionOffset);
1877 PropVariantInit(&var);
1880 var.u.iVal = This->codePage;
1881 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1882 &var, §ionOffset);
1886 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1889 var.u.lVal = This->locale;
1890 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1891 &var, §ionOffset);
1896 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1900 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1901 &var, §ionOffset);
1906 hr = PropertyStorage_WritePropertiesToStream(This, prop, §ionOffset);
1910 /* Now write the byte count of the section */
1911 seek.QuadPart = SECTIONHEADER_OFFSET;
1912 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1915 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1916 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1922 /***********************************************************************
1923 * PropertyStorage_Construct
1925 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1927 dictionary_destroy(This->name_to_propid);
1928 This->name_to_propid = NULL;
1929 dictionary_destroy(This->propid_to_name);
1930 This->propid_to_name = NULL;
1931 dictionary_destroy(This->propid_to_prop);
1932 This->propid_to_prop = NULL;
1935 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
1939 This->name_to_propid = dictionary_create(
1940 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1942 if (!This->name_to_propid)
1944 hr = STG_E_INSUFFICIENTMEMORY;
1947 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1949 if (!This->propid_to_name)
1951 hr = STG_E_INSUFFICIENTMEMORY;
1954 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1955 PropertyStorage_PropertyDestroy, This);
1956 if (!This->propid_to_prop)
1958 hr = STG_E_INSUFFICIENTMEMORY;
1963 PropertyStorage_DestroyDictionaries(This);
1967 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1968 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1974 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1976 return E_OUTOFMEMORY;
1978 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1980 InitializeCriticalSection(&(*pps)->cs);
1982 memcpy(&(*pps)->fmtid, rfmtid, sizeof((*pps)->fmtid));
1983 (*pps)->grfMode = grfMode;
1985 hr = PropertyStorage_CreateDictionaries(*pps);
1988 IStream_Release(stm);
1989 DeleteCriticalSection(&(*pps)->cs);
1990 HeapFree(GetProcessHeap(), 0, *pps);
1997 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
1998 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2000 PropertyStorage_impl *ps;
2004 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2007 hr = PropertyStorage_ReadFromStream(ps);
2010 *pps = (IPropertyStorage *)ps;
2011 TRACE("PropertyStorage %p constructed\n", ps);
2016 PropertyStorage_DestroyDictionaries(ps);
2017 HeapFree(GetProcessHeap(), 0, ps);
2023 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2024 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2026 PropertyStorage_impl *ps;
2030 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2034 ps->grfFlags = grfFlags;
2035 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2037 /* default to Unicode unless told not to, as specified here:
2038 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2040 if (ps->grfFlags & PROPSETFLAG_ANSI)
2041 ps->codePage = GetACP();
2043 ps->codePage = CP_UNICODE;
2044 ps->locale = LOCALE_SYSTEM_DEFAULT;
2045 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2046 *pps = (IPropertyStorage *)ps;
2047 TRACE("PropertyStorage %p constructed\n", ps);
2054 /***********************************************************************
2055 * Implementation of IPropertySetStorage
2058 /************************************************************************
2059 * IPropertySetStorage_fnQueryInterface (IUnknown)
2061 * This method forwards to the common QueryInterface implementation
2063 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2064 IPropertySetStorage *ppstg,
2068 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2069 return IStorage_QueryInterface( (IStorage*)This, riid, ppvObject );
2072 /************************************************************************
2073 * IPropertySetStorage_fnAddRef (IUnknown)
2075 * This method forwards to the common AddRef implementation
2077 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2078 IPropertySetStorage *ppstg)
2080 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2081 return IStorage_AddRef( (IStorage*)This );
2084 /************************************************************************
2085 * IPropertySetStorage_fnRelease (IUnknown)
2087 * This method forwards to the common Release implementation
2089 static ULONG WINAPI IPropertySetStorage_fnRelease(
2090 IPropertySetStorage *ppstg)
2092 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2093 return IStorage_Release( (IStorage*)This );
2096 /************************************************************************
2097 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2099 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2100 IPropertySetStorage *ppstg,
2102 const CLSID* pclsid,
2105 IPropertyStorage** ppprstg)
2107 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2108 WCHAR name[CCH_MAX_PROPSTG_NAME];
2109 IStream *stm = NULL;
2112 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2116 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2118 r = STG_E_INVALIDFLAG;
2128 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2129 * storage, not a stream. For now, disallow it.
2131 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2133 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2134 r = STG_E_INVALIDFLAG;
2138 r = FmtIdToPropStgName(rfmtid, name);
2142 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2146 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2149 TRACE("returning 0x%08x\n", r);
2153 /************************************************************************
2154 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2156 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2157 IPropertySetStorage *ppstg,
2160 IPropertyStorage** ppprstg)
2162 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2163 IStream *stm = NULL;
2164 WCHAR name[CCH_MAX_PROPSTG_NAME];
2167 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2170 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2171 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2173 r = STG_E_INVALIDFLAG;
2183 r = FmtIdToPropStgName(rfmtid, name);
2187 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2191 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2194 TRACE("returning 0x%08x\n", r);
2198 /************************************************************************
2199 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2201 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2202 IPropertySetStorage *ppstg,
2205 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2206 IStorage *stg = NULL;
2207 WCHAR name[CCH_MAX_PROPSTG_NAME];
2210 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2213 return E_INVALIDARG;
2215 r = FmtIdToPropStgName(rfmtid, name);
2219 stg = (IStorage*) This;
2220 return IStorage_DestroyElement(stg, name);
2223 /************************************************************************
2224 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2226 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2227 IPropertySetStorage *ppstg,
2228 IEnumSTATPROPSETSTG** ppenum)
2230 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2231 return create_EnumSTATPROPSETSTG(This, ppenum);
2234 /************************************************************************
2235 * Implement IEnumSTATPROPSETSTG using enumx
2237 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2238 IEnumSTATPROPSETSTG *iface,
2242 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2245 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2246 IEnumSTATPROPSETSTG *iface)
2248 return enumx_AddRef((enumx_impl*)iface);
2251 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2252 IEnumSTATPROPSETSTG *iface)
2254 return enumx_Release((enumx_impl*)iface);
2257 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2258 IEnumSTATPROPSETSTG *iface,
2260 STATPROPSETSTG *rgelt,
2261 ULONG *pceltFetched)
2263 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2266 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2267 IEnumSTATPROPSETSTG *iface,
2270 return enumx_Skip((enumx_impl*)iface, celt);
2273 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2274 IEnumSTATPROPSETSTG *iface)
2276 return enumx_Reset((enumx_impl*)iface);
2279 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2280 IEnumSTATPROPSETSTG *iface,
2281 IEnumSTATPROPSETSTG **ppenum)
2283 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2286 static HRESULT create_EnumSTATPROPSETSTG(
2288 IEnumSTATPROPSETSTG** ppenum)
2290 IStorage *stg = (IStorage*) &This->base.lpVtbl;
2291 IEnumSTATSTG *penum = NULL;
2295 STATPROPSETSTG statpss;
2298 TRACE("%p %p\n", This, ppenum);
2300 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2301 &IEnumSTATPROPSETSTG_Vtbl,
2302 sizeof (STATPROPSETSTG));
2304 /* add all the property set elements into a list */
2305 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2307 return E_OUTOFMEMORY;
2312 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2319 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2321 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2322 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2323 debugstr_guid(&statpss.fmtid));
2324 statpss.mtime = stat.mtime;
2325 statpss.atime = stat.atime;
2326 statpss.ctime = stat.ctime;
2327 statpss.grfFlags = stat.grfMode;
2328 memcpy(&statpss.clsid, &stat.clsid, sizeof stat.clsid);
2329 enumx_add_element(enumx, &statpss);
2331 CoTaskMemFree(stat.pwcsName);
2333 IEnumSTATSTG_Release(penum);
2335 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2340 /************************************************************************
2341 * Implement IEnumSTATPROPSTG using enumx
2343 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2344 IEnumSTATPROPSTG *iface,
2348 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2351 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2352 IEnumSTATPROPSTG *iface)
2354 return enumx_AddRef((enumx_impl*)iface);
2357 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2358 IEnumSTATPROPSTG *iface)
2360 return enumx_Release((enumx_impl*)iface);
2363 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2364 IEnumSTATPROPSTG *iface,
2367 ULONG *pceltFetched)
2369 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2372 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2373 IEnumSTATPROPSTG *iface,
2376 return enumx_Skip((enumx_impl*)iface, celt);
2379 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2380 IEnumSTATPROPSTG *iface)
2382 return enumx_Reset((enumx_impl*)iface);
2385 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2386 IEnumSTATPROPSTG *iface,
2387 IEnumSTATPROPSTG **ppenum)
2389 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2392 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2394 enumx_impl *enumx = arg;
2395 PROPID propid = (PROPID) k;
2396 const PROPVARIANT *prop = v;
2399 stat.lpwstrName = NULL;
2400 stat.propid = propid;
2403 enumx_add_element(enumx, &stat);
2408 static HRESULT create_EnumSTATPROPSTG(
2409 PropertyStorage_impl *This,
2410 IEnumSTATPROPSTG** ppenum)
2414 TRACE("%p %p\n", This, ppenum);
2416 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2417 &IEnumSTATPROPSTG_Vtbl,
2418 sizeof (STATPROPSTG));
2420 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2422 *ppenum = (IEnumSTATPROPSTG*) enumx;
2427 /***********************************************************************
2430 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2432 IPropertySetStorage_fnQueryInterface,
2433 IPropertySetStorage_fnAddRef,
2434 IPropertySetStorage_fnRelease,
2435 IPropertySetStorage_fnCreate,
2436 IPropertySetStorage_fnOpen,
2437 IPropertySetStorage_fnDelete,
2438 IPropertySetStorage_fnEnum
2441 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2443 IPropertyStorage_fnQueryInterface,
2444 IPropertyStorage_fnAddRef,
2445 IPropertyStorage_fnRelease,
2446 IPropertyStorage_fnReadMultiple,
2447 IPropertyStorage_fnWriteMultiple,
2448 IPropertyStorage_fnDeleteMultiple,
2449 IPropertyStorage_fnReadPropertyNames,
2450 IPropertyStorage_fnWritePropertyNames,
2451 IPropertyStorage_fnDeletePropertyNames,
2452 IPropertyStorage_fnCommit,
2453 IPropertyStorage_fnRevert,
2454 IPropertyStorage_fnEnum,
2455 IPropertyStorage_fnSetTimes,
2456 IPropertyStorage_fnSetClass,
2457 IPropertyStorage_fnStat,
2460 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2462 IEnumSTATPROPSETSTG_fnQueryInterface,
2463 IEnumSTATPROPSETSTG_fnAddRef,
2464 IEnumSTATPROPSETSTG_fnRelease,
2465 IEnumSTATPROPSETSTG_fnNext,
2466 IEnumSTATPROPSETSTG_fnSkip,
2467 IEnumSTATPROPSETSTG_fnReset,
2468 IEnumSTATPROPSETSTG_fnClone,
2471 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2473 IEnumSTATPROPSTG_fnQueryInterface,
2474 IEnumSTATPROPSTG_fnAddRef,
2475 IEnumSTATPROPSTG_fnRelease,
2476 IEnumSTATPROPSTG_fnNext,
2477 IEnumSTATPROPSTG_fnSkip,
2478 IEnumSTATPROPSTG_fnReset,
2479 IEnumSTATPROPSTG_fnClone,
2482 /***********************************************************************
2483 * Format ID <-> name conversion
2485 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2486 'I','n','f','o','r','m','a','t','i','o','n',0 };
2487 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2488 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2490 #define BITS_PER_BYTE 8
2491 #define CHARMASK 0x1f
2492 #define BITS_IN_CHARMASK 5
2493 #define NUM_ALPHA_CHARS 26
2495 /***********************************************************************
2496 * FmtIdToPropStgName [ole32.@]
2497 * Returns the storage name of the format ID rfmtid.
2499 * rfmtid [I] Format ID for which to return a storage name
2500 * str [O] Storage name associated with rfmtid.
2503 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2506 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2507 * Based on the algorithm described here:
2508 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2510 HRESULT WINAPI FmtIdToPropStgName(const FMTID *rfmtid, LPOLESTR str)
2512 static const char fmtMap[] = "abcdefghijklmnopqrstuvwxyz012345";
2514 TRACE("%s, %p\n", debugstr_guid(rfmtid), str);
2516 if (!rfmtid) return E_INVALIDARG;
2517 if (!str) return E_INVALIDARG;
2519 if (IsEqualGUID(&FMTID_SummaryInformation, rfmtid))
2520 lstrcpyW(str, szSummaryInfo);
2521 else if (IsEqualGUID(&FMTID_DocSummaryInformation, rfmtid))
2522 lstrcpyW(str, szDocSummaryInfo);
2523 else if (IsEqualGUID(&FMTID_UserDefinedProperties, rfmtid))
2524 lstrcpyW(str, szDocSummaryInfo);
2529 ULONG bitsRemaining = BITS_PER_BYTE;
2532 for (fmtptr = (const BYTE *)rfmtid; fmtptr < (const BYTE *)rfmtid + sizeof(FMTID); )
2534 ULONG i = *fmtptr >> (BITS_PER_BYTE - bitsRemaining);
2536 if (bitsRemaining >= BITS_IN_CHARMASK)
2538 *pstr = (WCHAR)(fmtMap[i & CHARMASK]);
2539 if (bitsRemaining == BITS_PER_BYTE && *pstr >= 'a' &&
2543 bitsRemaining -= BITS_IN_CHARMASK;
2544 if (bitsRemaining == 0)
2547 bitsRemaining = BITS_PER_BYTE;
2552 if (++fmtptr < (const BYTE *)rfmtid + sizeof(FMTID))
2553 i |= *fmtptr << bitsRemaining;
2554 *pstr++ = (WCHAR)(fmtMap[i & CHARMASK]);
2555 bitsRemaining += BITS_PER_BYTE - BITS_IN_CHARMASK;
2560 TRACE("returning %s\n", debugstr_w(str));
2564 /***********************************************************************
2565 * PropStgNameToFmtId [ole32.@]
2566 * Returns the format ID corresponding to the given name.
2568 * str [I] Storage name to convert to a format ID.
2569 * rfmtid [O] Format ID corresponding to str.
2572 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2573 * a format ID, S_OK otherwise.
2576 * Based on the algorithm described here:
2577 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2579 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2581 HRESULT hr = STG_E_INVALIDNAME;
2583 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2585 if (!rfmtid) return E_INVALIDARG;
2586 if (!str) return STG_E_INVALIDNAME;
2588 if (!lstrcmpiW(str, szDocSummaryInfo))
2590 memcpy(rfmtid, &FMTID_DocSummaryInformation, sizeof(*rfmtid));
2593 else if (!lstrcmpiW(str, szSummaryInfo))
2595 memcpy(rfmtid, &FMTID_SummaryInformation, sizeof(*rfmtid));
2601 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2602 const WCHAR *pstr = str;
2604 memset(rfmtid, 0, sizeof(*rfmtid));
2605 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2606 bits += BITS_IN_CHARMASK)
2608 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2614 if (wc > NUM_ALPHA_CHARS)
2617 if (wc > NUM_ALPHA_CHARS)
2619 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2622 WARN("invalid character (%d)\n", *pstr);
2627 *fmtptr |= wc << bitsUsed;
2628 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2629 if (bitsStored < BITS_IN_CHARMASK)
2631 wc >>= BITS_PER_BYTE - bitsUsed;
2632 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2636 WARN("extra bits\n");
2642 *fmtptr |= (BYTE)wc;