msvcp90: Fix usage of fpos<>::off vs fpos<>::pos.
[wine.git] / dlls / d3dxof / d3dxof.c
blobbbdfe643f5a234ad58df9aba966c4914e90cdc31
1 /*
2 * Implementation of DirectX File Interfaces
4 * Copyright 2004, 2008, 2010 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/debug.h"
24 #define COBJMACROS
26 #include "winbase.h"
27 #include "wingdi.h"
29 #include "d3dxof_private.h"
30 #include "dxfile.h"
32 #include <stdio.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
35 WINE_DECLARE_DEBUG_CHANNEL(d3dxof_dump);
37 static const struct IDirectXFileVtbl IDirectXFile_Vtbl;
38 static const struct IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl;
39 static const struct IDirectXFileDataVtbl IDirectXFileData_Vtbl;
40 static const struct IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl;
41 static const struct IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl;
42 static const struct IDirectXFileObjectVtbl IDirectXFileObject_Vtbl;
43 static const struct IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl;
45 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj);
46 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj);
47 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj);
49 HRESULT IDirectXFileImpl_Create(IUnknown* pUnkOuter, LPVOID* ppObj)
51 IDirectXFileImpl* object;
53 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
55 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileImpl));
56 if (!object)
58 ERR("Out of memory\n");
59 return DXFILEERR_BADALLOC;
62 object->IDirectXFile_iface.lpVtbl = &IDirectXFile_Vtbl;
63 object->ref = 1;
65 *ppObj = &object->IDirectXFile_iface;
67 return S_OK;
70 static inline IDirectXFileImpl *impl_from_IDirectXFile(IDirectXFile *iface)
72 return CONTAINING_RECORD(iface, IDirectXFileImpl, IDirectXFile_iface);
75 /*** IUnknown methods ***/
76 static HRESULT WINAPI IDirectXFileImpl_QueryInterface(IDirectXFile* iface, REFIID riid, void** ppvObject)
78 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
80 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
82 if (IsEqualGUID(riid, &IID_IUnknown)
83 || IsEqualGUID(riid, &IID_IDirectXFile))
85 IUnknown_AddRef(iface);
86 *ppvObject = &This->IDirectXFile_iface;
87 return S_OK;
90 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
91 return E_NOINTERFACE;
94 static ULONG WINAPI IDirectXFileImpl_AddRef(IDirectXFile* iface)
96 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
97 ULONG ref = InterlockedIncrement(&This->ref);
99 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
101 return ref;
104 static ULONG WINAPI IDirectXFileImpl_Release(IDirectXFile* iface)
106 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
107 ULONG ref = InterlockedDecrement(&This->ref);
109 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
111 if (!ref)
112 HeapFree(GetProcessHeap(), 0, This);
114 return ref;
117 /*** IDirectXFile methods ***/
118 static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPVOID pvSource, DXFILELOADOPTIONS dwLoadOptions, LPDIRECTXFILEENUMOBJECT* ppEnumObj)
120 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
121 IDirectXFileEnumObjectImpl* object;
122 HRESULT hr;
123 LPBYTE file_buffer;
124 DWORD file_size;
126 TRACE("(%p/%p)->(%p,%x,%p)\n", This, iface, pvSource, dwLoadOptions, ppEnumObj);
128 if (!ppEnumObj)
129 return DXFILEERR_BADVALUE;
131 /* Only lowest 4 bits are relevant in DXFILELOADOPTIONS */
132 dwLoadOptions &= 0xF;
134 hr = IDirectXFileEnumObjectImpl_Create(&object);
135 if (FAILED(hr))
136 return hr;
138 if (dwLoadOptions == DXFILELOAD_FROMFILE)
140 HANDLE hFile, file_mapping;
142 TRACE("Open source file '%s'\n", (char*)pvSource);
144 hFile = CreateFileA(pvSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
145 if (hFile == INVALID_HANDLE_VALUE)
147 TRACE("File '%s' not found\n", (char*)pvSource);
148 return DXFILEERR_FILENOTFOUND;
151 file_size = GetFileSize(hFile, NULL);
153 file_mapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
154 CloseHandle(hFile);
155 if (!file_mapping)
157 hr = DXFILEERR_BADFILETYPE;
158 goto error;
161 object->mapped_memory = MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0);
162 CloseHandle(file_mapping);
163 if (!object->mapped_memory)
165 hr = DXFILEERR_BADFILETYPE;
166 goto error;
168 file_buffer = object->mapped_memory;
170 else if (dwLoadOptions == DXFILELOAD_FROMRESOURCE)
172 HRSRC resource_info;
173 HGLOBAL resource_data;
174 LPDXFILELOADRESOURCE lpdxflr = pvSource;
176 TRACE("Source in resource (module = %p, name = %s, type = %s\n", lpdxflr->hModule, debugstr_a(lpdxflr->lpName), debugstr_a(lpdxflr->lpType));
178 resource_info = FindResourceA(lpdxflr->hModule, lpdxflr->lpName, lpdxflr->lpType);
179 if (!resource_info)
181 hr = DXFILEERR_RESOURCENOTFOUND;
182 goto error;
185 file_size = SizeofResource(lpdxflr->hModule, resource_info);
187 resource_data = LoadResource(lpdxflr->hModule, resource_info);
188 if (!resource_data)
190 hr = DXFILEERR_BADRESOURCE;
191 goto error;
194 file_buffer = LockResource(resource_data);
195 if (!file_buffer)
197 hr = DXFILEERR_BADRESOURCE;
198 goto error;
201 else if (dwLoadOptions == DXFILELOAD_FROMMEMORY)
203 LPDXFILELOADMEMORY lpdxflm = pvSource;
205 TRACE("Source in memory at %p with size %d\n", lpdxflm->lpMemory, lpdxflm->dSize);
207 file_buffer = lpdxflm->lpMemory;
208 file_size = lpdxflm->dSize;
210 else
212 FIXME("Source type %d is not handled yet\n", dwLoadOptions);
213 hr = DXFILEERR_NOTDONEYET;
214 goto error;
217 TRACE("File size is %d bytes\n", file_size);
219 if (TRACE_ON(d3dxof_dump))
221 static USHORT num;
222 char tmp[12];
223 HANDLE file;
224 sprintf(tmp, "file%05u.x", num++);
226 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
227 if (file != INVALID_HANDLE_VALUE)
229 WriteFile(file, file_buffer, file_size, NULL, NULL);
230 CloseHandle(file);
234 object->pDirectXFile = This;
236 object->buf.pdxf = This;
237 object->buf.token_present = FALSE;
238 object->buf.buffer = file_buffer;
239 object->buf.rem_bytes = file_size;
240 hr = parse_header(&object->buf, &object->decomp_buffer);
241 if (FAILED(hr))
242 goto error;
244 if (!parse_templates(&object->buf)) {
245 hr = DXFILEERR_BADVALUE;
246 goto error;
249 if (TRACE_ON(d3dxof))
251 int i;
252 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
253 for (i = 0; i < This->nb_xtemplates; i++)
254 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
257 *ppEnumObj = &object->IDirectXFileEnumObject_iface;
259 return DXFILE_OK;
261 error:
262 IDirectXFileEnumObject_Release(&object->IDirectXFileEnumObject_iface);
263 *ppEnumObj = NULL;
265 return hr;
268 static HRESULT WINAPI IDirectXFileImpl_CreateSaveObject(IDirectXFile* iface, LPCSTR szFileName, DXFILEFORMAT dwFileFormat, LPDIRECTXFILESAVEOBJECT* ppSaveObj)
270 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
271 IDirectXFileSaveObjectImpl *object;
272 HRESULT hr;
274 FIXME("(%p/%p)->(%s,%x,%p) partial stub!\n", This, iface, szFileName, dwFileFormat, ppSaveObj);
276 if (!szFileName || !ppSaveObj)
277 return E_POINTER;
279 hr = IDirectXFileSaveObjectImpl_Create(&object);
280 if (SUCCEEDED(hr))
281 *ppSaveObj = &object->IDirectXFileSaveObject_iface;
282 return hr;
285 static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LPVOID pvData, DWORD cbSize)
287 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
288 parse_buffer buf;
289 HRESULT hr;
290 LPBYTE decomp_buffer = NULL;
292 buf.buffer = pvData;
293 buf.rem_bytes = cbSize;
294 buf.txt = FALSE;
295 buf.token_present = FALSE;
296 buf.pdxf = This;
298 TRACE("(%p/%p)->(%p,%d)\n", This, iface, pvData, cbSize);
300 if (!pvData)
301 return DXFILEERR_BADVALUE;
303 if (TRACE_ON(d3dxof_dump))
305 static USHORT num;
306 char tmp[16];
307 HANDLE file;
308 sprintf(tmp, "template%05u.x", num++);
310 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
311 if (file != INVALID_HANDLE_VALUE)
313 WriteFile(file, pvData, cbSize, NULL, NULL);
314 CloseHandle(file);
318 hr = parse_header(&buf, &decomp_buffer);
319 if (FAILED(hr))
320 goto cleanup;
322 if (!parse_templates(&buf)) {
323 hr = DXFILEERR_BADVALUE;
324 goto cleanup;
327 if (TRACE_ON(d3dxof))
329 int i;
330 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
331 for (i = 0; i < This->nb_xtemplates; i++)
332 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
335 hr = DXFILE_OK;
336 cleanup:
337 HeapFree(GetProcessHeap(), 0, decomp_buffer);
338 return hr;
341 static const IDirectXFileVtbl IDirectXFile_Vtbl =
343 IDirectXFileImpl_QueryInterface,
344 IDirectXFileImpl_AddRef,
345 IDirectXFileImpl_Release,
346 IDirectXFileImpl_CreateEnumObject,
347 IDirectXFileImpl_CreateSaveObject,
348 IDirectXFileImpl_RegisterTemplates
351 static HRESULT IDirectXFileBinaryImpl_Create(IDirectXFileBinaryImpl** ppObj)
353 IDirectXFileBinaryImpl* object;
355 TRACE("(%p)\n", ppObj);
357 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileBinaryImpl));
358 if (!object)
360 ERR("Out of memory\n");
361 return DXFILEERR_BADALLOC;
364 object->IDirectXFileBinary_iface.lpVtbl = &IDirectXFileBinary_Vtbl;
365 object->ref = 1;
367 *ppObj = object;
369 return DXFILE_OK;
372 static inline IDirectXFileBinaryImpl *impl_from_IDirectXFileBinary(IDirectXFileBinary *iface)
374 return CONTAINING_RECORD(iface, IDirectXFileBinaryImpl, IDirectXFileBinary_iface);
377 /*** IUnknown methods ***/
378 static HRESULT WINAPI IDirectXFileBinaryImpl_QueryInterface(IDirectXFileBinary* iface, REFIID riid, void** ppvObject)
380 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
382 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
384 if (IsEqualGUID(riid, &IID_IUnknown)
385 || IsEqualGUID(riid, &IID_IDirectXFileObject)
386 || IsEqualGUID(riid, &IID_IDirectXFileBinary))
388 IUnknown_AddRef(iface);
389 *ppvObject = &This->IDirectXFileBinary_iface;
390 return S_OK;
393 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
394 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
395 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
396 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
398 return E_NOINTERFACE;
401 static ULONG WINAPI IDirectXFileBinaryImpl_AddRef(IDirectXFileBinary* iface)
403 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
404 ULONG ref = InterlockedIncrement(&This->ref);
406 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
408 return ref;
411 static ULONG WINAPI IDirectXFileBinaryImpl_Release(IDirectXFileBinary* iface)
413 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
414 ULONG ref = InterlockedDecrement(&This->ref);
416 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
418 if (!ref)
419 HeapFree(GetProcessHeap(), 0, This);
421 return ref;
424 /*** IDirectXFileObject methods ***/
425 static HRESULT WINAPI IDirectXFileBinaryImpl_GetName(IDirectXFileBinary* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
428 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
430 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pstrNameBuf, pdwBufLen);
432 return DXFILEERR_BADVALUE;
435 static HRESULT WINAPI IDirectXFileBinaryImpl_GetId(IDirectXFileBinary* iface, LPGUID pGuid)
437 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
439 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pGuid);
441 return DXFILEERR_BADVALUE;
444 /*** IDirectXFileBinary methods ***/
445 static HRESULT WINAPI IDirectXFileBinaryImpl_GetSize(IDirectXFileBinary* iface, DWORD* pcbSize)
447 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
449 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pcbSize);
451 return DXFILEERR_BADVALUE;
454 static HRESULT WINAPI IDirectXFileBinaryImpl_GetMimeType(IDirectXFileBinary* iface, LPCSTR* pszMimeType)
456 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
458 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pszMimeType);
460 return DXFILEERR_BADVALUE;
463 static HRESULT WINAPI IDirectXFileBinaryImpl_Read(IDirectXFileBinary* iface, LPVOID pvData, DWORD cbSize, LPDWORD pcbRead)
465 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
467 FIXME("(%p/%p)->(%p, %d, %p) stub!\n", This, iface, pvData, cbSize, pcbRead);
469 return DXFILEERR_BADVALUE;
472 static const IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl =
474 IDirectXFileBinaryImpl_QueryInterface,
475 IDirectXFileBinaryImpl_AddRef,
476 IDirectXFileBinaryImpl_Release,
477 IDirectXFileBinaryImpl_GetName,
478 IDirectXFileBinaryImpl_GetId,
479 IDirectXFileBinaryImpl_GetSize,
480 IDirectXFileBinaryImpl_GetMimeType,
481 IDirectXFileBinaryImpl_Read
484 static HRESULT IDirectXFileDataImpl_Create(IDirectXFileDataImpl** ppObj)
486 IDirectXFileDataImpl* object;
488 TRACE("(%p)\n", ppObj);
490 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataImpl));
491 if (!object)
493 ERR("Out of memory\n");
494 return DXFILEERR_BADALLOC;
497 object->IDirectXFileData_iface.lpVtbl = &IDirectXFileData_Vtbl;
498 object->ref = 1;
500 *ppObj = object;
502 return S_OK;
505 static inline IDirectXFileDataImpl *impl_from_IDirectXFileData(IDirectXFileData *iface)
507 return CONTAINING_RECORD(iface, IDirectXFileDataImpl, IDirectXFileData_iface);
510 /*** IUnknown methods ***/
511 static HRESULT WINAPI IDirectXFileDataImpl_QueryInterface(IDirectXFileData* iface, REFIID riid, void** ppvObject)
513 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
515 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
517 if (IsEqualGUID(riid, &IID_IUnknown)
518 || IsEqualGUID(riid, &IID_IDirectXFileObject)
519 || IsEqualGUID(riid, &IID_IDirectXFileData))
521 IUnknown_AddRef(iface);
522 *ppvObject = &This->IDirectXFileData_iface;
523 return S_OK;
526 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
527 if (!IsEqualGUID(riid, &IID_IDirectXFileBinary)
528 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
529 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
531 return E_NOINTERFACE;
534 static ULONG WINAPI IDirectXFileDataImpl_AddRef(IDirectXFileData* iface)
536 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
537 ULONG ref = InterlockedIncrement(&This->ref);
539 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
541 return ref;
544 static ULONG WINAPI IDirectXFileDataImpl_Release(IDirectXFileData* iface)
546 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
547 ULONG ref = InterlockedDecrement(&This->ref);
549 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
551 if (!ref)
553 if (!This->level && !This->from_ref)
555 HeapFree(GetProcessHeap(), 0, This->pstrings);
556 if (This->pobj)
558 HeapFree(GetProcessHeap(), 0, This->pobj->pdata);
559 HeapFree(GetProcessHeap(), 0, This->pobj);
562 HeapFree(GetProcessHeap(), 0, This);
565 return ref;
568 /*** IDirectXFileObject methods ***/
569 static HRESULT WINAPI IDirectXFileDataImpl_GetName(IDirectXFileData* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
571 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
572 DWORD len;
574 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
576 if (!pdwBufLen)
577 return DXFILEERR_BADVALUE;
579 len = strlen(This->pobj->name);
580 if (len)
581 len++;
583 if (pstrNameBuf) {
584 if (*pdwBufLen < len)
585 return DXFILEERR_BADVALUE;
586 CopyMemory(pstrNameBuf, This->pobj->name, len);
588 *pdwBufLen = len;
590 return DXFILE_OK;
593 static HRESULT WINAPI IDirectXFileDataImpl_GetId(IDirectXFileData* iface, LPGUID pGuid)
595 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
597 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
599 if (!pGuid)
600 return DXFILEERR_BADVALUE;
602 memcpy(pGuid, &This->pobj->class_id, 16);
604 return DXFILE_OK;
607 /*** IDirectXFileData methods ***/
608 static HRESULT WINAPI IDirectXFileDataImpl_GetData(IDirectXFileData* iface, LPCSTR szMember, DWORD* pcbSize, void** ppvData)
610 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
612 TRACE("(%p/%p)->(%s,%p,%p)\n", This, iface, debugstr_a(szMember), pcbSize, ppvData);
614 if (!pcbSize || !ppvData)
615 return DXFILEERR_BADVALUE;
617 if (szMember)
619 int i;
620 for (i = 0; i < This->pobj->nb_members; i++)
621 if (!strcmp(This->pobj->members[i].name, szMember))
622 break;
623 if (i == This->pobj->nb_members)
625 WARN("Unknown member '%s'\n", szMember);
626 return DXFILEERR_BADDATAREFERENCE;
628 *pcbSize = This->pobj->members[i].size;
629 *ppvData = This->pobj->root->pdata + This->pobj->members[i].start;
631 else
633 *pcbSize = This->pobj->size;
634 *ppvData = This->pobj->root->pdata + This->pobj->pos_data;
637 return DXFILE_OK;
640 static HRESULT WINAPI IDirectXFileDataImpl_GetType(IDirectXFileData* iface, const GUID** pguid)
642 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
643 static GUID guid;
645 TRACE("(%p/%p)->(%p)\n", This, iface, pguid);
647 if (!pguid)
648 return DXFILEERR_BADVALUE;
650 memcpy(&guid, &This->pobj->type, 16);
651 *pguid = &guid;
653 return DXFILE_OK;
656 static HRESULT WINAPI IDirectXFileDataImpl_GetNextObject(IDirectXFileData* iface, LPDIRECTXFILEOBJECT* ppChildObj)
658 HRESULT hr;
659 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
661 TRACE("(%p/%p)->(%p)\n", This, iface, ppChildObj);
663 if (This->cur_enum_object >= This->pobj->nb_children)
665 *ppChildObj = NULL;
666 return DXFILEERR_NOMOREOBJECTS;
669 if (This->from_ref && (This->level >= 1))
671 /* Only 2 levels can be enumerated if the object is obtained from a reference */
672 *ppChildObj = NULL;
673 return DXFILEERR_NOMOREOBJECTS;
676 if (This->pobj->children[This->cur_enum_object]->binary)
678 IDirectXFileBinaryImpl *object;
680 hr = IDirectXFileBinaryImpl_Create(&object);
681 if (FAILED(hr))
682 return hr;
684 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileBinary_iface;
686 else if (This->pobj->children[This->cur_enum_object]->ptarget)
688 IDirectXFileDataReferenceImpl *object;
690 hr = IDirectXFileDataReferenceImpl_Create(&object);
691 if (FAILED(hr))
692 return hr;
694 object->ptarget = This->pobj->children[This->cur_enum_object++]->ptarget;
696 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileDataReference_iface;
698 else
700 IDirectXFileDataImpl *object;
702 hr = IDirectXFileDataImpl_Create(&object);
703 if (FAILED(hr))
704 return hr;
706 object->pobj = This->pobj->children[This->cur_enum_object++];
707 object->cur_enum_object = 0;
708 object->from_ref = This->from_ref;
709 object->level = This->level + 1;
711 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileData_iface;
714 return DXFILE_OK;
717 static HRESULT WINAPI IDirectXFileDataImpl_AddDataObject(IDirectXFileData* iface, LPDIRECTXFILEDATA pDataObj)
719 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
721 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDataObj);
723 return DXFILEERR_BADVALUE;
726 static HRESULT WINAPI IDirectXFileDataImpl_AddDataReference(IDirectXFileData* iface, LPCSTR szRef, const GUID* pguidRef)
728 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
730 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szRef, pguidRef);
732 return DXFILEERR_BADVALUE;
735 static HRESULT WINAPI IDirectXFileDataImpl_AddBinaryObject(IDirectXFileData* iface, LPCSTR szName, const GUID* pguid, LPCSTR szMimeType, LPVOID pvData, DWORD cbSize)
737 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
739 FIXME("(%p/%p)->(%s,%p,%s,%p,%d) stub!\n", This, iface, szName, pguid, szMimeType, pvData, cbSize);
741 return DXFILEERR_BADVALUE;
744 static const IDirectXFileDataVtbl IDirectXFileData_Vtbl =
746 IDirectXFileDataImpl_QueryInterface,
747 IDirectXFileDataImpl_AddRef,
748 IDirectXFileDataImpl_Release,
749 IDirectXFileDataImpl_GetName,
750 IDirectXFileDataImpl_GetId,
751 IDirectXFileDataImpl_GetData,
752 IDirectXFileDataImpl_GetType,
753 IDirectXFileDataImpl_GetNextObject,
754 IDirectXFileDataImpl_AddDataObject,
755 IDirectXFileDataImpl_AddDataReference,
756 IDirectXFileDataImpl_AddBinaryObject
759 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj)
761 IDirectXFileDataReferenceImpl* object;
763 TRACE("(%p)\n", ppObj);
765 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataReferenceImpl));
766 if (!object)
768 ERR("Out of memory\n");
769 return DXFILEERR_BADALLOC;
772 object->IDirectXFileDataReference_iface.lpVtbl = &IDirectXFileDataReference_Vtbl;
773 object->ref = 1;
775 *ppObj = object;
777 return S_OK;
780 static inline IDirectXFileDataReferenceImpl *impl_from_IDirectXFileDataReference(IDirectXFileDataReference *iface)
782 return CONTAINING_RECORD(iface, IDirectXFileDataReferenceImpl, IDirectXFileDataReference_iface);
785 /*** IUnknown methods ***/
786 static HRESULT WINAPI IDirectXFileDataReferenceImpl_QueryInterface(IDirectXFileDataReference* iface, REFIID riid, void** ppvObject)
788 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
790 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
792 if (IsEqualGUID(riid, &IID_IUnknown)
793 || IsEqualGUID(riid, &IID_IDirectXFileObject)
794 || IsEqualGUID(riid, &IID_IDirectXFileDataReference))
796 IUnknown_AddRef(iface);
797 *ppvObject = &This->IDirectXFileDataReference_iface;
798 return S_OK;
801 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
802 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
803 && !IsEqualGUID(riid, &IID_IDirectXFileBinary))
804 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
806 return E_NOINTERFACE;
809 static ULONG WINAPI IDirectXFileDataReferenceImpl_AddRef(IDirectXFileDataReference* iface)
811 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
812 ULONG ref = InterlockedIncrement(&This->ref);
814 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
816 return ref;
819 static ULONG WINAPI IDirectXFileDataReferenceImpl_Release(IDirectXFileDataReference* iface)
821 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
822 ULONG ref = InterlockedDecrement(&This->ref);
824 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
826 if (!ref)
827 HeapFree(GetProcessHeap(), 0, This);
829 return ref;
832 /*** IDirectXFileObject methods ***/
833 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetName(IDirectXFileDataReference* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
835 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
836 DWORD len;
838 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
840 if (!pdwBufLen)
841 return DXFILEERR_BADVALUE;
843 len = strlen(This->ptarget->name);
844 if (len)
845 len++;
847 if (pstrNameBuf) {
848 if (*pdwBufLen < len)
849 return DXFILEERR_BADVALUE;
850 CopyMemory(pstrNameBuf, This->ptarget->name, len);
852 *pdwBufLen = len;
854 return DXFILE_OK;
857 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetId(IDirectXFileDataReference* iface, LPGUID pGuid)
859 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
861 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
863 if (!pGuid)
864 return DXFILEERR_BADVALUE;
866 memcpy(pGuid, &This->ptarget->class_id, 16);
868 return DXFILE_OK;
871 /*** IDirectXFileDataReference ***/
872 static HRESULT WINAPI IDirectXFileDataReferenceImpl_Resolve(IDirectXFileDataReference* iface, LPDIRECTXFILEDATA* ppDataObj)
874 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
875 IDirectXFileDataImpl *object;
876 HRESULT hr;
878 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
880 if (!ppDataObj)
881 return DXFILEERR_BADVALUE;
883 hr = IDirectXFileDataImpl_Create(&object);
884 if (FAILED(hr))
885 return hr;
887 object->pobj = This->ptarget;
888 object->cur_enum_object = 0;
889 object->level = 0;
890 object->from_ref = TRUE;
892 *ppDataObj = (LPDIRECTXFILEDATA)object;
894 return DXFILE_OK;
897 static const IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl =
899 IDirectXFileDataReferenceImpl_QueryInterface,
900 IDirectXFileDataReferenceImpl_AddRef,
901 IDirectXFileDataReferenceImpl_Release,
902 IDirectXFileDataReferenceImpl_GetName,
903 IDirectXFileDataReferenceImpl_GetId,
904 IDirectXFileDataReferenceImpl_Resolve
907 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj)
909 IDirectXFileEnumObjectImpl* object;
911 TRACE("(%p)\n", ppObj);
913 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileEnumObjectImpl));
914 if (!object)
916 ERR("Out of memory\n");
917 return DXFILEERR_BADALLOC;
920 object->IDirectXFileEnumObject_iface.lpVtbl = &IDirectXFileEnumObject_Vtbl;
921 object->ref = 1;
923 *ppObj = object;
925 return S_OK;
928 static inline IDirectXFileEnumObjectImpl *impl_from_IDirectXFileEnumObject(IDirectXFileEnumObject *iface)
930 return CONTAINING_RECORD(iface, IDirectXFileEnumObjectImpl, IDirectXFileEnumObject_iface);
933 /*** IUnknown methods ***/
934 static HRESULT WINAPI IDirectXFileEnumObjectImpl_QueryInterface(IDirectXFileEnumObject* iface, REFIID riid, void** ppvObject)
936 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
938 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
940 if (IsEqualGUID(riid, &IID_IUnknown)
941 || IsEqualGUID(riid, &IID_IDirectXFileEnumObject))
943 IUnknown_AddRef(iface);
944 *ppvObject = &This->IDirectXFileEnumObject_iface;
945 return S_OK;
948 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
949 return E_NOINTERFACE;
952 static ULONG WINAPI IDirectXFileEnumObjectImpl_AddRef(IDirectXFileEnumObject* iface)
954 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
955 ULONG ref = InterlockedIncrement(&This->ref);
957 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
959 return ref;
962 static ULONG WINAPI IDirectXFileEnumObjectImpl_Release(IDirectXFileEnumObject* iface)
964 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
965 ULONG ref = InterlockedDecrement(&This->ref);
967 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
969 if (!ref)
971 int i;
972 for (i = 0; i < This->nb_xobjects; i++)
973 IDirectXFileData_Release(This->pRefObjects[i]);
974 if (This->mapped_memory)
975 UnmapViewOfFile(This->mapped_memory);
976 HeapFree(GetProcessHeap(), 0, This->decomp_buffer);
977 HeapFree(GetProcessHeap(), 0, This);
980 return ref;
983 /*** IDirectXFileEnumObject methods ***/
984 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetNextDataObject(IDirectXFileEnumObject* iface, LPDIRECTXFILEDATA* ppDataObj)
986 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
987 IDirectXFileDataImpl* object;
988 HRESULT hr;
990 if (!ppDataObj)
991 return E_POINTER;
993 *ppDataObj = NULL;
995 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
997 if (This->nb_xobjects >= MAX_OBJECTS)
999 ERR("Too many objects\n");
1000 return DXFILEERR_NOMOREOBJECTS;
1003 /* Check if there are templates defined before the object */
1004 if (!parse_templates(&This->buf))
1005 return DXFILEERR_BADVALUE;
1007 if (!This->buf.rem_bytes)
1008 return DXFILEERR_NOMOREOBJECTS;
1010 hr = IDirectXFileDataImpl_Create(&object);
1011 if (FAILED(hr))
1012 return hr;
1014 object->pobj = HeapAlloc(GetProcessHeap(), 0, sizeof(xobject)*MAX_SUBOBJECTS);
1015 if (!object->pobj)
1017 ERR("Out of memory\n");
1018 hr = DXFILEERR_BADALLOC;
1019 goto error;
1022 object->pstrings = HeapAlloc(GetProcessHeap(), 0, MAX_STRINGS_BUFFER);
1023 if (!object->pstrings)
1025 ERR("Out of memory\n");
1026 hr = DXFILEERR_BADALLOC;
1027 goto error;
1030 object->cur_enum_object = 0;
1031 object->level = 0;
1032 object->from_ref = FALSE;
1034 This->buf.pxo_globals = This->xobjects;
1035 This->buf.nb_pxo_globals = This->nb_xobjects;
1036 This->buf.level = 0;
1037 This->buf.pdata = NULL;
1038 This->buf.capacity = 0;
1039 This->buf.cur_pos_data = 0;
1040 This->buf.cur_pstrings = This->buf.pstrings = object->pstrings;
1041 This->buf.pxo = This->xobjects[This->nb_xobjects] = This->buf.pxo_tab = object->pobj;
1042 This->buf.pxo->pdata = NULL;
1043 This->buf.pxo->nb_subobjects = 1;
1045 if (!parse_object(&This->buf))
1047 WARN("Object is not correct\n");
1048 hr = DXFILEERR_PARSEERROR;
1049 goto error;
1052 *ppDataObj = (LPDIRECTXFILEDATA)object;
1054 /* Get a reference to created object */
1055 This->pRefObjects[This->nb_xobjects] = (LPDIRECTXFILEDATA)object;
1056 IDirectXFileData_AddRef(This->pRefObjects[This->nb_xobjects]);
1058 This->nb_xobjects++;
1060 return DXFILE_OK;
1062 error:
1064 IDirectXFileData_Release(&object->IDirectXFileData_iface);
1066 return hr;
1069 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectById(IDirectXFileEnumObject* iface, REFGUID rguid, LPDIRECTXFILEDATA* ppDataObj)
1071 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
1073 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, rguid, ppDataObj);
1075 return DXFILEERR_BADVALUE;
1078 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectByName(IDirectXFileEnumObject* iface, LPCSTR szName, LPDIRECTXFILEDATA* ppDataObj)
1080 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
1082 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szName, ppDataObj);
1084 return DXFILEERR_BADVALUE;
1087 static const IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl =
1089 IDirectXFileEnumObjectImpl_QueryInterface,
1090 IDirectXFileEnumObjectImpl_AddRef,
1091 IDirectXFileEnumObjectImpl_Release,
1092 IDirectXFileEnumObjectImpl_GetNextDataObject,
1093 IDirectXFileEnumObjectImpl_GetDataObjectById,
1094 IDirectXFileEnumObjectImpl_GetDataObjectByName
1097 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj)
1099 IDirectXFileSaveObjectImpl* object;
1101 TRACE("(%p)\n", ppObj);
1103 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileSaveObjectImpl));
1104 if (!object)
1106 ERR("Out of memory\n");
1107 return DXFILEERR_BADALLOC;
1110 object->IDirectXFileSaveObject_iface.lpVtbl = &IDirectXFileSaveObject_Vtbl;
1111 object->ref = 1;
1113 *ppObj = object;
1115 return S_OK;
1118 static inline IDirectXFileSaveObjectImpl *impl_from_IDirectXFileSaveObject(IDirectXFileSaveObject *iface)
1120 return CONTAINING_RECORD(iface, IDirectXFileSaveObjectImpl, IDirectXFileSaveObject_iface);
1123 /*** IUnknown methods ***/
1124 static HRESULT WINAPI IDirectXFileSaveObjectImpl_QueryInterface(IDirectXFileSaveObject* iface, REFIID riid, void** ppvObject)
1126 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1128 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
1130 if (IsEqualGUID(riid, &IID_IUnknown)
1131 || IsEqualGUID(riid, &IID_IDirectXFileSaveObject))
1133 IUnknown_AddRef(iface);
1134 *ppvObject = &This->IDirectXFileSaveObject_iface;
1135 return S_OK;
1138 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
1139 return E_NOINTERFACE;
1142 static ULONG WINAPI IDirectXFileSaveObjectImpl_AddRef(IDirectXFileSaveObject* iface)
1144 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1145 ULONG ref = InterlockedIncrement(&This->ref);
1147 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
1149 return ref;
1152 static ULONG WINAPI IDirectXFileSaveObjectImpl_Release(IDirectXFileSaveObject* iface)
1154 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1155 ULONG ref = InterlockedDecrement(&This->ref);
1157 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
1159 if (!ref)
1160 HeapFree(GetProcessHeap(), 0, This);
1162 return ref;
1165 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveTemplates(IDirectXFileSaveObject* iface, DWORD cTemplates, const GUID** ppguidTemplates)
1167 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1169 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, cTemplates, ppguidTemplates);
1171 return DXFILE_OK;
1174 static HRESULT WINAPI IDirectXFileSaveObjectImpl_CreateDataObject(IDirectXFileSaveObject* iface, REFGUID rguidTemplate, LPCSTR szName, const GUID* pguid, DWORD cbSize, LPVOID pvData, LPDIRECTXFILEDATA* ppDataObj)
1176 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1178 FIXME("(%p/%p)->(%p,%s,%p,%d,%p,%p) stub!\n", This, iface, rguidTemplate, szName, pguid, cbSize, pvData, ppDataObj);
1180 return DXFILEERR_BADVALUE;
1183 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveData(IDirectXFileSaveObject* iface, LPDIRECTXFILEDATA ppDataObj)
1185 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1187 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppDataObj);
1189 return DXFILEERR_BADVALUE;
1192 static const IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl =
1194 IDirectXFileSaveObjectImpl_QueryInterface,
1195 IDirectXFileSaveObjectImpl_AddRef,
1196 IDirectXFileSaveObjectImpl_Release,
1197 IDirectXFileSaveObjectImpl_SaveTemplates,
1198 IDirectXFileSaveObjectImpl_CreateDataObject,
1199 IDirectXFileSaveObjectImpl_SaveData