d3dcompiler: Don't allow semantics on void functions.
[wine/multimedia.git] / dlls / windowscodecs / metadatahandler.c
blob7072a954897e6f42a168dda3d9d10316b81f1fd3
1 /*
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
20 #include "config.h"
22 #include <stdarg.h>
23 #include <stdio.h>
25 #define COBJMACROS
26 #define NONAMELESSUNION
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "objbase.h"
32 #include "wincodec.h"
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;
43 LONG ref;
44 IWICPersistStream IWICPersistStream_iface;
45 const MetadataHandlerVtbl *vtable;
46 MetadataItem *items;
47 DWORD item_count;
48 CRITICAL_SECTION lock;
49 } MetadataHandler;
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)
63 int i;
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,
79 void **ppv)
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;
98 else
100 *ppv = NULL;
101 return E_NOINTERFACE;
104 IUnknown_AddRef((IUnknown*)*ppv);
105 return S_OK;
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);
115 return 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);
125 if (ref == 0)
127 MetadataHandler_FreeItems(This);
128 This->lock.DebugInfo->Spare[0] = 0;
129 DeleteCriticalSection(&This->lock);
130 HeapFree(GetProcessHeap(), 0, This);
133 return ref;
136 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
137 IWICMetadataHandlerInfo **ppIHandler)
139 HRESULT hr;
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);
152 return hr;
155 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
156 GUID *pguidMetadataFormat)
158 HRESULT hr;
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);
171 return hr;
174 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
175 UINT *pcCount)
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;
184 return S_OK;
187 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
188 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
190 HRESULT hr = S_OK;
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);
200 return E_INVALIDARG;
203 if (schema)
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);
213 return hr;
216 static BOOL get_int_value(const PROPVARIANT *pv, LONGLONG *value)
218 switch (pv->vt)
220 case VT_NULL:
221 case VT_EMPTY:
222 *value = 0;
223 break;
224 case VT_I1:
225 *value = pv->u.cVal;
226 break;
227 case VT_UI1:
228 *value = pv->u.bVal;
229 break;
230 case VT_I2:
231 *value = pv->u.iVal;
232 break;
233 case VT_UI2:
234 *value = pv->u.uiVal;
235 break;
236 case VT_I4:
237 *value = pv->u.lVal;
238 break;
239 case VT_UI4:
240 *value = pv->u.ulVal;
241 break;
242 case VT_I8:
243 case VT_UI8:
244 *value = pv->u.hVal.QuadPart;
245 break;
246 default:
247 FIXME("not supported variant type %d\n", pv->vt);
248 return FALSE;
250 return TRUE;
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 (!get_int_value(v1, &value1)) return -1;
259 if (!get_int_value(v2, &value2)) return -1;
261 value1 -= value2;
262 if (value1) return value1 < 0 ? -1 : 1;
263 return 0;
266 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
267 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
269 UINT i;
270 HRESULT hr = WINCODEC_ERR_PROPERTYNOTFOUND;
271 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
273 TRACE("(%p,%p,%p,%p)\n", iface, schema, id, value);
275 if (!id) return E_INVALIDARG;
277 EnterCriticalSection(&This->lock);
279 for (i = 0; i < This->item_count; i++)
281 if (schema && This->items[i].schema.vt != VT_EMPTY)
283 if (propvar_cmp(schema, &This->items[i].schema) != 0) continue;
286 if (propvar_cmp(id, &This->items[i].id) != 0) continue;
288 hr = value ? PropVariantCopy(value, &This->items[i].value) : S_OK;
289 break;
292 LeaveCriticalSection(&This->lock);
293 return hr;
296 static HRESULT WINAPI MetadataHandler_GetEnumerator(IWICMetadataWriter *iface,
297 IWICEnumMetadataItem **ppIEnumMetadata)
299 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
300 TRACE("(%p,%p)\n", iface, ppIEnumMetadata);
301 return MetadataHandlerEnum_Create(This, 0, ppIEnumMetadata);
304 static HRESULT WINAPI MetadataHandler_SetValue(IWICMetadataWriter *iface,
305 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
307 FIXME("(%p,%p,%p,%p): stub\n", iface, pvarSchema, pvarId, pvarValue);
308 return E_NOTIMPL;
311 static HRESULT WINAPI MetadataHandler_SetValueByIndex(IWICMetadataWriter *iface,
312 UINT nIndex, const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
314 FIXME("(%p,%u,%p,%p,%p): stub\n", iface, nIndex, pvarSchema, pvarId, pvarValue);
315 return E_NOTIMPL;
318 static HRESULT WINAPI MetadataHandler_RemoveValue(IWICMetadataWriter *iface,
319 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId)
321 FIXME("(%p,%p,%p): stub\n", iface, pvarSchema, pvarId);
322 return E_NOTIMPL;
325 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
326 UINT nIndex)
328 FIXME("(%p,%u): stub\n", iface, nIndex);
329 return E_NOTIMPL;
332 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl = {
333 MetadataHandler_QueryInterface,
334 MetadataHandler_AddRef,
335 MetadataHandler_Release,
336 MetadataHandler_GetMetadataFormat,
337 MetadataHandler_GetMetadataHandlerInfo,
338 MetadataHandler_GetCount,
339 MetadataHandler_GetValueByIndex,
340 MetadataHandler_GetValue,
341 MetadataHandler_GetEnumerator,
342 MetadataHandler_SetValue,
343 MetadataHandler_SetValueByIndex,
344 MetadataHandler_RemoveValue,
345 MetadataHandler_RemoveValueByIndex
348 static HRESULT WINAPI MetadataHandler_PersistStream_QueryInterface(IWICPersistStream *iface,
349 REFIID iid, void **ppv)
351 MetadataHandler *This = impl_from_IWICPersistStream(iface);
352 return IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
355 static ULONG WINAPI MetadataHandler_PersistStream_AddRef(IWICPersistStream *iface)
357 MetadataHandler *This = impl_from_IWICPersistStream(iface);
358 return IWICMetadataWriter_AddRef(&This->IWICMetadataWriter_iface);
361 static ULONG WINAPI MetadataHandler_PersistStream_Release(IWICPersistStream *iface)
363 MetadataHandler *This = impl_from_IWICPersistStream(iface);
364 return IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
367 static HRESULT WINAPI MetadataHandler_GetClassID(IWICPersistStream *iface,
368 CLSID *pClassID)
370 FIXME("(%p,%p): stub\n", iface, pClassID);
371 return E_NOTIMPL;
374 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
376 FIXME("(%p): stub\n", iface);
377 return E_NOTIMPL;
380 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
381 IStream *pStm)
383 FIXME("(%p,%p): stub\n", iface, pStm);
384 return E_NOTIMPL;
387 static HRESULT WINAPI MetadataHandler_Save(IWICPersistStream *iface,
388 IStream *pStm, BOOL fClearDirty)
390 FIXME("(%p,%p,%i): stub\n", iface, pStm, fClearDirty);
391 return E_NOTIMPL;
394 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
395 ULARGE_INTEGER *pcbSize)
397 FIXME("(%p,%p): stub\n", iface, pcbSize);
398 return E_NOTIMPL;
401 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
402 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
404 MetadataHandler *This = impl_from_IWICPersistStream(iface);
405 HRESULT hr;
406 MetadataItem *new_items=NULL;
407 DWORD item_count=0;
409 TRACE("(%p,%p,%s,%x)\n", iface, pIStream, debugstr_guid(pguidPreferredVendor), dwPersistOptions);
411 EnterCriticalSection(&This->lock);
413 hr = This->vtable->fnLoad(pIStream, pguidPreferredVendor, dwPersistOptions,
414 &new_items, &item_count);
416 if (SUCCEEDED(hr))
418 MetadataHandler_FreeItems(This);
419 This->items = new_items;
420 This->item_count = item_count;
423 LeaveCriticalSection(&This->lock);
425 return hr;
428 static HRESULT WINAPI MetadataHandler_SaveEx(IWICPersistStream *iface,
429 IStream *pIStream, DWORD dwPersistOptions, BOOL fClearDirty)
431 FIXME("(%p,%p,%x,%i): stub\n", iface, pIStream, dwPersistOptions, fClearDirty);
432 return E_NOTIMPL;
435 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl = {
436 MetadataHandler_PersistStream_QueryInterface,
437 MetadataHandler_PersistStream_AddRef,
438 MetadataHandler_PersistStream_Release,
439 MetadataHandler_GetClassID,
440 MetadataHandler_IsDirty,
441 MetadataHandler_Load,
442 MetadataHandler_Save,
443 MetadataHandler_GetSizeMax,
444 MetadataHandler_LoadEx,
445 MetadataHandler_SaveEx
448 static HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, IUnknown *pUnkOuter, REFIID iid, void** ppv)
450 MetadataHandler *This;
451 HRESULT hr;
453 TRACE("%s\n", debugstr_guid(vtable->clsid));
455 *ppv = NULL;
457 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
459 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
460 if (!This) return E_OUTOFMEMORY;
462 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
463 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
464 This->ref = 1;
465 This->vtable = vtable;
466 This->items = NULL;
467 This->item_count = 0;
469 InitializeCriticalSection(&This->lock);
470 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
472 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
474 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
476 return hr;
479 typedef struct MetadataHandlerEnum {
480 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
481 LONG ref;
482 MetadataHandler *parent;
483 DWORD index;
484 } MetadataHandlerEnum;
486 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
488 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
491 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
492 void **ppv)
494 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
495 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
497 if (!ppv) return E_INVALIDARG;
499 if (IsEqualIID(&IID_IUnknown, iid) ||
500 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
502 *ppv = &This->IWICEnumMetadataItem_iface;
504 else
506 *ppv = NULL;
507 return E_NOINTERFACE;
510 IUnknown_AddRef((IUnknown*)*ppv);
511 return S_OK;
514 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
516 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
517 ULONG ref = InterlockedIncrement(&This->ref);
519 TRACE("(%p) refcount=%u\n", iface, ref);
521 return ref;
524 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
526 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
527 ULONG ref = InterlockedDecrement(&This->ref);
529 TRACE("(%p) refcount=%u\n", iface, ref);
531 if (ref == 0)
533 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
534 HeapFree(GetProcessHeap(), 0, This);
537 return ref;
540 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
541 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
542 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
544 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
545 ULONG new_index;
546 HRESULT hr=S_FALSE;
547 int i;
549 TRACE("(%p,%i)\n", iface, celt);
551 EnterCriticalSection(&This->parent->lock);
553 if (This->index >= This->parent->item_count)
555 *pceltFetched = 0;
556 LeaveCriticalSection(&This->parent->lock);
557 return S_FALSE;
560 new_index = min(This->parent->item_count, This->index + celt);
561 *pceltFetched = new_index - This->index;
563 if (rgeltSchema)
565 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
566 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
569 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
570 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
572 if (rgeltValue)
574 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
575 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
578 if (SUCCEEDED(hr))
580 This->index = new_index;
583 LeaveCriticalSection(&This->parent->lock);
585 return hr;
588 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
589 ULONG celt)
591 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
593 EnterCriticalSection(&This->parent->lock);
595 This->index += celt;
597 LeaveCriticalSection(&This->parent->lock);
599 return S_OK;
602 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
604 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
606 EnterCriticalSection(&This->parent->lock);
608 This->index = 0;
610 LeaveCriticalSection(&This->parent->lock);
612 return S_OK;
615 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
616 IWICEnumMetadataItem **ppIEnumMetadataItem)
618 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
619 HRESULT hr;
621 EnterCriticalSection(&This->parent->lock);
623 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
625 LeaveCriticalSection(&This->parent->lock);
627 return hr;
630 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
631 MetadataHandlerEnum_QueryInterface,
632 MetadataHandlerEnum_AddRef,
633 MetadataHandlerEnum_Release,
634 MetadataHandlerEnum_Next,
635 MetadataHandlerEnum_Skip,
636 MetadataHandlerEnum_Reset,
637 MetadataHandlerEnum_Clone
640 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
641 IWICEnumMetadataItem **ppIEnumMetadataItem)
643 MetadataHandlerEnum *This;
645 if (!ppIEnumMetadataItem) return E_INVALIDARG;
647 *ppIEnumMetadataItem = NULL;
649 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
650 if (!This) return E_OUTOFMEMORY;
652 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
654 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
655 This->ref = 1;
656 This->parent = parent;
657 This->index = index;
659 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
661 return S_OK;
664 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
665 DWORD persist_options, MetadataItem **items, DWORD *item_count)
667 HRESULT hr;
668 MetadataItem *result;
669 STATSTG stat;
670 BYTE *data;
671 ULONG bytesread;
673 TRACE("\n");
675 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
676 if (FAILED(hr))
677 return hr;
679 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
680 if (!data) return E_OUTOFMEMORY;
682 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
683 if (FAILED(hr))
685 HeapFree(GetProcessHeap(), 0, data);
686 return hr;
689 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
690 if (!result)
692 HeapFree(GetProcessHeap(), 0, data);
693 return E_OUTOFMEMORY;
696 PropVariantInit(&result[0].schema);
697 PropVariantInit(&result[0].id);
698 PropVariantInit(&result[0].value);
700 result[0].value.vt = VT_BLOB;
701 result[0].value.u.blob.cbSize = bytesread;
702 result[0].value.u.blob.pBlobData = data;
704 *items = result;
705 *item_count = 1;
707 return S_OK;
710 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
712 &CLSID_WICUnknownMetadataReader,
713 LoadUnknownMetadata
716 HRESULT UnknownMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
718 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, pUnkOuter, iid, ppv);
721 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
722 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
723 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
725 struct IFD_entry
727 SHORT id;
728 SHORT type;
729 ULONG count;
730 LONG value;
733 #define IFD_BYTE 1
734 #define IFD_ASCII 2
735 #define IFD_SHORT 3
736 #define IFD_LONG 4
737 #define IFD_RATIONAL 5
738 #define IFD_SBYTE 6
739 #define IFD_UNDEFINED 7
740 #define IFD_SSHORT 8
741 #define IFD_SLONG 9
742 #define IFD_SRATIONAL 10
743 #define IFD_FLOAT 11
744 #define IFD_DOUBLE 12
745 #define IFD_IFD 13
747 static int tag_to_vt(SHORT tag)
749 static const int tag2vt[] =
751 VT_EMPTY, /* 0 */
752 VT_UI1, /* IFD_BYTE 1 */
753 VT_LPSTR, /* IFD_ASCII 2 */
754 VT_UI2, /* IFD_SHORT 3 */
755 VT_UI4, /* IFD_LONG 4 */
756 VT_UI8, /* IFD_RATIONAL 5 */
757 VT_I1, /* IFD_SBYTE 6 */
758 VT_BLOB, /* IFD_UNDEFINED 7 */
759 VT_I2, /* IFD_SSHORT 8 */
760 VT_I4, /* IFD_SLONG 9 */
761 VT_I8, /* IFD_SRATIONAL 10 */
762 VT_R4, /* IFD_FLOAT 11 */
763 VT_R8, /* IFD_DOUBLE 12 */
764 VT_BLOB, /* IFD_IFD 13 */
766 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
769 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
770 MetadataItem *item, BOOL native_byte_order)
772 ULONG count, value, i;
773 SHORT type;
774 LARGE_INTEGER pos;
775 HRESULT hr;
777 item->schema.vt = VT_EMPTY;
778 item->id.vt = VT_UI2;
779 item->id.u.uiVal = entry->id;
780 SWAP_USHORT(item->id.u.uiVal);
782 count = entry->count;
783 SWAP_ULONG(count);
784 type = entry->type;
785 SWAP_USHORT(type);
786 item->value.vt = tag_to_vt(type);
787 value = entry->value;
788 SWAP_ULONG(value);
790 switch (type)
792 case IFD_BYTE:
793 case IFD_SBYTE:
794 if (!count) count = 1;
796 if (count <= 4)
798 const BYTE *data = (const BYTE *)&entry->value;
800 if (count == 1)
801 item->value.u.bVal = data[0];
802 else
804 item->value.vt |= VT_VECTOR;
805 item->value.u.caub.cElems = count;
806 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
807 memcpy(item->value.u.caub.pElems, data, count);
809 break;
812 item->value.vt |= VT_VECTOR;
813 item->value.u.caub.cElems = count;
814 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
815 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
817 pos.QuadPart = value;
818 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
819 if (FAILED(hr))
821 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
822 return hr;
824 hr = IStream_Read(input, item->value.u.caub.pElems, count, NULL);
825 if (FAILED(hr))
827 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
828 return hr;
830 break;
831 case IFD_SHORT:
832 case IFD_SSHORT:
833 if (!count) count = 1;
835 if (count <= 2)
837 const SHORT *data = (const SHORT *)&entry->value;
839 if (count == 1)
841 item->value.u.uiVal = data[0];
842 SWAP_USHORT(item->value.u.uiVal);
844 else
846 item->value.vt |= VT_VECTOR;
847 item->value.u.caui.cElems = count;
848 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
849 memcpy(item->value.u.caui.pElems, data, count * 2);
850 for (i = 0; i < count; i++)
851 SWAP_USHORT(item->value.u.caui.pElems[i]);
853 break;
856 item->value.vt |= VT_VECTOR;
857 item->value.u.caui.cElems = count;
858 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
859 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
861 pos.QuadPart = value;
862 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
863 if (FAILED(hr))
865 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
866 return hr;
868 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, NULL);
869 if (FAILED(hr))
871 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
872 return hr;
874 for (i = 0; i < count; i++)
875 SWAP_USHORT(item->value.u.caui.pElems[i]);
876 break;
877 case IFD_LONG:
878 case IFD_SLONG:
879 case IFD_FLOAT:
880 if (!count) count = 1;
882 if (count == 1)
884 item->value.u.ulVal = value;
885 break;
888 item->value.vt |= VT_VECTOR;
889 item->value.u.caul.cElems = count;
890 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
891 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
893 pos.QuadPart = value;
894 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
895 if (FAILED(hr))
897 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
898 return hr;
900 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, NULL);
901 if (FAILED(hr))
903 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
904 return hr;
906 for (i = 0; i < count; i++)
907 SWAP_ULONG(item->value.u.caul.pElems[i]);
908 break;
909 case IFD_RATIONAL:
910 case IFD_SRATIONAL:
911 case IFD_DOUBLE:
912 if (!count)
914 FIXME("IFD field type %d, count 0\n", type);
915 item->value.vt = VT_EMPTY;
916 break;
919 if (count == 1)
921 ULONGLONG ull;
923 pos.QuadPart = value;
924 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
925 if (FAILED(hr)) return hr;
927 hr = IStream_Read(input, &ull, sizeof(ull), NULL);
928 if (FAILED(hr)) return hr;
930 item->value.u.uhVal.QuadPart = ull;
932 if (type == IFD_DOUBLE)
933 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
934 else
936 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
937 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
939 break;
941 else
943 item->value.vt |= VT_VECTOR;
944 item->value.u.cauh.cElems = count;
945 item->value.u.cauh.pElems = HeapAlloc(GetProcessHeap(), 0, count * 8);
946 if (!item->value.u.cauh.pElems) return E_OUTOFMEMORY;
948 pos.QuadPart = value;
949 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
950 if (FAILED(hr))
952 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
953 return hr;
955 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, NULL);
956 if (FAILED(hr))
958 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
959 return hr;
961 for (i = 0; i < count; i++)
963 if (type == IFD_DOUBLE)
964 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
965 else
967 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
968 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
972 break;
973 case IFD_ASCII:
974 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
975 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
977 if (count <= 4)
979 const char *data = (const char *)&entry->value;
980 memcpy(item->value.u.pszVal, data, count);
981 item->value.u.pszVal[count] = 0;
982 break;
985 pos.QuadPart = value;
986 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
987 if (FAILED(hr))
989 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
990 return hr;
992 hr = IStream_Read(input, item->value.u.pszVal, count, NULL);
993 if (FAILED(hr))
995 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
996 return hr;
998 item->value.u.pszVal[count] = 0;
999 break;
1000 case IFD_UNDEFINED:
1001 if (!count)
1003 FIXME("IFD field type %d, count 0\n", type);
1004 item->value.vt = VT_EMPTY;
1005 break;
1008 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
1009 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
1011 item->value.u.blob.cbSize = count;
1013 if (count <= 4)
1015 const char *data = (const char *)&entry->value;
1016 memcpy(item->value.u.blob.pBlobData, data, count);
1017 break;
1020 pos.QuadPart = value;
1021 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1022 if (FAILED(hr))
1024 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1025 return hr;
1027 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, NULL);
1028 if (FAILED(hr))
1030 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1031 return hr;
1033 break;
1034 default:
1035 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
1036 break;
1038 return S_OK;
1041 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
1042 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1044 HRESULT hr;
1045 MetadataItem *result;
1046 USHORT count, i;
1047 struct IFD_entry *entry;
1048 BOOL native_byte_order = TRUE;
1050 TRACE("\n");
1052 #ifdef WORDS_BIGENDIAN
1053 if (persist_options & WICPersistOptionsLittleEndian)
1054 #else
1055 if (persist_options & WICPersistOptionsBigEndian)
1056 #endif
1057 native_byte_order = FALSE;
1059 hr = IStream_Read(input, &count, sizeof(count), NULL);
1060 if (FAILED(hr)) return hr;
1062 SWAP_USHORT(count);
1064 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1065 if (!entry) return E_OUTOFMEMORY;
1067 hr = IStream_Read(input, entry, count * sizeof(*entry), NULL);
1068 if (FAILED(hr))
1070 HeapFree(GetProcessHeap(), 0, entry);
1071 return hr;
1074 /* limit number of IFDs to 4096 to avoid infinite loop */
1075 for (i = 0; i < 4096; i++)
1077 ULONG next_ifd_offset;
1078 LARGE_INTEGER pos;
1079 USHORT next_ifd_count;
1081 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), NULL);
1082 if (FAILED(hr)) break;
1084 SWAP_ULONG(next_ifd_offset);
1085 if (!next_ifd_offset) break;
1087 pos.QuadPart = next_ifd_offset;
1088 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1089 if (FAILED(hr)) break;
1091 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), NULL);
1092 if (FAILED(hr)) break;
1094 SWAP_USHORT(next_ifd_count);
1096 pos.QuadPart = next_ifd_count * sizeof(*entry);
1097 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1098 if (FAILED(hr)) break;
1101 if (FAILED(hr) || i == 4096)
1103 HeapFree(GetProcessHeap(), 0, entry);
1104 return WINCODEC_ERR_BADMETADATAHEADER;
1107 result = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*result));
1108 if (!result)
1110 HeapFree(GetProcessHeap(), 0, entry);
1111 return E_OUTOFMEMORY;
1114 for (i = 0; i < count; i++)
1116 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1117 if (FAILED(hr))
1119 HeapFree(GetProcessHeap(), 0, entry);
1120 HeapFree(GetProcessHeap(), 0, result);
1121 return hr;
1125 HeapFree(GetProcessHeap(), 0, entry);
1127 *items = result;
1128 *item_count = count;
1130 return S_OK;
1133 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1135 &CLSID_WICIfdMetadataReader,
1136 LoadIfdMetadata
1139 HRESULT IfdMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void **ppv)
1141 return MetadataReader_Create(&IfdMetadataReader_Vtbl, pUnkOuter, iid, ppv);