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
);
64 static inline StorageImpl
*impl_from_IPropertySetStorage( IPropertySetStorage
*iface
)
66 return CONTAINING_RECORD(iface
, StorageImpl
, base
.IPropertySetStorage_iface
);
69 /* These are documented in MSDN,
70 * but they don't seem to be in any header file.
72 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
73 #define PROPSETHDR_OSVER_KIND_WIN16 0
74 #define PROPSETHDR_OSVER_KIND_MAC 1
75 #define PROPSETHDR_OSVER_KIND_WIN32 2
77 #define CP_UNICODE 1200
79 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
81 #define CFTAG_WINDOWS (-1L)
82 #define CFTAG_MACINTOSH (-2L)
83 #define CFTAG_FMTID (-3L)
84 #define CFTAG_NODATA 0L
86 typedef struct tagPROPERTYSETHEADER
88 WORD wByteOrder
; /* always 0xfffe */
89 WORD wFormat
; /* can be zero or one */
90 DWORD dwOSVer
; /* OS version of originating system */
91 CLSID clsid
; /* application CLSID */
92 DWORD reserved
; /* always 1 */
95 typedef struct tagFORMATIDOFFSET
98 DWORD dwOffset
; /* from beginning of stream */
101 typedef struct tagPROPERTYSECTIONHEADER
105 } PROPERTYSECTIONHEADER
;
107 typedef struct tagPROPERTYIDOFFSET
110 DWORD dwOffset
; /* from beginning of section */
113 typedef struct tagPropertyStorage_impl PropertyStorage_impl
;
115 /* Initializes the property storage from the stream (and undoes any uncommitted
116 * changes in the process.) Returns an error if there is an error reading or
117 * if the stream format doesn't match what's expected.
119 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*);
121 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*);
123 /* Creates the dictionaries used by the property storage. If successful, all
124 * the dictionaries have been created. If failed, none has been. (This makes
125 * it a bit easier to deal with destroying them.)
127 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*);
129 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*);
131 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
132 * string using PropertyStorage_StringCopy.
134 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
135 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
);
137 /* Copies the string src, which is encoded using code page srcCP, and returns
138 * it in *dst, in the code page specified by targetCP. The returned string is
139 * allocated using CoTaskMemAlloc.
140 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
141 * is CP_UNICODE, the returned string is in fact an LPWSTR.
142 * Returns S_OK on success, something else on failure.
144 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
147 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
;
148 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
;
149 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
;
150 static HRESULT
create_EnumSTATPROPSETSTG(StorageImpl
*, IEnumSTATPROPSETSTG
**);
151 static HRESULT
create_EnumSTATPROPSTG(PropertyStorage_impl
*, IEnumSTATPROPSTG
**);
153 /***********************************************************************
154 * Implementation of IPropertyStorage
156 struct tagPropertyStorage_impl
158 IPropertyStorage IPropertyStorage_iface
;
172 struct dictionary
*name_to_propid
;
173 struct dictionary
*propid_to_name
;
174 struct dictionary
*propid_to_prop
;
177 static inline PropertyStorage_impl
*impl_from_IPropertyStorage(IPropertyStorage
*iface
)
179 return CONTAINING_RECORD(iface
, PropertyStorage_impl
, IPropertyStorage_iface
);
182 /************************************************************************
183 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
185 static HRESULT WINAPI
IPropertyStorage_fnQueryInterface(
186 IPropertyStorage
*iface
,
190 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
192 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppvObject
);
199 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
200 IsEqualGUID(&IID_IPropertyStorage
, riid
))
202 IPropertyStorage_AddRef(iface
);
207 return E_NOINTERFACE
;
210 /************************************************************************
211 * IPropertyStorage_fnAddRef (IPropertyStorage)
213 static ULONG WINAPI
IPropertyStorage_fnAddRef(
214 IPropertyStorage
*iface
)
216 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
217 return InterlockedIncrement(&This
->ref
);
220 /************************************************************************
221 * IPropertyStorage_fnRelease (IPropertyStorage)
223 static ULONG WINAPI
IPropertyStorage_fnRelease(
224 IPropertyStorage
*iface
)
226 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
229 ref
= InterlockedDecrement(&This
->ref
);
232 TRACE("Destroying %p\n", This
);
234 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
235 IStream_Release(This
->stm
);
236 This
->cs
.DebugInfo
->Spare
[0] = 0;
237 DeleteCriticalSection(&This
->cs
);
238 PropertyStorage_DestroyDictionaries(This
);
239 HeapFree(GetProcessHeap(), 0, This
);
244 static PROPVARIANT
*PropertyStorage_FindProperty(PropertyStorage_impl
*This
,
247 PROPVARIANT
*ret
= NULL
;
249 dictionary_find(This
->propid_to_prop
, UlongToPtr(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
;
263 if (This
->codePage
== CP_UNICODE
)
265 if (dictionary_find(This
->name_to_propid
, name
, &propid
))
266 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
271 HRESULT hr
= PropertyStorage_StringCopy((LPCSTR
)name
, CP_UNICODE
,
272 &ansiName
, This
->codePage
);
276 if (dictionary_find(This
->name_to_propid
, ansiName
, &propid
))
277 ret
= PropertyStorage_FindProperty(This
, PtrToUlong(propid
));
278 CoTaskMemFree(ansiName
);
281 TRACE("returning %p\n", ret
);
285 static LPWSTR
PropertyStorage_FindPropertyNameById(PropertyStorage_impl
*This
,
290 dictionary_find(This
->propid_to_name
, UlongToPtr(propid
), (void **)&ret
);
291 TRACE("returning %p\n", ret
);
295 /************************************************************************
296 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
298 static HRESULT WINAPI
IPropertyStorage_fnReadMultiple(
299 IPropertyStorage
* iface
,
301 const PROPSPEC rgpspec
[],
302 PROPVARIANT rgpropvar
[])
304 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
308 TRACE("(%p, %d, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
312 if (!rgpspec
|| !rgpropvar
)
314 EnterCriticalSection(&This
->cs
);
315 for (i
= 0; i
< cpspec
; i
++)
317 PropVariantInit(&rgpropvar
[i
]);
318 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
320 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
321 rgpspec
[i
].u
.lpwstr
);
324 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
, GetACP(),
329 switch (rgpspec
[i
].u
.propid
)
332 rgpropvar
[i
].vt
= VT_I2
;
333 rgpropvar
[i
].u
.iVal
= This
->codePage
;
336 rgpropvar
[i
].vt
= VT_I4
;
337 rgpropvar
[i
].u
.lVal
= This
->locale
;
341 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
,
342 rgpspec
[i
].u
.propid
);
345 PropertyStorage_PropVariantCopy(&rgpropvar
[i
], prop
,
346 GetACP(), This
->codePage
);
353 LeaveCriticalSection(&This
->cs
);
357 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
363 TRACE("%s, %p, %d, %d\n",
364 srcCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)src
) : debugstr_a(src
), dst
,
373 if (dstCP
== CP_UNICODE
)
374 len
= (strlenW((LPCWSTR
)src
) + 1) * sizeof(WCHAR
);
376 len
= strlen(src
) + 1;
377 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
379 hr
= STG_E_INSUFFICIENTMEMORY
;
381 memcpy(*dst
, src
, len
);
385 if (dstCP
== CP_UNICODE
)
387 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
388 *dst
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
390 hr
= STG_E_INSUFFICIENTMEMORY
;
392 MultiByteToWideChar(srcCP
, 0, src
, -1, (LPWSTR
)*dst
, len
);
396 LPCWSTR wideStr
= NULL
;
397 LPWSTR wideStr_tmp
= NULL
;
399 if (srcCP
== CP_UNICODE
)
400 wideStr
= (LPCWSTR
)src
;
403 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
404 wideStr_tmp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
407 MultiByteToWideChar(srcCP
, 0, src
, -1, wideStr_tmp
, len
);
408 wideStr
= wideStr_tmp
;
411 hr
= STG_E_INSUFFICIENTMEMORY
;
415 len
= WideCharToMultiByte(dstCP
, 0, wideStr
, -1, NULL
, 0,
417 *dst
= CoTaskMemAlloc(len
);
419 hr
= STG_E_INSUFFICIENTMEMORY
;
422 BOOL defCharUsed
= FALSE
;
424 if (WideCharToMultiByte(dstCP
, 0, wideStr
, -1, *dst
, len
,
425 NULL
, &defCharUsed
) == 0 || defCharUsed
)
429 hr
= HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION
);
433 HeapFree(GetProcessHeap(), 0, wideStr_tmp
);
436 TRACE("returning 0x%08x (%s)\n", hr
,
437 dstCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)*dst
) : debugstr_a(*dst
));
441 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
442 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
)
448 if (propvar
->vt
== VT_LPSTR
)
450 hr
= PropertyStorage_StringCopy(propvar
->u
.pszVal
, srcCP
,
451 &prop
->u
.pszVal
, targetCP
);
456 PropVariantCopy(prop
, propvar
);
460 /* Stores the property with id propid and value propvar into this property
461 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
462 * type is VT_LPSTR, converts the string using lcid as the source code page
463 * and This->codePage as the target code page before storing.
464 * As a side effect, may change This->format to 1 if the type of propvar is
465 * a version 1-only property.
467 static HRESULT
PropertyStorage_StorePropWithId(PropertyStorage_impl
*This
,
468 PROPID propid
, const PROPVARIANT
*propvar
, LCID lcid
)
471 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
, propid
);
474 if (propvar
->vt
& VT_BYREF
|| propvar
->vt
& VT_ARRAY
)
482 case VT_VECTOR
|VT_I1
:
485 TRACE("Setting 0x%08x to type %d\n", propid
, propvar
->vt
);
488 PropVariantClear(prop
);
489 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
494 prop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
495 sizeof(PROPVARIANT
));
498 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
502 dictionary_insert(This
->propid_to_prop
, UlongToPtr(propid
), prop
);
503 if (propid
> This
->highestProp
)
504 This
->highestProp
= propid
;
507 HeapFree(GetProcessHeap(), 0, prop
);
510 hr
= STG_E_INSUFFICIENTMEMORY
;
515 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
516 * srcName is encoded in code page cp, and is converted to This->codePage.
517 * If cp is CP_UNICODE, srcName is actually a unicode string.
518 * As a side effect, may change This->format to 1 if srcName is too long for
519 * a version 0 property storage.
520 * Doesn't validate id.
522 static HRESULT
PropertyStorage_StoreNameWithId(PropertyStorage_impl
*This
,
523 LPCSTR srcName
, LCID cp
, PROPID id
)
530 hr
= PropertyStorage_StringCopy(srcName
, cp
, &name
, This
->codePage
);
533 if (This
->codePage
== CP_UNICODE
)
535 if (lstrlenW((LPWSTR
)name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
540 if (strlen(name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
543 TRACE("Adding prop name %s, propid %d\n",
544 This
->codePage
== CP_UNICODE
? debugstr_w((LPCWSTR
)name
) :
545 debugstr_a(name
), id
);
546 dictionary_insert(This
->name_to_propid
, name
, UlongToPtr(id
));
547 dictionary_insert(This
->propid_to_name
, UlongToPtr(id
), name
);
552 /************************************************************************
553 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
555 static HRESULT WINAPI
IPropertyStorage_fnWriteMultiple(
556 IPropertyStorage
* iface
,
558 const PROPSPEC rgpspec
[],
559 const PROPVARIANT rgpropvar
[],
560 PROPID propidNameFirst
)
562 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
566 TRACE("(%p, %d, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
568 if (cpspec
&& (!rgpspec
|| !rgpropvar
))
570 if (!(This
->grfMode
& STGM_READWRITE
))
571 return STG_E_ACCESSDENIED
;
572 EnterCriticalSection(&This
->cs
);
574 This
->originatorOS
= (DWORD
)MAKELONG(LOWORD(GetVersion()),
575 PROPSETHDR_OSVER_KIND_WIN32
) ;
576 for (i
= 0; i
< cpspec
; i
++)
578 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
580 PROPVARIANT
*prop
= PropertyStorage_FindPropertyByName(This
,
581 rgpspec
[i
].u
.lpwstr
);
584 PropVariantCopy(prop
, &rgpropvar
[i
]);
587 /* Note that I don't do the special cases here that I do below,
588 * because naming the special PIDs isn't supported.
590 if (propidNameFirst
< PID_FIRST_USABLE
||
591 propidNameFirst
>= PID_MIN_READONLY
)
592 hr
= STG_E_INVALIDPARAMETER
;
595 PROPID nextId
= max(propidNameFirst
, This
->highestProp
+ 1);
597 hr
= PropertyStorage_StoreNameWithId(This
,
598 (LPCSTR
)rgpspec
[i
].u
.lpwstr
, CP_UNICODE
, nextId
);
600 hr
= PropertyStorage_StorePropWithId(This
, nextId
,
601 &rgpropvar
[i
], GetACP());
607 switch (rgpspec
[i
].u
.propid
)
610 /* Can't set the dictionary */
611 hr
= STG_E_INVALIDPARAMETER
;
614 /* Can only set the code page if nothing else has been set */
615 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
616 rgpropvar
[i
].vt
== VT_I2
)
618 This
->codePage
= rgpropvar
[i
].u
.iVal
;
619 if (This
->codePage
== CP_UNICODE
)
620 This
->grfFlags
&= ~PROPSETFLAG_ANSI
;
622 This
->grfFlags
|= PROPSETFLAG_ANSI
;
625 hr
= STG_E_INVALIDPARAMETER
;
628 /* Can only set the locale if nothing else has been set */
629 if (dictionary_num_entries(This
->propid_to_prop
) == 0 &&
630 rgpropvar
[i
].vt
== VT_I4
)
631 This
->locale
= rgpropvar
[i
].u
.lVal
;
633 hr
= STG_E_INVALIDPARAMETER
;
636 /* silently ignore like MSDN says */
639 if (rgpspec
[i
].u
.propid
>= PID_MIN_READONLY
)
640 hr
= STG_E_INVALIDPARAMETER
;
642 hr
= PropertyStorage_StorePropWithId(This
,
643 rgpspec
[i
].u
.propid
, &rgpropvar
[i
], GetACP());
647 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
648 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
649 LeaveCriticalSection(&This
->cs
);
653 /************************************************************************
654 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
656 static HRESULT WINAPI
IPropertyStorage_fnDeleteMultiple(
657 IPropertyStorage
* iface
,
659 const PROPSPEC rgpspec
[])
661 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
665 TRACE("(%p, %d, %p)\n", iface
, cpspec
, rgpspec
);
667 if (cpspec
&& !rgpspec
)
669 if (!(This
->grfMode
& STGM_READWRITE
))
670 return STG_E_ACCESSDENIED
;
672 EnterCriticalSection(&This
->cs
);
674 for (i
= 0; i
< cpspec
; i
++)
676 if (rgpspec
[i
].ulKind
== PRSPEC_LPWSTR
)
680 if (dictionary_find(This
->name_to_propid
, rgpspec
[i
].u
.lpwstr
, &propid
))
681 dictionary_remove(This
->propid_to_prop
, 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
, UlongToPtr(rgpspec
[i
].u
.propid
));
689 hr
= STG_E_INVALIDPARAMETER
;
692 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
693 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
694 LeaveCriticalSection(&This
->cs
);
698 /************************************************************************
699 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
701 static HRESULT WINAPI
IPropertyStorage_fnReadPropertyNames(
702 IPropertyStorage
* iface
,
704 const PROPID rgpropid
[],
705 LPOLESTR rglpwstrName
[])
707 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
709 HRESULT hr
= S_FALSE
;
711 TRACE("(%p, %d, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
713 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
715 EnterCriticalSection(&This
->cs
);
716 for (i
= 0; i
< cpropid
&& SUCCEEDED(hr
); i
++)
718 LPWSTR name
= PropertyStorage_FindPropertyNameById(This
, rgpropid
[i
]);
722 size_t len
= lstrlenW(name
);
725 rglpwstrName
[i
] = CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
));
727 memcpy(rglpwstrName
[i
], name
, (len
+ 1) * sizeof(WCHAR
));
729 hr
= STG_E_INSUFFICIENTMEMORY
;
732 rglpwstrName
[i
] = NULL
;
734 LeaveCriticalSection(&This
->cs
);
738 /************************************************************************
739 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
741 static HRESULT WINAPI
IPropertyStorage_fnWritePropertyNames(
742 IPropertyStorage
* iface
,
744 const PROPID rgpropid
[],
745 const LPOLESTR rglpwstrName
[])
747 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
751 TRACE("(%p, %d, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
753 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
755 if (!(This
->grfMode
& STGM_READWRITE
))
756 return STG_E_ACCESSDENIED
;
758 EnterCriticalSection(&This
->cs
);
760 for (i
= 0; SUCCEEDED(hr
) && i
< cpropid
; i
++)
762 if (rgpropid
[i
] != PID_ILLEGAL
)
763 hr
= PropertyStorage_StoreNameWithId(This
, (LPCSTR
)rglpwstrName
[i
],
764 CP_UNICODE
, rgpropid
[i
]);
766 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
767 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
768 LeaveCriticalSection(&This
->cs
);
772 /************************************************************************
773 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
775 static HRESULT WINAPI
IPropertyStorage_fnDeletePropertyNames(
776 IPropertyStorage
* iface
,
778 const PROPID rgpropid
[])
780 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
784 TRACE("(%p, %d, %p)\n", iface
, cpropid
, rgpropid
);
786 if (cpropid
&& !rgpropid
)
788 if (!(This
->grfMode
& STGM_READWRITE
))
789 return STG_E_ACCESSDENIED
;
791 EnterCriticalSection(&This
->cs
);
793 for (i
= 0; i
< cpropid
; i
++)
797 if (dictionary_find(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]), (void **)&name
))
799 dictionary_remove(This
->propid_to_name
, UlongToPtr(rgpropid
[i
]));
800 dictionary_remove(This
->name_to_propid
, name
);
803 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
804 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
805 LeaveCriticalSection(&This
->cs
);
809 /************************************************************************
810 * IPropertyStorage_fnCommit (IPropertyStorage)
812 static HRESULT WINAPI
IPropertyStorage_fnCommit(
813 IPropertyStorage
* iface
,
814 DWORD grfCommitFlags
)
816 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
819 TRACE("(%p, 0x%08x)\n", iface
, grfCommitFlags
);
821 if (!(This
->grfMode
& STGM_READWRITE
))
822 return STG_E_ACCESSDENIED
;
823 EnterCriticalSection(&This
->cs
);
825 hr
= PropertyStorage_WriteToStream(This
);
828 LeaveCriticalSection(&This
->cs
);
832 /************************************************************************
833 * IPropertyStorage_fnRevert (IPropertyStorage)
835 static HRESULT WINAPI
IPropertyStorage_fnRevert(
836 IPropertyStorage
* iface
)
839 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
841 TRACE("%p\n", iface
);
843 EnterCriticalSection(&This
->cs
);
846 PropertyStorage_DestroyDictionaries(This
);
847 hr
= PropertyStorage_CreateDictionaries(This
);
849 hr
= PropertyStorage_ReadFromStream(This
);
853 LeaveCriticalSection(&This
->cs
);
857 /************************************************************************
858 * IPropertyStorage_fnEnum (IPropertyStorage)
860 static HRESULT WINAPI
IPropertyStorage_fnEnum(
861 IPropertyStorage
* iface
,
862 IEnumSTATPROPSTG
** ppenum
)
864 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
865 return create_EnumSTATPROPSTG(This
, ppenum
);
868 /************************************************************************
869 * IPropertyStorage_fnSetTimes (IPropertyStorage)
871 static HRESULT WINAPI
IPropertyStorage_fnSetTimes(
872 IPropertyStorage
* iface
,
873 const FILETIME
* pctime
,
874 const FILETIME
* patime
,
875 const FILETIME
* pmtime
)
881 /************************************************************************
882 * IPropertyStorage_fnSetClass (IPropertyStorage)
884 static HRESULT WINAPI
IPropertyStorage_fnSetClass(
885 IPropertyStorage
* iface
,
888 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
890 TRACE("%p, %s\n", iface
, debugstr_guid(clsid
));
894 if (!(This
->grfMode
& STGM_READWRITE
))
895 return STG_E_ACCESSDENIED
;
896 This
->clsid
= *clsid
;
898 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
899 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
903 /************************************************************************
904 * IPropertyStorage_fnStat (IPropertyStorage)
906 static HRESULT WINAPI
IPropertyStorage_fnStat(
907 IPropertyStorage
* iface
,
908 STATPROPSETSTG
* statpsstg
)
910 PropertyStorage_impl
*This
= impl_from_IPropertyStorage(iface
);
914 TRACE("%p, %p\n", iface
, statpsstg
);
919 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
922 statpsstg
->fmtid
= This
->fmtid
;
923 statpsstg
->clsid
= This
->clsid
;
924 statpsstg
->grfFlags
= This
->grfFlags
;
925 statpsstg
->mtime
= stat
.mtime
;
926 statpsstg
->ctime
= stat
.ctime
;
927 statpsstg
->atime
= stat
.atime
;
928 statpsstg
->dwOSVersion
= This
->originatorOS
;
933 static int PropertyStorage_PropNameCompare(const void *a
, const void *b
,
936 PropertyStorage_impl
*This
= extra
;
938 if (This
->codePage
== CP_UNICODE
)
940 TRACE("(%s, %s)\n", debugstr_w(a
), debugstr_w(b
));
941 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
942 return lstrcmpW(a
, b
);
944 return lstrcmpiW(a
, b
);
948 TRACE("(%s, %s)\n", debugstr_a(a
), debugstr_a(b
));
949 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
950 return lstrcmpA(a
, b
);
952 return lstrcmpiA(a
, b
);
956 static void PropertyStorage_PropNameDestroy(void *k
, void *d
, void *extra
)
961 static int PropertyStorage_PropCompare(const void *a
, const void *b
,
964 TRACE("(%d, %d)\n", PtrToUlong(a
), PtrToUlong(b
));
965 return PtrToUlong(a
) - PtrToUlong(b
);
968 static void PropertyStorage_PropertyDestroy(void *k
, void *d
, void *extra
)
971 HeapFree(GetProcessHeap(), 0, d
);
974 #ifdef WORDS_BIGENDIAN
975 /* Swaps each character in str to or from little endian; assumes the conversion
976 * is symmetric, that is, that lendian16toh is equivalent to htole16.
978 static void PropertyStorage_ByteSwapString(LPWSTR str
, size_t len
)
982 /* Swap characters to host order.
985 for (i
= 0; i
< len
; i
++)
986 str
[i
] = lendian16toh(str
[i
]);
989 #define PropertyStorage_ByteSwapString(s, l)
992 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
993 * the entries according to the values of This->codePage and This->locale.
994 * FIXME: there isn't any checking whether the read property extends past the
997 static HRESULT
PropertyStorage_ReadDictionary(PropertyStorage_impl
*This
,
1000 DWORD numEntries
, i
;
1003 assert(This
->name_to_propid
);
1004 assert(This
->propid_to_name
);
1006 StorageUtl_ReadDWord(ptr
, 0, &numEntries
);
1007 TRACE("Reading %d entries:\n", numEntries
);
1008 ptr
+= sizeof(DWORD
);
1009 for (i
= 0; SUCCEEDED(hr
) && i
< numEntries
; i
++)
1014 StorageUtl_ReadDWord(ptr
, 0, &propid
);
1015 ptr
+= sizeof(PROPID
);
1016 StorageUtl_ReadDWord(ptr
, 0, &cbEntry
);
1017 ptr
+= sizeof(DWORD
);
1018 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid
, cbEntry
);
1019 /* Make sure the source string is NULL-terminated */
1020 if (This
->codePage
!= CP_UNICODE
)
1021 ptr
[cbEntry
- 1] = '\0';
1023 *((LPWSTR
)ptr
+ cbEntry
/ sizeof(WCHAR
)) = '\0';
1024 hr
= PropertyStorage_StoreNameWithId(This
, (char*)ptr
, This
->codePage
, propid
);
1025 if (This
->codePage
== CP_UNICODE
)
1027 /* Unicode entries are padded to DWORD boundaries */
1028 if (cbEntry
% sizeof(DWORD
))
1029 ptr
+= sizeof(DWORD
) - (cbEntry
% sizeof(DWORD
));
1031 ptr
+= sizeof(DWORD
) + cbEntry
;
1036 static void* WINAPI
Allocate_CoTaskMemAlloc(void *this, ULONG size
)
1038 return CoTaskMemAlloc(size
);
1041 /* FIXME: there isn't any checking whether the read property extends past the
1042 * end of the buffer.
1044 static HRESULT
PropertyStorage_ReadProperty(PROPVARIANT
*prop
, const BYTE
*data
,
1045 UINT codepage
, void* (WINAPI
*allocate
)(void *this, ULONG size
), void *allocate_data
)
1051 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->vt
);
1052 data
+= sizeof(DWORD
);
1059 prop
->u
.cVal
= *(const char *)data
;
1060 TRACE("Read char 0x%x ('%c')\n", prop
->u
.cVal
, prop
->u
.cVal
);
1063 prop
->u
.bVal
= *data
;
1064 TRACE("Read byte 0x%x\n", prop
->u
.bVal
);
1067 StorageUtl_ReadWord(data
, 0, (WORD
*)&prop
->u
.iVal
);
1068 TRACE("Read short %d\n", prop
->u
.iVal
);
1071 StorageUtl_ReadWord(data
, 0, &prop
->u
.uiVal
);
1072 TRACE("Read ushort %d\n", prop
->u
.uiVal
);
1076 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->u
.lVal
);
1077 TRACE("Read long %d\n", prop
->u
.lVal
);
1081 StorageUtl_ReadDWord(data
, 0, &prop
->u
.ulVal
);
1082 TRACE("Read ulong %d\n", prop
->u
.ulVal
);
1088 StorageUtl_ReadDWord(data
, 0, &count
);
1089 if (codepage
== CP_UNICODE
&& count
% 2)
1091 WARN("Unicode string has odd number of bytes\n");
1092 hr
= STG_E_INVALIDHEADER
;
1096 prop
->u
.pszVal
= allocate(allocate_data
, count
);
1099 memcpy(prop
->u
.pszVal
, data
+ sizeof(DWORD
), count
);
1100 /* This is stored in the code page specified in codepage.
1101 * Don't convert it, the caller will just store it as-is.
1103 if (codepage
== CP_UNICODE
)
1105 /* Make sure it's NULL-terminated */
1106 prop
->u
.pszVal
[count
/ sizeof(WCHAR
) - 1] = '\0';
1107 TRACE("Read string value %s\n",
1108 debugstr_w(prop
->u
.pwszVal
));
1112 /* Make sure it's NULL-terminated */
1113 prop
->u
.pszVal
[count
- 1] = '\0';
1114 TRACE("Read string value %s\n", debugstr_a(prop
->u
.pszVal
));
1118 hr
= STG_E_INSUFFICIENTMEMORY
;
1124 DWORD count
, wcount
;
1126 StorageUtl_ReadDWord(data
, 0, &count
);
1127 if (codepage
== CP_UNICODE
&& count
% 2)
1129 WARN("Unicode string has odd number of bytes\n");
1130 hr
= STG_E_INVALIDHEADER
;
1134 if (codepage
== CP_UNICODE
)
1137 wcount
= MultiByteToWideChar(codepage
, 0, (LPCSTR
)(data
+ sizeof(DWORD
)), count
, NULL
, 0);
1139 prop
->u
.bstrVal
= SysAllocStringLen(NULL
, wcount
); /* FIXME: use allocator? */
1141 if (prop
->u
.bstrVal
)
1143 if (codepage
== CP_UNICODE
)
1144 memcpy(prop
->u
.bstrVal
, data
+ sizeof(DWORD
), count
);
1146 MultiByteToWideChar(codepage
, 0, (LPCSTR
)(data
+ sizeof(DWORD
)), count
, prop
->u
.bstrVal
, wcount
);
1148 prop
->u
.bstrVal
[wcount
- 1] = '\0';
1149 TRACE("Read string value %s\n", debugstr_w(prop
->u
.bstrVal
));
1152 hr
= STG_E_INSUFFICIENTMEMORY
;
1160 StorageUtl_ReadDWord(data
, 0, &count
);
1161 prop
->u
.blob
.cbSize
= count
;
1162 prop
->u
.blob
.pBlobData
= allocate(allocate_data
, count
);
1163 if (prop
->u
.blob
.pBlobData
)
1165 memcpy(prop
->u
.blob
.pBlobData
, data
+ sizeof(DWORD
), count
);
1166 TRACE("Read blob value of size %d\n", count
);
1169 hr
= STG_E_INSUFFICIENTMEMORY
;
1176 StorageUtl_ReadDWord(data
, 0, &count
);
1177 prop
->u
.pwszVal
= allocate(allocate_data
, count
* sizeof(WCHAR
));
1178 if (prop
->u
.pwszVal
)
1180 memcpy(prop
->u
.pwszVal
, data
+ sizeof(DWORD
),
1181 count
* sizeof(WCHAR
));
1182 /* make sure string is NULL-terminated */
1183 prop
->u
.pwszVal
[count
- 1] = '\0';
1184 PropertyStorage_ByteSwapString(prop
->u
.pwszVal
, count
);
1185 TRACE("Read string value %s\n", debugstr_w(prop
->u
.pwszVal
));
1188 hr
= STG_E_INSUFFICIENTMEMORY
;
1192 StorageUtl_ReadULargeInteger(data
, 0,
1193 (ULARGE_INTEGER
*)&prop
->u
.filetime
);
1197 DWORD len
= 0, tag
= 0;
1199 StorageUtl_ReadDWord(data
, 0, &len
);
1200 StorageUtl_ReadDWord(data
, 4, &tag
);
1204 prop
->u
.pclipdata
= allocate(allocate_data
, sizeof (CLIPDATA
));
1205 prop
->u
.pclipdata
->cbSize
= len
;
1206 prop
->u
.pclipdata
->ulClipFmt
= tag
;
1207 prop
->u
.pclipdata
->pClipData
= allocate(allocate_data
, len
- sizeof(prop
->u
.pclipdata
->ulClipFmt
));
1208 memcpy(prop
->u
.pclipdata
->pClipData
, data
+8, len
- sizeof(prop
->u
.pclipdata
->ulClipFmt
));
1211 hr
= STG_E_INVALIDPARAMETER
;
1215 FIXME("unsupported type %d\n", prop
->vt
);
1216 hr
= STG_E_INVALIDPARAMETER
;
1221 static HRESULT
PropertyStorage_ReadHeaderFromStream(IStream
*stm
,
1222 PROPERTYSETHEADER
*hdr
)
1224 BYTE buf
[sizeof(PROPERTYSETHEADER
)];
1230 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1233 if (count
!= sizeof(buf
))
1235 WARN("read only %d\n", count
);
1236 hr
= STG_E_INVALIDHEADER
;
1240 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wByteOrder
),
1242 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wFormat
),
1244 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, dwOSVer
),
1246 StorageUtl_ReadGUID(buf
, offsetof(PROPERTYSETHEADER
, clsid
),
1248 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, reserved
),
1252 TRACE("returning 0x%08x\n", hr
);
1256 static HRESULT
PropertyStorage_ReadFmtIdOffsetFromStream(IStream
*stm
,
1257 FORMATIDOFFSET
*fmt
)
1259 BYTE buf
[sizeof(FORMATIDOFFSET
)];
1265 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1268 if (count
!= sizeof(buf
))
1270 WARN("read only %d\n", count
);
1271 hr
= STG_E_INVALIDHEADER
;
1275 StorageUtl_ReadGUID(buf
, offsetof(FORMATIDOFFSET
, fmtid
),
1277 StorageUtl_ReadDWord(buf
, offsetof(FORMATIDOFFSET
, dwOffset
),
1281 TRACE("returning 0x%08x\n", hr
);
1285 static HRESULT
PropertyStorage_ReadSectionHeaderFromStream(IStream
*stm
,
1286 PROPERTYSECTIONHEADER
*hdr
)
1288 BYTE buf
[sizeof(PROPERTYSECTIONHEADER
)];
1294 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1297 if (count
!= sizeof(buf
))
1299 WARN("read only %d\n", count
);
1300 hr
= STG_E_INVALIDHEADER
;
1304 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1305 cbSection
), &hdr
->cbSection
);
1306 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1307 cProperties
), &hdr
->cProperties
);
1310 TRACE("returning 0x%08x\n", hr
);
1314 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*This
)
1316 PROPERTYSETHEADER hdr
;
1317 FORMATIDOFFSET fmtOffset
;
1318 PROPERTYSECTIONHEADER sectionHdr
;
1325 DWORD dictOffset
= 0;
1327 This
->dirty
= FALSE
;
1328 This
->highestProp
= 0;
1329 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1332 if (stat
.cbSize
.u
.HighPart
)
1334 WARN("stream too big\n");
1335 /* maximum size varies, but it can't be this big */
1336 hr
= STG_E_INVALIDHEADER
;
1339 if (stat
.cbSize
.u
.LowPart
== 0)
1341 /* empty stream is okay */
1345 else if (stat
.cbSize
.u
.LowPart
< sizeof(PROPERTYSETHEADER
) +
1346 sizeof(FORMATIDOFFSET
))
1348 WARN("stream too small\n");
1349 hr
= STG_E_INVALIDHEADER
;
1353 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1356 hr
= PropertyStorage_ReadHeaderFromStream(This
->stm
, &hdr
);
1357 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1360 if (hdr
.wByteOrder
!= PROPSETHDR_BYTEORDER_MAGIC
|| hdr
.reserved
< 1)
1362 WARN("bad magic in prop set header\n");
1363 hr
= STG_E_INVALIDHEADER
;
1366 if (hdr
.wFormat
!= 0 && hdr
.wFormat
!= 1)
1368 WARN("bad format version %d\n", hdr
.wFormat
);
1369 hr
= STG_E_INVALIDHEADER
;
1372 This
->format
= hdr
.wFormat
;
1373 This
->clsid
= hdr
.clsid
;
1374 This
->originatorOS
= hdr
.dwOSVer
;
1375 if (PROPSETHDR_OSVER_KIND(hdr
.dwOSVer
) == PROPSETHDR_OSVER_KIND_MAC
)
1376 WARN("File comes from a Mac, strings will probably be screwed up\n");
1377 hr
= PropertyStorage_ReadFmtIdOffsetFromStream(This
->stm
, &fmtOffset
);
1380 if (fmtOffset
.dwOffset
> stat
.cbSize
.u
.LowPart
)
1382 WARN("invalid offset %d (stream length is %d)\n", fmtOffset
.dwOffset
,
1383 stat
.cbSize
.u
.LowPart
);
1384 hr
= STG_E_INVALIDHEADER
;
1387 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1388 * follows not one, but two sections. The first contains the standard properties
1389 * for the document summary information, and the second consists of user-defined
1390 * properties. This is the only case in which multiple sections are
1392 * Reading the second stream isn't implemented yet.
1394 hr
= PropertyStorage_ReadSectionHeaderFromStream(This
->stm
, §ionHdr
);
1397 /* The section size includes the section header, so check it */
1398 if (sectionHdr
.cbSection
< sizeof(PROPERTYSECTIONHEADER
))
1400 WARN("section header too small, got %d\n", sectionHdr
.cbSection
);
1401 hr
= STG_E_INVALIDHEADER
;
1404 buf
= HeapAlloc(GetProcessHeap(), 0, sectionHdr
.cbSection
-
1405 sizeof(PROPERTYSECTIONHEADER
));
1408 hr
= STG_E_INSUFFICIENTMEMORY
;
1411 hr
= IStream_Read(This
->stm
, buf
, sectionHdr
.cbSection
-
1412 sizeof(PROPERTYSECTIONHEADER
), &count
);
1415 TRACE("Reading %d properties:\n", sectionHdr
.cProperties
);
1416 for (i
= 0; SUCCEEDED(hr
) && i
< sectionHdr
.cProperties
; i
++)
1418 PROPERTYIDOFFSET
*idOffset
= (PROPERTYIDOFFSET
*)(buf
+
1419 i
* sizeof(PROPERTYIDOFFSET
));
1421 if (idOffset
->dwOffset
< sizeof(PROPERTYSECTIONHEADER
) ||
1422 idOffset
->dwOffset
> sectionHdr
.cbSection
- sizeof(DWORD
))
1423 hr
= STG_E_INVALIDPOINTER
;
1426 if (idOffset
->propid
>= PID_FIRST_USABLE
&&
1427 idOffset
->propid
< PID_MIN_READONLY
&& idOffset
->propid
>
1429 This
->highestProp
= idOffset
->propid
;
1430 if (idOffset
->propid
== PID_DICTIONARY
)
1432 /* Don't read the dictionary yet, its entries depend on the
1433 * code page. Just store the offset so we know to read it
1436 dictOffset
= idOffset
->dwOffset
;
1437 TRACE("Dictionary offset is %d\n", dictOffset
);
1443 PropVariantInit(&prop
);
1444 if (SUCCEEDED(PropertyStorage_ReadProperty(&prop
,
1445 buf
+ idOffset
->dwOffset
- sizeof(PROPERTYSECTIONHEADER
),
1446 This
->codePage
, Allocate_CoTaskMemAlloc
, NULL
)))
1448 TRACE("Read property with ID 0x%08x, type %d\n",
1449 idOffset
->propid
, prop
.vt
);
1450 switch(idOffset
->propid
)
1453 if (prop
.vt
== VT_I2
)
1454 This
->codePage
= (UINT
)prop
.u
.iVal
;
1457 if (prop
.vt
== VT_I4
)
1458 This
->locale
= (LCID
)prop
.u
.lVal
;
1461 if (prop
.vt
== VT_I4
&& prop
.u
.lVal
)
1462 This
->grfFlags
|= PROPSETFLAG_CASE_SENSITIVE
;
1463 /* The format should already be 1, but just in case */
1467 hr
= PropertyStorage_StorePropWithId(This
,
1468 idOffset
->propid
, &prop
, This
->codePage
);
1471 PropVariantClear(&prop
);
1475 if (!This
->codePage
)
1477 /* default to Unicode unless told not to, as specified on msdn */
1478 if (This
->grfFlags
& PROPSETFLAG_ANSI
)
1479 This
->codePage
= GetACP();
1481 This
->codePage
= CP_UNICODE
;
1484 This
->locale
= LOCALE_SYSTEM_DEFAULT
;
1485 TRACE("Code page is %d, locale is %d\n", This
->codePage
, This
->locale
);
1487 hr
= PropertyStorage_ReadDictionary(This
,
1488 buf
+ dictOffset
- sizeof(PROPERTYSECTIONHEADER
));
1491 HeapFree(GetProcessHeap(), 0, buf
);
1494 dictionary_destroy(This
->name_to_propid
);
1495 This
->name_to_propid
= NULL
;
1496 dictionary_destroy(This
->propid_to_name
);
1497 This
->propid_to_name
= NULL
;
1498 dictionary_destroy(This
->propid_to_prop
);
1499 This
->propid_to_prop
= NULL
;
1504 static void PropertyStorage_MakeHeader(PropertyStorage_impl
*This
,
1505 PROPERTYSETHEADER
*hdr
)
1508 StorageUtl_WriteWord((BYTE
*)&hdr
->wByteOrder
, 0,
1509 PROPSETHDR_BYTEORDER_MAGIC
);
1510 StorageUtl_WriteWord((BYTE
*)&hdr
->wFormat
, 0, This
->format
);
1511 StorageUtl_WriteDWord((BYTE
*)&hdr
->dwOSVer
, 0, This
->originatorOS
);
1512 StorageUtl_WriteGUID((BYTE
*)&hdr
->clsid
, 0, &This
->clsid
);
1513 StorageUtl_WriteDWord((BYTE
*)&hdr
->reserved
, 0, 1);
1516 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl
*This
,
1517 FORMATIDOFFSET
*fmtOffset
)
1520 StorageUtl_WriteGUID((BYTE
*)fmtOffset
, 0, &This
->fmtid
);
1521 StorageUtl_WriteDWord((BYTE
*)fmtOffset
, offsetof(FORMATIDOFFSET
, dwOffset
),
1522 sizeof(PROPERTYSETHEADER
) + sizeof(FORMATIDOFFSET
));
1525 static void PropertyStorage_MakeSectionHdr(DWORD cbSection
, DWORD numProps
,
1526 PROPERTYSECTIONHEADER
*hdr
)
1529 StorageUtl_WriteDWord((BYTE
*)hdr
, 0, cbSection
);
1530 StorageUtl_WriteDWord((BYTE
*)hdr
,
1531 offsetof(PROPERTYSECTIONHEADER
, cProperties
), numProps
);
1534 static void PropertyStorage_MakePropertyIdOffset(DWORD propid
, DWORD dwOffset
,
1535 PROPERTYIDOFFSET
*propIdOffset
)
1537 assert(propIdOffset
);
1538 StorageUtl_WriteDWord((BYTE
*)propIdOffset
, 0, propid
);
1539 StorageUtl_WriteDWord((BYTE
*)propIdOffset
,
1540 offsetof(PROPERTYIDOFFSET
, dwOffset
), dwOffset
);
1543 static inline HRESULT
PropertStorage_WriteWStringToStream(IStream
*stm
,
1544 LPCWSTR str
, DWORD len
, DWORD
*written
)
1546 #ifdef WORDS_BIGENDIAN
1547 WCHAR
*leStr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1551 return E_OUTOFMEMORY
;
1552 memcpy(leStr
, str
, len
* sizeof(WCHAR
));
1553 PropertyStorage_ByteSwapString(leStr
, len
);
1554 hr
= IStream_Write(stm
, leStr
, len
, written
);
1555 HeapFree(GetProcessHeap(), 0, leStr
);
1558 return IStream_Write(stm
, str
, len
, written
);
1562 struct DictionaryClosure
1568 static BOOL
PropertyStorage_DictionaryWriter(const void *key
,
1569 const void *value
, void *extra
, void *closure
)
1571 PropertyStorage_impl
*This
= extra
;
1572 struct DictionaryClosure
*c
= closure
;
1578 StorageUtl_WriteDWord((LPBYTE
)&propid
, 0, PtrToUlong(value
));
1579 c
->hr
= IStream_Write(This
->stm
, &propid
, sizeof(propid
), &count
);
1582 c
->bytesWritten
+= sizeof(DWORD
);
1583 if (This
->codePage
== CP_UNICODE
)
1585 DWORD keyLen
, pad
= 0;
1587 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0,
1588 (lstrlenW((LPCWSTR
)key
) + 1) * sizeof(WCHAR
));
1589 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1592 c
->bytesWritten
+= sizeof(DWORD
);
1593 c
->hr
= PropertStorage_WriteWStringToStream(This
->stm
, key
, keyLen
,
1597 c
->bytesWritten
+= keyLen
* sizeof(WCHAR
);
1598 if (keyLen
% sizeof(DWORD
))
1600 c
->hr
= IStream_Write(This
->stm
, &pad
,
1601 sizeof(DWORD
) - keyLen
% sizeof(DWORD
), &count
);
1604 c
->bytesWritten
+= sizeof(DWORD
) - keyLen
% sizeof(DWORD
);
1611 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0, strlen((LPCSTR
)key
) + 1);
1612 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1615 c
->bytesWritten
+= sizeof(DWORD
);
1616 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1619 c
->bytesWritten
+= keyLen
;
1622 return SUCCEEDED(c
->hr
);
1625 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1627 /* Writes the dictionary to the stream. Assumes without checking that the
1628 * dictionary isn't empty.
1630 static HRESULT
PropertyStorage_WriteDictionaryToStream(
1631 PropertyStorage_impl
*This
, DWORD
*sectionOffset
)
1635 PROPERTYIDOFFSET propIdOffset
;
1638 struct DictionaryClosure closure
;
1640 assert(sectionOffset
);
1642 /* The dictionary's always the first property written, so seek to its
1645 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
);
1646 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1649 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY
, *sectionOffset
,
1651 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1655 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1656 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1659 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0,
1660 dictionary_num_entries(This
->name_to_propid
));
1661 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1664 *sectionOffset
+= sizeof(dwTemp
);
1667 closure
.bytesWritten
= 0;
1668 dictionary_enumerate(This
->name_to_propid
, PropertyStorage_DictionaryWriter
,
1673 *sectionOffset
+= closure
.bytesWritten
;
1674 if (closure
.bytesWritten
% sizeof(DWORD
))
1676 DWORD padding
= sizeof(DWORD
) - closure
.bytesWritten
% sizeof(DWORD
);
1677 TRACE("adding %d bytes of padding\n", padding
);
1678 *sectionOffset
+= padding
;
1685 static HRESULT
PropertyStorage_WritePropertyToStream(PropertyStorage_impl
*This
,
1686 DWORD propNum
, DWORD propid
, const PROPVARIANT
*var
, DWORD
*sectionOffset
)
1690 PROPERTYIDOFFSET propIdOffset
;
1692 DWORD dwType
, bytesWritten
;
1695 assert(sectionOffset
);
1697 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This
, propNum
, propid
, var
->vt
,
1700 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
) +
1701 propNum
* sizeof(PROPERTYIDOFFSET
);
1702 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1705 PropertyStorage_MakePropertyIdOffset(propid
, *sectionOffset
, &propIdOffset
);
1706 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1710 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1711 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1714 StorageUtl_WriteDWord((LPBYTE
)&dwType
, 0, var
->vt
);
1715 hr
= IStream_Write(This
->stm
, &dwType
, sizeof(dwType
), &count
);
1718 *sectionOffset
+= sizeof(dwType
);
1728 hr
= IStream_Write(This
->stm
, &var
->u
.cVal
, sizeof(var
->u
.cVal
),
1730 bytesWritten
= count
;
1737 StorageUtl_WriteWord((LPBYTE
)&wTemp
, 0, var
->u
.iVal
);
1738 hr
= IStream_Write(This
->stm
, &wTemp
, sizeof(wTemp
), &count
);
1739 bytesWritten
= count
;
1747 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, var
->u
.lVal
);
1748 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1749 bytesWritten
= count
;
1756 if (This
->codePage
== CP_UNICODE
)
1757 len
= (lstrlenW(var
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
1759 len
= lstrlenA(var
->u
.pszVal
) + 1;
1760 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1761 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1764 hr
= IStream_Write(This
->stm
, var
->u
.pszVal
, len
, &count
);
1765 bytesWritten
= count
+ sizeof(DWORD
);
1770 DWORD len
= lstrlenW(var
->u
.pwszVal
) + 1, dwTemp
;
1772 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1773 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1776 hr
= IStream_Write(This
->stm
, var
->u
.pwszVal
, len
* sizeof(WCHAR
),
1778 bytesWritten
= count
+ sizeof(DWORD
);
1785 StorageUtl_WriteULargeInteger((BYTE
*)&temp
, 0,
1786 (const ULARGE_INTEGER
*)&var
->u
.filetime
);
1787 hr
= IStream_Write(This
->stm
, &temp
, sizeof(FILETIME
), &count
);
1788 bytesWritten
= count
;
1793 DWORD cf_hdr
[2], len
;
1795 len
= var
->u
.pclipdata
->cbSize
;
1796 StorageUtl_WriteDWord((LPBYTE
)&cf_hdr
[0], 0, len
+ 8);
1797 StorageUtl_WriteDWord((LPBYTE
)&cf_hdr
[1], 0, var
->u
.pclipdata
->ulClipFmt
);
1798 hr
= IStream_Write(This
->stm
, cf_hdr
, sizeof(cf_hdr
), &count
);
1801 hr
= IStream_Write(This
->stm
, var
->u
.pclipdata
->pClipData
,
1802 len
- sizeof(var
->u
.pclipdata
->ulClipFmt
), &count
);
1805 bytesWritten
= count
+ sizeof cf_hdr
;
1812 StorageUtl_WriteGUID((BYTE
*)&temp
, 0, var
->u
.puuid
);
1813 hr
= IStream_Write(This
->stm
, &temp
, sizeof(temp
), &count
);
1814 bytesWritten
= count
;
1818 FIXME("unsupported type: %d\n", var
->vt
);
1819 return STG_E_INVALIDPARAMETER
;
1824 *sectionOffset
+= bytesWritten
;
1825 if (bytesWritten
% sizeof(DWORD
))
1827 DWORD padding
= sizeof(DWORD
) - bytesWritten
% sizeof(DWORD
);
1828 TRACE("adding %d bytes of padding\n", padding
);
1829 *sectionOffset
+= padding
;
1837 struct PropertyClosure
1841 DWORD
*sectionOffset
;
1844 static BOOL
PropertyStorage_PropertiesWriter(const void *key
, const void *value
,
1845 void *extra
, void *closure
)
1847 PropertyStorage_impl
*This
= extra
;
1848 struct PropertyClosure
*c
= closure
;
1854 c
->hr
= PropertyStorage_WritePropertyToStream(This
, c
->propNum
++,
1855 PtrToUlong(key
), value
, c
->sectionOffset
);
1856 return SUCCEEDED(c
->hr
);
1859 static HRESULT
PropertyStorage_WritePropertiesToStream(
1860 PropertyStorage_impl
*This
, DWORD startingPropNum
, DWORD
*sectionOffset
)
1862 struct PropertyClosure closure
;
1864 assert(sectionOffset
);
1866 closure
.propNum
= startingPropNum
;
1867 closure
.sectionOffset
= sectionOffset
;
1868 dictionary_enumerate(This
->propid_to_prop
, PropertyStorage_PropertiesWriter
,
1873 static HRESULT
PropertyStorage_WriteHeadersToStream(PropertyStorage_impl
*This
)
1877 LARGE_INTEGER seek
= { {0} };
1878 PROPERTYSETHEADER hdr
;
1879 FORMATIDOFFSET fmtOffset
;
1881 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1884 PropertyStorage_MakeHeader(This
, &hdr
);
1885 hr
= IStream_Write(This
->stm
, &hdr
, sizeof(hdr
), &count
);
1888 if (count
!= sizeof(hdr
))
1890 hr
= STG_E_WRITEFAULT
;
1894 PropertyStorage_MakeFmtIdOffset(This
, &fmtOffset
);
1895 hr
= IStream_Write(This
->stm
, &fmtOffset
, sizeof(fmtOffset
), &count
);
1898 if (count
!= sizeof(fmtOffset
))
1900 hr
= STG_E_WRITEFAULT
;
1909 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*This
)
1911 PROPERTYSECTIONHEADER sectionHdr
;
1915 DWORD numProps
, prop
, sectionOffset
, dwTemp
;
1918 PropertyStorage_WriteHeadersToStream(This
);
1920 /* Count properties. Always at least one property, the code page */
1922 if (dictionary_num_entries(This
->name_to_propid
))
1924 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1926 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1928 numProps
+= dictionary_num_entries(This
->propid_to_prop
);
1930 /* Write section header with 0 bytes right now, I'll adjust it after
1931 * writing properties.
1933 PropertyStorage_MakeSectionHdr(0, numProps
, §ionHdr
);
1934 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1935 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1938 hr
= IStream_Write(This
->stm
, §ionHdr
, sizeof(sectionHdr
), &count
);
1943 sectionOffset
= sizeof(PROPERTYSECTIONHEADER
) +
1944 numProps
* sizeof(PROPERTYIDOFFSET
);
1946 if (dictionary_num_entries(This
->name_to_propid
))
1949 hr
= PropertyStorage_WriteDictionaryToStream(This
, §ionOffset
);
1954 PropVariantInit(&var
);
1957 var
.u
.iVal
= This
->codePage
;
1958 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_CODEPAGE
,
1959 &var
, §ionOffset
);
1963 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1966 var
.u
.lVal
= This
->locale
;
1967 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_LOCALE
,
1968 &var
, §ionOffset
);
1973 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1977 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_BEHAVIOR
,
1978 &var
, §ionOffset
);
1983 hr
= PropertyStorage_WritePropertiesToStream(This
, prop
, §ionOffset
);
1987 /* Now write the byte count of the section */
1988 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1989 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1992 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, sectionOffset
);
1993 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1999 /***********************************************************************
2000 * PropertyStorage_Construct
2002 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*This
)
2004 dictionary_destroy(This
->name_to_propid
);
2005 This
->name_to_propid
= NULL
;
2006 dictionary_destroy(This
->propid_to_name
);
2007 This
->propid_to_name
= NULL
;
2008 dictionary_destroy(This
->propid_to_prop
);
2009 This
->propid_to_prop
= NULL
;
2012 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*This
)
2016 This
->name_to_propid
= dictionary_create(
2017 PropertyStorage_PropNameCompare
, PropertyStorage_PropNameDestroy
,
2019 if (!This
->name_to_propid
)
2021 hr
= STG_E_INSUFFICIENTMEMORY
;
2024 This
->propid_to_name
= dictionary_create(PropertyStorage_PropCompare
,
2026 if (!This
->propid_to_name
)
2028 hr
= STG_E_INSUFFICIENTMEMORY
;
2031 This
->propid_to_prop
= dictionary_create(PropertyStorage_PropCompare
,
2032 PropertyStorage_PropertyDestroy
, This
);
2033 if (!This
->propid_to_prop
)
2035 hr
= STG_E_INSUFFICIENTMEMORY
;
2040 PropertyStorage_DestroyDictionaries(This
);
2044 static HRESULT
PropertyStorage_BaseConstruct(IStream
*stm
,
2045 REFFMTID rfmtid
, DWORD grfMode
, PropertyStorage_impl
**pps
)
2051 *pps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof **pps
);
2053 return E_OUTOFMEMORY
;
2055 (*pps
)->IPropertyStorage_iface
.lpVtbl
= &IPropertyStorage_Vtbl
;
2057 InitializeCriticalSection(&(*pps
)->cs
);
2058 (*pps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PropertyStorage_impl.cs");
2060 (*pps
)->fmtid
= *rfmtid
;
2061 (*pps
)->grfMode
= grfMode
;
2063 hr
= PropertyStorage_CreateDictionaries(*pps
);
2066 IStream_Release(stm
);
2067 (*pps
)->cs
.DebugInfo
->Spare
[0] = 0;
2068 DeleteCriticalSection(&(*pps
)->cs
);
2069 HeapFree(GetProcessHeap(), 0, *pps
);
2076 static HRESULT
PropertyStorage_ConstructFromStream(IStream
*stm
,
2077 REFFMTID rfmtid
, DWORD grfMode
, IPropertyStorage
** pps
)
2079 PropertyStorage_impl
*ps
;
2083 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2086 hr
= PropertyStorage_ReadFromStream(ps
);
2089 *pps
= &ps
->IPropertyStorage_iface
;
2090 TRACE("PropertyStorage %p constructed\n", ps
);
2095 PropertyStorage_DestroyDictionaries(ps
);
2096 HeapFree(GetProcessHeap(), 0, ps
);
2102 static HRESULT
PropertyStorage_ConstructEmpty(IStream
*stm
,
2103 REFFMTID rfmtid
, DWORD grfFlags
, DWORD grfMode
, IPropertyStorage
** pps
)
2105 PropertyStorage_impl
*ps
;
2109 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2113 ps
->grfFlags
= grfFlags
;
2114 if (ps
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2116 /* default to Unicode unless told not to, as specified on msdn */
2117 if (ps
->grfFlags
& PROPSETFLAG_ANSI
)
2118 ps
->codePage
= GetACP();
2120 ps
->codePage
= CP_UNICODE
;
2121 ps
->locale
= LOCALE_SYSTEM_DEFAULT
;
2122 TRACE("Code page is %d, locale is %d\n", ps
->codePage
, ps
->locale
);
2123 *pps
= &ps
->IPropertyStorage_iface
;
2124 TRACE("PropertyStorage %p constructed\n", ps
);
2131 /***********************************************************************
2132 * Implementation of IPropertySetStorage
2135 /************************************************************************
2136 * IPropertySetStorage_fnQueryInterface (IUnknown)
2138 * This method forwards to the common QueryInterface implementation
2140 static HRESULT WINAPI
IPropertySetStorage_fnQueryInterface(
2141 IPropertySetStorage
*ppstg
,
2145 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2146 return IStorage_QueryInterface( &This
->base
.IStorage_iface
, riid
, ppvObject
);
2149 /************************************************************************
2150 * IPropertySetStorage_fnAddRef (IUnknown)
2152 * This method forwards to the common AddRef implementation
2154 static ULONG WINAPI
IPropertySetStorage_fnAddRef(
2155 IPropertySetStorage
*ppstg
)
2157 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2158 return IStorage_AddRef( &This
->base
.IStorage_iface
);
2161 /************************************************************************
2162 * IPropertySetStorage_fnRelease (IUnknown)
2164 * This method forwards to the common Release implementation
2166 static ULONG WINAPI
IPropertySetStorage_fnRelease(
2167 IPropertySetStorage
*ppstg
)
2169 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2170 return IStorage_Release( &This
->base
.IStorage_iface
);
2173 /************************************************************************
2174 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2176 static HRESULT WINAPI
IPropertySetStorage_fnCreate(
2177 IPropertySetStorage
*ppstg
,
2179 const CLSID
* pclsid
,
2182 IPropertyStorage
** ppprstg
)
2184 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2185 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2186 IStream
*stm
= NULL
;
2189 TRACE("%p %s %08x %08x %p\n", This
, debugstr_guid(rfmtid
), grfFlags
,
2193 if (grfMode
!= (STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
))
2195 r
= STG_E_INVALIDFLAG
;
2205 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2206 * storage, not a stream. For now, disallow it.
2208 if (grfFlags
& PROPSETFLAG_NONSIMPLE
)
2210 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2211 r
= STG_E_INVALIDFLAG
;
2215 r
= FmtIdToPropStgName(rfmtid
, name
);
2219 r
= IStorage_CreateStream( &This
->base
.IStorage_iface
, name
, grfMode
, 0, 0, &stm
);
2223 r
= PropertyStorage_ConstructEmpty(stm
, rfmtid
, grfFlags
, grfMode
, ppprstg
);
2226 TRACE("returning 0x%08x\n", r
);
2230 /************************************************************************
2231 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2233 static HRESULT WINAPI
IPropertySetStorage_fnOpen(
2234 IPropertySetStorage
*ppstg
,
2237 IPropertyStorage
** ppprstg
)
2239 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2240 IStream
*stm
= NULL
;
2241 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2244 TRACE("%p %s %08x %p\n", This
, debugstr_guid(rfmtid
), grfMode
, ppprstg
);
2247 if (grfMode
!= (STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
) &&
2248 grfMode
!= (STGM_READ
|STGM_SHARE_EXCLUSIVE
))
2250 r
= STG_E_INVALIDFLAG
;
2260 r
= FmtIdToPropStgName(rfmtid
, name
);
2264 r
= IStorage_OpenStream( &This
->base
.IStorage_iface
, name
, 0, grfMode
, 0, &stm
);
2268 r
= PropertyStorage_ConstructFromStream(stm
, rfmtid
, grfMode
, ppprstg
);
2271 TRACE("returning 0x%08x\n", r
);
2275 /************************************************************************
2276 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2278 static HRESULT WINAPI
IPropertySetStorage_fnDelete(
2279 IPropertySetStorage
*ppstg
,
2282 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2283 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2286 TRACE("%p %s\n", This
, debugstr_guid(rfmtid
));
2289 return E_INVALIDARG
;
2291 r
= FmtIdToPropStgName(rfmtid
, name
);
2295 return IStorage_DestroyElement(&This
->base
.IStorage_iface
, name
);
2298 /************************************************************************
2299 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2301 static HRESULT WINAPI
IPropertySetStorage_fnEnum(
2302 IPropertySetStorage
*ppstg
,
2303 IEnumSTATPROPSETSTG
** ppenum
)
2305 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2306 return create_EnumSTATPROPSETSTG(This
, ppenum
);
2309 /************************************************************************
2310 * Implement IEnumSTATPROPSETSTG using enumx
2312 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnQueryInterface(
2313 IEnumSTATPROPSETSTG
*iface
,
2317 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2320 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnAddRef(
2321 IEnumSTATPROPSETSTG
*iface
)
2323 return enumx_AddRef((enumx_impl
*)iface
);
2326 static ULONG WINAPI
IEnumSTATPROPSETSTG_fnRelease(
2327 IEnumSTATPROPSETSTG
*iface
)
2329 return enumx_Release((enumx_impl
*)iface
);
2332 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnNext(
2333 IEnumSTATPROPSETSTG
*iface
,
2335 STATPROPSETSTG
*rgelt
,
2336 ULONG
*pceltFetched
)
2338 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2341 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnSkip(
2342 IEnumSTATPROPSETSTG
*iface
,
2345 return enumx_Skip((enumx_impl
*)iface
, celt
);
2348 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnReset(
2349 IEnumSTATPROPSETSTG
*iface
)
2351 return enumx_Reset((enumx_impl
*)iface
);
2354 static HRESULT WINAPI
IEnumSTATPROPSETSTG_fnClone(
2355 IEnumSTATPROPSETSTG
*iface
,
2356 IEnumSTATPROPSETSTG
**ppenum
)
2358 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2361 static HRESULT
create_EnumSTATPROPSETSTG(
2363 IEnumSTATPROPSETSTG
** ppenum
)
2365 IStorage
*stg
= &This
->base
.IStorage_iface
;
2366 IEnumSTATSTG
*penum
= NULL
;
2370 STATPROPSETSTG statpss
;
2373 TRACE("%p %p\n", This
, ppenum
);
2375 enumx
= enumx_allocate(&IID_IEnumSTATPROPSETSTG
,
2376 &IEnumSTATPROPSETSTG_Vtbl
,
2377 sizeof (STATPROPSETSTG
));
2379 /* add all the property set elements into a list */
2380 r
= IStorage_EnumElements(stg
, 0, NULL
, 0, &penum
);
2383 enumx_Release(enumx
);
2384 return E_OUTOFMEMORY
;
2390 r
= IEnumSTATSTG_Next(penum
, 1, &stat
, &count
);
2397 if (stat
.pwcsName
[0] == 5 && stat
.type
== STGTY_STREAM
)
2399 PropStgNameToFmtId(stat
.pwcsName
, &statpss
.fmtid
);
2400 TRACE("adding %s (%s)\n", debugstr_w(stat
.pwcsName
),
2401 debugstr_guid(&statpss
.fmtid
));
2402 statpss
.mtime
= stat
.mtime
;
2403 statpss
.atime
= stat
.atime
;
2404 statpss
.ctime
= stat
.ctime
;
2405 statpss
.grfFlags
= stat
.grfMode
;
2406 statpss
.clsid
= stat
.clsid
;
2407 enumx_add_element(enumx
, &statpss
);
2409 CoTaskMemFree(stat
.pwcsName
);
2411 IEnumSTATSTG_Release(penum
);
2413 *ppenum
= (IEnumSTATPROPSETSTG
*) enumx
;
2418 /************************************************************************
2419 * Implement IEnumSTATPROPSTG using enumx
2421 static HRESULT WINAPI
IEnumSTATPROPSTG_fnQueryInterface(
2422 IEnumSTATPROPSTG
*iface
,
2426 return enumx_QueryInterface((enumx_impl
*)iface
, riid
, ppvObject
);
2429 static ULONG WINAPI
IEnumSTATPROPSTG_fnAddRef(
2430 IEnumSTATPROPSTG
*iface
)
2432 return enumx_AddRef((enumx_impl
*)iface
);
2435 static ULONG WINAPI
IEnumSTATPROPSTG_fnRelease(
2436 IEnumSTATPROPSTG
*iface
)
2438 return enumx_Release((enumx_impl
*)iface
);
2441 static HRESULT WINAPI
IEnumSTATPROPSTG_fnNext(
2442 IEnumSTATPROPSTG
*iface
,
2445 ULONG
*pceltFetched
)
2447 return enumx_Next((enumx_impl
*)iface
, celt
, rgelt
, pceltFetched
);
2450 static HRESULT WINAPI
IEnumSTATPROPSTG_fnSkip(
2451 IEnumSTATPROPSTG
*iface
,
2454 return enumx_Skip((enumx_impl
*)iface
, celt
);
2457 static HRESULT WINAPI
IEnumSTATPROPSTG_fnReset(
2458 IEnumSTATPROPSTG
*iface
)
2460 return enumx_Reset((enumx_impl
*)iface
);
2463 static HRESULT WINAPI
IEnumSTATPROPSTG_fnClone(
2464 IEnumSTATPROPSTG
*iface
,
2465 IEnumSTATPROPSTG
**ppenum
)
2467 return enumx_Clone((enumx_impl
*)iface
, (enumx_impl
**)ppenum
);
2470 static BOOL
prop_enum_stat(const void *k
, const void *v
, void *extra
, void *arg
)
2472 enumx_impl
*enumx
= arg
;
2473 PROPID propid
= PtrToUlong(k
);
2474 const PROPVARIANT
*prop
= v
;
2477 stat
.lpwstrName
= NULL
;
2478 stat
.propid
= propid
;
2481 enumx_add_element(enumx
, &stat
);
2486 static HRESULT
create_EnumSTATPROPSTG(
2487 PropertyStorage_impl
*This
,
2488 IEnumSTATPROPSTG
** ppenum
)
2492 TRACE("%p %p\n", This
, ppenum
);
2494 enumx
= enumx_allocate(&IID_IEnumSTATPROPSTG
,
2495 &IEnumSTATPROPSTG_Vtbl
,
2496 sizeof (STATPROPSTG
));
2498 dictionary_enumerate(This
->propid_to_prop
, prop_enum_stat
, enumx
);
2500 *ppenum
= (IEnumSTATPROPSTG
*) enumx
;
2505 /***********************************************************************
2508 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
=
2510 IPropertySetStorage_fnQueryInterface
,
2511 IPropertySetStorage_fnAddRef
,
2512 IPropertySetStorage_fnRelease
,
2513 IPropertySetStorage_fnCreate
,
2514 IPropertySetStorage_fnOpen
,
2515 IPropertySetStorage_fnDelete
,
2516 IPropertySetStorage_fnEnum
2519 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
=
2521 IPropertyStorage_fnQueryInterface
,
2522 IPropertyStorage_fnAddRef
,
2523 IPropertyStorage_fnRelease
,
2524 IPropertyStorage_fnReadMultiple
,
2525 IPropertyStorage_fnWriteMultiple
,
2526 IPropertyStorage_fnDeleteMultiple
,
2527 IPropertyStorage_fnReadPropertyNames
,
2528 IPropertyStorage_fnWritePropertyNames
,
2529 IPropertyStorage_fnDeletePropertyNames
,
2530 IPropertyStorage_fnCommit
,
2531 IPropertyStorage_fnRevert
,
2532 IPropertyStorage_fnEnum
,
2533 IPropertyStorage_fnSetTimes
,
2534 IPropertyStorage_fnSetClass
,
2535 IPropertyStorage_fnStat
,
2538 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl
=
2540 IEnumSTATPROPSETSTG_fnQueryInterface
,
2541 IEnumSTATPROPSETSTG_fnAddRef
,
2542 IEnumSTATPROPSETSTG_fnRelease
,
2543 IEnumSTATPROPSETSTG_fnNext
,
2544 IEnumSTATPROPSETSTG_fnSkip
,
2545 IEnumSTATPROPSETSTG_fnReset
,
2546 IEnumSTATPROPSETSTG_fnClone
,
2549 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl
=
2551 IEnumSTATPROPSTG_fnQueryInterface
,
2552 IEnumSTATPROPSTG_fnAddRef
,
2553 IEnumSTATPROPSTG_fnRelease
,
2554 IEnumSTATPROPSTG_fnNext
,
2555 IEnumSTATPROPSTG_fnSkip
,
2556 IEnumSTATPROPSTG_fnReset
,
2557 IEnumSTATPROPSTG_fnClone
,
2560 /***********************************************************************
2561 * Format ID <-> name conversion
2563 static const WCHAR szSummaryInfo
[] = { 5,'S','u','m','m','a','r','y',
2564 'I','n','f','o','r','m','a','t','i','o','n',0 };
2565 static const WCHAR szDocSummaryInfo
[] = { 5,'D','o','c','u','m','e','n','t',
2566 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2568 #define BITS_PER_BYTE 8
2569 #define CHARMASK 0x1f
2570 #define BITS_IN_CHARMASK 5
2571 #define NUM_ALPHA_CHARS 26
2573 /***********************************************************************
2574 * FmtIdToPropStgName [ole32.@]
2575 * Returns the storage name of the format ID rfmtid.
2577 * rfmtid [I] Format ID for which to return a storage name
2578 * str [O] Storage name associated with rfmtid.
2581 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2584 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2586 HRESULT WINAPI
FmtIdToPropStgName(const FMTID
*rfmtid
, LPOLESTR str
)
2588 static const char fmtMap
[] = "abcdefghijklmnopqrstuvwxyz012345";
2590 TRACE("%s, %p\n", debugstr_guid(rfmtid
), str
);
2592 if (!rfmtid
) return E_INVALIDARG
;
2593 if (!str
) return E_INVALIDARG
;
2595 if (IsEqualGUID(&FMTID_SummaryInformation
, rfmtid
))
2596 lstrcpyW(str
, szSummaryInfo
);
2597 else if (IsEqualGUID(&FMTID_DocSummaryInformation
, rfmtid
))
2598 lstrcpyW(str
, szDocSummaryInfo
);
2599 else if (IsEqualGUID(&FMTID_UserDefinedProperties
, rfmtid
))
2600 lstrcpyW(str
, szDocSummaryInfo
);
2605 ULONG bitsRemaining
= BITS_PER_BYTE
;
2608 for (fmtptr
= (const BYTE
*)rfmtid
; fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
); )
2610 ULONG i
= *fmtptr
>> (BITS_PER_BYTE
- bitsRemaining
);
2612 if (bitsRemaining
>= BITS_IN_CHARMASK
)
2614 *pstr
= (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2615 if (bitsRemaining
== BITS_PER_BYTE
&& *pstr
>= 'a' &&
2619 bitsRemaining
-= BITS_IN_CHARMASK
;
2620 if (bitsRemaining
== 0)
2623 bitsRemaining
= BITS_PER_BYTE
;
2628 if (++fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
))
2629 i
|= *fmtptr
<< bitsRemaining
;
2630 *pstr
++ = (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2631 bitsRemaining
+= BITS_PER_BYTE
- BITS_IN_CHARMASK
;
2636 TRACE("returning %s\n", debugstr_w(str
));
2640 /***********************************************************************
2641 * PropStgNameToFmtId [ole32.@]
2642 * Returns the format ID corresponding to the given name.
2644 * str [I] Storage name to convert to a format ID.
2645 * rfmtid [O] Format ID corresponding to str.
2648 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2649 * a format ID, S_OK otherwise.
2651 HRESULT WINAPI
PropStgNameToFmtId(const LPOLESTR str
, FMTID
*rfmtid
)
2653 HRESULT hr
= STG_E_INVALIDNAME
;
2655 TRACE("%s, %p\n", debugstr_w(str
), rfmtid
);
2657 if (!rfmtid
) return E_INVALIDARG
;
2658 if (!str
) return STG_E_INVALIDNAME
;
2660 if (!lstrcmpiW(str
, szDocSummaryInfo
))
2662 *rfmtid
= FMTID_DocSummaryInformation
;
2665 else if (!lstrcmpiW(str
, szSummaryInfo
))
2667 *rfmtid
= FMTID_SummaryInformation
;
2673 BYTE
*fmtptr
= (BYTE
*)rfmtid
- 1;
2674 const WCHAR
*pstr
= str
;
2676 memset(rfmtid
, 0, sizeof(*rfmtid
));
2677 for (bits
= 0; bits
< sizeof(FMTID
) * BITS_PER_BYTE
;
2678 bits
+= BITS_IN_CHARMASK
)
2680 ULONG bitsUsed
= bits
% BITS_PER_BYTE
, bitsStored
;
2686 if (wc
> NUM_ALPHA_CHARS
)
2689 if (wc
> NUM_ALPHA_CHARS
)
2691 wc
+= 'a' - '0' + NUM_ALPHA_CHARS
;
2694 WARN("invalid character (%d)\n", *pstr
);
2699 *fmtptr
|= wc
<< bitsUsed
;
2700 bitsStored
= min(BITS_PER_BYTE
- bitsUsed
, BITS_IN_CHARMASK
);
2701 if (bitsStored
< BITS_IN_CHARMASK
)
2703 wc
>>= BITS_PER_BYTE
- bitsUsed
;
2704 if (bits
+ bitsStored
== sizeof(FMTID
) * BITS_PER_BYTE
)
2708 WARN("extra bits\n");
2714 *fmtptr
|= (BYTE
)wc
;
2723 #ifdef __i386__ /* thiscall functions are i386-specific */
2725 #define DEFINE_STDCALL_WRAPPER(num,func,args) \
2726 __ASM_STDCALL_FUNC(func, args, \
2730 "movl (%ecx), %eax\n\t" \
2731 "jmp *(4*(" #num "))(%eax)" )
2733 DEFINE_STDCALL_WRAPPER(0,Allocate_PMemoryAllocator
,8)
2734 extern void* WINAPI
Allocate_PMemoryAllocator(void *this, ULONG cbSize
);
2738 static void* WINAPI
Allocate_PMemoryAllocator(void *this, ULONG cbSize
)
2740 void* (WINAPI
*fn
)(void*,ULONG
) = **(void***)this;
2741 return fn(this, cbSize
);
2746 BOOLEAN WINAPI
StgConvertPropertyToVariant(const SERIALIZEDPROPERTYVALUE
* prop
,
2747 USHORT CodePage
, PROPVARIANT
* pvar
, void* pma
)
2751 hr
= PropertyStorage_ReadProperty(pvar
, (const BYTE
*)prop
, CodePage
, Allocate_PMemoryAllocator
, pma
);
2755 FIXME("should raise C++ exception on failure\n");
2756 PropVariantInit(pvar
);
2762 SERIALIZEDPROPERTYVALUE
* WINAPI
StgConvertVariantToProperty(const PROPVARIANT
*pvar
,
2763 USHORT CodePage
, SERIALIZEDPROPERTYVALUE
*pprop
, ULONG
*pcb
, PROPID pid
,
2764 BOOLEAN fReserved
, ULONG
*pcIndirect
)
2766 FIXME("%p,%d,%p,%p,%d,%d,%p\n", pvar
, CodePage
, pprop
, pcb
, pid
, fReserved
, pcIndirect
);