Sync with trunk rev.61910 to get latest improvements and bugfixes.
[reactos.git] / dll / win32 / windowscodecs / metadatahandler.c
1 /*
2 * Copyright 2012 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "wincodecs_private.h"
21
22 #include <stdio.h>
23 #include <winternl.h>
24
25 typedef struct MetadataHandler {
26 IWICMetadataWriter IWICMetadataWriter_iface;
27 LONG ref;
28 IWICPersistStream IWICPersistStream_iface;
29 const MetadataHandlerVtbl *vtable;
30 MetadataItem *items;
31 DWORD item_count;
32 CRITICAL_SECTION lock;
33 } MetadataHandler;
34
35 static inline MetadataHandler *impl_from_IWICMetadataWriter(IWICMetadataWriter *iface)
36 {
37 return CONTAINING_RECORD(iface, MetadataHandler, IWICMetadataWriter_iface);
38 }
39
40 static inline MetadataHandler *impl_from_IWICPersistStream(IWICPersistStream *iface)
41 {
42 return CONTAINING_RECORD(iface, MetadataHandler, IWICPersistStream_iface);
43 }
44
45 static void MetadataHandler_FreeItems(MetadataHandler *This)
46 {
47 DWORD i;
48
49 for (i=0; i<This->item_count; i++)
50 {
51 PropVariantClear(&This->items[i].schema);
52 PropVariantClear(&This->items[i].id);
53 PropVariantClear(&This->items[i].value);
54 }
55
56 HeapFree(GetProcessHeap(), 0, This->items);
57 }
58
59 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
60 IWICEnumMetadataItem **ppIEnumMetadataItem);
61
62 static HRESULT WINAPI MetadataHandler_QueryInterface(IWICMetadataWriter *iface, REFIID iid,
63 void **ppv)
64 {
65 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
66 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
67
68 if (!ppv) return E_INVALIDARG;
69
70 if (IsEqualIID(&IID_IUnknown, iid) ||
71 IsEqualIID(&IID_IWICMetadataReader, iid) ||
72 (IsEqualIID(&IID_IWICMetadataWriter, iid) && This->vtable->is_writer))
73 {
74 *ppv = &This->IWICMetadataWriter_iface;
75 }
76 else if (IsEqualIID(&IID_IPersist, iid) ||
77 IsEqualIID(&IID_IPersistStream, iid) ||
78 IsEqualIID(&IID_IWICPersistStream, iid))
79 {
80 *ppv = &This->IWICPersistStream_iface;
81 }
82 else
83 {
84 *ppv = NULL;
85 return E_NOINTERFACE;
86 }
87
88 IUnknown_AddRef((IUnknown*)*ppv);
89 return S_OK;
90 }
91
92 static ULONG WINAPI MetadataHandler_AddRef(IWICMetadataWriter *iface)
93 {
94 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
95 ULONG ref = InterlockedIncrement(&This->ref);
96
97 TRACE("(%p) refcount=%u\n", iface, ref);
98
99 return ref;
100 }
101
102 static ULONG WINAPI MetadataHandler_Release(IWICMetadataWriter *iface)
103 {
104 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
105 ULONG ref = InterlockedDecrement(&This->ref);
106
107 TRACE("(%p) refcount=%u\n", iface, ref);
108
109 if (ref == 0)
110 {
111 MetadataHandler_FreeItems(This);
112 This->lock.DebugInfo->Spare[0] = 0;
113 DeleteCriticalSection(&This->lock);
114 HeapFree(GetProcessHeap(), 0, This);
115 }
116
117 return ref;
118 }
119
120 static HRESULT WINAPI MetadataHandler_GetMetadataHandlerInfo(IWICMetadataWriter *iface,
121 IWICMetadataHandlerInfo **ppIHandler)
122 {
123 HRESULT hr;
124 IWICComponentInfo *component_info;
125 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
126
127 TRACE("%p,%p\n", iface, ppIHandler);
128
129 hr = CreateComponentInfo(This->vtable->clsid, &component_info);
130 if (FAILED(hr)) return hr;
131
132 hr = IWICComponentInfo_QueryInterface(component_info, &IID_IWICMetadataHandlerInfo,
133 (void **)ppIHandler);
134
135 IWICComponentInfo_Release(component_info);
136 return hr;
137 }
138
139 static HRESULT WINAPI MetadataHandler_GetMetadataFormat(IWICMetadataWriter *iface,
140 GUID *pguidMetadataFormat)
141 {
142 HRESULT hr;
143 IWICMetadataHandlerInfo *metadata_info;
144
145 TRACE("%p,%p\n", iface, pguidMetadataFormat);
146
147 if (!pguidMetadataFormat) return E_INVALIDARG;
148
149 hr = MetadataHandler_GetMetadataHandlerInfo(iface, &metadata_info);
150 if (FAILED(hr)) return hr;
151
152 hr = IWICMetadataHandlerInfo_GetMetadataFormat(metadata_info, pguidMetadataFormat);
153 IWICMetadataHandlerInfo_Release(metadata_info);
154
155 return hr;
156 }
157
158 static HRESULT WINAPI MetadataHandler_GetCount(IWICMetadataWriter *iface,
159 UINT *pcCount)
160 {
161 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
162
163 TRACE("%p,%p\n", iface, pcCount);
164
165 if (!pcCount) return E_INVALIDARG;
166
167 *pcCount = This->item_count;
168 return S_OK;
169 }
170
171 static HRESULT WINAPI MetadataHandler_GetValueByIndex(IWICMetadataWriter *iface,
172 UINT index, PROPVARIANT *schema, PROPVARIANT *id, PROPVARIANT *value)
173 {
174 HRESULT hr = S_OK;
175 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
176
177 TRACE("%p,%u,%p,%p,%p\n", iface, index, schema, id, value);
178
179 EnterCriticalSection(&This->lock);
180
181 if (index >= This->item_count)
182 {
183 LeaveCriticalSection(&This->lock);
184 return E_INVALIDARG;
185 }
186
187 if (schema)
188 hr = PropVariantCopy(schema, &This->items[index].schema);
189
190 if (SUCCEEDED(hr) && id)
191 hr = PropVariantCopy(id, &This->items[index].id);
192
193 if (SUCCEEDED(hr) && value)
194 hr = PropVariantCopy(value, &This->items[index].value);
195
196 LeaveCriticalSection(&This->lock);
197 return hr;
198 }
199
200 static BOOL get_int_value(const PROPVARIANT *pv, LONGLONG *value)
201 {
202 switch (pv->vt)
203 {
204 case VT_NULL:
205 case VT_EMPTY:
206 *value = 0;
207 break;
208 case VT_I1:
209 *value = pv->u.cVal;
210 break;
211 case VT_UI1:
212 *value = pv->u.bVal;
213 break;
214 case VT_I2:
215 *value = pv->u.iVal;
216 break;
217 case VT_UI2:
218 *value = pv->u.uiVal;
219 break;
220 case VT_I4:
221 *value = pv->u.lVal;
222 break;
223 case VT_UI4:
224 *value = pv->u.ulVal;
225 break;
226 case VT_I8:
227 case VT_UI8:
228 *value = pv->u.hVal.QuadPart;
229 break;
230 default:
231 FIXME("not supported variant type %d\n", pv->vt);
232 return FALSE;
233 }
234 return TRUE;
235 }
236
237 /* FiXME: Use propsys.PropVariantCompareEx once it's implemented */
238 static int propvar_cmp(const PROPVARIANT *v1, const PROPVARIANT *v2)
239 {
240 LONGLONG value1, value2;
241
242 if (v1->vt == VT_LPSTR && v2->vt == VT_LPSTR)
243 {
244 return lstrcmpA(v1->u.pszVal, v2->u.pszVal);
245 }
246
247 if (v1->vt == VT_LPWSTR && v2->vt == VT_LPWSTR)
248 {
249 return lstrcmpiW(v1->u.pwszVal, v2->u.pwszVal);
250 }
251
252 if (!get_int_value(v1, &value1)) return -1;
253 if (!get_int_value(v2, &value2)) return -1;
254
255 value1 -= value2;
256 if (value1) return value1 < 0 ? -1 : 1;
257 return 0;
258 }
259
260 static HRESULT WINAPI MetadataHandler_GetValue(IWICMetadataWriter *iface,
261 const PROPVARIANT *schema, const PROPVARIANT *id, PROPVARIANT *value)
262 {
263 UINT i;
264 HRESULT hr = WINCODEC_ERR_PROPERTYNOTFOUND;
265 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
266
267 TRACE("(%p,%p,%p,%p)\n", iface, schema, id, value);
268
269 if (!id) return E_INVALIDARG;
270
271 EnterCriticalSection(&This->lock);
272
273 for (i = 0; i < This->item_count; i++)
274 {
275 if (schema && This->items[i].schema.vt != VT_EMPTY)
276 {
277 if (propvar_cmp(schema, &This->items[i].schema) != 0) continue;
278 }
279
280 if (propvar_cmp(id, &This->items[i].id) != 0) continue;
281
282 hr = value ? PropVariantCopy(value, &This->items[i].value) : S_OK;
283 break;
284 }
285
286 LeaveCriticalSection(&This->lock);
287 return hr;
288 }
289
290 static HRESULT WINAPI MetadataHandler_GetEnumerator(IWICMetadataWriter *iface,
291 IWICEnumMetadataItem **ppIEnumMetadata)
292 {
293 MetadataHandler *This = impl_from_IWICMetadataWriter(iface);
294 TRACE("(%p,%p)\n", iface, ppIEnumMetadata);
295 return MetadataHandlerEnum_Create(This, 0, ppIEnumMetadata);
296 }
297
298 static HRESULT WINAPI MetadataHandler_SetValue(IWICMetadataWriter *iface,
299 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
300 {
301 FIXME("(%p,%p,%p,%p): stub\n", iface, pvarSchema, pvarId, pvarValue);
302 return E_NOTIMPL;
303 }
304
305 static HRESULT WINAPI MetadataHandler_SetValueByIndex(IWICMetadataWriter *iface,
306 UINT nIndex, const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId, const PROPVARIANT *pvarValue)
307 {
308 FIXME("(%p,%u,%p,%p,%p): stub\n", iface, nIndex, pvarSchema, pvarId, pvarValue);
309 return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI MetadataHandler_RemoveValue(IWICMetadataWriter *iface,
313 const PROPVARIANT *pvarSchema, const PROPVARIANT *pvarId)
314 {
315 FIXME("(%p,%p,%p): stub\n", iface, pvarSchema, pvarId);
316 return E_NOTIMPL;
317 }
318
319 static HRESULT WINAPI MetadataHandler_RemoveValueByIndex(IWICMetadataWriter *iface,
320 UINT nIndex)
321 {
322 FIXME("(%p,%u): stub\n", iface, nIndex);
323 return E_NOTIMPL;
324 }
325
326 static const IWICMetadataWriterVtbl MetadataHandler_Vtbl = {
327 MetadataHandler_QueryInterface,
328 MetadataHandler_AddRef,
329 MetadataHandler_Release,
330 MetadataHandler_GetMetadataFormat,
331 MetadataHandler_GetMetadataHandlerInfo,
332 MetadataHandler_GetCount,
333 MetadataHandler_GetValueByIndex,
334 MetadataHandler_GetValue,
335 MetadataHandler_GetEnumerator,
336 MetadataHandler_SetValue,
337 MetadataHandler_SetValueByIndex,
338 MetadataHandler_RemoveValue,
339 MetadataHandler_RemoveValueByIndex
340 };
341
342 static HRESULT WINAPI MetadataHandler_PersistStream_QueryInterface(IWICPersistStream *iface,
343 REFIID iid, void **ppv)
344 {
345 MetadataHandler *This = impl_from_IWICPersistStream(iface);
346 return IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
347 }
348
349 static ULONG WINAPI MetadataHandler_PersistStream_AddRef(IWICPersistStream *iface)
350 {
351 MetadataHandler *This = impl_from_IWICPersistStream(iface);
352 return IWICMetadataWriter_AddRef(&This->IWICMetadataWriter_iface);
353 }
354
355 static ULONG WINAPI MetadataHandler_PersistStream_Release(IWICPersistStream *iface)
356 {
357 MetadataHandler *This = impl_from_IWICPersistStream(iface);
358 return IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
359 }
360
361 static HRESULT WINAPI MetadataHandler_GetClassID(IWICPersistStream *iface,
362 CLSID *pClassID)
363 {
364 FIXME("(%p,%p): stub\n", iface, pClassID);
365 return E_NOTIMPL;
366 }
367
368 static HRESULT WINAPI MetadataHandler_IsDirty(IWICPersistStream *iface)
369 {
370 FIXME("(%p): stub\n", iface);
371 return E_NOTIMPL;
372 }
373
374 static HRESULT WINAPI MetadataHandler_Load(IWICPersistStream *iface,
375 IStream *pStm)
376 {
377 MetadataHandler *This = impl_from_IWICPersistStream(iface);
378 TRACE("(%p,%p)\n", iface, pStm);
379 return IWICPersistStream_LoadEx(&This->IWICPersistStream_iface, pStm, NULL, WICPersistOptionsDefault);
380 }
381
382 static HRESULT WINAPI MetadataHandler_Save(IWICPersistStream *iface,
383 IStream *pStm, BOOL fClearDirty)
384 {
385 FIXME("(%p,%p,%i): stub\n", iface, pStm, fClearDirty);
386 return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI MetadataHandler_GetSizeMax(IWICPersistStream *iface,
390 ULARGE_INTEGER *pcbSize)
391 {
392 FIXME("(%p,%p): stub\n", iface, pcbSize);
393 return E_NOTIMPL;
394 }
395
396 static HRESULT WINAPI MetadataHandler_LoadEx(IWICPersistStream *iface,
397 IStream *pIStream, const GUID *pguidPreferredVendor, DWORD dwPersistOptions)
398 {
399 MetadataHandler *This = impl_from_IWICPersistStream(iface);
400 HRESULT hr;
401 MetadataItem *new_items=NULL;
402 DWORD item_count=0;
403
404 TRACE("(%p,%p,%s,%x)\n", iface, pIStream, debugstr_guid(pguidPreferredVendor), dwPersistOptions);
405
406 EnterCriticalSection(&This->lock);
407
408 hr = This->vtable->fnLoad(pIStream, pguidPreferredVendor, dwPersistOptions,
409 &new_items, &item_count);
410
411 if (SUCCEEDED(hr))
412 {
413 MetadataHandler_FreeItems(This);
414 This->items = new_items;
415 This->item_count = item_count;
416 }
417
418 LeaveCriticalSection(&This->lock);
419
420 return hr;
421 }
422
423 static HRESULT WINAPI MetadataHandler_SaveEx(IWICPersistStream *iface,
424 IStream *pIStream, DWORD dwPersistOptions, BOOL fClearDirty)
425 {
426 FIXME("(%p,%p,%x,%i): stub\n", iface, pIStream, dwPersistOptions, fClearDirty);
427 return E_NOTIMPL;
428 }
429
430 static const IWICPersistStreamVtbl MetadataHandler_PersistStream_Vtbl = {
431 MetadataHandler_PersistStream_QueryInterface,
432 MetadataHandler_PersistStream_AddRef,
433 MetadataHandler_PersistStream_Release,
434 MetadataHandler_GetClassID,
435 MetadataHandler_IsDirty,
436 MetadataHandler_Load,
437 MetadataHandler_Save,
438 MetadataHandler_GetSizeMax,
439 MetadataHandler_LoadEx,
440 MetadataHandler_SaveEx
441 };
442
443 HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, IUnknown *pUnkOuter, REFIID iid, void** ppv)
444 {
445 MetadataHandler *This;
446 HRESULT hr;
447
448 TRACE("%s\n", debugstr_guid(vtable->clsid));
449
450 *ppv = NULL;
451
452 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
453
454 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandler));
455 if (!This) return E_OUTOFMEMORY;
456
457 This->IWICMetadataWriter_iface.lpVtbl = &MetadataHandler_Vtbl;
458 This->IWICPersistStream_iface.lpVtbl = &MetadataHandler_PersistStream_Vtbl;
459 This->ref = 1;
460 This->vtable = vtable;
461 This->items = NULL;
462 This->item_count = 0;
463
464 InitializeCriticalSection(&This->lock);
465 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MetadataHandler.lock");
466
467 hr = IWICMetadataWriter_QueryInterface(&This->IWICMetadataWriter_iface, iid, ppv);
468
469 IWICMetadataWriter_Release(&This->IWICMetadataWriter_iface);
470
471 return hr;
472 }
473
474 typedef struct MetadataHandlerEnum {
475 IWICEnumMetadataItem IWICEnumMetadataItem_iface;
476 LONG ref;
477 MetadataHandler *parent;
478 DWORD index;
479 } MetadataHandlerEnum;
480
481 static inline MetadataHandlerEnum *impl_from_IWICEnumMetadataItem(IWICEnumMetadataItem *iface)
482 {
483 return CONTAINING_RECORD(iface, MetadataHandlerEnum, IWICEnumMetadataItem_iface);
484 }
485
486 static HRESULT WINAPI MetadataHandlerEnum_QueryInterface(IWICEnumMetadataItem *iface, REFIID iid,
487 void **ppv)
488 {
489 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
490 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
491
492 if (!ppv) return E_INVALIDARG;
493
494 if (IsEqualIID(&IID_IUnknown, iid) ||
495 IsEqualIID(&IID_IWICEnumMetadataItem, iid))
496 {
497 *ppv = &This->IWICEnumMetadataItem_iface;
498 }
499 else
500 {
501 *ppv = NULL;
502 return E_NOINTERFACE;
503 }
504
505 IUnknown_AddRef((IUnknown*)*ppv);
506 return S_OK;
507 }
508
509 static ULONG WINAPI MetadataHandlerEnum_AddRef(IWICEnumMetadataItem *iface)
510 {
511 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
512 ULONG ref = InterlockedIncrement(&This->ref);
513
514 TRACE("(%p) refcount=%u\n", iface, ref);
515
516 return ref;
517 }
518
519 static ULONG WINAPI MetadataHandlerEnum_Release(IWICEnumMetadataItem *iface)
520 {
521 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
522 ULONG ref = InterlockedDecrement(&This->ref);
523
524 TRACE("(%p) refcount=%u\n", iface, ref);
525
526 if (ref == 0)
527 {
528 IWICMetadataWriter_Release(&This->parent->IWICMetadataWriter_iface);
529 HeapFree(GetProcessHeap(), 0, This);
530 }
531
532 return ref;
533 }
534
535 static HRESULT WINAPI MetadataHandlerEnum_Next(IWICEnumMetadataItem *iface,
536 ULONG celt, PROPVARIANT *rgeltSchema, PROPVARIANT *rgeltId,
537 PROPVARIANT *rgeltValue, ULONG *pceltFetched)
538 {
539 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
540 ULONG new_index;
541 HRESULT hr=S_FALSE;
542 ULONG i;
543
544 TRACE("(%p,%i)\n", iface, celt);
545
546 EnterCriticalSection(&This->parent->lock);
547
548 if (This->index >= This->parent->item_count)
549 {
550 *pceltFetched = 0;
551 LeaveCriticalSection(&This->parent->lock);
552 return S_FALSE;
553 }
554
555 new_index = min(This->parent->item_count, This->index + celt);
556 *pceltFetched = new_index - This->index;
557
558 if (rgeltSchema)
559 {
560 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
561 hr = PropVariantCopy(&rgeltSchema[i], &This->parent->items[i+This->index].schema);
562 }
563
564 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
565 hr = PropVariantCopy(&rgeltId[i], &This->parent->items[i+This->index].id);
566
567 if (rgeltValue)
568 {
569 for (i=0; SUCCEEDED(hr) && i < *pceltFetched; i++)
570 hr = PropVariantCopy(&rgeltValue[i], &This->parent->items[i+This->index].value);
571 }
572
573 if (SUCCEEDED(hr))
574 {
575 This->index = new_index;
576 }
577
578 LeaveCriticalSection(&This->parent->lock);
579
580 return hr;
581 }
582
583 static HRESULT WINAPI MetadataHandlerEnum_Skip(IWICEnumMetadataItem *iface,
584 ULONG celt)
585 {
586 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
587
588 EnterCriticalSection(&This->parent->lock);
589
590 This->index += celt;
591
592 LeaveCriticalSection(&This->parent->lock);
593
594 return S_OK;
595 }
596
597 static HRESULT WINAPI MetadataHandlerEnum_Reset(IWICEnumMetadataItem *iface)
598 {
599 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
600
601 EnterCriticalSection(&This->parent->lock);
602
603 This->index = 0;
604
605 LeaveCriticalSection(&This->parent->lock);
606
607 return S_OK;
608 }
609
610 static HRESULT WINAPI MetadataHandlerEnum_Clone(IWICEnumMetadataItem *iface,
611 IWICEnumMetadataItem **ppIEnumMetadataItem)
612 {
613 MetadataHandlerEnum *This = impl_from_IWICEnumMetadataItem(iface);
614 HRESULT hr;
615
616 EnterCriticalSection(&This->parent->lock);
617
618 hr = MetadataHandlerEnum_Create(This->parent, This->index, ppIEnumMetadataItem);
619
620 LeaveCriticalSection(&This->parent->lock);
621
622 return hr;
623 }
624
625 static const IWICEnumMetadataItemVtbl MetadataHandlerEnum_Vtbl = {
626 MetadataHandlerEnum_QueryInterface,
627 MetadataHandlerEnum_AddRef,
628 MetadataHandlerEnum_Release,
629 MetadataHandlerEnum_Next,
630 MetadataHandlerEnum_Skip,
631 MetadataHandlerEnum_Reset,
632 MetadataHandlerEnum_Clone
633 };
634
635 static HRESULT MetadataHandlerEnum_Create(MetadataHandler *parent, DWORD index,
636 IWICEnumMetadataItem **ppIEnumMetadataItem)
637 {
638 MetadataHandlerEnum *This;
639
640 if (!ppIEnumMetadataItem) return E_INVALIDARG;
641
642 *ppIEnumMetadataItem = NULL;
643
644 This = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataHandlerEnum));
645 if (!This) return E_OUTOFMEMORY;
646
647 IWICMetadataWriter_AddRef(&parent->IWICMetadataWriter_iface);
648
649 This->IWICEnumMetadataItem_iface.lpVtbl = &MetadataHandlerEnum_Vtbl;
650 This->ref = 1;
651 This->parent = parent;
652 This->index = index;
653
654 *ppIEnumMetadataItem = &This->IWICEnumMetadataItem_iface;
655
656 return S_OK;
657 }
658
659 static HRESULT LoadUnknownMetadata(IStream *input, const GUID *preferred_vendor,
660 DWORD persist_options, MetadataItem **items, DWORD *item_count)
661 {
662 HRESULT hr;
663 MetadataItem *result;
664 STATSTG stat;
665 BYTE *data;
666 ULONG bytesread;
667
668 TRACE("\n");
669
670 hr = IStream_Stat(input, &stat, STATFLAG_NONAME);
671 if (FAILED(hr))
672 return hr;
673
674 data = HeapAlloc(GetProcessHeap(), 0, stat.cbSize.QuadPart);
675 if (!data) return E_OUTOFMEMORY;
676
677 hr = IStream_Read(input, data, stat.cbSize.QuadPart, &bytesread);
678 if (bytesread != stat.cbSize.QuadPart) hr = E_FAIL;
679 if (hr != S_OK)
680 {
681 HeapFree(GetProcessHeap(), 0, data);
682 return hr;
683 }
684
685 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
686 if (!result)
687 {
688 HeapFree(GetProcessHeap(), 0, data);
689 return E_OUTOFMEMORY;
690 }
691
692 PropVariantInit(&result[0].schema);
693 PropVariantInit(&result[0].id);
694 PropVariantInit(&result[0].value);
695
696 result[0].value.vt = VT_BLOB;
697 result[0].value.u.blob.cbSize = bytesread;
698 result[0].value.u.blob.pBlobData = data;
699
700 *items = result;
701 *item_count = 1;
702
703 return S_OK;
704 }
705
706 static const MetadataHandlerVtbl UnknownMetadataReader_Vtbl = {
707 0,
708 &CLSID_WICUnknownMetadataReader,
709 LoadUnknownMetadata
710 };
711
712 HRESULT UnknownMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
713 {
714 return MetadataReader_Create(&UnknownMetadataReader_Vtbl, pUnkOuter, iid, ppv);
715 }
716
717 #define SWAP_USHORT(x) do { if (!native_byte_order) (x) = RtlUshortByteSwap(x); } while(0)
718 #define SWAP_ULONG(x) do { if (!native_byte_order) (x) = RtlUlongByteSwap(x); } while(0)
719 #define SWAP_ULONGLONG(x) do { if (!native_byte_order) (x) = RtlUlonglongByteSwap(x); } while(0)
720
721 struct IFD_entry
722 {
723 SHORT id;
724 SHORT type;
725 ULONG count;
726 LONG value;
727 };
728
729 #define IFD_BYTE 1
730 #define IFD_ASCII 2
731 #define IFD_SHORT 3
732 #define IFD_LONG 4
733 #define IFD_RATIONAL 5
734 #define IFD_SBYTE 6
735 #define IFD_UNDEFINED 7
736 #define IFD_SSHORT 8
737 #define IFD_SLONG 9
738 #define IFD_SRATIONAL 10
739 #define IFD_FLOAT 11
740 #define IFD_DOUBLE 12
741 #define IFD_IFD 13
742
743 static int tag_to_vt(SHORT tag)
744 {
745 static const int tag2vt[] =
746 {
747 VT_EMPTY, /* 0 */
748 VT_UI1, /* IFD_BYTE 1 */
749 VT_LPSTR, /* IFD_ASCII 2 */
750 VT_UI2, /* IFD_SHORT 3 */
751 VT_UI4, /* IFD_LONG 4 */
752 VT_UI8, /* IFD_RATIONAL 5 */
753 VT_I1, /* IFD_SBYTE 6 */
754 VT_BLOB, /* IFD_UNDEFINED 7 */
755 VT_I2, /* IFD_SSHORT 8 */
756 VT_I4, /* IFD_SLONG 9 */
757 VT_I8, /* IFD_SRATIONAL 10 */
758 VT_R4, /* IFD_FLOAT 11 */
759 VT_R8, /* IFD_DOUBLE 12 */
760 VT_BLOB, /* IFD_IFD 13 */
761 };
762 return (tag > 0 && tag <= 13) ? tag2vt[tag] : VT_BLOB;
763 }
764
765 static HRESULT load_IFD_entry(IStream *input, const struct IFD_entry *entry,
766 MetadataItem *item, BOOL native_byte_order)
767 {
768 ULONG count, value, i, bytesread;
769 SHORT type;
770 LARGE_INTEGER pos;
771 HRESULT hr;
772
773 item->schema.vt = VT_EMPTY;
774 item->id.vt = VT_UI2;
775 item->id.u.uiVal = entry->id;
776 SWAP_USHORT(item->id.u.uiVal);
777
778 count = entry->count;
779 SWAP_ULONG(count);
780 type = entry->type;
781 SWAP_USHORT(type);
782 item->value.vt = tag_to_vt(type);
783 value = entry->value;
784 SWAP_ULONG(value);
785
786 switch (type)
787 {
788 case IFD_BYTE:
789 case IFD_SBYTE:
790 if (!count) count = 1;
791
792 if (count <= 4)
793 {
794 const BYTE *data = (const BYTE *)&entry->value;
795
796 if (count == 1)
797 item->value.u.bVal = data[0];
798 else
799 {
800 item->value.vt |= VT_VECTOR;
801 item->value.u.caub.cElems = count;
802 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
803 memcpy(item->value.u.caub.pElems, data, count);
804 }
805 break;
806 }
807
808 item->value.vt |= VT_VECTOR;
809 item->value.u.caub.cElems = count;
810 item->value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, count);
811 if (!item->value.u.caub.pElems) return E_OUTOFMEMORY;
812
813 pos.QuadPart = value;
814 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
815 if (FAILED(hr))
816 {
817 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
818 return hr;
819 }
820 hr = IStream_Read(input, item->value.u.caub.pElems, count, &bytesread);
821 if (bytesread != count) hr = E_FAIL;
822 if (hr != S_OK)
823 {
824 HeapFree(GetProcessHeap(), 0, item->value.u.caub.pElems);
825 return hr;
826 }
827 break;
828 case IFD_SHORT:
829 case IFD_SSHORT:
830 if (!count) count = 1;
831
832 if (count <= 2)
833 {
834 const SHORT *data = (const SHORT *)&entry->value;
835
836 if (count == 1)
837 {
838 item->value.u.uiVal = data[0];
839 SWAP_USHORT(item->value.u.uiVal);
840 }
841 else
842 {
843 item->value.vt |= VT_VECTOR;
844 item->value.u.caui.cElems = count;
845 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
846 memcpy(item->value.u.caui.pElems, data, count * 2);
847 for (i = 0; i < count; i++)
848 SWAP_USHORT(item->value.u.caui.pElems[i]);
849 }
850 break;
851 }
852
853 item->value.vt |= VT_VECTOR;
854 item->value.u.caui.cElems = count;
855 item->value.u.caui.pElems = HeapAlloc(GetProcessHeap(), 0, count * 2);
856 if (!item->value.u.caui.pElems) return E_OUTOFMEMORY;
857
858 pos.QuadPart = value;
859 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
860 if (FAILED(hr))
861 {
862 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
863 return hr;
864 }
865 hr = IStream_Read(input, item->value.u.caui.pElems, count * 2, &bytesread);
866 if (bytesread != count * 2) hr = E_FAIL;
867 if (hr != S_OK)
868 {
869 HeapFree(GetProcessHeap(), 0, item->value.u.caui.pElems);
870 return hr;
871 }
872 for (i = 0; i < count; i++)
873 SWAP_USHORT(item->value.u.caui.pElems[i]);
874 break;
875 case IFD_LONG:
876 case IFD_SLONG:
877 case IFD_FLOAT:
878 if (!count) count = 1;
879
880 if (count == 1)
881 {
882 item->value.u.ulVal = value;
883 break;
884 }
885
886 item->value.vt |= VT_VECTOR;
887 item->value.u.caul.cElems = count;
888 item->value.u.caul.pElems = HeapAlloc(GetProcessHeap(), 0, count * 4);
889 if (!item->value.u.caul.pElems) return E_OUTOFMEMORY;
890
891 pos.QuadPart = value;
892 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
893 if (FAILED(hr))
894 {
895 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
896 return hr;
897 }
898 hr = IStream_Read(input, item->value.u.caul.pElems, count * 4, &bytesread);
899 if (bytesread != count * 4) hr = E_FAIL;
900 if (hr != S_OK)
901 {
902 HeapFree(GetProcessHeap(), 0, item->value.u.caul.pElems);
903 return hr;
904 }
905 for (i = 0; i < count; i++)
906 SWAP_ULONG(item->value.u.caul.pElems[i]);
907 break;
908 case IFD_RATIONAL:
909 case IFD_SRATIONAL:
910 case IFD_DOUBLE:
911 if (!count)
912 {
913 FIXME("IFD field type %d, count 0\n", type);
914 item->value.vt = VT_EMPTY;
915 break;
916 }
917
918 if (count == 1)
919 {
920 ULONGLONG ull;
921
922 pos.QuadPart = value;
923 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
924 if (FAILED(hr)) return hr;
925
926 hr = IStream_Read(input, &ull, sizeof(ull), &bytesread);
927 if (bytesread != sizeof(ull)) hr = E_FAIL;
928 if (hr != S_OK) return hr;
929
930 item->value.u.uhVal.QuadPart = ull;
931
932 if (type == IFD_DOUBLE)
933 SWAP_ULONGLONG(item->value.u.uhVal.QuadPart);
934 else
935 {
936 SWAP_ULONG(item->value.u.uhVal.u.LowPart);
937 SWAP_ULONG(item->value.u.uhVal.u.HighPart);
938 }
939 break;
940 }
941 else
942 {
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;
947
948 pos.QuadPart = value;
949 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
950 if (FAILED(hr))
951 {
952 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
953 return hr;
954 }
955 hr = IStream_Read(input, item->value.u.cauh.pElems, count * 8, &bytesread);
956 if (bytesread != count * 8) hr = E_FAIL;
957 if (hr != S_OK)
958 {
959 HeapFree(GetProcessHeap(), 0, item->value.u.cauh.pElems);
960 return hr;
961 }
962 for (i = 0; i < count; i++)
963 {
964 if (type == IFD_DOUBLE)
965 SWAP_ULONGLONG(item->value.u.cauh.pElems[i].QuadPart);
966 else
967 {
968 SWAP_ULONG(item->value.u.cauh.pElems[i].u.LowPart);
969 SWAP_ULONG(item->value.u.cauh.pElems[i].u.HighPart);
970 }
971 }
972 }
973 break;
974 case IFD_ASCII:
975 item->value.u.pszVal = HeapAlloc(GetProcessHeap(), 0, count + 1);
976 if (!item->value.u.pszVal) return E_OUTOFMEMORY;
977
978 if (count <= 4)
979 {
980 const char *data = (const char *)&entry->value;
981 memcpy(item->value.u.pszVal, data, count);
982 item->value.u.pszVal[count] = 0;
983 break;
984 }
985
986 pos.QuadPart = value;
987 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
988 if (FAILED(hr))
989 {
990 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
991 return hr;
992 }
993 hr = IStream_Read(input, item->value.u.pszVal, count, &bytesread);
994 if (bytesread != count) hr = E_FAIL;
995 if (hr != S_OK)
996 {
997 HeapFree(GetProcessHeap(), 0, item->value.u.pszVal);
998 return hr;
999 }
1000 item->value.u.pszVal[count] = 0;
1001 break;
1002 case IFD_UNDEFINED:
1003 if (!count)
1004 {
1005 FIXME("IFD field type %d, count 0\n", type);
1006 item->value.vt = VT_EMPTY;
1007 break;
1008 }
1009
1010 item->value.u.blob.pBlobData = HeapAlloc(GetProcessHeap(), 0, count);
1011 if (!item->value.u.blob.pBlobData) return E_OUTOFMEMORY;
1012
1013 item->value.u.blob.cbSize = count;
1014
1015 if (count <= 4)
1016 {
1017 const char *data = (const char *)&entry->value;
1018 memcpy(item->value.u.blob.pBlobData, data, count);
1019 break;
1020 }
1021
1022 pos.QuadPart = value;
1023 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1024 if (FAILED(hr))
1025 {
1026 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1027 return hr;
1028 }
1029 hr = IStream_Read(input, item->value.u.blob.pBlobData, count, &bytesread);
1030 if (bytesread != count) hr = E_FAIL;
1031 if (hr != S_OK)
1032 {
1033 HeapFree(GetProcessHeap(), 0, item->value.u.blob.pBlobData);
1034 return hr;
1035 }
1036 break;
1037 default:
1038 FIXME("loading field of type %d, count %u is not implemented\n", type, count);
1039 break;
1040 }
1041 return S_OK;
1042 }
1043
1044 static HRESULT LoadIfdMetadata(IStream *input, const GUID *preferred_vendor,
1045 DWORD persist_options, MetadataItem **items, DWORD *item_count)
1046 {
1047 HRESULT hr;
1048 MetadataItem *result;
1049 USHORT count, i;
1050 struct IFD_entry *entry;
1051 BOOL native_byte_order = TRUE;
1052 ULONG bytesread;
1053
1054 TRACE("\n");
1055
1056 #ifdef WORDS_BIGENDIAN
1057 if (persist_options & WICPersistOptionsLittleEndian)
1058 #else
1059 if (persist_options & WICPersistOptionsBigEndian)
1060 #endif
1061 native_byte_order = FALSE;
1062
1063 hr = IStream_Read(input, &count, sizeof(count), &bytesread);
1064 if (bytesread != sizeof(count)) hr = E_FAIL;
1065 if (hr != S_OK) return hr;
1066
1067 SWAP_USHORT(count);
1068
1069 entry = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*entry));
1070 if (!entry) return E_OUTOFMEMORY;
1071
1072 hr = IStream_Read(input, entry, count * sizeof(*entry), &bytesread);
1073 if (bytesread != count * sizeof(*entry)) hr = E_FAIL;
1074 if (hr != S_OK)
1075 {
1076 HeapFree(GetProcessHeap(), 0, entry);
1077 return hr;
1078 }
1079
1080 /* limit number of IFDs to 4096 to avoid infinite loop */
1081 for (i = 0; i < 4096; i++)
1082 {
1083 ULONG next_ifd_offset;
1084 LARGE_INTEGER pos;
1085 USHORT next_ifd_count;
1086
1087 hr = IStream_Read(input, &next_ifd_offset, sizeof(next_ifd_offset), &bytesread);
1088 if (bytesread != sizeof(next_ifd_offset)) hr = E_FAIL;
1089 if (hr != S_OK) break;
1090
1091 SWAP_ULONG(next_ifd_offset);
1092 if (!next_ifd_offset) break;
1093
1094 pos.QuadPart = next_ifd_offset;
1095 hr = IStream_Seek(input, pos, SEEK_SET, NULL);
1096 if (FAILED(hr)) break;
1097
1098 hr = IStream_Read(input, &next_ifd_count, sizeof(next_ifd_count), &bytesread);
1099 if (bytesread != sizeof(next_ifd_count)) hr = E_FAIL;
1100 if (hr != S_OK) break;
1101
1102 SWAP_USHORT(next_ifd_count);
1103
1104 pos.QuadPart = next_ifd_count * sizeof(*entry);
1105 hr = IStream_Seek(input, pos, SEEK_CUR, NULL);
1106 if (FAILED(hr)) break;
1107 }
1108
1109 if (hr != S_OK || i == 4096)
1110 {
1111 HeapFree(GetProcessHeap(), 0, entry);
1112 return WINCODEC_ERR_BADMETADATAHEADER;
1113 }
1114
1115 result = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*result));
1116 if (!result)
1117 {
1118 HeapFree(GetProcessHeap(), 0, entry);
1119 return E_OUTOFMEMORY;
1120 }
1121
1122 for (i = 0; i < count; i++)
1123 {
1124 hr = load_IFD_entry(input, &entry[i], &result[i], native_byte_order);
1125 if (FAILED(hr))
1126 {
1127 HeapFree(GetProcessHeap(), 0, entry);
1128 HeapFree(GetProcessHeap(), 0, result);
1129 return hr;
1130 }
1131 }
1132
1133 HeapFree(GetProcessHeap(), 0, entry);
1134
1135 *items = result;
1136 *item_count = count;
1137
1138 return S_OK;
1139 }
1140
1141 static const MetadataHandlerVtbl IfdMetadataReader_Vtbl = {
1142 0,
1143 &CLSID_WICIfdMetadataReader,
1144 LoadIfdMetadata
1145 };
1146
1147 HRESULT IfdMetadataReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void **ppv)
1148 {
1149 return MetadataReader_Create(&IfdMetadataReader_Vtbl, pUnkOuter, iid, ppv);
1150 }