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
22 #include "wine/debug.h"
29 #include "d3dxof_private.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')
49 #define TOKEN_STRING 2
50 #define TOKEN_INTEGER 3
52 #define TOKEN_INTEGER_LIST 6
53 #define TOKEN_FLOAT_LIST 7
54 #define TOKEN_OBRACE 10
55 #define TOKEN_CBRACE 11
56 #define TOKEN_OPAREN 12
57 #define TOKEN_CPAREN 13
58 #define TOKEN_OBRACKET 14
59 #define TOKEN_CBRACKET 15
60 #define TOKEN_OANGLE 16
61 #define TOKEN_CANGLE 17
63 #define TOKEN_COMMA 19
64 #define TOKEN_SEMICOLON 20
65 #define TOKEN_TEMPLATE 31
67 #define TOKEN_DWORD 41
68 #define TOKEN_FLOAT 42
69 #define TOKEN_DOUBLE 43
71 #define TOKEN_UCHAR 45
72 #define TOKEN_SWORD 46
73 #define TOKEN_SDWORD 47
75 #define TOKEN_LPSTR 49
76 #define TOKEN_UNICODE 50
77 #define TOKEN_CSTRING 51
78 #define TOKEN_ARRAY 52
80 #define CLSIDFMT "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>"
82 /* FOURCC to string conversion for debug messages */
83 static const char *debugstr_fourcc(DWORD fourcc)
85 if (!fourcc) return "'null'";
86 return wine_dbg_sprintf ("\'%c%c%c%c\'",
87 (char)(fourcc), (char)(fourcc >> 8),
88 (char)(fourcc >> 16), (char)(fourcc >> 24));
91 static const char* get_primitive_string(WORD token)
125 static void dump_template(xtemplate* templates_array, xtemplate* ptemplate)
130 clsid = &ptemplate->class_id;
132 DPRINTF("template %s\n", ptemplate->name);
134 DPRINTF(CLSIDFMT "\n", clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
135 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
136 for (j = 0; j < ptemplate->nb_members; j++)
138 if (ptemplate->members[j].nb_dims)
140 if (ptemplate->members[j].type == TOKEN_NAME)
141 DPRINTF("%s ", templates_array[ptemplate->members[j].idx_template].name);
143 DPRINTF("%s ", get_primitive_string(ptemplate->members[j].type));
144 DPRINTF("%s", ptemplate->members[j].name);
145 for (k = 0; k < ptemplate->members[j].nb_dims; k++)
147 if (ptemplate->members[j].dim_fixed[k])
148 DPRINTF("[%d]", ptemplate->members[j].dim_value[k]);
150 DPRINTF("[%s]", ptemplate->members[ptemplate->members[j].dim_value[k]].name);
156 else if (ptemplate->nb_children)
158 DPRINTF("[%s", ptemplate->children[0]);
159 for (j = 1; j < ptemplate->nb_children; j++)
160 DPRINTF(",%s", ptemplate->children[j]);
166 static BOOL read_bytes(parse_buffer * buf, LPVOID data, DWORD size)
168 if (buf->rem_bytes < size)
170 memcpy(data, buf->buffer, size);
172 buf->rem_bytes -= size;
176 static void rewind_bytes(parse_buffer * buf, DWORD size)
179 buf->rem_bytes += size;
182 HRESULT parse_header(parse_buffer * buf, BYTE ** decomp_buffer_ptr)
184 /* X File common header:
185 * 0-3 -> Magic Number (format identifier)
186 * 4-7 -> Format Version
187 * 8-11 -> Format Type (text or binary, decompressed or compressed)
188 * 12-15 -> Float Size (32 or 64 bits) */
191 if (!read_bytes(buf, header, sizeof(header)))
192 return DXFILEERR_BADFILETYPE;
194 if (TRACE_ON(d3dxof_parsing))
197 memcpy(string, header, 16);
199 TRACE("header = '%s'\n", string);
202 if (header[0] != XOFFILE_FORMAT_MAGIC)
203 return DXFILEERR_BADFILETYPE;
205 if (header[1] != XOFFILE_FORMAT_VERSION_302 && header[1] != XOFFILE_FORMAT_VERSION_303)
206 return DXFILEERR_BADFILEVERSION;
208 if (header[2] != XOFFILE_FORMAT_BINARY && header[2] != XOFFILE_FORMAT_TEXT &&
209 header[2] != XOFFILE_FORMAT_BINARY_MSZIP && header[2] != XOFFILE_FORMAT_TEXT_MSZIP)
211 WARN("File type %s unknown\n", debugstr_fourcc(header[2]));
212 return DXFILEERR_BADFILETYPE;
215 if (header[3] != XOFFILE_FORMAT_FLOAT_BITS_32 && header[3] != XOFFILE_FORMAT_FLOAT_BITS_64)
216 return DXFILEERR_BADFILEFLOATSIZE;
218 buf->txt = header[2] == XOFFILE_FORMAT_TEXT || header[2] == XOFFILE_FORMAT_TEXT_MSZIP;
220 if (header[2] == XOFFILE_FORMAT_BINARY_MSZIP || header[2] == XOFFILE_FORMAT_TEXT_MSZIP)
222 /* Extended header for compressed data:
223 * 16-19 -> size of decompressed file including xof header,
224 * 20-21 -> size of first decompressed MSZIP chunk, 22-23 -> size of first compressed MSZIP chunk
225 * 24-xx -> compressed MSZIP data chunk
226 * xx-xx -> size of next decompressed MSZIP chunk, xx-xx -> size of next compressed MSZIP chunk
227 * xx-xx -> compressed MSZIP data chunk
228 * .............................................................................................. */
230 DWORD decomp_file_size;
231 WORD decomp_chunk_size;
232 WORD comp_chunk_size;
233 LPBYTE decomp_buffer;
235 if (!read_bytes(buf, &decomp_file_size, sizeof(decomp_file_size)))
236 return DXFILEERR_BADFILETYPE;
238 TRACE("Compressed format %s detected: decompressed file size with xof header = %d\n",
239 debugstr_fourcc(header[2]), decomp_file_size);
241 /* Does not take xof header into account */
242 decomp_file_size -= 16;
244 decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_file_size);
247 ERR("Out of memory\n");
248 return DXFILEERR_BADALLOC;
250 *decomp_buffer_ptr = decomp_buffer;
252 while (buf->rem_bytes)
254 if (!read_bytes(buf, &decomp_chunk_size, sizeof(decomp_chunk_size)))
255 return DXFILEERR_BADFILETYPE;
256 if (!read_bytes(buf, &comp_chunk_size, sizeof(comp_chunk_size)))
257 return DXFILEERR_BADFILETYPE;
259 TRACE("Process chunk: compressed_size = %d, decompressed_size = %d\n",
260 comp_chunk_size, decomp_chunk_size);
262 err = mszip_decompress(comp_chunk_size, decomp_chunk_size, (char*)buf->buffer, (char*)decomp_buffer);
265 WARN("Error while decompressing MSZIP chunk %d\n", err);
266 HeapFree(GetProcessHeap(), 0, decomp_buffer);
267 return DXFILEERR_BADALLOC;
269 buf->rem_bytes -= comp_chunk_size;
270 buf->buffer += comp_chunk_size;
271 decomp_buffer += decomp_chunk_size;
274 if ((decomp_buffer - *decomp_buffer_ptr) != decomp_file_size)
275 ERR("Size of all decompressed chunks (%u) does not match decompressed file size (%u)\n",
276 (DWORD)(decomp_buffer - *decomp_buffer_ptr), decomp_file_size);
278 /* Use decompressed data */
279 buf->buffer = *decomp_buffer_ptr;
280 buf->rem_bytes = decomp_file_size;
283 TRACE("Header is correct\n");
288 static void dump_TOKEN(WORD token)
290 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
293 DUMP_TOKEN(TOKEN_NAME);
294 DUMP_TOKEN(TOKEN_STRING);
295 DUMP_TOKEN(TOKEN_INTEGER);
296 DUMP_TOKEN(TOKEN_GUID);
297 DUMP_TOKEN(TOKEN_INTEGER_LIST);
298 DUMP_TOKEN(TOKEN_FLOAT_LIST);
299 DUMP_TOKEN(TOKEN_OBRACE);
300 DUMP_TOKEN(TOKEN_CBRACE);
301 DUMP_TOKEN(TOKEN_OPAREN);
302 DUMP_TOKEN(TOKEN_CPAREN);
303 DUMP_TOKEN(TOKEN_OBRACKET);
304 DUMP_TOKEN(TOKEN_CBRACKET);
305 DUMP_TOKEN(TOKEN_OANGLE);
306 DUMP_TOKEN(TOKEN_CANGLE);
307 DUMP_TOKEN(TOKEN_DOT);
308 DUMP_TOKEN(TOKEN_COMMA);
309 DUMP_TOKEN(TOKEN_SEMICOLON);
310 DUMP_TOKEN(TOKEN_TEMPLATE);
311 DUMP_TOKEN(TOKEN_WORD);
312 DUMP_TOKEN(TOKEN_DWORD);
313 DUMP_TOKEN(TOKEN_FLOAT);
314 DUMP_TOKEN(TOKEN_DOUBLE);
315 DUMP_TOKEN(TOKEN_CHAR);
316 DUMP_TOKEN(TOKEN_UCHAR);
317 DUMP_TOKEN(TOKEN_SWORD);
318 DUMP_TOKEN(TOKEN_SDWORD);
319 DUMP_TOKEN(TOKEN_VOID);
320 DUMP_TOKEN(TOKEN_LPSTR);
321 DUMP_TOKEN(TOKEN_UNICODE);
322 DUMP_TOKEN(TOKEN_CSTRING);
323 DUMP_TOKEN(TOKEN_ARRAY);
326 TRACE("Unknown token %d\n", token);
332 static BOOL is_space(char c)
346 static BOOL is_operator(char c)
365 static inline BOOL is_separator(char c)
367 return is_space(c) || is_operator(c);
370 static WORD get_operator_token(char c)
379 return TOKEN_OBRACKET;
381 return TOKEN_CBRACKET;
393 return TOKEN_SEMICOLON;
398 static BOOL is_keyword(parse_buffer* buf, const char* keyword)
400 char tmp[8]; /* longest keyword size (template) */
401 DWORD len = strlen(keyword);
403 if (!read_bytes(buf, tmp, len))
405 if (strncasecmp(tmp, keyword, len))
407 rewind_bytes(buf, len);
411 if (!read_bytes(buf, tmp, 1))
413 if (is_separator(tmp[0]))
415 rewind_bytes(buf, 1);
418 rewind_bytes(buf, len+1);
422 static WORD get_keyword_token(parse_buffer* buf)
424 if (is_keyword(buf, "template"))
425 return TOKEN_TEMPLATE;
426 if (is_keyword(buf, "WORD"))
428 if (is_keyword(buf, "DWORD"))
430 if (is_keyword(buf, "FLOAT"))
432 if (is_keyword(buf, "DOUBLE"))
434 if (is_keyword(buf, "CHAR"))
436 if (is_keyword(buf, "UCHAR"))
438 if (is_keyword(buf, "SWORD"))
440 if (is_keyword(buf, "SDWORD"))
442 if (is_keyword(buf, "VOID"))
444 if (is_keyword(buf, "STRING"))
446 if (is_keyword(buf, "UNICODE"))
447 return TOKEN_UNICODE;
448 if (is_keyword(buf, "CSTRING"))
449 return TOKEN_CSTRING;
450 if (is_keyword(buf, "array"))
456 static BOOL is_guid(parse_buffer* buf)
464 if (buf->rem_bytes < 38 || *buf->buffer != '<')
467 while (pos < sizeof(tmp) - 2 && *(buf->buffer+pos) != '>')
469 tmp[pos] = *(buf->buffer+pos);
474 if (pos != 38 /* <+36+> */)
476 TRACE("Wrong guid %s (%d)\n", tmp, pos);
480 buf->rem_bytes -= pos;
482 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 TRACE("Wrong guid %s (%d)\n", tmp, pos);
488 TRACE("Found guid %s (%d)\n", tmp, pos);
490 class_id.Data2 = tab[0];
491 class_id.Data3 = tab[1];
492 class_id.Data4[0] = tab[2];
493 class_id.Data4[1] = tab[3];
494 class_id.Data4[2] = tab[4];
495 class_id.Data4[3] = tab[5];
496 class_id.Data4[4] = tab[6];
497 class_id.Data4[5] = tab[7];
498 class_id.Data4[6] = tab[8];
499 class_id.Data4[7] = tab[9];
501 *(GUID*)buf->value = class_id;
506 static BOOL is_name(parse_buffer* buf)
512 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
514 if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_') || (c == '-')))
516 if (pos < sizeof(tmp))
520 tmp[min(pos, sizeof(tmp) - 1)] = 0;
524 TRACE("Wrong name %s\n", tmp);
529 buf->rem_bytes -= pos;
531 TRACE("Found name %s\n", tmp);
532 strcpy((char*)buf->value, tmp);
537 static BOOL is_float(parse_buffer* buf)
545 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
547 if (!((!pos && (c == '-')) || ((c >= '0') && (c <= '9')) || (!dot && (c == '.'))))
551 if (pos < sizeof(tmp))
555 tmp[min(pos, sizeof(tmp) - 1)] = 0;
558 buf->rem_bytes -= pos;
560 sscanf(tmp, "%f", &decimal);
562 TRACE("Found float %s - %f\n", tmp, decimal);
564 *(float*)buf->value = decimal;
569 static BOOL is_integer(parse_buffer* buf)
576 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
578 if (!((c >= '0') && (c <= '9')))
580 if (pos < sizeof(tmp))
584 tmp[min(pos, sizeof(tmp) - 1)] = 0;
587 buf->rem_bytes -= pos;
589 sscanf(tmp, "%d", &integer);
591 TRACE("Found integer %s - %d\n", tmp, integer);
593 *(DWORD*)buf->value = integer;
598 static BOOL is_string(parse_buffer* buf)
605 if (*buf->buffer != '"')
608 while ((pos+1) < buf->rem_bytes)
610 c = *(buf->buffer+pos+1);
616 if (pos < sizeof(tmp))
620 tmp[min(pos, sizeof(tmp) - 1)] = 0;
624 TRACE("Wrong string %s\n", tmp);
628 buf->buffer += pos + 2;
629 buf->rem_bytes -= pos + 2;
631 TRACE("Found string %s\n", tmp);
632 strcpy((char*)buf->value, tmp);
637 static WORD parse_TOKEN(parse_buffer * buf)
646 if (!read_bytes(buf, &c, 1))
648 /*TRACE("char = '%c'\n", is_space(c) ? ' ' : c);*/
649 if ((c == '#') || (c == '/'))
651 /* Handle comment (# or //) */
654 if (!read_bytes(buf, &c, 1))
662 if (!read_bytes(buf, &c, 1))
669 if (is_operator(c) && (c != '<'))
671 token = get_operator_token(c);
681 rewind_bytes(buf, 1);
683 if ((token = get_keyword_token(buf)))
693 token = TOKEN_INTEGER;
712 FIXME("Unrecognize element\n");
724 if (!read_bytes(buf, &token, 2))
727 /* Convert integer and float list into separate elements */
728 if (token == TOKEN_INTEGER_LIST)
730 if (!read_bytes(buf, &nb_elem, 4))
732 token = TOKEN_INTEGER;
734 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", nb_elem);
736 else if (token == TOKEN_FLOAT_LIST)
738 if (!read_bytes(buf, &nb_elem, 4))
742 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", nb_elem);
748 token = is_float ? TOKEN_FLOAT : TOKEN_INTEGER;
753 if (!read_bytes(buf, &integer, 4))
756 *(DWORD*)buf->value = integer;
769 if (!read_bytes(buf, &count, 4))
771 if (!read_bytes(buf, strname, count))
774 /*TRACE("name = %s\n", strname);*/
776 strcpy((char*)buf->value, strname);
783 if (!read_bytes(buf, &integer, 4))
785 /*TRACE("integer = %ld\n", integer);*/
787 *(DWORD*)buf->value = integer;
795 if (!read_bytes(buf, &class_id, 16))
797 sprintf(strguid, CLSIDFMT, class_id.Data1, class_id.Data2, class_id.Data3, class_id.Data4[0],
798 class_id.Data4[1], class_id.Data4[2], class_id.Data4[3], class_id.Data4[4], class_id.Data4[5],
799 class_id.Data4[6], class_id.Data4[7]);
800 /*TRACE("guid = {%s}\n", strguid);*/
802 *(GUID*)buf->value = class_id;
810 if (!read_bytes(buf, &count, 4))
812 if (!read_bytes(buf, strname, count))
815 if (!read_bytes(buf, &tmp_token, 2))
817 if ((tmp_token != TOKEN_COMMA) && (tmp_token != TOKEN_SEMICOLON))
818 ERR("No comma or semicolon (got %d)\n", tmp_token);
819 /*TRACE("name = %s\n", strname);*/
821 strcpy((char*)buf->value, strname);
835 case TOKEN_SEMICOLON:
861 static WORD get_TOKEN(parse_buffer * buf)
863 if (buf->token_present)
865 buf->token_present = FALSE;
866 return buf->current_token;
869 buf->current_token = parse_TOKEN(buf);
871 return buf->current_token;
874 static WORD check_TOKEN(parse_buffer * buf)
876 if (buf->token_present)
877 return buf->current_token;
879 buf->current_token = parse_TOKEN(buf);
880 buf->token_present = TRUE;
882 return buf->current_token;
885 static BOOL is_template_available(parse_buffer * buf)
887 return check_TOKEN(buf) == TOKEN_TEMPLATE;
890 static inline BOOL is_primitive_type(WORD token)
915 static BOOL parse_template_option_info(parse_buffer * buf)
917 xtemplate* cur_template = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates];
919 if (check_TOKEN(buf) == TOKEN_DOT)
922 if (get_TOKEN(buf) != TOKEN_DOT)
924 if (get_TOKEN(buf) != TOKEN_DOT)
926 cur_template->open = TRUE;
932 if (get_TOKEN(buf) != TOKEN_NAME)
934 strcpy(cur_template->children[cur_template->nb_children], (char*)buf->value);
935 if (check_TOKEN(buf) == TOKEN_GUID)
937 cur_template->nb_children++;
938 if (check_TOKEN(buf) != TOKEN_COMMA)
942 cur_template->open = FALSE;
948 static BOOL parse_template_members_list(parse_buffer * buf)
957 cur_member = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[idx_member];
959 if (check_TOKEN(buf) == TOKEN_ARRAY)
965 if (check_TOKEN(buf) == TOKEN_NAME)
967 cur_member->type = get_TOKEN(buf);
968 cur_member->idx_template = 0;
969 while (cur_member->idx_template < buf->pdxf->nb_xtemplates)
971 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[cur_member->idx_template].name))
973 cur_member->idx_template++;
975 if (cur_member->idx_template == buf->pdxf->nb_xtemplates)
977 ERR("Reference to a nonexistent template '%s'\n", (char*)buf->value);
981 else if (is_primitive_type(check_TOKEN(buf)))
982 cur_member->type = get_TOKEN(buf);
986 if (get_TOKEN(buf) != TOKEN_NAME)
988 strcpy(cur_member->name, (char*)buf->value);
992 while (check_TOKEN(buf) == TOKEN_OBRACKET)
994 if (nb_dims >= MAX_ARRAY_DIM)
996 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims + 1);
1000 if (check_TOKEN(buf) == TOKEN_INTEGER)
1003 cur_member->dim_fixed[nb_dims] = TRUE;
1004 cur_member->dim_value[nb_dims] = *(DWORD*)buf->value;
1009 if (get_TOKEN(buf) != TOKEN_NAME)
1011 for (i = 0; i < idx_member; i++)
1013 if (!strcmp((char*)buf->value, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].name))
1015 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].nb_dims)
1017 ERR("Array cannot be used to specify variable array size\n");
1020 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].type != TOKEN_DWORD)
1022 FIXME("Only DWORD supported to specify variable array size\n");
1028 if (i == idx_member)
1030 ERR("Reference to unknown member %s\n", (char*)buf->value);
1033 cur_member->dim_fixed[nb_dims] = FALSE;
1034 cur_member->dim_value[nb_dims] = i;
1036 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1042 cur_member->nb_dims = nb_dims;
1044 if (get_TOKEN(buf) != TOKEN_SEMICOLON)
1050 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].nb_members = idx_member;
1055 static BOOL parse_template_parts(parse_buffer * buf)
1057 if (!parse_template_members_list(buf))
1059 if (check_TOKEN(buf) == TOKEN_OBRACKET)
1062 if (!parse_template_option_info(buf))
1064 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1071 static void go_to_next_definition(parse_buffer * buf)
1074 while (buf->rem_bytes)
1076 if (!read_bytes(buf, &c, 1))
1078 if ((c == '#') || (c == '/'))
1080 /* Handle comment (# or //) */
1083 if (!read_bytes(buf, &c, 1))
1091 if (!read_bytes(buf, &c, 1))
1096 else if (!is_space(c))
1098 rewind_bytes(buf, 1);
1104 static BOOL parse_template(parse_buffer * buf)
1106 if (get_TOKEN(buf) != TOKEN_TEMPLATE)
1108 if (get_TOKEN(buf) != TOKEN_NAME)
1110 strcpy(buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, (char*)buf->value);
1111 if (get_TOKEN(buf) != TOKEN_OBRACE)
1113 if (get_TOKEN(buf) != TOKEN_GUID)
1115 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id = *(GUID*)buf->value;
1116 if (!parse_template_parts(buf))
1118 if (get_TOKEN(buf) != TOKEN_CBRACE)
1122 /* Go to the next template */
1123 go_to_next_definition(buf);
1126 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));
1127 buf->pdxf->nb_xtemplates++;
1132 BOOL parse_templates(parse_buffer * buf)
1134 while (buf->rem_bytes && is_template_available(buf))
1136 if (!parse_template(buf))
1138 WARN("Template is not correct\n");
1143 TRACE("Template successfully parsed:\n");
1144 if (TRACE_ON(d3dxof_parsing))
1145 dump_template(buf->pdxf->xtemplates, &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates - 1]);
1151 static BOOL check_buffer(parse_buffer * buf, ULONG size)
1153 if ((buf->cur_pos_data + size) > buf->capacity)
1156 ULONG new_capacity = buf->capacity ? 2 * buf->capacity : 100000;
1158 pdata = HeapAlloc(GetProcessHeap(), 0, new_capacity);
1161 memcpy(pdata, buf->pdata, buf->cur_pos_data);
1162 HeapFree(GetProcessHeap(), 0, buf->pdata);
1163 buf->capacity = new_capacity;
1165 buf->pxo->root->pdata = pdata;
1170 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional);
1171 static BOOL parse_object_members_list(parse_buffer * buf)
1175 xtemplate* pt = buf->pxt[buf->level];
1177 buf->pxo->nb_members = pt->nb_members;
1179 for (i = 0; i < pt->nb_members; i++)
1184 buf->pxo->members[i].name = pt->members[i].name;
1185 buf->pxo->members[i].start = buf->cur_pos_data;
1187 for (k = 0; k < pt->members[i].nb_dims; k++)
1189 if (pt->members[i].dim_fixed[k])
1190 nb_elems *= pt->members[i].dim_value[k];
1192 nb_elems *= *(DWORD*)(buf->pxo->root->pdata + buf->pxo->members[pt->members[i].dim_value[k]].start);
1195 TRACE("Elements to consider: %u\n", nb_elems);
1197 for (k = 0; k < nb_elems; k++)
1201 token = check_TOKEN(buf);
1202 if (token == TOKEN_COMMA)
1208 /* Allow comma omission */
1209 if (!((token == TOKEN_FLOAT) || (token == TOKEN_INTEGER)))
1214 if (pt->members[i].type == TOKEN_NAME)
1218 TRACE("Found sub-object %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1220 /* To do template lookup */
1221 for (j = 0; j < buf->pdxf->nb_xtemplates; j++)
1223 if (!strcasecmp(buf->pdxf->xtemplates[pt->members[i].idx_template].name, buf->pdxf->xtemplates[j].name))
1225 buf->pxt[buf->level] = &buf->pdxf->xtemplates[j];
1229 if (j == buf->pdxf->nb_xtemplates)
1231 ERR("Unknown template %s\n", (char*)buf->value);
1235 TRACE("Enter %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1236 if (!parse_object_parts(buf, FALSE))
1245 token = check_TOKEN(buf);
1246 if (token == TOKEN_INTEGER)
1249 TRACE("%s = %d\n", pt->members[i].name, *(DWORD*)buf->value);
1250 /* Assume larger size */
1251 if (!check_buffer(buf, 4))
1253 if (pt->members[i].type == TOKEN_WORD)
1255 *(((WORD*)(buf->pdata + buf->cur_pos_data))) = (WORD)(*(DWORD*)buf->value);
1256 buf->cur_pos_data += 2;
1258 else if (pt->members[i].type == TOKEN_DWORD)
1260 *(((DWORD*)(buf->pdata + buf->cur_pos_data))) = (DWORD)(*(DWORD*)buf->value);
1261 buf->cur_pos_data += 4;
1265 FIXME("Token %d not supported\n", pt->members[i].type);
1269 else if (token == TOKEN_FLOAT)
1272 TRACE("%s = %f\n", pt->members[i].name, *(float*)buf->value);
1273 if (!check_buffer(buf, 4))
1275 if (pt->members[i].type == TOKEN_FLOAT)
1277 *(((float*)(buf->pdata + buf->cur_pos_data))) = (float)(*(float*)buf->value);
1278 buf->cur_pos_data += 4;
1282 FIXME("Token %d not supported\n", pt->members[i].type);
1286 else if (token == TOKEN_LPSTR)
1289 TRACE("%s = %s\n", pt->members[i].name, (char*)buf->value);
1290 if (!check_buffer(buf, sizeof(LPSTR)))
1292 if (pt->members[i].type == TOKEN_LPSTR)
1294 int len = strlen((char*)buf->value) + 1;
1295 if ((buf->cur_pstrings - buf->pstrings + len) > MAX_STRINGS_BUFFER)
1297 FIXME("Buffer too small %p %p %d\n", buf->cur_pstrings, buf->pstrings, len);
1300 strcpy((char*)buf->cur_pstrings, (char*)buf->value);
1301 *(((LPCSTR*)(buf->pdata + buf->cur_pos_data))) = (char*)buf->cur_pstrings;
1302 buf->cur_pstrings += len;
1303 buf->cur_pos_data += sizeof(LPSTR);
1307 FIXME("Token %d not supported\n", pt->members[i].type);
1313 FIXME("Unexpected token %d\n", token);
1319 /* Empty arrays can have the semicolon at the end or not so remove it if any and skip next check */
1320 if (!nb_elems && (check_TOKEN(buf) == TOKEN_SEMICOLON))
1323 if (nb_elems && buf->txt && (check_TOKEN(buf) != TOKEN_CBRACE) && (check_TOKEN(buf) != TOKEN_NAME))
1325 token = get_TOKEN(buf);
1326 if ((token != TOKEN_SEMICOLON) && (token != TOKEN_COMMA))
1329 buf->pxo->members[i].size = buf->cur_pos_data - buf->pxo->members[i].start;
1335 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional)
1337 buf->pxo->nb_children = 0;
1339 if (!parse_object_members_list(buf))
1344 buf->pxo->size = buf->cur_pos_data - buf->pxo->pos_data;
1346 /* Skip trailing semicolon */
1347 while (check_TOKEN(buf) == TOKEN_SEMICOLON)
1352 if (check_TOKEN(buf) == TOKEN_OBRACE)
1356 if (get_TOKEN(buf) != TOKEN_NAME)
1358 if (get_TOKEN(buf) != TOKEN_CBRACE)
1360 TRACE("Found optional reference %s\n", (char*)buf->value);
1361 for (i = 0; i < (buf->nb_pxo_globals+1); i++)
1363 for (j = 0; j < (buf->pxo_globals[i])[0].nb_subobjects; j++)
1365 if (!strcmp((buf->pxo_globals[i])[j].name, (char*)buf->value))
1370 if (i == (buf->nb_pxo_globals+1))
1372 ERR("Reference to unknown object %s\n", (char*)buf->value);
1376 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1378 FIXME("Too many sub-objects\n");
1382 buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1383 buf->pxo->children[buf->pxo->nb_children]->ptarget = &(buf->pxo_globals[i])[j];
1384 buf->pxo->children[buf->pxo->nb_children]->binary = FALSE;
1385 buf->pxo->nb_children++;
1387 else if (check_TOKEN(buf) == TOKEN_NAME)
1389 xobject* pxo = buf->pxo;
1391 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1393 FIXME("Too many sub-objects\n");
1397 buf->pxo = buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1399 TRACE("Enter optional %s\n", (char*)buf->value);
1401 if (!parse_object(buf))
1408 buf->pxo->nb_children++;
1415 if (buf->pxo->nb_children > MAX_CHILDREN)
1417 FIXME("Too many children %d\n", buf->pxo->nb_children);
1424 BOOL parse_object(parse_buffer * buf)
1428 buf->pxo->pos_data = buf->cur_pos_data;
1429 buf->pxo->ptarget = NULL;
1430 buf->pxo->binary = FALSE;
1431 buf->pxo->root = buf->pxo_tab;
1433 if (get_TOKEN(buf) != TOKEN_NAME)
1436 /* To do template lookup */
1437 for (i = 0; i < buf->pdxf->nb_xtemplates; i++)
1439 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[i].name))
1441 buf->pxt[buf->level] = &buf->pdxf->xtemplates[i];
1442 memcpy(&buf->pxo->type, &buf->pdxf->xtemplates[i].class_id, 16);
1446 if (i == buf->pdxf->nb_xtemplates)
1448 ERR("Unknown template %s\n", (char*)buf->value);
1452 if (check_TOKEN(buf) == TOKEN_NAME)
1455 strcpy(buf->pxo->name, (char*)buf->value);
1458 buf->pxo->name[0] = 0;
1460 if (get_TOKEN(buf) != TOKEN_OBRACE)
1462 if (check_TOKEN(buf) == TOKEN_GUID)
1465 memcpy(&buf->pxo->class_id, buf->value, 16);
1468 memset(&buf->pxo->class_id, 0, 16);
1470 if (!parse_object_parts(buf, TRUE))
1472 if (get_TOKEN(buf) != TOKEN_CBRACE)
1477 /* Go to the next object */
1478 go_to_next_definition(buf);