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, (void *)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, (void **)&propid))
256 ret = PropertyStorage_FindProperty(This, propid);
261 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
262 &ansiName, This->codePage);
266 if (dictionary_find(This->name_to_propid, ansiName,
268 ret = PropertyStorage_FindProperty(This, propid);
269 CoTaskMemFree(ansiName);
272 TRACE("returning %p\n", ret);
276 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
281 dictionary_find(This->propid_to_name, (void *)propid, (void **)&ret);
282 TRACE("returning %p\n", ret);
286 /************************************************************************
287 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
289 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
290 IPropertyStorage* iface,
292 const PROPSPEC rgpspec[],
293 PROPVARIANT rgpropvar[])
295 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
299 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
303 if (!rgpspec || !rgpropvar)
305 EnterCriticalSection(&This->cs);
306 for (i = 0; i < cpspec; i++)
308 PropVariantInit(&rgpropvar[i]);
309 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
311 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
312 rgpspec[i].u.lpwstr);
315 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
320 switch (rgpspec[i].u.propid)
323 rgpropvar[i].vt = VT_I2;
324 rgpropvar[i].u.iVal = This->codePage;
327 rgpropvar[i].vt = VT_I4;
328 rgpropvar[i].u.lVal = This->locale;
332 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
333 rgpspec[i].u.propid);
336 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
337 GetACP(), This->codePage);
344 LeaveCriticalSection(&This->cs);
348 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
354 TRACE("%s, %p, %d, %d\n",
355 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
364 if (dstCP == CP_UNICODE)
365 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
367 len = strlen(src) + 1;
368 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
370 hr = STG_E_INSUFFICIENTMEMORY;
372 memcpy(*dst, src, len);
376 if (dstCP == CP_UNICODE)
378 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
379 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
381 hr = STG_E_INSUFFICIENTMEMORY;
383 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
387 LPCWSTR wideStr = NULL;
388 LPWSTR wideStr_tmp = NULL;
390 if (srcCP == CP_UNICODE)
391 wideStr = (LPCWSTR)src;
394 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
395 wideStr_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
398 MultiByteToWideChar(srcCP, 0, src, -1, wideStr_tmp, len);
399 wideStr = wideStr_tmp;
402 hr = STG_E_INSUFFICIENTMEMORY;
406 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
408 *dst = CoTaskMemAlloc(len);
410 hr = STG_E_INSUFFICIENTMEMORY;
413 BOOL defCharUsed = FALSE;
415 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
416 NULL, &defCharUsed) == 0 || defCharUsed)
420 hr = HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
424 HeapFree(GetProcessHeap(), 0, wideStr_tmp);
427 TRACE("returning 0x%08x (%s)\n", hr,
428 dstCP == CP_UNICODE ? debugstr_w((LPCWSTR)*dst) : debugstr_a(*dst));
432 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
433 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
439 if (propvar->vt == VT_LPSTR)
441 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
442 &prop->u.pszVal, targetCP);
447 PropVariantCopy(prop, propvar);
451 /* Stores the property with id propid and value propvar into this property
452 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
453 * type is VT_LPSTR, converts the string using lcid as the source code page
454 * and This->codePage as the target code page before storing.
455 * As a side effect, may change This->format to 1 if the type of propvar is
456 * a version 1-only property.
458 static HRESULT PropertyStorage_StorePropWithId(PropertyStorage_impl *This,
459 PROPID propid, const PROPVARIANT *propvar, LCID lcid)
462 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
465 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
473 case VT_VECTOR|VT_I1:
476 TRACE("Setting 0x%08x to type %d\n", propid, propvar->vt);
479 PropVariantClear(prop);
480 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
485 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
486 sizeof(PROPVARIANT));
489 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
493 dictionary_insert(This->propid_to_prop, (void *)propid, prop);
494 if (propid > This->highestProp)
495 This->highestProp = propid;
498 HeapFree(GetProcessHeap(), 0, prop);
501 hr = STG_E_INSUFFICIENTMEMORY;
506 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
507 * srcName is encoded in code page cp, and is converted to This->codePage.
508 * If cp is CP_UNICODE, srcName is actually a unicode string.
509 * As a side effect, may change This->format to 1 if srcName is too long for
510 * a version 0 property storage.
511 * Doesn't validate id.
513 static HRESULT PropertyStorage_StoreNameWithId(PropertyStorage_impl *This,
514 LPCSTR srcName, LCID cp, PROPID id)
521 hr = PropertyStorage_StringCopy(srcName, cp, &name, This->codePage);
524 if (This->codePage == CP_UNICODE)
526 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
531 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
534 TRACE("Adding prop name %s, propid %d\n",
535 This->codePage == CP_UNICODE ? debugstr_w((LPCWSTR)name) :
536 debugstr_a(name), id);
537 dictionary_insert(This->name_to_propid, name, (void *)id);
538 dictionary_insert(This->propid_to_name, (void *)id, name);
543 /************************************************************************
544 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
546 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
547 IPropertyStorage* iface,
549 const PROPSPEC rgpspec[],
550 const PROPVARIANT rgpropvar[],
551 PROPID propidNameFirst)
553 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
557 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
559 if (cpspec && (!rgpspec || !rgpropvar))
561 if (!(This->grfMode & STGM_READWRITE))
562 return STG_E_ACCESSDENIED;
563 EnterCriticalSection(&This->cs);
565 This->originatorOS = (DWORD)MAKELONG(LOWORD(GetVersion()),
566 PROPSETHDR_OSVER_KIND_WIN32) ;
567 for (i = 0; i < cpspec; i++)
569 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
571 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
572 rgpspec[i].u.lpwstr);
575 PropVariantCopy(prop, &rgpropvar[i]);
578 /* Note that I don't do the special cases here that I do below,
579 * because naming the special PIDs isn't supported.
581 if (propidNameFirst < PID_FIRST_USABLE ||
582 propidNameFirst >= PID_MIN_READONLY)
583 hr = STG_E_INVALIDPARAMETER;
586 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
588 hr = PropertyStorage_StoreNameWithId(This,
589 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
591 hr = PropertyStorage_StorePropWithId(This, nextId,
592 &rgpropvar[i], GetACP());
598 switch (rgpspec[i].u.propid)
601 /* Can't set the dictionary */
602 hr = STG_E_INVALIDPARAMETER;
605 /* Can only set the code page if nothing else has been set */
606 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
607 rgpropvar[i].vt == VT_I2)
609 This->codePage = rgpropvar[i].u.iVal;
610 if (This->codePage == CP_UNICODE)
611 This->grfFlags &= ~PROPSETFLAG_ANSI;
613 This->grfFlags |= PROPSETFLAG_ANSI;
616 hr = STG_E_INVALIDPARAMETER;
619 /* Can only set the locale if nothing else has been set */
620 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
621 rgpropvar[i].vt == VT_I4)
622 This->locale = rgpropvar[i].u.lVal;
624 hr = STG_E_INVALIDPARAMETER;
627 /* silently ignore like MSDN says */
630 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
631 hr = STG_E_INVALIDPARAMETER;
633 hr = PropertyStorage_StorePropWithId(This,
634 rgpspec[i].u.propid, &rgpropvar[i], GetACP());
638 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
639 IPropertyStorage_Commit(iface, STGC_DEFAULT);
640 LeaveCriticalSection(&This->cs);
644 /************************************************************************
645 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
647 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
648 IPropertyStorage* iface,
650 const PROPSPEC rgpspec[])
652 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
656 TRACE("(%p, %d, %p)\n", iface, cpspec, rgpspec);
658 if (cpspec && !rgpspec)
660 if (!(This->grfMode & STGM_READWRITE))
661 return STG_E_ACCESSDENIED;
663 EnterCriticalSection(&This->cs);
665 for (i = 0; i < cpspec; i++)
667 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
671 if (dictionary_find(This->name_to_propid,
672 (void *)rgpspec[i].u.lpwstr, (void **)&propid))
673 dictionary_remove(This->propid_to_prop, (void *)propid);
677 if (rgpspec[i].u.propid >= PID_FIRST_USABLE &&
678 rgpspec[i].u.propid < PID_MIN_READONLY)
679 dictionary_remove(This->propid_to_prop,
680 (void *)rgpspec[i].u.propid);
682 hr = STG_E_INVALIDPARAMETER;
685 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
686 IPropertyStorage_Commit(iface, STGC_DEFAULT);
687 LeaveCriticalSection(&This->cs);
691 /************************************************************************
692 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
694 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
695 IPropertyStorage* iface,
697 const PROPID rgpropid[],
698 LPOLESTR rglpwstrName[])
700 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
702 HRESULT hr = S_FALSE;
704 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
706 if (cpropid && (!rgpropid || !rglpwstrName))
708 EnterCriticalSection(&This->cs);
709 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
711 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
715 size_t len = lstrlenW(name);
718 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
720 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
722 hr = STG_E_INSUFFICIENTMEMORY;
725 rglpwstrName[i] = NULL;
727 LeaveCriticalSection(&This->cs);
731 /************************************************************************
732 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
734 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
735 IPropertyStorage* iface,
737 const PROPID rgpropid[],
738 const LPOLESTR rglpwstrName[])
740 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
744 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
746 if (cpropid && (!rgpropid || !rglpwstrName))
748 if (!(This->grfMode & STGM_READWRITE))
749 return STG_E_ACCESSDENIED;
751 EnterCriticalSection(&This->cs);
753 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
755 if (rgpropid[i] != PID_ILLEGAL)
756 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
757 CP_UNICODE, rgpropid[i]);
759 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
760 IPropertyStorage_Commit(iface, STGC_DEFAULT);
761 LeaveCriticalSection(&This->cs);
765 /************************************************************************
766 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
768 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
769 IPropertyStorage* iface,
771 const PROPID rgpropid[])
773 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
777 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
779 if (cpropid && !rgpropid)
781 if (!(This->grfMode & STGM_READWRITE))
782 return STG_E_ACCESSDENIED;
784 EnterCriticalSection(&This->cs);
786 for (i = 0; i < cpropid; i++)
790 if (dictionary_find(This->propid_to_name, (void *)rgpropid[i],
793 dictionary_remove(This->propid_to_name, (void *)rgpropid[i]);
794 dictionary_remove(This->name_to_propid, name);
797 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
798 IPropertyStorage_Commit(iface, STGC_DEFAULT);
799 LeaveCriticalSection(&This->cs);
803 /************************************************************************
804 * IPropertyStorage_fnCommit (IPropertyStorage)
806 static HRESULT WINAPI IPropertyStorage_fnCommit(
807 IPropertyStorage* iface,
808 DWORD grfCommitFlags)
810 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
813 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
815 if (!(This->grfMode & STGM_READWRITE))
816 return STG_E_ACCESSDENIED;
817 EnterCriticalSection(&This->cs);
819 hr = PropertyStorage_WriteToStream(This);
822 LeaveCriticalSection(&This->cs);
826 /************************************************************************
827 * IPropertyStorage_fnRevert (IPropertyStorage)
829 static HRESULT WINAPI IPropertyStorage_fnRevert(
830 IPropertyStorage* iface)
833 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
835 TRACE("%p\n", iface);
837 EnterCriticalSection(&This->cs);
840 PropertyStorage_DestroyDictionaries(This);
841 hr = PropertyStorage_CreateDictionaries(This);
843 hr = PropertyStorage_ReadFromStream(This);
847 LeaveCriticalSection(&This->cs);
851 /************************************************************************
852 * IPropertyStorage_fnEnum (IPropertyStorage)
854 static HRESULT WINAPI IPropertyStorage_fnEnum(
855 IPropertyStorage* iface,
856 IEnumSTATPROPSTG** ppenum)
858 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
859 return create_EnumSTATPROPSTG(This, ppenum);
862 /************************************************************************
863 * IPropertyStorage_fnSetTimes (IPropertyStorage)
865 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
866 IPropertyStorage* iface,
867 const FILETIME* pctime,
868 const FILETIME* patime,
869 const FILETIME* pmtime)
875 /************************************************************************
876 * IPropertyStorage_fnSetClass (IPropertyStorage)
878 static HRESULT WINAPI IPropertyStorage_fnSetClass(
879 IPropertyStorage* iface,
882 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
884 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
888 if (!(This->grfMode & STGM_READWRITE))
889 return STG_E_ACCESSDENIED;
890 This->clsid = *clsid;
892 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
893 IPropertyStorage_Commit(iface, STGC_DEFAULT);
897 /************************************************************************
898 * IPropertyStorage_fnStat (IPropertyStorage)
900 static HRESULT WINAPI IPropertyStorage_fnStat(
901 IPropertyStorage* iface,
902 STATPROPSETSTG* statpsstg)
904 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
908 TRACE("%p, %p\n", iface, statpsstg);
913 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
916 statpsstg->fmtid = This->fmtid;
917 statpsstg->clsid = This->clsid;
918 statpsstg->grfFlags = This->grfFlags;
919 statpsstg->mtime = stat.mtime;
920 statpsstg->ctime = stat.ctime;
921 statpsstg->atime = stat.atime;
922 statpsstg->dwOSVersion = This->originatorOS;
927 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
930 PropertyStorage_impl *This = extra;
932 if (This->codePage == CP_UNICODE)
934 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
935 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
936 return lstrcmpW(a, b);
938 return lstrcmpiW(a, b);
942 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
943 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
944 return lstrcmpA(a, b);
946 return lstrcmpiA(a, b);
950 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
955 static int PropertyStorage_PropCompare(const void *a, const void *b,
958 TRACE("(%d, %d)\n", (PROPID)a, (PROPID)b);
959 return (PROPID)a - (PROPID)b;
962 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
965 HeapFree(GetProcessHeap(), 0, d);
968 #ifdef WORDS_BIGENDIAN
969 /* Swaps each character in str to or from little endian; assumes the conversion
970 * is symmetric, that is, that lendian16toh is equivalent to htole16.
972 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
976 /* Swap characters to host order.
979 for (i = 0; i < len; i++)
980 str[i] = lendian16toh(str[i]);
983 #define PropertyStorage_ByteSwapString(s, l)
986 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
987 * the entries according to the values of This->codePage and This->locale.
988 * FIXME: there isn't any checking whether the read property extends past the
991 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
997 assert(This->name_to_propid);
998 assert(This->propid_to_name);
1000 StorageUtl_ReadDWord(ptr, 0, &numEntries);
1001 TRACE("Reading %d entries:\n", numEntries);
1002 ptr += sizeof(DWORD);
1003 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1008 StorageUtl_ReadDWord(ptr, 0, &propid);
1009 ptr += sizeof(PROPID);
1010 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1011 ptr += sizeof(DWORD);
1012 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1013 /* Make sure the source string is NULL-terminated */
1014 if (This->codePage != CP_UNICODE)
1015 ptr[cbEntry - 1] = '\0';
1017 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1018 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1019 if (This->codePage == CP_UNICODE)
1021 /* Unicode entries are padded to DWORD boundaries */
1022 if (cbEntry % sizeof(DWORD))
1023 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1025 ptr += sizeof(DWORD) + cbEntry;
1030 /* FIXME: there isn't any checking whether the read property extends past the
1031 * end of the buffer.
1033 static HRESULT PropertyStorage_ReadProperty(PropertyStorage_impl *This,
1034 PROPVARIANT *prop, const BYTE *data)
1040 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1041 data += sizeof(DWORD);
1048 prop->u.cVal = *(const char *)data;
1049 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1052 prop->u.bVal = *data;
1053 TRACE("Read byte 0x%x\n", prop->u.bVal);
1056 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1057 TRACE("Read short %d\n", prop->u.iVal);
1060 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1061 TRACE("Read ushort %d\n", prop->u.uiVal);
1065 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1066 TRACE("Read long %d\n", prop->u.lVal);
1070 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1071 TRACE("Read ulong %d\n", prop->u.ulVal);
1077 StorageUtl_ReadDWord(data, 0, &count);
1078 if (This->codePage == CP_UNICODE && count / 2)
1080 WARN("Unicode string has odd number of bytes\n");
1081 hr = STG_E_INVALIDHEADER;
1085 prop->u.pszVal = CoTaskMemAlloc(count);
1088 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1089 /* This is stored in the code page specified in This->codePage.
1090 * Don't convert it, the caller will just store it as-is.
1092 if (This->codePage == CP_UNICODE)
1094 /* Make sure it's NULL-terminated */
1095 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1096 TRACE("Read string value %s\n",
1097 debugstr_w(prop->u.pwszVal));
1101 /* Make sure it's NULL-terminated */
1102 prop->u.pszVal[count - 1] = '\0';
1103 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1107 hr = STG_E_INSUFFICIENTMEMORY;
1115 StorageUtl_ReadDWord(data, 0, &count);
1116 prop->u.pwszVal = CoTaskMemAlloc(count * sizeof(WCHAR));
1117 if (prop->u.pwszVal)
1119 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1120 count * sizeof(WCHAR));
1121 /* make sure string is NULL-terminated */
1122 prop->u.pwszVal[count - 1] = '\0';
1123 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1124 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1127 hr = STG_E_INSUFFICIENTMEMORY;
1131 StorageUtl_ReadULargeInteger(data, 0,
1132 (ULARGE_INTEGER *)&prop->u.filetime);
1136 DWORD len = 0, tag = 0;
1138 StorageUtl_ReadDWord(data, 0, &len);
1139 StorageUtl_ReadDWord(data, 4, &tag);
1143 prop->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
1144 prop->u.pclipdata->cbSize = len;
1145 prop->u.pclipdata->ulClipFmt = tag;
1146 prop->u.pclipdata->pClipData = CoTaskMemAlloc(len - sizeof(prop->u.pclipdata->ulClipFmt));
1147 memcpy(prop->u.pclipdata->pClipData, data+8, len - sizeof(prop->u.pclipdata->ulClipFmt));
1150 hr = STG_E_INVALIDPARAMETER;
1154 FIXME("unsupported type %d\n", prop->vt);
1155 hr = STG_E_INVALIDPARAMETER;
1160 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1161 PROPERTYSETHEADER *hdr)
1163 BYTE buf[sizeof(PROPERTYSETHEADER)];
1169 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1172 if (count != sizeof(buf))
1174 WARN("read only %d\n", count);
1175 hr = STG_E_INVALIDHEADER;
1179 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1181 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1183 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1185 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1187 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1191 TRACE("returning 0x%08x\n", hr);
1195 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1196 FORMATIDOFFSET *fmt)
1198 BYTE buf[sizeof(FORMATIDOFFSET)];
1204 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1207 if (count != sizeof(buf))
1209 WARN("read only %d\n", count);
1210 hr = STG_E_INVALIDHEADER;
1214 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1216 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1220 TRACE("returning 0x%08x\n", hr);
1224 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1225 PROPERTYSECTIONHEADER *hdr)
1227 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1233 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1236 if (count != sizeof(buf))
1238 WARN("read only %d\n", count);
1239 hr = STG_E_INVALIDHEADER;
1243 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1244 cbSection), &hdr->cbSection);
1245 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1246 cProperties), &hdr->cProperties);
1249 TRACE("returning 0x%08x\n", hr);
1253 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1255 PROPERTYSETHEADER hdr;
1256 FORMATIDOFFSET fmtOffset;
1257 PROPERTYSECTIONHEADER sectionHdr;
1264 DWORD dictOffset = 0;
1266 This->dirty = FALSE;
1267 This->highestProp = 0;
1268 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1271 if (stat.cbSize.u.HighPart)
1273 WARN("stream too big\n");
1274 /* maximum size varies, but it can't be this big */
1275 hr = STG_E_INVALIDHEADER;
1278 if (stat.cbSize.u.LowPart == 0)
1280 /* empty stream is okay */
1284 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1285 sizeof(FORMATIDOFFSET))
1287 WARN("stream too small\n");
1288 hr = STG_E_INVALIDHEADER;
1292 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1295 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1296 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1299 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1301 WARN("bad magic in prop set header\n");
1302 hr = STG_E_INVALIDHEADER;
1305 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1307 WARN("bad format version %d\n", hdr.wFormat);
1308 hr = STG_E_INVALIDHEADER;
1311 This->format = hdr.wFormat;
1312 This->clsid = hdr.clsid;
1313 This->originatorOS = hdr.dwOSVer;
1314 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1315 WARN("File comes from a Mac, strings will probably be screwed up\n");
1316 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1319 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1321 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1322 stat.cbSize.u.LowPart);
1323 hr = STG_E_INVALIDHEADER;
1326 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1327 * follow not one, but two sections. The first is the standard properties
1328 * for the document summary information, and the second is user-defined
1329 * properties. This is the only case in which multiple sections are
1331 * Reading the second stream isn't implemented yet.
1333 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, §ionHdr);
1336 /* The section size includes the section header, so check it */
1337 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1339 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1340 hr = STG_E_INVALIDHEADER;
1343 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1344 sizeof(PROPERTYSECTIONHEADER));
1347 hr = STG_E_INSUFFICIENTMEMORY;
1350 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1351 sizeof(PROPERTYSECTIONHEADER), &count);
1354 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1355 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1357 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1358 i * sizeof(PROPERTYIDOFFSET));
1360 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1361 idOffset->dwOffset >= sectionHdr.cbSection - sizeof(DWORD))
1362 hr = STG_E_INVALIDPOINTER;
1365 if (idOffset->propid >= PID_FIRST_USABLE &&
1366 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1368 This->highestProp = idOffset->propid;
1369 if (idOffset->propid == PID_DICTIONARY)
1371 /* Don't read the dictionary yet, its entries depend on the
1372 * code page. Just store the offset so we know to read it
1375 dictOffset = idOffset->dwOffset;
1376 TRACE("Dictionary offset is %d\n", dictOffset);
1382 PropVariantInit(&prop);
1383 if (SUCCEEDED(PropertyStorage_ReadProperty(This, &prop,
1384 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER))))
1386 TRACE("Read property with ID 0x%08x, type %d\n",
1387 idOffset->propid, prop.vt);
1388 switch(idOffset->propid)
1391 if (prop.vt == VT_I2)
1392 This->codePage = (UINT)prop.u.iVal;
1395 if (prop.vt == VT_I4)
1396 This->locale = (LCID)prop.u.lVal;
1399 if (prop.vt == VT_I4 && prop.u.lVal)
1400 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1401 /* The format should already be 1, but just in case */
1405 hr = PropertyStorage_StorePropWithId(This,
1406 idOffset->propid, &prop, This->codePage);
1412 if (!This->codePage)
1414 /* default to Unicode unless told not to, as specified on msdn */
1415 if (This->grfFlags & PROPSETFLAG_ANSI)
1416 This->codePage = GetACP();
1418 This->codePage = CP_UNICODE;
1421 This->locale = LOCALE_SYSTEM_DEFAULT;
1422 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1424 hr = PropertyStorage_ReadDictionary(This,
1425 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1428 HeapFree(GetProcessHeap(), 0, buf);
1431 dictionary_destroy(This->name_to_propid);
1432 This->name_to_propid = NULL;
1433 dictionary_destroy(This->propid_to_name);
1434 This->propid_to_name = NULL;
1435 dictionary_destroy(This->propid_to_prop);
1436 This->propid_to_prop = NULL;
1441 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1442 PROPERTYSETHEADER *hdr)
1445 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1446 PROPSETHDR_BYTEORDER_MAGIC);
1447 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1448 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1449 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1450 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1453 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1454 FORMATIDOFFSET *fmtOffset)
1457 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1458 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1459 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1462 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1463 PROPERTYSECTIONHEADER *hdr)
1466 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1467 StorageUtl_WriteDWord((BYTE *)hdr,
1468 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1471 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1472 PROPERTYIDOFFSET *propIdOffset)
1474 assert(propIdOffset);
1475 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1476 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1477 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1480 static inline HRESULT PropertStorage_WriteWStringToStream(IStream *stm,
1481 LPCWSTR str, DWORD len, DWORD *written)
1483 #ifdef WORDS_BIGENDIAN
1484 WCHAR *leStr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1488 return E_OUTOFMEMORY;
1489 memcpy(leStr, str, len * sizeof(WCHAR));
1490 PropertyStorage_ByteSwapString(leStr, len);
1491 hr = IStream_Write(stm, leStr, len, written);
1492 HeapFree(GetProcessHeap(), 0, leStr);
1495 return IStream_Write(stm, str, len, written);
1499 struct DictionaryClosure
1505 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1506 const void *value, void *extra, void *closure)
1508 PropertyStorage_impl *This = extra;
1509 struct DictionaryClosure *c = closure;
1515 StorageUtl_WriteDWord((LPBYTE)&propid, 0, (DWORD)value);
1516 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1519 c->bytesWritten += sizeof(DWORD);
1520 if (This->codePage == CP_UNICODE)
1522 DWORD keyLen, pad = 0;
1524 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1525 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1526 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1529 c->bytesWritten += sizeof(DWORD);
1530 c->hr = PropertStorage_WriteWStringToStream(This->stm, key, keyLen,
1534 c->bytesWritten += keyLen * sizeof(WCHAR);
1535 if (keyLen % sizeof(DWORD))
1537 c->hr = IStream_Write(This->stm, &pad,
1538 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1541 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1548 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1549 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1552 c->bytesWritten += sizeof(DWORD);
1553 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1556 c->bytesWritten += keyLen;
1559 return SUCCEEDED(c->hr);
1562 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1564 /* Writes the dictionary to the stream. Assumes without checking that the
1565 * dictionary isn't empty.
1567 static HRESULT PropertyStorage_WriteDictionaryToStream(
1568 PropertyStorage_impl *This, DWORD *sectionOffset)
1572 PROPERTYIDOFFSET propIdOffset;
1575 struct DictionaryClosure closure;
1577 assert(sectionOffset);
1579 /* The dictionary's always the first property written, so seek to its
1582 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1583 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1586 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1588 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1592 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1593 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1596 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1597 dictionary_num_entries(This->name_to_propid));
1598 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1601 *sectionOffset += sizeof(dwTemp);
1604 closure.bytesWritten = 0;
1605 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1610 *sectionOffset += closure.bytesWritten;
1611 if (closure.bytesWritten % sizeof(DWORD))
1613 DWORD padding = sizeof(DWORD) - closure.bytesWritten % sizeof(DWORD);
1614 TRACE("adding %d bytes of padding\n", padding);
1615 *sectionOffset += padding;
1622 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1623 DWORD propNum, DWORD propid, const PROPVARIANT *var, DWORD *sectionOffset)
1627 PROPERTYIDOFFSET propIdOffset;
1629 DWORD dwType, bytesWritten;
1632 assert(sectionOffset);
1634 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This, propNum, propid, var->vt,
1637 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1638 propNum * sizeof(PROPERTYIDOFFSET);
1639 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1642 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1643 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1647 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1648 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1651 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1652 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1655 *sectionOffset += sizeof(dwType);
1665 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1667 bytesWritten = count;
1674 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1675 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1676 bytesWritten = count;
1684 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1685 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1686 bytesWritten = count;
1693 if (This->codePage == CP_UNICODE)
1694 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1696 len = lstrlenA(var->u.pszVal) + 1;
1697 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1698 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1701 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1702 bytesWritten = count + sizeof(DWORD);
1707 DWORD len = lstrlenW(var->u.pwszVal) + 1, dwTemp;
1709 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1710 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1713 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1715 bytesWritten = count + sizeof(DWORD);
1722 StorageUtl_WriteULargeInteger((BYTE *)&temp, 0,
1723 (const ULARGE_INTEGER *)&var->u.filetime);
1724 hr = IStream_Write(This->stm, &temp, sizeof(FILETIME), &count);
1725 bytesWritten = count;
1730 DWORD cf_hdr[2], len;
1732 len = var->u.pclipdata->cbSize;
1733 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[0], 0, len + 8);
1734 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[1], 0, var->u.pclipdata->ulClipFmt);
1735 hr = IStream_Write(This->stm, cf_hdr, sizeof(cf_hdr), &count);
1738 hr = IStream_Write(This->stm, var->u.pclipdata->pClipData,
1739 len - sizeof(var->u.pclipdata->ulClipFmt), &count);
1742 bytesWritten = count + sizeof cf_hdr;
1746 FIXME("unsupported type: %d\n", var->vt);
1747 return STG_E_INVALIDPARAMETER;
1752 *sectionOffset += bytesWritten;
1753 if (bytesWritten % sizeof(DWORD))
1755 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1756 TRACE("adding %d bytes of padding\n", padding);
1757 *sectionOffset += padding;
1765 struct PropertyClosure
1769 DWORD *sectionOffset;
1772 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1773 void *extra, void *closure)
1775 PropertyStorage_impl *This = extra;
1776 struct PropertyClosure *c = closure;
1782 c->hr = PropertyStorage_WritePropertyToStream(This, c->propNum++,
1783 (DWORD)key, value, c->sectionOffset);
1784 return SUCCEEDED(c->hr);
1787 static HRESULT PropertyStorage_WritePropertiesToStream(
1788 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1790 struct PropertyClosure closure;
1792 assert(sectionOffset);
1794 closure.propNum = startingPropNum;
1795 closure.sectionOffset = sectionOffset;
1796 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1801 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1805 LARGE_INTEGER seek = { {0} };
1806 PROPERTYSETHEADER hdr;
1807 FORMATIDOFFSET fmtOffset;
1809 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1812 PropertyStorage_MakeHeader(This, &hdr);
1813 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1816 if (count != sizeof(hdr))
1818 hr = STG_E_WRITEFAULT;
1822 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1823 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1826 if (count != sizeof(fmtOffset))
1828 hr = STG_E_WRITEFAULT;
1837 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1839 PROPERTYSECTIONHEADER sectionHdr;
1843 DWORD numProps, prop, sectionOffset, dwTemp;
1846 PropertyStorage_WriteHeadersToStream(This);
1848 /* Count properties. Always at least one property, the code page */
1850 if (dictionary_num_entries(This->name_to_propid))
1852 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1854 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1856 numProps += dictionary_num_entries(This->propid_to_prop);
1858 /* Write section header with 0 bytes right now, I'll adjust it after
1859 * writing properties.
1861 PropertyStorage_MakeSectionHdr(0, numProps, §ionHdr);
1862 seek.QuadPart = SECTIONHEADER_OFFSET;
1863 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1866 hr = IStream_Write(This->stm, §ionHdr, sizeof(sectionHdr), &count);
1871 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1872 numProps * sizeof(PROPERTYIDOFFSET);
1874 if (dictionary_num_entries(This->name_to_propid))
1877 hr = PropertyStorage_WriteDictionaryToStream(This, §ionOffset);
1882 PropVariantInit(&var);
1885 var.u.iVal = This->codePage;
1886 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1887 &var, §ionOffset);
1891 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1894 var.u.lVal = This->locale;
1895 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1896 &var, §ionOffset);
1901 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1905 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1906 &var, §ionOffset);
1911 hr = PropertyStorage_WritePropertiesToStream(This, prop, §ionOffset);
1915 /* Now write the byte count of the section */
1916 seek.QuadPart = SECTIONHEADER_OFFSET;
1917 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1920 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1921 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1927 /***********************************************************************
1928 * PropertyStorage_Construct
1930 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1932 dictionary_destroy(This->name_to_propid);
1933 This->name_to_propid = NULL;
1934 dictionary_destroy(This->propid_to_name);
1935 This->propid_to_name = NULL;
1936 dictionary_destroy(This->propid_to_prop);
1937 This->propid_to_prop = NULL;
1940 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
1944 This->name_to_propid = dictionary_create(
1945 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1947 if (!This->name_to_propid)
1949 hr = STG_E_INSUFFICIENTMEMORY;
1952 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1954 if (!This->propid_to_name)
1956 hr = STG_E_INSUFFICIENTMEMORY;
1959 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1960 PropertyStorage_PropertyDestroy, This);
1961 if (!This->propid_to_prop)
1963 hr = STG_E_INSUFFICIENTMEMORY;
1968 PropertyStorage_DestroyDictionaries(This);
1972 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1973 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1979 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1981 return E_OUTOFMEMORY;
1983 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1985 InitializeCriticalSection(&(*pps)->cs);
1986 (*pps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PropertyStorage_impl.cs");
1988 (*pps)->fmtid = *rfmtid;
1989 (*pps)->grfMode = grfMode;
1991 hr = PropertyStorage_CreateDictionaries(*pps);
1994 IStream_Release(stm);
1995 (*pps)->cs.DebugInfo->Spare[0] = 0;
1996 DeleteCriticalSection(&(*pps)->cs);
1997 HeapFree(GetProcessHeap(), 0, *pps);
2004 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
2005 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2007 PropertyStorage_impl *ps;
2011 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2014 hr = PropertyStorage_ReadFromStream(ps);
2017 *pps = (IPropertyStorage *)ps;
2018 TRACE("PropertyStorage %p constructed\n", ps);
2023 PropertyStorage_DestroyDictionaries(ps);
2024 HeapFree(GetProcessHeap(), 0, ps);
2030 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2031 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2033 PropertyStorage_impl *ps;
2037 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2041 ps->grfFlags = grfFlags;
2042 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2044 /* default to Unicode unless told not to, as specified on msdn */
2045 if (ps->grfFlags & PROPSETFLAG_ANSI)
2046 ps->codePage = GetACP();
2048 ps->codePage = CP_UNICODE;
2049 ps->locale = LOCALE_SYSTEM_DEFAULT;
2050 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2051 *pps = (IPropertyStorage *)ps;
2052 TRACE("PropertyStorage %p constructed\n", ps);
2059 /***********************************************************************
2060 * Implementation of IPropertySetStorage
2063 /************************************************************************
2064 * IPropertySetStorage_fnQueryInterface (IUnknown)
2066 * This method forwards to the common QueryInterface implementation
2068 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2069 IPropertySetStorage *ppstg,
2073 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2074 return IStorage_QueryInterface( (IStorage*)This, riid, ppvObject );
2077 /************************************************************************
2078 * IPropertySetStorage_fnAddRef (IUnknown)
2080 * This method forwards to the common AddRef implementation
2082 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2083 IPropertySetStorage *ppstg)
2085 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2086 return IStorage_AddRef( (IStorage*)This );
2089 /************************************************************************
2090 * IPropertySetStorage_fnRelease (IUnknown)
2092 * This method forwards to the common Release implementation
2094 static ULONG WINAPI IPropertySetStorage_fnRelease(
2095 IPropertySetStorage *ppstg)
2097 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2098 return IStorage_Release( (IStorage*)This );
2101 /************************************************************************
2102 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2104 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2105 IPropertySetStorage *ppstg,
2107 const CLSID* pclsid,
2110 IPropertyStorage** ppprstg)
2112 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2113 WCHAR name[CCH_MAX_PROPSTG_NAME];
2114 IStream *stm = NULL;
2117 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2121 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2123 r = STG_E_INVALIDFLAG;
2133 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2134 * storage, not a stream. For now, disallow it.
2136 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2138 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2139 r = STG_E_INVALIDFLAG;
2143 r = FmtIdToPropStgName(rfmtid, name);
2147 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2151 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2154 TRACE("returning 0x%08x\n", r);
2158 /************************************************************************
2159 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2161 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2162 IPropertySetStorage *ppstg,
2165 IPropertyStorage** ppprstg)
2167 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2168 IStream *stm = NULL;
2169 WCHAR name[CCH_MAX_PROPSTG_NAME];
2172 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2175 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2176 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2178 r = STG_E_INVALIDFLAG;
2188 r = FmtIdToPropStgName(rfmtid, name);
2192 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2196 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2199 TRACE("returning 0x%08x\n", r);
2203 /************************************************************************
2204 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2206 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2207 IPropertySetStorage *ppstg,
2210 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2211 IStorage *stg = NULL;
2212 WCHAR name[CCH_MAX_PROPSTG_NAME];
2215 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2218 return E_INVALIDARG;
2220 r = FmtIdToPropStgName(rfmtid, name);
2224 stg = (IStorage*) This;
2225 return IStorage_DestroyElement(stg, name);
2228 /************************************************************************
2229 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2231 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2232 IPropertySetStorage *ppstg,
2233 IEnumSTATPROPSETSTG** ppenum)
2235 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2236 return create_EnumSTATPROPSETSTG(This, ppenum);
2239 /************************************************************************
2240 * Implement IEnumSTATPROPSETSTG using enumx
2242 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2243 IEnumSTATPROPSETSTG *iface,
2247 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2250 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2251 IEnumSTATPROPSETSTG *iface)
2253 return enumx_AddRef((enumx_impl*)iface);
2256 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2257 IEnumSTATPROPSETSTG *iface)
2259 return enumx_Release((enumx_impl*)iface);
2262 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2263 IEnumSTATPROPSETSTG *iface,
2265 STATPROPSETSTG *rgelt,
2266 ULONG *pceltFetched)
2268 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2271 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2272 IEnumSTATPROPSETSTG *iface,
2275 return enumx_Skip((enumx_impl*)iface, celt);
2278 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2279 IEnumSTATPROPSETSTG *iface)
2281 return enumx_Reset((enumx_impl*)iface);
2284 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2285 IEnumSTATPROPSETSTG *iface,
2286 IEnumSTATPROPSETSTG **ppenum)
2288 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2291 static HRESULT create_EnumSTATPROPSETSTG(
2293 IEnumSTATPROPSETSTG** ppenum)
2295 IStorage *stg = (IStorage*) &This->base.lpVtbl;
2296 IEnumSTATSTG *penum = NULL;
2300 STATPROPSETSTG statpss;
2303 TRACE("%p %p\n", This, ppenum);
2305 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2306 &IEnumSTATPROPSETSTG_Vtbl,
2307 sizeof (STATPROPSETSTG));
2309 /* add all the property set elements into a list */
2310 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2312 return E_OUTOFMEMORY;
2317 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2324 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2326 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2327 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2328 debugstr_guid(&statpss.fmtid));
2329 statpss.mtime = stat.mtime;
2330 statpss.atime = stat.atime;
2331 statpss.ctime = stat.ctime;
2332 statpss.grfFlags = stat.grfMode;
2333 statpss.clsid = stat.clsid;
2334 enumx_add_element(enumx, &statpss);
2336 CoTaskMemFree(stat.pwcsName);
2338 IEnumSTATSTG_Release(penum);
2340 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2345 /************************************************************************
2346 * Implement IEnumSTATPROPSTG using enumx
2348 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2349 IEnumSTATPROPSTG *iface,
2353 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2356 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2357 IEnumSTATPROPSTG *iface)
2359 return enumx_AddRef((enumx_impl*)iface);
2362 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2363 IEnumSTATPROPSTG *iface)
2365 return enumx_Release((enumx_impl*)iface);
2368 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2369 IEnumSTATPROPSTG *iface,
2372 ULONG *pceltFetched)
2374 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2377 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2378 IEnumSTATPROPSTG *iface,
2381 return enumx_Skip((enumx_impl*)iface, celt);
2384 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2385 IEnumSTATPROPSTG *iface)
2387 return enumx_Reset((enumx_impl*)iface);
2390 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2391 IEnumSTATPROPSTG *iface,
2392 IEnumSTATPROPSTG **ppenum)
2394 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2397 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2399 enumx_impl *enumx = arg;
2400 PROPID propid = (PROPID) k;
2401 const PROPVARIANT *prop = v;
2404 stat.lpwstrName = NULL;
2405 stat.propid = propid;
2408 enumx_add_element(enumx, &stat);
2413 static HRESULT create_EnumSTATPROPSTG(
2414 PropertyStorage_impl *This,
2415 IEnumSTATPROPSTG** ppenum)
2419 TRACE("%p %p\n", This, ppenum);
2421 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2422 &IEnumSTATPROPSTG_Vtbl,
2423 sizeof (STATPROPSTG));
2425 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2427 *ppenum = (IEnumSTATPROPSTG*) enumx;
2432 /***********************************************************************
2435 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2437 IPropertySetStorage_fnQueryInterface,
2438 IPropertySetStorage_fnAddRef,
2439 IPropertySetStorage_fnRelease,
2440 IPropertySetStorage_fnCreate,
2441 IPropertySetStorage_fnOpen,
2442 IPropertySetStorage_fnDelete,
2443 IPropertySetStorage_fnEnum
2446 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2448 IPropertyStorage_fnQueryInterface,
2449 IPropertyStorage_fnAddRef,
2450 IPropertyStorage_fnRelease,
2451 IPropertyStorage_fnReadMultiple,
2452 IPropertyStorage_fnWriteMultiple,
2453 IPropertyStorage_fnDeleteMultiple,
2454 IPropertyStorage_fnReadPropertyNames,
2455 IPropertyStorage_fnWritePropertyNames,
2456 IPropertyStorage_fnDeletePropertyNames,
2457 IPropertyStorage_fnCommit,
2458 IPropertyStorage_fnRevert,
2459 IPropertyStorage_fnEnum,
2460 IPropertyStorage_fnSetTimes,
2461 IPropertyStorage_fnSetClass,
2462 IPropertyStorage_fnStat,
2465 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2467 IEnumSTATPROPSETSTG_fnQueryInterface,
2468 IEnumSTATPROPSETSTG_fnAddRef,
2469 IEnumSTATPROPSETSTG_fnRelease,
2470 IEnumSTATPROPSETSTG_fnNext,
2471 IEnumSTATPROPSETSTG_fnSkip,
2472 IEnumSTATPROPSETSTG_fnReset,
2473 IEnumSTATPROPSETSTG_fnClone,
2476 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2478 IEnumSTATPROPSTG_fnQueryInterface,
2479 IEnumSTATPROPSTG_fnAddRef,
2480 IEnumSTATPROPSTG_fnRelease,
2481 IEnumSTATPROPSTG_fnNext,
2482 IEnumSTATPROPSTG_fnSkip,
2483 IEnumSTATPROPSTG_fnReset,
2484 IEnumSTATPROPSTG_fnClone,
2487 /***********************************************************************
2488 * Format ID <-> name conversion
2490 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2491 'I','n','f','o','r','m','a','t','i','o','n',0 };
2492 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2493 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2495 #define BITS_PER_BYTE 8
2496 #define CHARMASK 0x1f
2497 #define BITS_IN_CHARMASK 5
2498 #define NUM_ALPHA_CHARS 26
2500 /***********************************************************************
2501 * FmtIdToPropStgName [ole32.@]
2502 * Returns the storage name of the format ID rfmtid.
2504 * rfmtid [I] Format ID for which to return a storage name
2505 * str [O] Storage name associated with rfmtid.
2508 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2511 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2513 HRESULT WINAPI FmtIdToPropStgName(const FMTID *rfmtid, LPOLESTR str)
2515 static const char fmtMap[] = "abcdefghijklmnopqrstuvwxyz012345";
2517 TRACE("%s, %p\n", debugstr_guid(rfmtid), str);
2519 if (!rfmtid) return E_INVALIDARG;
2520 if (!str) return E_INVALIDARG;
2522 if (IsEqualGUID(&FMTID_SummaryInformation, rfmtid))
2523 lstrcpyW(str, szSummaryInfo);
2524 else if (IsEqualGUID(&FMTID_DocSummaryInformation, rfmtid))
2525 lstrcpyW(str, szDocSummaryInfo);
2526 else if (IsEqualGUID(&FMTID_UserDefinedProperties, rfmtid))
2527 lstrcpyW(str, szDocSummaryInfo);
2532 ULONG bitsRemaining = BITS_PER_BYTE;
2535 for (fmtptr = (const BYTE *)rfmtid; fmtptr < (const BYTE *)rfmtid + sizeof(FMTID); )
2537 ULONG i = *fmtptr >> (BITS_PER_BYTE - bitsRemaining);
2539 if (bitsRemaining >= BITS_IN_CHARMASK)
2541 *pstr = (WCHAR)(fmtMap[i & CHARMASK]);
2542 if (bitsRemaining == BITS_PER_BYTE && *pstr >= 'a' &&
2546 bitsRemaining -= BITS_IN_CHARMASK;
2547 if (bitsRemaining == 0)
2550 bitsRemaining = BITS_PER_BYTE;
2555 if (++fmtptr < (const BYTE *)rfmtid + sizeof(FMTID))
2556 i |= *fmtptr << bitsRemaining;
2557 *pstr++ = (WCHAR)(fmtMap[i & CHARMASK]);
2558 bitsRemaining += BITS_PER_BYTE - BITS_IN_CHARMASK;
2563 TRACE("returning %s\n", debugstr_w(str));
2567 /***********************************************************************
2568 * PropStgNameToFmtId [ole32.@]
2569 * Returns the format ID corresponding to the given name.
2571 * str [I] Storage name to convert to a format ID.
2572 * rfmtid [O] Format ID corresponding to str.
2575 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2576 * a format ID, S_OK otherwise.
2578 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2580 HRESULT hr = STG_E_INVALIDNAME;
2582 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2584 if (!rfmtid) return E_INVALIDARG;
2585 if (!str) return STG_E_INVALIDNAME;
2587 if (!lstrcmpiW(str, szDocSummaryInfo))
2589 *rfmtid = FMTID_DocSummaryInformation;
2592 else if (!lstrcmpiW(str, szSummaryInfo))
2594 *rfmtid = FMTID_SummaryInformation;
2600 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2601 const WCHAR *pstr = str;
2603 memset(rfmtid, 0, sizeof(*rfmtid));
2604 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2605 bits += BITS_IN_CHARMASK)
2607 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2613 if (wc > NUM_ALPHA_CHARS)
2616 if (wc > NUM_ALPHA_CHARS)
2618 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2621 WARN("invalid character (%d)\n", *pstr);
2626 *fmtptr |= wc << bitsUsed;
2627 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2628 if (bitsStored < BITS_IN_CHARMASK)
2630 wc >>= BITS_PER_BYTE - bitsUsed;
2631 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2635 WARN("extra bits\n");
2641 *fmtptr |= (BYTE)wc;