progman: Fix GROUP_AddGroup() declaration.
[wine.git] / dlls / d3dxof / parsing.c
blob54566b08e870dcca43bcba04912949bb9a16bc7b
1 /*
2 * X Files parsing
4 * Copyright 2008 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/port.h"
23 #include "wine/debug.h"
25 #define COBJMACROS
27 #include "winbase.h"
28 #include "wingdi.h"
30 #include "d3dxof_private.h"
31 #include "dxfile.h"
33 #include <stdio.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof_parsing);
37 #define MAKEFOUR(a,b,c,d) ((DWORD)a + ((DWORD)b << 8) + ((DWORD)c << 16) + ((DWORD)d << 24))
38 #define XOFFILE_FORMAT_MAGIC MAKEFOUR('x','o','f',' ')
39 #define XOFFILE_FORMAT_VERSION_302 MAKEFOUR('0','3','0','2')
40 #define XOFFILE_FORMAT_VERSION_303 MAKEFOUR('0','3','0','3')
41 #define XOFFILE_FORMAT_BINARY MAKEFOUR('b','i','n',' ')
42 #define XOFFILE_FORMAT_TEXT MAKEFOUR('t','x','t',' ')
43 #define XOFFILE_FORMAT_BINARY_MSZIP MAKEFOUR('b','z','i','p')
44 #define XOFFILE_FORMAT_TEXT_MSZIP MAKEFOUR('t','z','i','p')
45 #define XOFFILE_FORMAT_COMPRESSED MAKEFOUR('c','m','p',' ')
46 #define XOFFILE_FORMAT_FLOAT_BITS_32 MAKEFOUR('0','0','3','2')
47 #define XOFFILE_FORMAT_FLOAT_BITS_64 MAKEFOUR('0','0','6','4')
49 #define TOKEN_ERROR 0xffff
50 #define TOKEN_NONE 0
51 #define TOKEN_NAME 1
52 #define TOKEN_STRING 2
53 #define TOKEN_INTEGER 3
54 #define TOKEN_GUID 5
55 #define TOKEN_INTEGER_LIST 6
56 #define TOKEN_FLOAT_LIST 7
57 #define TOKEN_OBRACE 10
58 #define TOKEN_CBRACE 11
59 #define TOKEN_OPAREN 12
60 #define TOKEN_CPAREN 13
61 #define TOKEN_OBRACKET 14
62 #define TOKEN_CBRACKET 15
63 #define TOKEN_OANGLE 16
64 #define TOKEN_CANGLE 17
65 #define TOKEN_DOT 18
66 #define TOKEN_COMMA 19
67 #define TOKEN_SEMICOLON 20
68 #define TOKEN_TEMPLATE 31
69 #define TOKEN_WORD 40
70 #define TOKEN_DWORD 41
71 #define TOKEN_FLOAT 42
72 #define TOKEN_DOUBLE 43
73 #define TOKEN_CHAR 44
74 #define TOKEN_UCHAR 45
75 #define TOKEN_SWORD 46
76 #define TOKEN_SDWORD 47
77 #define TOKEN_VOID 48
78 #define TOKEN_LPSTR 49
79 #define TOKEN_UNICODE 50
80 #define TOKEN_CSTRING 51
81 #define TOKEN_ARRAY 52
83 #define CLSIDFMT "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>"
85 /* FOURCC to string conversion for debug messages */
86 static const char *debugstr_fourcc(DWORD fourcc)
88 if (!fourcc) return "'null'";
89 return wine_dbg_sprintf ("\'%c%c%c%c\'",
90 (char)(fourcc), (char)(fourcc >> 8),
91 (char)(fourcc >> 16), (char)(fourcc >> 24));
94 static const char* get_primitive_string(DWORD token)
96 switch(token)
98 case TOKEN_WORD:
99 return "WORD";
100 case TOKEN_DWORD:
101 return "DWORD";
102 case TOKEN_FLOAT:
103 return "FLOAT";
104 case TOKEN_DOUBLE:
105 return "DOUBLE";
106 case TOKEN_CHAR:
107 return "CHAR";
108 case TOKEN_UCHAR:
109 return "UCHAR";
110 case TOKEN_SWORD:
111 return "SWORD";
112 case TOKEN_SDWORD:
113 return "SDWORD";
114 case TOKEN_VOID:
115 return "VOID";
116 case TOKEN_LPSTR:
117 return "STRING";
118 case TOKEN_UNICODE:
119 return "UNICODE";
120 case TOKEN_CSTRING:
121 return "CSTRING ";
122 default:
123 break;
125 return NULL;
128 static void dump_template(xtemplate* templates_array, xtemplate* ptemplate)
130 ULONG j, k;
131 GUID* clsid;
133 clsid = &ptemplate->class_id;
135 DPRINTF("template %s\n", ptemplate->name);
136 DPRINTF("{\n");
137 DPRINTF(CLSIDFMT "\n", clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
138 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
139 for (j = 0; j < ptemplate->nb_members; j++)
141 if (ptemplate->members[j].nb_dims)
142 DPRINTF("array ");
143 if (ptemplate->members[j].type == TOKEN_NAME)
144 DPRINTF("%s ", templates_array[ptemplate->members[j].idx_template].name);
145 else
146 DPRINTF("%s ", get_primitive_string(ptemplate->members[j].type));
147 DPRINTF("%s", ptemplate->members[j].name);
148 for (k = 0; k < ptemplate->members[j].nb_dims; k++)
150 if (ptemplate->members[j].dim_fixed[k])
151 DPRINTF("[%d]", ptemplate->members[j].dim_value[k]);
152 else
153 DPRINTF("[%s]", ptemplate->members[ptemplate->members[j].dim_value[k]].name);
155 DPRINTF(";\n");
157 if (ptemplate->open)
158 DPRINTF("[...]\n");
159 else if (ptemplate->nb_children)
161 DPRINTF("[%s", ptemplate->children[0]);
162 for (j = 1; j < ptemplate->nb_children; j++)
163 DPRINTF(",%s", ptemplate->children[j]);
164 DPRINTF("]\n");
166 DPRINTF("}\n");
169 static BOOL read_bytes(parse_buffer * buf, LPVOID data, DWORD size)
171 if (buf->rem_bytes < size)
172 return FALSE;
173 memcpy(data, buf->buffer, size);
174 buf->buffer += size;
175 buf->rem_bytes -= size;
176 return TRUE;
179 static void rewind_bytes(parse_buffer * buf, DWORD size)
181 buf->buffer -= size;
182 buf->rem_bytes += size;
185 HRESULT parse_header(parse_buffer * buf, BYTE ** decomp_buffer_ptr)
187 /* X File common header:
188 * 0-3 -> Magic Number (format identifier)
189 * 4-7 -> Format Version
190 * 8-11 -> Format Type (text or binary, decompressed or compressed)
191 * 12-15 -> Float Size (32 or 64 bits) */
192 DWORD header[4];
194 if (!read_bytes(buf, header, sizeof(header)))
195 return DXFILEERR_BADFILETYPE;
197 if (TRACE_ON(d3dxof_parsing))
199 char string[17];
200 memcpy(string, header, 16);
201 string[16] = 0;
202 TRACE("header = '%s'\n", string);
205 if (header[0] != XOFFILE_FORMAT_MAGIC)
206 return DXFILEERR_BADFILETYPE;
208 if (header[1] != XOFFILE_FORMAT_VERSION_302 && header[1] != XOFFILE_FORMAT_VERSION_303)
209 return DXFILEERR_BADFILEVERSION;
211 if (header[2] != XOFFILE_FORMAT_BINARY && header[2] != XOFFILE_FORMAT_TEXT &&
212 header[2] != XOFFILE_FORMAT_BINARY_MSZIP && header[2] != XOFFILE_FORMAT_TEXT_MSZIP)
214 WARN("File type %s unknown\n", debugstr_fourcc(header[2]));
215 return DXFILEERR_BADFILETYPE;
218 if (header[3] != XOFFILE_FORMAT_FLOAT_BITS_32 && header[3] != XOFFILE_FORMAT_FLOAT_BITS_64)
219 return DXFILEERR_BADFILEFLOATSIZE;
221 buf->txt = header[2] == XOFFILE_FORMAT_TEXT || header[2] == XOFFILE_FORMAT_TEXT_MSZIP;
223 if (header[2] == XOFFILE_FORMAT_BINARY_MSZIP || header[2] == XOFFILE_FORMAT_TEXT_MSZIP)
225 /* Extended header for compressed data:
226 * 16-19 -> size of decompressed file including xof header,
227 * 20-21 -> size of first decompressed MSZIP chunk, 22-23 -> size of first compressed MSZIP chunk
228 * 24-xx -> compressed MSZIP data chunk
229 * xx-xx -> size of next decompressed MSZIP chunk, xx-xx -> size of next compressed MSZIP chunk
230 * xx-xx -> compressed MSZIP data chunk
231 * .............................................................................................. */
232 int err;
233 DWORD decomp_file_size;
234 WORD decomp_chunk_size;
235 WORD comp_chunk_size;
236 LPBYTE decomp_buffer;
238 if (!read_bytes(buf, &decomp_file_size, sizeof(decomp_file_size)))
239 return DXFILEERR_BADFILETYPE;
241 TRACE("Compressed format %s detected: decompressed file size with xof header = %d\n",
242 debugstr_fourcc(header[2]), decomp_file_size);
244 /* Does not take xof header into account */
245 decomp_file_size -= 16;
247 decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_file_size);
248 if (!decomp_buffer)
250 ERR("Out of memory\n");
251 return DXFILEERR_BADALLOC;
253 *decomp_buffer_ptr = decomp_buffer;
255 while (buf->rem_bytes)
257 if (!read_bytes(buf, &decomp_chunk_size, sizeof(decomp_chunk_size)))
258 return DXFILEERR_BADFILETYPE;
259 if (!read_bytes(buf, &comp_chunk_size, sizeof(comp_chunk_size)))
260 return DXFILEERR_BADFILETYPE;
262 TRACE("Process chunk: compressed_size = %d, decompressed_size = %d\n",
263 comp_chunk_size, decomp_chunk_size);
265 err = mszip_decompress(comp_chunk_size, decomp_chunk_size, (char*)buf->buffer, (char*)decomp_buffer);
266 if (err)
268 WARN("Error while decompressing MSZIP chunk %d\n", err);
269 HeapFree(GetProcessHeap(), 0, decomp_buffer);
270 return DXFILEERR_BADALLOC;
272 buf->rem_bytes -= comp_chunk_size;
273 buf->buffer += comp_chunk_size;
274 decomp_buffer += decomp_chunk_size;
277 if ((decomp_buffer - *decomp_buffer_ptr) != decomp_file_size)
278 ERR("Size of all decompressed chunks (%u) does not match decompressed file size (%u)\n",
279 (DWORD)(decomp_buffer - *decomp_buffer_ptr), decomp_file_size);
281 /* Use decompressed data */
282 buf->buffer = *decomp_buffer_ptr;
283 buf->rem_bytes = decomp_file_size;
286 TRACE("Header is correct\n");
288 return S_OK;
291 static void dump_TOKEN(WORD token)
293 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
294 switch(token)
296 DUMP_TOKEN(TOKEN_NAME);
297 DUMP_TOKEN(TOKEN_STRING);
298 DUMP_TOKEN(TOKEN_INTEGER);
299 DUMP_TOKEN(TOKEN_GUID);
300 DUMP_TOKEN(TOKEN_INTEGER_LIST);
301 DUMP_TOKEN(TOKEN_FLOAT_LIST);
302 DUMP_TOKEN(TOKEN_OBRACE);
303 DUMP_TOKEN(TOKEN_CBRACE);
304 DUMP_TOKEN(TOKEN_OPAREN);
305 DUMP_TOKEN(TOKEN_CPAREN);
306 DUMP_TOKEN(TOKEN_OBRACKET);
307 DUMP_TOKEN(TOKEN_CBRACKET);
308 DUMP_TOKEN(TOKEN_OANGLE);
309 DUMP_TOKEN(TOKEN_CANGLE);
310 DUMP_TOKEN(TOKEN_DOT);
311 DUMP_TOKEN(TOKEN_COMMA);
312 DUMP_TOKEN(TOKEN_SEMICOLON);
313 DUMP_TOKEN(TOKEN_TEMPLATE);
314 DUMP_TOKEN(TOKEN_WORD);
315 DUMP_TOKEN(TOKEN_DWORD);
316 DUMP_TOKEN(TOKEN_FLOAT);
317 DUMP_TOKEN(TOKEN_DOUBLE);
318 DUMP_TOKEN(TOKEN_CHAR);
319 DUMP_TOKEN(TOKEN_UCHAR);
320 DUMP_TOKEN(TOKEN_SWORD);
321 DUMP_TOKEN(TOKEN_SDWORD);
322 DUMP_TOKEN(TOKEN_VOID);
323 DUMP_TOKEN(TOKEN_LPSTR);
324 DUMP_TOKEN(TOKEN_UNICODE);
325 DUMP_TOKEN(TOKEN_CSTRING);
326 DUMP_TOKEN(TOKEN_ARRAY);
327 default:
328 if (0)
329 TRACE("Unknown token %d\n", token);
330 break;
332 #undef DUMP_TOKEN
335 static BOOL is_space(char c)
337 switch (c)
339 case 0x00:
340 case 0x0D:
341 case 0x0A:
342 case ' ':
343 case '\t':
344 return TRUE;
346 return FALSE;
349 static BOOL is_operator(char c)
351 switch(c)
353 case '{':
354 case '}':
355 case '[':
356 case ']':
357 case '(':
358 case ')':
359 case '<':
360 case '>':
361 case ',':
362 case ';':
363 return TRUE;
365 return FALSE;
368 static inline BOOL is_separator(char c)
370 return is_space(c) || is_operator(c);
373 static WORD get_operator_token(char c)
375 switch(c)
377 case '{':
378 return TOKEN_OBRACE;
379 case '}':
380 return TOKEN_CBRACE;
381 case '[':
382 return TOKEN_OBRACKET;
383 case ']':
384 return TOKEN_CBRACKET;
385 case '(':
386 return TOKEN_OPAREN;
387 case ')':
388 return TOKEN_CPAREN;
389 case '<':
390 return TOKEN_OANGLE;
391 case '>':
392 return TOKEN_CANGLE;
393 case ',':
394 return TOKEN_COMMA;
395 case ';':
396 return TOKEN_SEMICOLON;
398 return 0;
401 static BOOL is_keyword(parse_buffer* buf, const char* keyword)
403 char tmp[8]; /* longest keyword size (template) */
404 DWORD len = strlen(keyword);
406 if (!read_bytes(buf, tmp, len))
407 return FALSE;
408 if (strncasecmp(tmp, keyword, len))
410 rewind_bytes(buf, len);
411 return FALSE;
414 if (!read_bytes(buf, tmp, 1))
415 return TRUE;
416 if (is_separator(tmp[0]))
418 rewind_bytes(buf, 1);
419 return TRUE;
421 rewind_bytes(buf, len+1);
422 return FALSE;
425 static WORD get_keyword_token(parse_buffer* buf)
427 if (is_keyword(buf, "template"))
428 return TOKEN_TEMPLATE;
429 if (is_keyword(buf, "WORD"))
430 return TOKEN_WORD;
431 if (is_keyword(buf, "DWORD"))
432 return TOKEN_DWORD;
433 if (is_keyword(buf, "FLOAT"))
434 return TOKEN_FLOAT;
435 if (is_keyword(buf, "DOUBLE"))
436 return TOKEN_DOUBLE;
437 if (is_keyword(buf, "CHAR"))
438 return TOKEN_CHAR;
439 if (is_keyword(buf, "UCHAR"))
440 return TOKEN_UCHAR;
441 if (is_keyword(buf, "SWORD"))
442 return TOKEN_SWORD;
443 if (is_keyword(buf, "SDWORD"))
444 return TOKEN_SDWORD;
445 if (is_keyword(buf, "VOID"))
446 return TOKEN_VOID;
447 if (is_keyword(buf, "STRING"))
448 return TOKEN_LPSTR;
449 if (is_keyword(buf, "UNICODE"))
450 return TOKEN_UNICODE;
451 if (is_keyword(buf, "CSTRING"))
452 return TOKEN_CSTRING;
453 if (is_keyword(buf, "array"))
454 return TOKEN_ARRAY;
456 return 0;
459 static BOOL is_guid(parse_buffer* buf)
461 char tmp[50];
462 DWORD pos = 1;
463 GUID class_id;
464 DWORD tab[10];
465 int ret;
467 if (buf->rem_bytes < 38 || *buf->buffer != '<')
468 return FALSE;
469 tmp[0] = '<';
470 while (pos < sizeof(tmp) - 2 && *(buf->buffer+pos) != '>')
472 tmp[pos] = *(buf->buffer+pos);
473 pos++;
475 tmp[pos++] = '>';
476 tmp[pos] = 0;
477 if (pos != 38 /* <+36+> */)
479 TRACE("Wrong guid %s (%d)\n", tmp, pos);
480 return FALSE;
482 buf->buffer += pos;
483 buf->rem_bytes -= pos;
485 ret = sscanf(tmp, CLSIDFMT, &class_id.Data1, tab, tab+1, tab+2, tab+3, tab+4, tab+5, tab+6, tab+7, tab+8, tab+9);
486 if (ret != 11)
488 TRACE("Wrong guid %s (%d)\n", tmp, pos);
489 return FALSE;
491 TRACE("Found guid %s (%d)\n", tmp, pos);
493 class_id.Data2 = tab[0];
494 class_id.Data3 = tab[1];
495 class_id.Data4[0] = tab[2];
496 class_id.Data4[1] = tab[3];
497 class_id.Data4[2] = tab[4];
498 class_id.Data4[3] = tab[5];
499 class_id.Data4[4] = tab[6];
500 class_id.Data4[5] = tab[7];
501 class_id.Data4[6] = tab[8];
502 class_id.Data4[7] = tab[9];
504 *(GUID*)buf->value = class_id;
506 return TRUE;
509 static BOOL is_name(parse_buffer* buf)
511 char tmp[512];
512 DWORD pos = 0;
513 char c;
514 BOOL error = FALSE;
515 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
517 if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_') || (c == '-')))
518 error = TRUE;
519 if (pos < sizeof(tmp))
520 tmp[pos] = c;
521 pos++;
523 tmp[min(pos, sizeof(tmp) - 1)] = 0;
525 if (error)
527 TRACE("Wrong name %s\n", tmp);
528 return FALSE;
531 buf->buffer += pos;
532 buf->rem_bytes -= pos;
534 TRACE("Found name %s\n", tmp);
535 strcpy((char*)buf->value, tmp);
537 return TRUE;
540 static BOOL is_float(parse_buffer* buf)
542 char tmp[512];
543 DWORD pos = 0;
544 char c;
545 float decimal;
546 BOOL dot = FALSE;
548 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
550 if (!((!pos && (c == '-')) || ((c >= '0') && (c <= '9')) || (!dot && (c == '.'))))
551 return FALSE;
552 if (c == '.')
553 dot = TRUE;
554 if (pos < sizeof(tmp))
555 tmp[pos] = c;
556 pos++;
558 tmp[min(pos, sizeof(tmp) - 1)] = 0;
560 buf->buffer += pos;
561 buf->rem_bytes -= pos;
563 sscanf(tmp, "%f", &decimal);
565 TRACE("Found float %s - %f\n", tmp, decimal);
567 *(float*)buf->value = decimal;
569 return TRUE;
572 static BOOL is_integer(parse_buffer* buf)
574 char tmp[512];
575 DWORD pos = 0;
576 char c;
577 DWORD integer;
579 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
581 if (!((c >= '0') && (c <= '9')))
582 return FALSE;
583 if (pos < sizeof(tmp))
584 tmp[pos] = c;
585 pos++;
587 tmp[min(pos, sizeof(tmp) - 1)] = 0;
589 buf->buffer += pos;
590 buf->rem_bytes -= pos;
592 sscanf(tmp, "%d", &integer);
594 TRACE("Found integer %s - %d\n", tmp, integer);
596 *(DWORD*)buf->value = integer;
598 return TRUE;
601 static BOOL is_string(parse_buffer* buf)
603 char tmp[512];
604 DWORD pos = 0;
605 char c;
606 BOOL ok = FALSE;
608 if (*buf->buffer != '"')
609 return FALSE;
611 while ((pos+1) < buf->rem_bytes)
613 c = *(buf->buffer+pos+1);
614 if (c == '"')
616 ok = TRUE;
617 break;
619 if (pos < sizeof(tmp))
620 tmp[pos] = c;
621 pos++;
623 tmp[min(pos, sizeof(tmp) - 1)] = 0;
625 if (!ok)
627 TRACE("Wrong string %s\n", tmp);
628 return FALSE;
631 buf->buffer += pos + 2;
632 buf->rem_bytes -= pos + 2;
634 TRACE("Found string %s\n", tmp);
635 strcpy((char*)buf->value, tmp);
637 return TRUE;
640 static WORD parse_TOKEN(parse_buffer * buf)
642 WORD token;
644 if (buf->txt)
646 while(1)
648 char c;
649 if (!read_bytes(buf, &c, 1))
650 return TOKEN_NONE;
651 if ((c == '#') || (c == '/'))
653 /* Handle comment (# or //) */
654 if (c == '/')
656 if (!read_bytes(buf, &c, 1))
657 return TOKEN_ERROR;
658 if (c != '/')
659 return TOKEN_ERROR;
661 c = 0;
662 while (c != 0x0A)
664 if (!read_bytes(buf, &c, 1))
665 return TOKEN_NONE;
667 continue;
669 if (is_space(c))
670 continue;
671 if (is_operator(c) && (c != '<'))
673 token = get_operator_token(c);
674 break;
676 else if (c == '.')
678 token = TOKEN_DOT;
679 break;
681 else
683 rewind_bytes(buf, 1);
685 if ((token = get_keyword_token(buf)))
686 break;
688 if (is_guid(buf))
690 token = TOKEN_GUID;
691 break;
693 if (is_integer(buf))
695 token = TOKEN_INTEGER;
696 break;
698 if (is_float(buf))
700 token = TOKEN_FLOAT;
701 break;
703 if (is_string(buf))
705 token = TOKEN_LPSTR;
706 break;
708 if (is_name(buf))
710 token = TOKEN_NAME;
711 break;
714 FIXME("Unrecognize element\n");
715 return TOKEN_ERROR;
719 else
721 if (!buf->list_nb_elements)
723 if (!read_bytes(buf, &token, 2))
724 return TOKEN_NONE;
726 /* Convert integer and float list into separate elements */
727 if (token == TOKEN_INTEGER_LIST)
729 if (!read_bytes(buf, &buf->list_nb_elements, 4))
730 return TOKEN_ERROR;
731 token = TOKEN_INTEGER;
732 buf->list_type_float = FALSE;
733 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", buf->list_nb_elements);
735 else if (token == TOKEN_FLOAT_LIST)
737 if (!read_bytes(buf, &buf->list_nb_elements, 4))
738 return TOKEN_ERROR;
739 token = TOKEN_FLOAT;
740 buf->list_type_float = TRUE;
741 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", buf->list_nb_elements);
745 if (buf->list_nb_elements)
747 if (buf->list_separator)
749 buf->list_nb_elements--;
750 buf->list_separator = FALSE;
751 /* Insert separarator between each values and since list does not accept separator at the end
752 use a comma so any extra separator will generate an error */
753 token = TOKEN_COMMA;
755 else
757 DWORD value;
759 if (!read_bytes(buf, &value, 4))
760 return TOKEN_ERROR;
761 *(DWORD*)buf->value = value;
763 buf->list_separator = TRUE;
764 /* Convert list into a serie of their basic type counterpart */
765 token = buf->list_type_float ? TOKEN_FLOAT : TOKEN_INTEGER;
767 dump_TOKEN(token);
768 return token;
771 switch (token)
773 case TOKEN_NAME:
775 DWORD count;
776 char *name = (char*)buf->value;
778 if (!read_bytes(buf, &count, 4))
779 return TOKEN_ERROR;
780 if (!read_bytes(buf, name, count))
781 return TOKEN_ERROR;
782 name[count] = 0;
783 TRACE("name = %s\n", name);
785 break;
786 case TOKEN_INTEGER:
788 DWORD integer;
790 if (!read_bytes(buf, &integer, 4))
791 return TOKEN_ERROR;
792 TRACE("integer = %u\n", integer);
794 *(DWORD*)buf->value = integer;
796 break;
797 case TOKEN_GUID:
799 char strguid[39];
800 GUID class_id;
802 if (!read_bytes(buf, &class_id, 16))
803 return TOKEN_ERROR;
804 sprintf(strguid, CLSIDFMT, class_id.Data1, class_id.Data2, class_id.Data3, class_id.Data4[0],
805 class_id.Data4[1], class_id.Data4[2], class_id.Data4[3], class_id.Data4[4], class_id.Data4[5],
806 class_id.Data4[6], class_id.Data4[7]);
807 TRACE("guid = %s\n", strguid);
809 *(GUID*)buf->value = class_id;
811 break;
812 case TOKEN_STRING:
814 DWORD count;
815 char *string = (char*)buf->value;
817 if (!read_bytes(buf, &count, 4))
818 return TOKEN_ERROR;
819 if (!read_bytes(buf, string, count))
820 return TOKEN_ERROR;
821 string[count] = 0;
822 TRACE("string = %s\n", string);
824 token = TOKEN_LPSTR;
826 break;
827 case TOKEN_OBRACE:
828 case TOKEN_CBRACE:
829 case TOKEN_OPAREN:
830 case TOKEN_CPAREN:
831 case TOKEN_OBRACKET:
832 case TOKEN_CBRACKET:
833 case TOKEN_OANGLE:
834 case TOKEN_CANGLE:
835 case TOKEN_DOT:
836 case TOKEN_COMMA:
837 case TOKEN_SEMICOLON:
838 case TOKEN_TEMPLATE:
839 case TOKEN_WORD:
840 case TOKEN_DWORD:
841 case TOKEN_FLOAT:
842 case TOKEN_DOUBLE:
843 case TOKEN_CHAR:
844 case TOKEN_UCHAR:
845 case TOKEN_SWORD:
846 case TOKEN_SDWORD:
847 case TOKEN_VOID:
848 case TOKEN_LPSTR:
849 case TOKEN_UNICODE:
850 case TOKEN_CSTRING:
851 case TOKEN_ARRAY:
852 break;
853 default:
854 return TOKEN_ERROR;
858 dump_TOKEN(token);
860 return token;
863 static WORD get_TOKEN(parse_buffer * buf)
865 if (buf->token_present)
867 buf->token_present = FALSE;
868 return buf->current_token;
871 buf->current_token = parse_TOKEN(buf);
873 return buf->current_token;
876 static WORD check_TOKEN(parse_buffer * buf)
878 if (buf->token_present)
879 return buf->current_token;
881 buf->current_token = parse_TOKEN(buf);
882 buf->token_present = TRUE;
884 return buf->current_token;
887 static inline BOOL is_primitive_type(WORD token)
889 BOOL ret;
890 switch(token)
892 case TOKEN_WORD:
893 case TOKEN_DWORD:
894 case TOKEN_FLOAT:
895 case TOKEN_DOUBLE:
896 case TOKEN_CHAR:
897 case TOKEN_UCHAR:
898 case TOKEN_SWORD:
899 case TOKEN_SDWORD:
900 case TOKEN_LPSTR:
901 case TOKEN_UNICODE:
902 case TOKEN_CSTRING:
903 ret = TRUE;
904 break;
905 default:
906 ret = FALSE;
907 break;
909 return ret;
912 static BOOL parse_template_option_info(parse_buffer * buf)
914 xtemplate* cur_template = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates];
916 if (check_TOKEN(buf) == TOKEN_DOT)
918 get_TOKEN(buf);
919 if (get_TOKEN(buf) != TOKEN_DOT)
920 return FALSE;
921 if (get_TOKEN(buf) != TOKEN_DOT)
922 return FALSE;
923 cur_template->open = TRUE;
925 else
927 while (1)
929 if (get_TOKEN(buf) != TOKEN_NAME)
930 return FALSE;
931 strcpy(cur_template->children[cur_template->nb_children], (char*)buf->value);
932 if (check_TOKEN(buf) == TOKEN_GUID)
933 get_TOKEN(buf);
934 cur_template->nb_children++;
935 if (check_TOKEN(buf) != TOKEN_COMMA)
936 break;
937 get_TOKEN(buf);
939 cur_template->open = FALSE;
942 return TRUE;
945 static BOOL parse_template_members_list(parse_buffer * buf)
947 int idx_member = 0;
948 member* cur_member;
950 while (1)
952 BOOL array = FALSE;
953 int nb_dims = 0;
954 cur_member = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[idx_member];
956 if (check_TOKEN(buf) == TOKEN_ARRAY)
958 get_TOKEN(buf);
959 array = TRUE;
962 if (check_TOKEN(buf) == TOKEN_NAME)
964 cur_member->type = get_TOKEN(buf);
965 if (!strcmp((char*)buf->value, "indexColor"))
967 /* Case sensitive legacy type indexColor is described in the first template */
968 cur_member->idx_template = 0;
970 else
972 cur_member->idx_template = 1;
973 while (cur_member->idx_template < buf->pdxf->nb_xtemplates)
975 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[cur_member->idx_template].name))
976 break;
977 cur_member->idx_template++;
979 if (cur_member->idx_template == buf->pdxf->nb_xtemplates)
981 WARN("Reference to a nonexistent template '%s'\n", (char*)buf->value);
982 return FALSE;
986 else if (is_primitive_type(check_TOKEN(buf)))
987 cur_member->type = get_TOKEN(buf);
988 else
989 break;
991 if (get_TOKEN(buf) != TOKEN_NAME)
992 return FALSE;
993 strcpy(cur_member->name, (char*)buf->value);
995 if (array)
997 while (check_TOKEN(buf) == TOKEN_OBRACKET)
999 if (nb_dims >= MAX_ARRAY_DIM)
1001 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims + 1);
1002 return FALSE;
1004 get_TOKEN(buf);
1005 if (check_TOKEN(buf) == TOKEN_INTEGER)
1007 get_TOKEN(buf);
1008 cur_member->dim_fixed[nb_dims] = TRUE;
1009 cur_member->dim_value[nb_dims] = *(DWORD*)buf->value;
1011 else
1013 int i;
1014 if (get_TOKEN(buf) != TOKEN_NAME)
1015 return FALSE;
1016 for (i = 0; i < idx_member; i++)
1018 if (!strcmp((char*)buf->value, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].name))
1020 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].nb_dims)
1022 ERR("Array cannot be used to specify variable array size\n");
1023 return FALSE;
1025 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].type != TOKEN_DWORD)
1027 FIXME("Only DWORD supported to specify variable array size\n");
1028 return FALSE;
1030 break;
1033 if (i == idx_member)
1035 ERR("Reference to unknown member %s\n", (char*)buf->value);
1036 return FALSE;
1038 cur_member->dim_fixed[nb_dims] = FALSE;
1039 cur_member->dim_value[nb_dims] = i;
1041 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1042 return FALSE;
1043 nb_dims++;
1045 if (!nb_dims)
1046 return FALSE;
1047 cur_member->nb_dims = nb_dims;
1049 if (get_TOKEN(buf) != TOKEN_SEMICOLON)
1050 return FALSE;
1052 idx_member++;
1055 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].nb_members = idx_member;
1057 return TRUE;
1060 static BOOL parse_template_parts(parse_buffer * buf)
1062 if (!parse_template_members_list(buf))
1063 return FALSE;
1064 if (check_TOKEN(buf) == TOKEN_OBRACKET)
1066 get_TOKEN(buf);
1067 if (!parse_template_option_info(buf))
1068 return FALSE;
1069 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1070 return FALSE;
1073 return TRUE;
1076 static BOOL parse_template(parse_buffer * buf)
1078 if (get_TOKEN(buf) != TOKEN_TEMPLATE)
1079 return FALSE;
1080 if (get_TOKEN(buf) != TOKEN_NAME)
1081 return FALSE;
1082 strcpy(buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, (char*)buf->value);
1083 if (get_TOKEN(buf) != TOKEN_OBRACE)
1084 return FALSE;
1085 if (get_TOKEN(buf) != TOKEN_GUID)
1086 return FALSE;
1087 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id = *(GUID*)buf->value;
1088 if (!parse_template_parts(buf))
1089 return FALSE;
1090 if (get_TOKEN(buf) != TOKEN_CBRACE)
1091 return FALSE;
1093 TRACE("%d - %s - %s\n", buf->pdxf->nb_xtemplates, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, debugstr_guid(&buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id));
1094 buf->pdxf->nb_xtemplates++;
1096 return TRUE;
1099 BOOL parse_templates(parse_buffer * buf, BOOL templates_only)
1101 while (check_TOKEN(buf) != TOKEN_NONE)
1103 if (templates_only && (check_TOKEN(buf) != TOKEN_TEMPLATE))
1104 return TRUE;
1105 if (!parse_template(buf))
1107 WARN("Template is not correct\n");
1108 return FALSE;
1110 else
1112 TRACE("Template successfully parsed:\n");
1113 if (TRACE_ON(d3dxof_parsing))
1114 dump_template(buf->pdxf->xtemplates, &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates - 1]);
1117 return TRUE;
1120 static BOOL check_buffer(parse_buffer * buf, ULONG size)
1122 if ((buf->cur_pos_data + size) > buf->capacity)
1124 LPBYTE pdata;
1125 ULONG new_capacity = buf->capacity ? 2 * buf->capacity : 100000;
1127 pdata = HeapAlloc(GetProcessHeap(), 0, new_capacity);
1128 if (!pdata)
1129 return FALSE;
1130 memcpy(pdata, buf->pdata, buf->cur_pos_data);
1131 HeapFree(GetProcessHeap(), 0, buf->pdata);
1132 buf->capacity = new_capacity;
1133 buf->pdata = pdata;
1134 buf->pxo->root->pdata = pdata;
1136 return TRUE;
1139 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional);
1140 static BOOL parse_object_members_list(parse_buffer * buf)
1142 DWORD token;
1143 ULONG i;
1144 xtemplate* pt = buf->pxt[buf->level];
1146 buf->pxo->nb_members = pt->nb_members;
1148 for (i = 0; i < pt->nb_members; i++)
1150 ULONG k;
1151 ULONG nb_elems = 1;
1152 BOOL basic_type = TRUE;
1154 buf->pxo->members[i].name = pt->members[i].name;
1155 buf->pxo->members[i].start = buf->cur_pos_data;
1157 for (k = 0; k < pt->members[i].nb_dims; k++)
1159 if (pt->members[i].dim_fixed[k])
1160 nb_elems *= pt->members[i].dim_value[k];
1161 else
1162 nb_elems *= *(DWORD*)(buf->pxo->root->pdata + buf->pxo->members[pt->members[i].dim_value[k]].start);
1165 TRACE("Elements to consider: %u\n", nb_elems);
1167 for (k = 0; k < nb_elems; k++)
1169 if (pt->members[i].type == TOKEN_NAME)
1171 ULONG j;
1173 TRACE("Found sub-object %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1174 basic_type = FALSE;
1175 buf->level++;
1176 /* To do template lookup */
1177 for (j = 0; j < buf->pdxf->nb_xtemplates; j++)
1179 if (!strcasecmp(buf->pdxf->xtemplates[pt->members[i].idx_template].name, buf->pdxf->xtemplates[j].name))
1181 buf->pxt[buf->level] = &buf->pdxf->xtemplates[j];
1182 break;
1185 if (j == buf->pdxf->nb_xtemplates)
1187 ERR("Unknown template %s\n", (char*)buf->value);
1188 buf->level--;
1189 return FALSE;
1191 TRACE("Enter %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1192 if (!parse_object_parts(buf, FALSE))
1194 buf->level--;
1195 return FALSE;
1197 buf->level--;
1199 else
1201 token = check_TOKEN(buf);
1202 if (token == TOKEN_INTEGER)
1204 get_TOKEN(buf);
1205 TRACE("%s = %d\n", pt->members[i].name, *(DWORD*)buf->value);
1206 /* Assume larger size */
1207 if (!check_buffer(buf, 4))
1208 return FALSE;
1209 if (pt->members[i].type == TOKEN_WORD)
1211 *(((WORD*)(buf->pdata + buf->cur_pos_data))) = (WORD)(*(DWORD*)buf->value);
1212 buf->cur_pos_data += 2;
1214 else if (pt->members[i].type == TOKEN_DWORD)
1216 *(((DWORD*)(buf->pdata + buf->cur_pos_data))) = (DWORD)(*(DWORD*)buf->value);
1217 buf->cur_pos_data += 4;
1219 else
1221 FIXME("Token %d not supported\n", pt->members[i].type);
1222 return FALSE;
1225 else if (token == TOKEN_FLOAT)
1227 get_TOKEN(buf);
1228 TRACE("%s = %f\n", pt->members[i].name, *(float*)buf->value);
1229 if (!check_buffer(buf, 4))
1230 return FALSE;
1231 if (pt->members[i].type == TOKEN_FLOAT)
1233 *(((float*)(buf->pdata + buf->cur_pos_data))) = (float)(*(float*)buf->value);
1234 buf->cur_pos_data += 4;
1236 else
1238 FIXME("Token %d not supported\n", pt->members[i].type);
1239 return FALSE;
1242 else if (token == TOKEN_LPSTR)
1244 get_TOKEN(buf);
1245 TRACE("%s = %s\n", pt->members[i].name, (char*)buf->value);
1246 if (!check_buffer(buf, sizeof(LPSTR)))
1247 return FALSE;
1248 if (pt->members[i].type == TOKEN_LPSTR)
1250 int len = strlen((char*)buf->value) + 1;
1251 if ((buf->cur_pstrings - buf->pstrings + len) > MAX_STRINGS_BUFFER)
1253 FIXME("Buffer too small %p %p %d\n", buf->cur_pstrings, buf->pstrings, len);
1254 return FALSE;
1256 strcpy((char*)buf->cur_pstrings, (char*)buf->value);
1257 *(((LPCSTR*)(buf->pdata + buf->cur_pos_data))) = (char*)buf->cur_pstrings;
1258 buf->cur_pstrings += len;
1259 buf->cur_pos_data += sizeof(LPSTR);
1261 else
1263 FIXME("Token %d not supported\n", pt->members[i].type);
1264 return FALSE;
1267 else
1269 WARN("Unexpected token %d\n", token);
1270 return FALSE;
1274 if (basic_type)
1276 /* Handle separator only for basic types */
1277 token = check_TOKEN(buf);
1278 if ((token != TOKEN_COMMA) && (token != TOKEN_SEMICOLON))
1279 return FALSE;
1280 /* Allow multi-semicolons + single comma separator */
1281 while (check_TOKEN(buf) == TOKEN_SEMICOLON)
1282 get_TOKEN(buf);
1283 if (check_TOKEN(buf) == TOKEN_COMMA)
1284 get_TOKEN(buf);
1288 buf->pxo->members[i].size = buf->cur_pos_data - buf->pxo->members[i].start;
1291 return TRUE;
1294 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional)
1296 buf->pxo->nb_children = 0;
1298 if (!parse_object_members_list(buf))
1299 return FALSE;
1301 if (allow_optional)
1303 buf->pxo->size = buf->cur_pos_data - buf->pxo->pos_data;
1305 while (1)
1307 if (check_TOKEN(buf) == TOKEN_OBRACE)
1309 ULONG i, j;
1310 get_TOKEN(buf);
1311 if (get_TOKEN(buf) != TOKEN_NAME)
1312 return FALSE;
1313 if (get_TOKEN(buf) != TOKEN_CBRACE)
1314 return FALSE;
1315 TRACE("Found optional reference %s\n", (char*)buf->value);
1316 for (i = 0; i < (buf->nb_pxo_globals+1); i++)
1318 for (j = 0; j < (buf->pxo_globals[i])[0].nb_subobjects; j++)
1320 if (!strcmp((buf->pxo_globals[i])[j].name, (char*)buf->value))
1321 goto _exit;
1324 _exit:
1325 if (i == (buf->nb_pxo_globals+1))
1327 ERR("Reference to unknown object %s\n", (char*)buf->value);
1328 return FALSE;
1331 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1333 FIXME("Too many sub-objects\n");
1334 return FALSE;
1337 buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1338 buf->pxo->children[buf->pxo->nb_children]->ptarget = &(buf->pxo_globals[i])[j];
1339 buf->pxo->children[buf->pxo->nb_children]->binary = FALSE;
1340 buf->pxo->nb_children++;
1342 else if (check_TOKEN(buf) == TOKEN_NAME)
1344 xobject* pxo = buf->pxo;
1346 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1348 FIXME("Too many sub-objects\n");
1349 return FALSE;
1352 buf->pxo = buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects];
1353 pxo->root->nb_subobjects++;
1355 TRACE("Enter optional %s\n", (char*)buf->value);
1356 buf->level++;
1357 if (!parse_object(buf))
1359 buf->level--;
1360 return FALSE;
1362 buf->level--;
1363 buf->pxo = pxo;
1364 buf->pxo->nb_children++;
1366 else
1367 break;
1371 if (buf->pxo->nb_children > MAX_CHILDREN)
1373 FIXME("Too many children %d\n", buf->pxo->nb_children);
1374 return FALSE;
1377 return TRUE;
1380 BOOL parse_object(parse_buffer * buf)
1382 ULONG i;
1384 buf->pxo->pos_data = buf->cur_pos_data;
1385 buf->pxo->ptarget = NULL;
1386 buf->pxo->binary = FALSE;
1387 buf->pxo->root = buf->pxo_tab;
1389 if (get_TOKEN(buf) != TOKEN_NAME)
1390 return FALSE;
1392 /* To do template lookup */
1393 for (i = 0; i < buf->pdxf->nb_xtemplates; i++)
1395 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[i].name))
1397 buf->pxt[buf->level] = &buf->pdxf->xtemplates[i];
1398 memcpy(&buf->pxo->type, &buf->pdxf->xtemplates[i].class_id, 16);
1399 break;
1402 if (i == buf->pdxf->nb_xtemplates)
1404 ERR("Unknown template %s\n", (char*)buf->value);
1405 return FALSE;
1408 if (check_TOKEN(buf) == TOKEN_NAME)
1410 get_TOKEN(buf);
1411 strcpy(buf->pxo->name, (char*)buf->value);
1413 else
1414 buf->pxo->name[0] = 0;
1416 if (get_TOKEN(buf) != TOKEN_OBRACE)
1417 return FALSE;
1418 if (check_TOKEN(buf) == TOKEN_GUID)
1420 get_TOKEN(buf);
1421 memcpy(&buf->pxo->class_id, buf->value, 16);
1423 else
1424 memset(&buf->pxo->class_id, 0, 16);
1426 if (!parse_object_parts(buf, TRUE))
1427 return FALSE;
1428 if (get_TOKEN(buf) != TOKEN_CBRACE)
1429 return FALSE;
1431 /* For seeking to a possibly eof to avoid parsing another object next time */
1432 check_TOKEN(buf);
1434 return TRUE;