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 (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;
272 if (value1
) return value1
< 0 ? -1 : 1;
276 static HRESULT WINAPI
MetadataHandler_GetValue(IWICMetadataWriter
*iface
,
277 const PROPVARIANT
*schema
, const PROPVARIANT
*id
, PROPVARIANT
*value
)
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
;
302 LeaveCriticalSection(&This
->lock
);
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
);
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
);
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
);
335 static HRESULT WINAPI
MetadataHandler_RemoveValueByIndex(IWICMetadataWriter
*iface
,
338 FIXME("(%p,%u): stub\n", iface
, nIndex
);
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
,
380 FIXME("(%p,%p): stub\n", iface
, pClassID
);
384 static HRESULT WINAPI
MetadataHandler_IsDirty(IWICPersistStream
*iface
)
386 FIXME("(%p): stub\n", iface
);
390 static HRESULT WINAPI
MetadataHandler_Load(IWICPersistStream
*iface
,
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
);
405 static HRESULT WINAPI
MetadataHandler_GetSizeMax(IWICPersistStream
*iface
,
406 ULARGE_INTEGER
*pcbSize
)
408 FIXME("(%p,%p): stub\n", iface
, pcbSize
);
412 static HRESULT WINAPI
MetadataHandler_LoadEx(IWICPersistStream
*iface
,
413 IStream
*pIStream
, const GUID
*pguidPreferredVendor
, DWORD dwPersistOptions
)
415 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
417 MetadataItem
*new_items
=NULL
;
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
);
429 MetadataHandler_FreeItems(This
);
430 This
->items
= new_items
;
431 This
->item_count
= item_count
;
434 LeaveCriticalSection(&This
->lock
);
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
);
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
;
464 TRACE("%s\n", debugstr_guid(vtable
->clsid
));
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
;
474 This
->vtable
= vtable
;
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
);
488 typedef struct MetadataHandlerEnum
{
489 IWICEnumMetadataItem IWICEnumMetadataItem_iface
;
491 MetadataHandler
*parent
;
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
,
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
;
516 return E_NOINTERFACE
;
519 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
542 IWICMetadataWriter_Release(&This
->parent
->IWICMetadataWriter_iface
);
543 HeapFree(GetProcessHeap(), 0, This
);
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
);
558 TRACE("(%p,%i)\n", iface
, celt
);
560 EnterCriticalSection(&This
->parent
->lock
);
562 if (This
->index
>= This
->parent
->item_count
)
565 LeaveCriticalSection(&This
->parent
->lock
);
569 new_index
= min(This
->parent
->item_count
, This
->index
+ celt
);
570 *pceltFetched
= new_index
- This
->index
;
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
);
583 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
584 hr
= PropVariantCopy(&rgeltValue
[i
], &This
->parent
->items
[i
+This
->index
].value
);
589 This
->index
= new_index
;
592 LeaveCriticalSection(&This
->parent
->lock
);
597 static HRESULT WINAPI
MetadataHandlerEnum_Skip(IWICEnumMetadataItem
*iface
,
600 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
602 EnterCriticalSection(&This
->parent
->lock
);
606 LeaveCriticalSection(&This
->parent
->lock
);
611 static HRESULT WINAPI
MetadataHandlerEnum_Reset(IWICEnumMetadataItem
*iface
)
613 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
615 EnterCriticalSection(&This
->parent
->lock
);
619 LeaveCriticalSection(&This
->parent
->lock
);
624 static HRESULT WINAPI
MetadataHandlerEnum_Clone(IWICEnumMetadataItem
*iface
,
625 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
627 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
630 EnterCriticalSection(&This
->parent
->lock
);
632 hr
= MetadataHandlerEnum_Create(This
->parent
, This
->index
, ppIEnumMetadataItem
);
634 LeaveCriticalSection(&This
->parent
->lock
);
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
;
665 This
->parent
= parent
;
668 *ppIEnumMetadataItem
= &This
->IWICEnumMetadataItem_iface
;
673 static HRESULT
LoadUnknownMetadata(IStream
*input
, const GUID
*preferred_vendor
,
674 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
677 MetadataItem
*result
;
684 hr
= IStream_Stat(input
, &stat
, STATFLAG_NONAME
);
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
;
695 HeapFree(GetProcessHeap(), 0, data
);
699 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
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
;
720 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl
= {
722 &CLSID_WICUnknownMetadataReader
,
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)
747 #define IFD_RATIONAL 5
749 #define IFD_UNDEFINED 7
752 #define IFD_SRATIONAL 10
754 #define IFD_DOUBLE 12
757 static int tag_to_vt(SHORT tag
)
759 static const int tag2vt
[] =
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
;
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
;
796 item
->value
.vt
= tag_to_vt(type
);
797 value
= entry
->value
;
804 if (!count
) count
= 1;
808 const BYTE
*data
= (const BYTE
*)&entry
->value
;
811 item
->value
.u
.bVal
= data
[0];
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
);
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
);
831 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
834 hr
= IStream_Read(input
, item
->value
.u
.caub
.pElems
, count
, &bytesread
);
835 if (bytesread
!= count
) hr
= E_FAIL
;
838 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
844 if (!count
) count
= 1;
848 const SHORT
*data
= (const SHORT
*)&entry
->value
;
852 item
->value
.u
.uiVal
= data
[0];
853 SWAP_USHORT(item
->value
.u
.uiVal
);
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
]);
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
);
876 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
879 hr
= IStream_Read(input
, item
->value
.u
.caui
.pElems
, count
* 2, &bytesread
);
880 if (bytesread
!= count
* 2) hr
= E_FAIL
;
883 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
886 for (i
= 0; i
< count
; i
++)
887 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
892 if (!count
) count
= 1;
896 item
->value
.u
.ulVal
= value
;
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
);
909 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
912 hr
= IStream_Read(input
, item
->value
.u
.caul
.pElems
, count
* 4, &bytesread
);
913 if (bytesread
!= count
* 4) hr
= E_FAIL
;
916 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
919 for (i
= 0; i
< count
; i
++)
920 SWAP_ULONG(item
->value
.u
.caul
.pElems
[i
]);
927 FIXME("IFD field type %d, count 0\n", type
);
928 item
->value
.vt
= VT_EMPTY
;
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
);
950 SWAP_ULONG(item
->value
.u
.uhVal
.u
.LowPart
);
951 SWAP_ULONG(item
->value
.u
.uhVal
.u
.HighPart
);
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
);
966 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
969 hr
= IStream_Read(input
, item
->value
.u
.cauh
.pElems
, count
* 8, &bytesread
);
970 if (bytesread
!= count
* 8) hr
= E_FAIL
;
973 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
976 for (i
= 0; i
< count
; i
++)
978 if (type
== IFD_DOUBLE
)
979 SWAP_ULONGLONG(item
->value
.u
.cauh
.pElems
[i
].QuadPart
);
982 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.LowPart
);
983 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.HighPart
);
989 item
->value
.u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, count
+ 1);
990 if (!item
->value
.u
.pszVal
) return E_OUTOFMEMORY
;
994 const char *data
= (const char *)&entry
->value
;
995 memcpy(item
->value
.u
.pszVal
, data
, count
);
996 item
->value
.u
.pszVal
[count
] = 0;
1000 pos
.QuadPart
= value
;
1001 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1004 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
1007 hr
= IStream_Read(input
, item
->value
.u
.pszVal
, count
, &bytesread
);
1008 if (bytesread
!= count
) hr
= E_FAIL
;
1011 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
1014 item
->value
.u
.pszVal
[count
] = 0;
1019 FIXME("IFD field type %d, count 0\n", type
);
1020 item
->value
.vt
= VT_EMPTY
;
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
;
1031 const char *data
= (const char *)&entry
->value
;
1032 memcpy(item
->value
.u
.blob
.pBlobData
, data
, count
);
1036 pos
.QuadPart
= value
;
1037 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1040 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
1043 hr
= IStream_Read(input
, item
->value
.u
.blob
.pBlobData
, count
, &bytesread
);
1044 if (bytesread
!= count
) hr
= E_FAIL
;
1047 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
1052 FIXME("loading field of type %d, count %u is not implemented\n", type
, count
);
1058 static HRESULT
LoadIfdMetadata(IStream
*input
, const GUID
*preferred_vendor
,
1059 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
1062 MetadataItem
*result
;
1064 struct IFD_entry
*entry
;
1065 BOOL native_byte_order
= TRUE
;
1070 #ifdef WORDS_BIGENDIAN
1071 if (persist_options
& WICPersistOptionsLittleEndian
)
1073 if (persist_options
& WICPersistOptionsBigEndian
)
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
;
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
;
1090 HeapFree(GetProcessHeap(), 0, entry
);
1094 /* limit number of IFDs to 4096 to avoid infinite loop */
1095 for (i
= 0; i
< 4096; i
++)
1097 ULONG next_ifd_offset
;
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(), 0, count
* sizeof(*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
);
1141 HeapFree(GetProcessHeap(), 0, entry
);
1142 HeapFree(GetProcessHeap(), 0, result
);
1147 HeapFree(GetProcessHeap(), 0, entry
);
1150 *item_count
= count
;
1155 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl
= {
1157 &CLSID_WICIfdMetadataReader
,
1161 HRESULT
IfdMetadataReader_CreateInstance(REFIID iid
, void **ppv
)
1163 return MetadataReader_Create(&IfdMetadataReader_Vtbl
, iid
, ppv
);