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
26 #define NONAMELESSUNION
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;
44 IWICPersistStream IWICPersistStream_iface;
45 const MetadataHandlerVtbl *vtable;
48 CRITICAL_SECTION lock;
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)
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,
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;
101 return E_NOINTERFACE;
104 IUnknown_AddRef((IUnknown*)*ppv);
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);
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);
127 MetadataHandler_FreeItems(This);
128 This->lock.DebugInfo->Spare[0] = 0;
129 DeleteCriticalSection(&This->lock);
130 HeapFree(GetProcessHeap(), 0, This);
136 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
137 IWICMetadataHandlerInfo **ppIHandler)
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);
155 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
156 GUID *pguidMetadataFormat)
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);
174 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
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;
187 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
188 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
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);
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);
216 static BOOL get_int_value(const PROPVARIANT *pv, LONGLONG *value)
234 *value = pv->u.uiVal;
240 *value = pv->u.ulVal;
244 *value = pv->u.hVal.QuadPart;
247 FIXME("not supported variant type %d\n", pv->vt);
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 (!get_int_value(v1, &value1)) return -1;
259 if (!get_int_value(v2, &value2)) return -1;
262 if (value1) return value1 < 0 ? -1 : 1;
266 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
267 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
270 HRESULT hr = WINCODEC_ERR_PROPERTYNOTFOUND;
271 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
273 TRACE("(%p,%p,%p,%p)\n", iface, schema, id, value);
275 if (!id) return E_INVALIDARG;
277 EnterCriticalSection(&This->lock);
279 for (i = 0; i < This->item_count; i++)
281 if (schema && This->items[i].schema.vt != VT_EMPTY)
283 if (propvar_cmp(schema, &This->items[i].schema) != 0) continue;
286 if (propvar_cmp(id, &This->items[i].id) != 0) continue;
288 hr = value ? PropVariantCopy(value, &This->items[i].value) : S_OK;
292 LeaveCriticalSection(&This->lock);
296 static HRESULT WINAPI MetadataHandler_GetEnumerator(IWICMetadataWriter *iface,
297 IWICEnumMetadataItem **ppIEnumMetadata)
299 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
300 TRACE("(%p,%p)\n", iface, ppIEnumMetadata);
301 return MetadataHandlerEnum_Create(This, 0, ppIEnumMetadata);
304 static HRESULT WINAPI MetadataHandler_SetValue(IWICMetadataWriter *iface,
305 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
307 FIXME("(%p,%p,%p,%p): stub\n", iface, pvarSchema, pvarId, pvarValue);
311 static HRESULT WINAPI MetadataHandler_SetValueByIndex(IWICMetadataWriter *iface,
312 UINT nIndex, const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
314 FIXME("(%p,%u,%p,%p,%p): stub\n", iface, nIndex, pvarSchema, pvarId, pvarValue);
318 static HRESULT WINAPI MetadataHandler_RemoveValue(IWICMetadataWriter *iface,
319 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId)
321 FIXME("(%p,%p,%p): stub\n", iface, pvarSchema, pvarId);
325 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
328 FIXME("(%p,%u): stub\n", iface, nIndex);
332 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl = {
333 MetadataHandler_QueryInterface,
334 MetadataHandler_AddRef,
335 MetadataHandler_Release,
336 MetadataHandler_GetMetadataFormat,
337 MetadataHandler_GetMetadataHandlerInfo,
338 MetadataHandler_GetCount,
339 MetadataHandler_GetValueByIndex,
340 MetadataHandler_GetValue,
341 MetadataHandler_GetEnumerator,
342 MetadataHandler_SetValue,
343 MetadataHandler_SetValueByIndex,
344 MetadataHandler_RemoveValue,
345 MetadataHandler_RemoveValueByIndex
348 static HRESULT WINAPI MetadataHandler_PersistStream_QueryInterface(IWICPersistStream *iface,
349 REFIID iid, void **ppv)
351 MetadataHandler *This = impl_from_IWICPersistStream(iface);
352 return IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
355 static ULONG WINAPI MetadataHandler_PersistStream_AddRef(IWICPersistStream *iface)
357 MetadataHandler *This = impl_from_IWICPersistStream(iface);
358 return IWICMetadataWriter_AddRef(&This->IWICMetadataWriter_iface);
361 static ULONG WINAPI MetadataHandler_PersistStream_Release(IWICPersistStream *iface)
363 MetadataHandler *This = impl_from_IWICPersistStream(iface);
364 return IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
367 static HRESULT WINAPI MetadataHandler_GetClassID(IWICPersistStream *iface,
370 FIXME("(%p,%p): stub\n", iface, pClassID);
374 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
376 FIXME("(%p): stub\n", iface);
380 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
383 FIXME("(%p,%p): stub\n", iface, pStm);
387 static HRESULT WINAPI MetadataHandler_Save(IWICPersistStream *iface,
388 IStream *pStm, BOOL fClearDirty)
390 FIXME("(%p,%p,%i): stub\n", iface, pStm, fClearDirty);
394 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
395 ULARGE_INTEGER *pcbSize)
397 FIXME("(%p,%p): stub\n", iface, pcbSize);
401 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
402 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
404 MetadataHandler *This = impl_from_IWICPersistStream(iface);
406 MetadataItem *new_items=NULL;
409 TRACE("(%p,%p,%s,%x)\n", iface, pIStream, debugstr_guid(pguidPreferredVendor), dwPersistOptions);
411 EnterCriticalSection(&This->lock);
413 hr = This->vtable->fnLoad(pIStream, pguidPreferredVendor, dwPersistOptions,
414 &new_items, &item_count);
418 MetadataHandler_FreeItems(This);
419 This->items = new_items;
420 This->item_count = item_count;
423 LeaveCriticalSection(&This->lock);
428 static HRESULT WINAPI MetadataHandler_SaveEx(IWICPersistStream *iface,
429 IStream *pIStream, DWORD dwPersistOptions, BOOL fClearDirty)
431 FIXME("(%p,%p,%x,%i): stub\n", iface, pIStream, dwPersistOptions, fClearDirty);
435 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl = {
436 MetadataHandler_PersistStream_QueryInterface,
437 MetadataHandler_PersistStream_AddRef,
438 MetadataHandler_PersistStream_Release,
439 MetadataHandler_GetClassID,
440 MetadataHandler_IsDirty,
441 MetadataHandler_Load,
442 MetadataHandler_Save,
443 MetadataHandler_GetSizeMax,
444 MetadataHandler_LoadEx,
445 MetadataHandler_SaveEx
448 static HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, IUnknown *pUnkOuter, REFIID iid, void** ppv)
450 MetadataHandler *This;
453 TRACE("%s\n", debugstr_guid(vtable->clsid));
457 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
459 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
460 if (!This) return E_OUTOFMEMORY;
462 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
463 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
465 This->vtable = vtable;
467 This->item_count = 0;
469 InitializeCriticalSection(&This->lock);
470 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
472 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
474 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
479 typedef struct MetadataHandlerEnum {
480 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
482 MetadataHandler *parent;
484 } MetadataHandlerEnum;
486 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
488 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
491 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
494 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
495 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
497 if (!ppv) return E_INVALIDARG;
499 if (IsEqualIID(&IID_IUnknown, iid) ||
500 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
502 *ppv = &This->IWICEnumMetadataItem_iface;
507 return E_NOINTERFACE;
510 IUnknown_AddRef((IUnknown*)*ppv);
514 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
516 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
517 ULONG ref = InterlockedIncrement(&This->ref);
519 TRACE("(%p) refcount=%u\n", iface, ref);
524 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
526 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
527 ULONG ref = InterlockedDecrement(&This->ref);
529 TRACE("(%p) refcount=%u\n", iface, ref);
533 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
534 HeapFree(GetProcessHeap(), 0, This);
540 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
541 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
542 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
544 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
549 TRACE("(%p,%i)\n", iface, celt);
551 EnterCriticalSection(&This->parent->lock);
553 if (This->index >= This->parent->item_count)
556 LeaveCriticalSection(&This->parent->lock);
560 new_index = min(This->parent->item_count, This->index + celt);
561 *pceltFetched = new_index - This->index;
565 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
566 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
569 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
570 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
574 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
575 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
580 This->index = new_index;
583 LeaveCriticalSection(&This->parent->lock);
588 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
591 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
593 EnterCriticalSection(&This->parent->lock);
597 LeaveCriticalSection(&This->parent->lock);
602 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
604 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
606 EnterCriticalSection(&This->parent->lock);
610 LeaveCriticalSection(&This->parent->lock);
615 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
616 IWICEnumMetadataItem **ppIEnumMetadataItem)
618 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
621 EnterCriticalSection(&This->parent->lock);
623 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
625 LeaveCriticalSection(&This->parent->lock);
630 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
631 MetadataHandlerEnum_QueryInterface,
632 MetadataHandlerEnum_AddRef,
633 MetadataHandlerEnum_Release,
634 MetadataHandlerEnum_Next,
635 MetadataHandlerEnum_Skip,
636 MetadataHandlerEnum_Reset,
637 MetadataHandlerEnum_Clone
640 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
641 IWICEnumMetadataItem **ppIEnumMetadataItem)
643 MetadataHandlerEnum *This;
645 if (!ppIEnumMetadataItem) return E_INVALIDARG;
647 *ppIEnumMetadataItem = NULL;
649 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
650 if (!This) return E_OUTOFMEMORY;
652 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
654 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
656 This->parent = parent;
659 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
664 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
665 DWORD persist_options, MetadataItem **items, DWORD *item_count)
668 MetadataItem *result;
675 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
679 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
680 if (!data) return E_OUTOFMEMORY;
682 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
685 HeapFree(GetProcessHeap(), 0, data);
689 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
692 HeapFree(GetProcessHeap(), 0, data);
693 return E_OUTOFMEMORY;
696 PropVariantInit(&result[0].schema);
697 PropVariantInit(&result[0].id);
698 PropVariantInit(&result[0].value);
700 result[0].value.vt = VT_BLOB;
701 result[0].value.u.blob.cbSize = bytesread;
702 result[0].value.u.blob.pBlobData = data;
710 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
712 &CLSID_WICUnknownMetadataReader,
716 HRESULT UnknownMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
718 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, pUnkOuter, iid, ppv);
721 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
722 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
723 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
737 #define IFD_RATIONAL 5
739 #define IFD_UNDEFINED 7
742 #define IFD_SRATIONAL 10
744 #define IFD_DOUBLE 12
747 static int tag_to_vt(SHORT tag)
749 static const int tag2vt[] =
752 VT_UI1, /* IFD_BYTE 1 */
753 VT_LPSTR, /* IFD_ASCII 2 */
754 VT_UI2, /* IFD_SHORT 3 */
755 VT_UI4, /* IFD_LONG 4 */
756 VT_UI8, /* IFD_RATIONAL 5 */
757 VT_I1, /* IFD_SBYTE 6 */
758 VT_BLOB, /* IFD_UNDEFINED 7 */
759 VT_I2, /* IFD_SSHORT 8 */
760 VT_I4, /* IFD_SLONG 9 */
761 VT_I8, /* IFD_SRATIONAL 10 */
762 VT_R4, /* IFD_FLOAT 11 */
763 VT_R8, /* IFD_DOUBLE 12 */
764 VT_BLOB, /* IFD_IFD 13 */
766 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
769 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
770 MetadataItem *item, BOOL native_byte_order)
772 ULONG count, value, i;
777 item->schema.vt = VT_EMPTY;
778 item->id.vt = VT_UI2;
779 item->id.u.uiVal = entry->id;
780 SWAP_USHORT(item->id.u.uiVal);
782 count = entry->count;
786 item->value.vt = tag_to_vt(type);
787 value = entry->value;
794 if (!count) count = 1;
798 const BYTE *data = (const BYTE *)&entry->value;
801 item->value.u.bVal = data[0];
804 item->value.vt |= VT_VECTOR;
805 item->value.u.caub.cElems = count;
806 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
807 memcpy(item->value.u.caub.pElems, data, count);
812 item->value.vt |= VT_VECTOR;
813 item->value.u.caub.cElems = count;
814 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
815 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
817 pos.QuadPart = value;
818 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
821 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
824 hr = IStream_Read(input, item->value.u.caub.pElems, count, NULL);
827 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
833 if (!count) count = 1;
837 const SHORT *data = (const SHORT *)&entry->value;
841 item->value.u.uiVal = data[0];
842 SWAP_USHORT(item->value.u.uiVal);
846 item->value.vt |= VT_VECTOR;
847 item->value.u.caui.cElems = count;
848 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
849 memcpy(item->value.u.caui.pElems, data, count * 2);
850 for (i = 0; i < count; i++)
851 SWAP_USHORT(item->value.u.caui.pElems[i]);
856 item->value.vt |= VT_VECTOR;
857 item->value.u.caui.cElems = count;
858 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
859 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
861 pos.QuadPart = value;
862 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
865 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
868 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, NULL);
871 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
874 for (i = 0; i < count; i++)
875 SWAP_USHORT(item->value.u.caui.pElems[i]);
880 if (!count) count = 1;
884 item->value.u.ulVal = value;
888 item->value.vt |= VT_VECTOR;
889 item->value.u.caul.cElems = count;
890 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
891 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
893 pos.QuadPart = value;
894 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
897 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
900 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, NULL);
903 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
906 for (i = 0; i < count; i++)
907 SWAP_ULONG(item->value.u.caul.pElems[i]);
914 FIXME("IFD field type %d, count 0\n", type);
915 item->value.vt = VT_EMPTY;
923 pos.QuadPart = value;
924 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
925 if (FAILED(hr)) return hr;
927 hr = IStream_Read(input, &ull, sizeof(ull), NULL);
928 if (FAILED(hr)) return hr;
930 item->value.u.uhVal.QuadPart = ull;
932 if (type == IFD_DOUBLE)
933 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
936 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
937 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
943 item->value.vt |= VT_VECTOR;
944 item->value.u.cauh.cElems = count;
945 item->value.u.cauh.pElems = HeapAlloc(GetProcessHeap(), 0, count * 8);
946 if (!item->value.u.cauh.pElems) return E_OUTOFMEMORY;
948 pos.QuadPart = value;
949 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
952 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
955 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, NULL);
958 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
961 for (i = 0; i < count; i++)
963 if (type == IFD_DOUBLE)
964 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
967 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
968 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
974 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
975 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
979 const char *data = (const char *)&entry->value;
980 memcpy(item->value.u.pszVal, data, count);
981 item->value.u.pszVal[count] = 0;
985 pos.QuadPart = value;
986 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
989 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
992 hr = IStream_Read(input, item->value.u.pszVal, count, NULL);
995 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
998 item->value.u.pszVal[count] = 0;
1003 FIXME("IFD field type %d, count 0\n", type);
1004 item->value.vt = VT_EMPTY;
1008 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
1009 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
1011 item->value.u.blob.cbSize = count;
1015 const char *data = (const char *)&entry->value;
1016 memcpy(item->value.u.blob.pBlobData, data, count);
1020 pos.QuadPart = value;
1021 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1024 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1027 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, NULL);
1030 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1035 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
1041 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
1042 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1045 MetadataItem *result;
1047 struct IFD_entry *entry;
1048 BOOL native_byte_order = TRUE;
1052 #ifdef WORDS_BIGENDIAN
1053 if (persist_options & WICPersistOptionsLittleEndian)
1055 if (persist_options & WICPersistOptionsBigEndian)
1057 native_byte_order = FALSE;
1059 hr = IStream_Read(input, &count, sizeof(count), NULL);
1060 if (FAILED(hr)) return hr;
1064 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1065 if (!entry) return E_OUTOFMEMORY;
1067 hr = IStream_Read(input, entry, count * sizeof(*entry), NULL);
1070 HeapFree(GetProcessHeap(), 0, entry);
1074 /* limit number of IFDs to 4096 to avoid infinite loop */
1075 for (i = 0; i < 4096; i++)
1077 ULONG next_ifd_offset;
1079 USHORT next_ifd_count;
1081 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), NULL);
1082 if (FAILED(hr)) break;
1084 SWAP_ULONG(next_ifd_offset);
1085 if (!next_ifd_offset) break;
1087 pos.QuadPart = next_ifd_offset;
1088 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1089 if (FAILED(hr)) break;
1091 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), NULL);
1092 if (FAILED(hr)) break;
1094 SWAP_USHORT(next_ifd_count);
1096 pos.QuadPart = next_ifd_count * sizeof(*entry);
1097 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1098 if (FAILED(hr)) break;
1101 if (FAILED(hr) || i == 4096)
1103 HeapFree(GetProcessHeap(), 0, entry);
1104 return WINCODEC_ERR_BADMETADATAHEADER;
1107 result = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*result));
1110 HeapFree(GetProcessHeap(), 0, entry);
1111 return E_OUTOFMEMORY;
1114 for (i = 0; i < count; i++)
1116 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1119 HeapFree(GetProcessHeap(), 0, entry);
1120 HeapFree(GetProcessHeap(), 0, result);
1125 HeapFree(GetProcessHeap(), 0, entry);
1128 *item_count = count;
1133 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1135 &CLSID_WICIfdMetadataReader,
1139 HRESULT IfdMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void **ppv)
1141 return MetadataReader_Create(&IfdMetadataReader_Vtbl, pUnkOuter, iid, ppv);