2 * Compound Storage (32 bit version)
3 * Storage implementation
5 * This file contains the compound file implementation
6 * of the storage interface.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Sylvain St-Germain
10 * Copyright 1999 Thuy Nguyen
11 * Copyright 2005 Mike McCormack
12 * Copyright 2005 Juan Lang
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * There's a decent overview of property set storage here:
29 * http://msdn.microsoft.com/archive/en-us/dnarolegen/html/msdn_propset.asp
30 * It's a little bit out of date, and more definitive references are given
31 * below, but it gives the best "big picture" that I've found.
34 * - I don't honor the maximum property set size.
35 * - Certain bogus files could result in reading past the end of a buffer.
36 * - Mac-generated files won't be read correctly, even if they're little
37 * endian, because I disregard whether the generator was a Mac. This means
38 * strings will probably be munged (as I don't understand Mac scripts.)
39 * - Not all PROPVARIANT types are supported.
40 * - User defined properties are not supported, see comment in
41 * PropertyStorage_ReadFromStream
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
60 #include "dictionary.h"
61 #include "storage32.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
66 static inline StorageImpl
*impl_from_IPropertySetStorage( IPropertySetStorage
*iface
)
68 return (StorageImpl
*)((char*)iface
- FIELD_OFFSET(StorageImpl
, base
.pssVtbl
));
71 /* These are documented in MSDN, e.g.
72 * http://msdn.microsoft.com/library/en-us/stg/stg/property_set_header.asp
73 * http://msdn.microsoft.com/library/library/en-us/stg/stg/section.asp
74 * but they don't seem to be in any header file.
76 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
77 #define PROPSETHDR_OSVER_KIND_WIN16 0
78 #define PROPSETHDR_OSVER_KIND_MAC 1
79 #define PROPSETHDR_OSVER_KIND_WIN32 2
81 #define CP_UNICODE 1200
83 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
85 #define CFTAG_WINDOWS (-1L)
86 #define CFTAG_MACINTOSH (-2L)
87 #define CFTAG_FMTID (-3L)
88 #define CFTAG_NODATA 0L
90 /* The format version (and what it implies) is described here:
91 * http://msdn.microsoft.com/library/en-us/stg/stg/format_version.asp
93 typedef struct tagPROPERTYSETHEADER
95 WORD wByteOrder
; /* always 0xfffe */
96 WORD wFormat
; /* can be zero or one */
97 DWORD dwOSVer
; /* OS version of originating system */
98 CLSID clsid
; /* application CLSID */
99 DWORD reserved
; /* always 1 */
102 typedef struct tagFORMATIDOFFSET
105 DWORD dwOffset
; /* from beginning of stream */
108 typedef struct tagPROPERTYSECTIONHEADER
112 } PROPERTYSECTIONHEADER
;
114 typedef struct tagPROPERTYIDOFFSET
117 DWORD dwOffset
; /* from beginning of section */
120 typedef struct tagPropertyStorage_impl PropertyStorage_impl
;
122 /* Initializes the property storage from the stream (and undoes any uncommitted
123 * changes in the process.) Returns an error if there is an error reading or
124 * if the stream format doesn't match what's expected.
126 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*);
128 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*);
130 /* Creates the dictionaries used by the property storage. If successful, all
131 * the dictionaries have been created. If failed, none has been. (This makes
132 * it a bit easier to deal with destroying them.)
134 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*);
136 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*);
138 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
139 * string using PropertyStorage_StringCopy.
141 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
142 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
);
144 /* Copies the string src, which is encoded using code page srcCP, and returns
145 * it in *dst, in the code page specified by targetCP. The returned string is
146 * allocated using CoTaskMemAlloc.
147 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
148 * is CP_UNICODE, the returned string is in fact an LPWSTR.
149 * Returns S_OK on success, something else on failure.
151 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
154 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
;
155 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
;
156 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
;
157 static HRESULT
create_EnumSTATPROPSETSTG(StorageImpl
*, IEnumSTATPROPSETSTG
**);
158 static HRESULT
create_EnumSTATPROPSTG(PropertyStorage_impl
*, IEnumSTATPROPSTG
**);
160 /***********************************************************************
161 * Implementation of IPropertyStorage
163 struct tagPropertyStorage_impl
165 const IPropertyStorageVtbl
*vtbl
;
179 struct dictionary
*name_to_propid
;
180 struct dictionary
*propid_to_name
;
181 struct dictionary
*propid_to_prop
;
184 /************************************************************************
185 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
187 static HRESULT WINAPI
IPropertyStorage_fnQueryInterface(
188 IPropertyStorage
*iface
,
192 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
194 if ( (This
==0) || (ppvObject
==0) )
199 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
200 IsEqualGUID(&IID_IPropertyStorage
, riid
))
202 IPropertyStorage_AddRef(iface
);
203 *ppvObject
= (IPropertyStorage
*)iface
;
207 return E_NOINTERFACE
;
210 /************************************************************************
211 * IPropertyStorage_fnAddRef (IPropertyStorage)
213 static ULONG WINAPI
IPropertyStorage_fnAddRef(
214 IPropertyStorage
*iface
)
216 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
217 return InterlockedIncrement(&This
->ref
);
220 /************************************************************************
221 * IPropertyStorage_fnRelease (IPropertyStorage)
223 static ULONG WINAPI
IPropertyStorage_fnRelease(
224 IPropertyStorage
*iface
)
226 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
229 ref
= InterlockedDecrement(&This
->ref
);
232 TRACE("Destroying %p\n", This
);
234 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
235 IStream_Release(This
->stm
);
236 DeleteCriticalSection(&This
->cs
);
237 PropertyStorage_DestroyDictionaries(This
);
238 HeapFree(GetProcessHeap(), 0, This
);
243 static PROPVARIANT
*PropertyStorage_FindProperty(PropertyStorage_impl
*This
,
246 PROPVARIANT
*ret
= NULL
;
249 dictionary_find(This
->propid_to_prop
, (void *)propid
, (void **)&ret
);
250 TRACE("returning %p\n", ret
);
254 /* Returns NULL if name is NULL. */
255 static PROPVARIANT
*PropertyStorage_FindPropertyByName(
256 PropertyStorage_impl
*This
, LPCWSTR name
)
258 PROPVARIANT
*ret
= NULL
;
264 if (This
->codePage
== CP_UNICODE
)
266 if (dictionary_find(This
->name_to_propid
, name
, (void **)&propid
))
267 ret
= PropertyStorage_FindProperty(This
, (PROPID
)propid
);
272 HRESULT hr
= PropertyStorage_StringCopy((LPCSTR
)name
, CP_UNICODE
,
273 &ansiName
, This
->codePage
);
277 if (dictionary_find(This
->name_to_propid
, ansiName
,
279 ret
= PropertyStorage_FindProperty(This
, (PROPID
)propid
);
280 CoTaskMemFree(ansiName
);
283 TRACE("returning %p\n", ret
);
287 static LPWSTR
PropertyStorage_FindPropertyNameById(PropertyStorage_impl
*This
,
293 dictionary_find(This
->propid_to_name
, (void *)propid
, (void **)&ret
);
294 TRACE("returning %p\n", ret
);
298 /************************************************************************
299 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
301 static HRESULT WINAPI
IPropertyStorage_fnReadMultiple(
302 IPropertyStorage
* iface
,
304 const PROPSPEC rgpspec
[],
305 PROPVARIANT rgpropvar
[])
307 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
311 TRACE("(%p, %ld, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
313 if (cpspec
&& (!rgpspec
|| !rgpropvar
))
315 EnterCriticalSection(&This
->cs
);
316 for (i
= 0; i
< cpspec
; i
++)
318 PropVariantInit(&rgpropvar
[i
]);
319 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
321 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
322 rgpspec
[i
].u
.lpwstr
);
325 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
, GetACP(),
330 switch (rgpspec
[i
].u
.propid
)
333 rgpropvar
[i
].vt
= VT_I2
;
334 rgpropvar
[i
].u
.iVal
= This
->codePage
;
337 rgpropvar
[i
].vt
= VT_I4
;
338 rgpropvar
[i
].u
.lVal
= This
->locale
;
342 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
,
343 rgpspec
[i
].u
.propid
);
346 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
,
347 GetACP(), This
->codePage
);
354 LeaveCriticalSection(&This
->cs
);
358 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
364 TRACE("%s, %p, %ld, %ld\n",
365 srcCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)src
) : debugstr_a(src
), dst
,
374 if (dstCP
== CP_UNICODE
)
375 len
= (strlenW((LPCWSTR
)src
) + 1) * sizeof(WCHAR
);
377 len
= strlen(src
) + 1;
378 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
380 hr
= STG_E_INSUFFICIENTMEMORY
;
382 memcpy(*dst
, src
, len
);
386 if (dstCP
== CP_UNICODE
)
388 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
389 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
391 hr
= STG_E_INSUFFICIENTMEMORY
;
393 MultiByteToWideChar(srcCP
, 0, src
, -1, (LPWSTR
)*dst
, len
);
399 if (srcCP
== CP_UNICODE
)
400 wideStr
= (LPWSTR
)src
;
403 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
404 wideStr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
406 MultiByteToWideChar(srcCP
, 0, src
, -1, wideStr
, len
);
408 hr
= STG_E_INSUFFICIENTMEMORY
;
412 len
= WideCharToMultiByte(dstCP
, 0, wideStr
, -1, NULL
, 0,
414 *dst
= CoTaskMemAlloc(len
);
416 hr
= STG_E_INSUFFICIENTMEMORY
;
419 BOOL defCharUsed
= FALSE
;
421 if (WideCharToMultiByte(dstCP
, 0, wideStr
, -1, *dst
, len
,
422 NULL
, &defCharUsed
) == 0 || defCharUsed
)
426 hr
= HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION
);
430 if (wideStr
!= (LPWSTR
)src
)
431 HeapFree(GetProcessHeap(), 0, wideStr
);
434 TRACE("returning 0x%08lx (%s)\n", hr
,
435 dstCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)*dst
) : debugstr_a(*dst
));
439 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
440 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
)
446 if (propvar
->vt
== VT_LPSTR
)
448 hr
= PropertyStorage_StringCopy(propvar
->u
.pszVal
, srcCP
,
449 &prop
->u
.pszVal
, targetCP
);
454 PropVariantCopy(prop
, propvar
);
458 /* Stores the property with id propid and value propvar into this property
459 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
460 * type is VT_LPSTR, converts the string using lcid as the source code page
461 * and This->codePage as the target code page before storing.
462 * As a side effect, may change This->format to 1 if the type of propvar is
463 * a version 1-only property.
465 static HRESULT
PropertyStorage_StorePropWithId(PropertyStorage_impl
*This
,
466 PROPID propid
, const PROPVARIANT
*propvar
, LCID lcid
)
469 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
, propid
);
473 if (propvar
->vt
& VT_BYREF
|| propvar
->vt
& VT_ARRAY
)
481 case VT_VECTOR
|VT_I1
:
484 TRACE("Setting 0x%08lx to type %d\n", propid
, propvar
->vt
);
487 PropVariantClear(prop
);
488 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
493 prop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
494 sizeof(PROPVARIANT
));
497 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
501 dictionary_insert(This
->propid_to_prop
, (void *)propid
, prop
);
502 if (propid
> This
->highestProp
)
503 This
->highestProp
= propid
;
506 HeapFree(GetProcessHeap(), 0, prop
);
509 hr
= STG_E_INSUFFICIENTMEMORY
;
514 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
515 * srcName is encoded in code page cp, and is converted to This->codePage.
516 * If cp is CP_UNICODE, srcName is actually a unicode string.
517 * As a side effect, may change This->format to 1 if srcName is too long for
518 * a version 0 property storage.
519 * Doesn't validate id.
521 static HRESULT
PropertyStorage_StoreNameWithId(PropertyStorage_impl
*This
,
522 LPCSTR srcName
, LCID cp
, PROPID id
)
529 hr
= PropertyStorage_StringCopy((LPCSTR
)srcName
, cp
, &name
, This
->codePage
);
532 if (This
->codePage
== CP_UNICODE
)
534 if (lstrlenW((LPWSTR
)name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
539 if (strlen(name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
542 TRACE("Adding prop name %s, propid %ld\n",
543 This
->codePage
== CP_UNICODE
? debugstr_w((LPCWSTR
)name
) :
544 debugstr_a(name
), id
);
545 dictionary_insert(This
->name_to_propid
, name
, (void *)id
);
546 dictionary_insert(This
->propid_to_name
, (void *)id
, name
);
551 /************************************************************************
552 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
554 static HRESULT WINAPI
IPropertyStorage_fnWriteMultiple(
555 IPropertyStorage
* iface
,
557 const PROPSPEC rgpspec
[],
558 const PROPVARIANT rgpropvar
[],
559 PROPID propidNameFirst
)
561 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
565 TRACE("(%p, %ld, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
567 if (cpspec
&& (!rgpspec
|| !rgpropvar
))
569 if (!(This
->grfMode
& STGM_READWRITE
))
570 return STG_E_ACCESSDENIED
;
571 EnterCriticalSection(&This
->cs
);
573 This
->originatorOS
= (DWORD
)MAKELONG(LOWORD(GetVersion()),
574 PROPSETHDR_OSVER_KIND_WIN32
) ;
575 for (i
= 0; i
< cpspec
; i
++)
577 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
579 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
580 rgpspec
[i
].u
.lpwstr
);
583 PropVariantCopy(prop
, &rgpropvar
[i
]);
586 /* Note that I don't do the special cases here that I do below,
587 * because naming the special PIDs isn't supported.
589 if (propidNameFirst
< PID_FIRST_USABLE
||
590 propidNameFirst
>= PID_MIN_READONLY
)
591 hr
= STG_E_INVALIDPARAMETER
;
594 PROPID nextId
= max(propidNameFirst
, This
->highestProp
+ 1);
596 hr
= PropertyStorage_StoreNameWithId(This
,
597 (LPCSTR
)rgpspec
[i
].u
.lpwstr
, CP_UNICODE
, nextId
);
599 hr
= PropertyStorage_StorePropWithId(This
, nextId
,
600 &rgpropvar
[i
], GetACP());
606 switch (rgpspec
[i
].u
.propid
)
609 /* Can't set the dictionary */
610 hr
= STG_E_INVALIDPARAMETER
;
613 /* Can only set the code page if nothing else has been set */
614 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
615 rgpropvar
[i
].vt
== VT_I2
)
617 This
->codePage
= rgpropvar
[i
].u
.iVal
;
618 if (This
->codePage
== CP_UNICODE
)
619 This
->grfFlags
&= ~PROPSETFLAG_ANSI
;
621 This
->grfFlags
|= PROPSETFLAG_ANSI
;
624 hr
= STG_E_INVALIDPARAMETER
;
627 /* Can only set the locale if nothing else has been set */
628 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
629 rgpropvar
[i
].vt
== VT_I4
)
630 This
->locale
= rgpropvar
[i
].u
.lVal
;
632 hr
= STG_E_INVALIDPARAMETER
;
635 /* silently ignore like MSDN says */
638 if (rgpspec
[i
].u
.propid
>= PID_MIN_READONLY
)
639 hr
= STG_E_INVALIDPARAMETER
;
641 hr
= PropertyStorage_StorePropWithId(This
,
642 rgpspec
[i
].u
.propid
, &rgpropvar
[i
], GetACP());
646 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
647 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
648 LeaveCriticalSection(&This
->cs
);
652 /************************************************************************
653 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
655 static HRESULT WINAPI
IPropertyStorage_fnDeleteMultiple(
656 IPropertyStorage
* iface
,
658 const PROPSPEC rgpspec
[])
660 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
664 TRACE("(%p, %ld, %p)\n", iface
, cpspec
, rgpspec
);
666 if (cpspec
&& !rgpspec
)
668 if (!(This
->grfMode
& STGM_READWRITE
))
669 return STG_E_ACCESSDENIED
;
671 EnterCriticalSection(&This
->cs
);
673 for (i
= 0; i
< cpspec
; i
++)
675 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
679 if (dictionary_find(This
->name_to_propid
,
680 (void *)rgpspec
[i
].u
.lpwstr
, (void **)&propid
))
681 dictionary_remove(This
->propid_to_prop
, (void *)propid
);
685 if (rgpspec
[i
].u
.propid
>= PID_FIRST_USABLE
&&
686 rgpspec
[i
].u
.propid
< PID_MIN_READONLY
)
687 dictionary_remove(This
->propid_to_prop
,
688 (void *)rgpspec
[i
].u
.propid
);
690 hr
= STG_E_INVALIDPARAMETER
;
693 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
694 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
695 LeaveCriticalSection(&This
->cs
);
699 /************************************************************************
700 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
702 static HRESULT WINAPI
IPropertyStorage_fnReadPropertyNames(
703 IPropertyStorage
* iface
,
705 const PROPID rgpropid
[],
706 LPOLESTR rglpwstrName
[])
708 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
710 HRESULT hr
= S_FALSE
;
712 TRACE("(%p, %ld, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
714 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
716 EnterCriticalSection(&This
->cs
);
717 for (i
= 0; i
< cpropid
&& SUCCEEDED(hr
); i
++)
719 LPWSTR name
= PropertyStorage_FindPropertyNameById(This
, rgpropid
[i
]);
723 size_t len
= lstrlenW(name
);
726 rglpwstrName
[i
] = CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
));
728 memcpy(rglpwstrName
, name
, (len
+ 1) * sizeof(WCHAR
));
730 hr
= STG_E_INSUFFICIENTMEMORY
;
733 rglpwstrName
[i
] = NULL
;
735 LeaveCriticalSection(&This
->cs
);
739 /************************************************************************
740 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
742 static HRESULT WINAPI
IPropertyStorage_fnWritePropertyNames(
743 IPropertyStorage
* iface
,
745 const PROPID rgpropid
[],
746 const LPOLESTR rglpwstrName
[])
748 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
752 TRACE("(%p, %ld, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
754 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
756 if (!(This
->grfMode
& STGM_READWRITE
))
757 return STG_E_ACCESSDENIED
;
759 EnterCriticalSection(&This
->cs
);
761 for (i
= 0; SUCCEEDED(hr
) && i
< cpropid
; i
++)
763 if (rgpropid
[i
] != PID_ILLEGAL
)
764 hr
= PropertyStorage_StoreNameWithId(This
, (LPCSTR
)rglpwstrName
[i
],
765 CP_UNICODE
, rgpropid
[i
]);
767 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
768 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
769 LeaveCriticalSection(&This
->cs
);
773 /************************************************************************
774 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
776 static HRESULT WINAPI
IPropertyStorage_fnDeletePropertyNames(
777 IPropertyStorage
* iface
,
779 const PROPID rgpropid
[])
781 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
785 TRACE("(%p, %ld, %p)\n", iface
, cpropid
, rgpropid
);
787 if (cpropid
&& !rgpropid
)
789 if (!(This
->grfMode
& STGM_READWRITE
))
790 return STG_E_ACCESSDENIED
;
792 EnterCriticalSection(&This
->cs
);
794 for (i
= 0; i
< cpropid
; i
++)
798 if (dictionary_find(This
->propid_to_name
, (void *)rgpropid
[i
],
801 dictionary_remove(This
->propid_to_name
, (void *)rgpropid
[i
]);
802 dictionary_remove(This
->name_to_propid
, name
);
805 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
806 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
807 LeaveCriticalSection(&This
->cs
);
811 /************************************************************************
812 * IPropertyStorage_fnCommit (IPropertyStorage)
814 static HRESULT WINAPI
IPropertyStorage_fnCommit(
815 IPropertyStorage
* iface
,
816 DWORD grfCommitFlags
)
818 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
821 TRACE("(%p, 0x%08lx)\n", iface
, grfCommitFlags
);
823 if (!(This
->grfMode
& STGM_READWRITE
))
824 return STG_E_ACCESSDENIED
;
825 EnterCriticalSection(&This
->cs
);
827 hr
= PropertyStorage_WriteToStream(This
);
830 LeaveCriticalSection(&This
->cs
);
834 /************************************************************************
835 * IPropertyStorage_fnRevert (IPropertyStorage)
837 static HRESULT WINAPI
IPropertyStorage_fnRevert(
838 IPropertyStorage
* iface
)
841 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
843 TRACE("%p\n", iface
);
845 EnterCriticalSection(&This
->cs
);
848 PropertyStorage_DestroyDictionaries(This
);
849 hr
= PropertyStorage_CreateDictionaries(This
);
851 hr
= PropertyStorage_ReadFromStream(This
);
855 LeaveCriticalSection(&This
->cs
);
859 /************************************************************************
860 * IPropertyStorage_fnEnum (IPropertyStorage)
862 static HRESULT WINAPI
IPropertyStorage_fnEnum(
863 IPropertyStorage
* iface
,
864 IEnumSTATPROPSTG
** ppenum
)
866 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
867 return create_EnumSTATPROPSTG(This
, ppenum
);
870 /************************************************************************
871 * IPropertyStorage_fnSetTimes (IPropertyStorage)
873 static HRESULT WINAPI
IPropertyStorage_fnSetTimes(
874 IPropertyStorage
* iface
,
875 const FILETIME
* pctime
,
876 const FILETIME
* patime
,
877 const FILETIME
* pmtime
)
883 /************************************************************************
884 * IPropertyStorage_fnSetClass (IPropertyStorage)
886 static HRESULT WINAPI
IPropertyStorage_fnSetClass(
887 IPropertyStorage
* iface
,
890 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
892 TRACE("%p, %s\n", iface
, debugstr_guid(clsid
));
896 if (!(This
->grfMode
& STGM_READWRITE
))
897 return STG_E_ACCESSDENIED
;
898 memcpy(&This
->clsid
, clsid
, sizeof(This
->clsid
));
900 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
901 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
905 /************************************************************************
906 * IPropertyStorage_fnStat (IPropertyStorage)
908 static HRESULT WINAPI
IPropertyStorage_fnStat(
909 IPropertyStorage
* iface
,
910 STATPROPSETSTG
* statpsstg
)
912 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
916 TRACE("%p, %p\n", iface
, statpsstg
);
921 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
924 memcpy(&statpsstg
->fmtid
, &This
->fmtid
, sizeof(statpsstg
->fmtid
));
925 memcpy(&statpsstg
->clsid
, &This
->clsid
, sizeof(statpsstg
->clsid
));
926 statpsstg
->grfFlags
= This
->grfFlags
;
927 memcpy(&statpsstg
->mtime
, &stat
.mtime
, sizeof(statpsstg
->mtime
));
928 memcpy(&statpsstg
->ctime
, &stat
.ctime
, sizeof(statpsstg
->ctime
));
929 memcpy(&statpsstg
->atime
, &stat
.atime
, sizeof(statpsstg
->atime
));
930 statpsstg
->dwOSVersion
= This
->originatorOS
;
935 static int PropertyStorage_PropNameCompare(const void *a
, const void *b
,
938 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
940 if (This
->codePage
== CP_UNICODE
)
942 TRACE("(%s, %s)\n", debugstr_w(a
), debugstr_w(b
));
943 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
944 return lstrcmpW((LPCWSTR
)a
, (LPCWSTR
)b
);
946 return lstrcmpiW((LPCWSTR
)a
, (LPCWSTR
)b
);
950 TRACE("(%s, %s)\n", debugstr_a(a
), debugstr_a(b
));
951 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
952 return lstrcmpA((LPCSTR
)a
, (LPCSTR
)b
);
954 return lstrcmpiA((LPCSTR
)a
, (LPCSTR
)b
);
958 static void PropertyStorage_PropNameDestroy(void *k
, void *d
, void *extra
)
963 static int PropertyStorage_PropCompare(const void *a
, const void *b
,
966 TRACE("(%ld, %ld)\n", (PROPID
)a
, (PROPID
)b
);
967 return (PROPID
)a
- (PROPID
)b
;
970 static void PropertyStorage_PropertyDestroy(void *k
, void *d
, void *extra
)
972 PropVariantClear((PROPVARIANT
*)d
);
973 HeapFree(GetProcessHeap(), 0, d
);
976 #ifdef WORDS_BIGENDIAN
977 /* Swaps each character in str to or from little endian; assumes the conversion
978 * is symmetric, that is, that le16toh is equivalent to htole16.
980 static void PropertyStorage_ByteSwapString(LPWSTR str
, size_t len
)
984 /* Swap characters to host order.
987 for (i
= 0; i
< len
; i
++)
988 str
[i
] = le16toh(str
[i
]);
991 #define PropertyStorage_ByteSwapString(s, l)
994 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
995 * the entries according to the values of This->codePage and This->locale.
996 * FIXME: there isn't any checking whether the read property extends past the
999 static HRESULT
PropertyStorage_ReadDictionary(PropertyStorage_impl
*This
,
1002 DWORD numEntries
, i
;
1006 assert(This
->name_to_propid
);
1007 assert(This
->propid_to_name
);
1009 StorageUtl_ReadDWord(ptr
, 0, &numEntries
);
1010 TRACE("Reading %ld entries:\n", numEntries
);
1011 ptr
+= sizeof(DWORD
);
1012 for (i
= 0; SUCCEEDED(hr
) && i
< numEntries
; i
++)
1017 StorageUtl_ReadDWord(ptr
, 0, &propid
);
1018 ptr
+= sizeof(PROPID
);
1019 StorageUtl_ReadDWord(ptr
, 0, &cbEntry
);
1020 ptr
+= sizeof(DWORD
);
1021 TRACE("Reading entry with ID 0x%08lx, %ld bytes\n", propid
, cbEntry
);
1022 /* Make sure the source string is NULL-terminated */
1023 if (This
->codePage
!= CP_UNICODE
)
1024 ptr
[cbEntry
- 1] = '\0';
1026 *((LPWSTR
)ptr
+ cbEntry
/ sizeof(WCHAR
)) = '\0';
1027 hr
= PropertyStorage_StoreNameWithId(This
, (char*)ptr
, This
->codePage
, propid
);
1028 if (This
->codePage
== CP_UNICODE
)
1030 /* Unicode entries are padded to DWORD boundaries */
1031 if (cbEntry
% sizeof(DWORD
))
1032 ptr
+= sizeof(DWORD
) - (cbEntry
% sizeof(DWORD
));
1034 ptr
+= sizeof(DWORD
) + cbEntry
;
1039 /* FIXME: there isn't any checking whether the read property extends past the
1040 * end of the buffer.
1042 static HRESULT
PropertyStorage_ReadProperty(PropertyStorage_impl
*This
,
1043 PROPVARIANT
*prop
, const BYTE
*data
)
1049 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->vt
);
1050 data
+= sizeof(DWORD
);
1057 prop
->u
.cVal
= *(const char *)data
;
1058 TRACE("Read char 0x%x ('%c')\n", prop
->u
.cVal
, prop
->u
.cVal
);
1061 prop
->u
.bVal
= *(const UCHAR
*)data
;
1062 TRACE("Read byte 0x%x\n", prop
->u
.bVal
);
1065 StorageUtl_ReadWord(data
, 0, (WORD
*)&prop
->u
.iVal
);
1066 TRACE("Read short %d\n", prop
->u
.iVal
);
1069 StorageUtl_ReadWord(data
, 0, &prop
->u
.uiVal
);
1070 TRACE("Read ushort %d\n", prop
->u
.uiVal
);
1074 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->u
.lVal
);
1075 TRACE("Read long %ld\n", prop
->u
.lVal
);
1079 StorageUtl_ReadDWord(data
, 0, &prop
->u
.ulVal
);
1080 TRACE("Read ulong %ld\n", prop
->u
.ulVal
);
1086 StorageUtl_ReadDWord(data
, 0, &count
);
1087 if (This
->codePage
== CP_UNICODE
&& count
/ 2)
1089 WARN("Unicode string has odd number of bytes\n");
1090 hr
= STG_E_INVALIDHEADER
;
1094 prop
->u
.pszVal
= CoTaskMemAlloc(count
);
1097 memcpy(prop
->u
.pszVal
, data
+ sizeof(DWORD
), count
);
1098 /* This is stored in the code page specified in This->codePage.
1099 * Don't convert it, the caller will just store it as-is.
1101 if (This
->codePage
== CP_UNICODE
)
1103 /* Make sure it's NULL-terminated */
1104 prop
->u
.pszVal
[count
/ sizeof(WCHAR
) - 1] = '\0';
1105 TRACE("Read string value %s\n",
1106 debugstr_w(prop
->u
.pwszVal
));
1110 /* Make sure it's NULL-terminated */
1111 prop
->u
.pszVal
[count
- 1] = '\0';
1112 TRACE("Read string value %s\n", debugstr_a(prop
->u
.pszVal
));
1116 hr
= STG_E_INSUFFICIENTMEMORY
;
1124 StorageUtl_ReadDWord(data
, 0, &count
);
1125 prop
->u
.pwszVal
= CoTaskMemAlloc(count
* sizeof(WCHAR
));
1126 if (prop
->u
.pwszVal
)
1128 memcpy(prop
->u
.pwszVal
, data
+ sizeof(DWORD
),
1129 count
* sizeof(WCHAR
));
1130 /* make sure string is NULL-terminated */
1131 prop
->u
.pwszVal
[count
- 1] = '\0';
1132 PropertyStorage_ByteSwapString(prop
->u
.pwszVal
, count
);
1133 TRACE("Read string value %s\n", debugstr_w(prop
->u
.pwszVal
));
1136 hr
= STG_E_INSUFFICIENTMEMORY
;
1140 StorageUtl_ReadULargeInteger(data
, 0,
1141 (ULARGE_INTEGER
*)&prop
->u
.filetime
);
1145 DWORD len
= 0, type
= 0, tag
= 0;
1147 StorageUtl_ReadDWord(data
, 0, &len
);
1148 StorageUtl_ReadDWord(data
, 4, &tag
);
1149 StorageUtl_ReadDWord(data
, 8, &type
);
1150 if (tag
== CFTAG_WINDOWS
&& len
> 12)
1152 prop
->u
.pclipdata
= CoTaskMemAlloc(sizeof (CLIPDATA
));
1153 prop
->u
.pclipdata
->cbSize
= len
;
1154 prop
->u
.pclipdata
->ulClipFmt
= type
;
1155 prop
->u
.pclipdata
->pClipData
= CoTaskMemAlloc(len
);
1156 memcpy(prop
->u
.pclipdata
->pClipData
, data
+12, len
- 12);
1157 TRACE("returning %p, len %ld\n", prop
->u
.pclipdata
->pClipData
, len
- 12);
1160 hr
= STG_E_INVALIDPARAMETER
;
1164 FIXME("unsupported type %d\n", prop
->vt
);
1165 hr
= STG_E_INVALIDPARAMETER
;
1170 static HRESULT
PropertyStorage_ReadHeaderFromStream(IStream
*stm
,
1171 PROPERTYSETHEADER
*hdr
)
1173 BYTE buf
[sizeof(PROPERTYSETHEADER
)];
1179 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1182 if (count
!= sizeof(buf
))
1184 WARN("read only %ld\n", count
);
1185 hr
= STG_E_INVALIDHEADER
;
1189 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wByteOrder
),
1191 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wFormat
),
1193 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, dwOSVer
),
1195 StorageUtl_ReadGUID(buf
, offsetof(PROPERTYSETHEADER
, clsid
),
1197 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, reserved
),
1201 TRACE("returning 0x%08lx\n", hr
);
1205 static HRESULT
PropertyStorage_ReadFmtIdOffsetFromStream(IStream
*stm
,
1206 FORMATIDOFFSET
*fmt
)
1208 BYTE buf
[sizeof(FORMATIDOFFSET
)];
1214 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1217 if (count
!= sizeof(buf
))
1219 WARN("read only %ld\n", count
);
1220 hr
= STG_E_INVALIDHEADER
;
1224 StorageUtl_ReadGUID(buf
, offsetof(FORMATIDOFFSET
, fmtid
),
1226 StorageUtl_ReadDWord(buf
, offsetof(FORMATIDOFFSET
, dwOffset
),
1230 TRACE("returning 0x%08lx\n", hr
);
1234 static HRESULT
PropertyStorage_ReadSectionHeaderFromStream(IStream
*stm
,
1235 PROPERTYSECTIONHEADER
*hdr
)
1237 BYTE buf
[sizeof(PROPERTYSECTIONHEADER
)];
1243 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1246 if (count
!= sizeof(buf
))
1248 WARN("read only %ld\n", count
);
1249 hr
= STG_E_INVALIDHEADER
;
1253 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1254 cbSection
), &hdr
->cbSection
);
1255 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1256 cProperties
), &hdr
->cProperties
);
1259 TRACE("returning 0x%08lx\n", hr
);
1263 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*This
)
1265 PROPERTYSETHEADER hdr
;
1266 FORMATIDOFFSET fmtOffset
;
1267 PROPERTYSECTIONHEADER sectionHdr
;
1274 DWORD dictOffset
= 0;
1277 This
->dirty
= FALSE
;
1278 This
->highestProp
= 0;
1279 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1282 if (stat
.cbSize
.u
.HighPart
)
1284 WARN("stream too big\n");
1285 /* maximum size varies, but it can't be this big */
1286 hr
= STG_E_INVALIDHEADER
;
1289 if (stat
.cbSize
.u
.LowPart
== 0)
1291 /* empty stream is okay */
1295 else if (stat
.cbSize
.u
.LowPart
< sizeof(PROPERTYSETHEADER
) +
1296 sizeof(FORMATIDOFFSET
))
1298 WARN("stream too small\n");
1299 hr
= STG_E_INVALIDHEADER
;
1303 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1306 hr
= PropertyStorage_ReadHeaderFromStream(This
->stm
, &hdr
);
1307 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1310 if (hdr
.wByteOrder
!= PROPSETHDR_BYTEORDER_MAGIC
|| hdr
.reserved
< 1)
1312 WARN("bad magic in prop set header\n");
1313 hr
= STG_E_INVALIDHEADER
;
1316 if (hdr
.wFormat
!= 0 && hdr
.wFormat
!= 1)
1318 WARN("bad format version %d\n", hdr
.wFormat
);
1319 hr
= STG_E_INVALIDHEADER
;
1322 This
->format
= hdr
.wFormat
;
1323 memcpy(&This
->clsid
, &hdr
.clsid
, sizeof(This
->clsid
));
1324 This
->originatorOS
= hdr
.dwOSVer
;
1325 if (PROPSETHDR_OSVER_KIND(hdr
.dwOSVer
) == PROPSETHDR_OSVER_KIND_MAC
)
1326 WARN("File comes from a Mac, strings will probably be screwed up\n");
1327 hr
= PropertyStorage_ReadFmtIdOffsetFromStream(This
->stm
, &fmtOffset
);
1330 if (fmtOffset
.dwOffset
> stat
.cbSize
.u
.LowPart
)
1332 WARN("invalid offset %ld (stream length is %ld)\n", fmtOffset
.dwOffset
,
1333 stat
.cbSize
.u
.LowPart
);
1334 hr
= STG_E_INVALIDHEADER
;
1337 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1338 * follow not one, but two sections. The first is the standard properties
1339 * for the document summary information, and the second is user-defined
1340 * properties. This is the only case in which multiple sections are
1342 * Reading the second stream isn't implemented yet.
1344 hr
= PropertyStorage_ReadSectionHeaderFromStream(This
->stm
, §ionHdr
);
1347 /* The section size includes the section header, so check it */
1348 if (sectionHdr
.cbSection
< sizeof(PROPERTYSECTIONHEADER
))
1350 WARN("section header too small, got %ld\n", sectionHdr
.cbSection
);
1351 hr
= STG_E_INVALIDHEADER
;
1354 buf
= HeapAlloc(GetProcessHeap(), 0, sectionHdr
.cbSection
-
1355 sizeof(PROPERTYSECTIONHEADER
));
1358 hr
= STG_E_INSUFFICIENTMEMORY
;
1361 hr
= IStream_Read(This
->stm
, buf
, sectionHdr
.cbSection
-
1362 sizeof(PROPERTYSECTIONHEADER
), &count
);
1365 TRACE("Reading %ld properties:\n", sectionHdr
.cProperties
);
1366 for (i
= 0; SUCCEEDED(hr
) && i
< sectionHdr
.cProperties
; i
++)
1368 PROPERTYIDOFFSET
*idOffset
= (PROPERTYIDOFFSET
*)(buf
+
1369 i
* sizeof(PROPERTYIDOFFSET
));
1371 if (idOffset
->dwOffset
< sizeof(PROPERTYSECTIONHEADER
) ||
1372 idOffset
->dwOffset
>= sectionHdr
.cbSection
- sizeof(DWORD
))
1373 hr
= STG_E_INVALIDPOINTER
;
1376 if (idOffset
->propid
>= PID_FIRST_USABLE
&&
1377 idOffset
->propid
< PID_MIN_READONLY
&& idOffset
->propid
>
1379 This
->highestProp
= idOffset
->propid
;
1380 if (idOffset
->propid
== PID_DICTIONARY
)
1382 /* Don't read the dictionary yet, its entries depend on the
1383 * code page. Just store the offset so we know to read it
1386 dictOffset
= idOffset
->dwOffset
;
1387 TRACE("Dictionary offset is %ld\n", dictOffset
);
1393 if (SUCCEEDED(PropertyStorage_ReadProperty(This
, &prop
,
1394 buf
+ idOffset
->dwOffset
- sizeof(PROPERTYSECTIONHEADER
))))
1396 TRACE("Read property with ID 0x%08lx, type %d\n",
1397 idOffset
->propid
, prop
.vt
);
1398 switch(idOffset
->propid
)
1401 if (prop
.vt
== VT_I2
)
1402 This
->codePage
= (UINT
)prop
.u
.iVal
;
1405 if (prop
.vt
== VT_I4
)
1406 This
->locale
= (LCID
)prop
.u
.lVal
;
1409 if (prop
.vt
== VT_I4
&& prop
.u
.lVal
)
1410 This
->grfFlags
|= PROPSETFLAG_CASE_SENSITIVE
;
1411 /* The format should already be 1, but just in case */
1415 hr
= PropertyStorage_StorePropWithId(This
,
1416 idOffset
->propid
, &prop
, This
->codePage
);
1422 if (!This
->codePage
)
1424 /* default to Unicode unless told not to, as specified here:
1425 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
1427 if (This
->grfFlags
& PROPSETFLAG_ANSI
)
1428 This
->codePage
= GetACP();
1430 This
->codePage
= CP_UNICODE
;
1433 This
->locale
= LOCALE_SYSTEM_DEFAULT
;
1434 TRACE("Code page is %d, locale is %ld\n", This
->codePage
, This
->locale
);
1436 hr
= PropertyStorage_ReadDictionary(This
,
1437 buf
+ dictOffset
- sizeof(PROPERTYSECTIONHEADER
));
1440 HeapFree(GetProcessHeap(), 0, buf
);
1443 dictionary_destroy(This
->name_to_propid
);
1444 This
->name_to_propid
= NULL
;
1445 dictionary_destroy(This
->propid_to_name
);
1446 This
->propid_to_name
= NULL
;
1447 dictionary_destroy(This
->propid_to_prop
);
1448 This
->propid_to_prop
= NULL
;
1453 static void PropertyStorage_MakeHeader(PropertyStorage_impl
*This
,
1454 PROPERTYSETHEADER
*hdr
)
1458 StorageUtl_WriteWord((BYTE
*)&hdr
->wByteOrder
, 0,
1459 PROPSETHDR_BYTEORDER_MAGIC
);
1460 StorageUtl_WriteWord((BYTE
*)&hdr
->wFormat
, 0, This
->format
);
1461 StorageUtl_WriteDWord((BYTE
*)&hdr
->dwOSVer
, 0, This
->originatorOS
);
1462 StorageUtl_WriteGUID((BYTE
*)&hdr
->clsid
, 0, &This
->clsid
);
1463 StorageUtl_WriteDWord((BYTE
*)&hdr
->reserved
, 0, 1);
1466 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl
*This
,
1467 FORMATIDOFFSET
*fmtOffset
)
1471 StorageUtl_WriteGUID((BYTE
*)fmtOffset
, 0, &This
->fmtid
);
1472 StorageUtl_WriteDWord((BYTE
*)fmtOffset
, offsetof(FORMATIDOFFSET
, dwOffset
),
1473 sizeof(PROPERTYSETHEADER
) + sizeof(FORMATIDOFFSET
));
1476 static void PropertyStorage_MakeSectionHdr(DWORD cbSection
, DWORD numProps
,
1477 PROPERTYSECTIONHEADER
*hdr
)
1480 StorageUtl_WriteDWord((BYTE
*)hdr
, 0, cbSection
);
1481 StorageUtl_WriteDWord((BYTE
*)hdr
,
1482 offsetof(PROPERTYSECTIONHEADER
, cProperties
), numProps
);
1485 static void PropertyStorage_MakePropertyIdOffset(DWORD propid
, DWORD dwOffset
,
1486 PROPERTYIDOFFSET
*propIdOffset
)
1488 assert(propIdOffset
);
1489 StorageUtl_WriteDWord((BYTE
*)propIdOffset
, 0, propid
);
1490 StorageUtl_WriteDWord((BYTE
*)propIdOffset
,
1491 offsetof(PROPERTYIDOFFSET
, dwOffset
), dwOffset
);
1494 struct DictionaryClosure
1500 static BOOL
PropertyStorage_DictionaryWriter(const void *key
,
1501 const void *value
, void *extra
, void *closure
)
1503 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
1504 struct DictionaryClosure
*c
= (struct DictionaryClosure
*)closure
;
1511 StorageUtl_WriteDWord((LPBYTE
)&propid
, 0, (DWORD
)value
);
1512 c
->hr
= IStream_Write(This
->stm
, &propid
, sizeof(propid
), &count
);
1515 c
->bytesWritten
+= sizeof(DWORD
);
1516 if (This
->codePage
== CP_UNICODE
)
1518 DWORD keyLen
, pad
= 0;
1520 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0,
1521 (lstrlenW((LPWSTR
)key
) + 1) * sizeof(WCHAR
));
1522 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1525 c
->bytesWritten
+= sizeof(DWORD
);
1526 /* Rather than allocate a copy, I'll swap the string to little-endian
1527 * in-place, write it, then swap it back.
1529 PropertyStorage_ByteSwapString(key
, keyLen
);
1530 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1531 PropertyStorage_ByteSwapString(key
, keyLen
);
1534 c
->bytesWritten
+= keyLen
;
1535 if (keyLen
% sizeof(DWORD
))
1537 c
->hr
= IStream_Write(This
->stm
, &pad
,
1538 sizeof(DWORD
) - keyLen
% sizeof(DWORD
), &count
);
1541 c
->bytesWritten
+= sizeof(DWORD
) - keyLen
% sizeof(DWORD
);
1548 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0, strlen((LPCSTR
)key
) + 1);
1549 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1552 c
->bytesWritten
+= sizeof(DWORD
);
1553 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1556 c
->bytesWritten
+= keyLen
;
1559 return SUCCEEDED(c
->hr
);
1562 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1564 /* Writes the dictionary to the stream. Assumes without checking that the
1565 * dictionary isn't empty.
1567 static HRESULT
PropertyStorage_WriteDictionaryToStream(
1568 PropertyStorage_impl
*This
, DWORD
*sectionOffset
)
1572 PROPERTYIDOFFSET propIdOffset
;
1575 struct DictionaryClosure closure
;
1578 assert(sectionOffset
);
1580 /* The dictionary's always the first property written, so seek to its
1583 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
);
1584 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1587 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY
, *sectionOffset
,
1589 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1593 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1594 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1597 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0,
1598 dictionary_num_entries(This
->name_to_propid
));
1599 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1602 *sectionOffset
+= sizeof(dwTemp
);
1605 closure
.bytesWritten
= 0;
1606 dictionary_enumerate(This
->name_to_propid
, PropertyStorage_DictionaryWriter
,
1611 *sectionOffset
+= closure
.bytesWritten
;
1612 if (closure
.bytesWritten
% sizeof(DWORD
))
1614 TRACE("adding %ld bytes of padding\n", sizeof(DWORD
) -
1615 closure
.bytesWritten
% sizeof(DWORD
));
1616 *sectionOffset
+= sizeof(DWORD
) - closure
.bytesWritten
% sizeof(DWORD
);
1623 static HRESULT
PropertyStorage_WritePropertyToStream(PropertyStorage_impl
*This
,
1624 DWORD propNum
, DWORD propid
, PROPVARIANT
*var
, DWORD
*sectionOffset
)
1628 PROPERTYIDOFFSET propIdOffset
;
1630 DWORD dwType
, bytesWritten
;
1634 assert(sectionOffset
);
1636 TRACE("%p, %ld, 0x%08lx, (%d), (%ld)\n", This
, propNum
, propid
, var
->vt
,
1639 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
) +
1640 propNum
* sizeof(PROPERTYIDOFFSET
);
1641 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1644 PropertyStorage_MakePropertyIdOffset(propid
, *sectionOffset
, &propIdOffset
);
1645 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1649 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1650 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1653 StorageUtl_WriteDWord((LPBYTE
)&dwType
, 0, var
->vt
);
1654 hr
= IStream_Write(This
->stm
, &dwType
, sizeof(dwType
), &count
);
1657 *sectionOffset
+= sizeof(dwType
);
1667 hr
= IStream_Write(This
->stm
, &var
->u
.cVal
, sizeof(var
->u
.cVal
),
1669 bytesWritten
= count
;
1676 StorageUtl_WriteWord((LPBYTE
)&wTemp
, 0, var
->u
.iVal
);
1677 hr
= IStream_Write(This
->stm
, &wTemp
, sizeof(wTemp
), &count
);
1678 bytesWritten
= count
;
1686 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, var
->u
.lVal
);
1687 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1688 bytesWritten
= count
;
1695 if (This
->codePage
== CP_UNICODE
)
1696 len
= (lstrlenW(var
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
1698 len
= lstrlenA(var
->u
.pszVal
) + 1;
1699 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1700 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1703 hr
= IStream_Write(This
->stm
, var
->u
.pszVal
, len
, &count
);
1704 bytesWritten
= count
+ sizeof(DWORD
);
1709 DWORD len
= lstrlenW(var
->u
.pwszVal
) + 1, dwTemp
;
1711 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1712 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1715 hr
= IStream_Write(This
->stm
, var
->u
.pwszVal
, len
* sizeof(WCHAR
),
1717 bytesWritten
= count
+ sizeof(DWORD
);
1724 StorageUtl_WriteULargeInteger((BYTE
*)&temp
, 0,
1725 (ULARGE_INTEGER
*)&var
->u
.filetime
);
1726 hr
= IStream_Write(This
->stm
, &temp
, sizeof(FILETIME
), &count
);
1727 bytesWritten
= count
;
1731 FIXME("unsupported type: %d\n", var
->vt
);
1732 return STG_E_INVALIDPARAMETER
;
1737 *sectionOffset
+= bytesWritten
;
1738 if (bytesWritten
% sizeof(DWORD
))
1740 TRACE("adding %ld bytes of padding\n", sizeof(DWORD
) -
1741 bytesWritten
% sizeof(DWORD
));
1742 *sectionOffset
+= sizeof(DWORD
) - bytesWritten
% sizeof(DWORD
);
1750 struct PropertyClosure
1754 DWORD
*sectionOffset
;
1757 static BOOL
PropertyStorage_PropertiesWriter(const void *key
, const void *value
,
1758 void *extra
, void *closure
)
1760 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
1761 struct PropertyClosure
*c
= (struct PropertyClosure
*)closure
;
1767 c
->hr
= PropertyStorage_WritePropertyToStream(This
,
1768 c
->propNum
++, (DWORD
)key
, (PROPVARIANT
*)value
, c
->sectionOffset
);
1769 return SUCCEEDED(c
->hr
);
1772 static HRESULT
PropertyStorage_WritePropertiesToStream(
1773 PropertyStorage_impl
*This
, DWORD startingPropNum
, DWORD
*sectionOffset
)
1775 struct PropertyClosure closure
;
1778 assert(sectionOffset
);
1780 closure
.propNum
= startingPropNum
;
1781 closure
.sectionOffset
= sectionOffset
;
1782 dictionary_enumerate(This
->propid_to_prop
, PropertyStorage_PropertiesWriter
,
1787 static HRESULT
PropertyStorage_WriteHeadersToStream(PropertyStorage_impl
*This
)
1791 LARGE_INTEGER seek
= { {0} };
1792 PROPERTYSETHEADER hdr
;
1793 FORMATIDOFFSET fmtOffset
;
1796 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1799 PropertyStorage_MakeHeader(This
, &hdr
);
1800 hr
= IStream_Write(This
->stm
, &hdr
, sizeof(hdr
), &count
);
1803 if (count
!= sizeof(hdr
))
1805 hr
= STG_E_WRITEFAULT
;
1809 PropertyStorage_MakeFmtIdOffset(This
, &fmtOffset
);
1810 hr
= IStream_Write(This
->stm
, &fmtOffset
, sizeof(fmtOffset
), &count
);
1813 if (count
!= sizeof(fmtOffset
))
1815 hr
= STG_E_WRITEFAULT
;
1824 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*This
)
1826 PROPERTYSECTIONHEADER sectionHdr
;
1830 DWORD numProps
, prop
, sectionOffset
, dwTemp
;
1835 PropertyStorage_WriteHeadersToStream(This
);
1837 /* Count properties. Always at least one property, the code page */
1839 if (dictionary_num_entries(This
->name_to_propid
))
1841 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1843 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1845 numProps
+= dictionary_num_entries(This
->propid_to_prop
);
1847 /* Write section header with 0 bytes right now, I'll adjust it after
1848 * writing properties.
1850 PropertyStorage_MakeSectionHdr(0, numProps
, §ionHdr
);
1851 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1852 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1855 hr
= IStream_Write(This
->stm
, §ionHdr
, sizeof(sectionHdr
), &count
);
1860 sectionOffset
= sizeof(PROPERTYSECTIONHEADER
) +
1861 numProps
* sizeof(PROPERTYIDOFFSET
);
1863 if (dictionary_num_entries(This
->name_to_propid
))
1866 hr
= PropertyStorage_WriteDictionaryToStream(This
, §ionOffset
);
1871 PropVariantInit(&var
);
1874 var
.u
.iVal
= This
->codePage
;
1875 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_CODEPAGE
,
1876 &var
, §ionOffset
);
1880 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1883 var
.u
.lVal
= This
->locale
;
1884 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_LOCALE
,
1885 &var
, §ionOffset
);
1890 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1894 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_BEHAVIOR
,
1895 &var
, §ionOffset
);
1900 hr
= PropertyStorage_WritePropertiesToStream(This
, prop
, §ionOffset
);
1904 /* Now write the byte count of the section */
1905 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1906 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1909 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, sectionOffset
);
1910 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1916 /***********************************************************************
1917 * PropertyStorage_Construct
1919 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*This
)
1922 dictionary_destroy(This
->name_to_propid
);
1923 This
->name_to_propid
= NULL
;
1924 dictionary_destroy(This
->propid_to_name
);
1925 This
->propid_to_name
= NULL
;
1926 dictionary_destroy(This
->propid_to_prop
);
1927 This
->propid_to_prop
= NULL
;
1930 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*This
)
1935 This
->name_to_propid
= dictionary_create(
1936 PropertyStorage_PropNameCompare
, PropertyStorage_PropNameDestroy
,
1938 if (!This
->name_to_propid
)
1940 hr
= STG_E_INSUFFICIENTMEMORY
;
1943 This
->propid_to_name
= dictionary_create(PropertyStorage_PropCompare
,
1945 if (!This
->propid_to_name
)
1947 hr
= STG_E_INSUFFICIENTMEMORY
;
1950 This
->propid_to_prop
= dictionary_create(PropertyStorage_PropCompare
,
1951 PropertyStorage_PropertyDestroy
, This
);
1952 if (!This
->propid_to_prop
)
1954 hr
= STG_E_INSUFFICIENTMEMORY
;
1959 PropertyStorage_DestroyDictionaries(This
);
1963 static HRESULT
PropertyStorage_BaseConstruct(IStream
*stm
,
1964 REFFMTID rfmtid
, DWORD grfMode
, PropertyStorage_impl
**pps
)
1970 *pps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof **pps
);
1972 return E_OUTOFMEMORY
;
1974 (*pps
)->vtbl
= &IPropertyStorage_Vtbl
;
1976 InitializeCriticalSection(&(*pps
)->cs
);
1978 memcpy(&(*pps
)->fmtid
, rfmtid
, sizeof((*pps
)->fmtid
));
1979 (*pps
)->grfMode
= grfMode
;
1981 hr
= PropertyStorage_CreateDictionaries(*pps
);
1984 IStream_Release(stm
);
1985 DeleteCriticalSection(&(*pps
)->cs
);
1986 HeapFree(GetProcessHeap(), 0, *pps
);
1993 static HRESULT
PropertyStorage_ConstructFromStream(IStream
*stm
,
1994 REFFMTID rfmtid
, DWORD grfMode
, IPropertyStorage
** pps
)
1996 PropertyStorage_impl
*ps
;
2000 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2003 hr
= PropertyStorage_ReadFromStream(ps
);
2006 *pps
= (IPropertyStorage
*)ps
;
2007 TRACE("PropertyStorage %p constructed\n", ps
);
2012 PropertyStorage_DestroyDictionaries(ps
);
2013 HeapFree(GetProcessHeap(), 0, ps
);
2019 static HRESULT
PropertyStorage_ConstructEmpty(IStream
*stm
,
2020 REFFMTID rfmtid
, DWORD grfFlags
, DWORD grfMode
, IPropertyStorage
** pps
)
2022 PropertyStorage_impl
*ps
;
2026 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2030 ps
->grfFlags
= grfFlags
;
2031 if (ps
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2033 /* default to Unicode unless told not to, as specified here:
2034 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2036 if (ps
->grfFlags
& PROPSETFLAG_ANSI
)
2037 ps
->codePage
= GetACP();
2039 ps
->codePage
= CP_UNICODE
;
2040 ps
->locale
= LOCALE_SYSTEM_DEFAULT
;
2041 TRACE("Code page is %d, locale is %ld\n", ps
->codePage
, ps
->locale
);
2042 *pps
= (IPropertyStorage
*)ps
;
2043 TRACE("PropertyStorage %p constructed\n", ps
);
2050 /***********************************************************************
2051 * Implementation of IPropertySetStorage
2054 /************************************************************************
2055 * IPropertySetStorage_fnQueryInterface (IUnknown)
2057 * This method forwards to the common QueryInterface implementation
2059 static HRESULT WINAPI
IPropertySetStorage_fnQueryInterface(
2060 IPropertySetStorage
*ppstg
,
2064 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2065 return IStorage_QueryInterface( (IStorage
*)This
, riid
, ppvObject
);
2068 /************************************************************************
2069 * IPropertySetStorage_fnAddRef (IUnknown)
2071 * This method forwards to the common AddRef implementation
2073 static ULONG WINAPI
IPropertySetStorage_fnAddRef(
2074 IPropertySetStorage
*ppstg
)
2076 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2077 return IStorage_AddRef( (IStorage
*)This
);
2080 /************************************************************************
2081 * IPropertySetStorage_fnRelease (IUnknown)
2083 * This method forwards to the common Release implementation
2085 static ULONG WINAPI
IPropertySetStorage_fnRelease(
2086 IPropertySetStorage
*ppstg
)
2088 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2089 return IStorage_Release( (IStorage
*)This
);
2092 /************************************************************************
2093 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2095 static HRESULT WINAPI
IPropertySetStorage_fnCreate(
2096 IPropertySetStorage
*ppstg
,
2098 const CLSID
* pclsid
,
2101 IPropertyStorage
** ppprstg
)
2103 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2104 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2105 IStream
*stm
= NULL
;
2108 TRACE("%p %s %08lx %08lx %p\n", This
, debugstr_guid(rfmtid
), grfFlags
,
2112 if (grfMode
!= (STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
))
2114 r
= STG_E_INVALIDFLAG
;
2124 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2125 * storage, not a stream. For now, disallow it.
2127 if (grfFlags
& PROPSETFLAG_NONSIMPLE
)
2129 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2130 r
= STG_E_INVALIDFLAG
;
2134 r
= FmtIdToPropStgName(rfmtid
, name
);
2138 r
= IStorage_CreateStream( (IStorage
*)This
, name
, grfMode
, 0, 0, &stm
);
2142 r
= PropertyStorage_ConstructEmpty(stm
, rfmtid
, grfFlags
, grfMode
, ppprstg
);
2145 TRACE("returning 0x%08lx\n", r
);
2149 /************************************************************************
2150 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2152 static HRESULT WINAPI
IPropertySetStorage_fnOpen(
2153 IPropertySetStorage
*ppstg
,
2156 IPropertyStorage
** ppprstg
)
2158 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2159 IStream
*stm
= NULL
;
2160 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2163 TRACE("%p %s %08lx %p\n", This
, debugstr_guid(rfmtid
), grfMode
, ppprstg
);
2166 if (grfMode
!= (STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
) &&
2167 grfMode
!= (STGM_READ
|STGM_SHARE_EXCLUSIVE
))
2169 r
= STG_E_INVALIDFLAG
;
2179 r
= FmtIdToPropStgName(rfmtid
, name
);
2183 r
= IStorage_OpenStream((IStorage
*) This
, name
, 0, grfMode
, 0, &stm
);
2187 r
= PropertyStorage_ConstructFromStream(stm
, rfmtid
, grfMode
, ppprstg
);
2190 TRACE("returning 0x%08lx\n", r
);
2194 /************************************************************************
2195 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2197 static HRESULT WINAPI
IPropertySetStorage_fnDelete(
2198 IPropertySetStorage
*ppstg
,
2201 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2202 IStorage
*stg
= NULL
;
2203 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2206 TRACE("%p %s\n", This
, debugstr_guid(rfmtid
));
2209 return E_INVALIDARG
;
2211 r
= FmtIdToPropStgName(rfmtid
, name
);
2215 stg
= (IStorage
*) This
;
2216 return IStorage_DestroyElement(stg
, name
);
2219 /************************************************************************
2220 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2222 static HRESULT WINAPI
IPropertySetStorage_fnEnum(
2223 IPropertySetStorage
*ppstg
,
2224 IEnumSTATPROPSETSTG
** ppenum
)
2226 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2227 return create_EnumSTATPROPSETSTG(This
, ppenum
);
2230 /************************************************************************
2231 * Implement IEnumSTATPROPSETSTG using enumx
2233 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnQueryInterface(
2234 IEnumSTATPROPSETSTG
*iface
,
2238 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2241 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnAddRef(
2242 IEnumSTATPROPSETSTG
*iface
)
2244 return enumx_AddRef((enumx_impl
*)iface
);
2247 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnRelease(
2248 IEnumSTATPROPSETSTG
*iface
)
2250 return enumx_Release((enumx_impl
*)iface
);
2253 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnNext(
2254 IEnumSTATPROPSETSTG
*iface
,
2256 STATPROPSETSTG
*rgelt
,
2257 ULONG
*pceltFetched
)
2259 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2262 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnSkip(
2263 IEnumSTATPROPSETSTG
*iface
,
2266 return enumx_Skip((enumx_impl
*)iface
, celt
);
2269 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnReset(
2270 IEnumSTATPROPSETSTG
*iface
)
2272 return enumx_Reset((enumx_impl
*)iface
);
2275 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnClone(
2276 IEnumSTATPROPSETSTG
*iface
,
2277 IEnumSTATPROPSETSTG
**ppenum
)
2279 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2282 static HRESULT
create_EnumSTATPROPSETSTG(
2284 IEnumSTATPROPSETSTG
** ppenum
)
2286 IStorage
*stg
= (IStorage
*) &This
->base
.lpVtbl
;
2287 IEnumSTATSTG
*penum
= NULL
;
2291 STATPROPSETSTG statpss
;
2294 TRACE("%p %p\n", This
, ppenum
);
2296 enumx
= enumx_allocate(&IID_IEnumSTATPROPSETSTG
,
2297 &IEnumSTATPROPSETSTG_Vtbl
,
2298 sizeof (STATPROPSETSTG
));
2300 /* add all the property set elements into a list */
2301 r
= IStorage_EnumElements(stg
, 0, NULL
, 0, &penum
);
2303 return E_OUTOFMEMORY
;
2308 r
= IEnumSTATSTG_Next(penum
, 1, &stat
, &count
);
2315 if (stat
.pwcsName
[0] == 5 && stat
.type
== STGTY_STREAM
)
2317 PropStgNameToFmtId(stat
.pwcsName
, &statpss
.fmtid
);
2318 TRACE("adding %s (%s)\n", debugstr_w(stat
.pwcsName
),
2319 debugstr_guid(&statpss
.fmtid
));
2320 statpss
.mtime
= stat
.mtime
;
2321 statpss
.atime
= stat
.atime
;
2322 statpss
.ctime
= stat
.ctime
;
2323 statpss
.grfFlags
= stat
.grfMode
;
2324 memcpy(&statpss
.clsid
, &stat
.clsid
, sizeof stat
.clsid
);
2325 enumx_add_element(enumx
, &statpss
);
2327 CoTaskMemFree(stat
.pwcsName
);
2329 IEnumSTATSTG_Release(penum
);
2331 *ppenum
= (IEnumSTATPROPSETSTG
*) enumx
;
2336 /************************************************************************
2337 * Implement IEnumSTATPROPSTG using enumx
2339 static HRESULT WINAPI
IEnumSTATPROPSTG_fnQueryInterface(
2340 IEnumSTATPROPSTG
*iface
,
2344 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2347 static ULONG WINAPI
IEnumSTATPROPSTG_fnAddRef(
2348 IEnumSTATPROPSTG
*iface
)
2350 return enumx_AddRef((enumx_impl
*)iface
);
2353 static ULONG WINAPI
IEnumSTATPROPSTG_fnRelease(
2354 IEnumSTATPROPSTG
*iface
)
2356 return enumx_Release((enumx_impl
*)iface
);
2359 static HRESULT WINAPI
IEnumSTATPROPSTG_fnNext(
2360 IEnumSTATPROPSTG
*iface
,
2363 ULONG
*pceltFetched
)
2365 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2368 static HRESULT WINAPI
IEnumSTATPROPSTG_fnSkip(
2369 IEnumSTATPROPSTG
*iface
,
2372 return enumx_Skip((enumx_impl
*)iface
, celt
);
2375 static HRESULT WINAPI
IEnumSTATPROPSTG_fnReset(
2376 IEnumSTATPROPSTG
*iface
)
2378 return enumx_Reset((enumx_impl
*)iface
);
2381 static HRESULT WINAPI
IEnumSTATPROPSTG_fnClone(
2382 IEnumSTATPROPSTG
*iface
,
2383 IEnumSTATPROPSTG
**ppenum
)
2385 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2388 static BOOL
prop_enum_stat(const void *k
, const void *v
, void *extra
, void *arg
)
2390 enumx_impl
*enumx
= arg
;
2391 PROPID propid
= (PROPID
) k
;
2392 const PROPVARIANT
*prop
= v
;
2395 stat
.lpwstrName
= NULL
;
2396 stat
.propid
= propid
;
2399 enumx_add_element(enumx
, &stat
);
2404 static HRESULT
create_EnumSTATPROPSTG(
2405 PropertyStorage_impl
*This
,
2406 IEnumSTATPROPSTG
** ppenum
)
2410 TRACE("%p %p\n", This
, ppenum
);
2412 enumx
= enumx_allocate(&IID_IEnumSTATPROPSTG
,
2413 &IEnumSTATPROPSTG_Vtbl
,
2414 sizeof (STATPROPSTG
));
2416 dictionary_enumerate(This
->propid_to_prop
, prop_enum_stat
, enumx
);
2418 *ppenum
= (IEnumSTATPROPSTG
*) enumx
;
2423 /***********************************************************************
2426 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
=
2428 IPropertySetStorage_fnQueryInterface
,
2429 IPropertySetStorage_fnAddRef
,
2430 IPropertySetStorage_fnRelease
,
2431 IPropertySetStorage_fnCreate
,
2432 IPropertySetStorage_fnOpen
,
2433 IPropertySetStorage_fnDelete
,
2434 IPropertySetStorage_fnEnum
2437 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
=
2439 IPropertyStorage_fnQueryInterface
,
2440 IPropertyStorage_fnAddRef
,
2441 IPropertyStorage_fnRelease
,
2442 IPropertyStorage_fnReadMultiple
,
2443 IPropertyStorage_fnWriteMultiple
,
2444 IPropertyStorage_fnDeleteMultiple
,
2445 IPropertyStorage_fnReadPropertyNames
,
2446 IPropertyStorage_fnWritePropertyNames
,
2447 IPropertyStorage_fnDeletePropertyNames
,
2448 IPropertyStorage_fnCommit
,
2449 IPropertyStorage_fnRevert
,
2450 IPropertyStorage_fnEnum
,
2451 IPropertyStorage_fnSetTimes
,
2452 IPropertyStorage_fnSetClass
,
2453 IPropertyStorage_fnStat
,
2456 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
=
2458 IEnumSTATPROPSETSTG_fnQueryInterface
,
2459 IEnumSTATPROPSETSTG_fnAddRef
,
2460 IEnumSTATPROPSETSTG_fnRelease
,
2461 IEnumSTATPROPSETSTG_fnNext
,
2462 IEnumSTATPROPSETSTG_fnSkip
,
2463 IEnumSTATPROPSETSTG_fnReset
,
2464 IEnumSTATPROPSETSTG_fnClone
,
2467 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
=
2469 IEnumSTATPROPSTG_fnQueryInterface
,
2470 IEnumSTATPROPSTG_fnAddRef
,
2471 IEnumSTATPROPSTG_fnRelease
,
2472 IEnumSTATPROPSTG_fnNext
,
2473 IEnumSTATPROPSTG_fnSkip
,
2474 IEnumSTATPROPSTG_fnReset
,
2475 IEnumSTATPROPSTG_fnClone
,
2478 /***********************************************************************
2479 * Format ID <-> name conversion
2481 static const WCHAR szSummaryInfo
[] = { 5,'S','u','m','m','a','r','y',
2482 'I','n','f','o','r','m','a','t','i','o','n',0 };
2483 static const WCHAR szDocSummaryInfo
[] = { 5,'D','o','c','u','m','e','n','t',
2484 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2486 #define BITS_PER_BYTE 8
2487 #define CHARMASK 0x1f
2488 #define BITS_IN_CHARMASK 5
2489 #define NUM_ALPHA_CHARS 26
2491 /***********************************************************************
2492 * FmtIdToPropStgName [ole32.@]
2493 * Returns the storage name of the format ID rfmtid.
2495 * rfmtid [I] Format ID for which to return a storage name
2496 * str [O] Storage name associated with rfmtid.
2499 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2502 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2503 * Based on the algorithm described here:
2504 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2506 HRESULT WINAPI
FmtIdToPropStgName(const FMTID
*rfmtid
, LPOLESTR str
)
2508 static const char fmtMap
[] = "abcdefghijklmnopqrstuvwxyz012345";
2510 TRACE("%s, %p\n", debugstr_guid(rfmtid
), str
);
2512 if (!rfmtid
) return E_INVALIDARG
;
2513 if (!str
) return E_INVALIDARG
;
2515 if (IsEqualGUID(&FMTID_SummaryInformation
, rfmtid
))
2516 lstrcpyW(str
, szSummaryInfo
);
2517 else if (IsEqualGUID(&FMTID_DocSummaryInformation
, rfmtid
))
2518 lstrcpyW(str
, szDocSummaryInfo
);
2519 else if (IsEqualGUID(&FMTID_UserDefinedProperties
, rfmtid
))
2520 lstrcpyW(str
, szDocSummaryInfo
);
2525 ULONG bitsRemaining
= BITS_PER_BYTE
;
2528 for (fmtptr
= (BYTE
*)rfmtid
; fmtptr
< (BYTE
*)rfmtid
+ sizeof(FMTID
); )
2530 ULONG i
= *fmtptr
>> (BITS_PER_BYTE
- bitsRemaining
);
2532 if (bitsRemaining
>= BITS_IN_CHARMASK
)
2534 *pstr
= (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2535 if (bitsRemaining
== BITS_PER_BYTE
&& *pstr
>= 'a' &&
2539 bitsRemaining
-= BITS_IN_CHARMASK
;
2540 if (bitsRemaining
== 0)
2543 bitsRemaining
= BITS_PER_BYTE
;
2548 if (++fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
))
2549 i
|= *fmtptr
<< bitsRemaining
;
2550 *pstr
++ = (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2551 bitsRemaining
+= BITS_PER_BYTE
- BITS_IN_CHARMASK
;
2556 TRACE("returning %s\n", debugstr_w(str
));
2560 /***********************************************************************
2561 * PropStgNameToFmtId [ole32.@]
2562 * Returns the format ID corresponding to the given name.
2564 * str [I] Storage name to convert to a format ID.
2565 * rfmtid [O] Format ID corresponding to str.
2568 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2569 * a format ID, S_OK otherwise.
2572 * Based on the algorithm described here:
2573 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2575 HRESULT WINAPI
PropStgNameToFmtId(const LPOLESTR str
, FMTID
*rfmtid
)
2577 HRESULT hr
= STG_E_INVALIDNAME
;
2579 TRACE("%s, %p\n", debugstr_w(str
), rfmtid
);
2581 if (!rfmtid
) return E_INVALIDARG
;
2582 if (!str
) return STG_E_INVALIDNAME
;
2584 if (!lstrcmpiW(str
, szDocSummaryInfo
))
2586 memcpy(rfmtid
, &FMTID_DocSummaryInformation
, sizeof(*rfmtid
));
2589 else if (!lstrcmpiW(str
, szSummaryInfo
))
2591 memcpy(rfmtid
, &FMTID_SummaryInformation
, sizeof(*rfmtid
));
2597 BYTE
*fmtptr
= (BYTE
*)rfmtid
- 1;
2598 const WCHAR
*pstr
= str
;
2600 memset(rfmtid
, 0, sizeof(*rfmtid
));
2601 for (bits
= 0; bits
< sizeof(FMTID
) * BITS_PER_BYTE
;
2602 bits
+= BITS_IN_CHARMASK
)
2604 ULONG bitsUsed
= bits
% BITS_PER_BYTE
, bitsStored
;
2610 if (wc
> NUM_ALPHA_CHARS
)
2613 if (wc
> NUM_ALPHA_CHARS
)
2615 wc
+= 'a' - '0' + NUM_ALPHA_CHARS
;
2618 WARN("invalid character (%d)\n", *pstr
);
2623 *fmtptr
|= wc
<< bitsUsed
;
2624 bitsStored
= min(BITS_PER_BYTE
- bitsUsed
, BITS_IN_CHARMASK
);
2625 if (bitsStored
< BITS_IN_CHARMASK
)
2627 wc
>>= BITS_PER_BYTE
- bitsUsed
;
2628 if (bits
+ bitsStored
== sizeof(FMTID
) * BITS_PER_BYTE
)
2632 WARN("extra bits\n");
2638 *fmtptr
|= (BYTE
)wc
;