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
24 #define NONAMELESSUNION
30 #include "propvarutil.h"
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
38 typedef struct MetadataHandler
{
39 IWICMetadataWriter IWICMetadataWriter_iface
;
41 IWICPersistStream IWICPersistStream_iface
;
42 const MetadataHandlerVtbl
*vtable
;
45 CRITICAL_SECTION lock
;
48 static inline MetadataHandler
*impl_from_IWICMetadataWriter(IWICMetadataWriter
*iface
)
50 return CONTAINING_RECORD(iface
, MetadataHandler
, IWICMetadataWriter_iface
);
53 static inline MetadataHandler
*impl_from_IWICPersistStream(IWICPersistStream
*iface
)
55 return CONTAINING_RECORD(iface
, MetadataHandler
, IWICPersistStream_iface
);
58 static void MetadataHandler_FreeItems(MetadataHandler
*This
)
62 for (i
=0; i
<This
->item_count
; i
++)
64 PropVariantClear(&This
->items
[i
].schema
);
65 PropVariantClear(&This
->items
[i
].id
);
66 PropVariantClear(&This
->items
[i
].value
);
69 HeapFree(GetProcessHeap(), 0, This
->items
);
72 static HRESULT
MetadataHandlerEnum_Create(MetadataHandler
*parent
, DWORD index
,
73 IWICEnumMetadataItem
**ppIEnumMetadataItem
);
75 static HRESULT WINAPI
MetadataHandler_QueryInterface(IWICMetadataWriter
*iface
, REFIID iid
,
78 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
79 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
81 if (!ppv
) return E_INVALIDARG
;
83 if (IsEqualIID(&IID_IUnknown
, iid
) ||
84 IsEqualIID(&IID_IWICMetadataReader
, iid
) ||
85 (IsEqualIID(&IID_IWICMetadataWriter
, iid
) && This
->vtable
->is_writer
))
87 *ppv
= &This
->IWICMetadataWriter_iface
;
89 else if (IsEqualIID(&IID_IPersist
, iid
) ||
90 IsEqualIID(&IID_IPersistStream
, iid
) ||
91 IsEqualIID(&IID_IWICPersistStream
, iid
))
93 *ppv
= &This
->IWICPersistStream_iface
;
101 IUnknown_AddRef((IUnknown
*)*ppv
);
105 static ULONG WINAPI
MetadataHandler_AddRef(IWICMetadataWriter
*iface
)
107 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
108 ULONG ref
= InterlockedIncrement(&This
->ref
);
110 TRACE("(%p) refcount=%u\n", iface
, ref
);
115 static ULONG WINAPI
MetadataHandler_Release(IWICMetadataWriter
*iface
)
117 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
118 ULONG ref
= InterlockedDecrement(&This
->ref
);
120 TRACE("(%p) refcount=%u\n", iface
, ref
);
124 MetadataHandler_FreeItems(This
);
125 This
->lock
.DebugInfo
->Spare
[0] = 0;
126 DeleteCriticalSection(&This
->lock
);
127 HeapFree(GetProcessHeap(), 0, This
);
133 static HRESULT WINAPI
MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter
*iface
,
134 IWICMetadataHandlerInfo
**ppIHandler
)
137 IWICComponentInfo
*component_info
;
138 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
140 TRACE("%p,%p\n", iface
, ppIHandler
);
142 hr
= CreateComponentInfo(This
->vtable
->clsid
, &component_info
);
143 if (FAILED(hr
)) return hr
;
145 hr
= IWICComponentInfo_QueryInterface(component_info
, &IID_IWICMetadataHandlerInfo
,
146 (void **)ppIHandler
);
148 IWICComponentInfo_Release(component_info
);
152 static HRESULT WINAPI
MetadataHandler_GetMetadataFormat(IWICMetadataWriter
*iface
,
153 GUID
*pguidMetadataFormat
)
156 IWICMetadataHandlerInfo
*metadata_info
;
158 TRACE("%p,%p\n", iface
, pguidMetadataFormat
);
160 if (!pguidMetadataFormat
) return E_INVALIDARG
;
162 hr
= MetadataHandler_GetMetadataHandlerInfo(iface
, &metadata_info
);
163 if (FAILED(hr
)) return hr
;
165 hr
= IWICMetadataHandlerInfo_GetMetadataFormat(metadata_info
, pguidMetadataFormat
);
166 IWICMetadataHandlerInfo_Release(metadata_info
);
171 static HRESULT WINAPI
MetadataHandler_GetCount(IWICMetadataWriter
*iface
,
174 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
176 TRACE("%p,%p\n", iface
, pcCount
);
178 if (!pcCount
) return E_INVALIDARG
;
180 *pcCount
= This
->item_count
;
184 static HRESULT WINAPI
MetadataHandler_GetValueByIndex(IWICMetadataWriter
*iface
,
185 UINT index
, PROPVARIANT
*schema
, PROPVARIANT
*id
, PROPVARIANT
*value
)
188 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
190 TRACE("%p,%u,%p,%p,%p\n", iface
, index
, schema
, id
, value
);
192 EnterCriticalSection(&This
->lock
);
194 if (index
>= This
->item_count
)
196 LeaveCriticalSection(&This
->lock
);
201 hr
= PropVariantCopy(schema
, &This
->items
[index
].schema
);
203 if (SUCCEEDED(hr
) && id
)
204 hr
= PropVariantCopy(id
, &This
->items
[index
].id
);
206 if (SUCCEEDED(hr
) && value
)
207 hr
= PropVariantCopy(value
, &This
->items
[index
].value
);
209 LeaveCriticalSection(&This
->lock
);
213 static HRESULT WINAPI
MetadataHandler_GetValue(IWICMetadataWriter
*iface
,
214 const PROPVARIANT
*schema
, const PROPVARIANT
*id
, PROPVARIANT
*value
)
217 HRESULT hr
= WINCODEC_ERR_PROPERTYNOTFOUND
;
218 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
220 TRACE("(%p,%s,%s,%p)\n", iface
, wine_dbgstr_variant((const VARIANT
*)schema
), wine_dbgstr_variant((const VARIANT
*)id
), value
);
222 if (!id
) return E_INVALIDARG
;
224 EnterCriticalSection(&This
->lock
);
226 for (i
= 0; i
< This
->item_count
; i
++)
228 if (schema
&& This
->items
[i
].schema
.vt
!= VT_EMPTY
)
230 if (PropVariantCompareEx(schema
, &This
->items
[i
].schema
, 0, PVCF_USESTRCMPI
) != 0) continue;
233 if (PropVariantCompareEx(id
, &This
->items
[i
].id
, 0, PVCF_USESTRCMPI
) != 0) continue;
235 hr
= value
? PropVariantCopy(value
, &This
->items
[i
].value
) : S_OK
;
239 LeaveCriticalSection(&This
->lock
);
243 static HRESULT WINAPI
MetadataHandler_GetEnumerator(IWICMetadataWriter
*iface
,
244 IWICEnumMetadataItem
**ppIEnumMetadata
)
246 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
247 TRACE("(%p,%p)\n", iface
, ppIEnumMetadata
);
248 return MetadataHandlerEnum_Create(This
, 0, ppIEnumMetadata
);
251 static HRESULT WINAPI
MetadataHandler_SetValue(IWICMetadataWriter
*iface
,
252 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
254 FIXME("(%p,%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
, pvarValue
);
258 static HRESULT WINAPI
MetadataHandler_SetValueByIndex(IWICMetadataWriter
*iface
,
259 UINT nIndex
, const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
261 FIXME("(%p,%u,%p,%p,%p): stub\n", iface
, nIndex
, pvarSchema
, pvarId
, pvarValue
);
265 static HRESULT WINAPI
MetadataHandler_RemoveValue(IWICMetadataWriter
*iface
,
266 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
)
268 FIXME("(%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
);
272 static HRESULT WINAPI
MetadataHandler_RemoveValueByIndex(IWICMetadataWriter
*iface
,
275 FIXME("(%p,%u): stub\n", iface
, nIndex
);
279 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl
= {
280 MetadataHandler_QueryInterface
,
281 MetadataHandler_AddRef
,
282 MetadataHandler_Release
,
283 MetadataHandler_GetMetadataFormat
,
284 MetadataHandler_GetMetadataHandlerInfo
,
285 MetadataHandler_GetCount
,
286 MetadataHandler_GetValueByIndex
,
287 MetadataHandler_GetValue
,
288 MetadataHandler_GetEnumerator
,
289 MetadataHandler_SetValue
,
290 MetadataHandler_SetValueByIndex
,
291 MetadataHandler_RemoveValue
,
292 MetadataHandler_RemoveValueByIndex
295 static HRESULT WINAPI
MetadataHandler_PersistStream_QueryInterface(IWICPersistStream
*iface
,
296 REFIID iid
, void **ppv
)
298 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
299 return IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
302 static ULONG WINAPI
MetadataHandler_PersistStream_AddRef(IWICPersistStream
*iface
)
304 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
305 return IWICMetadataWriter_AddRef(&This
->IWICMetadataWriter_iface
);
308 static ULONG WINAPI
MetadataHandler_PersistStream_Release(IWICPersistStream
*iface
)
310 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
311 return IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
314 static HRESULT WINAPI
MetadataHandler_GetClassID(IWICPersistStream
*iface
,
317 FIXME("(%p,%p): stub\n", iface
, pClassID
);
321 static HRESULT WINAPI
MetadataHandler_IsDirty(IWICPersistStream
*iface
)
323 FIXME("(%p): stub\n", iface
);
327 static HRESULT WINAPI
MetadataHandler_Load(IWICPersistStream
*iface
,
330 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
331 TRACE("(%p,%p)\n", iface
, pStm
);
332 return IWICPersistStream_LoadEx(&This
->IWICPersistStream_iface
, pStm
, NULL
, WICPersistOptionDefault
);
335 static HRESULT WINAPI
MetadataHandler_Save(IWICPersistStream
*iface
,
336 IStream
*pStm
, BOOL fClearDirty
)
338 FIXME("(%p,%p,%i): stub\n", iface
, pStm
, fClearDirty
);
342 static HRESULT WINAPI
MetadataHandler_GetSizeMax(IWICPersistStream
*iface
,
343 ULARGE_INTEGER
*pcbSize
)
345 FIXME("(%p,%p): stub\n", iface
, pcbSize
);
349 static HRESULT WINAPI
MetadataHandler_LoadEx(IWICPersistStream
*iface
,
350 IStream
*pIStream
, const GUID
*pguidPreferredVendor
, DWORD dwPersistOptions
)
352 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
354 MetadataItem
*new_items
=NULL
;
357 TRACE("(%p,%p,%s,%x)\n", iface
, pIStream
, debugstr_guid(pguidPreferredVendor
), dwPersistOptions
);
359 EnterCriticalSection(&This
->lock
);
361 hr
= This
->vtable
->fnLoad(pIStream
, pguidPreferredVendor
, dwPersistOptions
,
362 &new_items
, &item_count
);
366 MetadataHandler_FreeItems(This
);
367 This
->items
= new_items
;
368 This
->item_count
= item_count
;
371 LeaveCriticalSection(&This
->lock
);
376 static HRESULT WINAPI
MetadataHandler_SaveEx(IWICPersistStream
*iface
,
377 IStream
*pIStream
, DWORD dwPersistOptions
, BOOL fClearDirty
)
379 FIXME("(%p,%p,%x,%i): stub\n", iface
, pIStream
, dwPersistOptions
, fClearDirty
);
383 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl
= {
384 MetadataHandler_PersistStream_QueryInterface
,
385 MetadataHandler_PersistStream_AddRef
,
386 MetadataHandler_PersistStream_Release
,
387 MetadataHandler_GetClassID
,
388 MetadataHandler_IsDirty
,
389 MetadataHandler_Load
,
390 MetadataHandler_Save
,
391 MetadataHandler_GetSizeMax
,
392 MetadataHandler_LoadEx
,
393 MetadataHandler_SaveEx
396 HRESULT
MetadataReader_Create(const MetadataHandlerVtbl
*vtable
, REFIID iid
, void** ppv
)
398 MetadataHandler
*This
;
401 TRACE("%s\n", debugstr_guid(vtable
->clsid
));
405 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler
));
406 if (!This
) return E_OUTOFMEMORY
;
408 This
->IWICMetadataWriter_iface
.lpVtbl
= &MetadataHandler_Vtbl
;
409 This
->IWICPersistStream_iface
.lpVtbl
= &MetadataHandler_PersistStream_Vtbl
;
411 This
->vtable
= vtable
;
413 This
->item_count
= 0;
415 InitializeCriticalSection(&This
->lock
);
416 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MetadataHandler.lock");
418 hr
= IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
420 IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
425 typedef struct MetadataHandlerEnum
{
426 IWICEnumMetadataItem IWICEnumMetadataItem_iface
;
428 MetadataHandler
*parent
;
430 } MetadataHandlerEnum
;
432 static inline MetadataHandlerEnum
*impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem
*iface
)
434 return CONTAINING_RECORD(iface
, MetadataHandlerEnum
, IWICEnumMetadataItem_iface
);
437 static HRESULT WINAPI
MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem
*iface
, REFIID iid
,
440 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
441 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
443 if (!ppv
) return E_INVALIDARG
;
445 if (IsEqualIID(&IID_IUnknown
, iid
) ||
446 IsEqualIID(&IID_IWICEnumMetadataItem
, iid
))
448 *ppv
= &This
->IWICEnumMetadataItem_iface
;
453 return E_NOINTERFACE
;
456 IUnknown_AddRef((IUnknown
*)*ppv
);
460 static ULONG WINAPI
MetadataHandlerEnum_AddRef(IWICEnumMetadataItem
*iface
)
462 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
463 ULONG ref
= InterlockedIncrement(&This
->ref
);
465 TRACE("(%p) refcount=%u\n", iface
, ref
);
470 static ULONG WINAPI
MetadataHandlerEnum_Release(IWICEnumMetadataItem
*iface
)
472 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
473 ULONG ref
= InterlockedDecrement(&This
->ref
);
475 TRACE("(%p) refcount=%u\n", iface
, ref
);
479 IWICMetadataWriter_Release(&This
->parent
->IWICMetadataWriter_iface
);
480 HeapFree(GetProcessHeap(), 0, This
);
486 static HRESULT WINAPI
MetadataHandlerEnum_Next(IWICEnumMetadataItem
*iface
,
487 ULONG celt
, PROPVARIANT
*rgeltSchema
, PROPVARIANT
*rgeltId
,
488 PROPVARIANT
*rgeltValue
, ULONG
*pceltFetched
)
490 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
495 TRACE("(%p,%i)\n", iface
, celt
);
497 EnterCriticalSection(&This
->parent
->lock
);
499 if (This
->index
>= This
->parent
->item_count
)
502 LeaveCriticalSection(&This
->parent
->lock
);
506 new_index
= min(This
->parent
->item_count
, This
->index
+ celt
);
507 *pceltFetched
= new_index
- This
->index
;
511 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
512 hr
= PropVariantCopy(&rgeltSchema
[i
], &This
->parent
->items
[i
+This
->index
].schema
);
515 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
516 hr
= PropVariantCopy(&rgeltId
[i
], &This
->parent
->items
[i
+This
->index
].id
);
520 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
521 hr
= PropVariantCopy(&rgeltValue
[i
], &This
->parent
->items
[i
+This
->index
].value
);
526 This
->index
= new_index
;
529 LeaveCriticalSection(&This
->parent
->lock
);
534 static HRESULT WINAPI
MetadataHandlerEnum_Skip(IWICEnumMetadataItem
*iface
,
537 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
539 EnterCriticalSection(&This
->parent
->lock
);
543 LeaveCriticalSection(&This
->parent
->lock
);
548 static HRESULT WINAPI
MetadataHandlerEnum_Reset(IWICEnumMetadataItem
*iface
)
550 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
552 EnterCriticalSection(&This
->parent
->lock
);
556 LeaveCriticalSection(&This
->parent
->lock
);
561 static HRESULT WINAPI
MetadataHandlerEnum_Clone(IWICEnumMetadataItem
*iface
,
562 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
564 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
567 EnterCriticalSection(&This
->parent
->lock
);
569 hr
= MetadataHandlerEnum_Create(This
->parent
, This
->index
, ppIEnumMetadataItem
);
571 LeaveCriticalSection(&This
->parent
->lock
);
576 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl
= {
577 MetadataHandlerEnum_QueryInterface
,
578 MetadataHandlerEnum_AddRef
,
579 MetadataHandlerEnum_Release
,
580 MetadataHandlerEnum_Next
,
581 MetadataHandlerEnum_Skip
,
582 MetadataHandlerEnum_Reset
,
583 MetadataHandlerEnum_Clone
586 static HRESULT
MetadataHandlerEnum_Create(MetadataHandler
*parent
, DWORD index
,
587 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
589 MetadataHandlerEnum
*This
;
591 if (!ppIEnumMetadataItem
) return E_INVALIDARG
;
593 *ppIEnumMetadataItem
= NULL
;
595 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum
));
596 if (!This
) return E_OUTOFMEMORY
;
598 IWICMetadataWriter_AddRef(&parent
->IWICMetadataWriter_iface
);
600 This
->IWICEnumMetadataItem_iface
.lpVtbl
= &MetadataHandlerEnum_Vtbl
;
602 This
->parent
= parent
;
605 *ppIEnumMetadataItem
= &This
->IWICEnumMetadataItem_iface
;
610 static HRESULT
LoadUnknownMetadata(IStream
*input
, const GUID
*preferred_vendor
,
611 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
614 MetadataItem
*result
;
621 hr
= IStream_Stat(input
, &stat
, STATFLAG_NONAME
);
625 data
= HeapAlloc(GetProcessHeap(), 0, stat
.cbSize
.QuadPart
);
626 if (!data
) return E_OUTOFMEMORY
;
628 hr
= IStream_Read(input
, data
, stat
.cbSize
.QuadPart
, &bytesread
);
629 if (bytesread
!= stat
.cbSize
.QuadPart
) hr
= E_FAIL
;
632 HeapFree(GetProcessHeap(), 0, data
);
636 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MetadataItem
));
639 HeapFree(GetProcessHeap(), 0, data
);
640 return E_OUTOFMEMORY
;
643 PropVariantInit(&result
[0].schema
);
644 PropVariantInit(&result
[0].id
);
645 PropVariantInit(&result
[0].value
);
647 result
[0].value
.vt
= VT_BLOB
;
648 result
[0].value
.blob
.cbSize
= bytesread
;
649 result
[0].value
.blob
.pBlobData
= data
;
657 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl
= {
659 &CLSID_WICUnknownMetadataReader
,
663 HRESULT
UnknownMetadataReader_CreateInstance(REFIID iid
, void** ppv
)
665 return MetadataReader_Create(&UnknownMetadataReader_Vtbl
, iid
, ppv
);
668 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
669 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
670 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
684 #define IFD_RATIONAL 5
686 #define IFD_UNDEFINED 7
689 #define IFD_SRATIONAL 10
691 #define IFD_DOUBLE 12
694 static int tag_to_vt(SHORT tag
)
696 static const int tag2vt
[] =
699 VT_UI1
, /* IFD_BYTE 1 */
700 VT_LPSTR
, /* IFD_ASCII 2 */
701 VT_UI2
, /* IFD_SHORT 3 */
702 VT_UI4
, /* IFD_LONG 4 */
703 VT_UI8
, /* IFD_RATIONAL 5 */
704 VT_I1
, /* IFD_SBYTE 6 */
705 VT_BLOB
, /* IFD_UNDEFINED 7 */
706 VT_I2
, /* IFD_SSHORT 8 */
707 VT_I4
, /* IFD_SLONG 9 */
708 VT_I8
, /* IFD_SRATIONAL 10 */
709 VT_R4
, /* IFD_FLOAT 11 */
710 VT_R8
, /* IFD_DOUBLE 12 */
711 VT_BLOB
, /* IFD_IFD 13 */
713 return (tag
> 0 && tag
<= 13) ? tag2vt
[tag
] : VT_BLOB
;
716 static HRESULT
load_IFD_entry(IStream
*input
, const struct IFD_entry
*entry
,
717 MetadataItem
*item
, BOOL native_byte_order
)
719 ULONG count
, value
, i
, bytesread
;
724 item
->schema
.vt
= VT_EMPTY
;
725 item
->id
.vt
= VT_UI2
;
726 item
->id
.uiVal
= entry
->id
;
727 SWAP_USHORT(item
->id
.uiVal
);
729 count
= entry
->count
;
733 item
->value
.vt
= tag_to_vt(type
);
734 value
= entry
->value
;
741 if (!count
) count
= 1;
745 const BYTE
*data
= (const BYTE
*)&entry
->value
;
748 item
->value
.bVal
= data
[0];
751 item
->value
.vt
|= VT_VECTOR
;
752 item
->value
.caub
.cElems
= count
;
753 item
->value
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
754 memcpy(item
->value
.caub
.pElems
, data
, count
);
759 item
->value
.vt
|= VT_VECTOR
;
760 item
->value
.caub
.cElems
= count
;
761 item
->value
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
762 if (!item
->value
.caub
.pElems
) return E_OUTOFMEMORY
;
764 pos
.QuadPart
= value
;
765 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
768 HeapFree(GetProcessHeap(), 0, item
->value
.caub
.pElems
);
771 hr
= IStream_Read(input
, item
->value
.caub
.pElems
, count
, &bytesread
);
772 if (bytesread
!= count
) hr
= E_FAIL
;
775 HeapFree(GetProcessHeap(), 0, item
->value
.caub
.pElems
);
781 if (!count
) count
= 1;
785 const SHORT
*data
= (const SHORT
*)&entry
->value
;
789 item
->value
.uiVal
= data
[0];
790 SWAP_USHORT(item
->value
.uiVal
);
794 item
->value
.vt
|= VT_VECTOR
;
795 item
->value
.caui
.cElems
= count
;
796 item
->value
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
797 memcpy(item
->value
.caui
.pElems
, data
, count
* 2);
798 for (i
= 0; i
< count
; i
++)
799 SWAP_USHORT(item
->value
.caui
.pElems
[i
]);
804 item
->value
.vt
|= VT_VECTOR
;
805 item
->value
.caui
.cElems
= count
;
806 item
->value
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
807 if (!item
->value
.caui
.pElems
) return E_OUTOFMEMORY
;
809 pos
.QuadPart
= value
;
810 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
813 HeapFree(GetProcessHeap(), 0, item
->value
.caui
.pElems
);
816 hr
= IStream_Read(input
, item
->value
.caui
.pElems
, count
* 2, &bytesread
);
817 if (bytesread
!= count
* 2) hr
= E_FAIL
;
820 HeapFree(GetProcessHeap(), 0, item
->value
.caui
.pElems
);
823 for (i
= 0; i
< count
; i
++)
824 SWAP_USHORT(item
->value
.caui
.pElems
[i
]);
829 if (!count
) count
= 1;
833 item
->value
.ulVal
= value
;
837 item
->value
.vt
|= VT_VECTOR
;
838 item
->value
.caul
.cElems
= count
;
839 item
->value
.caul
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 4);
840 if (!item
->value
.caul
.pElems
) return E_OUTOFMEMORY
;
842 pos
.QuadPart
= value
;
843 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
846 HeapFree(GetProcessHeap(), 0, item
->value
.caul
.pElems
);
849 hr
= IStream_Read(input
, item
->value
.caul
.pElems
, count
* 4, &bytesread
);
850 if (bytesread
!= count
* 4) hr
= E_FAIL
;
853 HeapFree(GetProcessHeap(), 0, item
->value
.caul
.pElems
);
856 for (i
= 0; i
< count
; i
++)
857 SWAP_ULONG(item
->value
.caul
.pElems
[i
]);
864 FIXME("IFD field type %d, count 0\n", type
);
865 item
->value
.vt
= VT_EMPTY
;
873 pos
.QuadPart
= value
;
874 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
875 if (FAILED(hr
)) return hr
;
877 hr
= IStream_Read(input
, &ull
, sizeof(ull
), &bytesread
);
878 if (bytesread
!= sizeof(ull
)) hr
= E_FAIL
;
879 if (hr
!= S_OK
) return hr
;
881 item
->value
.uhVal
.QuadPart
= ull
;
883 if (type
== IFD_DOUBLE
)
884 SWAP_ULONGLONG(item
->value
.uhVal
.QuadPart
);
887 SWAP_ULONG(item
->value
.uhVal
.LowPart
);
888 SWAP_ULONG(item
->value
.uhVal
.HighPart
);
894 item
->value
.vt
|= VT_VECTOR
;
895 item
->value
.cauh
.cElems
= count
;
896 item
->value
.cauh
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 8);
897 if (!item
->value
.cauh
.pElems
) return E_OUTOFMEMORY
;
899 pos
.QuadPart
= value
;
900 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
903 HeapFree(GetProcessHeap(), 0, item
->value
.cauh
.pElems
);
906 hr
= IStream_Read(input
, item
->value
.cauh
.pElems
, count
* 8, &bytesread
);
907 if (bytesread
!= count
* 8) hr
= E_FAIL
;
910 HeapFree(GetProcessHeap(), 0, item
->value
.cauh
.pElems
);
913 for (i
= 0; i
< count
; i
++)
915 if (type
== IFD_DOUBLE
)
916 SWAP_ULONGLONG(item
->value
.cauh
.pElems
[i
].QuadPart
);
919 SWAP_ULONG(item
->value
.cauh
.pElems
[i
].LowPart
);
920 SWAP_ULONG(item
->value
.cauh
.pElems
[i
].HighPart
);
926 item
->value
.pszVal
= HeapAlloc(GetProcessHeap(), 0, count
+ 1);
927 if (!item
->value
.pszVal
) return E_OUTOFMEMORY
;
931 const char *data
= (const char *)&entry
->value
;
932 memcpy(item
->value
.pszVal
, data
, count
);
933 item
->value
.pszVal
[count
] = 0;
937 pos
.QuadPart
= value
;
938 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
941 HeapFree(GetProcessHeap(), 0, item
->value
.pszVal
);
944 hr
= IStream_Read(input
, item
->value
.pszVal
, count
, &bytesread
);
945 if (bytesread
!= count
) hr
= E_FAIL
;
948 HeapFree(GetProcessHeap(), 0, item
->value
.pszVal
);
951 item
->value
.pszVal
[count
] = 0;
956 FIXME("IFD field type %d, count 0\n", type
);
957 item
->value
.vt
= VT_EMPTY
;
961 item
->value
.blob
.pBlobData
= HeapAlloc(GetProcessHeap(), 0, count
);
962 if (!item
->value
.blob
.pBlobData
) return E_OUTOFMEMORY
;
964 item
->value
.blob
.cbSize
= count
;
968 const char *data
= (const char *)&entry
->value
;
969 memcpy(item
->value
.blob
.pBlobData
, data
, count
);
973 pos
.QuadPart
= value
;
974 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
977 HeapFree(GetProcessHeap(), 0, item
->value
.blob
.pBlobData
);
980 hr
= IStream_Read(input
, item
->value
.blob
.pBlobData
, count
, &bytesread
);
981 if (bytesread
!= count
) hr
= E_FAIL
;
984 HeapFree(GetProcessHeap(), 0, item
->value
.blob
.pBlobData
);
989 FIXME("loading field of type %d, count %u is not implemented\n", type
, count
);
995 static HRESULT
LoadIfdMetadata(IStream
*input
, const GUID
*preferred_vendor
,
996 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
999 MetadataItem
*result
;
1001 struct IFD_entry
*entry
;
1002 BOOL native_byte_order
= TRUE
;
1007 #ifdef WORDS_BIGENDIAN
1008 if (persist_options
& WICPersistOptionLittleEndian
)
1010 if (persist_options
& WICPersistOptionBigEndian
)
1012 native_byte_order
= FALSE
;
1014 hr
= IStream_Read(input
, &count
, sizeof(count
), &bytesread
);
1015 if (bytesread
!= sizeof(count
)) hr
= E_FAIL
;
1016 if (hr
!= S_OK
) return hr
;
1020 entry
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(*entry
));
1021 if (!entry
) return E_OUTOFMEMORY
;
1023 hr
= IStream_Read(input
, entry
, count
* sizeof(*entry
), &bytesread
);
1024 if (bytesread
!= count
* sizeof(*entry
)) hr
= E_FAIL
;
1027 HeapFree(GetProcessHeap(), 0, entry
);
1031 /* limit number of IFDs to 4096 to avoid infinite loop */
1032 for (i
= 0; i
< 4096; i
++)
1034 ULONG next_ifd_offset
;
1036 USHORT next_ifd_count
;
1038 hr
= IStream_Read(input
, &next_ifd_offset
, sizeof(next_ifd_offset
), &bytesread
);
1039 if (bytesread
!= sizeof(next_ifd_offset
)) hr
= E_FAIL
;
1040 if (hr
!= S_OK
) break;
1042 SWAP_ULONG(next_ifd_offset
);
1043 if (!next_ifd_offset
) break;
1045 pos
.QuadPart
= next_ifd_offset
;
1046 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1047 if (FAILED(hr
)) break;
1049 hr
= IStream_Read(input
, &next_ifd_count
, sizeof(next_ifd_count
), &bytesread
);
1050 if (bytesread
!= sizeof(next_ifd_count
)) hr
= E_FAIL
;
1051 if (hr
!= S_OK
) break;
1053 SWAP_USHORT(next_ifd_count
);
1055 pos
.QuadPart
= next_ifd_count
* sizeof(*entry
);
1056 hr
= IStream_Seek(input
, pos
, SEEK_CUR
, NULL
);
1057 if (FAILED(hr
)) break;
1060 if (hr
!= S_OK
|| i
== 4096)
1062 HeapFree(GetProcessHeap(), 0, entry
);
1063 return WINCODEC_ERR_BADMETADATAHEADER
;
1066 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, count
* sizeof(*result
));
1069 HeapFree(GetProcessHeap(), 0, entry
);
1070 return E_OUTOFMEMORY
;
1073 for (i
= 0; i
< count
; i
++)
1075 hr
= load_IFD_entry(input
, &entry
[i
], &result
[i
], native_byte_order
);
1078 HeapFree(GetProcessHeap(), 0, entry
);
1079 HeapFree(GetProcessHeap(), 0, result
);
1084 HeapFree(GetProcessHeap(), 0, entry
);
1087 *item_count
= count
;
1092 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl
= {
1094 &CLSID_WICIfdMetadataReader
,
1098 HRESULT
IfdMetadataReader_CreateInstance(REFIID iid
, void **ppv
)
1100 return MetadataReader_Create(&IfdMetadataReader_Vtbl
, iid
, ppv
);