msvcrt: Import cbrt implementation from musl.
[wine.git] / dlls / windowscodecs / metadatahandler.c
blob128514f6a315e539cb5afce6cb2d17879c60036d
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 <stdarg.h>
21 #include <stdio.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "objbase.h"
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;
40 LONG ref;
41 IWICPersistStream IWICPersistStream_iface;
42 const MetadataHandlerVtbl *vtable;
43 MetadataItem *items;
44 DWORD item_count;
45 CRITICAL_SECTION lock;
46 } MetadataHandler;
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)
60 DWORD i;
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,
76 void **ppv)
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;
95 else
97 *ppv = NULL;
98 return E_NOINTERFACE;
101 IUnknown_AddRef((IUnknown*)*ppv);
102 return S_OK;
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);
112 return 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);
122 if (ref == 0)
124 MetadataHandler_FreeItems(This);
125 This->lock.DebugInfo->Spare[0] = 0;
126 DeleteCriticalSection(&This->lock);
127 HeapFree(GetProcessHeap(), 0, This);
130 return ref;
133 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
134 IWICMetadataHandlerInfo **ppIHandler)
136 HRESULT hr;
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);
149 return hr;
152 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
153 GUID *pguidMetadataFormat)
155 HRESULT hr;
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);
168 return hr;
171 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
172 UINT *pcCount)
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;
181 return S_OK;
184 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
185 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
187 HRESULT hr = S_OK;
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);
197 return E_INVALIDARG;
200 if (schema)
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);
210 return hr;
213 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
214 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
216 UINT i;
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;
236 break;
239 LeaveCriticalSection(&This->lock);
240 return hr;
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);
255 return E_NOTIMPL;
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);
262 return E_NOTIMPL;
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);
269 return E_NOTIMPL;
272 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
273 UINT nIndex)
275 FIXME("(%p,%u): stub\n", iface, nIndex);
276 return E_NOTIMPL;
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,
315 CLSID *pClassID)
317 FIXME("(%p,%p): stub\n", iface, pClassID);
318 return E_NOTIMPL;
321 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
323 FIXME("(%p): stub\n", iface);
324 return E_NOTIMPL;
327 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
328 IStream *pStm)
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);
339 return E_NOTIMPL;
342 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
343 ULARGE_INTEGER *pcbSize)
345 FIXME("(%p,%p): stub\n", iface, pcbSize);
346 return E_NOTIMPL;
349 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
350 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
352 MetadataHandler *This = impl_from_IWICPersistStream(iface);
353 HRESULT hr;
354 MetadataItem *new_items=NULL;
355 DWORD item_count=0;
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);
364 if (SUCCEEDED(hr))
366 MetadataHandler_FreeItems(This);
367 This->items = new_items;
368 This->item_count = item_count;
371 LeaveCriticalSection(&This->lock);
373 return hr;
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);
380 return E_NOTIMPL;
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;
399 HRESULT hr;
401 TRACE("%s\n", debugstr_guid(vtable->clsid));
403 *ppv = NULL;
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;
410 This->ref = 1;
411 This->vtable = vtable;
412 This->items = NULL;
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);
422 return hr;
425 typedef struct MetadataHandlerEnum {
426 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
427 LONG ref;
428 MetadataHandler *parent;
429 DWORD index;
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,
438 void **ppv)
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;
450 else
452 *ppv = NULL;
453 return E_NOINTERFACE;
456 IUnknown_AddRef((IUnknown*)*ppv);
457 return S_OK;
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);
467 return 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);
477 if (ref == 0)
479 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
480 HeapFree(GetProcessHeap(), 0, This);
483 return ref;
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);
491 ULONG new_index;
492 HRESULT hr=S_FALSE;
493 ULONG i;
495 TRACE("(%p,%i)\n", iface, celt);
497 EnterCriticalSection(&This->parent->lock);
499 if (This->index >= This->parent->item_count)
501 *pceltFetched = 0;
502 LeaveCriticalSection(&This->parent->lock);
503 return S_FALSE;
506 new_index = min(This->parent->item_count, This->index + celt);
507 *pceltFetched = new_index - This->index;
509 if (rgeltSchema)
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);
518 if (rgeltValue)
520 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
521 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
524 if (SUCCEEDED(hr))
526 This->index = new_index;
529 LeaveCriticalSection(&This->parent->lock);
531 return hr;
534 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
535 ULONG celt)
537 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
539 EnterCriticalSection(&This->parent->lock);
541 This->index += celt;
543 LeaveCriticalSection(&This->parent->lock);
545 return S_OK;
548 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
550 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
552 EnterCriticalSection(&This->parent->lock);
554 This->index = 0;
556 LeaveCriticalSection(&This->parent->lock);
558 return S_OK;
561 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
562 IWICEnumMetadataItem **ppIEnumMetadataItem)
564 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
565 HRESULT hr;
567 EnterCriticalSection(&This->parent->lock);
569 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
571 LeaveCriticalSection(&This->parent->lock);
573 return hr;
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;
601 This->ref = 1;
602 This->parent = parent;
603 This->index = index;
605 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
607 return S_OK;
610 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
611 DWORD persist_options, MetadataItem **items, DWORD *item_count)
613 HRESULT hr;
614 MetadataItem *result;
615 STATSTG stat;
616 BYTE *data;
617 ULONG bytesread;
619 TRACE("\n");
621 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
622 if (FAILED(hr))
623 return hr;
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;
630 if (hr != S_OK)
632 HeapFree(GetProcessHeap(), 0, data);
633 return hr;
636 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
637 if (!result)
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;
651 *items = result;
652 *item_count = 1;
654 return S_OK;
657 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
659 &CLSID_WICUnknownMetadataReader,
660 LoadUnknownMetadata
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)
672 struct IFD_entry
674 SHORT id;
675 SHORT type;
676 ULONG count;
677 LONG value;
680 #define IFD_BYTE 1
681 #define IFD_ASCII 2
682 #define IFD_SHORT 3
683 #define IFD_LONG 4
684 #define IFD_RATIONAL 5
685 #define IFD_SBYTE 6
686 #define IFD_UNDEFINED 7
687 #define IFD_SSHORT 8
688 #define IFD_SLONG 9
689 #define IFD_SRATIONAL 10
690 #define IFD_FLOAT 11
691 #define IFD_DOUBLE 12
692 #define IFD_IFD 13
694 static int tag_to_vt(SHORT tag)
696 static const int tag2vt[] =
698 VT_EMPTY, /* 0 */
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;
720 SHORT type;
721 LARGE_INTEGER pos;
722 HRESULT hr;
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;
730 SWAP_ULONG(count);
731 type = entry->type;
732 SWAP_USHORT(type);
733 item->value.vt = tag_to_vt(type);
734 value = entry->value;
735 SWAP_ULONG(value);
737 switch (type)
739 case IFD_BYTE:
740 case IFD_SBYTE:
741 if (!count) count = 1;
743 if (count <= 4)
745 const BYTE *data = (const BYTE *)&entry->value;
747 if (count == 1)
748 item->value.bVal = data[0];
749 else
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);
756 break;
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);
766 if (FAILED(hr))
768 HeapFree(GetProcessHeap(), 0, item->value.caub.pElems);
769 return hr;
771 hr = IStream_Read(input, item->value.caub.pElems, count, &bytesread);
772 if (bytesread != count) hr = E_FAIL;
773 if (hr != S_OK)
775 HeapFree(GetProcessHeap(), 0, item->value.caub.pElems);
776 return hr;
778 break;
779 case IFD_SHORT:
780 case IFD_SSHORT:
781 if (!count) count = 1;
783 if (count <= 2)
785 const SHORT *data = (const SHORT *)&entry->value;
787 if (count == 1)
789 item->value.uiVal = data[0];
790 SWAP_USHORT(item->value.uiVal);
792 else
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]);
801 break;
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);
811 if (FAILED(hr))
813 HeapFree(GetProcessHeap(), 0, item->value.caui.pElems);
814 return hr;
816 hr = IStream_Read(input, item->value.caui.pElems, count * 2, &bytesread);
817 if (bytesread != count * 2) hr = E_FAIL;
818 if (hr != S_OK)
820 HeapFree(GetProcessHeap(), 0, item->value.caui.pElems);
821 return hr;
823 for (i = 0; i < count; i++)
824 SWAP_USHORT(item->value.caui.pElems[i]);
825 break;
826 case IFD_LONG:
827 case IFD_SLONG:
828 case IFD_FLOAT:
829 if (!count) count = 1;
831 if (count == 1)
833 item->value.ulVal = value;
834 break;
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);
844 if (FAILED(hr))
846 HeapFree(GetProcessHeap(), 0, item->value.caul.pElems);
847 return hr;
849 hr = IStream_Read(input, item->value.caul.pElems, count * 4, &bytesread);
850 if (bytesread != count * 4) hr = E_FAIL;
851 if (hr != S_OK)
853 HeapFree(GetProcessHeap(), 0, item->value.caul.pElems);
854 return hr;
856 for (i = 0; i < count; i++)
857 SWAP_ULONG(item->value.caul.pElems[i]);
858 break;
859 case IFD_RATIONAL:
860 case IFD_SRATIONAL:
861 case IFD_DOUBLE:
862 if (!count)
864 FIXME("IFD field type %d, count 0\n", type);
865 item->value.vt = VT_EMPTY;
866 break;
869 if (count == 1)
871 ULONGLONG ull;
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);
885 else
887 SWAP_ULONG(item->value.uhVal.LowPart);
888 SWAP_ULONG(item->value.uhVal.HighPart);
890 break;
892 else
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);
901 if (FAILED(hr))
903 HeapFree(GetProcessHeap(), 0, item->value.cauh.pElems);
904 return hr;
906 hr = IStream_Read(input, item->value.cauh.pElems, count * 8, &bytesread);
907 if (bytesread != count * 8) hr = E_FAIL;
908 if (hr != S_OK)
910 HeapFree(GetProcessHeap(), 0, item->value.cauh.pElems);
911 return hr;
913 for (i = 0; i < count; i++)
915 if (type == IFD_DOUBLE)
916 SWAP_ULONGLONG(item->value.cauh.pElems[i].QuadPart);
917 else
919 SWAP_ULONG(item->value.cauh.pElems[i].LowPart);
920 SWAP_ULONG(item->value.cauh.pElems[i].HighPart);
924 break;
925 case IFD_ASCII:
926 item->value.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
927 if (!item->value.pszVal) return E_OUTOFMEMORY;
929 if (count <= 4)
931 const char *data = (const char *)&entry->value;
932 memcpy(item->value.pszVal, data, count);
933 item->value.pszVal[count] = 0;
934 break;
937 pos.QuadPart = value;
938 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
939 if (FAILED(hr))
941 HeapFree(GetProcessHeap(), 0, item->value.pszVal);
942 return hr;
944 hr = IStream_Read(input, item->value.pszVal, count, &bytesread);
945 if (bytesread != count) hr = E_FAIL;
946 if (hr != S_OK)
948 HeapFree(GetProcessHeap(), 0, item->value.pszVal);
949 return hr;
951 item->value.pszVal[count] = 0;
952 break;
953 case IFD_UNDEFINED:
954 if (!count)
956 FIXME("IFD field type %d, count 0\n", type);
957 item->value.vt = VT_EMPTY;
958 break;
961 item->value.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
962 if (!item->value.blob.pBlobData) return E_OUTOFMEMORY;
964 item->value.blob.cbSize = count;
966 if (count <= 4)
968 const char *data = (const char *)&entry->value;
969 memcpy(item->value.blob.pBlobData, data, count);
970 break;
973 pos.QuadPart = value;
974 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
975 if (FAILED(hr))
977 HeapFree(GetProcessHeap(), 0, item->value.blob.pBlobData);
978 return hr;
980 hr = IStream_Read(input, item->value.blob.pBlobData, count, &bytesread);
981 if (bytesread != count) hr = E_FAIL;
982 if (hr != S_OK)
984 HeapFree(GetProcessHeap(), 0, item->value.blob.pBlobData);
985 return hr;
987 break;
988 default:
989 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
990 break;
992 return S_OK;
995 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
996 DWORD persist_options, MetadataItem **items, DWORD *item_count)
998 HRESULT hr;
999 MetadataItem *result;
1000 USHORT count, i;
1001 struct IFD_entry *entry;
1002 BOOL native_byte_order = TRUE;
1003 ULONG bytesread;
1005 TRACE("\n");
1007 #ifdef WORDS_BIGENDIAN
1008 if (persist_options & WICPersistOptionLittleEndian)
1009 #else
1010 if (persist_options & WICPersistOptionBigEndian)
1011 #endif
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;
1018 SWAP_USHORT(count);
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;
1025 if (hr != S_OK)
1027 HeapFree(GetProcessHeap(), 0, entry);
1028 return hr;
1031 /* limit number of IFDs to 4096 to avoid infinite loop */
1032 for (i = 0; i < 4096; i++)
1034 ULONG next_ifd_offset;
1035 LARGE_INTEGER pos;
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));
1067 if (!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);
1076 if (FAILED(hr))
1078 HeapFree(GetProcessHeap(), 0, entry);
1079 HeapFree(GetProcessHeap(), 0, result);
1080 return hr;
1084 HeapFree(GetProcessHeap(), 0, entry);
1086 *items = result;
1087 *item_count = count;
1089 return S_OK;
1092 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1094 &CLSID_WICIfdMetadataReader,
1095 LoadIfdMetadata
1098 HRESULT IfdMetadataReader_CreateInstance(REFIID iid, void **ppv)
1100 return MetadataReader_Create(&IfdMetadataReader_Vtbl, iid, ppv);