wined3d: Use wined3d_bit_scan() in vshader_get_input().
[wine.git] / dlls / d3dxof / parsing.c
blobe29d17810d5183f370c671e6d364ffc36ac5e3f2
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 "wine/debug.h"
23 #define COBJMACROS
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winternl.h"
29 #include "d3dxof_private.h"
30 #include "dxfile.h"
32 #include <stdio.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof_parsing);
36 #define MAKEFOUR(a,b,c,d) ((DWORD)a + ((DWORD)b << 8) + ((DWORD)c << 16) + ((DWORD)d << 24))
37 #define XOFFILE_FORMAT_MAGIC MAKEFOUR('x','o','f',' ')
38 #define XOFFILE_FORMAT_VERSION_302 MAKEFOUR('0','3','0','2')
39 #define XOFFILE_FORMAT_VERSION_303 MAKEFOUR('0','3','0','3')
40 #define XOFFILE_FORMAT_BINARY MAKEFOUR('b','i','n',' ')
41 #define XOFFILE_FORMAT_TEXT MAKEFOUR('t','x','t',' ')
42 #define XOFFILE_FORMAT_BINARY_MSZIP MAKEFOUR('b','z','i','p')
43 #define XOFFILE_FORMAT_TEXT_MSZIP MAKEFOUR('t','z','i','p')
44 #define XOFFILE_FORMAT_COMPRESSED MAKEFOUR('c','m','p',' ')
45 #define XOFFILE_FORMAT_FLOAT_BITS_32 MAKEFOUR('0','0','3','2')
46 #define XOFFILE_FORMAT_FLOAT_BITS_64 MAKEFOUR('0','0','6','4')
48 #define TOKEN_ERROR 0xffff
49 #define TOKEN_NONE 0
50 #define TOKEN_NAME 1
51 #define TOKEN_STRING 2
52 #define TOKEN_INTEGER 3
53 #define TOKEN_GUID 5
54 #define TOKEN_INTEGER_LIST 6
55 #define TOKEN_FLOAT_LIST 7
56 #define TOKEN_OBRACE 10
57 #define TOKEN_CBRACE 11
58 #define TOKEN_OPAREN 12
59 #define TOKEN_CPAREN 13
60 #define TOKEN_OBRACKET 14
61 #define TOKEN_CBRACKET 15
62 #define TOKEN_OANGLE 16
63 #define TOKEN_CANGLE 17
64 #define TOKEN_DOT 18
65 #define TOKEN_COMMA 19
66 #define TOKEN_SEMICOLON 20
67 #define TOKEN_TEMPLATE 31
68 #define TOKEN_WORD 40
69 #define TOKEN_DWORD 41
70 #define TOKEN_FLOAT 42
71 #define TOKEN_DOUBLE 43
72 #define TOKEN_CHAR 44
73 #define TOKEN_UCHAR 45
74 #define TOKEN_SWORD 46
75 #define TOKEN_SDWORD 47
76 #define TOKEN_VOID 48
77 #define TOKEN_LPSTR 49
78 #define TOKEN_UNICODE 50
79 #define TOKEN_CSTRING 51
80 #define TOKEN_ARRAY 52
82 #define CLSIDFMT "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>"
84 /* FOURCC to string conversion for debug messages */
85 static const char *debugstr_fourcc(DWORD fourcc)
87 if (!fourcc) return "'null'";
88 return wine_dbg_sprintf ("\'%c%c%c%c\'",
89 (char)(fourcc), (char)(fourcc >> 8),
90 (char)(fourcc >> 16), (char)(fourcc >> 24));
93 static const char* get_primitive_string(DWORD token)
95 switch(token)
97 case TOKEN_WORD:
98 return "WORD";
99 case TOKEN_DWORD:
100 return "DWORD";
101 case TOKEN_FLOAT:
102 return "FLOAT";
103 case TOKEN_DOUBLE:
104 return "DOUBLE";
105 case TOKEN_CHAR:
106 return "CHAR";
107 case TOKEN_UCHAR:
108 return "UCHAR";
109 case TOKEN_SWORD:
110 return "SWORD";
111 case TOKEN_SDWORD:
112 return "SDWORD";
113 case TOKEN_VOID:
114 return "VOID";
115 case TOKEN_LPSTR:
116 return "STRING";
117 case TOKEN_UNICODE:
118 return "UNICODE";
119 case TOKEN_CSTRING:
120 return "CSTRING ";
121 default:
122 break;
124 return NULL;
127 static void dump_template(xtemplate* templates_array, xtemplate* ptemplate)
129 ULONG j, k;
130 GUID* clsid;
132 clsid = &ptemplate->class_id;
134 wine_dbg_printf("template %s\n", ptemplate->name);
135 wine_dbg_printf("{\n");
136 wine_dbg_printf(CLSIDFMT "\n", clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
137 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
138 for (j = 0; j < ptemplate->nb_members; j++)
140 if (ptemplate->members[j].nb_dims)
141 wine_dbg_printf("array ");
142 if (ptemplate->members[j].type == TOKEN_NAME)
143 wine_dbg_printf("%s ", templates_array[ptemplate->members[j].idx_template].name);
144 else
145 wine_dbg_printf("%s ", get_primitive_string(ptemplate->members[j].type));
146 wine_dbg_printf("%s", ptemplate->members[j].name);
147 for (k = 0; k < ptemplate->members[j].nb_dims; k++)
149 if (ptemplate->members[j].dim_fixed[k])
150 wine_dbg_printf("[%d]", ptemplate->members[j].dim_value[k]);
151 else
152 wine_dbg_printf("[%s]", ptemplate->members[ptemplate->members[j].dim_value[k]].name);
154 wine_dbg_printf(";\n");
156 if (ptemplate->open)
157 wine_dbg_printf("[...]\n");
158 else if (ptemplate->nb_children)
160 wine_dbg_printf("[%s", ptemplate->children[0]);
161 for (j = 1; j < ptemplate->nb_children; j++)
162 wine_dbg_printf(",%s", ptemplate->children[j]);
163 wine_dbg_printf("]\n");
165 wine_dbg_printf("}\n");
168 static BOOL read_bytes(parse_buffer * buf, LPVOID data, DWORD size)
170 if (buf->rem_bytes < size)
171 return FALSE;
172 memcpy(data, buf->buffer, size);
173 buf->buffer += size;
174 buf->rem_bytes -= size;
175 return TRUE;
178 static void rewind_bytes(parse_buffer * buf, DWORD size)
180 buf->buffer -= size;
181 buf->rem_bytes += size;
184 HRESULT parse_header(parse_buffer * buf, BYTE ** decomp_buffer_ptr)
186 /* X File common header:
187 * 0-3 -> Magic Number (format identifier)
188 * 4-7 -> Format Version
189 * 8-11 -> Format Type (text or binary, decompressed or compressed)
190 * 12-15 -> Float Size (32 or 64 bits) */
191 DWORD header[4];
193 if (!read_bytes(buf, header, sizeof(header)))
194 return DXFILEERR_BADFILETYPE;
196 if (TRACE_ON(d3dxof_parsing))
198 char string[17];
199 memcpy(string, header, 16);
200 string[16] = 0;
201 TRACE("header = '%s'\n", string);
204 if (header[0] != XOFFILE_FORMAT_MAGIC)
205 return DXFILEERR_BADFILETYPE;
207 if (header[1] != XOFFILE_FORMAT_VERSION_302 && header[1] != XOFFILE_FORMAT_VERSION_303)
208 return DXFILEERR_BADFILEVERSION;
210 if (header[2] != XOFFILE_FORMAT_BINARY && header[2] != XOFFILE_FORMAT_TEXT &&
211 header[2] != XOFFILE_FORMAT_BINARY_MSZIP && header[2] != XOFFILE_FORMAT_TEXT_MSZIP)
213 WARN("File type %s unknown\n", debugstr_fourcc(header[2]));
214 return DXFILEERR_BADFILETYPE;
217 if (header[3] != XOFFILE_FORMAT_FLOAT_BITS_32 && header[3] != XOFFILE_FORMAT_FLOAT_BITS_64)
218 return DXFILEERR_BADFILEFLOATSIZE;
220 buf->txt = header[2] == XOFFILE_FORMAT_TEXT || header[2] == XOFFILE_FORMAT_TEXT_MSZIP;
222 if (header[2] == XOFFILE_FORMAT_BINARY_MSZIP || header[2] == XOFFILE_FORMAT_TEXT_MSZIP)
224 /* Extended header for compressed data:
225 * 16-19 -> size of decompressed file including xof header,
226 * 20-21 -> size of first decompressed MSZIP chunk, 22-23 -> size of first compressed MSZIP chunk
227 * 24-xx -> compressed MSZIP data chunk
228 * xx-xx -> size of next decompressed MSZIP chunk, xx-xx -> size of next compressed MSZIP chunk
229 * xx-xx -> compressed MSZIP data chunk
230 * .............................................................................................. */
231 int err;
232 DWORD decomp_file_size;
233 WORD decomp_chunk_size;
234 WORD comp_chunk_size;
235 LPBYTE decomp_buffer;
237 if (!read_bytes(buf, &decomp_file_size, sizeof(decomp_file_size)))
238 return DXFILEERR_BADFILETYPE;
240 TRACE("Compressed format %s detected: decompressed file size with xof header = %d\n",
241 debugstr_fourcc(header[2]), decomp_file_size);
243 /* Does not take xof header into account */
244 decomp_file_size -= 16;
246 decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_file_size);
247 if (!decomp_buffer)
249 ERR("Out of memory\n");
250 return DXFILEERR_BADALLOC;
252 *decomp_buffer_ptr = decomp_buffer;
254 while (buf->rem_bytes)
256 if (!read_bytes(buf, &decomp_chunk_size, sizeof(decomp_chunk_size)))
257 return DXFILEERR_BADFILETYPE;
258 if (!read_bytes(buf, &comp_chunk_size, sizeof(comp_chunk_size)))
259 return DXFILEERR_BADFILETYPE;
261 TRACE("Process chunk: compressed_size = %d, decompressed_size = %d\n",
262 comp_chunk_size, decomp_chunk_size);
264 err = mszip_decompress(comp_chunk_size, decomp_chunk_size, (char*)buf->buffer, (char*)decomp_buffer);
265 if (err)
267 WARN("Error while decompressing MSZIP chunk %d\n", err);
268 HeapFree(GetProcessHeap(), 0, decomp_buffer);
269 return DXFILEERR_BADALLOC;
271 buf->rem_bytes -= comp_chunk_size;
272 buf->buffer += comp_chunk_size;
273 decomp_buffer += decomp_chunk_size;
276 if ((decomp_buffer - *decomp_buffer_ptr) != decomp_file_size)
277 ERR("Size of all decompressed chunks (%u) does not match decompressed file size (%u)\n",
278 (DWORD)(decomp_buffer - *decomp_buffer_ptr), decomp_file_size);
280 /* Use decompressed data */
281 buf->buffer = *decomp_buffer_ptr;
282 buf->rem_bytes = decomp_file_size;
285 TRACE("Header is correct\n");
287 return S_OK;
290 static void dump_TOKEN(WORD token)
292 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
293 switch(token)
295 DUMP_TOKEN(TOKEN_NAME);
296 DUMP_TOKEN(TOKEN_STRING);
297 DUMP_TOKEN(TOKEN_INTEGER);
298 DUMP_TOKEN(TOKEN_GUID);
299 DUMP_TOKEN(TOKEN_INTEGER_LIST);
300 DUMP_TOKEN(TOKEN_FLOAT_LIST);
301 DUMP_TOKEN(TOKEN_OBRACE);
302 DUMP_TOKEN(TOKEN_CBRACE);
303 DUMP_TOKEN(TOKEN_OPAREN);
304 DUMP_TOKEN(TOKEN_CPAREN);
305 DUMP_TOKEN(TOKEN_OBRACKET);
306 DUMP_TOKEN(TOKEN_CBRACKET);
307 DUMP_TOKEN(TOKEN_OANGLE);
308 DUMP_TOKEN(TOKEN_CANGLE);
309 DUMP_TOKEN(TOKEN_DOT);
310 DUMP_TOKEN(TOKEN_COMMA);
311 DUMP_TOKEN(TOKEN_SEMICOLON);
312 DUMP_TOKEN(TOKEN_TEMPLATE);
313 DUMP_TOKEN(TOKEN_WORD);
314 DUMP_TOKEN(TOKEN_DWORD);
315 DUMP_TOKEN(TOKEN_FLOAT);
316 DUMP_TOKEN(TOKEN_DOUBLE);
317 DUMP_TOKEN(TOKEN_CHAR);
318 DUMP_TOKEN(TOKEN_UCHAR);
319 DUMP_TOKEN(TOKEN_SWORD);
320 DUMP_TOKEN(TOKEN_SDWORD);
321 DUMP_TOKEN(TOKEN_VOID);
322 DUMP_TOKEN(TOKEN_LPSTR);
323 DUMP_TOKEN(TOKEN_UNICODE);
324 DUMP_TOKEN(TOKEN_CSTRING);
325 DUMP_TOKEN(TOKEN_ARRAY);
326 default:
327 if (0)
328 TRACE("Unknown token %d\n", token);
329 break;
331 #undef DUMP_TOKEN
334 static BOOL is_space(char c)
336 switch (c)
338 case 0x00:
339 case 0x0D:
340 case 0x0A:
341 case ' ':
342 case '\t':
343 return TRUE;
345 return FALSE;
348 static BOOL is_operator(char c)
350 switch(c)
352 case '{':
353 case '}':
354 case '[':
355 case ']':
356 case '(':
357 case ')':
358 case '<':
359 case '>':
360 case ',':
361 case ';':
362 return TRUE;
364 return FALSE;
367 static inline BOOL is_separator(char c)
369 return is_space(c) || is_operator(c);
372 static WORD get_operator_token(char c)
374 switch(c)
376 case '{':
377 return TOKEN_OBRACE;
378 case '}':
379 return TOKEN_CBRACE;
380 case '[':
381 return TOKEN_OBRACKET;
382 case ']':
383 return TOKEN_CBRACKET;
384 case '(':
385 return TOKEN_OPAREN;
386 case ')':
387 return TOKEN_CPAREN;
388 case '<':
389 return TOKEN_OANGLE;
390 case '>':
391 return TOKEN_CANGLE;
392 case ',':
393 return TOKEN_COMMA;
394 case ';':
395 return TOKEN_SEMICOLON;
397 return 0;
400 static BOOL is_keyword(parse_buffer* buf, const char* keyword)
402 char tmp[8]; /* longest keyword size (template) */
403 DWORD len = strlen(keyword);
405 if (!read_bytes(buf, tmp, len))
406 return FALSE;
407 if (_strnicmp(tmp, keyword, len))
409 rewind_bytes(buf, len);
410 return FALSE;
413 if (!read_bytes(buf, tmp, 1))
414 return TRUE;
415 if (is_separator(tmp[0]))
417 rewind_bytes(buf, 1);
418 return TRUE;
420 rewind_bytes(buf, len+1);
421 return FALSE;
424 static WORD get_keyword_token(parse_buffer* buf)
426 if (is_keyword(buf, "template"))
427 return TOKEN_TEMPLATE;
428 if (is_keyword(buf, "WORD"))
429 return TOKEN_WORD;
430 if (is_keyword(buf, "DWORD"))
431 return TOKEN_DWORD;
432 if (is_keyword(buf, "FLOAT"))
433 return TOKEN_FLOAT;
434 if (is_keyword(buf, "DOUBLE"))
435 return TOKEN_DOUBLE;
436 if (is_keyword(buf, "CHAR"))
437 return TOKEN_CHAR;
438 if (is_keyword(buf, "UCHAR"))
439 return TOKEN_UCHAR;
440 if (is_keyword(buf, "SWORD"))
441 return TOKEN_SWORD;
442 if (is_keyword(buf, "SDWORD"))
443 return TOKEN_SDWORD;
444 if (is_keyword(buf, "VOID"))
445 return TOKEN_VOID;
446 if (is_keyword(buf, "STRING"))
447 return TOKEN_LPSTR;
448 if (is_keyword(buf, "UNICODE"))
449 return TOKEN_UNICODE;
450 if (is_keyword(buf, "CSTRING"))
451 return TOKEN_CSTRING;
452 if (is_keyword(buf, "array"))
453 return TOKEN_ARRAY;
455 return 0;
458 static BOOL is_guid(parse_buffer* buf)
460 char tmp[50];
461 DWORD pos = 1;
462 GUID class_id;
463 DWORD tab[10];
464 int ret;
466 if (buf->rem_bytes < 38 || *buf->buffer != '<')
467 return FALSE;
468 tmp[0] = '<';
469 while (pos < sizeof(tmp) - 2 && *(buf->buffer+pos) != '>')
471 tmp[pos] = *(buf->buffer+pos);
472 pos++;
474 tmp[pos++] = '>';
475 tmp[pos] = 0;
476 if (pos != 38 /* <+36+> */)
478 TRACE("Wrong guid %s (%d)\n", tmp, pos);
479 return FALSE;
481 buf->buffer += pos;
482 buf->rem_bytes -= pos;
484 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);
485 if (ret != 11)
487 TRACE("Wrong guid %s (%d)\n", tmp, pos);
488 return FALSE;
490 TRACE("Found guid %s (%d)\n", tmp, pos);
492 class_id.Data2 = tab[0];
493 class_id.Data3 = tab[1];
494 class_id.Data4[0] = tab[2];
495 class_id.Data4[1] = tab[3];
496 class_id.Data4[2] = tab[4];
497 class_id.Data4[3] = tab[5];
498 class_id.Data4[4] = tab[6];
499 class_id.Data4[5] = tab[7];
500 class_id.Data4[6] = tab[8];
501 class_id.Data4[7] = tab[9];
503 *(GUID*)buf->value = class_id;
505 return TRUE;
508 static BOOL is_name(parse_buffer* buf)
510 char tmp[512];
511 DWORD pos = 0;
512 char c;
513 BOOL error = FALSE;
514 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
516 if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_') || (c == '-')))
517 error = TRUE;
518 if (pos < sizeof(tmp))
519 tmp[pos] = c;
520 pos++;
522 tmp[min(pos, sizeof(tmp) - 1)] = 0;
524 if (error)
526 TRACE("Wrong name %s\n", tmp);
527 return FALSE;
530 buf->buffer += pos;
531 buf->rem_bytes -= pos;
533 TRACE("Found name %s\n", tmp);
534 strcpy((char*)buf->value, tmp);
536 return TRUE;
539 static BOOL is_float(parse_buffer* buf)
541 char tmp[512];
542 DWORD pos = 0;
543 char c;
544 float decimal;
545 BOOL dot = FALSE;
547 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
549 if (!((!pos && (c == '-')) || ((c >= '0') && (c <= '9')) || (!dot && (c == '.'))))
550 return FALSE;
551 if (c == '.')
552 dot = TRUE;
553 if (pos < sizeof(tmp))
554 tmp[pos] = c;
555 pos++;
557 tmp[min(pos, sizeof(tmp) - 1)] = 0;
559 buf->buffer += pos;
560 buf->rem_bytes -= pos;
562 sscanf(tmp, "%f", &decimal);
564 TRACE("Found float %s - %f\n", tmp, decimal);
566 *(float*)buf->value = decimal;
568 return TRUE;
571 static BOOL is_integer(parse_buffer* buf)
573 char tmp[512];
574 DWORD pos = 0;
575 char c;
576 DWORD integer;
578 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
580 if (!((c >= '0') && (c <= '9')))
581 return FALSE;
582 if (pos < sizeof(tmp))
583 tmp[pos] = c;
584 pos++;
586 tmp[min(pos, sizeof(tmp) - 1)] = 0;
588 buf->buffer += pos;
589 buf->rem_bytes -= pos;
591 sscanf(tmp, "%d", &integer);
593 TRACE("Found integer %s - %d\n", tmp, integer);
595 *(DWORD*)buf->value = integer;
597 return TRUE;
600 static BOOL is_string(parse_buffer* buf)
602 char tmp[512];
603 DWORD pos = 0;
604 char c;
605 BOOL ok = FALSE;
607 if (*buf->buffer != '"')
608 return FALSE;
610 while ((pos+1) < buf->rem_bytes)
612 c = *(buf->buffer+pos+1);
613 if (c == '"')
615 ok = TRUE;
616 break;
618 if (pos < sizeof(tmp))
619 tmp[pos] = c;
620 pos++;
622 tmp[min(pos, sizeof(tmp) - 1)] = 0;
624 if (!ok)
626 TRACE("Wrong string %s\n", tmp);
627 return FALSE;
630 buf->buffer += pos + 2;
631 buf->rem_bytes -= pos + 2;
633 TRACE("Found string %s\n", tmp);
634 strcpy((char*)buf->value, tmp);
636 return TRUE;
639 static WORD parse_TOKEN(parse_buffer * buf)
641 WORD token;
643 if (buf->txt)
645 while(1)
647 char c;
648 if (!read_bytes(buf, &c, 1))
649 return TOKEN_NONE;
650 if ((c == '#') || (c == '/'))
652 /* Handle comment (# or //) */
653 if (c == '/')
655 if (!read_bytes(buf, &c, 1))
656 return TOKEN_ERROR;
657 if (c != '/')
658 return TOKEN_ERROR;
660 c = 0;
661 while (c != 0x0A)
663 if (!read_bytes(buf, &c, 1))
664 return TOKEN_NONE;
666 continue;
668 if (is_space(c))
669 continue;
670 if (is_operator(c) && (c != '<'))
672 token = get_operator_token(c);
673 break;
675 else if (c == '.')
677 token = TOKEN_DOT;
678 break;
680 else
682 rewind_bytes(buf, 1);
684 if ((token = get_keyword_token(buf)))
685 break;
687 if (is_guid(buf))
689 token = TOKEN_GUID;
690 break;
692 if (is_integer(buf))
694 token = TOKEN_INTEGER;
695 break;
697 if (is_float(buf))
699 token = TOKEN_FLOAT;
700 break;
702 if (is_string(buf))
704 token = TOKEN_LPSTR;
705 break;
707 if (is_name(buf))
709 token = TOKEN_NAME;
710 break;
713 FIXME("Unrecognize element\n");
714 return TOKEN_ERROR;
718 else
720 if (!buf->list_nb_elements)
722 if (!read_bytes(buf, &token, 2))
723 return TOKEN_NONE;
725 /* Convert integer and float list into separate elements */
726 if (token == TOKEN_INTEGER_LIST)
728 if (!read_bytes(buf, &buf->list_nb_elements, 4))
729 return TOKEN_ERROR;
730 token = TOKEN_INTEGER;
731 buf->list_type_float = FALSE;
732 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", buf->list_nb_elements);
734 else if (token == TOKEN_FLOAT_LIST)
736 if (!read_bytes(buf, &buf->list_nb_elements, 4))
737 return TOKEN_ERROR;
738 token = TOKEN_FLOAT;
739 buf->list_type_float = TRUE;
740 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", buf->list_nb_elements);
744 if (buf->list_nb_elements)
746 if (buf->list_separator)
748 buf->list_nb_elements--;
749 buf->list_separator = FALSE;
750 /* Insert separator between each value, and since list does not accept separator at the end
751 use a comma so any extra separator will generate an error */
752 token = TOKEN_COMMA;
754 else
756 DWORD value;
758 if (!read_bytes(buf, &value, 4))
759 return TOKEN_ERROR;
760 *(DWORD*)buf->value = value;
762 buf->list_separator = TRUE;
763 /* Convert list into a series of their basic type counterpart */
764 token = buf->list_type_float ? TOKEN_FLOAT : TOKEN_INTEGER;
766 dump_TOKEN(token);
767 return token;
770 switch (token)
772 case TOKEN_NAME:
774 DWORD count;
775 char *name = (char*)buf->value;
777 if (!read_bytes(buf, &count, 4))
778 return TOKEN_ERROR;
779 if (!read_bytes(buf, name, count))
780 return TOKEN_ERROR;
781 name[count] = 0;
782 TRACE("name = %s\n", name);
784 break;
785 case TOKEN_INTEGER:
787 DWORD integer;
789 if (!read_bytes(buf, &integer, 4))
790 return TOKEN_ERROR;
791 TRACE("integer = %u\n", integer);
793 *(DWORD*)buf->value = integer;
795 break;
796 case TOKEN_GUID:
798 char strguid[39];
799 GUID class_id;
801 if (!read_bytes(buf, &class_id, 16))
802 return TOKEN_ERROR;
803 sprintf(strguid, CLSIDFMT, class_id.Data1, class_id.Data2, class_id.Data3, class_id.Data4[0],
804 class_id.Data4[1], class_id.Data4[2], class_id.Data4[3], class_id.Data4[4], class_id.Data4[5],
805 class_id.Data4[6], class_id.Data4[7]);
806 TRACE("guid = %s\n", strguid);
808 *(GUID*)buf->value = class_id;
810 break;
811 case TOKEN_STRING:
813 DWORD count;
814 char *string = (char*)buf->value;
816 if (!read_bytes(buf, &count, 4))
817 return TOKEN_ERROR;
818 if (!read_bytes(buf, string, count))
819 return TOKEN_ERROR;
820 string[count] = 0;
821 TRACE("string = %s\n", string);
823 token = TOKEN_LPSTR;
825 break;
826 case TOKEN_OBRACE:
827 case TOKEN_CBRACE:
828 case TOKEN_OPAREN:
829 case TOKEN_CPAREN:
830 case TOKEN_OBRACKET:
831 case TOKEN_CBRACKET:
832 case TOKEN_OANGLE:
833 case TOKEN_CANGLE:
834 case TOKEN_DOT:
835 case TOKEN_COMMA:
836 case TOKEN_SEMICOLON:
837 case TOKEN_TEMPLATE:
838 case TOKEN_WORD:
839 case TOKEN_DWORD:
840 case TOKEN_FLOAT:
841 case TOKEN_DOUBLE:
842 case TOKEN_CHAR:
843 case TOKEN_UCHAR:
844 case TOKEN_SWORD:
845 case TOKEN_SDWORD:
846 case TOKEN_VOID:
847 case TOKEN_LPSTR:
848 case TOKEN_UNICODE:
849 case TOKEN_CSTRING:
850 case TOKEN_ARRAY:
851 break;
852 default:
853 return TOKEN_ERROR;
857 dump_TOKEN(token);
859 return token;
862 static WORD get_TOKEN(parse_buffer * buf)
864 if (buf->token_present)
866 buf->token_present = FALSE;
867 return buf->current_token;
870 buf->current_token = parse_TOKEN(buf);
872 return buf->current_token;
875 static WORD check_TOKEN(parse_buffer * buf)
877 if (buf->token_present)
878 return buf->current_token;
880 buf->current_token = parse_TOKEN(buf);
881 buf->token_present = TRUE;
883 return buf->current_token;
886 static inline BOOL is_primitive_type(WORD token)
888 BOOL ret;
889 switch(token)
891 case TOKEN_WORD:
892 case TOKEN_DWORD:
893 case TOKEN_FLOAT:
894 case TOKEN_DOUBLE:
895 case TOKEN_CHAR:
896 case TOKEN_UCHAR:
897 case TOKEN_SWORD:
898 case TOKEN_SDWORD:
899 case TOKEN_LPSTR:
900 case TOKEN_UNICODE:
901 case TOKEN_CSTRING:
902 ret = TRUE;
903 break;
904 default:
905 ret = FALSE;
906 break;
908 return ret;
911 static BOOL parse_template_option_info(parse_buffer * buf)
913 xtemplate* cur_template = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates];
915 if (check_TOKEN(buf) == TOKEN_DOT)
917 get_TOKEN(buf);
918 if (get_TOKEN(buf) != TOKEN_DOT)
919 return FALSE;
920 if (get_TOKEN(buf) != TOKEN_DOT)
921 return FALSE;
922 cur_template->open = TRUE;
924 else
926 while (1)
928 if (get_TOKEN(buf) != TOKEN_NAME)
929 return FALSE;
930 strcpy(cur_template->children[cur_template->nb_children], (char*)buf->value);
931 if (check_TOKEN(buf) == TOKEN_GUID)
932 get_TOKEN(buf);
933 cur_template->nb_children++;
934 if (check_TOKEN(buf) != TOKEN_COMMA)
935 break;
936 get_TOKEN(buf);
938 cur_template->open = FALSE;
941 return TRUE;
944 static BOOL parse_template_members_list(parse_buffer * buf)
946 int idx_member = 0;
947 member* cur_member;
949 while (1)
951 BOOL array = FALSE;
952 int nb_dims = 0;
953 cur_member = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[idx_member];
955 if (check_TOKEN(buf) == TOKEN_ARRAY)
957 get_TOKEN(buf);
958 array = TRUE;
961 if (check_TOKEN(buf) == TOKEN_NAME)
963 cur_member->type = get_TOKEN(buf);
964 if (!strcmp((char*)buf->value, "indexColor"))
966 /* Case sensitive legacy type indexColor is described in the first template */
967 cur_member->idx_template = 0;
969 else
971 cur_member->idx_template = 1;
972 while (cur_member->idx_template < buf->pdxf->nb_xtemplates)
974 if (!stricmp((char*)buf->value, buf->pdxf->xtemplates[cur_member->idx_template].name))
975 break;
976 cur_member->idx_template++;
978 if (cur_member->idx_template == buf->pdxf->nb_xtemplates)
980 WARN("Reference to a nonexistent template '%s'\n", (char*)buf->value);
981 return FALSE;
985 else if (is_primitive_type(check_TOKEN(buf)))
986 cur_member->type = get_TOKEN(buf);
987 else
988 break;
990 if (get_TOKEN(buf) != TOKEN_NAME)
991 return FALSE;
992 strcpy(cur_member->name, (char*)buf->value);
994 if (array)
996 while (check_TOKEN(buf) == TOKEN_OBRACKET)
998 if (nb_dims >= MAX_ARRAY_DIM)
1000 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims + 1);
1001 return FALSE;
1003 get_TOKEN(buf);
1004 if (check_TOKEN(buf) == TOKEN_INTEGER)
1006 get_TOKEN(buf);
1007 cur_member->dim_fixed[nb_dims] = TRUE;
1008 cur_member->dim_value[nb_dims] = *(DWORD*)buf->value;
1010 else
1012 int i;
1013 if (get_TOKEN(buf) != TOKEN_NAME)
1014 return FALSE;
1015 for (i = 0; i < idx_member; i++)
1017 if (!strcmp((char*)buf->value, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].name))
1019 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].nb_dims)
1021 ERR("Array cannot be used to specify variable array size\n");
1022 return FALSE;
1024 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].type != TOKEN_DWORD)
1026 FIXME("Only DWORD supported to specify variable array size\n");
1027 return FALSE;
1029 break;
1032 if (i == idx_member)
1034 ERR("Reference to unknown member %s\n", (char*)buf->value);
1035 return FALSE;
1037 cur_member->dim_fixed[nb_dims] = FALSE;
1038 cur_member->dim_value[nb_dims] = i;
1040 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1041 return FALSE;
1042 nb_dims++;
1044 if (!nb_dims)
1045 return FALSE;
1046 cur_member->nb_dims = nb_dims;
1048 if (get_TOKEN(buf) != TOKEN_SEMICOLON)
1049 return FALSE;
1051 idx_member++;
1054 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].nb_members = idx_member;
1056 return TRUE;
1059 static BOOL parse_template_parts(parse_buffer * buf)
1061 if (!parse_template_members_list(buf))
1062 return FALSE;
1063 if (check_TOKEN(buf) == TOKEN_OBRACKET)
1065 get_TOKEN(buf);
1066 if (!parse_template_option_info(buf))
1067 return FALSE;
1068 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1069 return FALSE;
1072 return TRUE;
1075 static BOOL parse_template(parse_buffer * buf)
1077 if (get_TOKEN(buf) != TOKEN_TEMPLATE)
1078 return FALSE;
1079 if (get_TOKEN(buf) != TOKEN_NAME)
1080 return FALSE;
1081 strcpy(buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, (char*)buf->value);
1082 if (get_TOKEN(buf) != TOKEN_OBRACE)
1083 return FALSE;
1084 if (get_TOKEN(buf) != TOKEN_GUID)
1085 return FALSE;
1086 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id = *(GUID*)buf->value;
1087 if (!parse_template_parts(buf))
1088 return FALSE;
1089 if (get_TOKEN(buf) != TOKEN_CBRACE)
1090 return FALSE;
1092 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));
1093 buf->pdxf->nb_xtemplates++;
1095 return TRUE;
1098 BOOL parse_templates(parse_buffer * buf, BOOL templates_only)
1100 while (check_TOKEN(buf) != TOKEN_NONE)
1102 if (templates_only && (check_TOKEN(buf) != TOKEN_TEMPLATE))
1103 return TRUE;
1104 if (!parse_template(buf))
1106 WARN("Template is not correct\n");
1107 return FALSE;
1109 else
1111 TRACE("Template successfully parsed:\n");
1112 if (TRACE_ON(d3dxof_parsing))
1113 dump_template(buf->pdxf->xtemplates, &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates - 1]);
1116 return TRUE;
1119 static BOOL check_buffer(parse_buffer * buf, ULONG size)
1121 if ((buf->cur_pos_data + size) > buf->capacity)
1123 LPBYTE pdata;
1124 ULONG new_capacity = buf->capacity ? 2 * buf->capacity : 100000;
1126 pdata = HeapAlloc(GetProcessHeap(), 0, new_capacity);
1127 if (!pdata)
1128 return FALSE;
1129 memcpy(pdata, buf->pdata, buf->cur_pos_data);
1130 HeapFree(GetProcessHeap(), 0, buf->pdata);
1131 buf->capacity = new_capacity;
1132 buf->pdata = pdata;
1133 buf->pxo->root->pdata = pdata;
1135 return TRUE;
1138 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional);
1139 static BOOL parse_object_members_list(parse_buffer * buf)
1141 DWORD token;
1142 ULONG i;
1143 xtemplate* pt = buf->pxt[buf->level];
1145 buf->pxo->nb_members = pt->nb_members;
1147 for (i = 0; i < pt->nb_members; i++)
1149 ULONG k;
1150 ULONG nb_elems = 1;
1151 BOOL basic_type = TRUE;
1153 buf->pxo->members[i].name = pt->members[i].name;
1154 buf->pxo->members[i].start = buf->cur_pos_data;
1156 for (k = 0; k < pt->members[i].nb_dims; k++)
1158 if (pt->members[i].dim_fixed[k])
1159 nb_elems *= pt->members[i].dim_value[k];
1160 else
1161 nb_elems *= *(DWORD*)(buf->pxo->root->pdata + buf->pxo->members[pt->members[i].dim_value[k]].start);
1164 TRACE("Elements to consider: %u\n", nb_elems);
1166 for (k = 0; k < nb_elems; k++)
1168 if (pt->members[i].type == TOKEN_NAME)
1170 ULONG j;
1172 TRACE("Found sub-object %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1173 basic_type = FALSE;
1174 buf->level++;
1175 /* To do template lookup */
1176 for (j = 0; j < buf->pdxf->nb_xtemplates; j++)
1178 if (!stricmp(buf->pdxf->xtemplates[pt->members[i].idx_template].name, buf->pdxf->xtemplates[j].name))
1180 buf->pxt[buf->level] = &buf->pdxf->xtemplates[j];
1181 break;
1184 if (j == buf->pdxf->nb_xtemplates)
1186 ERR("Unknown template %s\n", (char*)buf->value);
1187 buf->level--;
1188 return FALSE;
1190 TRACE("Enter %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1191 if (!parse_object_parts(buf, FALSE))
1193 buf->level--;
1194 return FALSE;
1196 buf->level--;
1198 else
1200 token = check_TOKEN(buf);
1201 if (token == TOKEN_INTEGER)
1203 get_TOKEN(buf);
1204 TRACE("%s = %d\n", pt->members[i].name, *(DWORD*)buf->value);
1205 /* Assume larger size */
1206 if (!check_buffer(buf, 4))
1207 return FALSE;
1208 if (pt->members[i].type == TOKEN_WORD)
1210 *(WORD *)(buf->pdata + buf->cur_pos_data) = *(DWORD *)buf->value;
1211 buf->cur_pos_data += 2;
1213 else if (pt->members[i].type == TOKEN_DWORD)
1215 *(DWORD *)(buf->pdata + buf->cur_pos_data) = *(DWORD *)buf->value;
1216 buf->cur_pos_data += 4;
1218 else
1220 FIXME("Token %d not supported\n", pt->members[i].type);
1221 return FALSE;
1224 else if (token == TOKEN_FLOAT)
1226 get_TOKEN(buf);
1227 TRACE("%s = %f\n", pt->members[i].name, *(float*)buf->value);
1228 if (!check_buffer(buf, 4))
1229 return FALSE;
1230 if (pt->members[i].type == TOKEN_FLOAT)
1232 *(float *)(buf->pdata + buf->cur_pos_data) = *(float *)buf->value;
1233 buf->cur_pos_data += 4;
1235 else
1237 FIXME("Token %d not supported\n", pt->members[i].type);
1238 return FALSE;
1241 else if (token == TOKEN_LPSTR)
1243 get_TOKEN(buf);
1244 TRACE("%s = %s\n", pt->members[i].name, (char*)buf->value);
1245 if (!check_buffer(buf, sizeof(LPSTR)))
1246 return FALSE;
1247 if (pt->members[i].type == TOKEN_LPSTR)
1249 int len = strlen((char*)buf->value) + 1;
1250 if ((buf->cur_pstrings - buf->pstrings + len) > MAX_STRINGS_BUFFER)
1252 FIXME("Buffer too small %p %p %d\n", buf->cur_pstrings, buf->pstrings, len);
1253 return FALSE;
1255 strcpy((char*)buf->cur_pstrings, (char*)buf->value);
1256 *(((LPCSTR*)(buf->pdata + buf->cur_pos_data))) = (char*)buf->cur_pstrings;
1257 buf->cur_pstrings += len;
1258 buf->cur_pos_data += sizeof(LPSTR);
1260 else
1262 FIXME("Token %d not supported\n", pt->members[i].type);
1263 return FALSE;
1266 else
1268 WARN("Unexpected token %d\n", token);
1269 return FALSE;
1273 if (basic_type)
1275 /* Handle separator only for basic types */
1276 token = check_TOKEN(buf);
1277 if ((token != TOKEN_COMMA) && (token != TOKEN_SEMICOLON))
1278 return FALSE;
1279 /* Allow multi-semicolons + single comma separator */
1280 while (check_TOKEN(buf) == TOKEN_SEMICOLON)
1281 get_TOKEN(buf);
1282 if (check_TOKEN(buf) == TOKEN_COMMA)
1283 get_TOKEN(buf);
1287 buf->pxo->members[i].size = buf->cur_pos_data - buf->pxo->members[i].start;
1290 return TRUE;
1293 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional)
1295 buf->pxo->nb_children = 0;
1297 if (!parse_object_members_list(buf))
1298 return FALSE;
1300 if (allow_optional)
1302 buf->pxo->size = buf->cur_pos_data - buf->pxo->pos_data;
1304 while (1)
1306 if (check_TOKEN(buf) == TOKEN_OBRACE)
1308 ULONG i, j;
1309 get_TOKEN(buf);
1310 if (get_TOKEN(buf) != TOKEN_NAME)
1311 return FALSE;
1312 if (get_TOKEN(buf) != TOKEN_CBRACE)
1313 return FALSE;
1314 TRACE("Found optional reference %s\n", (char*)buf->value);
1315 for (i = 0; i < (buf->nb_pxo_globals+1); i++)
1317 for (j = 0; j < (buf->pxo_globals[i])[0].nb_subobjects; j++)
1319 if (!strcmp((buf->pxo_globals[i])[j].name, (char*)buf->value))
1320 goto _exit;
1323 _exit:
1324 if (i == (buf->nb_pxo_globals+1))
1326 ERR("Reference to unknown object %s\n", (char*)buf->value);
1327 return FALSE;
1330 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1332 FIXME("Too many sub-objects\n");
1333 return FALSE;
1336 buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1337 buf->pxo->children[buf->pxo->nb_children]->ptarget = &(buf->pxo_globals[i])[j];
1338 buf->pxo->children[buf->pxo->nb_children]->binary = FALSE;
1339 buf->pxo->nb_children++;
1341 else if (check_TOKEN(buf) == TOKEN_NAME)
1343 xobject* pxo = buf->pxo;
1345 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1347 FIXME("Too many sub-objects\n");
1348 return FALSE;
1351 buf->pxo = buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects];
1352 pxo->root->nb_subobjects++;
1354 TRACE("Enter optional %s\n", (char*)buf->value);
1355 buf->level++;
1356 if (!parse_object(buf))
1358 buf->level--;
1359 return FALSE;
1361 buf->level--;
1362 buf->pxo = pxo;
1363 buf->pxo->nb_children++;
1365 else
1366 break;
1370 if (buf->pxo->nb_children > MAX_CHILDREN)
1372 FIXME("Too many children %d\n", buf->pxo->nb_children);
1373 return FALSE;
1376 return TRUE;
1379 BOOL parse_object(parse_buffer * buf)
1381 ULONG i;
1383 buf->pxo->pos_data = buf->cur_pos_data;
1384 buf->pxo->ptarget = NULL;
1385 buf->pxo->binary = FALSE;
1386 buf->pxo->root = buf->pxo_tab;
1388 if (get_TOKEN(buf) != TOKEN_NAME)
1389 return FALSE;
1391 /* To do template lookup */
1392 for (i = 0; i < buf->pdxf->nb_xtemplates; i++)
1394 if (!stricmp((char*)buf->value, buf->pdxf->xtemplates[i].name))
1396 buf->pxt[buf->level] = &buf->pdxf->xtemplates[i];
1397 memcpy(&buf->pxo->type, &buf->pdxf->xtemplates[i].class_id, 16);
1398 break;
1401 if (i == buf->pdxf->nb_xtemplates)
1403 ERR("Unknown template %s\n", (char*)buf->value);
1404 return FALSE;
1407 if (check_TOKEN(buf) == TOKEN_NAME)
1409 get_TOKEN(buf);
1410 strcpy(buf->pxo->name, (char*)buf->value);
1412 else
1413 buf->pxo->name[0] = 0;
1415 if (get_TOKEN(buf) != TOKEN_OBRACE)
1416 return FALSE;
1417 if (check_TOKEN(buf) == TOKEN_GUID)
1419 get_TOKEN(buf);
1420 memcpy(&buf->pxo->class_id, buf->value, 16);
1422 else
1423 memset(&buf->pxo->class_id, 0, 16);
1425 if (!parse_object_parts(buf, TRUE))
1426 return FALSE;
1427 if (get_TOKEN(buf) != TOKEN_CBRACE)
1428 return FALSE;
1430 /* For seeking to a possibly eof to avoid parsing another object next time */
1431 check_TOKEN(buf);
1433 return TRUE;