user32: Replaced oic_winlogo.ico with Tango compliant icon.
[wine.git] / dlls / ole32 / stg_prop.c
blobb3362bc21de9c5d5893177aaafbe269d636383dd
1 /*
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 * TODO:
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
39 #include <assert.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #define COBJMACROS
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winnls.h"
52 #include "winuser.h"
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
55 #include "dictionary.h"
56 #include "storage32.h"
57 #include "enumx.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(storage);
61 static inline StorageImpl *impl_from_IPropertySetStorage( IPropertySetStorage *iface )
63 return (StorageImpl *)((char*)iface - FIELD_OFFSET(StorageImpl, base.pssVtbl));
66 /* These are documented in MSDN,
67 * but they don't seem to be in any header file.
69 #define PROPSETHDR_BYTEORDER_MAGIC 0xfffe
70 #define PROPSETHDR_OSVER_KIND_WIN16 0
71 #define PROPSETHDR_OSVER_KIND_MAC 1
72 #define PROPSETHDR_OSVER_KIND_WIN32 2
74 #define CP_UNICODE 1200
76 #define MAX_VERSION_0_PROP_NAME_LENGTH 256
78 #define CFTAG_WINDOWS (-1L)
79 #define CFTAG_MACINTOSH (-2L)
80 #define CFTAG_FMTID (-3L)
81 #define CFTAG_NODATA 0L
83 typedef struct tagPROPERTYSETHEADER
85 WORD wByteOrder; /* always 0xfffe */
86 WORD wFormat; /* can be zero or one */
87 DWORD dwOSVer; /* OS version of originating system */
88 CLSID clsid; /* application CLSID */
89 DWORD reserved; /* always 1 */
90 } PROPERTYSETHEADER;
92 typedef struct tagFORMATIDOFFSET
94 FMTID fmtid;
95 DWORD dwOffset; /* from beginning of stream */
96 } FORMATIDOFFSET;
98 typedef struct tagPROPERTYSECTIONHEADER
100 DWORD cbSection;
101 DWORD cProperties;
102 } PROPERTYSECTIONHEADER;
104 typedef struct tagPROPERTYIDOFFSET
106 DWORD propid;
107 DWORD dwOffset; /* from beginning of section */
108 } PROPERTYIDOFFSET;
110 typedef struct tagPropertyStorage_impl PropertyStorage_impl;
112 /* Initializes the property storage from the stream (and undoes any uncommitted
113 * changes in the process.) Returns an error if there is an error reading or
114 * if the stream format doesn't match what's expected.
116 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *);
118 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *);
120 /* Creates the dictionaries used by the property storage. If successful, all
121 * the dictionaries have been created. If failed, none has been. (This makes
122 * it a bit easier to deal with destroying them.)
124 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *);
126 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *);
128 /* Copies from propvar to prop. If propvar's type is VT_LPSTR, copies the
129 * string using PropertyStorage_StringCopy.
131 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
132 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP);
134 /* Copies the string src, which is encoded using code page srcCP, and returns
135 * it in *dst, in the code page specified by targetCP. The returned string is
136 * allocated using CoTaskMemAlloc.
137 * If srcCP is CP_UNICODE, src is in fact an LPCWSTR. Similarly, if targetCP
138 * is CP_UNICODE, the returned string is in fact an LPWSTR.
139 * Returns S_OK on success, something else on failure.
141 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
142 LCID targetCP);
144 static const IPropertyStorageVtbl IPropertyStorage_Vtbl;
145 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl;
146 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl;
147 static HRESULT create_EnumSTATPROPSETSTG(StorageImpl *, IEnumSTATPROPSETSTG**);
148 static HRESULT create_EnumSTATPROPSTG(PropertyStorage_impl *, IEnumSTATPROPSTG**);
150 /***********************************************************************
151 * Implementation of IPropertyStorage
153 struct tagPropertyStorage_impl
155 const IPropertyStorageVtbl *vtbl;
156 LONG ref;
157 CRITICAL_SECTION cs;
158 IStream *stm;
159 BOOL dirty;
160 FMTID fmtid;
161 CLSID clsid;
162 WORD format;
163 DWORD originatorOS;
164 DWORD grfFlags;
165 DWORD grfMode;
166 UINT codePage;
167 LCID locale;
168 PROPID highestProp;
169 struct dictionary *name_to_propid;
170 struct dictionary *propid_to_name;
171 struct dictionary *propid_to_prop;
174 /************************************************************************
175 * IPropertyStorage_fnQueryInterface (IPropertyStorage)
177 static HRESULT WINAPI IPropertyStorage_fnQueryInterface(
178 IPropertyStorage *iface,
179 REFIID riid,
180 void** ppvObject)
182 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
184 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
186 if (!ppvObject)
187 return E_INVALIDARG;
189 *ppvObject = 0;
191 if (IsEqualGUID(&IID_IUnknown, riid) ||
192 IsEqualGUID(&IID_IPropertyStorage, riid))
194 IPropertyStorage_AddRef(iface);
195 *ppvObject = iface;
196 return S_OK;
199 return E_NOINTERFACE;
202 /************************************************************************
203 * IPropertyStorage_fnAddRef (IPropertyStorage)
205 static ULONG WINAPI IPropertyStorage_fnAddRef(
206 IPropertyStorage *iface)
208 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
209 return InterlockedIncrement(&This->ref);
212 /************************************************************************
213 * IPropertyStorage_fnRelease (IPropertyStorage)
215 static ULONG WINAPI IPropertyStorage_fnRelease(
216 IPropertyStorage *iface)
218 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
219 ULONG ref;
221 ref = InterlockedDecrement(&This->ref);
222 if (ref == 0)
224 TRACE("Destroying %p\n", This);
225 if (This->dirty)
226 IPropertyStorage_Commit(iface, STGC_DEFAULT);
227 IStream_Release(This->stm);
228 This->cs.DebugInfo->Spare[0] = 0;
229 DeleteCriticalSection(&This->cs);
230 PropertyStorage_DestroyDictionaries(This);
231 HeapFree(GetProcessHeap(), 0, This);
233 return ref;
236 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
237 DWORD propid)
239 PROPVARIANT *ret = NULL;
241 dictionary_find(This->propid_to_prop, UlongToPtr(propid), (void **)&ret);
242 TRACE("returning %p\n", ret);
243 return ret;
246 /* Returns NULL if name is NULL. */
247 static PROPVARIANT *PropertyStorage_FindPropertyByName(
248 PropertyStorage_impl *This, LPCWSTR name)
250 PROPVARIANT *ret = NULL;
251 void *propid;
253 if (!name)
254 return NULL;
255 if (This->codePage == CP_UNICODE)
257 if (dictionary_find(This->name_to_propid, name, &propid))
258 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
260 else
262 LPSTR ansiName;
263 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
264 &ansiName, This->codePage);
266 if (SUCCEEDED(hr))
268 if (dictionary_find(This->name_to_propid, ansiName, &propid))
269 ret = PropertyStorage_FindProperty(This, PtrToUlong(propid));
270 CoTaskMemFree(ansiName);
273 TRACE("returning %p\n", ret);
274 return ret;
277 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
278 DWORD propid)
280 LPWSTR ret = NULL;
282 dictionary_find(This->propid_to_name, UlongToPtr(propid), (void **)&ret);
283 TRACE("returning %p\n", ret);
284 return ret;
287 /************************************************************************
288 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
290 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
291 IPropertyStorage* iface,
292 ULONG cpspec,
293 const PROPSPEC rgpspec[],
294 PROPVARIANT rgpropvar[])
296 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
297 HRESULT hr = S_OK;
298 ULONG i;
300 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
302 if (!cpspec)
303 return S_FALSE;
304 if (!rgpspec || !rgpropvar)
305 return E_INVALIDARG;
306 EnterCriticalSection(&This->cs);
307 for (i = 0; i < cpspec; i++)
309 PropVariantInit(&rgpropvar[i]);
310 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
312 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
313 rgpspec[i].u.lpwstr);
315 if (prop)
316 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
317 This->codePage);
319 else
321 switch (rgpspec[i].u.propid)
323 case PID_CODEPAGE:
324 rgpropvar[i].vt = VT_I2;
325 rgpropvar[i].u.iVal = This->codePage;
326 break;
327 case PID_LOCALE:
328 rgpropvar[i].vt = VT_I4;
329 rgpropvar[i].u.lVal = This->locale;
330 break;
331 default:
333 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
334 rgpspec[i].u.propid);
336 if (prop)
337 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
338 GetACP(), This->codePage);
339 else
340 hr = S_FALSE;
345 LeaveCriticalSection(&This->cs);
346 return hr;
349 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
350 LCID dstCP)
352 HRESULT hr = S_OK;
353 int len;
355 TRACE("%s, %p, %d, %d\n",
356 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
357 dstCP, srcCP);
358 assert(src);
359 assert(dst);
360 *dst = NULL;
361 if (dstCP == srcCP)
363 size_t len;
365 if (dstCP == CP_UNICODE)
366 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
367 else
368 len = strlen(src) + 1;
369 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
370 if (!*dst)
371 hr = STG_E_INSUFFICIENTMEMORY;
372 else
373 memcpy(*dst, src, len);
375 else
377 if (dstCP == CP_UNICODE)
379 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
380 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
381 if (!*dst)
382 hr = STG_E_INSUFFICIENTMEMORY;
383 else
384 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
386 else
388 LPCWSTR wideStr = NULL;
389 LPWSTR wideStr_tmp = NULL;
391 if (srcCP == CP_UNICODE)
392 wideStr = (LPCWSTR)src;
393 else
395 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
396 wideStr_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
397 if (wideStr_tmp)
399 MultiByteToWideChar(srcCP, 0, src, -1, wideStr_tmp, len);
400 wideStr = wideStr_tmp;
402 else
403 hr = STG_E_INSUFFICIENTMEMORY;
405 if (SUCCEEDED(hr))
407 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
408 NULL, NULL);
409 *dst = CoTaskMemAlloc(len);
410 if (!*dst)
411 hr = STG_E_INSUFFICIENTMEMORY;
412 else
414 BOOL defCharUsed = FALSE;
416 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
417 NULL, &defCharUsed) == 0 || defCharUsed)
419 CoTaskMemFree(*dst);
420 *dst = NULL;
421 hr = HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
425 HeapFree(GetProcessHeap(), 0, wideStr_tmp);
428 TRACE("returning 0x%08x (%s)\n", hr,
429 dstCP == CP_UNICODE ? debugstr_w((LPCWSTR)*dst) : debugstr_a(*dst));
430 return hr;
433 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
434 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
436 HRESULT hr = S_OK;
438 assert(prop);
439 assert(propvar);
440 if (propvar->vt == VT_LPSTR)
442 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
443 &prop->u.pszVal, targetCP);
444 if (SUCCEEDED(hr))
445 prop->vt = VT_LPSTR;
447 else
448 PropVariantCopy(prop, propvar);
449 return hr;
452 /* Stores the property with id propid and value propvar into this property
453 * storage. lcid is ignored if propvar's type is not VT_LPSTR. If propvar's
454 * type is VT_LPSTR, converts the string using lcid as the source code page
455 * and This->codePage as the target code page before storing.
456 * As a side effect, may change This->format to 1 if the type of propvar is
457 * a version 1-only property.
459 static HRESULT PropertyStorage_StorePropWithId(PropertyStorage_impl *This,
460 PROPID propid, const PROPVARIANT *propvar, LCID lcid)
462 HRESULT hr = S_OK;
463 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
465 assert(propvar);
466 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
467 This->format = 1;
468 switch (propvar->vt)
470 case VT_DECIMAL:
471 case VT_I1:
472 case VT_INT:
473 case VT_UINT:
474 case VT_VECTOR|VT_I1:
475 This->format = 1;
477 TRACE("Setting 0x%08x to type %d\n", propid, propvar->vt);
478 if (prop)
480 PropVariantClear(prop);
481 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
482 lcid);
484 else
486 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
487 sizeof(PROPVARIANT));
488 if (prop)
490 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
491 lcid);
492 if (SUCCEEDED(hr))
494 dictionary_insert(This->propid_to_prop, UlongToPtr(propid), prop);
495 if (propid > This->highestProp)
496 This->highestProp = propid;
498 else
499 HeapFree(GetProcessHeap(), 0, prop);
501 else
502 hr = STG_E_INSUFFICIENTMEMORY;
504 return hr;
507 /* Adds the name srcName to the name dictionaries, mapped to property ID id.
508 * srcName is encoded in code page cp, and is converted to This->codePage.
509 * If cp is CP_UNICODE, srcName is actually a unicode string.
510 * As a side effect, may change This->format to 1 if srcName is too long for
511 * a version 0 property storage.
512 * Doesn't validate id.
514 static HRESULT PropertyStorage_StoreNameWithId(PropertyStorage_impl *This,
515 LPCSTR srcName, LCID cp, PROPID id)
517 LPSTR name;
518 HRESULT hr;
520 assert(srcName);
522 hr = PropertyStorage_StringCopy(srcName, cp, &name, This->codePage);
523 if (SUCCEEDED(hr))
525 if (This->codePage == CP_UNICODE)
527 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
528 This->format = 1;
530 else
532 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
533 This->format = 1;
535 TRACE("Adding prop name %s, propid %d\n",
536 This->codePage == CP_UNICODE ? debugstr_w((LPCWSTR)name) :
537 debugstr_a(name), id);
538 dictionary_insert(This->name_to_propid, name, UlongToPtr(id));
539 dictionary_insert(This->propid_to_name, UlongToPtr(id), name);
541 return hr;
544 /************************************************************************
545 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
547 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
548 IPropertyStorage* iface,
549 ULONG cpspec,
550 const PROPSPEC rgpspec[],
551 const PROPVARIANT rgpropvar[],
552 PROPID propidNameFirst)
554 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
555 HRESULT hr = S_OK;
556 ULONG i;
558 TRACE("(%p, %d, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
560 if (cpspec && (!rgpspec || !rgpropvar))
561 return E_INVALIDARG;
562 if (!(This->grfMode & STGM_READWRITE))
563 return STG_E_ACCESSDENIED;
564 EnterCriticalSection(&This->cs);
565 This->dirty = TRUE;
566 This->originatorOS = (DWORD)MAKELONG(LOWORD(GetVersion()),
567 PROPSETHDR_OSVER_KIND_WIN32) ;
568 for (i = 0; i < cpspec; i++)
570 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
572 PROPVARIANT *prop = PropertyStorage_FindPropertyByName(This,
573 rgpspec[i].u.lpwstr);
575 if (prop)
576 PropVariantCopy(prop, &rgpropvar[i]);
577 else
579 /* Note that I don't do the special cases here that I do below,
580 * because naming the special PIDs isn't supported.
582 if (propidNameFirst < PID_FIRST_USABLE ||
583 propidNameFirst >= PID_MIN_READONLY)
584 hr = STG_E_INVALIDPARAMETER;
585 else
587 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
589 hr = PropertyStorage_StoreNameWithId(This,
590 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
591 if (SUCCEEDED(hr))
592 hr = PropertyStorage_StorePropWithId(This, nextId,
593 &rgpropvar[i], GetACP());
597 else
599 switch (rgpspec[i].u.propid)
601 case PID_DICTIONARY:
602 /* Can't set the dictionary */
603 hr = STG_E_INVALIDPARAMETER;
604 break;
605 case PID_CODEPAGE:
606 /* Can only set the code page if nothing else has been set */
607 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
608 rgpropvar[i].vt == VT_I2)
610 This->codePage = rgpropvar[i].u.iVal;
611 if (This->codePage == CP_UNICODE)
612 This->grfFlags &= ~PROPSETFLAG_ANSI;
613 else
614 This->grfFlags |= PROPSETFLAG_ANSI;
616 else
617 hr = STG_E_INVALIDPARAMETER;
618 break;
619 case PID_LOCALE:
620 /* Can only set the locale if nothing else has been set */
621 if (dictionary_num_entries(This->propid_to_prop) == 0 &&
622 rgpropvar[i].vt == VT_I4)
623 This->locale = rgpropvar[i].u.lVal;
624 else
625 hr = STG_E_INVALIDPARAMETER;
626 break;
627 case PID_ILLEGAL:
628 /* silently ignore like MSDN says */
629 break;
630 default:
631 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
632 hr = STG_E_INVALIDPARAMETER;
633 else
634 hr = PropertyStorage_StorePropWithId(This,
635 rgpspec[i].u.propid, &rgpropvar[i], GetACP());
639 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
640 IPropertyStorage_Commit(iface, STGC_DEFAULT);
641 LeaveCriticalSection(&This->cs);
642 return hr;
645 /************************************************************************
646 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
648 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
649 IPropertyStorage* iface,
650 ULONG cpspec,
651 const PROPSPEC rgpspec[])
653 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
654 ULONG i;
655 HRESULT hr;
657 TRACE("(%p, %d, %p)\n", iface, cpspec, rgpspec);
659 if (cpspec && !rgpspec)
660 return E_INVALIDARG;
661 if (!(This->grfMode & STGM_READWRITE))
662 return STG_E_ACCESSDENIED;
663 hr = S_OK;
664 EnterCriticalSection(&This->cs);
665 This->dirty = TRUE;
666 for (i = 0; i < cpspec; i++)
668 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
670 void *propid;
672 if (dictionary_find(This->name_to_propid, rgpspec[i].u.lpwstr, &propid))
673 dictionary_remove(This->propid_to_prop, propid);
675 else
677 if (rgpspec[i].u.propid >= PID_FIRST_USABLE &&
678 rgpspec[i].u.propid < PID_MIN_READONLY)
679 dictionary_remove(This->propid_to_prop, UlongToPtr(rgpspec[i].u.propid));
680 else
681 hr = STG_E_INVALIDPARAMETER;
684 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
685 IPropertyStorage_Commit(iface, STGC_DEFAULT);
686 LeaveCriticalSection(&This->cs);
687 return hr;
690 /************************************************************************
691 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
693 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
694 IPropertyStorage* iface,
695 ULONG cpropid,
696 const PROPID rgpropid[],
697 LPOLESTR rglpwstrName[])
699 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
700 ULONG i;
701 HRESULT hr = S_FALSE;
703 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
705 if (cpropid && (!rgpropid || !rglpwstrName))
706 return E_INVALIDARG;
707 EnterCriticalSection(&This->cs);
708 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
710 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
712 if (name)
714 size_t len = lstrlenW(name);
716 hr = S_OK;
717 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
718 if (rglpwstrName)
719 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
720 else
721 hr = STG_E_INSUFFICIENTMEMORY;
723 else
724 rglpwstrName[i] = NULL;
726 LeaveCriticalSection(&This->cs);
727 return hr;
730 /************************************************************************
731 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
733 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
734 IPropertyStorage* iface,
735 ULONG cpropid,
736 const PROPID rgpropid[],
737 const LPOLESTR rglpwstrName[])
739 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
740 ULONG i;
741 HRESULT hr;
743 TRACE("(%p, %d, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
745 if (cpropid && (!rgpropid || !rglpwstrName))
746 return E_INVALIDARG;
747 if (!(This->grfMode & STGM_READWRITE))
748 return STG_E_ACCESSDENIED;
749 hr = S_OK;
750 EnterCriticalSection(&This->cs);
751 This->dirty = TRUE;
752 for (i = 0; SUCCEEDED(hr) && i < cpropid; i++)
754 if (rgpropid[i] != PID_ILLEGAL)
755 hr = PropertyStorage_StoreNameWithId(This, (LPCSTR)rglpwstrName[i],
756 CP_UNICODE, rgpropid[i]);
758 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
759 IPropertyStorage_Commit(iface, STGC_DEFAULT);
760 LeaveCriticalSection(&This->cs);
761 return hr;
764 /************************************************************************
765 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
767 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
768 IPropertyStorage* iface,
769 ULONG cpropid,
770 const PROPID rgpropid[])
772 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
773 ULONG i;
774 HRESULT hr;
776 TRACE("(%p, %d, %p)\n", iface, cpropid, rgpropid);
778 if (cpropid && !rgpropid)
779 return E_INVALIDARG;
780 if (!(This->grfMode & STGM_READWRITE))
781 return STG_E_ACCESSDENIED;
782 hr = S_OK;
783 EnterCriticalSection(&This->cs);
784 This->dirty = TRUE;
785 for (i = 0; i < cpropid; i++)
787 LPWSTR name = NULL;
789 if (dictionary_find(This->propid_to_name, UlongToPtr(rgpropid[i]), (void **)&name))
791 dictionary_remove(This->propid_to_name, UlongToPtr(rgpropid[i]));
792 dictionary_remove(This->name_to_propid, name);
795 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
796 IPropertyStorage_Commit(iface, STGC_DEFAULT);
797 LeaveCriticalSection(&This->cs);
798 return hr;
801 /************************************************************************
802 * IPropertyStorage_fnCommit (IPropertyStorage)
804 static HRESULT WINAPI IPropertyStorage_fnCommit(
805 IPropertyStorage* iface,
806 DWORD grfCommitFlags)
808 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
809 HRESULT hr;
811 TRACE("(%p, 0x%08x)\n", iface, grfCommitFlags);
813 if (!(This->grfMode & STGM_READWRITE))
814 return STG_E_ACCESSDENIED;
815 EnterCriticalSection(&This->cs);
816 if (This->dirty)
817 hr = PropertyStorage_WriteToStream(This);
818 else
819 hr = S_OK;
820 LeaveCriticalSection(&This->cs);
821 return hr;
824 /************************************************************************
825 * IPropertyStorage_fnRevert (IPropertyStorage)
827 static HRESULT WINAPI IPropertyStorage_fnRevert(
828 IPropertyStorage* iface)
830 HRESULT hr;
831 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
833 TRACE("%p\n", iface);
835 EnterCriticalSection(&This->cs);
836 if (This->dirty)
838 PropertyStorage_DestroyDictionaries(This);
839 hr = PropertyStorage_CreateDictionaries(This);
840 if (SUCCEEDED(hr))
841 hr = PropertyStorage_ReadFromStream(This);
843 else
844 hr = S_OK;
845 LeaveCriticalSection(&This->cs);
846 return hr;
849 /************************************************************************
850 * IPropertyStorage_fnEnum (IPropertyStorage)
852 static HRESULT WINAPI IPropertyStorage_fnEnum(
853 IPropertyStorage* iface,
854 IEnumSTATPROPSTG** ppenum)
856 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
857 return create_EnumSTATPROPSTG(This, ppenum);
860 /************************************************************************
861 * IPropertyStorage_fnSetTimes (IPropertyStorage)
863 static HRESULT WINAPI IPropertyStorage_fnSetTimes(
864 IPropertyStorage* iface,
865 const FILETIME* pctime,
866 const FILETIME* patime,
867 const FILETIME* pmtime)
869 FIXME("\n");
870 return E_NOTIMPL;
873 /************************************************************************
874 * IPropertyStorage_fnSetClass (IPropertyStorage)
876 static HRESULT WINAPI IPropertyStorage_fnSetClass(
877 IPropertyStorage* iface,
878 REFCLSID clsid)
880 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
882 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
884 if (!clsid)
885 return E_INVALIDARG;
886 if (!(This->grfMode & STGM_READWRITE))
887 return STG_E_ACCESSDENIED;
888 This->clsid = *clsid;
889 This->dirty = TRUE;
890 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
891 IPropertyStorage_Commit(iface, STGC_DEFAULT);
892 return S_OK;
895 /************************************************************************
896 * IPropertyStorage_fnStat (IPropertyStorage)
898 static HRESULT WINAPI IPropertyStorage_fnStat(
899 IPropertyStorage* iface,
900 STATPROPSETSTG* statpsstg)
902 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
903 STATSTG stat;
904 HRESULT hr;
906 TRACE("%p, %p\n", iface, statpsstg);
908 if (!statpsstg)
909 return E_INVALIDARG;
911 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
912 if (SUCCEEDED(hr))
914 statpsstg->fmtid = This->fmtid;
915 statpsstg->clsid = This->clsid;
916 statpsstg->grfFlags = This->grfFlags;
917 statpsstg->mtime = stat.mtime;
918 statpsstg->ctime = stat.ctime;
919 statpsstg->atime = stat.atime;
920 statpsstg->dwOSVersion = This->originatorOS;
922 return hr;
925 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
926 void *extra)
928 PropertyStorage_impl *This = extra;
930 if (This->codePage == CP_UNICODE)
932 TRACE("(%s, %s)\n", debugstr_w(a), debugstr_w(b));
933 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
934 return lstrcmpW(a, b);
935 else
936 return lstrcmpiW(a, b);
938 else
940 TRACE("(%s, %s)\n", debugstr_a(a), debugstr_a(b));
941 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
942 return lstrcmpA(a, b);
943 else
944 return lstrcmpiA(a, b);
948 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
950 CoTaskMemFree(k);
953 static int PropertyStorage_PropCompare(const void *a, const void *b,
954 void *extra)
956 TRACE("(%d, %d)\n", PtrToUlong(a), PtrToUlong(b));
957 return PtrToUlong(a) - PtrToUlong(b);
960 static void PropertyStorage_PropertyDestroy(void *k, void *d, void *extra)
962 PropVariantClear(d);
963 HeapFree(GetProcessHeap(), 0, d);
966 #ifdef WORDS_BIGENDIAN
967 /* Swaps each character in str to or from little endian; assumes the conversion
968 * is symmetric, that is, that lendian16toh is equivalent to htole16.
970 static void PropertyStorage_ByteSwapString(LPWSTR str, size_t len)
972 DWORD i;
974 /* Swap characters to host order.
975 * FIXME: alignment?
977 for (i = 0; i < len; i++)
978 str[i] = lendian16toh(str[i]);
980 #else
981 #define PropertyStorage_ByteSwapString(s, l)
982 #endif
984 /* Reads the dictionary from the memory buffer beginning at ptr. Interprets
985 * the entries according to the values of This->codePage and This->locale.
986 * FIXME: there isn't any checking whether the read property extends past the
987 * end of the buffer.
989 static HRESULT PropertyStorage_ReadDictionary(PropertyStorage_impl *This,
990 BYTE *ptr)
992 DWORD numEntries, i;
993 HRESULT hr = S_OK;
995 assert(This->name_to_propid);
996 assert(This->propid_to_name);
998 StorageUtl_ReadDWord(ptr, 0, &numEntries);
999 TRACE("Reading %d entries:\n", numEntries);
1000 ptr += sizeof(DWORD);
1001 for (i = 0; SUCCEEDED(hr) && i < numEntries; i++)
1003 PROPID propid;
1004 DWORD cbEntry;
1006 StorageUtl_ReadDWord(ptr, 0, &propid);
1007 ptr += sizeof(PROPID);
1008 StorageUtl_ReadDWord(ptr, 0, &cbEntry);
1009 ptr += sizeof(DWORD);
1010 TRACE("Reading entry with ID 0x%08x, %d bytes\n", propid, cbEntry);
1011 /* Make sure the source string is NULL-terminated */
1012 if (This->codePage != CP_UNICODE)
1013 ptr[cbEntry - 1] = '\0';
1014 else
1015 *((LPWSTR)ptr + cbEntry / sizeof(WCHAR)) = '\0';
1016 hr = PropertyStorage_StoreNameWithId(This, (char*)ptr, This->codePage, propid);
1017 if (This->codePage == CP_UNICODE)
1019 /* Unicode entries are padded to DWORD boundaries */
1020 if (cbEntry % sizeof(DWORD))
1021 ptr += sizeof(DWORD) - (cbEntry % sizeof(DWORD));
1023 ptr += sizeof(DWORD) + cbEntry;
1025 return hr;
1028 /* FIXME: there isn't any checking whether the read property extends past the
1029 * end of the buffer.
1031 static HRESULT PropertyStorage_ReadProperty(PropertyStorage_impl *This,
1032 PROPVARIANT *prop, const BYTE *data)
1034 HRESULT hr = S_OK;
1036 assert(prop);
1037 assert(data);
1038 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1039 data += sizeof(DWORD);
1040 switch (prop->vt)
1042 case VT_EMPTY:
1043 case VT_NULL:
1044 break;
1045 case VT_I1:
1046 prop->u.cVal = *(const char *)data;
1047 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1048 break;
1049 case VT_UI1:
1050 prop->u.bVal = *data;
1051 TRACE("Read byte 0x%x\n", prop->u.bVal);
1052 break;
1053 case VT_I2:
1054 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1055 TRACE("Read short %d\n", prop->u.iVal);
1056 break;
1057 case VT_UI2:
1058 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1059 TRACE("Read ushort %d\n", prop->u.uiVal);
1060 break;
1061 case VT_INT:
1062 case VT_I4:
1063 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1064 TRACE("Read long %d\n", prop->u.lVal);
1065 break;
1066 case VT_UINT:
1067 case VT_UI4:
1068 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1069 TRACE("Read ulong %d\n", prop->u.ulVal);
1070 break;
1071 case VT_LPSTR:
1073 DWORD count;
1075 StorageUtl_ReadDWord(data, 0, &count);
1076 if (This->codePage == CP_UNICODE && count / 2)
1078 WARN("Unicode string has odd number of bytes\n");
1079 hr = STG_E_INVALIDHEADER;
1081 else
1083 prop->u.pszVal = CoTaskMemAlloc(count);
1084 if (prop->u.pszVal)
1086 memcpy(prop->u.pszVal, data + sizeof(DWORD), count);
1087 /* This is stored in the code page specified in This->codePage.
1088 * Don't convert it, the caller will just store it as-is.
1090 if (This->codePage == CP_UNICODE)
1092 /* Make sure it's NULL-terminated */
1093 prop->u.pszVal[count / sizeof(WCHAR) - 1] = '\0';
1094 TRACE("Read string value %s\n",
1095 debugstr_w(prop->u.pwszVal));
1097 else
1099 /* Make sure it's NULL-terminated */
1100 prop->u.pszVal[count - 1] = '\0';
1101 TRACE("Read string value %s\n", debugstr_a(prop->u.pszVal));
1104 else
1105 hr = STG_E_INSUFFICIENTMEMORY;
1107 break;
1109 case VT_LPWSTR:
1111 DWORD count;
1113 StorageUtl_ReadDWord(data, 0, &count);
1114 prop->u.pwszVal = CoTaskMemAlloc(count * sizeof(WCHAR));
1115 if (prop->u.pwszVal)
1117 memcpy(prop->u.pwszVal, data + sizeof(DWORD),
1118 count * sizeof(WCHAR));
1119 /* make sure string is NULL-terminated */
1120 prop->u.pwszVal[count - 1] = '\0';
1121 PropertyStorage_ByteSwapString(prop->u.pwszVal, count);
1122 TRACE("Read string value %s\n", debugstr_w(prop->u.pwszVal));
1124 else
1125 hr = STG_E_INSUFFICIENTMEMORY;
1126 break;
1128 case VT_FILETIME:
1129 StorageUtl_ReadULargeInteger(data, 0,
1130 (ULARGE_INTEGER *)&prop->u.filetime);
1131 break;
1132 case VT_CF:
1134 DWORD len = 0, tag = 0;
1136 StorageUtl_ReadDWord(data, 0, &len);
1137 StorageUtl_ReadDWord(data, 4, &tag);
1138 if (len > 8)
1140 len -= 8;
1141 prop->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
1142 prop->u.pclipdata->cbSize = len;
1143 prop->u.pclipdata->ulClipFmt = tag;
1144 prop->u.pclipdata->pClipData = CoTaskMemAlloc(len - sizeof(prop->u.pclipdata->ulClipFmt));
1145 memcpy(prop->u.pclipdata->pClipData, data+8, len - sizeof(prop->u.pclipdata->ulClipFmt));
1147 else
1148 hr = STG_E_INVALIDPARAMETER;
1150 break;
1151 default:
1152 FIXME("unsupported type %d\n", prop->vt);
1153 hr = STG_E_INVALIDPARAMETER;
1155 return hr;
1158 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1159 PROPERTYSETHEADER *hdr)
1161 BYTE buf[sizeof(PROPERTYSETHEADER)];
1162 ULONG count = 0;
1163 HRESULT hr;
1165 assert(stm);
1166 assert(hdr);
1167 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1168 if (SUCCEEDED(hr))
1170 if (count != sizeof(buf))
1172 WARN("read only %d\n", count);
1173 hr = STG_E_INVALIDHEADER;
1175 else
1177 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1178 &hdr->wByteOrder);
1179 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1180 &hdr->wFormat);
1181 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1182 &hdr->dwOSVer);
1183 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1184 &hdr->clsid);
1185 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1186 &hdr->reserved);
1189 TRACE("returning 0x%08x\n", hr);
1190 return hr;
1193 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1194 FORMATIDOFFSET *fmt)
1196 BYTE buf[sizeof(FORMATIDOFFSET)];
1197 ULONG count = 0;
1198 HRESULT hr;
1200 assert(stm);
1201 assert(fmt);
1202 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1203 if (SUCCEEDED(hr))
1205 if (count != sizeof(buf))
1207 WARN("read only %d\n", count);
1208 hr = STG_E_INVALIDHEADER;
1210 else
1212 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1213 &fmt->fmtid);
1214 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1215 &fmt->dwOffset);
1218 TRACE("returning 0x%08x\n", hr);
1219 return hr;
1222 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1223 PROPERTYSECTIONHEADER *hdr)
1225 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1226 ULONG count = 0;
1227 HRESULT hr;
1229 assert(stm);
1230 assert(hdr);
1231 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1232 if (SUCCEEDED(hr))
1234 if (count != sizeof(buf))
1236 WARN("read only %d\n", count);
1237 hr = STG_E_INVALIDHEADER;
1239 else
1241 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1242 cbSection), &hdr->cbSection);
1243 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSECTIONHEADER,
1244 cProperties), &hdr->cProperties);
1247 TRACE("returning 0x%08x\n", hr);
1248 return hr;
1251 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1253 PROPERTYSETHEADER hdr;
1254 FORMATIDOFFSET fmtOffset;
1255 PROPERTYSECTIONHEADER sectionHdr;
1256 LARGE_INTEGER seek;
1257 ULONG i;
1258 STATSTG stat;
1259 HRESULT hr;
1260 BYTE *buf = NULL;
1261 ULONG count = 0;
1262 DWORD dictOffset = 0;
1264 This->dirty = FALSE;
1265 This->highestProp = 0;
1266 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1267 if (FAILED(hr))
1268 goto end;
1269 if (stat.cbSize.u.HighPart)
1271 WARN("stream too big\n");
1272 /* maximum size varies, but it can't be this big */
1273 hr = STG_E_INVALIDHEADER;
1274 goto end;
1276 if (stat.cbSize.u.LowPart == 0)
1278 /* empty stream is okay */
1279 hr = S_OK;
1280 goto end;
1282 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1283 sizeof(FORMATIDOFFSET))
1285 WARN("stream too small\n");
1286 hr = STG_E_INVALIDHEADER;
1287 goto end;
1289 seek.QuadPart = 0;
1290 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1291 if (FAILED(hr))
1292 goto end;
1293 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1294 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1295 * higher values.
1297 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1299 WARN("bad magic in prop set header\n");
1300 hr = STG_E_INVALIDHEADER;
1301 goto end;
1303 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1305 WARN("bad format version %d\n", hdr.wFormat);
1306 hr = STG_E_INVALIDHEADER;
1307 goto end;
1309 This->format = hdr.wFormat;
1310 This->clsid = hdr.clsid;
1311 This->originatorOS = hdr.dwOSVer;
1312 if (PROPSETHDR_OSVER_KIND(hdr.dwOSVer) == PROPSETHDR_OSVER_KIND_MAC)
1313 WARN("File comes from a Mac, strings will probably be screwed up\n");
1314 hr = PropertyStorage_ReadFmtIdOffsetFromStream(This->stm, &fmtOffset);
1315 if (FAILED(hr))
1316 goto end;
1317 if (fmtOffset.dwOffset > stat.cbSize.u.LowPart)
1319 WARN("invalid offset %d (stream length is %d)\n", fmtOffset.dwOffset,
1320 stat.cbSize.u.LowPart);
1321 hr = STG_E_INVALIDHEADER;
1322 goto end;
1324 /* wackiness alert: if the format ID is FMTID_DocSummaryInformation, there
1325 * follow not one, but two sections. The first is the standard properties
1326 * for the document summary information, and the second is user-defined
1327 * properties. This is the only case in which multiple sections are
1328 * allowed.
1329 * Reading the second stream isn't implemented yet.
1331 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, &sectionHdr);
1332 if (FAILED(hr))
1333 goto end;
1334 /* The section size includes the section header, so check it */
1335 if (sectionHdr.cbSection < sizeof(PROPERTYSECTIONHEADER))
1337 WARN("section header too small, got %d\n", sectionHdr.cbSection);
1338 hr = STG_E_INVALIDHEADER;
1339 goto end;
1341 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1342 sizeof(PROPERTYSECTIONHEADER));
1343 if (!buf)
1345 hr = STG_E_INSUFFICIENTMEMORY;
1346 goto end;
1348 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1349 sizeof(PROPERTYSECTIONHEADER), &count);
1350 if (FAILED(hr))
1351 goto end;
1352 TRACE("Reading %d properties:\n", sectionHdr.cProperties);
1353 for (i = 0; SUCCEEDED(hr) && i < sectionHdr.cProperties; i++)
1355 PROPERTYIDOFFSET *idOffset = (PROPERTYIDOFFSET *)(buf +
1356 i * sizeof(PROPERTYIDOFFSET));
1358 if (idOffset->dwOffset < sizeof(PROPERTYSECTIONHEADER) ||
1359 idOffset->dwOffset >= sectionHdr.cbSection - sizeof(DWORD))
1360 hr = STG_E_INVALIDPOINTER;
1361 else
1363 if (idOffset->propid >= PID_FIRST_USABLE &&
1364 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1365 This->highestProp)
1366 This->highestProp = idOffset->propid;
1367 if (idOffset->propid == PID_DICTIONARY)
1369 /* Don't read the dictionary yet, its entries depend on the
1370 * code page. Just store the offset so we know to read it
1371 * later.
1373 dictOffset = idOffset->dwOffset;
1374 TRACE("Dictionary offset is %d\n", dictOffset);
1376 else
1378 PROPVARIANT prop;
1380 PropVariantInit(&prop);
1381 if (SUCCEEDED(PropertyStorage_ReadProperty(This, &prop,
1382 buf + idOffset->dwOffset - sizeof(PROPERTYSECTIONHEADER))))
1384 TRACE("Read property with ID 0x%08x, type %d\n",
1385 idOffset->propid, prop.vt);
1386 switch(idOffset->propid)
1388 case PID_CODEPAGE:
1389 if (prop.vt == VT_I2)
1390 This->codePage = (UINT)prop.u.iVal;
1391 break;
1392 case PID_LOCALE:
1393 if (prop.vt == VT_I4)
1394 This->locale = (LCID)prop.u.lVal;
1395 break;
1396 case PID_BEHAVIOR:
1397 if (prop.vt == VT_I4 && prop.u.lVal)
1398 This->grfFlags |= PROPSETFLAG_CASE_SENSITIVE;
1399 /* The format should already be 1, but just in case */
1400 This->format = 1;
1401 break;
1402 default:
1403 hr = PropertyStorage_StorePropWithId(This,
1404 idOffset->propid, &prop, This->codePage);
1407 PropVariantClear(&prop);
1411 if (!This->codePage)
1413 /* default to Unicode unless told not to, as specified on msdn */
1414 if (This->grfFlags & PROPSETFLAG_ANSI)
1415 This->codePage = GetACP();
1416 else
1417 This->codePage = CP_UNICODE;
1419 if (!This->locale)
1420 This->locale = LOCALE_SYSTEM_DEFAULT;
1421 TRACE("Code page is %d, locale is %d\n", This->codePage, This->locale);
1422 if (dictOffset)
1423 hr = PropertyStorage_ReadDictionary(This,
1424 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1426 end:
1427 HeapFree(GetProcessHeap(), 0, buf);
1428 if (FAILED(hr))
1430 dictionary_destroy(This->name_to_propid);
1431 This->name_to_propid = NULL;
1432 dictionary_destroy(This->propid_to_name);
1433 This->propid_to_name = NULL;
1434 dictionary_destroy(This->propid_to_prop);
1435 This->propid_to_prop = NULL;
1437 return hr;
1440 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1441 PROPERTYSETHEADER *hdr)
1443 assert(hdr);
1444 StorageUtl_WriteWord((BYTE *)&hdr->wByteOrder, 0,
1445 PROPSETHDR_BYTEORDER_MAGIC);
1446 StorageUtl_WriteWord((BYTE *)&hdr->wFormat, 0, This->format);
1447 StorageUtl_WriteDWord((BYTE *)&hdr->dwOSVer, 0, This->originatorOS);
1448 StorageUtl_WriteGUID((BYTE *)&hdr->clsid, 0, &This->clsid);
1449 StorageUtl_WriteDWord((BYTE *)&hdr->reserved, 0, 1);
1452 static void PropertyStorage_MakeFmtIdOffset(PropertyStorage_impl *This,
1453 FORMATIDOFFSET *fmtOffset)
1455 assert(fmtOffset);
1456 StorageUtl_WriteGUID((BYTE *)fmtOffset, 0, &This->fmtid);
1457 StorageUtl_WriteDWord((BYTE *)fmtOffset, offsetof(FORMATIDOFFSET, dwOffset),
1458 sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET));
1461 static void PropertyStorage_MakeSectionHdr(DWORD cbSection, DWORD numProps,
1462 PROPERTYSECTIONHEADER *hdr)
1464 assert(hdr);
1465 StorageUtl_WriteDWord((BYTE *)hdr, 0, cbSection);
1466 StorageUtl_WriteDWord((BYTE *)hdr,
1467 offsetof(PROPERTYSECTIONHEADER, cProperties), numProps);
1470 static void PropertyStorage_MakePropertyIdOffset(DWORD propid, DWORD dwOffset,
1471 PROPERTYIDOFFSET *propIdOffset)
1473 assert(propIdOffset);
1474 StorageUtl_WriteDWord((BYTE *)propIdOffset, 0, propid);
1475 StorageUtl_WriteDWord((BYTE *)propIdOffset,
1476 offsetof(PROPERTYIDOFFSET, dwOffset), dwOffset);
1479 static inline HRESULT PropertStorage_WriteWStringToStream(IStream *stm,
1480 LPCWSTR str, DWORD len, DWORD *written)
1482 #ifdef WORDS_BIGENDIAN
1483 WCHAR *leStr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1484 HRESULT hr;
1486 if (!leStr)
1487 return E_OUTOFMEMORY;
1488 memcpy(leStr, str, len * sizeof(WCHAR));
1489 PropertyStorage_ByteSwapString(leStr, len);
1490 hr = IStream_Write(stm, leStr, len, written);
1491 HeapFree(GetProcessHeap(), 0, leStr);
1492 return hr;
1493 #else
1494 return IStream_Write(stm, str, len, written);
1495 #endif
1498 struct DictionaryClosure
1500 HRESULT hr;
1501 DWORD bytesWritten;
1504 static BOOL PropertyStorage_DictionaryWriter(const void *key,
1505 const void *value, void *extra, void *closure)
1507 PropertyStorage_impl *This = extra;
1508 struct DictionaryClosure *c = closure;
1509 DWORD propid;
1510 ULONG count;
1512 assert(key);
1513 assert(closure);
1514 StorageUtl_WriteDWord((LPBYTE)&propid, 0, PtrToUlong(value));
1515 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1516 if (FAILED(c->hr))
1517 goto end;
1518 c->bytesWritten += sizeof(DWORD);
1519 if (This->codePage == CP_UNICODE)
1521 DWORD keyLen, pad = 0;
1523 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0,
1524 (lstrlenW((LPCWSTR)key) + 1) * sizeof(WCHAR));
1525 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1526 if (FAILED(c->hr))
1527 goto end;
1528 c->bytesWritten += sizeof(DWORD);
1529 c->hr = PropertStorage_WriteWStringToStream(This->stm, key, keyLen,
1530 &count);
1531 if (FAILED(c->hr))
1532 goto end;
1533 c->bytesWritten += keyLen * sizeof(WCHAR);
1534 if (keyLen % sizeof(DWORD))
1536 c->hr = IStream_Write(This->stm, &pad,
1537 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1538 if (FAILED(c->hr))
1539 goto end;
1540 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1543 else
1545 DWORD keyLen;
1547 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1548 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1549 if (FAILED(c->hr))
1550 goto end;
1551 c->bytesWritten += sizeof(DWORD);
1552 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1553 if (FAILED(c->hr))
1554 goto end;
1555 c->bytesWritten += keyLen;
1557 end:
1558 return SUCCEEDED(c->hr);
1561 #define SECTIONHEADER_OFFSET sizeof(PROPERTYSETHEADER) + sizeof(FORMATIDOFFSET)
1563 /* Writes the dictionary to the stream. Assumes without checking that the
1564 * dictionary isn't empty.
1566 static HRESULT PropertyStorage_WriteDictionaryToStream(
1567 PropertyStorage_impl *This, DWORD *sectionOffset)
1569 HRESULT hr;
1570 LARGE_INTEGER seek;
1571 PROPERTYIDOFFSET propIdOffset;
1572 ULONG count;
1573 DWORD dwTemp;
1574 struct DictionaryClosure closure;
1576 assert(sectionOffset);
1578 /* The dictionary's always the first property written, so seek to its
1579 * spot.
1581 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1582 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1583 if (FAILED(hr))
1584 goto end;
1585 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1586 &propIdOffset);
1587 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1588 if (FAILED(hr))
1589 goto end;
1591 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1592 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1593 if (FAILED(hr))
1594 goto end;
1595 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1596 dictionary_num_entries(This->name_to_propid));
1597 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1598 if (FAILED(hr))
1599 goto end;
1600 *sectionOffset += sizeof(dwTemp);
1602 closure.hr = S_OK;
1603 closure.bytesWritten = 0;
1604 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1605 &closure);
1606 hr = closure.hr;
1607 if (FAILED(hr))
1608 goto end;
1609 *sectionOffset += closure.bytesWritten;
1610 if (closure.bytesWritten % sizeof(DWORD))
1612 DWORD padding = sizeof(DWORD) - closure.bytesWritten % sizeof(DWORD);
1613 TRACE("adding %d bytes of padding\n", padding);
1614 *sectionOffset += padding;
1617 end:
1618 return hr;
1621 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1622 DWORD propNum, DWORD propid, const PROPVARIANT *var, DWORD *sectionOffset)
1624 HRESULT hr;
1625 LARGE_INTEGER seek;
1626 PROPERTYIDOFFSET propIdOffset;
1627 ULONG count;
1628 DWORD dwType, bytesWritten;
1630 assert(var);
1631 assert(sectionOffset);
1633 TRACE("%p, %d, 0x%08x, (%d), (%d)\n", This, propNum, propid, var->vt,
1634 *sectionOffset);
1636 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1637 propNum * sizeof(PROPERTYIDOFFSET);
1638 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1639 if (FAILED(hr))
1640 goto end;
1641 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1642 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1643 if (FAILED(hr))
1644 goto end;
1646 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1647 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1648 if (FAILED(hr))
1649 goto end;
1650 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1651 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1652 if (FAILED(hr))
1653 goto end;
1654 *sectionOffset += sizeof(dwType);
1656 switch (var->vt)
1658 case VT_EMPTY:
1659 case VT_NULL:
1660 bytesWritten = 0;
1661 break;
1662 case VT_I1:
1663 case VT_UI1:
1664 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1665 &count);
1666 bytesWritten = count;
1667 break;
1668 case VT_I2:
1669 case VT_UI2:
1671 WORD wTemp;
1673 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1674 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1675 bytesWritten = count;
1676 break;
1678 case VT_I4:
1679 case VT_UI4:
1681 DWORD dwTemp;
1683 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1684 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1685 bytesWritten = count;
1686 break;
1688 case VT_LPSTR:
1690 DWORD len, dwTemp;
1692 if (This->codePage == CP_UNICODE)
1693 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1694 else
1695 len = lstrlenA(var->u.pszVal) + 1;
1696 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1697 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1698 if (FAILED(hr))
1699 goto end;
1700 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1701 bytesWritten = count + sizeof(DWORD);
1702 break;
1704 case VT_LPWSTR:
1706 DWORD len = lstrlenW(var->u.pwszVal) + 1, dwTemp;
1708 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1709 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1710 if (FAILED(hr))
1711 goto end;
1712 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1713 &count);
1714 bytesWritten = count + sizeof(DWORD);
1715 break;
1717 case VT_FILETIME:
1719 FILETIME temp;
1721 StorageUtl_WriteULargeInteger((BYTE *)&temp, 0,
1722 (const ULARGE_INTEGER *)&var->u.filetime);
1723 hr = IStream_Write(This->stm, &temp, sizeof(FILETIME), &count);
1724 bytesWritten = count;
1725 break;
1727 case VT_CF:
1729 DWORD cf_hdr[2], len;
1731 len = var->u.pclipdata->cbSize;
1732 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[0], 0, len + 8);
1733 StorageUtl_WriteDWord((LPBYTE)&cf_hdr[1], 0, var->u.pclipdata->ulClipFmt);
1734 hr = IStream_Write(This->stm, cf_hdr, sizeof(cf_hdr), &count);
1735 if (FAILED(hr))
1736 goto end;
1737 hr = IStream_Write(This->stm, var->u.pclipdata->pClipData,
1738 len - sizeof(var->u.pclipdata->ulClipFmt), &count);
1739 if (FAILED(hr))
1740 goto end;
1741 bytesWritten = count + sizeof cf_hdr;
1742 break;
1744 default:
1745 FIXME("unsupported type: %d\n", var->vt);
1746 return STG_E_INVALIDPARAMETER;
1749 if (SUCCEEDED(hr))
1751 *sectionOffset += bytesWritten;
1752 if (bytesWritten % sizeof(DWORD))
1754 DWORD padding = sizeof(DWORD) - bytesWritten % sizeof(DWORD);
1755 TRACE("adding %d bytes of padding\n", padding);
1756 *sectionOffset += padding;
1760 end:
1761 return hr;
1764 struct PropertyClosure
1766 HRESULT hr;
1767 DWORD propNum;
1768 DWORD *sectionOffset;
1771 static BOOL PropertyStorage_PropertiesWriter(const void *key, const void *value,
1772 void *extra, void *closure)
1774 PropertyStorage_impl *This = extra;
1775 struct PropertyClosure *c = closure;
1777 assert(key);
1778 assert(value);
1779 assert(extra);
1780 assert(closure);
1781 c->hr = PropertyStorage_WritePropertyToStream(This, c->propNum++,
1782 PtrToUlong(key), value, c->sectionOffset);
1783 return SUCCEEDED(c->hr);
1786 static HRESULT PropertyStorage_WritePropertiesToStream(
1787 PropertyStorage_impl *This, DWORD startingPropNum, DWORD *sectionOffset)
1789 struct PropertyClosure closure;
1791 assert(sectionOffset);
1792 closure.hr = S_OK;
1793 closure.propNum = startingPropNum;
1794 closure.sectionOffset = sectionOffset;
1795 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1796 &closure);
1797 return closure.hr;
1800 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1802 HRESULT hr;
1803 ULONG count = 0;
1804 LARGE_INTEGER seek = { {0} };
1805 PROPERTYSETHEADER hdr;
1806 FORMATIDOFFSET fmtOffset;
1808 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1809 if (FAILED(hr))
1810 goto end;
1811 PropertyStorage_MakeHeader(This, &hdr);
1812 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1813 if (FAILED(hr))
1814 goto end;
1815 if (count != sizeof(hdr))
1817 hr = STG_E_WRITEFAULT;
1818 goto end;
1821 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1822 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1823 if (FAILED(hr))
1824 goto end;
1825 if (count != sizeof(fmtOffset))
1827 hr = STG_E_WRITEFAULT;
1828 goto end;
1830 hr = S_OK;
1832 end:
1833 return hr;
1836 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1838 PROPERTYSECTIONHEADER sectionHdr;
1839 HRESULT hr;
1840 ULONG count;
1841 LARGE_INTEGER seek;
1842 DWORD numProps, prop, sectionOffset, dwTemp;
1843 PROPVARIANT var;
1845 PropertyStorage_WriteHeadersToStream(This);
1847 /* Count properties. Always at least one property, the code page */
1848 numProps = 1;
1849 if (dictionary_num_entries(This->name_to_propid))
1850 numProps++;
1851 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1852 numProps++;
1853 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1854 numProps++;
1855 numProps += dictionary_num_entries(This->propid_to_prop);
1857 /* Write section header with 0 bytes right now, I'll adjust it after
1858 * writing properties.
1860 PropertyStorage_MakeSectionHdr(0, numProps, &sectionHdr);
1861 seek.QuadPart = SECTIONHEADER_OFFSET;
1862 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1863 if (FAILED(hr))
1864 goto end;
1865 hr = IStream_Write(This->stm, &sectionHdr, sizeof(sectionHdr), &count);
1866 if (FAILED(hr))
1867 goto end;
1869 prop = 0;
1870 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1871 numProps * sizeof(PROPERTYIDOFFSET);
1873 if (dictionary_num_entries(This->name_to_propid))
1875 prop++;
1876 hr = PropertyStorage_WriteDictionaryToStream(This, &sectionOffset);
1877 if (FAILED(hr))
1878 goto end;
1881 PropVariantInit(&var);
1883 var.vt = VT_I2;
1884 var.u.iVal = This->codePage;
1885 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1886 &var, &sectionOffset);
1887 if (FAILED(hr))
1888 goto end;
1890 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1892 var.vt = VT_I4;
1893 var.u.lVal = This->locale;
1894 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1895 &var, &sectionOffset);
1896 if (FAILED(hr))
1897 goto end;
1900 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1902 var.vt = VT_I4;
1903 var.u.lVal = 1;
1904 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1905 &var, &sectionOffset);
1906 if (FAILED(hr))
1907 goto end;
1910 hr = PropertyStorage_WritePropertiesToStream(This, prop, &sectionOffset);
1911 if (FAILED(hr))
1912 goto end;
1914 /* Now write the byte count of the section */
1915 seek.QuadPart = SECTIONHEADER_OFFSET;
1916 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1917 if (FAILED(hr))
1918 goto end;
1919 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1920 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1922 end:
1923 return hr;
1926 /***********************************************************************
1927 * PropertyStorage_Construct
1929 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1931 dictionary_destroy(This->name_to_propid);
1932 This->name_to_propid = NULL;
1933 dictionary_destroy(This->propid_to_name);
1934 This->propid_to_name = NULL;
1935 dictionary_destroy(This->propid_to_prop);
1936 This->propid_to_prop = NULL;
1939 static HRESULT PropertyStorage_CreateDictionaries(PropertyStorage_impl *This)
1941 HRESULT hr = S_OK;
1943 This->name_to_propid = dictionary_create(
1944 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1945 This);
1946 if (!This->name_to_propid)
1948 hr = STG_E_INSUFFICIENTMEMORY;
1949 goto end;
1951 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1952 NULL, This);
1953 if (!This->propid_to_name)
1955 hr = STG_E_INSUFFICIENTMEMORY;
1956 goto end;
1958 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1959 PropertyStorage_PropertyDestroy, This);
1960 if (!This->propid_to_prop)
1962 hr = STG_E_INSUFFICIENTMEMORY;
1963 goto end;
1965 end:
1966 if (FAILED(hr))
1967 PropertyStorage_DestroyDictionaries(This);
1968 return hr;
1971 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1972 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1974 HRESULT hr = S_OK;
1976 assert(pps);
1977 assert(rfmtid);
1978 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1979 if (!*pps)
1980 return E_OUTOFMEMORY;
1982 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1983 (*pps)->ref = 1;
1984 InitializeCriticalSection(&(*pps)->cs);
1985 (*pps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PropertyStorage_impl.cs");
1986 (*pps)->stm = stm;
1987 (*pps)->fmtid = *rfmtid;
1988 (*pps)->grfMode = grfMode;
1990 hr = PropertyStorage_CreateDictionaries(*pps);
1991 if (FAILED(hr))
1993 IStream_Release(stm);
1994 (*pps)->cs.DebugInfo->Spare[0] = 0;
1995 DeleteCriticalSection(&(*pps)->cs);
1996 HeapFree(GetProcessHeap(), 0, *pps);
1997 *pps = NULL;
2000 return hr;
2003 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
2004 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2006 PropertyStorage_impl *ps;
2007 HRESULT hr;
2009 assert(pps);
2010 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2011 if (SUCCEEDED(hr))
2013 hr = PropertyStorage_ReadFromStream(ps);
2014 if (SUCCEEDED(hr))
2016 *pps = (IPropertyStorage *)ps;
2017 TRACE("PropertyStorage %p constructed\n", ps);
2018 hr = S_OK;
2020 else
2022 PropertyStorage_DestroyDictionaries(ps);
2023 HeapFree(GetProcessHeap(), 0, ps);
2026 return hr;
2029 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2030 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2032 PropertyStorage_impl *ps;
2033 HRESULT hr;
2035 assert(pps);
2036 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2037 if (SUCCEEDED(hr))
2039 ps->format = 0;
2040 ps->grfFlags = grfFlags;
2041 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2042 ps->format = 1;
2043 /* default to Unicode unless told not to, as specified on msdn */
2044 if (ps->grfFlags & PROPSETFLAG_ANSI)
2045 ps->codePage = GetACP();
2046 else
2047 ps->codePage = CP_UNICODE;
2048 ps->locale = LOCALE_SYSTEM_DEFAULT;
2049 TRACE("Code page is %d, locale is %d\n", ps->codePage, ps->locale);
2050 *pps = (IPropertyStorage *)ps;
2051 TRACE("PropertyStorage %p constructed\n", ps);
2052 hr = S_OK;
2054 return hr;
2058 /***********************************************************************
2059 * Implementation of IPropertySetStorage
2062 /************************************************************************
2063 * IPropertySetStorage_fnQueryInterface (IUnknown)
2065 * This method forwards to the common QueryInterface implementation
2067 static HRESULT WINAPI IPropertySetStorage_fnQueryInterface(
2068 IPropertySetStorage *ppstg,
2069 REFIID riid,
2070 void** ppvObject)
2072 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2073 return IStorage_QueryInterface( (IStorage*)This, riid, ppvObject );
2076 /************************************************************************
2077 * IPropertySetStorage_fnAddRef (IUnknown)
2079 * This method forwards to the common AddRef implementation
2081 static ULONG WINAPI IPropertySetStorage_fnAddRef(
2082 IPropertySetStorage *ppstg)
2084 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2085 return IStorage_AddRef( (IStorage*)This );
2088 /************************************************************************
2089 * IPropertySetStorage_fnRelease (IUnknown)
2091 * This method forwards to the common Release implementation
2093 static ULONG WINAPI IPropertySetStorage_fnRelease(
2094 IPropertySetStorage *ppstg)
2096 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2097 return IStorage_Release( (IStorage*)This );
2100 /************************************************************************
2101 * IPropertySetStorage_fnCreate (IPropertySetStorage)
2103 static HRESULT WINAPI IPropertySetStorage_fnCreate(
2104 IPropertySetStorage *ppstg,
2105 REFFMTID rfmtid,
2106 const CLSID* pclsid,
2107 DWORD grfFlags,
2108 DWORD grfMode,
2109 IPropertyStorage** ppprstg)
2111 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2112 WCHAR name[CCH_MAX_PROPSTG_NAME];
2113 IStream *stm = NULL;
2114 HRESULT r;
2116 TRACE("%p %s %08x %08x %p\n", This, debugstr_guid(rfmtid), grfFlags,
2117 grfMode, ppprstg);
2119 /* be picky */
2120 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2122 r = STG_E_INVALIDFLAG;
2123 goto end;
2126 if (!rfmtid)
2128 r = E_INVALIDARG;
2129 goto end;
2132 /* FIXME: if (grfFlags & PROPSETFLAG_NONSIMPLE), we need to create a
2133 * storage, not a stream. For now, disallow it.
2135 if (grfFlags & PROPSETFLAG_NONSIMPLE)
2137 FIXME("PROPSETFLAG_NONSIMPLE not supported\n");
2138 r = STG_E_INVALIDFLAG;
2139 goto end;
2142 r = FmtIdToPropStgName(rfmtid, name);
2143 if (FAILED(r))
2144 goto end;
2146 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2147 if (FAILED(r))
2148 goto end;
2150 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2152 end:
2153 TRACE("returning 0x%08x\n", r);
2154 return r;
2157 /************************************************************************
2158 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2160 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2161 IPropertySetStorage *ppstg,
2162 REFFMTID rfmtid,
2163 DWORD grfMode,
2164 IPropertyStorage** ppprstg)
2166 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2167 IStream *stm = NULL;
2168 WCHAR name[CCH_MAX_PROPSTG_NAME];
2169 HRESULT r;
2171 TRACE("%p %s %08x %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2173 /* be picky */
2174 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2175 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2177 r = STG_E_INVALIDFLAG;
2178 goto end;
2181 if (!rfmtid)
2183 r = E_INVALIDARG;
2184 goto end;
2187 r = FmtIdToPropStgName(rfmtid, name);
2188 if (FAILED(r))
2189 goto end;
2191 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2192 if (FAILED(r))
2193 goto end;
2195 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2197 end:
2198 TRACE("returning 0x%08x\n", r);
2199 return r;
2202 /************************************************************************
2203 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2205 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2206 IPropertySetStorage *ppstg,
2207 REFFMTID rfmtid)
2209 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2210 IStorage *stg = NULL;
2211 WCHAR name[CCH_MAX_PROPSTG_NAME];
2212 HRESULT r;
2214 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2216 if (!rfmtid)
2217 return E_INVALIDARG;
2219 r = FmtIdToPropStgName(rfmtid, name);
2220 if (FAILED(r))
2221 return r;
2223 stg = (IStorage*) This;
2224 return IStorage_DestroyElement(stg, name);
2227 /************************************************************************
2228 * IPropertySetStorage_fnEnum (IPropertySetStorage)
2230 static HRESULT WINAPI IPropertySetStorage_fnEnum(
2231 IPropertySetStorage *ppstg,
2232 IEnumSTATPROPSETSTG** ppenum)
2234 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2235 return create_EnumSTATPROPSETSTG(This, ppenum);
2238 /************************************************************************
2239 * Implement IEnumSTATPROPSETSTG using enumx
2241 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnQueryInterface(
2242 IEnumSTATPROPSETSTG *iface,
2243 REFIID riid,
2244 void** ppvObject)
2246 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2249 static ULONG WINAPI IEnumSTATPROPSETSTG_fnAddRef(
2250 IEnumSTATPROPSETSTG *iface)
2252 return enumx_AddRef((enumx_impl*)iface);
2255 static ULONG WINAPI IEnumSTATPROPSETSTG_fnRelease(
2256 IEnumSTATPROPSETSTG *iface)
2258 return enumx_Release((enumx_impl*)iface);
2261 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnNext(
2262 IEnumSTATPROPSETSTG *iface,
2263 ULONG celt,
2264 STATPROPSETSTG *rgelt,
2265 ULONG *pceltFetched)
2267 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2270 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnSkip(
2271 IEnumSTATPROPSETSTG *iface,
2272 ULONG celt)
2274 return enumx_Skip((enumx_impl*)iface, celt);
2277 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnReset(
2278 IEnumSTATPROPSETSTG *iface)
2280 return enumx_Reset((enumx_impl*)iface);
2283 static HRESULT WINAPI IEnumSTATPROPSETSTG_fnClone(
2284 IEnumSTATPROPSETSTG *iface,
2285 IEnumSTATPROPSETSTG **ppenum)
2287 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2290 static HRESULT create_EnumSTATPROPSETSTG(
2291 StorageImpl *This,
2292 IEnumSTATPROPSETSTG** ppenum)
2294 IStorage *stg = (IStorage*) &This->base.lpVtbl;
2295 IEnumSTATSTG *penum = NULL;
2296 STATSTG stat;
2297 ULONG count;
2298 HRESULT r;
2299 STATPROPSETSTG statpss;
2300 enumx_impl *enumx;
2302 TRACE("%p %p\n", This, ppenum);
2304 enumx = enumx_allocate(&IID_IEnumSTATPROPSETSTG,
2305 &IEnumSTATPROPSETSTG_Vtbl,
2306 sizeof (STATPROPSETSTG));
2308 /* add all the property set elements into a list */
2309 r = IStorage_EnumElements(stg, 0, NULL, 0, &penum);
2310 if (FAILED(r))
2311 return E_OUTOFMEMORY;
2313 while (1)
2315 count = 0;
2316 r = IEnumSTATSTG_Next(penum, 1, &stat, &count);
2317 if (FAILED(r))
2318 break;
2319 if (!count)
2320 break;
2321 if (!stat.pwcsName)
2322 continue;
2323 if (stat.pwcsName[0] == 5 && stat.type == STGTY_STREAM)
2325 PropStgNameToFmtId(stat.pwcsName, &statpss.fmtid);
2326 TRACE("adding %s (%s)\n", debugstr_w(stat.pwcsName),
2327 debugstr_guid(&statpss.fmtid));
2328 statpss.mtime = stat.mtime;
2329 statpss.atime = stat.atime;
2330 statpss.ctime = stat.ctime;
2331 statpss.grfFlags = stat.grfMode;
2332 statpss.clsid = stat.clsid;
2333 enumx_add_element(enumx, &statpss);
2335 CoTaskMemFree(stat.pwcsName);
2337 IEnumSTATSTG_Release(penum);
2339 *ppenum = (IEnumSTATPROPSETSTG*) enumx;
2341 return S_OK;
2344 /************************************************************************
2345 * Implement IEnumSTATPROPSTG using enumx
2347 static HRESULT WINAPI IEnumSTATPROPSTG_fnQueryInterface(
2348 IEnumSTATPROPSTG *iface,
2349 REFIID riid,
2350 void** ppvObject)
2352 return enumx_QueryInterface((enumx_impl*)iface, riid, ppvObject);
2355 static ULONG WINAPI IEnumSTATPROPSTG_fnAddRef(
2356 IEnumSTATPROPSTG *iface)
2358 return enumx_AddRef((enumx_impl*)iface);
2361 static ULONG WINAPI IEnumSTATPROPSTG_fnRelease(
2362 IEnumSTATPROPSTG *iface)
2364 return enumx_Release((enumx_impl*)iface);
2367 static HRESULT WINAPI IEnumSTATPROPSTG_fnNext(
2368 IEnumSTATPROPSTG *iface,
2369 ULONG celt,
2370 STATPROPSTG *rgelt,
2371 ULONG *pceltFetched)
2373 return enumx_Next((enumx_impl*)iface, celt, rgelt, pceltFetched);
2376 static HRESULT WINAPI IEnumSTATPROPSTG_fnSkip(
2377 IEnumSTATPROPSTG *iface,
2378 ULONG celt)
2380 return enumx_Skip((enumx_impl*)iface, celt);
2383 static HRESULT WINAPI IEnumSTATPROPSTG_fnReset(
2384 IEnumSTATPROPSTG *iface)
2386 return enumx_Reset((enumx_impl*)iface);
2389 static HRESULT WINAPI IEnumSTATPROPSTG_fnClone(
2390 IEnumSTATPROPSTG *iface,
2391 IEnumSTATPROPSTG **ppenum)
2393 return enumx_Clone((enumx_impl*)iface, (enumx_impl**)ppenum);
2396 static BOOL prop_enum_stat(const void *k, const void *v, void *extra, void *arg)
2398 enumx_impl *enumx = arg;
2399 PROPID propid = PtrToUlong(k);
2400 const PROPVARIANT *prop = v;
2401 STATPROPSTG stat;
2403 stat.lpwstrName = NULL;
2404 stat.propid = propid;
2405 stat.vt = prop->vt;
2407 enumx_add_element(enumx, &stat);
2409 return TRUE;
2412 static HRESULT create_EnumSTATPROPSTG(
2413 PropertyStorage_impl *This,
2414 IEnumSTATPROPSTG** ppenum)
2416 enumx_impl *enumx;
2418 TRACE("%p %p\n", This, ppenum);
2420 enumx = enumx_allocate(&IID_IEnumSTATPROPSTG,
2421 &IEnumSTATPROPSTG_Vtbl,
2422 sizeof (STATPROPSTG));
2424 dictionary_enumerate(This->propid_to_prop, prop_enum_stat, enumx);
2426 *ppenum = (IEnumSTATPROPSTG*) enumx;
2428 return S_OK;
2431 /***********************************************************************
2432 * vtables
2434 const IPropertySetStorageVtbl IPropertySetStorage_Vtbl =
2436 IPropertySetStorage_fnQueryInterface,
2437 IPropertySetStorage_fnAddRef,
2438 IPropertySetStorage_fnRelease,
2439 IPropertySetStorage_fnCreate,
2440 IPropertySetStorage_fnOpen,
2441 IPropertySetStorage_fnDelete,
2442 IPropertySetStorage_fnEnum
2445 static const IPropertyStorageVtbl IPropertyStorage_Vtbl =
2447 IPropertyStorage_fnQueryInterface,
2448 IPropertyStorage_fnAddRef,
2449 IPropertyStorage_fnRelease,
2450 IPropertyStorage_fnReadMultiple,
2451 IPropertyStorage_fnWriteMultiple,
2452 IPropertyStorage_fnDeleteMultiple,
2453 IPropertyStorage_fnReadPropertyNames,
2454 IPropertyStorage_fnWritePropertyNames,
2455 IPropertyStorage_fnDeletePropertyNames,
2456 IPropertyStorage_fnCommit,
2457 IPropertyStorage_fnRevert,
2458 IPropertyStorage_fnEnum,
2459 IPropertyStorage_fnSetTimes,
2460 IPropertyStorage_fnSetClass,
2461 IPropertyStorage_fnStat,
2464 static const IEnumSTATPROPSETSTGVtbl IEnumSTATPROPSETSTG_Vtbl =
2466 IEnumSTATPROPSETSTG_fnQueryInterface,
2467 IEnumSTATPROPSETSTG_fnAddRef,
2468 IEnumSTATPROPSETSTG_fnRelease,
2469 IEnumSTATPROPSETSTG_fnNext,
2470 IEnumSTATPROPSETSTG_fnSkip,
2471 IEnumSTATPROPSETSTG_fnReset,
2472 IEnumSTATPROPSETSTG_fnClone,
2475 static const IEnumSTATPROPSTGVtbl IEnumSTATPROPSTG_Vtbl =
2477 IEnumSTATPROPSTG_fnQueryInterface,
2478 IEnumSTATPROPSTG_fnAddRef,
2479 IEnumSTATPROPSTG_fnRelease,
2480 IEnumSTATPROPSTG_fnNext,
2481 IEnumSTATPROPSTG_fnSkip,
2482 IEnumSTATPROPSTG_fnReset,
2483 IEnumSTATPROPSTG_fnClone,
2486 /***********************************************************************
2487 * Format ID <-> name conversion
2489 static const WCHAR szSummaryInfo[] = { 5,'S','u','m','m','a','r','y',
2490 'I','n','f','o','r','m','a','t','i','o','n',0 };
2491 static const WCHAR szDocSummaryInfo[] = { 5,'D','o','c','u','m','e','n','t',
2492 'S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
2494 #define BITS_PER_BYTE 8
2495 #define CHARMASK 0x1f
2496 #define BITS_IN_CHARMASK 5
2497 #define NUM_ALPHA_CHARS 26
2499 /***********************************************************************
2500 * FmtIdToPropStgName [ole32.@]
2501 * Returns the storage name of the format ID rfmtid.
2502 * PARAMS
2503 * rfmtid [I] Format ID for which to return a storage name
2504 * str [O] Storage name associated with rfmtid.
2506 * RETURNS
2507 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2509 * NOTES
2510 * str must be at least CCH_MAX_PROPSTG_NAME characters in length.
2512 HRESULT WINAPI FmtIdToPropStgName(const FMTID *rfmtid, LPOLESTR str)
2514 static const char fmtMap[] = "abcdefghijklmnopqrstuvwxyz012345";
2516 TRACE("%s, %p\n", debugstr_guid(rfmtid), str);
2518 if (!rfmtid) return E_INVALIDARG;
2519 if (!str) return E_INVALIDARG;
2521 if (IsEqualGUID(&FMTID_SummaryInformation, rfmtid))
2522 lstrcpyW(str, szSummaryInfo);
2523 else if (IsEqualGUID(&FMTID_DocSummaryInformation, rfmtid))
2524 lstrcpyW(str, szDocSummaryInfo);
2525 else if (IsEqualGUID(&FMTID_UserDefinedProperties, rfmtid))
2526 lstrcpyW(str, szDocSummaryInfo);
2527 else
2529 const BYTE *fmtptr;
2530 WCHAR *pstr = str;
2531 ULONG bitsRemaining = BITS_PER_BYTE;
2533 *pstr++ = 5;
2534 for (fmtptr = (const BYTE *)rfmtid; fmtptr < (const BYTE *)rfmtid + sizeof(FMTID); )
2536 ULONG i = *fmtptr >> (BITS_PER_BYTE - bitsRemaining);
2538 if (bitsRemaining >= BITS_IN_CHARMASK)
2540 *pstr = (WCHAR)(fmtMap[i & CHARMASK]);
2541 if (bitsRemaining == BITS_PER_BYTE && *pstr >= 'a' &&
2542 *pstr <= 'z')
2543 *pstr += 'A' - 'a';
2544 pstr++;
2545 bitsRemaining -= BITS_IN_CHARMASK;
2546 if (bitsRemaining == 0)
2548 fmtptr++;
2549 bitsRemaining = BITS_PER_BYTE;
2552 else
2554 if (++fmtptr < (const BYTE *)rfmtid + sizeof(FMTID))
2555 i |= *fmtptr << bitsRemaining;
2556 *pstr++ = (WCHAR)(fmtMap[i & CHARMASK]);
2557 bitsRemaining += BITS_PER_BYTE - BITS_IN_CHARMASK;
2560 *pstr = 0;
2562 TRACE("returning %s\n", debugstr_w(str));
2563 return S_OK;
2566 /***********************************************************************
2567 * PropStgNameToFmtId [ole32.@]
2568 * Returns the format ID corresponding to the given name.
2569 * PARAMS
2570 * str [I] Storage name to convert to a format ID.
2571 * rfmtid [O] Format ID corresponding to str.
2573 * RETURNS
2574 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2575 * a format ID, S_OK otherwise.
2577 HRESULT WINAPI PropStgNameToFmtId(const LPOLESTR str, FMTID *rfmtid)
2579 HRESULT hr = STG_E_INVALIDNAME;
2581 TRACE("%s, %p\n", debugstr_w(str), rfmtid);
2583 if (!rfmtid) return E_INVALIDARG;
2584 if (!str) return STG_E_INVALIDNAME;
2586 if (!lstrcmpiW(str, szDocSummaryInfo))
2588 *rfmtid = FMTID_DocSummaryInformation;
2589 hr = S_OK;
2591 else if (!lstrcmpiW(str, szSummaryInfo))
2593 *rfmtid = FMTID_SummaryInformation;
2594 hr = S_OK;
2596 else
2598 ULONG bits;
2599 BYTE *fmtptr = (BYTE *)rfmtid - 1;
2600 const WCHAR *pstr = str;
2602 memset(rfmtid, 0, sizeof(*rfmtid));
2603 for (bits = 0; bits < sizeof(FMTID) * BITS_PER_BYTE;
2604 bits += BITS_IN_CHARMASK)
2606 ULONG bitsUsed = bits % BITS_PER_BYTE, bitsStored;
2607 WCHAR wc;
2609 if (bitsUsed == 0)
2610 fmtptr++;
2611 wc = *++pstr - 'A';
2612 if (wc > NUM_ALPHA_CHARS)
2614 wc += 'A' - 'a';
2615 if (wc > NUM_ALPHA_CHARS)
2617 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2618 if (wc > CHARMASK)
2620 WARN("invalid character (%d)\n", *pstr);
2621 goto end;
2625 *fmtptr |= wc << bitsUsed;
2626 bitsStored = min(BITS_PER_BYTE - bitsUsed, BITS_IN_CHARMASK);
2627 if (bitsStored < BITS_IN_CHARMASK)
2629 wc >>= BITS_PER_BYTE - bitsUsed;
2630 if (bits + bitsStored == sizeof(FMTID) * BITS_PER_BYTE)
2632 if (wc != 0)
2634 WARN("extra bits\n");
2635 goto end;
2637 break;
2639 fmtptr++;
2640 *fmtptr |= (BYTE)wc;
2643 hr = S_OK;
2645 end:
2646 return hr;