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
40 #include "wine/port.h"
49 #define NONAMELESSUNION
55 #include "wine/unicode.h"
56 #include "wine/debug.h"
57 #include "dictionary.h"
58 #include "storage32.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
65 #define __ASM_STDCALL_FUNC(name,args,code)
68 static inline StorageImpl
*impl_from_IPropertySetStorage( IPropertySetStorage
*iface
)
70 return CONTAINING_RECORD(iface
, StorageImpl
, base
.IPropertySetStorage_iface
);
73 /* These are documented in MSDN,
74 * but they don't seem to be in any header file.
76 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
77 #define PROPSETHDR_OSVER_KIND_WIN16 0
78 #define PROPSETHDR_OSVER_KIND_MAC 1
79 #define PROPSETHDR_OSVER_KIND_WIN32 2
81 #define CP_UNICODE 1200
83 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
85 #define CFTAG_WINDOWS (-1L)
86 #define CFTAG_MACINTOSH (-2L)
87 #define CFTAG_FMTID (-3L)
88 #define CFTAG_NODATA 0L
90 typedef struct tagPROPERTYSETHEADER
92 WORD wByteOrder
; /* always 0xfffe */
93 WORD wFormat
; /* can be zero or one */
94 DWORD dwOSVer
; /* OS version of originating system */
95 CLSID clsid
; /* application CLSID */
96 DWORD reserved
; /* always 1 */
99 typedef struct tagFORMATIDOFFSET
102 DWORD dwOffset
; /* from beginning of stream */
105 typedef struct tagPROPERTYSECTIONHEADER
109 } PROPERTYSECTIONHEADER
;
111 typedef struct tagPROPERTYIDOFFSET
114 DWORD dwOffset
; /* from beginning of section */
117 typedef struct tagPropertyStorage_impl PropertyStorage_impl
;
119 /* Initializes the property storage from the stream (and undoes any uncommitted
120 * changes in the process.) Returns an error if there is an error reading or
121 * if the stream format doesn't match what's expected.
123 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*);
125 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*);
127 /* Creates the dictionaries used by the property storage. If successful, all
128 * the dictionaries have been created. If failed, none has been. (This makes
129 * it a bit easier to deal with destroying them.)
131 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*);
133 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*);
135 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
136 * string using PropertyStorage_StringCopy.
138 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
139 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
);
141 /* Copies the string src, which is encoded using code page srcCP, and returns
142 * it in *dst, in the code page specified by targetCP. The returned string is
143 * allocated using CoTaskMemAlloc.
144 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
145 * is CP_UNICODE, the returned string is in fact an LPWSTR.
146 * Returns S_OK on success, something else on failure.
148 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
151 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
;
152 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
;
153 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
;
154 static HRESULT
create_EnumSTATPROPSETSTG(StorageImpl
*, IEnumSTATPROPSETSTG
**);
155 static HRESULT
create_EnumSTATPROPSTG(PropertyStorage_impl
*, IEnumSTATPROPSTG
**);
157 /***********************************************************************
158 * Implementation of IPropertyStorage
160 struct tagPropertyStorage_impl
162 IPropertyStorage IPropertyStorage_iface
;
176 struct dictionary
*name_to_propid
;
177 struct dictionary
*propid_to_name
;
178 struct dictionary
*propid_to_prop
;
181 static inline PropertyStorage_impl
*impl_from_IPropertyStorage(IPropertyStorage
*iface
)
183 return CONTAINING_RECORD(iface
, PropertyStorage_impl
, IPropertyStorage_iface
);
186 /************************************************************************
187 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
189 static HRESULT WINAPI
IPropertyStorage_fnQueryInterface(
190 IPropertyStorage
*iface
,
194 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
196 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppvObject
);
203 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
204 IsEqualGUID(&IID_IPropertyStorage
, riid
))
206 IPropertyStorage_AddRef(iface
);
211 return E_NOINTERFACE
;
214 /************************************************************************
215 * IPropertyStorage_fnAddRef (IPropertyStorage)
217 static ULONG WINAPI
IPropertyStorage_fnAddRef(
218 IPropertyStorage
*iface
)
220 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
221 return InterlockedIncrement(&This
->ref
);
224 /************************************************************************
225 * IPropertyStorage_fnRelease (IPropertyStorage)
227 static ULONG WINAPI
IPropertyStorage_fnRelease(
228 IPropertyStorage
*iface
)
230 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
233 ref
= InterlockedDecrement(&This
->ref
);
236 TRACE("Destroying %p\n", This
);
238 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
239 IStream_Release(This
->stm
);
240 This
->cs
.DebugInfo
->Spare
[0] = 0;
241 DeleteCriticalSection(&This
->cs
);
242 PropertyStorage_DestroyDictionaries(This
);
243 HeapFree(GetProcessHeap(), 0, This
);
248 static PROPVARIANT
*PropertyStorage_FindProperty(PropertyStorage_impl
*This
,
251 PROPVARIANT
*ret
= NULL
;
253 dictionary_find(This
->propid_to_prop
, UlongToPtr(propid
), (void **)&ret
);
254 TRACE("returning %p\n", ret
);
258 /* Returns NULL if name is NULL. */
259 static PROPVARIANT
*PropertyStorage_FindPropertyByName(
260 PropertyStorage_impl
*This
, LPCWSTR name
)
262 PROPVARIANT
*ret
= NULL
;
267 if (This
->codePage
== CP_UNICODE
)
269 if (dictionary_find(This
->name_to_propid
, name
, &propid
))
270 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
275 HRESULT hr
= PropertyStorage_StringCopy((LPCSTR
)name
, CP_UNICODE
,
276 &ansiName
, This
->codePage
);
280 if (dictionary_find(This
->name_to_propid
, ansiName
, &propid
))
281 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
282 CoTaskMemFree(ansiName
);
285 TRACE("returning %p\n", ret
);
289 static LPWSTR
PropertyStorage_FindPropertyNameById(PropertyStorage_impl
*This
,
294 dictionary_find(This
->propid_to_name
, UlongToPtr(propid
), (void **)&ret
);
295 TRACE("returning %p\n", ret
);
299 /************************************************************************
300 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
302 static HRESULT WINAPI
IPropertyStorage_fnReadMultiple(
303 IPropertyStorage
* iface
,
305 const PROPSPEC rgpspec
[],
306 PROPVARIANT rgpropvar
[])
308 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
312 TRACE("(%p, %d, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
316 if (!rgpspec
|| !rgpropvar
)
318 EnterCriticalSection(&This
->cs
);
319 for (i
= 0; i
< cpspec
; i
++)
321 PropVariantInit(&rgpropvar
[i
]);
322 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
324 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
325 rgpspec
[i
].u
.lpwstr
);
328 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
, GetACP(),
333 switch (rgpspec
[i
].u
.propid
)
336 rgpropvar
[i
].vt
= VT_I2
;
337 rgpropvar
[i
].u
.iVal
= This
->codePage
;
340 rgpropvar
[i
].vt
= VT_I4
;
341 rgpropvar
[i
].u
.lVal
= This
->locale
;
345 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
,
346 rgpspec
[i
].u
.propid
);
349 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
,
350 GetACP(), This
->codePage
);
357 LeaveCriticalSection(&This
->cs
);
361 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
367 TRACE("%s, %p, %d, %d\n",
368 srcCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)src
) : debugstr_a(src
), dst
,
377 if (dstCP
== CP_UNICODE
)
378 len
= (strlenW((LPCWSTR
)src
) + 1) * sizeof(WCHAR
);
380 len
= strlen(src
) + 1;
381 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
383 hr
= STG_E_INSUFFICIENTMEMORY
;
385 memcpy(*dst
, src
, len
);
389 if (dstCP
== CP_UNICODE
)
391 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
392 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
394 hr
= STG_E_INSUFFICIENTMEMORY
;
396 MultiByteToWideChar(srcCP
, 0, src
, -1, (LPWSTR
)*dst
, len
);
400 LPCWSTR wideStr
= NULL
;
401 LPWSTR wideStr_tmp
= NULL
;
403 if (srcCP
== CP_UNICODE
)
404 wideStr
= (LPCWSTR
)src
;
407 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
408 wideStr_tmp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
411 MultiByteToWideChar(srcCP
, 0, src
, -1, wideStr_tmp
, len
);
412 wideStr
= wideStr_tmp
;
415 hr
= STG_E_INSUFFICIENTMEMORY
;
419 len
= WideCharToMultiByte(dstCP
, 0, wideStr
, -1, NULL
, 0,
421 *dst
= CoTaskMemAlloc(len
);
423 hr
= STG_E_INSUFFICIENTMEMORY
;
426 BOOL defCharUsed
= FALSE
;
428 if (WideCharToMultiByte(dstCP
, 0, wideStr
, -1, *dst
, len
,
429 NULL
, &defCharUsed
) == 0 || defCharUsed
)
433 hr
= HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION
);
437 HeapFree(GetProcessHeap(), 0, wideStr_tmp
);
440 TRACE("returning 0x%08x (%s)\n", hr
,
441 dstCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)*dst
) : debugstr_a(*dst
));
445 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
446 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
)
452 if (propvar
->vt
== VT_LPSTR
)
454 hr
= PropertyStorage_StringCopy(propvar
->u
.pszVal
, srcCP
,
455 &prop
->u
.pszVal
, targetCP
);
460 PropVariantCopy(prop
, propvar
);
464 /* Stores the property with id propid and value propvar into this property
465 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
466 * type is VT_LPSTR, converts the string using lcid as the source code page
467 * and This->codePage as the target code page before storing.
468 * As a side effect, may change This->format to 1 if the type of propvar is
469 * a version 1-only property.
471 static HRESULT
PropertyStorage_StorePropWithId(PropertyStorage_impl
*This
,
472 PROPID propid
, const PROPVARIANT
*propvar
, LCID lcid
)
475 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
, propid
);
478 if (propvar
->vt
& VT_BYREF
|| propvar
->vt
& VT_ARRAY
)
486 case VT_VECTOR
|VT_I1
:
489 TRACE("Setting 0x%08x to type %d\n", propid
, propvar
->vt
);
492 PropVariantClear(prop
);
493 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
498 prop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
499 sizeof(PROPVARIANT
));
502 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
506 dictionary_insert(This
->propid_to_prop
, UlongToPtr(propid
), prop
);
507 if (propid
> This
->highestProp
)
508 This
->highestProp
= propid
;
511 HeapFree(GetProcessHeap(), 0, prop
);
514 hr
= STG_E_INSUFFICIENTMEMORY
;
519 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
520 * srcName is encoded in code page cp, and is converted to This->codePage.
521 * If cp is CP_UNICODE, srcName is actually a unicode string.
522 * As a side effect, may change This->format to 1 if srcName is too long for
523 * a version 0 property storage.
524 * Doesn't validate id.
526 static HRESULT
PropertyStorage_StoreNameWithId(PropertyStorage_impl
*This
,
527 LPCSTR srcName
, LCID cp
, PROPID id
)
534 hr
= PropertyStorage_StringCopy(srcName
, cp
, &name
, This
->codePage
);
537 if (This
->codePage
== CP_UNICODE
)
539 if (lstrlenW((LPWSTR
)name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
544 if (strlen(name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
547 TRACE("Adding prop name %s, propid %d\n",
548 This
->codePage
== CP_UNICODE
? debugstr_w((LPCWSTR
)name
) :
549 debugstr_a(name
), id
);
550 dictionary_insert(This
->name_to_propid
, name
, UlongToPtr(id
));
551 dictionary_insert(This
->propid_to_name
, UlongToPtr(id
), name
);
556 /************************************************************************
557 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
559 static HRESULT WINAPI
IPropertyStorage_fnWriteMultiple(
560 IPropertyStorage
* iface
,
562 const PROPSPEC rgpspec
[],
563 const PROPVARIANT rgpropvar
[],
564 PROPID propidNameFirst
)
566 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
570 TRACE("(%p, %d, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
572 if (cpspec
&& (!rgpspec
|| !rgpropvar
))
574 if (!(This
->grfMode
& STGM_READWRITE
))
575 return STG_E_ACCESSDENIED
;
576 EnterCriticalSection(&This
->cs
);
578 This
->originatorOS
= (DWORD
)MAKELONG(LOWORD(GetVersion()),
579 PROPSETHDR_OSVER_KIND_WIN32
) ;
580 for (i
= 0; i
< cpspec
; i
++)
582 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
584 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
585 rgpspec
[i
].u
.lpwstr
);
588 PropVariantCopy(prop
, &rgpropvar
[i
]);
591 /* Note that I don't do the special cases here that I do below,
592 * because naming the special PIDs isn't supported.
594 if (propidNameFirst
< PID_FIRST_USABLE
||
595 propidNameFirst
>= PID_MIN_READONLY
)
596 hr
= STG_E_INVALIDPARAMETER
;
599 PROPID nextId
= max(propidNameFirst
, This
->highestProp
+ 1);
601 hr
= PropertyStorage_StoreNameWithId(This
,
602 (LPCSTR
)rgpspec
[i
].u
.lpwstr
, CP_UNICODE
, nextId
);
604 hr
= PropertyStorage_StorePropWithId(This
, nextId
,
605 &rgpropvar
[i
], GetACP());
611 switch (rgpspec
[i
].u
.propid
)
614 /* Can't set the dictionary */
615 hr
= STG_E_INVALIDPARAMETER
;
618 /* Can only set the code page if nothing else has been set */
619 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
620 rgpropvar
[i
].vt
== VT_I2
)
622 This
->codePage
= rgpropvar
[i
].u
.iVal
;
623 if (This
->codePage
== CP_UNICODE
)
624 This
->grfFlags
&= ~PROPSETFLAG_ANSI
;
626 This
->grfFlags
|= PROPSETFLAG_ANSI
;
629 hr
= STG_E_INVALIDPARAMETER
;
632 /* Can only set the locale if nothing else has been set */
633 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
634 rgpropvar
[i
].vt
== VT_I4
)
635 This
->locale
= rgpropvar
[i
].u
.lVal
;
637 hr
= STG_E_INVALIDPARAMETER
;
640 /* silently ignore like MSDN says */
643 if (rgpspec
[i
].u
.propid
>= PID_MIN_READONLY
)
644 hr
= STG_E_INVALIDPARAMETER
;
646 hr
= PropertyStorage_StorePropWithId(This
,
647 rgpspec
[i
].u
.propid
, &rgpropvar
[i
], GetACP());
651 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
652 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
653 LeaveCriticalSection(&This
->cs
);
657 /************************************************************************
658 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
660 static HRESULT WINAPI
IPropertyStorage_fnDeleteMultiple(
661 IPropertyStorage
* iface
,
663 const PROPSPEC rgpspec
[])
665 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
669 TRACE("(%p, %d, %p)\n", iface
, cpspec
, rgpspec
);
671 if (cpspec
&& !rgpspec
)
673 if (!(This
->grfMode
& STGM_READWRITE
))
674 return STG_E_ACCESSDENIED
;
676 EnterCriticalSection(&This
->cs
);
678 for (i
= 0; i
< cpspec
; i
++)
680 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
684 if (dictionary_find(This
->name_to_propid
, rgpspec
[i
].u
.lpwstr
, &propid
))
685 dictionary_remove(This
->propid_to_prop
, propid
);
689 if (rgpspec
[i
].u
.propid
>= PID_FIRST_USABLE
&&
690 rgpspec
[i
].u
.propid
< PID_MIN_READONLY
)
691 dictionary_remove(This
->propid_to_prop
, UlongToPtr(rgpspec
[i
].u
.propid
));
693 hr
= STG_E_INVALIDPARAMETER
;
696 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
697 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
698 LeaveCriticalSection(&This
->cs
);
702 /************************************************************************
703 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
705 static HRESULT WINAPI
IPropertyStorage_fnReadPropertyNames(
706 IPropertyStorage
* iface
,
708 const PROPID rgpropid
[],
709 LPOLESTR rglpwstrName
[])
711 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
713 HRESULT hr
= S_FALSE
;
715 TRACE("(%p, %d, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
717 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
719 EnterCriticalSection(&This
->cs
);
720 for (i
= 0; i
< cpropid
&& SUCCEEDED(hr
); i
++)
722 LPWSTR name
= PropertyStorage_FindPropertyNameById(This
, rgpropid
[i
]);
726 size_t len
= lstrlenW(name
);
729 rglpwstrName
[i
] = CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
));
731 memcpy(rglpwstrName
[i
], name
, (len
+ 1) * sizeof(WCHAR
));
733 hr
= STG_E_INSUFFICIENTMEMORY
;
736 rglpwstrName
[i
] = NULL
;
738 LeaveCriticalSection(&This
->cs
);
742 /************************************************************************
743 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
745 static HRESULT WINAPI
IPropertyStorage_fnWritePropertyNames(
746 IPropertyStorage
* iface
,
748 const PROPID rgpropid
[],
749 const LPOLESTR rglpwstrName
[])
751 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
755 TRACE("(%p, %d, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
757 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
759 if (!(This
->grfMode
& STGM_READWRITE
))
760 return STG_E_ACCESSDENIED
;
762 EnterCriticalSection(&This
->cs
);
764 for (i
= 0; SUCCEEDED(hr
) && i
< cpropid
; i
++)
766 if (rgpropid
[i
] != PID_ILLEGAL
)
767 hr
= PropertyStorage_StoreNameWithId(This
, (LPCSTR
)rglpwstrName
[i
],
768 CP_UNICODE
, rgpropid
[i
]);
770 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
771 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
772 LeaveCriticalSection(&This
->cs
);
776 /************************************************************************
777 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
779 static HRESULT WINAPI
IPropertyStorage_fnDeletePropertyNames(
780 IPropertyStorage
* iface
,
782 const PROPID rgpropid
[])
784 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
788 TRACE("(%p, %d, %p)\n", iface
, cpropid
, rgpropid
);
790 if (cpropid
&& !rgpropid
)
792 if (!(This
->grfMode
& STGM_READWRITE
))
793 return STG_E_ACCESSDENIED
;
795 EnterCriticalSection(&This
->cs
);
797 for (i
= 0; i
< cpropid
; i
++)
801 if (dictionary_find(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]), (void **)&name
))
803 dictionary_remove(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]));
804 dictionary_remove(This
->name_to_propid
, name
);
807 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
808 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
809 LeaveCriticalSection(&This
->cs
);
813 /************************************************************************
814 * IPropertyStorage_fnCommit (IPropertyStorage)
816 static HRESULT WINAPI
IPropertyStorage_fnCommit(
817 IPropertyStorage
* iface
,
818 DWORD grfCommitFlags
)
820 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
823 TRACE("(%p, 0x%08x)\n", iface
, grfCommitFlags
);
825 if (!(This
->grfMode
& STGM_READWRITE
))
826 return STG_E_ACCESSDENIED
;
827 EnterCriticalSection(&This
->cs
);
829 hr
= PropertyStorage_WriteToStream(This
);
832 LeaveCriticalSection(&This
->cs
);
836 /************************************************************************
837 * IPropertyStorage_fnRevert (IPropertyStorage)
839 static HRESULT WINAPI
IPropertyStorage_fnRevert(
840 IPropertyStorage
* iface
)
843 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
845 TRACE("%p\n", iface
);
847 EnterCriticalSection(&This
->cs
);
850 PropertyStorage_DestroyDictionaries(This
);
851 hr
= PropertyStorage_CreateDictionaries(This
);
853 hr
= PropertyStorage_ReadFromStream(This
);
857 LeaveCriticalSection(&This
->cs
);
861 /************************************************************************
862 * IPropertyStorage_fnEnum (IPropertyStorage)
864 static HRESULT WINAPI
IPropertyStorage_fnEnum(
865 IPropertyStorage
* iface
,
866 IEnumSTATPROPSTG
** ppenum
)
868 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
869 return create_EnumSTATPROPSTG(This
, ppenum
);
872 /************************************************************************
873 * IPropertyStorage_fnSetTimes (IPropertyStorage)
875 static HRESULT WINAPI
IPropertyStorage_fnSetTimes(
876 IPropertyStorage
* iface
,
877 const FILETIME
* pctime
,
878 const FILETIME
* patime
,
879 const FILETIME
* pmtime
)
885 /************************************************************************
886 * IPropertyStorage_fnSetClass (IPropertyStorage)
888 static HRESULT WINAPI
IPropertyStorage_fnSetClass(
889 IPropertyStorage
* iface
,
892 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
894 TRACE("%p, %s\n", iface
, debugstr_guid(clsid
));
898 if (!(This
->grfMode
& STGM_READWRITE
))
899 return STG_E_ACCESSDENIED
;
900 This
->clsid
= *clsid
;
902 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
903 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
907 /************************************************************************
908 * IPropertyStorage_fnStat (IPropertyStorage)
910 static HRESULT WINAPI
IPropertyStorage_fnStat(
911 IPropertyStorage
* iface
,
912 STATPROPSETSTG
* statpsstg
)
914 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
918 TRACE("%p, %p\n", iface
, statpsstg
);
923 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
926 statpsstg
->fmtid
= This
->fmtid
;
927 statpsstg
->clsid
= This
->clsid
;
928 statpsstg
->grfFlags
= This
->grfFlags
;
929 statpsstg
->mtime
= stat
.mtime
;
930 statpsstg
->ctime
= stat
.ctime
;
931 statpsstg
->atime
= stat
.atime
;
932 statpsstg
->dwOSVersion
= This
->originatorOS
;
937 static int PropertyStorage_PropNameCompare(const void *a
, const void *b
,
940 PropertyStorage_impl
*This
= extra
;
942 if (This
->codePage
== CP_UNICODE
)
944 TRACE("(%s, %s)\n", debugstr_w(a
), debugstr_w(b
));
945 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
946 return lstrcmpW(a
, b
);
948 return lstrcmpiW(a
, b
);
952 TRACE("(%s, %s)\n", debugstr_a(a
), debugstr_a(b
));
953 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
954 return lstrcmpA(a
, b
);
956 return lstrcmpiA(a
, b
);
960 static void PropertyStorage_PropNameDestroy(void *k
, void *d
, void *extra
)
965 static int PropertyStorage_PropCompare(const void *a
, const void *b
,
968 TRACE("(%d, %d)\n", PtrToUlong(a
), PtrToUlong(b
));
969 return PtrToUlong(a
) - PtrToUlong(b
);
972 static void PropertyStorage_PropertyDestroy(void *k
, void *d
, void *extra
)
975 HeapFree(GetProcessHeap(), 0, d
);
978 #ifdef WORDS_BIGENDIAN
979 /* Swaps each character in str to or from little endian; assumes the conversion
980 * is symmetric, that is, that lendian16toh is equivalent to htole16.
982 static void PropertyStorage_ByteSwapString(LPWSTR str
, size_t len
)
986 /* Swap characters to host order.
989 for (i
= 0; i
< len
; i
++)
990 str
[i
] = lendian16toh(str
[i
]);
993 #define PropertyStorage_ByteSwapString(s, l)
996 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
997 * the entries according to the values of This->codePage and This->locale.
998 * FIXME: there isn't any checking whether the read property extends past the
1001 static HRESULT
PropertyStorage_ReadDictionary(PropertyStorage_impl
*This
,
1004 DWORD numEntries
, i
;
1007 assert(This
->name_to_propid
);
1008 assert(This
->propid_to_name
);
1010 StorageUtl_ReadDWord(ptr
, 0, &numEntries
);
1011 TRACE("Reading %d entries:\n", numEntries
);
1012 ptr
+= sizeof(DWORD
);
1013 for (i
= 0; SUCCEEDED(hr
) && i
< numEntries
; i
++)
1018 StorageUtl_ReadDWord(ptr
, 0, &propid
);
1019 ptr
+= sizeof(PROPID
);
1020 StorageUtl_ReadDWord(ptr
, 0, &cbEntry
);
1021 ptr
+= sizeof(DWORD
);
1022 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid
, cbEntry
);
1023 /* Make sure the source string is NULL-terminated */
1024 if (This
->codePage
!= CP_UNICODE
)
1025 ptr
[cbEntry
- 1] = '\0';
1027 ((LPWSTR
)ptr
)[cbEntry
- 1] = 0;
1028 hr
= PropertyStorage_StoreNameWithId(This
, (char*)ptr
, This
->codePage
, propid
);
1029 if (This
->codePage
== CP_UNICODE
)
1031 /* cbEntry is the number of characters */
1034 /* Unicode entries are padded to DWORD boundaries */
1035 if (cbEntry
% sizeof(DWORD
))
1036 ptr
+= sizeof(DWORD
) - (cbEntry
% sizeof(DWORD
));
1044 #define __thiscall_wrapper __stdcall
1046 #define __thiscall_wrapper __cdecl
1049 static void* __thiscall_wrapper
Allocate_CoTaskMemAlloc(void *this, ULONG size
)
1051 return CoTaskMemAlloc(size
);
1054 /* FIXME: there isn't any checking whether the read property extends past the
1055 * end of the buffer.
1057 static HRESULT
PropertyStorage_ReadProperty(PROPVARIANT
*prop
, const BYTE
*data
,
1058 UINT codepage
, void* (__thiscall_wrapper
*allocate
)(void *this, ULONG size
), void *allocate_data
)
1065 StorageUtl_ReadDWord(data
, 0, &vt
);
1066 data
+= sizeof(DWORD
);
1074 prop
->u
.cVal
= *(const char *)data
;
1075 TRACE("Read char 0x%x ('%c')\n", prop
->u
.cVal
, prop
->u
.cVal
);
1078 prop
->u
.bVal
= *data
;
1079 TRACE("Read byte 0x%x\n", prop
->u
.bVal
);
1082 StorageUtl_ReadWord(data
, 0, (WORD
*)&prop
->u
.boolVal
);
1083 TRACE("Read bool %d\n", prop
->u
.boolVal
);
1086 StorageUtl_ReadWord(data
, 0, (WORD
*)&prop
->u
.iVal
);
1087 TRACE("Read short %d\n", prop
->u
.iVal
);
1090 StorageUtl_ReadWord(data
, 0, &prop
->u
.uiVal
);
1091 TRACE("Read ushort %d\n", prop
->u
.uiVal
);
1095 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->u
.lVal
);
1096 TRACE("Read long %d\n", prop
->u
.lVal
);
1100 StorageUtl_ReadDWord(data
, 0, &prop
->u
.ulVal
);
1101 TRACE("Read ulong %d\n", prop
->u
.ulVal
);
1104 StorageUtl_ReadULargeInteger(data
, 0, (ULARGE_INTEGER
*)&prop
->u
.hVal
);
1105 TRACE("Read long long %s\n", wine_dbgstr_longlong(prop
->u
.hVal
.QuadPart
));
1108 StorageUtl_ReadULargeInteger(data
, 0, &prop
->u
.uhVal
);
1109 TRACE("Read ulong long %s\n", wine_dbgstr_longlong(prop
->u
.uhVal
.QuadPart
));
1112 memcpy(&prop
->u
.dblVal
, data
, sizeof(double));
1113 TRACE("Read double %f\n", prop
->u
.dblVal
);
1119 StorageUtl_ReadDWord(data
, 0, &count
);
1120 if (codepage
== CP_UNICODE
&& count
% 2)
1122 WARN("Unicode string has odd number of bytes\n");
1123 hr
= STG_E_INVALIDHEADER
;
1127 prop
->u
.pszVal
= allocate(allocate_data
, count
);
1130 memcpy(prop
->u
.pszVal
, data
+ sizeof(DWORD
), count
);
1131 /* This is stored in the code page specified in codepage.
1132 * Don't convert it, the caller will just store it as-is.
1134 if (codepage
== CP_UNICODE
)
1136 /* Make sure it's NULL-terminated */
1137 prop
->u
.pszVal
[count
/ sizeof(WCHAR
) - 1] = '\0';
1138 TRACE("Read string value %s\n",
1139 debugstr_w(prop
->u
.pwszVal
));
1143 /* Make sure it's NULL-terminated */
1144 prop
->u
.pszVal
[count
- 1] = '\0';
1145 TRACE("Read string value %s\n", debugstr_a(prop
->u
.pszVal
));
1149 hr
= STG_E_INSUFFICIENTMEMORY
;
1155 DWORD count
, wcount
;
1157 StorageUtl_ReadDWord(data
, 0, &count
);
1158 if (codepage
== CP_UNICODE
&& count
% 2)
1160 WARN("Unicode string has odd number of bytes\n");
1161 hr
= STG_E_INVALIDHEADER
;
1165 if (codepage
== CP_UNICODE
)
1168 wcount
= MultiByteToWideChar(codepage
, 0, (LPCSTR
)(data
+ sizeof(DWORD
)), count
, NULL
, 0);
1170 prop
->u
.bstrVal
= SysAllocStringLen(NULL
, wcount
); /* FIXME: use allocator? */
1172 if (prop
->u
.bstrVal
)
1174 if (codepage
== CP_UNICODE
)
1175 memcpy(prop
->u
.bstrVal
, data
+ sizeof(DWORD
), count
);
1177 MultiByteToWideChar(codepage
, 0, (LPCSTR
)(data
+ sizeof(DWORD
)), count
, prop
->u
.bstrVal
, wcount
);
1179 prop
->u
.bstrVal
[wcount
- 1] = '\0';
1180 TRACE("Read string value %s\n", debugstr_w(prop
->u
.bstrVal
));
1183 hr
= STG_E_INSUFFICIENTMEMORY
;
1191 StorageUtl_ReadDWord(data
, 0, &count
);
1192 prop
->u
.blob
.cbSize
= count
;
1193 prop
->u
.blob
.pBlobData
= allocate(allocate_data
, count
);
1194 if (prop
->u
.blob
.pBlobData
)
1196 memcpy(prop
->u
.blob
.pBlobData
, data
+ sizeof(DWORD
), count
);
1197 TRACE("Read blob value of size %d\n", count
);
1200 hr
= STG_E_INSUFFICIENTMEMORY
;
1207 StorageUtl_ReadDWord(data
, 0, &count
);
1208 prop
->u
.pwszVal
= allocate(allocate_data
, count
* sizeof(WCHAR
));
1209 if (prop
->u
.pwszVal
)
1211 memcpy(prop
->u
.pwszVal
, data
+ sizeof(DWORD
),
1212 count
* sizeof(WCHAR
));
1213 /* make sure string is NULL-terminated */
1214 prop
->u
.pwszVal
[count
- 1] = '\0';
1215 PropertyStorage_ByteSwapString(prop
->u
.pwszVal
, count
);
1216 TRACE("Read string value %s\n", debugstr_w(prop
->u
.pwszVal
));
1219 hr
= STG_E_INSUFFICIENTMEMORY
;
1223 StorageUtl_ReadULargeInteger(data
, 0,
1224 (ULARGE_INTEGER
*)&prop
->u
.filetime
);
1228 DWORD len
= 0, tag
= 0;
1230 StorageUtl_ReadDWord(data
, 0, &len
);
1231 StorageUtl_ReadDWord(data
, 4, &tag
);
1235 prop
->u
.pclipdata
= allocate(allocate_data
, sizeof (CLIPDATA
));
1236 prop
->u
.pclipdata
->cbSize
= len
;
1237 prop
->u
.pclipdata
->ulClipFmt
= tag
;
1238 prop
->u
.pclipdata
->pClipData
= allocate(allocate_data
, len
- sizeof(prop
->u
.pclipdata
->ulClipFmt
));
1239 memcpy(prop
->u
.pclipdata
->pClipData
, data
+8, len
- sizeof(prop
->u
.pclipdata
->ulClipFmt
));
1242 hr
= STG_E_INVALIDPARAMETER
;
1246 FIXME("unsupported type %d\n", prop
->vt
);
1247 hr
= STG_E_INVALIDPARAMETER
;
1252 static HRESULT
PropertyStorage_ReadHeaderFromStream(IStream
*stm
,
1253 PROPERTYSETHEADER
*hdr
)
1255 BYTE buf
[sizeof(PROPERTYSETHEADER
)];
1261 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1264 if (count
!= sizeof(buf
))
1266 WARN("read only %d\n", count
);
1267 hr
= STG_E_INVALIDHEADER
;
1271 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wByteOrder
),
1273 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wFormat
),
1275 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, dwOSVer
),
1277 StorageUtl_ReadGUID(buf
, offsetof(PROPERTYSETHEADER
, clsid
),
1279 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, reserved
),
1283 TRACE("returning 0x%08x\n", hr
);
1287 static HRESULT
PropertyStorage_ReadFmtIdOffsetFromStream(IStream
*stm
,
1288 FORMATIDOFFSET
*fmt
)
1290 BYTE buf
[sizeof(FORMATIDOFFSET
)];
1296 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1299 if (count
!= sizeof(buf
))
1301 WARN("read only %d\n", count
);
1302 hr
= STG_E_INVALIDHEADER
;
1306 StorageUtl_ReadGUID(buf
, offsetof(FORMATIDOFFSET
, fmtid
),
1308 StorageUtl_ReadDWord(buf
, offsetof(FORMATIDOFFSET
, dwOffset
),
1312 TRACE("returning 0x%08x\n", hr
);
1316 static HRESULT
PropertyStorage_ReadSectionHeaderFromStream(IStream
*stm
,
1317 PROPERTYSECTIONHEADER
*hdr
)
1319 BYTE buf
[sizeof(PROPERTYSECTIONHEADER
)];
1325 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1328 if (count
!= sizeof(buf
))
1330 WARN("read only %d\n", count
);
1331 hr
= STG_E_INVALIDHEADER
;
1335 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1336 cbSection
), &hdr
->cbSection
);
1337 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1338 cProperties
), &hdr
->cProperties
);
1341 TRACE("returning 0x%08x\n", hr
);
1345 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*This
)
1347 PROPERTYSETHEADER hdr
;
1348 FORMATIDOFFSET fmtOffset
;
1349 PROPERTYSECTIONHEADER sectionHdr
;
1356 DWORD dictOffset
= 0;
1358 This
->dirty
= FALSE
;
1359 This
->highestProp
= 0;
1360 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1363 if (stat
.cbSize
.u
.HighPart
)
1365 WARN("stream too big\n");
1366 /* maximum size varies, but it can't be this big */
1367 hr
= STG_E_INVALIDHEADER
;
1370 if (stat
.cbSize
.u
.LowPart
== 0)
1372 /* empty stream is okay */
1376 else if (stat
.cbSize
.u
.LowPart
< sizeof(PROPERTYSETHEADER
) +
1377 sizeof(FORMATIDOFFSET
))
1379 WARN("stream too small\n");
1380 hr
= STG_E_INVALIDHEADER
;
1384 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1387 hr
= PropertyStorage_ReadHeaderFromStream(This
->stm
, &hdr
);
1388 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1391 if (hdr
.wByteOrder
!= PROPSETHDR_BYTEORDER_MAGIC
|| hdr
.reserved
< 1)
1393 WARN("bad magic in prop set header\n");
1394 hr
= STG_E_INVALIDHEADER
;
1397 if (hdr
.wFormat
!= 0 && hdr
.wFormat
!= 1)
1399 WARN("bad format version %d\n", hdr
.wFormat
);
1400 hr
= STG_E_INVALIDHEADER
;
1403 This
->format
= hdr
.wFormat
;
1404 This
->clsid
= hdr
.clsid
;
1405 This
->originatorOS
= hdr
.dwOSVer
;
1406 if (PROPSETHDR_OSVER_KIND(hdr
.dwOSVer
) == PROPSETHDR_OSVER_KIND_MAC
)
1407 WARN("File comes from a Mac, strings will probably be screwed up\n");
1408 hr
= PropertyStorage_ReadFmtIdOffsetFromStream(This
->stm
, &fmtOffset
);
1411 if (fmtOffset
.dwOffset
> stat
.cbSize
.u
.LowPart
)
1413 WARN("invalid offset %d (stream length is %d)\n", fmtOffset
.dwOffset
,
1414 stat
.cbSize
.u
.LowPart
);
1415 hr
= STG_E_INVALIDHEADER
;
1418 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1419 * follows not one, but two sections. The first contains the standard properties
1420 * for the document summary information, and the second consists of user-defined
1421 * properties. This is the only case in which multiple sections are
1423 * Reading the second stream isn't implemented yet.
1425 hr
= PropertyStorage_ReadSectionHeaderFromStream(This
->stm
, §ionHdr
);
1428 /* The section size includes the section header, so check it */
1429 if (sectionHdr
.cbSection
< sizeof(PROPERTYSECTIONHEADER
))
1431 WARN("section header too small, got %d\n", sectionHdr
.cbSection
);
1432 hr
= STG_E_INVALIDHEADER
;
1435 buf
= HeapAlloc(GetProcessHeap(), 0, sectionHdr
.cbSection
-
1436 sizeof(PROPERTYSECTIONHEADER
));
1439 hr
= STG_E_INSUFFICIENTMEMORY
;
1442 hr
= IStream_Read(This
->stm
, buf
, sectionHdr
.cbSection
-
1443 sizeof(PROPERTYSECTIONHEADER
), &count
);
1446 TRACE("Reading %d properties:\n", sectionHdr
.cProperties
);
1447 for (i
= 0; SUCCEEDED(hr
) && i
< sectionHdr
.cProperties
; i
++)
1449 PROPERTYIDOFFSET
*idOffset
= (PROPERTYIDOFFSET
*)(buf
+
1450 i
* sizeof(PROPERTYIDOFFSET
));
1452 if (idOffset
->dwOffset
< sizeof(PROPERTYSECTIONHEADER
) ||
1453 idOffset
->dwOffset
> sectionHdr
.cbSection
- sizeof(DWORD
))
1454 hr
= STG_E_INVALIDPOINTER
;
1457 if (idOffset
->propid
>= PID_FIRST_USABLE
&&
1458 idOffset
->propid
< PID_MIN_READONLY
&& idOffset
->propid
>
1460 This
->highestProp
= idOffset
->propid
;
1461 if (idOffset
->propid
== PID_DICTIONARY
)
1463 /* Don't read the dictionary yet, its entries depend on the
1464 * code page. Just store the offset so we know to read it
1467 dictOffset
= idOffset
->dwOffset
;
1468 TRACE("Dictionary offset is %d\n", dictOffset
);
1474 PropVariantInit(&prop
);
1475 if (SUCCEEDED(PropertyStorage_ReadProperty(&prop
,
1476 buf
+ idOffset
->dwOffset
- sizeof(PROPERTYSECTIONHEADER
),
1477 This
->codePage
, Allocate_CoTaskMemAlloc
, NULL
)))
1479 TRACE("Read property with ID 0x%08x, type %d\n",
1480 idOffset
->propid
, prop
.vt
);
1481 switch(idOffset
->propid
)
1484 if (prop
.vt
== VT_I2
)
1485 This
->codePage
= (UINT
)prop
.u
.iVal
;
1488 if (prop
.vt
== VT_I4
)
1489 This
->locale
= (LCID
)prop
.u
.lVal
;
1492 if (prop
.vt
== VT_I4
&& prop
.u
.lVal
)
1493 This
->grfFlags
|= PROPSETFLAG_CASE_SENSITIVE
;
1494 /* The format should already be 1, but just in case */
1498 hr
= PropertyStorage_StorePropWithId(This
,
1499 idOffset
->propid
, &prop
, This
->codePage
);
1502 PropVariantClear(&prop
);
1506 if (!This
->codePage
)
1508 /* default to Unicode unless told not to, as specified on msdn */
1509 if (This
->grfFlags
& PROPSETFLAG_ANSI
)
1510 This
->codePage
= GetACP();
1512 This
->codePage
= CP_UNICODE
;
1515 This
->locale
= LOCALE_SYSTEM_DEFAULT
;
1516 TRACE("Code page is %d, locale is %d\n", This
->codePage
, This
->locale
);
1518 hr
= PropertyStorage_ReadDictionary(This
,
1519 buf
+ dictOffset
- sizeof(PROPERTYSECTIONHEADER
));
1522 HeapFree(GetProcessHeap(), 0, buf
);
1525 dictionary_destroy(This
->name_to_propid
);
1526 This
->name_to_propid
= NULL
;
1527 dictionary_destroy(This
->propid_to_name
);
1528 This
->propid_to_name
= NULL
;
1529 dictionary_destroy(This
->propid_to_prop
);
1530 This
->propid_to_prop
= NULL
;
1535 static void PropertyStorage_MakeHeader(PropertyStorage_impl
*This
,
1536 PROPERTYSETHEADER
*hdr
)
1539 StorageUtl_WriteWord((BYTE
*)&hdr
->wByteOrder
, 0,
1540 PROPSETHDR_BYTEORDER_MAGIC
);
1541 StorageUtl_WriteWord((BYTE
*)&hdr
->wFormat
, 0, This
->format
);
1542 StorageUtl_WriteDWord((BYTE
*)&hdr
->dwOSVer
, 0, This
->originatorOS
);
1543 StorageUtl_WriteGUID((BYTE
*)&hdr
->clsid
, 0, &This
->clsid
);
1544 StorageUtl_WriteDWord((BYTE
*)&hdr
->reserved
, 0, 1);
1547 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl
*This
,
1548 FORMATIDOFFSET
*fmtOffset
)
1551 StorageUtl_WriteGUID((BYTE
*)fmtOffset
, 0, &This
->fmtid
);
1552 StorageUtl_WriteDWord((BYTE
*)fmtOffset
, offsetof(FORMATIDOFFSET
, dwOffset
),
1553 sizeof(PROPERTYSETHEADER
) + sizeof(FORMATIDOFFSET
));
1556 static void PropertyStorage_MakeSectionHdr(DWORD cbSection
, DWORD numProps
,
1557 PROPERTYSECTIONHEADER
*hdr
)
1560 StorageUtl_WriteDWord((BYTE
*)hdr
, 0, cbSection
);
1561 StorageUtl_WriteDWord((BYTE
*)hdr
,
1562 offsetof(PROPERTYSECTIONHEADER
, cProperties
), numProps
);
1565 static void PropertyStorage_MakePropertyIdOffset(DWORD propid
, DWORD dwOffset
,
1566 PROPERTYIDOFFSET
*propIdOffset
)
1568 assert(propIdOffset
);
1569 StorageUtl_WriteDWord((BYTE
*)propIdOffset
, 0, propid
);
1570 StorageUtl_WriteDWord((BYTE
*)propIdOffset
,
1571 offsetof(PROPERTYIDOFFSET
, dwOffset
), dwOffset
);
1574 static inline HRESULT
PropertStorage_WriteWStringToStream(IStream
*stm
,
1575 LPCWSTR str
, DWORD len
, DWORD
*written
)
1577 #ifdef WORDS_BIGENDIAN
1578 WCHAR
*leStr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1582 return E_OUTOFMEMORY
;
1583 memcpy(leStr
, str
, len
* sizeof(WCHAR
));
1584 PropertyStorage_ByteSwapString(leStr
, len
);
1585 hr
= IStream_Write(stm
, leStr
, len
, written
);
1586 HeapFree(GetProcessHeap(), 0, leStr
);
1589 return IStream_Write(stm
, str
, len
, written
);
1593 struct DictionaryClosure
1599 static BOOL
PropertyStorage_DictionaryWriter(const void *key
,
1600 const void *value
, void *extra
, void *closure
)
1602 PropertyStorage_impl
*This
= extra
;
1603 struct DictionaryClosure
*c
= closure
;
1609 StorageUtl_WriteDWord((LPBYTE
)&propid
, 0, PtrToUlong(value
));
1610 c
->hr
= IStream_Write(This
->stm
, &propid
, sizeof(propid
), &count
);
1613 c
->bytesWritten
+= sizeof(DWORD
);
1614 if (This
->codePage
== CP_UNICODE
)
1616 DWORD keyLen
, pad
= 0;
1618 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0,
1619 (lstrlenW((LPCWSTR
)key
) + 1) * sizeof(WCHAR
));
1620 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1623 c
->bytesWritten
+= sizeof(DWORD
);
1624 c
->hr
= PropertStorage_WriteWStringToStream(This
->stm
, key
, keyLen
,
1628 c
->bytesWritten
+= keyLen
* sizeof(WCHAR
);
1629 if (keyLen
% sizeof(DWORD
))
1631 c
->hr
= IStream_Write(This
->stm
, &pad
,
1632 sizeof(DWORD
) - keyLen
% sizeof(DWORD
), &count
);
1635 c
->bytesWritten
+= sizeof(DWORD
) - keyLen
% sizeof(DWORD
);
1642 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0, strlen((LPCSTR
)key
) + 1);
1643 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1646 c
->bytesWritten
+= sizeof(DWORD
);
1647 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1650 c
->bytesWritten
+= keyLen
;
1653 return SUCCEEDED(c
->hr
);
1656 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1658 /* Writes the dictionary to the stream. Assumes without checking that the
1659 * dictionary isn't empty.
1661 static HRESULT
PropertyStorage_WriteDictionaryToStream(
1662 PropertyStorage_impl
*This
, DWORD
*sectionOffset
)
1666 PROPERTYIDOFFSET propIdOffset
;
1669 struct DictionaryClosure closure
;
1671 assert(sectionOffset
);
1673 /* The dictionary's always the first property written, so seek to its
1676 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
);
1677 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1680 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY
, *sectionOffset
,
1682 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1686 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1687 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1690 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0,
1691 dictionary_num_entries(This
->name_to_propid
));
1692 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1695 *sectionOffset
+= sizeof(dwTemp
);
1698 closure
.bytesWritten
= 0;
1699 dictionary_enumerate(This
->name_to_propid
, PropertyStorage_DictionaryWriter
,
1704 *sectionOffset
+= closure
.bytesWritten
;
1705 if (closure
.bytesWritten
% sizeof(DWORD
))
1707 DWORD padding
= sizeof(DWORD
) - closure
.bytesWritten
% sizeof(DWORD
);
1708 TRACE("adding %d bytes of padding\n", padding
);
1709 *sectionOffset
+= padding
;
1716 static HRESULT
PropertyStorage_WritePropertyToStream(PropertyStorage_impl
*This
,
1717 DWORD propNum
, DWORD propid
, const PROPVARIANT
*var
, DWORD
*sectionOffset
)
1721 PROPERTYIDOFFSET propIdOffset
;
1723 DWORD dwType
, bytesWritten
;
1726 assert(sectionOffset
);
1728 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This
, propNum
, propid
, var
->vt
,
1731 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
) +
1732 propNum
* sizeof(PROPERTYIDOFFSET
);
1733 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1736 PropertyStorage_MakePropertyIdOffset(propid
, *sectionOffset
, &propIdOffset
);
1737 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1741 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1742 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1745 StorageUtl_WriteDWord((LPBYTE
)&dwType
, 0, var
->vt
);
1746 hr
= IStream_Write(This
->stm
, &dwType
, sizeof(dwType
), &count
);
1749 *sectionOffset
+= sizeof(dwType
);
1759 hr
= IStream_Write(This
->stm
, &var
->u
.cVal
, sizeof(var
->u
.cVal
),
1761 bytesWritten
= count
;
1768 StorageUtl_WriteWord((LPBYTE
)&wTemp
, 0, var
->u
.iVal
);
1769 hr
= IStream_Write(This
->stm
, &wTemp
, sizeof(wTemp
), &count
);
1770 bytesWritten
= count
;
1778 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, var
->u
.lVal
);
1779 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1780 bytesWritten
= count
;
1787 if (This
->codePage
== CP_UNICODE
)
1788 len
= (lstrlenW(var
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
1790 len
= lstrlenA(var
->u
.pszVal
) + 1;
1791 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1792 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1795 hr
= IStream_Write(This
->stm
, var
->u
.pszVal
, len
, &count
);
1796 bytesWritten
= count
+ sizeof(DWORD
);
1801 DWORD len
= lstrlenW(var
->u
.pwszVal
) + 1, dwTemp
;
1803 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1804 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1807 hr
= IStream_Write(This
->stm
, var
->u
.pwszVal
, len
* sizeof(WCHAR
),
1809 bytesWritten
= count
+ sizeof(DWORD
);
1816 StorageUtl_WriteULargeInteger((BYTE
*)&temp
, 0,
1817 (const ULARGE_INTEGER
*)&var
->u
.filetime
);
1818 hr
= IStream_Write(This
->stm
, &temp
, sizeof(FILETIME
), &count
);
1819 bytesWritten
= count
;
1824 DWORD cf_hdr
[2], len
;
1826 len
= var
->u
.pclipdata
->cbSize
;
1827 StorageUtl_WriteDWord((LPBYTE
)&cf_hdr
[0], 0, len
+ 8);
1828 StorageUtl_WriteDWord((LPBYTE
)&cf_hdr
[1], 0, var
->u
.pclipdata
->ulClipFmt
);
1829 hr
= IStream_Write(This
->stm
, cf_hdr
, sizeof(cf_hdr
), &count
);
1832 hr
= IStream_Write(This
->stm
, var
->u
.pclipdata
->pClipData
,
1833 len
- sizeof(var
->u
.pclipdata
->ulClipFmt
), &count
);
1836 bytesWritten
= count
+ sizeof cf_hdr
;
1843 StorageUtl_WriteGUID((BYTE
*)&temp
, 0, var
->u
.puuid
);
1844 hr
= IStream_Write(This
->stm
, &temp
, sizeof(temp
), &count
);
1845 bytesWritten
= count
;
1849 FIXME("unsupported type: %d\n", var
->vt
);
1850 return STG_E_INVALIDPARAMETER
;
1855 *sectionOffset
+= bytesWritten
;
1856 if (bytesWritten
% sizeof(DWORD
))
1858 DWORD padding
= sizeof(DWORD
) - bytesWritten
% sizeof(DWORD
);
1859 TRACE("adding %d bytes of padding\n", padding
);
1860 *sectionOffset
+= padding
;
1868 struct PropertyClosure
1872 DWORD
*sectionOffset
;
1875 static BOOL
PropertyStorage_PropertiesWriter(const void *key
, const void *value
,
1876 void *extra
, void *closure
)
1878 PropertyStorage_impl
*This
= extra
;
1879 struct PropertyClosure
*c
= closure
;
1885 c
->hr
= PropertyStorage_WritePropertyToStream(This
, c
->propNum
++,
1886 PtrToUlong(key
), value
, c
->sectionOffset
);
1887 return SUCCEEDED(c
->hr
);
1890 static HRESULT
PropertyStorage_WritePropertiesToStream(
1891 PropertyStorage_impl
*This
, DWORD startingPropNum
, DWORD
*sectionOffset
)
1893 struct PropertyClosure closure
;
1895 assert(sectionOffset
);
1897 closure
.propNum
= startingPropNum
;
1898 closure
.sectionOffset
= sectionOffset
;
1899 dictionary_enumerate(This
->propid_to_prop
, PropertyStorage_PropertiesWriter
,
1904 static HRESULT
PropertyStorage_WriteHeadersToStream(PropertyStorage_impl
*This
)
1908 LARGE_INTEGER seek
= { {0} };
1909 PROPERTYSETHEADER hdr
;
1910 FORMATIDOFFSET fmtOffset
;
1912 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1915 PropertyStorage_MakeHeader(This
, &hdr
);
1916 hr
= IStream_Write(This
->stm
, &hdr
, sizeof(hdr
), &count
);
1919 if (count
!= sizeof(hdr
))
1921 hr
= STG_E_WRITEFAULT
;
1925 PropertyStorage_MakeFmtIdOffset(This
, &fmtOffset
);
1926 hr
= IStream_Write(This
->stm
, &fmtOffset
, sizeof(fmtOffset
), &count
);
1929 if (count
!= sizeof(fmtOffset
))
1931 hr
= STG_E_WRITEFAULT
;
1940 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*This
)
1942 PROPERTYSECTIONHEADER sectionHdr
;
1946 DWORD numProps
, prop
, sectionOffset
, dwTemp
;
1949 PropertyStorage_WriteHeadersToStream(This
);
1951 /* Count properties. Always at least one property, the code page */
1953 if (dictionary_num_entries(This
->name_to_propid
))
1955 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1957 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1959 numProps
+= dictionary_num_entries(This
->propid_to_prop
);
1961 /* Write section header with 0 bytes right now, I'll adjust it after
1962 * writing properties.
1964 PropertyStorage_MakeSectionHdr(0, numProps
, §ionHdr
);
1965 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1966 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1969 hr
= IStream_Write(This
->stm
, §ionHdr
, sizeof(sectionHdr
), &count
);
1974 sectionOffset
= sizeof(PROPERTYSECTIONHEADER
) +
1975 numProps
* sizeof(PROPERTYIDOFFSET
);
1977 if (dictionary_num_entries(This
->name_to_propid
))
1980 hr
= PropertyStorage_WriteDictionaryToStream(This
, §ionOffset
);
1985 PropVariantInit(&var
);
1988 var
.u
.iVal
= This
->codePage
;
1989 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_CODEPAGE
,
1990 &var
, §ionOffset
);
1994 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1997 var
.u
.lVal
= This
->locale
;
1998 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_LOCALE
,
1999 &var
, §ionOffset
);
2004 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2008 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_BEHAVIOR
,
2009 &var
, §ionOffset
);
2014 hr
= PropertyStorage_WritePropertiesToStream(This
, prop
, §ionOffset
);
2018 /* Now write the byte count of the section */
2019 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
2020 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
2023 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, sectionOffset
);
2024 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
2030 /***********************************************************************
2031 * PropertyStorage_Construct
2033 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*This
)
2035 dictionary_destroy(This
->name_to_propid
);
2036 This
->name_to_propid
= NULL
;
2037 dictionary_destroy(This
->propid_to_name
);
2038 This
->propid_to_name
= NULL
;
2039 dictionary_destroy(This
->propid_to_prop
);
2040 This
->propid_to_prop
= NULL
;
2043 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*This
)
2047 This
->name_to_propid
= dictionary_create(
2048 PropertyStorage_PropNameCompare
, PropertyStorage_PropNameDestroy
,
2050 if (!This
->name_to_propid
)
2052 hr
= STG_E_INSUFFICIENTMEMORY
;
2055 This
->propid_to_name
= dictionary_create(PropertyStorage_PropCompare
,
2057 if (!This
->propid_to_name
)
2059 hr
= STG_E_INSUFFICIENTMEMORY
;
2062 This
->propid_to_prop
= dictionary_create(PropertyStorage_PropCompare
,
2063 PropertyStorage_PropertyDestroy
, This
);
2064 if (!This
->propid_to_prop
)
2066 hr
= STG_E_INSUFFICIENTMEMORY
;
2071 PropertyStorage_DestroyDictionaries(This
);
2075 static HRESULT
PropertyStorage_BaseConstruct(IStream
*stm
,
2076 REFFMTID rfmtid
, DWORD grfMode
, PropertyStorage_impl
**pps
)
2082 *pps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof **pps
);
2084 return E_OUTOFMEMORY
;
2086 (*pps
)->IPropertyStorage_iface
.lpVtbl
= &IPropertyStorage_Vtbl
;
2088 InitializeCriticalSection(&(*pps
)->cs
);
2089 (*pps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PropertyStorage_impl.cs");
2091 (*pps
)->fmtid
= *rfmtid
;
2092 (*pps
)->grfMode
= grfMode
;
2094 hr
= PropertyStorage_CreateDictionaries(*pps
);
2097 (*pps
)->cs
.DebugInfo
->Spare
[0] = 0;
2098 DeleteCriticalSection(&(*pps
)->cs
);
2099 HeapFree(GetProcessHeap(), 0, *pps
);
2102 else IStream_AddRef( stm
);
2107 static HRESULT
PropertyStorage_ConstructFromStream(IStream
*stm
,
2108 REFFMTID rfmtid
, DWORD grfMode
, IPropertyStorage
** pps
)
2110 PropertyStorage_impl
*ps
;
2114 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2117 hr
= PropertyStorage_ReadFromStream(ps
);
2120 *pps
= &ps
->IPropertyStorage_iface
;
2121 TRACE("PropertyStorage %p constructed\n", ps
);
2124 else IPropertyStorage_Release( &ps
->IPropertyStorage_iface
);
2129 static HRESULT
PropertyStorage_ConstructEmpty(IStream
*stm
,
2130 REFFMTID rfmtid
, DWORD grfFlags
, DWORD grfMode
, IPropertyStorage
** pps
)
2132 PropertyStorage_impl
*ps
;
2136 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2140 ps
->grfFlags
= grfFlags
;
2141 if (ps
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2143 /* default to Unicode unless told not to, as specified on msdn */
2144 if (ps
->grfFlags
& PROPSETFLAG_ANSI
)
2145 ps
->codePage
= GetACP();
2147 ps
->codePage
= CP_UNICODE
;
2148 ps
->locale
= LOCALE_SYSTEM_DEFAULT
;
2149 TRACE("Code page is %d, locale is %d\n", ps
->codePage
, ps
->locale
);
2150 *pps
= &ps
->IPropertyStorage_iface
;
2151 TRACE("PropertyStorage %p constructed\n", ps
);
2158 /***********************************************************************
2159 * Implementation of IPropertySetStorage
2162 /************************************************************************
2163 * IPropertySetStorage_fnQueryInterface (IUnknown)
2165 * This method forwards to the common QueryInterface implementation
2167 static HRESULT WINAPI
IPropertySetStorage_fnQueryInterface(
2168 IPropertySetStorage
*ppstg
,
2172 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2173 return IStorage_QueryInterface( &This
->base
.IStorage_iface
, riid
, ppvObject
);
2176 /************************************************************************
2177 * IPropertySetStorage_fnAddRef (IUnknown)
2179 * This method forwards to the common AddRef implementation
2181 static ULONG WINAPI
IPropertySetStorage_fnAddRef(
2182 IPropertySetStorage
*ppstg
)
2184 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2185 return IStorage_AddRef( &This
->base
.IStorage_iface
);
2188 /************************************************************************
2189 * IPropertySetStorage_fnRelease (IUnknown)
2191 * This method forwards to the common Release implementation
2193 static ULONG WINAPI
IPropertySetStorage_fnRelease(
2194 IPropertySetStorage
*ppstg
)
2196 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2197 return IStorage_Release( &This
->base
.IStorage_iface
);
2200 /************************************************************************
2201 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2203 static HRESULT WINAPI
IPropertySetStorage_fnCreate(
2204 IPropertySetStorage
*ppstg
,
2206 const CLSID
* pclsid
,
2209 IPropertyStorage
** ppprstg
)
2211 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2212 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2213 IStream
*stm
= NULL
;
2216 TRACE("%p %s %08x %08x %p\n", This
, debugstr_guid(rfmtid
), grfFlags
,
2220 if (grfMode
!= (STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
))
2222 r
= STG_E_INVALIDFLAG
;
2232 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2233 * storage, not a stream. For now, disallow it.
2235 if (grfFlags
& PROPSETFLAG_NONSIMPLE
)
2237 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2238 r
= STG_E_INVALIDFLAG
;
2242 r
= FmtIdToPropStgName(rfmtid
, name
);
2246 r
= IStorage_CreateStream( &This
->base
.IStorage_iface
, name
, grfMode
, 0, 0, &stm
);
2250 r
= PropertyStorage_ConstructEmpty(stm
, rfmtid
, grfFlags
, grfMode
, ppprstg
);
2252 IStream_Release( stm
);
2255 TRACE("returning 0x%08x\n", r
);
2259 /************************************************************************
2260 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2262 static HRESULT WINAPI
IPropertySetStorage_fnOpen(
2263 IPropertySetStorage
*ppstg
,
2266 IPropertyStorage
** ppprstg
)
2268 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2269 IStream
*stm
= NULL
;
2270 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2273 TRACE("%p %s %08x %p\n", This
, debugstr_guid(rfmtid
), grfMode
, ppprstg
);
2276 if (grfMode
!= (STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
) &&
2277 grfMode
!= (STGM_READ
|STGM_SHARE_EXCLUSIVE
))
2279 r
= STG_E_INVALIDFLAG
;
2289 r
= FmtIdToPropStgName(rfmtid
, name
);
2293 r
= IStorage_OpenStream( &This
->base
.IStorage_iface
, name
, 0, grfMode
, 0, &stm
);
2297 r
= PropertyStorage_ConstructFromStream(stm
, rfmtid
, grfMode
, ppprstg
);
2299 IStream_Release( stm
);
2302 TRACE("returning 0x%08x\n", r
);
2306 /************************************************************************
2307 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2309 static HRESULT WINAPI
IPropertySetStorage_fnDelete(
2310 IPropertySetStorage
*ppstg
,
2313 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2314 WCHAR name
[CCH_MAX_PROPSTG_NAME
+ 1];
2317 TRACE("%p %s\n", This
, debugstr_guid(rfmtid
));
2320 return E_INVALIDARG
;
2322 r
= FmtIdToPropStgName(rfmtid
, name
);
2326 return IStorage_DestroyElement(&This
->base
.IStorage_iface
, name
);
2329 /************************************************************************
2330 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2332 static HRESULT WINAPI
IPropertySetStorage_fnEnum(
2333 IPropertySetStorage
*ppstg
,
2334 IEnumSTATPROPSETSTG
** ppenum
)
2336 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2337 return create_EnumSTATPROPSETSTG(This
, ppenum
);
2340 /************************************************************************
2341 * Implement IEnumSTATPROPSETSTG using enumx
2343 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnQueryInterface(
2344 IEnumSTATPROPSETSTG
*iface
,
2348 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2351 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnAddRef(
2352 IEnumSTATPROPSETSTG
*iface
)
2354 return enumx_AddRef((enumx_impl
*)iface
);
2357 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnRelease(
2358 IEnumSTATPROPSETSTG
*iface
)
2360 return enumx_Release((enumx_impl
*)iface
);
2363 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnNext(
2364 IEnumSTATPROPSETSTG
*iface
,
2366 STATPROPSETSTG
*rgelt
,
2367 ULONG
*pceltFetched
)
2369 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2372 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnSkip(
2373 IEnumSTATPROPSETSTG
*iface
,
2376 return enumx_Skip((enumx_impl
*)iface
, celt
);
2379 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnReset(
2380 IEnumSTATPROPSETSTG
*iface
)
2382 return enumx_Reset((enumx_impl
*)iface
);
2385 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnClone(
2386 IEnumSTATPROPSETSTG
*iface
,
2387 IEnumSTATPROPSETSTG
**ppenum
)
2389 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2392 static HRESULT
create_EnumSTATPROPSETSTG(
2394 IEnumSTATPROPSETSTG
** ppenum
)
2396 IStorage
*stg
= &This
->base
.IStorage_iface
;
2397 IEnumSTATSTG
*penum
= NULL
;
2401 STATPROPSETSTG statpss
;
2404 TRACE("%p %p\n", This
, ppenum
);
2406 enumx
= enumx_allocate(&IID_IEnumSTATPROPSETSTG
,
2407 &IEnumSTATPROPSETSTG_Vtbl
,
2408 sizeof (STATPROPSETSTG
),
2409 (IUnknown
*)&This
->base
.IStorage_iface
,
2412 /* add all the property set elements into a list */
2413 r
= IStorage_EnumElements(stg
, 0, NULL
, 0, &penum
);
2416 enumx_Release(enumx
);
2417 return E_OUTOFMEMORY
;
2423 r
= IEnumSTATSTG_Next(penum
, 1, &stat
, &count
);
2430 if (stat
.pwcsName
[0] == 5 && stat
.type
== STGTY_STREAM
)
2432 PropStgNameToFmtId(stat
.pwcsName
, &statpss
.fmtid
);
2433 TRACE("adding %s (%s)\n", debugstr_w(stat
.pwcsName
),
2434 debugstr_guid(&statpss
.fmtid
));
2435 statpss
.mtime
= stat
.mtime
;
2436 statpss
.atime
= stat
.atime
;
2437 statpss
.ctime
= stat
.ctime
;
2438 statpss
.grfFlags
= stat
.grfMode
;
2439 statpss
.clsid
= stat
.clsid
;
2440 enumx_add_element(enumx
, &statpss
);
2442 CoTaskMemFree(stat
.pwcsName
);
2444 IEnumSTATSTG_Release(penum
);
2446 *ppenum
= (IEnumSTATPROPSETSTG
*) enumx
;
2451 /************************************************************************
2452 * Implement IEnumSTATPROPSTG using enumx
2454 static HRESULT WINAPI
IEnumSTATPROPSTG_fnQueryInterface(
2455 IEnumSTATPROPSTG
*iface
,
2459 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2462 static ULONG WINAPI
IEnumSTATPROPSTG_fnAddRef(
2463 IEnumSTATPROPSTG
*iface
)
2465 return enumx_AddRef((enumx_impl
*)iface
);
2468 static ULONG WINAPI
IEnumSTATPROPSTG_fnRelease(
2469 IEnumSTATPROPSTG
*iface
)
2471 return enumx_Release((enumx_impl
*)iface
);
2474 static HRESULT WINAPI
IEnumSTATPROPSTG_fnNext(
2475 IEnumSTATPROPSTG
*iface
,
2478 ULONG
*pceltFetched
)
2480 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2483 static HRESULT WINAPI
IEnumSTATPROPSTG_fnSkip(
2484 IEnumSTATPROPSTG
*iface
,
2487 return enumx_Skip((enumx_impl
*)iface
, celt
);
2490 static HRESULT WINAPI
IEnumSTATPROPSTG_fnReset(
2491 IEnumSTATPROPSTG
*iface
)
2493 return enumx_Reset((enumx_impl
*)iface
);
2496 static HRESULT WINAPI
IEnumSTATPROPSTG_fnClone(
2497 IEnumSTATPROPSTG
*iface
,
2498 IEnumSTATPROPSTG
**ppenum
)
2500 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2503 static void prop_enum_copy_cb(IUnknown
*parent
, void *orig
, void *dest
)
2505 PropertyStorage_impl
*storage
= impl_from_IPropertyStorage((IPropertyStorage
*)parent
);
2506 STATPROPSTG
*src_prop
= orig
;
2507 STATPROPSTG
*dest_prop
= dest
;
2510 dest_prop
->propid
= src_prop
->propid
;
2511 dest_prop
->vt
= src_prop
->vt
;
2512 dest_prop
->lpwstrName
= NULL
;
2514 if (dictionary_find(storage
->propid_to_name
, UlongToPtr(src_prop
->propid
), (void**)&name
))
2516 DWORD size
= (strlenW(name
) + 1) * sizeof(WCHAR
);
2518 dest_prop
->lpwstrName
= CoTaskMemAlloc(size
);
2519 if (!dest_prop
->lpwstrName
) return;
2520 memcpy(dest_prop
->lpwstrName
, name
, size
);
2524 static BOOL
prop_enum_stat(const void *k
, const void *v
, void *extra
, void *arg
)
2526 enumx_impl
*enumx
= arg
;
2527 PROPID propid
= PtrToUlong(k
);
2528 const PROPVARIANT
*prop
= v
;
2531 stat
.lpwstrName
= NULL
;
2532 stat
.propid
= propid
;
2535 enumx_add_element(enumx
, &stat
);
2540 static HRESULT
create_EnumSTATPROPSTG(
2541 PropertyStorage_impl
*This
,
2542 IEnumSTATPROPSTG
** ppenum
)
2546 TRACE("%p %p\n", This
, ppenum
);
2548 enumx
= enumx_allocate(&IID_IEnumSTATPROPSTG
,
2549 &IEnumSTATPROPSTG_Vtbl
,
2550 sizeof (STATPROPSTG
),
2551 (IUnknown
*)&This
->IPropertyStorage_iface
,
2554 dictionary_enumerate(This
->propid_to_prop
, prop_enum_stat
, enumx
);
2556 *ppenum
= (IEnumSTATPROPSTG
*) enumx
;
2561 /***********************************************************************
2564 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
=
2566 IPropertySetStorage_fnQueryInterface
,
2567 IPropertySetStorage_fnAddRef
,
2568 IPropertySetStorage_fnRelease
,
2569 IPropertySetStorage_fnCreate
,
2570 IPropertySetStorage_fnOpen
,
2571 IPropertySetStorage_fnDelete
,
2572 IPropertySetStorage_fnEnum
2575 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
=
2577 IPropertyStorage_fnQueryInterface
,
2578 IPropertyStorage_fnAddRef
,
2579 IPropertyStorage_fnRelease
,
2580 IPropertyStorage_fnReadMultiple
,
2581 IPropertyStorage_fnWriteMultiple
,
2582 IPropertyStorage_fnDeleteMultiple
,
2583 IPropertyStorage_fnReadPropertyNames
,
2584 IPropertyStorage_fnWritePropertyNames
,
2585 IPropertyStorage_fnDeletePropertyNames
,
2586 IPropertyStorage_fnCommit
,
2587 IPropertyStorage_fnRevert
,
2588 IPropertyStorage_fnEnum
,
2589 IPropertyStorage_fnSetTimes
,
2590 IPropertyStorage_fnSetClass
,
2591 IPropertyStorage_fnStat
,
2594 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
=
2596 IEnumSTATPROPSETSTG_fnQueryInterface
,
2597 IEnumSTATPROPSETSTG_fnAddRef
,
2598 IEnumSTATPROPSETSTG_fnRelease
,
2599 IEnumSTATPROPSETSTG_fnNext
,
2600 IEnumSTATPROPSETSTG_fnSkip
,
2601 IEnumSTATPROPSETSTG_fnReset
,
2602 IEnumSTATPROPSETSTG_fnClone
,
2605 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
=
2607 IEnumSTATPROPSTG_fnQueryInterface
,
2608 IEnumSTATPROPSTG_fnAddRef
,
2609 IEnumSTATPROPSTG_fnRelease
,
2610 IEnumSTATPROPSTG_fnNext
,
2611 IEnumSTATPROPSTG_fnSkip
,
2612 IEnumSTATPROPSTG_fnReset
,
2613 IEnumSTATPROPSTG_fnClone
,
2616 /***********************************************************************
2617 * Format ID <-> name conversion
2619 static const WCHAR szSummaryInfo
[] = { 5,'S','u','m','m','a','r','y',
2620 'I','n','f','o','r','m','a','t','i','o','n',0 };
2621 static const WCHAR szDocSummaryInfo
[] = { 5,'D','o','c','u','m','e','n','t',
2622 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2624 #define BITS_PER_BYTE 8
2625 #define CHARMASK 0x1f
2626 #define BITS_IN_CHARMASK 5
2627 #define NUM_ALPHA_CHARS 26
2629 /***********************************************************************
2630 * FmtIdToPropStgName [ole32.@]
2631 * Returns the storage name of the format ID rfmtid.
2633 * rfmtid [I] Format ID for which to return a storage name
2634 * str [O] Storage name associated with rfmtid.
2637 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2640 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2642 HRESULT WINAPI
FmtIdToPropStgName(const FMTID
*rfmtid
, LPOLESTR str
)
2644 static const char fmtMap
[] = "abcdefghijklmnopqrstuvwxyz012345";
2646 TRACE("%s, %p\n", debugstr_guid(rfmtid
), str
);
2648 if (!rfmtid
) return E_INVALIDARG
;
2649 if (!str
) return E_INVALIDARG
;
2651 if (IsEqualGUID(&FMTID_SummaryInformation
, rfmtid
))
2652 lstrcpyW(str
, szSummaryInfo
);
2653 else if (IsEqualGUID(&FMTID_DocSummaryInformation
, rfmtid
))
2654 lstrcpyW(str
, szDocSummaryInfo
);
2655 else if (IsEqualGUID(&FMTID_UserDefinedProperties
, rfmtid
))
2656 lstrcpyW(str
, szDocSummaryInfo
);
2661 ULONG bitsRemaining
= BITS_PER_BYTE
;
2664 for (fmtptr
= (const BYTE
*)rfmtid
; fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
); )
2666 ULONG i
= *fmtptr
>> (BITS_PER_BYTE
- bitsRemaining
);
2668 if (bitsRemaining
>= BITS_IN_CHARMASK
)
2670 *pstr
= (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2671 if (bitsRemaining
== BITS_PER_BYTE
&& *pstr
>= 'a' &&
2675 bitsRemaining
-= BITS_IN_CHARMASK
;
2676 if (bitsRemaining
== 0)
2679 bitsRemaining
= BITS_PER_BYTE
;
2684 if (++fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
))
2685 i
|= *fmtptr
<< bitsRemaining
;
2686 *pstr
++ = (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2687 bitsRemaining
+= BITS_PER_BYTE
- BITS_IN_CHARMASK
;
2692 TRACE("returning %s\n", debugstr_w(str
));
2696 /***********************************************************************
2697 * PropStgNameToFmtId [ole32.@]
2698 * Returns the format ID corresponding to the given name.
2700 * str [I] Storage name to convert to a format ID.
2701 * rfmtid [O] Format ID corresponding to str.
2704 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2705 * a format ID, S_OK otherwise.
2707 HRESULT WINAPI
PropStgNameToFmtId(const LPOLESTR str
, FMTID
*rfmtid
)
2709 HRESULT hr
= STG_E_INVALIDNAME
;
2711 TRACE("%s, %p\n", debugstr_w(str
), rfmtid
);
2713 if (!rfmtid
) return E_INVALIDARG
;
2714 if (!str
) return STG_E_INVALIDNAME
;
2716 if (!lstrcmpiW(str
, szDocSummaryInfo
))
2718 *rfmtid
= FMTID_DocSummaryInformation
;
2721 else if (!lstrcmpiW(str
, szSummaryInfo
))
2723 *rfmtid
= FMTID_SummaryInformation
;
2729 BYTE
*fmtptr
= (BYTE
*)rfmtid
- 1;
2730 const WCHAR
*pstr
= str
;
2732 memset(rfmtid
, 0, sizeof(*rfmtid
));
2733 for (bits
= 0; bits
< sizeof(FMTID
) * BITS_PER_BYTE
;
2734 bits
+= BITS_IN_CHARMASK
)
2736 ULONG bitsUsed
= bits
% BITS_PER_BYTE
, bitsStored
;
2742 if (wc
> NUM_ALPHA_CHARS
)
2745 if (wc
> NUM_ALPHA_CHARS
)
2747 wc
+= 'a' - '0' + NUM_ALPHA_CHARS
;
2750 WARN("invalid character (%d)\n", *pstr
);
2755 *fmtptr
|= wc
<< bitsUsed
;
2756 bitsStored
= min(BITS_PER_BYTE
- bitsUsed
, BITS_IN_CHARMASK
);
2757 if (bitsStored
< BITS_IN_CHARMASK
)
2759 wc
>>= BITS_PER_BYTE
- bitsUsed
;
2760 if (bits
+ bitsStored
== sizeof(FMTID
) * BITS_PER_BYTE
)
2764 WARN("extra bits\n");
2770 *fmtptr
|= (BYTE
)wc
;
2779 #ifdef __i386__ /* thiscall functions are i386-specific */
2781 #define DEFINE_STDCALL_WRAPPER(num,func,args) \
2782 __ASM_STDCALL_FUNC(func, args, \
2786 "movl (%ecx), %eax\n\t" \
2787 "jmp *(4*(" #num "))(%eax)" )
2789 DEFINE_STDCALL_WRAPPER(0,Allocate_PMemoryAllocator
,8)
2790 extern void* __stdcall
Allocate_PMemoryAllocator(void *this, ULONG cbSize
);
2794 static void* __cdecl
Allocate_PMemoryAllocator(void *this, ULONG cbSize
)
2796 void* (__cdecl
*fn
)(void*,ULONG
) = **(void***)this;
2797 return fn(this, cbSize
);
2802 BOOLEAN WINAPI
StgConvertPropertyToVariant(const SERIALIZEDPROPERTYVALUE
* prop
,
2803 USHORT CodePage
, PROPVARIANT
* pvar
, void* pma
)
2807 hr
= PropertyStorage_ReadProperty(pvar
, (const BYTE
*)prop
, CodePage
, Allocate_PMemoryAllocator
, pma
);
2811 FIXME("should raise C++ exception on failure\n");
2812 PropVariantInit(pvar
);
2818 SERIALIZEDPROPERTYVALUE
* WINAPI
StgConvertVariantToProperty(const PROPVARIANT
*pvar
,
2819 USHORT CodePage
, SERIALIZEDPROPERTYVALUE
*pprop
, ULONG
*pcb
, PROPID pid
,
2820 BOOLEAN fReserved
, ULONG
*pcIndirect
)
2822 FIXME("%p,%d,%p,%p,%d,%d,%p\n", pvar
, CodePage
, pprop
, pcb
, pid
, fReserved
, pcIndirect
);
2827 HRESULT WINAPI
StgCreatePropStg(IUnknown
*unk
, REFFMTID fmt
, const CLSID
*clsid
,
2828 DWORD flags
, DWORD reserved
, IPropertyStorage
**prop_stg
)
2834 TRACE("%p %s %s %08x %d %p\n", unk
, debugstr_guid(fmt
), debugstr_guid(clsid
), flags
, reserved
, prop_stg
);
2836 if (!fmt
|| reserved
)
2842 if (flags
& PROPSETFLAG_NONSIMPLE
)
2844 r
= IUnknown_QueryInterface(unk
, &IID_IStorage
, (void **)&stg
);
2848 /* FIXME: if (flags & PROPSETFLAG_NONSIMPLE), we need to create a
2849 * storage, not a stream. For now, disallow it.
2851 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2852 IStorage_Release(stg
);
2853 r
= STG_E_INVALIDFLAG
;
2857 r
= IUnknown_QueryInterface(unk
, &IID_IStream
, (void **)&stm
);
2861 r
= PropertyStorage_ConstructEmpty(stm
, fmt
, flags
,
2862 STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, prop_stg
);
2864 IStream_Release( stm
);
2868 TRACE("returning 0x%08x\n", r
);
2872 HRESULT WINAPI
StgOpenPropStg(IUnknown
*unk
, REFFMTID fmt
, DWORD flags
,
2873 DWORD reserved
, IPropertyStorage
**prop_stg
)
2879 TRACE("%p %s %08x %d %p\n", unk
, debugstr_guid(fmt
), flags
, reserved
, prop_stg
);
2881 if (!fmt
|| reserved
)
2887 if (flags
& PROPSETFLAG_NONSIMPLE
)
2889 r
= IUnknown_QueryInterface(unk
, &IID_IStorage
, (void **)&stg
);
2893 /* FIXME: if (flags & PROPSETFLAG_NONSIMPLE), we need to open a
2894 * storage, not a stream. For now, disallow it.
2896 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2897 IStorage_Release(stg
);
2898 r
= STG_E_INVALIDFLAG
;
2902 r
= IUnknown_QueryInterface(unk
, &IID_IStream
, (void **)&stm
);
2906 r
= PropertyStorage_ConstructFromStream(stm
, fmt
,
2907 STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, prop_stg
);
2909 IStream_Release( stm
);
2913 TRACE("returning 0x%08x\n", r
);