winspool.drv: Avoid bitwise operation.
[wine.git] / dlls / windowscodecs / metadatahandler.c
blobcdd7420901118aa230ac4cdcdcfb50a6e7a4890d
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 "propvarutil.h"
34 #include "wincodecs_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
40 typedef struct MetadataHandler {
41 IWICMetadataWriter IWICMetadataWriter_iface;
42 LONG ref;
43 IWICPersistStream IWICPersistStream_iface;
44 const MetadataHandlerVtbl *vtable;
45 MetadataItem *items;
46 DWORD item_count;
47 CRITICAL_SECTION lock;
48 } MetadataHandler;
50 static inline MetadataHandler *impl_from_IWICMetadataWriter(IWICMetadataWriter *iface)
52 return CONTAINING_RECORD(iface, MetadataHandler, IWICMetadataWriter_iface);
55 static inline MetadataHandler *impl_from_IWICPersistStream(IWICPersistStream *iface)
57 return CONTAINING_RECORD(iface, MetadataHandler, IWICPersistStream_iface);
60 static void MetadataHandler_FreeItems(MetadataHandler *This)
62 DWORD i;
64 for (i=0; i<This->item_count; i++)
66 PropVariantClear(&This->items[i].schema);
67 PropVariantClear(&This->items[i].id);
68 PropVariantClear(&This->items[i].value);
71 HeapFree(GetProcessHeap(), 0, This->items);
74 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
75 IWICEnumMetadataItem **ppIEnumMetadataItem);
77 static HRESULT WINAPI MetadataHandler_QueryInterface(IWICMetadataWriter *iface, REFIID iid,
78 void **ppv)
80 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
81 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
83 if (!ppv) return E_INVALIDARG;
85 if (IsEqualIID(&IID_IUnknown, iid) ||
86 IsEqualIID(&IID_IWICMetadataReader, iid) ||
87 (IsEqualIID(&IID_IWICMetadataWriter, iid) && This->vtable->is_writer))
89 *ppv = &This->IWICMetadataWriter_iface;
91 else if (IsEqualIID(&IID_IPersist, iid) ||
92 IsEqualIID(&IID_IPersistStream, iid) ||
93 IsEqualIID(&IID_IWICPersistStream, iid))
95 *ppv = &This->IWICPersistStream_iface;
97 else
99 *ppv = NULL;
100 return E_NOINTERFACE;
103 IUnknown_AddRef((IUnknown*)*ppv);
104 return S_OK;
107 static ULONG WINAPI MetadataHandler_AddRef(IWICMetadataWriter *iface)
109 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
110 ULONG ref = InterlockedIncrement(&This->ref);
112 TRACE("(%p) refcount=%u\n", iface, ref);
114 return ref;
117 static ULONG WINAPI MetadataHandler_Release(IWICMetadataWriter *iface)
119 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
120 ULONG ref = InterlockedDecrement(&This->ref);
122 TRACE("(%p) refcount=%u\n", iface, ref);
124 if (ref == 0)
126 MetadataHandler_FreeItems(This);
127 This->lock.DebugInfo->Spare[0] = 0;
128 DeleteCriticalSection(&This->lock);
129 HeapFree(GetProcessHeap(), 0, This);
132 return ref;
135 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
136 IWICMetadataHandlerInfo **ppIHandler)
138 HRESULT hr;
139 IWICComponentInfo *component_info;
140 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
142 TRACE("%p,%p\n", iface, ppIHandler);
144 hr = CreateComponentInfo(This->vtable->clsid, &component_info);
145 if (FAILED(hr)) return hr;
147 hr = IWICComponentInfo_QueryInterface(component_info, &IID_IWICMetadataHandlerInfo,
148 (void **)ppIHandler);
150 IWICComponentInfo_Release(component_info);
151 return hr;
154 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
155 GUID *pguidMetadataFormat)
157 HRESULT hr;
158 IWICMetadataHandlerInfo *metadata_info;
160 TRACE("%p,%p\n", iface, pguidMetadataFormat);
162 if (!pguidMetadataFormat) return E_INVALIDARG;
164 hr = MetadataHandler_GetMetadataHandlerInfo(iface, &metadata_info);
165 if (FAILED(hr)) return hr;
167 hr = IWICMetadataHandlerInfo_GetMetadataFormat(metadata_info, pguidMetadataFormat);
168 IWICMetadataHandlerInfo_Release(metadata_info);
170 return hr;
173 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
174 UINT *pcCount)
176 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
178 TRACE("%p,%p\n", iface, pcCount);
180 if (!pcCount) return E_INVALIDARG;
182 *pcCount = This->item_count;
183 return S_OK;
186 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
187 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
189 HRESULT hr = S_OK;
190 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
192 TRACE("%p,%u,%p,%p,%p\n", iface, index, schema, id, value);
194 EnterCriticalSection(&This->lock);
196 if (index >= This->item_count)
198 LeaveCriticalSection(&This->lock);
199 return E_INVALIDARG;
202 if (schema)
203 hr = PropVariantCopy(schema, &This->items[index].schema);
205 if (SUCCEEDED(hr) && id)
206 hr = PropVariantCopy(id, &This->items[index].id);
208 if (SUCCEEDED(hr) && value)
209 hr = PropVariantCopy(value, &This->items[index].value);
211 LeaveCriticalSection(&This->lock);
212 return hr;
215 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
216 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
218 UINT i;
219 HRESULT hr = WINCODEC_ERR_PROPERTYNOTFOUND;
220 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
222 TRACE("(%p,%s,%s,%p)\n", iface, wine_dbgstr_variant((const VARIANT *)schema), wine_dbgstr_variant((const VARIANT *)id), value);
224 if (!id) return E_INVALIDARG;
226 EnterCriticalSection(&This->lock);
228 for (i = 0; i < This->item_count; i++)
230 if (schema && This->items[i].schema.vt != VT_EMPTY)
232 if (PropVariantCompareEx(schema, &This->items[i].schema, 0, PVCF_USESTRCMPI) != 0) continue;
235 if (PropVariantCompareEx(id, &This->items[i].id, 0, PVCF_USESTRCMPI) != 0) continue;
237 hr = value ? PropVariantCopy(value, &This->items[i].value) : S_OK;
238 break;
241 LeaveCriticalSection(&This->lock);
242 return hr;
245 static HRESULT WINAPI MetadataHandler_GetEnumerator(IWICMetadataWriter *iface,
246 IWICEnumMetadataItem **ppIEnumMetadata)
248 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
249 TRACE("(%p,%p)\n", iface, ppIEnumMetadata);
250 return MetadataHandlerEnum_Create(This, 0, ppIEnumMetadata);
253 static HRESULT WINAPI MetadataHandler_SetValue(IWICMetadataWriter *iface,
254 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
256 FIXME("(%p,%p,%p,%p): stub\n", iface, pvarSchema, pvarId, pvarValue);
257 return E_NOTIMPL;
260 static HRESULT WINAPI MetadataHandler_SetValueByIndex(IWICMetadataWriter *iface,
261 UINT nIndex, const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
263 FIXME("(%p,%u,%p,%p,%p): stub\n", iface, nIndex, pvarSchema, pvarId, pvarValue);
264 return E_NOTIMPL;
267 static HRESULT WINAPI MetadataHandler_RemoveValue(IWICMetadataWriter *iface,
268 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId)
270 FIXME("(%p,%p,%p): stub\n", iface, pvarSchema, pvarId);
271 return E_NOTIMPL;
274 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
275 UINT nIndex)
277 FIXME("(%p,%u): stub\n", iface, nIndex);
278 return E_NOTIMPL;
281 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl = {
282 MetadataHandler_QueryInterface,
283 MetadataHandler_AddRef,
284 MetadataHandler_Release,
285 MetadataHandler_GetMetadataFormat,
286 MetadataHandler_GetMetadataHandlerInfo,
287 MetadataHandler_GetCount,
288 MetadataHandler_GetValueByIndex,
289 MetadataHandler_GetValue,
290 MetadataHandler_GetEnumerator,
291 MetadataHandler_SetValue,
292 MetadataHandler_SetValueByIndex,
293 MetadataHandler_RemoveValue,
294 MetadataHandler_RemoveValueByIndex
297 static HRESULT WINAPI MetadataHandler_PersistStream_QueryInterface(IWICPersistStream *iface,
298 REFIID iid, void **ppv)
300 MetadataHandler *This = impl_from_IWICPersistStream(iface);
301 return IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
304 static ULONG WINAPI MetadataHandler_PersistStream_AddRef(IWICPersistStream *iface)
306 MetadataHandler *This = impl_from_IWICPersistStream(iface);
307 return IWICMetadataWriter_AddRef(&This->IWICMetadataWriter_iface);
310 static ULONG WINAPI MetadataHandler_PersistStream_Release(IWICPersistStream *iface)
312 MetadataHandler *This = impl_from_IWICPersistStream(iface);
313 return IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
316 static HRESULT WINAPI MetadataHandler_GetClassID(IWICPersistStream *iface,
317 CLSID *pClassID)
319 FIXME("(%p,%p): stub\n", iface, pClassID);
320 return E_NOTIMPL;
323 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
325 FIXME("(%p): stub\n", iface);
326 return E_NOTIMPL;
329 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
330 IStream *pStm)
332 MetadataHandler *This = impl_from_IWICPersistStream(iface);
333 TRACE("(%p,%p)\n", iface, pStm);
334 return IWICPersistStream_LoadEx(&This->IWICPersistStream_iface, pStm, NULL, WICPersistOptionDefault);
337 static HRESULT WINAPI MetadataHandler_Save(IWICPersistStream *iface,
338 IStream *pStm, BOOL fClearDirty)
340 FIXME("(%p,%p,%i): stub\n", iface, pStm, fClearDirty);
341 return E_NOTIMPL;
344 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
345 ULARGE_INTEGER *pcbSize)
347 FIXME("(%p,%p): stub\n", iface, pcbSize);
348 return E_NOTIMPL;
351 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
352 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
354 MetadataHandler *This = impl_from_IWICPersistStream(iface);
355 HRESULT hr;
356 MetadataItem *new_items=NULL;
357 DWORD item_count=0;
359 TRACE("(%p,%p,%s,%x)\n", iface, pIStream, debugstr_guid(pguidPreferredVendor), dwPersistOptions);
361 EnterCriticalSection(&This->lock);
363 hr = This->vtable->fnLoad(pIStream, pguidPreferredVendor, dwPersistOptions,
364 &new_items, &item_count);
366 if (SUCCEEDED(hr))
368 MetadataHandler_FreeItems(This);
369 This->items = new_items;
370 This->item_count = item_count;
373 LeaveCriticalSection(&This->lock);
375 return hr;
378 static HRESULT WINAPI MetadataHandler_SaveEx(IWICPersistStream *iface,
379 IStream *pIStream, DWORD dwPersistOptions, BOOL fClearDirty)
381 FIXME("(%p,%p,%x,%i): stub\n", iface, pIStream, dwPersistOptions, fClearDirty);
382 return E_NOTIMPL;
385 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl = {
386 MetadataHandler_PersistStream_QueryInterface,
387 MetadataHandler_PersistStream_AddRef,
388 MetadataHandler_PersistStream_Release,
389 MetadataHandler_GetClassID,
390 MetadataHandler_IsDirty,
391 MetadataHandler_Load,
392 MetadataHandler_Save,
393 MetadataHandler_GetSizeMax,
394 MetadataHandler_LoadEx,
395 MetadataHandler_SaveEx
398 HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, REFIID iid, void** ppv)
400 MetadataHandler *This;
401 HRESULT hr;
403 TRACE("%s\n", debugstr_guid(vtable->clsid));
405 *ppv = NULL;
407 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
408 if (!This) return E_OUTOFMEMORY;
410 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
411 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
412 This->ref = 1;
413 This->vtable = vtable;
414 This->items = NULL;
415 This->item_count = 0;
417 InitializeCriticalSection(&This->lock);
418 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
420 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
422 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
424 return hr;
427 typedef struct MetadataHandlerEnum {
428 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
429 LONG ref;
430 MetadataHandler *parent;
431 DWORD index;
432 } MetadataHandlerEnum;
434 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
436 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
439 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
440 void **ppv)
442 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
443 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
445 if (!ppv) return E_INVALIDARG;
447 if (IsEqualIID(&IID_IUnknown, iid) ||
448 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
450 *ppv = &This->IWICEnumMetadataItem_iface;
452 else
454 *ppv = NULL;
455 return E_NOINTERFACE;
458 IUnknown_AddRef((IUnknown*)*ppv);
459 return S_OK;
462 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
464 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
465 ULONG ref = InterlockedIncrement(&This->ref);
467 TRACE("(%p) refcount=%u\n", iface, ref);
469 return ref;
472 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
474 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
475 ULONG ref = InterlockedDecrement(&This->ref);
477 TRACE("(%p) refcount=%u\n", iface, ref);
479 if (ref == 0)
481 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
482 HeapFree(GetProcessHeap(), 0, This);
485 return ref;
488 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
489 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
490 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
492 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
493 ULONG new_index;
494 HRESULT hr=S_FALSE;
495 ULONG i;
497 TRACE("(%p,%i)\n", iface, celt);
499 EnterCriticalSection(&This->parent->lock);
501 if (This->index >= This->parent->item_count)
503 *pceltFetched = 0;
504 LeaveCriticalSection(&This->parent->lock);
505 return S_FALSE;
508 new_index = min(This->parent->item_count, This->index + celt);
509 *pceltFetched = new_index - This->index;
511 if (rgeltSchema)
513 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
514 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
517 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
518 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
520 if (rgeltValue)
522 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
523 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
526 if (SUCCEEDED(hr))
528 This->index = new_index;
531 LeaveCriticalSection(&This->parent->lock);
533 return hr;
536 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
537 ULONG celt)
539 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
541 EnterCriticalSection(&This->parent->lock);
543 This->index += celt;
545 LeaveCriticalSection(&This->parent->lock);
547 return S_OK;
550 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
552 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
554 EnterCriticalSection(&This->parent->lock);
556 This->index = 0;
558 LeaveCriticalSection(&This->parent->lock);
560 return S_OK;
563 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
564 IWICEnumMetadataItem **ppIEnumMetadataItem)
566 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
567 HRESULT hr;
569 EnterCriticalSection(&This->parent->lock);
571 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
573 LeaveCriticalSection(&This->parent->lock);
575 return hr;
578 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
579 MetadataHandlerEnum_QueryInterface,
580 MetadataHandlerEnum_AddRef,
581 MetadataHandlerEnum_Release,
582 MetadataHandlerEnum_Next,
583 MetadataHandlerEnum_Skip,
584 MetadataHandlerEnum_Reset,
585 MetadataHandlerEnum_Clone
588 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
589 IWICEnumMetadataItem **ppIEnumMetadataItem)
591 MetadataHandlerEnum *This;
593 if (!ppIEnumMetadataItem) return E_INVALIDARG;
595 *ppIEnumMetadataItem = NULL;
597 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
598 if (!This) return E_OUTOFMEMORY;
600 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
602 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
603 This->ref = 1;
604 This->parent = parent;
605 This->index = index;
607 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
609 return S_OK;
612 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
613 DWORD persist_options, MetadataItem **items, DWORD *item_count)
615 HRESULT hr;
616 MetadataItem *result;
617 STATSTG stat;
618 BYTE *data;
619 ULONG bytesread;
621 TRACE("\n");
623 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
624 if (FAILED(hr))
625 return hr;
627 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
628 if (!data) return E_OUTOFMEMORY;
630 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
631 if (bytesread != stat.cbSize.QuadPart) hr = E_FAIL;
632 if (hr != S_OK)
634 HeapFree(GetProcessHeap(), 0, data);
635 return hr;
638 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
639 if (!result)
641 HeapFree(GetProcessHeap(), 0, data);
642 return E_OUTOFMEMORY;
645 PropVariantInit(&result[0].schema);
646 PropVariantInit(&result[0].id);
647 PropVariantInit(&result[0].value);
649 result[0].value.vt = VT_BLOB;
650 result[0].value.u.blob.cbSize = bytesread;
651 result[0].value.u.blob.pBlobData = data;
653 *items = result;
654 *item_count = 1;
656 return S_OK;
659 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
661 &CLSID_WICUnknownMetadataReader,
662 LoadUnknownMetadata
665 HRESULT UnknownMetadataReader_CreateInstance(REFIID iid, void** ppv)
667 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, iid, ppv);
670 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
671 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
672 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
674 struct IFD_entry
676 SHORT id;
677 SHORT type;
678 ULONG count;
679 LONG value;
682 #define IFD_BYTE 1
683 #define IFD_ASCII 2
684 #define IFD_SHORT 3
685 #define IFD_LONG 4
686 #define IFD_RATIONAL 5
687 #define IFD_SBYTE 6
688 #define IFD_UNDEFINED 7
689 #define IFD_SSHORT 8
690 #define IFD_SLONG 9
691 #define IFD_SRATIONAL 10
692 #define IFD_FLOAT 11
693 #define IFD_DOUBLE 12
694 #define IFD_IFD 13
696 static int tag_to_vt(SHORT tag)
698 static const int tag2vt[] =
700 VT_EMPTY, /* 0 */
701 VT_UI1, /* IFD_BYTE 1 */
702 VT_LPSTR, /* IFD_ASCII 2 */
703 VT_UI2, /* IFD_SHORT 3 */
704 VT_UI4, /* IFD_LONG 4 */
705 VT_UI8, /* IFD_RATIONAL 5 */
706 VT_I1, /* IFD_SBYTE 6 */
707 VT_BLOB, /* IFD_UNDEFINED 7 */
708 VT_I2, /* IFD_SSHORT 8 */
709 VT_I4, /* IFD_SLONG 9 */
710 VT_I8, /* IFD_SRATIONAL 10 */
711 VT_R4, /* IFD_FLOAT 11 */
712 VT_R8, /* IFD_DOUBLE 12 */
713 VT_BLOB, /* IFD_IFD 13 */
715 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
718 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
719 MetadataItem *item, BOOL native_byte_order)
721 ULONG count, value, i, bytesread;
722 SHORT type;
723 LARGE_INTEGER pos;
724 HRESULT hr;
726 item->schema.vt = VT_EMPTY;
727 item->id.vt = VT_UI2;
728 item->id.u.uiVal = entry->id;
729 SWAP_USHORT(item->id.u.uiVal);
731 count = entry->count;
732 SWAP_ULONG(count);
733 type = entry->type;
734 SWAP_USHORT(type);
735 item->value.vt = tag_to_vt(type);
736 value = entry->value;
737 SWAP_ULONG(value);
739 switch (type)
741 case IFD_BYTE:
742 case IFD_SBYTE:
743 if (!count) count = 1;
745 if (count <= 4)
747 const BYTE *data = (const BYTE *)&entry->value;
749 if (count == 1)
750 item->value.u.bVal = data[0];
751 else
753 item->value.vt |= VT_VECTOR;
754 item->value.u.caub.cElems = count;
755 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
756 memcpy(item->value.u.caub.pElems, data, count);
758 break;
761 item->value.vt |= VT_VECTOR;
762 item->value.u.caub.cElems = count;
763 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
764 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
766 pos.QuadPart = value;
767 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
768 if (FAILED(hr))
770 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
771 return hr;
773 hr = IStream_Read(input, item->value.u.caub.pElems, count, &bytesread);
774 if (bytesread != count) hr = E_FAIL;
775 if (hr != S_OK)
777 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
778 return hr;
780 break;
781 case IFD_SHORT:
782 case IFD_SSHORT:
783 if (!count) count = 1;
785 if (count <= 2)
787 const SHORT *data = (const SHORT *)&entry->value;
789 if (count == 1)
791 item->value.u.uiVal = data[0];
792 SWAP_USHORT(item->value.u.uiVal);
794 else
796 item->value.vt |= VT_VECTOR;
797 item->value.u.caui.cElems = count;
798 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
799 memcpy(item->value.u.caui.pElems, data, count * 2);
800 for (i = 0; i < count; i++)
801 SWAP_USHORT(item->value.u.caui.pElems[i]);
803 break;
806 item->value.vt |= VT_VECTOR;
807 item->value.u.caui.cElems = count;
808 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
809 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
811 pos.QuadPart = value;
812 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
813 if (FAILED(hr))
815 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
816 return hr;
818 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, &bytesread);
819 if (bytesread != count * 2) hr = E_FAIL;
820 if (hr != S_OK)
822 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
823 return hr;
825 for (i = 0; i < count; i++)
826 SWAP_USHORT(item->value.u.caui.pElems[i]);
827 break;
828 case IFD_LONG:
829 case IFD_SLONG:
830 case IFD_FLOAT:
831 if (!count) count = 1;
833 if (count == 1)
835 item->value.u.ulVal = value;
836 break;
839 item->value.vt |= VT_VECTOR;
840 item->value.u.caul.cElems = count;
841 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
842 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
844 pos.QuadPart = value;
845 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
846 if (FAILED(hr))
848 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
849 return hr;
851 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, &bytesread);
852 if (bytesread != count * 4) hr = E_FAIL;
853 if (hr != S_OK)
855 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
856 return hr;
858 for (i = 0; i < count; i++)
859 SWAP_ULONG(item->value.u.caul.pElems[i]);
860 break;
861 case IFD_RATIONAL:
862 case IFD_SRATIONAL:
863 case IFD_DOUBLE:
864 if (!count)
866 FIXME("IFD field type %d, count 0\n", type);
867 item->value.vt = VT_EMPTY;
868 break;
871 if (count == 1)
873 ULONGLONG ull;
875 pos.QuadPart = value;
876 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
877 if (FAILED(hr)) return hr;
879 hr = IStream_Read(input, &ull, sizeof(ull), &bytesread);
880 if (bytesread != sizeof(ull)) hr = E_FAIL;
881 if (hr != S_OK) return hr;
883 item->value.u.uhVal.QuadPart = ull;
885 if (type == IFD_DOUBLE)
886 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
887 else
889 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
890 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
892 break;
894 else
896 item->value.vt |= VT_VECTOR;
897 item->value.u.cauh.cElems = count;
898 item->value.u.cauh.pElems = HeapAlloc(GetProcessHeap(), 0, count * 8);
899 if (!item->value.u.cauh.pElems) return E_OUTOFMEMORY;
901 pos.QuadPart = value;
902 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
903 if (FAILED(hr))
905 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
906 return hr;
908 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, &bytesread);
909 if (bytesread != count * 8) hr = E_FAIL;
910 if (hr != S_OK)
912 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
913 return hr;
915 for (i = 0; i < count; i++)
917 if (type == IFD_DOUBLE)
918 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
919 else
921 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
922 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
926 break;
927 case IFD_ASCII:
928 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
929 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
931 if (count <= 4)
933 const char *data = (const char *)&entry->value;
934 memcpy(item->value.u.pszVal, data, count);
935 item->value.u.pszVal[count] = 0;
936 break;
939 pos.QuadPart = value;
940 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
941 if (FAILED(hr))
943 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
944 return hr;
946 hr = IStream_Read(input, item->value.u.pszVal, count, &bytesread);
947 if (bytesread != count) hr = E_FAIL;
948 if (hr != S_OK)
950 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
951 return hr;
953 item->value.u.pszVal[count] = 0;
954 break;
955 case IFD_UNDEFINED:
956 if (!count)
958 FIXME("IFD field type %d, count 0\n", type);
959 item->value.vt = VT_EMPTY;
960 break;
963 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
964 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
966 item->value.u.blob.cbSize = count;
968 if (count <= 4)
970 const char *data = (const char *)&entry->value;
971 memcpy(item->value.u.blob.pBlobData, data, count);
972 break;
975 pos.QuadPart = value;
976 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
977 if (FAILED(hr))
979 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
980 return hr;
982 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, &bytesread);
983 if (bytesread != count) hr = E_FAIL;
984 if (hr != S_OK)
986 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
987 return hr;
989 break;
990 default:
991 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
992 break;
994 return S_OK;
997 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
998 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1000 HRESULT hr;
1001 MetadataItem *result;
1002 USHORT count, i;
1003 struct IFD_entry *entry;
1004 BOOL native_byte_order = TRUE;
1005 ULONG bytesread;
1007 TRACE("\n");
1009 #ifdef WORDS_BIGENDIAN
1010 if (persist_options & WICPersistOptionLittleEndian)
1011 #else
1012 if (persist_options & WICPersistOptionBigEndian)
1013 #endif
1014 native_byte_order = FALSE;
1016 hr = IStream_Read(input, &count, sizeof(count), &bytesread);
1017 if (bytesread != sizeof(count)) hr = E_FAIL;
1018 if (hr != S_OK) return hr;
1020 SWAP_USHORT(count);
1022 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1023 if (!entry) return E_OUTOFMEMORY;
1025 hr = IStream_Read(input, entry, count * sizeof(*entry), &bytesread);
1026 if (bytesread != count * sizeof(*entry)) hr = E_FAIL;
1027 if (hr != S_OK)
1029 HeapFree(GetProcessHeap(), 0, entry);
1030 return hr;
1033 /* limit number of IFDs to 4096 to avoid infinite loop */
1034 for (i = 0; i < 4096; i++)
1036 ULONG next_ifd_offset;
1037 LARGE_INTEGER pos;
1038 USHORT next_ifd_count;
1040 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), &bytesread);
1041 if (bytesread != sizeof(next_ifd_offset)) hr = E_FAIL;
1042 if (hr != S_OK) break;
1044 SWAP_ULONG(next_ifd_offset);
1045 if (!next_ifd_offset) break;
1047 pos.QuadPart = next_ifd_offset;
1048 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1049 if (FAILED(hr)) break;
1051 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), &bytesread);
1052 if (bytesread != sizeof(next_ifd_count)) hr = E_FAIL;
1053 if (hr != S_OK) break;
1055 SWAP_USHORT(next_ifd_count);
1057 pos.QuadPart = next_ifd_count * sizeof(*entry);
1058 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1059 if (FAILED(hr)) break;
1062 if (hr != S_OK || i == 4096)
1064 HeapFree(GetProcessHeap(), 0, entry);
1065 return WINCODEC_ERR_BADMETADATAHEADER;
1068 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(*result));
1069 if (!result)
1071 HeapFree(GetProcessHeap(), 0, entry);
1072 return E_OUTOFMEMORY;
1075 for (i = 0; i < count; i++)
1077 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1078 if (FAILED(hr))
1080 HeapFree(GetProcessHeap(), 0, entry);
1081 HeapFree(GetProcessHeap(), 0, result);
1082 return hr;
1086 HeapFree(GetProcessHeap(), 0, entry);
1088 *items = result;
1089 *item_count = count;
1091 return S_OK;
1094 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1096 &CLSID_WICIfdMetadataReader,
1097 LoadIfdMetadata
1100 HRESULT IfdMetadataReader_CreateInstance(REFIID iid, void **ppv)
1102 return MetadataReader_Create(&IfdMetadataReader_Vtbl, iid, ppv);