ole32: Only return S_FALSE in IPropertyStorage::ReadMultiple if a property was not...
[wine/multimedia.git] / dlls / ole32 / stg_prop.c
blobc7a7b6ec8c0646ffeb5987e9e20eab3328651d3d
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 * 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.
33 * TODO:
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
45 #include <assert.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #define COBJMACROS
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
55 #include "windef.h"
56 #include "winbase.h"
57 #include "winnls.h"
58 #include "winuser.h"
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 */
100 } PROPERTYSETHEADER;
102 typedef struct tagFORMATIDOFFSET
104 FMTID fmtid;
105 DWORD dwOffset; /* from beginning of stream */
106 } FORMATIDOFFSET;
108 typedef struct tagPROPERTYSECTIONHEADER
110 DWORD cbSection;
111 DWORD cProperties;
112 } PROPERTYSECTIONHEADER;
114 typedef struct tagPROPERTYIDOFFSET
116 DWORD propid;
117 DWORD dwOffset; /* from beginning of section */
118 } PROPERTYIDOFFSET;
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,
154 LCID targetCP);
156 static const IPropertyStorageVtbl IPropertyStorage_Vtbl;
158 /***********************************************************************
159 * Implementation of IPropertyStorage
161 typedef struct tagPropertyStorage_impl
163 const IPropertyStorageVtbl *vtbl;
164 LONG ref;
165 CRITICAL_SECTION cs;
166 IStream *stm;
167 BOOL dirty;
168 FMTID fmtid;
169 CLSID clsid;
170 WORD format;
171 DWORD originatorOS;
172 DWORD grfFlags;
173 DWORD grfMode;
174 UINT codePage;
175 LCID locale;
176 PROPID highestProp;
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,
187 REFIID riid,
188 void** ppvObject)
190 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
192 if ( (This==0) || (ppvObject==0) )
193 return E_INVALIDARG;
195 *ppvObject = 0;
197 if (IsEqualGUID(&IID_IUnknown, riid) ||
198 IsEqualGUID(&IID_IPropertyStorage, riid))
200 IPropertyStorage_AddRef(iface);
201 *ppvObject = (IPropertyStorage*)iface;
202 return S_OK;
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;
225 ULONG ref;
227 ref = InterlockedDecrement(&This->ref);
228 if (ref == 0)
230 TRACE("Destroying %p\n", This);
231 if (This->dirty)
232 IPropertyStorage_Commit(iface, STGC_DEFAULT);
233 IStream_Release(This->stm);
234 DeleteCriticalSection(&This->cs);
235 PropertyStorage_DestroyDictionaries(This);
236 HeapFree(GetProcessHeap(), 0, This);
238 return ref;
241 static PROPVARIANT *PropertyStorage_FindProperty(PropertyStorage_impl *This,
242 DWORD propid)
244 PROPVARIANT *ret = NULL;
246 assert(This);
247 dictionary_find(This->propid_to_prop, (void *)propid, (void **)&ret);
248 TRACE("returning %p\n", ret);
249 return ret;
252 /* Returns NULL if name is NULL. */
253 static PROPVARIANT *PropertyStorage_FindPropertyByName(
254 PropertyStorage_impl *This, LPCWSTR name)
256 PROPVARIANT *ret = NULL;
257 PROPID propid;
259 assert(This);
260 if (!name)
261 return 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);
267 else
269 LPSTR ansiName;
270 HRESULT hr = PropertyStorage_StringCopy((LPCSTR)name, CP_UNICODE,
271 &ansiName, This->codePage);
273 if (SUCCEEDED(hr))
275 if (dictionary_find(This->name_to_propid, ansiName,
276 (void **)&propid))
277 ret = PropertyStorage_FindProperty(This, (PROPID)propid);
278 CoTaskMemFree(ansiName);
281 TRACE("returning %p\n", ret);
282 return ret;
285 static LPWSTR PropertyStorage_FindPropertyNameById(PropertyStorage_impl *This,
286 DWORD propid)
288 LPWSTR ret = NULL;
290 assert(This);
291 dictionary_find(This->propid_to_name, (void *)propid, (void **)&ret);
292 TRACE("returning %p\n", ret);
293 return ret;
296 /************************************************************************
297 * IPropertyStorage_fnReadMultiple (IPropertyStorage)
299 static HRESULT WINAPI IPropertyStorage_fnReadMultiple(
300 IPropertyStorage* iface,
301 ULONG cpspec,
302 const PROPSPEC rgpspec[],
303 PROPVARIANT rgpropvar[])
305 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
306 HRESULT hr = S_OK;
307 ULONG i;
309 TRACE("(%p, %ld, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
310 if (!This)
311 return E_INVALIDARG;
312 if (cpspec && (!rgpspec || !rgpropvar))
313 return E_INVALIDARG;
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);
323 if (prop)
324 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop, GetACP(),
325 This->codePage);
327 else
329 switch (rgpspec[i].u.propid)
331 case PID_CODEPAGE:
332 rgpropvar[i].vt = VT_I2;
333 rgpropvar[i].u.iVal = This->codePage;
334 break;
335 case PID_LOCALE:
336 rgpropvar[i].vt = VT_I4;
337 rgpropvar[i].u.lVal = This->locale;
338 break;
339 default:
341 PROPVARIANT *prop = PropertyStorage_FindProperty(This,
342 rgpspec[i].u.propid);
344 if (prop)
345 PropertyStorage_PropVariantCopy(&rgpropvar[i], prop,
346 GetACP(), This->codePage);
347 else
348 hr = S_FALSE;
353 LeaveCriticalSection(&This->cs);
354 return hr;
357 static HRESULT PropertyStorage_StringCopy(LPCSTR src, LCID srcCP, LPSTR *dst,
358 LCID dstCP)
360 HRESULT hr = S_OK;
361 int len;
363 TRACE("%s, %p, %ld, %ld\n",
364 srcCP == CP_UNICODE ? debugstr_w((LPCWSTR)src) : debugstr_a(src), dst,
365 dstCP, srcCP);
366 assert(src);
367 assert(dst);
368 *dst = NULL;
369 if (dstCP == srcCP)
371 size_t len;
373 if (dstCP == CP_UNICODE)
374 len = (strlenW((LPCWSTR)src) + 1) * sizeof(WCHAR);
375 else
376 len = strlen(src) + 1;
377 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
378 if (!*dst)
379 hr = STG_E_INSUFFICIENTMEMORY;
380 else
381 memcpy(*dst, src, len);
383 else
385 if (dstCP == CP_UNICODE)
387 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
388 *dst = CoTaskMemAlloc(len * sizeof(WCHAR));
389 if (!*dst)
390 hr = STG_E_INSUFFICIENTMEMORY;
391 else
392 MultiByteToWideChar(srcCP, 0, src, -1, (LPWSTR)*dst, len);
394 else
396 LPWSTR wideStr;
398 if (srcCP == CP_UNICODE)
399 wideStr = (LPWSTR)src;
400 else
402 len = MultiByteToWideChar(srcCP, 0, src, -1, NULL, 0);
403 wideStr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
404 if (wideStr)
405 MultiByteToWideChar(srcCP, 0, src, -1, wideStr, len);
406 else
407 hr = STG_E_INSUFFICIENTMEMORY;
409 if (SUCCEEDED(hr))
411 len = WideCharToMultiByte(dstCP, 0, wideStr, -1, NULL, 0,
412 NULL, NULL);
413 *dst = CoTaskMemAlloc(len);
414 if (!*dst)
415 hr = STG_E_INSUFFICIENTMEMORY;
416 else
418 BOOL defCharUsed = FALSE;
420 if (WideCharToMultiByte(dstCP, 0, wideStr, -1, *dst, len,
421 NULL, &defCharUsed) == 0 || defCharUsed)
423 CoTaskMemFree(*dst);
424 *dst = NULL;
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));
435 return hr;
438 static HRESULT PropertyStorage_PropVariantCopy(PROPVARIANT *prop,
439 const PROPVARIANT *propvar, LCID targetCP, LCID srcCP)
441 HRESULT hr = S_OK;
443 assert(prop);
444 assert(propvar);
445 if (propvar->vt == VT_LPSTR)
447 hr = PropertyStorage_StringCopy(propvar->u.pszVal, srcCP,
448 &prop->u.pszVal, targetCP);
449 if (SUCCEEDED(hr))
450 prop->vt = VT_LPSTR;
452 else
453 PropVariantCopy(prop, propvar);
454 return hr;
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)
467 HRESULT hr = S_OK;
468 PROPVARIANT *prop = PropertyStorage_FindProperty(This, propid);
470 assert(This);
471 assert(propvar);
472 if (propvar->vt & VT_BYREF || propvar->vt & VT_ARRAY)
473 This->format = 1;
474 switch (propvar->vt)
476 case VT_DECIMAL:
477 case VT_I1:
478 case VT_INT:
479 case VT_UINT:
480 case VT_VECTOR|VT_I1:
481 This->format = 1;
483 TRACE("Setting 0x%08lx to type %d\n", propid, propvar->vt);
484 if (prop)
486 PropVariantClear(prop);
487 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
488 lcid);
490 else
492 prop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
493 sizeof(PROPVARIANT));
494 if (prop)
496 hr = PropertyStorage_PropVariantCopy(prop, propvar, This->codePage,
497 lcid);
498 if (SUCCEEDED(hr))
500 dictionary_insert(This->propid_to_prop, (void *)propid, prop);
501 if (propid > This->highestProp)
502 This->highestProp = propid;
504 else
505 HeapFree(GetProcessHeap(), 0, prop);
507 else
508 hr = STG_E_INSUFFICIENTMEMORY;
510 return hr;
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)
523 LPSTR name;
524 HRESULT hr;
526 assert(srcName);
528 hr = PropertyStorage_StringCopy((LPCSTR)srcName, cp, &name, This->codePage);
529 if (SUCCEEDED(hr))
531 if (This->codePage == CP_UNICODE)
533 if (lstrlenW((LPWSTR)name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
534 This->format = 1;
536 else
538 if (strlen(name) >= MAX_VERSION_0_PROP_NAME_LENGTH)
539 This->format = 1;
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);
547 return hr;
550 /************************************************************************
551 * IPropertyStorage_fnWriteMultiple (IPropertyStorage)
553 static HRESULT WINAPI IPropertyStorage_fnWriteMultiple(
554 IPropertyStorage* iface,
555 ULONG cpspec,
556 const PROPSPEC rgpspec[],
557 const PROPVARIANT rgpropvar[],
558 PROPID propidNameFirst)
560 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
561 HRESULT hr = S_OK;
562 ULONG i;
564 TRACE("(%p, %ld, %p, %p)\n", iface, cpspec, rgpspec, rgpropvar);
565 if (!This)
566 return E_INVALIDARG;
567 if (cpspec && (!rgpspec || !rgpropvar))
568 return E_INVALIDARG;
569 if (!(This->grfMode & STGM_READWRITE))
570 return STG_E_ACCESSDENIED;
571 EnterCriticalSection(&This->cs);
572 This->dirty = TRUE;
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);
582 if (prop)
583 PropVariantCopy(prop, &rgpropvar[i]);
584 else
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;
592 else
594 PROPID nextId = max(propidNameFirst, This->highestProp + 1);
596 hr = PropertyStorage_StoreNameWithId(This,
597 (LPCSTR)rgpspec[i].u.lpwstr, CP_UNICODE, nextId);
598 if (SUCCEEDED(hr))
599 hr = PropertyStorage_StorePropWithId(This, nextId,
600 &rgpropvar[i], GetACP());
604 else
606 switch (rgpspec[i].u.propid)
608 case PID_DICTIONARY:
609 /* Can't set the dictionary */
610 hr = STG_E_INVALIDPARAMETER;
611 break;
612 case PID_CODEPAGE:
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;
620 else
621 This->grfFlags |= PROPSETFLAG_ANSI;
623 else
624 hr = STG_E_INVALIDPARAMETER;
625 break;
626 case PID_LOCALE:
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;
631 else
632 hr = STG_E_INVALIDPARAMETER;
633 break;
634 case PID_ILLEGAL:
635 /* silently ignore like MSDN says */
636 break;
637 default:
638 if (rgpspec[i].u.propid >= PID_MIN_READONLY)
639 hr = STG_E_INVALIDPARAMETER;
640 else
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);
649 return hr;
652 /************************************************************************
653 * IPropertyStorage_fnDeleteMultiple (IPropertyStorage)
655 static HRESULT WINAPI IPropertyStorage_fnDeleteMultiple(
656 IPropertyStorage* iface,
657 ULONG cpspec,
658 const PROPSPEC rgpspec[])
660 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
661 ULONG i;
662 HRESULT hr;
664 TRACE("(%p, %ld, %p)\n", iface, cpspec, rgpspec);
665 if (!This)
666 return E_INVALIDARG;
667 if (cpspec && !rgpspec)
668 return E_INVALIDARG;
669 if (!(This->grfMode & STGM_READWRITE))
670 return STG_E_ACCESSDENIED;
671 hr = S_OK;
672 EnterCriticalSection(&This->cs);
673 This->dirty = TRUE;
674 for (i = 0; i < cpspec; i++)
676 if (rgpspec[i].ulKind == PRSPEC_LPWSTR)
678 PROPID propid;
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);
684 else
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);
690 else
691 hr = STG_E_INVALIDPARAMETER;
694 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
695 IPropertyStorage_Commit(iface, STGC_DEFAULT);
696 LeaveCriticalSection(&This->cs);
697 return hr;
700 /************************************************************************
701 * IPropertyStorage_fnReadPropertyNames (IPropertyStorage)
703 static HRESULT WINAPI IPropertyStorage_fnReadPropertyNames(
704 IPropertyStorage* iface,
705 ULONG cpropid,
706 const PROPID rgpropid[],
707 LPOLESTR rglpwstrName[])
709 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
710 ULONG i;
711 HRESULT hr = S_FALSE;
713 TRACE("(%p, %ld, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
714 if (!This)
715 return E_INVALIDARG;
716 if (cpropid && (!rgpropid || !rglpwstrName))
717 return E_INVALIDARG;
718 EnterCriticalSection(&This->cs);
719 for (i = 0; i < cpropid && SUCCEEDED(hr); i++)
721 LPWSTR name = PropertyStorage_FindPropertyNameById(This, rgpropid[i]);
723 if (name)
725 size_t len = lstrlenW(name);
727 hr = S_OK;
728 rglpwstrName[i] = CoTaskMemAlloc((len + 1) * sizeof(WCHAR));
729 if (rglpwstrName)
730 memcpy(rglpwstrName, name, (len + 1) * sizeof(WCHAR));
731 else
732 hr = STG_E_INSUFFICIENTMEMORY;
734 else
735 rglpwstrName[i] = NULL;
737 LeaveCriticalSection(&This->cs);
738 return hr;
741 /************************************************************************
742 * IPropertyStorage_fnWritePropertyNames (IPropertyStorage)
744 static HRESULT WINAPI IPropertyStorage_fnWritePropertyNames(
745 IPropertyStorage* iface,
746 ULONG cpropid,
747 const PROPID rgpropid[],
748 const LPOLESTR rglpwstrName[])
750 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
751 ULONG i;
752 HRESULT hr;
754 TRACE("(%p, %ld, %p, %p)\n", iface, cpropid, rgpropid, rglpwstrName);
755 if (!This)
756 return E_INVALIDARG;
757 if (cpropid && (!rgpropid || !rglpwstrName))
758 return E_INVALIDARG;
759 if (!(This->grfMode & STGM_READWRITE))
760 return STG_E_ACCESSDENIED;
761 hr = S_OK;
762 EnterCriticalSection(&This->cs);
763 This->dirty = TRUE;
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);
773 return hr;
776 /************************************************************************
777 * IPropertyStorage_fnDeletePropertyNames (IPropertyStorage)
779 static HRESULT WINAPI IPropertyStorage_fnDeletePropertyNames(
780 IPropertyStorage* iface,
781 ULONG cpropid,
782 const PROPID rgpropid[])
784 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
785 ULONG i;
786 HRESULT hr;
788 TRACE("(%p, %ld, %p)\n", iface, cpropid, rgpropid);
789 if (!This)
790 return E_INVALIDARG;
791 if (cpropid && !rgpropid)
792 return E_INVALIDARG;
793 if (!(This->grfMode & STGM_READWRITE))
794 return STG_E_ACCESSDENIED;
795 hr = S_OK;
796 EnterCriticalSection(&This->cs);
797 This->dirty = TRUE;
798 for (i = 0; i < cpropid; i++)
800 LPWSTR name = NULL;
802 if (dictionary_find(This->propid_to_name, (void *)rgpropid[i],
803 (void **)&name))
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);
812 return hr;
815 /************************************************************************
816 * IPropertyStorage_fnCommit (IPropertyStorage)
818 static HRESULT WINAPI IPropertyStorage_fnCommit(
819 IPropertyStorage* iface,
820 DWORD grfCommitFlags)
822 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
823 HRESULT hr;
825 TRACE("(%p, 0x%08lx)\n", iface, grfCommitFlags);
826 if (!This)
827 return E_INVALIDARG;
828 if (!(This->grfMode & STGM_READWRITE))
829 return STG_E_ACCESSDENIED;
830 EnterCriticalSection(&This->cs);
831 if (This->dirty)
832 hr = PropertyStorage_WriteToStream(This);
833 else
834 hr = S_OK;
835 LeaveCriticalSection(&This->cs);
836 return hr;
839 /************************************************************************
840 * IPropertyStorage_fnRevert (IPropertyStorage)
842 static HRESULT WINAPI IPropertyStorage_fnRevert(
843 IPropertyStorage* iface)
845 HRESULT hr;
846 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
848 TRACE("%p\n", iface);
849 if (!This)
850 return E_INVALIDARG;
852 EnterCriticalSection(&This->cs);
853 if (This->dirty)
855 PropertyStorage_DestroyDictionaries(This);
856 hr = PropertyStorage_CreateDictionaries(This);
857 if (SUCCEEDED(hr))
858 hr = PropertyStorage_ReadFromStream(This);
860 else
861 hr = S_OK;
862 LeaveCriticalSection(&This->cs);
863 return hr;
866 /************************************************************************
867 * IPropertyStorage_fnEnum (IPropertyStorage)
869 static HRESULT WINAPI IPropertyStorage_fnEnum(
870 IPropertyStorage* iface,
871 IEnumSTATPROPSTG** ppenum)
873 FIXME("\n");
874 return E_NOTIMPL;
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)
886 FIXME("\n");
887 return E_NOTIMPL;
890 /************************************************************************
891 * IPropertyStorage_fnSetClass (IPropertyStorage)
893 static HRESULT WINAPI IPropertyStorage_fnSetClass(
894 IPropertyStorage* iface,
895 REFCLSID clsid)
897 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
899 TRACE("%p, %s\n", iface, debugstr_guid(clsid));
900 if (!This || !clsid)
901 return E_INVALIDARG;
902 if (!(This->grfMode & STGM_READWRITE))
903 return STG_E_ACCESSDENIED;
904 memcpy(&This->clsid, clsid, sizeof(This->clsid));
905 This->dirty = TRUE;
906 if (This->grfFlags & PROPSETFLAG_UNBUFFERED)
907 IPropertyStorage_Commit(iface, STGC_DEFAULT);
908 return S_OK;
911 /************************************************************************
912 * IPropertyStorage_fnStat (IPropertyStorage)
914 static HRESULT WINAPI IPropertyStorage_fnStat(
915 IPropertyStorage* iface,
916 STATPROPSETSTG* statpsstg)
918 PropertyStorage_impl *This = (PropertyStorage_impl *)iface;
919 STATSTG stat;
920 HRESULT hr;
922 TRACE("%p, %p\n", iface, statpsstg);
923 if (!This || !statpsstg)
924 return E_INVALIDARG;
926 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
927 if (SUCCEEDED(hr))
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;
937 return hr;
940 static int PropertyStorage_PropNameCompare(const void *a, const void *b,
941 void *extra)
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);
950 else
951 return lstrcmpiW((LPCWSTR)a, (LPCWSTR)b);
953 else
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);
958 else
959 return lstrcmpiA((LPCSTR)a, (LPCSTR)b);
963 static void PropertyStorage_PropNameDestroy(void *k, void *d, void *extra)
965 CoTaskMemFree(k);
968 static int PropertyStorage_PropCompare(const void *a, const void *b,
969 void *extra)
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)
987 DWORD i;
989 /* Swap characters to host order.
990 * FIXME: alignment?
992 for (i = 0; i < len; i++)
993 str[i] = le16toh(str[i]);
995 #else
996 #define PropertyStorage_ByteSwapString(s, l)
997 #endif
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,
1005 BYTE *ptr)
1007 DWORD numEntries, i;
1008 HRESULT hr = S_OK;
1010 assert(This);
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++)
1019 PROPID propid;
1020 DWORD cbEntry;
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';
1030 else
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;
1041 return hr;
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)
1050 HRESULT hr = S_OK;
1052 assert(prop);
1053 assert(data);
1054 StorageUtl_ReadDWord(data, 0, (DWORD *)&prop->vt);
1055 data += sizeof(DWORD);
1056 switch (prop->vt)
1058 case VT_EMPTY:
1059 case VT_NULL:
1060 break;
1061 case VT_I1:
1062 prop->u.cVal = *(const char *)data;
1063 TRACE("Read char 0x%x ('%c')\n", prop->u.cVal, prop->u.cVal);
1064 break;
1065 case VT_UI1:
1066 prop->u.bVal = *(const UCHAR *)data;
1067 TRACE("Read byte 0x%x\n", prop->u.bVal);
1068 break;
1069 case VT_I2:
1070 StorageUtl_ReadWord(data, 0, (WORD*)&prop->u.iVal);
1071 TRACE("Read short %d\n", prop->u.iVal);
1072 break;
1073 case VT_UI2:
1074 StorageUtl_ReadWord(data, 0, &prop->u.uiVal);
1075 TRACE("Read ushort %d\n", prop->u.uiVal);
1076 break;
1077 case VT_INT:
1078 case VT_I4:
1079 StorageUtl_ReadDWord(data, 0, (DWORD*)&prop->u.lVal);
1080 TRACE("Read long %ld\n", prop->u.lVal);
1081 break;
1082 case VT_UINT:
1083 case VT_UI4:
1084 StorageUtl_ReadDWord(data, 0, &prop->u.ulVal);
1085 TRACE("Read ulong %ld\n", prop->u.ulVal);
1086 break;
1087 case VT_LPSTR:
1089 DWORD count;
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;
1097 else
1099 prop->u.pszVal = CoTaskMemAlloc(count);
1100 if (prop->u.pszVal)
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));
1113 else
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));
1120 else
1121 hr = STG_E_INSUFFICIENTMEMORY;
1123 break;
1125 case VT_LPWSTR:
1127 DWORD count;
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));
1140 else
1141 hr = STG_E_INSUFFICIENTMEMORY;
1142 break;
1144 case VT_FILETIME:
1145 StorageUtl_ReadULargeInteger(data, 0,
1146 (ULARGE_INTEGER *)&prop->u.filetime);
1147 break;
1148 case VT_CF:
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);
1164 else
1165 hr = STG_E_INVALIDPARAMETER;
1167 break;
1168 default:
1169 FIXME("unsupported type %d\n", prop->vt);
1170 hr = STG_E_INVALIDPARAMETER;
1172 return hr;
1175 static HRESULT PropertyStorage_ReadHeaderFromStream(IStream *stm,
1176 PROPERTYSETHEADER *hdr)
1178 BYTE buf[sizeof(PROPERTYSETHEADER)];
1179 ULONG count = 0;
1180 HRESULT hr;
1182 assert(stm);
1183 assert(hdr);
1184 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1185 if (SUCCEEDED(hr))
1187 if (count != sizeof(buf))
1189 WARN("read only %ld\n", count);
1190 hr = STG_E_INVALIDHEADER;
1192 else
1194 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wByteOrder),
1195 &hdr->wByteOrder);
1196 StorageUtl_ReadWord(buf, offsetof(PROPERTYSETHEADER, wFormat),
1197 &hdr->wFormat);
1198 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, dwOSVer),
1199 &hdr->dwOSVer);
1200 StorageUtl_ReadGUID(buf, offsetof(PROPERTYSETHEADER, clsid),
1201 &hdr->clsid);
1202 StorageUtl_ReadDWord(buf, offsetof(PROPERTYSETHEADER, reserved),
1203 &hdr->reserved);
1206 TRACE("returning 0x%08lx\n", hr);
1207 return hr;
1210 static HRESULT PropertyStorage_ReadFmtIdOffsetFromStream(IStream *stm,
1211 FORMATIDOFFSET *fmt)
1213 BYTE buf[sizeof(FORMATIDOFFSET)];
1214 ULONG count = 0;
1215 HRESULT hr;
1217 assert(stm);
1218 assert(fmt);
1219 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1220 if (SUCCEEDED(hr))
1222 if (count != sizeof(buf))
1224 WARN("read only %ld\n", count);
1225 hr = STG_E_INVALIDHEADER;
1227 else
1229 StorageUtl_ReadGUID(buf, offsetof(FORMATIDOFFSET, fmtid),
1230 &fmt->fmtid);
1231 StorageUtl_ReadDWord(buf, offsetof(FORMATIDOFFSET, dwOffset),
1232 &fmt->dwOffset);
1235 TRACE("returning 0x%08lx\n", hr);
1236 return hr;
1239 static HRESULT PropertyStorage_ReadSectionHeaderFromStream(IStream *stm,
1240 PROPERTYSECTIONHEADER *hdr)
1242 BYTE buf[sizeof(PROPERTYSECTIONHEADER)];
1243 ULONG count = 0;
1244 HRESULT hr;
1246 assert(stm);
1247 assert(hdr);
1248 hr = IStream_Read(stm, buf, sizeof(buf), &count);
1249 if (SUCCEEDED(hr))
1251 if (count != sizeof(buf))
1253 WARN("read only %ld\n", count);
1254 hr = STG_E_INVALIDHEADER;
1256 else
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);
1265 return hr;
1268 static HRESULT PropertyStorage_ReadFromStream(PropertyStorage_impl *This)
1270 PROPERTYSETHEADER hdr;
1271 FORMATIDOFFSET fmtOffset;
1272 PROPERTYSECTIONHEADER sectionHdr;
1273 LARGE_INTEGER seek;
1274 ULONG i;
1275 STATSTG stat;
1276 HRESULT hr;
1277 BYTE *buf = NULL;
1278 ULONG count = 0;
1279 DWORD dictOffset = 0;
1281 assert(This);
1282 This->dirty = FALSE;
1283 This->highestProp = 0;
1284 hr = IStream_Stat(This->stm, &stat, STATFLAG_NONAME);
1285 if (FAILED(hr))
1286 goto end;
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;
1292 goto end;
1294 if (stat.cbSize.u.LowPart == 0)
1296 /* empty stream is okay */
1297 hr = S_OK;
1298 goto end;
1300 else if (stat.cbSize.u.LowPart < sizeof(PROPERTYSETHEADER) +
1301 sizeof(FORMATIDOFFSET))
1303 WARN("stream too small\n");
1304 hr = STG_E_INVALIDHEADER;
1305 goto end;
1307 seek.QuadPart = 0;
1308 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1309 if (FAILED(hr))
1310 goto end;
1311 hr = PropertyStorage_ReadHeaderFromStream(This->stm, &hdr);
1312 /* I've only seen reserved == 1, but the article says I shouldn't disallow
1313 * higher values.
1315 if (hdr.wByteOrder != PROPSETHDR_BYTEORDER_MAGIC || hdr.reserved < 1)
1317 WARN("bad magic in prop set header\n");
1318 hr = STG_E_INVALIDHEADER;
1319 goto end;
1321 if (hdr.wFormat != 0 && hdr.wFormat != 1)
1323 WARN("bad format version %d\n", hdr.wFormat);
1324 hr = STG_E_INVALIDHEADER;
1325 goto end;
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);
1333 if (FAILED(hr))
1334 goto end;
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;
1340 goto end;
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
1346 * allowed.
1347 * Reading the second stream isn't implemented yet.
1349 hr = PropertyStorage_ReadSectionHeaderFromStream(This->stm, &sectionHdr);
1350 if (FAILED(hr))
1351 goto end;
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;
1357 goto end;
1359 buf = HeapAlloc(GetProcessHeap(), 0, sectionHdr.cbSection -
1360 sizeof(PROPERTYSECTIONHEADER));
1361 if (!buf)
1363 hr = STG_E_INSUFFICIENTMEMORY;
1364 goto end;
1366 hr = IStream_Read(This->stm, buf, sectionHdr.cbSection -
1367 sizeof(PROPERTYSECTIONHEADER), &count);
1368 if (FAILED(hr))
1369 goto end;
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;
1379 else
1381 if (idOffset->propid >= PID_FIRST_USABLE &&
1382 idOffset->propid < PID_MIN_READONLY && idOffset->propid >
1383 This->highestProp)
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
1389 * later.
1391 dictOffset = idOffset->dwOffset;
1392 TRACE("Dictionary offset is %ld\n", dictOffset);
1394 else
1396 PROPVARIANT prop;
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)
1405 case PID_CODEPAGE:
1406 if (prop.vt == VT_I2)
1407 This->codePage = (UINT)prop.u.iVal;
1408 break;
1409 case PID_LOCALE:
1410 if (prop.vt == VT_I4)
1411 This->locale = (LCID)prop.u.lVal;
1412 break;
1413 case PID_BEHAVIOR:
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 */
1417 This->format = 1;
1418 break;
1419 default:
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();
1434 else
1435 This->codePage = CP_UNICODE;
1437 if (!This->locale)
1438 This->locale = LOCALE_SYSTEM_DEFAULT;
1439 TRACE("Code page is %d, locale is %ld\n", This->codePage, This->locale);
1440 if (dictOffset)
1441 hr = PropertyStorage_ReadDictionary(This,
1442 buf + dictOffset - sizeof(PROPERTYSECTIONHEADER));
1444 end:
1445 HeapFree(GetProcessHeap(), 0, buf);
1446 if (FAILED(hr))
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;
1455 return hr;
1458 static void PropertyStorage_MakeHeader(PropertyStorage_impl *This,
1459 PROPERTYSETHEADER *hdr)
1461 assert(This);
1462 assert(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)
1474 assert(This);
1475 assert(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)
1484 assert(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
1501 HRESULT hr;
1502 DWORD bytesWritten;
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;
1510 DWORD propid;
1511 ULONG count;
1513 assert(key);
1514 assert(This);
1515 assert(closure);
1516 StorageUtl_WriteDWord((LPBYTE)&propid, 0, (DWORD)value);
1517 c->hr = IStream_Write(This->stm, &propid, sizeof(propid), &count);
1518 if (FAILED(c->hr))
1519 goto end;
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);
1528 if (FAILED(c->hr))
1529 goto end;
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);
1537 if (FAILED(c->hr))
1538 goto end;
1539 c->bytesWritten += keyLen;
1540 if (keyLen % sizeof(DWORD))
1542 c->hr = IStream_Write(This->stm, &pad,
1543 sizeof(DWORD) - keyLen % sizeof(DWORD), &count);
1544 if (FAILED(c->hr))
1545 goto end;
1546 c->bytesWritten += sizeof(DWORD) - keyLen % sizeof(DWORD);
1549 else
1551 DWORD keyLen;
1553 StorageUtl_WriteDWord((LPBYTE)&keyLen, 0, strlen((LPCSTR)key) + 1);
1554 c->hr = IStream_Write(This->stm, &keyLen, sizeof(keyLen), &count);
1555 if (FAILED(c->hr))
1556 goto end;
1557 c->bytesWritten += sizeof(DWORD);
1558 c->hr = IStream_Write(This->stm, key, keyLen, &count);
1559 if (FAILED(c->hr))
1560 goto end;
1561 c->bytesWritten += keyLen;
1563 end:
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)
1575 HRESULT hr;
1576 LARGE_INTEGER seek;
1577 PROPERTYIDOFFSET propIdOffset;
1578 ULONG count;
1579 DWORD dwTemp;
1580 struct DictionaryClosure closure;
1582 assert(This);
1583 assert(sectionOffset);
1585 /* The dictionary's always the first property written, so seek to its
1586 * spot.
1588 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER);
1589 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1590 if (FAILED(hr))
1591 goto end;
1592 PropertyStorage_MakePropertyIdOffset(PID_DICTIONARY, *sectionOffset,
1593 &propIdOffset);
1594 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1595 if (FAILED(hr))
1596 goto end;
1598 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1599 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1600 if (FAILED(hr))
1601 goto end;
1602 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0,
1603 dictionary_num_entries(This->name_to_propid));
1604 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1605 if (FAILED(hr))
1606 goto end;
1607 *sectionOffset += sizeof(dwTemp);
1609 closure.hr = S_OK;
1610 closure.bytesWritten = 0;
1611 dictionary_enumerate(This->name_to_propid, PropertyStorage_DictionaryWriter,
1612 &closure);
1613 hr = closure.hr;
1614 if (FAILED(hr))
1615 goto end;
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);
1624 end:
1625 return hr;
1628 static HRESULT PropertyStorage_WritePropertyToStream(PropertyStorage_impl *This,
1629 DWORD propNum, DWORD propid, PROPVARIANT *var, DWORD *sectionOffset)
1631 HRESULT hr;
1632 LARGE_INTEGER seek;
1633 PROPERTYIDOFFSET propIdOffset;
1634 ULONG count;
1635 DWORD dwType, bytesWritten;
1637 assert(This);
1638 assert(var);
1639 assert(sectionOffset);
1641 TRACE("%p, %ld, 0x%08lx, (%d), (%ld)\n", This, propNum, propid, var->vt,
1642 *sectionOffset);
1644 seek.QuadPart = SECTIONHEADER_OFFSET + sizeof(PROPERTYSECTIONHEADER) +
1645 propNum * sizeof(PROPERTYIDOFFSET);
1646 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1647 if (FAILED(hr))
1648 goto end;
1649 PropertyStorage_MakePropertyIdOffset(propid, *sectionOffset, &propIdOffset);
1650 hr = IStream_Write(This->stm, &propIdOffset, sizeof(propIdOffset), &count);
1651 if (FAILED(hr))
1652 goto end;
1654 seek.QuadPart = SECTIONHEADER_OFFSET + *sectionOffset;
1655 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1656 if (FAILED(hr))
1657 goto end;
1658 StorageUtl_WriteDWord((LPBYTE)&dwType, 0, var->vt);
1659 hr = IStream_Write(This->stm, &dwType, sizeof(dwType), &count);
1660 if (FAILED(hr))
1661 goto end;
1662 *sectionOffset += sizeof(dwType);
1664 switch (var->vt)
1666 case VT_EMPTY:
1667 case VT_NULL:
1668 bytesWritten = 0;
1669 break;
1670 case VT_I1:
1671 case VT_UI1:
1672 hr = IStream_Write(This->stm, &var->u.cVal, sizeof(var->u.cVal),
1673 &count);
1674 bytesWritten = count;
1675 break;
1676 case VT_I2:
1677 case VT_UI2:
1679 WORD wTemp;
1681 StorageUtl_WriteWord((LPBYTE)&wTemp, 0, var->u.iVal);
1682 hr = IStream_Write(This->stm, &wTemp, sizeof(wTemp), &count);
1683 bytesWritten = count;
1684 break;
1686 case VT_I4:
1687 case VT_UI4:
1689 DWORD dwTemp;
1691 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, var->u.lVal);
1692 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1693 bytesWritten = count;
1694 break;
1696 case VT_LPSTR:
1698 DWORD len, dwTemp;
1700 if (This->codePage == CP_UNICODE)
1701 len = (lstrlenW(var->u.pwszVal) + 1) * sizeof(WCHAR);
1702 else
1703 len = lstrlenA(var->u.pszVal) + 1;
1704 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, len);
1705 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1706 if (FAILED(hr))
1707 goto end;
1708 hr = IStream_Write(This->stm, var->u.pszVal, len, &count);
1709 bytesWritten = count + sizeof(DWORD);
1710 break;
1712 case VT_LPWSTR:
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);
1718 if (FAILED(hr))
1719 goto end;
1720 hr = IStream_Write(This->stm, var->u.pwszVal, len * sizeof(WCHAR),
1721 &count);
1722 bytesWritten = count + sizeof(DWORD);
1723 break;
1725 case VT_FILETIME:
1727 FILETIME temp;
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;
1733 break;
1735 default:
1736 FIXME("unsupported type: %d\n", var->vt);
1737 return STG_E_INVALIDPARAMETER;
1740 if (SUCCEEDED(hr))
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);
1751 end:
1752 return hr;
1755 struct PropertyClosure
1757 HRESULT hr;
1758 DWORD propNum;
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;
1768 assert(key);
1769 assert(value);
1770 assert(extra);
1771 assert(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;
1782 assert(This);
1783 assert(sectionOffset);
1784 closure.hr = S_OK;
1785 closure.propNum = startingPropNum;
1786 closure.sectionOffset = sectionOffset;
1787 dictionary_enumerate(This->propid_to_prop, PropertyStorage_PropertiesWriter,
1788 &closure);
1789 return closure.hr;
1792 static HRESULT PropertyStorage_WriteHeadersToStream(PropertyStorage_impl *This)
1794 HRESULT hr;
1795 ULONG count = 0;
1796 LARGE_INTEGER seek = { {0} };
1797 PROPERTYSETHEADER hdr;
1798 FORMATIDOFFSET fmtOffset;
1800 assert(This);
1801 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1802 if (FAILED(hr))
1803 goto end;
1804 PropertyStorage_MakeHeader(This, &hdr);
1805 hr = IStream_Write(This->stm, &hdr, sizeof(hdr), &count);
1806 if (FAILED(hr))
1807 goto end;
1808 if (count != sizeof(hdr))
1810 hr = STG_E_WRITEFAULT;
1811 goto end;
1814 PropertyStorage_MakeFmtIdOffset(This, &fmtOffset);
1815 hr = IStream_Write(This->stm, &fmtOffset, sizeof(fmtOffset), &count);
1816 if (FAILED(hr))
1817 goto end;
1818 if (count != sizeof(fmtOffset))
1820 hr = STG_E_WRITEFAULT;
1821 goto end;
1823 hr = S_OK;
1825 end:
1826 return hr;
1829 static HRESULT PropertyStorage_WriteToStream(PropertyStorage_impl *This)
1831 PROPERTYSECTIONHEADER sectionHdr;
1832 HRESULT hr;
1833 ULONG count;
1834 LARGE_INTEGER seek;
1835 DWORD numProps, prop, sectionOffset, dwTemp;
1836 PROPVARIANT var;
1838 assert(This);
1840 PropertyStorage_WriteHeadersToStream(This);
1842 /* Count properties. Always at least one property, the code page */
1843 numProps = 1;
1844 if (dictionary_num_entries(This->name_to_propid))
1845 numProps++;
1846 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1847 numProps++;
1848 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1849 numProps++;
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, &sectionHdr);
1856 seek.QuadPart = SECTIONHEADER_OFFSET;
1857 hr = IStream_Seek(This->stm, seek, STREAM_SEEK_SET, NULL);
1858 if (FAILED(hr))
1859 goto end;
1860 hr = IStream_Write(This->stm, &sectionHdr, sizeof(sectionHdr), &count);
1861 if (FAILED(hr))
1862 goto end;
1864 prop = 0;
1865 sectionOffset = sizeof(PROPERTYSECTIONHEADER) +
1866 numProps * sizeof(PROPERTYIDOFFSET);
1868 if (dictionary_num_entries(This->name_to_propid))
1870 prop++;
1871 hr = PropertyStorage_WriteDictionaryToStream(This, &sectionOffset);
1872 if (FAILED(hr))
1873 goto end;
1876 PropVariantInit(&var);
1878 var.vt = VT_I2;
1879 var.u.iVal = This->codePage;
1880 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_CODEPAGE,
1881 &var, &sectionOffset);
1882 if (FAILED(hr))
1883 goto end;
1885 if (This->locale != LOCALE_SYSTEM_DEFAULT)
1887 var.vt = VT_I4;
1888 var.u.lVal = This->locale;
1889 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_LOCALE,
1890 &var, &sectionOffset);
1891 if (FAILED(hr))
1892 goto end;
1895 if (This->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
1897 var.vt = VT_I4;
1898 var.u.lVal = 1;
1899 hr = PropertyStorage_WritePropertyToStream(This, prop++, PID_BEHAVIOR,
1900 &var, &sectionOffset);
1901 if (FAILED(hr))
1902 goto end;
1905 hr = PropertyStorage_WritePropertiesToStream(This, prop, &sectionOffset);
1906 if (FAILED(hr))
1907 goto end;
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);
1912 if (FAILED(hr))
1913 goto end;
1914 StorageUtl_WriteDWord((LPBYTE)&dwTemp, 0, sectionOffset);
1915 hr = IStream_Write(This->stm, &dwTemp, sizeof(dwTemp), &count);
1917 end:
1918 return hr;
1921 /***********************************************************************
1922 * PropertyStorage_Construct
1924 static void PropertyStorage_DestroyDictionaries(PropertyStorage_impl *This)
1926 assert(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)
1937 HRESULT hr = S_OK;
1939 assert(This);
1940 This->name_to_propid = dictionary_create(
1941 PropertyStorage_PropNameCompare, PropertyStorage_PropNameDestroy,
1942 This);
1943 if (!This->name_to_propid)
1945 hr = STG_E_INSUFFICIENTMEMORY;
1946 goto end;
1948 This->propid_to_name = dictionary_create(PropertyStorage_PropCompare,
1949 NULL, This);
1950 if (!This->propid_to_name)
1952 hr = STG_E_INSUFFICIENTMEMORY;
1953 goto end;
1955 This->propid_to_prop = dictionary_create(PropertyStorage_PropCompare,
1956 PropertyStorage_PropertyDestroy, This);
1957 if (!This->propid_to_prop)
1959 hr = STG_E_INSUFFICIENTMEMORY;
1960 goto end;
1962 end:
1963 if (FAILED(hr))
1964 PropertyStorage_DestroyDictionaries(This);
1965 return hr;
1968 static HRESULT PropertyStorage_BaseConstruct(IStream *stm,
1969 REFFMTID rfmtid, DWORD grfMode, PropertyStorage_impl **pps)
1971 HRESULT hr = S_OK;
1973 assert(pps);
1974 assert(rfmtid);
1975 *pps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof **pps);
1976 if (!*pps)
1977 return E_OUTOFMEMORY;
1979 (*pps)->vtbl = &IPropertyStorage_Vtbl;
1980 (*pps)->ref = 1;
1981 InitializeCriticalSection(&(*pps)->cs);
1982 (*pps)->stm = stm;
1983 memcpy(&(*pps)->fmtid, rfmtid, sizeof((*pps)->fmtid));
1984 (*pps)->grfMode = grfMode;
1986 hr = PropertyStorage_CreateDictionaries(*pps);
1987 if (FAILED(hr))
1989 IStream_Release(stm);
1990 DeleteCriticalSection(&(*pps)->cs);
1991 HeapFree(GetProcessHeap(), 0, *pps);
1992 *pps = NULL;
1995 return hr;
1998 static HRESULT PropertyStorage_ConstructFromStream(IStream *stm,
1999 REFFMTID rfmtid, DWORD grfMode, IPropertyStorage** pps)
2001 PropertyStorage_impl *ps;
2002 HRESULT hr;
2004 assert(pps);
2005 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2006 if (SUCCEEDED(hr))
2008 hr = PropertyStorage_ReadFromStream(ps);
2009 if (SUCCEEDED(hr))
2011 *pps = (IPropertyStorage *)ps;
2012 TRACE("PropertyStorage %p constructed\n", ps);
2013 hr = S_OK;
2015 else
2017 PropertyStorage_DestroyDictionaries(ps);
2018 HeapFree(GetProcessHeap(), 0, ps);
2021 return hr;
2024 static HRESULT PropertyStorage_ConstructEmpty(IStream *stm,
2025 REFFMTID rfmtid, DWORD grfFlags, DWORD grfMode, IPropertyStorage** pps)
2027 PropertyStorage_impl *ps;
2028 HRESULT hr;
2030 assert(pps);
2031 hr = PropertyStorage_BaseConstruct(stm, rfmtid, grfMode, &ps);
2032 if (SUCCEEDED(hr))
2034 ps->format = 0;
2035 ps->grfFlags = grfFlags;
2036 if (ps->grfFlags & PROPSETFLAG_CASE_SENSITIVE)
2037 ps->format = 1;
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();
2043 else
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);
2049 hr = S_OK;
2051 return hr;
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,
2066 REFIID riid,
2067 void** ppvObject)
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,
2102 REFFMTID rfmtid,
2103 const CLSID* pclsid,
2104 DWORD grfFlags,
2105 DWORD grfMode,
2106 IPropertyStorage** ppprstg)
2108 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2109 WCHAR name[CCH_MAX_PROPSTG_NAME];
2110 IStream *stm = NULL;
2111 HRESULT r;
2113 TRACE("%p %s %08lx %08lx %p\n", This, debugstr_guid(rfmtid), grfFlags,
2114 grfMode, ppprstg);
2116 /* be picky */
2117 if (grfMode != (STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE))
2119 r = STG_E_INVALIDFLAG;
2120 goto end;
2123 if (!rfmtid)
2125 r = E_INVALIDARG;
2126 goto end;
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;
2136 goto end;
2139 r = FmtIdToPropStgName(rfmtid, name);
2140 if (FAILED(r))
2141 goto end;
2143 r = IStorage_CreateStream( (IStorage*)This, name, grfMode, 0, 0, &stm );
2144 if (FAILED(r))
2145 goto end;
2147 r = PropertyStorage_ConstructEmpty(stm, rfmtid, grfFlags, grfMode, ppprstg);
2149 end:
2150 TRACE("returning 0x%08lx\n", r);
2151 return r;
2154 /************************************************************************
2155 * IPropertySetStorage_fnOpen (IPropertySetStorage)
2157 static HRESULT WINAPI IPropertySetStorage_fnOpen(
2158 IPropertySetStorage *ppstg,
2159 REFFMTID rfmtid,
2160 DWORD grfMode,
2161 IPropertyStorage** ppprstg)
2163 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2164 IStream *stm = NULL;
2165 WCHAR name[CCH_MAX_PROPSTG_NAME];
2166 HRESULT r;
2168 TRACE("%p %s %08lx %p\n", This, debugstr_guid(rfmtid), grfMode, ppprstg);
2170 /* be picky */
2171 if (grfMode != (STGM_READWRITE|STGM_SHARE_EXCLUSIVE) &&
2172 grfMode != (STGM_READ|STGM_SHARE_EXCLUSIVE))
2174 r = STG_E_INVALIDFLAG;
2175 goto end;
2178 if (!rfmtid)
2180 r = E_INVALIDARG;
2181 goto end;
2184 r = FmtIdToPropStgName(rfmtid, name);
2185 if (FAILED(r))
2186 goto end;
2188 r = IStorage_OpenStream((IStorage*) This, name, 0, grfMode, 0, &stm );
2189 if (FAILED(r))
2190 goto end;
2192 r = PropertyStorage_ConstructFromStream(stm, rfmtid, grfMode, ppprstg);
2194 end:
2195 TRACE("returning 0x%08lx\n", r);
2196 return r;
2199 /************************************************************************
2200 * IPropertySetStorage_fnDelete (IPropertySetStorage)
2202 static HRESULT WINAPI IPropertySetStorage_fnDelete(
2203 IPropertySetStorage *ppstg,
2204 REFFMTID rfmtid)
2206 StorageImpl *This = impl_from_IPropertySetStorage(ppstg);
2207 IStorage *stg = NULL;
2208 WCHAR name[CCH_MAX_PROPSTG_NAME];
2209 HRESULT r;
2211 TRACE("%p %s\n", This, debugstr_guid(rfmtid));
2213 if (!rfmtid)
2214 return E_INVALIDARG;
2216 r = FmtIdToPropStgName(rfmtid, name);
2217 if (FAILED(r))
2218 return r;
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);
2233 return E_NOTIMPL;
2237 /***********************************************************************
2238 * vtables
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.
2286 * PARAMS
2287 * rfmtid [I] Format ID for which to return a storage name
2288 * str [O] Storage name associated with rfmtid.
2290 * RETURNS
2291 * E_INVALIDARG if rfmtid or str i NULL, S_OK otherwise.
2293 * NOTES
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);
2313 else
2315 BYTE *fmtptr;
2316 WCHAR *pstr = str;
2317 ULONG bitsRemaining = BITS_PER_BYTE;
2319 *pstr++ = 5;
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' &&
2328 *pstr <= 'z')
2329 *pstr += 'A' - 'a';
2330 pstr++;
2331 bitsRemaining -= BITS_IN_CHARMASK;
2332 if (bitsRemaining == 0)
2334 fmtptr++;
2335 bitsRemaining = BITS_PER_BYTE;
2338 else
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;
2346 *pstr = 0;
2348 TRACE("returning %s\n", debugstr_w(str));
2349 return S_OK;
2352 /***********************************************************************
2353 * PropStgNameToFmtId [ole32.@]
2354 * Returns the format ID corresponding to the given name.
2355 * PARAMS
2356 * str [I] Storage name to convert to a format ID.
2357 * rfmtid [O] Format ID corresponding to str.
2359 * RETURNS
2360 * E_INVALIDARG if rfmtid or str is NULL or if str can't be converted to
2361 * a format ID, S_OK otherwise.
2363 * NOTES
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));
2379 hr = S_OK;
2381 else if (!lstrcmpiW(str, szSummaryInfo))
2383 memcpy(rfmtid, &FMTID_SummaryInformation, sizeof(*rfmtid));
2384 hr = S_OK;
2386 else
2388 ULONG bits;
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;
2397 WCHAR wc;
2399 if (bitsUsed == 0)
2400 fmtptr++;
2401 wc = *++pstr - 'A';
2402 if (wc > NUM_ALPHA_CHARS)
2404 wc += 'A' - 'a';
2405 if (wc > NUM_ALPHA_CHARS)
2407 wc += 'a' - '0' + NUM_ALPHA_CHARS;
2408 if (wc > CHARMASK)
2410 WARN("invalid character (%d)\n", *pstr);
2411 goto end;
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)
2422 if (wc != 0)
2424 WARN("extra bits\n");
2425 goto end;
2427 break;
2429 fmtptr++;
2430 *fmtptr |= (BYTE)wc;
2433 hr = S_OK;
2435 end:
2436 return hr;