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
13 * Copyright 2006 Mike McCormack
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 * - I don't honor the maximum property set size.
31 * - Certain bogus files could result in reading past the end of a buffer.
32 * - Mac-generated files won't be read correctly, even if they're little
33 * endian, because I disregard whether the generator was a Mac. This means
34 * strings will probably be munged (as I don't understand Mac scripts.)
35 * - Not all PROPVARIANT types are supported.
36 * - User defined properties are not supported, see comment in
37 * PropertyStorage_ReadFromStream
52 #include "wine/debug.h"
53 #include "wine/heap.h"
54 #include "dictionary.h"
55 #include "storage32.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
60 static inline StorageImpl
*impl_from_IPropertySetStorage( IPropertySetStorage
*iface
)
62 return CONTAINING_RECORD(iface
, StorageImpl
, base
.IPropertySetStorage_iface
);
65 /* These are documented in MSDN,
66 * but they don't seem to be in any header file.
68 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
69 #define PROPSETHDR_OSVER_KIND_WIN16 0
70 #define PROPSETHDR_OSVER_KIND_MAC 1
71 #define PROPSETHDR_OSVER_KIND_WIN32 2
73 #define CP_UNICODE 1200
75 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
77 #define CFTAG_WINDOWS (-1L)
78 #define CFTAG_MACINTOSH (-2L)
79 #define CFTAG_FMTID (-3L)
80 #define CFTAG_NODATA 0L
82 #define ALIGNED_LENGTH(_Len, _Align) (((_Len)+(_Align))&~(_Align))
84 typedef struct tagPROPERTYSETHEADER
86 WORD wByteOrder
; /* always 0xfffe */
87 WORD wFormat
; /* can be zero or one */
88 DWORD dwOSVer
; /* OS version of originating system */
89 CLSID clsid
; /* application CLSID */
90 DWORD reserved
; /* always 1 */
93 typedef struct tagFORMATIDOFFSET
96 DWORD dwOffset
; /* from beginning of stream */
99 typedef struct tagPROPERTYSECTIONHEADER
103 } PROPERTYSECTIONHEADER
;
105 typedef struct tagPROPERTYIDOFFSET
108 DWORD dwOffset
; /* from beginning of section */
111 typedef struct tagPropertyStorage_impl PropertyStorage_impl
;
113 /* Initializes the property storage from the stream (and undoes any uncommitted
114 * changes in the process.) Returns an error if there is an error reading or
115 * if the stream format doesn't match what's expected.
117 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*);
119 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*);
121 /* Creates the dictionaries used by the property storage. If successful, all
122 * the dictionaries have been created. If failed, none has been. (This makes
123 * it a bit easier to deal with destroying them.)
125 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*);
127 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*);
129 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
130 * string using PropertyStorage_StringCopy.
132 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
133 const PROPVARIANT
*propvar
, UINT targetCP
, UINT srcCP
);
135 /* Copies the string src, which is encoded using code page srcCP, and returns
136 * it in *dst, in the code page specified by targetCP. The returned string is
137 * allocated using CoTaskMemAlloc.
138 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
139 * is CP_UNICODE, the returned string is in fact an LPWSTR.
140 * Returns S_OK on success, something else on failure.
142 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, UINT srcCP
, LPSTR
*dst
,
145 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
;
147 /***********************************************************************
148 * Implementation of IPropertyStorage
150 struct tagPropertyStorage_impl
152 IPropertyStorage IPropertyStorage_iface
;
166 struct dictionary
*name_to_propid
;
167 struct dictionary
*propid_to_name
;
168 struct dictionary
*propid_to_prop
;
171 static inline PropertyStorage_impl
*impl_from_IPropertyStorage(IPropertyStorage
*iface
)
173 return CONTAINING_RECORD(iface
, PropertyStorage_impl
, IPropertyStorage_iface
);
176 struct enum_stat_prop_stg
178 IEnumSTATPROPSTG IEnumSTATPROPSTG_iface
;
180 PropertyStorage_impl
*storage
;
186 static struct enum_stat_prop_stg
*impl_from_IEnumSTATPROPSTG(IEnumSTATPROPSTG
*iface
)
188 return CONTAINING_RECORD(iface
, struct enum_stat_prop_stg
, IEnumSTATPROPSTG_iface
);
191 static HRESULT WINAPI
enum_stat_prop_stg_QueryInterface(IEnumSTATPROPSTG
*iface
, REFIID riid
, void **obj
)
193 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), obj
);
195 if (IsEqualIID(riid
, &IID_IEnumSTATPROPSTG
) ||
196 IsEqualIID(riid
, &IID_IUnknown
))
199 IEnumSTATPROPSTG_AddRef(iface
);
203 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
204 return E_NOINTERFACE
;
207 static ULONG WINAPI
enum_stat_prop_stg_AddRef(IEnumSTATPROPSTG
*iface
)
209 struct enum_stat_prop_stg
*penum
= impl_from_IEnumSTATPROPSTG(iface
);
210 LONG refcount
= InterlockedIncrement(&penum
->refcount
);
212 TRACE("%p, refcount %lu.\n", iface
, refcount
);
217 static ULONG WINAPI
enum_stat_prop_stg_Release(IEnumSTATPROPSTG
*iface
)
219 struct enum_stat_prop_stg
*penum
= impl_from_IEnumSTATPROPSTG(iface
);
220 LONG refcount
= InterlockedDecrement(&penum
->refcount
);
222 TRACE("%p, refcount %lu.\n", iface
, refcount
);
226 IPropertyStorage_Release(&penum
->storage
->IPropertyStorage_iface
);
227 heap_free(penum
->stats
);
234 static HRESULT WINAPI
enum_stat_prop_stg_Next(IEnumSTATPROPSTG
*iface
, ULONG celt
, STATPROPSTG
*ret
, ULONG
*fetched
)
236 struct enum_stat_prop_stg
*penum
= impl_from_IEnumSTATPROPSTG(iface
);
240 TRACE("%p, %lu, %p, %p.\n", iface
, celt
, ret
, fetched
);
242 if (penum
->current
== ~0u)
245 while (count
< celt
&& penum
->current
< penum
->count
)
247 *ret
= penum
->stats
[penum
->current
++];
249 if (dictionary_find(penum
->storage
->propid_to_name
, UlongToPtr(ret
->propid
), (void **)&name
))
251 SIZE_T size
= (lstrlenW(name
) + 1) * sizeof(WCHAR
);
252 ret
->lpwstrName
= CoTaskMemAlloc(size
);
254 memcpy(ret
->lpwstrName
, name
, size
);
263 return count
< celt
? S_FALSE
: S_OK
;
266 static HRESULT WINAPI
enum_stat_prop_stg_Skip(IEnumSTATPROPSTG
*iface
, ULONG celt
)
268 FIXME("%p, %lu.\n", iface
, celt
);
273 static HRESULT WINAPI
enum_stat_prop_stg_Reset(IEnumSTATPROPSTG
*iface
)
275 struct enum_stat_prop_stg
*penum
= impl_from_IEnumSTATPROPSTG(iface
);
277 TRACE("%p.\n", iface
);
279 penum
->current
= ~0u;
284 static HRESULT WINAPI
enum_stat_prop_stg_Clone(IEnumSTATPROPSTG
*iface
, IEnumSTATPROPSTG
**ppenum
)
286 FIXME("%p, %p.\n", iface
, ppenum
);
291 static const IEnumSTATPROPSTGVtbl enum_stat_prop_stg_vtbl
=
293 enum_stat_prop_stg_QueryInterface
,
294 enum_stat_prop_stg_AddRef
,
295 enum_stat_prop_stg_Release
,
296 enum_stat_prop_stg_Next
,
297 enum_stat_prop_stg_Skip
,
298 enum_stat_prop_stg_Reset
,
299 enum_stat_prop_stg_Clone
,
302 static BOOL
prop_enum_stat(const void *k
, const void *v
, void *extra
, void *arg
)
304 struct enum_stat_prop_stg
*stg
= arg
;
305 PROPID propid
= PtrToUlong(k
);
306 const PROPVARIANT
*prop
= v
;
309 dest
= &stg
->stats
[stg
->count
];
311 dest
->lpwstrName
= NULL
;
312 dest
->propid
= propid
;
319 static BOOL
prop_enum_stat_count(const void *k
, const void *v
, void *extra
, void *arg
)
328 static HRESULT
create_enum_stat_prop_stg(PropertyStorage_impl
*storage
, IEnumSTATPROPSTG
**ret
)
330 struct enum_stat_prop_stg
*enum_obj
;
333 enum_obj
= heap_alloc_zero(sizeof(*enum_obj
));
335 return E_OUTOFMEMORY
;
337 enum_obj
->IEnumSTATPROPSTG_iface
.lpVtbl
= &enum_stat_prop_stg_vtbl
;
338 enum_obj
->refcount
= 1;
339 enum_obj
->storage
= storage
;
340 IPropertyStorage_AddRef(&storage
->IPropertyStorage_iface
);
343 dictionary_enumerate(storage
->propid_to_prop
, prop_enum_stat_count
, &count
);
347 if (!(enum_obj
->stats
= heap_alloc(sizeof(*enum_obj
->stats
) * count
)))
349 IEnumSTATPROPSTG_Release(&enum_obj
->IEnumSTATPROPSTG_iface
);
350 return E_OUTOFMEMORY
;
353 dictionary_enumerate(storage
->propid_to_prop
, prop_enum_stat
, enum_obj
);
356 *ret
= &enum_obj
->IEnumSTATPROPSTG_iface
;
361 /************************************************************************
362 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
364 static HRESULT WINAPI
IPropertyStorage_fnQueryInterface(
365 IPropertyStorage
*iface
,
369 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
371 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppvObject
);
378 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
379 IsEqualGUID(&IID_IPropertyStorage
, riid
))
381 IPropertyStorage_AddRef(iface
);
386 return E_NOINTERFACE
;
389 /************************************************************************
390 * IPropertyStorage_fnAddRef (IPropertyStorage)
392 static ULONG WINAPI
IPropertyStorage_fnAddRef(
393 IPropertyStorage
*iface
)
395 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
396 return InterlockedIncrement(&This
->ref
);
399 /************************************************************************
400 * IPropertyStorage_fnRelease (IPropertyStorage)
402 static ULONG WINAPI
IPropertyStorage_fnRelease(
403 IPropertyStorage
*iface
)
405 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
408 ref
= InterlockedDecrement(&This
->ref
);
411 TRACE("Destroying %p\n", This
);
413 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
414 IStream_Release(This
->stm
);
415 This
->cs
.DebugInfo
->Spare
[0] = 0;
416 DeleteCriticalSection(&This
->cs
);
417 PropertyStorage_DestroyDictionaries(This
);
418 HeapFree(GetProcessHeap(), 0, This
);
423 static PROPVARIANT
*PropertyStorage_FindProperty(PropertyStorage_impl
*This
,
426 PROPVARIANT
*ret
= NULL
;
428 dictionary_find(This
->propid_to_prop
, UlongToPtr(propid
), (void **)&ret
);
429 TRACE("returning %p\n", ret
);
433 /* Returns NULL if name is NULL. */
434 static PROPVARIANT
*PropertyStorage_FindPropertyByName(
435 PropertyStorage_impl
*This
, LPCWSTR name
)
437 PROPVARIANT
*ret
= NULL
;
442 if (This
->codePage
== CP_UNICODE
)
444 if (dictionary_find(This
->name_to_propid
, name
, &propid
))
445 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
450 HRESULT hr
= PropertyStorage_StringCopy((LPCSTR
)name
, CP_UNICODE
,
451 &ansiName
, This
->codePage
);
455 if (dictionary_find(This
->name_to_propid
, ansiName
, &propid
))
456 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
457 CoTaskMemFree(ansiName
);
460 TRACE("returning %p\n", ret
);
464 static LPWSTR
PropertyStorage_FindPropertyNameById(PropertyStorage_impl
*This
,
469 dictionary_find(This
->propid_to_name
, UlongToPtr(propid
), (void **)&ret
);
470 TRACE("returning %p\n", ret
);
474 /************************************************************************
475 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
477 static HRESULT WINAPI
IPropertyStorage_fnReadMultiple(
478 IPropertyStorage
* iface
,
480 const PROPSPEC rgpspec
[],
481 PROPVARIANT rgpropvar
[])
483 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
487 TRACE("%p, %lu, %p, %p\n", iface
, cpspec
, rgpspec
, rgpropvar
);
491 if (!rgpspec
|| !rgpropvar
)
493 EnterCriticalSection(&This
->cs
);
494 for (i
= 0; i
< cpspec
; i
++)
496 PropVariantInit(&rgpropvar
[i
]);
497 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
499 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
503 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
, GetACP(),
508 switch (rgpspec
[i
].propid
)
511 rgpropvar
[i
].vt
= VT_I2
;
512 rgpropvar
[i
].iVal
= This
->codePage
;
515 rgpropvar
[i
].vt
= VT_I4
;
516 rgpropvar
[i
].lVal
= This
->locale
;
520 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
,
524 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
,
525 GetACP(), This
->codePage
);
532 LeaveCriticalSection(&This
->cs
);
536 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, UINT srcCP
, LPSTR
*dst
, UINT dstCP
)
541 TRACE("%s, %p, %d, %d\n",
542 srcCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)src
) : debugstr_a(src
), dst
,
551 if (dstCP
== CP_UNICODE
)
552 len
= (lstrlenW((LPCWSTR
)src
) + 1) * sizeof(WCHAR
);
554 len
= strlen(src
) + 1;
555 *dst
= CoTaskMemAlloc(len
);
557 hr
= STG_E_INSUFFICIENTMEMORY
;
559 memcpy(*dst
, src
, len
);
563 if (dstCP
== CP_UNICODE
)
565 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
566 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
568 hr
= STG_E_INSUFFICIENTMEMORY
;
570 MultiByteToWideChar(srcCP
, 0, src
, -1, (LPWSTR
)*dst
, len
);
574 LPCWSTR wideStr
= NULL
;
575 LPWSTR wideStr_tmp
= NULL
;
577 if (srcCP
== CP_UNICODE
)
578 wideStr
= (LPCWSTR
)src
;
581 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
582 wideStr_tmp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
585 MultiByteToWideChar(srcCP
, 0, src
, -1, wideStr_tmp
, len
);
586 wideStr
= wideStr_tmp
;
589 hr
= STG_E_INSUFFICIENTMEMORY
;
593 len
= WideCharToMultiByte(dstCP
, 0, wideStr
, -1, NULL
, 0,
595 *dst
= CoTaskMemAlloc(len
);
597 hr
= STG_E_INSUFFICIENTMEMORY
;
600 BOOL defCharUsed
= FALSE
;
602 if (WideCharToMultiByte(dstCP
, 0, wideStr
, -1, *dst
, len
,
603 NULL
, &defCharUsed
) == 0 || defCharUsed
)
607 hr
= HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION
);
611 HeapFree(GetProcessHeap(), 0, wideStr_tmp
);
614 TRACE("returning %#lx (%s)\n", hr
,
615 dstCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)*dst
) : debugstr_a(*dst
));
619 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
, const PROPVARIANT
*propvar
,
620 UINT targetCP
, UINT srcCP
)
630 hr
= PropertyStorage_StringCopy(propvar
->pszVal
, srcCP
, &prop
->pszVal
, targetCP
);
635 if ((prop
->bstrVal
= SysAllocStringLen(propvar
->bstrVal
, SysStringLen(propvar
->bstrVal
))))
641 hr
= PropVariantCopy(prop
, propvar
);
647 /* Stores the property with id propid and value propvar into this property
648 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
649 * type is VT_LPSTR, converts the string using lcid as the source code page
650 * and This->codePage as the target code page before storing.
651 * As a side effect, may change This->format to 1 if the type of propvar is
652 * a version 1-only property.
654 static HRESULT
PropertyStorage_StorePropWithId(PropertyStorage_impl
*This
,
655 PROPID propid
, const PROPVARIANT
*propvar
, UINT cp
)
658 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
, propid
);
661 if (propvar
->vt
& VT_BYREF
|| propvar
->vt
& VT_ARRAY
)
669 case VT_VECTOR
|VT_I1
:
672 TRACE("Setting %#lx to type %d\n", propid
, propvar
->vt
);
675 PropVariantClear(prop
);
676 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
, cp
);
680 prop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
681 sizeof(PROPVARIANT
));
684 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
, cp
);
687 dictionary_insert(This
->propid_to_prop
, UlongToPtr(propid
), prop
);
688 if (propid
> This
->highestProp
)
689 This
->highestProp
= propid
;
692 HeapFree(GetProcessHeap(), 0, prop
);
695 hr
= STG_E_INSUFFICIENTMEMORY
;
700 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
701 * srcName is encoded in code page cp, and is converted to This->codePage.
702 * If cp is CP_UNICODE, srcName is actually a unicode string.
703 * As a side effect, may change This->format to 1 if srcName is too long for
704 * a version 0 property storage.
705 * Doesn't validate id.
707 static HRESULT
PropertyStorage_StoreNameWithId(PropertyStorage_impl
*This
,
708 LPCSTR srcName
, UINT cp
, PROPID id
)
715 hr
= PropertyStorage_StringCopy(srcName
, cp
, &name
, This
->codePage
);
718 if (This
->codePage
== CP_UNICODE
)
720 if (lstrlenW((LPWSTR
)name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
725 if (strlen(name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
728 TRACE("Adding prop name %s, propid %ld\n",
729 This
->codePage
== CP_UNICODE
? debugstr_w((LPCWSTR
)name
) :
730 debugstr_a(name
), id
);
731 dictionary_insert(This
->name_to_propid
, name
, UlongToPtr(id
));
732 dictionary_insert(This
->propid_to_name
, UlongToPtr(id
), name
);
737 /************************************************************************
738 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
740 static HRESULT WINAPI
IPropertyStorage_fnWriteMultiple(
741 IPropertyStorage
* iface
,
743 const PROPSPEC rgpspec
[],
744 const PROPVARIANT rgpropvar
[],
745 PROPID propidNameFirst
)
747 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
751 TRACE("%p, %lu, %p, %p.\n", iface
, cpspec
, rgpspec
, rgpropvar
);
753 if (cpspec
&& (!rgpspec
|| !rgpropvar
))
755 if (!(This
->grfMode
& STGM_READWRITE
))
756 return STG_E_ACCESSDENIED
;
757 EnterCriticalSection(&This
->cs
);
759 This
->originatorOS
= (DWORD
)MAKELONG(LOWORD(GetVersion()),
760 PROPSETHDR_OSVER_KIND_WIN32
) ;
761 for (i
= 0; i
< cpspec
; i
++)
763 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
765 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
769 PropVariantCopy(prop
, &rgpropvar
[i
]);
772 /* Note that I don't do the special cases here that I do below,
773 * because naming the special PIDs isn't supported.
775 if (propidNameFirst
< PID_FIRST_USABLE
||
776 propidNameFirst
>= PID_MIN_READONLY
)
777 hr
= STG_E_INVALIDPARAMETER
;
780 PROPID nextId
= max(propidNameFirst
, This
->highestProp
+ 1);
782 hr
= PropertyStorage_StoreNameWithId(This
,
783 (LPCSTR
)rgpspec
[i
].lpwstr
, CP_UNICODE
, nextId
);
785 hr
= PropertyStorage_StorePropWithId(This
, nextId
,
786 &rgpropvar
[i
], GetACP());
792 switch (rgpspec
[i
].propid
)
795 /* Can't set the dictionary */
796 hr
= STG_E_INVALIDPARAMETER
;
799 /* Can only set the code page if nothing else has been set */
800 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
801 rgpropvar
[i
].vt
== VT_I2
)
803 This
->codePage
= (USHORT
)rgpropvar
[i
].iVal
;
804 if (This
->codePage
== CP_UNICODE
)
805 This
->grfFlags
&= ~PROPSETFLAG_ANSI
;
807 This
->grfFlags
|= PROPSETFLAG_ANSI
;
810 hr
= STG_E_INVALIDPARAMETER
;
813 /* Can only set the locale if nothing else has been set */
814 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
815 rgpropvar
[i
].vt
== VT_I4
)
816 This
->locale
= rgpropvar
[i
].lVal
;
818 hr
= STG_E_INVALIDPARAMETER
;
821 /* silently ignore like MSDN says */
824 if (rgpspec
[i
].propid
>= PID_MIN_READONLY
)
825 hr
= STG_E_INVALIDPARAMETER
;
827 hr
= PropertyStorage_StorePropWithId(This
,
828 rgpspec
[i
].propid
, &rgpropvar
[i
], GetACP());
832 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
833 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
834 LeaveCriticalSection(&This
->cs
);
838 /************************************************************************
839 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
841 static HRESULT WINAPI
IPropertyStorage_fnDeleteMultiple(
842 IPropertyStorage
* iface
,
844 const PROPSPEC rgpspec
[])
846 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
850 TRACE("%p, %ld, %p.\n", iface
, cpspec
, rgpspec
);
852 if (cpspec
&& !rgpspec
)
854 if (!(This
->grfMode
& STGM_READWRITE
))
855 return STG_E_ACCESSDENIED
;
857 EnterCriticalSection(&This
->cs
);
859 for (i
= 0; i
< cpspec
; i
++)
861 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
865 if (dictionary_find(This
->name_to_propid
, rgpspec
[i
].lpwstr
, &propid
))
866 dictionary_remove(This
->propid_to_prop
, propid
);
870 if (rgpspec
[i
].propid
>= PID_FIRST_USABLE
&&
871 rgpspec
[i
].propid
< PID_MIN_READONLY
)
872 dictionary_remove(This
->propid_to_prop
, UlongToPtr(rgpspec
[i
].propid
));
874 hr
= STG_E_INVALIDPARAMETER
;
877 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
878 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
879 LeaveCriticalSection(&This
->cs
);
883 /************************************************************************
884 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
886 static HRESULT WINAPI
IPropertyStorage_fnReadPropertyNames(
887 IPropertyStorage
* iface
,
889 const PROPID rgpropid
[],
890 LPOLESTR rglpwstrName
[])
892 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
894 HRESULT hr
= S_FALSE
;
896 TRACE("%p, %ld, %p, %p.\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
898 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
900 EnterCriticalSection(&This
->cs
);
901 for (i
= 0; i
< cpropid
&& SUCCEEDED(hr
); i
++)
903 LPWSTR name
= PropertyStorage_FindPropertyNameById(This
, rgpropid
[i
]);
907 size_t len
= lstrlenW(name
);
910 rglpwstrName
[i
] = CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
));
912 memcpy(rglpwstrName
[i
], name
, (len
+ 1) * sizeof(WCHAR
));
914 hr
= STG_E_INSUFFICIENTMEMORY
;
917 rglpwstrName
[i
] = NULL
;
919 LeaveCriticalSection(&This
->cs
);
923 /************************************************************************
924 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
926 static HRESULT WINAPI
IPropertyStorage_fnWritePropertyNames(
927 IPropertyStorage
* iface
,
929 const PROPID rgpropid
[],
930 const LPOLESTR rglpwstrName
[])
932 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
936 TRACE("%p, %lu, %p, %p.\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
938 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
940 if (!(This
->grfMode
& STGM_READWRITE
))
941 return STG_E_ACCESSDENIED
;
943 EnterCriticalSection(&This
->cs
);
945 for (i
= 0; SUCCEEDED(hr
) && i
< cpropid
; i
++)
947 if (rgpropid
[i
] != PID_ILLEGAL
)
948 hr
= PropertyStorage_StoreNameWithId(This
, (LPCSTR
)rglpwstrName
[i
],
949 CP_UNICODE
, rgpropid
[i
]);
951 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
952 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
953 LeaveCriticalSection(&This
->cs
);
957 /************************************************************************
958 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
960 static HRESULT WINAPI
IPropertyStorage_fnDeletePropertyNames(
961 IPropertyStorage
* iface
,
963 const PROPID rgpropid
[])
965 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
969 TRACE("%p, %ld, %p.\n", iface
, cpropid
, rgpropid
);
971 if (cpropid
&& !rgpropid
)
973 if (!(This
->grfMode
& STGM_READWRITE
))
974 return STG_E_ACCESSDENIED
;
976 EnterCriticalSection(&This
->cs
);
978 for (i
= 0; i
< cpropid
; i
++)
982 if (dictionary_find(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]), (void **)&name
))
984 dictionary_remove(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]));
985 dictionary_remove(This
->name_to_propid
, name
);
988 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
989 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
990 LeaveCriticalSection(&This
->cs
);
994 /************************************************************************
995 * IPropertyStorage_fnCommit (IPropertyStorage)
997 static HRESULT WINAPI
IPropertyStorage_fnCommit(
998 IPropertyStorage
* iface
,
999 DWORD grfCommitFlags
)
1001 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
1004 TRACE("%p, %#lx.\n", iface
, grfCommitFlags
);
1006 if (!(This
->grfMode
& STGM_READWRITE
))
1007 return STG_E_ACCESSDENIED
;
1008 EnterCriticalSection(&This
->cs
);
1010 hr
= PropertyStorage_WriteToStream(This
);
1013 LeaveCriticalSection(&This
->cs
);
1017 /************************************************************************
1018 * IPropertyStorage_fnRevert (IPropertyStorage)
1020 static HRESULT WINAPI
IPropertyStorage_fnRevert(
1021 IPropertyStorage
* iface
)
1024 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
1026 TRACE("%p\n", iface
);
1028 EnterCriticalSection(&This
->cs
);
1031 PropertyStorage_DestroyDictionaries(This
);
1032 hr
= PropertyStorage_CreateDictionaries(This
);
1034 hr
= PropertyStorage_ReadFromStream(This
);
1038 LeaveCriticalSection(&This
->cs
);
1042 /************************************************************************
1043 * IPropertyStorage_fnEnum (IPropertyStorage)
1045 static HRESULT WINAPI
IPropertyStorage_fnEnum(IPropertyStorage
*iface
, IEnumSTATPROPSTG
**ppenum
)
1047 PropertyStorage_impl
*storage
= impl_from_IPropertyStorage(iface
);
1049 TRACE("%p, %p.\n", iface
, ppenum
);
1051 return create_enum_stat_prop_stg(storage
, ppenum
);
1054 /************************************************************************
1055 * IPropertyStorage_fnSetTimes (IPropertyStorage)
1057 static HRESULT WINAPI
IPropertyStorage_fnSetTimes(
1058 IPropertyStorage
* iface
,
1059 const FILETIME
* pctime
,
1060 const FILETIME
* patime
,
1061 const FILETIME
* pmtime
)
1067 /************************************************************************
1068 * IPropertyStorage_fnSetClass (IPropertyStorage)
1070 static HRESULT WINAPI
IPropertyStorage_fnSetClass(
1071 IPropertyStorage
* iface
,
1074 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
1076 TRACE("%p, %s\n", iface
, debugstr_guid(clsid
));
1079 return E_INVALIDARG
;
1080 if (!(This
->grfMode
& STGM_READWRITE
))
1081 return STG_E_ACCESSDENIED
;
1082 This
->clsid
= *clsid
;
1084 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
1085 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
1089 /************************************************************************
1090 * IPropertyStorage_fnStat (IPropertyStorage)
1092 static HRESULT WINAPI
IPropertyStorage_fnStat(
1093 IPropertyStorage
* iface
,
1094 STATPROPSETSTG
* statpsstg
)
1096 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
1100 TRACE("%p, %p\n", iface
, statpsstg
);
1103 return E_INVALIDARG
;
1105 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1108 statpsstg
->fmtid
= This
->fmtid
;
1109 statpsstg
->clsid
= This
->clsid
;
1110 statpsstg
->grfFlags
= This
->grfFlags
;
1111 statpsstg
->mtime
= stat
.mtime
;
1112 statpsstg
->ctime
= stat
.ctime
;
1113 statpsstg
->atime
= stat
.atime
;
1114 statpsstg
->dwOSVersion
= This
->originatorOS
;
1119 static int PropertyStorage_PropNameCompare(const void *a
, const void *b
,
1122 PropertyStorage_impl
*This
= extra
;
1124 if (This
->codePage
== CP_UNICODE
)
1126 TRACE("(%s, %s)\n", debugstr_w(a
), debugstr_w(b
));
1127 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1128 return wcscmp(a
, b
);
1130 return lstrcmpiW(a
, b
);
1134 TRACE("(%s, %s)\n", debugstr_a(a
), debugstr_a(b
));
1135 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1136 return lstrcmpA(a
, b
);
1138 return lstrcmpiA(a
, b
);
1142 static void PropertyStorage_PropNameDestroy(void *k
, void *d
, void *extra
)
1147 static int PropertyStorage_PropCompare(const void *a
, const void *b
,
1150 TRACE("%lu, %lu.\n", PtrToUlong(a
), PtrToUlong(b
));
1151 return PtrToUlong(a
) - PtrToUlong(b
);
1154 static void PropertyStorage_PropertyDestroy(void *k
, void *d
, void *extra
)
1156 PropVariantClear(d
);
1157 HeapFree(GetProcessHeap(), 0, d
);
1160 #ifdef WORDS_BIGENDIAN
1161 /* Swaps each character in str to or from little endian; assumes the conversion
1162 * is symmetric, that is, that lendian16toh is equivalent to htole16.
1164 static void PropertyStorage_ByteSwapString(LPWSTR str
, size_t len
)
1168 /* Swap characters to host order.
1171 for (i
= 0; i
< len
; i
++)
1172 str
[i
] = lendian16toh(str
[i
]);
1175 #define PropertyStorage_ByteSwapString(s, l)
1178 static void* WINAPI
Allocate_CoTaskMemAlloc(void *this, ULONG size
)
1180 return CoTaskMemAlloc(size
);
1189 static HRESULT
buffer_test_offset(const struct read_buffer
*buffer
, size_t offset
, size_t len
)
1191 return len
> buffer
->size
|| offset
> buffer
->size
- len
? STG_E_READFAULT
: S_OK
;
1194 static HRESULT
buffer_read_uint64(const struct read_buffer
*buffer
, size_t offset
, ULARGE_INTEGER
*data
)
1198 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, sizeof(*data
))))
1199 StorageUtl_ReadULargeInteger(buffer
->data
, offset
, data
);
1204 static HRESULT
buffer_read_dword(const struct read_buffer
*buffer
, size_t offset
, DWORD
*data
)
1208 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, sizeof(*data
))))
1209 StorageUtl_ReadDWord(buffer
->data
, offset
, data
);
1214 static HRESULT
buffer_read_word(const struct read_buffer
*buffer
, size_t offset
, WORD
*data
)
1218 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, sizeof(*data
))))
1219 StorageUtl_ReadWord(buffer
->data
, offset
, data
);
1224 static HRESULT
buffer_read_byte(const struct read_buffer
*buffer
, size_t offset
, BYTE
*data
)
1228 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, sizeof(*data
))))
1229 *data
= *(buffer
->data
+ offset
);
1234 static HRESULT
buffer_read_len(const struct read_buffer
*buffer
, size_t offset
, void *dest
, size_t len
)
1238 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, len
)))
1239 memcpy(dest
, buffer
->data
+ offset
, len
);
1244 static HRESULT
propertystorage_read_scalar(PROPVARIANT
*prop
, const struct read_buffer
*buffer
, size_t offset
,
1245 UINT codepage
, void* (WINAPI
*allocate
)(void *this, ULONG size
), void *allocate_data
)
1249 assert(!(prop
->vt
& (VT_ARRAY
| VT_VECTOR
)));
1258 hr
= buffer_read_byte(buffer
, offset
, (BYTE
*)&prop
->cVal
);
1259 TRACE("Read char 0x%x ('%c')\n", prop
->cVal
, prop
->cVal
);
1262 hr
= buffer_read_byte(buffer
, offset
, &prop
->bVal
);
1263 TRACE("Read byte 0x%x\n", prop
->bVal
);
1266 hr
= buffer_read_word(buffer
, offset
, (WORD
*)&prop
->boolVal
);
1267 TRACE("Read bool %d\n", prop
->boolVal
);
1270 hr
= buffer_read_word(buffer
, offset
, (WORD
*)&prop
->iVal
);
1271 TRACE("Read short %d\n", prop
->iVal
);
1274 hr
= buffer_read_word(buffer
, offset
, &prop
->uiVal
);
1275 TRACE("Read ushort %d\n", prop
->uiVal
);
1279 hr
= buffer_read_dword(buffer
, offset
, (DWORD
*)&prop
->lVal
);
1280 TRACE("Read long %ld\n", prop
->lVal
);
1284 hr
= buffer_read_dword(buffer
, offset
, &prop
->ulVal
);
1285 TRACE("Read ulong %ld\n", prop
->ulVal
);
1288 hr
= buffer_read_uint64(buffer
, offset
, (ULARGE_INTEGER
*)&prop
->hVal
);
1289 TRACE("Read long long %s\n", wine_dbgstr_longlong(prop
->hVal
.QuadPart
));
1292 hr
= buffer_read_uint64(buffer
, offset
, &prop
->uhVal
);
1293 TRACE("Read ulong long %s\n", wine_dbgstr_longlong(prop
->uhVal
.QuadPart
));
1296 hr
= buffer_read_len(buffer
, offset
, &prop
->dblVal
, sizeof(prop
->dblVal
));
1297 TRACE("Read double %f\n", prop
->dblVal
);
1303 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &count
)))
1306 offset
+= sizeof(DWORD
);
1308 if (codepage
== CP_UNICODE
&& count
% sizeof(WCHAR
))
1310 WARN("Unicode string has odd number of bytes\n");
1311 hr
= STG_E_INVALIDHEADER
;
1315 prop
->pszVal
= allocate(allocate_data
, count
);
1318 if (FAILED(hr
= buffer_read_len(buffer
, offset
, prop
->pszVal
, count
)))
1321 /* This is stored in the code page specified in codepage.
1322 * Don't convert it, the caller will just store it as-is.
1324 if (codepage
== CP_UNICODE
)
1326 /* Make sure it's NULL-terminated */
1327 prop
->pszVal
[count
/ sizeof(WCHAR
) - 1] = '\0';
1328 TRACE("Read string value %s\n",
1329 debugstr_w(prop
->pwszVal
));
1333 /* Make sure it's NULL-terminated */
1334 prop
->pszVal
[count
- 1] = '\0';
1335 TRACE("Read string value %s\n", debugstr_a(prop
->pszVal
));
1339 hr
= STG_E_INSUFFICIENTMEMORY
;
1345 DWORD count
, wcount
;
1347 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &count
)))
1350 offset
+= sizeof(DWORD
);
1352 if (codepage
== CP_UNICODE
&& count
% sizeof(WCHAR
))
1354 WARN("Unicode string has odd number of bytes\n");
1355 hr
= STG_E_INVALIDHEADER
;
1359 if (codepage
== CP_UNICODE
)
1360 wcount
= count
/ sizeof(WCHAR
);
1363 if (FAILED(hr
= buffer_test_offset(buffer
, offset
, count
)))
1365 wcount
= MultiByteToWideChar(codepage
, 0, (LPCSTR
)(buffer
->data
+ offset
), count
, NULL
, 0);
1368 prop
->bstrVal
= SysAllocStringLen(NULL
, wcount
); /* FIXME: use allocator? */
1372 if (codepage
== CP_UNICODE
)
1373 hr
= buffer_read_len(buffer
, offset
, prop
->bstrVal
, count
);
1375 MultiByteToWideChar(codepage
, 0, (LPCSTR
)(buffer
->data
+ offset
), count
, prop
->bstrVal
, wcount
);
1377 prop
->bstrVal
[wcount
- 1] = '\0';
1378 TRACE("Read string value %s\n", debugstr_w(prop
->bstrVal
));
1381 hr
= STG_E_INSUFFICIENTMEMORY
;
1389 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &count
)))
1392 offset
+= sizeof(DWORD
);
1394 prop
->blob
.cbSize
= count
;
1395 prop
->blob
.pBlobData
= allocate(allocate_data
, count
);
1396 if (prop
->blob
.pBlobData
)
1398 hr
= buffer_read_len(buffer
, offset
, prop
->blob
.pBlobData
, count
);
1399 TRACE("Read blob value of size %ld\n", count
);
1402 hr
= STG_E_INSUFFICIENTMEMORY
;
1409 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &count
)))
1412 offset
+= sizeof(DWORD
);
1414 prop
->pwszVal
= allocate(allocate_data
, count
* sizeof(WCHAR
));
1417 if (SUCCEEDED(hr
= buffer_read_len(buffer
, offset
, prop
->pwszVal
, count
* sizeof(WCHAR
))))
1419 /* make sure string is NULL-terminated */
1420 prop
->pwszVal
[count
- 1] = '\0';
1421 PropertyStorage_ByteSwapString(prop
->pwszVal
, count
);
1422 TRACE("Read string value %s\n", debugstr_w(prop
->pwszVal
));
1426 hr
= STG_E_INSUFFICIENTMEMORY
;
1430 hr
= buffer_read_uint64(buffer
, offset
, (ULARGE_INTEGER
*)&prop
->filetime
);
1434 DWORD len
= 0, tag
= 0;
1436 if (SUCCEEDED(hr
= buffer_read_dword(buffer
, offset
, &len
)))
1437 hr
= buffer_read_dword(buffer
, offset
+ sizeof(DWORD
), &tag
);
1441 offset
+= 2 * sizeof(DWORD
);
1446 prop
->pclipdata
= allocate(allocate_data
, sizeof (CLIPDATA
));
1447 prop
->pclipdata
->cbSize
= len
;
1448 prop
->pclipdata
->ulClipFmt
= tag
;
1449 prop
->pclipdata
->pClipData
= allocate(allocate_data
, len
- sizeof(prop
->pclipdata
->ulClipFmt
));
1450 hr
= buffer_read_len(buffer
, offset
, prop
->pclipdata
->pClipData
, len
- sizeof(prop
->pclipdata
->ulClipFmt
));
1453 hr
= STG_E_INVALIDPARAMETER
;
1457 if (!(prop
->puuid
= allocate(allocate_data
, sizeof (*prop
->puuid
))))
1458 return STG_E_INSUFFICIENTMEMORY
;
1460 if (SUCCEEDED(hr
= buffer_test_offset(buffer
, offset
, sizeof(*prop
->puuid
))))
1461 StorageUtl_ReadGUID(buffer
->data
, offset
, prop
->puuid
);
1465 FIXME("unsupported type %d\n", prop
->vt
);
1466 hr
= STG_E_INVALIDPARAMETER
;
1472 static size_t propertystorage_get_elemsize(const PROPVARIANT
*prop
)
1474 if (!(prop
->vt
& VT_VECTOR
))
1477 switch (prop
->vt
& ~VT_VECTOR
)
1479 case VT_I1
: return sizeof(*prop
->cac
.pElems
);
1480 case VT_UI1
: return sizeof(*prop
->caub
.pElems
);
1481 case VT_I2
: return sizeof(*prop
->cai
.pElems
);
1482 case VT_UI2
: return sizeof(*prop
->caui
.pElems
);
1483 case VT_BOOL
: return sizeof(*prop
->cabool
.pElems
);
1484 case VT_I4
: return sizeof(*prop
->cal
.pElems
);
1485 case VT_UI4
: return sizeof(*prop
->caul
.pElems
);
1486 case VT_R4
: return sizeof(*prop
->caflt
.pElems
);
1487 case VT_ERROR
: return sizeof(*prop
->cascode
.pElems
);
1488 case VT_I8
: return sizeof(*prop
->cah
.pElems
);
1489 case VT_UI8
: return sizeof(*prop
->cauh
.pElems
);
1490 case VT_R8
: return sizeof(*prop
->cadbl
.pElems
);
1491 case VT_CY
: return sizeof(*prop
->cacy
.pElems
);
1492 case VT_DATE
: return sizeof(*prop
->cadate
.pElems
);
1493 case VT_FILETIME
: return sizeof(*prop
->cafiletime
.pElems
);
1494 case VT_CLSID
: return sizeof(*prop
->cauuid
.pElems
);
1495 case VT_VARIANT
: return sizeof(*prop
->capropvar
.pElems
);
1497 FIXME("Unhandled type %#x.\n", prop
->vt
);
1502 static HRESULT
PropertyStorage_ReadProperty(PROPVARIANT
*prop
, const struct read_buffer
*buffer
,
1503 size_t offset
, UINT codepage
, void* (WINAPI
*allocate
)(void *this, ULONG size
), void *allocate_data
)
1509 assert(buffer
->data
);
1511 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &vt
)))
1514 offset
+= sizeof(DWORD
);
1517 if (prop
->vt
& VT_VECTOR
)
1521 switch (prop
->vt
& VT_VECTOR
)
1528 FIXME("Vector with variable length elements are not supported.\n");
1529 return STG_E_INVALIDPARAMETER
;
1534 if (SUCCEEDED(hr
= buffer_read_dword(buffer
, offset
, &count
)))
1536 size_t elemsize
= propertystorage_get_elemsize(prop
);
1539 offset
+= sizeof(DWORD
);
1541 if ((prop
->capropvar
.pElems
= allocate(allocate_data
, elemsize
* count
)))
1543 prop
->capropvar
.cElems
= count
;
1544 elem
.vt
= prop
->vt
& ~VT_VECTOR
;
1546 for (i
= 0; i
< count
; ++i
)
1548 if (SUCCEEDED(hr
= propertystorage_read_scalar(&elem
, buffer
, offset
+ i
* elemsize
, codepage
,
1549 allocate
, allocate_data
)))
1551 memcpy(&prop
->capropvar
.pElems
[i
], &elem
.lVal
, elemsize
);
1556 hr
= STG_E_INSUFFICIENTMEMORY
;
1559 else if (prop
->vt
& VT_ARRAY
)
1561 FIXME("VT_ARRAY properties are not supported.\n");
1562 hr
= STG_E_INVALIDPARAMETER
;
1565 hr
= propertystorage_read_scalar(prop
, buffer
, offset
, codepage
, allocate
, allocate_data
);
1570 static HRESULT
PropertyStorage_ReadHeaderFromStream(IStream
*stm
,
1571 PROPERTYSETHEADER
*hdr
)
1573 BYTE buf
[sizeof(PROPERTYSETHEADER
)];
1579 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1582 if (count
!= sizeof(buf
))
1584 WARN("read only %ld\n", count
);
1585 hr
= STG_E_INVALIDHEADER
;
1589 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wByteOrder
),
1591 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wFormat
),
1593 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, dwOSVer
),
1595 StorageUtl_ReadGUID(buf
, offsetof(PROPERTYSETHEADER
, clsid
),
1597 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, reserved
),
1601 TRACE("returning %#lx\n", hr
);
1605 static HRESULT
PropertyStorage_ReadFmtIdOffsetFromStream(IStream
*stm
,
1606 FORMATIDOFFSET
*fmt
)
1608 BYTE buf
[sizeof(FORMATIDOFFSET
)];
1614 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1617 if (count
!= sizeof(buf
))
1619 WARN("read only %ld\n", count
);
1620 hr
= STG_E_INVALIDHEADER
;
1624 StorageUtl_ReadGUID(buf
, offsetof(FORMATIDOFFSET
, fmtid
),
1626 StorageUtl_ReadDWord(buf
, offsetof(FORMATIDOFFSET
, dwOffset
),
1630 TRACE("returning %#lx\n", hr
);
1634 static HRESULT
PropertyStorage_ReadSectionHeaderFromStream(IStream
*stm
,
1635 PROPERTYSECTIONHEADER
*hdr
)
1637 BYTE buf
[sizeof(PROPERTYSECTIONHEADER
)];
1643 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1646 if (count
!= sizeof(buf
))
1648 WARN("read only %ld\n", count
);
1649 hr
= STG_E_INVALIDHEADER
;
1653 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1654 cbSection
), &hdr
->cbSection
);
1655 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1656 cProperties
), &hdr
->cProperties
);
1659 TRACE("returning %#lx\n", hr
);
1663 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
1664 * the entries according to the values of This->codePage and This->locale.
1666 static HRESULT
PropertyStorage_ReadDictionary(PropertyStorage_impl
*This
, const struct read_buffer
*buffer
,
1669 DWORD numEntries
, i
;
1672 assert(This
->name_to_propid
);
1673 assert(This
->propid_to_name
);
1675 if (FAILED(hr
= buffer_read_dword(buffer
, offset
, &numEntries
)))
1678 TRACE("Reading %ld entries:\n", numEntries
);
1680 offset
+= sizeof(DWORD
);
1682 for (i
= 0; SUCCEEDED(hr
) && i
< numEntries
; i
++)
1688 if (SUCCEEDED(hr
= buffer_read_dword(buffer
, offset
, &propid
)))
1689 hr
= buffer_read_dword(buffer
, offset
+ sizeof(PROPID
), &cbEntry
);
1693 offset
+= sizeof(PROPID
) + sizeof(DWORD
);
1695 if (FAILED(hr
= buffer_test_offset(buffer
, offset
, This
->codePage
== CP_UNICODE
?
1696 ALIGNED_LENGTH(cbEntry
* sizeof(WCHAR
), 3) : cbEntry
)))
1698 WARN("Broken name length for entry %ld.\n", i
);
1702 /* Make sure the source string is NULL-terminated */
1703 if (This
->codePage
!= CP_UNICODE
)
1704 buffer_read_byte(buffer
, offset
+ cbEntry
- 1, (BYTE
*)&ch
);
1706 buffer_read_word(buffer
, offset
+ (cbEntry
- 1) * sizeof(WCHAR
), &ch
);
1710 WARN("Dictionary entry name %ld is not null-terminated.\n", i
);
1714 TRACE("Reading entry with ID %#lx, %ld chars, name %s.\n", propid
, cbEntry
, This
->codePage
== CP_UNICODE
?
1715 debugstr_wn((WCHAR
*)buffer
->data
, cbEntry
) : debugstr_an((char *)buffer
->data
, cbEntry
));
1717 hr
= PropertyStorage_StoreNameWithId(This
, (char *)buffer
->data
+ offset
, This
->codePage
, propid
);
1718 /* Unicode entries are padded to DWORD boundaries */
1719 if (This
->codePage
== CP_UNICODE
)
1720 cbEntry
= ALIGNED_LENGTH(cbEntry
* sizeof(WCHAR
), 3);
1728 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*This
)
1730 struct read_buffer read_buffer
;
1731 PROPERTYSETHEADER hdr
;
1732 FORMATIDOFFSET fmtOffset
;
1733 PROPERTYSECTIONHEADER sectionHdr
;
1740 DWORD dictOffset
= 0;
1742 This
->dirty
= FALSE
;
1743 This
->highestProp
= 0;
1744 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1747 if (stat
.cbSize
.HighPart
)
1749 WARN("stream too big\n");
1750 /* maximum size varies, but it can't be this big */
1751 hr
= STG_E_INVALIDHEADER
;
1754 if (stat
.cbSize
.LowPart
== 0)
1756 /* empty stream is okay */
1760 else if (stat
.cbSize
.LowPart
< sizeof(PROPERTYSETHEADER
) +
1761 sizeof(FORMATIDOFFSET
))
1763 WARN("stream too small\n");
1764 hr
= STG_E_INVALIDHEADER
;
1768 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1771 hr
= PropertyStorage_ReadHeaderFromStream(This
->stm
, &hdr
);
1772 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1775 if (hdr
.wByteOrder
!= PROPSETHDR_BYTEORDER_MAGIC
|| hdr
.reserved
< 1)
1777 WARN("bad magic in prop set header\n");
1778 hr
= STG_E_INVALIDHEADER
;
1781 if (hdr
.wFormat
!= 0 && hdr
.wFormat
!= 1)
1783 WARN("bad format version %d\n", hdr
.wFormat
);
1784 hr
= STG_E_INVALIDHEADER
;
1787 This
->format
= hdr
.wFormat
;
1788 This
->clsid
= hdr
.clsid
;
1789 This
->originatorOS
= hdr
.dwOSVer
;
1790 if (PROPSETHDR_OSVER_KIND(hdr
.dwOSVer
) == PROPSETHDR_OSVER_KIND_MAC
)
1791 WARN("File comes from a Mac, strings will probably be screwed up\n");
1792 hr
= PropertyStorage_ReadFmtIdOffsetFromStream(This
->stm
, &fmtOffset
);
1795 if (fmtOffset
.dwOffset
> stat
.cbSize
.LowPart
)
1797 WARN("invalid offset %ld (stream length is %ld)\n", fmtOffset
.dwOffset
, stat
.cbSize
.LowPart
);
1798 hr
= STG_E_INVALIDHEADER
;
1801 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1802 * follows not one, but two sections. The first contains the standard properties
1803 * for the document summary information, and the second consists of user-defined
1804 * properties. This is the only case in which multiple sections are
1806 * Reading the second stream isn't implemented yet.
1808 hr
= PropertyStorage_ReadSectionHeaderFromStream(This
->stm
, §ionHdr
);
1811 /* The section size includes the section header, so check it */
1812 if (sectionHdr
.cbSection
< sizeof(PROPERTYSECTIONHEADER
))
1814 WARN("section header too small, got %ld\n", sectionHdr
.cbSection
);
1815 hr
= STG_E_INVALIDHEADER
;
1818 buf
= HeapAlloc(GetProcessHeap(), 0, sectionHdr
.cbSection
-
1819 sizeof(PROPERTYSECTIONHEADER
));
1822 hr
= STG_E_INSUFFICIENTMEMORY
;
1825 read_buffer
.data
= buf
;
1826 read_buffer
.size
= sectionHdr
.cbSection
- sizeof(sectionHdr
);
1828 hr
= IStream_Read(This
->stm
, read_buffer
.data
, read_buffer
.size
, &count
);
1831 TRACE("Reading %ld properties:\n", sectionHdr
.cProperties
);
1832 for (i
= 0; SUCCEEDED(hr
) && i
< sectionHdr
.cProperties
; i
++)
1834 PROPERTYIDOFFSET
*idOffset
= (PROPERTYIDOFFSET
*)(read_buffer
.data
+
1835 i
* sizeof(PROPERTYIDOFFSET
));
1837 if (idOffset
->dwOffset
< sizeof(PROPERTYSECTIONHEADER
) ||
1838 idOffset
->dwOffset
> sectionHdr
.cbSection
- sizeof(DWORD
))
1839 hr
= STG_E_INVALIDPOINTER
;
1842 if (idOffset
->propid
>= PID_FIRST_USABLE
&&
1843 idOffset
->propid
< PID_MIN_READONLY
&& idOffset
->propid
>
1845 This
->highestProp
= idOffset
->propid
;
1846 if (idOffset
->propid
== PID_DICTIONARY
)
1848 /* Don't read the dictionary yet, its entries depend on the
1849 * code page. Just store the offset so we know to read it
1852 dictOffset
= idOffset
->dwOffset
;
1853 TRACE("Dictionary offset is %ld\n", dictOffset
);
1859 PropVariantInit(&prop
);
1860 if (SUCCEEDED(PropertyStorage_ReadProperty(&prop
, &read_buffer
,
1861 idOffset
->dwOffset
- sizeof(PROPERTYSECTIONHEADER
), This
->codePage
,
1862 Allocate_CoTaskMemAlloc
, NULL
)))
1864 TRACE("Read property with ID %#lx, type %d\n", idOffset
->propid
, prop
.vt
);
1865 switch(idOffset
->propid
)
1868 if (prop
.vt
== VT_I2
)
1869 This
->codePage
= (USHORT
)prop
.iVal
;
1872 if (prop
.vt
== VT_I4
)
1873 This
->locale
= (LCID
)prop
.lVal
;
1876 if (prop
.vt
== VT_I4
&& prop
.lVal
)
1877 This
->grfFlags
|= PROPSETFLAG_CASE_SENSITIVE
;
1878 /* The format should already be 1, but just in case */
1882 hr
= PropertyStorage_StorePropWithId(This
,
1883 idOffset
->propid
, &prop
, This
->codePage
);
1886 PropVariantClear(&prop
);
1890 if (!This
->codePage
)
1892 /* default to Unicode unless told not to, as specified on msdn */
1893 if (This
->grfFlags
& PROPSETFLAG_ANSI
)
1894 This
->codePage
= GetACP();
1896 This
->codePage
= CP_UNICODE
;
1899 This
->locale
= LOCALE_SYSTEM_DEFAULT
;
1900 TRACE("Code page is %d, locale is %ld\n", This
->codePage
, This
->locale
);
1902 hr
= PropertyStorage_ReadDictionary(This
, &read_buffer
, dictOffset
- sizeof(PROPERTYSECTIONHEADER
));
1905 HeapFree(GetProcessHeap(), 0, buf
);
1908 dictionary_destroy(This
->name_to_propid
);
1909 This
->name_to_propid
= NULL
;
1910 dictionary_destroy(This
->propid_to_name
);
1911 This
->propid_to_name
= NULL
;
1912 dictionary_destroy(This
->propid_to_prop
);
1913 This
->propid_to_prop
= NULL
;
1918 static void PropertyStorage_MakeHeader(PropertyStorage_impl
*This
,
1919 PROPERTYSETHEADER
*hdr
)
1922 StorageUtl_WriteWord(&hdr
->wByteOrder
, 0, PROPSETHDR_BYTEORDER_MAGIC
);
1923 StorageUtl_WriteWord(&hdr
->wFormat
, 0, This
->format
);
1924 StorageUtl_WriteDWord(&hdr
->dwOSVer
, 0, This
->originatorOS
);
1925 StorageUtl_WriteGUID(&hdr
->clsid
, 0, &This
->clsid
);
1926 StorageUtl_WriteDWord(&hdr
->reserved
, 0, 1);
1929 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl
*This
,
1930 FORMATIDOFFSET
*fmtOffset
)
1933 StorageUtl_WriteGUID(fmtOffset
, 0, &This
->fmtid
);
1934 StorageUtl_WriteDWord(fmtOffset
, offsetof(FORMATIDOFFSET
, dwOffset
),
1935 sizeof(PROPERTYSETHEADER
) + sizeof(FORMATIDOFFSET
));
1938 static void PropertyStorage_MakeSectionHdr(DWORD cbSection
, DWORD numProps
,
1939 PROPERTYSECTIONHEADER
*hdr
)
1942 StorageUtl_WriteDWord(hdr
, 0, cbSection
);
1943 StorageUtl_WriteDWord(hdr
, offsetof(PROPERTYSECTIONHEADER
, cProperties
), numProps
);
1946 static void PropertyStorage_MakePropertyIdOffset(DWORD propid
, DWORD dwOffset
,
1947 PROPERTYIDOFFSET
*propIdOffset
)
1949 assert(propIdOffset
);
1950 StorageUtl_WriteDWord(propIdOffset
, 0, propid
);
1951 StorageUtl_WriteDWord(propIdOffset
, offsetof(PROPERTYIDOFFSET
, dwOffset
), dwOffset
);
1954 static inline HRESULT
PropertyStorage_WriteWStringToStream(IStream
*stm
,
1955 LPCWSTR str
, DWORD len
, DWORD
*written
)
1957 #ifdef WORDS_BIGENDIAN
1958 WCHAR
*leStr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1962 return E_OUTOFMEMORY
;
1963 memcpy(leStr
, str
, len
* sizeof(WCHAR
));
1964 PropertyStorage_ByteSwapString(leStr
, len
);
1965 hr
= IStream_Write(stm
, leStr
, len
, written
);
1966 HeapFree(GetProcessHeap(), 0, leStr
);
1969 return IStream_Write(stm
, str
, len
* sizeof(WCHAR
), written
);
1973 struct DictionaryClosure
1979 static BOOL
PropertyStorage_DictionaryWriter(const void *key
,
1980 const void *value
, void *extra
, void *closure
)
1982 PropertyStorage_impl
*This
= extra
;
1983 struct DictionaryClosure
*c
= closure
;
1984 DWORD propid
, keyLen
;
1989 StorageUtl_WriteDWord(&propid
, 0, PtrToUlong(value
));
1990 c
->hr
= IStream_Write(This
->stm
, &propid
, sizeof(propid
), &count
);
1993 c
->bytesWritten
+= sizeof(DWORD
);
1994 if (This
->codePage
== CP_UNICODE
)
1996 DWORD pad
= 0, pad_len
;
1998 StorageUtl_WriteDWord(&keyLen
, 0, lstrlenW((LPCWSTR
)key
) + 1);
1999 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
2002 c
->bytesWritten
+= sizeof(DWORD
);
2003 c
->hr
= PropertyStorage_WriteWStringToStream(This
->stm
, key
, keyLen
,
2007 keyLen
*= sizeof(WCHAR
);
2008 c
->bytesWritten
+= keyLen
;
2010 /* Align to 4 bytes. */
2011 pad_len
= sizeof(DWORD
) - keyLen
% sizeof(DWORD
);
2014 c
->hr
= IStream_Write(This
->stm
, &pad
, pad_len
, &count
);
2017 c
->bytesWritten
+= pad_len
;
2022 StorageUtl_WriteDWord(&keyLen
, 0, strlen((LPCSTR
)key
) + 1);
2023 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
2026 c
->bytesWritten
+= sizeof(DWORD
);
2027 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
2030 c
->bytesWritten
+= keyLen
;
2033 return SUCCEEDED(c
->hr
);
2036 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
2038 /* Writes the dictionary to the stream. Assumes without checking that the
2039 * dictionary isn't empty.
2041 static HRESULT
PropertyStorage_WriteDictionaryToStream(
2042 PropertyStorage_impl
*This
, DWORD
*sectionOffset
)
2046 PROPERTYIDOFFSET propIdOffset
;
2049 struct DictionaryClosure closure
;
2051 assert(sectionOffset
);
2053 /* The dictionary's always the first property written, so seek to its
2056 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
);
2057 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2060 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY
, *sectionOffset
,
2062 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
2066 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
2067 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2070 StorageUtl_WriteDWord(&dwTemp
, 0, dictionary_num_entries(This
->name_to_propid
));
2071 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2074 *sectionOffset
+= sizeof(dwTemp
);
2077 closure
.bytesWritten
= 0;
2078 dictionary_enumerate(This
->name_to_propid
, PropertyStorage_DictionaryWriter
,
2083 *sectionOffset
+= closure
.bytesWritten
;
2084 if (closure
.bytesWritten
% sizeof(DWORD
))
2086 DWORD padding
= sizeof(DWORD
) - closure
.bytesWritten
% sizeof(DWORD
);
2087 TRACE("adding %ld bytes of padding\n", padding
);
2088 *sectionOffset
+= padding
;
2095 static HRESULT
PropertyStorage_WritePropertyToStream(PropertyStorage_impl
*This
,
2096 DWORD propNum
, DWORD propid
, const PROPVARIANT
*var
, DWORD
*sectionOffset
)
2098 DWORD len
, dwType
, dwTemp
, bytesWritten
;
2101 PROPERTYIDOFFSET propIdOffset
;
2102 ULARGE_INTEGER ularge
;
2106 assert(sectionOffset
);
2108 TRACE("%p, %ld, %#lx, %d, %ld.\n", This
, propNum
, propid
, var
->vt
,
2111 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
) +
2112 propNum
* sizeof(PROPERTYIDOFFSET
);
2113 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2116 PropertyStorage_MakePropertyIdOffset(propid
, *sectionOffset
, &propIdOffset
);
2117 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
2121 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
2122 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2125 StorageUtl_WriteDWord(&dwType
, 0, var
->vt
);
2126 hr
= IStream_Write(This
->stm
, &dwType
, sizeof(dwType
), &count
);
2129 *sectionOffset
+= sizeof(dwType
);
2139 hr
= IStream_Write(This
->stm
, &var
->cVal
, sizeof(var
->cVal
),
2141 bytesWritten
= count
;
2148 StorageUtl_WriteWord(&wTemp
, 0, var
->iVal
);
2149 hr
= IStream_Write(This
->stm
, &wTemp
, sizeof(wTemp
), &count
);
2150 bytesWritten
= count
;
2156 StorageUtl_WriteDWord(&dwTemp
, 0, var
->lVal
);
2157 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2158 bytesWritten
= count
;
2164 StorageUtl_WriteULargeInteger(&ularge
, 0, &var
->uhVal
);
2165 hr
= IStream_Write(This
->stm
, &ularge
, sizeof(ularge
), &bytesWritten
);
2170 if (This
->codePage
== CP_UNICODE
)
2171 len
= (lstrlenW(var
->pwszVal
) + 1) * sizeof(WCHAR
);
2173 len
= lstrlenA(var
->pszVal
) + 1;
2174 StorageUtl_WriteDWord(&dwTemp
, 0, len
);
2175 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2178 hr
= IStream_Write(This
->stm
, var
->pszVal
, len
, &count
);
2179 bytesWritten
= count
+ sizeof(DWORD
);
2184 if (This
->codePage
== CP_UNICODE
)
2186 len
= SysStringByteLen(var
->bstrVal
) + sizeof(WCHAR
);
2187 StorageUtl_WriteDWord(&dwTemp
, 0, len
);
2188 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2190 hr
= IStream_Write(This
->stm
, var
->bstrVal
, len
, &count
);
2196 len
= WideCharToMultiByte(This
->codePage
, 0, var
->bstrVal
, SysStringLen(var
->bstrVal
) + 1,
2197 NULL
, 0, NULL
, NULL
);
2199 str
= heap_alloc(len
);
2206 WideCharToMultiByte(This
->codePage
, 0, var
->bstrVal
, SysStringLen(var
->bstrVal
),
2207 str
, len
, NULL
, NULL
);
2208 StorageUtl_WriteDWord(&dwTemp
, 0, len
);
2209 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2211 hr
= IStream_Write(This
->stm
, str
, len
, &count
);
2215 bytesWritten
= count
+ sizeof(DWORD
);
2220 len
= lstrlenW(var
->pwszVal
) + 1;
2222 StorageUtl_WriteDWord(&dwTemp
, 0, len
);
2223 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2226 hr
= IStream_Write(This
->stm
, var
->pwszVal
, len
* sizeof(WCHAR
),
2228 bytesWritten
= count
+ sizeof(DWORD
);
2235 StorageUtl_WriteULargeInteger(&temp
, 0, (const ULARGE_INTEGER
*)&var
->filetime
);
2236 hr
= IStream_Write(This
->stm
, &temp
, sizeof(temp
), &count
);
2237 bytesWritten
= count
;
2244 len
= var
->pclipdata
->cbSize
;
2245 StorageUtl_WriteDWord(&cf_hdr
[0], 0, len
+ 8);
2246 StorageUtl_WriteDWord(&cf_hdr
[1], 0, var
->pclipdata
->ulClipFmt
);
2247 hr
= IStream_Write(This
->stm
, cf_hdr
, sizeof(cf_hdr
), &count
);
2250 hr
= IStream_Write(This
->stm
, var
->pclipdata
->pClipData
,
2251 len
- sizeof(var
->pclipdata
->ulClipFmt
), &count
);
2254 bytesWritten
= count
+ sizeof cf_hdr
;
2261 StorageUtl_WriteGUID(&temp
, 0, var
->puuid
);
2262 hr
= IStream_Write(This
->stm
, &temp
, sizeof(temp
), &count
);
2263 bytesWritten
= count
;
2268 StorageUtl_WriteDWord(&dwTemp
, 0, var
->blob
.cbSize
);
2269 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2272 hr
= IStream_Write(This
->stm
, var
->blob
.pBlobData
, var
->blob
.cbSize
, &count
);
2273 bytesWritten
= count
+ sizeof(DWORD
);
2277 FIXME("unsupported type: %d\n", var
->vt
);
2278 return STG_E_INVALIDPARAMETER
;
2283 *sectionOffset
+= bytesWritten
;
2284 if (bytesWritten
% sizeof(DWORD
))
2286 DWORD padding
= sizeof(DWORD
) - bytesWritten
% sizeof(DWORD
);
2287 TRACE("adding %ld bytes of padding\n", padding
);
2288 *sectionOffset
+= padding
;
2296 struct PropertyClosure
2300 DWORD
*sectionOffset
;
2303 static BOOL
PropertyStorage_PropertiesWriter(const void *key
, const void *value
,
2304 void *extra
, void *closure
)
2306 PropertyStorage_impl
*This
= extra
;
2307 struct PropertyClosure
*c
= closure
;
2313 c
->hr
= PropertyStorage_WritePropertyToStream(This
, c
->propNum
++,
2314 PtrToUlong(key
), value
, c
->sectionOffset
);
2315 return SUCCEEDED(c
->hr
);
2318 static HRESULT
PropertyStorage_WritePropertiesToStream(
2319 PropertyStorage_impl
*This
, DWORD startingPropNum
, DWORD
*sectionOffset
)
2321 struct PropertyClosure closure
;
2323 assert(sectionOffset
);
2325 closure
.propNum
= startingPropNum
;
2326 closure
.sectionOffset
= sectionOffset
;
2327 dictionary_enumerate(This
->propid_to_prop
, PropertyStorage_PropertiesWriter
,
2332 static HRESULT
PropertyStorage_WriteHeadersToStream(PropertyStorage_impl
*This
)
2336 LARGE_INTEGER seek
= { {0} };
2337 PROPERTYSETHEADER hdr
;
2338 FORMATIDOFFSET fmtOffset
;
2340 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2343 PropertyStorage_MakeHeader(This
, &hdr
);
2344 hr
= IStream_Write(This
->stm
, &hdr
, sizeof(hdr
), &count
);
2347 if (count
!= sizeof(hdr
))
2349 hr
= STG_E_WRITEFAULT
;
2353 PropertyStorage_MakeFmtIdOffset(This
, &fmtOffset
);
2354 hr
= IStream_Write(This
->stm
, &fmtOffset
, sizeof(fmtOffset
), &count
);
2357 if (count
!= sizeof(fmtOffset
))
2359 hr
= STG_E_WRITEFAULT
;
2368 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*This
)
2370 PROPERTYSECTIONHEADER sectionHdr
;
2374 DWORD numProps
, prop
, sectionOffset
, dwTemp
;
2377 PropertyStorage_WriteHeadersToStream(This
);
2379 /* Count properties. Always at least one property, the code page */
2381 if (dictionary_num_entries(This
->name_to_propid
))
2383 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
2385 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2387 numProps
+= dictionary_num_entries(This
->propid_to_prop
);
2389 /* Write section header with 0 bytes right now, I'll adjust it after
2390 * writing properties.
2392 PropertyStorage_MakeSectionHdr(0, numProps
, §ionHdr
);
2393 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
2394 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2397 hr
= IStream_Write(This
->stm
, §ionHdr
, sizeof(sectionHdr
), &count
);
2402 sectionOffset
= sizeof(PROPERTYSECTIONHEADER
) +
2403 numProps
* sizeof(PROPERTYIDOFFSET
);
2405 if (dictionary_num_entries(This
->name_to_propid
))
2408 hr
= PropertyStorage_WriteDictionaryToStream(This
, §ionOffset
);
2413 PropVariantInit(&var
);
2416 var
.iVal
= This
->codePage
;
2417 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_CODEPAGE
,
2418 &var
, §ionOffset
);
2422 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
2425 var
.lVal
= This
->locale
;
2426 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_LOCALE
,
2427 &var
, §ionOffset
);
2432 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2436 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_BEHAVIOR
,
2437 &var
, §ionOffset
);
2442 hr
= PropertyStorage_WritePropertiesToStream(This
, prop
, §ionOffset
);
2446 /* Now write the byte count of the section */
2447 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
2448 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2451 StorageUtl_WriteDWord(&dwTemp
, 0, sectionOffset
);
2452 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2458 /***********************************************************************
2459 * PropertyStorage_Construct
2461 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*This
)
2463 dictionary_destroy(This
->name_to_propid
);
2464 This
->name_to_propid
= NULL
;
2465 dictionary_destroy(This
->propid_to_name
);
2466 This
->propid_to_name
= NULL
;
2467 dictionary_destroy(This
->propid_to_prop
);
2468 This
->propid_to_prop
= NULL
;
2471 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*This
)
2475 This
->name_to_propid
= dictionary_create(
2476 PropertyStorage_PropNameCompare
, PropertyStorage_PropNameDestroy
,
2478 if (!This
->name_to_propid
)
2480 hr
= STG_E_INSUFFICIENTMEMORY
;
2483 This
->propid_to_name
= dictionary_create(PropertyStorage_PropCompare
,
2485 if (!This
->propid_to_name
)
2487 hr
= STG_E_INSUFFICIENTMEMORY
;
2490 This
->propid_to_prop
= dictionary_create(PropertyStorage_PropCompare
,
2491 PropertyStorage_PropertyDestroy
, This
);
2492 if (!This
->propid_to_prop
)
2494 hr
= STG_E_INSUFFICIENTMEMORY
;
2499 PropertyStorage_DestroyDictionaries(This
);
2503 static HRESULT
PropertyStorage_BaseConstruct(IStream
*stm
,
2504 REFFMTID rfmtid
, DWORD grfMode
, PropertyStorage_impl
**pps
)
2510 *pps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof **pps
);
2512 return E_OUTOFMEMORY
;
2514 (*pps
)->IPropertyStorage_iface
.lpVtbl
= &IPropertyStorage_Vtbl
;
2516 InitializeCriticalSection(&(*pps
)->cs
);
2517 (*pps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PropertyStorage_impl.cs");
2519 (*pps
)->fmtid
= *rfmtid
;
2520 (*pps
)->grfMode
= grfMode
;
2522 hr
= PropertyStorage_CreateDictionaries(*pps
);
2525 (*pps
)->cs
.DebugInfo
->Spare
[0] = 0;
2526 DeleteCriticalSection(&(*pps
)->cs
);
2527 HeapFree(GetProcessHeap(), 0, *pps
);
2530 else IStream_AddRef( stm
);
2535 static HRESULT
PropertyStorage_ConstructFromStream(IStream
*stm
,
2536 REFFMTID rfmtid
, DWORD grfMode
, IPropertyStorage
** pps
)
2538 PropertyStorage_impl
*ps
;
2542 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2545 hr
= PropertyStorage_ReadFromStream(ps
);
2548 *pps
= &ps
->IPropertyStorage_iface
;
2549 TRACE("PropertyStorage %p constructed\n", ps
);
2552 else IPropertyStorage_Release( &ps
->IPropertyStorage_iface
);
2557 static HRESULT
PropertyStorage_ConstructEmpty(IStream
*stm
,
2558 REFFMTID rfmtid
, DWORD grfFlags
, DWORD grfMode
, IPropertyStorage
** pps
)
2560 PropertyStorage_impl
*ps
;
2564 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2568 ps
->grfFlags
= grfFlags
;
2569 if (ps
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2571 /* default to Unicode unless told not to, as specified on msdn */
2572 if (ps
->grfFlags
& PROPSETFLAG_ANSI
)
2573 ps
->codePage
= GetACP();
2575 ps
->codePage
= CP_UNICODE
;
2576 ps
->locale
= LOCALE_SYSTEM_DEFAULT
;
2577 TRACE("Code page is %d, locale is %ld\n", ps
->codePage
, ps
->locale
);
2578 *pps
= &ps
->IPropertyStorage_iface
;
2579 TRACE("PropertyStorage %p constructed\n", ps
);
2586 /***********************************************************************
2587 * Implementation of IPropertySetStorage
2590 struct enum_stat_propset_stg
2592 IEnumSTATPROPSETSTG IEnumSTATPROPSETSTG_iface
;
2594 STATPROPSETSTG
*stats
;
2599 static struct enum_stat_propset_stg
*impl_from_IEnumSTATPROPSETSTG(IEnumSTATPROPSETSTG
*iface
)
2601 return CONTAINING_RECORD(iface
, struct enum_stat_propset_stg
, IEnumSTATPROPSETSTG_iface
);
2604 static HRESULT WINAPI
enum_stat_propset_stg_QueryInterface(IEnumSTATPROPSETSTG
*iface
, REFIID riid
, void **obj
)
2606 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), obj
);
2608 if (IsEqualIID(riid
, &IID_IEnumSTATPROPSETSTG
) ||
2609 IsEqualIID(riid
, &IID_IUnknown
))
2612 IEnumSTATPROPSETSTG_AddRef(iface
);
2616 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
2617 return E_NOINTERFACE
;
2620 static ULONG WINAPI
enum_stat_propset_stg_AddRef(IEnumSTATPROPSETSTG
*iface
)
2622 struct enum_stat_propset_stg
*psenum
= impl_from_IEnumSTATPROPSETSTG(iface
);
2623 LONG refcount
= InterlockedIncrement(&psenum
->refcount
);
2625 TRACE("%p, refcount %lu.\n", iface
, refcount
);
2630 static ULONG WINAPI
enum_stat_propset_stg_Release(IEnumSTATPROPSETSTG
*iface
)
2632 struct enum_stat_propset_stg
*psenum
= impl_from_IEnumSTATPROPSETSTG(iface
);
2633 LONG refcount
= InterlockedDecrement(&psenum
->refcount
);
2635 TRACE("%p, refcount %lu.\n", iface
, refcount
);
2639 heap_free(psenum
->stats
);
2646 static HRESULT WINAPI
enum_stat_propset_stg_Next(IEnumSTATPROPSETSTG
*iface
, ULONG celt
,
2647 STATPROPSETSTG
*ret
, ULONG
*fetched
)
2649 struct enum_stat_propset_stg
*psenum
= impl_from_IEnumSTATPROPSETSTG(iface
);
2652 TRACE("%p, %lu, %p, %p.\n", iface
, celt
, ret
, fetched
);
2654 if (psenum
->current
== ~0u)
2655 psenum
->current
= 0;
2657 while (count
< celt
&& psenum
->current
< psenum
->count
)
2658 ret
[count
++] = psenum
->stats
[psenum
->current
++];
2663 return count
< celt
? S_FALSE
: S_OK
;
2666 static HRESULT WINAPI
enum_stat_propset_stg_Skip(IEnumSTATPROPSETSTG
*iface
, ULONG celt
)
2668 FIXME("%p, %lu.\n", iface
, celt
);
2673 static HRESULT WINAPI
enum_stat_propset_stg_Reset(IEnumSTATPROPSETSTG
*iface
)
2675 struct enum_stat_propset_stg
*psenum
= impl_from_IEnumSTATPROPSETSTG(iface
);
2677 TRACE("%p.\n", iface
);
2679 psenum
->current
= ~0u;
2684 static HRESULT WINAPI
enum_stat_propset_stg_Clone(IEnumSTATPROPSETSTG
*iface
, IEnumSTATPROPSETSTG
**ppenum
)
2686 FIXME("%p, %p.\n", iface
, ppenum
);
2691 static const IEnumSTATPROPSETSTGVtbl enum_stat_propset_stg_vtbl
=
2693 enum_stat_propset_stg_QueryInterface
,
2694 enum_stat_propset_stg_AddRef
,
2695 enum_stat_propset_stg_Release
,
2696 enum_stat_propset_stg_Next
,
2697 enum_stat_propset_stg_Skip
,
2698 enum_stat_propset_stg_Reset
,
2699 enum_stat_propset_stg_Clone
,
2702 static HRESULT
create_enum_stat_propset_stg(StorageImpl
*storage
, IEnumSTATPROPSETSTG
**ret
)
2704 IStorage
*stg
= &storage
->base
.IStorage_iface
;
2705 IEnumSTATSTG
*penum
= NULL
;
2710 struct enum_stat_propset_stg
*enum_obj
;
2712 enum_obj
= heap_alloc_zero(sizeof(*enum_obj
));
2714 return E_OUTOFMEMORY
;
2716 enum_obj
->IEnumSTATPROPSETSTG_iface
.lpVtbl
= &enum_stat_propset_stg_vtbl
;
2717 enum_obj
->refcount
= 1;
2719 /* add all the property set elements into a list */
2720 hr
= IStorage_EnumElements(stg
, 0, NULL
, 0, &penum
);
2724 /* Allocate stats array and fill it. */
2725 while ((hr
= IEnumSTATSTG_Next(penum
, 1, &stat
, &count
)) == S_OK
)
2728 CoTaskMemFree(stat
.pwcsName
);
2734 enum_obj
->stats
= heap_alloc(enum_obj
->count
* sizeof(*enum_obj
->stats
));
2735 if (!enum_obj
->stats
)
2740 enum_obj
->count
= 0;
2742 if (FAILED(hr
= IEnumSTATSTG_Reset(penum
)))
2745 while (IEnumSTATSTG_Next(penum
, 1, &stat
, &count
) == S_OK
)
2750 if (stat
.pwcsName
[0] == 5 && stat
.type
== STGTY_STREAM
)
2752 STATPROPSETSTG
*ptr
= &enum_obj
->stats
[enum_obj
->count
++];
2754 PropStgNameToFmtId(stat
.pwcsName
, &ptr
->fmtid
);
2756 TRACE("adding %s - %s.\n", debugstr_w(stat
.pwcsName
), debugstr_guid(&ptr
->fmtid
));
2758 ptr
->mtime
= stat
.mtime
;
2759 ptr
->atime
= stat
.atime
;
2760 ptr
->ctime
= stat
.ctime
;
2761 ptr
->grfFlags
= stat
.grfMode
;
2762 ptr
->clsid
= stat
.clsid
;
2764 CoTaskMemFree(stat
.pwcsName
);
2770 IEnumSTATSTG_Release(penum
);
2774 *ret
= &enum_obj
->IEnumSTATPROPSETSTG_iface
;
2779 IEnumSTATPROPSETSTG_Release(&enum_obj
->IEnumSTATPROPSETSTG_iface
);
2785 /************************************************************************
2786 * IPropertySetStorage_fnQueryInterface (IUnknown)
2788 * This method forwards to the common QueryInterface implementation
2790 static HRESULT WINAPI
IPropertySetStorage_fnQueryInterface(
2791 IPropertySetStorage
*ppstg
,
2795 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2796 return IStorage_QueryInterface( &This
->base
.IStorage_iface
, riid
, ppvObject
);
2799 /************************************************************************
2800 * IPropertySetStorage_fnAddRef (IUnknown)
2802 * This method forwards to the common AddRef implementation
2804 static ULONG WINAPI
IPropertySetStorage_fnAddRef(
2805 IPropertySetStorage
*ppstg
)
2807 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2808 return IStorage_AddRef( &This
->base
.IStorage_iface
);
2811 /************************************************************************
2812 * IPropertySetStorage_fnRelease (IUnknown)
2814 * This method forwards to the common Release implementation
2816 static ULONG WINAPI
IPropertySetStorage_fnRelease(
2817 IPropertySetStorage
*ppstg
)
2819 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2820 return IStorage_Release( &This
->base
.IStorage_iface
);
2823 /************************************************************************
2824 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2826 static HRESULT WINAPI
IPropertySetStorage_fnCreate(
2827 IPropertySetStorage
*ppstg
,
2829 const CLSID
* pclsid
,
2832 IPropertyStorage
** ppprstg
)
2834 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2835 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2836 IStream
*stm
= NULL
;
2839 TRACE("%p, %s %#lx, %#lx, %p.\n", This
, debugstr_guid(rfmtid
), grfFlags
,
2843 if (grfMode
!= (STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
))
2845 r
= STG_E_INVALIDFLAG
;
2855 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2856 * storage, not a stream. For now, disallow it.
2858 if (grfFlags
& PROPSETFLAG_NONSIMPLE
)
2860 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2861 r
= STG_E_INVALIDFLAG
;
2865 r
= FmtIdToPropStgName(rfmtid
, name
);
2869 r
= IStorage_CreateStream( &This
->base
.IStorage_iface
, name
, grfMode
, 0, 0, &stm
);
2873 r
= PropertyStorage_ConstructEmpty(stm
, rfmtid
, grfFlags
, grfMode
, ppprstg
);
2875 IStream_Release( stm
);
2878 TRACE("returning %#lx\n", r
);
2882 /************************************************************************
2883 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2885 static HRESULT WINAPI
IPropertySetStorage_fnOpen(
2886 IPropertySetStorage
*ppstg
,
2889 IPropertyStorage
** ppprstg
)
2891 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2892 IStream
*stm
= NULL
;
2893 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2896 TRACE("%p, %s, %#lx, %p.\n", This
, debugstr_guid(rfmtid
), grfMode
, ppprstg
);
2899 if (grfMode
!= (STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
) &&
2900 grfMode
!= (STGM_READ
|STGM_SHARE_EXCLUSIVE
))
2902 r
= STG_E_INVALIDFLAG
;
2912 r
= FmtIdToPropStgName(rfmtid
, name
);
2916 r
= IStorage_OpenStream( &This
->base
.IStorage_iface
, name
, 0, grfMode
, 0, &stm
);
2920 r
= PropertyStorage_ConstructFromStream(stm
, rfmtid
, grfMode
, ppprstg
);
2922 IStream_Release( stm
);
2925 TRACE("returning %#lx\n", r
);
2929 /************************************************************************
2930 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2932 static HRESULT WINAPI
IPropertySetStorage_fnDelete(
2933 IPropertySetStorage
*ppstg
,
2936 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2937 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2940 TRACE("%p %s\n", This
, debugstr_guid(rfmtid
));
2943 return E_INVALIDARG
;
2945 r
= FmtIdToPropStgName(rfmtid
, name
);
2949 return IStorage_DestroyElement(&This
->base
.IStorage_iface
, name
);
2952 static HRESULT WINAPI
IPropertySetStorage_fnEnum(IPropertySetStorage
*iface
, IEnumSTATPROPSETSTG
**enum_obj
)
2954 StorageImpl
*storage
= impl_from_IPropertySetStorage(iface
);
2956 TRACE("%p, %p.\n", iface
, enum_obj
);
2959 return E_INVALIDARG
;
2961 return create_enum_stat_propset_stg(storage
, enum_obj
);
2964 /***********************************************************************
2967 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
=
2969 IPropertySetStorage_fnQueryInterface
,
2970 IPropertySetStorage_fnAddRef
,
2971 IPropertySetStorage_fnRelease
,
2972 IPropertySetStorage_fnCreate
,
2973 IPropertySetStorage_fnOpen
,
2974 IPropertySetStorage_fnDelete
,
2975 IPropertySetStorage_fnEnum
2978 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
=
2980 IPropertyStorage_fnQueryInterface
,
2981 IPropertyStorage_fnAddRef
,
2982 IPropertyStorage_fnRelease
,
2983 IPropertyStorage_fnReadMultiple
,
2984 IPropertyStorage_fnWriteMultiple
,
2985 IPropertyStorage_fnDeleteMultiple
,
2986 IPropertyStorage_fnReadPropertyNames
,
2987 IPropertyStorage_fnWritePropertyNames
,
2988 IPropertyStorage_fnDeletePropertyNames
,
2989 IPropertyStorage_fnCommit
,
2990 IPropertyStorage_fnRevert
,
2991 IPropertyStorage_fnEnum
,
2992 IPropertyStorage_fnSetTimes
,
2993 IPropertyStorage_fnSetClass
,
2994 IPropertyStorage_fnStat
,
2997 /***********************************************************************
2998 * Format ID <-> name conversion
3000 static const WCHAR szSummaryInfo
[] = L
"\5SummaryInformation";
3001 static const WCHAR szDocSummaryInfo
[] = L
"\5DocumentSummaryInformation";
3003 #define BITS_PER_BYTE 8
3004 #define CHARMASK 0x1f
3005 #define BITS_IN_CHARMASK 5
3006 #define NUM_ALPHA_CHARS 26
3008 /***********************************************************************
3009 * FmtIdToPropStgName [ole32.@]
3010 * Returns the storage name of the format ID rfmtid.
3012 * rfmtid [I] Format ID for which to return a storage name
3013 * str [O] Storage name associated with rfmtid.
3016 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
3019 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
3021 HRESULT WINAPI
FmtIdToPropStgName(const FMTID
*rfmtid
, LPOLESTR str
)
3023 static const char fmtMap
[] = "abcdefghijklmnopqrstuvwxyz012345";
3025 TRACE("%s, %p\n", debugstr_guid(rfmtid
), str
);
3027 if (!rfmtid
) return E_INVALIDARG
;
3028 if (!str
) return E_INVALIDARG
;
3030 if (IsEqualGUID(&FMTID_SummaryInformation
, rfmtid
))
3031 lstrcpyW(str
, szSummaryInfo
);
3032 else if (IsEqualGUID(&FMTID_DocSummaryInformation
, rfmtid
))
3033 lstrcpyW(str
, szDocSummaryInfo
);
3034 else if (IsEqualGUID(&FMTID_UserDefinedProperties
, rfmtid
))
3035 lstrcpyW(str
, szDocSummaryInfo
);
3040 ULONG bitsRemaining
= BITS_PER_BYTE
;
3043 for (fmtptr
= (const BYTE
*)rfmtid
; fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
); )
3045 ULONG i
= *fmtptr
>> (BITS_PER_BYTE
- bitsRemaining
);
3047 if (bitsRemaining
>= BITS_IN_CHARMASK
)
3049 *pstr
= (WCHAR
)(fmtMap
[i
& CHARMASK
]);
3050 if (bitsRemaining
== BITS_PER_BYTE
&& *pstr
>= 'a' &&
3054 bitsRemaining
-= BITS_IN_CHARMASK
;
3055 if (bitsRemaining
== 0)
3058 bitsRemaining
= BITS_PER_BYTE
;
3063 if (++fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
))
3064 i
|= *fmtptr
<< bitsRemaining
;
3065 *pstr
++ = (WCHAR
)(fmtMap
[i
& CHARMASK
]);
3066 bitsRemaining
+= BITS_PER_BYTE
- BITS_IN_CHARMASK
;
3071 TRACE("returning %s\n", debugstr_w(str
));
3075 /***********************************************************************
3076 * PropStgNameToFmtId [ole32.@]
3077 * Returns the format ID corresponding to the given name.
3079 * str [I] Storage name to convert to a format ID.
3080 * rfmtid [O] Format ID corresponding to str.
3083 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
3084 * a format ID, S_OK otherwise.
3086 HRESULT WINAPI
PropStgNameToFmtId(const LPOLESTR str
, FMTID
*rfmtid
)
3088 HRESULT hr
= STG_E_INVALIDNAME
;
3090 TRACE("%s, %p\n", debugstr_w(str
), rfmtid
);
3092 if (!rfmtid
) return E_INVALIDARG
;
3093 if (!str
) return STG_E_INVALIDNAME
;
3095 if (!lstrcmpiW(str
, szDocSummaryInfo
))
3097 *rfmtid
= FMTID_DocSummaryInformation
;
3100 else if (!lstrcmpiW(str
, szSummaryInfo
))
3102 *rfmtid
= FMTID_SummaryInformation
;
3108 BYTE
*fmtptr
= (BYTE
*)rfmtid
- 1;
3109 const WCHAR
*pstr
= str
;
3111 memset(rfmtid
, 0, sizeof(*rfmtid
));
3112 for (bits
= 0; bits
< sizeof(FMTID
) * BITS_PER_BYTE
;
3113 bits
+= BITS_IN_CHARMASK
)
3115 ULONG bitsUsed
= bits
% BITS_PER_BYTE
, bitsStored
;
3121 if (wc
> NUM_ALPHA_CHARS
)
3124 if (wc
> NUM_ALPHA_CHARS
)
3126 wc
+= 'a' - '0' + NUM_ALPHA_CHARS
;
3129 WARN("invalid character (%d)\n", *pstr
);
3134 *fmtptr
|= wc
<< bitsUsed
;
3135 bitsStored
= min(BITS_PER_BYTE
- bitsUsed
, BITS_IN_CHARMASK
);
3136 if (bitsStored
< BITS_IN_CHARMASK
)
3138 wc
>>= BITS_PER_BYTE
- bitsUsed
;
3139 if (bits
+ bitsStored
== sizeof(FMTID
) * BITS_PER_BYTE
)
3143 WARN("extra bits\n");
3149 *fmtptr
|= (BYTE
)wc
;
3158 #ifdef __i386__ /* thiscall functions are i386-specific */
3160 #define DEFINE_STDCALL_WRAPPER(num,func,args) \
3161 __ASM_STDCALL_FUNC(func, args, \
3165 "movl (%ecx), %eax\n\t" \
3166 "jmp *(4*(" #num "))(%eax)" )
3168 DEFINE_STDCALL_WRAPPER(0,Allocate_PMemoryAllocator
,8)
3169 extern void* WINAPI
Allocate_PMemoryAllocator(void *this, ULONG cbSize
);
3173 static void* WINAPI
Allocate_PMemoryAllocator(void *this, ULONG cbSize
)
3175 void* (WINAPI
*fn
)(void*,ULONG
) = **(void***)this;
3176 return fn(this, cbSize
);
3181 BOOLEAN WINAPI
StgConvertPropertyToVariant(const SERIALIZEDPROPERTYVALUE
* prop
,
3182 USHORT CodePage
, PROPVARIANT
* pvar
, void* pma
)
3184 struct read_buffer read_buffer
;
3187 read_buffer
.data
= (BYTE
*)prop
;
3188 read_buffer
.size
= ~(size_t)0;
3189 hr
= PropertyStorage_ReadProperty(pvar
, &read_buffer
, 0, CodePage
, Allocate_PMemoryAllocator
, pma
);
3193 FIXME("should raise C++ exception on failure\n");
3194 PropVariantInit(pvar
);
3200 SERIALIZEDPROPERTYVALUE
* WINAPI
StgConvertVariantToProperty(const PROPVARIANT
*pvar
,
3201 USHORT CodePage
, SERIALIZEDPROPERTYVALUE
*pprop
, ULONG
*pcb
, PROPID pid
,
3202 BOOLEAN fReserved
, ULONG
*pcIndirect
)
3204 FIXME("%p, %d, %p, %p, %ld, %d, %p.\n", pvar
, CodePage
, pprop
, pcb
, pid
, fReserved
, pcIndirect
);
3209 HRESULT WINAPI
StgCreatePropStg(IUnknown
*unk
, REFFMTID fmt
, const CLSID
*clsid
,
3210 DWORD flags
, DWORD reserved
, IPropertyStorage
**prop_stg
)
3216 TRACE("%p, %s, %s, %#lx, %ld, %p.\n", unk
, debugstr_guid(fmt
), debugstr_guid(clsid
), flags
, reserved
, prop_stg
);
3218 if (!fmt
|| reserved
)
3224 if (flags
& PROPSETFLAG_NONSIMPLE
)
3226 r
= IUnknown_QueryInterface(unk
, &IID_IStorage
, (void **)&stg
);
3230 /* FIXME: if (flags & PROPSETFLAG_NONSIMPLE), we need to create a
3231 * storage, not a stream. For now, disallow it.
3233 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
3234 IStorage_Release(stg
);
3235 r
= STG_E_INVALIDFLAG
;
3239 r
= IUnknown_QueryInterface(unk
, &IID_IStream
, (void **)&stm
);
3243 r
= PropertyStorage_ConstructEmpty(stm
, fmt
, flags
,
3244 STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, prop_stg
);
3246 IStream_Release( stm
);
3250 TRACE("returning %#lx\n", r
);
3254 HRESULT WINAPI
StgOpenPropStg(IUnknown
*unk
, REFFMTID fmt
, DWORD flags
,
3255 DWORD reserved
, IPropertyStorage
**prop_stg
)
3261 TRACE("%p, %s, %#lx, %ld, %p.\n", unk
, debugstr_guid(fmt
), flags
, reserved
, prop_stg
);
3263 if (!fmt
|| reserved
)
3269 if (flags
& PROPSETFLAG_NONSIMPLE
)
3271 r
= IUnknown_QueryInterface(unk
, &IID_IStorage
, (void **)&stg
);
3275 /* FIXME: if (flags & PROPSETFLAG_NONSIMPLE), we need to open a
3276 * storage, not a stream. For now, disallow it.
3278 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
3279 IStorage_Release(stg
);
3280 r
= STG_E_INVALIDFLAG
;
3284 r
= IUnknown_QueryInterface(unk
, &IID_IStream
, (void **)&stm
);
3288 r
= PropertyStorage_ConstructFromStream(stm
, fmt
,
3289 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, prop_stg
);
3291 IStream_Release( stm
);
3295 TRACE("returning %#lx\n", r
);