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
42 * - IPropertyStorage::Enum is unimplemented
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
59 #include "wine/unicode.h"
60 #include "wine/debug.h"
61 #include "dictionary.h"
62 #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 struct tagPropertyStorage_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(struct tagPropertyStorage_impl
*);
128 static HRESULT
PropertyStorage_WriteToStream(struct tagPropertyStorage_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(
135 struct tagPropertyStorage_impl
*);
137 static void PropertyStorage_DestroyDictionaries(
138 struct tagPropertyStorage_impl
*);
140 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
141 * string using PropertyStorage_StringCopy.
143 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
144 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
);
146 /* Copies the string src, which is encoded using code page srcCP, and returns
147 * it in *dst, in the code page specified by targetCP. The returned string is
148 * allocated using CoTaskMemAlloc.
149 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
150 * is CP_UNICODE, the returned string is in fact an LPWSTR.
151 * Returns S_OK on success, something else on failure.
153 static HRESULT
PropertyStorage_StringCopy(LPCSTR src
, LCID srcCP
, LPSTR
*dst
,
156 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
;
158 /***********************************************************************
159 * Implementation of IPropertyStorage
161 typedef struct tagPropertyStorage_impl
163 const IPropertyStorageVtbl
*vtbl
;
177 struct dictionary
*name_to_propid
;
178 struct dictionary
*propid_to_name
;
179 struct dictionary
*propid_to_prop
;
180 } PropertyStorage_impl
;
182 /************************************************************************
183 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
185 static HRESULT WINAPI
IPropertyStorage_fnQueryInterface(
186 IPropertyStorage
*iface
,
190 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
192 if ( (This
==0) || (ppvObject
==0) )
197 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
198 IsEqualGUID(&IID_IPropertyStorage
, riid
))
200 IPropertyStorage_AddRef(iface
);
201 *ppvObject
= (IPropertyStorage
*)iface
;
205 return E_NOINTERFACE
;
208 /************************************************************************
209 * IPropertyStorage_fnAddRef (IPropertyStorage)
211 static ULONG WINAPI
IPropertyStorage_fnAddRef(
212 IPropertyStorage
*iface
)
214 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
215 return InterlockedIncrement(&This
->ref
);
218 /************************************************************************
219 * IPropertyStorage_fnRelease (IPropertyStorage)
221 static ULONG WINAPI
IPropertyStorage_fnRelease(
222 IPropertyStorage
*iface
)
224 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
227 ref
= InterlockedDecrement(&This
->ref
);
230 TRACE("Destroying %p\n", This
);
232 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
233 IStream_Release(This
->stm
);
234 DeleteCriticalSection(&This
->cs
);
235 PropertyStorage_DestroyDictionaries(This
);
236 HeapFree(GetProcessHeap(), 0, This
);
241 static PROPVARIANT
*PropertyStorage_FindProperty(PropertyStorage_impl
*This
,
244 PROPVARIANT
*ret
= NULL
;
247 dictionary_find(This
->propid_to_prop
, (void *)propid
, (void **)&ret
);
248 TRACE("returning %p\n", ret
);
252 /* Returns NULL if name is NULL. */
253 static PROPVARIANT
*PropertyStorage_FindPropertyByName(
254 PropertyStorage_impl
*This
, LPCWSTR name
)
256 PROPVARIANT
*ret
= NULL
;
262 if (This
->codePage
== CP_UNICODE
)
264 if (dictionary_find(This
->name_to_propid
, name
, (void **)&propid
))
265 ret
= PropertyStorage_FindProperty(This
, (PROPID
)propid
);
270 HRESULT hr
= PropertyStorage_StringCopy((LPCSTR
)name
, CP_UNICODE
,
271 &ansiName
, This
->codePage
);
275 if (dictionary_find(This
->name_to_propid
, ansiName
,
277 ret
= PropertyStorage_FindProperty(This
, (PROPID
)propid
);
278 CoTaskMemFree(ansiName
);
281 TRACE("returning %p\n", ret
);
285 static LPWSTR
PropertyStorage_FindPropertyNameById(PropertyStorage_impl
*This
,
291 dictionary_find(This
->propid_to_name
, (void *)propid
, (void **)&ret
);
292 TRACE("returning %p\n", ret
);
296 /************************************************************************
297 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
299 static HRESULT WINAPI
IPropertyStorage_fnReadMultiple(
300 IPropertyStorage
* iface
,
302 const PROPSPEC rgpspec
[],
303 PROPVARIANT rgpropvar
[])
305 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
309 TRACE("(%p, %ld, %p, %p)\n", iface
, cpspec
, rgpspec
, rgpropvar
);
312 if (cpspec
&& (!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, %ld, %ld\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
);
398 if (srcCP
== CP_UNICODE
)
399 wideStr
= (LPWSTR
)src
;
402 len
= MultiByteToWideChar(srcCP
, 0, src
, -1, NULL
, 0);
403 wideStr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
405 MultiByteToWideChar(srcCP
, 0, src
, -1, wideStr
, len
);
407 hr
= STG_E_INSUFFICIENTMEMORY
;
411 len
= WideCharToMultiByte(dstCP
, 0, wideStr
, -1, NULL
, 0,
413 *dst
= CoTaskMemAlloc(len
);
415 hr
= STG_E_INSUFFICIENTMEMORY
;
418 BOOL defCharUsed
= FALSE
;
420 if (WideCharToMultiByte(dstCP
, 0, wideStr
, -1, *dst
, len
,
421 NULL
, &defCharUsed
) == 0 || defCharUsed
)
425 hr
= HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION
);
429 if (wideStr
!= (LPWSTR
)src
)
430 HeapFree(GetProcessHeap(), 0, wideStr
);
433 TRACE("returning 0x%08lx (%s)\n", hr
,
434 dstCP
== CP_UNICODE
? debugstr_w((LPCWSTR
)*dst
) : debugstr_a(*dst
));
438 static HRESULT
PropertyStorage_PropVariantCopy(PROPVARIANT
*prop
,
439 const PROPVARIANT
*propvar
, LCID targetCP
, LCID srcCP
)
445 if (propvar
->vt
== VT_LPSTR
)
447 hr
= PropertyStorage_StringCopy(propvar
->u
.pszVal
, srcCP
,
448 &prop
->u
.pszVal
, targetCP
);
453 PropVariantCopy(prop
, propvar
);
457 /* Stores the property with id propid and value propvar into this property
458 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
459 * type is VT_LPSTR, converts the string using lcid as the source code page
460 * and This->codePage as the target code page before storing.
461 * As a side effect, may change This->format to 1 if the type of propvar is
462 * a version 1-only property.
464 static HRESULT
PropertyStorage_StorePropWithId(PropertyStorage_impl
*This
,
465 PROPID propid
, const PROPVARIANT
*propvar
, LCID lcid
)
468 PROPVARIANT
*prop
= PropertyStorage_FindProperty(This
, propid
);
472 if (propvar
->vt
& VT_BYREF
|| propvar
->vt
& VT_ARRAY
)
480 case VT_VECTOR
|VT_I1
:
483 TRACE("Setting 0x%08lx to type %d\n", propid
, propvar
->vt
);
486 PropVariantClear(prop
);
487 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
492 prop
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
493 sizeof(PROPVARIANT
));
496 hr
= PropertyStorage_PropVariantCopy(prop
, propvar
, This
->codePage
,
500 dictionary_insert(This
->propid_to_prop
, (void *)propid
, prop
);
501 if (propid
> This
->highestProp
)
502 This
->highestProp
= propid
;
505 HeapFree(GetProcessHeap(), 0, prop
);
508 hr
= STG_E_INSUFFICIENTMEMORY
;
513 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
514 * srcName is encoded in code page cp, and is converted to This->codePage.
515 * If cp is CP_UNICODE, srcName is actually a unicode string.
516 * As a side effect, may change This->format to 1 if srcName is too long for
517 * a version 0 property storage.
518 * Doesn't validate id.
520 static HRESULT
PropertyStorage_StoreNameWithId(PropertyStorage_impl
*This
,
521 LPCSTR srcName
, LCID cp
, PROPID id
)
528 hr
= PropertyStorage_StringCopy((LPCSTR
)srcName
, cp
, &name
, This
->codePage
);
531 if (This
->codePage
== CP_UNICODE
)
533 if (lstrlenW((LPWSTR
)name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
538 if (strlen(name
) >= MAX_VERSION_0_PROP_NAME_LENGTH
)
541 TRACE("Adding prop name %s, propid %ld\n",
542 This
->codePage
== CP_UNICODE
? debugstr_w((LPCWSTR
)name
) :
543 debugstr_a(name
), id
);
544 dictionary_insert(This
->name_to_propid
, name
, (void *)id
);
545 dictionary_insert(This
->propid_to_name
, (void *)id
, name
);
550 /************************************************************************
551 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
553 static HRESULT WINAPI
IPropertyStorage_fnWriteMultiple(
554 IPropertyStorage
* iface
,
556 const PROPSPEC rgpspec
[],
557 const PROPVARIANT rgpropvar
[],
558 PROPID propidNameFirst
)
560 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
564 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
);
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
,
681 (void *)rgpspec
[i
].u
.lpwstr
, (void **)&propid
))
682 dictionary_remove(This
->propid_to_prop
, (void *)propid
);
686 if (rgpspec
[i
].u
.propid
>= PID_FIRST_USABLE
&&
687 rgpspec
[i
].u
.propid
< PID_MIN_READONLY
)
688 dictionary_remove(This
->propid_to_prop
,
689 (void *)rgpspec
[i
].u
.propid
);
691 hr
= STG_E_INVALIDPARAMETER
;
694 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
695 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
696 LeaveCriticalSection(&This
->cs
);
700 /************************************************************************
701 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
703 static HRESULT WINAPI
IPropertyStorage_fnReadPropertyNames(
704 IPropertyStorage
* iface
,
706 const PROPID rgpropid
[],
707 LPOLESTR rglpwstrName
[])
709 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
711 HRESULT hr
= S_FALSE
;
713 TRACE("(%p, %ld, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
716 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
718 EnterCriticalSection(&This
->cs
);
719 for (i
= 0; i
< cpropid
&& SUCCEEDED(hr
); i
++)
721 LPWSTR name
= PropertyStorage_FindPropertyNameById(This
, rgpropid
[i
]);
725 size_t len
= lstrlenW(name
);
728 rglpwstrName
[i
] = CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
));
730 memcpy(rglpwstrName
, name
, (len
+ 1) * sizeof(WCHAR
));
732 hr
= STG_E_INSUFFICIENTMEMORY
;
735 rglpwstrName
[i
] = NULL
;
737 LeaveCriticalSection(&This
->cs
);
741 /************************************************************************
742 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
744 static HRESULT WINAPI
IPropertyStorage_fnWritePropertyNames(
745 IPropertyStorage
* iface
,
747 const PROPID rgpropid
[],
748 const LPOLESTR rglpwstrName
[])
750 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
754 TRACE("(%p, %ld, %p, %p)\n", iface
, cpropid
, rgpropid
, rglpwstrName
);
757 if (cpropid
&& (!rgpropid
|| !rglpwstrName
))
759 if (!(This
->grfMode
& STGM_READWRITE
))
760 return STG_E_ACCESSDENIED
;
762 EnterCriticalSection(&This
->cs
);
764 for (i
= 0; SUCCEEDED(hr
) && i
< cpropid
; i
++)
766 if (rgpropid
[i
] != PID_ILLEGAL
)
767 hr
= PropertyStorage_StoreNameWithId(This
, (LPCSTR
)rglpwstrName
[i
],
768 CP_UNICODE
, rgpropid
[i
]);
770 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
771 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
772 LeaveCriticalSection(&This
->cs
);
776 /************************************************************************
777 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
779 static HRESULT WINAPI
IPropertyStorage_fnDeletePropertyNames(
780 IPropertyStorage
* iface
,
782 const PROPID rgpropid
[])
784 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
788 TRACE("(%p, %ld, %p)\n", iface
, cpropid
, rgpropid
);
791 if (cpropid
&& !rgpropid
)
793 if (!(This
->grfMode
& STGM_READWRITE
))
794 return STG_E_ACCESSDENIED
;
796 EnterCriticalSection(&This
->cs
);
798 for (i
= 0; i
< cpropid
; i
++)
802 if (dictionary_find(This
->propid_to_name
, (void *)rgpropid
[i
],
805 dictionary_remove(This
->propid_to_name
, (void *)rgpropid
[i
]);
806 dictionary_remove(This
->name_to_propid
, name
);
809 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
810 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
811 LeaveCriticalSection(&This
->cs
);
815 /************************************************************************
816 * IPropertyStorage_fnCommit (IPropertyStorage)
818 static HRESULT WINAPI
IPropertyStorage_fnCommit(
819 IPropertyStorage
* iface
,
820 DWORD grfCommitFlags
)
822 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
825 TRACE("(%p, 0x%08lx)\n", iface
, grfCommitFlags
);
828 if (!(This
->grfMode
& STGM_READWRITE
))
829 return STG_E_ACCESSDENIED
;
830 EnterCriticalSection(&This
->cs
);
832 hr
= PropertyStorage_WriteToStream(This
);
835 LeaveCriticalSection(&This
->cs
);
839 /************************************************************************
840 * IPropertyStorage_fnRevert (IPropertyStorage)
842 static HRESULT WINAPI
IPropertyStorage_fnRevert(
843 IPropertyStorage
* iface
)
846 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
848 TRACE("%p\n", iface
);
852 EnterCriticalSection(&This
->cs
);
855 PropertyStorage_DestroyDictionaries(This
);
856 hr
= PropertyStorage_CreateDictionaries(This
);
858 hr
= PropertyStorage_ReadFromStream(This
);
862 LeaveCriticalSection(&This
->cs
);
866 /************************************************************************
867 * IPropertyStorage_fnEnum (IPropertyStorage)
869 static HRESULT WINAPI
IPropertyStorage_fnEnum(
870 IPropertyStorage
* iface
,
871 IEnumSTATPROPSTG
** ppenum
)
877 /************************************************************************
878 * IPropertyStorage_fnSetTimes (IPropertyStorage)
880 static HRESULT WINAPI
IPropertyStorage_fnSetTimes(
881 IPropertyStorage
* iface
,
882 const FILETIME
* pctime
,
883 const FILETIME
* patime
,
884 const FILETIME
* pmtime
)
890 /************************************************************************
891 * IPropertyStorage_fnSetClass (IPropertyStorage)
893 static HRESULT WINAPI
IPropertyStorage_fnSetClass(
894 IPropertyStorage
* iface
,
897 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
899 TRACE("%p, %s\n", iface
, debugstr_guid(clsid
));
902 if (!(This
->grfMode
& STGM_READWRITE
))
903 return STG_E_ACCESSDENIED
;
904 memcpy(&This
->clsid
, clsid
, sizeof(This
->clsid
));
906 if (This
->grfFlags
& PROPSETFLAG_UNBUFFERED
)
907 IPropertyStorage_Commit(iface
, STGC_DEFAULT
);
911 /************************************************************************
912 * IPropertyStorage_fnStat (IPropertyStorage)
914 static HRESULT WINAPI
IPropertyStorage_fnStat(
915 IPropertyStorage
* iface
,
916 STATPROPSETSTG
* statpsstg
)
918 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)iface
;
922 TRACE("%p, %p\n", iface
, statpsstg
);
923 if (!This
|| !statpsstg
)
926 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
929 memcpy(&statpsstg
->fmtid
, &This
->fmtid
, sizeof(statpsstg
->fmtid
));
930 memcpy(&statpsstg
->clsid
, &This
->clsid
, sizeof(statpsstg
->clsid
));
931 statpsstg
->grfFlags
= This
->grfFlags
;
932 memcpy(&statpsstg
->mtime
, &stat
.mtime
, sizeof(statpsstg
->mtime
));
933 memcpy(&statpsstg
->ctime
, &stat
.ctime
, sizeof(statpsstg
->ctime
));
934 memcpy(&statpsstg
->atime
, &stat
.atime
, sizeof(statpsstg
->atime
));
935 statpsstg
->dwOSVersion
= This
->originatorOS
;
940 static int PropertyStorage_PropNameCompare(const void *a
, const void *b
,
943 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
945 if (This
->codePage
== CP_UNICODE
)
947 TRACE("(%s, %s)\n", debugstr_w(a
), debugstr_w(b
));
948 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
949 return lstrcmpW((LPCWSTR
)a
, (LPCWSTR
)b
);
951 return lstrcmpiW((LPCWSTR
)a
, (LPCWSTR
)b
);
955 TRACE("(%s, %s)\n", debugstr_a(a
), debugstr_a(b
));
956 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
957 return lstrcmpA((LPCSTR
)a
, (LPCSTR
)b
);
959 return lstrcmpiA((LPCSTR
)a
, (LPCSTR
)b
);
963 static void PropertyStorage_PropNameDestroy(void *k
, void *d
, void *extra
)
968 static int PropertyStorage_PropCompare(const void *a
, const void *b
,
971 TRACE("(%ld, %ld)\n", (PROPID
)a
, (PROPID
)b
);
972 return (PROPID
)a
- (PROPID
)b
;
975 static void PropertyStorage_PropertyDestroy(void *k
, void *d
, void *extra
)
977 PropVariantClear((PROPVARIANT
*)d
);
978 HeapFree(GetProcessHeap(), 0, d
);
981 #ifdef WORDS_BIGENDIAN
982 /* Swaps each character in str to or from little endian; assumes the conversion
983 * is symmetric, that is, that le16toh is equivalent to htole16.
985 static void PropertyStorage_ByteSwapString(LPWSTR str
, size_t len
)
989 /* Swap characters to host order.
992 for (i
= 0; i
< len
; i
++)
993 str
[i
] = le16toh(str
[i
]);
996 #define PropertyStorage_ByteSwapString(s, l)
999 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
1000 * the entries according to the values of This->codePage and This->locale.
1001 * FIXME: there isn't any checking whether the read property extends past the
1002 * end of the buffer.
1004 static HRESULT
PropertyStorage_ReadDictionary(PropertyStorage_impl
*This
,
1007 DWORD numEntries
, i
;
1011 assert(This
->name_to_propid
);
1012 assert(This
->propid_to_name
);
1014 StorageUtl_ReadDWord(ptr
, 0, &numEntries
);
1015 TRACE("Reading %ld entries:\n", numEntries
);
1016 ptr
+= sizeof(DWORD
);
1017 for (i
= 0; SUCCEEDED(hr
) && i
< numEntries
; i
++)
1022 StorageUtl_ReadDWord(ptr
, 0, &propid
);
1023 ptr
+= sizeof(PROPID
);
1024 StorageUtl_ReadDWord(ptr
, 0, &cbEntry
);
1025 ptr
+= sizeof(DWORD
);
1026 TRACE("Reading entry with ID 0x%08lx, %ld bytes\n", propid
, cbEntry
);
1027 /* Make sure the source string is NULL-terminated */
1028 if (This
->codePage
!= CP_UNICODE
)
1029 ptr
[cbEntry
- 1] = '\0';
1031 *((LPWSTR
)ptr
+ cbEntry
/ sizeof(WCHAR
)) = '\0';
1032 hr
= PropertyStorage_StoreNameWithId(This
, (char*)ptr
, This
->codePage
, propid
);
1033 if (This
->codePage
== CP_UNICODE
)
1035 /* Unicode entries are padded to DWORD boundaries */
1036 if (cbEntry
% sizeof(DWORD
))
1037 ptr
+= sizeof(DWORD
) - (cbEntry
% sizeof(DWORD
));
1039 ptr
+= sizeof(DWORD
) + cbEntry
;
1044 /* FIXME: there isn't any checking whether the read property extends past the
1045 * end of the buffer.
1047 static HRESULT
PropertyStorage_ReadProperty(PropertyStorage_impl
*This
,
1048 PROPVARIANT
*prop
, const BYTE
*data
)
1054 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->vt
);
1055 data
+= sizeof(DWORD
);
1062 prop
->u
.cVal
= *(const char *)data
;
1063 TRACE("Read char 0x%x ('%c')\n", prop
->u
.cVal
, prop
->u
.cVal
);
1066 prop
->u
.bVal
= *(const UCHAR
*)data
;
1067 TRACE("Read byte 0x%x\n", prop
->u
.bVal
);
1070 StorageUtl_ReadWord(data
, 0, (WORD
*)&prop
->u
.iVal
);
1071 TRACE("Read short %d\n", prop
->u
.iVal
);
1074 StorageUtl_ReadWord(data
, 0, &prop
->u
.uiVal
);
1075 TRACE("Read ushort %d\n", prop
->u
.uiVal
);
1079 StorageUtl_ReadDWord(data
, 0, (DWORD
*)&prop
->u
.lVal
);
1080 TRACE("Read long %ld\n", prop
->u
.lVal
);
1084 StorageUtl_ReadDWord(data
, 0, &prop
->u
.ulVal
);
1085 TRACE("Read ulong %ld\n", prop
->u
.ulVal
);
1091 StorageUtl_ReadDWord(data
, 0, &count
);
1092 if (This
->codePage
== CP_UNICODE
&& count
/ 2)
1094 WARN("Unicode string has odd number of bytes\n");
1095 hr
= STG_E_INVALIDHEADER
;
1099 prop
->u
.pszVal
= CoTaskMemAlloc(count
);
1102 memcpy(prop
->u
.pszVal
, data
+ sizeof(DWORD
), count
);
1103 /* This is stored in the code page specified in This->codePage.
1104 * Don't convert it, the caller will just store it as-is.
1106 if (This
->codePage
== CP_UNICODE
)
1108 /* Make sure it's NULL-terminated */
1109 prop
->u
.pszVal
[count
/ sizeof(WCHAR
) - 1] = '\0';
1110 TRACE("Read string value %s\n",
1111 debugstr_w(prop
->u
.pwszVal
));
1115 /* Make sure it's NULL-terminated */
1116 prop
->u
.pszVal
[count
- 1] = '\0';
1117 TRACE("Read string value %s\n", debugstr_a(prop
->u
.pszVal
));
1121 hr
= STG_E_INSUFFICIENTMEMORY
;
1129 StorageUtl_ReadDWord(data
, 0, &count
);
1130 prop
->u
.pwszVal
= CoTaskMemAlloc(count
* sizeof(WCHAR
));
1131 if (prop
->u
.pwszVal
)
1133 memcpy(prop
->u
.pwszVal
, data
+ sizeof(DWORD
),
1134 count
* sizeof(WCHAR
));
1135 /* make sure string is NULL-terminated */
1136 prop
->u
.pwszVal
[count
- 1] = '\0';
1137 PropertyStorage_ByteSwapString(prop
->u
.pwszVal
, count
);
1138 TRACE("Read string value %s\n", debugstr_w(prop
->u
.pwszVal
));
1141 hr
= STG_E_INSUFFICIENTMEMORY
;
1145 StorageUtl_ReadULargeInteger(data
, 0,
1146 (ULARGE_INTEGER
*)&prop
->u
.filetime
);
1150 DWORD len
= 0, type
= 0, tag
= 0;
1152 StorageUtl_ReadDWord(data
, 0, &len
);
1153 StorageUtl_ReadDWord(data
, 4, &tag
);
1154 StorageUtl_ReadDWord(data
, 8, &type
);
1155 if (tag
== CFTAG_WINDOWS
&& len
> 12)
1157 prop
->u
.pclipdata
= CoTaskMemAlloc(sizeof (CLIPDATA
));
1158 prop
->u
.pclipdata
->cbSize
= len
;
1159 prop
->u
.pclipdata
->ulClipFmt
= type
;
1160 prop
->u
.pclipdata
->pClipData
= CoTaskMemAlloc(len
);
1161 memcpy(prop
->u
.pclipdata
->pClipData
, data
+12, len
- 12);
1162 TRACE("returning %p, len %ld\n", prop
->u
.pclipdata
->pClipData
, len
- 12);
1165 hr
= STG_E_INVALIDPARAMETER
;
1169 FIXME("unsupported type %d\n", prop
->vt
);
1170 hr
= STG_E_INVALIDPARAMETER
;
1175 static HRESULT
PropertyStorage_ReadHeaderFromStream(IStream
*stm
,
1176 PROPERTYSETHEADER
*hdr
)
1178 BYTE buf
[sizeof(PROPERTYSETHEADER
)];
1184 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1187 if (count
!= sizeof(buf
))
1189 WARN("read only %ld\n", count
);
1190 hr
= STG_E_INVALIDHEADER
;
1194 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wByteOrder
),
1196 StorageUtl_ReadWord(buf
, offsetof(PROPERTYSETHEADER
, wFormat
),
1198 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, dwOSVer
),
1200 StorageUtl_ReadGUID(buf
, offsetof(PROPERTYSETHEADER
, clsid
),
1202 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSETHEADER
, reserved
),
1206 TRACE("returning 0x%08lx\n", hr
);
1210 static HRESULT
PropertyStorage_ReadFmtIdOffsetFromStream(IStream
*stm
,
1211 FORMATIDOFFSET
*fmt
)
1213 BYTE buf
[sizeof(FORMATIDOFFSET
)];
1219 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1222 if (count
!= sizeof(buf
))
1224 WARN("read only %ld\n", count
);
1225 hr
= STG_E_INVALIDHEADER
;
1229 StorageUtl_ReadGUID(buf
, offsetof(FORMATIDOFFSET
, fmtid
),
1231 StorageUtl_ReadDWord(buf
, offsetof(FORMATIDOFFSET
, dwOffset
),
1235 TRACE("returning 0x%08lx\n", hr
);
1239 static HRESULT
PropertyStorage_ReadSectionHeaderFromStream(IStream
*stm
,
1240 PROPERTYSECTIONHEADER
*hdr
)
1242 BYTE buf
[sizeof(PROPERTYSECTIONHEADER
)];
1248 hr
= IStream_Read(stm
, buf
, sizeof(buf
), &count
);
1251 if (count
!= sizeof(buf
))
1253 WARN("read only %ld\n", count
);
1254 hr
= STG_E_INVALIDHEADER
;
1258 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1259 cbSection
), &hdr
->cbSection
);
1260 StorageUtl_ReadDWord(buf
, offsetof(PROPERTYSECTIONHEADER
,
1261 cProperties
), &hdr
->cProperties
);
1264 TRACE("returning 0x%08lx\n", hr
);
1268 static HRESULT
PropertyStorage_ReadFromStream(PropertyStorage_impl
*This
)
1270 PROPERTYSETHEADER hdr
;
1271 FORMATIDOFFSET fmtOffset
;
1272 PROPERTYSECTIONHEADER sectionHdr
;
1279 DWORD dictOffset
= 0;
1282 This
->dirty
= FALSE
;
1283 This
->highestProp
= 0;
1284 hr
= IStream_Stat(This
->stm
, &stat
, STATFLAG_NONAME
);
1287 if (stat
.cbSize
.u
.HighPart
)
1289 WARN("stream too big\n");
1290 /* maximum size varies, but it can't be this big */
1291 hr
= STG_E_INVALIDHEADER
;
1294 if (stat
.cbSize
.u
.LowPart
== 0)
1296 /* empty stream is okay */
1300 else if (stat
.cbSize
.u
.LowPart
< sizeof(PROPERTYSETHEADER
) +
1301 sizeof(FORMATIDOFFSET
))
1303 WARN("stream too small\n");
1304 hr
= STG_E_INVALIDHEADER
;
1308 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1311 hr
= PropertyStorage_ReadHeaderFromStream(This
->stm
, &hdr
);
1312 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1315 if (hdr
.wByteOrder
!= PROPSETHDR_BYTEORDER_MAGIC
|| hdr
.reserved
< 1)
1317 WARN("bad magic in prop set header\n");
1318 hr
= STG_E_INVALIDHEADER
;
1321 if (hdr
.wFormat
!= 0 && hdr
.wFormat
!= 1)
1323 WARN("bad format version %d\n", hdr
.wFormat
);
1324 hr
= STG_E_INVALIDHEADER
;
1327 This
->format
= hdr
.wFormat
;
1328 memcpy(&This
->clsid
, &hdr
.clsid
, sizeof(This
->clsid
));
1329 This
->originatorOS
= hdr
.dwOSVer
;
1330 if (PROPSETHDR_OSVER_KIND(hdr
.dwOSVer
) == PROPSETHDR_OSVER_KIND_MAC
)
1331 WARN("File comes from a Mac, strings will probably be screwed up\n");
1332 hr
= PropertyStorage_ReadFmtIdOffsetFromStream(This
->stm
, &fmtOffset
);
1335 if (fmtOffset
.dwOffset
> stat
.cbSize
.u
.LowPart
)
1337 WARN("invalid offset %ld (stream length is %ld)\n", fmtOffset
.dwOffset
,
1338 stat
.cbSize
.u
.LowPart
);
1339 hr
= STG_E_INVALIDHEADER
;
1342 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1343 * follow not one, but two sections. The first is the standard properties
1344 * for the document summary information, and the second is user-defined
1345 * properties. This is the only case in which multiple sections are
1347 * Reading the second stream isn't implemented yet.
1349 hr
= PropertyStorage_ReadSectionHeaderFromStream(This
->stm
, §ionHdr
);
1352 /* The section size includes the section header, so check it */
1353 if (sectionHdr
.cbSection
< sizeof(PROPERTYSECTIONHEADER
))
1355 WARN("section header too small, got %ld\n", sectionHdr
.cbSection
);
1356 hr
= STG_E_INVALIDHEADER
;
1359 buf
= HeapAlloc(GetProcessHeap(), 0, sectionHdr
.cbSection
-
1360 sizeof(PROPERTYSECTIONHEADER
));
1363 hr
= STG_E_INSUFFICIENTMEMORY
;
1366 hr
= IStream_Read(This
->stm
, buf
, sectionHdr
.cbSection
-
1367 sizeof(PROPERTYSECTIONHEADER
), &count
);
1370 TRACE("Reading %ld properties:\n", sectionHdr
.cProperties
);
1371 for (i
= 0; SUCCEEDED(hr
) && i
< sectionHdr
.cProperties
; i
++)
1373 PROPERTYIDOFFSET
*idOffset
= (PROPERTYIDOFFSET
*)(buf
+
1374 i
* sizeof(PROPERTYIDOFFSET
));
1376 if (idOffset
->dwOffset
< sizeof(PROPERTYSECTIONHEADER
) ||
1377 idOffset
->dwOffset
>= sectionHdr
.cbSection
- sizeof(DWORD
))
1378 hr
= STG_E_INVALIDPOINTER
;
1381 if (idOffset
->propid
>= PID_FIRST_USABLE
&&
1382 idOffset
->propid
< PID_MIN_READONLY
&& idOffset
->propid
>
1384 This
->highestProp
= idOffset
->propid
;
1385 if (idOffset
->propid
== PID_DICTIONARY
)
1387 /* Don't read the dictionary yet, its entries depend on the
1388 * code page. Just store the offset so we know to read it
1391 dictOffset
= idOffset
->dwOffset
;
1392 TRACE("Dictionary offset is %ld\n", dictOffset
);
1398 if (SUCCEEDED(PropertyStorage_ReadProperty(This
, &prop
,
1399 buf
+ idOffset
->dwOffset
- sizeof(PROPERTYSECTIONHEADER
))))
1401 TRACE("Read property with ID 0x%08lx, type %d\n",
1402 idOffset
->propid
, prop
.vt
);
1403 switch(idOffset
->propid
)
1406 if (prop
.vt
== VT_I2
)
1407 This
->codePage
= (UINT
)prop
.u
.iVal
;
1410 if (prop
.vt
== VT_I4
)
1411 This
->locale
= (LCID
)prop
.u
.lVal
;
1414 if (prop
.vt
== VT_I4
&& prop
.u
.lVal
)
1415 This
->grfFlags
|= PROPSETFLAG_CASE_SENSITIVE
;
1416 /* The format should already be 1, but just in case */
1420 hr
= PropertyStorage_StorePropWithId(This
,
1421 idOffset
->propid
, &prop
, This
->codePage
);
1427 if (!This
->codePage
)
1429 /* default to Unicode unless told not to, as specified here:
1430 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
1432 if (This
->grfFlags
& PROPSETFLAG_ANSI
)
1433 This
->codePage
= GetACP();
1435 This
->codePage
= CP_UNICODE
;
1438 This
->locale
= LOCALE_SYSTEM_DEFAULT
;
1439 TRACE("Code page is %d, locale is %ld\n", This
->codePage
, This
->locale
);
1441 hr
= PropertyStorage_ReadDictionary(This
,
1442 buf
+ dictOffset
- sizeof(PROPERTYSECTIONHEADER
));
1445 HeapFree(GetProcessHeap(), 0, buf
);
1448 dictionary_destroy(This
->name_to_propid
);
1449 This
->name_to_propid
= NULL
;
1450 dictionary_destroy(This
->propid_to_name
);
1451 This
->propid_to_name
= NULL
;
1452 dictionary_destroy(This
->propid_to_prop
);
1453 This
->propid_to_prop
= NULL
;
1458 static void PropertyStorage_MakeHeader(PropertyStorage_impl
*This
,
1459 PROPERTYSETHEADER
*hdr
)
1463 StorageUtl_WriteWord((BYTE
*)&hdr
->wByteOrder
, 0,
1464 PROPSETHDR_BYTEORDER_MAGIC
);
1465 StorageUtl_WriteWord((BYTE
*)&hdr
->wFormat
, 0, This
->format
);
1466 StorageUtl_WriteDWord((BYTE
*)&hdr
->dwOSVer
, 0, This
->originatorOS
);
1467 StorageUtl_WriteGUID((BYTE
*)&hdr
->clsid
, 0, &This
->clsid
);
1468 StorageUtl_WriteDWord((BYTE
*)&hdr
->reserved
, 0, 1);
1471 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl
*This
,
1472 FORMATIDOFFSET
*fmtOffset
)
1476 StorageUtl_WriteGUID((BYTE
*)fmtOffset
, 0, &This
->fmtid
);
1477 StorageUtl_WriteDWord((BYTE
*)fmtOffset
, offsetof(FORMATIDOFFSET
, dwOffset
),
1478 sizeof(PROPERTYSETHEADER
) + sizeof(FORMATIDOFFSET
));
1481 static void PropertyStorage_MakeSectionHdr(DWORD cbSection
, DWORD numProps
,
1482 PROPERTYSECTIONHEADER
*hdr
)
1485 StorageUtl_WriteDWord((BYTE
*)hdr
, 0, cbSection
);
1486 StorageUtl_WriteDWord((BYTE
*)hdr
,
1487 offsetof(PROPERTYSECTIONHEADER
, cProperties
), numProps
);
1490 static void PropertyStorage_MakePropertyIdOffset(DWORD propid
, DWORD dwOffset
,
1491 PROPERTYIDOFFSET
*propIdOffset
)
1493 assert(propIdOffset
);
1494 StorageUtl_WriteDWord((BYTE
*)propIdOffset
, 0, propid
);
1495 StorageUtl_WriteDWord((BYTE
*)propIdOffset
,
1496 offsetof(PROPERTYIDOFFSET
, dwOffset
), dwOffset
);
1499 struct DictionaryClosure
1505 static BOOL
PropertyStorage_DictionaryWriter(const void *key
,
1506 const void *value
, void *extra
, void *closure
)
1508 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
1509 struct DictionaryClosure
*c
= (struct DictionaryClosure
*)closure
;
1516 StorageUtl_WriteDWord((LPBYTE
)&propid
, 0, (DWORD
)value
);
1517 c
->hr
= IStream_Write(This
->stm
, &propid
, sizeof(propid
), &count
);
1520 c
->bytesWritten
+= sizeof(DWORD
);
1521 if (This
->codePage
== CP_UNICODE
)
1523 DWORD keyLen
, pad
= 0;
1525 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0,
1526 (lstrlenW((LPWSTR
)key
) + 1) * sizeof(WCHAR
));
1527 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1530 c
->bytesWritten
+= sizeof(DWORD
);
1531 /* Rather than allocate a copy, I'll swap the string to little-endian
1532 * in-place, write it, then swap it back.
1534 PropertyStorage_ByteSwapString(key
, keyLen
);
1535 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1536 PropertyStorage_ByteSwapString(key
, keyLen
);
1539 c
->bytesWritten
+= keyLen
;
1540 if (keyLen
% sizeof(DWORD
))
1542 c
->hr
= IStream_Write(This
->stm
, &pad
,
1543 sizeof(DWORD
) - keyLen
% sizeof(DWORD
), &count
);
1546 c
->bytesWritten
+= sizeof(DWORD
) - keyLen
% sizeof(DWORD
);
1553 StorageUtl_WriteDWord((LPBYTE
)&keyLen
, 0, strlen((LPCSTR
)key
) + 1);
1554 c
->hr
= IStream_Write(This
->stm
, &keyLen
, sizeof(keyLen
), &count
);
1557 c
->bytesWritten
+= sizeof(DWORD
);
1558 c
->hr
= IStream_Write(This
->stm
, key
, keyLen
, &count
);
1561 c
->bytesWritten
+= keyLen
;
1564 return SUCCEEDED(c
->hr
);
1567 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1569 /* Writes the dictionary to the stream. Assumes without checking that the
1570 * dictionary isn't empty.
1572 static HRESULT
PropertyStorage_WriteDictionaryToStream(
1573 PropertyStorage_impl
*This
, DWORD
*sectionOffset
)
1577 PROPERTYIDOFFSET propIdOffset
;
1580 struct DictionaryClosure closure
;
1583 assert(sectionOffset
);
1585 /* The dictionary's always the first property written, so seek to its
1588 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
);
1589 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1592 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY
, *sectionOffset
,
1594 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1598 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1599 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1602 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0,
1603 dictionary_num_entries(This
->name_to_propid
));
1604 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1607 *sectionOffset
+= sizeof(dwTemp
);
1610 closure
.bytesWritten
= 0;
1611 dictionary_enumerate(This
->name_to_propid
, PropertyStorage_DictionaryWriter
,
1616 *sectionOffset
+= closure
.bytesWritten
;
1617 if (closure
.bytesWritten
% sizeof(DWORD
))
1619 TRACE("adding %ld bytes of padding\n", sizeof(DWORD
) -
1620 closure
.bytesWritten
% sizeof(DWORD
));
1621 *sectionOffset
+= sizeof(DWORD
) - closure
.bytesWritten
% sizeof(DWORD
);
1628 static HRESULT
PropertyStorage_WritePropertyToStream(PropertyStorage_impl
*This
,
1629 DWORD propNum
, DWORD propid
, PROPVARIANT
*var
, DWORD
*sectionOffset
)
1633 PROPERTYIDOFFSET propIdOffset
;
1635 DWORD dwType
, bytesWritten
;
1639 assert(sectionOffset
);
1641 TRACE("%p, %ld, 0x%08lx, (%d), (%ld)\n", This
, propNum
, propid
, var
->vt
,
1644 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ sizeof(PROPERTYSECTIONHEADER
) +
1645 propNum
* sizeof(PROPERTYIDOFFSET
);
1646 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1649 PropertyStorage_MakePropertyIdOffset(propid
, *sectionOffset
, &propIdOffset
);
1650 hr
= IStream_Write(This
->stm
, &propIdOffset
, sizeof(propIdOffset
), &count
);
1654 seek
.QuadPart
= SECTIONHEADER_OFFSET
+ *sectionOffset
;
1655 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1658 StorageUtl_WriteDWord((LPBYTE
)&dwType
, 0, var
->vt
);
1659 hr
= IStream_Write(This
->stm
, &dwType
, sizeof(dwType
), &count
);
1662 *sectionOffset
+= sizeof(dwType
);
1672 hr
= IStream_Write(This
->stm
, &var
->u
.cVal
, sizeof(var
->u
.cVal
),
1674 bytesWritten
= count
;
1681 StorageUtl_WriteWord((LPBYTE
)&wTemp
, 0, var
->u
.iVal
);
1682 hr
= IStream_Write(This
->stm
, &wTemp
, sizeof(wTemp
), &count
);
1683 bytesWritten
= count
;
1691 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, var
->u
.lVal
);
1692 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1693 bytesWritten
= count
;
1700 if (This
->codePage
== CP_UNICODE
)
1701 len
= (lstrlenW(var
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
1703 len
= lstrlenA(var
->u
.pszVal
) + 1;
1704 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1705 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1708 hr
= IStream_Write(This
->stm
, var
->u
.pszVal
, len
, &count
);
1709 bytesWritten
= count
+ sizeof(DWORD
);
1714 DWORD len
= lstrlenW(var
->u
.pwszVal
) + 1, dwTemp
;
1716 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, len
);
1717 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1720 hr
= IStream_Write(This
->stm
, var
->u
.pwszVal
, len
* sizeof(WCHAR
),
1722 bytesWritten
= count
+ sizeof(DWORD
);
1729 StorageUtl_WriteULargeInteger((BYTE
*)&temp
, 0,
1730 (ULARGE_INTEGER
*)&var
->u
.filetime
);
1731 hr
= IStream_Write(This
->stm
, &temp
, sizeof(FILETIME
), &count
);
1732 bytesWritten
= count
;
1736 FIXME("unsupported type: %d\n", var
->vt
);
1737 return STG_E_INVALIDPARAMETER
;
1742 *sectionOffset
+= bytesWritten
;
1743 if (bytesWritten
% sizeof(DWORD
))
1745 TRACE("adding %ld bytes of padding\n", sizeof(DWORD
) -
1746 bytesWritten
% sizeof(DWORD
));
1747 *sectionOffset
+= sizeof(DWORD
) - bytesWritten
% sizeof(DWORD
);
1755 struct PropertyClosure
1759 DWORD
*sectionOffset
;
1762 static BOOL
PropertyStorage_PropertiesWriter(const void *key
, const void *value
,
1763 void *extra
, void *closure
)
1765 PropertyStorage_impl
*This
= (PropertyStorage_impl
*)extra
;
1766 struct PropertyClosure
*c
= (struct PropertyClosure
*)closure
;
1772 c
->hr
= PropertyStorage_WritePropertyToStream(This
,
1773 c
->propNum
++, (DWORD
)key
, (PROPVARIANT
*)value
, c
->sectionOffset
);
1774 return SUCCEEDED(c
->hr
);
1777 static HRESULT
PropertyStorage_WritePropertiesToStream(
1778 PropertyStorage_impl
*This
, DWORD startingPropNum
, DWORD
*sectionOffset
)
1780 struct PropertyClosure closure
;
1783 assert(sectionOffset
);
1785 closure
.propNum
= startingPropNum
;
1786 closure
.sectionOffset
= sectionOffset
;
1787 dictionary_enumerate(This
->propid_to_prop
, PropertyStorage_PropertiesWriter
,
1792 static HRESULT
PropertyStorage_WriteHeadersToStream(PropertyStorage_impl
*This
)
1796 LARGE_INTEGER seek
= { {0} };
1797 PROPERTYSETHEADER hdr
;
1798 FORMATIDOFFSET fmtOffset
;
1801 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1804 PropertyStorage_MakeHeader(This
, &hdr
);
1805 hr
= IStream_Write(This
->stm
, &hdr
, sizeof(hdr
), &count
);
1808 if (count
!= sizeof(hdr
))
1810 hr
= STG_E_WRITEFAULT
;
1814 PropertyStorage_MakeFmtIdOffset(This
, &fmtOffset
);
1815 hr
= IStream_Write(This
->stm
, &fmtOffset
, sizeof(fmtOffset
), &count
);
1818 if (count
!= sizeof(fmtOffset
))
1820 hr
= STG_E_WRITEFAULT
;
1829 static HRESULT
PropertyStorage_WriteToStream(PropertyStorage_impl
*This
)
1831 PROPERTYSECTIONHEADER sectionHdr
;
1835 DWORD numProps
, prop
, sectionOffset
, dwTemp
;
1840 PropertyStorage_WriteHeadersToStream(This
);
1842 /* Count properties. Always at least one property, the code page */
1844 if (dictionary_num_entries(This
->name_to_propid
))
1846 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1848 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1850 numProps
+= dictionary_num_entries(This
->propid_to_prop
);
1852 /* Write section header with 0 bytes right now, I'll adjust it after
1853 * writing properties.
1855 PropertyStorage_MakeSectionHdr(0, numProps
, §ionHdr
);
1856 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1857 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1860 hr
= IStream_Write(This
->stm
, §ionHdr
, sizeof(sectionHdr
), &count
);
1865 sectionOffset
= sizeof(PROPERTYSECTIONHEADER
) +
1866 numProps
* sizeof(PROPERTYIDOFFSET
);
1868 if (dictionary_num_entries(This
->name_to_propid
))
1871 hr
= PropertyStorage_WriteDictionaryToStream(This
, §ionOffset
);
1876 PropVariantInit(&var
);
1879 var
.u
.iVal
= This
->codePage
;
1880 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_CODEPAGE
,
1881 &var
, §ionOffset
);
1885 if (This
->locale
!= LOCALE_SYSTEM_DEFAULT
)
1888 var
.u
.lVal
= This
->locale
;
1889 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_LOCALE
,
1890 &var
, §ionOffset
);
1895 if (This
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
1899 hr
= PropertyStorage_WritePropertyToStream(This
, prop
++, PID_BEHAVIOR
,
1900 &var
, §ionOffset
);
1905 hr
= PropertyStorage_WritePropertiesToStream(This
, prop
, §ionOffset
);
1909 /* Now write the byte count of the section */
1910 seek
.QuadPart
= SECTIONHEADER_OFFSET
;
1911 hr
= IStream_Seek(This
->stm
, seek
, STREAM_SEEK_SET
, NULL
);
1914 StorageUtl_WriteDWord((LPBYTE
)&dwTemp
, 0, sectionOffset
);
1915 hr
= IStream_Write(This
->stm
, &dwTemp
, sizeof(dwTemp
), &count
);
1921 /***********************************************************************
1922 * PropertyStorage_Construct
1924 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl
*This
)
1927 dictionary_destroy(This
->name_to_propid
);
1928 This
->name_to_propid
= NULL
;
1929 dictionary_destroy(This
->propid_to_name
);
1930 This
->propid_to_name
= NULL
;
1931 dictionary_destroy(This
->propid_to_prop
);
1932 This
->propid_to_prop
= NULL
;
1935 static HRESULT
PropertyStorage_CreateDictionaries(PropertyStorage_impl
*This
)
1940 This
->name_to_propid
= dictionary_create(
1941 PropertyStorage_PropNameCompare
, PropertyStorage_PropNameDestroy
,
1943 if (!This
->name_to_propid
)
1945 hr
= STG_E_INSUFFICIENTMEMORY
;
1948 This
->propid_to_name
= dictionary_create(PropertyStorage_PropCompare
,
1950 if (!This
->propid_to_name
)
1952 hr
= STG_E_INSUFFICIENTMEMORY
;
1955 This
->propid_to_prop
= dictionary_create(PropertyStorage_PropCompare
,
1956 PropertyStorage_PropertyDestroy
, This
);
1957 if (!This
->propid_to_prop
)
1959 hr
= STG_E_INSUFFICIENTMEMORY
;
1964 PropertyStorage_DestroyDictionaries(This
);
1968 static HRESULT
PropertyStorage_BaseConstruct(IStream
*stm
,
1969 REFFMTID rfmtid
, DWORD grfMode
, PropertyStorage_impl
**pps
)
1975 *pps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof **pps
);
1977 return E_OUTOFMEMORY
;
1979 (*pps
)->vtbl
= &IPropertyStorage_Vtbl
;
1981 InitializeCriticalSection(&(*pps
)->cs
);
1983 memcpy(&(*pps
)->fmtid
, rfmtid
, sizeof((*pps
)->fmtid
));
1984 (*pps
)->grfMode
= grfMode
;
1986 hr
= PropertyStorage_CreateDictionaries(*pps
);
1989 IStream_Release(stm
);
1990 DeleteCriticalSection(&(*pps
)->cs
);
1991 HeapFree(GetProcessHeap(), 0, *pps
);
1998 static HRESULT
PropertyStorage_ConstructFromStream(IStream
*stm
,
1999 REFFMTID rfmtid
, DWORD grfMode
, IPropertyStorage
** pps
)
2001 PropertyStorage_impl
*ps
;
2005 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2008 hr
= PropertyStorage_ReadFromStream(ps
);
2011 *pps
= (IPropertyStorage
*)ps
;
2012 TRACE("PropertyStorage %p constructed\n", ps
);
2017 PropertyStorage_DestroyDictionaries(ps
);
2018 HeapFree(GetProcessHeap(), 0, ps
);
2024 static HRESULT
PropertyStorage_ConstructEmpty(IStream
*stm
,
2025 REFFMTID rfmtid
, DWORD grfFlags
, DWORD grfMode
, IPropertyStorage
** pps
)
2027 PropertyStorage_impl
*ps
;
2031 hr
= PropertyStorage_BaseConstruct(stm
, rfmtid
, grfMode
, &ps
);
2035 ps
->grfFlags
= grfFlags
;
2036 if (ps
->grfFlags
& PROPSETFLAG_CASE_SENSITIVE
)
2038 /* default to Unicode unless told not to, as specified here:
2039 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2041 if (ps
->grfFlags
& PROPSETFLAG_ANSI
)
2042 ps
->codePage
= GetACP();
2044 ps
->codePage
= CP_UNICODE
;
2045 ps
->locale
= LOCALE_SYSTEM_DEFAULT
;
2046 TRACE("Code page is %d, locale is %ld\n", ps
->codePage
, ps
->locale
);
2047 *pps
= (IPropertyStorage
*)ps
;
2048 TRACE("PropertyStorage %p constructed\n", ps
);
2055 /***********************************************************************
2056 * Implementation of IPropertySetStorage
2059 /************************************************************************
2060 * IPropertySetStorage_fnQueryInterface (IUnknown)
2062 * This method forwards to the common QueryInterface implementation
2064 static HRESULT WINAPI
IPropertySetStorage_fnQueryInterface(
2065 IPropertySetStorage
*ppstg
,
2069 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2070 return IStorage_QueryInterface( (IStorage
*)This
, riid
, ppvObject
);
2073 /************************************************************************
2074 * IPropertySetStorage_fnAddRef (IUnknown)
2076 * This method forwards to the common AddRef implementation
2078 static ULONG WINAPI
IPropertySetStorage_fnAddRef(
2079 IPropertySetStorage
*ppstg
)
2081 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2082 return IStorage_AddRef( (IStorage
*)This
);
2085 /************************************************************************
2086 * IPropertySetStorage_fnRelease (IUnknown)
2088 * This method forwards to the common Release implementation
2090 static ULONG WINAPI
IPropertySetStorage_fnRelease(
2091 IPropertySetStorage
*ppstg
)
2093 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2094 return IStorage_Release( (IStorage
*)This
);
2097 /************************************************************************
2098 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2100 static HRESULT WINAPI
IPropertySetStorage_fnCreate(
2101 IPropertySetStorage
*ppstg
,
2103 const CLSID
* pclsid
,
2106 IPropertyStorage
** ppprstg
)
2108 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2109 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2110 IStream
*stm
= NULL
;
2113 TRACE("%p %s %08lx %08lx %p\n", This
, debugstr_guid(rfmtid
), grfFlags
,
2117 if (grfMode
!= (STGM_CREATE
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
))
2119 r
= STG_E_INVALIDFLAG
;
2129 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2130 * storage, not a stream. For now, disallow it.
2132 if (grfFlags
& PROPSETFLAG_NONSIMPLE
)
2134 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2135 r
= STG_E_INVALIDFLAG
;
2139 r
= FmtIdToPropStgName(rfmtid
, name
);
2143 r
= IStorage_CreateStream( (IStorage
*)This
, name
, grfMode
, 0, 0, &stm
);
2147 r
= PropertyStorage_ConstructEmpty(stm
, rfmtid
, grfFlags
, grfMode
, ppprstg
);
2150 TRACE("returning 0x%08lx\n", r
);
2154 /************************************************************************
2155 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2157 static HRESULT WINAPI
IPropertySetStorage_fnOpen(
2158 IPropertySetStorage
*ppstg
,
2161 IPropertyStorage
** ppprstg
)
2163 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2164 IStream
*stm
= NULL
;
2165 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2168 TRACE("%p %s %08lx %p\n", This
, debugstr_guid(rfmtid
), grfMode
, ppprstg
);
2171 if (grfMode
!= (STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
) &&
2172 grfMode
!= (STGM_READ
|STGM_SHARE_EXCLUSIVE
))
2174 r
= STG_E_INVALIDFLAG
;
2184 r
= FmtIdToPropStgName(rfmtid
, name
);
2188 r
= IStorage_OpenStream((IStorage
*) This
, name
, 0, grfMode
, 0, &stm
);
2192 r
= PropertyStorage_ConstructFromStream(stm
, rfmtid
, grfMode
, ppprstg
);
2195 TRACE("returning 0x%08lx\n", r
);
2199 /************************************************************************
2200 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2202 static HRESULT WINAPI
IPropertySetStorage_fnDelete(
2203 IPropertySetStorage
*ppstg
,
2206 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2207 IStorage
*stg
= NULL
;
2208 WCHAR name
[CCH_MAX_PROPSTG_NAME
];
2211 TRACE("%p %s\n", This
, debugstr_guid(rfmtid
));
2214 return E_INVALIDARG
;
2216 r
= FmtIdToPropStgName(rfmtid
, name
);
2220 stg
= (IStorage
*) This
;
2221 return IStorage_DestroyElement(stg
, name
);
2224 /************************************************************************
2225 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2227 static HRESULT WINAPI
IPropertySetStorage_fnEnum(
2228 IPropertySetStorage
*ppstg
,
2229 IEnumSTATPROPSETSTG
** ppenum
)
2231 StorageImpl
*This
= impl_from_IPropertySetStorage(ppstg
);
2232 FIXME("%p\n", This
);
2237 /***********************************************************************
2240 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
=
2242 IPropertySetStorage_fnQueryInterface
,
2243 IPropertySetStorage_fnAddRef
,
2244 IPropertySetStorage_fnRelease
,
2245 IPropertySetStorage_fnCreate
,
2246 IPropertySetStorage_fnOpen
,
2247 IPropertySetStorage_fnDelete
,
2248 IPropertySetStorage_fnEnum
2251 static const IPropertyStorageVtbl IPropertyStorage_Vtbl
=
2253 IPropertyStorage_fnQueryInterface
,
2254 IPropertyStorage_fnAddRef
,
2255 IPropertyStorage_fnRelease
,
2256 IPropertyStorage_fnReadMultiple
,
2257 IPropertyStorage_fnWriteMultiple
,
2258 IPropertyStorage_fnDeleteMultiple
,
2259 IPropertyStorage_fnReadPropertyNames
,
2260 IPropertyStorage_fnWritePropertyNames
,
2261 IPropertyStorage_fnDeletePropertyNames
,
2262 IPropertyStorage_fnCommit
,
2263 IPropertyStorage_fnRevert
,
2264 IPropertyStorage_fnEnum
,
2265 IPropertyStorage_fnSetTimes
,
2266 IPropertyStorage_fnSetClass
,
2267 IPropertyStorage_fnStat
,
2270 /***********************************************************************
2271 * Format ID <-> name conversion
2273 static const WCHAR szSummaryInfo
[] = { 5,'S','u','m','m','a','r','y',
2274 'I','n','f','o','r','m','a','t','i','o','n',0 };
2275 static const WCHAR szDocSummaryInfo
[] = { 5,'D','o','c','u','m','e','n','t',
2276 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2278 #define BITS_PER_BYTE 8
2279 #define CHARMASK 0x1f
2280 #define BITS_IN_CHARMASK 5
2281 #define NUM_ALPHA_CHARS 26
2283 /***********************************************************************
2284 * FmtIdToPropStgName [ole32.@]
2285 * Returns the storage name of the format ID rfmtid.
2287 * rfmtid [I] Format ID for which to return a storage name
2288 * str [O] Storage name associated with rfmtid.
2291 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2294 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2295 * Based on the algorithm described here:
2296 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2298 HRESULT WINAPI
FmtIdToPropStgName(const FMTID
*rfmtid
, LPOLESTR str
)
2300 static const char fmtMap
[] = "abcdefghijklmnopqrstuvwxyz012345";
2302 TRACE("%s, %p\n", debugstr_guid(rfmtid
), str
);
2304 if (!rfmtid
) return E_INVALIDARG
;
2305 if (!str
) return E_INVALIDARG
;
2307 if (IsEqualGUID(&FMTID_SummaryInformation
, rfmtid
))
2308 lstrcpyW(str
, szSummaryInfo
);
2309 else if (IsEqualGUID(&FMTID_DocSummaryInformation
, rfmtid
))
2310 lstrcpyW(str
, szDocSummaryInfo
);
2311 else if (IsEqualGUID(&FMTID_UserDefinedProperties
, rfmtid
))
2312 lstrcpyW(str
, szDocSummaryInfo
);
2317 ULONG bitsRemaining
= BITS_PER_BYTE
;
2320 for (fmtptr
= (BYTE
*)rfmtid
; fmtptr
< (BYTE
*)rfmtid
+ sizeof(FMTID
); )
2322 ULONG i
= *fmtptr
>> (BITS_PER_BYTE
- bitsRemaining
);
2324 if (bitsRemaining
>= BITS_IN_CHARMASK
)
2326 *pstr
= (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2327 if (bitsRemaining
== BITS_PER_BYTE
&& *pstr
>= 'a' &&
2331 bitsRemaining
-= BITS_IN_CHARMASK
;
2332 if (bitsRemaining
== 0)
2335 bitsRemaining
= BITS_PER_BYTE
;
2340 if (++fmtptr
< (const BYTE
*)rfmtid
+ sizeof(FMTID
))
2341 i
|= *fmtptr
<< bitsRemaining
;
2342 *pstr
++ = (WCHAR
)(fmtMap
[i
& CHARMASK
]);
2343 bitsRemaining
+= BITS_PER_BYTE
- BITS_IN_CHARMASK
;
2348 TRACE("returning %s\n", debugstr_w(str
));
2352 /***********************************************************************
2353 * PropStgNameToFmtId [ole32.@]
2354 * Returns the format ID corresponding to the given name.
2356 * str [I] Storage name to convert to a format ID.
2357 * rfmtid [O] Format ID corresponding to str.
2360 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2361 * a format ID, S_OK otherwise.
2364 * Based on the algorithm described here:
2365 * http://msdn.microsoft.com/library/en-us/stg/stg/names_in_istorage.asp
2367 HRESULT WINAPI
PropStgNameToFmtId(const LPOLESTR str
, FMTID
*rfmtid
)
2369 HRESULT hr
= STG_E_INVALIDNAME
;
2371 TRACE("%s, %p\n", debugstr_w(str
), rfmtid
);
2373 if (!rfmtid
) return E_INVALIDARG
;
2374 if (!str
) return STG_E_INVALIDNAME
;
2376 if (!lstrcmpiW(str
, szDocSummaryInfo
))
2378 memcpy(rfmtid
, &FMTID_DocSummaryInformation
, sizeof(*rfmtid
));
2381 else if (!lstrcmpiW(str
, szSummaryInfo
))
2383 memcpy(rfmtid
, &FMTID_SummaryInformation
, sizeof(*rfmtid
));
2389 BYTE
*fmtptr
= (BYTE
*)rfmtid
- 1;
2390 const WCHAR
*pstr
= str
;
2392 memset(rfmtid
, 0, sizeof(*rfmtid
));
2393 for (bits
= 0; bits
< sizeof(FMTID
) * BITS_PER_BYTE
;
2394 bits
+= BITS_IN_CHARMASK
)
2396 ULONG bitsUsed
= bits
% BITS_PER_BYTE
, bitsStored
;
2402 if (wc
> NUM_ALPHA_CHARS
)
2405 if (wc
> NUM_ALPHA_CHARS
)
2407 wc
+= 'a' - '0' + NUM_ALPHA_CHARS
;
2410 WARN("invalid character (%d)\n", *pstr
);
2415 *fmtptr
|= wc
<< bitsUsed
;
2416 bitsStored
= min(BITS_PER_BYTE
- bitsUsed
, BITS_IN_CHARMASK
);
2417 if (bitsStored
< BITS_IN_CHARMASK
)
2419 wc
>>= BITS_PER_BYTE
- bitsUsed
;
2420 if (bits
+ bitsStored
== sizeof(FMTID
) * BITS_PER_BYTE
)
2424 WARN("extra bits\n");
2430 *fmtptr
|= (BYTE
)wc
;