jscript: Pass correct cpbegin to InitMatch.
[wine] / dlls / d3dxof / tests / d3dxof.c
1 /*
2  * Some unit tests for d3dxof
3  *
4  * Copyright (C) 2008 Christian Costa
5  *
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.
10  *
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.
15  *
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
19  */
20 #define COBJMACROS
21
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "initguid.h"
26 #include "dxfile.h"
27
28 static inline void debugstr_guid( char* buf, CONST GUID *id )
29 {
30     sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
31             id->Data1, id->Data2, id->Data3,
32             id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
33             id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
34 }
35
36 static HMODULE hd3dxof;
37 static HRESULT (WINAPI *pDirectXFileCreate)(LPDIRECTXFILE*);
38
39 static char template[] =
40 "xof 0302txt 0064\n"
41 "template Header\n"
42 "{\n"
43 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
44 "WORD major;\n"
45 "WORD minor;\n"
46 "DWORD flags;\n"
47 "}\n";
48
49 /* Same version as above compressed with mszip */
50 static char compressed_template[] =
51 "xof 0302tzip0064\x71\x00\x00\x00\x61\x00\x5a\x00"
52 "\x43\x4B\x2B\x49\xCD\x2D\xC8\x49\x2C\x49\x55\xF0\x48\x4D\x4C\x49"
53 "\x2D\xE2\xAA\xE6\xB2\x31\x76\xB1\x30\x72\x74\x32\x31\xD6\x35\x33"
54 "\x72\x71\xD4\x35\x34\x74\x76\xD3\x75\x74\x32\xB6\xD4\x35\x30\x30"
55 "\x32\x70\x74\x33\x37\x74\x35\x31\x36\xB6\xE3\x0A\xF7\x0F\x72\x51"
56 "\xC8\x4D\xCC\xCA\x2F\xB2\x86\xB2\x33\xF3\x40\x6C\x17\x30\x27\x2D"
57 "\x27\x31\xBD\xD8\x9A\xAB\x96\x8B\x0B\x00";
58
59 static char object[] =
60 "xof 0302txt 0064\n"
61 "Header Object\n"
62 "{\n"
63 "1; 2; 3;\n"
64 "}\n";
65
66 /* Same version as above compressed with mszip */
67 static char compressed_object[] =
68 "xof 0302tzip0064\x2c\x00\x00\x00\x1c\x00\x20\x00"
69 "\x43\x4b\xf3\x48\x4d\x4c\x49\x2d\x52\xf0\x4f\xca\x4a\x4d\x2e\xe1"
70 "\xaa\xe6\x32\xb4\x56\x30\xb2\x56\x30\xb6\xe6\xaa\xe5\xe2\x02\x00";
71
72 static char empty_txt_file[]  = "xof 0302txt 0064";
73 static char empty_bin_file[]  = "xof 0302bin 0064";
74 /* MSZip data is generated with the command "MAKECAB.EXE /D Compress=ON /D CompressionType=MSZip file packed"
75  * Data in cab is after the filename (null terminated) and the 32-bit checksum:
76  * size (16-bit), packed_size (16-bit) and compressed data (with leading 16-bit CK signature)
77  * for each MSZIP chunk whose decompressed size can not exceed 32768 bytes
78  * Data in x files is preceeding by the size (32-bit) of the decompressed file including the xof header (16 bytes)
79  * It does not seem possible to generate a MSZip data chunk with no byte, so put just 1 byte here */
80 /* "\n" packed with MSZip => no text */
81 static char empty_tzip_file[] = "xof 0302tzip0064\x11\x00\x00\x00\x01\x00\x05\x00\x43\x4b\xe3\x02\x00";
82 /* "\n" packed with MSZip => no token (token are 16-bit and there is only 1 byte) */
83 static char empty_bzip_file[] = "xof 0302bzip0064\x11\x00\x00\x00\x01\x00\x05\x00\x43\x4b\xe3\x02\x00";
84 static char empty_cmp_file[]  = "xof 0302cmp 0064";
85 static char empty_xxxx_file[] = "xof 0302xxxx0064";
86
87 static char object_noname[] =
88 "xof 0302txt 0064\n"
89 "Header\n"
90 "{\n"
91 "1; 2; 3;\n"
92 "}\n";
93
94 static char template_syntax_empty_array[] =
95 "xof 0302txt 0064\n"
96 "template Buffer\n"
97 "{\n"
98 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
99 "DWORD num_elem;\n"
100 "array DWORD value[num_elem];\n"
101 "DWORD dummy;\n"
102 "}\n";
103
104 static char object_syntax_empty_array_semicolon[] =
105 "xof 0302txt 0064\n"
106 "Buffer\n"
107 "{\n"
108 "0;\n"
109 ";\n"
110 "1234;\n"
111 "}\n";
112
113 static char object_syntax_empty_array_nosemicolon[] =
114 "xof 0302txt 0064\n"
115 "Buffer\n"
116 "{\n"
117 "0;\n"
118 "1234;\n"
119 "}\n";
120
121
122 static void init_function_pointers(void)
123 {
124     /* We have to use LoadLibrary as no d3dxof functions are referenced directly */
125     hd3dxof = LoadLibraryA("d3dxof.dll");
126
127     pDirectXFileCreate = (void *)GetProcAddress(hd3dxof, "DirectXFileCreate");
128 }
129
130 static ULONG getRefcount(IUnknown *iface)
131 {
132     IUnknown_AddRef(iface);
133     return IUnknown_Release(iface);
134 }
135
136 static void test_refcount(void)
137 {
138     HRESULT hr;
139     ULONG ref;
140     LPDIRECTXFILE lpDirectXFile = NULL;
141     LPDIRECTXFILEENUMOBJECT lpdxfeo;
142     LPDIRECTXFILEDATA lpdxfd;
143     DXFILELOADMEMORY dxflm;
144
145     if (!pDirectXFileCreate)
146     {
147         win_skip("DirectXFileCreate is not available\n");
148         return;
149     }
150
151     hr = pDirectXFileCreate(&lpDirectXFile);
152     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
153     if (!lpDirectXFile)
154     {
155         skip("Couldn't create DirectXFile interface\n");
156         return;
157     }
158
159     ref = getRefcount( (IUnknown *) lpDirectXFile);
160     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
161     ref = IDirectXFile_AddRef(lpDirectXFile);
162     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
163     ref = IDirectXFile_Release(lpDirectXFile);
164     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
165
166     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
167     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
168
169     dxflm.lpMemory = &object;
170     dxflm.dSize = sizeof(object) - 1;
171     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
172     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
173     ref = getRefcount( (IUnknown *) lpDirectXFile);
174     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
175     ref = getRefcount( (IUnknown *) lpdxfeo);
176     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
177
178     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
179     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
180     ref = getRefcount( (IUnknown *) lpDirectXFile);
181     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
182     ref = getRefcount( (IUnknown *) lpdxfeo);
183     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
184     /* Enum object gets references to all top level objects */
185     ref = getRefcount( (IUnknown *) lpdxfd);
186     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
187
188     ref = IDirectXFile_Release(lpDirectXFile);
189     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
190     /* Nothing changes for all other objects */
191     ref = getRefcount( (IUnknown *) lpdxfeo);
192     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
193     ref = getRefcount( (IUnknown *) lpdxfd);
194     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
195
196     ref = IDirectXFileEnumObject_Release(lpdxfeo);
197     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
198     /* Enum object releases references to all top level objects */
199     ref = getRefcount( (IUnknown *) lpdxfd);
200     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
201
202     ref = IDirectXFileData_Release(lpdxfd);
203     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
204 }
205
206 static void test_CreateEnumObject(void)
207 {
208     HRESULT hr;
209     ULONG ref;
210     LPDIRECTXFILE lpDirectXFile = NULL;
211     LPDIRECTXFILEENUMOBJECT lpdxfeo;
212     LPDIRECTXFILEDATA lpdxfd;
213     DXFILELOADMEMORY dxflm;
214     BYTE* pdata;
215     DWORD size;
216
217     if (!pDirectXFileCreate)
218     {
219         win_skip("DirectXFileCreate is not available\n");
220         return;
221     }
222
223     hr = pDirectXFileCreate(&lpDirectXFile);
224     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
225     if (!lpDirectXFile)
226     {
227         skip("Couldn't create DirectXFile interface\n");
228         return;
229     }
230
231     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
232     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
233
234     dxflm.lpMemory = &object;
235     dxflm.dSize = sizeof(object) - 1;
236     /* Check that only lowest 4 bits are relevant in DXFILELOADOPTIONS */
237     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, 0xFFFFFFF0 + DXFILELOAD_FROMMEMORY, &lpdxfeo);
238     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
239
240     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
241     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
242
243     /* Get all data (szMember == NULL) */
244     hr = IDirectXFileData_GetData(lpdxfd, NULL, &size, (void**)&pdata);
245     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
246
247     ok(size == 8, "Retrieved data size is wrong (%u instead of 8)\n", size);
248     ok((*((WORD*)pdata) == 1) && (*((WORD*)(pdata+2)) == 2) && (*((DWORD*)(pdata+4)) == 3), "Retrieved data is wrong\n");
249
250     /* Get only "major" member (szMember == "major") */
251     hr = IDirectXFileData_GetData(lpdxfd, "major", &size, (void**)&pdata);
252     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
253
254     ok(size == 2, "Retrieved data size is wrong (%u instead of 2)\n", size);
255     ok(*((WORD*)pdata) == 1, "Retrieved data is wrong (%u instead of 1)\n", *((WORD*)pdata));
256
257     /* Get only "minor" member (szMember == "minor") */
258     hr = IDirectXFileData_GetData(lpdxfd, "minor", &size, (void**)&pdata);
259     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
260
261     ok(size == 2, "Retrieved data size is wrong (%u instead of 2)\n", size);
262     ok(*((WORD*)pdata) == 2, "Retrieved data is wrong (%u instead of 2)\n", *((WORD*)pdata));
263
264     /* Get only "flags" member (szMember == "flags") */
265     hr = IDirectXFileData_GetData(lpdxfd, "flags", &size, (void**)&pdata);
266     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
267
268     ok(size == 4, "Retrieved data size is wrong (%u instead of 4)\n", size);
269     ok(*((WORD*)pdata) == 3, "Retrieved data is wrong (%u instead of 3)\n", *((WORD*)pdata));
270
271     /* Try to get not existing member (szMember == "unknown") */
272     hr = IDirectXFileData_GetData(lpdxfd, "unknow", &size, (void**)&pdata);
273     ok(hr == DXFILEERR_BADDATAREFERENCE, "IDirectXFileData_GetData: %x\n", hr);
274
275     ref = IDirectXFileEnumObject_Release(lpdxfeo);
276     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
277
278     ref = IDirectXFile_Release(lpDirectXFile);
279     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
280
281     ref = IDirectXFileData_Release(lpdxfd);
282     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
283 }
284
285 static void test_file_types(void)
286 {
287     HRESULT hr;
288     LPDIRECTXFILE dxfile = NULL;
289     LPDIRECTXFILEENUMOBJECT enum_object;
290     DXFILELOADMEMORY lminfo;
291
292     if (!pDirectXFileCreate)
293     {
294         win_skip("DirectXFileCreate is not available\n");
295         return;
296     }
297
298     hr = pDirectXFileCreate(&dxfile);
299     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
300     if (!dxfile)
301     {
302         skip("Couldn't create DirectXFile interface\n");
303         return;
304     }
305
306     hr = IDirectXFile_RegisterTemplates(dxfile, empty_txt_file, sizeof(empty_txt_file) - 1);
307     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
308
309     hr = IDirectXFile_RegisterTemplates(dxfile, empty_bin_file, sizeof(empty_bin_file) - 1);
310     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
311
312     hr = IDirectXFile_RegisterTemplates(dxfile, empty_tzip_file, sizeof(empty_tzip_file) - 1);
313     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
314
315     hr = IDirectXFile_RegisterTemplates(dxfile, empty_bzip_file, sizeof(empty_bzip_file) - 1);
316     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
317
318     hr = IDirectXFile_RegisterTemplates(dxfile, empty_cmp_file, sizeof(empty_cmp_file) - 1);
319     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
320
321     hr = IDirectXFile_RegisterTemplates(dxfile, empty_xxxx_file, sizeof(empty_xxxx_file) - 1);
322     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
323
324     lminfo.lpMemory = empty_txt_file;
325     lminfo.dSize = sizeof(empty_txt_file) - 1;
326     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
327     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
328     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
329
330     lminfo.lpMemory = empty_bin_file;
331     lminfo.dSize = sizeof(empty_bin_file) - 1;
332     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
333     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
334     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
335
336     lminfo.lpMemory = empty_tzip_file;
337     lminfo.dSize = sizeof(empty_tzip_file) - 1;
338     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
339     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
340     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
341
342     lminfo.lpMemory = empty_bzip_file;
343     lminfo.dSize = sizeof(empty_bzip_file) - 1;
344     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
345     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
346     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
347
348     lminfo.lpMemory = empty_cmp_file;
349     lminfo.dSize = sizeof(empty_cmp_file) - 1;
350     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
351     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFile_CreateEnumObject: %x\n", hr);
352
353     lminfo.lpMemory = empty_xxxx_file;
354     lminfo.dSize = sizeof(empty_xxxx_file) - 1;
355     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
356     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFile_CreateEnumObject: %x\n", hr);
357
358     IDirectXFile_Release(dxfile);
359 }
360
361 static void test_compressed_files(void)
362 {
363     HRESULT hr;
364     LPDIRECTXFILE dxfile = NULL;
365     LPDIRECTXFILEENUMOBJECT enum_object;
366     LPDIRECTXFILEDATA file_data;
367     DXFILELOADMEMORY lminfo;
368     BYTE* data;
369     DWORD size;
370
371     if (!pDirectXFileCreate)
372     {
373         win_skip("DirectXFileCreate is not available\n");
374         return;
375     }
376
377     hr = pDirectXFileCreate(&dxfile);
378     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
379     if (!dxfile)
380     {
381         skip("Couldn't create DirectXFile interface\n");
382         return;
383     }
384
385     hr = IDirectXFile_RegisterTemplates(dxfile, compressed_template, sizeof(compressed_template) - 1);
386     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
387
388     lminfo.lpMemory = compressed_object;
389     lminfo.dSize = sizeof(compressed_object) - 1;
390     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
391     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
392
393     hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &file_data);
394     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
395
396     hr = IDirectXFileData_GetData(file_data, NULL, &size, (void**)&data);
397     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
398
399     ok(size == 8, "Retrieved data size is wrong\n");
400     ok((*((WORD*)data) == 1) && (*((WORD*)(data+2)) == 2) && (*((DWORD*)(data+4)) == 3), "Retrieved data is wrong\n");
401
402     IDirectXFileData_Release(file_data);
403     IDirectXFileEnumObject_Release(enum_object);
404     IDirectXFile_Release(dxfile);
405 }
406
407 static void test_getname(void)
408 {
409     HRESULT hr;
410     ULONG ref;
411     LPDIRECTXFILE lpDirectXFile = NULL;
412     LPDIRECTXFILEENUMOBJECT lpdxfeo;
413     LPDIRECTXFILEDATA lpdxfd;
414     DXFILELOADMEMORY dxflm;
415     char name[100];
416     DWORD length;
417
418     if (!pDirectXFileCreate)
419     {
420         win_skip("DirectXFileCreate is not available\n");
421         return;
422     }
423
424     hr = pDirectXFileCreate(&lpDirectXFile);
425     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
426     if (!lpDirectXFile)
427     {
428         skip("Couldn't create DirectXFile interface\n");
429         return;
430     }
431
432     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
433     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
434
435     /* Check object with name */
436     dxflm.lpMemory = &object;
437     dxflm.dSize = sizeof(object) - 1;
438     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
439     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
440     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
441     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
442
443     hr = IDirectXFileData_GetName(lpdxfd, NULL, NULL);
444     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
445     hr = IDirectXFileData_GetName(lpdxfd, name, NULL);
446     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
447     hr = IDirectXFileData_GetName(lpdxfd, NULL, &length);
448     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
449     ok(length == 7, "Returned length should be 7 instead of %u\n", length);
450     length = sizeof(name);
451     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
452     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
453     ok(length == 7, "Returned length should be 7 instead of %u\n", length);
454     ok(!strcmp(name, "Object"), "Returned string should be 'Object' intead of '%s'\n", name);
455     length = 3;
456     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
457     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
458
459     ref = IDirectXFileEnumObject_Release(lpdxfeo);
460     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
461     ref = IDirectXFileData_Release(lpdxfd);
462     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
463
464     /* Check object without name */
465     dxflm.lpMemory = &object_noname;
466     dxflm.dSize = sizeof(object_noname) - 1;
467     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
468     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
469     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
470     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
471
472     hr = IDirectXFileData_GetName(lpdxfd, NULL, &length);
473     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
474     ok(length == 0, "Returned length should be 0 instead of %u\n", length);
475     length = sizeof(name);
476     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
477     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
478     ok(length == 0, "Returned length should be 0 instead of %u\n", length);
479
480     ref = IDirectXFileEnumObject_Release(lpdxfeo);
481     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
482     ref = IDirectXFileData_Release(lpdxfd);
483     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
484     ref = IDirectXFile_Release(lpDirectXFile);
485     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
486 }
487
488 static void test_syntax(void)
489 {
490     HRESULT hr;
491     ULONG ref;
492     LPDIRECTXFILE lpDirectXFile = NULL;
493     LPDIRECTXFILEENUMOBJECT lpdxfeo;
494     LPDIRECTXFILEDATA lpdxfd;
495     DXFILELOADMEMORY dxflm;
496
497     if (!pDirectXFileCreate)
498     {
499         win_skip("DirectXFileCreate is not available\n");
500         return;
501     }
502
503     hr = pDirectXFileCreate(&lpDirectXFile);
504     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
505     if (!lpDirectXFile)
506     {
507         skip("Couldn't create DirectXFile interface\n");
508         return;
509     }
510
511     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template_syntax_empty_array, sizeof(template_syntax_empty_array) - 1);
512     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
513
514     dxflm.lpMemory = &object_syntax_empty_array_semicolon;
515     dxflm.dSize = sizeof(object_syntax_empty_array_semicolon) - 1;
516     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
517     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
518     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
519     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
520
521     ref = IDirectXFileEnumObject_Release(lpdxfeo);
522     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
523     if (hr == DXFILE_OK)
524     {
525         ref = IDirectXFileData_Release(lpdxfd);
526         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
527     }
528
529     dxflm.lpMemory = &object_syntax_empty_array_nosemicolon;
530     dxflm.dSize = sizeof(object_syntax_empty_array_nosemicolon) - 1;
531     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
532     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
533     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
534     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
535
536     ref = IDirectXFileEnumObject_Release(lpdxfeo);
537     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
538     if (hr == DXFILE_OK)
539     {
540         ref = IDirectXFileData_Release(lpdxfd);
541         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
542     }
543
544     ref = IDirectXFile_Release(lpDirectXFile);
545     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
546 }
547
548 /* Set it to 1 to expand the string when dumping the object. This is useful when there is
549  * only one string in a sub-object (very common). Use with care, this may lead to a crash. */
550 #define EXPAND_STRING 0
551
552 static void process_data(LPDIRECTXFILEDATA lpDirectXFileData, int* plevel)
553 {
554     HRESULT hr;
555     char name[100];
556     GUID clsid;
557     CONST GUID* clsid_type = NULL;
558     char str_clsid[40];
559     char str_clsid_type[40];
560     DWORD len = 100;
561     LPDIRECTXFILEOBJECT pChildObj;
562     int i;
563     int j = 0;
564     LPBYTE pData;
565     DWORD k, size;
566
567     hr = IDirectXFileData_GetId(lpDirectXFileData, &clsid);
568     ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
569     hr = IDirectXFileData_GetName(lpDirectXFileData, name, &len);
570     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
571     hr = IDirectXFileData_GetType(lpDirectXFileData, &clsid_type);
572     ok(hr == DXFILE_OK, "IDirectXFileData_GetType: %x\n", hr);
573     hr = IDirectXFileData_GetData(lpDirectXFileData, NULL, &size, (void**)&pData);
574     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
575     for (i = 0; i < *plevel; i++)
576         printf("  ");
577     debugstr_guid(str_clsid, &clsid);
578     debugstr_guid(str_clsid_type, clsid_type);
579     printf("Found object '%s' - %s - %s - %d\n", name, str_clsid, str_clsid_type, size);
580
581     if (EXPAND_STRING && size == 4)
582     {
583         char * str = *(char**)pData;
584         printf("string %s\n", str);
585     }
586     else if (size)
587     {
588         for (k = 0; k < size; k++)
589         {
590             if (k && !(k%16))
591                 printf("\n");
592             printf("%02x ", pData[k]);
593         }
594         printf("\n");
595     }
596     (*plevel)++;
597     while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(lpDirectXFileData, &pChildObj)))
598     {
599         LPDIRECTXFILEDATA p1;
600         LPDIRECTXFILEDATAREFERENCE p2;
601         LPDIRECTXFILEBINARY p3;
602         j++;
603
604         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileData, (void **) &p1);
605         if (SUCCEEDED(hr))
606         {
607             for (i = 0; i < *plevel; i++)
608                 printf("  ");
609             printf("Found Data (%d)\n", j);
610             process_data(p1, plevel);
611             IDirectXFileData_Release(p1);
612         }
613         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileDataReference, (void **) &p2);
614         if (SUCCEEDED(hr))
615         {
616             LPDIRECTXFILEDATA pfdo;
617             for (i = 0; i < *plevel; i++)
618                 printf("  ");
619             printf("Found Data Reference (%d)\n", j);
620 #if 0
621             hr = IDirectXFileDataReference_GetId(lpDirectXFileData, &clsid);
622             ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
623             hr = IDirectXFileDataReference_GetName(lpDirectXFileData, name, &len);
624             ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
625 #endif
626             IDirectXFileDataReference_Resolve(p2, &pfdo);
627             process_data(pfdo, plevel);
628             IDirectXFileData_Release(pfdo);
629             IDirectXFileDataReference_Release(p2);
630         }
631         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileBinary, (void **) &p3);
632         if (SUCCEEDED(hr))
633         {
634             for (i = 0; i < *plevel; i++)
635                 printf("  ");
636             printf("Found Binary (%d)\n", j);
637             IDirectXFileBinary_Release(p3);
638         }
639         IDirectXFileObject_Release(pChildObj);
640     }
641     (*plevel)--;
642     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileData_GetNextObject: %x\n", hr);
643 }
644
645 static void test_dump(void)
646 {
647     HRESULT hr;
648     ULONG ref;
649     LPDIRECTXFILE lpDirectXFile = NULL;
650     LPDIRECTXFILEENUMOBJECT lpDirectXFileEnumObject = NULL;
651     LPDIRECTXFILEDATA lpDirectXFileData = NULL;
652     HANDLE hFile;
653     LPVOID pvData = NULL;
654     DWORD cbSize;
655
656     if (!pDirectXFileCreate)
657     {
658         win_skip("DirectXFileCreate is not available\n");
659         goto exit;
660     }
661
662     /* Dump data only if there is an object and a template */
663     hFile = CreateFileA("objects.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
664     if (hFile == INVALID_HANDLE_VALUE)
665       return;
666     CloseHandle(hFile);
667
668     hFile = CreateFileA("templates.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
669     if (hFile == INVALID_HANDLE_VALUE)
670       return;
671
672     pvData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 10000);
673
674     if (!ReadFile(hFile, pvData, 10000, &cbSize, NULL))
675     {
676       skip("Template file is too big\n");
677       goto exit;
678     }
679
680     printf("Load %d bytes\n", cbSize);
681
682     hr = pDirectXFileCreate(&lpDirectXFile);
683     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
684     if(!lpDirectXFile)
685     {
686         skip("Couldn't create DirectXFile interface\n");
687         goto exit;
688     }
689
690     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, pvData, cbSize);
691     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
692
693     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, (LPVOID)"objects.txt", DXFILELOAD_FROMFILE, &lpDirectXFileEnumObject);
694     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
695
696     while (SUCCEEDED(hr = IDirectXFileEnumObject_GetNextDataObject(lpDirectXFileEnumObject, &lpDirectXFileData)))
697     {
698         int level = 0;
699         printf("\n");
700         process_data(lpDirectXFileData, &level);
701         IDirectXFileData_Release(lpDirectXFileData);
702     }
703     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
704
705     ref = IDirectXFile_Release(lpDirectXFileEnumObject);
706     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
707
708     ref = IDirectXFile_Release(lpDirectXFile);
709     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
710
711     CloseHandle(hFile);
712
713 exit:
714     HeapFree(GetProcessHeap(), 0, pvData);
715 }
716
717 START_TEST(d3dxof)
718 {
719     init_function_pointers();
720
721     test_refcount();
722     test_CreateEnumObject();
723     test_file_types();
724     test_compressed_files();
725     test_getname();
726     test_syntax();
727     test_dump();
728
729     FreeLibrary(hd3dxof);
730 }