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
32 #include "propvarutil.h"
34 #include "wincodecs_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
40 typedef struct MetadataHandler
{
41 IWICMetadataWriter IWICMetadataWriter_iface
;
43 IWICPersistStream IWICPersistStream_iface
;
44 const MetadataHandlerVtbl
*vtable
;
47 CRITICAL_SECTION lock
;
50 static inline MetadataHandler
*impl_from_IWICMetadataWriter(IWICMetadataWriter
*iface
)
52 return CONTAINING_RECORD(iface
, MetadataHandler
, IWICMetadataWriter_iface
);
55 static inline MetadataHandler
*impl_from_IWICPersistStream(IWICPersistStream
*iface
)
57 return CONTAINING_RECORD(iface
, MetadataHandler
, IWICPersistStream_iface
);
60 static void MetadataHandler_FreeItems(MetadataHandler
*This
)
64 for (i
=0; i
<This
->item_count
; i
++)
66 PropVariantClear(&This
->items
[i
].schema
);
67 PropVariantClear(&This
->items
[i
].id
);
68 PropVariantClear(&This
->items
[i
].value
);
71 HeapFree(GetProcessHeap(), 0, This
->items
);
74 static HRESULT
MetadataHandlerEnum_Create(MetadataHandler
*parent
, DWORD index
,
75 IWICEnumMetadataItem
**ppIEnumMetadataItem
);
77 static HRESULT WINAPI
MetadataHandler_QueryInterface(IWICMetadataWriter
*iface
, REFIID iid
,
80 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
81 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
83 if (!ppv
) return E_INVALIDARG
;
85 if (IsEqualIID(&IID_IUnknown
, iid
) ||
86 IsEqualIID(&IID_IWICMetadataReader
, iid
) ||
87 (IsEqualIID(&IID_IWICMetadataWriter
, iid
) && This
->vtable
->is_writer
))
89 *ppv
= &This
->IWICMetadataWriter_iface
;
91 else if (IsEqualIID(&IID_IPersist
, iid
) ||
92 IsEqualIID(&IID_IPersistStream
, iid
) ||
93 IsEqualIID(&IID_IWICPersistStream
, iid
))
95 *ppv
= &This
->IWICPersistStream_iface
;
100 return E_NOINTERFACE
;
103 IUnknown_AddRef((IUnknown
*)*ppv
);
107 static ULONG WINAPI
MetadataHandler_AddRef(IWICMetadataWriter
*iface
)
109 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
110 ULONG ref
= InterlockedIncrement(&This
->ref
);
112 TRACE("(%p) refcount=%u\n", iface
, ref
);
117 static ULONG WINAPI
MetadataHandler_Release(IWICMetadataWriter
*iface
)
119 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
120 ULONG ref
= InterlockedDecrement(&This
->ref
);
122 TRACE("(%p) refcount=%u\n", iface
, ref
);
126 MetadataHandler_FreeItems(This
);
127 This
->lock
.DebugInfo
->Spare
[0] = 0;
128 DeleteCriticalSection(&This
->lock
);
129 HeapFree(GetProcessHeap(), 0, This
);
135 static HRESULT WINAPI
MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter
*iface
,
136 IWICMetadataHandlerInfo
**ppIHandler
)
139 IWICComponentInfo
*component_info
;
140 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
142 TRACE("%p,%p\n", iface
, ppIHandler
);
144 hr
= CreateComponentInfo(This
->vtable
->clsid
, &component_info
);
145 if (FAILED(hr
)) return hr
;
147 hr
= IWICComponentInfo_QueryInterface(component_info
, &IID_IWICMetadataHandlerInfo
,
148 (void **)ppIHandler
);
150 IWICComponentInfo_Release(component_info
);
154 static HRESULT WINAPI
MetadataHandler_GetMetadataFormat(IWICMetadataWriter
*iface
,
155 GUID
*pguidMetadataFormat
)
158 IWICMetadataHandlerInfo
*metadata_info
;
160 TRACE("%p,%p\n", iface
, pguidMetadataFormat
);
162 if (!pguidMetadataFormat
) return E_INVALIDARG
;
164 hr
= MetadataHandler_GetMetadataHandlerInfo(iface
, &metadata_info
);
165 if (FAILED(hr
)) return hr
;
167 hr
= IWICMetadataHandlerInfo_GetMetadataFormat(metadata_info
, pguidMetadataFormat
);
168 IWICMetadataHandlerInfo_Release(metadata_info
);
173 static HRESULT WINAPI
MetadataHandler_GetCount(IWICMetadataWriter
*iface
,
176 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
178 TRACE("%p,%p\n", iface
, pcCount
);
180 if (!pcCount
) return E_INVALIDARG
;
182 *pcCount
= This
->item_count
;
186 static HRESULT WINAPI
MetadataHandler_GetValueByIndex(IWICMetadataWriter
*iface
,
187 UINT index
, PROPVARIANT
*schema
, PROPVARIANT
*id
, PROPVARIANT
*value
)
190 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
192 TRACE("%p,%u,%p,%p,%p\n", iface
, index
, schema
, id
, value
);
194 EnterCriticalSection(&This
->lock
);
196 if (index
>= This
->item_count
)
198 LeaveCriticalSection(&This
->lock
);
203 hr
= PropVariantCopy(schema
, &This
->items
[index
].schema
);
205 if (SUCCEEDED(hr
) && id
)
206 hr
= PropVariantCopy(id
, &This
->items
[index
].id
);
208 if (SUCCEEDED(hr
) && value
)
209 hr
= PropVariantCopy(value
, &This
->items
[index
].value
);
211 LeaveCriticalSection(&This
->lock
);
215 static HRESULT WINAPI
MetadataHandler_GetValue(IWICMetadataWriter
*iface
,
216 const PROPVARIANT
*schema
, const PROPVARIANT
*id
, PROPVARIANT
*value
)
219 HRESULT hr
= WINCODEC_ERR_PROPERTYNOTFOUND
;
220 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
222 TRACE("(%p,%s,%s,%p)\n", iface
, wine_dbgstr_variant((const VARIANT
*)schema
), wine_dbgstr_variant((const VARIANT
*)id
), value
);
224 if (!id
) return E_INVALIDARG
;
226 EnterCriticalSection(&This
->lock
);
228 for (i
= 0; i
< This
->item_count
; i
++)
230 if (schema
&& This
->items
[i
].schema
.vt
!= VT_EMPTY
)
232 if (PropVariantCompareEx(schema
, &This
->items
[i
].schema
, 0, PVCF_USESTRCMPI
) != 0) continue;
235 if (PropVariantCompareEx(id
, &This
->items
[i
].id
, 0, PVCF_USESTRCMPI
) != 0) continue;
237 hr
= value
? PropVariantCopy(value
, &This
->items
[i
].value
) : S_OK
;
241 LeaveCriticalSection(&This
->lock
);
245 static HRESULT WINAPI
MetadataHandler_GetEnumerator(IWICMetadataWriter
*iface
,
246 IWICEnumMetadataItem
**ppIEnumMetadata
)
248 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
249 TRACE("(%p,%p)\n", iface
, ppIEnumMetadata
);
250 return MetadataHandlerEnum_Create(This
, 0, ppIEnumMetadata
);
253 static HRESULT WINAPI
MetadataHandler_SetValue(IWICMetadataWriter
*iface
,
254 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
256 FIXME("(%p,%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
, pvarValue
);
260 static HRESULT WINAPI
MetadataHandler_SetValueByIndex(IWICMetadataWriter
*iface
,
261 UINT nIndex
, const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
263 FIXME("(%p,%u,%p,%p,%p): stub\n", iface
, nIndex
, pvarSchema
, pvarId
, pvarValue
);
267 static HRESULT WINAPI
MetadataHandler_RemoveValue(IWICMetadataWriter
*iface
,
268 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
)
270 FIXME("(%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
);
274 static HRESULT WINAPI
MetadataHandler_RemoveValueByIndex(IWICMetadataWriter
*iface
,
277 FIXME("(%p,%u): stub\n", iface
, nIndex
);
281 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl
= {
282 MetadataHandler_QueryInterface
,
283 MetadataHandler_AddRef
,
284 MetadataHandler_Release
,
285 MetadataHandler_GetMetadataFormat
,
286 MetadataHandler_GetMetadataHandlerInfo
,
287 MetadataHandler_GetCount
,
288 MetadataHandler_GetValueByIndex
,
289 MetadataHandler_GetValue
,
290 MetadataHandler_GetEnumerator
,
291 MetadataHandler_SetValue
,
292 MetadataHandler_SetValueByIndex
,
293 MetadataHandler_RemoveValue
,
294 MetadataHandler_RemoveValueByIndex
297 static HRESULT WINAPI
MetadataHandler_PersistStream_QueryInterface(IWICPersistStream
*iface
,
298 REFIID iid
, void **ppv
)
300 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
301 return IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
304 static ULONG WINAPI
MetadataHandler_PersistStream_AddRef(IWICPersistStream
*iface
)
306 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
307 return IWICMetadataWriter_AddRef(&This
->IWICMetadataWriter_iface
);
310 static ULONG WINAPI
MetadataHandler_PersistStream_Release(IWICPersistStream
*iface
)
312 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
313 return IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
316 static HRESULT WINAPI
MetadataHandler_GetClassID(IWICPersistStream
*iface
,
319 FIXME("(%p,%p): stub\n", iface
, pClassID
);
323 static HRESULT WINAPI
MetadataHandler_IsDirty(IWICPersistStream
*iface
)
325 FIXME("(%p): stub\n", iface
);
329 static HRESULT WINAPI
MetadataHandler_Load(IWICPersistStream
*iface
,
332 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
333 TRACE("(%p,%p)\n", iface
, pStm
);
334 return IWICPersistStream_LoadEx(&This
->IWICPersistStream_iface
, pStm
, NULL
, WICPersistOptionDefault
);
337 static HRESULT WINAPI
MetadataHandler_Save(IWICPersistStream
*iface
,
338 IStream
*pStm
, BOOL fClearDirty
)
340 FIXME("(%p,%p,%i): stub\n", iface
, pStm
, fClearDirty
);
344 static HRESULT WINAPI
MetadataHandler_GetSizeMax(IWICPersistStream
*iface
,
345 ULARGE_INTEGER
*pcbSize
)
347 FIXME("(%p,%p): stub\n", iface
, pcbSize
);
351 static HRESULT WINAPI
MetadataHandler_LoadEx(IWICPersistStream
*iface
,
352 IStream
*pIStream
, const GUID
*pguidPreferredVendor
, DWORD dwPersistOptions
)
354 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
356 MetadataItem
*new_items
=NULL
;
359 TRACE("(%p,%p,%s,%x)\n", iface
, pIStream
, debugstr_guid(pguidPreferredVendor
), dwPersistOptions
);
361 EnterCriticalSection(&This
->lock
);
363 hr
= This
->vtable
->fnLoad(pIStream
, pguidPreferredVendor
, dwPersistOptions
,
364 &new_items
, &item_count
);
368 MetadataHandler_FreeItems(This
);
369 This
->items
= new_items
;
370 This
->item_count
= item_count
;
373 LeaveCriticalSection(&This
->lock
);
378 static HRESULT WINAPI
MetadataHandler_SaveEx(IWICPersistStream
*iface
,
379 IStream
*pIStream
, DWORD dwPersistOptions
, BOOL fClearDirty
)
381 FIXME("(%p,%p,%x,%i): stub\n", iface
, pIStream
, dwPersistOptions
, fClearDirty
);
385 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl
= {
386 MetadataHandler_PersistStream_QueryInterface
,
387 MetadataHandler_PersistStream_AddRef
,
388 MetadataHandler_PersistStream_Release
,
389 MetadataHandler_GetClassID
,
390 MetadataHandler_IsDirty
,
391 MetadataHandler_Load
,
392 MetadataHandler_Save
,
393 MetadataHandler_GetSizeMax
,
394 MetadataHandler_LoadEx
,
395 MetadataHandler_SaveEx
398 HRESULT
MetadataReader_Create(const MetadataHandlerVtbl
*vtable
, REFIID iid
, void** ppv
)
400 MetadataHandler
*This
;
403 TRACE("%s\n", debugstr_guid(vtable
->clsid
));
407 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler
));
408 if (!This
) return E_OUTOFMEMORY
;
410 This
->IWICMetadataWriter_iface
.lpVtbl
= &MetadataHandler_Vtbl
;
411 This
->IWICPersistStream_iface
.lpVtbl
= &MetadataHandler_PersistStream_Vtbl
;
413 This
->vtable
= vtable
;
415 This
->item_count
= 0;
417 InitializeCriticalSection(&This
->lock
);
418 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MetadataHandler.lock");
420 hr
= IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
422 IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
427 typedef struct MetadataHandlerEnum
{
428 IWICEnumMetadataItem IWICEnumMetadataItem_iface
;
430 MetadataHandler
*parent
;
432 } MetadataHandlerEnum
;
434 static inline MetadataHandlerEnum
*impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem
*iface
)
436 return CONTAINING_RECORD(iface
, MetadataHandlerEnum
, IWICEnumMetadataItem_iface
);
439 static HRESULT WINAPI
MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem
*iface
, REFIID iid
,
442 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
443 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
445 if (!ppv
) return E_INVALIDARG
;
447 if (IsEqualIID(&IID_IUnknown
, iid
) ||
448 IsEqualIID(&IID_IWICEnumMetadataItem
, iid
))
450 *ppv
= &This
->IWICEnumMetadataItem_iface
;
455 return E_NOINTERFACE
;
458 IUnknown_AddRef((IUnknown
*)*ppv
);
462 static ULONG WINAPI
MetadataHandlerEnum_AddRef(IWICEnumMetadataItem
*iface
)
464 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
465 ULONG ref
= InterlockedIncrement(&This
->ref
);
467 TRACE("(%p) refcount=%u\n", iface
, ref
);
472 static ULONG WINAPI
MetadataHandlerEnum_Release(IWICEnumMetadataItem
*iface
)
474 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
475 ULONG ref
= InterlockedDecrement(&This
->ref
);
477 TRACE("(%p) refcount=%u\n", iface
, ref
);
481 IWICMetadataWriter_Release(&This
->parent
->IWICMetadataWriter_iface
);
482 HeapFree(GetProcessHeap(), 0, This
);
488 static HRESULT WINAPI
MetadataHandlerEnum_Next(IWICEnumMetadataItem
*iface
,
489 ULONG celt
, PROPVARIANT
*rgeltSchema
, PROPVARIANT
*rgeltId
,
490 PROPVARIANT
*rgeltValue
, ULONG
*pceltFetched
)
492 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
497 TRACE("(%p,%i)\n", iface
, celt
);
499 EnterCriticalSection(&This
->parent
->lock
);
501 if (This
->index
>= This
->parent
->item_count
)
504 LeaveCriticalSection(&This
->parent
->lock
);
508 new_index
= min(This
->parent
->item_count
, This
->index
+ celt
);
509 *pceltFetched
= new_index
- This
->index
;
513 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
514 hr
= PropVariantCopy(&rgeltSchema
[i
], &This
->parent
->items
[i
+This
->index
].schema
);
517 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
518 hr
= PropVariantCopy(&rgeltId
[i
], &This
->parent
->items
[i
+This
->index
].id
);
522 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
523 hr
= PropVariantCopy(&rgeltValue
[i
], &This
->parent
->items
[i
+This
->index
].value
);
528 This
->index
= new_index
;
531 LeaveCriticalSection(&This
->parent
->lock
);
536 static HRESULT WINAPI
MetadataHandlerEnum_Skip(IWICEnumMetadataItem
*iface
,
539 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
541 EnterCriticalSection(&This
->parent
->lock
);
545 LeaveCriticalSection(&This
->parent
->lock
);
550 static HRESULT WINAPI
MetadataHandlerEnum_Reset(IWICEnumMetadataItem
*iface
)
552 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
554 EnterCriticalSection(&This
->parent
->lock
);
558 LeaveCriticalSection(&This
->parent
->lock
);
563 static HRESULT WINAPI
MetadataHandlerEnum_Clone(IWICEnumMetadataItem
*iface
,
564 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
566 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
569 EnterCriticalSection(&This
->parent
->lock
);
571 hr
= MetadataHandlerEnum_Create(This
->parent
, This
->index
, ppIEnumMetadataItem
);
573 LeaveCriticalSection(&This
->parent
->lock
);
578 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl
= {
579 MetadataHandlerEnum_QueryInterface
,
580 MetadataHandlerEnum_AddRef
,
581 MetadataHandlerEnum_Release
,
582 MetadataHandlerEnum_Next
,
583 MetadataHandlerEnum_Skip
,
584 MetadataHandlerEnum_Reset
,
585 MetadataHandlerEnum_Clone
588 static HRESULT
MetadataHandlerEnum_Create(MetadataHandler
*parent
, DWORD index
,
589 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
591 MetadataHandlerEnum
*This
;
593 if (!ppIEnumMetadataItem
) return E_INVALIDARG
;
595 *ppIEnumMetadataItem
= NULL
;
597 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum
));
598 if (!This
) return E_OUTOFMEMORY
;
600 IWICMetadataWriter_AddRef(&parent
->IWICMetadataWriter_iface
);
602 This
->IWICEnumMetadataItem_iface
.lpVtbl
= &MetadataHandlerEnum_Vtbl
;
604 This
->parent
= parent
;
607 *ppIEnumMetadataItem
= &This
->IWICEnumMetadataItem_iface
;
612 static HRESULT
LoadUnknownMetadata(IStream
*input
, const GUID
*preferred_vendor
,
613 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
616 MetadataItem
*result
;
623 hr
= IStream_Stat(input
, &stat
, STATFLAG_NONAME
);
627 data
= HeapAlloc(GetProcessHeap(), 0, stat
.cbSize
.QuadPart
);
628 if (!data
) return E_OUTOFMEMORY
;
630 hr
= IStream_Read(input
, data
, stat
.cbSize
.QuadPart
, &bytesread
);
631 if (bytesread
!= stat
.cbSize
.QuadPart
) hr
= E_FAIL
;
634 HeapFree(GetProcessHeap(), 0, data
);
638 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MetadataItem
));
641 HeapFree(GetProcessHeap(), 0, data
);
642 return E_OUTOFMEMORY
;
645 PropVariantInit(&result
[0].schema
);
646 PropVariantInit(&result
[0].id
);
647 PropVariantInit(&result
[0].value
);
649 result
[0].value
.vt
= VT_BLOB
;
650 result
[0].value
.u
.blob
.cbSize
= bytesread
;
651 result
[0].value
.u
.blob
.pBlobData
= data
;
659 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl
= {
661 &CLSID_WICUnknownMetadataReader
,
665 HRESULT
UnknownMetadataReader_CreateInstance(REFIID iid
, void** ppv
)
667 return MetadataReader_Create(&UnknownMetadataReader_Vtbl
, iid
, ppv
);
670 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
671 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
672 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
686 #define IFD_RATIONAL 5
688 #define IFD_UNDEFINED 7
691 #define IFD_SRATIONAL 10
693 #define IFD_DOUBLE 12
696 static int tag_to_vt(SHORT tag
)
698 static const int tag2vt
[] =
701 VT_UI1
, /* IFD_BYTE 1 */
702 VT_LPSTR
, /* IFD_ASCII 2 */
703 VT_UI2
, /* IFD_SHORT 3 */
704 VT_UI4
, /* IFD_LONG 4 */
705 VT_UI8
, /* IFD_RATIONAL 5 */
706 VT_I1
, /* IFD_SBYTE 6 */
707 VT_BLOB
, /* IFD_UNDEFINED 7 */
708 VT_I2
, /* IFD_SSHORT 8 */
709 VT_I4
, /* IFD_SLONG 9 */
710 VT_I8
, /* IFD_SRATIONAL 10 */
711 VT_R4
, /* IFD_FLOAT 11 */
712 VT_R8
, /* IFD_DOUBLE 12 */
713 VT_BLOB
, /* IFD_IFD 13 */
715 return (tag
> 0 && tag
<= 13) ? tag2vt
[tag
] : VT_BLOB
;
718 static HRESULT
load_IFD_entry(IStream
*input
, const struct IFD_entry
*entry
,
719 MetadataItem
*item
, BOOL native_byte_order
)
721 ULONG count
, value
, i
, bytesread
;
726 item
->schema
.vt
= VT_EMPTY
;
727 item
->id
.vt
= VT_UI2
;
728 item
->id
.u
.uiVal
= entry
->id
;
729 SWAP_USHORT(item
->id
.u
.uiVal
);
731 count
= entry
->count
;
735 item
->value
.vt
= tag_to_vt(type
);
736 value
= entry
->value
;
743 if (!count
) count
= 1;
747 const BYTE
*data
= (const BYTE
*)&entry
->value
;
750 item
->value
.u
.bVal
= data
[0];
753 item
->value
.vt
|= VT_VECTOR
;
754 item
->value
.u
.caub
.cElems
= count
;
755 item
->value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
756 memcpy(item
->value
.u
.caub
.pElems
, data
, count
);
761 item
->value
.vt
|= VT_VECTOR
;
762 item
->value
.u
.caub
.cElems
= count
;
763 item
->value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
764 if (!item
->value
.u
.caub
.pElems
) return E_OUTOFMEMORY
;
766 pos
.QuadPart
= value
;
767 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
770 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
773 hr
= IStream_Read(input
, item
->value
.u
.caub
.pElems
, count
, &bytesread
);
774 if (bytesread
!= count
) hr
= E_FAIL
;
777 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
783 if (!count
) count
= 1;
787 const SHORT
*data
= (const SHORT
*)&entry
->value
;
791 item
->value
.u
.uiVal
= data
[0];
792 SWAP_USHORT(item
->value
.u
.uiVal
);
796 item
->value
.vt
|= VT_VECTOR
;
797 item
->value
.u
.caui
.cElems
= count
;
798 item
->value
.u
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
799 memcpy(item
->value
.u
.caui
.pElems
, data
, count
* 2);
800 for (i
= 0; i
< count
; i
++)
801 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
806 item
->value
.vt
|= VT_VECTOR
;
807 item
->value
.u
.caui
.cElems
= count
;
808 item
->value
.u
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
809 if (!item
->value
.u
.caui
.pElems
) return E_OUTOFMEMORY
;
811 pos
.QuadPart
= value
;
812 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
815 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
818 hr
= IStream_Read(input
, item
->value
.u
.caui
.pElems
, count
* 2, &bytesread
);
819 if (bytesread
!= count
* 2) hr
= E_FAIL
;
822 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
825 for (i
= 0; i
< count
; i
++)
826 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
831 if (!count
) count
= 1;
835 item
->value
.u
.ulVal
= value
;
839 item
->value
.vt
|= VT_VECTOR
;
840 item
->value
.u
.caul
.cElems
= count
;
841 item
->value
.u
.caul
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 4);
842 if (!item
->value
.u
.caul
.pElems
) return E_OUTOFMEMORY
;
844 pos
.QuadPart
= value
;
845 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
848 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
851 hr
= IStream_Read(input
, item
->value
.u
.caul
.pElems
, count
* 4, &bytesread
);
852 if (bytesread
!= count
* 4) hr
= E_FAIL
;
855 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
858 for (i
= 0; i
< count
; i
++)
859 SWAP_ULONG(item
->value
.u
.caul
.pElems
[i
]);
866 FIXME("IFD field type %d, count 0\n", type
);
867 item
->value
.vt
= VT_EMPTY
;
875 pos
.QuadPart
= value
;
876 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
877 if (FAILED(hr
)) return hr
;
879 hr
= IStream_Read(input
, &ull
, sizeof(ull
), &bytesread
);
880 if (bytesread
!= sizeof(ull
)) hr
= E_FAIL
;
881 if (hr
!= S_OK
) return hr
;
883 item
->value
.u
.uhVal
.QuadPart
= ull
;
885 if (type
== IFD_DOUBLE
)
886 SWAP_ULONGLONG(item
->value
.u
.uhVal
.QuadPart
);
889 SWAP_ULONG(item
->value
.u
.uhVal
.u
.LowPart
);
890 SWAP_ULONG(item
->value
.u
.uhVal
.u
.HighPart
);
896 item
->value
.vt
|= VT_VECTOR
;
897 item
->value
.u
.cauh
.cElems
= count
;
898 item
->value
.u
.cauh
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 8);
899 if (!item
->value
.u
.cauh
.pElems
) return E_OUTOFMEMORY
;
901 pos
.QuadPart
= value
;
902 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
905 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
908 hr
= IStream_Read(input
, item
->value
.u
.cauh
.pElems
, count
* 8, &bytesread
);
909 if (bytesread
!= count
* 8) hr
= E_FAIL
;
912 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
915 for (i
= 0; i
< count
; i
++)
917 if (type
== IFD_DOUBLE
)
918 SWAP_ULONGLONG(item
->value
.u
.cauh
.pElems
[i
].QuadPart
);
921 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.LowPart
);
922 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.HighPart
);
928 item
->value
.u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, count
+ 1);
929 if (!item
->value
.u
.pszVal
) return E_OUTOFMEMORY
;
933 const char *data
= (const char *)&entry
->value
;
934 memcpy(item
->value
.u
.pszVal
, data
, count
);
935 item
->value
.u
.pszVal
[count
] = 0;
939 pos
.QuadPart
= value
;
940 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
943 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
946 hr
= IStream_Read(input
, item
->value
.u
.pszVal
, count
, &bytesread
);
947 if (bytesread
!= count
) hr
= E_FAIL
;
950 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
953 item
->value
.u
.pszVal
[count
] = 0;
958 FIXME("IFD field type %d, count 0\n", type
);
959 item
->value
.vt
= VT_EMPTY
;
963 item
->value
.u
.blob
.pBlobData
= HeapAlloc(GetProcessHeap(), 0, count
);
964 if (!item
->value
.u
.blob
.pBlobData
) return E_OUTOFMEMORY
;
966 item
->value
.u
.blob
.cbSize
= count
;
970 const char *data
= (const char *)&entry
->value
;
971 memcpy(item
->value
.u
.blob
.pBlobData
, data
, count
);
975 pos
.QuadPart
= value
;
976 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
979 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
982 hr
= IStream_Read(input
, item
->value
.u
.blob
.pBlobData
, count
, &bytesread
);
983 if (bytesread
!= count
) hr
= E_FAIL
;
986 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
991 FIXME("loading field of type %d, count %u is not implemented\n", type
, count
);
997 static HRESULT
LoadIfdMetadata(IStream
*input
, const GUID
*preferred_vendor
,
998 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
1001 MetadataItem
*result
;
1003 struct IFD_entry
*entry
;
1004 BOOL native_byte_order
= TRUE
;
1009 #ifdef WORDS_BIGENDIAN
1010 if (persist_options
& WICPersistOptionLittleEndian
)
1012 if (persist_options
& WICPersistOptionBigEndian
)
1014 native_byte_order
= FALSE
;
1016 hr
= IStream_Read(input
, &count
, sizeof(count
), &bytesread
);
1017 if (bytesread
!= sizeof(count
)) hr
= E_FAIL
;
1018 if (hr
!= S_OK
) return hr
;
1022 entry
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(*entry
));
1023 if (!entry
) return E_OUTOFMEMORY
;
1025 hr
= IStream_Read(input
, entry
, count
* sizeof(*entry
), &bytesread
);
1026 if (bytesread
!= count
* sizeof(*entry
)) hr
= E_FAIL
;
1029 HeapFree(GetProcessHeap(), 0, entry
);
1033 /* limit number of IFDs to 4096 to avoid infinite loop */
1034 for (i
= 0; i
< 4096; i
++)
1036 ULONG next_ifd_offset
;
1038 USHORT next_ifd_count
;
1040 hr
= IStream_Read(input
, &next_ifd_offset
, sizeof(next_ifd_offset
), &bytesread
);
1041 if (bytesread
!= sizeof(next_ifd_offset
)) hr
= E_FAIL
;
1042 if (hr
!= S_OK
) break;
1044 SWAP_ULONG(next_ifd_offset
);
1045 if (!next_ifd_offset
) break;
1047 pos
.QuadPart
= next_ifd_offset
;
1048 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1049 if (FAILED(hr
)) break;
1051 hr
= IStream_Read(input
, &next_ifd_count
, sizeof(next_ifd_count
), &bytesread
);
1052 if (bytesread
!= sizeof(next_ifd_count
)) hr
= E_FAIL
;
1053 if (hr
!= S_OK
) break;
1055 SWAP_USHORT(next_ifd_count
);
1057 pos
.QuadPart
= next_ifd_count
* sizeof(*entry
);
1058 hr
= IStream_Seek(input
, pos
, SEEK_CUR
, NULL
);
1059 if (FAILED(hr
)) break;
1062 if (hr
!= S_OK
|| i
== 4096)
1064 HeapFree(GetProcessHeap(), 0, entry
);
1065 return WINCODEC_ERR_BADMETADATAHEADER
;
1068 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, count
* sizeof(*result
));
1071 HeapFree(GetProcessHeap(), 0, entry
);
1072 return E_OUTOFMEMORY
;
1075 for (i
= 0; i
< count
; i
++)
1077 hr
= load_IFD_entry(input
, &entry
[i
], &result
[i
], native_byte_order
);
1080 HeapFree(GetProcessHeap(), 0, entry
);
1081 HeapFree(GetProcessHeap(), 0, result
);
1086 HeapFree(GetProcessHeap(), 0, entry
);
1089 *item_count
= count
;
1094 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl
= {
1096 &CLSID_WICIfdMetadataReader
,
1100 HRESULT
IfdMetadataReader_CreateInstance(REFIID iid
, void **ppv
)
1102 return MetadataReader_Create(&IfdMetadataReader_Vtbl
, iid
, ppv
);