2 * Compound Storage (32 bit version)
3 * Storage implementation
5 * This file contains the compound file implementation
6 * of the storage interface.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Sylvain St-Germain
10 * Copyright 1999 Thuy Nguyen
11 * Copyright 2005 Mike McCormack
12 * Copyright 2005 Juan Lang
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 * - I don't honor the maximum property set size.
30 * - Certain bogus files could result in reading past the end of a buffer.
31 * - Mac-generated files won't be read correctly, even if they're little
32 * endian, because I disregard whether the generator was a Mac. This means
33 * strings will probably be munged (as I don't understand Mac scripts.)
34 * - Not all PROPVARIANT types are supported.
35 * - User defined properties are not supported, see comment in
36 * PropertyStorage_ReadFromStream
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
55 #include "dictionary.h"
56 #include "storage32.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(storage);
61 static inline StorageImpl *impl_from_IPropertySetStorage( IPropertySetStorage *iface )
63 return (StorageImpl *)((char*)iface - FIELD_OFFSET(StorageImpl, base.pssVtbl));
66 /* These are documented in MSDN,
67 * but they don't seem to be in any header file.
69 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
70 #define PROPSETHDR_OSVER_KIND_WIN16 0
71 #define PROPSETHDR_OSVER_KIND_MAC 1
72 #define PROPSETHDR_OSVER_KIND_WIN32 2
74 #define CP_UNICODE 1200
76 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
78 #define CFTAG_WINDOWS (-1L)
79 #define CFTAG_MACINTOSH (-2L)
80 #define CFTAG_FMTID (-3L)
81 #define CFTAG_NODATA 0L
83 typedef struct tagPROPERTYSETHEADER
85 WORD wByteOrder; /* always 0xfffe */
86 WORD wFormat; /* can be zero or one */
87 DWORD dwOSVer; /* OS version of originating system */
88 CLSID clsid; /* application CLSID */
89 DWORD reserved; /* always 1 */
92 typedef struct tagFORMATIDOFFSET
95 DWORD dwOffset; /* from beginning of stream */
98 typedef struct tagPROPERTYSECTIONHEADER
102 } PROPERTYSECTIONHEADER;
104 typedef struct tagPROPERTYIDOFFSET
107 DWORD dwOffset; /* from beginning of section */
110 typedef struct tagPropertyStorage_impl PropertyStorage_impl;
112 /* Initializes the property storage from the stream (and undoes any uncommitted
113 * changes in the process.) Returns an error if there is an error reading or
114 * if the stream format doesn't match what's expected.
116 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *);
118 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *);
120 /* Creates the dictionaries used by the property storage. If successful, all
121 * the dictionaries have been created. If failed, none has been. (This makes
122 * it a bit easier to deal with destroying them.)
124 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *);
126 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *);
128 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
129 * string using PropertyStorage_StringCopy.
131 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
132 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP);
134 /* Copies the string src, which is encoded using code page srcCP, and returns
135 * it in *dst, in the code page specified by targetCP. The returned string is
136 * allocated using CoTaskMemAlloc.
137 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
138 * is CP_UNICODE, the returned string is in fact an LPWSTR.
139 * Returns S_OK on success, something else on failure.
141 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
144 static const IPropertyStorageVtbl IPropertyStorage_Vtbl;
145 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl;
146 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl;
147 static HRESULT create_EnumSTATPROPSETSTG(StorageImpl *, IEnumSTATPROPSETSTG**);
148 static HRESULT create_EnumSTATPROPSTG(PropertyStorage_impl *, IEnumSTATPROPSTG**);
150 /***********************************************************************
151 * Implementation of IPropertyStorage
153 struct tagPropertyStorage_impl
155 const IPropertyStorageVtbl *vtbl;
169 struct dictionary *name_to_propid;
170 struct dictionary *propid_to_name;
171 struct dictionary *propid_to_prop;
174 /************************************************************************
175 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
177 static HRESULT WINAPI IPropertyStorage_fnQueryInterface(
178 IPropertyStorage *iface,
182 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
184 if ( (This==0) || (ppvObject==0) )
189 if (IsEqualGUID(&IID_IUnknown, riid) ||
190 IsEqualGUID(&IID_IPropertyStorage, riid))
192 IPropertyStorage_AddRef(iface);
197 return E_NOINTERFACE;
200 /************************************************************************
201 * IPropertyStorage_fnAddRef (IPropertyStorage)
203 static ULONG WINAPI IPropertyStorage_fnAddRef(
204 IPropertyStorage *iface)
206 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
207 return InterlockedIncrement(&This->ref);
210 /************************************************************************
211 * IPropertyStorage_fnRelease (IPropertyStorage)
213 static ULONG WINAPI IPropertyStorage_fnRelease(
214 IPropertyStorage *iface)
216 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
219 ref = InterlockedDecrement(&This->ref);
222 TRACE("Destroying %p\n", This);
224 IPropertyStorage_Commit(iface, STGC_DEFAULT);
225 IStream_Release(This->stm);
226 This->cs.DebugInfo->Spare[0] = 0;
227 DeleteCriticalSection(&This->cs);
228 PropertyStorage_DestroyDictionaries(This);
229 HeapFree(GetProcessHeap(), 0, This);
234 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
237 PROPVARIANT *ret = NULL;
239 dictionary_find(This->propid_to_prop, UlongToPtr(propid), (void **)&ret);
240 TRACE("returning %p\n", ret);
244 /* Returns NULL if name is NULL. */
245 static PROPVARIANT *PropertyStorage_FindPropertyByName(
246 PropertyStorage_impl *This, LPCWSTR name)
248 PROPVARIANT *ret = NULL;
253 if (This->codePage == CP_UNICODE)
255 if (dictionary_find(This->name_to_propid, name, &propid))
256 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
261 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
262 &ansiName, This->codePage);
266 if (dictionary_find(This->name_to_propid, ansiName, &propid))
267 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
268 CoTaskMemFree(ansiName);
271 TRACE("returning %p\n", ret);
275 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
280 dictionary_find(This->propid_to_name, UlongToPtr(propid), (void **)&ret);
281 TRACE("returning %p\n", ret);
285 /************************************************************************
286 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
288 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
289 IPropertyStorage* iface,
291 const PROPSPEC rgpspec[],
292 PROPVARIANT rgpropvar[])
294 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
298 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
302 if (!rgpspec || !rgpropvar)
304 EnterCriticalSection(&This->cs);
305 for (i = 0; i < cpspec; i++)
307 PropVariantInit(&rgpropvar[i]);
308 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
310 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
311 rgpspec[i].u.lpwstr);
314 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
319 switch (rgpspec[i].u.propid)
322 rgpropvar[i].vt = VT_I2;
323 rgpropvar[i].u.iVal = This->codePage;
326 rgpropvar[i].vt = VT_I4;
327 rgpropvar[i].u.lVal = This->locale;
331 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
332 rgpspec[i].u.propid);
335 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
336 GetACP(), This->codePage);
343 LeaveCriticalSection(&This->cs);
347 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
353 TRACE("%s, %p, %d, %d\n",
354 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
363 if (dstCP == CP_UNICODE)
364 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
366 len = strlen(src) + 1;
367 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
369 hr = STG_E_INSUFFICIENTMEMORY;
371 memcpy(*dst, src, len);
375 if (dstCP == CP_UNICODE)
377 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
378 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
380 hr = STG_E_INSUFFICIENTMEMORY;
382 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
386 LPCWSTR wideStr = NULL;
387 LPWSTR wideStr_tmp = NULL;
389 if (srcCP == CP_UNICODE)
390 wideStr = (LPCWSTR)src;
393 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
394 wideStr_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
397 MultiByteToWideChar(srcCP, 0, src, -1, wideStr_tmp, len);
398 wideStr = wideStr_tmp;
401 hr = STG_E_INSUFFICIENTMEMORY;
405 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
407 *dst = CoTaskMemAlloc(len);
409 hr = STG_E_INSUFFICIENTMEMORY;
412 BOOL defCharUsed = FALSE;
414 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
415 NULL, &defCharUsed) == 0 || defCharUsed)
419 hr = HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
423 HeapFree(GetProcessHeap(), 0, wideStr_tmp);
426 TRACE("returning 0x%08x (%s)\n", hr,
427 dstCP == CP_UNICODE ? debugstr_w((LPCWSTR)*dst) : debugstr_a(*dst));
431 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
432 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
438 if (propvar->vt == VT_LPSTR)
440 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
441 &prop->u.pszVal, targetCP);
446 PropVariantCopy(prop, propvar);
450 /* Stores the property with id propid and value propvar into this property
451 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
452 * type is VT_LPSTR, converts the string using lcid as the source code page
453 * and This->codePage as the target code page before storing.
454 * As a side effect, may change This->format to 1 if the type of propvar is
455 * a version 1-only property.
457 static HRESULT PropertyStorage_StorePropWithId(PropertyStorage_impl *This,
458 PROPID propid, const PROPVARIANT *propvar, LCID lcid)
461 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
464 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
472 case VT_VECTOR|VT_I1:
475 TRACE("Setting 0x%08x to type %d\n", propid, propvar->vt);
478 PropVariantClear(prop);
479 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
484 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
485 sizeof(PROPVARIANT));
488 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
492 dictionary_insert(This->propid_to_prop, UlongToPtr(propid), prop);
493 if (propid > This->highestProp)
494 This->highestProp = propid;
497 HeapFree(GetProcessHeap(), 0, prop);
500 hr = STG_E_INSUFFICIENTMEMORY;
505 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
506 * srcName is encoded in code page cp, and is converted to This->codePage.
507 * If cp is CP_UNICODE, srcName is actually a unicode string.
508 * As a side effect, may change This->format to 1 if srcName is too long for
509 * a version 0 property storage.
510 * Doesn't validate id.
512 static HRESULT PropertyStorage_StoreNameWithId(PropertyStorage_impl *This,
513 LPCSTR srcName, LCID cp, PROPID id)
520 hr = PropertyStorage_StringCopy(srcName, cp, &name, This->codePage);
523 if (This->codePage == CP_UNICODE)
525 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
530 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
533 TRACE("Adding prop name %s, propid %d\n",
534 This->codePage == CP_UNICODE ? debugstr_w((LPCWSTR)name) :
535 debugstr_a(name), id);
536 dictionary_insert(This->name_to_propid, name, UlongToPtr(id));
537 dictionary_insert(This->propid_to_name, UlongToPtr(id), name);
542 /************************************************************************
543 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
545 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
546 IPropertyStorage* iface,
548 const PROPSPEC rgpspec[],
549 const PROPVARIANT rgpropvar[],
550 PROPID propidNameFirst)
552 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
556 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
558 if (cpspec && (!rgpspec || !rgpropvar))
560 if (!(This->grfMode & STGM_READWRITE))
561 return STG_E_ACCESSDENIED;
562 EnterCriticalSection(&This->cs);
564 This->originatorOS = (DWORD)MAKELONG(LOWORD(GetVersion()),
565 PROPSETHDR_OSVER_KIND_WIN32) ;
566 for (i = 0; i < cpspec; i++)
568 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
570 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
571 rgpspec[i].u.lpwstr);
574 PropVariantCopy(prop, &rgpropvar[i]);
577 /* Note that I don't do the special cases here that I do below,
578 * because naming the special PIDs isn't supported.
580 if (propidNameFirst < PID_FIRST_USABLE ||
581 propidNameFirst >= PID_MIN_READONLY)
582 hr = STG_E_INVALIDPARAMETER;
585 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
587 hr = PropertyStorage_StoreNameWithId(This,
588 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
590 hr = PropertyStorage_StorePropWithId(This, nextId,
591 &rgpropvar[i], GetACP());
597 switch (rgpspec[i].u.propid)
600 /* Can't set the dictionary */
601 hr = STG_E_INVALIDPARAMETER;
604 /* Can only set the code page if nothing else has been set */
605 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
606 rgpropvar[i].vt == VT_I2)
608 This->codePage = rgpropvar[i].u.iVal;
609 if (This->codePage == CP_UNICODE)
610 This->grfFlags &= ~PROPSETFLAG_ANSI;
612 This->grfFlags |= PROPSETFLAG_ANSI;
615 hr = STG_E_INVALIDPARAMETER;
618 /* Can only set the locale if nothing else has been set */
619 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
620 rgpropvar[i].vt == VT_I4)
621 This->locale = rgpropvar[i].u.lVal;
623 hr = STG_E_INVALIDPARAMETER;
626 /* silently ignore like MSDN says */
629 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
630 hr = STG_E_INVALIDPARAMETER;
632 hr = PropertyStorage_StorePropWithId(This,
633 rgpspec[i].u.propid, &rgpropvar[i], GetACP());
637 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
638 IPropertyStorage_Commit(iface, STGC_DEFAULT);
639 LeaveCriticalSection(&This->cs);
643 /************************************************************************
644 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
646 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
647 IPropertyStorage* iface,
649 const PROPSPEC rgpspec[])
651 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
655 TRACE("(%p, %d, %p)\n", iface, cpspec, rgpspec);
657 if (cpspec && !rgpspec)
659 if (!(This->grfMode & STGM_READWRITE))
660 return STG_E_ACCESSDENIED;
662 EnterCriticalSection(&This->cs);
664 for (i = 0; i < cpspec; i++)
666 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
670 if (dictionary_find(This->name_to_propid, rgpspec[i].u.lpwstr, &propid))
671 dictionary_remove(This->propid_to_prop, propid);
675 if (rgpspec[i].u.propid >= PID_FIRST_USABLE &&
676 rgpspec[i].u.propid < PID_MIN_READONLY)
677 dictionary_remove(This->propid_to_prop, UlongToPtr(rgpspec[i].u.propid));
679 hr = STG_E_INVALIDPARAMETER;
682 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
683 IPropertyStorage_Commit(iface, STGC_DEFAULT);
684 LeaveCriticalSection(&This->cs);
688 /************************************************************************
689 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
691 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
692 IPropertyStorage* iface,
694 const PROPID rgpropid[],
695 LPOLESTR rglpwstrName[])
697 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
699 HRESULT hr = S_FALSE;
701 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
703 if (cpropid && (!rgpropid || !rglpwstrName))
705 EnterCriticalSection(&This->cs);
706 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
708 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
712 size_t len = lstrlenW(name);
715 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
717 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
719 hr = STG_E_INSUFFICIENTMEMORY;
722 rglpwstrName[i] = NULL;
724 LeaveCriticalSection(&This->cs);
728 /************************************************************************
729 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
731 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
732 IPropertyStorage* iface,
734 const PROPID rgpropid[],
735 const LPOLESTR rglpwstrName[])
737 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
741 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
743 if (cpropid && (!rgpropid || !rglpwstrName))
745 if (!(This->grfMode & STGM_READWRITE))
746 return STG_E_ACCESSDENIED;
748 EnterCriticalSection(&This->cs);
750 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
752 if (rgpropid[i] != PID_ILLEGAL)
753 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
754 CP_UNICODE, rgpropid[i]);
756 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
757 IPropertyStorage_Commit(iface, STGC_DEFAULT);
758 LeaveCriticalSection(&This->cs);
762 /************************************************************************
763 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
765 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
766 IPropertyStorage* iface,
768 const PROPID rgpropid[])
770 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
774 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
776 if (cpropid && !rgpropid)
778 if (!(This->grfMode & STGM_READWRITE))
779 return STG_E_ACCESSDENIED;
781 EnterCriticalSection(&This->cs);
783 for (i = 0; i < cpropid; i++)
787 if (dictionary_find(This->propid_to_name, UlongToPtr(rgpropid[i]), (void **)&name))
789 dictionary_remove(This->propid_to_name, UlongToPtr(rgpropid[i]));
790 dictionary_remove(This->name_to_propid, name);
793 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
794 IPropertyStorage_Commit(iface, STGC_DEFAULT);
795 LeaveCriticalSection(&This->cs);
799 /************************************************************************
800 * IPropertyStorage_fnCommit (IPropertyStorage)
802 static HRESULT WINAPI IPropertyStorage_fnCommit(
803 IPropertyStorage* iface,
804 DWORD grfCommitFlags)
806 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
809 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
811 if (!(This->grfMode & STGM_READWRITE))
812 return STG_E_ACCESSDENIED;
813 EnterCriticalSection(&This->cs);
815 hr = PropertyStorage_WriteToStream(This);
818 LeaveCriticalSection(&This->cs);
822 /************************************************************************
823 * IPropertyStorage_fnRevert (IPropertyStorage)
825 static HRESULT WINAPI IPropertyStorage_fnRevert(
826 IPropertyStorage* iface)
829 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
831 TRACE("%p\n", iface);
833 EnterCriticalSection(&This->cs);
836 PropertyStorage_DestroyDictionaries(This);
837 hr = PropertyStorage_CreateDictionaries(This);
839 hr = PropertyStorage_ReadFromStream(This);
843 LeaveCriticalSection(&This->cs);
847 /************************************************************************
848 * IPropertyStorage_fnEnum (IPropertyStorage)
850 static HRESULT WINAPI IPropertyStorage_fnEnum(
851 IPropertyStorage* iface,
852 IEnumSTATPROPSTG** ppenum)
854 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
855 return create_EnumSTATPROPSTG(This, ppenum);
858 /************************************************************************
859 * IPropertyStorage_fnSetTimes (IPropertyStorage)
861 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
862 IPropertyStorage* iface,
863 const FILETIME* pctime,
864 const FILETIME* patime,
865 const FILETIME* pmtime)
871 /************************************************************************
872 * IPropertyStorage_fnSetClass (IPropertyStorage)
874 static HRESULT WINAPI IPropertyStorage_fnSetClass(
875 IPropertyStorage* iface,
878 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
880 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
884 if (!(This->grfMode & STGM_READWRITE))
885 return STG_E_ACCESSDENIED;
886 This->clsid = *clsid;
888 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
889 IPropertyStorage_Commit(iface, STGC_DEFAULT);
893 /************************************************************************
894 * IPropertyStorage_fnStat (IPropertyStorage)
896 static HRESULT WINAPI IPropertyStorage_fnStat(
897 IPropertyStorage* iface,
898 STATPROPSETSTG* statpsstg)
900 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
904 TRACE("%p, %p\n", iface, statpsstg);
909 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
912 statpsstg->fmtid = This->fmtid;
913 statpsstg->clsid = This->clsid;
914 statpsstg->grfFlags = This->grfFlags;
915 statpsstg->mtime = stat.mtime;
916 statpsstg->ctime = stat.ctime;
917 statpsstg->atime = stat.atime;
918 statpsstg->dwOSVersion = This->originatorOS;
923 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
926 PropertyStorage_impl *This = extra;
928 if (This->codePage == CP_UNICODE)
930 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
931 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
932 return lstrcmpW(a, b);
934 return lstrcmpiW(a, b);
938 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
939 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
940 return lstrcmpA(a, b);
942 return lstrcmpiA(a, b);
946 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
951 static int PropertyStorage_PropCompare(const void *a, const void *b,
954 TRACE("(%d, %d)\n", PtrToUlong(a), PtrToUlong(b));
955 return PtrToUlong(a) - PtrToUlong(b);
958 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
961 HeapFree(GetProcessHeap(), 0, d);
964 #ifdef WORDS_BIGENDIAN
965 /* Swaps each character in str to or from little endian; assumes the conversion
966 * is symmetric, that is, that lendian16toh is equivalent to htole16.
968 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
972 /* Swap characters to host order.
975 for (i = 0; i < len; i++)
976 str[i] = lendian16toh(str[i]);
979 #define PropertyStorage_ByteSwapString(s, l)
982 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
983 * the entries according to the values of This->codePage and This->locale.
984 * FIXME: there isn't any checking whether the read property extends past the
987 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
993 assert(This->name_to_propid);
994 assert(This->propid_to_name);
996 StorageUtl_ReadDWord(ptr, 0, &numEntries);
997 TRACE("Reading %d entries:\n", numEntries);
998 ptr += sizeof(DWORD);
999 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1004 StorageUtl_ReadDWord(ptr, 0, &propid);
1005 ptr += sizeof(PROPID);
1006 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1007 ptr += sizeof(DWORD);
1008 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1009 /* Make sure the source string is NULL-terminated */
1010 if (This->codePage != CP_UNICODE)
1011 ptr[cbEntry - 1] = '\0';
1013 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1014 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1015 if (This->codePage == CP_UNICODE)
1017 /* Unicode entries are padded to DWORD boundaries */
1018 if (cbEntry % sizeof(DWORD))
1019 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1021 ptr += sizeof(DWORD) + cbEntry;
1026 /* FIXME: there isn't any checking whether the read property extends past the
1027 * end of the buffer.
1029 static HRESULT PropertyStorage_ReadProperty(PropertyStorage_impl *This,
1030 PROPVARIANT *prop, const BYTE *data)
1036 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1037 data += sizeof(DWORD);
1044 prop->u.cVal = *(const char *)data;
1045 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1048 prop->u.bVal = *data;
1049 TRACE("Read byte 0x%x\n", prop->u.bVal);
1052 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1053 TRACE("Read short %d\n", prop->u.iVal);
1056 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1057 TRACE("Read ushort %d\n", prop->u.uiVal);
1061 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1062 TRACE("Read long %d\n", prop->u.lVal);
1066 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1067 TRACE("Read ulong %d\n", prop->u.ulVal);
1073 StorageUtl_ReadDWord(data, 0, &count);
1074 if (This->codePage == CP_UNICODE && count / 2)
1076 WARN("Unicode string has odd number of bytes\n");
1077 hr = STG_E_INVALIDHEADER;
1081 prop->u.pszVal = CoTaskMemAlloc(count);
1084 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1085 /* This is stored in the code page specified in This->codePage.
1086 * Don't convert it, the caller will just store it as-is.
1088 if (This->codePage == CP_UNICODE)
1090 /* Make sure it's NULL-terminated */
1091 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1092 TRACE("Read string value %s\n",
1093 debugstr_w(prop->u.pwszVal));
1097 /* Make sure it's NULL-terminated */
1098 prop->u.pszVal[count - 1] = '\0';
1099 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1103 hr = STG_E_INSUFFICIENTMEMORY;
1111 StorageUtl_ReadDWord(data, 0, &count);
1112 prop->u.pwszVal = CoTaskMemAlloc(count * sizeof(WCHAR));
1113 if (prop->u.pwszVal)
1115 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1116 count * sizeof(WCHAR));
1117 /* make sure string is NULL-terminated */
1118 prop->u.pwszVal[count - 1] = '\0';
1119 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1120 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1123 hr = STG_E_INSUFFICIENTMEMORY;
1127 StorageUtl_ReadULargeInteger(data, 0,
1128 (ULARGE_INTEGER *)&prop->u.filetime);
1132 DWORD len = 0, tag = 0;
1134 StorageUtl_ReadDWord(data, 0, &len);
1135 StorageUtl_ReadDWord(data, 4, &tag);
1139 prop->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
1140 prop->u.pclipdata->cbSize = len;
1141 prop->u.pclipdata->ulClipFmt = tag;
1142 prop->u.pclipdata->pClipData = CoTaskMemAlloc(len - sizeof(prop->u.pclipdata->ulClipFmt));
1143 memcpy(prop->u.pclipdata->pClipData, data+8, len - sizeof(prop->u.pclipdata->ulClipFmt));
1146 hr = STG_E_INVALIDPARAMETER;
1150 FIXME("unsupported type %d\n", prop->vt);
1151 hr = STG_E_INVALIDPARAMETER;
1156 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1157 PROPERTYSETHEADER *hdr)
1159 BYTE buf[sizeof(PROPERTYSETHEADER)];
1165 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1168 if (count != sizeof(buf))
1170 WARN("read only %d\n", count);
1171 hr = STG_E_INVALIDHEADER;
1175 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1177 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1179 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1181 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1183 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1187 TRACE("returning 0x%08x\n", hr);
1191 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1192 FORMATIDOFFSET *fmt)
1194 BYTE buf[sizeof(FORMATIDOFFSET)];
1200 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1203 if (count != sizeof(buf))
1205 WARN("read only %d\n", count);
1206 hr = STG_E_INVALIDHEADER;
1210 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1212 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1216 TRACE("returning 0x%08x\n", hr);
1220 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1221 PROPERTYSECTIONHEADER *hdr)
1223 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1229 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1232 if (count != sizeof(buf))
1234 WARN("read only %d\n", count);
1235 hr = STG_E_INVALIDHEADER;
1239 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1240 cbSection), &hdr->cbSection);
1241 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1242 cProperties), &hdr->cProperties);
1245 TRACE("returning 0x%08x\n", hr);
1249 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1251 PROPERTYSETHEADER hdr;
1252 FORMATIDOFFSET fmtOffset;
1253 PROPERTYSECTIONHEADER sectionHdr;
1260 DWORD dictOffset = 0;
1262 This->dirty = FALSE;
1263 This->highestProp = 0;
1264 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1267 if (stat.cbSize.u.HighPart)
1269 WARN("stream too big\n");
1270 /* maximum size varies, but it can't be this big */
1271 hr = STG_E_INVALIDHEADER;
1274 if (stat.cbSize.u.LowPart == 0)
1276 /* empty stream is okay */
1280 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1281 sizeof(FORMATIDOFFSET))
1283 WARN("stream too small\n");
1284 hr = STG_E_INVALIDHEADER;
1288 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1291 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1292 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1295 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1297 WARN("bad magic in prop set header\n");
1298 hr = STG_E_INVALIDHEADER;
1301 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1303 WARN("bad format version %d\n", hdr.wFormat);
1304 hr = STG_E_INVALIDHEADER;
1307 This->format = hdr.wFormat;
1308 This->clsid = hdr.clsid;
1309 This->originatorOS = hdr.dwOSVer;
1310 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1311 WARN("File comes from a Mac, strings will probably be screwed up\n");
1312 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1315 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1317 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1318 stat.cbSize.u.LowPart);
1319 hr = STG_E_INVALIDHEADER;
1322 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1323 * follow not one, but two sections. The first is the standard properties
1324 * for the document summary information, and the second is user-defined
1325 * properties. This is the only case in which multiple sections are
1327 * Reading the second stream isn't implemented yet.
1329 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, §ionHdr);
1332 /* The section size includes the section header, so check it */
1333 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1335 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1336 hr = STG_E_INVALIDHEADER;
1339 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1340 sizeof(PROPERTYSECTIONHEADER));
1343 hr = STG_E_INSUFFICIENTMEMORY;
1346 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1347 sizeof(PROPERTYSECTIONHEADER), &count);
1350 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1351 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1353 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1354 i * sizeof(PROPERTYIDOFFSET));
1356 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1357 idOffset->dwOffset >= sectionHdr.cbSection - sizeof(DWORD))
1358 hr = STG_E_INVALIDPOINTER;
1361 if (idOffset->propid >= PID_FIRST_USABLE &&
1362 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1364 This->highestProp = idOffset->propid;
1365 if (idOffset->propid == PID_DICTIONARY)
1367 /* Don't read the dictionary yet, its entries depend on the
1368 * code page. Just store the offset so we know to read it
1371 dictOffset = idOffset->dwOffset;
1372 TRACE("Dictionary offset is %d\n", dictOffset);
1378 PropVariantInit(&prop);
1379 if (SUCCEEDED(PropertyStorage_ReadProperty(This, &prop,
1380 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER))))
1382 TRACE("Read property with ID 0x%08x, type %d\n",
1383 idOffset->propid, prop.vt);
1384 switch(idOffset->propid)
1387 if (prop.vt == VT_I2)
1388 This->codePage = (UINT)prop.u.iVal;
1391 if (prop.vt == VT_I4)
1392 This->locale = (LCID)prop.u.lVal;
1395 if (prop.vt == VT_I4 && prop.u.lVal)
1396 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1397 /* The format should already be 1, but just in case */
1401 hr = PropertyStorage_StorePropWithId(This,
1402 idOffset->propid, &prop, This->codePage);
1405 PropVariantClear(&prop);
1409 if (!This->codePage)
1411 /* default to Unicode unless told not to, as specified on msdn */
1412 if (This->grfFlags & PROPSETFLAG_ANSI)
1413 This->codePage = GetACP();
1415 This->codePage = CP_UNICODE;
1418 This->locale = LOCALE_SYSTEM_DEFAULT;
1419 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1421 hr = PropertyStorage_ReadDictionary(This,
1422 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1425 HeapFree(GetProcessHeap(), 0, buf);
1428 dictionary_destroy(This->name_to_propid);
1429 This->name_to_propid = NULL;
1430 dictionary_destroy(This->propid_to_name);
1431 This->propid_to_name = NULL;
1432 dictionary_destroy(This->propid_to_prop);
1433 This->propid_to_prop = NULL;
1438 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1439 PROPERTYSETHEADER *hdr)
1442 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1443 PROPSETHDR_BYTEORDER_MAGIC);
1444 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1445 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1446 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1447 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1450 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1451 FORMATIDOFFSET *fmtOffset)
1454 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1455 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1456 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1459 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1460 PROPERTYSECTIONHEADER *hdr)
1463 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1464 StorageUtl_WriteDWord((BYTE *)hdr,
1465 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1468 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1469 PROPERTYIDOFFSET *propIdOffset)
1471 assert(propIdOffset);
1472 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1473 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1474 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1477 static inline HRESULT PropertStorage_WriteWStringToStream(IStream *stm,
1478 LPCWSTR str, DWORD len, DWORD *written)
1480 #ifdef WORDS_BIGENDIAN
1481 WCHAR *leStr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1485 return E_OUTOFMEMORY;
1486 memcpy(leStr, str, len * sizeof(WCHAR));
1487 PropertyStorage_ByteSwapString(leStr, len);
1488 hr = IStream_Write(stm, leStr, len, written);
1489 HeapFree(GetProcessHeap(), 0, leStr);
1492 return IStream_Write(stm, str, len, written);
1496 struct DictionaryClosure
1502 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1503 const void *value, void *extra, void *closure)
1505 PropertyStorage_impl *This = extra;
1506 struct DictionaryClosure *c = closure;
1512 StorageUtl_WriteDWord((LPBYTE)&propid, 0, PtrToUlong(value));
1513 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1516 c->bytesWritten += sizeof(DWORD);
1517 if (This->codePage == CP_UNICODE)
1519 DWORD keyLen, pad = 0;
1521 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1522 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1523 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1526 c->bytesWritten += sizeof(DWORD);
1527 c->hr = PropertStorage_WriteWStringToStream(This->stm, key, keyLen,
1531 c->bytesWritten += keyLen * sizeof(WCHAR);
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,
1736 len - sizeof(var->u.pclipdata->ulClipFmt), &count);
1739 bytesWritten = count + sizeof cf_hdr;
1743 FIXME("unsupported type: %d\n", var->vt);
1744 return STG_E_INVALIDPARAMETER;
1749 *sectionOffset += bytesWritten;
1750 if (bytesWritten % sizeof(DWORD))
1752 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1753 TRACE("adding %d bytes of padding\n", padding);
1754 *sectionOffset += padding;
1762 struct PropertyClosure
1766 DWORD *sectionOffset;
1769 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1770 void *extra, void *closure)
1772 PropertyStorage_impl *This = extra;
1773 struct PropertyClosure *c = closure;
1779 c->hr = PropertyStorage_WritePropertyToStream(This, c->propNum++,
1780 PtrToUlong(key), value, c->sectionOffset);
1781 return SUCCEEDED(c->hr);
1784 static HRESULT PropertyStorage_WritePropertiesToStream(
1785 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1787 struct PropertyClosure closure;
1789 assert(sectionOffset);
1791 closure.propNum = startingPropNum;
1792 closure.sectionOffset = sectionOffset;
1793 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1798 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1802 LARGE_INTEGER seek = { {0} };
1803 PROPERTYSETHEADER hdr;
1804 FORMATIDOFFSET fmtOffset;
1806 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1809 PropertyStorage_MakeHeader(This, &hdr);
1810 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1813 if (count != sizeof(hdr))
1815 hr = STG_E_WRITEFAULT;
1819 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1820 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1823 if (count != sizeof(fmtOffset))
1825 hr = STG_E_WRITEFAULT;
1834 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1836 PROPERTYSECTIONHEADER sectionHdr;
1840 DWORD numProps, prop, sectionOffset, dwTemp;
1843 PropertyStorage_WriteHeadersToStream(This);
1845 /* Count properties. Always at least one property, the code page */
1847 if (dictionary_num_entries(This->name_to_propid))
1849 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1851 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1853 numProps += dictionary_num_entries(This->propid_to_prop);
1855 /* Write section header with 0 bytes right now, I'll adjust it after
1856 * writing properties.
1858 PropertyStorage_MakeSectionHdr(0, numProps, §ionHdr);
1859 seek.QuadPart = SECTIONHEADER_OFFSET;
1860 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1863 hr = IStream_Write(This->stm, §ionHdr, sizeof(sectionHdr), &count);
1868 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1869 numProps * sizeof(PROPERTYIDOFFSET);
1871 if (dictionary_num_entries(This->name_to_propid))
1874 hr = PropertyStorage_WriteDictionaryToStream(This, §ionOffset);
1879 PropVariantInit(&var);
1882 var.u.iVal = This->codePage;
1883 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1884 &var, §ionOffset);
1888 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1891 var.u.lVal = This->locale;
1892 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1893 &var, §ionOffset);
1898 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1902 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1903 &var, §ionOffset);
1908 hr = PropertyStorage_WritePropertiesToStream(This, prop, §ionOffset);
1912 /* Now write the byte count of the section */
1913 seek.QuadPart = SECTIONHEADER_OFFSET;
1914 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1917 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1918 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1924 /***********************************************************************
1925 * PropertyStorage_Construct
1927 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1929 dictionary_destroy(This->name_to_propid);
1930 This->name_to_propid = NULL;
1931 dictionary_destroy(This->propid_to_name);
1932 This->propid_to_name = NULL;
1933 dictionary_destroy(This->propid_to_prop);
1934 This->propid_to_prop = NULL;
1937 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
1941 This->name_to_propid = dictionary_create(
1942 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1944 if (!This->name_to_propid)
1946 hr = STG_E_INSUFFICIENTMEMORY;
1949 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1951 if (!This->propid_to_name)
1953 hr = STG_E_INSUFFICIENTMEMORY;
1956 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1957 PropertyStorage_PropertyDestroy, This);
1958 if (!This->propid_to_prop)
1960 hr = STG_E_INSUFFICIENTMEMORY;
1965 PropertyStorage_DestroyDictionaries(This);
1969 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1970 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1976 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1978 return E_OUTOFMEMORY;
1980 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1982 InitializeCriticalSection(&(*pps)->cs);
1983 (*pps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PropertyStorage_impl.cs");
1985 (*pps)->fmtid = *rfmtid;
1986 (*pps)->grfMode = grfMode;
1988 hr = PropertyStorage_CreateDictionaries(*pps);
1991 IStream_Release(stm);
1992 (*pps)->cs.DebugInfo->Spare[0] = 0;
1993 DeleteCriticalSection(&(*pps)->cs);
1994 HeapFree(GetProcessHeap(), 0, *pps);
2001 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
2002 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2004 PropertyStorage_impl *ps;
2008 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2011 hr = PropertyStorage_ReadFromStream(ps);
2014 *pps = (IPropertyStorage *)ps;
2015 TRACE("PropertyStorage %p constructed\n", ps);
2020 PropertyStorage_DestroyDictionaries(ps);
2021 HeapFree(GetProcessHeap(), 0, ps);
2027 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2028 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2030 PropertyStorage_impl *ps;
2034 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2038 ps->grfFlags = grfFlags;
2039 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2041 /* default to Unicode unless told not to, as specified on msdn */
2042 if (ps->grfFlags & PROPSETFLAG_ANSI)
2043 ps->codePage = GetACP();
2045 ps->codePage = CP_UNICODE;
2046 ps->locale = LOCALE_SYSTEM_DEFAULT;
2047 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2048 *pps = (IPropertyStorage *)ps;
2049 TRACE("PropertyStorage %p constructed\n", ps);
2056 /***********************************************************************
2057 * Implementation of IPropertySetStorage
2060 /************************************************************************
2061 * IPropertySetStorage_fnQueryInterface (IUnknown)
2063 * This method forwards to the common QueryInterface implementation
2065 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2066 IPropertySetStorage *ppstg,
2070 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2071 return IStorage_QueryInterface( (IStorage*)This, riid, ppvObject );
2074 /************************************************************************
2075 * IPropertySetStorage_fnAddRef (IUnknown)
2077 * This method forwards to the common AddRef implementation
2079 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2080 IPropertySetStorage *ppstg)
2082 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2083 return IStorage_AddRef( (IStorage*)This );
2086 /************************************************************************
2087 * IPropertySetStorage_fnRelease (IUnknown)
2089 * This method forwards to the common Release implementation
2091 static ULONG WINAPI IPropertySetStorage_fnRelease(
2092 IPropertySetStorage *ppstg)
2094 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2095 return IStorage_Release( (IStorage*)This );
2098 /************************************************************************
2099 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2101 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2102 IPropertySetStorage *ppstg,
2104 const CLSID* pclsid,
2107 IPropertyStorage** ppprstg)
2109 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2110 WCHAR name[CCH_MAX_PROPSTG_NAME];
2111 IStream *stm = NULL;
2114 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2118 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2120 r = STG_E_INVALIDFLAG;
2130 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2131 * storage, not a stream. For now, disallow it.
2133 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2135 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2136 r = STG_E_INVALIDFLAG;
2140 r = FmtIdToPropStgName(rfmtid, name);
2144 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2148 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2151 TRACE("returning 0x%08x\n", r);
2155 /************************************************************************
2156 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2158 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2159 IPropertySetStorage *ppstg,
2162 IPropertyStorage** ppprstg)
2164 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2165 IStream *stm = NULL;
2166 WCHAR name[CCH_MAX_PROPSTG_NAME];
2169 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2172 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2173 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2175 r = STG_E_INVALIDFLAG;
2185 r = FmtIdToPropStgName(rfmtid, name);
2189 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2193 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2196 TRACE("returning 0x%08x\n", r);
2200 /************************************************************************
2201 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2203 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2204 IPropertySetStorage *ppstg,
2207 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2208 IStorage *stg = NULL;
2209 WCHAR name[CCH_MAX_PROPSTG_NAME];
2212 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2215 return E_INVALIDARG;
2217 r = FmtIdToPropStgName(rfmtid, name);
2221 stg = (IStorage*) This;
2222 return IStorage_DestroyElement(stg, name);
2225 /************************************************************************
2226 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2228 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2229 IPropertySetStorage *ppstg,
2230 IEnumSTATPROPSETSTG** ppenum)
2232 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2233 return create_EnumSTATPROPSETSTG(This, ppenum);
2236 /************************************************************************
2237 * Implement IEnumSTATPROPSETSTG using enumx
2239 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2240 IEnumSTATPROPSETSTG *iface,
2244 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2247 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2248 IEnumSTATPROPSETSTG *iface)
2250 return enumx_AddRef((enumx_impl*)iface);
2253 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2254 IEnumSTATPROPSETSTG *iface)
2256 return enumx_Release((enumx_impl*)iface);
2259 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2260 IEnumSTATPROPSETSTG *iface,
2262 STATPROPSETSTG *rgelt,
2263 ULONG *pceltFetched)
2265 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2268 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2269 IEnumSTATPROPSETSTG *iface,
2272 return enumx_Skip((enumx_impl*)iface, celt);
2275 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2276 IEnumSTATPROPSETSTG *iface)
2278 return enumx_Reset((enumx_impl*)iface);
2281 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2282 IEnumSTATPROPSETSTG *iface,
2283 IEnumSTATPROPSETSTG **ppenum)
2285 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2288 static HRESULT create_EnumSTATPROPSETSTG(
2290 IEnumSTATPROPSETSTG** ppenum)
2292 IStorage *stg = (IStorage*) &This->base.lpVtbl;
2293 IEnumSTATSTG *penum = NULL;
2297 STATPROPSETSTG statpss;
2300 TRACE("%p %p\n", This, ppenum);
2302 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2303 &IEnumSTATPROPSETSTG_Vtbl,
2304 sizeof (STATPROPSETSTG));
2306 /* add all the property set elements into a list */
2307 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2309 return E_OUTOFMEMORY;
2314 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2321 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2323 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2324 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2325 debugstr_guid(&statpss.fmtid));
2326 statpss.mtime = stat.mtime;
2327 statpss.atime = stat.atime;
2328 statpss.ctime = stat.ctime;
2329 statpss.grfFlags = stat.grfMode;
2330 statpss.clsid = stat.clsid;
2331 enumx_add_element(enumx, &statpss);
2333 CoTaskMemFree(stat.pwcsName);
2335 IEnumSTATSTG_Release(penum);
2337 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2342 /************************************************************************
2343 * Implement IEnumSTATPROPSTG using enumx
2345 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2346 IEnumSTATPROPSTG *iface,
2350 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2353 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2354 IEnumSTATPROPSTG *iface)
2356 return enumx_AddRef((enumx_impl*)iface);
2359 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2360 IEnumSTATPROPSTG *iface)
2362 return enumx_Release((enumx_impl*)iface);
2365 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2366 IEnumSTATPROPSTG *iface,
2369 ULONG *pceltFetched)
2371 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2374 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2375 IEnumSTATPROPSTG *iface,
2378 return enumx_Skip((enumx_impl*)iface, celt);
2381 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2382 IEnumSTATPROPSTG *iface)
2384 return enumx_Reset((enumx_impl*)iface);
2387 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2388 IEnumSTATPROPSTG *iface,
2389 IEnumSTATPROPSTG **ppenum)
2391 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2394 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2396 enumx_impl *enumx = arg;
2397 PROPID propid = PtrToUlong(k);
2398 const PROPVARIANT *prop = v;
2401 stat.lpwstrName = NULL;
2402 stat.propid = propid;
2405 enumx_add_element(enumx, &stat);
2410 static HRESULT create_EnumSTATPROPSTG(
2411 PropertyStorage_impl *This,
2412 IEnumSTATPROPSTG** ppenum)
2416 TRACE("%p %p\n", This, ppenum);
2418 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2419 &IEnumSTATPROPSTG_Vtbl,
2420 sizeof (STATPROPSTG));
2422 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2424 *ppenum = (IEnumSTATPROPSTG*) enumx;
2429 /***********************************************************************
2432 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2434 IPropertySetStorage_fnQueryInterface,
2435 IPropertySetStorage_fnAddRef,
2436 IPropertySetStorage_fnRelease,
2437 IPropertySetStorage_fnCreate,
2438 IPropertySetStorage_fnOpen,
2439 IPropertySetStorage_fnDelete,
2440 IPropertySetStorage_fnEnum
2443 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2445 IPropertyStorage_fnQueryInterface,
2446 IPropertyStorage_fnAddRef,
2447 IPropertyStorage_fnRelease,
2448 IPropertyStorage_fnReadMultiple,
2449 IPropertyStorage_fnWriteMultiple,
2450 IPropertyStorage_fnDeleteMultiple,
2451 IPropertyStorage_fnReadPropertyNames,
2452 IPropertyStorage_fnWritePropertyNames,
2453 IPropertyStorage_fnDeletePropertyNames,
2454 IPropertyStorage_fnCommit,
2455 IPropertyStorage_fnRevert,
2456 IPropertyStorage_fnEnum,
2457 IPropertyStorage_fnSetTimes,
2458 IPropertyStorage_fnSetClass,
2459 IPropertyStorage_fnStat,
2462 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2464 IEnumSTATPROPSETSTG_fnQueryInterface,
2465 IEnumSTATPROPSETSTG_fnAddRef,
2466 IEnumSTATPROPSETSTG_fnRelease,
2467 IEnumSTATPROPSETSTG_fnNext,
2468 IEnumSTATPROPSETSTG_fnSkip,
2469 IEnumSTATPROPSETSTG_fnReset,
2470 IEnumSTATPROPSETSTG_fnClone,
2473 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2475 IEnumSTATPROPSTG_fnQueryInterface,
2476 IEnumSTATPROPSTG_fnAddRef,
2477 IEnumSTATPROPSTG_fnRelease,
2478 IEnumSTATPROPSTG_fnNext,
2479 IEnumSTATPROPSTG_fnSkip,
2480 IEnumSTATPROPSTG_fnReset,
2481 IEnumSTATPROPSTG_fnClone,
2484 /***********************************************************************
2485 * Format ID <-> name conversion
2487 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2488 'I','n','f','o','r','m','a','t','i','o','n',0 };
2489 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2490 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2492 #define BITS_PER_BYTE 8
2493 #define CHARMASK 0x1f
2494 #define BITS_IN_CHARMASK 5
2495 #define NUM_ALPHA_CHARS 26
2497 /***********************************************************************
2498 * FmtIdToPropStgName [ole32.@]
2499 * Returns the storage name of the format ID rfmtid.
2501 * rfmtid [I] Format ID for which to return a storage name
2502 * str [O] Storage name associated with rfmtid.
2505 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2508 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
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.
2575 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2577 HRESULT hr = STG_E_INVALIDNAME;
2579 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2581 if (!rfmtid) return E_INVALIDARG;
2582 if (!str) return STG_E_INVALIDNAME;
2584 if (!lstrcmpiW(str, szDocSummaryInfo))
2586 *rfmtid = FMTID_DocSummaryInformation;
2589 else if (!lstrcmpiW(str, szSummaryInfo))
2591 *rfmtid = FMTID_SummaryInformation;
2597 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2598 const WCHAR *pstr = str;
2600 memset(rfmtid, 0, sizeof(*rfmtid));
2601 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2602 bits += BITS_IN_CHARMASK)
2604 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2610 if (wc > NUM_ALPHA_CHARS)
2613 if (wc > NUM_ALPHA_CHARS)
2615 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2618 WARN("invalid character (%d)\n", *pstr);
2623 *fmtptr |= wc << bitsUsed;
2624 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2625 if (bitsStored < BITS_IN_CHARMASK)
2627 wc >>= BITS_PER_BYTE - bitsUsed;
2628 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2632 WARN("extra bits\n");
2638 *fmtptr |= (BYTE)wc;