Release 1.6-rc2.
[wine/testsucceed.git] / dlls / windowscodecs / metadatahandler.c
blob7615a6dfa231de6e038733d137244b659695d355
1 /*
2 * Copyright 2012 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
22 #include <stdarg.h>
23 #include <stdio.h>
25 #define COBJMACROS
26 #define NONAMELESSUNION
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "objbase.h"
32 #include "wincodec.h"
33 #include "wincodecsdk.h"
35 #include "wincodecs_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
41 typedef struct MetadataHandler {
42 IWICMetadataWriter IWICMetadataWriter_iface;
43 LONG ref;
44 IWICPersistStream IWICPersistStream_iface;
45 const MetadataHandlerVtbl *vtable;
46 MetadataItem *items;
47 DWORD item_count;
48 CRITICAL_SECTION lock;
49 } MetadataHandler;
51 static inline MetadataHandler *impl_from_IWICMetadataWriter(IWICMetadataWriter *iface)
53 return CONTAINING_RECORD(iface, MetadataHandler, IWICMetadataWriter_iface);
56 static inline MetadataHandler *impl_from_IWICPersistStream(IWICPersistStream *iface)
58 return CONTAINING_RECORD(iface, MetadataHandler, IWICPersistStream_iface);
61 static void MetadataHandler_FreeItems(MetadataHandler *This)
63 DWORD i;
65 for (i=0; i<This->item_count; i++)
67 PropVariantClear(&This->items[i].schema);
68 PropVariantClear(&This->items[i].id);
69 PropVariantClear(&This->items[i].value);
72 HeapFree(GetProcessHeap(), 0, This->items);
75 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
76 IWICEnumMetadataItem **ppIEnumMetadataItem);
78 static HRESULT WINAPI MetadataHandler_QueryInterface(IWICMetadataWriter *iface, REFIID iid,
79 void **ppv)
81 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
82 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
84 if (!ppv) return E_INVALIDARG;
86 if (IsEqualIID(&IID_IUnknown, iid) ||
87 IsEqualIID(&IID_IWICMetadataReader, iid) ||
88 (IsEqualIID(&IID_IWICMetadataWriter, iid) && This->vtable->is_writer))
90 *ppv = &This->IWICMetadataWriter_iface;
92 else if (IsEqualIID(&IID_IPersist, iid) ||
93 IsEqualIID(&IID_IPersistStream, iid) ||
94 IsEqualIID(&IID_IWICPersistStream, iid))
96 *ppv = &This->IWICPersistStream_iface;
98 else
100 *ppv = NULL;
101 return E_NOINTERFACE;
104 IUnknown_AddRef((IUnknown*)*ppv);
105 return S_OK;
108 static ULONG WINAPI MetadataHandler_AddRef(IWICMetadataWriter *iface)
110 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
111 ULONG ref = InterlockedIncrement(&This->ref);
113 TRACE("(%p) refcount=%u\n", iface, ref);
115 return ref;
118 static ULONG WINAPI MetadataHandler_Release(IWICMetadataWriter *iface)
120 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
121 ULONG ref = InterlockedDecrement(&This->ref);
123 TRACE("(%p) refcount=%u\n", iface, ref);
125 if (ref == 0)
127 MetadataHandler_FreeItems(This);
128 This->lock.DebugInfo->Spare[0] = 0;
129 DeleteCriticalSection(&This->lock);
130 HeapFree(GetProcessHeap(), 0, This);
133 return ref;
136 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
137 IWICMetadataHandlerInfo **ppIHandler)
139 HRESULT hr;
140 IWICComponentInfo *component_info;
141 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
143 TRACE("%p,%p\n", iface, ppIHandler);
145 hr = CreateComponentInfo(This->vtable->clsid, &component_info);
146 if (FAILED(hr)) return hr;
148 hr = IWICComponentInfo_QueryInterface(component_info, &IID_IWICMetadataHandlerInfo,
149 (void **)ppIHandler);
151 IWICComponentInfo_Release(component_info);
152 return hr;
155 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
156 GUID *pguidMetadataFormat)
158 HRESULT hr;
159 IWICMetadataHandlerInfo *metadata_info;
161 TRACE("%p,%p\n", iface, pguidMetadataFormat);
163 if (!pguidMetadataFormat) return E_INVALIDARG;
165 hr = MetadataHandler_GetMetadataHandlerInfo(iface, &metadata_info);
166 if (FAILED(hr)) return hr;
168 hr = IWICMetadataHandlerInfo_GetMetadataFormat(metadata_info, pguidMetadataFormat);
169 IWICMetadataHandlerInfo_Release(metadata_info);
171 return hr;
174 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
175 UINT *pcCount)
177 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
179 TRACE("%p,%p\n", iface, pcCount);
181 if (!pcCount) return E_INVALIDARG;
183 *pcCount = This->item_count;
184 return S_OK;
187 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
188 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
190 HRESULT hr = S_OK;
191 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
193 TRACE("%p,%u,%p,%p,%p\n", iface, index, schema, id, value);
195 EnterCriticalSection(&This->lock);
197 if (index >= This->item_count)
199 LeaveCriticalSection(&This->lock);
200 return E_INVALIDARG;
203 if (schema)
204 hr = PropVariantCopy(schema, &This->items[index].schema);
206 if (SUCCEEDED(hr) && id)
207 hr = PropVariantCopy(id, &This->items[index].id);
209 if (SUCCEEDED(hr) && value)
210 hr = PropVariantCopy(value, &This->items[index].value);
212 LeaveCriticalSection(&This->lock);
213 return hr;
216 static BOOL get_int_value(const PROPVARIANT *pv, LONGLONG *value)
218 switch (pv->vt)
220 case VT_NULL:
221 case VT_EMPTY:
222 *value = 0;
223 break;
224 case VT_I1:
225 *value = pv->u.cVal;
226 break;
227 case VT_UI1:
228 *value = pv->u.bVal;
229 break;
230 case VT_I2:
231 *value = pv->u.iVal;
232 break;
233 case VT_UI2:
234 *value = pv->u.uiVal;
235 break;
236 case VT_I4:
237 *value = pv->u.lVal;
238 break;
239 case VT_UI4:
240 *value = pv->u.ulVal;
241 break;
242 case VT_I8:
243 case VT_UI8:
244 *value = pv->u.hVal.QuadPart;
245 break;
246 default:
247 FIXME("not supported variant type %d\n", pv->vt);
248 return FALSE;
250 return TRUE;
253 /* FiXME: Use propsys.PropVariantCompareEx once it's implemented */
254 static int propvar_cmp(const PROPVARIANT *v1, const PROPVARIANT *v2)
256 LONGLONG value1, value2;
258 if (v1->vt == VT_LPSTR && v2->vt == VT_LPSTR)
260 return lstrcmpA(v1->u.pszVal, v2->u.pszVal);
263 if (v1->vt == VT_LPWSTR && v2->vt == VT_LPWSTR)
265 return lstrcmpiW(v1->u.pwszVal, v2->u.pwszVal);
268 if (!get_int_value(v1, &value1)) return -1;
269 if (!get_int_value(v2, &value2)) return -1;
271 value1 -= value2;
272 if (value1) return value1 < 0 ? -1 : 1;
273 return 0;
276 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
277 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
279 UINT i;
280 HRESULT hr = WINCODEC_ERR_PROPERTYNOTFOUND;
281 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
283 TRACE("(%p,%p,%p,%p)\n", iface, schema, id, value);
285 if (!id) return E_INVALIDARG;
287 EnterCriticalSection(&This->lock);
289 for (i = 0; i < This->item_count; i++)
291 if (schema && This->items[i].schema.vt != VT_EMPTY)
293 if (propvar_cmp(schema, &This->items[i].schema) != 0) continue;
296 if (propvar_cmp(id, &This->items[i].id) != 0) continue;
298 hr = value ? PropVariantCopy(value, &This->items[i].value) : S_OK;
299 break;
302 LeaveCriticalSection(&This->lock);
303 return hr;
306 static HRESULT WINAPI MetadataHandler_GetEnumerator(IWICMetadataWriter *iface,
307 IWICEnumMetadataItem **ppIEnumMetadata)
309 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
310 TRACE("(%p,%p)\n", iface, ppIEnumMetadata);
311 return MetadataHandlerEnum_Create(This, 0, ppIEnumMetadata);
314 static HRESULT WINAPI MetadataHandler_SetValue(IWICMetadataWriter *iface,
315 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
317 FIXME("(%p,%p,%p,%p): stub\n", iface, pvarSchema, pvarId, pvarValue);
318 return E_NOTIMPL;
321 static HRESULT WINAPI MetadataHandler_SetValueByIndex(IWICMetadataWriter *iface,
322 UINT nIndex, const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
324 FIXME("(%p,%u,%p,%p,%p): stub\n", iface, nIndex, pvarSchema, pvarId, pvarValue);
325 return E_NOTIMPL;
328 static HRESULT WINAPI MetadataHandler_RemoveValue(IWICMetadataWriter *iface,
329 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId)
331 FIXME("(%p,%p,%p): stub\n", iface, pvarSchema, pvarId);
332 return E_NOTIMPL;
335 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
336 UINT nIndex)
338 FIXME("(%p,%u): stub\n", iface, nIndex);
339 return E_NOTIMPL;
342 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl = {
343 MetadataHandler_QueryInterface,
344 MetadataHandler_AddRef,
345 MetadataHandler_Release,
346 MetadataHandler_GetMetadataFormat,
347 MetadataHandler_GetMetadataHandlerInfo,
348 MetadataHandler_GetCount,
349 MetadataHandler_GetValueByIndex,
350 MetadataHandler_GetValue,
351 MetadataHandler_GetEnumerator,
352 MetadataHandler_SetValue,
353 MetadataHandler_SetValueByIndex,
354 MetadataHandler_RemoveValue,
355 MetadataHandler_RemoveValueByIndex
358 static HRESULT WINAPI MetadataHandler_PersistStream_QueryInterface(IWICPersistStream *iface,
359 REFIID iid, void **ppv)
361 MetadataHandler *This = impl_from_IWICPersistStream(iface);
362 return IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
365 static ULONG WINAPI MetadataHandler_PersistStream_AddRef(IWICPersistStream *iface)
367 MetadataHandler *This = impl_from_IWICPersistStream(iface);
368 return IWICMetadataWriter_AddRef(&This->IWICMetadataWriter_iface);
371 static ULONG WINAPI MetadataHandler_PersistStream_Release(IWICPersistStream *iface)
373 MetadataHandler *This = impl_from_IWICPersistStream(iface);
374 return IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
377 static HRESULT WINAPI MetadataHandler_GetClassID(IWICPersistStream *iface,
378 CLSID *pClassID)
380 FIXME("(%p,%p): stub\n", iface, pClassID);
381 return E_NOTIMPL;
384 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
386 FIXME("(%p): stub\n", iface);
387 return E_NOTIMPL;
390 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
391 IStream *pStm)
393 MetadataHandler *This = impl_from_IWICPersistStream(iface);
394 TRACE("(%p,%p)\n", iface, pStm);
395 return IWICPersistStream_LoadEx(&This->IWICPersistStream_iface, pStm, NULL, WICPersistOptionsDefault);
398 static HRESULT WINAPI MetadataHandler_Save(IWICPersistStream *iface,
399 IStream *pStm, BOOL fClearDirty)
401 FIXME("(%p,%p,%i): stub\n", iface, pStm, fClearDirty);
402 return E_NOTIMPL;
405 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
406 ULARGE_INTEGER *pcbSize)
408 FIXME("(%p,%p): stub\n", iface, pcbSize);
409 return E_NOTIMPL;
412 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
413 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
415 MetadataHandler *This = impl_from_IWICPersistStream(iface);
416 HRESULT hr;
417 MetadataItem *new_items=NULL;
418 DWORD item_count=0;
420 TRACE("(%p,%p,%s,%x)\n", iface, pIStream, debugstr_guid(pguidPreferredVendor), dwPersistOptions);
422 EnterCriticalSection(&This->lock);
424 hr = This->vtable->fnLoad(pIStream, pguidPreferredVendor, dwPersistOptions,
425 &new_items, &item_count);
427 if (SUCCEEDED(hr))
429 MetadataHandler_FreeItems(This);
430 This->items = new_items;
431 This->item_count = item_count;
434 LeaveCriticalSection(&This->lock);
436 return hr;
439 static HRESULT WINAPI MetadataHandler_SaveEx(IWICPersistStream *iface,
440 IStream *pIStream, DWORD dwPersistOptions, BOOL fClearDirty)
442 FIXME("(%p,%p,%x,%i): stub\n", iface, pIStream, dwPersistOptions, fClearDirty);
443 return E_NOTIMPL;
446 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl = {
447 MetadataHandler_PersistStream_QueryInterface,
448 MetadataHandler_PersistStream_AddRef,
449 MetadataHandler_PersistStream_Release,
450 MetadataHandler_GetClassID,
451 MetadataHandler_IsDirty,
452 MetadataHandler_Load,
453 MetadataHandler_Save,
454 MetadataHandler_GetSizeMax,
455 MetadataHandler_LoadEx,
456 MetadataHandler_SaveEx
459 HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, IUnknown *pUnkOuter, REFIID iid, void** ppv)
461 MetadataHandler *This;
462 HRESULT hr;
464 TRACE("%s\n", debugstr_guid(vtable->clsid));
466 *ppv = NULL;
468 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
470 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
471 if (!This) return E_OUTOFMEMORY;
473 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
474 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
475 This->ref = 1;
476 This->vtable = vtable;
477 This->items = NULL;
478 This->item_count = 0;
480 InitializeCriticalSection(&This->lock);
481 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
483 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
485 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
487 return hr;
490 typedef struct MetadataHandlerEnum {
491 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
492 LONG ref;
493 MetadataHandler *parent;
494 DWORD index;
495 } MetadataHandlerEnum;
497 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
499 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
502 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
503 void **ppv)
505 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
506 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
508 if (!ppv) return E_INVALIDARG;
510 if (IsEqualIID(&IID_IUnknown, iid) ||
511 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
513 *ppv = &This->IWICEnumMetadataItem_iface;
515 else
517 *ppv = NULL;
518 return E_NOINTERFACE;
521 IUnknown_AddRef((IUnknown*)*ppv);
522 return S_OK;
525 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
527 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
528 ULONG ref = InterlockedIncrement(&This->ref);
530 TRACE("(%p) refcount=%u\n", iface, ref);
532 return ref;
535 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
537 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
538 ULONG ref = InterlockedDecrement(&This->ref);
540 TRACE("(%p) refcount=%u\n", iface, ref);
542 if (ref == 0)
544 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
545 HeapFree(GetProcessHeap(), 0, This);
548 return ref;
551 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
552 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
553 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
555 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
556 ULONG new_index;
557 HRESULT hr=S_FALSE;
558 ULONG i;
560 TRACE("(%p,%i)\n", iface, celt);
562 EnterCriticalSection(&This->parent->lock);
564 if (This->index >= This->parent->item_count)
566 *pceltFetched = 0;
567 LeaveCriticalSection(&This->parent->lock);
568 return S_FALSE;
571 new_index = min(This->parent->item_count, This->index + celt);
572 *pceltFetched = new_index - This->index;
574 if (rgeltSchema)
576 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
577 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
580 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
581 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
583 if (rgeltValue)
585 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
586 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
589 if (SUCCEEDED(hr))
591 This->index = new_index;
594 LeaveCriticalSection(&This->parent->lock);
596 return hr;
599 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
600 ULONG celt)
602 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
604 EnterCriticalSection(&This->parent->lock);
606 This->index += celt;
608 LeaveCriticalSection(&This->parent->lock);
610 return S_OK;
613 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
615 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
617 EnterCriticalSection(&This->parent->lock);
619 This->index = 0;
621 LeaveCriticalSection(&This->parent->lock);
623 return S_OK;
626 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
627 IWICEnumMetadataItem **ppIEnumMetadataItem)
629 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
630 HRESULT hr;
632 EnterCriticalSection(&This->parent->lock);
634 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
636 LeaveCriticalSection(&This->parent->lock);
638 return hr;
641 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
642 MetadataHandlerEnum_QueryInterface,
643 MetadataHandlerEnum_AddRef,
644 MetadataHandlerEnum_Release,
645 MetadataHandlerEnum_Next,
646 MetadataHandlerEnum_Skip,
647 MetadataHandlerEnum_Reset,
648 MetadataHandlerEnum_Clone
651 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
652 IWICEnumMetadataItem **ppIEnumMetadataItem)
654 MetadataHandlerEnum *This;
656 if (!ppIEnumMetadataItem) return E_INVALIDARG;
658 *ppIEnumMetadataItem = NULL;
660 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
661 if (!This) return E_OUTOFMEMORY;
663 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
665 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
666 This->ref = 1;
667 This->parent = parent;
668 This->index = index;
670 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
672 return S_OK;
675 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
676 DWORD persist_options, MetadataItem **items, DWORD *item_count)
678 HRESULT hr;
679 MetadataItem *result;
680 STATSTG stat;
681 BYTE *data;
682 ULONG bytesread;
684 TRACE("\n");
686 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
687 if (FAILED(hr))
688 return hr;
690 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
691 if (!data) return E_OUTOFMEMORY;
693 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
694 if (bytesread != stat.cbSize.QuadPart) hr = E_FAIL;
695 if (hr != S_OK)
697 HeapFree(GetProcessHeap(), 0, data);
698 return hr;
701 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
702 if (!result)
704 HeapFree(GetProcessHeap(), 0, data);
705 return E_OUTOFMEMORY;
708 PropVariantInit(&result[0].schema);
709 PropVariantInit(&result[0].id);
710 PropVariantInit(&result[0].value);
712 result[0].value.vt = VT_BLOB;
713 result[0].value.u.blob.cbSize = bytesread;
714 result[0].value.u.blob.pBlobData = data;
716 *items = result;
717 *item_count = 1;
719 return S_OK;
722 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
724 &CLSID_WICUnknownMetadataReader,
725 LoadUnknownMetadata
728 HRESULT UnknownMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
730 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, pUnkOuter, iid, ppv);
733 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
734 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
735 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
737 struct IFD_entry
739 SHORT id;
740 SHORT type;
741 ULONG count;
742 LONG value;
745 #define IFD_BYTE 1
746 #define IFD_ASCII 2
747 #define IFD_SHORT 3
748 #define IFD_LONG 4
749 #define IFD_RATIONAL 5
750 #define IFD_SBYTE 6
751 #define IFD_UNDEFINED 7
752 #define IFD_SSHORT 8
753 #define IFD_SLONG 9
754 #define IFD_SRATIONAL 10
755 #define IFD_FLOAT 11
756 #define IFD_DOUBLE 12
757 #define IFD_IFD 13
759 static int tag_to_vt(SHORT tag)
761 static const int tag2vt[] =
763 VT_EMPTY, /* 0 */
764 VT_UI1, /* IFD_BYTE 1 */
765 VT_LPSTR, /* IFD_ASCII 2 */
766 VT_UI2, /* IFD_SHORT 3 */
767 VT_UI4, /* IFD_LONG 4 */
768 VT_UI8, /* IFD_RATIONAL 5 */
769 VT_I1, /* IFD_SBYTE 6 */
770 VT_BLOB, /* IFD_UNDEFINED 7 */
771 VT_I2, /* IFD_SSHORT 8 */
772 VT_I4, /* IFD_SLONG 9 */
773 VT_I8, /* IFD_SRATIONAL 10 */
774 VT_R4, /* IFD_FLOAT 11 */
775 VT_R8, /* IFD_DOUBLE 12 */
776 VT_BLOB, /* IFD_IFD 13 */
778 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
781 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
782 MetadataItem *item, BOOL native_byte_order)
784 ULONG count, value, i, bytesread;
785 SHORT type;
786 LARGE_INTEGER pos;
787 HRESULT hr;
789 item->schema.vt = VT_EMPTY;
790 item->id.vt = VT_UI2;
791 item->id.u.uiVal = entry->id;
792 SWAP_USHORT(item->id.u.uiVal);
794 count = entry->count;
795 SWAP_ULONG(count);
796 type = entry->type;
797 SWAP_USHORT(type);
798 item->value.vt = tag_to_vt(type);
799 value = entry->value;
800 SWAP_ULONG(value);
802 switch (type)
804 case IFD_BYTE:
805 case IFD_SBYTE:
806 if (!count) count = 1;
808 if (count <= 4)
810 const BYTE *data = (const BYTE *)&entry->value;
812 if (count == 1)
813 item->value.u.bVal = data[0];
814 else
816 item->value.vt |= VT_VECTOR;
817 item->value.u.caub.cElems = count;
818 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
819 memcpy(item->value.u.caub.pElems, data, count);
821 break;
824 item->value.vt |= VT_VECTOR;
825 item->value.u.caub.cElems = count;
826 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
827 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
829 pos.QuadPart = value;
830 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
831 if (FAILED(hr))
833 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
834 return hr;
836 hr = IStream_Read(input, item->value.u.caub.pElems, count, &bytesread);
837 if (bytesread != count) hr = E_FAIL;
838 if (hr != S_OK)
840 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
841 return hr;
843 break;
844 case IFD_SHORT:
845 case IFD_SSHORT:
846 if (!count) count = 1;
848 if (count <= 2)
850 const SHORT *data = (const SHORT *)&entry->value;
852 if (count == 1)
854 item->value.u.uiVal = data[0];
855 SWAP_USHORT(item->value.u.uiVal);
857 else
859 item->value.vt |= VT_VECTOR;
860 item->value.u.caui.cElems = count;
861 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
862 memcpy(item->value.u.caui.pElems, data, count * 2);
863 for (i = 0; i < count; i++)
864 SWAP_USHORT(item->value.u.caui.pElems[i]);
866 break;
869 item->value.vt |= VT_VECTOR;
870 item->value.u.caui.cElems = count;
871 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
872 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
874 pos.QuadPart = value;
875 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
876 if (FAILED(hr))
878 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
879 return hr;
881 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, &bytesread);
882 if (bytesread != count * 2) hr = E_FAIL;
883 if (hr != S_OK)
885 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
886 return hr;
888 for (i = 0; i < count; i++)
889 SWAP_USHORT(item->value.u.caui.pElems[i]);
890 break;
891 case IFD_LONG:
892 case IFD_SLONG:
893 case IFD_FLOAT:
894 if (!count) count = 1;
896 if (count == 1)
898 item->value.u.ulVal = value;
899 break;
902 item->value.vt |= VT_VECTOR;
903 item->value.u.caul.cElems = count;
904 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
905 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
907 pos.QuadPart = value;
908 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
909 if (FAILED(hr))
911 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
912 return hr;
914 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, &bytesread);
915 if (bytesread != count * 4) hr = E_FAIL;
916 if (hr != S_OK)
918 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
919 return hr;
921 for (i = 0; i < count; i++)
922 SWAP_ULONG(item->value.u.caul.pElems[i]);
923 break;
924 case IFD_RATIONAL:
925 case IFD_SRATIONAL:
926 case IFD_DOUBLE:
927 if (!count)
929 FIXME("IFD field type %d, count 0\n", type);
930 item->value.vt = VT_EMPTY;
931 break;
934 if (count == 1)
936 ULONGLONG ull;
938 pos.QuadPart = value;
939 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
940 if (FAILED(hr)) return hr;
942 hr = IStream_Read(input, &ull, sizeof(ull), &bytesread);
943 if (bytesread != sizeof(ull)) hr = E_FAIL;
944 if (hr != S_OK) return hr;
946 item->value.u.uhVal.QuadPart = ull;
948 if (type == IFD_DOUBLE)
949 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
950 else
952 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
953 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
955 break;
957 else
959 item->value.vt |= VT_VECTOR;
960 item->value.u.cauh.cElems = count;
961 item->value.u.cauh.pElems = HeapAlloc(GetProcessHeap(), 0, count * 8);
962 if (!item->value.u.cauh.pElems) return E_OUTOFMEMORY;
964 pos.QuadPart = value;
965 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
966 if (FAILED(hr))
968 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
969 return hr;
971 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, &bytesread);
972 if (bytesread != count * 8) hr = E_FAIL;
973 if (hr != S_OK)
975 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
976 return hr;
978 for (i = 0; i < count; i++)
980 if (type == IFD_DOUBLE)
981 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
982 else
984 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
985 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
989 break;
990 case IFD_ASCII:
991 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
992 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
994 if (count <= 4)
996 const char *data = (const char *)&entry->value;
997 memcpy(item->value.u.pszVal, data, count);
998 item->value.u.pszVal[count] = 0;
999 break;
1002 pos.QuadPart = value;
1003 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1004 if (FAILED(hr))
1006 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
1007 return hr;
1009 hr = IStream_Read(input, item->value.u.pszVal, count, &bytesread);
1010 if (bytesread != count) hr = E_FAIL;
1011 if (hr != S_OK)
1013 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
1014 return hr;
1016 item->value.u.pszVal[count] = 0;
1017 break;
1018 case IFD_UNDEFINED:
1019 if (!count)
1021 FIXME("IFD field type %d, count 0\n", type);
1022 item->value.vt = VT_EMPTY;
1023 break;
1026 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
1027 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
1029 item->value.u.blob.cbSize = count;
1031 if (count <= 4)
1033 const char *data = (const char *)&entry->value;
1034 memcpy(item->value.u.blob.pBlobData, data, count);
1035 break;
1038 pos.QuadPart = value;
1039 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1040 if (FAILED(hr))
1042 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1043 return hr;
1045 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, &bytesread);
1046 if (bytesread != count) hr = E_FAIL;
1047 if (hr != S_OK)
1049 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1050 return hr;
1052 break;
1053 default:
1054 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
1055 break;
1057 return S_OK;
1060 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
1061 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1063 HRESULT hr;
1064 MetadataItem *result;
1065 USHORT count, i;
1066 struct IFD_entry *entry;
1067 BOOL native_byte_order = TRUE;
1068 ULONG bytesread;
1070 TRACE("\n");
1072 #ifdef WORDS_BIGENDIAN
1073 if (persist_options & WICPersistOptionsLittleEndian)
1074 #else
1075 if (persist_options & WICPersistOptionsBigEndian)
1076 #endif
1077 native_byte_order = FALSE;
1079 hr = IStream_Read(input, &count, sizeof(count), &bytesread);
1080 if (bytesread != sizeof(count)) hr = E_FAIL;
1081 if (hr != S_OK) return hr;
1083 SWAP_USHORT(count);
1085 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1086 if (!entry) return E_OUTOFMEMORY;
1088 hr = IStream_Read(input, entry, count * sizeof(*entry), &bytesread);
1089 if (bytesread != count * sizeof(*entry)) hr = E_FAIL;
1090 if (hr != S_OK)
1092 HeapFree(GetProcessHeap(), 0, entry);
1093 return hr;
1096 /* limit number of IFDs to 4096 to avoid infinite loop */
1097 for (i = 0; i < 4096; i++)
1099 ULONG next_ifd_offset;
1100 LARGE_INTEGER pos;
1101 USHORT next_ifd_count;
1103 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), &bytesread);
1104 if (bytesread != sizeof(next_ifd_offset)) hr = E_FAIL;
1105 if (hr != S_OK) break;
1107 SWAP_ULONG(next_ifd_offset);
1108 if (!next_ifd_offset) break;
1110 pos.QuadPart = next_ifd_offset;
1111 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1112 if (FAILED(hr)) break;
1114 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), &bytesread);
1115 if (bytesread != sizeof(next_ifd_count)) hr = E_FAIL;
1116 if (hr != S_OK) break;
1118 SWAP_USHORT(next_ifd_count);
1120 pos.QuadPart = next_ifd_count * sizeof(*entry);
1121 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1122 if (FAILED(hr)) break;
1125 if (hr != S_OK || i == 4096)
1127 HeapFree(GetProcessHeap(), 0, entry);
1128 return WINCODEC_ERR_BADMETADATAHEADER;
1131 result = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*result));
1132 if (!result)
1134 HeapFree(GetProcessHeap(), 0, entry);
1135 return E_OUTOFMEMORY;
1138 for (i = 0; i < count; i++)
1140 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1141 if (FAILED(hr))
1143 HeapFree(GetProcessHeap(), 0, entry);
1144 HeapFree(GetProcessHeap(), 0, result);
1145 return hr;
1149 HeapFree(GetProcessHeap(), 0, entry);
1151 *items = result;
1152 *item_count = count;
1154 return S_OK;
1157 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1159 &CLSID_WICIfdMetadataReader,
1160 LoadIfdMetadata
1163 HRESULT IfdMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void **ppv)
1165 return MetadataReader_Create(&IfdMetadataReader_Vtbl, pUnkOuter, iid, ppv);