wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / windowscodecs / metadatahandler.c
blobfb35f80622a0c82bde3adb6997b6ca70a976dcd0
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, REFIID iid, void** ppv)
461 MetadataHandler *This;
462 HRESULT hr;
464 TRACE("%s\n", debugstr_guid(vtable->clsid));
466 *ppv = NULL;
468 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
469 if (!This) return E_OUTOFMEMORY;
471 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
472 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
473 This->ref = 1;
474 This->vtable = vtable;
475 This->items = NULL;
476 This->item_count = 0;
478 InitializeCriticalSection(&This->lock);
479 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
481 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
483 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
485 return hr;
488 typedef struct MetadataHandlerEnum {
489 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
490 LONG ref;
491 MetadataHandler *parent;
492 DWORD index;
493 } MetadataHandlerEnum;
495 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
497 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
500 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
501 void **ppv)
503 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
504 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
506 if (!ppv) return E_INVALIDARG;
508 if (IsEqualIID(&IID_IUnknown, iid) ||
509 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
511 *ppv = &This->IWICEnumMetadataItem_iface;
513 else
515 *ppv = NULL;
516 return E_NOINTERFACE;
519 IUnknown_AddRef((IUnknown*)*ppv);
520 return S_OK;
523 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
525 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
526 ULONG ref = InterlockedIncrement(&This->ref);
528 TRACE("(%p) refcount=%u\n", iface, ref);
530 return ref;
533 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
535 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
536 ULONG ref = InterlockedDecrement(&This->ref);
538 TRACE("(%p) refcount=%u\n", iface, ref);
540 if (ref == 0)
542 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
543 HeapFree(GetProcessHeap(), 0, This);
546 return ref;
549 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
550 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
551 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
553 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
554 ULONG new_index;
555 HRESULT hr=S_FALSE;
556 ULONG i;
558 TRACE("(%p,%i)\n", iface, celt);
560 EnterCriticalSection(&This->parent->lock);
562 if (This->index >= This->parent->item_count)
564 *pceltFetched = 0;
565 LeaveCriticalSection(&This->parent->lock);
566 return S_FALSE;
569 new_index = min(This->parent->item_count, This->index + celt);
570 *pceltFetched = new_index - This->index;
572 if (rgeltSchema)
574 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
575 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
578 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
579 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
581 if (rgeltValue)
583 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
584 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
587 if (SUCCEEDED(hr))
589 This->index = new_index;
592 LeaveCriticalSection(&This->parent->lock);
594 return hr;
597 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
598 ULONG celt)
600 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
602 EnterCriticalSection(&This->parent->lock);
604 This->index += celt;
606 LeaveCriticalSection(&This->parent->lock);
608 return S_OK;
611 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
613 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
615 EnterCriticalSection(&This->parent->lock);
617 This->index = 0;
619 LeaveCriticalSection(&This->parent->lock);
621 return S_OK;
624 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
625 IWICEnumMetadataItem **ppIEnumMetadataItem)
627 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
628 HRESULT hr;
630 EnterCriticalSection(&This->parent->lock);
632 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
634 LeaveCriticalSection(&This->parent->lock);
636 return hr;
639 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
640 MetadataHandlerEnum_QueryInterface,
641 MetadataHandlerEnum_AddRef,
642 MetadataHandlerEnum_Release,
643 MetadataHandlerEnum_Next,
644 MetadataHandlerEnum_Skip,
645 MetadataHandlerEnum_Reset,
646 MetadataHandlerEnum_Clone
649 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
650 IWICEnumMetadataItem **ppIEnumMetadataItem)
652 MetadataHandlerEnum *This;
654 if (!ppIEnumMetadataItem) return E_INVALIDARG;
656 *ppIEnumMetadataItem = NULL;
658 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
659 if (!This) return E_OUTOFMEMORY;
661 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
663 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
664 This->ref = 1;
665 This->parent = parent;
666 This->index = index;
668 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
670 return S_OK;
673 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
674 DWORD persist_options, MetadataItem **items, DWORD *item_count)
676 HRESULT hr;
677 MetadataItem *result;
678 STATSTG stat;
679 BYTE *data;
680 ULONG bytesread;
682 TRACE("\n");
684 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
685 if (FAILED(hr))
686 return hr;
688 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
689 if (!data) return E_OUTOFMEMORY;
691 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
692 if (bytesread != stat.cbSize.QuadPart) hr = E_FAIL;
693 if (hr != S_OK)
695 HeapFree(GetProcessHeap(), 0, data);
696 return hr;
699 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
700 if (!result)
702 HeapFree(GetProcessHeap(), 0, data);
703 return E_OUTOFMEMORY;
706 PropVariantInit(&result[0].schema);
707 PropVariantInit(&result[0].id);
708 PropVariantInit(&result[0].value);
710 result[0].value.vt = VT_BLOB;
711 result[0].value.u.blob.cbSize = bytesread;
712 result[0].value.u.blob.pBlobData = data;
714 *items = result;
715 *item_count = 1;
717 return S_OK;
720 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
722 &CLSID_WICUnknownMetadataReader,
723 LoadUnknownMetadata
726 HRESULT UnknownMetadataReader_CreateInstance(REFIID iid, void** ppv)
728 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, iid, ppv);
731 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
732 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
733 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
735 struct IFD_entry
737 SHORT id;
738 SHORT type;
739 ULONG count;
740 LONG value;
743 #define IFD_BYTE 1
744 #define IFD_ASCII 2
745 #define IFD_SHORT 3
746 #define IFD_LONG 4
747 #define IFD_RATIONAL 5
748 #define IFD_SBYTE 6
749 #define IFD_UNDEFINED 7
750 #define IFD_SSHORT 8
751 #define IFD_SLONG 9
752 #define IFD_SRATIONAL 10
753 #define IFD_FLOAT 11
754 #define IFD_DOUBLE 12
755 #define IFD_IFD 13
757 static int tag_to_vt(SHORT tag)
759 static const int tag2vt[] =
761 VT_EMPTY, /* 0 */
762 VT_UI1, /* IFD_BYTE 1 */
763 VT_LPSTR, /* IFD_ASCII 2 */
764 VT_UI2, /* IFD_SHORT 3 */
765 VT_UI4, /* IFD_LONG 4 */
766 VT_UI8, /* IFD_RATIONAL 5 */
767 VT_I1, /* IFD_SBYTE 6 */
768 VT_BLOB, /* IFD_UNDEFINED 7 */
769 VT_I2, /* IFD_SSHORT 8 */
770 VT_I4, /* IFD_SLONG 9 */
771 VT_I8, /* IFD_SRATIONAL 10 */
772 VT_R4, /* IFD_FLOAT 11 */
773 VT_R8, /* IFD_DOUBLE 12 */
774 VT_BLOB, /* IFD_IFD 13 */
776 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
779 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
780 MetadataItem *item, BOOL native_byte_order)
782 ULONG count, value, i, bytesread;
783 SHORT type;
784 LARGE_INTEGER pos;
785 HRESULT hr;
787 item->schema.vt = VT_EMPTY;
788 item->id.vt = VT_UI2;
789 item->id.u.uiVal = entry->id;
790 SWAP_USHORT(item->id.u.uiVal);
792 count = entry->count;
793 SWAP_ULONG(count);
794 type = entry->type;
795 SWAP_USHORT(type);
796 item->value.vt = tag_to_vt(type);
797 value = entry->value;
798 SWAP_ULONG(value);
800 switch (type)
802 case IFD_BYTE:
803 case IFD_SBYTE:
804 if (!count) count = 1;
806 if (count <= 4)
808 const BYTE *data = (const BYTE *)&entry->value;
810 if (count == 1)
811 item->value.u.bVal = data[0];
812 else
814 item->value.vt |= VT_VECTOR;
815 item->value.u.caub.cElems = count;
816 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
817 memcpy(item->value.u.caub.pElems, data, count);
819 break;
822 item->value.vt |= VT_VECTOR;
823 item->value.u.caub.cElems = count;
824 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
825 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
827 pos.QuadPart = value;
828 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
829 if (FAILED(hr))
831 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
832 return hr;
834 hr = IStream_Read(input, item->value.u.caub.pElems, count, &bytesread);
835 if (bytesread != count) hr = E_FAIL;
836 if (hr != S_OK)
838 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
839 return hr;
841 break;
842 case IFD_SHORT:
843 case IFD_SSHORT:
844 if (!count) count = 1;
846 if (count <= 2)
848 const SHORT *data = (const SHORT *)&entry->value;
850 if (count == 1)
852 item->value.u.uiVal = data[0];
853 SWAP_USHORT(item->value.u.uiVal);
855 else
857 item->value.vt |= VT_VECTOR;
858 item->value.u.caui.cElems = count;
859 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
860 memcpy(item->value.u.caui.pElems, data, count * 2);
861 for (i = 0; i < count; i++)
862 SWAP_USHORT(item->value.u.caui.pElems[i]);
864 break;
867 item->value.vt |= VT_VECTOR;
868 item->value.u.caui.cElems = count;
869 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
870 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
872 pos.QuadPart = value;
873 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
874 if (FAILED(hr))
876 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
877 return hr;
879 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, &bytesread);
880 if (bytesread != count * 2) hr = E_FAIL;
881 if (hr != S_OK)
883 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
884 return hr;
886 for (i = 0; i < count; i++)
887 SWAP_USHORT(item->value.u.caui.pElems[i]);
888 break;
889 case IFD_LONG:
890 case IFD_SLONG:
891 case IFD_FLOAT:
892 if (!count) count = 1;
894 if (count == 1)
896 item->value.u.ulVal = value;
897 break;
900 item->value.vt |= VT_VECTOR;
901 item->value.u.caul.cElems = count;
902 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
903 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
905 pos.QuadPart = value;
906 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
907 if (FAILED(hr))
909 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
910 return hr;
912 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, &bytesread);
913 if (bytesread != count * 4) hr = E_FAIL;
914 if (hr != S_OK)
916 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
917 return hr;
919 for (i = 0; i < count; i++)
920 SWAP_ULONG(item->value.u.caul.pElems[i]);
921 break;
922 case IFD_RATIONAL:
923 case IFD_SRATIONAL:
924 case IFD_DOUBLE:
925 if (!count)
927 FIXME("IFD field type %d, count 0\n", type);
928 item->value.vt = VT_EMPTY;
929 break;
932 if (count == 1)
934 ULONGLONG ull;
936 pos.QuadPart = value;
937 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
938 if (FAILED(hr)) return hr;
940 hr = IStream_Read(input, &ull, sizeof(ull), &bytesread);
941 if (bytesread != sizeof(ull)) hr = E_FAIL;
942 if (hr != S_OK) return hr;
944 item->value.u.uhVal.QuadPart = ull;
946 if (type == IFD_DOUBLE)
947 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
948 else
950 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
951 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
953 break;
955 else
957 item->value.vt |= VT_VECTOR;
958 item->value.u.cauh.cElems = count;
959 item->value.u.cauh.pElems = HeapAlloc(GetProcessHeap(), 0, count * 8);
960 if (!item->value.u.cauh.pElems) return E_OUTOFMEMORY;
962 pos.QuadPart = value;
963 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
964 if (FAILED(hr))
966 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
967 return hr;
969 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, &bytesread);
970 if (bytesread != count * 8) hr = E_FAIL;
971 if (hr != S_OK)
973 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
974 return hr;
976 for (i = 0; i < count; i++)
978 if (type == IFD_DOUBLE)
979 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
980 else
982 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
983 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
987 break;
988 case IFD_ASCII:
989 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
990 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
992 if (count <= 4)
994 const char *data = (const char *)&entry->value;
995 memcpy(item->value.u.pszVal, data, count);
996 item->value.u.pszVal[count] = 0;
997 break;
1000 pos.QuadPart = value;
1001 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1002 if (FAILED(hr))
1004 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
1005 return hr;
1007 hr = IStream_Read(input, item->value.u.pszVal, count, &bytesread);
1008 if (bytesread != count) hr = E_FAIL;
1009 if (hr != S_OK)
1011 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
1012 return hr;
1014 item->value.u.pszVal[count] = 0;
1015 break;
1016 case IFD_UNDEFINED:
1017 if (!count)
1019 FIXME("IFD field type %d, count 0\n", type);
1020 item->value.vt = VT_EMPTY;
1021 break;
1024 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
1025 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
1027 item->value.u.blob.cbSize = count;
1029 if (count <= 4)
1031 const char *data = (const char *)&entry->value;
1032 memcpy(item->value.u.blob.pBlobData, data, count);
1033 break;
1036 pos.QuadPart = value;
1037 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1038 if (FAILED(hr))
1040 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1041 return hr;
1043 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, &bytesread);
1044 if (bytesread != count) hr = E_FAIL;
1045 if (hr != S_OK)
1047 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1048 return hr;
1050 break;
1051 default:
1052 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
1053 break;
1055 return S_OK;
1058 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
1059 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1061 HRESULT hr;
1062 MetadataItem *result;
1063 USHORT count, i;
1064 struct IFD_entry *entry;
1065 BOOL native_byte_order = TRUE;
1066 ULONG bytesread;
1068 TRACE("\n");
1070 #ifdef WORDS_BIGENDIAN
1071 if (persist_options & WICPersistOptionsLittleEndian)
1072 #else
1073 if (persist_options & WICPersistOptionsBigEndian)
1074 #endif
1075 native_byte_order = FALSE;
1077 hr = IStream_Read(input, &count, sizeof(count), &bytesread);
1078 if (bytesread != sizeof(count)) hr = E_FAIL;
1079 if (hr != S_OK) return hr;
1081 SWAP_USHORT(count);
1083 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1084 if (!entry) return E_OUTOFMEMORY;
1086 hr = IStream_Read(input, entry, count * sizeof(*entry), &bytesread);
1087 if (bytesread != count * sizeof(*entry)) hr = E_FAIL;
1088 if (hr != S_OK)
1090 HeapFree(GetProcessHeap(), 0, entry);
1091 return hr;
1094 /* limit number of IFDs to 4096 to avoid infinite loop */
1095 for (i = 0; i < 4096; i++)
1097 ULONG next_ifd_offset;
1098 LARGE_INTEGER pos;
1099 USHORT next_ifd_count;
1101 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), &bytesread);
1102 if (bytesread != sizeof(next_ifd_offset)) hr = E_FAIL;
1103 if (hr != S_OK) break;
1105 SWAP_ULONG(next_ifd_offset);
1106 if (!next_ifd_offset) break;
1108 pos.QuadPart = next_ifd_offset;
1109 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1110 if (FAILED(hr)) break;
1112 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), &bytesread);
1113 if (bytesread != sizeof(next_ifd_count)) hr = E_FAIL;
1114 if (hr != S_OK) break;
1116 SWAP_USHORT(next_ifd_count);
1118 pos.QuadPart = next_ifd_count * sizeof(*entry);
1119 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1120 if (FAILED(hr)) break;
1123 if (hr != S_OK || i == 4096)
1125 HeapFree(GetProcessHeap(), 0, entry);
1126 return WINCODEC_ERR_BADMETADATAHEADER;
1129 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(*result));
1130 if (!result)
1132 HeapFree(GetProcessHeap(), 0, entry);
1133 return E_OUTOFMEMORY;
1136 for (i = 0; i < count; i++)
1138 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1139 if (FAILED(hr))
1141 HeapFree(GetProcessHeap(), 0, entry);
1142 HeapFree(GetProcessHeap(), 0, result);
1143 return hr;
1147 HeapFree(GetProcessHeap(), 0, entry);
1149 *items = result;
1150 *item_count = count;
1152 return S_OK;
1155 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1157 &CLSID_WICIfdMetadataReader,
1158 LoadIfdMetadata
1161 HRESULT IfdMetadataReader_CreateInstance(REFIID iid, void **ppv)
1163 return MetadataReader_Create(&IfdMetadataReader_Vtbl, iid, ppv);