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
, IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
461 MetadataHandler
*This
;
464 TRACE("%s\n", debugstr_guid(vtable
->clsid
));
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
;
476 This
->vtable
= vtable
;
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
);
490 typedef struct MetadataHandlerEnum
{
491 IWICEnumMetadataItem IWICEnumMetadataItem_iface
;
493 MetadataHandler
*parent
;
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
,
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
;
518 return E_NOINTERFACE
;
521 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
544 IWICMetadataWriter_Release(&This
->parent
->IWICMetadataWriter_iface
);
545 HeapFree(GetProcessHeap(), 0, This
);
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
);
560 TRACE("(%p,%i)\n", iface
, celt
);
562 EnterCriticalSection(&This
->parent
->lock
);
564 if (This
->index
>= This
->parent
->item_count
)
567 LeaveCriticalSection(&This
->parent
->lock
);
571 new_index
= min(This
->parent
->item_count
, This
->index
+ celt
);
572 *pceltFetched
= new_index
- This
->index
;
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
);
585 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
586 hr
= PropVariantCopy(&rgeltValue
[i
], &This
->parent
->items
[i
+This
->index
].value
);
591 This
->index
= new_index
;
594 LeaveCriticalSection(&This
->parent
->lock
);
599 static HRESULT WINAPI
MetadataHandlerEnum_Skip(IWICEnumMetadataItem
*iface
,
602 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
604 EnterCriticalSection(&This
->parent
->lock
);
608 LeaveCriticalSection(&This
->parent
->lock
);
613 static HRESULT WINAPI
MetadataHandlerEnum_Reset(IWICEnumMetadataItem
*iface
)
615 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
617 EnterCriticalSection(&This
->parent
->lock
);
621 LeaveCriticalSection(&This
->parent
->lock
);
626 static HRESULT WINAPI
MetadataHandlerEnum_Clone(IWICEnumMetadataItem
*iface
,
627 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
629 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
632 EnterCriticalSection(&This
->parent
->lock
);
634 hr
= MetadataHandlerEnum_Create(This
->parent
, This
->index
, ppIEnumMetadataItem
);
636 LeaveCriticalSection(&This
->parent
->lock
);
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
;
667 This
->parent
= parent
;
670 *ppIEnumMetadataItem
= &This
->IWICEnumMetadataItem_iface
;
675 static HRESULT
LoadUnknownMetadata(IStream
*input
, const GUID
*preferred_vendor
,
676 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
679 MetadataItem
*result
;
686 hr
= IStream_Stat(input
, &stat
, STATFLAG_NONAME
);
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
;
697 HeapFree(GetProcessHeap(), 0, data
);
701 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
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
;
722 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl
= {
724 &CLSID_WICUnknownMetadataReader
,
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)
749 #define IFD_RATIONAL 5
751 #define IFD_UNDEFINED 7
754 #define IFD_SRATIONAL 10
756 #define IFD_DOUBLE 12
759 static int tag_to_vt(SHORT tag
)
761 static const int tag2vt
[] =
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
;
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
;
798 item
->value
.vt
= tag_to_vt(type
);
799 value
= entry
->value
;
806 if (!count
) count
= 1;
810 const BYTE
*data
= (const BYTE
*)&entry
->value
;
813 item
->value
.u
.bVal
= data
[0];
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
);
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
);
833 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
836 hr
= IStream_Read(input
, item
->value
.u
.caub
.pElems
, count
, &bytesread
);
837 if (bytesread
!= count
) hr
= E_FAIL
;
840 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
846 if (!count
) count
= 1;
850 const SHORT
*data
= (const SHORT
*)&entry
->value
;
854 item
->value
.u
.uiVal
= data
[0];
855 SWAP_USHORT(item
->value
.u
.uiVal
);
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
]);
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
);
878 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
881 hr
= IStream_Read(input
, item
->value
.u
.caui
.pElems
, count
* 2, &bytesread
);
882 if (bytesread
!= count
* 2) hr
= E_FAIL
;
885 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
888 for (i
= 0; i
< count
; i
++)
889 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
894 if (!count
) count
= 1;
898 item
->value
.u
.ulVal
= value
;
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
);
911 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
914 hr
= IStream_Read(input
, item
->value
.u
.caul
.pElems
, count
* 4, &bytesread
);
915 if (bytesread
!= count
* 4) hr
= E_FAIL
;
918 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
921 for (i
= 0; i
< count
; i
++)
922 SWAP_ULONG(item
->value
.u
.caul
.pElems
[i
]);
929 FIXME("IFD field type %d, count 0\n", type
);
930 item
->value
.vt
= VT_EMPTY
;
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
);
952 SWAP_ULONG(item
->value
.u
.uhVal
.u
.LowPart
);
953 SWAP_ULONG(item
->value
.u
.uhVal
.u
.HighPart
);
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
);
968 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
971 hr
= IStream_Read(input
, item
->value
.u
.cauh
.pElems
, count
* 8, &bytesread
);
972 if (bytesread
!= count
* 8) hr
= E_FAIL
;
975 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
978 for (i
= 0; i
< count
; i
++)
980 if (type
== IFD_DOUBLE
)
981 SWAP_ULONGLONG(item
->value
.u
.cauh
.pElems
[i
].QuadPart
);
984 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.LowPart
);
985 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.HighPart
);
991 item
->value
.u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, count
+ 1);
992 if (!item
->value
.u
.pszVal
) return E_OUTOFMEMORY
;
996 const char *data
= (const char *)&entry
->value
;
997 memcpy(item
->value
.u
.pszVal
, data
, count
);
998 item
->value
.u
.pszVal
[count
] = 0;
1002 pos
.QuadPart
= value
;
1003 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1006 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
1009 hr
= IStream_Read(input
, item
->value
.u
.pszVal
, count
, &bytesread
);
1010 if (bytesread
!= count
) hr
= E_FAIL
;
1013 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
1016 item
->value
.u
.pszVal
[count
] = 0;
1021 FIXME("IFD field type %d, count 0\n", type
);
1022 item
->value
.vt
= VT_EMPTY
;
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
;
1033 const char *data
= (const char *)&entry
->value
;
1034 memcpy(item
->value
.u
.blob
.pBlobData
, data
, count
);
1038 pos
.QuadPart
= value
;
1039 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1042 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
1045 hr
= IStream_Read(input
, item
->value
.u
.blob
.pBlobData
, count
, &bytesread
);
1046 if (bytesread
!= count
) hr
= E_FAIL
;
1049 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
1054 FIXME("loading field of type %d, count %u is not implemented\n", type
, count
);
1060 static HRESULT
LoadIfdMetadata(IStream
*input
, const GUID
*preferred_vendor
,
1061 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
1064 MetadataItem
*result
;
1066 struct IFD_entry
*entry
;
1067 BOOL native_byte_order
= TRUE
;
1072 #ifdef WORDS_BIGENDIAN
1073 if (persist_options
& WICPersistOptionsLittleEndian
)
1075 if (persist_options
& WICPersistOptionsBigEndian
)
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
;
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
;
1092 HeapFree(GetProcessHeap(), 0, entry
);
1096 /* limit number of IFDs to 4096 to avoid infinite loop */
1097 for (i
= 0; i
< 4096; i
++)
1099 ULONG next_ifd_offset
;
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
));
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
);
1143 HeapFree(GetProcessHeap(), 0, entry
);
1144 HeapFree(GetProcessHeap(), 0, result
);
1149 HeapFree(GetProcessHeap(), 0, entry
);
1152 *item_count
= count
;
1157 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl
= {
1159 &CLSID_WICIfdMetadataReader
,
1163 HRESULT
IfdMetadataReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
1165 return MetadataReader_Create(&IfdMetadataReader_Vtbl
, pUnkOuter
, iid
, ppv
);