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 HRESULT WINAPI
MetadataHandler_GetValue(IWICMetadataWriter
*iface
,
217 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, PROPVARIANT
*pvarValue
)
219 FIXME("(%p,%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
, pvarValue
);
223 static HRESULT WINAPI
MetadataHandler_GetEnumerator(IWICMetadataWriter
*iface
,
224 IWICEnumMetadataItem
**ppIEnumMetadata
)
226 MetadataHandler
*This
= impl_from_IWICMetadataWriter(iface
);
227 TRACE("(%p,%p)\n", iface
, ppIEnumMetadata
);
228 return MetadataHandlerEnum_Create(This
, 0, ppIEnumMetadata
);
231 static HRESULT WINAPI
MetadataHandler_SetValue(IWICMetadataWriter
*iface
,
232 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
234 FIXME("(%p,%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
, pvarValue
);
238 static HRESULT WINAPI
MetadataHandler_SetValueByIndex(IWICMetadataWriter
*iface
,
239 UINT nIndex
, const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
, const PROPVARIANT
*pvarValue
)
241 FIXME("(%p,%u,%p,%p,%p): stub\n", iface
, nIndex
, pvarSchema
, pvarId
, pvarValue
);
245 static HRESULT WINAPI
MetadataHandler_RemoveValue(IWICMetadataWriter
*iface
,
246 const PROPVARIANT
*pvarSchema
, const PROPVARIANT
*pvarId
)
248 FIXME("(%p,%p,%p): stub\n", iface
, pvarSchema
, pvarId
);
252 static HRESULT WINAPI
MetadataHandler_RemoveValueByIndex(IWICMetadataWriter
*iface
,
255 FIXME("(%p,%u): stub\n", iface
, nIndex
);
259 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl
= {
260 MetadataHandler_QueryInterface
,
261 MetadataHandler_AddRef
,
262 MetadataHandler_Release
,
263 MetadataHandler_GetMetadataFormat
,
264 MetadataHandler_GetMetadataHandlerInfo
,
265 MetadataHandler_GetCount
,
266 MetadataHandler_GetValueByIndex
,
267 MetadataHandler_GetValue
,
268 MetadataHandler_GetEnumerator
,
269 MetadataHandler_SetValue
,
270 MetadataHandler_SetValueByIndex
,
271 MetadataHandler_RemoveValue
,
272 MetadataHandler_RemoveValueByIndex
275 static HRESULT WINAPI
MetadataHandler_PersistStream_QueryInterface(IWICPersistStream
*iface
,
276 REFIID iid
, void **ppv
)
278 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
279 return IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
282 static ULONG WINAPI
MetadataHandler_PersistStream_AddRef(IWICPersistStream
*iface
)
284 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
285 return IWICMetadataWriter_AddRef(&This
->IWICMetadataWriter_iface
);
288 static ULONG WINAPI
MetadataHandler_PersistStream_Release(IWICPersistStream
*iface
)
290 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
291 return IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
294 static HRESULT WINAPI
MetadataHandler_GetClassID(IWICPersistStream
*iface
,
297 FIXME("(%p,%p): stub\n", iface
, pClassID
);
301 static HRESULT WINAPI
MetadataHandler_IsDirty(IWICPersistStream
*iface
)
303 FIXME("(%p): stub\n", iface
);
307 static HRESULT WINAPI
MetadataHandler_Load(IWICPersistStream
*iface
,
310 FIXME("(%p,%p): stub\n", iface
, pStm
);
314 static HRESULT WINAPI
MetadataHandler_Save(IWICPersistStream
*iface
,
315 IStream
*pStm
, BOOL fClearDirty
)
317 FIXME("(%p,%p,%i): stub\n", iface
, pStm
, fClearDirty
);
321 static HRESULT WINAPI
MetadataHandler_GetSizeMax(IWICPersistStream
*iface
,
322 ULARGE_INTEGER
*pcbSize
)
324 FIXME("(%p,%p): stub\n", iface
, pcbSize
);
328 static HRESULT WINAPI
MetadataHandler_LoadEx(IWICPersistStream
*iface
,
329 IStream
*pIStream
, const GUID
*pguidPreferredVendor
, DWORD dwPersistOptions
)
331 MetadataHandler
*This
= impl_from_IWICPersistStream(iface
);
333 MetadataItem
*new_items
=NULL
;
336 TRACE("(%p,%p,%s,%x)\n", iface
, pIStream
, debugstr_guid(pguidPreferredVendor
), dwPersistOptions
);
338 EnterCriticalSection(&This
->lock
);
340 hr
= This
->vtable
->fnLoad(pIStream
, pguidPreferredVendor
, dwPersistOptions
,
341 &new_items
, &item_count
);
345 MetadataHandler_FreeItems(This
);
346 This
->items
= new_items
;
347 This
->item_count
= item_count
;
350 LeaveCriticalSection(&This
->lock
);
355 static HRESULT WINAPI
MetadataHandler_SaveEx(IWICPersistStream
*iface
,
356 IStream
*pIStream
, DWORD dwPersistOptions
, BOOL fClearDirty
)
358 FIXME("(%p,%p,%x,%i): stub\n", iface
, pIStream
, dwPersistOptions
, fClearDirty
);
362 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl
= {
363 MetadataHandler_PersistStream_QueryInterface
,
364 MetadataHandler_PersistStream_AddRef
,
365 MetadataHandler_PersistStream_Release
,
366 MetadataHandler_GetClassID
,
367 MetadataHandler_IsDirty
,
368 MetadataHandler_Load
,
369 MetadataHandler_Save
,
370 MetadataHandler_GetSizeMax
,
371 MetadataHandler_LoadEx
,
372 MetadataHandler_SaveEx
375 static HRESULT
MetadataReader_Create(const MetadataHandlerVtbl
*vtable
, IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
377 MetadataHandler
*This
;
380 TRACE("%s\n", debugstr_guid(vtable
->clsid
));
384 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
386 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler
));
387 if (!This
) return E_OUTOFMEMORY
;
389 This
->IWICMetadataWriter_iface
.lpVtbl
= &MetadataHandler_Vtbl
;
390 This
->IWICPersistStream_iface
.lpVtbl
= &MetadataHandler_PersistStream_Vtbl
;
392 This
->vtable
= vtable
;
394 This
->item_count
= 0;
396 InitializeCriticalSection(&This
->lock
);
397 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MetadataHandler.lock");
399 hr
= IWICMetadataWriter_QueryInterface(&This
->IWICMetadataWriter_iface
, iid
, ppv
);
401 IWICMetadataWriter_Release(&This
->IWICMetadataWriter_iface
);
406 typedef struct MetadataHandlerEnum
{
407 IWICEnumMetadataItem IWICEnumMetadataItem_iface
;
409 MetadataHandler
*parent
;
411 } MetadataHandlerEnum
;
413 static inline MetadataHandlerEnum
*impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem
*iface
)
415 return CONTAINING_RECORD(iface
, MetadataHandlerEnum
, IWICEnumMetadataItem_iface
);
418 static HRESULT WINAPI
MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem
*iface
, REFIID iid
,
421 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
422 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
424 if (!ppv
) return E_INVALIDARG
;
426 if (IsEqualIID(&IID_IUnknown
, iid
) ||
427 IsEqualIID(&IID_IWICEnumMetadataItem
, iid
))
429 *ppv
= &This
->IWICEnumMetadataItem_iface
;
434 return E_NOINTERFACE
;
437 IUnknown_AddRef((IUnknown
*)*ppv
);
441 static ULONG WINAPI
MetadataHandlerEnum_AddRef(IWICEnumMetadataItem
*iface
)
443 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
444 ULONG ref
= InterlockedIncrement(&This
->ref
);
446 TRACE("(%p) refcount=%u\n", iface
, ref
);
451 static ULONG WINAPI
MetadataHandlerEnum_Release(IWICEnumMetadataItem
*iface
)
453 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
454 ULONG ref
= InterlockedDecrement(&This
->ref
);
456 TRACE("(%p) refcount=%u\n", iface
, ref
);
460 IWICMetadataWriter_Release(&This
->parent
->IWICMetadataWriter_iface
);
461 HeapFree(GetProcessHeap(), 0, This
);
467 static HRESULT WINAPI
MetadataHandlerEnum_Next(IWICEnumMetadataItem
*iface
,
468 ULONG celt
, PROPVARIANT
*rgeltSchema
, PROPVARIANT
*rgeltId
,
469 PROPVARIANT
*rgeltValue
, ULONG
*pceltFetched
)
471 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
476 TRACE("(%p,%i)\n", iface
, celt
);
478 EnterCriticalSection(&This
->parent
->lock
);
480 if (This
->index
>= This
->parent
->item_count
)
483 LeaveCriticalSection(&This
->parent
->lock
);
487 new_index
= min(This
->parent
->item_count
, This
->index
+ celt
);
488 *pceltFetched
= new_index
- This
->index
;
492 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
493 hr
= PropVariantCopy(&rgeltSchema
[i
], &This
->parent
->items
[i
+This
->index
].schema
);
496 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
497 hr
= PropVariantCopy(&rgeltId
[i
], &This
->parent
->items
[i
+This
->index
].id
);
501 for (i
=0; SUCCEEDED(hr
) && i
< *pceltFetched
; i
++)
502 hr
= PropVariantCopy(&rgeltValue
[i
], &This
->parent
->items
[i
+This
->index
].value
);
507 This
->index
= new_index
;
510 LeaveCriticalSection(&This
->parent
->lock
);
515 static HRESULT WINAPI
MetadataHandlerEnum_Skip(IWICEnumMetadataItem
*iface
,
518 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
520 EnterCriticalSection(&This
->parent
->lock
);
524 LeaveCriticalSection(&This
->parent
->lock
);
529 static HRESULT WINAPI
MetadataHandlerEnum_Reset(IWICEnumMetadataItem
*iface
)
531 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
533 EnterCriticalSection(&This
->parent
->lock
);
537 LeaveCriticalSection(&This
->parent
->lock
);
542 static HRESULT WINAPI
MetadataHandlerEnum_Clone(IWICEnumMetadataItem
*iface
,
543 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
545 MetadataHandlerEnum
*This
= impl_from_IWICEnumMetadataItem(iface
);
548 EnterCriticalSection(&This
->parent
->lock
);
550 hr
= MetadataHandlerEnum_Create(This
->parent
, This
->index
, ppIEnumMetadataItem
);
552 LeaveCriticalSection(&This
->parent
->lock
);
557 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl
= {
558 MetadataHandlerEnum_QueryInterface
,
559 MetadataHandlerEnum_AddRef
,
560 MetadataHandlerEnum_Release
,
561 MetadataHandlerEnum_Next
,
562 MetadataHandlerEnum_Skip
,
563 MetadataHandlerEnum_Reset
,
564 MetadataHandlerEnum_Clone
567 static HRESULT
MetadataHandlerEnum_Create(MetadataHandler
*parent
, DWORD index
,
568 IWICEnumMetadataItem
**ppIEnumMetadataItem
)
570 MetadataHandlerEnum
*This
;
572 if (!ppIEnumMetadataItem
) return E_INVALIDARG
;
574 *ppIEnumMetadataItem
= NULL
;
576 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum
));
577 if (!This
) return E_OUTOFMEMORY
;
579 IWICMetadataWriter_AddRef(&parent
->IWICMetadataWriter_iface
);
581 This
->IWICEnumMetadataItem_iface
.lpVtbl
= &MetadataHandlerEnum_Vtbl
;
583 This
->parent
= parent
;
586 *ppIEnumMetadataItem
= &This
->IWICEnumMetadataItem_iface
;
591 static HRESULT
LoadUnknownMetadata(IStream
*input
, const GUID
*preferred_vendor
,
592 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
595 MetadataItem
*result
;
602 hr
= IStream_Stat(input
, &stat
, STATFLAG_NONAME
);
606 data
= HeapAlloc(GetProcessHeap(), 0, stat
.cbSize
.QuadPart
);
607 if (!data
) return E_OUTOFMEMORY
;
609 hr
= IStream_Read(input
, data
, stat
.cbSize
.QuadPart
, &bytesread
);
612 HeapFree(GetProcessHeap(), 0, data
);
616 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
619 HeapFree(GetProcessHeap(), 0, data
);
620 return E_OUTOFMEMORY
;
623 PropVariantInit(&result
[0].schema
);
624 PropVariantInit(&result
[0].id
);
625 PropVariantInit(&result
[0].value
);
627 result
[0].value
.vt
= VT_BLOB
;
628 result
[0].value
.u
.blob
.cbSize
= bytesread
;
629 result
[0].value
.u
.blob
.pBlobData
= data
;
637 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl
= {
639 &CLSID_WICUnknownMetadataReader
,
643 HRESULT
UnknownMetadataReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
645 return MetadataReader_Create(&UnknownMetadataReader_Vtbl
, pUnkOuter
, iid
, ppv
);
648 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
649 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
650 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
664 #define IFD_RATIONAL 5
666 #define IFD_UNDEFINED 7
669 #define IFD_SRATIONAL 10
671 #define IFD_DOUBLE 12
674 static int tag_to_vt(SHORT tag
)
676 static const int tag2vt
[] =
679 VT_UI1
, /* IFD_BYTE 1 */
680 VT_LPSTR
, /* IFD_ASCII 2 */
681 VT_UI2
, /* IFD_SHORT 3 */
682 VT_UI4
, /* IFD_LONG 4 */
683 VT_UI8
, /* IFD_RATIONAL 5 */
684 VT_I1
, /* IFD_SBYTE 6 */
685 VT_BLOB
, /* IFD_UNDEFINED 7 */
686 VT_I2
, /* IFD_SSHORT 8 */
687 VT_I4
, /* IFD_SLONG 9 */
688 VT_I8
, /* IFD_SRATIONAL 10 */
689 VT_R4
, /* IFD_FLOAT 11 */
690 VT_R8
, /* IFD_DOUBLE 12 */
691 VT_BLOB
, /* IFD_IFD 13 */
693 return (tag
> 0 && tag
<= 13) ? tag2vt
[tag
] : VT_BLOB
;
696 static HRESULT
load_IFD_entry(IStream
*input
, const struct IFD_entry
*entry
,
697 MetadataItem
*item
, BOOL native_byte_order
)
699 ULONG count
, value
, i
;
704 item
->schema
.vt
= VT_EMPTY
;
705 item
->id
.vt
= VT_UI2
;
706 item
->id
.u
.uiVal
= entry
->id
;
707 SWAP_USHORT(item
->id
.u
.uiVal
);
709 count
= entry
->count
;
713 item
->value
.vt
= tag_to_vt(type
);
714 value
= entry
->value
;
721 if (!count
) count
= 1;
725 const BYTE
*data
= (const BYTE
*)&entry
->value
;
728 item
->value
.u
.bVal
= data
[0];
731 item
->value
.vt
|= VT_VECTOR
;
732 item
->value
.u
.caub
.cElems
= count
;
733 item
->value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
734 memcpy(item
->value
.u
.caub
.pElems
, data
, count
);
739 item
->value
.vt
|= VT_VECTOR
;
740 item
->value
.u
.caub
.cElems
= count
;
741 item
->value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
);
742 if (!item
->value
.u
.caub
.pElems
) return E_OUTOFMEMORY
;
744 pos
.QuadPart
= value
;
745 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
748 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
751 hr
= IStream_Read(input
, item
->value
.u
.caub
.pElems
, count
, NULL
);
754 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caub
.pElems
);
760 if (!count
) count
= 1;
764 const SHORT
*data
= (const SHORT
*)&entry
->value
;
768 item
->value
.u
.uiVal
= data
[0];
769 SWAP_USHORT(item
->value
.u
.uiVal
);
773 item
->value
.vt
|= VT_VECTOR
;
774 item
->value
.u
.caui
.cElems
= count
;
775 item
->value
.u
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
776 memcpy(item
->value
.u
.caui
.pElems
, data
, count
* 2);
777 for (i
= 0; i
< count
; i
++)
778 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
783 item
->value
.vt
|= VT_VECTOR
;
784 item
->value
.u
.caui
.cElems
= count
;
785 item
->value
.u
.caui
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 2);
786 if (!item
->value
.u
.caui
.pElems
) return E_OUTOFMEMORY
;
788 pos
.QuadPart
= value
;
789 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
792 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
795 hr
= IStream_Read(input
, item
->value
.u
.caui
.pElems
, count
* 2, NULL
);
798 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caui
.pElems
);
801 for (i
= 0; i
< count
; i
++)
802 SWAP_USHORT(item
->value
.u
.caui
.pElems
[i
]);
807 if (!count
) count
= 1;
811 item
->value
.u
.ulVal
= value
;
815 item
->value
.vt
|= VT_VECTOR
;
816 item
->value
.u
.caul
.cElems
= count
;
817 item
->value
.u
.caul
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 4);
818 if (!item
->value
.u
.caul
.pElems
) return E_OUTOFMEMORY
;
820 pos
.QuadPart
= value
;
821 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
824 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
827 hr
= IStream_Read(input
, item
->value
.u
.caul
.pElems
, count
* 4, NULL
);
830 HeapFree(GetProcessHeap(), 0, item
->value
.u
.caul
.pElems
);
833 for (i
= 0; i
< count
; i
++)
834 SWAP_ULONG(item
->value
.u
.caul
.pElems
[i
]);
841 FIXME("IFD field type %d, count 0\n", type
);
842 item
->value
.vt
= VT_EMPTY
;
850 pos
.QuadPart
= value
;
851 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
852 if (FAILED(hr
)) return hr
;
854 hr
= IStream_Read(input
, &ull
, sizeof(ull
), NULL
);
855 if (FAILED(hr
)) return hr
;
857 item
->value
.u
.uhVal
.QuadPart
= ull
;
859 if (type
== IFD_DOUBLE
)
860 SWAP_ULONGLONG(item
->value
.u
.uhVal
.QuadPart
);
863 SWAP_ULONG(item
->value
.u
.uhVal
.u
.LowPart
);
864 SWAP_ULONG(item
->value
.u
.uhVal
.u
.HighPart
);
870 item
->value
.vt
|= VT_VECTOR
;
871 item
->value
.u
.cauh
.cElems
= count
;
872 item
->value
.u
.cauh
.pElems
= HeapAlloc(GetProcessHeap(), 0, count
* 8);
873 if (!item
->value
.u
.cauh
.pElems
) return E_OUTOFMEMORY
;
875 pos
.QuadPart
= value
;
876 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
879 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
882 hr
= IStream_Read(input
, item
->value
.u
.cauh
.pElems
, count
* 8, NULL
);
885 HeapFree(GetProcessHeap(), 0, item
->value
.u
.cauh
.pElems
);
888 for (i
= 0; i
< count
; i
++)
890 if (type
== IFD_DOUBLE
)
891 SWAP_ULONGLONG(item
->value
.u
.cauh
.pElems
[i
].QuadPart
);
894 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.LowPart
);
895 SWAP_ULONG(item
->value
.u
.cauh
.pElems
[i
].u
.HighPart
);
901 item
->value
.u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, count
+ 1);
902 if (!item
->value
.u
.pszVal
) return E_OUTOFMEMORY
;
906 const char *data
= (const char *)&entry
->value
;
907 memcpy(item
->value
.u
.pszVal
, data
, count
);
908 item
->value
.u
.pszVal
[count
] = 0;
912 pos
.QuadPart
= value
;
913 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
916 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
919 hr
= IStream_Read(input
, item
->value
.u
.pszVal
, count
, NULL
);
922 HeapFree(GetProcessHeap(), 0, item
->value
.u
.pszVal
);
925 item
->value
.u
.pszVal
[count
] = 0;
930 FIXME("IFD field type %d, count 0\n", type
);
931 item
->value
.vt
= VT_EMPTY
;
935 item
->value
.u
.blob
.pBlobData
= HeapAlloc(GetProcessHeap(), 0, count
);
936 if (!item
->value
.u
.blob
.pBlobData
) return E_OUTOFMEMORY
;
938 item
->value
.u
.blob
.cbSize
= count
;
942 const char *data
= (const char *)&entry
->value
;
943 memcpy(item
->value
.u
.blob
.pBlobData
, data
, count
);
947 pos
.QuadPart
= value
;
948 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
951 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
954 hr
= IStream_Read(input
, item
->value
.u
.blob
.pBlobData
, count
, NULL
);
957 HeapFree(GetProcessHeap(), 0, item
->value
.u
.blob
.pBlobData
);
962 FIXME("loading field of type %d, count %u is not implemented\n", type
, count
);
968 static HRESULT
LoadIfdMetadata(IStream
*input
, const GUID
*preferred_vendor
,
969 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
972 MetadataItem
*result
;
974 struct IFD_entry
*entry
;
975 BOOL native_byte_order
= TRUE
;
979 #ifdef WORDS_BIGENDIAN
980 if (persist_options
& WICPersistOptionsLittleEndian
)
982 if (persist_options
& WICPersistOptionsBigEndian
)
984 native_byte_order
= FALSE
;
986 hr
= IStream_Read(input
, &count
, sizeof(count
), NULL
);
987 if (FAILED(hr
)) return hr
;
991 entry
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(*entry
));
992 if (!entry
) return E_OUTOFMEMORY
;
994 hr
= IStream_Read(input
, entry
, count
* sizeof(*entry
), NULL
);
997 HeapFree(GetProcessHeap(), 0, entry
);
1001 /* limit number of IFDs to 4096 to avoid infinite loop */
1002 for (i
= 0; i
< 4096; i
++)
1004 ULONG next_ifd_offset
;
1006 USHORT next_ifd_count
;
1008 hr
= IStream_Read(input
, &next_ifd_offset
, sizeof(next_ifd_offset
), NULL
);
1009 if (FAILED(hr
)) break;
1011 SWAP_ULONG(next_ifd_offset
);
1012 if (!next_ifd_offset
) break;
1014 pos
.QuadPart
= next_ifd_offset
;
1015 hr
= IStream_Seek(input
, pos
, SEEK_SET
, NULL
);
1016 if (FAILED(hr
)) break;
1018 hr
= IStream_Read(input
, &next_ifd_count
, sizeof(next_ifd_count
), NULL
);
1019 if (FAILED(hr
)) break;
1021 SWAP_USHORT(next_ifd_count
);
1023 pos
.QuadPart
= next_ifd_count
* sizeof(*entry
);
1024 hr
= IStream_Seek(input
, pos
, SEEK_CUR
, NULL
);
1025 if (FAILED(hr
)) break;
1028 if (FAILED(hr
) || i
== 4096)
1030 HeapFree(GetProcessHeap(), 0, entry
);
1031 return WINCODEC_ERR_BADMETADATAHEADER
;
1034 result
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(*result
));
1037 HeapFree(GetProcessHeap(), 0, entry
);
1038 return E_OUTOFMEMORY
;
1041 for (i
= 0; i
< count
; i
++)
1043 hr
= load_IFD_entry(input
, &entry
[i
], &result
[i
], native_byte_order
);
1046 HeapFree(GetProcessHeap(), 0, entry
);
1047 HeapFree(GetProcessHeap(), 0, result
);
1052 HeapFree(GetProcessHeap(), 0, entry
);
1055 *item_count
= count
;
1060 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl
= {
1062 &CLSID_WICIfdMetadataReader
,
1066 HRESULT
IfdMetadataReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
1068 return MetadataReader_Create(&IfdMetadataReader_Vtbl
, pUnkOuter
, iid
, ppv
);